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