xref: /llvm-project/compiler-rt/test/asan/TestCases/strdup_oob_test.cpp (revision 4afd2860222ee0413213dda05e089d5aa2773c42)
1 // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2 // RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
3 // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
4 // RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
5 
6 // When built as C on Linux, strdup is transformed to __strdup.
7 // RUN: %clangxx_asan -O3 -xc %s -o %t && not %run %t 2>&1 | FileCheck %s
8 
9 // Unwind problem on arm: "main" is missing from the allocation stack trace.
10 // REQUIRES: (arm-target-arch || armhf-target-arch), fast-unwinder-works
11 
12 // FIXME: We fail to intercept strdup with the dynamic WinASan RTL, so it's not
13 // in the stack trace.
14 // XFAIL: win32-dynamic-asan
15 
16 #include <string.h>
17 
18 char kString[] = "foo";
19 
main(int argc,char ** argv)20 int main(int argc, char **argv) {
21   char *copy = strdup(kString);
22   int x = copy[4 + argc];  // BOOM
23   // CHECK: AddressSanitizer: heap-buffer-overflow
24   // CHECK: #0 {{.*}}main {{.*}}strdup_oob_test.cpp:[[@LINE-2]]
25   // CHECK-LABEL: allocated by thread T{{.*}} here:
26   // CHECK: #{{[01]}} {{.*}}strdup
27   // CHECK: #{{.*}}main {{.*}}strdup_oob_test.cpp:[[@LINE-6]]
28   // CHECK-LABEL: SUMMARY
29   // CHECK: strdup_oob_test.cpp:[[@LINE-7]]
30   return x;
31 }
32