xref: /llvm-project/compiler-rt/test/asan/TestCases/global-location.cpp (revision 5888a47914f44ffaf102fcb7afd3500706fe753f)
1 // RUN: %clangxx_asan -g -O2 %s -o %t
2 // RUN: not %run %t g 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=GLOB
3 // RUN: not %run %t c 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CLASS_STATIC
4 // RUN: not %run %t f 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=FUNC_STATIC
5 // RUN: not %run %t l 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=LITERAL
6 
7 // COFF doesn't support debuginfo for globals. For the non-debuginfo tests, see global-location-nodebug.cpp.
8 // XFAIL: target={{.*windows-msvc.*}}
9 
10 // FIXME: Investigate failure on MinGW
11 // XFAIL: target={{.*-windows-gnu}}
12 
13 // atos doesn't show source line numbers for global variables.
14 // UNSUPPORTED: darwin
15 
16 // CHECK: AddressSanitizer: global-buffer-overflow
17 
18 #include <string.h>
19 
20 struct C {
21   static int array[10];
22   // CLASS_STATIC:      0x{{.*}} is located 4 bytes after global variable 'C::array' defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 40
23 };
24 
25 int global[10];
26 // GLOB:      0x{{.*}} is located 4 bytes after global variable 'global' defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 40
27 int C::array[10];
28 
main(int argc,char ** argv)29 int main(int argc, char **argv) {
30   int one = argc - 1;
31   switch (argv[1][0]) {
32   case 'g': return global[one * 11];
33   case 'c': return C::array[one * 11];
34   case 'f':
35     static int array[10];
36     // FUNC_STATIC:      0x{{.*}} is located 4 bytes after global variable 'main::array' defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 40
37     memset(array, 0, 10);
38     return array[one * 11];
39   case 'l':
40     const char *str = "0123456789";
41     // LITERAL:      0x{{.*}} is located 0 bytes after global variable {{.*}} defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 11
42     return str[one * 11];
43   }
44   return 0;
45 }
46 
47 // CHECK: SUMMARY: AddressSanitizer: global-buffer-overflow
48