xref: /llvm-project/compiler-rt/test/hwasan/TestCases/use-after-scope-goto.cpp (revision 6cc9244baa63fcb7c6f35f46dab9fa17a421a6ce)
1 // This is the ASAN test of the same name ported to HWAsan.
2 
3 // RUN: %clangxx_hwasan -O0 %s -o %t && %run %t
4 
5 // Function jumps over variable initialization making lifetime analysis
6 // ambiguous. Asan should ignore such variable and program must not fail.
7 
8 // REQUIRES: aarch64-target-arch || riscv64-target-arch
9 
10 #include <stdlib.h>
11 
12 int *ptr;
13 
f1(int cond)14 void f1(int cond) {
15   if (cond)
16     goto label;
17   int tmp;
18 
19 label:
20   ptr = &tmp;
21   *ptr = 5;
22 }
23 
f2(int cond)24 void f2(int cond) {
25   switch (cond) {
26   case 1: {
27     ++cond;
28     int tmp;
29     ptr = &tmp;
30     exit(0);
31   case 2:
32     ptr = &tmp;
33     *ptr = 5;
34     exit(0);
35   }
36   }
37 }
38 
f3(int cond)39 void f3(int cond) {
40   {
41     int tmp;
42     goto l2;
43   l1:
44     ptr = &tmp;
45     *ptr = 5;
46 
47     exit(0);
48   }
49 l2:
50   goto l1;
51 }
52 
use(int * x)53 void use(int *x) {
54   static int c = 10;
55   if (--c == 0)
56     exit(0);
57   (*x)++;
58 }
59 
f4()60 void f4() {
61   {
62     int x;
63   l2:
64     use(&x);
65     goto l1;
66   }
67 l1:
68   goto l2;
69 }
70 
main()71 int main() {
72   f1(1);
73   f2(1);
74   f3(1);
75   f4();
76   return 0;
77 }
78