[compiler-rt] de79b0b - [hwasan] Enable common syscall interceptors

Thurston Dang via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 27 16:20:56 PDT 2023


Author: Thurston Dang
Date: 2023-04-27T23:13:29Z
New Revision: de79b0baa12d7528a11d4b830fed698b7290df01

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

LOG: [hwasan] Enable common syscall interceptors

This adds the sanitizer_common syscall hooks to HWASan and also defines
the COMMON_SYSCALL_PRE_{READ/WRITE}_RANGE macros.

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

Added: 
    compiler-rt/test/hwasan/TestCases/Linux/syscalls.cpp

Modified: 
    compiler-rt/lib/hwasan/hwasan_interceptors.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
index 06f4eecd2bdcd..67edba40b5b90 100644
--- a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "hwasan.h"
+#include "hwasan_checks.h"
 #include "hwasan_thread.h"
 #include "interception/interception.h"
 #include "sanitizer_common/sanitizer_linux.h"
@@ -40,6 +41,22 @@ static void *HwasanThreadStartFunc(void *arg) {
   return A.callback(A.param);
 }
 
+#    define COMMON_SYSCALL_PRE_READ_RANGE(p, s) __hwasan_loadN((uptr)p, (uptr)s)
+#    define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
+      __hwasan_storeN((uptr)p, (uptr)s)
+#    define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
+      do {                                       \
+        (void)(p);                               \
+        (void)(s);                               \
+      } while (false)
+#    define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
+      do {                                        \
+        (void)(p);                                \
+        (void)(s);                                \
+      } while (false)
+#    include "sanitizer_common/sanitizer_common_syscalls.inc"
+#    include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
+
 INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
             void * param) {
   EnsureMainThreadIDIsCorrect();

diff  --git a/compiler-rt/test/hwasan/TestCases/Linux/syscalls.cpp b/compiler-rt/test/hwasan/TestCases/Linux/syscalls.cpp
new file mode 100644
index 0000000000000..d7bc34ef324f5
--- /dev/null
+++ b/compiler-rt/test/hwasan/TestCases/Linux/syscalls.cpp
@@ -0,0 +1,33 @@
+// RUN: %clangxx_hwasan -O0 %s -o %t && not %env_hwasan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O3 %s -o %t && not %env_hwasan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s
+
+// UNSUPPORTED: android
+
+#include <assert.h>
+#include <errno.h>
+#include <glob.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sanitizer/hwasan_interface.h>
+#include <sanitizer/linux_syscall_hooks.h>
+
+/* Test the presence of __sanitizer_syscall_ in the tool runtime, and general
+   sanity of their behaviour. */
+
+int main(int argc, char *argv[]) {
+  // lit.cfg.py currently sets 'disable_allocator_tagging=1'
+  __hwasan_enable_allocator_tagging();
+
+  char *buf = (char *)malloc(1000);
+  assert(buf != NULL);
+
+  __sanitizer_syscall_pre_recvmsg(0, buf - 1, 0);
+  // CHECK: HWAddressSanitizer: tag-mismatch on address [[PTR:0x[a-f0-9]+]]
+  // CHECK: Cause: heap-buffer-overflow
+  // CHECK: [[PTR]] is located 1 bytes before a 1000-byte region
+
+  free(buf);
+  return 0;
+}


        


More information about the llvm-commits mailing list