Properly restore SP tag on exceptions

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D152036
This commit is contained in:
Florian Mayer 2023-06-02 14:11:38 -07:00
parent f5371eb3d3
commit 6a2e0cb418
3 changed files with 39 additions and 6 deletions

View File

@ -62,7 +62,8 @@ __hwasan_personality_wrapper(int version, _Unwind_Action actions,
#error Unsupported architecture
#endif
uptr sp = get_cfa(context);
TagMemory(sp, fp - sp, 0);
TagMemory(UntagAddr(sp), UntagAddr(fp) - UntagAddr(sp),
GetTagFromPointer(sp));
}
return rc;

View File

@ -93,7 +93,9 @@ void Thread::InitStackRingBuffer(uptr stack_buffer_start,
void Thread::ClearShadowForThreadStackAndTLS() {
if (stack_top_ != stack_bottom_)
TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
TagMemory(UntagAddr(stack_bottom_),
UntagAddr(stack_top_) - UntagAddr(stack_bottom_),
GetTagFromPointer(stack_top_));
if (tls_begin_ != tls_end_)
TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
}

View File

@ -1,5 +1,6 @@
// This test is broken with shared libstdc++ / libc++ on Android.
// RUN: %clangxx_hwasan -static-libstdc++ %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
// RUN: %clangxx_hwasan -static-libstdc++ -DMALLOCEDSTACK %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
// RUN: %clangxx_hwasan -static-libstdc++ -DNO_SANITIZE_F %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
// RUN: %clangxx_hwasan_oldrt -static-libstdc++ %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=GOOD
// 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 @@
// RISC-V target doesn't support oldrt
// REQUIRES: aarch64-target-arch
#include <stdexcept>
#include <cassert>
#include <cstdio>
#include <errno.h>
#include <pthread.h>
#include <sanitizer/hwasan_interface.h>
#include <stdexcept>
#include <string.h>
static void optimization_barrier(void* arg) {
asm volatile("" : : "r"(arg) : "memory");
@ -42,12 +48,12 @@ __attribute__((noinline, no_sanitize("hwaddress"))) void after_catch() {
hwasan_read(&x[0], sizeof(x));
}
__attribute__((noinline))
#ifdef NO_SANITIZE_F
__attribute__((no_sanitize("hwaddress")))
#endif
void f() {
void *
f(void *) {
char x[1000];
try {
// Put two tagged frames on the stack, throw an exception from the deepest one.
@ -63,8 +69,32 @@ void f() {
// GOOD: hello
printf("%s\n", e.what());
}
return nullptr;
}
int main() {
f();
__hwasan_enable_allocator_tagging();
#ifdef MALLOCEDSTACK
pthread_attr_t attr;
void *stack = malloc(PTHREAD_STACK_MIN);
assert(pthread_attr_init(&attr) == 0);
if (pthread_attr_setstack(&attr, stack, PTHREAD_STACK_MIN) != 0) {
fprintf(stderr, "pthread_attr_setstack: %s", strerror(errno));
abort();
}
pthread_t thid;
if (pthread_create(&thid, &attr, f, nullptr) != 0) {
fprintf(stderr, "pthread_create: %s", strerror(errno));
abort();
}
void *ret;
if (pthread_join(thid, &ret) != 0) {
fprintf(stderr, "pthread_join: %s", strerror(errno));
abort();
}
assert(pthread_attr_destroy(&attr) == 0);
free(stack);
#else
f(nullptr);
#endif
}