Skip to content

Commit b44305b

Browse files
committed
[compiler-rt][asan] _aligned_malloc/_aligned_free interception.
1 parent 62cb1a3 commit b44305b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

compiler-rt/lib/asan/asan_malloc_win.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ __declspec(noinline) void *_recalloc_base(void *p, size_t n, size_t elem_size) {
142142
return _recalloc(p, n, elem_size);
143143
}
144144

145+
__declspec(noinline) void *_aligned_malloc(size_t alignment, size_t size) {
146+
GET_STACK_TRACE_MALLOC;
147+
return asan_aligned_alloc(alignment, size, &stack);
148+
}
149+
150+
__declspec(noinline) void *_aligned_realloc(void *p, size_t alignment,
151+
size_t size) {
152+
GET_STACK_TRACE_MALLOC;
153+
void *n = asan_aligned_alloc(alignment, size, &stack);
154+
if (n) {
155+
size_t osize = _msize(p);
156+
REAL(memcpy)(n, p, Min<size_t>(osize, size));
157+
free(p);
158+
}
159+
160+
return n;
161+
}
162+
163+
__declspec(noinline) void *_aligned_free(void *p) { free(p); }
164+
145165
__declspec(noinline) void *_expand(void *memblock, size_t size) {
146166
// _expand is used in realloc-like functions to resize the buffer if possible.
147167
// We don't want memory to stand still while resizing buffers, so return 0.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cl_asan %Od %s %Fe%t
2+
// RUN: %run %t
3+
4+
#include <windows.h>
5+
6+
#define CHECK_ALIGNED(ptr, alignment) \
7+
do { \
8+
if (((uintptr_t)(ptr) % (alignment)) != 0) \
9+
return __LINE__; \
10+
} while (0)
11+
12+
int main(void) {
13+
char *p = reinterpret_cast<char *>(_aligned_malloc(16, 8);
14+
CHECK_ALIGNED(p, 8);
15+
char *n = reinterpret_cast<char *>(_aligned_realloc(32, 16));
16+
CHECK_ALIGNED(n, 16);
17+
_aligned_free(n);
18+
19+
return 0;
20+
}

0 commit comments

Comments
 (0)