OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/heap_profiler_allocation_register.h" | 5 #include "base/trace_event/heap_profiler_allocation_register.h" |
6 | 6 |
7 #include "base/process/process_metrics.h" | 7 #include "base/process/process_metrics.h" |
8 #include "base/trace_event/heap_profiler_allocation_context.h" | 8 #include "base/trace_event/heap_profiler_allocation_context.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 namespace trace_event { | 12 namespace trace_event { |
13 | 13 |
14 class AllocationRegisterTest : public testing::Test { | 14 class AllocationRegisterTest : public testing::Test { |
15 public: | 15 public: |
16 static const uint32_t kNumBuckets = AllocationRegister::kNumBuckets; | 16 static const uint32_t kNumBuckets = AllocationRegister::kNumBuckets; |
| 17 static const uint32_t kNumCells = AllocationRegister::kNumCells; |
17 | 18 |
18 // Returns the number of cells that the |AllocationRegister| can store per | 19 // Returns the number of cells that the |AllocationRegister| can store per |
19 // system page. | 20 // system page. |
20 size_t GetNumCellsPerPage() { | 21 size_t GetNumCellsPerPage() { |
21 return GetPageSize() / sizeof(AllocationRegister::Cell); | 22 return GetPageSize() / sizeof(AllocationRegister::Cell); |
22 } | 23 } |
23 | 24 |
24 uint32_t GetHighWaterMark(const AllocationRegister& reg) { | 25 uint32_t GetHighWaterMark(const AllocationRegister& reg) { |
25 return reg.next_unused_cell_; | 26 return reg.next_unused_cell_; |
26 } | 27 } |
27 | |
28 uint32_t GetNumCells(const AllocationRegister& reg) { | |
29 return reg.num_cells_; | |
30 } | |
31 }; | 28 }; |
32 | 29 |
33 // Iterates over all entries in the allocation register and returns the bitwise | 30 // Iterates over all entries in the allocation register and returns the bitwise |
34 // or of all addresses stored in it. | 31 // or of all addresses stored in it. |
35 uintptr_t OrAllAddresses(const AllocationRegister& reg) { | 32 uintptr_t OrAllAddresses(const AllocationRegister& reg) { |
36 uintptr_t acc = 0; | 33 uintptr_t acc = 0; |
37 | 34 |
38 for (auto i : reg) | 35 for (auto i : reg) |
39 acc |= reinterpret_cast<uintptr_t>(i.address); | 36 acc |= reinterpret_cast<uintptr_t>(i.address); |
40 | 37 |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 196 |
200 // Inserting one more entry should use a fresh cell again. | 197 // Inserting one more entry should use a fresh cell again. |
201 reg.Insert(reinterpret_cast<void*>(prime), 0, ctx); | 198 reg.Insert(reinterpret_cast<void*>(prime), 0, ctx); |
202 ASSERT_EQ(prime - 1, GetHighWaterMark(reg) - initial_water_mark); | 199 ASSERT_EQ(prime - 1, GetHighWaterMark(reg) - initial_water_mark); |
203 } | 200 } |
204 | 201 |
205 // Check that the process aborts due to hitting the guard page when inserting | 202 // Check that the process aborts due to hitting the guard page when inserting |
206 // too many elements. | 203 // too many elements. |
207 #if GTEST_HAS_DEATH_TEST | 204 #if GTEST_HAS_DEATH_TEST |
208 TEST_F(AllocationRegisterTest, OverflowDeathTest) { | 205 TEST_F(AllocationRegisterTest, OverflowDeathTest) { |
209 // Use a smaller register to prevent OOM errors on low-end devices. | 206 AllocationRegister reg; |
210 AllocationRegister reg(GetNumCellsPerPage()); | |
211 AllocationContext ctx = AllocationContext::Empty(); | 207 AllocationContext ctx = AllocationContext::Empty(); |
212 uintptr_t i; | 208 uintptr_t i; |
213 | 209 |
214 // Fill up all of the memory allocated for the register. |GetNumCells(reg)| | 210 // Fill up all of the memory allocated for the register. |kNumCells| minus 1 |
215 // minus 1 elements are inserted, because cell 0 is unused, so this should | 211 // elements are inserted, because cell 0 is unused, so this should fill up |
216 // fill up the available cells exactly. | 212 // the available cells exactly. |
217 for (i = 1; i < GetNumCells(reg); i++) { | 213 for (i = 1; i < kNumCells; i++) { |
218 reg.Insert(reinterpret_cast<void*>(i), 0, ctx); | 214 reg.Insert(reinterpret_cast<void*>(i), 0, ctx); |
219 } | 215 } |
220 | 216 |
221 // Adding just one extra element might still work because the allocated memory | 217 // Adding just one extra element might still work because the allocated memory |
222 // is rounded up to the page size. Adding a page full of elements should cause | 218 // is rounded up to the page size. Adding a page full of elements should cause |
223 // overflow. | 219 // overflow. |
224 const size_t cells_per_page = GetNumCellsPerPage(); | 220 const size_t cells_per_page = GetNumCellsPerPage(); |
225 | 221 |
226 ASSERT_DEATH(for (size_t j = 0; j < cells_per_page; j++) { | 222 ASSERT_DEATH(for (size_t j = 0; j < cells_per_page; j++) { |
227 reg.Insert(reinterpret_cast<void*>(i + j), 0, ctx); | 223 reg.Insert(reinterpret_cast<void*>(i + j), 0, ctx); |
228 }, ""); | 224 }, ""); |
229 } | 225 } |
230 #endif | 226 #endif |
231 | 227 |
232 } // namespace trace_event | 228 } // namespace trace_event |
233 } // namespace base | 229 } // namespace base |
OLD | NEW |