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