Skip to content

Commit 07c005c

Browse files
committed
[HWASAN] Implement memcmp interceptor in HWASAN
DON NOT SUBMIT - NEED TO TEST ON ARM
1 parent 836411b commit 07c005c

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

compiler-rt/lib/hwasan/hwasan_interceptors.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@
3131

3232
using namespace __hwasan;
3333

34+
struct HWAsanInterceptorContext {
35+
const char *interceptor_name;
36+
};
37+
38+
# define ACCESS_MEMORY_RANGE(ctx, offset, size, access) \
39+
do { \
40+
__hwasan::CheckAddressSized<ErrorAction::Abort, access>((uptr)offset, \
41+
size); \
42+
} while (0)
43+
44+
#define HWASAN_READ_RANGE(ctx, offset, size) \
45+
ACCESS_MEMORY_RANGE(ctx, offset, size, AccessType::Load)
46+
#define HWASAN_WRITE_RANGE(ctx, offset, size) \
47+
ACCESS_MEMORY_RANGE(ctx, offset, size, AccessType::Store)
48+
3449
# if !SANITIZER_APPLE
3550
# define HWASAN_INTERCEPT_FUNC(name) \
3651
do { \
@@ -79,13 +94,11 @@ using namespace __hwasan;
7994
} while (false)
8095

8196
# define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
82-
do { \
83-
(void)(ctx); \
84-
(void)(ptr); \
85-
(void)(size); \
86-
} while (false)
97+
HWASAN_READ_RANGE(ctx, ptr, size)
8798

8899
# define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
100+
HWAsanInterceptorContext _ctx = {#func}; \
101+
ctx = (void *)&_ctx; \
89102
do { \
90103
(void)(ctx); \
91104
(void)(func); \

compiler-rt/lib/hwasan/hwasan_platform_interceptors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#define SANITIZER_INTERCEPT_MEMCPY 0
6767

6868
#undef SANITIZER_INTERCEPT_MEMCMP
69-
#define SANITIZER_INTERCEPT_MEMCMP 0
69+
#define SANITIZER_INTERCEPT_MEMCMP 1
7070

7171
#undef SANITIZER_INTERCEPT_BCMP
7272
#define SANITIZER_INTERCEPT_BCMP 0

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,13 @@ INTERCEPTOR(char*, textdomain, const char *domainname) {
445445
#define INIT_TEXTDOMAIN
446446
#endif
447447

448-
#if SANITIZER_INTERCEPT_STRCMP
448+
#if SANITIZER_INTERCEPT_STRCMP || SANITIZER_INTERCEPT_MEMCMP
449449
static inline int CharCmpX(unsigned char c1, unsigned char c2) {
450450
return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
451451
}
452+
#endif
452453

454+
#if SANITIZER_INTERCEPT_STRCMP
453455
DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
454456
const char *s1, const char *s2, int result)
455457

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
// REQUIRES: arm
7+
8+
#include <string.h>
9+
int main(int argc, char **argv) {
10+
char a1[] = {static_cast<char>(argc), 2, 3, 4};
11+
char a2[] = {1, static_cast<char>(2*argc), 3, 4};
12+
int res = memcmp(a1, a2, 4 + argc); // BOOM
13+
// CHECK: AddressSanitizer: stack-buffer-overflow
14+
// CHECK: {{#[0-9]+ .*memcmp}}
15+
// CHECK: {{#[0-9]+ .*main}}
16+
return res;
17+
}

0 commit comments

Comments
 (0)