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 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 47 TEST_F(AllocatorInterceptionTest, ShimNewMallocZones) { |
| 48 InitializeAllocatorShim(); |
| 49 ChromeMallocZone* default_malloc_zone = |
| 50 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone()); |
| 51 |
| 52 malloc_zone_t new_zone; |
| 53 memset(&new_zone, 1, sizeof(malloc_zone_t)); |
| 54 malloc_zone_register(&new_zone); |
| 55 EXPECT_NE(new_zone.malloc, default_malloc_zone->malloc); |
| 56 ShimNewMallocZones(); |
| 57 EXPECT_EQ(new_zone.malloc, default_malloc_zone->malloc); |
| 58 |
| 59 malloc_zone_unregister(&new_zone); |
| 60 } |
| 61 #endif |
| 62 |
| 63 } // namespace allocator |
| 64 } // namespace base |
OLD | NEW |