Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #if defined(LIBC_STATIC) |
| 30 | #error This file should not be compiled for static targets. |
| 31 | #endif |
| 32 | |
| 33 | #include <dlfcn.h> |
| 34 | #include <fcntl.h> |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 35 | #include <signal.h> |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <unistd.h> |
| 39 | |
Christopher Ferris | 2b0638e | 2019-09-11 19:05:29 -0700 | [diff] [blame] | 40 | #include <platform/bionic/malloc.h> |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 41 | #include <private/bionic_config.h> |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 42 | #include <private/bionic_malloc_dispatch.h> |
| 43 | #include <sys/system_properties.h> |
| 44 | |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 45 | #include "gwp_asan_wrappers.h" |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 46 | #include "malloc_common.h" |
| 47 | #include "malloc_common_dynamic.h" |
| 48 | #include "malloc_heapprofd.h" |
Mitch Phillips | 3083cc9 | 2020-02-11 15:23:47 -0800 | [diff] [blame] | 49 | #include "malloc_limit.h" |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 50 | |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 51 | // Installing heapprofd hooks is a multi step process, as outlined below. |
| 52 | // |
| 53 | // The incremental hooking and a dedicated task thread are used since we cannot |
| 54 | // do heavy work within a signal handler, or when blocking a malloc invocation. |
| 55 | // |
| 56 | // +--->+-------------+------------------+ |
| 57 | // | +->+kInitialState+----------------+ | malloc functions are not intercepted in any way. |
| 58 | // | | +-------+-----+ | | |
| 59 | // | | | | | |
| 60 | // | | v | | |
| 61 | // | | +-------+----------------+ | | currently installing the ephemeral hooks. |
| 62 | // | | |kInstallingEphemeralHook|<--+ | | |
| 63 | // | | +-------+----------------+ | | | |
| 64 | // | | | | | | |
| 65 | // | | v | | | |
| 66 | // | | +-------+---------------+ | | | ephemeral hooks are installed. on the first call to |
| 67 | // | | |kEphemeralHookInstalled| | | | malloc these hooks spawn a thread that installs the |
| 68 | // | | +-------+---------------+ | | | heapprofd hooks. |
| 69 | // | | | | | | |
| 70 | // | | v | | | |
| 71 | // | | +-------+--------------+ | | | first call to malloc happened. the hooks are reset to |
| 72 | // | +--|kRemovingEphemeralHook| | | | kInitialState. |
| 73 | // | +----------------------+ | | | |
| 74 | // | | | | |
| 75 | // | | | | |
| 76 | // | +---------------+ | | | currently installing the heapprofd hook |
| 77 | // | |kInstallingHook|<-----------|-+ | |
| 78 | // | +-------+-------+ | | |
| 79 | // | | | | |
| 80 | // | v | | |
| 81 | // | +-------+------+ | | heapprofd hooks are installed. these forward calls to |
| 82 | // | |kHookInstalled|-------------+ | malloc / free / etc. to heapprofd_client.so. |
| 83 | // | +-------+------+ | |
| 84 | // | | | |
| 85 | // | v | |
| 86 | // | +-------+---------+ | currently resetting the hooks to default. |
| 87 | // |----+kUninstallingHook| | |
| 88 | // +-----------------+ | |
| 89 | // | |
| 90 | // | |
| 91 | // +------------------+ | malloc debug / malloc hooks are active. these take |
| 92 | // |kIncompatibleHooks+<------------+ precendence over heapprofd, so heapprofd will not get |
| 93 | // +------------------+ enabled. this is a terminal state. |
| 94 | // |
| 95 | enum MallocHeapprofdState : uint8_t { |
| 96 | kInitialState, |
| 97 | kInstallingEphemeralHook, |
| 98 | kEphemeralHookInstalled, |
| 99 | kRemovingEphemeralHook, |
| 100 | kInstallingHook, |
| 101 | kHookInstalled, |
| 102 | kUninstallingHook, |
| 103 | kIncompatibleHooks |
| 104 | }; |
| 105 | |
| 106 | enum ModifyGlobalsMode { |
| 107 | kWithLock, // all calls to MaybeModifyGlobals with kWithLock will serialise. they can fail |
| 108 | // due to a concurrent call with kWithoutLock. |
| 109 | kWithoutLock // calls to MaybeModifyGlobals with kWithoutLock do not serialise. they can fail |
| 110 | // due to concurrent calls with kWithoutLock or kWithLock. |
| 111 | }; |
| 112 | |
| 113 | // Provide mutual exclusion so no two threads try to modify the globals at the same time. |
| 114 | template <typename Fn> |
| 115 | bool MaybeModifyGlobals(ModifyGlobalsMode mode, Fn f) { |
| 116 | bool success = false; |
| 117 | if (mode == kWithLock) { |
| 118 | pthread_mutex_lock(&gGlobalsMutateLock); |
| 119 | } |
| 120 | // As we have grabbed the mutex, the following condition should always hold, except |
| 121 | // if we are currently running HandleHeapprofdSignal. |
| 122 | if (!atomic_exchange(&gGlobalsMutating, true)) { |
| 123 | f(); |
| 124 | success = true; |
| 125 | atomic_store(&gGlobalsMutating, false); |
| 126 | } else { |
| 127 | error_log("%s: heapprofd client: concurrent modification.", getprogname()); |
| 128 | } |
| 129 | if (mode == kWithLock) { |
| 130 | pthread_mutex_unlock(&gGlobalsMutateLock); |
| 131 | } |
| 132 | return success; |
| 133 | } |
| 134 | |
| 135 | extern "C" void* MallocInitHeapprofdHook(size_t); |
| 136 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 137 | static constexpr char kHeapprofdSharedLib[] = "heapprofd_client.so"; |
| 138 | static constexpr char kHeapprofdPrefix[] = "heapprofd"; |
| 139 | static constexpr char kHeapprofdPropertyEnable[] = "heapprofd.enable"; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 140 | |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 141 | constexpr char kHeapprofdProgramPropertyPrefix[] = "heapprofd.enable."; |
| 142 | constexpr size_t kHeapprofdProgramPropertyPrefixSize = sizeof(kHeapprofdProgramPropertyPrefix) - 1; |
| 143 | constexpr size_t kMaxCmdlineSize = 512; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 144 | |
| 145 | // The handle returned by dlopen when previously loading the heapprofd |
| 146 | // hooks. nullptr if shared library has not been already been loaded. |
| 147 | static _Atomic (void*) gHeapprofdHandle = nullptr; |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 148 | static _Atomic MallocHeapprofdState gHeapprofdState = kInitialState; |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 149 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 150 | static bool GetHeapprofdProgramProperty(char* data, size_t size) { |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 151 | if (size < kHeapprofdProgramPropertyPrefixSize) { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 152 | error_log("%s: Overflow constructing heapprofd property", getprogname()); |
| 153 | return false; |
| 154 | } |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 155 | memcpy(data, kHeapprofdProgramPropertyPrefix, kHeapprofdProgramPropertyPrefixSize); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 156 | |
| 157 | int fd = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC); |
| 158 | if (fd == -1) { |
| 159 | error_log("%s: Failed to open /proc/self/cmdline", getprogname()); |
| 160 | return false; |
| 161 | } |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 162 | char cmdline[kMaxCmdlineSize]; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 163 | ssize_t rd = read(fd, cmdline, sizeof(cmdline) - 1); |
| 164 | close(fd); |
| 165 | if (rd == -1) { |
| 166 | error_log("%s: Failed to read /proc/self/cmdline", getprogname()); |
| 167 | return false; |
| 168 | } |
| 169 | cmdline[rd] = '\0'; |
| 170 | char* first_arg = static_cast<char*>(memchr(cmdline, '\0', rd)); |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 171 | if (first_arg == nullptr) { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 172 | error_log("%s: Overflow reading cmdline", getprogname()); |
| 173 | return false; |
| 174 | } |
| 175 | // For consistency with what we do with Java app cmdlines, trim everything |
| 176 | // after the @ sign of the first arg. |
| 177 | char* first_at = static_cast<char*>(memchr(cmdline, '@', rd)); |
| 178 | if (first_at != nullptr && first_at < first_arg) { |
| 179 | *first_at = '\0'; |
| 180 | first_arg = first_at; |
| 181 | } |
| 182 | |
| 183 | char* start = static_cast<char*>(memrchr(cmdline, '/', first_arg - cmdline)); |
| 184 | if (start == first_arg) { |
| 185 | // The first argument ended in a slash. |
| 186 | error_log("%s: cmdline ends in /", getprogname()); |
| 187 | return false; |
| 188 | } else if (start == nullptr) { |
| 189 | start = cmdline; |
| 190 | } else { |
| 191 | // Skip the /. |
| 192 | start++; |
| 193 | } |
| 194 | |
| 195 | size_t name_size = static_cast<size_t>(first_arg - start); |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 196 | if (name_size >= size - kHeapprofdProgramPropertyPrefixSize) { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 197 | error_log("%s: overflow constructing heapprofd property.", getprogname()); |
| 198 | return false; |
| 199 | } |
| 200 | // + 1 to also copy the trailing null byte. |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 201 | memcpy(data + kHeapprofdProgramPropertyPrefixSize, start, name_size + 1); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 205 | // Runtime triggering entry-point. Two possible call sites: |
| 206 | // * when receiving a profiling signal with a si_value indicating heapprofd. |
| 207 | // * when a Zygote child is marking itself as profileable, and there's a |
| 208 | // matching profiling request for this process (in which case heapprofd client |
| 209 | // is loaded synchronously). |
| 210 | // In both cases, the caller is responsible for verifying that the process is |
| 211 | // considered profileable. |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 212 | |
| 213 | // Previously installed default dispatch table, if it exists. This is used to |
| 214 | // load heapprofd properly when GWP-ASan was already installed. If GWP-ASan was |
| 215 | // already installed, heapprofd will take over the dispatch table, but will use |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 216 | // GWP-ASan as the backing dispatch. Writes to this variable is atomically |
| 217 | // protected by MaybeModifyGlobals. |
| 218 | // Reads are not protected, so this is atomic. We cannot fail the call in |
| 219 | // MallocInitHeapprofdHook. |
| 220 | static _Atomic (const MallocDispatch*) gPreviousDefaultDispatchTable = nullptr; |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 221 | static MallocDispatch gEphemeralDispatch; |
| 222 | |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 223 | void HandleHeapprofdSignal() { |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 224 | if (atomic_load(&gHeapprofdState) == kIncompatibleHooks) { |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 225 | error_log("%s: not enabling heapprofd, malloc_debug/malloc_hooks are enabled.", getprogname()); |
| 226 | return; |
| 227 | } |
| 228 | |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 229 | // We cannot grab the mutex here, as this is used in a signal handler. |
| 230 | MaybeModifyGlobals(kWithoutLock, [] { |
| 231 | MallocHeapprofdState expected = kInitialState; |
| 232 | // If hooks are already installed, we still want to install ephemeral hooks to retrigger |
| 233 | // heapprofd client initialization. |
| 234 | MallocHeapprofdState expected2 = kHookInstalled; |
| 235 | if (atomic_compare_exchange_strong(&gHeapprofdState, &expected, |
| 236 | kInstallingEphemeralHook) || |
| 237 | atomic_compare_exchange_strong(&gHeapprofdState, &expected2, |
| 238 | kInstallingEphemeralHook)) { |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 239 | const MallocDispatch* default_dispatch = GetDefaultDispatchTable(); |
Mitch Phillips | 5f91bf4 | 2020-02-26 11:28:11 -0800 | [diff] [blame] | 240 | |
| 241 | // Below, we initialize heapprofd lazily by redirecting libc's malloc() to |
| 242 | // call MallocInitHeapprofdHook, which spawns off a thread and initializes |
| 243 | // heapprofd. During the short period between now and when heapprofd is |
| 244 | // initialized, allocations may need to be serviced. There are three |
| 245 | // possible configurations: |
| 246 | |
| 247 | if (default_dispatch == nullptr) { |
| 248 | // 1. No malloc hooking has been done (heapprofd, GWP-ASan, etc.). In |
| 249 | // this case, everything but malloc() should come from the system |
| 250 | // allocator. |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 251 | atomic_store(&gPreviousDefaultDispatchTable, nullptr); |
Mitch Phillips | 5f91bf4 | 2020-02-26 11:28:11 -0800 | [diff] [blame] | 252 | gEphemeralDispatch = *NativeAllocatorDispatch(); |
| 253 | } else if (DispatchIsGwpAsan(default_dispatch)) { |
| 254 | // 2. GWP-ASan was installed. We should use GWP-ASan for everything but |
| 255 | // malloc() in the interim period before heapprofd is properly |
| 256 | // installed. After heapprofd is finished installing, we will use |
| 257 | // GWP-ASan as heapprofd's backing allocator to allow heapprofd and |
| 258 | // GWP-ASan to coexist. |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 259 | atomic_store(&gPreviousDefaultDispatchTable, default_dispatch); |
Mitch Phillips | 5f91bf4 | 2020-02-26 11:28:11 -0800 | [diff] [blame] | 260 | gEphemeralDispatch = *default_dispatch; |
| 261 | } else { |
| 262 | // 3. It may be possible at this point in time that heapprofd is |
| 263 | // *already* the default dispatch, and as such we don't want to use |
| 264 | // heapprofd as the backing store for itself (otherwise infinite |
| 265 | // recursion occurs). We will use the system allocator functions. Note: |
| 266 | // We've checked that no other malloc interceptors are being used by |
| 267 | // validating `gHeapprofdIncompatibleHooks` above, so we don't need to |
| 268 | // worry about that case here. |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 269 | atomic_store(&gPreviousDefaultDispatchTable, nullptr); |
Mitch Phillips | 5f91bf4 | 2020-02-26 11:28:11 -0800 | [diff] [blame] | 270 | gEphemeralDispatch = *NativeAllocatorDispatch(); |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Mitch Phillips | 5f91bf4 | 2020-02-26 11:28:11 -0800 | [diff] [blame] | 273 | // Now, replace the malloc function so that the next call to malloc() will |
| 274 | // initialize heapprofd. |
| 275 | gEphemeralDispatch.malloc = MallocInitHeapprofdHook; |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 276 | |
Mitch Phillips | 5f91bf4 | 2020-02-26 11:28:11 -0800 | [diff] [blame] | 277 | // And finally, install these new malloc-family interceptors. |
| 278 | __libc_globals.mutate([](libc_globals* globals) { |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 279 | atomic_store(&globals->default_dispatch_table, &gEphemeralDispatch); |
| 280 | if (!MallocLimitInstalled()) { |
| 281 | atomic_store(&globals->current_dispatch_table, &gEphemeralDispatch); |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 282 | } |
| 283 | }); |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 284 | atomic_store(&gHeapprofdState, kEphemeralHookInstalled); |
| 285 | } else { |
| 286 | error_log("%s: heapprofd: failed to transition kInitialState -> kInstallingEphemeralHook. " |
| 287 | "current state (possible race): %d", getprogname(), expected2); |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 288 | } |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 289 | }); |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 290 | // Otherwise, we're racing against malloc_limit's enable logic (at most once |
| 291 | // per process, and a niche feature). This is highly unlikely, so simply give |
| 292 | // up if it does happen. |
| 293 | } |
| 294 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 295 | bool HeapprofdShouldLoad() { |
| 296 | // First check for heapprofd.enable. If it is set to "all", enable |
| 297 | // heapprofd for all processes. Otherwise, check heapprofd.enable.${prog}, |
| 298 | // if it is set and not 0, enable heap profiling for this process. |
| 299 | char property_value[PROP_VALUE_MAX]; |
| 300 | if (__system_property_get(kHeapprofdPropertyEnable, property_value) == 0) { |
| 301 | return false; |
| 302 | } |
| 303 | if (strcmp(property_value, "all") == 0) { |
| 304 | return true; |
| 305 | } |
| 306 | |
Florian Mayer | f6d221e | 2019-05-03 16:24:52 +0100 | [diff] [blame] | 307 | char program_property[kHeapprofdProgramPropertyPrefixSize + kMaxCmdlineSize]; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 308 | if (!GetHeapprofdProgramProperty(program_property, |
| 309 | sizeof(program_property))) { |
| 310 | return false; |
| 311 | } |
| 312 | if (__system_property_get(program_property, property_value) == 0) { |
| 313 | return false; |
| 314 | } |
Christopher Ferris | 503c17b | 2019-02-22 12:47:23 -0800 | [diff] [blame] | 315 | return property_value[0] != '\0'; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 318 | void HeapprofdRememberHookConflict() { |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 319 | atomic_store(&gHeapprofdState, kIncompatibleHooks); |
Christopher Ferris | 2822856 | 2019-02-14 10:23:58 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 322 | static void CommonInstallHooks(libc_globals* globals) { |
| 323 | void* impl_handle = atomic_load(&gHeapprofdHandle); |
| 324 | bool reusing_handle = impl_handle != nullptr; |
| 325 | if (!reusing_handle) { |
| 326 | impl_handle = LoadSharedLibrary(kHeapprofdSharedLib, kHeapprofdPrefix, &globals->malloc_dispatch_table); |
| 327 | if (impl_handle == nullptr) { |
| 328 | return; |
| 329 | } |
| 330 | } else if (!InitSharedLibrary(impl_handle, kHeapprofdSharedLib, kHeapprofdPrefix, &globals->malloc_dispatch_table)) { |
| 331 | return; |
| 332 | } |
| 333 | |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 334 | // Before we set the new default_dispatch_table in FinishInstallHooks, save |
| 335 | // the previous dispatch table. If DispatchReset() gets called later, we want |
| 336 | // to be able to restore the dispatch. We're still under |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 337 | // MaybeModifyGlobals locks at this point. |
| 338 | atomic_store(&gPreviousDefaultDispatchTable, GetDefaultDispatchTable()); |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 339 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 340 | if (FinishInstallHooks(globals, nullptr, kHeapprofdPrefix)) { |
| 341 | atomic_store(&gHeapprofdHandle, impl_handle); |
| 342 | } else if (!reusing_handle) { |
| 343 | dlclose(impl_handle); |
| 344 | } |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void HeapprofdInstallHooksAtInit(libc_globals* globals) { |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 348 | MaybeModifyGlobals(kWithoutLock, [globals] { |
| 349 | MallocHeapprofdState expected = kInitialState; |
| 350 | if (atomic_compare_exchange_strong(&gHeapprofdState, &expected, kInstallingHook)) { |
| 351 | CommonInstallHooks(globals); |
| 352 | atomic_store(&gHeapprofdState, kHookInstalled); |
| 353 | } else { |
| 354 | error_log("%s: heapprofd: failed to transition kInitialState -> kInstallingHook. " |
| 355 | "current state (possible race): %d", getprogname(), expected); |
| 356 | } |
| 357 | }); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static void* InitHeapprofd(void*) { |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 361 | MaybeModifyGlobals(kWithLock, [] { |
| 362 | MallocHeapprofdState expected = kInitialState; |
| 363 | if (atomic_compare_exchange_strong(&gHeapprofdState, &expected, kInstallingHook)) { |
| 364 | __libc_globals.mutate([](libc_globals* globals) { |
| 365 | CommonInstallHooks(globals); |
| 366 | }); |
| 367 | atomic_store(&gHeapprofdState, kHookInstalled); |
| 368 | } else { |
| 369 | error_log("%s: heapprofd: failed to transition kInitialState -> kInstallingHook. " |
| 370 | "current state (possible race): %d", getprogname(), expected); |
| 371 | } |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 372 | }); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 373 | return nullptr; |
| 374 | } |
| 375 | |
| 376 | extern "C" void* MallocInitHeapprofdHook(size_t bytes) { |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 377 | MaybeModifyGlobals(kWithLock, [] { |
| 378 | MallocHeapprofdState expected = kEphemeralHookInstalled; |
| 379 | if (atomic_compare_exchange_strong(&gHeapprofdState, &expected, kRemovingEphemeralHook)) { |
| 380 | __libc_globals.mutate([](libc_globals* globals) { |
| 381 | const MallocDispatch* previous_dispatch = atomic_load(&gPreviousDefaultDispatchTable); |
| 382 | atomic_store(&globals->default_dispatch_table, previous_dispatch); |
| 383 | if (!MallocLimitInstalled()) { |
| 384 | atomic_store(&globals->current_dispatch_table, previous_dispatch); |
| 385 | } |
| 386 | }); |
| 387 | atomic_store(&gHeapprofdState, kInitialState); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 388 | |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 389 | pthread_t thread_id; |
| 390 | if (pthread_create(&thread_id, nullptr, InitHeapprofd, nullptr) != 0) { |
| 391 | error_log("%s: heapprofd: failed to pthread_create.", getprogname()); |
| 392 | } else if (pthread_setname_np(thread_id, "heapprofdinit") != 0) { |
| 393 | error_log("%s: heapprod: failed to pthread_setname_np", getprogname()); |
| 394 | } else if (pthread_detach(thread_id) != 0) { |
| 395 | error_log("%s: heapprofd: failed to pthread_detach", getprogname()); |
| 396 | } |
| 397 | } else { |
| 398 | warning_log("%s: heapprofd: could not transition kEphemeralHookInstalled -> " |
| 399 | "kRemovingEphemeralHook. current state (possible race): %d. this can be benign " |
| 400 | "if two threads try this transition at the same time", getprogname(), |
| 401 | expected); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 402 | } |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 403 | }); |
Mitch Phillips | 449c26a | 2020-02-28 07:37:19 -0800 | [diff] [blame] | 404 | // If we had a previous dispatch table, use that to service the allocation, |
| 405 | // otherwise fall back to the native allocator. |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 406 | // This could be modified by a concurrent HandleHeapprofdSignal, but that is |
| 407 | // benign as we will dispatch to the ephemeral handler, which will then dispatch |
| 408 | // to the underlying one. |
| 409 | const MallocDispatch* previous_dispatch = atomic_load(&gPreviousDefaultDispatchTable); |
| 410 | if (previous_dispatch) { |
| 411 | return previous_dispatch->malloc(bytes); |
Mitch Phillips | 449c26a | 2020-02-28 07:37:19 -0800 | [diff] [blame] | 412 | } |
| 413 | return NativeAllocatorDispatch()->malloc(bytes); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 416 | bool HeapprofdInitZygoteChildProfiling() { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 417 | // Conditionally start "from startup" profiling. |
| 418 | if (HeapprofdShouldLoad()) { |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 419 | // Directly call the signal handler codepath (properly protects against |
| 420 | // concurrent invocations). |
| 421 | HandleHeapprofdSignal(); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | static bool DispatchReset() { |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 427 | if (atomic_load(&gHeapprofdState) == kInitialState) { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 428 | return true; |
| 429 | } |
Florian Mayer | fdd5eb1 | 2020-03-02 18:25:46 -0800 | [diff] [blame^] | 430 | |
| 431 | bool success = false; |
| 432 | MaybeModifyGlobals(kWithLock, [&success] { |
| 433 | MallocHeapprofdState expected = kHookInstalled; |
| 434 | |
| 435 | if(atomic_compare_exchange_strong(&gHeapprofdState, &expected, kUninstallingHook)){ |
| 436 | __libc_globals.mutate([](libc_globals* globals) { |
| 437 | const MallocDispatch* previous_dispatch = atomic_load(&gPreviousDefaultDispatchTable); |
| 438 | atomic_store(&globals->default_dispatch_table, previous_dispatch); |
| 439 | if (!MallocLimitInstalled()) { |
| 440 | atomic_store(&globals->current_dispatch_table, previous_dispatch); |
| 441 | } |
| 442 | }); |
| 443 | atomic_store(&gHeapprofdState, kInitialState); |
| 444 | success = true; |
| 445 | } else { |
| 446 | error_log("%s: heapprofd: failed to transition kHookInstalled -> kUninstallingHook. " |
| 447 | "current state (possible race): %d", getprogname(), |
| 448 | expected); |
| 449 | } |
| 450 | }); |
| 451 | if (!success) { |
| 452 | errno = EAGAIN; |
| 453 | } |
| 454 | return success; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | bool HeapprofdMallopt(int opcode, void* arg, size_t arg_size) { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 458 | if (opcode == M_RESET_HOOKS) { |
| 459 | if (arg != nullptr || arg_size != 0) { |
| 460 | errno = EINVAL; |
| 461 | return false; |
| 462 | } |
| 463 | return DispatchReset(); |
| 464 | } |
| 465 | errno = ENOTSUP; |
| 466 | return false; |
| 467 | } |