xref: /llvm-project/compiler-rt/test/hwasan/TestCases/global.c (revision 9006b082a5e56c3be4e4116cfbea6d26ba781c59)
1 // RUN: %clang_hwasan %s -o %t
2 // RUN: %run %t 0
3 // RUN: not %run %t 1 2>&1 | FileCheck --check-prefixes=CHECK,RSYM %s
4 // RUN: not %env_hwasan_opts=symbolize=0 %run %t 1 2>&1 | FileCheck --check-prefixes=CHECK,RNOSYM %s
5 // RUN: not %run %t -1 2>&1 | FileCheck --check-prefixes=CHECK,LSYM %s
6 // RUN: not %env_hwasan_opts=symbolize=0 %run %t -1 2>&1 | FileCheck --check-prefixes=CHECK,LNOSYM %s
7 
8 // Test with and without optimizations, with and without PIC, since different
9 // backend passes run depending on these flags.
10 // RUN: %clang_hwasan -fno-pic %s -o %t
11 // RUN: not %run %t 1 2>&1 | FileCheck --check-prefixes=CHECK,RSYM %s
12 // RUN: %clang_hwasan -fno-pic -O2 %s -o %t
13 // RUN: not %run %t 1 2>&1 | FileCheck --check-prefixes=CHECK,RSYM %s
14 // RUN: %clang_hwasan -O2 %s -o %t
15 // RUN: not %run %t 1 2>&1 | FileCheck --check-prefixes=CHECK,RSYM %s
16 
17 // RUN: %clang_hwasan -DUSE_NOSANITIZE %s -o %t && %run %t 0
18 // RUN: %clang_hwasan -DUSE_NOSANITIZE %s -o %t && %run %t 1
19 // RUN: %clang_hwasan -DUSE_NOSANITIZE %s -o %t -fno-pic && %run %t 1
20 // RUN: %clang_hwasan -DUSE_NOSANITIZE %s -o %t -O2 && %run %t 1
21 // RUN: %clang_hwasan -DUSE_NOSANITIZE %s -o %t -fno-pic -O2 && %run %t 1
22 
23 // REQUIRES: pointer-tagging
24 
25 #include <stdlib.h>
26 
27 int a = 1;
28 #ifdef USE_NOSANITIZE
29 __attribute__((no_sanitize("hwaddress"))) int x = 1;
30 #else // USE_NOSANITIZE
31 int x = 1;
32 #endif // USE_NOSANITIZE
33 int b = 1;
34 
35 int atoi(const char *);
36 
main(int argc,char ** argv)37 int main(int argc, char **argv) {
38   // CHECK: Cause: global-overflow
39   // RSYM: is located 0 bytes after a 4-byte global variable x {{.*}} in {{.*}}global.c.tmp
40   // RNOSYM: is located after a 4-byte global variable in
41   // RNOSYM-NEXT: #0 0x{{.*}} ({{.*}}global.c.tmp+{{.*}})
42   // LSYM: is located 4 bytes before a 4-byte global variable x {{.*}} in {{.*}}global.c.tmp
43   // LNOSYM: is located before a 4-byte global variable in
44   // LNOSYM-NEXT: #0 0x{{.*}} ({{.*}}global.c.tmp+{{.*}})
45   // CHECK-NOT: can not describe
46   (&x)[atoi(argv[1])] = 1;
47 }
48