Skip to content

Commit 712ae4c

Browse files
committed
adding few missing calls.
1 parent 350009d commit 712ae4c

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

compiler-rt/lib/asan/asan_malloc_win.cpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ __declspec(noinline) void *_aligned_malloc(size_t size, size_t alignment) {
147147
return asan_aligned_alloc(alignment, size, &stack);
148148
}
149149

150+
__declspec(noinline) void *_aligned_malloc_dbg(size_t size, size_t alignment) {
151+
return _aligned_malloc(alignment, size);
152+
}
153+
150154
__declspec(noinline) void *_aligned_realloc(void *p, size_t size,
151155
size_t alignment) {
152156
GET_STACK_TRACE_MALLOC;
@@ -160,7 +164,55 @@ __declspec(noinline) void *_aligned_realloc(void *p, size_t size,
160164
return n;
161165
}
162166

163-
__declspec(noinline) void _aligned_free(void *p) { free(p); }
167+
__declspec(noinline) void *_aligned_realloc_dbg(void *p, size_t size,
168+
size_t alignment) {
169+
return _aligned_realloc(p, size, alignment);
170+
}
171+
172+
__declspec(noinline) void *_aligned_recalloc(void *p, size_t nmemb, size_t size,
173+
size_t alignment) {
174+
const size_t total = nmemb * size;
175+
if (total && total / size != nmemb)
176+
return nullptr;
177+
void *n = _aligned_realloc(p, total, alignment);
178+
if (n)
179+
REAL(memset)(n, 0, size);
180+
181+
return n;
182+
}
183+
184+
__declspec(noinline) void *_aligned_recalloc_dbg(void *p, size_t nmemb,
185+
size_t size,
186+
size_t alignment) {
187+
return _aligned_recalloc(p, nmemb, size, alignment);
188+
}
189+
190+
__declspec(noinline) void *_aligned_offset_malloc(size_t size, size_t alignment,
191+
size_t offset) {
192+
const size_t total = offset + size;
193+
if (total && (total - offset) != size)
194+
return nullptr;
195+
void *p = _aligned_malloc(total, alignment);
196+
if (p)
197+
return ((u8 *)p) + offset;
198+
199+
return nullptr;
200+
}
201+
202+
__declspec(noinline) void *_aligned_offset_malloc_dbg(size_t size,
203+
size_t alignment,
204+
size_t offset) {
205+
return _aligned_offset_malloc(size, alignment, offset);
206+
}
207+
208+
__declspec(noinline) void _aligned_free(void *p) {
209+
void *b = const_cast<void *>(
210+
__sanitizer_get_allocated_begin(const_cast<void *>(p)));
211+
CHECK(b != nullptr && "invalid pointer");
212+
free(b);
213+
}
214+
215+
__declspec(noinline) void _aligned_free_dbg(void *p) { _aligned_free(p); }
164216

165217
__declspec(noinline) size_t _aligned_msize(void *p) {
166218
GET_CURRENT_PC_BP_SP;
@@ -169,6 +221,10 @@ __declspec(noinline) size_t _aligned_msize(void *p) {
169221
return asan_malloc_usable_size(p, pc, bp);
170222
}
171223

224+
__declspec(noinline) size_t _aligned_msize_dbg(void *p) {
225+
return _aligned_msize(p);
226+
}
227+
172228
__declspec(noinline) void *_expand(void *memblock, size_t size) {
173229
// _expand is used in realloc-like functions to resize the buffer if possible.
174230
// We don't want memory to stand still while resizing buffers, so return 0.
@@ -531,8 +587,17 @@ void ReplaceSystemMalloc() {
531587
TryToOverrideFunction("_expand_base", (uptr)_expand);
532588
TryToOverrideFunction("_aligned_malloc", (uptr)_aligned_malloc);
533589
TryToOverrideFunction("_aligned_realloc", (uptr)_aligned_realloc);
590+
TryToOverrideFunction("_aligned_recalloc", (uptr)_aligned_recalloc);
534591
TryToOverrideFunction("_aligned_free", (uptr)_aligned_free);
535592
TryToOverrideFunction("_aligned_msize", (uptr)_aligned_msize);
593+
TryToOverrideFunction("_aligned_malloc_dbg", (uptr)_aligned_malloc_dbg);
594+
TryToOverrideFunction("_aligned_realloc_dbg", (uptr)_aligned_realloc_dbg);
595+
TryToOverrideFunction("_aligned_recalloc_dbg", (uptr)_aligned_recalloc_dbg);
596+
TryToOverrideFunction("_aligned_free_dbg", (uptr)_aligned_free_dbg);
597+
TryToOverrideFunction("_aligned_msize_dbg", (uptr)_aligned_msize_dbg);
598+
TryToOverrideFunction("_aligned_offset_malloc", (uptr)_aligned_offset_malloc);
599+
TryToOverrideFunction("_aligned_offset_malloc_dbg",
600+
(uptr)_aligned_offset_malloc_dbg);
536601

537602
if (flags()->windows_hook_rtl_allocators) {
538603
ASAN_INTERCEPT_FUNC(HeapSize);

compiler-rt/lib/asan/asan_win_static_runtime_thunk.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ INTERCEPT_LIBRARY_FUNCTION_ASAN(strstr);
6565
INTERCEPT_LIBRARY_FUNCTION_ASAN(strtok);
6666
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcslen);
6767
INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsnlen);
68-
INTERCEPT_LIBRARY_FUNCTION_ASAN(_aligned_malloc);
69-
INTERCEPT_LIBRARY_FUNCTION_ASAN(_aligned_realloc);
70-
INTERCEPT_LIBRARY_FUNCTION_ASAN(_aligned_free);
71-
INTERCEPT_LIBRARY_FUNCTION_ASAN(_aligned_msize);
7268

7369
// Note: Don't intercept strtol(l). They are supposed to set errno for out-of-
7470
// range values, but since the ASan runtime is linked against the dynamic CRT,

compiler-rt/test/asan/TestCases/Windows/aligned_mallocs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(void) {
3939
_aligned_free(u);
4040

4141
char *t = (char *)_aligned_malloc(128, 8);
42-
t[-1] = 'a';
42+
t[-153] = 'a';
4343
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
4444
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
4545

0 commit comments

Comments
 (0)