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/win_heap.h"
16 :
17 : #include "gtest/gtest.h"
18 :
19 : namespace agent {
20 : namespace asan {
21 : namespace heaps {
22 :
23 : namespace {
24 :
25 : class TestWinHeap : public WinHeap {
26 : public:
27 : using WinHeap::heap_lock_held_;
28 : };
29 :
30 : } // namespace
31 :
32 E : TEST(WinHeapTest, GetHeapTypeIsValid) {
33 E : WinHeap h;
34 E : EXPECT_EQ(kWinHeap, h.GetHeapType());
35 E : }
36 :
37 E : TEST(WinHeapTest, FeaturesAreValid) {
38 E : WinHeap h;
39 E : EXPECT_EQ(WinHeap::kHeapSupportsGetAllocationSize,
40 E : h.GetHeapFeatures());
41 E : }
42 :
43 E : TEST(WinHeapTest, HeapTest) {
44 E : WinHeap h;
45 :
46 : // Allocate and free a zero-sized allocation. This should succeed
47 : // by definition.
48 E : void* alloc = h.Allocate(0);
49 E : EXPECT_TRUE(h.Free(alloc));
50 :
51 : // Make a bunch of different sized allocations.
52 E : std::set<void*> allocs;
53 E : for (size_t i = 1; i < 1024 * 1024; i <<= 1) {
54 E : void* alloc = h.Allocate(i);
55 E : allocs.insert(alloc);
56 E : }
57 :
58 : // Now free them.
59 E : std::set<void*>::const_iterator it = allocs.begin();
60 E : for (; it != allocs.end(); ++it)
61 E : EXPECT_TRUE(h.Free(*it));
62 E : }
63 :
64 E : TEST(WinHeapTest, GetAllocationSize) {
65 E : WinHeap h;
66 :
67 E : void* alloc = h.Allocate(67);
68 E : ASSERT_TRUE(alloc != NULL);
69 E : EXPECT_EQ(67u, h.GetAllocationSize(alloc));
70 E : }
71 :
72 E : TEST(WinHeapTest, Lock) {
73 E : TestWinHeap h;
74 :
75 E : EXPECT_FALSE(h.heap_lock_held_);
76 E : h.Lock();
77 E : EXPECT_TRUE(h.heap_lock_held_);
78 :
79 : // TryLock does not acquire the underlying heap lock, as there's no
80 : // 'try' functionality for it.
81 E : EXPECT_TRUE(h.TryLock());
82 E : EXPECT_TRUE(h.heap_lock_held_);
83 E : h.Unlock();
84 E : EXPECT_TRUE(h.heap_lock_held_);
85 E : h.Unlock();
86 E : EXPECT_FALSE(h.heap_lock_held_);
87 E : }
88 :
89 : } // namespace heaps
90 : } // namespace asan
91 : } // namespace agent
|