From 534378334a1d04aee0bd7d38372d7085f0eb5a0a 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 GWP_ASan Options 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. Change-Id: I32f26e4b24234acb7ff278131babb7dbe2665ef9 Signed-off-by: Elfgo --- compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp | 8 +++++++- 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 ++++++ 4 files changed, 27 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..3582b092e9aa 100644 --- a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp +++ b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp @@ -8,6 +8,9 @@ #include "gwp_asan/guarded_pool_allocator.h" +// OHOS_LOCAL begin +#include "gwp_asan/optional/options_parser.h" +// OHOS_LOCAL end #include "gwp_asan/options.h" #include "gwp_asan/utilities.h" @@ -220,9 +223,12 @@ 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 < options::getOptions().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/gwp_asan_c_interface.cpp b/compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp index e0bbfde90950..7a63d3dffbcf 100644 --- a/compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp +++ b/compiler-rt/lib/gwp_asan/gwp_asan_c_interface.cpp @@ -29,6 +29,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) @@ -42,6 +46,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..e10fd2acebb6 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, 1024, + "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