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