@@ -58,97 +58,69 @@ using namespace __asan;
58
58
// MD: Memory allocation functions are defined in the CRT .dll,
59
59
// so we have to intercept them before they are called for the first time.
60
60
61
- #if ASAN_DYNAMIC
62
- # define ALLOCATION_FUNCTION_ATTRIBUTE
63
- #else
64
- # define ALLOCATION_FUNCTION_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
65
- #endif
66
-
67
61
extern " C" {
68
- ALLOCATION_FUNCTION_ATTRIBUTE
69
- size_t _msize (void *ptr) {
62
+ __declspec (noinline) size_t _msize(void *ptr) {
70
63
GET_CURRENT_PC_BP_SP;
71
64
(void )sp;
72
65
return asan_malloc_usable_size (ptr, pc, bp);
73
66
}
74
67
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); }
79
69
80
- ALLOCATION_FUNCTION_ATTRIBUTE
81
- void free (void *ptr) {
70
+ __declspec (noinline) void free(void *ptr) {
82
71
GET_STACK_TRACE_FREE;
83
72
return asan_free (ptr, &stack, FROM_MALLOC);
84
73
}
85
74
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); }
90
76
91
- ALLOCATION_FUNCTION_ATTRIBUTE
92
- void _free_base (void *ptr) {
93
- free (ptr);
94
- }
77
+ __declspec (noinline) void _free_base(void *ptr) { free (ptr); }
95
78
96
- ALLOCATION_FUNCTION_ATTRIBUTE
97
- void *malloc (size_t size) {
79
+ __declspec (noinline) void *malloc(size_t size) {
98
80
GET_STACK_TRACE_MALLOC;
99
81
return asan_malloc (size, &stack);
100
82
}
101
83
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); }
106
85
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 ) {
109
87
return malloc (size);
110
88
}
111
89
112
- ALLOCATION_FUNCTION_ATTRIBUTE
113
- void *calloc (size_t nmemb, size_t size) {
90
+ __declspec (noinline) void *calloc(size_t nmemb, size_t size) {
114
91
GET_STACK_TRACE_MALLOC;
115
92
return asan_calloc (nmemb, size, &stack);
116
93
}
117
94
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) {
120
96
return calloc (nmemb, size);
121
97
}
122
98
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 ) {
125
101
return calloc (nmemb, size);
126
102
}
127
103
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) {
130
106
return calloc (nmemb, size);
131
107
}
132
108
133
- ALLOCATION_FUNCTION_ATTRIBUTE
134
- void *realloc (void *ptr, size_t size) {
109
+ __declspec (noinline) void *realloc(void *ptr, size_t size) {
135
110
GET_STACK_TRACE_MALLOC;
136
111
return asan_realloc (ptr, size, &stack);
137
112
}
138
113
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 ) {
141
115
UNREACHABLE (" _realloc_dbg should not exist!" );
142
116
return 0 ;
143
117
}
144
118
145
- ALLOCATION_FUNCTION_ATTRIBUTE
146
- void *_realloc_base (void *ptr, size_t size) {
119
+ __declspec (noinline) void *_realloc_base(void *ptr, size_t size) {
147
120
return realloc (ptr, size);
148
121
}
149
122
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) {
152
124
if (!p)
153
125
return calloc (n, elem_size);
154
126
const size_t size = n * elem_size;
@@ -166,23 +138,41 @@ void *_recalloc(void *p, size_t n, size_t elem_size) {
166
138
return new_alloc;
167
139
}
168
140
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) {
171
142
return _recalloc (p, n, elem_size);
172
143
}
173
144
174
- ALLOCATION_FUNCTION_ATTRIBUTE
175
- void *_expand (void *memblock, size_t size) {
145
+ __declspec (noinline) void *_expand(void *memblock, size_t size) {
176
146
// _expand is used in realloc-like functions to resize the buffer if possible.
177
147
// We don't want memory to stand still while resizing buffers, so return 0.
178
148
return 0 ;
179
149
}
180
150
181
- ALLOCATION_FUNCTION_ATTRIBUTE
182
- void *_expand_dbg (void *memblock, size_t size) {
151
+ __declspec (noinline) void *_expand_dbg(void *memblock, size_t size) {
183
152
return _expand (memblock, size);
184
153
}
185
154
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
+
186
176
// TODO(timurrrr): Might want to add support for _aligned_* allocation
187
177
// functions to detect a bit more bugs. Those functions seem to wrap malloc().
188
178
@@ -487,7 +477,6 @@ static void TryToOverrideFunction(const char *fname, uptr new_func) {
487
477
}
488
478
489
479
void ReplaceSystemMalloc () {
490
- #if defined(ASAN_DYNAMIC)
491
480
TryToOverrideFunction (" free" , (uptr)free);
492
481
TryToOverrideFunction (" _free_base" , (uptr)free);
493
482
TryToOverrideFunction (" malloc" , (uptr)malloc);
@@ -543,8 +532,6 @@ void ReplaceSystemMalloc() {
543
532
// allocation API will be directed to ASan's heap. We don't currently
544
533
// intercept all calls to HeapAlloc. If we did, we would have to check on
545
534
// HeapFree whether the pointer came from ASan of from the system.
546
-
547
- #endif // defined(ASAN_DYNAMIC)
548
535
}
549
536
} // namespace __asan
550
537
0 commit comments