Skip to content

Commit 4d9f3ca

Browse files
[HWASAN] Add memset interceptor (llvm#71244)
Co-authored-by: Vitaly Buka <[email protected]>
1 parent 9832eb4 commit 4d9f3ca

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

compiler-rt/lib/hwasan/hwasan_interceptors.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "hwasan.h"
2020
#include "hwasan_allocator.h"
2121
#include "hwasan_checks.h"
22+
#include "hwasan_mapping.h"
2223
#include "hwasan_platform_interceptors.h"
2324
#include "hwasan_thread.h"
2425
#include "hwasan_thread_list.h"
@@ -146,13 +147,16 @@ struct HWAsanInterceptorContext {
146147
(void)(name); \
147148
} while (false)
148149

149-
# define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
150-
do { \
151-
(void)(ctx); \
152-
(void)(block); \
153-
(void)(c); \
154-
(void)(size); \
155-
} while (false)
150+
# define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
151+
{ \
152+
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
153+
return internal_memset(dst, v, size); \
154+
COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
155+
if (MemIsApp(UntagAddr(reinterpret_cast<uptr>(dst))) && \
156+
common_flags()->intercept_intrin) \
157+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
158+
return REAL(memset)(dst, v, size); \
159+
}
156160

157161
# define COMMON_INTERCEPTOR_STRERROR() \
158162
do { \

compiler-rt/lib/hwasan/hwasan_platform_interceptors.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
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

6262
// #undef SANITIZER_INTERCEPT_MEMMOVE
6363
// #define SANITIZER_INTERCEPT_MEMMOVE 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, 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)