1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/agent/asan/heaps/simple_block_heap.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/agent/asan/heaps/win_heap.h"
19 :
20 : namespace agent {
21 : namespace asan {
22 : namespace heaps {
23 :
24 : namespace {
25 :
26 : // Provides an ordering for BlockInfo objects.
27 : struct BlockInfoLessThan {
28 E : bool operator()(const BlockInfo& bi1, const BlockInfo& bi2) const {
29 E : return bi1.header < bi2.header;
30 E : }
31 : };
32 :
33 : typedef std::set<BlockInfo, BlockInfoLessThan> BlockInfoSet;
34 :
35 : } // namespace
36 :
37 E : TEST(SimpleBlockHeapTest, GetHeapTypeIsValid) {
38 E : WinHeap win_heap;
39 E : SimpleBlockHeap h(&win_heap);
40 E : EXPECT_EQ(win_heap.GetHeapType(), h.GetHeapType());
41 E : }
42 :
43 E : TEST(SimpleBlockHeapTest, FeaturesAreValid) {
44 E : WinHeap win_heap;
45 E : SimpleBlockHeap h(&win_heap);
46 E : EXPECT_EQ(win_heap.GetHeapFeatures(), h.GetHeapFeatures());
47 E : }
48 :
49 E : TEST(SimpleBlockHeapTest, EndToEnd) {
50 E : WinHeap win_heap;
51 E : SimpleBlockHeap h(&win_heap);
52 :
53 E : BlockLayout layout = {};
54 E : BlockInfo block = {};
55 :
56 : // Allocate and free a zero-sized allocation. This should succeed
57 : // by definition.
58 E : void* alloc = h.AllocateBlock(0, 0, 0, &layout);
59 E : BlockInitialize(layout, alloc, false, &block);
60 E : EXPECT_TRUE(h.FreeBlock(block));
61 :
62 : // Make a bunch of different sized allocations.
63 E : BlockInfoSet blocks;
64 E : for (size_t i = 1; i < 1024 * 1024; i <<= 1) {
65 E : void* alloc = h.AllocateBlock(i, 0, 0, &layout);
66 E : BlockInitialize(layout, alloc, false, &block);
67 E : blocks.insert(block);
68 E : }
69 :
70 : // Now free them.
71 E : BlockInfoSet::const_iterator it = blocks.begin();
72 E : for (; it != blocks.end(); ++it)
73 E : EXPECT_TRUE(h.FreeBlock(*it));
74 E : }
75 :
76 E : TEST(SimpleBlockHeap, IsAllocated) {
77 E : WinHeap win_heap;
78 E : SimpleBlockHeap h(&win_heap);
79 :
80 E : EXPECT_FALSE(h.IsAllocated(NULL));
81 :
82 : // This heap doesn't support 'IsAllocated', so it should always return false.
83 E : void* a = h.Allocate(100);
84 E : EXPECT_FALSE(h.IsAllocated(a));
85 :
86 E : h.Free(a);
87 E : EXPECT_FALSE(h.IsAllocated(a));
88 E : }
89 :
90 E : TEST(SimpleBlockHeapTest, Lock) {
91 E : WinHeap win_heap;
92 E : SimpleBlockHeap h(&win_heap);
93 :
94 E : h.Lock();
95 E : EXPECT_TRUE(h.TryLock());
96 E : h.Unlock();
97 E : h.Unlock();
98 E : }
99 :
100 : } // namespace heaps
101 : } // namespace asan
102 : } // namespace agent
|