Skip to content

Commit 46c1671

Browse files
[HWASAN]Implement memcmp interceptor in HWASAN (#67204)
The plan is to fix memcmp interceptor in HWASAN and remove the unsupported statement at that time. --------- Co-authored-by: Vitaly Buka <[email protected]>
1 parent a3af099 commit 46c1671

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
#undef SANITIZER_INTERCEPT_MEMCPY
6666
#define SANITIZER_INTERCEPT_MEMCPY 0
6767

68-
#undef SANITIZER_INTERCEPT_MEMCMP
69-
#define SANITIZER_INTERCEPT_MEMCMP 0
68+
// #undef SANITIZER_INTERCEPT_MEMCMP
69+
// #define SANITIZER_INTERCEPT_MEMCMP 0
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
int main(int argc, char **argv) {
12+
__hwasan_enable_allocator_tagging();
13+
char a[] = {static_cast<char>(argc), 2, 3, 4};
14+
volatile int size = sizeof(a);
15+
char *volatile p = (char *)malloc(size);
16+
memcpy(p, a, size);
17+
free(p);
18+
return memcmp(p, a, size);
19+
// CHECK: HWAddressSanitizer: tag-mismatch on address
20+
// CHECK: READ of size 4
21+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-3]]
22+
// CHECK: Cause: use-after-free
23+
// CHECK: freed by thread
24+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-7]]
25+
// CHECK: previously allocated by thread
26+
// CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-11]]
27+
}

0 commit comments

Comments
 (0)