| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/allocator/allocator_shim.h" | 5 #include "base/allocator/allocator_shim.h" |
| 6 | 6 |
| 7 #include <malloc.h> | 7 #include <malloc.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 AllocatorDispatch g_mock_dispatch = { | 175 AllocatorDispatch g_mock_dispatch = { |
| 176 &AllocatorShimTest::MockAlloc, /* alloc_function */ | 176 &AllocatorShimTest::MockAlloc, /* alloc_function */ |
| 177 &AllocatorShimTest::MockAllocZeroInit, /* alloc_zero_initialized_function */ | 177 &AllocatorShimTest::MockAllocZeroInit, /* alloc_zero_initialized_function */ |
| 178 &AllocatorShimTest::MockAllocAligned, /* alloc_aligned_function */ | 178 &AllocatorShimTest::MockAllocAligned, /* alloc_aligned_function */ |
| 179 &AllocatorShimTest::MockRealloc, /* realloc_function */ | 179 &AllocatorShimTest::MockRealloc, /* realloc_function */ |
| 180 &AllocatorShimTest::MockFree, /* free_function */ | 180 &AllocatorShimTest::MockFree, /* free_function */ |
| 181 nullptr, /* next */ | 181 nullptr, /* next */ |
| 182 }; | 182 }; |
| 183 | 183 |
| 184 TEST_F(AllocatorShimTest, InterceptLibcSymbols) { | 184 TEST_F(AllocatorShimTest, InterceptLibcSymbols) { |
| 185 const size_t kPageSize = base::GetPageSize(); | |
| 186 InsertAllocatorDispatch(&g_mock_dispatch); | 185 InsertAllocatorDispatch(&g_mock_dispatch); |
| 187 | 186 |
| 188 void* alloc_ptr = malloc(19); | 187 void* alloc_ptr = malloc(19); |
| 189 ASSERT_NE(nullptr, alloc_ptr); | 188 ASSERT_NE(nullptr, alloc_ptr); |
| 190 ASSERT_GE(allocs_intercepted_by_size[19], 1u); | 189 ASSERT_GE(allocs_intercepted_by_size[19], 1u); |
| 191 | 190 |
| 192 void* zero_alloc_ptr = calloc(2, 23); | 191 void* zero_alloc_ptr = calloc(2, 23); |
| 193 ASSERT_NE(nullptr, zero_alloc_ptr); | 192 ASSERT_NE(nullptr, zero_alloc_ptr); |
| 194 ASSERT_GE(zero_allocs_intercepted_by_size[2 * 23], 1u); | 193 ASSERT_GE(zero_allocs_intercepted_by_size[2 * 23], 1u); |
| 195 | 194 |
| 196 #if !defined(OS_WIN) | 195 #if !defined(OS_WIN) |
| 197 void* memalign_ptr = memalign(128, 53); | 196 void* memalign_ptr = memalign(128, 53); |
| 198 ASSERT_NE(nullptr, memalign_ptr); | 197 ASSERT_NE(nullptr, memalign_ptr); |
| 199 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(memalign_ptr) % 128); | 198 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(memalign_ptr) % 128); |
| 200 ASSERT_GE(aligned_allocs_intercepted_by_alignment[128], 1u); | 199 ASSERT_GE(aligned_allocs_intercepted_by_alignment[128], 1u); |
| 201 ASSERT_GE(aligned_allocs_intercepted_by_size[53], 1u); | 200 ASSERT_GE(aligned_allocs_intercepted_by_size[53], 1u); |
| 202 | 201 |
| 203 void* posix_memalign_ptr = nullptr; | 202 void* posix_memalign_ptr = nullptr; |
| 204 int res = posix_memalign(&posix_memalign_ptr, 256, 59); | 203 int res = posix_memalign(&posix_memalign_ptr, 256, 59); |
| 205 ASSERT_EQ(0, res); | 204 ASSERT_EQ(0, res); |
| 206 ASSERT_NE(nullptr, posix_memalign_ptr); | 205 ASSERT_NE(nullptr, posix_memalign_ptr); |
| 207 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(posix_memalign_ptr) % 256); | 206 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(posix_memalign_ptr) % 256); |
| 208 ASSERT_GE(aligned_allocs_intercepted_by_alignment[256], 1u); | 207 ASSERT_GE(aligned_allocs_intercepted_by_alignment[256], 1u); |
| 209 ASSERT_GE(aligned_allocs_intercepted_by_size[59], 1u); | 208 ASSERT_GE(aligned_allocs_intercepted_by_size[59], 1u); |
| 210 | 209 |
| 211 void* valloc_ptr = valloc(61); | 210 void* valloc_ptr = valloc(61); |
| 212 ASSERT_NE(nullptr, valloc_ptr); | 211 ASSERT_NE(nullptr, valloc_ptr); |
| 212 const size_t kPageSize = base::GetPageSize(); |
| 213 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(valloc_ptr) % kPageSize); | 213 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(valloc_ptr) % kPageSize); |
| 214 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); | 214 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); |
| 215 ASSERT_GE(aligned_allocs_intercepted_by_size[61], 1u); | 215 ASSERT_GE(aligned_allocs_intercepted_by_size[61], 1u); |
| 216 | 216 |
| 217 void* pvalloc_ptr = pvalloc(67); | 217 void* pvalloc_ptr = pvalloc(67); |
| 218 ASSERT_NE(nullptr, pvalloc_ptr); | 218 ASSERT_NE(nullptr, pvalloc_ptr); |
| 219 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize); | 219 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize); |
| 220 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); | 220 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); |
| 221 // pvalloc rounds the size up to the next page. | 221 // pvalloc rounds the size up to the next page. |
| 222 ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u); | 222 ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 event.Signal(); | 325 event.Signal(); |
| 326 for (int i = 0; i < kNumThreads; ++i) | 326 for (int i = 0; i < kNumThreads; ++i) |
| 327 PlatformThread::Join(threads[i]); | 327 PlatformThread::Join(threads[i]); |
| 328 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); | 328 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); |
| 329 ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls()); | 329 ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls()); |
| 330 } | 330 } |
| 331 | 331 |
| 332 } // namespace | 332 } // namespace |
| 333 } // namespace allocator | 333 } // namespace allocator |
| 334 } // namespace base | 334 } // namespace base |
| OLD | NEW |