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