Defer creation of default_config_

(This is a merge of http://go/wvgerrit/152969.)

C++ makes absolutely no guarantees about the order of initialization of
global variables in different compilation units. The class-scope static
WvCdmTestBase::default_config_ in test_base.cpp invokes the
ConfigTestEnv constructor on creation, which depends on the prior
initialization of several file-scope static variables in
config_test_env.cpp. Since those are different compilation units, there
is no guarantee that they will initialize in the correct order to avoid
referencing uninitialized memory. This is one of the reasons Google
Style really encourages people not to have global-scope variables with
complex types.

As it happens, on all our internal platforms, these files get linked in
such a way that the variables get initialized in the right order and
there is no crash. But that's not guaranteed, and some partners have
reported crashes here. In at least one case, the "right" linker order
was platform-dependent, and the partner ended up having to maintain
separate linker orders for separate platforms.

This patch defers default_config_ initialization until
WvCdmTestBase::Initialize() is called. By that time, all static
variables will be initialized, so it will be safe to reference them.

Bug: 173252165
Test: x86-64
Test: build_and_run_all_unit_tests.sh
Change-Id: If31128a999c7d6945f47293ca57f08e43d8274de
This commit is contained in:
John "Juce" Bruce
2022-06-17 15:54:36 -07:00
parent a3453e8bf5
commit 6b19df3268
4 changed files with 30 additions and 26 deletions

View File

@@ -34,7 +34,7 @@ class RebootTest : public WvCdmTestBaseWithEngine {
static bool ParseDump(const std::string& dump,
std::map<std::string, std::string>* data);
static int test_pass() { return default_config_.test_pass(); }
static int test_pass() { return default_config_->test_pass(); }
// Load a previously saved time. Returns 0 if the value does not exist or
// cannot be parsed.