| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 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 <mach/mach.h> | |
| 6 | |
| 7 #include "base/allocator/allocator_interception_mac.h" | |
| 8 #include "base/allocator/allocator_shim.h" | |
| 9 #include "base/allocator/malloc_zone_functions_mac.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace base { | |
| 13 namespace allocator { | |
| 14 | |
| 15 namespace { | |
| 16 void ResetMallocZone(ChromeMallocZone* zone) { | |
| 17 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
| 18 ReplaceZoneFunctions(zone, &functions); | |
| 19 } | |
| 20 | |
| 21 void ResetAllMallocZones() { | |
| 22 ChromeMallocZone* default_malloc_zone = | |
| 23 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone()); | |
| 24 ResetMallocZone(default_malloc_zone); | |
| 25 | |
| 26 vm_address_t* zones; | |
| 27 unsigned int count; | |
| 28 kern_return_t kr = malloc_get_all_zones(mach_task_self(), 0, &zones, &count); | |
| 29 if (kr != KERN_SUCCESS) | |
| 30 return; | |
| 31 for (unsigned int i = 0; i < count; ++i) { | |
| 32 ChromeMallocZone* zone = reinterpret_cast<ChromeMallocZone*>(zones[i]); | |
| 33 ResetMallocZone(zone); | |
| 34 } | |
| 35 } | |
| 36 } // namespace | |
| 37 | |
| 38 class AllocatorInterceptionTest : public testing::Test { | |
| 39 protected: | |
| 40 void TearDown() override { | |
| 41 ResetAllMallocZones(); | |
| 42 ClearAllMallocZonesForTesting(); | |
| 43 } | |
| 44 }; | |
| 45 | |
| 46 TEST_F(AllocatorInterceptionTest, ShimNewMallocZones) { | |
| 47 InitializeAllocatorShim(); | |
| 48 ChromeMallocZone* default_malloc_zone = | |
| 49 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone()); | |
| 50 | |
| 51 malloc_zone_t new_zone; | |
| 52 memset(&new_zone, 1, sizeof(malloc_zone_t)); | |
| 53 malloc_zone_register(&new_zone); | |
| 54 EXPECT_NE(new_zone.malloc, default_malloc_zone->malloc); | |
| 55 ShimNewMallocZones(); | |
| 56 EXPECT_EQ(new_zone.malloc, default_malloc_zone->malloc); | |
| 57 | |
| 58 malloc_zone_unregister(&new_zone); | |
| 59 } | |
| 60 | |
| 61 } // namespace allocator | |
| 62 } // namespace base | |
| OLD | NEW |