Skip to content

Commit 3cf9bf3

Browse files
authored
[HWASAN] Enable memcpy and memmove interceptors (#71217)
1 parent fd887a3 commit 3cf9bf3

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

compiler-rt/lib/hwasan/hwasan_interceptors.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ struct HWAsanInterceptorContext {
9090
# include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
9191

9292
# define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
93-
do { \
94-
} while (false)
93+
HWASAN_WRITE_RANGE(ctx, ptr, size)
9594

9695
# define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
9796
HWASAN_READ_RANGE(ctx, ptr, size)
@@ -147,22 +146,6 @@ struct HWAsanInterceptorContext {
147146
(void)(name); \
148147
} while (false)
149148

150-
# define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
151-
do { \
152-
(void)(ctx); \
153-
(void)(to); \
154-
(void)(from); \
155-
(void)(size); \
156-
} while (false)
157-
158-
# define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
159-
do { \
160-
(void)(ctx); \
161-
(void)(to); \
162-
(void)(from); \
163-
(void)(size); \
164-
} while (false)
165-
166149
# define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
167150
do { \
168151
(void)(ctx); \

compiler-rt/lib/hwasan/hwasan_platform_interceptors.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
#undef SANITIZER_INTERCEPT_MEMSET
6060
#define SANITIZER_INTERCEPT_MEMSET 0
6161

62-
#undef SANITIZER_INTERCEPT_MEMMOVE
63-
#define SANITIZER_INTERCEPT_MEMMOVE 0
62+
// #undef SANITIZER_INTERCEPT_MEMMOVE
63+
// #define SANITIZER_INTERCEPT_MEMMOVE 0
6464

65-
#undef SANITIZER_INTERCEPT_MEMCPY
66-
#define SANITIZER_INTERCEPT_MEMCPY 0
65+
// #undef SANITIZER_INTERCEPT_MEMCPY
66+
// #define SANITIZER_INTERCEPT_MEMCPY 0
6767

6868
// #undef SANITIZER_INTERCEPT_MEMCMP
6969
// #define SANITIZER_INTERCEPT_MEMCMP 0
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2+
// RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
3+
// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
4+
// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
5+
6+
#include <sanitizer/hwasan_interface.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <unistd.h>
10+
11+
__attribute__((no_sanitize("hwaddress"))) void
12+
ForceCallInterceptor(void *p, const void *a, size_t size) {
13+
memcpy(p, a, size);
14+
}
15+
16+
int main(int argc, char **argv) {
17+
__hwasan_enable_allocator_tagging();
18+
char a[] = {static_cast<char>(argc), 2, 3, 4};
19+
int size = sizeof(a);
20+
char *volatile p = (char *)malloc(size);
21+
free(p);
22+
ForceCallInterceptor(p, a, size);
23+
return 0;
24+
// CHECK: HWAddressSanitizer: tag-mismatch on address
25+
// CHECK: WRITE of size 4
26+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-4]]
27+
// CHECK: Cause: use-after-free
28+
// CHECK: freed by thread
29+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-8]]
30+
// CHECK: previously allocated by thread
31+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcpy.cpp:[[@LINE-11]]
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2+
// RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
3+
// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
4+
// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
5+
6+
#include <sanitizer/hwasan_interface.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <unistd.h>
10+
11+
__attribute__((no_sanitize("hwaddress"))) void
12+
ForceCallInterceptor(void *p, const void *a, size_t size) {
13+
memmove(p, a, size);
14+
}
15+
16+
int main(int argc, char **argv) {
17+
__hwasan_enable_allocator_tagging();
18+
char a[] = {static_cast<char>(argc), 2, 3, 4};
19+
int size = sizeof(a);
20+
char *volatile p = (char *)malloc(size);
21+
free(p);
22+
ForceCallInterceptor(p, a, size);
23+
return 0;
24+
// CHECK: HWAddressSanitizer: tag-mismatch on address
25+
// CHECK: WRITE of size 4
26+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-4]]
27+
// CHECK: Cause: use-after-free
28+
// CHECK: freed by thread
29+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-8]]
30+
// CHECK: previously allocated by thread
31+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memmove.cpp:[[@LINE-11]]
32+
}

0 commit comments

Comments
 (0)