[scudo] [GWP-ASan] Add GWP-ASan variant of scudo benchmarks.

GWP-ASan is the "production" variant as compiled by compiler-rt, and it's useful to be able to benchmark changes in GWP-ASan or Scudo's GWP-ASan hooks across versions. GWP-ASan is sampled, and sampled allocations are much slower, but given the amount of allocations that happen under test here - we actually get a reasonable representation of GWP-ASan's negligent performance impact between runs.

Reviewed By: cryptoad

Differential Revision: https://reviews.llvm.org/D101865
This commit is contained in:
Mitch Phillips 2021-05-10 11:59:45 -07:00
parent 18f3a14e13
commit 8936608e6f
3 changed files with 25 additions and 5 deletions

View File

@ -18,4 +18,16 @@ foreach(arch ${SCUDO_STANDALONE_SUPPORTED_ARCH})
$<TARGET_OBJECTS:RTScudoStandalone.${arch}>) $<TARGET_OBJECTS:RTScudoStandalone.${arch}>)
set_property(TARGET ScudoBenchmarks.${arch} APPEND_STRING PROPERTY set_property(TARGET ScudoBenchmarks.${arch} APPEND_STRING PROPERTY
COMPILE_FLAGS "${SCUDO_BENCHMARK_CFLAGS}") COMPILE_FLAGS "${SCUDO_BENCHMARK_CFLAGS}")
if (COMPILER_RT_HAS_GWP_ASAN)
add_benchmark(
ScudoBenchmarksWithGwpAsan.${arch} malloc_benchmark.cpp
$<TARGET_OBJECTS:RTScudoStandalone.${arch}>
$<TARGET_OBJECTS:RTGwpAsan.${arch}>
$<TARGET_OBJECTS:RTGwpAsanBacktraceLibc.${arch}>
$<TARGET_OBJECTS:RTGwpAsanSegvHandler.${arch}>)
set_property(
TARGET ScudoBenchmarksWithGwpAsan.${arch} APPEND_STRING PROPERTY
COMPILE_FLAGS "${SCUDO_BENCHMARK_CFLAGS} -DGWP_ASAN_HOOKS")
endif()
endforeach() endforeach()

View File

@ -13,15 +13,22 @@
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include <memory> #include <memory>
#include <vector>
void *CurrentAllocator;
template <typename Config> void PostInitCallback() {
reinterpret_cast<scudo::Allocator<Config> *>(CurrentAllocator)->initGwpAsan();
}
template <typename Config> static void BM_malloc_free(benchmark::State &State) { template <typename Config> static void BM_malloc_free(benchmark::State &State) {
using AllocatorT = scudo::Allocator<Config>; using AllocatorT = scudo::Allocator<Config, PostInitCallback<Config>>;
auto Deleter = [](AllocatorT *A) { auto Deleter = [](AllocatorT *A) {
A->unmapTestOnly(); A->unmapTestOnly();
delete A; delete A;
}; };
std::unique_ptr<AllocatorT, decltype(Deleter)> Allocator(new AllocatorT, std::unique_ptr<AllocatorT, decltype(Deleter)> Allocator(new AllocatorT,
Deleter); Deleter);
CurrentAllocator = Allocator.get();
Allocator->reset(); Allocator->reset();
const size_t NBytes = State.range(0); const size_t NBytes = State.range(0);
@ -55,18 +62,19 @@ BENCHMARK_TEMPLATE(BM_malloc_free, scudo::FuchsiaConfig)
template <typename Config> template <typename Config>
static void BM_malloc_free_loop(benchmark::State &State) { static void BM_malloc_free_loop(benchmark::State &State) {
using AllocatorT = scudo::Allocator<Config>; using AllocatorT = scudo::Allocator<Config, PostInitCallback<Config>>;
auto Deleter = [](AllocatorT *A) { auto Deleter = [](AllocatorT *A) {
A->unmapTestOnly(); A->unmapTestOnly();
delete A; delete A;
}; };
std::unique_ptr<AllocatorT, decltype(Deleter)> Allocator(new AllocatorT, std::unique_ptr<AllocatorT, decltype(Deleter)> Allocator(new AllocatorT,
Deleter); Deleter);
CurrentAllocator = Allocator.get();
Allocator->reset(); Allocator->reset();
const size_t NumIters = State.range(0); const size_t NumIters = State.range(0);
size_t PageSize = scudo::getPageSizeCached(); size_t PageSize = scudo::getPageSizeCached();
void *Ptrs[NumIters]; std::vector<void *> Ptrs(NumIters);
for (auto _ : State) { for (auto _ : State) {
size_t SizeLog2 = 0; size_t SizeLog2 = 0;

View File

@ -51,8 +51,7 @@ public:
typedef typename Params::template TSDRegistryT<ThisT> TSDRegistryT; typedef typename Params::template TSDRegistryT<ThisT> TSDRegistryT;
void callPostInitCallback() { void callPostInitCallback() {
static pthread_once_t OnceControl = PTHREAD_ONCE_INIT; pthread_once(&PostInitNonce, PostInitCallback);
pthread_once(&OnceControl, PostInitCallback);
} }
struct QuarantineCallback { struct QuarantineCallback {
@ -952,6 +951,7 @@ private:
SecondaryT Secondary; SecondaryT Secondary;
QuarantineT Quarantine; QuarantineT Quarantine;
TSDRegistryT TSDRegistry; TSDRegistryT TSDRegistry;
pthread_once_t PostInitNonce = PTHREAD_ONCE_INIT;
#ifdef GWP_ASAN_HOOKS #ifdef GWP_ASAN_HOOKS
gwp_asan::GuardedPoolAllocator GuardedAlloc; gwp_asan::GuardedPoolAllocator GuardedAlloc;