From bcb22995dbe7e594eaba1ea7f9312c0c43fd77f7 Mon Sep 17 00:00:00 2001 From: Elfgo Date: Wed, 9 Apr 2025 11:19:52 +0000 Subject: [PATCH] [Compiler-RT][GWP_ASan] Add MinSampleSize & WhiteListPath options for GWP_ASan(Patch 1) Add MinSampleSize & WhiteListPath options for GWP_ASan MinSampleSize: the minimum memory allocate size should gwp_asan sample or not WhiteListPath: if the .so path in WhiteListPath, GWP_ASan determin to sample it without considering the sample rate. note:This MR does not currently utilize the WhiteListPath variable,I will be using it in the next MR. Change-Id: I32f26e4b24234acb7ff278131babb7dbe2665ef9 Signed-off-by: Elfgo --- compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp | 8 +++++++- compiler-rt/lib/gwp_asan/guarded_pool_allocator.h | 5 +++++ compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp | 8 ++++++++ compiler-rt/lib/gwp_asan/options.h | 6 ++++++ compiler-rt/lib/gwp_asan/options.inc | 6 ++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp index 5bf1284e1f89..6c06f10ab34e 100644 --- a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp +++ b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp @@ -58,6 +58,10 @@ void GuardedPoolAllocator::init(const options::Options &Opts) { SingletonPtr = this; Backtrace = Opts.Backtrace; + // OHOS_LOCAL begin + MinSampleSize = Opts.MinSampleSize; + WhiteListPath = Opts.WhiteListPath; + // OHOS_LOCAL end State.VersionMagic = {{AllocatorVersionMagic::kAllocatorVersionMagic[0], AllocatorVersionMagic::kAllocatorVersionMagic[1], @@ -220,9 +224,11 @@ void *GuardedPoolAllocator::allocate(size_t Size, size_t Alignment) { if (Alignment == 0) Alignment = alignof(max_align_t); + // OHOS_LOCAL begin if (!isPowerOfTwo(Alignment) || Alignment > State.maximumAllocationSize() || - Size > State.maximumAllocationSize()) + Size > State.maximumAllocationSize() || Size < MinSampleSize) return nullptr; + // OHOS_LOCAL end size_t BackingSize = getRequiredBackingSize(Size, Alignment, State.PageSize); if (BackingSize > State.maximumAllocationSize()) diff --git a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h index 152028acd822..59686070ab06 100644 --- a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h +++ b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h @@ -197,6 +197,11 @@ private: gwp_asan::AllocatorState State; + // OHOS_LOCAL begin + uint32_t MinSampleSize{0}; + const char *WhiteListPath = ""; + // OHOS_LOCAL end + // A mutex to protect the guarded slot and metadata pool for this class. Mutex PoolMutex; // Some unwinders can grab the libdl lock. In order to provide atfork diff --git a/compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp b/compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp index 5fff53b1e2cc..a46b33d939d4 100644 --- a/compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp +++ b/compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp @@ -32,6 +32,10 @@ typedef struct gwp_asan_option { gwp_asan::Printf_t gwp_asan_printf; gwp_asan::backtrace::PrintBacktrace_t printf_backtrace; gwp_asan::backtrace::SegvBacktrace_t segv_backtrace; + // OHOS_LOCAL begin + int min_sample_size; + const char *white_list_path; + // OHOS_LOCAL end } gwp_asan_option; void init_gwp_asan(void *init_options) @@ -50,6 +54,10 @@ void init_gwp_asan(void *init_options) opts.InstallSignalHandlers = input_opts->install_signal_handlers; opts.InstallForkHandlers = input_opts->install_fork_handlers; opts.Backtrace = input_opts->backtrace; + // OHOS_LOCAL begin + opts.MinSampleSize = input_opts->min_sample_size; + opts.WhiteListPath = input_opts->white_list_path; + // OHOS_LOCAL end guarded_poll_alloctor.init(opts); if (input_opts->install_signal_handlers) { gwp_asan::segv_handler::installSignalHandlersOhos(&guarded_poll_alloctor, input_opts->gwp_asan_printf, diff --git a/compiler-rt/lib/gwp_asan/options.h b/compiler-rt/lib/gwp_asan/options.h index 6fb43108b5de..06fd374e5310 100644 --- a/compiler-rt/lib/gwp_asan/options.h +++ b/compiler-rt/lib/gwp_asan/options.h @@ -35,6 +35,9 @@ typedef size_t (*Backtrace_t)(uintptr_t *TraceBuffer, size_t Size); struct Options { Backtrace_t Backtrace = nullptr; + // OHOS_LOCAL begin + const char *WhiteListPath = ""; + // OHOS_LOCAL end // Read the options from the included definitions file. #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description) \ @@ -49,6 +52,9 @@ struct Options { #undef GWP_ASAN_OPTION Backtrace = nullptr; + // OHOS_LOCAL begin + WhiteListPath = ""; + // OHOS_LOCAL end } }; } // namespace options diff --git a/compiler-rt/lib/gwp_asan/options.inc b/compiler-rt/lib/gwp_asan/options.inc index 9900a2ac40df..2cbb058a9e20 100644 --- a/compiler-rt/lib/gwp_asan/options.inc +++ b/compiler-rt/lib/gwp_asan/options.inc @@ -32,6 +32,12 @@ GWP_ASAN_OPTION(int, SampleRate, 5000, "selected for GWP-ASan sampling. Default is 5000. Sample rates " "up to (2^30 - 1) are supported.") +// OHOS_LOCAL begin +GWP_ASAN_OPTION(int, MinSampleSize, 0, + "Sampling is decided by whether the allocation size exceeds" + "the minimum threshold") +// OHOS_LOCAL end + // Developer note - This option is not actually processed by GWP-ASan itself. It // is included here so that a user can specify whether they want signal handlers // or not. The supporting allocator should inspect this value to see whether -- Gitee