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 <malloc/malloc.h>
8 #include <unistd.h>
9 
10 #include "build/build_config.h"
11 #include "partition_alloc/partition_alloc_base/apple/mach_logging.h"
12 #include "partition_alloc/partition_alloc_buildflags.h"
13 #include "partition_alloc/partition_alloc_check.h"
14 #include "partition_alloc/shim/allocator_interception_apple.h"
15 
16 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
17 #include "partition_alloc/shim/allocator_shim_default_dispatch_to_partition_alloc.h"
18 #endif
19 
20 // No calls to malloc / new in this file. They would cause re-entrancy of
21 // the shim, which is hard to deal with. Keep this code as simple as possible
22 // and don't use any external C++ object here, not even //base ones. Even if
23 // they are safe to use today, in future they might be refactored.
24 
25 #include "partition_alloc/shim/allocator_shim_functions.h"
26 
27 namespace allocator_shim {
28 
TryFreeDefaultFallbackToFindZoneAndFree(void * ptr)29 void TryFreeDefaultFallbackToFindZoneAndFree(void* ptr) {
30   unsigned int zone_count = 0;
31   vm_address_t* zones = nullptr;
32   kern_return_t result =
33       malloc_get_all_zones(mach_task_self(), nullptr, &zones, &zone_count);
34   PA_MACH_CHECK(result == KERN_SUCCESS, result) << "malloc_get_all_zones";
35 
36   // "find_zone_and_free" expected by try_free_default.
37   //
38   // libmalloc's zones call find_registered_zone() in case the default one
39   // doesn't handle the allocation. We can't, so we try to emulate it. See the
40   // implementation in libmalloc/src/malloc.c for details.
41   // https://github.com/apple-oss-distributions/libmalloc/blob/main/src/malloc.c
42   for (unsigned int i = 0; i < zone_count; ++i) {
43     malloc_zone_t* zone = reinterpret_cast<malloc_zone_t*>(zones[i]);
44     if (size_t size = zone->size(zone, ptr)) {
45       if (zone->version >= 6 && zone->free_definite_size) {
46         zone->free_definite_size(zone, ptr, size);
47       } else {
48         zone->free(zone, ptr);
49       }
50       return;
51     }
52   }
53 
54   // There must be an owner zone.
55   PA_CHECK(false);
56 }
57 
58 }  // namespace allocator_shim
59 
60 #include "partition_alloc/shim/shim_alloc_functions.h"
61 
62 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
63 // Cpp symbols (new / delete) should always be routed through the shim layer
64 // except on Windows and macOS (except for PartitionAlloc-Everywhere) where the
65 // malloc intercept is deep enough that it also catches the cpp calls.
66 //
67 // In case of PartitionAlloc-Everywhere on macOS, malloc backed by
68 // allocator_shim::internal::PartitionMalloc crashes on OOM, and we need to
69 // avoid crashes in case of operator new() noexcept.  Thus, operator new()
70 // noexcept needs to be routed to
71 // allocator_shim::internal::PartitionMallocUnchecked through the shim layer.
72 #include "partition_alloc/shim/allocator_shim_override_cpp_symbols.h"
73 #endif
74 
75 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
76 #include "partition_alloc/shim/allocator_shim_override_apple_default_zone.h"
77 #else  // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
78 #include "partition_alloc/shim/allocator_shim_override_apple_symbols.h"
79 #endif  // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
80 
81 namespace allocator_shim {
82 
InitializeAllocatorShim()83 void InitializeAllocatorShim() {
84 #if !BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
85   // Prepares the default dispatch. After the intercepted malloc calls have
86   // traversed the shim this will route them to the default malloc zone.
87   InitializeDefaultDispatchToMacAllocator();
88 
89   MallocZoneFunctions functions = MallocZoneFunctionsToReplaceDefault();
90 
91   // This replaces the default malloc zone, causing calls to malloc & friends
92   // from the codebase to be routed to ShimMalloc() above.
93   ReplaceFunctionsForStoredZones(&functions);
94 #endif  // !BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
95 }
96 
97 }  // namespace allocator_shim
98 
99 // Cross-checks.
100 
101 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
102 #error The allocator shim should not be compiled when building for memory tools.
103 #endif
104 
105 #if (defined(__GNUC__) && defined(__EXCEPTIONS)) || \
106     (defined(_MSC_VER) && defined(_CPPUNWIND))
107 #error This code cannot be used when exceptions are turned on.
108 #endif
109