2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_mac.cc -------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Mac-specific details.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-03-19 22:33:38 +08:00
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
2013-03-19 22:54:17 +08:00
|
|
|
#if SANITIZER_MAC
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-03-21 20:03:44 +08:00
|
|
|
#include "asan_interceptors.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_internal.h"
|
2012-02-14 01:14:31 +08:00
|
|
|
#include "asan_mapping.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_thread.h"
|
2013-07-16 17:29:48 +08:00
|
|
|
#include "sanitizer_common/sanitizer_atomic.h"
|
2012-06-04 22:27:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2014-02-03 23:32:19 +08:00
|
|
|
#include "sanitizer_common/sanitizer_mac.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-08-24 17:22:05 +08:00
|
|
|
#include <crt_externs.h> // for _NSGetArgv
|
|
|
|
#include <dlfcn.h> // for dladdr()
|
2012-01-18 19:16:05 +08:00
|
|
|
#include <mach-o/dyld.h>
|
2012-01-31 06:11:04 +08:00
|
|
|
#include <mach-o/loader.h>
|
2011-11-30 09:07:02 +08:00
|
|
|
#include <sys/mman.h>
|
2012-01-06 10:12:25 +08:00
|
|
|
#include <sys/resource.h>
|
2012-01-31 21:19:18 +08:00
|
|
|
#include <sys/sysctl.h>
|
2012-01-07 03:11:09 +08:00
|
|
|
#include <sys/ucontext.h>
|
2011-12-29 07:28:54 +08:00
|
|
|
#include <fcntl.h>
|
2012-06-21 06:29:09 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h> // for free()
|
2011-11-30 09:07:02 +08:00
|
|
|
#include <unistd.h>
|
2012-01-11 05:24:40 +08:00
|
|
|
#include <libkern/OSAtomic.h>
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
|
2012-01-07 03:11:09 +08:00
|
|
|
ucontext_t *ucontext = (ucontext_t*)context;
|
2012-11-21 20:38:58 +08:00
|
|
|
# if SANITIZER_WORDSIZE == 64
|
2012-01-07 03:11:09 +08:00
|
|
|
*pc = ucontext->uc_mcontext->__ss.__rip;
|
|
|
|
*bp = ucontext->uc_mcontext->__ss.__rbp;
|
|
|
|
*sp = ucontext->uc_mcontext->__ss.__rsp;
|
|
|
|
# else
|
|
|
|
*pc = ucontext->uc_mcontext->__ss.__eip;
|
|
|
|
*bp = ucontext->uc_mcontext->__ss.__ebp;
|
|
|
|
*sp = ucontext->uc_mcontext->__ss.__esp;
|
2012-11-21 20:38:58 +08:00
|
|
|
# endif // SANITIZER_WORDSIZE
|
2012-01-07 03:11:09 +08:00
|
|
|
}
|
|
|
|
|
2013-07-16 17:29:48 +08:00
|
|
|
|
2012-03-20 18:54:40 +08:00
|
|
|
bool PlatformHasDifferentMemcpyAndMemmove() {
|
|
|
|
// On OS X 10.7 memcpy() and memmove() are both resolved
|
|
|
|
// into memmove$VARIANT$sse42.
|
|
|
|
// See also http://code.google.com/p/address-sanitizer/issues/detail?id=34.
|
|
|
|
// TODO(glider): need to check dynamically that memcpy() and memmove() are
|
|
|
|
// actually the same function.
|
|
|
|
return GetMacosVersion() == MACOS_VERSION_SNOW_LEOPARD;
|
|
|
|
}
|
|
|
|
|
2012-08-24 17:22:05 +08:00
|
|
|
extern "C"
|
|
|
|
void __asan_init();
|
|
|
|
|
|
|
|
static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES";
|
2013-02-16 00:10:49 +08:00
|
|
|
LowLevelAllocator allocator_for_env;
|
|
|
|
|
|
|
|
// Change the value of the env var |name|, leaking the original value.
|
|
|
|
// If |name_value| is NULL, the variable is deleted from the environment,
|
|
|
|
// otherwise the corresponding "NAME=value" string is replaced with
|
|
|
|
// |name_value|.
|
|
|
|
void LeakyResetEnv(const char *name, const char *name_value) {
|
|
|
|
char ***env_ptr = _NSGetEnviron();
|
|
|
|
CHECK(env_ptr);
|
|
|
|
char **environ = *env_ptr;
|
|
|
|
CHECK(environ);
|
|
|
|
uptr name_len = internal_strlen(name);
|
|
|
|
while (*environ != 0) {
|
|
|
|
uptr len = internal_strlen(*environ);
|
|
|
|
if (len > name_len) {
|
|
|
|
const char *p = *environ;
|
|
|
|
if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') {
|
|
|
|
// Match.
|
|
|
|
if (name_value) {
|
|
|
|
// Replace the old value with the new one.
|
|
|
|
*environ = const_cast<char*>(name_value);
|
|
|
|
} else {
|
|
|
|
// Shift the subsequent pointers back.
|
|
|
|
char **del = environ;
|
|
|
|
do {
|
|
|
|
del[0] = del[1];
|
2013-02-19 19:30:25 +08:00
|
|
|
} while (*del++);
|
2013-02-16 00:10:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
environ++;
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 17:22:05 +08:00
|
|
|
|
|
|
|
void MaybeReexec() {
|
|
|
|
if (!flags()->allow_reexec) return;
|
2013-02-05 23:57:12 +08:00
|
|
|
// Make sure the dynamic ASan runtime library is preloaded so that the
|
|
|
|
// wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec
|
|
|
|
// ourselves.
|
2012-08-24 17:22:05 +08:00
|
|
|
Dl_info info;
|
2012-09-12 22:10:14 +08:00
|
|
|
CHECK(dladdr((void*)((uptr)__asan_init), &info));
|
2013-02-16 00:10:49 +08:00
|
|
|
char *dyld_insert_libraries =
|
|
|
|
const_cast<char*>(GetEnv(kDyldInsertLibraries));
|
2013-02-16 03:22:49 +08:00
|
|
|
uptr old_env_len = dyld_insert_libraries ?
|
2013-02-16 00:10:49 +08:00
|
|
|
internal_strlen(dyld_insert_libraries) : 0;
|
2013-02-16 03:02:32 +08:00
|
|
|
uptr fname_len = internal_strlen(info.dli_fname);
|
2012-08-24 17:22:05 +08:00
|
|
|
if (!dyld_insert_libraries ||
|
2014-11-19 09:31:59 +08:00
|
|
|
!REAL(strstr)(dyld_insert_libraries, StripModuleName(info.dli_fname))) {
|
2012-08-24 17:22:05 +08:00
|
|
|
// DYLD_INSERT_LIBRARIES is not set or does not contain the runtime
|
|
|
|
// library.
|
|
|
|
char program_name[1024];
|
|
|
|
uint32_t buf_size = sizeof(program_name);
|
|
|
|
_NSGetExecutablePath(program_name, &buf_size);
|
2013-02-16 00:10:49 +08:00
|
|
|
char *new_env = const_cast<char*>(info.dli_fname);
|
2013-02-14 01:52:55 +08:00
|
|
|
if (dyld_insert_libraries) {
|
|
|
|
// Append the runtime dylib name to the existing value of
|
|
|
|
// DYLD_INSERT_LIBRARIES.
|
2013-02-16 00:10:49 +08:00
|
|
|
new_env = (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2);
|
2013-02-14 01:52:55 +08:00
|
|
|
internal_strncpy(new_env, dyld_insert_libraries, old_env_len);
|
|
|
|
new_env[old_env_len] = ':';
|
|
|
|
// Copy fname_len and add a trailing zero.
|
2013-02-14 14:54:51 +08:00
|
|
|
internal_strncpy(new_env + old_env_len + 1, info.dli_fname,
|
|
|
|
fname_len + 1);
|
2013-02-16 00:10:49 +08:00
|
|
|
// Ok to use setenv() since the wrappers don't depend on the value of
|
|
|
|
// asan_inited.
|
2013-02-14 01:52:55 +08:00
|
|
|
setenv(kDyldInsertLibraries, new_env, /*overwrite*/1);
|
|
|
|
} else {
|
|
|
|
// Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name.
|
|
|
|
setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0);
|
|
|
|
}
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "exec()-ing the program with\n");
|
|
|
|
VReport(1, "%s=%s\n", kDyldInsertLibraries, new_env);
|
|
|
|
VReport(1, "to enable ASan wrappers.\n");
|
|
|
|
VReport(1, "Set ASAN_OPTIONS=allow_reexec=0 to disable this.\n");
|
2012-08-24 17:22:05 +08:00
|
|
|
execv(program_name, *_NSGetArgv());
|
2013-02-16 00:10:49 +08:00
|
|
|
} else {
|
|
|
|
// DYLD_INSERT_LIBRARIES is set and contains the runtime library.
|
|
|
|
if (old_env_len == fname_len) {
|
|
|
|
// It's just the runtime library name - fine to unset the variable.
|
|
|
|
LeakyResetEnv(kDyldInsertLibraries, NULL);
|
|
|
|
} else {
|
2013-02-16 03:22:49 +08:00
|
|
|
uptr env_name_len = internal_strlen(kDyldInsertLibraries);
|
2013-02-16 00:10:49 +08:00
|
|
|
// Allocate memory to hold the previous env var name, its value, the '='
|
|
|
|
// sign and the '\0' char.
|
|
|
|
char *new_env = (char*)allocator_for_env.Allocate(
|
|
|
|
old_env_len + 2 + env_name_len);
|
|
|
|
CHECK(new_env);
|
|
|
|
internal_memset(new_env, '\0', old_env_len + 2 + env_name_len);
|
|
|
|
internal_strncpy(new_env, kDyldInsertLibraries, env_name_len);
|
|
|
|
new_env[env_name_len] = '=';
|
|
|
|
char *new_env_pos = new_env + env_name_len + 1;
|
|
|
|
|
|
|
|
// Iterate over colon-separated pieces of |dyld_insert_libraries|.
|
|
|
|
char *piece_start = dyld_insert_libraries;
|
|
|
|
char *piece_end = NULL;
|
|
|
|
char *old_env_end = dyld_insert_libraries + old_env_len;
|
|
|
|
do {
|
|
|
|
if (piece_start[0] == ':') piece_start++;
|
|
|
|
piece_end = REAL(strchr)(piece_start, ':');
|
|
|
|
if (!piece_end) piece_end = dyld_insert_libraries + old_env_len;
|
2013-02-16 03:22:49 +08:00
|
|
|
if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break;
|
2013-02-16 00:10:49 +08:00
|
|
|
uptr piece_len = piece_end - piece_start;
|
|
|
|
|
2013-02-19 19:30:25 +08:00
|
|
|
// If the current piece isn't the runtime library name,
|
|
|
|
// append it to new_env.
|
2013-02-16 00:10:49 +08:00
|
|
|
if ((piece_len != fname_len) ||
|
|
|
|
(internal_strncmp(piece_start, info.dli_fname, fname_len) != 0)) {
|
|
|
|
if (new_env_pos != new_env + env_name_len + 1) {
|
|
|
|
new_env_pos[0] = ':';
|
|
|
|
new_env_pos++;
|
|
|
|
}
|
|
|
|
internal_strncpy(new_env_pos, piece_start, piece_len);
|
|
|
|
}
|
2013-02-19 19:30:25 +08:00
|
|
|
// Move on to the next piece.
|
2013-02-16 00:10:49 +08:00
|
|
|
new_env_pos += piece_len;
|
|
|
|
piece_start = piece_end;
|
|
|
|
} while (piece_start < old_env_end);
|
|
|
|
|
|
|
|
// Can't use setenv() here, because it requires the allocator to be
|
|
|
|
// initialized.
|
|
|
|
// FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in
|
|
|
|
// a separate function called after InitializeAllocator().
|
|
|
|
LeakyResetEnv(kDyldInsertLibraries, new_env);
|
|
|
|
}
|
2012-08-24 17:22:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// No-op. Mac does not support static linkage anyway.
|
|
|
|
void *AsanDoesNotSupportStaticLinkage() {
|
2012-05-31 22:35:53 +08:00
|
|
|
return 0;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2014-04-01 21:16:30 +08:00
|
|
|
// No-op. Mac does not support static linkage anyway.
|
|
|
|
void AsanCheckDynamicRTPrereqs() {}
|
|
|
|
|
|
|
|
// No-op. Mac does not support static linkage anyway.
|
|
|
|
void AsanCheckIncompatibleRT() {}
|
|
|
|
|
2012-01-10 02:53:15 +08:00
|
|
|
bool AsanInterceptsSignal(int signum) {
|
2014-01-28 17:28:57 +08:00
|
|
|
return (signum == SIGSEGV || signum == SIGBUS) &&
|
|
|
|
common_flags()->handle_segv;
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
|
|
|
|
2012-07-23 22:07:58 +08:00
|
|
|
void AsanPlatformThreadInit() {
|
|
|
|
}
|
|
|
|
|
2013-01-17 23:45:28 +08:00
|
|
|
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
|
2012-11-23 18:14:44 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// Support for the following functions from libdispatch on Mac OS:
|
|
|
|
// dispatch_async_f()
|
|
|
|
// dispatch_async()
|
|
|
|
// dispatch_sync_f()
|
|
|
|
// dispatch_sync()
|
|
|
|
// dispatch_after_f()
|
|
|
|
// dispatch_after()
|
|
|
|
// dispatch_group_async_f()
|
|
|
|
// dispatch_group_async()
|
|
|
|
// TODO(glider): libdispatch API contains other functions that we don't support
|
|
|
|
// yet.
|
|
|
|
//
|
|
|
|
// dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
|
|
|
|
// they can cause jobs to run on a thread different from the current one.
|
|
|
|
// TODO(glider): if so, we need a test for this (otherwise we should remove
|
|
|
|
// them).
|
|
|
|
//
|
|
|
|
// The following functions use dispatch_barrier_async_f() (which isn't a library
|
|
|
|
// function but is exported) and are thus supported:
|
|
|
|
// dispatch_source_set_cancel_handler_f()
|
|
|
|
// dispatch_source_set_cancel_handler()
|
|
|
|
// dispatch_source_set_event_handler_f()
|
|
|
|
// dispatch_source_set_event_handler()
|
|
|
|
//
|
|
|
|
// The reference manual for Grand Central Dispatch is available at
|
|
|
|
// http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
|
|
|
|
// The implementation details are at
|
|
|
|
// http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
|
|
|
|
|
2012-04-10 00:45:18 +08:00
|
|
|
typedef void* dispatch_group_t;
|
|
|
|
typedef void* dispatch_queue_t;
|
2012-08-23 17:34:40 +08:00
|
|
|
typedef void* dispatch_source_t;
|
2012-05-31 23:02:07 +08:00
|
|
|
typedef u64 dispatch_time_t;
|
2012-04-10 00:45:18 +08:00
|
|
|
typedef void (*dispatch_function_t)(void *block);
|
2012-03-21 20:29:54 +08:00
|
|
|
typedef void* (*worker_t)(void *block);
|
|
|
|
|
|
|
|
// A wrapper for the ObjC blocks used to support libdispatch.
|
|
|
|
typedef struct {
|
|
|
|
void *block;
|
|
|
|
dispatch_function_t func;
|
2012-06-06 23:06:58 +08:00
|
|
|
u32 parent_tid;
|
2012-03-21 20:29:54 +08:00
|
|
|
} asan_block_context_t;
|
|
|
|
|
2013-03-29 05:16:09 +08:00
|
|
|
ALWAYS_INLINE
|
2012-08-28 19:54:30 +08:00
|
|
|
void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
|
2013-03-20 18:11:24 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2012-08-23 17:34:40 +08:00
|
|
|
if (!t) {
|
2013-03-21 19:23:41 +08:00
|
|
|
t = AsanThread::Create(0, 0);
|
|
|
|
CreateThreadContextArgs args = { t, stack };
|
|
|
|
asanThreadRegistry().CreateThread(*(uptr*)t, true, parent_tid, &args);
|
2012-08-23 17:34:40 +08:00
|
|
|
t->Init();
|
2013-03-21 19:23:41 +08:00
|
|
|
asanThreadRegistry().StartThread(t->tid(), 0, 0);
|
2013-03-20 18:11:24 +08:00
|
|
|
SetCurrentThread(t);
|
2012-08-23 17:34:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 19:59:26 +08:00
|
|
|
// For use by only those functions that allocated the context via
|
|
|
|
// alloc_asan_context().
|
2011-11-30 09:07:02 +08:00
|
|
|
extern "C"
|
|
|
|
void asan_dispatch_call_block_and_release(void *block) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_THREAD;
|
2011-11-30 09:07:02 +08:00
|
|
|
asan_block_context_t *context = (asan_block_context_t*)block;
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(2,
|
|
|
|
"asan_dispatch_call_block_and_release(): "
|
|
|
|
"context: %p, pthread_self: %p\n",
|
|
|
|
block, pthread_self());
|
2012-08-23 17:34:40 +08:00
|
|
|
asan_register_worker_thread(context->parent_tid, &stack);
|
2011-11-30 09:07:02 +08:00
|
|
|
// Call the original dispatcher for the block.
|
|
|
|
context->func(context->block);
|
2012-12-21 16:53:59 +08:00
|
|
|
asan_free(context, &stack, FROM_MALLOC);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
// Wrap |ctxt| and |func| into an asan_block_context_t.
|
|
|
|
// The caller retains control of the allocated context.
|
|
|
|
extern "C"
|
|
|
|
asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
|
2014-10-28 00:20:59 +08:00
|
|
|
BufferedStackTrace *stack) {
|
2011-11-30 09:07:02 +08:00
|
|
|
asan_block_context_t *asan_ctxt =
|
|
|
|
(asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
|
|
|
|
asan_ctxt->block = ctxt;
|
|
|
|
asan_ctxt->func = func;
|
2013-03-20 18:11:24 +08:00
|
|
|
asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
|
2011-11-30 09:07:02 +08:00
|
|
|
return asan_ctxt;
|
|
|
|
}
|
|
|
|
|
2012-08-20 17:25:10 +08:00
|
|
|
// Define interceptor for dispatch_*_f function with the three most common
|
|
|
|
// parameters: dispatch_queue_t, context, dispatch_function_t.
|
|
|
|
#define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
|
|
|
|
INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \
|
|
|
|
dispatch_function_t func) { \
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_THREAD; \
|
2012-08-20 17:25:10 +08:00
|
|
|
asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \
|
2013-10-15 21:28:51 +08:00
|
|
|
if (common_flags()->verbosity >= 2) { \
|
2012-08-20 17:25:10 +08:00
|
|
|
Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n", \
|
|
|
|
asan_ctxt, pthread_self()); \
|
2013-12-05 20:04:51 +08:00
|
|
|
PRINT_CURRENT_STACK(); \
|
|
|
|
} \
|
|
|
|
return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \
|
|
|
|
asan_dispatch_call_block_and_release); \
|
2012-08-21 14:43:44 +08:00
|
|
|
}
|
2012-08-20 17:25:10 +08:00
|
|
|
|
|
|
|
INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
|
|
|
|
INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
|
|
|
|
INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
|
|
|
|
dispatch_queue_t dq, void *ctxt,
|
|
|
|
dispatch_function_t func) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_THREAD;
|
2011-11-30 09:07:02 +08:00
|
|
|
asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
|
2013-10-15 21:28:51 +08:00
|
|
|
if (common_flags()->verbosity >= 2) {
|
2011-11-30 09:07:02 +08:00
|
|
|
Report("dispatch_after_f: %p\n", asan_ctxt);
|
|
|
|
PRINT_CURRENT_STACK();
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt,
|
|
|
|
asan_dispatch_call_block_and_release);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-08-23 17:34:40 +08:00
|
|
|
INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
|
|
|
|
dispatch_queue_t dq, void *ctxt,
|
|
|
|
dispatch_function_t func) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_THREAD;
|
2012-08-23 17:34:40 +08:00
|
|
|
asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
|
2013-10-15 21:28:51 +08:00
|
|
|
if (common_flags()->verbosity >= 2) {
|
2012-08-23 17:34:40 +08:00
|
|
|
Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
|
|
|
|
asan_ctxt, pthread_self());
|
|
|
|
PRINT_CURRENT_STACK();
|
|
|
|
}
|
|
|
|
REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt,
|
|
|
|
asan_dispatch_call_block_and_release);
|
|
|
|
}
|
|
|
|
|
2013-02-05 23:57:12 +08:00
|
|
|
#if !defined(MISSING_BLOCKS_SUPPORT)
|
2012-08-23 17:34:40 +08:00
|
|
|
extern "C" {
|
|
|
|
void dispatch_async(dispatch_queue_t dq, void(^work)(void));
|
|
|
|
void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
|
|
|
|
void(^work)(void));
|
|
|
|
void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
|
|
|
|
void(^work)(void));
|
|
|
|
void dispatch_source_set_cancel_handler(dispatch_source_t ds,
|
|
|
|
void(^work)(void));
|
|
|
|
void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void));
|
2012-08-20 19:59:26 +08:00
|
|
|
}
|
|
|
|
|
2012-08-23 17:34:40 +08:00
|
|
|
#define GET_ASAN_BLOCK(work) \
|
|
|
|
void (^asan_block)(void); \
|
2013-03-20 18:11:24 +08:00
|
|
|
int parent_tid = GetCurrentTidOrInvalid(); \
|
2012-08-23 17:34:40 +08:00
|
|
|
asan_block = ^(void) { \
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_THREAD; \
|
2012-08-23 17:34:40 +08:00
|
|
|
asan_register_worker_thread(parent_tid, &stack); \
|
|
|
|
work(); \
|
2012-08-20 19:59:26 +08:00
|
|
|
}
|
|
|
|
|
2014-07-10 08:06:53 +08:00
|
|
|
// Forces the compiler to generate a frame pointer in the function.
|
|
|
|
#define ENABLE_FRAME_POINTER \
|
|
|
|
do { \
|
|
|
|
volatile uptr enable_fp; \
|
|
|
|
enable_fp = GET_CURRENT_FRAME(); \
|
|
|
|
} while (0)
|
|
|
|
|
2012-08-20 19:59:26 +08:00
|
|
|
INTERCEPTOR(void, dispatch_async,
|
2012-08-23 17:34:40 +08:00
|
|
|
dispatch_queue_t dq, void(^work)(void)) {
|
2014-07-10 08:06:53 +08:00
|
|
|
ENABLE_FRAME_POINTER;
|
2012-08-23 17:34:40 +08:00
|
|
|
GET_ASAN_BLOCK(work);
|
|
|
|
REAL(dispatch_async)(dq, asan_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void, dispatch_group_async,
|
|
|
|
dispatch_group_t dg, dispatch_queue_t dq, void(^work)(void)) {
|
2014-07-10 08:06:53 +08:00
|
|
|
ENABLE_FRAME_POINTER;
|
2012-08-23 17:34:40 +08:00
|
|
|
GET_ASAN_BLOCK(work);
|
|
|
|
REAL(dispatch_group_async)(dg, dq, asan_block);
|
2012-08-20 19:59:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void, dispatch_after,
|
2012-08-23 17:34:40 +08:00
|
|
|
dispatch_time_t when, dispatch_queue_t queue, void(^work)(void)) {
|
2014-07-10 08:06:53 +08:00
|
|
|
ENABLE_FRAME_POINTER;
|
2012-08-23 17:34:40 +08:00
|
|
|
GET_ASAN_BLOCK(work);
|
|
|
|
REAL(dispatch_after)(when, queue, asan_block);
|
2012-08-20 19:59:26 +08:00
|
|
|
}
|
|
|
|
|
2012-08-23 17:34:40 +08:00
|
|
|
INTERCEPTOR(void, dispatch_source_set_cancel_handler,
|
|
|
|
dispatch_source_t ds, void(^work)(void)) {
|
2014-07-10 08:06:53 +08:00
|
|
|
ENABLE_FRAME_POINTER;
|
2012-08-23 17:34:40 +08:00
|
|
|
GET_ASAN_BLOCK(work);
|
|
|
|
REAL(dispatch_source_set_cancel_handler)(ds, asan_block);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-08-23 17:34:40 +08:00
|
|
|
INTERCEPTOR(void, dispatch_source_set_event_handler,
|
|
|
|
dispatch_source_t ds, void(^work)(void)) {
|
2014-07-10 08:06:53 +08:00
|
|
|
ENABLE_FRAME_POINTER;
|
2012-08-23 17:34:40 +08:00
|
|
|
GET_ASAN_BLOCK(work);
|
|
|
|
REAL(dispatch_source_set_event_handler)(ds, asan_block);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-03 15:29:53 +08:00
|
|
|
#endif // SANITIZER_MAC
|