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++ -DMALLOCEDSTACK %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
4 // RUN: %clangxx_hwasan -static-libstdc++ -DNO_SANITIZE_F %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
5 // RUN: %clangxx_hwasan_oldrt -static-libstdc++ %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
6 // 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
8 // C++ tests on x86_64 require instrumented libc++/libstdc++.
9 // RISC-V target doesn't support oldrt
10 // REQUIRES: aarch64-target-arch
17 #include <sanitizer/hwasan_interface.h>
21 static void optimization_barrier(void* arg
) {
22 asm volatile("" : : "r"(arg
) : "memory");
25 __attribute__((noinline
))
28 optimization_barrier(x
);
29 throw std::runtime_error("hello");
32 __attribute__((noinline
))
35 optimization_barrier(x
);
37 optimization_barrier(x
);
40 __attribute__((noinline
))
41 void hwasan_read(char *p
, int size
) {
43 for (int i
= 0; i
< size
; ++i
)
47 __attribute__((noinline
, no_sanitize("hwaddress"))) void after_catch() {
49 hwasan_read(&x
[0], sizeof(x
));
52 __attribute__((noinline
))
54 __attribute__((no_sanitize("hwaddress")))
60 // Put two tagged frames on the stack, throw an exception from the deepest one.
62 } catch (const std::runtime_error
&e
) {
63 // Put an untagged frame on stack, check that it is indeed untagged.
64 // This relies on exception support zeroing out stack tags.
67 // Check that an in-scope stack allocation is still tagged.
68 // This relies on exception support not zeroing too much.
69 hwasan_read(&x
[0], sizeof(x
));
71 printf("%s\n", e
.what());
77 __hwasan_enable_allocator_tagging();
80 void *stack
= malloc(PTHREAD_STACK_MIN
);
81 assert(pthread_attr_init(&attr
) == 0);
82 if (pthread_attr_setstack(&attr
, stack
, PTHREAD_STACK_MIN
) != 0) {
83 fprintf(stderr
, "pthread_attr_setstack: %s", strerror(errno
));
87 if (pthread_create(&thid
, &attr
, f
, nullptr) != 0) {
88 fprintf(stderr
, "pthread_create: %s", strerror(errno
));
92 if (pthread_join(thid
, &ret
) != 0) {
93 fprintf(stderr
, "pthread_join: %s", strerror(errno
));
96 assert(pthread_attr_destroy(&attr
) == 0);