Skip to content

Commit 7adb7aa

Browse files
authored
[GWP-ASan] Various test fixes. (#94938)
When running some tests with --gtest_repeat=100 --gtest_shuffle, I encountered some problems because the allocator wasn't torn down completely, and the singleton pointer ended up pointing to a use-after-scope'd object. This patch has a couple of fixes and niceties: 1. Removing the once-init stuff from tests, now that it's implicitly done in GuardedPoolAllocator::installAtFork() anyway. 2. Calling uninitTestOnly() in the late_init test. 3. Resetting the HasReportedBadPoolAccess when the signal handlers are installed (allowing for --gtest_repeat w/ recoverable mode). 4. Adding a check and resetting the singleton pointer in uninitTestOnly().
1 parent b446f90 commit 7adb7aa

File tree

5 files changed

+5
-23
lines changed

5 files changed

+5
-23
lines changed

compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ void GuardedPoolAllocator::init(const options::Options &Opts) {
5757
Check(Opts.MaxSimultaneousAllocations >= 0,
5858
"GWP-ASan Error: MaxSimultaneousAllocations is < 0.");
5959

60+
Check(SingletonPtr == nullptr,
61+
"There's already a live GuardedPoolAllocator!");
6062
SingletonPtr = this;
6163
Backtrace = Opts.Backtrace;
6264

@@ -158,6 +160,7 @@ void GuardedPoolAllocator::uninitTestOnly() {
158160
FreeSlots = nullptr;
159161
}
160162
*getThreadLocals() = ThreadLocalPackedVariables();
163+
SingletonPtr = nullptr;
161164
}
162165

163166
// Note, minimum backing allocation size in GWP-ASan is always one page, and

compiler-rt/lib/gwp_asan/optional/segv_handler_posix.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ void installSignalHandlers(gwp_asan::GuardedPoolAllocator *GPA, Printf_t Printf,
257257
Action.sa_flags = SA_SIGINFO;
258258
sigaction(SIGSEGV, &Action, &PreviousHandler);
259259
SignalHandlerInstalled = true;
260+
HasReportedBadPoolAccess = false;
260261
}
261262

262263
void uninstallSignalHandlers() {

compiler-rt/lib/gwp_asan/tests/harness.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010

1111
#include <string>
1212

13-
namespace gwp_asan {
14-
namespace test {
15-
bool OnlyOnce() {
16-
static int x = 0;
17-
return !x++;
18-
}
19-
} // namespace test
20-
} // namespace gwp_asan
21-
2213
// Optnone to ensure that the calls to these functions are not optimized away,
2314
// as we're looking for them in the backtraces.
2415
__attribute__((optnone)) char *

compiler-rt/lib/gwp_asan/tests/harness.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ namespace test {
3939
// `optional/printf_sanitizer_common.cpp` which supplies the __sanitizer::Printf
4040
// for this purpose.
4141
Printf_t getPrintfFunction();
42-
43-
// First call returns true, all the following calls return false.
44-
bool OnlyOnce();
45-
4642
}; // namespace test
4743
}; // namespace gwp_asan
4844

@@ -57,10 +53,7 @@ class DefaultGuardedPoolAllocator : public ::testing::Test {
5753
public:
5854
void SetUp() override {
5955
gwp_asan::options::Options Opts;
60-
Opts.setDefaults();
6156
MaxSimultaneousAllocations = Opts.MaxSimultaneousAllocations;
62-
63-
Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce();
6457
GPA.init(Opts);
6558
}
6659

@@ -78,12 +71,8 @@ class CustomGuardedPoolAllocator : public ::testing::Test {
7871
InitNumSlots(decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
7972
MaxSimultaneousAllocationsArg) {
8073
gwp_asan::options::Options Opts;
81-
Opts.setDefaults();
82-
8374
Opts.MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
8475
MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
85-
86-
Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce();
8776
GPA.init(Opts);
8877
}
8978

@@ -100,10 +89,7 @@ class BacktraceGuardedPoolAllocator
10089
public:
10190
void SetUp() override {
10291
gwp_asan::options::Options Opts;
103-
Opts.setDefaults();
104-
10592
Opts.Backtrace = gwp_asan::backtrace::getBacktraceFunction();
106-
Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce();
10793
GPA.init(Opts);
10894

10995
// In recoverable mode, capture GWP-ASan logs to an internal buffer so that

compiler-rt/lib/gwp_asan/tests/late_init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ TEST(LateInit, CheckLateInitIsOK) {
2222

2323
GPA.init(Opts);
2424
EXPECT_TRUE(GPA.shouldSample());
25+
GPA.uninitTestOnly();
2526
}

0 commit comments

Comments
 (0)