| 1 | This document describes how malloc / new calls are routed in the various Chrome |
| 2 | platforms. |
| 3 | |
| 4 | Bare in mind that the chromium codebase does not always just use `malloc()`. |
| 5 | Some examples: |
| 6 | - Large parts of the renderer (Blink) use two home-brewed allocators, |
| 7 | PartitionAlloc and BlinkGC (Oilpan). |
| 8 | - Some subsystems, such as the V8 JavaScript engine, handle memory management |
| 9 | autonomously. |
| 10 | - Various parts of the codebase use abstractions such as `SharedMemory` or |
| 11 | `DiscardableMemory` which, similarly to the above, have their own page-level |
| 12 | memory management. |
| 13 | |
| 14 | Background |
| 15 | ---------- |
| 16 | The `allocator` target defines at compile-time the platform-specific choice of |
| 17 | the allocator and extra-hooks which services calls to malloc/new. The relevant |
| 18 | build-time flags involved are `use_allocator` and `use_allocator_shim`. |
| 19 | |
| 20 | The default choices are as follows: |
| 21 | |
| 22 | **Windows** |
| 23 | `use_allocator: winheap`, the default Windows heap. |
| 24 | Additionally, `static_library` (i.e. non-component) builds have a shim |
| 25 | layer wrapping malloc/new, which is controlled by `use_allocator_shim`. |
| 26 | The shim layer provides extra security features, such as preventing large |
| 27 | allocations that can hit signed vs. unsigned bugs in third_party code. |
| 28 | |
| 29 | **Linux Desktop / CrOS** |
| 30 | `use_allocator: tcmalloc`, a forked copy of tcmalloc which resides in |
| 31 | `third_party/tcmalloc/chromium`. Setting `use_allocator: none` causes the build |
| 32 | to fall back to the system (Glibc) symbols. |
| 33 | |
| 34 | **Android** |
| 35 | `use_allocator: none`, always use the allocator symbols coming from Android's |
| 36 | libc (Bionic). As it is developed as part of the OS, it is considered to be |
| 37 | optimized for small devices and more memory-efficient than other choices. |
| 38 | The actual implementation backing malloc symbols in Bionic is up to the board |
| 39 | config and can vary (typically *dlmalloc* or *jemalloc* on most Nexus devices). |
| 40 | |
| 41 | **Mac/iOS** |
| 42 | `use_allocator: none`, we always use the system's allocator implementation. |
| 43 | |
| 44 | In addition, when building for `asan` / `msan` both the allocator and the shim |
| 45 | layer are disabled. |
| 46 | |
| 47 | Layering and build deps |
| 48 | ----------------------- |
| 49 | The `allocator` target provides both the source files for tcmalloc (where |
| 50 | applicable) and the linker flags required for the Windows shim layer. |
| 51 | The `base` target is (almost) the only one depending on `allocator`. No other |
| 52 | targets should depend on it, with the exception of the very few executables / |
| 53 | dynamic libraries that don't depend, either directly or indirectly, on `base` |
| 54 | within the scope of a linker unit. |
| 55 | |
| 56 | More importantly, **no other place outside of `/base` should depend on the |
| 57 | specific allocator** (e.g., directly include `third_party/tcmalloc`). |
| 58 | If such a functional dependency is required that should be achieved using |
| 59 | abstractions in `base` (see `/base/allocator/allocator_extension.h` and |
| 60 | `/base/memory/`) |
| 61 | |
| 62 | **Why `base` depends on `allocator`?** |
| 63 | Because it needs to provide services that depend on the actual allocator |
| 64 | implementation. In the past `base` used to pretend to be allocator-agnostic |
| 65 | and get the dependencies injected by other layers. This ended up being an |
| 66 | inconsistent mess. |
| 67 | See the [allocator cleanup doc][url-allocator-cleanup] for more context. |
| 68 | |
| 69 | Linker unit targets (executables and shared libraries) that depend in some way |
| 70 | on `base` (most of the targets in the codebase) get automatically the correct |
| 71 | set of linker flags to pull in tcmalloc or the Windows shim-layer. |
| 72 | |
| 73 | |
| 74 | Source code |
| 75 | ----------- |
| 76 | This directory contains just the allocator (i.e. shim) layer that switches |
| 77 | between the different underlying memory allocation implementations. |
| 78 | |
| 79 | The tcmalloc library originates outside of Chromium and exists in |
| 80 | `../../third_party/tcmalloc` (currently, the actual location is defined in the |
| 81 | allocator.gyp file). The third party sources use a vendor-branch SCM pattern to |
| 82 | track Chromium-specific changes independently from upstream changes. |
| 83 | |
| 84 | The general intent is to push local changes upstream so that over |
| 85 | time we no longer need any forked files. |
| 86 | |
| 87 | |
| 88 | Unified allocator shim |
| 89 | ---------------------- |
| 90 | On most platforms, Chrome overrides the malloc / operator new symbols (and |
| 91 | corresponding free / delete and other variants). This is to enforce security |
| 92 | checks and lately to enable the |
| 93 | [memory-infra heap profiler][url-memory-infra-heap-profiler]. |
| 94 | Historically each platform had its special logic for defining the allocator |
| 95 | symbols in different places of the codebase. The unified allocator shim is |
| 96 | a project aimed to unify the symbol definition and allocator routing logic in |
| 97 | a central place. |
| 98 | |
| 99 | - Full documentation: [Allocator shim design doc][url-allocator-shim]. |
| 100 | - Current state: Available and enabled by default on Android, CrOS, Linux, |
| 101 | Mac OS and Windows. |
| 102 | - Tracking bug: [https://crbug.com/550886][crbug.com/550886]. |
| 103 | - Build-time flag: `use_allocator_shim`. |
| 104 | |
| 105 | **Overview of the unified allocator shim** |
| 106 | The allocator shim consists of three stages: |
| 107 | ``` |
| 108 | +-------------------------+ +-----------------------+ +----------------+ |
| 109 | | malloc & friends | -> | shim layer | -> | Routing to | |
| 110 | | symbols definition | | implementation | | allocator | |
| 111 | +-------------------------+ +-----------------------+ +----------------+ |
| 112 | | - libc symbols (malloc, | | - Security checks | | - tcmalloc | |
| 113 | | calloc, free, ...) | | - Chain of dispatchers| | - glibc | |
| 114 | | - C++ symbols (operator | | that can intercept | | - Android | |
| 115 | | new, delete, ...) | | and override | | bionic | |
| 116 | | - glibc weak symbols | | allocations | | - WinHeap | |
| 117 | | (__libc_malloc, ...) | +-----------------------+ +----------------+ |
| 118 | +-------------------------+ |
| 119 | ``` |
| 120 | |
| 121 | **1. malloc symbols definition** |
| 122 | This stage takes care of overriding the symbols `malloc`, `free`, |
| 123 | `operator new`, `operator delete` and friends and routing those calls inside the |
| 124 | allocator shim (next point). |
| 125 | This is taken care of by the headers in `allocator_shim_override_*`. |
| 126 | |
| 127 | *On Linux/CrOS*: the allocator symbols are defined as exported global symbols |
| 128 | in `allocator_shim_override_libc_symbols.h` (for `malloc`, `free` and friends) |
| 129 | and in `allocator_shim_override_cpp_symbols.h` (for `operator new`, |
| 130 | `operator delete` and friends). |
| 131 | This enables proper interposition of malloc symbols referenced by the main |
| 132 | executable and any third party libraries. Symbol resolution on Linux is a breadth first search that starts from the root link unit, that is the executable |
| 133 | (see EXECUTABLE AND LINKABLE FORMAT (ELF) - Portable Formats Specification). |
| 134 | Additionally, when tcmalloc is the default allocator, some extra glibc symbols |
| 135 | are also defined in `allocator_shim_override_glibc_weak_symbols.h`, for subtle |
| 136 | reasons explained in that file. |
| 137 | The Linux/CrOS shim was introduced by |
| 138 | [crrev.com/1675143004](https://crrev.com/1675143004). |
| 139 | |
| 140 | *On Android*: load-time symbol interposition (unlike the Linux/CrOS case) is not |
| 141 | possible. This is because Android processes are `fork()`-ed from the Android |
| 142 | zygote, which pre-loads libc.so and only later native code gets loaded via |
| 143 | `dlopen()` (symbols from `dlopen()`-ed libraries get a different resolution |
| 144 | scope). |
| 145 | In this case, the approach instead of wrapping symbol resolution at link time |
| 146 | (i.e. during the build), via the `--Wl,-wrap,malloc` linker flag. |
| 147 | The use of this wrapping flag causes: |
| 148 | - All references to allocator symbols in the Chrome codebase to be rewritten as |
| 149 | references to `__wrap_malloc` and friends. The `__wrap_malloc` symbols are |
| 150 | defined in the `allocator_shim_override_linker_wrapped_symbols.h` and |
| 151 | route allocator calls inside the shim layer. |
| 152 | - The reference to the original `malloc` symbols (which typically is defined by |
| 153 | the system's libc.so) are accessible via the special `__real_malloc` and |
| 154 | friends symbols (which will be relocated, at load time, against `malloc`). |
| 155 | |
| 156 | In summary, this approach is transparent to the dynamic loader, which still sees |
| 157 | undefined symbol references to malloc symbols. |
| 158 | These symbols will be resolved against libc.so as usual. |
| 159 | More details in [crrev.com/1719433002](https://crrev.com/1719433002). |
| 160 | |
| 161 | **2. Shim layer implementation** |
| 162 | This stage contains the actual shim implementation. This consists of: |
| 163 | - A singly linked list of dispatchers (structs with function pointers to `malloc`-like functions). Dispatchers can be dynamically inserted at runtime |
| 164 | (using the `InsertAllocatorDispatch` API). They can intercept and override |
| 165 | allocator calls. |
| 166 | - The security checks (suicide on malloc-failure via `std::new_handler`, etc). |
| 167 | This happens inside `allocator_shim.cc` |
| 168 | |
| 169 | **3. Final allocator routing** |
| 170 | The final element of the aforementioned dispatcher chain is statically defined |
| 171 | at build time and ultimately routes the allocator calls to the actual allocator |
| 172 | (as described in the *Background* section above). This is taken care of by the |
| 173 | headers in `allocator_shim_default_dispatch_to_*` files. |
| 174 | |
| 175 | |
| 176 | Appendixes |
| 177 | ---------- |
| 178 | **How does the Windows shim layer replace the malloc symbols?** |
| 179 | The mechanism for hooking LIBCMT in Windows is rather tricky. The core |
| 180 | problem is that by default, the Windows library does not declare malloc and |
| 181 | free as weak symbols. Because of this, they cannot be overridden. To work |
| 182 | around this, we start with the LIBCMT.LIB, and manually remove all allocator |
| 183 | related functions from it using the visual studio library tool. Once removed, |
| 184 | we can now link against the library and provide custom versions of the |
| 185 | allocator related functionality. |
| 186 | See the script `preb_libc.py` in this folder. |
| 187 | |
| 188 | Related links |
| 189 | ------------- |
| 190 | - [Unified allocator shim doc - Feb 2016][url-allocator-shim] |
| 191 | - [Allocator cleanup doc - Jan 2016][url-allocator-cleanup] |
| 192 | - [Proposal to use PartitionAlloc as default allocator](https://crbug.com/339604) |
| 193 | - [Memory-Infra: Tools to profile memory usage in Chrome](/docs/memory-infra/README.md) |
| 194 | |
| 195 | [url-allocator-cleanup]: https://docs.google.com/document/d/1V77Kgp_4tfaaWPEZVxNevoD02wXiatnAv7Ssgr0hmjg/edit?usp=sharing |
| 196 | [url-memory-infra-heap-profiler]: /docs/memory-infra/heap_profiler.md |
| 197 | [url-allocator-shim]: https://docs.google.com/document/d/1yKlO1AO4XjpDad9rjcBOI15EKdAGsuGO_IeZy0g0kxo/edit?usp=sharing |
| 198 | |