[compiler-rt] [HWASAN] Enable memcpy, memmove and memset interceptors (PR #70387)
Kirill Stoimenov via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 30 13:30:57 PDT 2023
https://github.com/kstoimenov updated https://github.com/llvm/llvm-project/pull/70387
>From f5e83ae9a4329b74b12f3e7d00ce2db5929cbb4f Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Thu, 26 Oct 2023 22:37:50 +0000
Subject: [PATCH 1/3] [HWASAN] Enable memcpy, memmove and memset interceptors
---
.../lib/hwasan/hwasan_interceptors.cpp | 27 +--------------
.../lib/hwasan/hwasan_platform_interceptors.h | 12 +++----
compiler-rt/test/hwasan/TestCases/bcmp.cpp | 7 +++-
compiler-rt/test/hwasan/TestCases/memcmp.cpp | 7 +++-
compiler-rt/test/hwasan/TestCases/memcpy.cpp | 33 +++++++++++++++++++
compiler-rt/test/hwasan/TestCases/memmove.cpp | 32 ++++++++++++++++++
compiler-rt/test/hwasan/TestCases/memset.cpp | 32 ++++++++++++++++++
7 files changed, 116 insertions(+), 34 deletions(-)
create mode 100644 compiler-rt/test/hwasan/TestCases/memcpy.cpp
create mode 100644 compiler-rt/test/hwasan/TestCases/memmove.cpp
create mode 100644 compiler-rt/test/hwasan/TestCases/memset.cpp
diff --git a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
index 0889831373a8039..5171f035f97f764 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,30 +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); \
- (void)(block); \
- (void)(c); \
- (void)(size); \
- } while (false)
-
# define COMMON_INTERCEPTOR_STRERROR() \
do { \
} while (false)
diff --git a/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h b/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h
index 86d26b5ac12d4a7..d92b51052194275 100644
--- a/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h
+++ b/compiler-rt/lib/hwasan/hwasan_platform_interceptors.h
@@ -56,14 +56,14 @@
#undef SANITIZER_INTERCEPT_STRCASECMP
#define SANITIZER_INTERCEPT_STRCASECMP 0
-#undef SANITIZER_INTERCEPT_MEMSET
-#define SANITIZER_INTERCEPT_MEMSET 0
+// #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
diff --git a/compiler-rt/test/hwasan/TestCases/bcmp.cpp b/compiler-rt/test/hwasan/TestCases/bcmp.cpp
index a83147b0f320528..6ace21eb506f1d8 100644
--- a/compiler-rt/test/hwasan/TestCases/bcmp.cpp
+++ b/compiler-rt/test/hwasan/TestCases/bcmp.cpp
@@ -9,6 +9,11 @@
#include <string.h>
#include <unistd.h>
+__attribute__((no_sanitize("hwaddress")))
+int ForceCallInterceptor(void *p, const void *a, size_t size) {
+ return bcmp(p, a, size);
+}
+
int main(int argc, char **argv) {
__hwasan_enable_allocator_tagging();
char a[] = {static_cast<char>(argc), 2, 3, 4};
@@ -16,7 +21,7 @@ int main(int argc, char **argv) {
char *p = (char *)malloc(size);
memcpy(p, a, size);
free(p);
- return bcmp(p, a, size);
+ return ForceCallInterceptor(p, a, size);
// CHECK: HWAddressSanitizer: tag-mismatch on address
// CHECK: READ of size 4
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-3]]
diff --git a/compiler-rt/test/hwasan/TestCases/memcmp.cpp b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
index 5f8a93f62a44a1d..6b7e86550015e67 100644
--- a/compiler-rt/test/hwasan/TestCases/memcmp.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
@@ -8,6 +8,11 @@
#include <string.h>
#include <unistd.h>
+__attribute__((no_sanitize("hwaddress")))
+int ForceCalInterceptor(void *p, const void *a, size_t size) {
+ return memcmp(p, a, size);
+}
+
int main(int argc, char **argv) {
__hwasan_enable_allocator_tagging();
char a[] = {static_cast<char>(argc), 2, 3, 4};
@@ -15,7 +20,7 @@ int main(int argc, char **argv) {
char *p = (char *)malloc(size);
memcpy(p, a, size);
free(p);
- return memcmp(p, a, size);
+ return ForceCalInterceptor(p, a, size);
// CHECK: HWAddressSanitizer: tag-mismatch on address
// CHECK: READ of size 4
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-3]]
diff --git a/compiler-rt/test/hwasan/TestCases/memcpy.cpp b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
new file mode 100644
index 000000000000000..e346409a1bd5e26
--- /dev/null
+++ b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
@@ -0,0 +1,33 @@
+// 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")))
+int ForceCallInterceptor(void *p, const void *a, size_t size) {
+ return memcpy(p, a, size) == nullptr;
+}
+
+
+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);
+ return ForceCallInterceptor(p, a, size);
+ // CHECK: HWAddressSanitizer: tag-mismatch on address
+ // CHECK: WRITE of size 4
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-3]]
+ // CHECK: Cause: use-after-free
+ // CHECK: freed by thread
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-7]]
+ // CHECK: previously allocated by thread
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-10]]
+}
+
diff --git a/compiler-rt/test/hwasan/TestCases/memmove.cpp b/compiler-rt/test/hwasan/TestCases/memmove.cpp
new file mode 100644
index 000000000000000..e3a3ec56aaf7866
--- /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")))
+int ForceCallInterceptor(void *p, const void *a, size_t size) {
+ return memmove(p, a, size) == nullptr;
+}
+
+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);
+ return ForceCallInterceptor(p, a, size);
+ // CHECK: HWAddressSanitizer: tag-mismatch on address
+ // CHECK: WRITE of size 4
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-3]]
+ // CHECK: Cause: use-after-free
+ // CHECK: freed by thread
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-7]]
+ // CHECK: previously allocated by thread
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-10]]
+}
+
diff --git a/compiler-rt/test/hwasan/TestCases/memset.cpp b/compiler-rt/test/hwasan/TestCases/memset.cpp
new file mode 100644
index 000000000000000..5ebe2fbad407814
--- /dev/null
+++ b/compiler-rt/test/hwasan/TestCases/memset.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")))
+int ForceCallInterceptor(void *p, int c, size_t size) {
+ return memset(p, c, size) == nullptr;
+}
+
+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);
+ return ForceCallInterceptor(p, 0, size);
+ // CHECK: HWAddressSanitizer: tag-mismatch on address
+ // CHECK: WRITE of size 4
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset.cpp:[[@LINE-3]]
+ // CHECK: Cause: use-after-free
+ // CHECK: freed by thread
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset.cpp:[[@LINE-7]]
+ // CHECK: previously allocated by thread
+ // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset.cpp:[[@LINE-10]]
+}
+
>From 78bb9c46d8274f611892b8bf12eaeb2fc516ebb0 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Mon, 30 Oct 2023 16:53:16 +0000
Subject: [PATCH 2/3] Fixed the spelling.
---
compiler-rt/test/hwasan/TestCases/memcmp.cpp | 4 ++--
compiler-rt/test/hwasan/TestCases/memcpy.cpp | 1 -
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/compiler-rt/test/hwasan/TestCases/memcmp.cpp b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
index 6b7e86550015e67..def31400bd0295d 100644
--- a/compiler-rt/test/hwasan/TestCases/memcmp.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
@@ -9,7 +9,7 @@
#include <unistd.h>
__attribute__((no_sanitize("hwaddress")))
-int ForceCalInterceptor(void *p, const void *a, size_t size) {
+int ForceCallInterceptor(void *p, const void *a, size_t size) {
return memcmp(p, a, size);
}
@@ -20,7 +20,7 @@ int main(int argc, char **argv) {
char *p = (char *)malloc(size);
memcpy(p, a, size);
free(p);
- return ForceCalInterceptor(p, a, size);
+ return ForceCallInterceptor(p, a, size);
// CHECK: HWAddressSanitizer: tag-mismatch on address
// CHECK: READ of size 4
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-3]]
diff --git a/compiler-rt/test/hwasan/TestCases/memcpy.cpp b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
index e346409a1bd5e26..1aaae3a19922df9 100644
--- a/compiler-rt/test/hwasan/TestCases/memcpy.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
@@ -13,7 +13,6 @@ int ForceCallInterceptor(void *p, const void *a, size_t size) {
return memcpy(p, a, size) == nullptr;
}
-
int main(int argc, char **argv) {
__hwasan_enable_allocator_tagging();
char a[] = {static_cast<char>(argc), 2, 3, 4};
>From 53cd18592f8e8d8c2b6762387cf6a1dbe8ff7d69 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Mon, 30 Oct 2023 20:30:11 +0000
Subject: [PATCH 3/3] Fix clang-format
---
compiler-rt/test/hwasan/TestCases/bcmp.cpp | 4 ++--
compiler-rt/test/hwasan/TestCases/memcmp.cpp | 4 ++--
compiler-rt/test/hwasan/TestCases/memcpy.cpp | 5 ++---
compiler-rt/test/hwasan/TestCases/memmove.cpp | 5 ++---
compiler-rt/test/hwasan/TestCases/memset.cpp | 5 ++---
5 files changed, 10 insertions(+), 13 deletions(-)
diff --git a/compiler-rt/test/hwasan/TestCases/bcmp.cpp b/compiler-rt/test/hwasan/TestCases/bcmp.cpp
index 6ace21eb506f1d8..bfc002bedac763f 100644
--- a/compiler-rt/test/hwasan/TestCases/bcmp.cpp
+++ b/compiler-rt/test/hwasan/TestCases/bcmp.cpp
@@ -9,8 +9,8 @@
#include <string.h>
#include <unistd.h>
-__attribute__((no_sanitize("hwaddress")))
-int ForceCallInterceptor(void *p, const void *a, size_t size) {
+__attribute__((no_sanitize("hwaddress"))) int
+ForceCallInterceptor(void *p, const void *a, size_t size) {
return bcmp(p, a, size);
}
diff --git a/compiler-rt/test/hwasan/TestCases/memcmp.cpp b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
index def31400bd0295d..1ad7dfa35da8414 100644
--- a/compiler-rt/test/hwasan/TestCases/memcmp.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memcmp.cpp
@@ -8,8 +8,8 @@
#include <string.h>
#include <unistd.h>
-__attribute__((no_sanitize("hwaddress")))
-int ForceCallInterceptor(void *p, const void *a, size_t size) {
+__attribute__((no_sanitize("hwaddress"))) int
+ForceCallInterceptor(void *p, const void *a, size_t size) {
return memcmp(p, a, size);
}
diff --git a/compiler-rt/test/hwasan/TestCases/memcpy.cpp b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
index 1aaae3a19922df9..7d9a69196c0dc9f 100644
--- a/compiler-rt/test/hwasan/TestCases/memcpy.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memcpy.cpp
@@ -8,8 +8,8 @@
#include <string.h>
#include <unistd.h>
-__attribute__((no_sanitize("hwaddress")))
-int ForceCallInterceptor(void *p, const void *a, size_t size) {
+__attribute__((no_sanitize("hwaddress"))) int
+ForceCallInterceptor(void *p, const void *a, size_t size) {
return memcpy(p, a, size) == nullptr;
}
@@ -29,4 +29,3 @@ int main(int argc, char **argv) {
// CHECK: previously allocated by thread
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-10]]
}
-
diff --git a/compiler-rt/test/hwasan/TestCases/memmove.cpp b/compiler-rt/test/hwasan/TestCases/memmove.cpp
index e3a3ec56aaf7866..6a1acb8872235b0 100644
--- a/compiler-rt/test/hwasan/TestCases/memmove.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memmove.cpp
@@ -8,8 +8,8 @@
#include <string.h>
#include <unistd.h>
-__attribute__((no_sanitize("hwaddress")))
-int ForceCallInterceptor(void *p, const void *a, size_t size) {
+__attribute__((no_sanitize("hwaddress"))) int
+ForceCallInterceptor(void *p, const void *a, size_t size) {
return memmove(p, a, size) == nullptr;
}
@@ -29,4 +29,3 @@ int main(int argc, char **argv) {
// CHECK: previously allocated by thread
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-10]]
}
-
diff --git a/compiler-rt/test/hwasan/TestCases/memset.cpp b/compiler-rt/test/hwasan/TestCases/memset.cpp
index 5ebe2fbad407814..3006b04998e3131 100644
--- a/compiler-rt/test/hwasan/TestCases/memset.cpp
+++ b/compiler-rt/test/hwasan/TestCases/memset.cpp
@@ -8,8 +8,8 @@
#include <string.h>
#include <unistd.h>
-__attribute__((no_sanitize("hwaddress")))
-int ForceCallInterceptor(void *p, int c, size_t size) {
+__attribute__((no_sanitize("hwaddress"))) int
+ForceCallInterceptor(void *p, int c, size_t size) {
return memset(p, c, size) == nullptr;
}
@@ -29,4 +29,3 @@ int main(int argc, char **argv) {
// CHECK: previously allocated by thread
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset.cpp:[[@LINE-10]]
}
-
More information about the llvm-commits
mailing list