1 // This test is broken with shared libstdc++ / libc++ on Android.
2 // RUN: %clangxx_hwasan -static-libstdc++ %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
3 // RUN: %clangxx_hwasan -static-libstdc++ -DNO_SANITIZE_F %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
4 // RUN: %clangxx_hwasan_oldrt -static-libstdc++ %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
5 // RUN: %clangxx_hwasan_oldrt -static-libstdc++ %s -mllvm -hwasan-instrument-landing-pads=0 -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=BAD
6 
7 // C++ tests on x86_64 require instrumented libc++/libstdc++.
8 // REQUIRES: aarch64-target-arch
9 
10 #include <stdexcept>
11 #include <cstdio>
12 
optimization_barrier(void * arg)13 static void optimization_barrier(void* arg) {
14   asm volatile("" : : "r"(arg) : "memory");
15 }
16 
17 __attribute__((noinline))
h()18 void h() {
19   char x[1000];
20   optimization_barrier(x);
21   throw std::runtime_error("hello");
22 }
23 
24 __attribute__((noinline))
g()25 void g() {
26   char x[1000];
27   optimization_barrier(x);
28   h();
29   optimization_barrier(x);
30 }
31 
32 __attribute__((noinline))
hwasan_read(char * p,int size)33 void hwasan_read(char *p, int size) {
34   char volatile sink;
35   for (int i = 0; i < size; ++i)
36     sink = p[i];
37 }
38 
after_catch()39 __attribute__((noinline, no_sanitize("hwaddress"))) void after_catch() {
40   char x[10000];
41   hwasan_read(&x[0], sizeof(x));
42 }
43 
44 
45 __attribute__((noinline))
46 #ifdef NO_SANITIZE_F
47 __attribute__((no_sanitize("hwaddress")))
48 #endif
f()49 void f() {
50   char x[1000];
51   try {
52     // Put two tagged frames on the stack, throw an exception from the deepest one.
53     g();
54   } catch (const std::runtime_error &e) {
55     // Put an untagged frame on stack, check that it is indeed untagged.
56     // This relies on exception support zeroing out stack tags.
57     // BAD: tag-mismatch
58     after_catch();
59     // Check that an in-scope stack allocation is still tagged.
60     // This relies on exception support not zeroing too much.
61     hwasan_read(&x[0], sizeof(x));
62     // GOOD: hello
63     printf("%s\n", e.what());
64   }
65 }
66 
main()67 int main() {
68   f();
69 }
70