[libcxx] [llvm] [libcxxabi] [compiler-rt] [lld] [mlir] [HWASAN] Enable memcpy and memmove interceptors (PR #71217)

Kirill Stoimenov via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 3 14:15:18 PDT 2023


https://github.com/kstoimenov updated https://github.com/llvm/llvm-project/pull/71217

>From dc2da32a4afd802bfe70f2d6c65b02a0a709ff94 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 3 Nov 2023 18:30:01 +0000
Subject: [PATCH 1/3] [HWASAN] Force inteceptors tests for memcmp and bcmp call
 interceptor

---
 compiler-rt/test/hwasan/TestCases/bcmp.cpp   | 15 +++++++++++----
 compiler-rt/test/hwasan/TestCases/memcmp.cpp | 15 +++++++++++----
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/compiler-rt/test/hwasan/TestCases/bcmp.cpp b/compiler-rt/test/hwasan/TestCases/bcmp.cpp
index a83147b0f320528..9b21bba56b1beea 100644
--- a/compiler-rt/test/hwasan/TestCases/bcmp.cpp
+++ b/compiler-rt/test/hwasan/TestCases/bcmp.cpp
@@ -4,11 +4,17 @@
 // RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
 // REQUIRES: !android
 
+#include <assert.h>
 #include <sanitizer/hwasan_interface.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+__attribute__((no_sanitize("hwaddress"))) void
+ForceCallInterceptor(void *p, const void *a, size_t size) {
+  assert(bcmp(p, a, size) == 0);
+}
+
 int main(int argc, char **argv) {
   __hwasan_enable_allocator_tagging();
   char a[] = {static_cast<char>(argc), 2, 3, 4};
@@ -16,13 +22,14 @@ int main(int argc, char **argv) {
   char *p = (char *)malloc(size);
   memcpy(p, a, size);
   free(p);
-  return bcmp(p, a, size);
+  ForceCallInterceptor(p, a, size);
+  return 0;
   // CHECK: HWAddressSanitizer: tag-mismatch on address
   // CHECK: READ of size 4
-  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-3]]
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-4]]
   // CHECK: Cause: use-after-free
   // CHECK: freed by thread
-  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-7]]
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-8]]
   // CHECK: previously allocated by thread
-  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-11]]
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-12]]
 }
diff --git a/compiler-rt/test/hwasan/TestCases/memcmp.cpp b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
index 5f8a93f62a44a1d..31915527c27fdd4 100644
--- a/compiler-rt/test/hwasan/TestCases/memcmp.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
@@ -3,11 +3,17 @@
 // RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
 // RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
 
+#include <assert.h>
 #include <sanitizer/hwasan_interface.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+__attribute__((no_sanitize("hwaddress"))) void
+ForceCallInterceptor(void *p, const void *a, size_t size) {
+  assert(memcmp(p, a, size) == 0);
+}
+
 int main(int argc, char **argv) {
   __hwasan_enable_allocator_tagging();
   char a[] = {static_cast<char>(argc), 2, 3, 4};
@@ -15,13 +21,14 @@ int main(int argc, char **argv) {
   char *p = (char *)malloc(size);
   memcpy(p, a, size);
   free(p);
-  return memcmp(p, a, size);
+  ForceCallInterceptor(p, a, size);
+  return 0;
   // CHECK: HWAddressSanitizer: tag-mismatch on address
   // CHECK: READ of size 4
-  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-3]]
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-4]]
   // CHECK: Cause: use-after-free
   // CHECK: freed by thread
-  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-7]]
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-8]]
   // CHECK: previously allocated by thread
-  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-11]]
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-12]]
 }

>From 56079222bc8e8dbee6d4e9785f7669c7e5f382c3 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 3 Nov 2023 18:43:58 +0000
Subject: [PATCH 2/3] [HWASAN] Enable memcpy and memmove interceptors

---
 .../lib/hwasan/hwasan_interceptors.cpp        | 19 +------------------
 .../lib/hwasan/hwasan_platform_interceptors.h |  8 ++++----
 2 files changed, 5 insertions(+), 22 deletions(-)

