asan_mac.cc revision d079db6dfbf3b0ec5fa1cc8d093e0dae6f970bf8
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]);
5959dc578df0de177b44c8c78f69d73735e38e5c14Alexander Potapenko  for (int 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) {
924803ab90ead451b55a5833f0fd38b10fd1fc83ebKostya Serebryany  return (signum == SIGSEGV || signum == SIGBUS) && FLAG_handle_segv;
934803ab90ead451b55a5833f0fd38b10fd1fc83ebKostya Serebryany}
944803ab90ead451b55a5833f0fd38b10fd1fc83ebKostya Serebryany
95d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya SerebryanyAsanLock::AsanLock(LinkerInitialized) {
96d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany  // We assume that OS_SPINLOCK_INIT is zero
97d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany}
98d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany
99d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryanyvoid AsanLock::Lock() {
100d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany  CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
101d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany  CHECK(OS_SPINLOCK_INIT == 0);
1023f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany  CHECK(owner_ != (uptr)pthread_self());
103d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany  OSSpinLockLock((OSSpinLock*)&opaque_storage_);
104d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany  CHECK(!owner_);
1053f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany  owner_ = (uptr)pthread_self();
106d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany}
107d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany
108d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryanyvoid AsanLock::Unlock() {
1093f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany  CHECK(owner_ == (uptr)pthread_self());
110d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany  owner_ = 0;
111d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany  OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
112d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany}
113d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany
1143f46cf42b0f8e0667a3bea88cf871ebd4dc0ecdfAlexey Samsonovvoid AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
1159cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  size = 0;
1169cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  trace[0] = pc;
1179cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  if ((max_s) > 1) {
1189cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov    max_size = max_s;
1199cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov    FastUnwindStack(pc, bp);
1209cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  }
1219cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov}
1229cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov
1235b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov// The range of pages to be used for escape islands.
1243281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko// TODO(glider): instead of mapping a fixed range we must find a range of
1253281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko// unmapped pages in vmmap and take them.
1261346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko// These constants were chosen empirically and may not work if the shadow
1271346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko// memory layout changes. Unfortunately they do necessarily depend on
1281346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko// kHighMemBeg or kHighMemEnd.
1293f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryanystatic void *island_allocator_pos = 0;
1305b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov
1311346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko#if __WORDSIZE == 32
1325b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandEnd (0xffdf0000 - kPageSize)
1335b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandBeg (kIslandEnd - 256 * kPageSize)
1341346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko#else
1355b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandEnd (0x7fffffdf0000 - kPageSize)
1365b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov# define kIslandBeg (kIslandEnd - 256 * kPageSize)
1371346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko#endif
1383281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko
1393281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenkoextern "C"
1405b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonovmach_error_t __interception_allocate_island(void **ptr,
1413f46cf42b0f8e0667a3bea88cf871ebd4dc0ecdfAlexey Samsonov                                            uptr unused_size,
1425b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonov                                            void *unused_hint) {
1433281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  if (!island_allocator_pos) {
1441346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko    island_allocator_pos =
145ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov        internal_mmap((void*)kIslandBeg, kIslandEnd - kIslandBeg,
146ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov                      PROT_READ | PROT_WRITE | PROT_EXEC,
147ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov                      MAP_PRIVATE | MAP_ANON | MAP_FIXED,
148ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov                      -1, 0);
1491346ced2eb8d10305e8d98496d9006cfbbad1548Alexander Potapenko    if (island_allocator_pos != (void*)kIslandBeg) {
1503281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko      return KERN_NO_SPACE;
1513281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko    }
152ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko    if (FLAG_v) {
153ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko      Report("Mapped pages %p--%p for branch islands.\n",
154675293d458c9f6c797a7211c11438eb9bfcfe9bbAlexey Samsonov             (void*)kIslandBeg, (void*)kIslandEnd);
155ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko    }
156ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko    // Should not be very performance-critical.
157ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko    internal_memset(island_allocator_pos, 0xCC, kIslandEnd - kIslandBeg);
1583281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  };
1593281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  *ptr = island_allocator_pos;
1603281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  island_allocator_pos = (char*)island_allocator_pos + kPageSize;
161ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko  if (FLAG_v) {
162ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko    Report("Branch island allocated at %p\n", *ptr);
163ebb9702cff96192c6a6ea963037929ca7ed60eaeAlexander Potapenko  }
1643281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  return err_none;
1653281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko}
1663281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko
1673281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenkoextern "C"
1685b29018cf422e7711fb760b733c32127397a43fcAlexey Samsonovmach_error_t __interception_deallocate_island(void *ptr) {
1693281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  // Do nothing.
1703281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  // TODO(glider): allow to free and reuse the island memory.
1713281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko  return err_none;
1723281209790b5e543c79acb2f5008d1df77fb76d9Alexander Potapenko}
173d55f5f8c413622db4bd28b5cca9bfeb4d61564e0Kostya Serebryany
1741e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// Support for the following functions from libdispatch on Mac OS:
1751e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_async_f()
1761e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_async()
1771e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_sync_f()
1781e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_sync()
1791e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_after_f()
1801e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_after()
1811e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_group_async_f()
1821e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_group_async()
1831e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// TODO(glider): libdispatch API contains other functions that we don't support
1841e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// yet.
1851e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
1861e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
1871e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// they can cause jobs to run on a thread different from the current one.
1881e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// TODO(glider): if so, we need a test for this (otherwise we should remove
1891e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// them).
1901e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
1911e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The following functions use dispatch_barrier_async_f() (which isn't a library
1921e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// function but is exported) and are thus supported:
1931e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_source_set_cancel_handler_f()
1941e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_source_set_cancel_handler()
1951e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_source_set_event_handler_f()
1961e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   dispatch_source_set_event_handler()
1971e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
1981e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The reference manual for Grand Central Dispatch is available at
1991e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
2001e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The implementation details are at
2011e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//   http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
2021e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
2035cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef void* pthread_workqueue_t;
2045cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef void* pthread_workitem_handle_t;
205f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov
206f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovtypedef void* dispatch_group_t;
207f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovtypedef void* dispatch_queue_t;
208ee3925515e4c7966f3ef489f687aa7e5692806a9Kostya Serebryanytypedef u64 dispatch_time_t;
209f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovtypedef void (*dispatch_function_t)(void *block);
2105cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef void* (*worker_t)(void *block);
2115cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov
2125cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov// A wrapper for the ObjC blocks used to support libdispatch.
2135cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovtypedef struct {
2145cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  void *block;
2155cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  dispatch_function_t func;
216e0cff0bc20ae51790c8edfbceb817e18ebf5355eKostya Serebryany  u32 parent_tid;
2175cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov} asan_block_context_t;
2185cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov
219f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// We use extern declarations of libdispatch functions here instead
220f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// of including <dispatch/dispatch.h>. This header is not present on
221f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// Mac OS X Leopard and eariler, and although we don't expect ASan to
222f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// work on legacy systems, it's bad to break the build of
223f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov// LLVM compiler-rt there.
2245cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovextern "C" {
225f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_async_f(dispatch_queue_t dq, void *ctxt,
226f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov                      dispatch_function_t func);
227f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_sync_f(dispatch_queue_t dq, void *ctxt,
228f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov                     dispatch_function_t func);
229f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_after_f(dispatch_time_t when, dispatch_queue_t dq, void *ctxt,
230f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov                      dispatch_function_t func);
2315cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovvoid dispatch_barrier_async_f(dispatch_queue_t dq, void *ctxt,
2325cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov                              dispatch_function_t func);
233f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonovvoid dispatch_group_async_f(dispatch_group_t group, dispatch_queue_t dq,
234f7ceaad2919d2e26e9edea29232bc9dd8f145c42Alexey Samsonov                            void *ctxt, dispatch_function_t func);
2355cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonovint pthread_workqueue_additem_np(pthread_workqueue_t workq,
2365cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov    void *(*workitem_func)(void *), void * workitem_arg,
2375cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov    pthread_workitem_handle_t * itemhandlep, unsigned int *gencountp);
2385cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov}  // extern "C"
2395cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov
2401e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyextern "C"
2411e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyvoid asan_dispatch_call_block_and_release(void *block) {
2429cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
2431e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *context = (asan_block_context_t*)block;
2441e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
2451e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("asan_dispatch_call_block_and_release(): "
2461e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany           "context: %p, pthread_self: %p\n",
2471e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany           block, pthread_self());
2481e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
2491e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  AsanThread *t = asanThreadRegistry().GetCurrent();
250af3441580555ceed092170232cd5f2cc180f19f4Kostya Serebryany  if (!t) {
2513f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany    t = AsanThread::Create(context->parent_tid, 0, 0, &stack);
25255cdfc6c5af92560bc0623b5a0d70af71511c3c8Alexey Samsonov    asanThreadRegistry().RegisterThread(t);
25369eca73ac96688c8bfe1f23ee006af29c7858c40Kostya Serebryany    t->Init();
2541e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    asanThreadRegistry().SetCurrent(t);
2551e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
2561e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  // Call the original dispatcher for the block.
2571e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  context->func(context->block);
2581e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_free(context, &stack);
2591e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
2601e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
2611e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}  // namespace __asan
2621e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
2631e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyusing namespace __asan;  // NOLINT
2641e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
2651e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// Wrap |ctxt| and |func| into an asan_block_context_t.
2661e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The caller retains control of the allocated context.
2671e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyextern "C"
2681e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyasan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
2691e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany                                         AsanStackTrace *stack) {
2701e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *asan_ctxt =
2711e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany      (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
2721e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_ctxt->block = ctxt;
2731e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_ctxt->func = func;
274e0cff0bc20ae51790c8edfbceb817e18ebf5355eKostya Serebryany  asan_ctxt->parent_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
2751e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  return asan_ctxt;
2761e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
2771e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
2781e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// TODO(glider): can we reduce code duplication by introducing a macro?
279f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(void, dispatch_async_f, dispatch_queue_t dq, void *ctxt,
280f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                    dispatch_function_t func) {
2819cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
2821e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
2831e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
2841e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("dispatch_async_f(): context: %p, pthread_self: %p\n",
2851e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany        asan_ctxt, pthread_self());
2861e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    PRINT_CURRENT_STACK();
2871e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
28809672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov  return REAL(dispatch_async_f)(dq, (void*)asan_ctxt,
28909672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov                                asan_dispatch_call_block_and_release);
2901e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
2911e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
292f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(void, dispatch_sync_f, dispatch_queue_t dq, void *ctxt,
293f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                   dispatch_function_t func) {
2949cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
2951e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
2961e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
2971e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("dispatch_sync_f(): context: %p, pthread_self: %p\n",
2981e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany        asan_ctxt, pthread_self());
2991e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    PRINT_CURRENT_STACK();
3001e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
30109672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov  return REAL(dispatch_sync_f)(dq, (void*)asan_ctxt,
30209672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov                               asan_dispatch_call_block_and_release);
3031e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
3041e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
305f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
306f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                    dispatch_queue_t dq, void *ctxt,
307f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                    dispatch_function_t func) {
3089cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
3091e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
3101e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
3111e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("dispatch_after_f: %p\n", asan_ctxt);
3121e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    PRINT_CURRENT_STACK();
3131e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
31409672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov  return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt,
31509672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov                                asan_dispatch_call_block_and_release);
3161e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
3171e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
318f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(void, dispatch_barrier_async_f, dispatch_queue_t dq, void *ctxt,
319f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                            dispatch_function_t func) {
3209cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
3211e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
3221e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
3231e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("dispatch_barrier_async_f(): context: %p, pthread_self: %p\n",
3241e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany           asan_ctxt, pthread_self());
3251e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    PRINT_CURRENT_STACK();
3261e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
32709672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov  REAL(dispatch_barrier_async_f)(dq, (void*)asan_ctxt,
32809672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov                                 asan_dispatch_call_block_and_release);
3291e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
3301e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
331f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
332f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                          dispatch_queue_t dq, void *ctxt,
333f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                          dispatch_function_t func) {
3349cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
3351e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
3361e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
3371e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
3381e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany           asan_ctxt, pthread_self());
3391e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    PRINT_CURRENT_STACK();
3401e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
34109672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov  REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt,
34209672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov                               asan_dispatch_call_block_and_release);
3431e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
3441e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
3451e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// The following stuff has been extremely helpful while looking for the
3461e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// unhandled functions that spawned jobs on Chromium shutdown. If the verbosity
3471e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// level is 2 or greater, we wrap pthread_workqueue_additem_np() in order to
3481e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// find the points of worker thread creation (each of such threads may be used
3491e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// to run several tasks, that's why this is not enough to support the whole
3501e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// libdispatch API.
3511e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyextern "C"
3521e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryanyvoid *wrap_workitem_func(void *arg) {
3531e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
3541e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("wrap_workitem_func: %p, pthread_self: %p\n", arg, pthread_self());
3551e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
3561e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *ctxt = (asan_block_context_t*)arg;
3571e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  worker_t fn = (worker_t)(ctxt->func);
3581e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  void *result =  fn(ctxt->block);
3599cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
3601e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_free(arg, &stack);
3611e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  return result;
3621e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
3631e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
364f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(int, pthread_workqueue_additem_np, pthread_workqueue_t workq,
3651e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    void *(*workitem_func)(void *), void * workitem_arg,
3661e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    pthread_workitem_handle_t * itemhandlep, unsigned int *gencountp) {
3679cfa194cc62026fc7c6e82f7303eee8ad4d10cf4Evgeniy Stepanov  GET_STACK_TRACE_HERE(kStackTraceMax);
3681e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_block_context_t *asan_ctxt =
3691e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany      (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), &stack);
3701e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_ctxt->block = workitem_arg;
3711e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  asan_ctxt->func = (dispatch_function_t)workitem_func;
372e0cff0bc20ae51790c8edfbceb817e18ebf5355eKostya Serebryany  asan_ctxt->parent_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
3731e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  if (FLAG_v >= 2) {
3741e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    Report("pthread_workqueue_additem_np: %p\n", asan_ctxt);
3751e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany    PRINT_CURRENT_STACK();
3761e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany  }
37709672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov  return REAL(pthread_workqueue_additem_np)(workq, wrap_workitem_func,
37809672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov                                            asan_ctxt, itemhandlep,
37909672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov                                            gencountp);
3801e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany}
381d6567c5166412f6acdde851e767c26f332d51d3dKostya Serebryany
382431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko// CF_RC_BITS, the layout of CFRuntimeBase and __CFStrIsConstant are internal
383431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko// and subject to change in further CoreFoundation versions. Apple does not
384431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko// guarantee any binary compatibility from release to release.
385431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko
386431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko// See http://opensource.apple.com/source/CF/CF-635.15/CFInternal.h
387431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#if defined(__BIG_ENDIAN__)
388431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#define CF_RC_BITS 0
389431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#endif
390431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko
391431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#if defined(__LITTLE_ENDIAN__)
392431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#define CF_RC_BITS 3
393431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#endif
394431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko
395431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko// See http://opensource.apple.com/source/CF/CF-635.15/CFRuntime.h
396431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenkotypedef struct __CFRuntimeBase {
3973f4c3875c42078e22c7e5356c5746fd18756d958Kostya Serebryany  uptr _cfisa;
398ee3925515e4c7966f3ef489f687aa7e5692806a9Kostya Serebryany  u8 _cfinfo[4];
399431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#if __LP64__
400ee3925515e4c7966f3ef489f687aa7e5692806a9Kostya Serebryany  u32 _rc;
401431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#endif
402431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko} CFRuntimeBase;
403431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko
404431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko// See http://opensource.apple.com/source/CF/CF-635.15/CFString.c
405431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenkoint __CFStrIsConstant(CFStringRef str) {
406431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko  CFRuntimeBase *base = (CFRuntimeBase*)str;
407431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#if __LP64__
408431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko  return base->_rc == 0;
409431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#else
410431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko  return (base->_cfinfo[CF_RC_BITS]) == 0;
411431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko#endif
412431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko}
413431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko
414f2598fc21bf651d23feab396a7581d48c01c3be5Alexey SamsonovINTERCEPTOR(CFStringRef, CFStringCreateCopy, CFAllocatorRef alloc,
415f2598fc21bf651d23feab396a7581d48c01c3be5Alexey Samsonov                                             CFStringRef str) {
416431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko  if (__CFStrIsConstant(str)) {
417431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko    return str;
418431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko  } else {
41909672caefb5694f1981a1712fdefa44840a95e67Alexey Samsonov    return REAL(CFStringCreateCopy)(alloc, str);
420431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko  }
421431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko}
422431e51782d62d1257348e41e24da6b544fe70507Alexander Potapenko
423decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander PotapenkoDECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
424decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko
425decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenkoextern "C"
426decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenkovoid __CFInitialize();
427decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander PotapenkoDECLARE_REAL_AND_INTERCEPTOR(void, __CFInitialize)
428decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko
42964ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonovnamespace __asan {
430beba6448539095b67cab266d09cd7b7d313b8c3dAlexey Samsonov
431beba6448539095b67cab266d09cd7b7d313b8c3dAlexey Samsonovvoid InitializeMacInterceptors() {
4325cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  CHECK(INTERCEPT_FUNCTION(dispatch_async_f));
4335cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  CHECK(INTERCEPT_FUNCTION(dispatch_sync_f));
4345cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  CHECK(INTERCEPT_FUNCTION(dispatch_after_f));
4355cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  CHECK(INTERCEPT_FUNCTION(dispatch_barrier_async_f));
4365cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  CHECK(INTERCEPT_FUNCTION(dispatch_group_async_f));
4375cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  // We don't need to intercept pthread_workqueue_additem_np() to support the
4385cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  // libdispatch API, but it helps us to debug the unsupported functions. Let's
4395cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  // intercept it only during verbose runs.
4405cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  if (FLAG_v >= 2) {
4415cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov    CHECK(INTERCEPT_FUNCTION(pthread_workqueue_additem_np));
4425cf832dc0a6566ae4bb8d48b1f41da623d2c2c1aAlexey Samsonov  }
44364ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  // Normally CFStringCreateCopy should not copy constant CF strings.
44464ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  // Replacing the default CFAllocator causes constant strings to be copied
44564ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  // rather than just returned, which leads to bugs in big applications like
44664ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  // Chromium and WebKit, see
44764ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  // http://code.google.com/p/address-sanitizer/issues/detail?id=10
44864ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  // Until this problem is fixed we need to check that the string is
44964ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  // non-constant before calling CFStringCreateCopy.
45064ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov  CHECK(INTERCEPT_FUNCTION(CFStringCreateCopy));
451e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko  // Some of the library functions call free() directly, so we have to
452e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko  // intercept it.
453e205a9daec9ec4afed956cf5455889725b9192fbAlexander Potapenko  CHECK(INTERCEPT_FUNCTION(free));
454decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko  if (FLAG_replace_cfallocator) {
455decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko    CHECK(INTERCEPT_FUNCTION(__CFInitialize));
456decaec9ee3177b5e81e358ad8e93ab70b38a1cc0Alexander Potapenko  }
45764ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov}
458beba6448539095b67cab266d09cd7b7d313b8c3dAlexey Samsonov
45964ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov}  // namespace __asan
46064ce2db7c838cd95315f7a4428e8a628eaa3e2fcAlexey Samsonov
461d6567c5166412f6acdde851e767c26f332d51d3dKostya Serebryany#endif  // __APPLE__
462