Avoid destroying default PosixEnv, safely (#11538)

Summary:
Use another static object to join threads instead.

This change is motivated by a case in which some code using NewLRUCache() -> ShardedCacheBase -> SemiStructuredUniqueIdGen -> GenerateRawUniqueId() -> Env::Default() was happening
during static destruction.

I didn't see anything else in PosixEnv or base classes that would cause a problem by not
destroying. (WinEnv is already not destroyed; see env_default.cc)

Pull Request resolved: https://github.com/facebook/rocksdb/pull/11538UndefinedBehaviorSanitizer: undefined-behavior env/env_test.cc:3561:23 in
$
```

Test Plan:
test added, which would previously fail with UBSAN:

```
$ ./env_test --gtest_filter=*Destruct*
Note: Google Test filter = *Destruct*
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from EnvTestMisc
[ RUN      ] EnvTestMisc.StaticDestruction
[       OK ] EnvTestMisc.StaticDestruction (0 ms)
[----------] 1 test from EnvTestMisc (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.
env/env_test.cc:3561:23: runtime error: member call on address 0x7f7b96671ca8 which does not point to an object of type 'rocksdb::Env'
0x7f7b96671ca8: note: object is of type 'N7rocksdb12ConfigurableE'
 00 00 00 00  90 a7 f7 95 7b 7f 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for 'N7rocksdb12ConfigurableE'

Reviewed By: jowlyzhang

Differential Revision: D46737389

Pulled By: pdillinger

fbshipit-source-id: 0f80a443bf799ffc5641e898cf3a75f7d10a987b
oxigraph-8.3.2
Peter Dillinger 2 years ago
parent 32c6de7bc3
commit e978dccd7a
  1. 20
      env/env_posix.cc
  2. 17
      env/env_test.cc
  3. 1
      unreleased_history/bug_fixes/destroy_posix_env.md

20
env/env_posix.cc vendored

@ -213,13 +213,14 @@ class PosixEnv : public CompositeEnv {
const char* Name() const override { return kClassName(); }
const char* NickName() const override { return kDefaultName(); }
~PosixEnv() override {
if (this == Env::Default()) {
for (const auto tid : threads_to_join_) {
struct JoinThreadsOnExit {
explicit JoinThreadsOnExit(PosixEnv& _deflt) : deflt(_deflt) {}
~JoinThreadsOnExit() {
for (const auto tid : deflt.threads_to_join_) {
pthread_join(tid, nullptr);
}
for (int pool_id = 0; pool_id < Env::Priority::TOTAL; ++pool_id) {
thread_pools_[pool_id].JoinAllThreads();
deflt.thread_pools_[pool_id].JoinAllThreads();
}
// Do not delete the thread_status_updater_ in order to avoid the
// free after use when Env::Default() is destructed while some other
@ -227,7 +228,8 @@ class PosixEnv : public CompositeEnv {
// PosixEnv instances use the same thread_status_updater_, so never
// explicitly delete it.
}
}
PosixEnv& deflt;
};
void SetFD_CLOEXEC(int fd, const EnvOptions* options) {
if ((options == nullptr || options->set_fd_cloexec) && fd > 0) {
@ -501,9 +503,11 @@ Env* Env::Default() {
ThreadLocalPtr::InitSingletons();
CompressionContextCache::InitSingleton();
INIT_SYNC_POINT_SINGLETONS();
// ~PosixEnv must be called on exit
//**TODO: Can we make this a STATIC_AVOID_DESTRUCTION?
static PosixEnv default_env;
// Avoid problems with accessing most members of Env::Default() during
// static destruction.
STATIC_AVOID_DESTRUCTION(PosixEnv, default_env);
// This destructor must be called on exit
static PosixEnv::JoinThreadsOnExit thread_joiner(default_env);
return &default_env;
}

17
env/env_test.cc vendored

@ -3551,6 +3551,23 @@ TEST_F(TestAsyncRead, ReadAsync) {
}
}
}
struct StaticDestructionTester {
bool activated = false;
~StaticDestructionTester() {
if (activated && !kMustFreeHeapAllocations) {
// Make sure we can still call some things on default Env.
std::string hostname;
Env::Default()->GetHostNameString(&hostname);
}
}
} static_destruction_tester;
TEST(EnvTestMisc, StaticDestruction) {
// Check for any crashes during static destruction.
static_destruction_tester.activated = true;
}
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {

@ -0,0 +1 @@
Reduced cases of illegally using Env::Default() during static destruction by never destroying the internal PosixEnv itself (except for builds checking for memory leaks). (#11538)
Loading…
Cancel
Save