Skip to content

Commit 31c8dca

Browse files
committed
Clean up asan_malloc_win.cpp and add exports
ALLOCATION_FUNCTION_ATTRIBUTE wasn't used elsewhere, and was just one attribute, so abstracting it through a macro wasn't doing much good now that it's not conditional on runtime type. We're always in the dynamic runtime now so eliminate the preprocessor conditional. The new exported functions are the interface used by the intercepted malloc/free family in the instrumented binary to call the asan versions inside the dll runtime.
1 parent ae456a2 commit 31c8dca

File tree

1 file changed

+42
-55
lines changed

1 file changed

+42
-55
lines changed

compiler-rt/lib/asan/asan_malloc_win.cpp

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,97 +58,69 @@ using namespace __asan;
5858
// MD: Memory allocation functions are defined in the CRT .dll,
5959
// so we have to intercept them before they are called for the first time.
6060

61-
#if ASAN_DYNAMIC
62-
# define ALLOCATION_FUNCTION_ATTRIBUTE
63-
#else
64-
# define ALLOCATION_FUNCTION_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
65-
#endif
66-
6761
extern "C" {
68-
ALLOCATION_FUNCTION_ATTRIBUTE
69-
size_t _msize(void *ptr) {
62+
__declspec(noinline) size_t _msize(void *ptr) {
7063
GET_CURRENT_PC_BP_SP;
7164
(void)sp;
7265
return asan_malloc_usable_size(ptr, pc, bp);
7366
}
7467

75-
ALLOCATION_FUNCTION_ATTRIBUTE
76-
size_t _msize_base(void *ptr) {
77-
return _msize(ptr);
78-
}
68+
__declspec(noinline) size_t _msize_base(void *ptr) { return _msize(ptr); }
7969

80-
ALLOCATION_FUNCTION_ATTRIBUTE
81-
void free(void *ptr) {
70+
__declspec(noinline) void free(void *ptr) {
8271
GET_STACK_TRACE_FREE;
8372
return asan_free(ptr, &stack, FROM_MALLOC);
8473
}
8574

86-
ALLOCATION_FUNCTION_ATTRIBUTE
87-
void _free_dbg(void *ptr, int) {
88-
free(ptr);
89-
}
75+
__declspec(noinline) void _free_dbg(void *ptr, int) { free(ptr); }
9076

91-
ALLOCATION_FUNCTION_ATTRIBUTE
92-
void _free_base(void *ptr) {
93-
free(ptr);
94-
}
77+
__declspec(noinline) void _free_base(void *ptr) { free(ptr); }
9578

96-
ALLOCATION_FUNCTION_ATTRIBUTE
97-
void *malloc(size_t size) {
79+
__declspec(noinline) void *malloc(size_t size) {
9880
GET_STACK_TRACE_MALLOC;
9981
return asan_malloc(size, &stack);
10082
}
10183

102-
ALLOCATION_FUNCTION_ATTRIBUTE
103-
void *_malloc_base(size_t size) {
104-
return malloc(size);
105-
}
84+
__declspec(noinline) void *_malloc_base(size_t size) { return malloc(size); }
10685

107-
ALLOCATION_FUNCTION_ATTRIBUTE
108-
void *_malloc_dbg(size_t size, int, const char *, int) {
86+
__declspec(noinline) void *_malloc_dbg(size_t size, int, const char *, int) {
10987
return malloc(size);
11088
}
11189

112-
ALLOCATION_FUNCTION_ATTRIBUTE
113-
void *calloc(size_t nmemb, size_t size) {
90+
__declspec(noinline) void *calloc(size_t nmemb, size_t size) {
11491
GET_STACK_TRACE_MALLOC;
11592
return asan_calloc(nmemb, size, &stack);
11693
}
11794

118-
ALLOCATION_FUNCTION_ATTRIBUTE
119-
void *_calloc_base(size_t nmemb, size_t size) {
95+
__declspec(noinline) void *_calloc_base(size_t nmemb, size_t size) {
12096
return calloc(nmemb, size);
12197
}
12298

123-
ALLOCATION_FUNCTION_ATTRIBUTE
124-
void *_calloc_dbg(size_t nmemb, size_t size, int, const char *, int) {
99+
__declspec(noinline) void *_calloc_dbg(size_t nmemb, size_t size, int,
100+
const char *, int) {
125101
return calloc(nmemb, size);
126102
}
127103

128-
ALLOCATION_FUNCTION_ATTRIBUTE
129-
void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
104+
__declspec(noinline) void *_calloc_impl(size_t nmemb, size_t size,
105+
int *errno_tmp) {
130106
return calloc(nmemb, size);
131107
}
132108

133-
ALLOCATION_FUNCTION_ATTRIBUTE
134-
void *realloc(void *ptr, size_t size) {
109+
__declspec(noinline) void *realloc(void *ptr, size_t size) {
135110
GET_STACK_TRACE_MALLOC;
136111
return asan_realloc(ptr, size, &stack);
137112
}
138113

139-
ALLOCATION_FUNCTION_ATTRIBUTE
140-
void *_realloc_dbg(void *ptr, size_t size, int) {
114+
__declspec(noinline) void *_realloc_dbg(void *ptr, size_t size, int) {
141115
UNREACHABLE("_realloc_dbg should not exist!");
142116
return 0;
143117
}
144118

145-
ALLOCATION_FUNCTION_ATTRIBUTE
146-
void *_realloc_base(void *ptr, size_t size) {
119+
__declspec(noinline) void *_realloc_base(void *ptr, size_t size) {
147120
return realloc(ptr, size);
148121
}
149122

150-
ALLOCATION_FUNCTION_ATTRIBUTE
151-
void *_recalloc(void *p, size_t n, size_t elem_size) {
123+
__declspec(noinline) void *_recalloc(void *p, size_t n, size_t elem_size) {
152124
if (!p)
153125
return calloc(n, elem_size);
154126
const size_t size = n * elem_size;
@@ -166,23 +138,41 @@ void *_recalloc(void *p, size_t n, size_t elem_size) {
166138
return new_alloc;
167139
}
168140

169-
ALLOCATION_FUNCTION_ATTRIBUTE
170-
void *_recalloc_base(void *p, size_t n, size_t elem_size) {
141+
__declspec(noinline) void *_recalloc_base(void *p, size_t n, size_t elem_size) {
171142
return _recalloc(p, n, elem_size);
172143
}
173144

174-
ALLOCATION_FUNCTION_ATTRIBUTE
175-
void *_expand(void *memblock, size_t size) {
145+
__declspec(noinline) void *_expand(void *memblock, size_t size) {
176146
// _expand is used in realloc-like functions to resize the buffer if possible.
177147
// We don't want memory to stand still while resizing buffers, so return 0.
178148
return 0;
179149
}
180150

181-
ALLOCATION_FUNCTION_ATTRIBUTE
182-
void *_expand_dbg(void *memblock, size_t size) {
151+
__declspec(noinline) void *_expand_dbg(void *memblock, size_t size) {
183152
return _expand(memblock, size);
184153
}
185154

155+
__declspec(dllexport) size_t __cdecl __asan_msize(void *ptr) {
156+
return _msize(ptr);
157+
}
158+
__declspec(dllexport) void __cdecl __asan_free(void *const ptr) { free(ptr); }
159+
__declspec(dllexport) void *__cdecl __asan_malloc(const size_t size) {
160+
return malloc(size);
161+
}
162+
__declspec(dllexport) void *__cdecl __asan_calloc(const size_t nmemb,
163+
const size_t size) {
164+
return calloc(nmemb, size);
165+
}
166+
__declspec(dllexport) void *__cdecl __asan_realloc(void *const ptr,
167+
const size_t size) {
168+
return realloc(ptr, size);
169+
}
170+
__declspec(dllexport) void *__cdecl __asan_recalloc(void *const ptr,
171+
const size_t nmemb,
172+
const size_t size) {
173+
return _recalloc(ptr, nmemb, size);
174+
}
175+
186176
// TODO(timurrrr): Might want to add support for _aligned_* allocation
187177
// functions to detect a bit more bugs. Those functions seem to wrap malloc().
188178

@@ -487,7 +477,6 @@ static void TryToOverrideFunction(const char *fname, uptr new_func) {
487477
}
488478

489479
void ReplaceSystemMalloc() {
490-
#if defined(ASAN_DYNAMIC)
491480
TryToOverrideFunction("free", (uptr)free);
492481
TryToOverrideFunction("_free_base", (uptr)free);
493482
TryToOverrideFunction("malloc", (uptr)malloc);
@@ -543,8 +532,6 @@ void ReplaceSystemMalloc() {
543532
// allocation API will be directed to ASan's heap. We don't currently
544533
// intercept all calls to HeapAlloc. If we did, we would have to check on
545534
// HeapFree whether the pointer came from ASan of from the system.
546-
547-
#endif // defined(ASAN_DYNAMIC)
548535
}
549536
} // namespace __asan
550537

0 commit comments

Comments
 (0)