Skip to content

Commit f42c138

Browse files
author
Maxim Ostapenko
committedSep 23, 2016
[msan] Prevent initialization failure with newer (2.23+) glibc in use.
This patch is pretty the same as http://reviews.llvm.org/D20235 that we used for ASan. Using the same hack for MSan fixes its initialization with newer Glibc in use. Differential Revision: https://reviews.llvm.org/D24736 llvm-svn: 282232
1 parent a02e394 commit f42c138

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed
 

‎compiler-rt/lib/asan/asan_malloc_linux.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
7878
if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
7979
uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
8080
uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
81-
void *new_ptr = asan_malloc(size, &stack);
81+
void *new_ptr;
82+
if (UNLIKELY(!asan_inited))
83+
new_ptr = AllocateFromLocalPool(size);
84+
else
85+
new_ptr = asan_malloc(size, &stack);
8286
internal_memcpy(new_ptr, ptr, copy_size);
8387
return new_ptr;
8488
}

‎compiler-rt/lib/msan/msan_interceptors.cc

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ bool IsInInterceptorScope() {
6464
return in_interceptor_scope;
6565
}
6666

67+
static uptr allocated_for_dlsym;
68+
static const uptr kDlsymAllocPoolSize = 1024;
69+
static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
70+
71+
static bool IsInDlsymAllocPool(const void *ptr) {
72+
uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
73+
return off < sizeof(alloc_memory_for_dlsym);
74+
}
75+
76+
static void *AllocateFromLocalPool(uptr size_in_bytes) {
77+
uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
78+
void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym];
79+
allocated_for_dlsym += size_in_words;
80+
CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
81+
return mem;
82+
}
83+
6784
#define ENSURE_MSAN_INITED() do { \
6885
CHECK(!msan_init_is_running); \
6986
if (!msan_inited) { \
@@ -227,14 +244,14 @@ INTERCEPTOR(void *, pvalloc, SIZE_T size) {
227244

228245
INTERCEPTOR(void, free, void *ptr) {
229246
GET_MALLOC_STACK_TRACE;
230-
if (!ptr) return;
247+
if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
231248
MsanDeallocate(&stack, ptr);
232249
}
233250

234251
#if !SANITIZER_FREEBSD
235252
INTERCEPTOR(void, cfree, void *ptr) {
236253
GET_MALLOC_STACK_TRACE;
237-
if (!ptr) return;
254+
if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
238255
MsanDeallocate(&stack, ptr);
239256
}
240257
#define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
@@ -907,27 +924,29 @@ INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
907924

908925
INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
909926
GET_MALLOC_STACK_TRACE;
910-
if (UNLIKELY(!msan_inited)) {
927+
if (UNLIKELY(!msan_inited))
911928
// Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
912-
const SIZE_T kCallocPoolSize = 1024;
913-
static uptr calloc_memory_for_dlsym[kCallocPoolSize];
914-
static SIZE_T allocated;
915-
SIZE_T size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
916-
void *mem = (void*)&calloc_memory_for_dlsym[allocated];
917-
allocated += size_in_words;
918-
CHECK(allocated < kCallocPoolSize);
919-
return mem;
920-
}
929+
return AllocateFromLocalPool(nmemb * size);
921930
return MsanCalloc(&stack, nmemb, size);
922931
}
923932

924933
INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
925934
GET_MALLOC_STACK_TRACE;
935+
if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
936+
uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
937+
uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
938+
void *new_ptr = AllocateFromLocalPool(size);
939+
internal_memcpy(new_ptr, ptr, copy_size);
940+
return new_ptr;
941+
}
926942
return MsanReallocate(&stack, ptr, size, sizeof(u64), false);
927943
}
928944

929945
INTERCEPTOR(void *, malloc, SIZE_T size) {
930946
GET_MALLOC_STACK_TRACE;
947+
if (UNLIKELY(!msan_inited))
948+
// Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
949+
return AllocateFromLocalPool(size);
931950
return MsanReallocate(&stack, nullptr, size, sizeof(u64), false);
932951
}
933952

0 commit comments

Comments
 (0)
Please sign in to comment.