Commit 4f664413 authored by Vlad Tsyrklevich's avatar Vlad Tsyrklevich Committed by Commit Bot

GWP-ASan: Rename allocator shims to malloc shims

In preparation for adding PartitionAlloc support to GWP-ASan, rename the
current sampling "allocator shims" to more specifically be the malloc
shims.

Bug: 956824
Change-Id: I05ab7a556063d1cd0966ab40474720299caf0f76
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1593838Reviewed-by: default avatarVitaly Buka <vitalybuka@chromium.org>
Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Auto-Submit: Vlad Tsyrklevich <vtsyrklevich@chromium.org>
Cr-Commit-Position: refs/heads/master@{#656248}
parent 668eec76
include_rules = [ include_rules = [
"+components/gwp_asan/buildflags/buildflags.h", "+components/gwp_asan/buildflags/buildflags.h",
"+components/gwp_asan/client/sampling_allocator_shims.h", "+components/gwp_asan/client/sampling_malloc_shims.h",
"+third_party/breakpad/breakpad/src/client/ios/Breakpad.h", "+third_party/breakpad/breakpad/src/client/ios/Breakpad.h",
"+third_party/breakpad/breakpad/src/client/ios/BreakpadController.h", "+third_party/breakpad/breakpad/src/client/ios/BreakpadController.h",
] ]
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "components/gwp_asan/buildflags/buildflags.h" #include "components/gwp_asan/buildflags/buildflags.h"
#if BUILDFLAG(ENABLE_GWP_ASAN_MALLOC) #if BUILDFLAG(ENABLE_GWP_ASAN_MALLOC)
#include "components/gwp_asan/client/sampling_allocator_shims.h" // nogncheck #include "components/gwp_asan/client/sampling_malloc_shims.h" // nogncheck
#endif #endif
// Deallocated objects are re-classed as |CrZombie|. No superclass // Deallocated objects are re-classed as |CrZombie|. No superclass
...@@ -99,7 +99,7 @@ void ZombieDealloc(id self, SEL _cmd) { ...@@ -99,7 +99,7 @@ void ZombieDealloc(id self, SEL _cmd) {
// Use the original |-dealloc| if the object doesn't wish to be // Use the original |-dealloc| if the object doesn't wish to be
// zombied or GWP-ASan is the backing allocator. // zombied or GWP-ASan is the backing allocator.
#if BUILDFLAG(ENABLE_GWP_ASAN_MALLOC) #if BUILDFLAG(ENABLE_GWP_ASAN_MALLOC)
bool gwp_asan_allocation = gwp_asan::IsGwpAsanAllocation(self); bool gwp_asan_allocation = gwp_asan::IsGwpAsanMallocAllocation(self);
#else #else
bool gwp_asan_allocation = false; bool gwp_asan_allocation = false;
#endif #endif
......
...@@ -22,8 +22,8 @@ component("client") { ...@@ -22,8 +22,8 @@ component("client") {
if (use_allocator_shim) { if (use_allocator_shim) {
sources += [ sources += [
"sampling_allocator_shims.cc", "sampling_malloc_shims.cc",
"sampling_allocator_shims.h", "sampling_malloc_shims.h",
] ]
} }
...@@ -44,7 +44,7 @@ source_set("unit_tests") { ...@@ -44,7 +44,7 @@ source_set("unit_tests") {
] ]
if (use_allocator_shim) { if (use_allocator_shim) {
sources += [ "sampling_allocator_shims_unittest.cc" ] sources += [ "sampling_malloc_shims_unittest.cc" ]
} }
deps = [ deps = [
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include "components/gwp_asan/client/guarded_page_allocator.h" #include "components/gwp_asan/client/guarded_page_allocator.h"
#if BUILDFLAG(USE_ALLOCATOR_SHIM) #if BUILDFLAG(USE_ALLOCATOR_SHIM)
#include "components/gwp_asan/client/sampling_allocator_shims.h" #include "components/gwp_asan/client/sampling_malloc_shims.h"
#endif // BUILDFLAG(USE_ALLOCATOR_SHIM) #endif // BUILDFLAG(USE_ALLOCATOR_SHIM)
namespace gwp_asan { namespace gwp_asan {
...@@ -177,8 +177,8 @@ bool EnableForMalloc(bool is_canary_dev, bool is_browser_process) { ...@@ -177,8 +177,8 @@ bool EnableForMalloc(bool is_canary_dev, bool is_browser_process) {
if (!SampleProcess(is_canary_dev, is_browser_process)) if (!SampleProcess(is_canary_dev, is_browser_process))
return false; return false;
InstallAllocatorHooks(max_allocations, max_metadata, total_pages, InstallMallocHooks(max_allocations, max_metadata, total_pages,
alloc_sampling_freq); alloc_sampling_freq);
return true; return true;
} }
#endif // BUILDFLAG(USE_ALLOCATOR_SHIM) #endif // BUILDFLAG(USE_ALLOCATOR_SHIM)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/gwp_asan/client/sampling_allocator_shims.h" #include "components/gwp_asan/client/sampling_malloc_shims.h"
#include <algorithm> #include <algorithm>
...@@ -244,14 +244,14 @@ AllocatorDispatch g_allocator_dispatch = { ...@@ -244,14 +244,14 @@ AllocatorDispatch g_allocator_dispatch = {
} // namespace } // namespace
// We expose the allocator singleton for unit tests. // We expose the allocator singleton for unit tests.
GWP_ASAN_EXPORT GuardedPageAllocator& GetGpaForTesting() { GWP_ASAN_EXPORT GuardedPageAllocator& GetMallocGpaForTesting() {
return *gpa; return *gpa;
} }
void InstallAllocatorHooks(size_t max_allocated_pages, void InstallMallocHooks(size_t max_allocated_pages,
size_t num_metadata, size_t num_metadata,
size_t total_pages, size_t total_pages,
size_t sampling_frequency) { size_t sampling_frequency) {
static crash_reporter::CrashKeyString<24> malloc_crash_key(kGpaCrashKey); static crash_reporter::CrashKeyString<24> malloc_crash_key(kGpaCrashKey);
gpa = new GuardedPageAllocator(); gpa = new GuardedPageAllocator();
gpa->Init(max_allocated_pages, num_metadata, total_pages); gpa->Init(max_allocated_pages, num_metadata, total_pages);
...@@ -262,7 +262,7 @@ void InstallAllocatorHooks(size_t max_allocated_pages, ...@@ -262,7 +262,7 @@ void InstallAllocatorHooks(size_t max_allocated_pages,
} // namespace internal } // namespace internal
bool IsGwpAsanAllocation(const void* ptr) { bool IsGwpAsanMallocAllocation(const void* ptr) {
return internal::gpa && internal::gpa->PointerIsMine(ptr); return internal::gpa && internal::gpa->PointerIsMine(ptr);
} }
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef COMPONENTS_GWP_ASAN_CLIENT_SAMPLING_ALLOCATOR_SHIMS_H_ #ifndef COMPONENTS_GWP_ASAN_CLIENT_SAMPLING_MALLOC_SHIMS_H_
#define COMPONENTS_GWP_ASAN_CLIENT_SAMPLING_ALLOCATOR_SHIMS_H_ #define COMPONENTS_GWP_ASAN_CLIENT_SAMPLING_MALLOC_SHIMS_H_
#include <stddef.h> // for size_t #include <stddef.h> // for size_t
...@@ -14,18 +14,18 @@ namespace gwp_asan { ...@@ -14,18 +14,18 @@ namespace gwp_asan {
namespace internal { namespace internal {
// Initialize the guarded allocator with the given parameters, and install the // Initialize the guarded allocator with the given parameters, and install the
// sampling allocator shims with the provided sampling frequency. // sampling malloc shims with the provided sampling frequency.
GWP_ASAN_EXPORT void InstallAllocatorHooks(size_t max_allocated_pages, GWP_ASAN_EXPORT void InstallMallocHooks(size_t max_allocated_pages,
size_t num_metadata, size_t num_metadata,
size_t total_pages, size_t total_pages,
size_t sampling_frequency); size_t sampling_frequency);
} // namespace internal } // namespace internal
// Checks if the |ptr| is a GWP-ASan allocation. (This is exposed for use by // Checks if the |ptr| is a GWP-ASan allocation. (This is exposed for use by
// Zombies on macOS.) // Zombies on macOS.)
GWP_ASAN_EXPORT bool IsGwpAsanAllocation(const void* ptr); GWP_ASAN_EXPORT bool IsGwpAsanMallocAllocation(const void* ptr);
} // namespace gwp_asan } // namespace gwp_asan
#endif // COMPONENTS_GWP_ASAN_CLIENT_SAMPLING_ALLOCATOR_SHIMS_H_ #endif // COMPONENTS_GWP_ASAN_CLIENT_SAMPLING_MALLOC_SHIMS_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/gwp_asan/client/sampling_allocator_shims.h" #include "components/gwp_asan/client/sampling_malloc_shims.h"
#include <stdlib.h> #include <stdlib.h>
#include <cstdlib> #include <cstdlib>
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#if defined(OS_WIN) #if defined(OS_WIN)
#include <malloc.h> #include <malloc.h>
static size_t GetAllocatedSize(void *mem) { static size_t GetAllocatedSize(void* mem) {
return _msize(mem); return _msize(mem);
} }
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
...@@ -47,7 +47,7 @@ static size_t GetAllocatedSize(void* mem) { ...@@ -47,7 +47,7 @@ static size_t GetAllocatedSize(void* mem) {
namespace gwp_asan { namespace gwp_asan {
namespace internal { namespace internal {
extern GuardedPageAllocator& GetGpaForTesting(); extern GuardedPageAllocator& GetMallocGpaForTesting();
namespace { namespace {
...@@ -59,16 +59,16 @@ constexpr size_t kLoopIterations = kSamplingFrequency * 4; ...@@ -59,16 +59,16 @@ constexpr size_t kLoopIterations = kSamplingFrequency * 4;
constexpr int kSuccess = 0; constexpr int kSuccess = 0;
constexpr int kFailure = 1; constexpr int kFailure = 1;
class SamplingAllocatorShimsTest : public base::MultiProcessTest { class SamplingMallocShimsTest : public base::MultiProcessTest {
public: public:
static void multiprocessTestSetup() { static void multiprocessTestSetup() {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
base::allocator::InitializeAllocatorShim(); base::allocator::InitializeAllocatorShim();
#endif // defined(OS_MACOSX) #endif // defined(OS_MACOSX)
crash_reporter::InitializeCrashKeys(); crash_reporter::InitializeCrashKeys();
InstallAllocatorHooks(AllocatorState::kMaxMetadata, InstallMallocHooks(AllocatorState::kMaxMetadata,
AllocatorState::kMaxMetadata, AllocatorState::kMaxMetadata, AllocatorState::kMaxSlots,
AllocatorState::kMaxSlots, kSamplingFrequency); kSamplingFrequency);
} }
protected: protected:
...@@ -97,7 +97,7 @@ bool allocationCheck(std::function<void*(void)> allocate, ...@@ -97,7 +97,7 @@ bool allocationCheck(std::function<void*(void)> allocate,
return false; return false;
} }
if (GetGpaForTesting().PointerIsMine(alloc.get())) if (GetMallocGpaForTesting().PointerIsMine(alloc.get()))
guarded++; guarded++;
else else
unguarded++; unguarded++;
...@@ -112,7 +112,7 @@ bool allocationCheck(std::function<void*(void)> allocate, ...@@ -112,7 +112,7 @@ bool allocationCheck(std::function<void*(void)> allocate,
MULTIPROCESS_TEST_MAIN_WITH_SETUP( MULTIPROCESS_TEST_MAIN_WITH_SETUP(
BasicFunctionality, BasicFunctionality,
SamplingAllocatorShimsTest::multiprocessTestSetup) { SamplingMallocShimsTest::multiprocessTestSetup) {
const size_t page_size = base::GetPageSize(); const size_t page_size = base::GetPageSize();
int failures = 0; int failures = 0;
...@@ -168,14 +168,14 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP( ...@@ -168,14 +168,14 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP(
return kFailure; return kFailure;
} }
TEST_F(SamplingAllocatorShimsTest, BasicFunctionality) { TEST_F(SamplingMallocShimsTest, BasicFunctionality) {
runTest("BasicFunctionality"); runTest("BasicFunctionality");
} }
MULTIPROCESS_TEST_MAIN_WITH_SETUP( MULTIPROCESS_TEST_MAIN_WITH_SETUP(
Realloc, Realloc,
SamplingAllocatorShimsTest::multiprocessTestSetup) { SamplingMallocShimsTest::multiprocessTestSetup) {
void* alloc = GetGpaForTesting().Allocate(base::GetPageSize()); void* alloc = GetMallocGpaForTesting().Allocate(base::GetPageSize());
CHECK_NE(alloc, nullptr); CHECK_NE(alloc, nullptr);
constexpr unsigned char kFillChar = 0xff; constexpr unsigned char kFillChar = 0xff;
...@@ -184,7 +184,7 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP( ...@@ -184,7 +184,7 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP(
unsigned char* new_alloc = unsigned char* new_alloc =
static_cast<unsigned char*>(realloc(alloc, base::GetPageSize() + 1)); static_cast<unsigned char*>(realloc(alloc, base::GetPageSize() + 1));
CHECK_NE(alloc, new_alloc); CHECK_NE(alloc, new_alloc);
CHECK_EQ(GetGpaForTesting().PointerIsMine(new_alloc), false); CHECK_EQ(GetMallocGpaForTesting().PointerIsMine(new_alloc), false);
for (size_t i = 0; i < base::GetPageSize(); i++) for (size_t i = 0; i < base::GetPageSize(); i++)
CHECK_EQ(new_alloc[i], kFillChar); CHECK_EQ(new_alloc[i], kFillChar);
...@@ -194,19 +194,19 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP( ...@@ -194,19 +194,19 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP(
return kSuccess; return kSuccess;
} }
TEST_F(SamplingAllocatorShimsTest, Realloc) { TEST_F(SamplingMallocShimsTest, Realloc) {
runTest("Realloc"); runTest("Realloc");
} }
MULTIPROCESS_TEST_MAIN_WITH_SETUP( MULTIPROCESS_TEST_MAIN_WITH_SETUP(
Calloc, Calloc,
SamplingAllocatorShimsTest::multiprocessTestSetup) { SamplingMallocShimsTest::multiprocessTestSetup) {
for (size_t i = 0; i < kLoopIterations; i++) { for (size_t i = 0; i < kLoopIterations; i++) {
unsigned char* alloc = unsigned char* alloc =
static_cast<unsigned char*>(calloc(base::GetPageSize(), 1)); static_cast<unsigned char*>(calloc(base::GetPageSize(), 1));
CHECK_NE(alloc, nullptr); CHECK_NE(alloc, nullptr);
if (GetGpaForTesting().PointerIsMine(alloc)) { if (GetMallocGpaForTesting().PointerIsMine(alloc)) {
for (size_t i = 0; i < base::GetPageSize(); i++) for (size_t i = 0; i < base::GetPageSize(); i++)
CHECK_EQ(alloc[i], 0U); CHECK_EQ(alloc[i], 0U);
free(alloc); free(alloc);
...@@ -219,7 +219,7 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP( ...@@ -219,7 +219,7 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP(
return kFailure; return kFailure;
} }
TEST_F(SamplingAllocatorShimsTest, Calloc) { TEST_F(SamplingMallocShimsTest, Calloc) {
runTest("Calloc"); runTest("Calloc");
} }
...@@ -228,30 +228,30 @@ TEST_F(SamplingAllocatorShimsTest, Calloc) { ...@@ -228,30 +228,30 @@ TEST_F(SamplingAllocatorShimsTest, Calloc) {
#if !defined(COMPONENT_BUILD) #if !defined(COMPONENT_BUILD)
MULTIPROCESS_TEST_MAIN_WITH_SETUP( MULTIPROCESS_TEST_MAIN_WITH_SETUP(
CrashKey, CrashKey,
SamplingAllocatorShimsTest::multiprocessTestSetup) { SamplingMallocShimsTest::multiprocessTestSetup) {
if (crash_reporter::GetCrashKeyValue(kGpaCrashKey) != if (crash_reporter::GetCrashKeyValue(kGpaCrashKey) !=
GetGpaForTesting().GetCrashKey()) { GetMallocGpaForTesting().GetCrashKey()) {
return kFailure; return kFailure;
} }
return kSuccess; return kSuccess;
} }
TEST_F(SamplingAllocatorShimsTest, CrashKey) { TEST_F(SamplingMallocShimsTest, CrashKey) {
runTest("CrashKey"); runTest("CrashKey");
} }
#endif // !defined(COMPONENT_BUILD) #endif // !defined(COMPONENT_BUILD)
MULTIPROCESS_TEST_MAIN_WITH_SETUP( MULTIPROCESS_TEST_MAIN_WITH_SETUP(
GetSizeEstimate, GetSizeEstimate,
SamplingAllocatorShimsTest::multiprocessTestSetup) { SamplingMallocShimsTest::multiprocessTestSetup) {
constexpr size_t kAllocationSize = 123; constexpr size_t kAllocationSize = 123;
for (size_t i = 0; i < kLoopIterations; i++) { for (size_t i = 0; i < kLoopIterations; i++) {
std::unique_ptr<void, decltype(&free)> alloc(malloc(kAllocationSize), free); std::unique_ptr<void, decltype(&free)> alloc(malloc(kAllocationSize), free);
CHECK_NE(alloc.get(), nullptr); CHECK_NE(alloc.get(), nullptr);
size_t alloc_sz = GetAllocatedSize(alloc.get()); size_t alloc_sz = GetAllocatedSize(alloc.get());
if (GetGpaForTesting().PointerIsMine(alloc.get())) if (GetMallocGpaForTesting().PointerIsMine(alloc.get()))
CHECK_EQ(alloc_sz, kAllocationSize); CHECK_EQ(alloc_sz, kAllocationSize);
else else
CHECK_GE(alloc_sz, kAllocationSize); CHECK_GE(alloc_sz, kAllocationSize);
...@@ -260,14 +260,14 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP( ...@@ -260,14 +260,14 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP(
return kSuccess; return kSuccess;
} }
TEST_F(SamplingAllocatorShimsTest, GetSizeEstimate) { TEST_F(SamplingMallocShimsTest, GetSizeEstimate) {
runTest("GetSizeEstimate"); runTest("GetSizeEstimate");
} }
#if defined(OS_WIN) #if defined(OS_WIN)
MULTIPROCESS_TEST_MAIN_WITH_SETUP( MULTIPROCESS_TEST_MAIN_WITH_SETUP(
AlignedRealloc, AlignedRealloc,
SamplingAllocatorShimsTest::multiprocessTestSetup) { SamplingMallocShimsTest::multiprocessTestSetup) {
// Exercise the _aligned_* shims and ensure that we handle them stably. // Exercise the _aligned_* shims and ensure that we handle them stably.
constexpr size_t kAllocationSize = 123; constexpr size_t kAllocationSize = 123;
constexpr size_t kAllocationAlignment = 64; constexpr size_t kAllocationAlignment = 64;
...@@ -282,7 +282,7 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP( ...@@ -282,7 +282,7 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP(
return kSuccess; return kSuccess;
} }
TEST_F(SamplingAllocatorShimsTest, AlignedRealloc) { TEST_F(SamplingMallocShimsTest, AlignedRealloc) {
runTest("AlignedRealloc"); runTest("AlignedRealloc");
} }
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
...@@ -290,14 +290,14 @@ TEST_F(SamplingAllocatorShimsTest, AlignedRealloc) { ...@@ -290,14 +290,14 @@ TEST_F(SamplingAllocatorShimsTest, AlignedRealloc) {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
MULTIPROCESS_TEST_MAIN_WITH_SETUP( MULTIPROCESS_TEST_MAIN_WITH_SETUP(
BatchFree, BatchFree,
SamplingAllocatorShimsTest::multiprocessTestSetup) { SamplingMallocShimsTest::multiprocessTestSetup) {
void* ptrs[AllocatorState::kMaxMetadata + 1]; void* ptrs[AllocatorState::kMaxMetadata + 1];
for (size_t i = 0; i < AllocatorState::kMaxMetadata; i++) { for (size_t i = 0; i < AllocatorState::kMaxMetadata; i++) {
ptrs[i] = GetGpaForTesting().Allocate(16); ptrs[i] = GetMallocGpaForTesting().Allocate(16);
CHECK(ptrs[i]); CHECK(ptrs[i]);
} }
// Check that all GPA allocations were consumed. // Check that all GPA allocations were consumed.
CHECK_EQ(GetGpaForTesting().Allocate(16), nullptr); CHECK_EQ(GetMallocGpaForTesting().Allocate(16), nullptr);
ptrs[AllocatorState::kMaxMetadata] = ptrs[AllocatorState::kMaxMetadata] =
malloc_zone_malloc(malloc_default_zone(), 16); malloc_zone_malloc(malloc_default_zone(), 16);
...@@ -307,12 +307,12 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP( ...@@ -307,12 +307,12 @@ MULTIPROCESS_TEST_MAIN_WITH_SETUP(
AllocatorState::kMaxMetadata + 1); AllocatorState::kMaxMetadata + 1);
// Check that GPA allocations were freed. // Check that GPA allocations were freed.
CHECK(GetGpaForTesting().Allocate(16)); CHECK(GetMallocGpaForTesting().Allocate(16));
return kSuccess; return kSuccess;
} }
TEST_F(SamplingAllocatorShimsTest, BatchFree) { TEST_F(SamplingMallocShimsTest, BatchFree) {
runTest("BatchFree"); runTest("BatchFree");
} }
#endif // defined(OS_MACOSX) #endif // defined(OS_MACOSX)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment