1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/gwp_asan/client/sampling_partitionalloc_shims.h"
#include <stdlib.h>
#include <algorithm>
#include <iterator>
#include <set>
#include <string>
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/page_size.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/gtest_util.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "components/crash/core/common/crash_key.h"
#include "components/gwp_asan/client/guarded_page_allocator.h"
#include "components/gwp_asan/client/gwp_asan.h"
#include "components/gwp_asan/common/crash_key_name.h"
#include "partition_alloc/partition_alloc.h"
#include "partition_alloc/partition_root.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
// PartitionAlloc (and hence hooking) are disabled with sanitizers that replace
// allocation routines.
#if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
// These tests install global PartitionAlloc hooks so they are not safe to run
// in multi-threaded contexts. Instead they're implemented as multi-process
// tests.
namespace gwp_asan {
namespace internal {
extern GuardedPageAllocator& GetPartitionAllocGpaForTesting();
namespace {
constexpr const char* kFakeType = "fake type";
constexpr const char* kFakeType2 = "fake type #2";
constexpr size_t kSamplingFrequency = 10;
// Number of loop iterations required to definitely hit a sampled allocation.
constexpr size_t kLoopIterations = kSamplingFrequency * 4;
constexpr int kSuccess = 0;
constexpr int kFailure = 1;
constexpr int kSamplingMaxSize = 16;
static constexpr size_t kMaxMetadata = 2048;
static constexpr size_t kMaxRequestedSlots = 8192;
constexpr partition_alloc::PartitionOptions kAllocatorOptions = {};
static void HandleOOM(size_t unused_size) {
LOG(FATAL) << "Out of memory.";
}
class SamplingPartitionAllocShimsTest : public base::MultiProcessTest {
public:
static void multiprocessTestSetup() {
crash_reporter::InitializeCrashKeys();
partition_alloc::PartitionAllocGlobalInit(HandleOOM);
CHECK(InstallPartitionAllocHooks(
AllocatorSettings{
.max_allocated_pages = kMaxMetadata,
.num_metadata = kMaxMetadata,
.total_pages = kMaxRequestedSlots,
.sampling_frequency = kSamplingFrequency,
.sampling_min_size = 1,
.sampling_max_size = std::numeric_limits<int>::max(),
},
base::DoNothing()));
}
static void multiprocessTestSetupWithSamplingMaxSize() {
crash_reporter::InitializeCrashKeys();
partition_alloc::PartitionAllocGlobalInit(HandleOOM);
CHECK(InstallPartitionAllocHooks(
AllocatorSettings{.max_allocated_pages = kMaxMetadata,
.num_metadata = kMaxMetadata,
.total_pages = kMaxRequestedSlots,
.sampling_frequency = kSamplingFrequency,
.sampling_min_size = 1,
.sampling_max_size = kSamplingMaxSize},
base::DoNothing()));
}
protected:
void runTest(const char* name) {
base::Process process = SpawnChild(name);
int exit_code = -1;
ASSERT_TRUE(WaitForMultiprocessTestChildExit(
process, TestTimeouts::action_max_timeout(), &exit_code));
EXPECT_EQ(exit_code, kSuccess);
}
};
MULTIPROCESS_TEST_MAIN_WITH_SETUP(
BasicFunctionality,
SamplingPartitionAllocShimsTest::multiprocessTestSetup) {
partition_alloc::PartitionAllocator allocator;
allocator.init(kAllocatorOptions);
for (size_t i = 0; i < kLoopIterations; i++) {
void* ptr = allocator.root()->Alloc(1, kFakeType);
if (GetPartitionAllocGpaForTesting().PointerIsMine(ptr))
return kSuccess;
allocator.root()->Free(ptr);
}
return kFailure;
}
TEST_F(SamplingPartitionAllocShimsTest, BasicFunctionality) {
runTest("BasicFunctionality");
}
MULTIPROCESS_TEST_MAIN_WITH_SETUP(
Realloc,
SamplingPartitionAllocShimsTest::multiprocessTestSetup) {
partition_alloc::PartitionAllocator allocator;
allocator.init(kAllocatorOptions);
void* alloc = GetPartitionAllocGpaForTesting().Allocate(base::GetPageSize());
CHECK_NE(alloc, nullptr);
constexpr unsigned char kFillChar = 0xff;
memset(alloc, kFillChar, base::GetPageSize());
unsigned char* new_alloc = static_cast<unsigned char*>(
allocator.root()->Realloc(alloc, base::GetPageSize() + 1, kFakeType));
CHECK_NE(alloc, new_alloc);
CHECK_EQ(GetPartitionAllocGpaForTesting().PointerIsMine(new_alloc), false);
for (size_t i = 0; i < base::GetPageSize(); i++)
CHECK_EQ(new_alloc[i], kFillChar);
allocator.root()->Free(new_alloc);
return kSuccess;
}
TEST_F(SamplingPartitionAllocShimsTest, Realloc) {
runTest("Realloc");
}
// Ensure sampled GWP-ASan allocations with different types never overlap.
MULTIPROCESS_TEST_MAIN_WITH_SETUP(
DifferentTypesDontOverlap,
SamplingPartitionAllocShimsTest::multiprocessTestSetup) {
partition_alloc::PartitionAllocator allocator;
allocator.init(kAllocatorOptions);
std::set<void*> type1, type2;
for (size_t i = 0; i < kLoopIterations * kMaxRequestedSlots;
i++) {
void* ptr1 = allocator.root()->Alloc(1, kFakeType);
void* ptr2 = allocator.root()->Alloc(1, kFakeType2);
if (GetPartitionAllocGpaForTesting().PointerIsMine(ptr1))
type1.insert(ptr1);
if (GetPartitionAllocGpaForTesting().PointerIsMine(ptr2))
type2.insert(ptr2);
allocator.root()->Free(ptr1);
allocator.root()->Free(ptr2);
}
std::vector<void*> intersection;
std::set_intersection(type1.begin(), type1.end(), type2.begin(), type2.end(),
std::back_inserter(intersection));
if (intersection.size() != 0)
return kFailure;
return kSuccess;
}
TEST_F(SamplingPartitionAllocShimsTest, DifferentTypesDontOverlap) {
runTest("DifferentTypesDontOverlap");
}
// GetCrashKeyValue() operates on a per-component basis, can't read the crash
// key from the gwp_asan_client component in a component build.
#if !defined(COMPONENT_BUILD)
MULTIPROCESS_TEST_MAIN_WITH_SETUP(
CrashKey,
SamplingPartitionAllocShimsTest::multiprocessTestSetup) {
if (crash_reporter::GetCrashKeyValue(kPartitionAllocCrashKey) !=
GetPartitionAllocGpaForTesting().GetCrashKey()) {
return kFailure;
}
return kSuccess;
}
TEST_F(SamplingPartitionAllocShimsTest, CrashKey) {
runTest("CrashKey");
}
#endif // !defined(COMPONENT_BUILD)
MULTIPROCESS_TEST_MAIN_WITH_SETUP(
SamplingRange,
SamplingPartitionAllocShimsTest::multiprocessTestSetupWithSamplingMaxSize) {
partition_alloc::PartitionAllocator allocator;
allocator.init(kAllocatorOptions);
for (size_t i = 0; i < kLoopIterations; i++) {
void* ptr = allocator.root()->Alloc(kSamplingMaxSize * 2, kFakeType);
if (GetPartitionAllocGpaForTesting().PointerIsMine(ptr)) {
return kFailure;
}
allocator.root()->Free(ptr);
}
return kSuccess;
}
TEST_F(SamplingPartitionAllocShimsTest, SamplingRange) {
runTest("SamplingRange");
}
} // namespace
} // namespace internal
} // namespace gwp_asan
#endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
|