asan_malloc_mac.cc revision 663c50134e01feefb6c5418c6ec7753be951c14f
1//===-- asan_rtl.cc -------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Mac-specific malloc interception.
13//===----------------------------------------------------------------------===//
14
15#ifdef __APPLE__
16
17#include <AvailabilityMacros.h>
18#include <CoreFoundation/CFBase.h>
19#include <dlfcn.h>
20#include <malloc/malloc.h>
21#include <setjmp.h>
22
23#include "asan_allocator.h"
24#include "asan_interceptors.h"
25#include "asan_internal.h"
26#include "asan_mac.h"
27#include "asan_report.h"
28#include "asan_stack.h"
29
30// Similar code is used in Google Perftools,
31// http://code.google.com/p/google-perftools.
32
33// ---------------------- Replacement functions ---------------- {{{1
34using namespace __asan;  // NOLINT
35
36// TODO(glider): do we need both zones?
37static malloc_zone_t *system_malloc_zone = 0;
38static malloc_zone_t *system_purgeable_zone = 0;
39static malloc_zone_t asan_zone;
40CFAllocatorRef cf_asan = 0;
41
42// The free() implementation provided by OS X calls malloc_zone_from_ptr()
43// to find the owner of |ptr|. If the result is 0, an invalid free() is
44// reported. Our implementation falls back to asan_free() in this case
45// in order to print an ASan-style report.
46//
47// For the objects created by _CFRuntimeCreateInstance a CFAllocatorRef is
48// placed at the beginning of the allocated chunk and the pointer returned by
49// our allocator is off by sizeof(CFAllocatorRef). This pointer can be then
50// passed directly to free(), which will lead to errors.
51// To overcome this we're checking whether |ptr-sizeof(CFAllocatorRef)|
52// contains a pointer to our CFAllocator (assuming no other allocator is used).
53// See http://code.google.com/p/address-sanitizer/issues/detail?id=70 for more
54// info.
55INTERCEPTOR(void, free, void *ptr) {
56  malloc_zone_t *zone = malloc_zone_from_ptr(ptr);
57  if (zone) {
58#if defined(MAC_OS_X_VERSION_10_6) && \
59    MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
60    if ((zone->version >= 6) && (zone->free_definite_size)) {
61      zone->free_definite_size(zone, ptr, malloc_size(ptr));
62    } else {
63      malloc_zone_free(zone, ptr);
64    }
65#else
66    malloc_zone_free(zone, ptr);
67#endif
68  } else {
69    if (flags()->replace_cfallocator) {
70      // Make sure we're not hitting the previous page. This may be incorrect
71      // if ASan's malloc returns an address ending with 0xFF8, which will be
72      // then padded to a page boundary with a CFAllocatorRef.
73      uptr arith_ptr = (uptr)ptr;
74      if ((arith_ptr & 0xFFF) > sizeof(CFAllocatorRef)) {
75        CFAllocatorRef *saved =
76            (CFAllocatorRef*)(arith_ptr - sizeof(CFAllocatorRef));
77        if ((*saved == cf_asan) && asan_mz_size(saved)) ptr = (void*)saved;
78      }
79    }
80    GET_STACK_TRACE_HERE_FOR_FREE(ptr);
81    asan_free(ptr, &stack);
82  }
83}
84
85namespace __asan {
86  void ReplaceCFAllocator();
87}
88
89// We can't always replace the default CFAllocator with cf_asan right in
90// ReplaceSystemMalloc(), because it is sometimes called before
91// __CFInitialize(), when the default allocator is invalid and replacing it may
92// crash the program. Instead we wait for the allocator to initialize and jump
93// in just after __CFInitialize(). Nobody is going to allocate memory using
94// CFAllocators before that, so we won't miss anything.
95//
96// See http://code.google.com/p/address-sanitizer/issues/detail?id=87
97// and http://opensource.apple.com/source/CF/CF-550.43/CFRuntime.c
98INTERCEPTOR(void, __CFInitialize) {
99  CHECK(flags()->replace_cfallocator);
100  CHECK(asan_inited);
101  REAL(__CFInitialize)();
102  if (!cf_asan) ReplaceCFAllocator();
103}
104
105namespace {
106
107// TODO(glider): the mz_* functions should be united with the Linux wrappers,
108// as they are basically copied from there.
109size_t mz_size(malloc_zone_t* zone, const void* ptr) {
110  return asan_mz_size(ptr);
111}
112
113void *mz_malloc(malloc_zone_t *zone, size_t size) {
114  if (!asan_inited) {
115    CHECK(system_malloc_zone);
116    return malloc_zone_malloc(system_malloc_zone, size);
117  }
118  GET_STACK_TRACE_HERE_FOR_MALLOC;
119  return asan_malloc(size, &stack);
120}
121
122void *cf_malloc(CFIndex size, CFOptionFlags hint, void *info) {
123  if (!asan_inited) {
124    CHECK(system_malloc_zone);
125    return malloc_zone_malloc(system_malloc_zone, size);
126  }
127  GET_STACK_TRACE_HERE_FOR_MALLOC;
128  return asan_malloc(size, &stack);
129}
130
131void *mz_calloc(malloc_zone_t *zone, size_t nmemb, size_t size) {
132  if (!asan_inited) {
133    // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
134    const size_t kCallocPoolSize = 1024;
135    static uptr calloc_memory_for_dlsym[kCallocPoolSize];
136    static size_t allocated;
137    size_t size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
138    void *mem = (void*)&calloc_memory_for_dlsym[allocated];
139    allocated += size_in_words;
140    CHECK(allocated < kCallocPoolSize);
141    return mem;
142  }
143  GET_STACK_TRACE_HERE_FOR_MALLOC;
144  return asan_calloc(nmemb, size, &stack);
145}
146
147void *mz_valloc(malloc_zone_t *zone, size_t size) {
148  if (!asan_inited) {
149    CHECK(system_malloc_zone);
150    return malloc_zone_valloc(system_malloc_zone, size);
151  }
152  GET_STACK_TRACE_HERE_FOR_MALLOC;
153  return asan_memalign(kPageSize, size, &stack);
154}
155
156#define GET_ZONE_FOR_PTR(ptr) \
157  malloc_zone_t *zone_ptr = malloc_zone_from_ptr(ptr); \
158  const char *zone_name = (zone_ptr == 0) ? 0 : zone_ptr->zone_name
159
160void ALWAYS_INLINE free_common(void *context, void *ptr) {
161  if (!ptr) return;
162  if (!flags()->mac_ignore_invalid_free || asan_mz_size(ptr)) {
163    GET_STACK_TRACE_HERE_FOR_FREE(ptr);
164    asan_free(ptr, &stack);
165  } else {
166    // Let us just leak this memory for now.
167    GET_STACK_TRACE_HERE_FOR_FREE(ptr);
168    GET_ZONE_FOR_PTR(ptr);
169    WarnMacFreeUnallocated((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
170    return;
171  }
172}
173
174// TODO(glider): the allocation callbacks need to be refactored.
175void mz_free(malloc_zone_t *zone, void *ptr) {
176  free_common(zone, ptr);
177}
178
179void cf_free(void *ptr, void *info) {
180  free_common(info, ptr);
181}
182
183void *mz_realloc(malloc_zone_t *zone, void *ptr, size_t size) {
184  if (!ptr) {
185    GET_STACK_TRACE_HERE_FOR_MALLOC;
186    return asan_malloc(size, &stack);
187  } else {
188    if (asan_mz_size(ptr)) {
189      GET_STACK_TRACE_HERE_FOR_MALLOC;
190      return asan_realloc(ptr, size, &stack);
191    } else {
192      // We can't recover from reallocating an unknown address, because
193      // this would require reading at most |size| bytes from
194      // potentially unaccessible memory.
195      GET_STACK_TRACE_HERE_FOR_FREE(ptr);
196      GET_ZONE_FOR_PTR(ptr);
197      ReportMacMzReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
198    }
199  }
200}
201
202void *cf_realloc(void *ptr, CFIndex size, CFOptionFlags hint, void *info) {
203  if (!ptr) {
204    GET_STACK_TRACE_HERE_FOR_MALLOC;
205    return asan_malloc(size, &stack);
206  } else {
207    if (asan_mz_size(ptr)) {
208      GET_STACK_TRACE_HERE_FOR_MALLOC;
209      return asan_realloc(ptr, size, &stack);
210    } else {
211      // We can't recover from reallocating an unknown address, because
212      // this would require reading at most |size| bytes from
213      // potentially unaccessible memory.
214      GET_STACK_TRACE_HERE_FOR_FREE(ptr);
215      GET_ZONE_FOR_PTR(ptr);
216      ReportMacCfReallocUnknown((uptr)ptr, (uptr)zone_ptr, zone_name, &stack);
217    }
218  }
219}
220
221void mz_destroy(malloc_zone_t* zone) {
222  // A no-op -- we will not be destroyed!
223  AsanPrintf("mz_destroy() called -- ignoring\n");
224}
225  // from AvailabilityMacros.h
226#if defined(MAC_OS_X_VERSION_10_6) && \
227    MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
228void *mz_memalign(malloc_zone_t *zone, size_t align, size_t size) {
229  if (!asan_inited) {
230    CHECK(system_malloc_zone);
231    return malloc_zone_memalign(system_malloc_zone, align, size);
232  }
233  GET_STACK_TRACE_HERE_FOR_MALLOC;
234  return asan_memalign(align, size, &stack);
235}
236
237// This function is currently unused, and we build with -Werror.
238#if 0
239void mz_free_definite_size(malloc_zone_t* zone, void *ptr, size_t size) {
240  // TODO(glider): check that |size| is valid.
241  UNIMPLEMENTED();
242}
243#endif
244#endif
245
246// malloc_introspection callbacks.  I'm not clear on what all of these do.
247kern_return_t mi_enumerator(task_t task, void *,
248                            unsigned type_mask, vm_address_t zone_address,
249                            memory_reader_t reader,
250                            vm_range_recorder_t recorder) {
251  // Should enumerate all the pointers we have.  Seems like a lot of work.
252  return KERN_FAILURE;
253}
254
255size_t mi_good_size(malloc_zone_t *zone, size_t size) {
256  // I think it's always safe to return size, but we maybe could do better.
257  return size;
258}
259
260boolean_t mi_check(malloc_zone_t *zone) {
261  UNIMPLEMENTED();
262  return true;
263}
264
265void mi_print(malloc_zone_t *zone, boolean_t verbose) {
266  UNIMPLEMENTED();
267  return;
268}
269
270void mi_log(malloc_zone_t *zone, void *address) {
271  // I don't think we support anything like this
272}
273
274void mi_force_lock(malloc_zone_t *zone) {
275  asan_mz_force_lock();
276}
277
278void mi_force_unlock(malloc_zone_t *zone) {
279  asan_mz_force_unlock();
280}
281
282// This function is currently unused, and we build with -Werror.
283#if 0
284void mi_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {
285  // TODO(csilvers): figure out how to fill these out
286  // TODO(glider): port this from tcmalloc when ready.
287  stats->blocks_in_use = 0;
288  stats->size_in_use = 0;
289  stats->max_size_in_use = 0;
290  stats->size_allocated = 0;
291}
292#endif
293
294#if defined(MAC_OS_X_VERSION_10_6) && \
295    MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
296boolean_t mi_zone_locked(malloc_zone_t *zone) {
297  // UNIMPLEMENTED();
298  return false;
299}
300#endif
301
302}  // unnamed namespace
303
304extern int __CFRuntimeClassTableSize;
305
306namespace __asan {
307void ReplaceCFAllocator() {
308  static CFAllocatorContext asan_context = {
309        /*version*/ 0, /*info*/ &asan_zone,
310        /*retain*/ 0, /*release*/ 0,
311        /*copyDescription*/0,
312        /*allocate*/ &cf_malloc,
313        /*reallocate*/ &cf_realloc,
314        /*deallocate*/ &cf_free,
315        /*preferredSize*/ 0 };
316  if (!cf_asan)
317    cf_asan = CFAllocatorCreate(kCFAllocatorUseContext, &asan_context);
318  if (CFAllocatorGetDefault() != cf_asan)
319    CFAllocatorSetDefault(cf_asan);
320}
321
322void ReplaceSystemMalloc() {
323  static malloc_introspection_t asan_introspection;
324  // Ok to use internal_memset, these places are not performance-critical.
325  internal_memset(&asan_introspection, 0, sizeof(asan_introspection));
326
327  asan_introspection.enumerator = &mi_enumerator;
328  asan_introspection.good_size = &mi_good_size;
329  asan_introspection.check = &mi_check;
330  asan_introspection.print = &mi_print;
331  asan_introspection.log = &mi_log;
332  asan_introspection.force_lock = &mi_force_lock;
333  asan_introspection.force_unlock = &mi_force_unlock;
334
335  internal_memset(&asan_zone, 0, sizeof(malloc_zone_t));
336
337  // Start with a version 4 zone which is used for OS X 10.4 and 10.5.
338  asan_zone.version = 4;
339  asan_zone.zone_name = "asan";
340  asan_zone.size = &mz_size;
341  asan_zone.malloc = &mz_malloc;
342  asan_zone.calloc = &mz_calloc;
343  asan_zone.valloc = &mz_valloc;
344  asan_zone.free = &mz_free;
345  asan_zone.realloc = &mz_realloc;
346  asan_zone.destroy = &mz_destroy;
347  asan_zone.batch_malloc = 0;
348  asan_zone.batch_free = 0;
349  asan_zone.introspect = &asan_introspection;
350
351  // from AvailabilityMacros.h
352#if defined(MAC_OS_X_VERSION_10_6) && \
353    MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
354  // Switch to version 6 on OSX 10.6 to support memalign.
355  asan_zone.version = 6;
356  asan_zone.free_definite_size = 0;
357  asan_zone.memalign = &mz_memalign;
358  asan_introspection.zone_locked = &mi_zone_locked;
359
360  // Request the default purgable zone to force its creation. The
361  // current default zone is registered with the purgable zone for
362  // doing tiny and small allocs.  Sadly, it assumes that the default
363  // zone is the szone implementation from OS X and will crash if it
364  // isn't.  By creating the zone now, this will be true and changing
365  // the default zone won't cause a problem.  (OS X 10.6 and higher.)
366  system_purgeable_zone = malloc_default_purgeable_zone();
367#endif
368
369  // Register the ASan zone. At this point, it will not be the
370  // default zone.
371  malloc_zone_register(&asan_zone);
372
373  // Unregister and reregister the default zone.  Unregistering swaps
374  // the specified zone with the last one registered which for the
375  // default zone makes the more recently registered zone the default
376  // zone.  The default zone is then re-registered to ensure that
377  // allocations made from it earlier will be handled correctly.
378  // Things are not guaranteed to work that way, but it's how they work now.
379  system_malloc_zone = malloc_default_zone();
380  malloc_zone_unregister(system_malloc_zone);
381  malloc_zone_register(system_malloc_zone);
382  // Make sure the default allocator was replaced.
383  CHECK(malloc_default_zone() == &asan_zone);
384
385  if (flags()->replace_cfallocator) {
386    // If __CFInitialize() hasn't been called yet, cf_asan will be created and
387    // installed as the default allocator after __CFInitialize() finishes (see
388    // the interceptor for __CFInitialize() above). Otherwise install cf_asan
389    // right now. On both Snow Leopard and Lion __CFInitialize() calls
390    // __CFAllocatorInitialize(), which initializes the _base._cfisa field of
391    // the default allocators we check here.
392    if (((CFRuntimeBase*)kCFAllocatorSystemDefault)->_cfisa) {
393      ReplaceCFAllocator();
394    }
395  }
396}
397}  // namespace __asan
398
399#endif  // __APPLE__
400