Skip to content

Commit 873f525

Browse files
authored
Allocate global objects so that their destructors don't run. (#6849)
Fixes #6844.
1 parent f943eba commit 873f525

File tree

9 files changed

+34
-26
lines changed

9 files changed

+34
-26
lines changed

Firestore/core/src/auth/user.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ User::User(std::string uid) : uid_{std::move(uid)}, is_authenticated_{true} {
3232
}
3333

3434
const User& User::Unauthenticated() {
35-
static const User kUnauthenticated;
36-
return kUnauthenticated;
35+
static const User* kUnauthenticated = new User();
36+
return *kUnauthenticated;
3737
}
3838

3939
} // namespace auth

Firestore/core/src/immutable/array_sorted_map.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google
2+
* Copyright 2018 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -326,9 +326,11 @@ class ArraySortedMap : public SortedMapBase {
326326

327327
private:
328328
static array_pointer EmptyArray() {
329-
static const array_pointer kEmptyArray =
330-
std::make_shared<const array_type>();
331-
return kEmptyArray;
329+
static const array_pointer* kEmptyArray = [] {
330+
auto array = new array_type();
331+
return new std::shared_ptr<const array_type>(array);
332+
}();
333+
return *kEmptyArray;
332334
}
333335

334336
static array_pointer SortedArray(std::initializer_list<value_type> entries,

Firestore/core/src/immutable/llrb_node.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,18 @@ class LlrbNode : public SortedMapBase {
162162
* Returns a shared Empty node, to cut down on allocations in the base case.
163163
*/
164164
static const std::shared_ptr<Rep>& EmptyRep() {
165-
static const std::shared_ptr<Rep> empty_rep = [] {
166-
auto empty = std::make_shared<Rep>(Rep{std::pair<K, V>{}, Color::Black,
167-
/* size= */ 0u, LlrbNode{nullptr},
168-
LlrbNode{nullptr}});
165+
static const std::shared_ptr<Rep>* empty_rep = [] {
166+
auto rep = new Rep{std::pair<K, V>{}, Color::Black,
167+
/* size= */ 0u, LlrbNode{nullptr}, LlrbNode{nullptr}};
168+
auto empty = new std::shared_ptr<Rep>{rep};
169169

170170
// Set up the empty Rep such that you can traverse infinitely down left
171171
// and right links.
172-
empty->left_.rep_ = empty;
173-
empty->right_.rep_ = empty;
172+
(*empty)->left_.rep_ = *empty;
173+
(*empty)->right_.rep_ = *empty;
174174
return empty;
175175
}();
176-
return empty_rep;
176+
return *empty_rep;
177177
}
178178

179179
/**

Firestore/core/src/local/leveldb_transaction.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ LevelDbTransaction::LevelDbTransaction(DB* db,
146146
}
147147

148148
const ReadOptions& LevelDbTransaction::DefaultReadOptions() {
149+
// ReadOptions is trivial so it does not need to be heap-allocated.
149150
static ReadOptions options = [] {
150151
ReadOptions read_options;
151152
read_options.verify_checksums = true;
@@ -155,6 +156,7 @@ const ReadOptions& LevelDbTransaction::DefaultReadOptions() {
155156
}
156157

157158
const WriteOptions& LevelDbTransaction::DefaultWriteOptions() {
159+
// WriteOptions is trivial so it does not need to be heap-allocated.
158160
static WriteOptions options;
159161
return options;
160162
}

Firestore/core/src/model/document_key.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ DocumentKey DocumentKey::FromSegments(std::initializer_list<std::string> list) {
5959
}
6060

6161
const DocumentKey& DocumentKey::Empty() {
62-
static const DocumentKey empty;
63-
return empty;
62+
static const DocumentKey* empty = new DocumentKey();
63+
return *empty;
6464
}
6565

6666
bool DocumentKey::IsDocumentKey(const ResourcePath& path) {

Firestore/core/src/remote/datastore.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,14 @@ bool Datastore::IsPermanentWriteError(const Status& error) {
334334

335335
std::string Datastore::GetAllowlistedHeadersAsString(
336336
const GrpcCall::Metadata& headers) {
337-
static std::unordered_set<std::string> allowlist = {
337+
static auto* allowlist = new std::unordered_set<std::string>{
338338
"date", "x-google-backends", "x-google-netmon-label", "x-google-service",
339339
"x-google-gfe-request-trace"};
340340

341341
std::string result;
342+
auto end = allowlist->end();
342343
for (const auto& kv : headers) {
343-
if (allowlist.find(MakeString(kv.first)) != allowlist.end()) {
344+
if (allowlist->find(MakeString(kv.first)) != end) {
344345
absl::StrAppend(&result, MakeStringView(kv.first), ": ",
345346
MakeStringView(kv.second), "\n");
346347
}

Firestore/core/src/remote/grpc_connection.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class HostConfigMap {
125125
};
126126

127127
HostConfigMap& Config() {
128-
static HostConfigMap config_by_host;
129-
return config_by_host;
128+
static auto* config_by_host = new HostConfigMap();
129+
return *config_by_host;
130130
}
131131

132132
std::string GetCppLanguageToken() {
@@ -170,8 +170,8 @@ class ClientLanguageToken {
170170
};
171171

172172
ClientLanguageToken& LanguageToken() {
173-
static ClientLanguageToken token;
174-
return token;
173+
static auto* token = new ClientLanguageToken();
174+
return *token;
175175
}
176176

177177
void AddCloudApiHeader(grpc::ClientContext& context) {

Firestore/core/src/util/autoid.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google
2+
* Copyright 2017 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,14 +24,16 @@
2424
namespace firebase {
2525
namespace firestore {
2626
namespace util {
27-
2827
namespace {
2928

3029
const int kAutoIdLength = 20;
3130
const char kAutoIdAlphabet[] =
3231
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
3332

34-
SecureRandom shared_random;
33+
SecureRandom& GetSharedRandom() {
34+
static auto* shared_random = new SecureRandom();
35+
return *shared_random;
36+
}
3537

3638
} // namespace
3739

@@ -44,6 +46,7 @@ std::string CreateAutoId() {
4446
// * uniform_int_distribution is inclusive on both sizes
4547
std::uniform_int_distribution<int> letters(0, sizeof(kAutoIdAlphabet) - 2);
4648

49+
SecureRandom& shared_random = GetSharedRandom();
4750
for (int i = 0; i < kAutoIdLength; i++) {
4851
int rand_index = letters(shared_random);
4952
auto_id.append(1, kAutoIdAlphabet[rand_index]);

Firestore/core/src/util/filesystem_common.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace firestore {
2727
namespace util {
2828

2929
Filesystem* Filesystem::Default() {
30-
static Filesystem filesystem;
31-
return &filesystem;
30+
static auto* filesystem = new Filesystem();
31+
return filesystem;
3232
}
3333

3434
Status Filesystem::RecursivelyCreateDir(const Path& path) {

0 commit comments

Comments
 (0)