[GWP-ASan] Add inbuilt options parser.
Adds a modified options parser (shamefully pulled from Scudo, which
shamefully pulled it from sanitizer-common) to GWP-ASan. This allows
customers (Android) to parse options strings in a common way.
Depends on D94117.
AOSP side of these patches is staged at:
- sepolicy (sysprops should only be settable by the shell, in both root and
unrooted conditions):
https://android-review.googlesource.com/c/platform/system/sepolicy/+/1517238
- zygote updates:
https://android-review.googlesource.com/c/platform/frameworks/base/+/1515009
- bionic changes to add `gwp_asan.<process_name>` system property, and
GWP_ASAN_OPTIONS environment variable:
https://android-review.googlesource.com/c/platform/bionic/+/1514989
Reviewed By: eugenis
Differential Revision: https://reviews.llvm.org/D92696
GitOrigin-RevId: 6a42cbf6d2116b52cb59aa3e23bef93a30cf2dc8
diff --git a/combined.h b/combined.h
index 11370be..0df7a65 100644
--- a/combined.h
+++ b/combined.h
@@ -28,6 +28,7 @@
#ifdef GWP_ASAN_HOOKS
#include "gwp_asan/guarded_pool_allocator.h"
#include "gwp_asan/optional/backtrace.h"
+#include "gwp_asan/optional/options_parser.h"
#include "gwp_asan/optional/segv_handler.h"
#endif // GWP_ASAN_HOOKS
@@ -183,17 +184,12 @@
// be functional, best called from PostInitCallback.
void initGwpAsan() {
#ifdef GWP_ASAN_HOOKS
- gwp_asan::options::Options Opt;
- Opt.Enabled = getFlags()->GWP_ASAN_Enabled;
// Bear in mind - Scudo has its own alignment guarantees that are strictly
// enforced. Scudo exposes the same allocation function for everything from
// malloc() to posix_memalign, so in general this flag goes unused, as Scudo
// will always ask GWP-ASan for an aligned amount of bytes.
- Opt.PerfectlyRightAlign = getFlags()->GWP_ASAN_PerfectlyRightAlign;
- Opt.MaxSimultaneousAllocations =
- getFlags()->GWP_ASAN_MaxSimultaneousAllocations;
- Opt.SampleRate = getFlags()->GWP_ASAN_SampleRate;
- Opt.InstallSignalHandlers = getFlags()->GWP_ASAN_InstallSignalHandlers;
+ gwp_asan::options::initOptions(getEnv("GWP_ASAN_OPTIONS"), Printf);
+ gwp_asan::options::Options Opt = gwp_asan::options::getOptions();
// Embedded GWP-ASan is locked through the Scudo atfork handler (via
// Allocator::disable calling GWPASan.disable). Disable GWP-ASan's atfork
// handler.
diff --git a/flags.cpp b/flags.cpp
index de5153b..285143a 100644
--- a/flags.cpp
+++ b/flags.cpp
@@ -23,13 +23,6 @@
#define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
#include "flags.inc"
#undef SCUDO_FLAG
-
-#ifdef GWP_ASAN_HOOKS
-#define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description) \
- GWP_ASAN_##Name = DefaultValue;
-#include "gwp_asan/options.inc"
-#undef GWP_ASAN_OPTION
-#endif // GWP_ASAN_HOOKS
}
void registerFlags(FlagParser *Parser, Flags *F) {
@@ -38,14 +31,6 @@
reinterpret_cast<void *>(&F->Name));
#include "flags.inc"
#undef SCUDO_FLAG
-
-#ifdef GWP_ASAN_HOOKS
-#define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description) \
- Parser->registerFlag("GWP_ASAN_" #Name, Description, FlagType::FT_##Type, \
- reinterpret_cast<void *>(&F->GWP_ASAN_##Name));
-#include "gwp_asan/options.inc"
-#undef GWP_ASAN_OPTION
-#endif // GWP_ASAN_HOOKS
}
static const char *getCompileDefinitionScudoDefaultOptions() {
diff --git a/tests/flags_test.cpp b/tests/flags_test.cpp
index 0205052..45918ad 100644
--- a/tests/flags_test.cpp
+++ b/tests/flags_test.cpp
@@ -117,18 +117,3 @@
EXPECT_TRUE(Flags.delete_size_mismatch);
EXPECT_EQ(2048, Flags.quarantine_max_chunk_size);
}
-
-#ifdef GWP_ASAN_HOOKS
-TEST(ScudoFlagsTest, GWPASanFlags) {
- scudo::FlagParser Parser;
- scudo::Flags Flags;
- scudo::registerFlags(&Parser, &Flags);
- Flags.setDefaults();
- Flags.GWP_ASAN_Enabled = false;
- Parser.parseString("GWP_ASAN_Enabled=true:GWP_ASAN_SampleRate=1:"
- "GWP_ASAN_InstallSignalHandlers=false");
- EXPECT_TRUE(Flags.GWP_ASAN_Enabled);
- EXPECT_FALSE(Flags.GWP_ASAN_InstallSignalHandlers);
- EXPECT_EQ(1, Flags.GWP_ASAN_SampleRate);
-}
-#endif // GWP_ASAN_HOOKS