Skip to content

Commit 6a2e0cb

Browse files
committed
Properly restore SP tag on exceptions
Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D152036
1 parent f5371eb commit 6a2e0cb

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

compiler-rt/lib/hwasan/hwasan_exceptions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ __hwasan_personality_wrapper(int version, _Unwind_Action actions,
6262
#error Unsupported architecture
6363
#endif
6464
uptr sp = get_cfa(context);
65-
TagMemory(sp, fp - sp, 0);
65+
TagMemory(UntagAddr(sp), UntagAddr(fp) - UntagAddr(sp),
66+
GetTagFromPointer(sp));
6667
}
6768

6869
return rc;

compiler-rt/lib/hwasan/hwasan_thread.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ void Thread::InitStackRingBuffer(uptr stack_buffer_start,
9393

9494
void Thread::ClearShadowForThreadStackAndTLS() {
9595
if (stack_top_ != stack_bottom_)
96-
TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
96+
TagMemory(UntagAddr(stack_bottom_),
97+
UntagAddr(stack_top_) - UntagAddr(stack_bottom_),
98+
GetTagFromPointer(stack_top_));
9799
if (tls_begin_ != tls_end_)
98100
TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
99101
}

compiler-rt/test/hwasan/TestCases/try-catch.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// This test is broken with shared libstdc++ / libc++ on Android.
22
// 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
34
// RUN: %clangxx_hwasan -static-libstdc++ -DNO_SANITIZE_F %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
45
// RUN: %clangxx_hwasan_oldrt -static-libstdc++ %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
56
// 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,8 +9,13 @@
89
// RISC-V target doesn't support oldrt
910
// REQUIRES: aarch64-target-arch
1011

11-
#include <stdexcept>
12+
#include <cassert>
1213
#include <cstdio>
14+
#include <errno.h>
15+
#include <pthread.h>
16+
#include <sanitizer/hwasan_interface.h>
17+
#include <stdexcept>
18+
#include <string.h>
1319

1420
static void optimization_barrier(void* arg) {
1521
asm volatile("" : : "r"(arg) : "memory");
@@ -42,12 +48,12 @@ __attribute__((noinline, no_sanitize("hwaddress"))) void after_catch() {
4248
hwasan_read(&x[0], sizeof(x));
4349
}
4450

45-
4651
__attribute__((noinline))
4752
#ifdef NO_SANITIZE_F
4853
__attribute__((no_sanitize("hwaddress")))
4954
#endif
50-
void f() {
55+
void *
56+
f(void *) {
5157
char x[1000];
5258
try {
5359
// Put two tagged frames on the stack, throw an exception from the deepest one.
@@ -63,8 +69,32 @@ void f() {
6369
// GOOD: hello
6470
printf("%s\n", e.what());
6571
}
72+
return nullptr;
6673
}
6774

6875
int main() {
69-
f();
76+
__hwasan_enable_allocator_tagging();
77+
#ifdef MALLOCEDSTACK
78+
pthread_attr_t attr;
79+
void *stack = malloc(PTHREAD_STACK_MIN);
80+
assert(pthread_attr_init(&attr) == 0);
81+
if (pthread_attr_setstack(&attr, stack, PTHREAD_STACK_MIN) != 0) {
82+
fprintf(stderr, "pthread_attr_setstack: %s", strerror(errno));
83+
abort();
84+
}
85+
pthread_t thid;
86+
if (pthread_create(&thid, &attr, f, nullptr) != 0) {
87+
fprintf(stderr, "pthread_create: %s", strerror(errno));
88+
abort();
89+
}
90+
void *ret;
91+
if (pthread_join(thid, &ret) != 0) {
92+
fprintf(stderr, "pthread_join: %s", strerror(errno));
93+
abort();
94+
}
95+
assert(pthread_attr_destroy(&attr) == 0);
96+
free(stack);
97+
#else
98+
f(nullptr);
99+
#endif
70100
}

0 commit comments

Comments
 (0)