Skip to content

Commit 659d300

Browse files
0xZOnecopybara-github
authored andcommitted
[PartitionAlloc] Fix crash when nullptr is passed to allocator shim on macOS
This change fixes an unexpected crash that occurred when nullptr was passed to the `TryFreeDefaultFallbackToFindZoneAndFree` function. Instead of a simple `PA_CHECK(false)` that would crash even for nullptr, we now properly handle nullptr by checking if the pointer is null and only trigger the assertion for non-null pointers, logging a more descriptive error message with the pointer address. Bug: 435051700 Change-Id: I689818ede88b84d0cb07d2bb41cf7656ad002232 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6802119 Reviewed-by: Kentaro Hara <[email protected]> Commit-Queue: Yuki Shiino <[email protected]> Reviewed-by: Yuki Shiino <[email protected]> Cr-Commit-Position: refs/heads/main@{#1496755} NOKEYCHECK=True GitOrigin-RevId: 9230a68bca474d1d62901026f2c9895326187047
1 parent 218e32d commit 659d300

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/partition_alloc/shim/allocator_shim_apple.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
namespace allocator_shim {
3333

3434
void TryFreeDefaultFallbackToFindZoneAndFree(void* ptr) {
35+
if (!ptr) [[unlikely]] {
36+
return;
37+
}
38+
3539
unsigned int zone_count = 0;
3640
vm_address_t* zones = nullptr;
3741
kern_return_t result =
@@ -57,7 +61,8 @@ void TryFreeDefaultFallbackToFindZoneAndFree(void* ptr) {
5761
}
5862

5963
// There must be an owner zone.
60-
PA_CHECK(false);
64+
PA_CHECK(false) << "Oops! No zone found for "
65+
<< reinterpret_cast<uintptr_t>(ptr);
6166
}
6267

6368
} // namespace allocator_shim

src/partition_alloc/shim/allocator_shim_default_dispatch_to_partition_alloc_unittest.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ TEST(PartitionAllocAsMalloc, GoodSize) {
209209
}
210210
#endif // PA_BUILDFLAG(IS_APPLE) && PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
211211

212+
#if PA_BUILDFLAG(IS_APPLE)
213+
TEST(PartitionAllocAsMalloc, TryFreeDefaultFallbackToFindZoneAndFree_Nullptr) {
214+
TryFreeDefaultFallbackToFindZoneAndFree(nullptr);
215+
}
216+
#endif // PA_BUILDFLAG(IS_APPLE)
217+
212218
} // namespace allocator_shim::internal
213219
#endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) &&
214220
// PA_BUILDFLAG(USE_PARTITION_ALLOC)

0 commit comments

Comments
 (0)