Skip to content

Commit 91cdd7d

Browse files
authored
[HWASAN] Enable memcpy, memmove and memset interceptors (#70387)
1 parent a41b149 commit 91cdd7d

File tree

7 files changed

+125
-40
lines changed

7 files changed

+125
-40
lines changed

compiler-rt/lib/hwasan/hwasan_interceptors.cpp

Lines changed: 1 addition & 26 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,30 +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-
166-
# define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
167-
do { \
168-
(void)(ctx); \
169-
(void)(block); \
170-
(void)(c); \
171-
(void)(size); \
172-
} while (false)
173-
174149
# define COMMON_INTERCEPTOR_STRERROR() \
175150
do { \
176151
} while (false)

compiler-rt/lib/hwasan/hwasan_platform_interceptors.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@
5656
#undef SANITIZER_INTERCEPT_STRCASECMP
5757
#define SANITIZER_INTERCEPT_STRCASECMP 0
5858

59-
#undef SANITIZER_INTERCEPT_MEMSET
60-
#define SANITIZER_INTERCEPT_MEMSET 0
59+
// #undef SANITIZER_INTERCEPT_MEMSET
60+
// #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

compiler-rt/test/hwasan/TestCases/bcmp.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,32 @@
44
// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
55
// REQUIRES: !android
66

7+
#include <assert.h>
78
#include <sanitizer/hwasan_interface.h>
89
#include <stdlib.h>
910
#include <string.h>
1011
#include <unistd.h>
1112

13+
__attribute__((no_sanitize("hwaddress"))) void
14+
ForceCallInterceptor(void *p, const void *a, size_t size) {
15+
assert(bcmp(p, a, size) == 0);
16+
}
17+
1218
int main(int argc, char **argv) {
1319
__hwasan_enable_allocator_tagging();
1420
char a[] = {static_cast<char>(argc), 2, 3, 4};
1521
int size = sizeof(a);
1622
char *p = (char *)malloc(size);
1723
memcpy(p, a, size);
1824
free(p);
19-
return bcmp(p, a, size);
25+
ForceCallInterceptor(p, a, size);
26+
return 0;
2027
// CHECK: HWAddressSanitizer: tag-mismatch on address
2128
// CHECK: READ of size 4
22-
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-3]]
29+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-4]]
2330
// CHECK: Cause: use-after-free
2431
// CHECK: freed by thread
25-
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-7]]
32+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-8]]
2633
// CHECK: previously allocated by thread
27-
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-11]]
34+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-12]]
2835
}

compiler-rt/test/hwasan/TestCases/memcmp.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@
33
// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
44
// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
55

6+
#include <assert.h>
67
#include <sanitizer/hwasan_interface.h>
78
#include <stdlib.h>
89
#include <string.h>
910
#include <unistd.h>
1011

12+
__attribute__((no_sanitize("hwaddress"))) void
13+
ForceCallInterceptor(void *p, const void *a, size_t size) {
14+
assert(memcmp(p, a, size) == 0);
15+
}
16+
1117
int main(int argc, char **argv) {
1218
__hwasan_enable_allocator_tagging();
1319
char a[] = {static_cast<char>(argc), 2, 3, 4};
1420
int size = sizeof(a);
1521
char *p = (char *)malloc(size);
1622
memcpy(p, a, size);
1723
free(p);
18-
return memcmp(p, a, size);
24+
ForceCallInterceptor(p, a, size);
25+
return 0;
1926
// CHECK: HWAddressSanitizer: tag-mismatch on address
2027
// CHECK: READ of size 4
21-
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-3]]
28+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-4]]
2229
// CHECK: Cause: use-after-free
2330
// CHECK: freed by thread
24-
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-7]]
31+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-8]]
2532
// CHECK: previously allocated by thread
26-
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-11]]
33+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-12]]
2734
}
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+
}
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, int c, size_t size) {
13+
memset(p, c, size) == nullptr;
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, 0, size);
23+
return 0;
24+
// CHECK: HWAddressSanitizer: tag-mismatch on address
25+
// CHECK: WRITE of size 4
26+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset.cpp:[[@LINE-4]]
27+
// CHECK: Cause: use-after-free
28+
// CHECK: freed by thread
29+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset.cpp:[[@LINE-8]]
30+
// CHECK: previously allocated by thread
31+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset.cpp:[[@LINE-11]]
32+
}

0 commit comments

Comments
 (0)