Skip to content

Commit 735bcc9

Browse files
committed
[NFC][asan] Add QuarantineCallback::{PreQuarantine,RecyclePassThrough}
Reviewed By: thurston Differential Revision: https://reviews.llvm.org/D153496
1 parent 5017344 commit 735bcc9

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "asan_allocator.h"
1818

19+
#include "asan_internal.h"
1920
#include "asan_mapping.h"
2021
#include "asan_poisoning.h"
2122
#include "asan_report.h"
@@ -24,6 +25,7 @@
2425
#include "lsan/lsan_common.h"
2526
#include "sanitizer_common/sanitizer_allocator_checks.h"
2627
#include "sanitizer_common/sanitizer_allocator_interface.h"
28+
#include "sanitizer_common/sanitizer_common.h"
2729
#include "sanitizer_common/sanitizer_errno.h"
2830
#include "sanitizer_common/sanitizer_flags.h"
2931
#include "sanitizer_common/sanitizer_internal_defs.h"
@@ -196,6 +198,24 @@ struct QuarantineCallback {
196198
stack_(stack) {
197199
}
198200

201+
void PreQuarantine(AsanChunk *m) {
202+
Flags &fl = *flags();
203+
204+
if (fl.max_free_fill_size > 0) {
205+
// We have to skip the chunk header, it contains free_context_id.
206+
uptr scribble_start = (uptr)m + kChunkHeaderSize + kChunkHeader2Size;
207+
if (m->UsedSize() >= kChunkHeader2Size) { // Skip Header2 in user area.
208+
uptr size_to_fill = m->UsedSize() - kChunkHeader2Size;
209+
size_to_fill = Min(size_to_fill, (uptr)fl.max_free_fill_size);
210+
REAL(memset)((void *)scribble_start, fl.free_fill_byte, size_to_fill);
211+
}
212+
}
213+
214+
// Poison the region.
215+
PoisonShadow(m->Beg(), RoundUpTo(m->UsedSize(), ASAN_SHADOW_GRANULARITY),
216+
kAsanHeapFreeMagic);
217+
}
218+
199219
void Recycle(AsanChunk *m) {
200220
void *p = get_allocator().GetBlockBegin(m);
201221
if (p != m) {
@@ -221,6 +241,12 @@ struct QuarantineCallback {
221241
get_allocator().Deallocate(cache_, p);
222242
}
223243

244+
void RecyclePassThrough(AsanChunk *m) {
245+
// TODO: We don't need all these here.
246+
PreQuarantine(m);
247+
Recycle(m);
248+
}
249+
224250
void *Allocate(uptr size) {
225251
void *res = get_allocator().Allocate(cache_, size, 1);
226252
// TODO(alekseys): Consider making quarantine OOM-friendly.
@@ -639,21 +665,6 @@ struct Allocator {
639665
AsanThread *t = GetCurrentThread();
640666
m->SetFreeContext(t ? t->tid() : 0, StackDepotPut(*stack));
641667

642-
Flags &fl = *flags();
643-
if (fl.max_free_fill_size > 0) {
644-
// We have to skip the chunk header, it contains free_context_id.
645-
uptr scribble_start = (uptr)m + kChunkHeaderSize + kChunkHeader2Size;
646-
if (m->UsedSize() >= kChunkHeader2Size) { // Skip Header2 in user area.
647-
uptr size_to_fill = m->UsedSize() - kChunkHeader2Size;
648-
size_to_fill = Min(size_to_fill, (uptr)fl.max_free_fill_size);
649-
REAL(memset)((void *)scribble_start, fl.free_fill_byte, size_to_fill);
650-
}
651-
}
652-
653-
// Poison the region.
654-
PoisonShadow(m->Beg(), RoundUpTo(m->UsedSize(), ASAN_SHADOW_GRANULARITY),
655-
kAsanHeapFreeMagic);
656-
657668
// Push into quarantine.
658669
if (t) {
659670
AsanThreadLocalMallocStorage *ms = &t->malloc_storage();

compiler-rt/lib/sanitizer_common/sanitizer_quarantine.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ struct QuarantineBatch {
6868

6969
COMPILER_CHECK(sizeof(QuarantineBatch) <= (1 << 13)); // 8Kb.
7070

71-
// The callback interface is:
72-
// void Callback::Recycle(Node *ptr);
73-
// void *cb.Allocate(uptr size);
74-
// void cb.Deallocate(void *ptr);
7571
template<typename Callback, typename Node>
7672
class Quarantine {
7773
public:
@@ -100,10 +96,11 @@ class Quarantine {
10096
void Put(Cache *c, Callback cb, Node *ptr, uptr size) {
10197
uptr max_cache_size = GetMaxCacheSize();
10298
if (max_cache_size && size <= GetMaxSize()) {
99+
cb.PreQuarantine(ptr);
103100
c->Enqueue(cb, ptr, size);
104101
} else {
105102
// GetMaxCacheSize() == 0 only when GetMaxSize() == 0 (see Init).
106-
cb.Recycle(ptr);
103+
cb.RecyclePassThrough(ptr);
107104
}
108105
// Check cache size anyway to accommodate for runtime cache_size change.
109106
if (c->Size() > max_cache_size)

0 commit comments

Comments
 (0)