[scudo][standalone] NFC corrections

Summary:
A few corrections:
- rename `TransferBatch::MaxCached` to `getMaxCached` to conform with
  the style guide;
- move `getBlockBegin` from `Chunk::` to `Allocator::`: I believe it
  was a fallacy to have this be a `Chunk` method, as chunks'
  relationship to backend blocks are up to the frontend allocator. It
  makes more sense now, particularly with regard to the offset. Update
  the associated chunk test as the method isn't available there
  anymore;
- add a forgotten `\n` to a log string;
- for `releaseToOs`, instead of starting at `1`, start at `0` and
  `continue` on `BatchClassId`: in the end it's identical but doesn't
  assume a particular class id for batches;
- change a `CHECK` to a `reportOutOfMemory`: it's a clearer message

Reviewers: hctim, morehouse, eugenis, vitalybuka

Reviewed By: hctim

Subscribers: delcypher, #sanitizers, llvm-commits

Tags: #llvm, #sanitizers

Differential Revision: https://reviews.llvm.org/D64570

llvm-svn: 365816
This commit is contained in:
Kostya Kortchinsky 2019-07-11 19:55:53 +00:00
parent 5dca95bc4e
commit 8f18a4c980
7 changed files with 25 additions and 19 deletions

View File

@ -97,12 +97,6 @@ const AtomicPackedHeader *getConstAtomicHeader(const void *Ptr) {
reinterpret_cast<uptr>(Ptr) - getHeaderSize());
}
INLINE void *getBlockBegin(const void *Ptr, UnpackedHeader *Header) {
return reinterpret_cast<void *>(reinterpret_cast<uptr>(Ptr) -
getHeaderSize() -
(Header->Offset << SCUDO_MIN_ALIGNMENT_LOG));
}
// We do not need a cryptographically strong hash for the checksum, but a CRC
// type function that can alert us in the event a header is invalid or
// corrupted. Ideally slightly better than a simple xor of all fields.

View File

