| Index: base/allocator/allocator_shim_unittest.cc
|
| diff --git a/base/allocator/allocator_shim_unittest.cc b/base/allocator/allocator_shim_unittest.cc
|
| index d67183ea943b9f402d7e2bd0da25bfc853952165..39b05101ac1b0aafd2cf142b105c9aba9d7c1427 100644
|
| --- a/base/allocator/allocator_shim_unittest.cc
|
| +++ b/base/allocator/allocator_shim_unittest.cc
|
| @@ -4,7 +4,6 @@
|
|
|
| #include "base/allocator/allocator_shim.h"
|
|
|
| -#include <malloc.h>
|
| #include <stdlib.h>
|
| #include <string.h>
|
|
|
| @@ -17,9 +16,16 @@
|
| #include "base/synchronization/waitable_event.h"
|
| #include "base/threading/platform_thread.h"
|
| #include "base/threading/thread_local.h"
|
| +#include "build/build_config.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| +#if defined(OS_MACOSX)
|
| +#include <malloc/malloc.h>
|
| +#else
|
| +#include <malloc.h>
|
| +#endif
|
| +
|
| #if !defined(OS_WIN)
|
| #include <unistd.h>
|
| #endif
|
| @@ -106,6 +112,11 @@ class AllocatorShimTest : public testing::Test {
|
| self->next->free_function(self->next, address);
|
| }
|
|
|
| + static size_t MockGetSizeEstimate(const AllocatorDispatch* self,
|
| + void* address) {
|
| + return self->next->get_size_estimate_function(self->next, address);
|
| + }
|
| +
|
| static void NewHandler() {
|
| if (!instance_)
|
| return;
|
| @@ -178,10 +189,13 @@ AllocatorDispatch g_mock_dispatch = {
|
| &AllocatorShimTest::MockAllocAligned, /* alloc_aligned_function */
|
| &AllocatorShimTest::MockRealloc, /* realloc_function */
|
| &AllocatorShimTest::MockFree, /* free_function */
|
| + &AllocatorShimTest::MockGetSizeEstimate,/* get_size_estimate_function */
|
| nullptr, /* next */
|
| };
|
|
|
| TEST_F(AllocatorShimTest, InterceptLibcSymbols) {
|
| + realloc(nullptr, 9);
|
| +
|
| InsertAllocatorDispatch(&g_mock_dispatch);
|
|
|
| void* alloc_ptr = malloc(19);
|
| @@ -192,13 +206,22 @@ TEST_F(AllocatorShimTest, InterceptLibcSymbols) {
|
| ASSERT_NE(nullptr, zero_alloc_ptr);
|
| ASSERT_GE(zero_allocs_intercepted_by_size[2 * 23], 1u);
|
|
|
| -#if !defined(OS_WIN)
|
| +#if !defined(OS_WIN) && !defined(OS_MACOSX)
|
| void* memalign_ptr = memalign(128, 53);
|
| ASSERT_NE(nullptr, memalign_ptr);
|
| ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(memalign_ptr) % 128);
|
| ASSERT_GE(aligned_allocs_intercepted_by_alignment[128], 1u);
|
| ASSERT_GE(aligned_allocs_intercepted_by_size[53], 1u);
|
|
|
| + void* pvalloc_ptr = pvalloc(67);
|
| + ASSERT_NE(nullptr, pvalloc_ptr);
|
| + ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize);
|
| + ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u);
|
| + // pvalloc rounds the size up to the next page.
|
| + ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u);
|
| +#endif // !OS_WIN && !OS_MACOSX
|
| +
|
| +#if !defined(OS_WIN)
|
| void* posix_memalign_ptr = nullptr;
|
| int res = posix_memalign(&posix_memalign_ptr, 256, 59);
|
| ASSERT_EQ(0, res);
|
| @@ -213,19 +236,9 @@ TEST_F(AllocatorShimTest, InterceptLibcSymbols) {
|
| ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(valloc_ptr) % kPageSize);
|
| ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u);
|
| ASSERT_GE(aligned_allocs_intercepted_by_size[61], 1u);
|
| +#endif // !OS_WIN
|
|
|
| - void* pvalloc_ptr = pvalloc(67);
|
| - ASSERT_NE(nullptr, pvalloc_ptr);
|
| - ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(pvalloc_ptr) % kPageSize);
|
| - ASSERT_GE(aligned_allocs_intercepted_by_alignment[kPageSize], 1u);
|
| - // pvalloc rounds the size up to the next page.
|
| - ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u);
|
| -#endif // OS_WIN
|
| -
|
| - char* realloc_ptr = static_cast<char*>(realloc(nullptr, 71));
|
| - ASSERT_NE(nullptr, realloc_ptr);
|
| - ASSERT_GE(reallocs_intercepted_by_size[71], 1u);
|
| - ASSERT_GE(reallocs_intercepted_by_addr[Hash(nullptr)], 1u);
|
| + char* realloc_ptr = static_cast<char*>(malloc(10));
|
| strcpy(realloc_ptr, "foobar");
|
| void* old_realloc_ptr = realloc_ptr;
|
| realloc_ptr = static_cast<char*>(realloc(realloc_ptr, 73));
|
| @@ -239,19 +252,21 @@ TEST_F(AllocatorShimTest, InterceptLibcSymbols) {
|
| free(zero_alloc_ptr);
|
| ASSERT_GE(frees_intercepted_by_addr[Hash(zero_alloc_ptr)], 1u);
|
|
|
| -#if !defined(OS_WIN)
|
| +#if !defined(OS_WIN) && !defined(OS_MACOSX)
|
| free(memalign_ptr);
|
| ASSERT_GE(frees_intercepted_by_addr[Hash(memalign_ptr)], 1u);
|
|
|
| + free(pvalloc_ptr);
|
| + ASSERT_GE(frees_intercepted_by_addr[Hash(pvalloc_ptr)], 1u);
|
| +#endif // !OS_WIN && !OS_MACOSX
|
| +
|
| +#if !defined(OS_WIN)
|
| free(posix_memalign_ptr);
|
| ASSERT_GE(frees_intercepted_by_addr[Hash(posix_memalign_ptr)], 1u);
|
|
|
| free(valloc_ptr);
|
| ASSERT_GE(frees_intercepted_by_addr[Hash(valloc_ptr)], 1u);
|
| -
|
| - free(pvalloc_ptr);
|
| - ASSERT_GE(frees_intercepted_by_addr[Hash(pvalloc_ptr)], 1u);
|
| -#endif // OS_WIN
|
| +#endif // !OS_WIN
|
|
|
| free(realloc_ptr);
|
| ASSERT_GE(frees_intercepted_by_addr[Hash(realloc_ptr)], 1u);
|
| @@ -298,6 +313,17 @@ TEST_F(AllocatorShimTest, InterceptCppSymbols) {
|
| RemoveAllocatorDispatchForTesting(&g_mock_dispatch);
|
| }
|
|
|
| +#if !defined(OS_MACOSX)
|
| +
|
| +/*
|
| + realloc() on macOS tries to find zone from ptr and asserts when that fails on
|
| + bogus value 0x420 we're using:
|
| +
|
| + base_unittests(54335,0x70000da6e000) malloc: *** error for object 0x420: pointer being realloc'd was not allocated
|
| +
|
| + See realloc() implementation in libSystem.
|
| +*/
|
| +
|
| // This test exercises the case of concurrent OOM failure, which would end up
|
| // invoking std::new_handler concurrently. This is to cover the CallNewHandler()
|
| // paths of allocator_shim.cc and smoke-test its thread safey.
|
| @@ -329,6 +355,8 @@ TEST_F(AllocatorShimTest, NewHandlerConcurrency) {
|
| ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls());
|
| }
|
|
|
| +#endif
|
| +
|
| } // namespace
|
| } // namespace allocator
|
| } // namespace base
|
|
|