Skip to content

Commit

Permalink
Further starboardize base
Browse files Browse the repository at this point in the history
I found some platform API leaks in base and used walk_dir.py on base to
starboardize it further. Some manual fixes are also used. After this CL
base_unittests still passes.

b/27798606

Change-Id: I648836fb15e26e5eb2651a3ef02a0b680c332bb6
  • Loading branch information
Cobalt Team authored and briantting committed May 2, 2019
1 parent 5f99f30 commit 8c59136
Show file tree
Hide file tree
Showing 767 changed files with 1,492 additions and 1,309 deletions.
1 change: 1 addition & 0 deletions base/allocator/allocator_check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#if defined(OS_MACOSX)
#include "base/allocator/allocator_interception_mac.h"
#include "starboard/types.h"
#endif

namespace base {
Expand Down
3 changes: 1 addition & 2 deletions base/allocator/allocator_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#ifndef BASE_ALLOCATOR_ALLOCATOR_EXTENSION_H_
#define BASE_ALLOCATOR_ALLOCATOR_EXTENSION_H_

#include <stddef.h> // for size_t

#include "base/base_export.h"
#include "build/build_config.h"
#include "starboard/types.h"

namespace base {
namespace allocator {
Expand Down
3 changes: 1 addition & 2 deletions base/allocator/allocator_interception_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#ifndef BASE_ALLOCATOR_ALLOCATOR_INTERCEPTION_MAC_H_
#define BASE_ALLOCATOR_ALLOCATOR_INTERCEPTION_MAC_H_

#include <stddef.h>

#include "base/base_export.h"
#include "starboard/types.h"
#include "third_party/apple_apsl/malloc.h"

namespace base {
Expand Down
1 change: 1 addition & 0 deletions base/allocator/allocator_shim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ ALWAYS_INLINE void ShimFreeDefiniteSize(void* ptr, size_t size, void* context) {
// accidentally performed on the glibc heap instead of the tcmalloc one.
#if defined(USE_TCMALLOC)
#include "base/allocator/allocator_shim_override_glibc_weak_symbols.h"
#include "starboard/types.h"
#endif

#if defined(OS_MACOSX)
Expand Down
3 changes: 1 addition & 2 deletions base/allocator/allocator_shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
#define BASE_ALLOCATOR_ALLOCATOR_SHIM_H_

#include <stddef.h>

#include "base/base_export.h"
#include "build/build_config.h"
#include "starboard/types.h"

namespace base {
namespace allocator {
Expand Down
2 changes: 2 additions & 0 deletions base/allocator/allocator_shim_default_dispatch_to_glibc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <malloc.h>

#include "starboard/types.h"

// This translation unit defines a default dispatch for the allocator shim which
// routes allocations to libc functions.
// The code here is strongly inspired from tcmalloc's libc_override_glibc.h.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#if defined(OS_ANDROID) && __ANDROID_API__ < 17
#include <dlfcn.h>

#include "starboard/types.h"
// This is defined in malloc.h on other platforms. We just need the definition
// for the decltype(malloc_usable_size)* call to work.
size_t malloc_usable_size(const void*);
Expand Down
3 changes: 2 additions & 1 deletion base/allocator/allocator_shim_default_dispatch_to_winheap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/allocator/winheap_stubs_win.h"
#include "base/logging.h"
#include "starboard/memory.h"

namespace {

Expand All @@ -28,7 +29,7 @@ void* DefaultWinHeapCallocImpl(const AllocatorDispatch* self,

void* result = DefaultWinHeapMallocImpl(self, size, context);
if (result) {
memset(result, 0, size);
SbMemorySet(result, 0, size);
}
return result;
}
Expand Down
2 changes: 2 additions & 0 deletions base/allocator/allocator_shim_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <sys/cdefs.h> // for __THROW

#include "starboard/types.h"

#ifndef __THROW // Not a glibc system
#ifdef _NOEXCEPT // LLVM libc++ uses noexcept instead
#define __THROW _NOEXCEPT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <new>

#include "base/allocator/allocator_shim_internals.h"
#include "starboard/types.h"

// __MALLOC_HOOK_VOLATILE not defined in all Glibc headers.
#if !defined(__MALLOC_HOOK_VOLATILE)
Expand Down
8 changes: 5 additions & 3 deletions base/allocator/allocator_shim_override_libc_symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
#include <malloc.h>

#include "base/allocator/allocator_shim_internals.h"
#include "starboard/memory.h"
#include "starboard/types.h"

extern "C" {

SHIM_ALWAYS_EXPORT void* malloc(size_t size) __THROW {
SHIM_ALWAYS_EXPORT void* SbMemoryAllocate(size_t size) __THROW {
return ShimMalloc(size, nullptr);
}

SHIM_ALWAYS_EXPORT void free(void* ptr) __THROW {
SHIM_ALWAYS_EXPORT void SbMemoryFree(void* ptr) __THROW {
ShimFree(ptr, nullptr);
}

SHIM_ALWAYS_EXPORT void* realloc(void* ptr, size_t size) __THROW {
SHIM_ALWAYS_EXPORT void* SbMemoryReallocate(void* ptr, size_t size) __THROW {
return ShimRealloc(ptr, size, nullptr);
}

Expand Down
3 changes: 2 additions & 1 deletion base/allocator/allocator_shim_override_mac_symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#define BASE_ALLOCATOR_ALLOCATOR_SHIM_OVERRIDE_MAC_SYMBOLS_H_

#include "base/allocator/malloc_zone_functions_mac.h"
#include "starboard/memory.h"
#include "third_party/apple_apsl/malloc.h"

namespace base {
namespace allocator {

MallocZoneFunctions MallocZoneFunctionsToReplaceDefault() {
MallocZoneFunctions new_functions;
memset(&new_functions, 0, sizeof(MallocZoneFunctions));
SbMemorySet(&new_functions, 0, sizeof(MallocZoneFunctions));
new_functions.size = [](malloc_zone_t* zone, const void* ptr) -> size_t {
return ShimGetSizeEstimate(ptr, zone);
};
Expand Down
9 changes: 6 additions & 3 deletions base/allocator/allocator_shim_override_ucrt_symbols_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include <windows.h>

#include "starboard/memory.h"
#include "starboard/types.h"

extern "C" {

void* (*malloc_unchecked)(size_t) = &base::allocator::UncheckedAlloc;
Expand Down Expand Up @@ -49,15 +52,15 @@ int _query_new_mode() {
}

// These symbols override the CRT's implementation of the same functions.
__declspec(restrict) void* malloc(size_t size) {
__declspec(restrict) void* SbMemoryAllocate(size_t size) {
return ShimMalloc(size, nullptr);
}

void free(void* ptr) {
void SbMemoryFree(void* ptr) {
ShimFree(ptr, nullptr);
}

__declspec(restrict) void* realloc(void* ptr, size_t size) {
__declspec(restrict) void* SbMemoryReallocate(void* ptr, size_t size) {
return ShimRealloc(ptr, size, nullptr);
}

Expand Down
56 changes: 31 additions & 25 deletions base/allocator/allocator_shim_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include <new>
#include <vector>

#include "starboard/types.h"

#include "starboard/string.h"

#include "starboard/memory.h"

#include "base/allocator/buildflags.h"
#include "base/allocator/partition_allocator/partition_alloc.h"
#include "base/atomicops.h"
Expand Down Expand Up @@ -182,15 +188,15 @@ class AllocatorShimTest : public testing::Test {

void SetUp() override {
const size_t array_size = kMaxSizeTracked * sizeof(size_t);
memset(&allocs_intercepted_by_size, 0, array_size);
memset(&zero_allocs_intercepted_by_size, 0, array_size);
memset(&aligned_allocs_intercepted_by_size, 0, array_size);
memset(&aligned_allocs_intercepted_by_alignment, 0, array_size);
memset(&reallocs_intercepted_by_size, 0, array_size);
memset(&frees_intercepted_by_addr, 0, array_size);
memset(&batch_mallocs_intercepted_by_size, 0, array_size);
memset(&batch_frees_intercepted_by_addr, 0, array_size);
memset(&free_definite_sizes_intercepted_by_size, 0, array_size);
SbMemorySet(&allocs_intercepted_by_size, 0, array_size);
SbMemorySet(&zero_allocs_intercepted_by_size, 0, array_size);
SbMemorySet(&aligned_allocs_intercepted_by_size, 0, array_size);
SbMemorySet(&aligned_allocs_intercepted_by_alignment, 0, array_size);
SbMemorySet(&reallocs_intercepted_by_size, 0, array_size);
SbMemorySet(&frees_intercepted_by_addr, 0, array_size);
SbMemorySet(&batch_mallocs_intercepted_by_size, 0, array_size);
SbMemorySet(&batch_frees_intercepted_by_addr, 0, array_size);
SbMemorySet(&free_definite_sizes_intercepted_by_size, 0, array_size);
did_fail_realloc_0xfeed_once.reset(new ThreadLocalBoolean());
subtle::Release_Store(&num_new_handler_calls, 0);
instance_ = this;
Expand Down Expand Up @@ -241,8 +247,8 @@ class ThreadDelegateForNewHandlerTest : public PlatformThread::Delegate {

void ThreadMain() override {
event_->Wait();
void* temp = malloc(1);
void* res = realloc(temp, 0xFEED);
void* temp = SbMemoryAllocate(1);
void* res = SbMemoryReallocate(temp, 0xFEED);
EXPECT_EQ(temp, res);
}

Expand All @@ -268,7 +274,7 @@ AllocatorDispatch g_mock_dispatch = {
TEST_F(AllocatorShimTest, InterceptLibcSymbols) {
InsertAllocatorDispatch(&g_mock_dispatch);

void* alloc_ptr = malloc(19);
void* alloc_ptr = SbMemoryAllocate(19);
ASSERT_NE(nullptr, alloc_ptr);
ASSERT_GE(allocs_intercepted_by_size[19], 1u);

Expand Down Expand Up @@ -308,45 +314,45 @@ TEST_F(AllocatorShimTest, InterceptLibcSymbols) {
ASSERT_GE(aligned_allocs_intercepted_by_size[kPageSize], 1u);
#endif // !OS_WIN && !OS_MACOSX

char* realloc_ptr = static_cast<char*>(malloc(10));
strcpy(realloc_ptr, "foobar");
char* realloc_ptr = static_cast<char*>(SbMemoryAllocate(10));
SbStringCopyUnsafe(realloc_ptr, "foobar");
void* old_realloc_ptr = realloc_ptr;
realloc_ptr = static_cast<char*>(realloc(realloc_ptr, 73));
SbMemoryReallocate_ptr = static_cast<char*>(SbMemoryReallocate(SbMemoryReallocate_ptr, 73));
ASSERT_GE(reallocs_intercepted_by_size[73], 1u);
ASSERT_GE(reallocs_intercepted_by_addr[Hash(old_realloc_ptr)], 1u);
ASSERT_EQ(0, strcmp(realloc_ptr, "foobar"));

free(alloc_ptr);
SbMemoryFree(alloc_ptr);
ASSERT_GE(frees_intercepted_by_addr[Hash(alloc_ptr)], 1u);

free(zero_alloc_ptr);
SbMemoryFree(zero_alloc_ptr);
ASSERT_GE(frees_intercepted_by_addr[Hash(zero_alloc_ptr)], 1u);

#if !defined(OS_WIN) && !defined(OS_MACOSX)
free(memalign_ptr);
SbMemoryFree(memalign_ptr);
ASSERT_GE(frees_intercepted_by_addr[Hash(memalign_ptr)], 1u);

free(pvalloc_ptr);
SbMemoryFree(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);
SbMemoryFree(posix_memalign_ptr);
ASSERT_GE(frees_intercepted_by_addr[Hash(posix_memalign_ptr)], 1u);

free(valloc_ptr);
SbMemoryFree(valloc_ptr);
ASSERT_GE(frees_intercepted_by_addr[Hash(valloc_ptr)], 1u);
#endif // !OS_WIN

free(realloc_ptr);
SbMemoryFree(realloc_ptr);
ASSERT_GE(frees_intercepted_by_addr[Hash(realloc_ptr)], 1u);

RemoveAllocatorDispatchForTesting(&g_mock_dispatch);

void* non_hooked_ptr = malloc(4095);
void* non_hooked_ptr = SbMemoryAllocate(4095);
ASSERT_NE(nullptr, non_hooked_ptr);
ASSERT_EQ(0u, allocs_intercepted_by_size[4095]);
free(non_hooked_ptr);
SbMemoryFree(non_hooked_ptr);
}

#if defined(OS_MACOSX)
Expand Down Expand Up @@ -378,7 +384,7 @@ TEST_F(AllocatorShimTest, InterceptLibcSymbolsBatchMallocFree) {
TEST_F(AllocatorShimTest, InterceptLibcSymbolsFreeDefiniteSize) {
InsertAllocatorDispatch(&g_mock_dispatch);

void* alloc_ptr = malloc(19);
void* alloc_ptr = SbMemoryAllocate(19);
ASSERT_NE(nullptr, alloc_ptr);
ASSERT_GE(allocs_intercepted_by_size[19], 1u);

Expand Down
5 changes: 3 additions & 2 deletions base/allocator/malloc_zone_functions_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/atomicops.h"
#include "base/synchronization/lock.h"
#include "starboard/memory.h"

namespace base {
namespace allocator {
Expand All @@ -16,7 +17,7 @@ static_assert(std::is_pod<MallocZoneFunctions>::value,

void StoreZoneFunctions(const ChromeMallocZone* zone,
MallocZoneFunctions* functions) {
memset(functions, 0, sizeof(MallocZoneFunctions));
SbMemorySet(functions, 0, sizeof(MallocZoneFunctions));
functions->malloc = zone->malloc;
functions->calloc = zone->calloc;
functions->valloc = zone->valloc;
Expand Down Expand Up @@ -106,7 +107,7 @@ int GetMallocZoneCountForTesting() {
void ClearAllMallocZonesForTesting() {
base::AutoLock l(GetLock());
EnsureMallocZonesInitializedLocked();
memset(g_malloc_zones, 0, kMaxZoneCount * sizeof(MallocZoneFunctions));
SbMemorySet(g_malloc_zones, 0, kMaxZoneCount * sizeof(MallocZoneFunctions));
g_zone_count = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion base/allocator/malloc_zone_functions_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#define BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_

#include <malloc/malloc.h>
#include <stddef.h>

#include "base/base_export.h"
#include "base/logging.h"
#include "starboard/types.h"
#include "third_party/apple_apsl/malloc.h"

namespace base {
Expand Down
3 changes: 2 additions & 1 deletion base/allocator/malloc_zone_functions_mac_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "base/allocator/malloc_zone_functions_mac.h"
#include "starboard/memory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
Expand Down Expand Up @@ -45,7 +46,7 @@ TEST_F(MallocZoneFunctionsTest, CannotStoreMoreThanMaxZones) {
zones.resize(kMaxZoneCount * 2);
for (int i = 0; i < kMaxZoneCount * 2; ++i) {
ChromeMallocZone& zone = zones[i];
memcpy(&zone, malloc_default_zone(), sizeof(ChromeMallocZone));
SbMemoryCopy(&zone, malloc_default_zone(), sizeof(ChromeMallocZone));
StoreMallocZone(&zone);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <windows.h> // Must be in front of other Windows header files.

#include <VersionHelpers.h>

#include "starboard/types.h"
#endif

namespace base {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <vector>

#include "starboard/types.h"

#include "base/allocator/partition_allocator/page_allocator.h"
#include "base/logging.h"
#include "build/build_config.h"
Expand Down
2 changes: 2 additions & 0 deletions base/allocator/partition_allocator/oom.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#if defined(OS_WIN)
#include <windows.h>

#include "starboard/types.h"
#endif

// Do not want trivial entry points just calling OOM_CRASH() to be
Expand Down
1 change: 1 addition & 0 deletions base/allocator/partition_allocator/page_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "base/allocator/partition_allocator/page_allocator_internals_win.h"
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
#include "base/allocator/partition_allocator/page_allocator_internals_posix.h"
#include "starboard/types.h"
#else
#error Platform not supported.
#endif
Expand Down
Loading

0 comments on commit 8c59136

Please sign in to comment.