test_mem_pool.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #define TESTING_MEM_POOL
23 
24 #include <gsl/gsl>
25 
26 #include <vector>
27 
28 #include <test.h>
30 
31 void
32 memory_manager_ut::test_mem_pool_free_zero()
33 {
34  mem_pool<128, 3> pool{100};
35 
36  pool.free(0);
37  pool.free(0xFFFFFFFFFFFFFFFF);
38 }
39 
40 void
41 memory_manager_ut::test_mem_pool_free_heap_twice()
42 {
43  mem_pool<128, 3> pool{100};
44 
45  auto &&addr1 = pool.alloc(1 << 3);
46  pool.free(addr1);
47  pool.free(addr1);
48 }
49 
50 void
51 memory_manager_ut::test_mem_pool_invalid_pool()
52 {
53  using pool_type = mem_pool<128, 3>;
54  this->expect_exception([&] { pool_type pool{0}; }, ""_ut_lee);
55  this->expect_exception([&] { pool_type pool{0xFFFFFFFFFFFFFFF0}; }, ""_ut_lee);
56 }
57 
58 void
59 memory_manager_ut::test_mem_pool_malloc_zero()
60 {
61  mem_pool<128, 3> pool{100};
62  this->expect_exception([&] { pool.alloc(0); }, ""_ut_ffe);
63 }
64 
65 void
66 memory_manager_ut::test_mem_pool_multiple_malloc_heap_should_be_contiguous()
67 {
68  mem_pool<128, 3> pool{100};
73 
74  addr1 = pool.alloc((1 << 3));
75  addr2 = pool.alloc((1 << 3));
76  addr3 = pool.alloc((1 << 3));
77  addr4 = pool.alloc((1 << 3));
78 
79  this->expect_true(addr1 == 100 + ((1 << 3) * 0)); // 100
80  this->expect_true(addr2 == 100 + ((1 << 3) * 1)); // 108
81  this->expect_true(addr3 == 100 + ((1 << 3) * 2)); // 116
82  this->expect_true(addr4 == 100 + ((1 << 3) * 3)); // 124
83 
84  pool.free(addr1);
85  pool.free(addr2);
86  pool.free(addr3);
87  pool.free(addr4);
88 
89  addr1 = pool.alloc((1 << 3) + 2);
90  addr2 = pool.alloc((1 << 3) + 2);
91  addr3 = pool.alloc((1 << 3) + 2);
92  addr4 = pool.alloc((1 << 3) * 4);
93 
94  this->expect_true(addr1 == 132 + ((1 << 3) * 0)); // 132
95  this->expect_true(addr2 == 132 + ((1 << 3) * 2)); // 148
96  this->expect_true(addr3 == 132 + ((1 << 3) * 4)); // 164
97  this->expect_true(addr4 == 132 + ((1 << 3) * 6)); // 180
98 
99  pool.free(addr1);
100  pool.free(addr2);
101  pool.free(addr3);
102  pool.free(addr4);
103 }
104 
105 void
106 memory_manager_ut::test_mem_pool_malloc_heap_all_of_memory()
107 {
108  mem_pool<128, 3> pool{100};
110 
111  for (auto i = 0; i < 16; i++)
112  addrs.push_back(pool.alloc(1 << 3));
113 
114  this->expect_exception([&] { pool.alloc(1 << 3); }, ""_ut_bae);
115 
116  for (const auto &addr : addrs)
117  pool.free(addr);
118 
119  for (auto i = 0; i < 16; i++)
120  addrs.push_back(pool.alloc(1 << 3));
121 
122  this->expect_exception([&] { pool.alloc(1 << 3); }, ""_ut_bae);
123 
124  for (const auto &addr : addrs)
125  pool.free(addr);
126 }
127 
128 void
129 memory_manager_ut::test_mem_pool_malloc_heap_all_of_memory_one_block()
130 {
131  mem_pool<128, 3> pool{100};
132  this->expect_true(pool.alloc(128) == 100);
133 }
134 
135 void
136 memory_manager_ut::test_mem_pool_malloc_heap_all_memory_fragmented()
137 {
138  mem_pool<128, 3> pool{100};
140 
141  for (auto i = 0; i < 16; i++)
142  addrs.push_back(pool.alloc(1 << 3));
143 
144  for (const auto &addr : addrs)
145  pool.free(addr);
146 
147  this->expect_true(pool.alloc(128) == 100);
148 }
149 
150 void
151 memory_manager_ut::test_mem_pool_malloc_heap_too_much_memory_one_block()
152 {
153  mem_pool<128, 3> pool{100};
154  this->expect_exception([&] { pool.alloc(136); }, ""_ut_ffe);
155 }
156 
157 void
158 memory_manager_ut::test_mem_pool_malloc_heap_too_much_memory_non_block_size()
159 {
160  mem_pool<128, 3> pool{100};
161  this->expect_exception([&] { pool.alloc(129); }, ""_ut_ffe);
162 }
163 
164 void
165 memory_manager_ut::test_mem_pool_malloc_heap_massive()
166 {
167  mem_pool<128, 3> pool{100};
168  this->expect_exception([&] { pool.alloc(0xFFFFFFFFFFFFFFFF); }, ""_ut_ffe);
169 }
170 
171 void
172 memory_manager_ut::test_mem_pool_size_out_of_bounds()
173 {
174  mem_pool<128, 3> pool{100};
175  this->expect_true(pool.size(0) == 0);
176 }
177 
178 void
179 memory_manager_ut::test_mem_pool_size_unallocated()
180 {
181  mem_pool<128, 3> pool{100};
182  this->expect_true(pool.size(100) == 0);
183 }
184 
185 void
186 memory_manager_ut::test_mem_pool_size()
187 {
188  mem_pool<128, 3> pool{100};
189 
190  pool.alloc(8);
191  this->expect_true(pool.size(100) == 8);
192 }
193 
194 void
195 memory_manager_ut::test_mem_pool_contains_out_of_bounds()
196 {
197  mem_pool<128, 3> pool{100};
198 
199  this->expect_false(pool.contains(0));
200  this->expect_false(pool.contains(99));
201  this->expect_false(pool.contains(228));
202  this->expect_false(pool.contains(500));
203 }
204 
205 void
206 memory_manager_ut::test_mem_pool_contains()
207 {
208  mem_pool<128, 3> pool{100};
209 
210  this->expect_true(pool.contains(100));
211  this->expect_true(pool.contains(227));
212 }
#define expect_exception(f, e)
Definition: unittest.h:162
uintptr_t integer_pointer
Definition: mem_pool.h:91
constexpr const auto addr
Definition: cpuid_x64.h:80
void free(integer_pointer addr) noexcept
Definition: mem_pool.h:169
integer_pointer alloc(size_type size)
Definition: mem_pool.h:137
uintptr_t integer_pointer
Definition: cache_x64.h:36
#define expect_false(a)
#define expect_true(a)