Skip to content

Commit 5a780ee

Browse files
committedJun 8, 2018
[asan, myriad] Use local pool for new/delete when ASan run-time is not up
This can happen on Myriad RTEMS so needs to be handled. Differential Revision: https://reviews.llvm.org/D47916 llvm-svn: 334329
1 parent e53fa05 commit 5a780ee

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed
 

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "asan_allocator.h"
2525
#include "asan_interceptors.h"
2626
#include "asan_internal.h"
27+
#include "asan_malloc_local.h"
2728
#include "asan_stack.h"
2829

2930
// ---------------------- Replacement functions ---------------- {{{1
@@ -67,12 +68,19 @@ static int PosixMemalignFromLocalPool(void **memptr, uptr alignment,
6768
return 0;
6869
}
6970

70-
// On RTEMS, we use the local pool to handle memory allocation before
71-
// the ASan run-time has been initialized.
72-
static INLINE bool EarlyMalloc() {
73-
return SANITIZER_RTEMS && (!asan_inited || asan_init_is_running);
71+
#if SANITIZER_RTEMS
72+
void* MemalignFromLocalPool(uptr alignment, uptr size) {
73+
void *ptr = nullptr;
74+
alignment = Max(alignment, kWordSize);
75+
PosixMemalignFromLocalPool(&ptr, alignment, size);
76+
return ptr;
7477
}
7578

79+
bool IsFromLocalPool(const void *ptr) {
80+
return IsInDlsymAllocPool(ptr);
81+
}
82+
#endif
83+
7684
static INLINE bool MaybeInDlsym() {
7785
// Fuchsia doesn't use dlsym-based interceptors.
7886
return !SANITIZER_FUCHSIA && asan_init_is_running;
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- asan_malloc_local.h -------------------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file is a part of AddressSanitizer, an address sanity checker.
11+
//
12+
// Provide interfaces to check for and handle local pool memory allocation.
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef ASAN_MALLOC_LOCAL_H
16+
#define ASAN_MALLOC_LOCAL_H
17+
18+
#include "sanitizer_common/sanitizer_platform.h"
19+
#include "asan_internal.h"
20+
21+
// On RTEMS, we use the local pool to handle memory allocation when the ASan
22+
// run-time is not up.
23+
static INLINE bool EarlyMalloc() {
24+
return SANITIZER_RTEMS && (!__asan::asan_inited ||
25+
__asan::asan_init_is_running);
26+
}
27+
28+
void* MemalignFromLocalPool(uptr alignment, uptr size);
29+
30+
#if SANITIZER_RTEMS
31+
32+
bool IsFromLocalPool(const void *ptr);
33+
34+
#define ALLOCATE_FROM_LOCAL_POOL UNLIKELY(EarlyMalloc())
35+
#define IS_FROM_LOCAL_POOL(ptr) UNLIKELY(IsFromLocalPool(ptr))
36+
37+
#else // SANITIZER_RTEMS
38+
39+
#define ALLOCATE_FROM_LOCAL_POOL 0
40+
#define IS_FROM_LOCAL_POOL(ptr) 0
41+
42+
#endif // SANITIZER_RTEMS
43+
44+
#endif // ASAN_MALLOC_LOCAL_H

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

+17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "asan_allocator.h"
1616
#include "asan_internal.h"
17+
#include "asan_malloc_local.h"
1718
#include "asan_report.h"
1819
#include "asan_stack.h"
1920

@@ -69,12 +70,24 @@ enum class align_val_t: size_t {};
6970
} // namespace std
7071

7172
// TODO(alekseyshl): throw std::bad_alloc instead of dying on OOM.
73+
// For local pool allocation, align to SHADOW_GRANULARITY to match asan
74+
// allocator behavior.
7275
#define OPERATOR_NEW_BODY(type, nothrow) \
76+
if (ALLOCATE_FROM_LOCAL_POOL) {\
77+
void *res = MemalignFromLocalPool(SHADOW_GRANULARITY, size);\
78+
if (!nothrow) CHECK(res);\
79+
return res;\
80+
}\
7381
GET_STACK_TRACE_MALLOC;\
7482
void *res = asan_memalign(0, size, &stack, type);\
7583
if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
7684
return res;
7785
#define OPERATOR_NEW_BODY_ALIGN(type, nothrow) \
86+
if (ALLOCATE_FROM_LOCAL_POOL) {\
87+
void *res = MemalignFromLocalPool((uptr)align, size);\
88+
if (!nothrow) CHECK(res);\
89+
return res;\
90+
}\
7891
GET_STACK_TRACE_MALLOC;\
7992
void *res = asan_memalign((uptr)align, size, &stack, type);\
8093
if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
@@ -129,18 +142,22 @@ INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
129142
#endif // !SANITIZER_MAC
130143

131144
#define OPERATOR_DELETE_BODY(type) \
145+
if (IS_FROM_LOCAL_POOL(ptr)) return;\
132146
GET_STACK_TRACE_FREE;\
133147
asan_delete(ptr, 0, 0, &stack, type);
134148

135149
#define OPERATOR_DELETE_BODY_SIZE(type) \
150+
if (IS_FROM_LOCAL_POOL(ptr)) return;\
136151
GET_STACK_TRACE_FREE;\
137152
asan_delete(ptr, size, 0, &stack, type);
138153

139154
#define OPERATOR_DELETE_BODY_ALIGN(type) \
155+
if (IS_FROM_LOCAL_POOL(ptr)) return;\
140156
GET_STACK_TRACE_FREE;\
141157
asan_delete(ptr, 0, static_cast<uptr>(align), &stack, type);
142158

143159
#define OPERATOR_DELETE_BODY_SIZE_ALIGN(type) \
160+
if (IS_FROM_LOCAL_POOL(ptr)) return;\
144161
GET_STACK_TRACE_FREE;\
145162
asan_delete(ptr, size, static_cast<uptr>(align), &stack, type);
146163

0 commit comments

Comments
 (0)
Please sign in to comment.