1 //===-- sanitizer_malloc_mac.inc --------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file contains Mac-specific malloc interceptors and a custom zone
9 // implementation, which together replace the system allocator.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
15 #error "This file should only be compiled on Darwin."
18 #include <AvailabilityMacros.h>
19 #include <CoreFoundation/CFBase.h>
21 #include <malloc/malloc.h>
24 #include "interception/interception.h"
25 #include "sanitizer_common/sanitizer_mac.h"
27 // Similar code is used in Google Perftools,
28 // https://github.com/gperftools/gperftools.
30 namespace __sanitizer {
31 extern malloc_zone_t sanitizer_zone;
34 INTERCEPTOR(malloc_zone_t *, malloc_create_zone,
35 vm_size_t start_size, unsigned zone_flags) {
36 COMMON_MALLOC_ENTER();
37 uptr page_size = GetPageSizeCached();
38 uptr allocated_size = RoundUpTo(sizeof(sanitizer_zone), page_size);
39 COMMON_MALLOC_MEMALIGN(page_size, allocated_size);
40 malloc_zone_t *new_zone = (malloc_zone_t *)p;
41 internal_memcpy(new_zone, &sanitizer_zone, sizeof(sanitizer_zone));
42 new_zone->zone_name = NULL; // The name will be changed anyway.
43 if (GetMacosVersion() >= MACOS_VERSION_LION) {
44 // Prevent the client app from overwriting the zone contents.
45 // Library functions that need to modify the zone will set PROT_WRITE on it.
46 // This matches the behavior of malloc_create_zone() on OSX 10.7 and higher.
47 mprotect(new_zone, allocated_size, PROT_READ);
49 // We're explicitly *NOT* registering the zone.
53 INTERCEPTOR(void, malloc_destroy_zone, malloc_zone_t *zone) {
54 COMMON_MALLOC_ENTER();
55 // We don't need to do anything here. We're not registering new zones, so we
56 // don't to unregister. Just un-mprotect and free() the zone.
57 if (GetMacosVersion() >= MACOS_VERSION_LION) {
58 uptr page_size = GetPageSizeCached();
59 uptr allocated_size = RoundUpTo(sizeof(sanitizer_zone), page_size);
60 mprotect(zone, allocated_size, PROT_READ | PROT_WRITE);
62 if (zone->zone_name) {
63 COMMON_MALLOC_FREE((void *)zone->zone_name);
65 COMMON_MALLOC_FREE(zone);
68 INTERCEPTOR(malloc_zone_t *, malloc_default_zone, void) {
69 COMMON_MALLOC_ENTER();
70 return &sanitizer_zone;
73 INTERCEPTOR(malloc_zone_t *, malloc_default_purgeable_zone, void) {
74 // FIXME: ASan should support purgeable allocations.
75 // https://github.com/google/sanitizers/issues/139
76 COMMON_MALLOC_ENTER();
77 return &sanitizer_zone;
80 INTERCEPTOR(void, malloc_make_purgeable, void *ptr) {
81 // FIXME: ASan should support purgeable allocations. Ignoring them is fine
83 COMMON_MALLOC_ENTER();
86 INTERCEPTOR(int, malloc_make_nonpurgeable, void *ptr) {
87 // FIXME: ASan should support purgeable allocations. Ignoring them is fine
89 COMMON_MALLOC_ENTER();
90 // Must return 0 if the contents were not purged since the last call to
91 // malloc_make_purgeable().
95 INTERCEPTOR(void, malloc_set_zone_name, malloc_zone_t *zone, const char *name) {
96 COMMON_MALLOC_ENTER();
97 // Allocate |sizeof(COMMON_MALLOC_ZONE_NAME "-") + internal_strlen(name)|
100 sizeof(COMMON_MALLOC_ZONE_NAME "-") + (name ? internal_strlen(name) : 0);
101 InternalScopedString new_name(buflen);
102 if (name && zone->introspect == sanitizer_zone.introspect) {
103 new_name.append(COMMON_MALLOC_ZONE_NAME "-%s", name);
104 name = new_name.data();
107 // Call the system malloc's implementation for both external and our zones,
108 // since that appropriately changes VM region protections on the zone.
109 REAL(malloc_set_zone_name)(zone, name);
112 INTERCEPTOR(void *, malloc, size_t size) {
113 COMMON_MALLOC_ENTER();
114 COMMON_MALLOC_MALLOC(size);
118 INTERCEPTOR(void, free, void *ptr) {
119 COMMON_MALLOC_ENTER();
121 COMMON_MALLOC_FREE(ptr);
124 INTERCEPTOR(void *, realloc, void *ptr, size_t size) {
125 COMMON_MALLOC_ENTER();
126 COMMON_MALLOC_REALLOC(ptr, size);
130 INTERCEPTOR(void *, calloc, size_t nmemb, size_t size) {
131 COMMON_MALLOC_ENTER();
132 COMMON_MALLOC_CALLOC(nmemb, size);
136 INTERCEPTOR(void *, valloc, size_t size) {
137 COMMON_MALLOC_ENTER();
138 COMMON_MALLOC_VALLOC(size);
142 INTERCEPTOR(size_t, malloc_good_size, size_t size) {
143 COMMON_MALLOC_ENTER();
144 return sanitizer_zone.introspect->good_size(&sanitizer_zone, size);
147 INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
148 COMMON_MALLOC_ENTER();
150 COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size);
156 // TODO(glider): the __sanitizer_mz_* functions should be united with the Linux
157 // wrappers, as they are basically copied from there.
159 SANITIZER_INTERFACE_ATTRIBUTE
160 size_t __sanitizer_mz_size(malloc_zone_t* zone, const void* ptr) {
161 COMMON_MALLOC_SIZE(ptr);
166 SANITIZER_INTERFACE_ATTRIBUTE
167 void *__sanitizer_mz_malloc(malloc_zone_t *zone, uptr size) {
168 COMMON_MALLOC_ENTER();
169 COMMON_MALLOC_MALLOC(size);
174 SANITIZER_INTERFACE_ATTRIBUTE
175 void *__sanitizer_mz_calloc(malloc_zone_t *zone, size_t nmemb, size_t size) {
176 if (UNLIKELY(!COMMON_MALLOC_SANITIZER_INITIALIZED)) {
177 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
178 const size_t kCallocPoolSize = 1024;
179 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
180 static size_t allocated;
181 size_t size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
182 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
183 allocated += size_in_words;
184 CHECK(allocated < kCallocPoolSize);
187 COMMON_MALLOC_CALLOC(nmemb, size);
192 SANITIZER_INTERFACE_ATTRIBUTE
193 void *__sanitizer_mz_valloc(malloc_zone_t *zone, size_t size) {
194 COMMON_MALLOC_ENTER();
195 COMMON_MALLOC_VALLOC(size);
199 // TODO(glider): the allocation callbacks need to be refactored.
201 SANITIZER_INTERFACE_ATTRIBUTE
202 void __sanitizer_mz_free(malloc_zone_t *zone, void *ptr) {
204 COMMON_MALLOC_FREE(ptr);
207 #define GET_ZONE_FOR_PTR(ptr) \
208 malloc_zone_t *zone_ptr = malloc_zone_from_ptr(ptr); \
209 const char *zone_name = (zone_ptr == 0) ? 0 : zone_ptr->zone_name
212 SANITIZER_INTERFACE_ATTRIBUTE
213 void *__sanitizer_mz_realloc(malloc_zone_t *zone, void *ptr, size_t new_size) {
215 COMMON_MALLOC_MALLOC(new_size);
218 COMMON_MALLOC_SIZE(ptr);
220 COMMON_MALLOC_REALLOC(ptr, new_size);
223 // We can't recover from reallocating an unknown address, because
224 // this would require reading at most |new_size| bytes from
225 // potentially unaccessible memory.
226 GET_ZONE_FOR_PTR(ptr);
227 COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name);
234 SANITIZER_INTERFACE_ATTRIBUTE
235 void __sanitizer_mz_destroy(malloc_zone_t* zone) {
236 // A no-op -- we will not be destroyed!
237 Report("__sanitizer_mz_destroy() called -- ignoring\n");
241 SANITIZER_INTERFACE_ATTRIBUTE
242 void *__sanitizer_mz_memalign(malloc_zone_t *zone, size_t align, size_t size) {
243 COMMON_MALLOC_ENTER();
244 COMMON_MALLOC_MEMALIGN(align, size);
248 // This function is currently unused, and we build with -Werror.
250 void __sanitizer_mz_free_definite_size(
251 malloc_zone_t* zone, void *ptr, size_t size) {
252 // TODO(glider): check that |size| is valid.
257 kern_return_t mi_enumerator(task_t task, void *,
258 unsigned type_mask, vm_address_t zone_address,
259 memory_reader_t reader,
260 vm_range_recorder_t recorder) {
261 // Should enumerate all the pointers we have. Seems like a lot of work.
265 size_t mi_good_size(malloc_zone_t *zone, size_t size) {
266 // I think it's always safe to return size, but we maybe could do better.
270 boolean_t mi_check(malloc_zone_t *zone) {
274 void mi_print(malloc_zone_t *zone, boolean_t verbose) {
278 void mi_log(malloc_zone_t *zone, void *address) {
279 // I don't think we support anything like this
282 void mi_force_lock(malloc_zone_t *zone) {
283 COMMON_MALLOC_FORCE_LOCK();
286 void mi_force_unlock(malloc_zone_t *zone) {
287 COMMON_MALLOC_FORCE_UNLOCK();
290 void mi_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {
291 COMMON_MALLOC_FILL_STATS(zone, stats);
294 boolean_t mi_zone_locked(malloc_zone_t *zone) {
299 } // unnamed namespace
301 namespace COMMON_MALLOC_NAMESPACE {
303 void ReplaceSystemMalloc() {
304 static malloc_introspection_t sanitizer_zone_introspection;
305 // Ok to use internal_memset, these places are not performance-critical.
306 internal_memset(&sanitizer_zone_introspection, 0,
307 sizeof(sanitizer_zone_introspection));
309 sanitizer_zone_introspection.enumerator = &mi_enumerator;
310 sanitizer_zone_introspection.good_size = &mi_good_size;
311 sanitizer_zone_introspection.check = &mi_check;
312 sanitizer_zone_introspection.print = &mi_print;
313 sanitizer_zone_introspection.log = &mi_log;
314 sanitizer_zone_introspection.force_lock = &mi_force_lock;
315 sanitizer_zone_introspection.force_unlock = &mi_force_unlock;
316 sanitizer_zone_introspection.statistics = &mi_statistics;
317 sanitizer_zone_introspection.zone_locked = &mi_zone_locked;
319 internal_memset(&sanitizer_zone, 0, sizeof(malloc_zone_t));
321 // Use version 6 for OSX >= 10.6.
322 sanitizer_zone.version = 6;
323 sanitizer_zone.zone_name = COMMON_MALLOC_ZONE_NAME;
324 sanitizer_zone.size = &__sanitizer_mz_size;
325 sanitizer_zone.malloc = &__sanitizer_mz_malloc;
326 sanitizer_zone.calloc = &__sanitizer_mz_calloc;
327 sanitizer_zone.valloc = &__sanitizer_mz_valloc;
328 sanitizer_zone.free = &__sanitizer_mz_free;
329 sanitizer_zone.realloc = &__sanitizer_mz_realloc;
330 sanitizer_zone.destroy = &__sanitizer_mz_destroy;
331 sanitizer_zone.batch_malloc = 0;
332 sanitizer_zone.batch_free = 0;
333 sanitizer_zone.free_definite_size = 0;
334 sanitizer_zone.memalign = &__sanitizer_mz_memalign;
335 sanitizer_zone.introspect = &sanitizer_zone_introspection;
337 // Register the zone.
338 malloc_zone_register(&sanitizer_zone);
341 } // namespace COMMON_MALLOC_NAMESPACE