xref: /llvm-project/compiler-rt/test/hwasan/TestCases/use-after-scope-setjmp.cpp (revision 6cc9244baa63fcb7c6f35f46dab9fa17a421a6ce)
1 // RUN: %clangxx_hwasan  -mllvm -hwasan-use-stack-safety=0 -O2 %s -o %t && \
2 // RUN:     %run %t 2>&1
3 
4 // REQUIRES: aarch64-target-arch || riscv64-target-arch
5 
6 #include <sanitizer/hwasan_interface.h>
7 #include <setjmp.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include <sys/types.h>
12 #include <unistd.h>
13 
14 volatile const char *stackbuf = nullptr;
15 jmp_buf jbuf;
16 
jump()17 __attribute__((noinline)) bool jump() {
18   // Fool the compiler so it cannot deduce noreturn.
19   if (getpid() != 0) {
20     longjmp(jbuf, 1);
21     return true;
22   }
23   return false;
24 }
25 
target()26 bool target() {
27   switch (setjmp(jbuf)) {
28   case 1:
29     return false;
30   default:
31     break;
32   }
33 
34   while (true) {
35     char buf[4096];
36     stackbuf = buf;
37     if (!jump()) {
38       break;
39     };
40   }
41   return true;
42 }
43 
main()44 int main() {
45   target();
46 
47   void *untagged = __hwasan_tag_pointer(stackbuf, 0);
48   if (stackbuf == untagged) {
49     // The buffer wasn't tagged in the first place, so the test will not work
50     // as expected.
51     return 2;
52   }
53   if (__hwasan_test_shadow(untagged, 4096) != -1) {
54     return 1;
55   }
56 
57   return 0;
58 }
59