1This document describes how malloc / new calls are routed in the various Chrome 2platforms. 3 4Bear in mind that the chromium codebase does not always just use `malloc()`. 5Some 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 14Background 15---------- 16The `allocator` target defines at compile-time the platform-specific choice of 17the allocator and extra-hooks which services calls to malloc/new. The relevant 18build-time flags involved are `use_allocator_shim` and 19`use_partition_alloc_as_malloc`. 20 21By default, these are true on all platforms except iOS (not yet supported) and 22NaCl (no plan to support). 23Furthermore, when building with a sanitizer (e.g. `asan`, `msan`, ...) both the 24allocator and the shim layer are disabled. 25 26 27Layering and build deps 28----------------------- 29The `allocator` target provides the linker flags required for the Windows shim 30layer. The `base` target is (almost) the only one depending on `allocator`. No 31other targets should depend on it, with the exception of the very few 32executables / dynamic libraries that don't depend, either directly or 33indirectly, on `base` within the scope of a linker unit. 34 35More importantly, **no other place outside of `/base` should depend on the 36specific allocator**. 37If such a functional dependency is required that should be achieved using 38abstractions in `base` (see `/base/memory/`) 39 40**Why `base` depends on `allocator`?** 41Because it needs to provide services that depend on the actual allocator 42implementation. In the past `base` used to pretend to be allocator-agnostic 43and get the dependencies injected by other layers. This ended up being an 44inconsistent mess. 45See the [allocator cleanup doc][url-allocator-cleanup] for more context. 46 47Linker unit targets (executables and shared libraries) that depend in some way 48on `base` (most of the targets in the codebase) automatically get the correct 49set of linker flags to pull in the Windows shim-layer (if needed). 50 51 52Source code 53----------- 54This directory contains just the allocator (i.e. shim) layer that switches 55between the different underlying memory allocation implementations. 56 57 58Unified allocator shim 59---------------------- 60On most platforms, Chrome overrides the malloc / operator new symbols (and 61corresponding free / delete and other variants). This is to enforce security 62checks and lately to enable the 63[memory-infra heap profiler][url-memory-infra-heap-profiler]. 64Historically each platform had its special logic for defining the allocator 65symbols in different places of the codebase. The unified allocator shim is 66a project aimed to unify the symbol definition and allocator routing logic in 67a central place. 68 69 - Full documentation: [Allocator shim design doc][url-allocator-shim]. 70 - Current state: Available and enabled by default on Android, CrOS, Linux, 71 Mac OS and Windows. 72 - Tracking bug: [crbug.com/550886](https://crbug.com/550886). 73 - Build-time flag: `use_allocator_shim`. 74 75**Overview of the unified allocator shim** 76The allocator shim consists of three stages: 77``` 78+-------------------------+ +-----------------------+ +----------------+ 79| malloc & friends | -> | shim layer | -> | Routing to | 80| symbols definition | | implementation | | allocator | 81+-------------------------+ +-----------------------+ +----------------+ 82| - libc symbols (malloc, | | - Security checks | | - glibc | 83| calloc, free, ...) | | - Chain of dispatchers| | - Android | 84| - C++ symbols (operator | | that can intercept | | bionic | 85| new, delete, ...) | | and override | | - WinHeap | 86| - glibc weak symbols | | allocations | | - Partition | 87| (__libc_malloc, ...) | +-----------------------+ | Alloc | 88+-------------------------+ +----------------+ 89``` 90 91**1. malloc symbols definition** 92This stage takes care of overriding the symbols `malloc`, `free`, 93`operator new`, `operator delete` and friends and routing those calls inside the 94allocator shim (next point). 95This is taken care of by the headers in `allocator_shim_override_*`. 96 97*On Windows*: Windows' UCRT (Universal C Runtime) exports weak symbols, that we 98can override in `allocator_shim_override_ucrt_symbols_win.h`. 99 100*On Linux/CrOS*: the allocator symbols are defined as exported global symbols 101in `allocator_shim_override_libc_symbols.h` (for `malloc`, `free` and friends) 102and in `allocator_shim_override_cpp_symbols.h` (for `operator new`, 103`operator delete` and friends). 104This enables proper interposition of malloc symbols referenced by the main 105executable 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 106(see EXECUTABLE AND LINKABLE FORMAT (ELF) - Portable Formats Specification). 107The Linux/CrOS shim was introduced by 108[crrev.com/1675143004](https://crrev.com/1675143004). 109 110*On Android*: load-time symbol interposition (unlike the Linux/CrOS case) is not 111possible. This is because Android processes are `fork()`-ed from the Android 112zygote, which pre-loads libc.so and only later native code gets loaded via 113`dlopen()` (symbols from `dlopen()`-ed libraries get a different resolution 114scope). 115In this case, the approach instead of wrapping symbol resolution at link time 116(i.e. during the build), via the `--Wl,-wrap,malloc` linker flag. 117The use of this wrapping flag causes: 118 - All references to allocator symbols in the Chrome codebase to be rewritten as 119 references to `__wrap_malloc` and friends. The `__wrap_malloc` symbols are 120 defined in the `allocator_shim_override_linker_wrapped_symbols.h` and 121 route allocator calls inside the shim layer. 122 - The reference to the original `malloc` symbols (which typically is defined by 123 the system's libc.so) are accessible via the special `__real_malloc` and 124 friends symbols (which will be relocated, at load time, against `malloc`). 125 126In summary, this approach is transparent to the dynamic loader, which still sees 127undefined symbol references to malloc symbols. 128These symbols will be resolved against libc.so as usual. 129More details in [crrev.com/1719433002](https://crrev.com/1719433002). 130 131**2. Shim layer implementation** 132This stage contains the actual shim implementation. This consists of: 133- A singly linked list of dispatchers (structs with function pointers to `malloc`-like functions). Dispatchers can be dynamically inserted at runtime 134(using the `InsertAllocatorDispatch` API). They can intercept and override 135allocator calls. 136- The security checks (suicide on malloc-failure via `std::new_handler`, etc). 137This happens inside `allocator_shim.cc` 138 139**3. Final allocator routing** 140The final element of the aforementioned dispatcher chain is statically defined 141at build time and ultimately routes the allocator calls to the actual allocator 142(as described in the *Background* section above). This is taken care of by the 143headers in `allocator_shim_default_dispatch_to_*` files. 144 145 146Related links 147------------- 148- [Unified allocator shim doc - Feb 2016][url-allocator-shim] 149- [Allocator cleanup doc - Jan 2016][url-allocator-cleanup] 150- [Proposal to use PartitionAlloc as default allocator](https://crbug.com/339604) 151- [Memory-Infra: Tools to profile memory usage in Chrome](/docs/memory-infra/README.md) 152 153[url-allocator-cleanup]: https://docs.google.com/document/d/1V77Kgp_4tfaaWPEZVxNevoD02wXiatnAv7Ssgr0hmjg/edit?usp=sharing 154[url-memory-infra-heap-profiler]: /docs/memory-infra/heap_profiler.md 155[url-allocator-shim]: https://docs.google.com/document/d/1yKlO1AO4XjpDad9rjcBOI15EKdAGsuGO_IeZy0g0kxo/edit?usp=sharing 156