forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator_interception_mac_unittest.mm
64 lines (53 loc) · 1.82 KB
/
allocator_interception_mac_unittest.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <mach/mach.h>
#include "base/allocator/allocator_interception_mac.h"
#include "base/allocator/allocator_shim.h"
#include "base/allocator/malloc_zone_functions_mac.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace allocator {
namespace {
void ResetMallocZone(ChromeMallocZone* zone) {
MallocZoneFunctions& functions = GetFunctionsForZone(zone);
ReplaceZoneFunctions(zone, &functions);
}
void ResetAllMallocZones() {
ChromeMallocZone* default_malloc_zone =
reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
ResetMallocZone(default_malloc_zone);
vm_address_t* zones;
unsigned int count;
kern_return_t kr = malloc_get_all_zones(mach_task_self(), 0, &zones, &count);
if (kr != KERN_SUCCESS)
return;
for (unsigned int i = 0; i < count; ++i) {
ChromeMallocZone* zone = reinterpret_cast<ChromeMallocZone*>(zones[i]);
ResetMallocZone(zone);
}
}
} // namespace
class AllocatorInterceptionTest : public testing::Test {
protected:
void TearDown() override {
ResetAllMallocZones();
ClearAllMallocZonesForTesting();
}
};
#if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
TEST_F(AllocatorInterceptionTest, ShimNewMallocZones) {
InitializeAllocatorShim();
ChromeMallocZone* default_malloc_zone =
reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
malloc_zone_t new_zone;
memset(&new_zone, 1, sizeof(malloc_zone_t));
malloc_zone_register(&new_zone);
EXPECT_NE(new_zone.malloc, default_malloc_zone->malloc);
ShimNewMallocZones();
EXPECT_EQ(new_zone.malloc, default_malloc_zone->malloc);
malloc_zone_unregister(&new_zone);
}
#endif
} // namespace allocator
} // namespace base