Skip to content

Commit e4bada2

Browse files
committed
[asan] get rid of std::map. No STL and almost no libstdc++ left.
llvm-svn: 145706
1 parent dbf20b8 commit e4bada2

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

compiler-rt/lib/asan/asan_globals.cc

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@
2121
#include "asan_thread.h"
2222

2323
#include <ctype.h>
24-
#include <map>
2524

2625
namespace __asan {
2726

2827
typedef __asan_global Global;
2928

29+
struct ListOfGlobals {
30+
const Global *g;
31+
ListOfGlobals *next;
32+
};
33+
3034
static AsanLock mu_for_globals(LINKER_INITIALIZED);
31-
typedef std::map<uintptr_t, Global> MapOfGlobals;
32-
static MapOfGlobals *g_all_globals = NULL;
35+
static ListOfGlobals *list_of_globals;
36+
static LowLevelAllocator allocator_for_globals(LINKER_INITIALIZED);
3337

3438
void PoisonRedZones(const Global &g) {
3539
size_t shadow_rz_size = kGlobalAndStackRedzone >> SHADOW_SCALE;
@@ -86,13 +90,9 @@ bool DescribeAddrIfMyRedZone(const Global &g, uintptr_t addr) {
8690
bool DescribeAddrIfGlobal(uintptr_t addr) {
8791
if (!FLAG_report_globals) return false;
8892
ScopedLock lock(&mu_for_globals);
89-
if (!g_all_globals) return false;
9093
bool res = false;
91-
// Just iterate. May want to use binary search instead.
92-
for (MapOfGlobals::iterator i = g_all_globals->begin(),
93-
end = g_all_globals->end(); i != end; ++i) {
94-
Global &g = i->second;
95-
CHECK(i->first == g.beg);
94+
for (ListOfGlobals *l = list_of_globals; l; l = l->next) {
95+
const Global &g = *l->g;
9696
if (FLAG_report_globals >= 2)
9797
Printf("Search Global: beg=%p size=%ld name=%s\n",
9898
g.beg, g.size, g.name);
@@ -108,15 +108,17 @@ static void RegisterGlobal(const Global *g) {
108108
CHECK(asan_inited);
109109
if (!FLAG_report_globals) return;
110110
ScopedLock lock(&mu_for_globals);
111-
if (!g_all_globals)
112-
g_all_globals = new MapOfGlobals;
113111
CHECK(AddrIsInMem(g->beg));
112+
CHECK(AddrIsAlignedByGranularity(g->beg));
113+
PoisonRedZones(*g);
114+
ListOfGlobals *l =
115+
(ListOfGlobals*)allocator_for_globals.Allocate(sizeof(ListOfGlobals));
116+
l->g = g;
117+
l->next = list_of_globals;
118+
list_of_globals = l;
114119
if (FLAG_report_globals >= 2)
115120
Printf("Added Global: beg=%p size=%ld name=%s\n",
116121
g->beg, g->size, g->name);
117-
CHECK(AddrIsAlignedByGranularity(g->beg));
118-
PoisonRedZones(*g);
119-
(*g_all_globals)[g->beg] = *g;
120122
}
121123

122124
} // namespace __asan

compiler-rt/lib/asan/asan_internal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ const int kAsanGlobalRedzoneMagic = 0xf9;
163163
static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
164164
static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
165165

166+
// -------------------------- LowLevelAllocator ----- {{{1
167+
// A simple low-level memory allocator for internal use.
168+
class LowLevelAllocator {
169+
public:
170+
explicit LowLevelAllocator(LinkerInitialized) {}
171+
// 'size' must be a power of two.
172+
// Requires an external lock.
173+
void *Allocate(size_t size);
174+
private:
175+
char *allocated_end_;
176+
char *allocated_current_;
177+
};
178+
166179
// -------------------------- Atomic ---------------- {{{1
167180
static inline int AtomicInc(int *a) {
168181
if (!FLAG_mt) return ++(*a);

compiler-rt/lib/asan/asan_rtl.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ static void protect_range(uintptr_t beg, uintptr_t end) {
174174
CHECK(res == (void*)beg);
175175
}
176176

177+
// ---------------------- LowLevelAllocator ------------- {{{1
178+
void *LowLevelAllocator::Allocate(size_t size) {
179+
CHECK((size & (size - 1)) == 0 && "size must be a power of two");
180+
if (allocated_end_ - allocated_current_ < size) {
181+
size_t size_to_allocate = Max(size, kPageSize);
182+
allocated_current_ = (char*)asan_mmap(0, size_to_allocate,
183+
PROT_READ | PROT_WRITE,
184+
MAP_PRIVATE | MAP_ANON, -1, 0);
185+
CHECK((allocated_current_ != (char*)-1) && "Can't mmap");
186+
allocated_end_ = allocated_current_ + size_to_allocate;
187+
}
188+
CHECK(allocated_end_ - allocated_current_ >= size);
189+
void *res = allocated_current_;
190+
allocated_current_ += size;
191+
return res;
192+
}
193+
177194
// ---------------------- DescribeAddress -------------------- {{{1
178195
static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
179196
AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);

0 commit comments

Comments
 (0)