Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_mac.cc -------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Mac-specific details. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_platform.h" |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 16 | #if SANITIZER_MAC |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | |
Alexey Samsonov | 64ce2db | 2012-03-21 12:03:44 +0000 | [diff] [blame] | 18 | #include "asan_interceptors.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 19 | #include "asan_internal.h" |
Alexander Potapenko | d079db6 | 2012-07-06 11:58:54 +0000 | [diff] [blame] | 20 | #include "asan_mac.h" |
Alexander Potapenko | 895b387 | 2012-02-13 17:14:31 +0000 | [diff] [blame] | 21 | #include "asan_mapping.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | #include "asan_stack.h" |
| 23 | #include "asan_thread.h" |
Alexander Potapenko | 31f78fd | 2013-07-16 09:29:48 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_atomic.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 26 | |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 27 | #include <crt_externs.h> // for _NSGetArgv |
| 28 | #include <dlfcn.h> // for dladdr() |
Alexander Potapenko | 8a34d38 | 2012-01-18 11:16:05 +0000 | [diff] [blame] | 29 | #include <mach-o/dyld.h> |
Kostya Serebryany | 9b993e8 | 2012-01-30 22:11:04 +0000 | [diff] [blame] | 30 | #include <mach-o/loader.h> |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 31 | #include <sys/mman.h> |
Kostya Serebryany | ef14ff6 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 32 | #include <sys/resource.h> |
Alexander Potapenko | 59dc578 | 2012-01-31 13:19:18 +0000 | [diff] [blame] | 33 | #include <sys/sysctl.h> |
Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 34 | #include <sys/ucontext.h> |
Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
Alexander Potapenko | e205a9d | 2012-06-20 22:29:09 +0000 | [diff] [blame] | 36 | #include <pthread.h> |
| 37 | #include <stdlib.h> // for free() |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
Kostya Serebryany | d55f5f8 | 2012-01-10 21:24:40 +0000 | [diff] [blame] | 39 | #include <libkern/OSAtomic.h> |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 40 | |
| 41 | namespace __asan { |
| 42 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 43 | void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { |
Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 44 | ucontext_t *ucontext = (ucontext_t*)context; |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 45 | # if SANITIZER_WORDSIZE == 64 |
Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 46 | *pc = ucontext->uc_mcontext->__ss.__rip; |
| 47 | *bp = ucontext->uc_mcontext->__ss.__rbp; |
| 48 | *sp = ucontext->uc_mcontext->__ss.__rsp; |
| 49 | # else |
| 50 | *pc = ucontext->uc_mcontext->__ss.__eip; |
| 51 | *bp = ucontext->uc_mcontext->__ss.__ebp; |
| 52 | *sp = ucontext->uc_mcontext->__ss.__esp; |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 53 | # endif // SANITIZER_WORDSIZE |
Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Alexander Potapenko | 31f78fd | 2013-07-16 09:29:48 +0000 | [diff] [blame] | 56 | MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED; |
| 57 | |
| 58 | MacosVersion GetMacosVersionInternal() { |
Alexander Potapenko | 59dc578 | 2012-01-31 13:19:18 +0000 | [diff] [blame] | 59 | int mib[2] = { CTL_KERN, KERN_OSRELEASE }; |
| 60 | char version[100]; |
Alexey Samsonov | 3f46cf4 | 2012-06-05 09:13:33 +0000 | [diff] [blame] | 61 | uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]); |
Alexey Samsonov | b0bb7fb | 2012-07-30 10:18:31 +0000 | [diff] [blame] | 62 | for (uptr i = 0; i < maxlen; i++) version[i] = '\0'; |
Alexander Potapenko | 59dc578 | 2012-01-31 13:19:18 +0000 | [diff] [blame] | 63 | // Get the version length. |
Kostya Serebryany | a27bdf7 | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 64 | CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1); |
| 65 | CHECK_LT(len, maxlen); |
| 66 | CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1); |
Alexander Potapenko | 59dc578 | 2012-01-31 13:19:18 +0000 | [diff] [blame] | 67 | switch (version[0]) { |
| 68 | case '9': return MACOS_VERSION_LEOPARD; |
| 69 | case '1': { |
| 70 | switch (version[1]) { |
| 71 | case '0': return MACOS_VERSION_SNOW_LEOPARD; |
| 72 | case '1': return MACOS_VERSION_LION; |
Alexander Potapenko | 679bf63 | 2012-10-16 16:24:49 +0000 | [diff] [blame] | 73 | case '2': return MACOS_VERSION_MOUNTAIN_LION; |
Alexander Potapenko | ad2ae54 | 2013-07-16 08:35:42 +0000 | [diff] [blame] | 74 | case '3': return MACOS_VERSION_MAVERICKS; |
Alexander Potapenko | 59dc578 | 2012-01-31 13:19:18 +0000 | [diff] [blame] | 75 | default: return MACOS_VERSION_UNKNOWN; |
| 76 | } |
| 77 | } |
| 78 | default: return MACOS_VERSION_UNKNOWN; |
| 79 | } |
| 80 | } |
| 81 | |
Alexander Potapenko | 31f78fd | 2013-07-16 09:29:48 +0000 | [diff] [blame] | 82 | MacosVersion GetMacosVersion() { |
| 83 | atomic_uint32_t *cache = |
| 84 | reinterpret_cast<atomic_uint32_t*>(&cached_macos_version); |
| 85 | MacosVersion result = |
| 86 | static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire)); |
| 87 | if (result == MACOS_VERSION_UNINITIALIZED) { |
| 88 | result = GetMacosVersionInternal(); |
| 89 | atomic_store(cache, result, memory_order_release); |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
Alexey Samsonov | 38dd4ed | 2012-03-20 10:54:40 +0000 | [diff] [blame] | 94 | bool PlatformHasDifferentMemcpyAndMemmove() { |
| 95 | // On OS X 10.7 memcpy() and memmove() are both resolved |
| 96 | // into memmove$VARIANT$sse42. |
| 97 | // See also http://code.google.com/p/address-sanitizer/issues/detail?id=34. |
| 98 | // TODO(glider): need to check dynamically that memcpy() and memmove() are |
| 99 | // actually the same function. |
| 100 | return GetMacosVersion() == MACOS_VERSION_SNOW_LEOPARD; |
| 101 | } |
| 102 | |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 103 | extern "C" |
| 104 | void __asan_init(); |
| 105 | |
| 106 | static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES"; |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 107 | LowLevelAllocator allocator_for_env; |
| 108 | |
| 109 | // Change the value of the env var |name|, leaking the original value. |
| 110 | // If |name_value| is NULL, the variable is deleted from the environment, |
| 111 | // otherwise the corresponding "NAME=value" string is replaced with |
| 112 | // |name_value|. |
| 113 | void LeakyResetEnv(const char *name, const char *name_value) { |
| 114 | char ***env_ptr = _NSGetEnviron(); |
| 115 | CHECK(env_ptr); |
| 116 | char **environ = *env_ptr; |
| 117 | CHECK(environ); |
| 118 | uptr name_len = internal_strlen(name); |
| 119 | while (*environ != 0) { |
| 120 | uptr len = internal_strlen(*environ); |
| 121 | if (len > name_len) { |
| 122 | const char *p = *environ; |
| 123 | if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') { |
| 124 | // Match. |
| 125 | if (name_value) { |
| 126 | // Replace the old value with the new one. |
| 127 | *environ = const_cast<char*>(name_value); |
| 128 | } else { |
| 129 | // Shift the subsequent pointers back. |
| 130 | char **del = environ; |
| 131 | do { |
| 132 | del[0] = del[1]; |
Kostya Serebryany | 366984e | 2013-02-19 11:30:25 +0000 | [diff] [blame] | 133 | } while (*del++); |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | environ++; |
| 138 | } |
| 139 | } |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 140 | |
| 141 | void MaybeReexec() { |
| 142 | if (!flags()->allow_reexec) return; |
Alexander Potapenko | 6956398 | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 143 | // Make sure the dynamic ASan runtime library is preloaded so that the |
| 144 | // wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec |
| 145 | // ourselves. |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 146 | Dl_info info; |
Alexey Samsonov | 4ea14c2 | 2012-09-12 14:10:14 +0000 | [diff] [blame] | 147 | CHECK(dladdr((void*)((uptr)__asan_init), &info)); |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 148 | char *dyld_insert_libraries = |
| 149 | const_cast<char*>(GetEnv(kDyldInsertLibraries)); |
Alexey Samsonov | 180e988 | 2013-02-15 19:22:49 +0000 | [diff] [blame] | 150 | uptr old_env_len = dyld_insert_libraries ? |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 151 | internal_strlen(dyld_insert_libraries) : 0; |
Alexey Samsonov | e6b91fd | 2013-02-15 19:02:32 +0000 | [diff] [blame] | 152 | uptr fname_len = internal_strlen(info.dli_fname); |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 153 | if (!dyld_insert_libraries || |
| 154 | !REAL(strstr)(dyld_insert_libraries, info.dli_fname)) { |
| 155 | // DYLD_INSERT_LIBRARIES is not set or does not contain the runtime |
| 156 | // library. |
| 157 | char program_name[1024]; |
| 158 | uint32_t buf_size = sizeof(program_name); |
| 159 | _NSGetExecutablePath(program_name, &buf_size); |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 160 | char *new_env = const_cast<char*>(info.dli_fname); |
Alexander Potapenko | eb5f427 | 2013-02-13 17:52:55 +0000 | [diff] [blame] | 161 | if (dyld_insert_libraries) { |
| 162 | // Append the runtime dylib name to the existing value of |
| 163 | // DYLD_INSERT_LIBRARIES. |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 164 | new_env = (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2); |
Alexander Potapenko | eb5f427 | 2013-02-13 17:52:55 +0000 | [diff] [blame] | 165 | internal_strncpy(new_env, dyld_insert_libraries, old_env_len); |
| 166 | new_env[old_env_len] = ':'; |
| 167 | // Copy fname_len and add a trailing zero. |
Kostya Serebryany | 8da17ea | 2013-02-14 06:54:51 +0000 | [diff] [blame] | 168 | internal_strncpy(new_env + old_env_len + 1, info.dli_fname, |
| 169 | fname_len + 1); |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 170 | // Ok to use setenv() since the wrappers don't depend on the value of |
| 171 | // asan_inited. |
Alexander Potapenko | eb5f427 | 2013-02-13 17:52:55 +0000 | [diff] [blame] | 172 | setenv(kDyldInsertLibraries, new_env, /*overwrite*/1); |
| 173 | } else { |
| 174 | // Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name. |
| 175 | setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0); |
| 176 | } |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 177 | if (flags()->verbosity >= 1) { |
| 178 | Report("exec()-ing the program with\n"); |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 179 | Report("%s=%s\n", kDyldInsertLibraries, new_env); |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 180 | Report("to enable ASan wrappers.\n"); |
| 181 | Report("Set ASAN_OPTIONS=allow_reexec=0 to disable this.\n"); |
| 182 | } |
| 183 | execv(program_name, *_NSGetArgv()); |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 184 | } else { |
| 185 | // DYLD_INSERT_LIBRARIES is set and contains the runtime library. |
| 186 | if (old_env_len == fname_len) { |
| 187 | // It's just the runtime library name - fine to unset the variable. |
| 188 | LeakyResetEnv(kDyldInsertLibraries, NULL); |
| 189 | } else { |
Alexey Samsonov | 180e988 | 2013-02-15 19:22:49 +0000 | [diff] [blame] | 190 | uptr env_name_len = internal_strlen(kDyldInsertLibraries); |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 191 | // Allocate memory to hold the previous env var name, its value, the '=' |
| 192 | // sign and the '\0' char. |
| 193 | char *new_env = (char*)allocator_for_env.Allocate( |
| 194 | old_env_len + 2 + env_name_len); |
| 195 | CHECK(new_env); |
| 196 | internal_memset(new_env, '\0', old_env_len + 2 + env_name_len); |
| 197 | internal_strncpy(new_env, kDyldInsertLibraries, env_name_len); |
| 198 | new_env[env_name_len] = '='; |
| 199 | char *new_env_pos = new_env + env_name_len + 1; |
| 200 | |
| 201 | // Iterate over colon-separated pieces of |dyld_insert_libraries|. |
| 202 | char *piece_start = dyld_insert_libraries; |
| 203 | char *piece_end = NULL; |
| 204 | char *old_env_end = dyld_insert_libraries + old_env_len; |
| 205 | do { |
| 206 | if (piece_start[0] == ':') piece_start++; |
| 207 | piece_end = REAL(strchr)(piece_start, ':'); |
| 208 | if (!piece_end) piece_end = dyld_insert_libraries + old_env_len; |
Alexey Samsonov | 180e988 | 2013-02-15 19:22:49 +0000 | [diff] [blame] | 209 | if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break; |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 210 | uptr piece_len = piece_end - piece_start; |
| 211 | |
Kostya Serebryany | 366984e | 2013-02-19 11:30:25 +0000 | [diff] [blame] | 212 | // If the current piece isn't the runtime library name, |
| 213 | // append it to new_env. |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 214 | if ((piece_len != fname_len) || |
| 215 | (internal_strncmp(piece_start, info.dli_fname, fname_len) != 0)) { |
| 216 | if (new_env_pos != new_env + env_name_len + 1) { |
| 217 | new_env_pos[0] = ':'; |
| 218 | new_env_pos++; |
| 219 | } |
| 220 | internal_strncpy(new_env_pos, piece_start, piece_len); |
| 221 | } |
Kostya Serebryany | 366984e | 2013-02-19 11:30:25 +0000 | [diff] [blame] | 222 | // Move on to the next piece. |
Alexander Potapenko | fe984cc | 2013-02-15 16:10:49 +0000 | [diff] [blame] | 223 | new_env_pos += piece_len; |
| 224 | piece_start = piece_end; |
| 225 | } while (piece_start < old_env_end); |
| 226 | |
| 227 | // Can't use setenv() here, because it requires the allocator to be |
| 228 | // initialized. |
| 229 | // FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in |
| 230 | // a separate function called after InitializeAllocator(). |
| 231 | LeakyResetEnv(kDyldInsertLibraries, new_env); |
| 232 | } |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 233 | } |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 236 | // No-op. Mac does not support static linkage anyway. |
| 237 | void *AsanDoesNotSupportStaticLinkage() { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 238 | return 0; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 241 | bool AsanInterceptsSignal(int signum) { |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 242 | return (signum == SIGSEGV || signum == SIGBUS) && flags()->handle_segv; |
Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 245 | void AsanPlatformThreadInit() { |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 248 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
Alexey Samsonov | f3950c6 | 2012-11-23 10:14:44 +0000 | [diff] [blame] | 249 | UNIMPLEMENTED(); |
| 250 | } |
| 251 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 252 | // Support for the following functions from libdispatch on Mac OS: |
| 253 | // dispatch_async_f() |
| 254 | // dispatch_async() |
| 255 | // dispatch_sync_f() |
| 256 | // dispatch_sync() |
| 257 | // dispatch_after_f() |
| 258 | // dispatch_after() |
| 259 | // dispatch_group_async_f() |
| 260 | // dispatch_group_async() |
| 261 | // TODO(glider): libdispatch API contains other functions that we don't support |
| 262 | // yet. |
| 263 | // |
| 264 | // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are |
| 265 | // they can cause jobs to run on a thread different from the current one. |
| 266 | // TODO(glider): if so, we need a test for this (otherwise we should remove |
| 267 | // them). |
| 268 | // |
| 269 | // The following functions use dispatch_barrier_async_f() (which isn't a library |
| 270 | // function but is exported) and are thus supported: |
| 271 | // dispatch_source_set_cancel_handler_f() |
| 272 | // dispatch_source_set_cancel_handler() |
| 273 | // dispatch_source_set_event_handler_f() |
| 274 | // dispatch_source_set_event_handler() |
| 275 | // |
| 276 | // The reference manual for Grand Central Dispatch is available at |
| 277 | // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html |
| 278 | // The implementation details are at |
| 279 | // http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c |
| 280 | |
Alexey Samsonov | f7ceaad | 2012-04-09 16:45:18 +0000 | [diff] [blame] | 281 | typedef void* dispatch_group_t; |
| 282 | typedef void* dispatch_queue_t; |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 283 | typedef void* dispatch_source_t; |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 284 | typedef u64 dispatch_time_t; |
Alexey Samsonov | f7ceaad | 2012-04-09 16:45:18 +0000 | [diff] [blame] | 285 | typedef void (*dispatch_function_t)(void *block); |
Alexey Samsonov | 5cf832d | 2012-03-21 12:29:54 +0000 | [diff] [blame] | 286 | typedef void* (*worker_t)(void *block); |
| 287 | |
| 288 | // A wrapper for the ObjC blocks used to support libdispatch. |
| 289 | typedef struct { |
| 290 | void *block; |
| 291 | dispatch_function_t func; |
Kostya Serebryany | e0cff0b | 2012-06-06 15:06:58 +0000 | [diff] [blame] | 292 | u32 parent_tid; |
Alexey Samsonov | 5cf832d | 2012-03-21 12:29:54 +0000 | [diff] [blame] | 293 | } asan_block_context_t; |
| 294 | |
Timur Iskhodzhanov | b157c67 | 2013-03-28 21:16:09 +0000 | [diff] [blame] | 295 | ALWAYS_INLINE |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 296 | void asan_register_worker_thread(int parent_tid, StackTrace *stack) { |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 297 | AsanThread *t = GetCurrentThread(); |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 298 | if (!t) { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 299 | t = AsanThread::Create(0, 0); |
| 300 | CreateThreadContextArgs args = { t, stack }; |
| 301 | asanThreadRegistry().CreateThread(*(uptr*)t, true, parent_tid, &args); |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 302 | t->Init(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 303 | asanThreadRegistry().StartThread(t->tid(), 0, 0); |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 304 | SetCurrentThread(t); |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Alexander Potapenko | 2483ce3 | 2012-08-20 11:59:26 +0000 | [diff] [blame] | 308 | // For use by only those functions that allocated the context via |
| 309 | // alloc_asan_context(). |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 310 | extern "C" |
| 311 | void asan_dispatch_call_block_and_release(void *block) { |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 312 | GET_STACK_TRACE_THREAD; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 313 | asan_block_context_t *context = (asan_block_context_t*)block; |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 314 | if (flags()->verbosity >= 2) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 315 | Report("asan_dispatch_call_block_and_release(): " |
| 316 | "context: %p, pthread_self: %p\n", |
| 317 | block, pthread_self()); |
| 318 | } |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 319 | asan_register_worker_thread(context->parent_tid, &stack); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 320 | // Call the original dispatcher for the block. |
| 321 | context->func(context->block); |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 322 | asan_free(context, &stack, FROM_MALLOC); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | } // namespace __asan |
| 326 | |
| 327 | using namespace __asan; // NOLINT |
| 328 | |
| 329 | // Wrap |ctxt| and |func| into an asan_block_context_t. |
| 330 | // The caller retains control of the allocated context. |
| 331 | extern "C" |
| 332 | asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func, |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 333 | StackTrace *stack) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 334 | asan_block_context_t *asan_ctxt = |
| 335 | (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack); |
| 336 | asan_ctxt->block = ctxt; |
| 337 | asan_ctxt->func = func; |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 338 | asan_ctxt->parent_tid = GetCurrentTidOrInvalid(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 339 | return asan_ctxt; |
| 340 | } |
| 341 | |
Alexander Potapenko | b09dd34 | 2012-08-20 09:25:10 +0000 | [diff] [blame] | 342 | // Define interceptor for dispatch_*_f function with the three most common |
| 343 | // parameters: dispatch_queue_t, context, dispatch_function_t. |
| 344 | #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \ |
| 345 | INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \ |
| 346 | dispatch_function_t func) { \ |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 347 | GET_STACK_TRACE_THREAD; \ |
Alexander Potapenko | b09dd34 | 2012-08-20 09:25:10 +0000 | [diff] [blame] | 348 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \ |
| 349 | if (flags()->verbosity >= 2) { \ |
| 350 | Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n", \ |
| 351 | asan_ctxt, pthread_self()); \ |
| 352 | PRINT_CURRENT_STACK(); \ |
| 353 | } \ |
| 354 | return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \ |
| 355 | asan_dispatch_call_block_and_release); \ |
Kostya Serebryany | 0ffc227 | 2012-08-21 06:43:44 +0000 | [diff] [blame] | 356 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 357 | |
Alexander Potapenko | b09dd34 | 2012-08-20 09:25:10 +0000 | [diff] [blame] | 358 | INTERCEPT_DISPATCH_X_F_3(dispatch_async_f) |
| 359 | INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f) |
| 360 | INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f) |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 361 | |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 362 | INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, |
| 363 | dispatch_queue_t dq, void *ctxt, |
| 364 | dispatch_function_t func) { |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 365 | GET_STACK_TRACE_THREAD; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 366 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 367 | if (flags()->verbosity >= 2) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 368 | Report("dispatch_after_f: %p\n", asan_ctxt); |
| 369 | PRINT_CURRENT_STACK(); |
| 370 | } |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 371 | return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt, |
| 372 | asan_dispatch_call_block_and_release); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Alexey Samsonov | f2598fc | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 375 | INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group, |
| 376 | dispatch_queue_t dq, void *ctxt, |
| 377 | dispatch_function_t func) { |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 378 | GET_STACK_TRACE_THREAD; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 379 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 380 | if (flags()->verbosity >= 2) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 381 | Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n", |
| 382 | asan_ctxt, pthread_self()); |
| 383 | PRINT_CURRENT_STACK(); |
| 384 | } |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 385 | REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt, |
| 386 | asan_dispatch_call_block_and_release); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Alexander Potapenko | 6956398 | 2013-02-05 15:57:12 +0000 | [diff] [blame] | 389 | #if !defined(MISSING_BLOCKS_SUPPORT) |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 390 | extern "C" { |
| 391 | // FIXME: consolidate these declarations with asan_intercepted_functions.h. |
| 392 | void dispatch_async(dispatch_queue_t dq, void(^work)(void)); |
| 393 | void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq, |
| 394 | void(^work)(void)); |
| 395 | void dispatch_after(dispatch_time_t when, dispatch_queue_t queue, |
| 396 | void(^work)(void)); |
| 397 | void dispatch_source_set_cancel_handler(dispatch_source_t ds, |
| 398 | void(^work)(void)); |
| 399 | void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void)); |
| 400 | } |
| 401 | |
| 402 | #define GET_ASAN_BLOCK(work) \ |
| 403 | void (^asan_block)(void); \ |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 404 | int parent_tid = GetCurrentTidOrInvalid(); \ |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 405 | asan_block = ^(void) { \ |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 406 | GET_STACK_TRACE_THREAD; \ |
Alexander Potapenko | af198e4 | 2012-08-23 09:34:40 +0000 | [diff] [blame] | 407 | asan_register_worker_thread(parent_tid, &stack); \ |
| 408 | work(); \ |
| 409 | } |
| 410 | |
| 411 | INTERCEPTOR(void, dispatch_async, |
| 412 | dispatch_queue_t dq, void(^work)(void)) { |
| 413 | GET_ASAN_BLOCK(work); |
| 414 | REAL(dispatch_async)(dq, asan_block); |
| 415 | } |
| 416 | |
| 417 | INTERCEPTOR(void, dispatch_group_async, |
| 418 | dispatch_group_t dg, dispatch_queue_t dq, void(^work)(void)) { |
| 419 | GET_ASAN_BLOCK(work); |
| 420 | REAL(dispatch_group_async)(dg, dq, asan_block); |
| 421 | } |
| 422 | |
| 423 | INTERCEPTOR(void, dispatch_after, |
| 424 | dispatch_time_t when, dispatch_queue_t queue, void(^work)(void)) { |
| 425 | GET_ASAN_BLOCK(work); |
| 426 | REAL(dispatch_after)(when, queue, asan_block); |
| 427 | } |
| 428 | |
| 429 | INTERCEPTOR(void, dispatch_source_set_cancel_handler, |
| 430 | dispatch_source_t ds, void(^work)(void)) { |
| 431 | GET_ASAN_BLOCK(work); |
| 432 | REAL(dispatch_source_set_cancel_handler)(ds, asan_block); |
| 433 | } |
| 434 | |
| 435 | INTERCEPTOR(void, dispatch_source_set_event_handler, |
| 436 | dispatch_source_t ds, void(^work)(void)) { |
| 437 | GET_ASAN_BLOCK(work); |
| 438 | REAL(dispatch_source_set_event_handler)(ds, asan_block); |
| 439 | } |
| 440 | #endif |
| 441 | |
Alexey Samsonov | 649a270 | 2013-04-03 07:29:53 +0000 | [diff] [blame] | 442 | #endif // SANITIZER_MAC |