1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "partition_alloc/shim/allocator_shim.h"
6 
7 #include <unistd.h>
8 
9 #include "build/build_config.h"
10 #include "partition_alloc/partition_alloc_buildflags.h"
11 
12 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
13 #include "partition_alloc/shim/allocator_shim_default_dispatch_to_partition_alloc.h"
14 #endif
15 
16 // No calls to malloc / new in this file. They would cause re-entrancy of
17 // the shim, which is hard to deal with. Keep this code as simple as possible
18 // and don't use any external C++ object here, not even //base ones. Even if
19 // they are safe to use today, in future they might be refactored.
20 
21 #include "partition_alloc/shim/allocator_shim_functions.h"
22 #include "partition_alloc/shim/shim_alloc_functions.h"
23 
24 // Cpp symbols (new / delete) should always be routed through the shim layer
25 // except on Windows and macOS (except for PartitionAlloc-Everywhere) where the
26 // malloc intercept is deep enough that it also catches the cpp calls.
27 //
28 // In case of PartitionAlloc-Everywhere on macOS, malloc backed by
29 // allocator_shim::internal::PartitionMalloc crashes on OOM, and we need to
30 // avoid crashes in case of operator new() noexcept.  Thus, operator new()
31 // noexcept needs to be routed to
32 // allocator_shim::internal::PartitionMallocUnchecked through the shim layer.
33 #include "partition_alloc/shim/allocator_shim_override_cpp_symbols.h"
34 
35 // Android does not support symbol interposition. The way malloc symbols are
36 // intercepted on Android is by using link-time -wrap flags.
37 #include "partition_alloc/shim/allocator_shim_override_linker_wrapped_symbols.h"
38 
39 // Cross-checks.
40 
41 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
42 #error The allocator shim should not be compiled when building for memory tools.
43 #endif
44 
45 #if (defined(__GNUC__) && defined(__EXCEPTIONS)) || \
46     (defined(_MSC_VER) && defined(_CPPUNWIND))
47 #error This code cannot be used when exceptions are turned on.
48 #endif
49