xref: /llvm-project/compiler-rt/test/hwasan/TestCases/global-with-reduction.c (revision 0e215e095f6ca0705c5aad4cbc61aaedec1a9e31)
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 // GlobalOpt may replace the current GV with a new boolean-typed GV. Previously,
28 // this resulted in the "nosanitize" getting dropped because while the data/code
29 // references to the GV were updated, the old metadata references weren't.
f()30 int* f() {
31 #ifdef USE_NOSANITIZE
32 __attribute__((no_sanitize("hwaddress"))) static int x = 1;
33 #else // USE_NOSANITIZE
34   static int x = 1;
35 #endif // USE_NOSANITIZE
36   if (x == 1) x = 0;
37   return &x;
38 }
39 
main(int argc,char ** argv)40 int main(int argc, char **argv) {
41   // CHECK: Cause: global-overflow
42   // RSYM: is located 0 bytes after a 4-byte global variable f.x {{.*}} in {{.*}}global-with-reduction.c.tmp
43   // RNOSYM: is located after a 4-byte global variable in
44   // RNOSYM-NEXT: #0 0x{{.*}} ({{.*}}global-with-reduction.c.tmp+{{.*}})
45   // LSYM: is located 4 bytes before a 4-byte global variable f.x {{.*}} in {{.*}}global-with-reduction.c.tmp
46   // LNOSYM: is located before a 4-byte global variable in
47   // LNOSYM-NEXT: #0 0x{{.*}} ({{.*}}global-with-reduction.c.tmp+{{.*}})
48   // CHECK-NOT: can not describe
49   f()[atoi(argv[1])] = 1;
50 }
51