Skip to content

Commit a0b9f1f

Browse files
FernandoChiaHungDuan
authored andcommitted
[scudo] set/check invalid cache entries
made checking for invalid cache entries and setting invalid cache entries more implicit and clear. Reviewed By: cferris Differential Revision: https://reviews.llvm.org/D155983
1 parent 4c6b8bb commit a0b9f1f

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

compiler-rt/lib/scudo/standalone/secondary.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ struct CachedBlock {
7979
uptr BlockBegin = 0;
8080
MemMapT MemMap = {};
8181
u64 Time = 0;
82+
83+
bool isValid() { return CommitBase != 0; }
84+
85+
void invalidate() { CommitBase = 0; }
8286
};
8387
} // namespace
8488

@@ -152,7 +156,7 @@ template <typename Config> class MapAllocatorCache {
152156
EntriesCount, atomic_load_relaxed(&MaxEntriesCount),
153157
atomic_load_relaxed(&MaxEntrySize));
154158
for (CachedBlock Entry : Entries) {
155-
if (!Entry.CommitBase)
159+
if (!Entry.isValid())
156160
continue;
157161
Str->append("StartBlockAddress: 0x%zx, EndBlockAddress: 0x%zx, "
158162
"BlockSize: %zu\n",
@@ -219,7 +223,7 @@ template <typename Config> class MapAllocatorCache {
219223
if (CacheConfig::QuarantineSize && useMemoryTagging<Config>(Options)) {
220224
QuarantinePos =
221225
(QuarantinePos + 1) % Max(CacheConfig::QuarantineSize, 1u);
222-
if (!Quarantine[QuarantinePos].CommitBase) {
226+
if (!Quarantine[QuarantinePos].isValid()) {
223227
Quarantine[QuarantinePos] = Entry;
224228
return;
225229
}
@@ -234,7 +238,7 @@ template <typename Config> class MapAllocatorCache {
234238
EmptyCache = true;
235239
} else {
236240
for (u32 I = 0; I < MaxCount; I++) {
237-
if (Entries[I].CommitBase)
241+
if (Entries[I].isValid())
238242
continue;
239243
if (I != 0)
240244
Entries[I] = Entries[0];
@@ -263,13 +267,13 @@ template <typename Config> class MapAllocatorCache {
263267
if (EntriesCount == 0)
264268
return false;
265269
for (u32 I = 0; I < MaxCount; I++) {
266-
if (!Entries[I].CommitBase)
270+
if (!Entries[I].isValid())
267271
continue;
268272
if (Size > Entries[I].CommitSize)
269273
continue;
270274
Found = true;
271275
Entry = Entries[I];
272-
Entries[I].CommitBase = 0;
276+
Entries[I].invalidate();
273277
EntriesCount--;
274278
break;
275279
}
@@ -310,15 +314,15 @@ template <typename Config> class MapAllocatorCache {
310314
void disableMemoryTagging() EXCLUDES(Mutex) {
311315
ScopedLock L(Mutex);
312316
for (u32 I = 0; I != CacheConfig::QuarantineSize; ++I) {
313-
if (Quarantine[I].CommitBase) {
317+
if (Quarantine[I].isValid()) {
314318
MemMapT &MemMap = Quarantine[I].MemMap;
315319
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
316-
Quarantine[I].CommitBase = 0;
320+
Quarantine[I].invalidate();
317321
}
318322
}
319323
const u32 MaxCount = atomic_load_relaxed(&MaxEntriesCount);
320324
for (u32 I = 0; I < MaxCount; I++) {
321-
if (Entries[I].CommitBase) {
325+
if (Entries[I].isValid()) {
322326
Entries[I].MemMap.setMemoryPermission(Entries[I].CommitBase,
323327
Entries[I].CommitSize, 0);
324328
}
@@ -339,10 +343,10 @@ template <typename Config> class MapAllocatorCache {
339343
{
340344
ScopedLock L(Mutex);
341345
for (uptr I = 0; I < CacheConfig::EntriesArraySize; I++) {
342-
if (!Entries[I].CommitBase)
346+
if (!Entries[I].isValid())
343347
continue;
344348
MapInfo[N] = Entries[I].MemMap;
345-
Entries[I].CommitBase = 0;
349+
Entries[I].invalidate();
346350
N++;
347351
}
348352
EntriesCount = 0;
@@ -355,7 +359,7 @@ template <typename Config> class MapAllocatorCache {
355359
}
356360

357361
void releaseIfOlderThan(CachedBlock &Entry, u64 Time) REQUIRES(Mutex) {
358-
if (!Entry.CommitBase || !Entry.Time)
362+
if (!Entry.isValid() || !Entry.Time)
359363
return;
360364
if (Entry.Time > Time) {
361365
if (OldestTime == 0 || Entry.Time < OldestTime)

0 commit comments

Comments
 (0)