Skip to content

Commit 2a5f7e5

Browse files
artempyanykhvitalybuka
authored andcommitted
[NFC][asan][odr] Use IntrusiveList for a ListOfGlobals
Extracted from llvm#100923.
1 parent c7c5e05 commit 2a5f7e5

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

compiler-rt/lib/asan/asan_globals.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "asan_suppressions.h"
2222
#include "asan_thread.h"
2323
#include "sanitizer_common/sanitizer_common.h"
24+
#include "sanitizer_common/sanitizer_list.h"
2425
#include "sanitizer_common/sanitizer_mutex.h"
2526
#include "sanitizer_common/sanitizer_placement_new.h"
2627
#include "sanitizer_common/sanitizer_stackdepot.h"
@@ -30,13 +31,14 @@ namespace __asan {
3031

3132
typedef __asan_global Global;
3233

33-
struct ListOfGlobals {
34-
const Global *g;
35-
ListOfGlobals *next;
34+
struct GlobalListNode {
35+
const Global *g = nullptr;
36+
GlobalListNode *next = nullptr;
3637
};
38+
typedef IntrusiveList<GlobalListNode> ListOfGlobals;
3739

3840
static Mutex mu_for_globals;
39-
static ListOfGlobals *list_of_all_globals;
41+
static ListOfGlobals list_of_all_globals;
4042

4143
static const int kDynamicInitGlobalsInitialCapacity = 512;
4244
struct DynInitGlobal {
@@ -73,6 +75,10 @@ ALWAYS_INLINE void PoisonRedZones(const Global &g) {
7375

7476
const uptr kMinimalDistanceFromAnotherGlobal = 64;
7577

78+
static void AddGlobalToList(ListOfGlobals &list, const Global *g) {
79+
list.push_front(new (GetGlobalLowLevelAllocator()) GlobalListNode{g});
80+
}
81+
7682
static bool IsAddressNearGlobal(uptr addr, const __asan_global &g) {
7783
if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
7884
if (addr >= g.beg + g.size_with_redzone) return false;
@@ -114,8 +120,8 @@ int GetGlobalsForAddress(uptr addr, Global *globals, u32 *reg_sites,
114120
if (!flags()->report_globals) return 0;
115121
Lock lock(&mu_for_globals);
116122
int res = 0;
117-
for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
118-
const Global &g = *l->g;
123+
for (const auto &l : list_of_all_globals) {
124+
const Global &g = *l.g;
119125
if (flags()->report_globals >= 2)
120126
ReportGlobal(g, "Search");
121127
if (IsAddressNearGlobal(addr, g)) {
@@ -149,12 +155,13 @@ static void CheckODRViolationViaIndicator(const Global *g) {
149155
}
150156
// If *odr_indicator is DEFINED, some module have already registered
151157
// externally visible symbol with the same name. This is an ODR violation.
152-
for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
153-
if (g->odr_indicator == l->g->odr_indicator &&
154-
(flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&
155-
!IsODRViolationSuppressed(g->name))
156-
ReportODRViolation(g, FindRegistrationSite(g),
157-
l->g, FindRegistrationSite(l->g));
158+
for (const auto &l : list_of_all_globals) {
159+
if (g->odr_indicator == l.g->odr_indicator &&
160+
(flags()->detect_odr_violation >= 2 || g->size != l.g->size) &&
161+
!IsODRViolationSuppressed(g->name)) {
162+
ReportODRViolation(g, FindRegistrationSite(g), l.g,
163+
FindRegistrationSite(l.g));
164+
}
158165
}
159166
}
160167

@@ -165,12 +172,13 @@ static void CheckODRViolationViaPoisoning(const Global *g) {
165172
if (__asan_region_is_poisoned(g->beg, g->size_with_redzone)) {
166173
// This check may not be enough: if the first global is much larger
167174
// the entire redzone of the second global may be within the first global.
168-
for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
169-
if (g->beg == l->g->beg &&
170-
(flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&
171-
!IsODRViolationSuppressed(g->name))
172-
ReportODRViolation(g, FindRegistrationSite(g),
173-
l->g, FindRegistrationSite(l->g));
175+
for (const auto &l : list_of_all_globals) {
176+
if (g->beg == l.g->beg &&
177+
(flags()->detect_odr_violation >= 2 || g->size != l.g->size) &&
178+
!IsODRViolationSuppressed(g->name)) {
179+
ReportODRViolation(g, FindRegistrationSite(g), l.g,
180+
FindRegistrationSite(l.g));
181+
}
174182
}
175183
}
176184
}
@@ -225,10 +233,9 @@ static void RegisterGlobal(const Global *g) {
225233
}
226234
if (CanPoisonMemory())
227235
PoisonRedZones(*g);
228-
ListOfGlobals *l = new (GetGlobalLowLevelAllocator()) ListOfGlobals;
229-
l->g = g;
230-
l->next = list_of_all_globals;
231-
list_of_all_globals = l;
236+
237+
AddGlobalToList(list_of_all_globals, g);
238+
232239
if (g->has_dynamic_init) {
233240
if (!dynamic_init_globals) {
234241
dynamic_init_globals = new (GetGlobalLowLevelAllocator()) VectorOfGlobals;

0 commit comments

Comments
 (0)