Skip to content

Commit 8531fa3

Browse files
committedJun 13, 2017
[ASan] Move rss_limit_is_exceeded_ flag to ASan.
Summary: Move the OOM decision based on RSS limits out of generic allocator to ASan allocator, where it makes more sense at the moment. Reviewers: eugenis Subscribers: kubamracek, llvm-commits Differential Revision: https://reviews.llvm.org/D34180 llvm-svn: 305342
1 parent e86fddd commit 8531fa3

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed
 

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ struct Allocator {
235235
AllocatorCache fallback_allocator_cache;
236236
QuarantineCache fallback_quarantine_cache;
237237

238+
atomic_uint8_t rss_limit_exceeded;
239+
238240
// ------------------- Options --------------------------
239241
atomic_uint16_t min_redzone;
240242
atomic_uint16_t max_redzone;
@@ -268,6 +270,14 @@ struct Allocator {
268270
SharedInitCode(options);
269271
}
270272

273+
bool RssLimitExceeded() {
274+
return atomic_load(&rss_limit_exceeded, memory_order_relaxed);
275+
}
276+
277+
void SetRssLimitExceeded(bool limit_exceeded) {
278+
atomic_store(&rss_limit_exceeded, limit_exceeded, memory_order_relaxed);
279+
}
280+
271281
void RePoisonChunk(uptr chunk) {
272282
// This could be a user-facing chunk (with redzones), or some internal
273283
// housekeeping chunk, like TransferBatch. Start by assuming the former.
@@ -363,6 +373,8 @@ struct Allocator {
363373
AllocType alloc_type, bool can_fill) {
364374
if (UNLIKELY(!asan_inited))
365375
AsanInitFromRtl();
376+
if (RssLimitExceeded())
377+
return allocator.ReturnNullOrDieOnOOM();
366378
Flags &fl = *flags();
367379
CHECK(stack);
368380
const uptr min_alignment = SHADOW_GRANULARITY;
@@ -400,16 +412,15 @@ struct Allocator {
400412

401413
AsanThread *t = GetCurrentThread();
402414
void *allocated;
403-
bool check_rss_limit = true;
404415
if (t) {
405416
AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
406417
allocated =
407-
allocator.Allocate(cache, needed_size, 8, false, check_rss_limit);
418+
allocator.Allocate(cache, needed_size, 8, false);
408419
} else {
409420
SpinMutexLock l(&fallback_mutex);
410421
AllocatorCache *cache = &fallback_allocator_cache;
411422
allocated =
412-
allocator.Allocate(cache, needed_size, 8, false, check_rss_limit);
423+
allocator.Allocate(cache, needed_size, 8, false);
413424
}
414425

415426
if (!allocated) return allocator.ReturnNullOrDieOnOOM();
@@ -866,8 +877,8 @@ void asan_mz_force_unlock() {
866877
instance.ForceUnlock();
867878
}
868879

869-
void AsanSoftRssLimitExceededCallback(bool exceeded) {
870-
instance.allocator.SetRssLimitIsExceeded(exceeded);
880+
void AsanSoftRssLimitExceededCallback(bool limit_exceeded) {
881+
instance.SetRssLimitExceeded(limit_exceeded);
871882
}
872883

873884
} // namespace __asan

‎compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h

+5-14
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ class CombinedAllocator {
4343
}
4444

4545
void *Allocate(AllocatorCache *cache, uptr size, uptr alignment,
46-
bool cleared = false, bool check_rss_limit = false) {
46+
bool cleared = false) {
4747
// Returning 0 on malloc(0) may break a lot of code.
4848
if (size == 0)
4949
size = 1;
50-
if (size + alignment < size) return ReturnNullOrDieOnBadRequest();
51-
if (check_rss_limit && RssLimitIsExceeded()) return ReturnNullOrDieOnOOM();
50+
if (size + alignment < size)
51+
return ReturnNullOrDieOnBadRequest();
5252
uptr original_size = size;
5353
// If alignment requirements are to be fulfilled by the frontend allocator
5454
// rather than by the primary or secondary, passing an alignment lower than
@@ -89,7 +89,8 @@ class CombinedAllocator {
8989
}
9090

9191
void *ReturnNullOrDieOnOOM() {
92-
if (MayReturnNull()) return nullptr;
92+
if (MayReturnNull())
93+
return nullptr;
9394
ReportAllocatorCannotReturnNull(true);
9495
}
9596

@@ -106,15 +107,6 @@ class CombinedAllocator {
106107
primary_.SetReleaseToOSIntervalMs(release_to_os_interval_ms);
107108
}
108109

109-
bool RssLimitIsExceeded() {
110-
return atomic_load(&rss_limit_is_exceeded_, memory_order_acquire);
111-
}
112-
113-
void SetRssLimitIsExceeded(bool rss_limit_is_exceeded) {
114-
atomic_store(&rss_limit_is_exceeded_, rss_limit_is_exceeded,
115-
memory_order_release);
116-
}
117-
118110
void Deallocate(AllocatorCache *cache, void *p) {
119111
if (!p) return;
120112
if (primary_.PointerIsMine(p))
@@ -228,6 +220,5 @@ class CombinedAllocator {
228220
SecondaryAllocator secondary_;
229221
AllocatorGlobalStats stats_;
230222
atomic_uint8_t may_return_null_;
231-
atomic_uint8_t rss_limit_is_exceeded_;
232223
};
233224

0 commit comments

Comments
 (0)
Please sign in to comment.