Skip to content

Commit d04dcac

Browse files
committed
[𝘀𝗽𝗿] changes to main this commit is based on
Created using spr 1.3.4 [skip ci]
1 parent ae6dc64 commit d04dcac

File tree

3 files changed

+37
-43
lines changed

3 files changed

+37
-43
lines changed

compiler-rt/lib/asan/asan_globals.cpp

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "sanitizer_common/sanitizer_placement_new.h"
2828
#include "sanitizer_common/sanitizer_stackdepot.h"
2929
#include "sanitizer_common/sanitizer_symbolizer.h"
30+
#include "sanitizer_common/sanitizer_thread_safety.h"
3031

3132
namespace __asan {
3233

@@ -40,17 +41,17 @@ typedef IntrusiveList<GlobalListNode> ListOfGlobals;
4041
typedef DenseMap<uptr, ListOfGlobals> MapOfGlobals;
4142

4243
static Mutex mu_for_globals;
43-
static ListOfGlobals list_of_all_globals;
44-
static MapOfGlobals map_of_globals_by_indicator;
44+
static ListOfGlobals list_of_all_globals SANITIZER_GUARDED_BY(mu_for_globals);
45+
static MapOfGlobals map_of_globals_by_indicator
46+
SANITIZER_GUARDED_BY(mu_for_globals);
4547

46-
static const int kDynamicInitGlobalsInitialCapacity = 512;
4748
struct DynInitGlobal {
48-
Global g;
49-
bool initialized;
49+
Global g = {};
50+
bool initialized = false;
51+
DynInitGlobal *next = nullptr;
5052
};
51-
typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;
52-
// Lazy-initialized and never deleted.
53-
static VectorOfGlobals *dynamic_init_globals;
53+
typedef IntrusiveList<DynInitGlobal> DynInitGlobals;
54+
static DynInitGlobals dynamic_init_globals SANITIZER_GUARDED_BY(mu_for_globals);
5455

5556
// We want to remember where a certain range of globals was registered.
5657
struct GlobalRegistrationSite {
@@ -147,7 +148,8 @@ enum GlobalSymbolState {
147148
// Check ODR violation for given global G via special ODR indicator. We use
148149
// this method in case compiler instruments global variables through their
149150
// local aliases.
150-
static void CheckODRViolationViaIndicator(const Global *g) {
151+
static void CheckODRViolationViaIndicator(const Global *g)
152+
SANITIZER_REQUIRES(mu_for_globals) {
151153
// Instrumentation requests to skip ODR check.
152154
if (g->odr_indicator == UINTPTR_MAX)
153155
return;
@@ -175,7 +177,8 @@ static void CheckODRViolationViaIndicator(const Global *g) {
175177
// Check ODR violation for given global G by checking if it's already poisoned.
176178
// We use this method in case compiler doesn't use private aliases for global
177179
// variables.
178-
static void CheckODRViolationViaPoisoning(const Global *g) {
180+
static void CheckODRViolationViaPoisoning(const Global *g)
181+
SANITIZER_REQUIRES(mu_for_globals) {
179182
if (__asan_region_is_poisoned(g->beg, g->size_with_redzone)) {
180183
// This check may not be enough: if the first global is much larger
181184
// the entire redzone of the second global may be within the first global.
@@ -213,7 +216,7 @@ static inline bool UseODRIndicator(const Global *g) {
213216
// Register a global variable.
214217
// This function may be called more than once for every global
215218
// so we store the globals in a map.
216-
static void RegisterGlobal(const Global *g) {
219+
static void RegisterGlobal(const Global *g) SANITIZER_REQUIRES(mu_for_globals) {
217220
CHECK(AsanInited());
218221
if (flags()->report_globals >= 2)
219222
ReportGlobal(*g, "Added");
@@ -244,16 +247,13 @@ static void RegisterGlobal(const Global *g) {
244247
AddGlobalToList(list_of_all_globals, g);
245248

246249
if (g->has_dynamic_init) {
247-
if (!dynamic_init_globals) {
248-
dynamic_init_globals = new (GetGlobalLowLevelAllocator()) VectorOfGlobals;
249-
dynamic_init_globals->reserve(kDynamicInitGlobalsInitialCapacity);
250-
}
251-
DynInitGlobal dyn_global = { *g, false };
252-
dynamic_init_globals->push_back(dyn_global);
250+
dynamic_init_globals.push_back(new (GetGlobalLowLevelAllocator())
251+
DynInitGlobal{*g, false});
253252
}
254253
}
255254

256-
static void UnregisterGlobal(const Global *g) {
255+
static void UnregisterGlobal(const Global *g)
256+
SANITIZER_REQUIRES(mu_for_globals) {
257257
CHECK(AsanInited());
258258
if (flags()->report_globals >= 2)
259259
ReportGlobal(*g, "Removed");
@@ -275,12 +275,11 @@ static void UnregisterGlobal(const Global *g) {
275275
}
276276

277277
void StopInitOrderChecking() {
278-
Lock lock(&mu_for_globals);
279-
if (!flags()->check_initialization_order || !dynamic_init_globals)
278+
if (!flags()->check_initialization_order)
280279
return;
280+
Lock lock(&mu_for_globals);
281281
flags()->check_initialization_order = false;
282-
for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
283-
DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
282+
for (const DynInitGlobal &dyn_g : dynamic_init_globals) {
284283
const Global *g = &dyn_g.g;
285284
// Unpoison the whole global.
286285
PoisonShadowForGlobal(g, 0);
@@ -441,18 +440,15 @@ void __asan_unregister_globals(__asan_global *globals, uptr n) {
441440
// poisons all global variables not defined in this TU, so that a dynamic
442441
// initializer can only touch global variables in the same TU.
443442
void __asan_before_dynamic_init(const char *module_name) {
444-
if (!flags()->check_initialization_order ||
445-
!CanPoisonMemory() ||
446-
!dynamic_init_globals)
443+
if (!flags()->check_initialization_order || !CanPoisonMemory())
447444
return;
448445
bool strict_init_order = flags()->strict_init_order;
449446
CHECK(module_name);
450447
CHECK(AsanInited());
451448
Lock lock(&mu_for_globals);
452449
if (flags()->report_globals >= 3)
453450
Printf("DynInitPoison module: %s\n", module_name);
454-
for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
455-
DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
451+
for (DynInitGlobal &dyn_g : dynamic_init_globals) {
456452
const Global *g = &dyn_g.g;
457453
if (dyn_g.initialized)
458454
continue;
@@ -467,15 +463,12 @@ void __asan_before_dynamic_init(const char *module_name) {
467463
// all dynamically initialized globals except for those defined in the current
468464
// TU are poisoned. It simply unpoisons all dynamically initialized globals.
469465
void __asan_after_dynamic_init() {
470-
if (!flags()->check_initialization_order ||
471-
!CanPoisonMemory() ||
472-
!dynamic_init_globals)
466+
if (!flags()->check_initialization_order || !CanPoisonMemory())
473467
return;
474468
CHECK(AsanInited());
475469
Lock lock(&mu_for_globals);
476470
// FIXME: Optionally report that we're unpoisoning globals from a module.
477-
for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
478-
DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
471+
for (const DynInitGlobal &dyn_g : dynamic_init_globals) {
479472
const Global *g = &dyn_g.g;
480473
if (!dyn_g.initialized) {
481474
// Unpoison the whole global.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Linker initialized:
22
int getAB();
3-
static int ab = getAB();
3+
int ab = getAB();
44
// Function local statics:
55
int countCalls();
6-
static int one = countCalls();
6+
int one = countCalls();
77
// Trivial constructor, non-trivial destructor:
88
int getStructWithDtorValue();
9-
static int val = getStructWithDtorValue();
9+
int val = getStructWithDtorValue();

compiler-rt/test/asan/TestCases/initialization-nobug.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
// A collection of various initializers which shouldn't trip up initialization
22
// order checking. If successful, this will just return 0.
33

4-
// RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-nobug-extra.cpp -o %t
5-
// RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
6-
// RUN: %clangxx_asan -O1 %s %p/Helpers/initialization-nobug-extra.cpp -o %t
7-
// RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
8-
// RUN: %clangxx_asan -O2 %s %p/Helpers/initialization-nobug-extra.cpp -o %t
9-
// RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
10-
// RUN: %clangxx_asan -O3 %s %p/Helpers/initialization-nobug-extra.cpp -o %t
11-
// RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
4+
// RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
5+
// RUN: %clangxx_asan -O1 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
6+
// RUN: %clangxx_asan -O2 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
7+
// RUN: %clangxx_asan -O3 %s %p/Helpers/initialization-nobug-extra.cpp -o %t && %env_asan_opts=check_initialization_order=true:report_globals=3 %run %t 2>&1 | FileCheck %s --implicit-check-not "DynInit"
128

139
// Simple access:
1410
// Make sure that accessing a global in the same TU is safe
1511

1612
bool condition = true;
13+
__attribute__((noinline, weak))
1714
int initializeSameTU() {
1815
return condition ? 0x2a : 052;
1916
}
@@ -46,3 +43,7 @@ StructWithDtor struct_with_dtor;
4643
int getStructWithDtorValue() { return struct_with_dtor.value; }
4744

4845
int main() { return 0; }
46+
47+
48+
// CHECK: DynInitPoison module: {{.*}}initialization-nobug.cpp
49+
// CHECK: DynInitPoison module: {{.*}}initialization-nobug-extra.cpp

0 commit comments

Comments
 (0)