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: windows-msvc
9 
10 // atos doesn't show source line numbers for global variables.
11 // UNSUPPORTED: darwin
12 
13 // CHECK: AddressSanitizer: global-buffer-overflow
14 
15 #include <string.h>
16 
17 struct C {
18   static int array[10];
19   // CLASS_STATIC:      0x{{.*}} is located 4 bytes to the right of global variable 'C::array' defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 40
20 };
21 
22 int global[10];
23 // GLOB:      0x{{.*}} is located 4 bytes to the right of global variable 'global' defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 40
24 int C::array[10];
25 
main(int argc,char ** argv)26 int main(int argc, char **argv) {
27   int one = argc - 1;
28   switch (argv[1][0]) {
29   case 'g': return global[one * 11];
30   case 'c': return C::array[one * 11];
31   case 'f':
32     static int array[10];
33     // FUNC_STATIC:      0x{{.*}} is located 4 bytes to the right of global variable 'main::array' defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 40
34     memset(array, 0, 10);
35     return array[one * 11];
36   case 'l':
37     const char *str = "0123456789";
38     // LITERAL:      0x{{.*}} is located 0 bytes to the right of global variable {{.*}} defined in '{{.*}}global-location.cpp:[[@LINE-1]]' {{.*}} of size 11
39     return str[one * 11];
40   }
41   return 0;
42 }
43 
44 // CHECK: SUMMARY: AddressSanitizer: global-buffer-overflow
45