asan_mac.cc revision 2483ce3e635515d907c0cd8c97db315142fb28db
11e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//===-- asan_mac.cc -------------------------------------------------------===// 21e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 31e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The LLVM Compiler Infrastructure 41e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 51e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// This file is distributed under the University of Illinois Open Source 61e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// License. See LICENSE.TXT for details. 71e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 81e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//===----------------------------------------------------------------------===// 91e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 101e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// This file is a part of AddressSanitizer, an address sanity checker. 111e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 121e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// Mac-specific details. 131e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//===----------------------------------------------------------------------===// 141e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 15d6567c5166412f6acdde851e767c26f332d51d3dKostya Serebryany#ifdef __APPLE__ 161e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 1764ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov#include "asan_interceptors.h" 181e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#include "asan_internal.h" 19d079db6dfbf3b0ec5fa1cc8d093e0dae6f970bf8Alexander Potapenko#include "asan_mac.h" 20895b3872acb5bcccb1769ea69d37dd33c722f99dAlexander Potapenko#include "asan_mapping.h" 211e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#include "asan_stack.h" 221e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#include "asan_thread.h" 231e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#include "asan_thread_registry.h" 24ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#include "sanitizer_common/sanitizer_libc.h" 251e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 261e316d7f488a75312539629e9d937e156280eeb6Alexander Potapenko#include <crt_externs.h> // for _NSGetEnviron 278a34d384255f9bf4c2a9b03a4df81b9af57124d8Alexander Potapenko#include <mach-o/dyld.h> 289b993e8cd0f8964782ee93524603d0c53adc2249Kostya Serebryany#include <mach-o/loader.h> 291e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#include <sys/mman.h> 30ef14ff6512d7b2e20aa3206dff820b5f90285420Kostya Serebryany#include <sys/resource.h> 3159dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko#include <sys/sysctl.h> 329107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany#include <sys/ucontext.h> 33a874fe5b5d67152e4e737498d532eec80940bdcdKostya Serebryany#include <fcntl.h> 34e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko#include <pthread.h> 35e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko#include <stdlib.h> // for free() 361e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#include <unistd.h> 37d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany#include <libkern/OSAtomic.h> 3864ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov#include <CoreFoundation/CFString.h> 391e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 401e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanynamespace __asan { 411e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 423f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryanyvoid GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { 439107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany ucontext_t *ucontext = (ucontext_t*)context; 449107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany# if __WORDSIZE == 64 459107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany *pc = ucontext->uc_mcontext->__ss.__rip; 469107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany *bp = ucontext->uc_mcontext->__ss.__rbp; 479107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany *sp = ucontext->uc_mcontext->__ss.__rsp; 489107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany# else 499107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany *pc = ucontext->uc_mcontext->__ss.__eip; 509107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany *bp = ucontext->uc_mcontext->__ss.__ebp; 519107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany *sp = ucontext->uc_mcontext->__ss.__esp; 529107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany# endif // __WORDSIZE 539107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany} 549107c26bd88fc9cf44a2cd7d6967eb830ac63be3Kostya Serebryany 55d079db6dfbf3b0ec5fa1cc8d093e0dae6f970bf8Alexander Potapenkoint GetMacosVersion() { 5659dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko int mib[2] = { CTL_KERN, KERN_OSRELEASE }; 5759dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko char version[100]; 583f46cf42b0f8e0667a3bea88cf871ebd4dc0ecdfAlexey Samsonov uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]); 59b0bb7fb31301ee9ac9cf41f21d3a19987dc30609Alexey Samsonov for (uptr i = 0; i < maxlen; i++) version[i] = '\0'; 6059dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko // Get the version length. 613f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany CHECK(sysctl(mib, 2, 0, &len, 0, 0) != -1); 6259dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko CHECK(len < maxlen); 633f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany CHECK(sysctl(mib, 2, version, &len, 0, 0) != -1); 6459dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko switch (version[0]) { 6559dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko case '9': return MACOS_VERSION_LEOPARD; 6659dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko case '1': { 6759dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko switch (version[1]) { 6859dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko case '0': return MACOS_VERSION_SNOW_LEOPARD; 6959dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko case '1': return MACOS_VERSION_LION; 7059dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko default: return MACOS_VERSION_UNKNOWN; 7159dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko } 7259dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko } 7359dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko default: return MACOS_VERSION_UNKNOWN; 7459dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko } 7559dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko} 7659dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko 7738dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonovbool PlatformHasDifferentMemcpyAndMemmove() { 7838dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov // On OS X 10.7 memcpy() and memmove() are both resolved 7938dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov // into memmove$VARIANT$sse42. 8038dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov // See also http://code.google.com/p/address-sanitizer/issues/detail?id=34. 8138dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov // TODO(glider): need to check dynamically that memcpy() and memmove() are 8238dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov // actually the same function. 8338dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov return GetMacosVersion() == MACOS_VERSION_SNOW_LEOPARD; 8438dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov} 8538dd4ed885e714c376466f6fe0d69f5f22d37014Alexey Samsonov 861e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// No-op. Mac does not support static linkage anyway. 871e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyvoid *AsanDoesNotSupportStaticLinkage() { 883f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany return 0; 891e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} 901e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 914803ab90ead451b55a5833f0fd38b10fd1fc83ebKostya Serebryanybool AsanInterceptsSignal(int signum) { 92cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov return (signum == SIGSEGV || signum == SIGBUS) && flags()->handle_segv; 934803ab90ead451b55a5833f0fd38b10fd1fc83ebKostya Serebryany} 944803ab90ead451b55a5833f0fd38b10fd1fc83ebKostya Serebryany 9575b19ebf25af204cf209d108997272822241d6daAlexander Potapenkovoid AsanPlatformThreadInit() { 9675b19ebf25af204cf209d108997272822241d6daAlexander Potapenko ReplaceCFAllocator(); 9775b19ebf25af204cf209d108997272822241d6daAlexander Potapenko} 9875b19ebf25af204cf209d108997272822241d6daAlexander Potapenko 99d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya SerebryanyAsanLock::AsanLock(LinkerInitialized) { 100d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany // We assume that OS_SPINLOCK_INIT is zero 101d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany} 102d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany 103d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryanyvoid AsanLock::Lock() { 104d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_)); 105d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany CHECK(OS_SPINLOCK_INIT == 0); 1063f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany CHECK(owner_ != (uptr)pthread_self()); 107d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany OSSpinLockLock((OSSpinLock*)&opaque_storage_); 108d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany CHECK(!owner_); 1093f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany owner_ = (uptr)pthread_self(); 110d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany} 111d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany 112d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryanyvoid AsanLock::Unlock() { 1133f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany CHECK(owner_ == (uptr)pthread_self()); 114d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany owner_ = 0; 115d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); 116d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany} 117d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany 1183f46cf42b0f8e0667a3bea88cf871ebd4dc0ecdfAlexey Samsonovvoid AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) { 1199cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov size = 0; 1209cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov trace[0] = pc; 1219cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov if ((max_s) > 1) { 1229cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov max_size = max_s; 1239cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov FastUnwindStack(pc, bp); 1249cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov } 1259cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov} 1269cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov 1275b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// The range of pages to be used for escape islands. 1283281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko// TODO(glider): instead of mapping a fixed range we must find a range of 1293281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko// unmapped pages in vmmap and take them. 1301346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko// These constants were chosen empirically and may not work if the shadow 1311346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko// memory layout changes. Unfortunately they do necessarily depend on 1321346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko// kHighMemBeg or kHighMemEnd. 1333f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryanystatic void *island_allocator_pos = 0; 1345b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov 1351346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko#if __WORDSIZE == 32 1365b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandEnd (0xffdf0000 - kPageSize) 1375b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandBeg (kIslandEnd - 256 * kPageSize) 1381346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko#else 1395b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandEnd (0x7fffffdf0000 - kPageSize) 1405b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandBeg (kIslandEnd - 256 * kPageSize) 1411346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko#endif 1423281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko 1433281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenkoextern "C" 1445b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonovmach_error_t __interception_allocate_island(void **ptr, 1453f46cf42b0f8e0667a3bea88cf871ebd4dc0ecdfAlexey Samsonov uptr unused_size, 1465b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov void *unused_hint) { 1473281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko if (!island_allocator_pos) { 1481346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko island_allocator_pos = 149ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov internal_mmap((void*)kIslandBeg, kIslandEnd - kIslandBeg, 150ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov PROT_READ | PROT_WRITE | PROT_EXEC, 151ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov MAP_PRIVATE | MAP_ANON | MAP_FIXED, 152ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov -1, 0); 1531346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko if (island_allocator_pos != (void*)kIslandBeg) { 1543281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko return KERN_NO_SPACE; 1553281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko } 156cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity) { 157ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko Report("Mapped pages %p--%p for branch islands.\n", 158675293d458c9f6c797a7211c11438eb9bfcfe9bbAlexey Samsonov (void*)kIslandBeg, (void*)kIslandEnd); 159ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko } 160ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko // Should not be very performance-critical. 161ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko internal_memset(island_allocator_pos, 0xCC, kIslandEnd - kIslandBeg); 1623281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko }; 1633281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko *ptr = island_allocator_pos; 1643281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko island_allocator_pos = (char*)island_allocator_pos + kPageSize; 165cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity) { 166ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko Report("Branch island allocated at %p\n", *ptr); 167ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko } 1683281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko return err_none; 1693281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko} 1703281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko 1713281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenkoextern "C" 1725b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonovmach_error_t __interception_deallocate_island(void *ptr) { 1733281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko // Do nothing. 1743281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko // TODO(glider): allow to free and reuse the island memory. 1753281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko return err_none; 1763281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko} 177d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany 1781e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// Support for the following functions from libdispatch on Mac OS: 1791e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_async_f() 1801e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_async() 1811e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_sync_f() 1821e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_sync() 1831e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_after_f() 1841e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_after() 1851e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_group_async_f() 1861e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_group_async() 1871e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// TODO(glider): libdispatch API contains other functions that we don't support 1881e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// yet. 1891e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 1901e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_sync() and dispatch_sync_f() are synchronous, although chances are 1911e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// they can cause jobs to run on a thread different from the current one. 1921e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// TODO(glider): if so, we need a test for this (otherwise we should remove 1931e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// them). 1941e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 1951e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The following functions use dispatch_barrier_async_f() (which isn't a library 1961e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// function but is exported) and are thus supported: 1971e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_source_set_cancel_handler_f() 1981e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_source_set_cancel_handler() 1991e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_source_set_event_handler_f() 2001e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_source_set_event_handler() 2011e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// 2021e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The reference manual for Grand Central Dispatch is available at 2031e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html 2041e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The implementation details are at 2051e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c 2061e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 2075cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef void* pthread_workqueue_t; 2085cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef void* pthread_workitem_handle_t; 209f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov 210f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovtypedef void* dispatch_group_t; 211f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovtypedef void* dispatch_queue_t; 212ee3925515e4c7966f3ef489f687aa7e5692806a9Kostya Serebryanytypedef u64 dispatch_time_t; 213f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovtypedef void (*dispatch_function_t)(void *block); 2145cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef void* (*worker_t)(void *block); 2155cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov 2165cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov// A wrapper for the ObjC blocks used to support libdispatch. 2175cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef struct { 2185cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov void *block; 2195cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov dispatch_function_t func; 220e0cff0bc20ae51790c8edfbceb817e18ebf5355eKostya Serebryany u32 parent_tid; 2215cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov} asan_block_context_t; 2225cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov 223f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// We use extern declarations of libdispatch functions here instead 224f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// of including <dispatch/dispatch.h>. This header is not present on 225f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// Mac OS X Leopard and eariler, and although we don't expect ASan to 226f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// work on legacy systems, it's bad to break the build of 227f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// LLVM compiler-rt there. 2285cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovextern "C" { 229f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_async_f(dispatch_queue_t dq, void *ctxt, 230f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov dispatch_function_t func); 231f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_sync_f(dispatch_queue_t dq, void *ctxt, 232f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov dispatch_function_t func); 233f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_after_f(dispatch_time_t when, dispatch_queue_t dq, void *ctxt, 234f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov dispatch_function_t func); 2355cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovvoid dispatch_barrier_async_f(dispatch_queue_t dq, void *ctxt, 2365cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov dispatch_function_t func); 237f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_group_async_f(dispatch_group_t group, dispatch_queue_t dq, 238f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov void *ctxt, dispatch_function_t func); 2395cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovint pthread_workqueue_additem_np(pthread_workqueue_t workq, 2405cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov void *(*workitem_func)(void *), void * workitem_arg, 2415cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov pthread_workitem_handle_t * itemhandlep, unsigned int *gencountp); 2425cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov} // extern "C" 2435cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov 2442483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// For use by only those functions that allocated the context via 2452483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// alloc_asan_context(). 2461e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyextern "C" 2471e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyvoid asan_dispatch_call_block_and_release(void *block) { 2489cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov GET_STACK_TRACE_HERE(kStackTraceMax); 2491e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_block_context_t *context = (asan_block_context_t*)block; 250cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity >= 2) { 2511e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany Report("asan_dispatch_call_block_and_release(): " 2521e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany "context: %p, pthread_self: %p\n", 2531e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany block, pthread_self()); 2541e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany } 2551e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany AsanThread *t = asanThreadRegistry().GetCurrent(); 256af3441580555ceed092170232cd5f2cc180f19f4Kostya Serebryany if (!t) { 2573f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany t = AsanThread::Create(context->parent_tid, 0, 0, &stack); 25855cdfc6c5af92560bc0623b5a0d70af71511c3c8Alexey Samsonov asanThreadRegistry().RegisterThread(t); 25969eca73ac96688c8bfe1f23ee006af29c7858c40Kostya Serebryany t->Init(); 2601e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asanThreadRegistry().SetCurrent(t); 2611e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany } 2621e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany // Call the original dispatcher for the block. 2631e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany context->func(context->block); 2641e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_free(context, &stack); 2651e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} 2661e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 2671e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} // namespace __asan 2681e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 2691e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyusing namespace __asan; // NOLINT 2701e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 2711e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// Wrap |ctxt| and |func| into an asan_block_context_t. 2721e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The caller retains control of the allocated context. 2731e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyextern "C" 2741e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyasan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func, 2751e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany AsanStackTrace *stack) { 2761e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_block_context_t *asan_ctxt = 2771e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack); 2781e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_ctxt->block = ctxt; 2791e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_ctxt->func = func; 280e0cff0bc20ae51790c8edfbceb817e18ebf5355eKostya Serebryany asan_ctxt->parent_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); 2811e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany return asan_ctxt; 2821e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} 2831e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 284b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko// Define interceptor for dispatch_*_f function with the three most common 285b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko// parameters: dispatch_queue_t, context, dispatch_function_t. 286b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko#define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \ 287b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \ 288b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko dispatch_function_t func) { \ 289b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko GET_STACK_TRACE_HERE(kStackTraceMax); \ 290b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \ 291b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko if (flags()->verbosity >= 2) { \ 292b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n", \ 293b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko asan_ctxt, pthread_self()); \ 294b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko PRINT_CURRENT_STACK(); \ 295b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko } \ 296b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \ 297b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko asan_dispatch_call_block_and_release); \ 298b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko } 299b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander Potapenko 300b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander PotapenkoINTERCEPT_DISPATCH_X_F_3(dispatch_async_f) 301b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander PotapenkoINTERCEPT_DISPATCH_X_F_3(dispatch_sync_f) 302b09dd34786713a150fed5c5ab1529f01de0e2bc0Alexander PotapenkoINTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f) 3031e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 304f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, 305f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov dispatch_queue_t dq, void *ctxt, 306f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov dispatch_function_t func) { 3079cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov GET_STACK_TRACE_HERE(kStackTraceMax); 3081e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); 309cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity >= 2) { 3101e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany Report("dispatch_after_f: %p\n", asan_ctxt); 3111e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany PRINT_CURRENT_STACK(); 3121e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany } 31309672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt, 31409672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov asan_dispatch_call_block_and_release); 3151e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} 3161e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 3172483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko#if MAC_INTERPOSE_FUNCTIONS 3182483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// dispatch_async and TODO tailcall the corresponding dispatch_*_f functions. 3192483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// When wrapping functions with mach_override, they are intercepted 3202483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// automatically. But with dylib interposition this does not work, because the 3212483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// calls within the same library are not interposed. 3222483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// Therefore we need to re-implement dispatch_async and friends. 3232483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko 3242483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// See dispatch/dispatch.h. 3252483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko#define DISPATCH_TIME_FOREVER (~0ull) 3262483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenkotypedef void (^dispatch_block_t)(void); 3272483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko 3282483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// See 3292483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// http://www.opensource.apple.com/source/libdispatch/libdispatch-228.18/src/init.c 3302483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// for the implementation of _dispatch_call_block_copy_and_release(). 3312483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenkostatic void _dispatch_call_block_and_release(void *block) { 3322483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko void (^b)(void) = (dispatch_block_t)block; 3332483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko b(); 3342483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko _Block_release(b); 3352483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko} 3362483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko 3372483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// See 3382483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// http://www.opensource.apple.com/source/libdispatch/libdispatch-228.18/src/internal.h 3392483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko#define fastpath(x) ((typeof(x))__builtin_expect((long)(x), ~0l)) 3402483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko 3412483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// See 3422483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// http://www.opensource.apple.com/source/libdispatch/libdispatch-228.18/src/init.c 3432483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenkostatic dispatch_block_t _dispatch_Block_copy(dispatch_block_t db) { 3442483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko dispatch_block_t rval; 3452483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko if (fastpath(db)) { 3462483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko while (!fastpath(rval = Block_copy(db))) { 3472483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko sleep(1); 3482483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko } 3492483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko return rval; 3502483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko } 3512483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko CHECK(0 && "NULL was passed where a block should have been"); 3522483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko return (dispatch_block_t)NULL; // Unreachable. 3532483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko} 3542483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko 3552483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// See 3562483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// http://www.opensource.apple.com/source/libdispatch/libdispatch-228.18/src/queue.c 3572483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// for the implementation of dispatch_async(), dispatch_sync(), 3582483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko// dispatch_after(). 3592483ce3e635515d907c0cd8c97db315142fb28dbAlexander PotapenkoINTERCEPTOR(void, dispatch_async, 3602483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko dispatch_queue_t dq, dispatch_block_t work) { 3612483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko WRAP(dispatch_async_f)(dq, _dispatch_Block_copy(work), 3622483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko _dispatch_call_block_and_release); 3632483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko} 3642483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko 3652483ce3e635515d907c0cd8c97db315142fb28dbAlexander PotapenkoINTERCEPTOR(void, dispatch_after, 3662483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko dispatch_time_t when, dispatch_queue_t queue, 3672483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko dispatch_block_t work) { 3682483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko if (when == DISPATCH_TIME_FOREVER) { 3692483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko CHECK(0 && "dispatch_after() called with 'when' == infinity"); 3702483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko return; // Unreachable. 3712483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko } 3722483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko WRAP(dispatch_after_f)(when, queue, _dispatch_Block_copy(work), 3732483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko _dispatch_call_block_and_release); 3742483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko} 3752483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko#endif 3762483ce3e635515d907c0cd8c97db315142fb28dbAlexander Potapenko 377f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group, 378f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov dispatch_queue_t dq, void *ctxt, 379f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov dispatch_function_t func) { 3809cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov GET_STACK_TRACE_HERE(kStackTraceMax); 3811e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); 382cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity >= 2) { 3831e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n", 3841e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_ctxt, pthread_self()); 3851e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany PRINT_CURRENT_STACK(); 3861e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany } 38709672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt, 38809672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov asan_dispatch_call_block_and_release); 3891e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} 3901e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 3911e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The following stuff has been extremely helpful while looking for the 3921e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// unhandled functions that spawned jobs on Chromium shutdown. If the verbosity 3931e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// level is 2 or greater, we wrap pthread_workqueue_additem_np() in order to 3941e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// find the points of worker thread creation (each of such threads may be used 3951e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// to run several tasks, that's why this is not enough to support the whole 3961e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// libdispatch API. 3971e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyextern "C" 3981e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyvoid *wrap_workitem_func(void *arg) { 399cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity >= 2) { 4001e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany Report("wrap_workitem_func: %p, pthread_self: %p\n", arg, pthread_self()); 4011e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany } 4021e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_block_context_t *ctxt = (asan_block_context_t*)arg; 4031e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany worker_t fn = (worker_t)(ctxt->func); 4041e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany void *result = fn(ctxt->block); 4059cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov GET_STACK_TRACE_HERE(kStackTraceMax); 4061e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_free(arg, &stack); 4071e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany return result; 4081e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} 4091e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany 410f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(int, pthread_workqueue_additem_np, pthread_workqueue_t workq, 4111e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany void *(*workitem_func)(void *), void * workitem_arg, 4121e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany pthread_workitem_handle_t * itemhandlep, unsigned int *gencountp) { 4139cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov GET_STACK_TRACE_HERE(kStackTraceMax); 4141e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_block_context_t *asan_ctxt = 4151e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), &stack); 4161e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_ctxt->block = workitem_arg; 4171e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany asan_ctxt->func = (dispatch_function_t)workitem_func; 418e0cff0bc20ae51790c8edfbceb817e18ebf5355eKostya Serebryany asan_ctxt->parent_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); 419cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity >= 2) { 4201e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany Report("pthread_workqueue_additem_np: %p\n", asan_ctxt); 4211e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany PRINT_CURRENT_STACK(); 4221e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany } 42309672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov return REAL(pthread_workqueue_additem_np)(workq, wrap_workitem_func, 42409672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov asan_ctxt, itemhandlep, 42509672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov gencountp); 4261e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany} 427d6567c5166412f6acdde851e767c26f332d51d3dKostya Serebryany 428431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko// See http://opensource.apple.com/source/CF/CF-635.15/CFString.c 429431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenkoint __CFStrIsConstant(CFStringRef str) { 430431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko CFRuntimeBase *base = (CFRuntimeBase*)str; 431431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#if __LP64__ 432431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko return base->_rc == 0; 433431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#else 434431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko return (base->_cfinfo[CF_RC_BITS]) == 0; 435431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#endif 436431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko} 437431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko 438f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(CFStringRef, CFStringCreateCopy, CFAllocatorRef alloc, 439f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov CFStringRef str) { 440431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko if (__CFStrIsConstant(str)) { 441431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko return str; 442431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko } else { 44309672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov return REAL(CFStringCreateCopy)(alloc, str); 444431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko } 445431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko} 446431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko 447decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander PotapenkoDECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr) 448decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko 449decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander PotapenkoDECLARE_REAL_AND_INTERCEPTOR(void, __CFInitialize) 450decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko 45164ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonovnamespace __asan { 452beba6448539095b67cab266d09cd7b7d313b8c3dAlexey Samsonov 453beba6448539095b67cab266d09cd7b7d313b8c3dAlexey Samsonovvoid InitializeMacInterceptors() { 4545cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov CHECK(INTERCEPT_FUNCTION(dispatch_async_f)); 4555cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov CHECK(INTERCEPT_FUNCTION(dispatch_sync_f)); 4565cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov CHECK(INTERCEPT_FUNCTION(dispatch_after_f)); 4575cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov CHECK(INTERCEPT_FUNCTION(dispatch_barrier_async_f)); 4585cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov CHECK(INTERCEPT_FUNCTION(dispatch_group_async_f)); 4595cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov // We don't need to intercept pthread_workqueue_additem_np() to support the 4605cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov // libdispatch API, but it helps us to debug the unsupported functions. Let's 4615cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov // intercept it only during verbose runs. 462cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->verbosity >= 2) { 4635cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov CHECK(INTERCEPT_FUNCTION(pthread_workqueue_additem_np)); 4645cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov } 46564ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov // Normally CFStringCreateCopy should not copy constant CF strings. 46664ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov // Replacing the default CFAllocator causes constant strings to be copied 46764ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov // rather than just returned, which leads to bugs in big applications like 46864ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov // Chromium and WebKit, see 46964ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov // http://code.google.com/p/address-sanitizer/issues/detail?id=10 47064ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov // Until this problem is fixed we need to check that the string is 47164ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov // non-constant before calling CFStringCreateCopy. 47264ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov CHECK(INTERCEPT_FUNCTION(CFStringCreateCopy)); 473e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko // Some of the library functions call free() directly, so we have to 474e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko // intercept it. 475e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko CHECK(INTERCEPT_FUNCTION(free)); 476cb8c4dce691097718d5af41b36899b72ef4b1d84Alexey Samsonov if (flags()->replace_cfallocator) { 477decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko CHECK(INTERCEPT_FUNCTION(__CFInitialize)); 478decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko } 47964ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov} 480beba6448539095b67cab266d09cd7b7d313b8c3dAlexey Samsonov 48164ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov} // namespace __asan 48264ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov 483d6567c5166412f6acdde851e767c26f332d51d3dKostya Serebryany#endif // __APPLE__ 484