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
14volatile const char *stackbuf = nullptr;
15jmp_buf jbuf;
16
17__attribute__((noinline)) bool jump() {
18 // Fool the compiler so it cannot deduce noreturn.
19 if (getpid() != 0) {
20 longjmp(env: jbuf, val: 1);
21 return true;
22 }
23 return false;
24}
25
26bool 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
44int main() {
45 target();
46
47 void *untagged = __hwasan_tag_pointer(p: stackbuf, tag: 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(x: untagged, size: 4096) != -1) {
54 return 1;
55 }
56
57 return 0;
58}
59

source code of compiler-rt/test/hwasan/TestCases/use-after-scope-setjmp.cpp