[compiler-rt] 5ac240b - [hwasan] Properly restore SP tag on exceptions

Florian Mayer via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 5 15:22:27 PDT 2023


Author: Florian Mayer
Date: 2023-06-05T15:22:18-07:00
New Revision: 5ac240bbea14d4edd0c484f5f879cd0f26be59e3

URL: https://github.com/llvm/llvm-project/commit/5ac240bbea14d4edd0c484f5f879cd0f26be59e3
DIFF: https://github.com/llvm/llvm-project/commit/5ac240bbea14d4edd0c484f5f879cd0f26be59e3.diff

LOG: [hwasan] Properly restore SP tag on exceptions

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D152036

Added: 
    

Modified: 
    compiler-rt/lib/hwasan/hwasan_exceptions.cpp
    compiler-rt/lib/hwasan/hwasan_thread.cpp
    compiler-rt/test/hwasan/TestCases/try-catch.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/hwasan/hwasan_exceptions.cpp b/compiler-rt/lib/hwasan/hwasan_exceptions.cpp
index c9968a5e36037..bf700bf568389 100644
--- a/compiler-rt/lib/hwasan/hwasan_exceptions.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_exceptions.cpp
@@ -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;

diff  --git a/compiler-rt/lib/hwasan/hwasan_thread.cpp b/compiler-rt/lib/hwasan/hwasan_thread.cpp
index c4ab091d956c5..a97b8e55f37cb 100644
--- a/compiler-rt/lib/hwasan/hwasan_thread.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_thread.cpp
@@ -93,9 +93,13 @@ 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);
+    TagMemory(UntagAddr(tls_begin_),
+              UntagAddr(tls_end_) - UntagAddr(tls_begin_),
+              GetTagFromPointer(tls_begin_));
 }
 
 void Thread::Destroy() {

diff  --git a/compiler-rt/test/hwasan/TestCases/try-catch.cpp b/compiler-rt/test/hwasan/TestCases/try-catch.cpp
index 1449d8d6f81d0..21bb8cae0def5 100644
--- a/compiler-rt/test/hwasan/TestCases/try-catch.cpp
+++ b/compiler-rt/test/hwasan/TestCases/try-catch.cpp
@@ -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,14 @@
 // RISC-V target doesn't support oldrt
 // REQUIRES: aarch64-target-arch
 
-#include <stdexcept>
+#include <cassert>
 #include <cstdio>
+#include <errno.h>
+#include <limits.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 +49,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 +70,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
 }


        


More information about the llvm-commits mailing list