Chromium Code Reviews| 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" |
| 17 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 19 #include "base/threading/thread_local.h" | 18 #include "base/threading/thread_local.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 21 |
| 22 #if !defined(OS_WIN) | |
| 23 #include <unistd.h> | |
| 24 #endif | |
| 25 | |
| 23 // Some new Android NDKs (64 bit) does not expose (p)valloc anymore. These | 26 // Some new Android NDKs (64 bit) does not expose (p)valloc anymore. These |
| 24 // functions are implemented at the shim-layer level. | 27 // functions are implemented at the shim-layer level. |
| 25 #if defined(OS_ANDROID) | 28 #if defined(OS_ANDROID) |
| 26 extern "C" { | 29 extern "C" { |
| 27 void* valloc(size_t size); | 30 void* valloc(size_t size); |
| 28 void* pvalloc(size_t size); | 31 void* pvalloc(size_t size); |
| 29 } | 32 } |
| 30 #endif | 33 #endif |
| 31 | 34 |
| 32 namespace base { | 35 namespace base { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 void* res = realloc(reinterpret_cast<void*>(0x420ul), 1); | 164 void* res = realloc(reinterpret_cast<void*>(0x420ul), 1); |
| 162 EXPECT_EQ(reinterpret_cast<void*>(0x420ul), res); | 165 EXPECT_EQ(reinterpret_cast<void*>(0x420ul), res); |
| 163 } | 166 } |
| 164 | 167 |
| 165 private: | 168 private: |
| 166 WaitableEvent* event_; | 169 WaitableEvent* event_; |
| 167 }; | 170 }; |
| 168 | 171 |
| 169 AllocatorShimTest* AllocatorShimTest::instance_ = nullptr; | 172 AllocatorShimTest* AllocatorShimTest::instance_ = nullptr; |
| 170 | 173 |
| 174 inline size_t GetPageSize() { | |
|
Primiano Tucci (use gerrit)
2016/07/12 14:51:05
I think you can use GetPageSize() from base/proces
Sigurður Ásgeirsson
2016/07/14 19:04:27
Done.
I wonder if it's safe to use it also in all
Primiano Tucci (use gerrit)
2016/07/15 14:02:10
the main reason why I didn't use it straight there
| |
| 175 #if defined(OS_WIN) | |
| 176 return 4096; | |
| 177 #else | |
| 178 static size_t pagesize = 0; | |
| 179 if (!pagesize) | |
| 180 pagesize = sysconf(_SC_PAGESIZE); | |
| 181 return pagesize; | |
| 182 #endif | |
| 183 } | |
| 184 | |
| 171 AllocatorDispatch g_mock_dispatch = { | 185 AllocatorDispatch g_mock_dispatch = { |
| 172 &AllocatorShimTest::MockAlloc, /* alloc_function */ | 186 &AllocatorShimTest::MockAlloc, /* alloc_function */ |
| 173 &AllocatorShimTest::MockAllocZeroInit, /* alloc_zero_initialized_function */ | 187 &AllocatorShimTest::MockAllocZeroInit, /* alloc_zero_initialized_function */ |
| 174 &AllocatorShimTest::MockAllocAligned, /* alloc_aligned_function */ | 188 &AllocatorShimTest::MockAllocAligned, /* alloc_aligned_function */ |
| 175 &AllocatorShimTest::MockRealloc, /* realloc_function */ | 189 &AllocatorShimTest::MockRealloc, /* realloc_function */ |
| 176 &AllocatorShimTest::MockFree, /* free_function */ | 190 &AllocatorShimTest::MockFree, /* free_function */ |
| 177 nullptr, /* next */ | 191 nullptr, /* next */ |
| 178 }; | 192 }; |
| 179 | 193 |
| 180 TEST_F(AllocatorShimTest, InterceptLibcSymbols) { | 194 TEST_F(AllocatorShimTest, InterceptLibcSymbols) { |
| 181 const size_t kPageSize = sysconf(_SC_PAGESIZE); | 195 const size_t kPageSize = GetPageSize(); |
| 182 InsertAllocatorDispatch(&g_mock_dispatch); | 196 InsertAllocatorDispatch(&g_mock_dispatch); |
| 183 | 197 |
| 184 void* alloc_ptr = malloc(19); | 198 void* alloc_ptr = malloc(19); |
| 185 ASSERT_NE(nullptr, alloc_ptr); | 199 ASSERT_NE(nullptr, alloc_ptr); |
| 186 ASSERT_GE(allocs_intercepted_by_size[19], 1u); | 200 ASSERT_GE(allocs_intercepted_by_size[19], 1u); |
| 187 | 201 |
| 188 void* zero_alloc_ptr = calloc(2, 23); | 202 void* zero_alloc_ptr = calloc(2, 23); |
| 189 ASSERT_NE(nullptr, zero_alloc_ptr); | 203 ASSERT_NE(nullptr, zero_alloc_ptr); |
| 190 ASSERT_GE(zero_allocs_intercepted_by_size[2 * 23], 1u); | 204 ASSERT_GE(zero_allocs_intercepted_by_size[2 * 23], 1u); |
| 191 | 205 |
| 206 #if !defined(OS_WIN) | |
| 192 void* memalign_ptr = memalign(128, 53); | 207 void* memalign_ptr = memalign(128, 53); |
| 193 ASSERT_NE(nullptr, memalign_ptr); | 208 ASSERT_NE(nullptr, memalign_ptr); |
| 194 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(memalign_ptr) % 128); | 209 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(memalign_ptr) % 128); |
| 195 ASSERT_GE(aligned_allocs_intercepted_by_alignment[128], 1u); | 210 ASSERT_GE(aligned_allocs_intercepted_by_alignment[128], 1u); |
| 196 ASSERT_GE(aligned_allocs_intercepted_by_size[53], 1u); | 211 ASSERT_GE(aligned_allocs_intercepted_by_size[53], 1u); |
| 197 | 212 |
| 198 void* posix_memalign_ptr = nullptr; | 213 void* posix_memalign_ptr = nullptr; |
| 199 int res = posix_memalign(&posix_memalign_ptr, 256, 59); | 214 int res = posix_memalign(&posix_memalign_ptr, 256, 59); |
| 200 ASSERT_EQ(0, res); | 215 ASSERT_EQ(0, res); |
| 201 ASSERT_NE(nullptr, posix_memalign_ptr); | 216 ASSERT_NE(nullptr, posix_memalign_ptr); |
| 202 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(posix_memalign_ptr) % 256); | 217 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(posix_memalign_ptr) % 256); |
| 203 ASSERT_GE(aligned_allocs_intercepted_by_alignment[256], 1u); | 218 ASSERT_GE(aligned_allocs_intercepted_by_alignment[256], 1u); |
| 204 ASSERT_GE(aligned_allocs_intercepted_by_size[59], 1u); | 219 ASSERT_GE(aligned_allocs_intercepted_by_size[59], 1u); |
| 205 | 220 |
| 206 void* valloc_ptr = valloc(61); | 221 void* valloc_ptr = valloc(61); |
| 207 ASSERT_NE(nullptr, valloc_ptr); | 222 ASSERT_NE(nullptr, valloc_ptr); |
| 208 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(valloc_ptr) % kPageSize); | 223 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(valloc_ptr) % kPageSize); |
| 209 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); | 224 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); |
| 210 ASSERT_GE(aligned_allocs_intercepted_by_size[61], 1u); | 225 ASSERT_GE(aligned_allocs_intercepted_by_size[61], 1u); |
| 211 | 226 |
| 212 void* pvalloc_ptr = pvalloc(67); | 227 void* pvalloc_ptr = pvalloc(67); |
| 213 ASSERT_NE(nullptr, pvalloc_ptr); | 228 ASSERT_NE(nullptr, pvalloc_ptr); |
| 214 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize); | 229 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize); |
| 215 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); | 230 ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u); |
| 216 // pvalloc rounds the size up to the next page. | 231 // pvalloc rounds the size up to the next page. |
| 217 ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u); | 232 ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u); |
| 233 #endif // OS_WIN | |
| 218 | 234 |
| 219 char* realloc_ptr = static_cast<char*>(realloc(nullptr, 71)); | 235 char* realloc_ptr = static_cast<char*>(realloc(nullptr, 71)); |
| 220 ASSERT_NE(nullptr, realloc_ptr); | 236 ASSERT_NE(nullptr, realloc_ptr); |
| 221 ASSERT_GE(reallocs_intercepted_by_size[71], 1u); | 237 ASSERT_GE(reallocs_intercepted_by_size[71], 1u); |
| 222 ASSERT_GE(reallocs_intercepted_by_addr[Hash(nullptr)], 1u); | 238 ASSERT_GE(reallocs_intercepted_by_addr[Hash(nullptr)], 1u); |
| 223 strcpy(realloc_ptr, "foobar"); | 239 strcpy(realloc_ptr, "foobar"); |
| 240 void* old_realloc_ptr = realloc_ptr; | |
| 224 realloc_ptr = static_cast<char*>(realloc(realloc_ptr, 73)); | 241 realloc_ptr = static_cast<char*>(realloc(realloc_ptr, 73)); |
| 225 ASSERT_GE(reallocs_intercepted_by_size[73], 1u); | 242 ASSERT_GE(reallocs_intercepted_by_size[73], 1u); |
| 226 ASSERT_GE(reallocs_intercepted_by_addr[Hash(realloc_ptr)], 1u); | 243 ASSERT_GE(reallocs_intercepted_by_addr[Hash(old_realloc_ptr)], 1u); |
|
Primiano Tucci (use gerrit)
2016/07/12 14:51:05
uh?
Sigurður Ásgeirsson
2016/07/14 19:04:27
The test fixture records the inbound pointer to th
Primiano Tucci (use gerrit)
2016/07/15 14:02:10
Ahh I see. makes sense. Thanks.
| |
| 227 ASSERT_EQ(0, strcmp(realloc_ptr, "foobar")); | 244 ASSERT_EQ(0, strcmp(realloc_ptr, "foobar")); |
| 228 | 245 |
| 229 free(alloc_ptr); | 246 free(alloc_ptr); |
| 230 ASSERT_GE(frees_intercepted_by_addr[Hash(alloc_ptr)], 1u); | 247 ASSERT_GE(frees_intercepted_by_addr[Hash(alloc_ptr)], 1u); |
| 231 | 248 |
| 232 free(zero_alloc_ptr); | 249 free(zero_alloc_ptr); |
| 233 ASSERT_GE(frees_intercepted_by_addr[Hash(zero_alloc_ptr)], 1u); | 250 ASSERT_GE(frees_intercepted_by_addr[Hash(zero_alloc_ptr)], 1u); |
| 234 | 251 |
| 252 #if !defined(OS_WIN) | |
| 235 free(memalign_ptr); | 253 free(memalign_ptr); |
| 236 ASSERT_GE(frees_intercepted_by_addr[Hash(memalign_ptr)], 1u); | 254 ASSERT_GE(frees_intercepted_by_addr[Hash(memalign_ptr)], 1u); |
| 237 | 255 |
| 238 free(posix_memalign_ptr); | 256 free(posix_memalign_ptr); |
| 239 ASSERT_GE(frees_intercepted_by_addr[Hash(posix_memalign_ptr)], 1u); | 257 ASSERT_GE(frees_intercepted_by_addr[Hash(posix_memalign_ptr)], 1u); |
| 240 | 258 |
| 241 free(valloc_ptr); | 259 free(valloc_ptr); |
| 242 ASSERT_GE(frees_intercepted_by_addr[Hash(valloc_ptr)], 1u); | 260 ASSERT_GE(frees_intercepted_by_addr[Hash(valloc_ptr)], 1u); |
| 243 | 261 |
| 244 free(pvalloc_ptr); | 262 free(pvalloc_ptr); |
| 245 ASSERT_GE(frees_intercepted_by_addr[Hash(pvalloc_ptr)], 1u); | 263 ASSERT_GE(frees_intercepted_by_addr[Hash(pvalloc_ptr)], 1u); |
| 264 #endif // OS_WIN | |
| 246 | 265 |
| 247 free(realloc_ptr); | 266 free(realloc_ptr); |
| 248 ASSERT_GE(frees_intercepted_by_addr[Hash(realloc_ptr)], 1u); | 267 ASSERT_GE(frees_intercepted_by_addr[Hash(realloc_ptr)], 1u); |
| 249 | 268 |
| 250 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); | 269 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); |
| 251 | 270 |
| 252 void* non_hooked_ptr = malloc(4095); | 271 void* non_hooked_ptr = malloc(4095); |
| 253 ASSERT_NE(nullptr, non_hooked_ptr); | 272 ASSERT_NE(nullptr, non_hooked_ptr); |
| 254 ASSERT_EQ(0u, allocs_intercepted_by_size[4095]); | 273 ASSERT_EQ(0u, allocs_intercepted_by_size[4095]); |
| 255 free(non_hooked_ptr); | 274 free(non_hooked_ptr); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 event.Signal(); | 335 event.Signal(); |
| 317 for (int i = 0; i < kNumThreads; ++i) | 336 for (int i = 0; i < kNumThreads; ++i) |
| 318 PlatformThread::Join(threads[i]); | 337 PlatformThread::Join(threads[i]); |
| 319 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); | 338 RemoveAllocatorDispatchForTesting(&g_mock_dispatch); |
| 320 ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls()); | 339 ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls()); |
| 321 } | 340 } |
| 322 | 341 |
| 323 } // namespace | 342 } // namespace |
| 324 } // namespace allocator | 343 } // namespace allocator |
| 325 } // namespace base | 344 } // namespace base |
| OLD | NEW |