| 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 #include <unistd.h> | |
| 11 | 10 |
| 12 #include <memory> | 11 #include <memory> |
| 13 #include <new> | 12 #include <new> |
| 14 #include <vector> | 13 #include <vector> |
| 15 | 14 |
| 16 #include "base/atomicops.h" | 15 #include "base/atomicops.h" |
| 16 #include "base/process/process_metrics.h" |
| 17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
| 19 #include "base/threading/thread_local.h" | 19 #include "base/threading/thread_local.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 #if !defined(OS_WIN) |
| 24 #include <unistd.h> |
| 25 #endif |
| 26 |
| 23 // Some new Android NDKs (64 bit) does not expose (p)valloc anymore. These | 27 // Some new Android NDKs (64 bit) does not expose (p)valloc anymore. These |
| 24 // functions are implemented at the shim-layer level. | 28 // functions are implemented at the shim-layer level. |
| 25 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
| 26 extern "C" { | 30 extern "C" { |
| 27 void* valloc(size_t size); | 31 void* valloc(size_t size); |
| 28 void* pvalloc(size_t size); | 32 void* pvalloc(size_t size); |
| 29 } | 33 } |
| 30 #endif | 34 #endif |
| 31 | 35 |
| 32 namespace base { | 36 namespace base { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 AllocatorDispatch g_mock_dispatch = { | 175 AllocatorDispatch g_mock_dispatch = { |
| 172 &AllocatorShimTest::MockAlloc, /* alloc_function */ | 176 &AllocatorShimTest::MockAlloc, /* alloc_function */ |
| 173 &AllocatorShimTest::MockAllocZeroInit, /* alloc_zero_initialized_function */ | 177 &AllocatorShimTest::MockAllocZeroInit, /* alloc_zero_initialized_function */ |
| 174 &AllocatorShimTest::MockAllocAligned, /* alloc_aligned_function */ | 178 &AllocatorShimTest::MockAllocAligned, /* alloc_aligned_function */ |
| 175 &AllocatorShimTest::MockRealloc, /* realloc_function */ | 179 &AllocatorShimTest::MockRealloc, /* realloc_function */ |
| 176 &AllocatorShimTest::MockFree, /* free_function */ | 180 &AllocatorShimTest::MockFree, /* free_function */ |
| 177 nullptr, /* next */ | 181 nullptr, /* next */ |
| 178 }; | 182 }; |
| 179 | 183 |
| 180 TEST_F(AllocatorShimTest, InterceptLibcSymbols) { | 184 TEST_F(AllocatorShimTest, InterceptLibcSymbols) { |
| 181 const size_t kPageSize = sysconf(_SC_PAGESIZE); | 185 const size_t kPageSize = base::GetPageSize(); |
| 182 InsertAllocatorDispatch(&g_mock_dispatch); | 186 InsertAllocatorDispatch(&g_mock_dispatch); |
| 183 | 187 |
| 184 void* alloc_ptr = malloc(19); | 188 void* alloc_ptr = malloc(19); |
| 185 ASSERT_NE(nullptr, alloc_ptr); | 189 ASSERT_NE(nullptr, alloc_ptr); |
| 186 ASSERT_GE(allocs_intercepted_by_size[19], 1u); | 190 ASSERT_GE(allocs_intercepted_by_size[19], 1u); |
| 187 | 191 |
| 188 void* zero_alloc_ptr = calloc(2, 23); | 192 void* zero_alloc_ptr = calloc(2, 23); |
| 189 ASSERT_NE(nullptr, zero_alloc_ptr); | 193 ASSERT_NE(nullptr, zero_alloc_ptr); |
| 190 ASSERT_GE(zero_allocs_intercepted_by_size[2 * 23], 1u); | 194 ASSERT_GE(zero_allocs_intercepted_by_size[2 * 23], 1u); |
| 191 | 195 |
| 196 #if !defined(OS_WIN) |
| 192 void* memalign_ptr = memalign(128, 53); | 197 void* memalign_ptr = memalign(128, 53); |
| 193 ASSERT_NE(nullptr, memalign_ptr); | 198 ASSERT_NE(nullptr, memalign_ptr); |
| 194 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(memalign_ptr) % 128); | 199 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(memalign_ptr) % 128); |
| 195 ASSERT_GE(aligned_allocs_intercepted_by_alignment[128], 1u); | 200 ASSERT_GE(aligned_allocs_intercepted_by_alignment[128], 1u); |
| 196 ASSERT_GE(aligned_allocs_intercepted_by_size[53], 1u); | 201 ASSERT_GE(aligned_allocs_intercepted_by_size[53], 1u); |
| 197 | 202 |
| 198 void* posix_memalign_ptr = nullptr; | 203 void* posix_memalign_ptr = nullptr; |
| 199 int res = posix_memalign(&posix_memalign_ptr, 256, 59); | 204 int res = posix_memalign(&posix_memalign_ptr, 256, 59); |
| 200 ASSERT_EQ(0, res); | 205 ASSERT_EQ(0, res); |
| 201 ASSERT_NE(nullptr, posix_memalign_ptr); | 206 ASSERT_NE(nullptr, posix_memalign_ptr); |
| 202 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(posix_memalign_ptr) % 256); | 207 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(posix_memalign_ptr) % 256); |
| 203 ASSERT_GE(aligned_allocs_intercepted_by_alignment[256], 1u); | 208 ASSERT_GE(aligned_allocs_intercepted_by_alignment[256], 1u); |
| 204 ASSERT_GE(aligned_allocs_intercepted_by_size[59], 1u); | 209 ASSERT_GE(aligned_allocs_intercepted_by_size[59], 1u); |
| 205 | 210 |
| 206 void* valloc_ptr = valloc(61); | 211 void* valloc_ptr = valloc(61); |
| 207 ASSERT_NE(nullptr, valloc_ptr); | 212 ASSERT_NE(nullptr, valloc_ptr); |
| 208 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(valloc_ptr) % kPageSize); | 213 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(valloc_ptr) % kPageSize); |
| 209 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); | 214 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); |
| 210 ASSERT_GE(aligned_allocs_intercepted_by_size[61], 1u); | 215 ASSERT_GE(aligned_allocs_intercepted_by_size[61], 1u); |
| 211 | 216 |
| 212 void* pvalloc_ptr = pvalloc(67); | 217 void* pvalloc_ptr = pvalloc(67); |
| 213 ASSERT_NE(nullptr, pvalloc_ptr); | 218 ASSERT_NE(nullptr, pvalloc_ptr); |
| 214 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize); | 219 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize); |
| 215 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); | 220 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); |
| 216 // pvalloc rounds the size up to the next page. | 221 // pvalloc rounds the size up to the next page. |
| 217 ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u); | 222 ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u); |
| 223 #endif // OS_WIN |
| 218 | 224 |
| 219 char* realloc_ptr = static_cast<char*>(realloc(nullptr, 71)); | 225 char* realloc_ptr = static_cast<char*>(realloc(nullptr, 71)); |
| 220 ASSERT_NE(nullptr, realloc_ptr); | 226 ASSERT_NE(nullptr, realloc_ptr); |
| 221 ASSERT_GE(reallocs_intercepted_by_size[71], 1u); | 227 ASSERT_GE(reallocs_intercepted_by_size[71], 1u); |
| 222 ASSERT_GE(reallocs_intercepted_by_addr[Hash(nullptr)], 1u); | 228 ASSERT_GE(reallocs_intercepted_by_addr[Hash(nullptr)], 1u); |
| 223 strcpy(realloc_ptr, "foobar"); | 229 strcpy(realloc_ptr, "foobar"); |
| 230 void* old_realloc_ptr = realloc_ptr; |
| 224 realloc_ptr = static_cast<char*>(realloc(realloc_ptr, 73)); | 231 realloc_ptr = static_cast<char*>(realloc(realloc_ptr, 73)); |
| 225 ASSERT_GE(reallocs_intercepted_by_size[73], 1u); | 232 ASSERT_GE(reallocs_intercepted_by_size[73], 1u); |
| 226 ASSERT_GE(reallocs_intercepted_by_addr[Hash(realloc_ptr)], 1u); | 233 ASSERT_GE(reallocs_intercepted_by_addr[Hash(old_realloc_ptr)], 1u); |
| 227 ASSERT_EQ(0, strcmp(realloc_ptr, "foobar")); | 234 ASSERT_EQ(0, strcmp(realloc_ptr, "foobar")); |
| 228 | 235 |
| 229 free(alloc_ptr); | 236 free(alloc_ptr); |
| 230 ASSERT_GE(frees_intercepted_by_addr[Hash(alloc_ptr)], 1u); | 237 ASSERT_GE(frees_intercepted_by_addr[Hash(alloc_ptr)], 1u); |
| 231 | 238 |
| 232 free(zero_alloc_ptr); | 239 free(zero_alloc_ptr); |
| 233 ASSERT_GE(frees_intercepted_by_addr[Hash(zero_alloc_ptr)], 1u); | 240 ASSERT_GE(frees_intercepted_by_addr[Hash(zero_alloc_ptr)], 1u); |
| 234 | 241 |
| 242 #if !defined(OS_WIN) |
| 235 free(memalign_ptr); | 243 free(memalign_ptr); |
| 236 ASSERT_GE(frees_intercepted_by_addr[Hash(memalign_ptr)], 1u); | 244 ASSERT_GE(frees_intercepted_by_addr[Hash(memalign_ptr)], 1u); |
| 237 | 245 |
| 238 free(posix_memalign_ptr); | 246 free(posix_memalign_ptr); |
| 239 ASSERT_GE(frees_intercepted_by_addr[Hash(posix_memalign_ptr)], 1u); | 247 ASSERT_GE(frees_intercepted_by_addr[Hash(posix_memalign_ptr)], 1u); |
| 240 | 248 |
| 241 free(valloc_ptr); | 249 free(valloc_ptr); |
| 242 ASSERT_GE(frees_intercepted_by_addr[Hash(valloc_ptr)], 1u); | 250 ASSERT_GE(frees_intercepted_by_addr[Hash(valloc_ptr)], 1u); |
| 243 | 251 |
| 244 free(pvalloc_ptr); | 252 free(pvalloc_ptr); |
| 245 ASSERT_GE(frees_intercepted_by_addr[Hash(pvalloc_ptr)], 1u); | 253 ASSERT_GE(frees_intercepted_by_addr[Hash(pvalloc_ptr)], 1u); |
| 254 #endif // OS_WIN |
| 246 | 255 |
| 247 free(realloc_ptr); | 256 free(realloc_ptr); |
| 248 ASSERT_GE(frees_intercepted_by_addr[Hash(realloc_ptr)], 1u); | 257 ASSERT_GE(frees_intercepted_by_addr[Hash(realloc_ptr)], 1u); |
| 249 | 258 |
| 250 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); | 259 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); |
| 251 | 260 |
| 252 void* non_hooked_ptr = malloc(4095); | 261 void* non_hooked_ptr = malloc(4095); |
| 253 ASSERT_NE(nullptr, non_hooked_ptr); | 262 ASSERT_NE(nullptr, non_hooked_ptr); |
| 254 ASSERT_EQ(0u, allocs_intercepted_by_size[4095]); | 263 ASSERT_EQ(0u, allocs_intercepted_by_size[4095]); |
| 255 free(non_hooked_ptr); | 264 free(non_hooked_ptr); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 event.Signal(); | 325 event.Signal(); |
| 317 for (int i = 0; i < kNumThreads; ++i) | 326 for (int i = 0; i < kNumThreads; ++i) |
| 318 PlatformThread::Join(threads[i]); | 327 PlatformThread::Join(threads[i]); |
| 319 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); | 328 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); |
| 320 ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls()); | 329 ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls()); |
| 321 } | 330 } |
| 322 | 331 |
| 323 } // namespace | 332 } // namespace |
| 324 } // namespace allocator | 333 } // namespace allocator |
| 325 } // namespace base | 334 } // namespace base |
| OLD | NEW |