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