@ -45,7 +45,7 @@ public:
NewHeader.State = Chunk::State::Available;
Chunk::compareExchangeHeader(Allocator.Cookie, Ptr, &NewHeader, &Header);
void *BlockBegin = Chunk::getBlockBegin(Ptr, &Header);
void *BlockBegin = Allocator::getBlockBegin(Ptr, &NewHeader);
const uptr ClassId = Header.ClassId;
if (ClassId)
Cache.deallocate(ClassId, BlockBegin);
@ -482,12 +482,19 @@ private:
reportSanityCheckError("class ID");
}
static INLINE void *getBlockBegin(const void *Ptr,
Chunk::UnpackedHeader *Header) {
return reinterpret_cast<void *>(reinterpret_cast<uptr>(Ptr) -
Chunk::getHeaderSize() -
(Header->Offset << MinAlignmentLog));
}
// Return the size of a chunk as requested during its allocation.
INLINE uptr getSize(const void *Ptr, Chunk::UnpackedHeader *Header) {
const uptr SizeOrUnusedBytes = Header->SizeOrUnusedBytes;
if (Header->ClassId)
return SizeOrUnusedBytes;
return SecondaryT::getBlockEnd(Chunk::getBlockBegin(Ptr, Header)) -
return SecondaryT::getBlockEnd(getBlockBegin(Ptr, Header)) -
reinterpret_cast<uptr>(Ptr) - SizeOrUnusedBytes;
}
@ -505,7 +512,7 @@ private:
if (BypassQuarantine) {
NewHeader.State = Chunk::State::Available;
Chunk::compareExchangeHeader(Cookie, Ptr, &NewHeader, Header);
void *BlockBegin = Chunk::getBlockBegin(Ptr, Header);
void *BlockBegin = getBlockBegin(Ptr, &NewHeader);
const uptr ClassId = NewHeader.ClassId;
if (ClassId) {
bool UnlockRequired;

View File

@ -10,6 +10,7 @@
#define SCUDO_LOCAL_CACHE_H_
#include "internal_defs.h"
#include "report.h"
#include "stats.h"
namespace scudo {
@ -39,7 +40,7 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
DCHECK_LE(I, Count);
return Batch[I];
}
static u32 MaxCached(uptr Size) {
static u32 getMaxCached(uptr Size) {
return Min(MaxNumCached, SizeClassMap::getMaxCachedHint(Size));
}
TransferBatch *Next;
@ -140,7 +141,7 @@ private:
for (uptr I = 0; I < NumClasses; I++) {
PerClass *P = &PerClassArray[I];
const uptr Size = SizeClassAllocator::getSizeByClassId(I);
P->MaxCount = 2 * TransferBatch::MaxCached(Size);
P->MaxCount = 2 * TransferBatch::getMaxCached(Size);
P->ClassSize = Size;
}
}
@ -166,7 +167,9 @@ private:
const u32 Count = Min(C->MaxCount / 2, C->Count);
const uptr FirstIndexToDrain = C->Count - Count;
TransferBatch *B = createBatch(ClassId, C->Chunks[FirstIndexToDrain]);
CHECK(B);
if (UNLIKELY(!B))
reportOutOfMemory(
SizeClassAllocator::getSizeByClassId(SizeClassMap::BatchClassId));
B->setFromArray(&C->Chunks[FirstIndexToDrain], Count);
C->Count -= Count;
Allocator->pushBatch(ClassId, B);

View File

@ -162,7 +162,9 @@ public:
}
void releaseToOS() {
for (uptr I = 1; I < NumClasses; I++) {
for (uptr I = 0; I < NumClasses; I++) {
if (I == SizeClassMap::BatchClassId)
continue;
SizeClassInfo *Sci = getSizeClassInfo(I);
ScopedLock L(Sci->Mutex);
releaseToOSMaybe(Sci, I, /*Force=*/true);
@ -291,7 +293,7 @@ private:
return nullptr;
C->getStats().add(StatMapped, RegionSize);
const uptr Size = getSizeByClassId(ClassId);
const u32 MaxCount = TransferBatch::MaxCached(Size);
const u32 MaxCount = TransferBatch::getMaxCached(Size);
DCHECK_GT(MaxCount, 0);
const uptr NumberOfBlocks = RegionSize / Size;
DCHECK_GT(NumberOfBlocks, 0);

View File

@ -166,7 +166,9 @@ public:
}
void releaseToOS() {
for (uptr I = 1; I < NumClasses; I++) {
for (uptr I = 0; I < NumClasses; I++) {
if (I == SizeClassMap::BatchClassId)
continue;
RegionInfo *Region = getRegionInfo(I);
ScopedLock L(Region->Mutex);
releaseToOSMaybe(Region, I, /*Force=*/true);
@ -249,7 +251,7 @@ private:
NOINLINE TransferBatch *populateFreeList(CacheT *C, uptr ClassId,
RegionInfo *Region) {
const uptr Size = getSizeByClassId(ClassId);
const u32 MaxCount = TransferBatch::MaxCached(Size);
const u32 MaxCount = TransferBatch::getMaxCached(Size);
const uptr RegionBeg = Region->RegionBeg;
const uptr MappedUser = Region->MappedUser;

View File

@ -52,7 +52,7 @@ void NORETURN reportCheckFailed(const char *File, int Line,
// Generic string fatal error message.
void NORETURN reportError(const char *Message) {
ScopedErrorReport Report;
Report.append("%s", Message);
Report.append("%s\n", Message);
}
void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {

View File

@ -30,7 +30,6 @@ TEST(ScudoChunkTest, ChunkBasic) {
HeaderSize);
scudo::Chunk::storeHeader(Cookie, P, &Header);
memset(P, 'A', Size);
EXPECT_EQ(scudo::Chunk::getBlockBegin(P, &Header), Block);
scudo::Chunk::loadHeader(Cookie, P, &Header);
EXPECT_TRUE(scudo::Chunk::isValid(Cookie, P, &Header));
EXPECT_FALSE(scudo::Chunk::isValid(InvalidCookie, P, &Header));
@ -70,7 +69,6 @@ TEST(ScudoChunkTest, CorruptHeader) {
HeaderSize);
scudo::Chunk::storeHeader(Cookie, P, &Header);
memset(P, 'A', Size);
EXPECT_EQ(scudo::Chunk::getBlockBegin(P, &Header), Block);
scudo::Chunk::loadHeader(Cookie, P, &Header);
// Simulate a couple of corrupted bits per byte of header data.
for (scudo::uptr I = 0; I < sizeof(scudo::Chunk::PackedHeader); I++) {