diff --git a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
index 0889831373a8039..1a77d776e65e4ba 100644
--- a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
@@ -90,8 +90,7 @@ struct HWAsanInterceptorContext {
 #    include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
 
 #    define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
-      do {                                                 \
-      } while (false)
+      HWASAN_WRITE_RANGE(ctx, ptr, size)
 
 #    define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
       HWASAN_READ_RANGE(ctx, ptr, size)
@@ -147,22 +146,6 @@ struct HWAsanInterceptorContext {
         (void)(name);                           \
       } while (false)
 
-#    define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
-      do {                                                       \
-        (void)(ctx);                                             \
-        (void)(to);                                              \
-        (void)(from);                                            \
-        (void)(size);                                            \
-      } while (false)
-
-#    define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
-      do {                                                      \
-        (void)(ctx);                                            \
-        (void)(to);                                             \
-        (void)(from);                                           \
-        (void)(size);                                           \
-      } while (false)
-
 #    define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
       do {                                                      \
         (void)(ctx);                                            \
diff --git a/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h b/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h
index 86d26b5ac12d4a7..e31ee9e406c67e3 100644
--- a/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h
+++ b/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h
@@ -59,11 +59,11 @@
 #undef SANITIZER_INTERCEPT_MEMSET
 #define SANITIZER_INTERCEPT_MEMSET 0
 
-#undef SANITIZER_INTERCEPT_MEMMOVE
-#define SANITIZER_INTERCEPT_MEMMOVE 0
+// #undef SANITIZER_INTERCEPT_MEMMOVE
+// #define SANITIZER_INTERCEPT_MEMMOVE 0
 
-#undef SANITIZER_INTERCEPT_MEMCPY
-#define SANITIZER_INTERCEPT_MEMCPY 0
+// #undef SANITIZER_INTERCEPT_MEMCPY
+// #define SANITIZER_INTERCEPT_MEMCPY 0
 
 // #undef SANITIZER_INTERCEPT_MEMCMP
 // #define SANITIZER_INTERCEPT_MEMCMP 0

>From f6911c7e9b9e0e212c0917ecb5844a670bc0e5dd Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 3 Nov 2023 20:01:49 +0000
Subject: [PATCH 3/3] Added tests.

---
 compiler-rt/test/hwasan/TestCases/memcpy.cpp  | 32 +++++++++++++++++++
 compiler-rt/test/hwasan/TestCases/memmove.cpp | 32 +++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 compiler-rt/test/hwasan/TestCases/memcpy.cpp
 create mode 100644 compiler-rt/test/hwasan/TestCases/memmove.cpp

diff --git a/compiler-rt/test/hwasan/TestCases/memcpy.cpp b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
new file mode 100644
index 000000000000000..830449488fec49d
--- /dev/null
+++ b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
@@ -0,0 +1,32 @@
+// RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+#include <sanitizer/hwasan_interface.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+__attribute__((no_sanitize("hwaddress"))) void
+ForceCallInterceptor(void *p, const void *a, size_t size) {
+  memcpy(p, a, size);
+}
+
+int main(int argc, char **argv) {
+  __hwasan_enable_allocator_tagging();
+  char a[] = {static_cast<char>(argc), 2, 3, 4};
+  int size = sizeof(a);
+  char *volatile p = (char *)malloc(size);
+  free(p);
+  ForceCallInterceptor(p, a, size);
+  return 0;
+  // CHECK: HWAddressSanitizer: tag-mismatch on address
+  // CHECK: WRITE of size 4
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-4]]
+  // CHECK: Cause: use-after-free
+  // CHECK: freed by thread
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-8]]
+  // CHECK: previously allocated by thread
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-11]]
+}
diff --git a/compiler-rt/test/hwasan/TestCases/memmove.cpp b/compiler-rt/test/hwasan/TestCases/memmove.cpp
new file mode 100644
index 000000000000000..40dc3deeb393507
--- /dev/null
+++ b/compiler-rt/test/hwasan/TestCases/memmove.cpp
@@ -0,0 +1,32 @@
+// RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+#include <sanitizer/hwasan_interface.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+__attribute__((no_sanitize("hwaddress"))) void
+ForceCallInterceptor(void *p, const void *a, size_t size) {
+  memmove(p, a, size);
+}
+
+int main(int argc, char **argv) {
+  __hwasan_enable_allocator_tagging();
+  char a[] = {static_cast<char>(argc), 2, 3, 4};
+  int size = sizeof(a);
+  char *volatile p = (char *)malloc(size);
+  free(p);
+  ForceCallInterceptor(p, a, size);
+  return 0;
+  // CHECK: HWAddressSanitizer: tag-mismatch on address
+  // CHECK: WRITE of size 4
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-4]]
+  // CHECK: Cause: use-after-free
+  // CHECK: freed by thread
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-8]]
+  // CHECK: previously allocated by thread
+  // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-11]]
+}



More information about the llvm-commits mailing list