[scudo] Make report pointers const. (#144624)

Mark as many of the reportXX functions that take pointers const. This
avoid the need to use const_cast when calling these functions on an
already const pointer.

Fix reportHeaderCorruption calls where an argument was passed into an
append call that didn't use them.
This commit is contained in:
Christopher Ferris
2025-06-18 09:12:53 -07:00
committed by GitHub
parent 0fa373c77d
commit a2cee05449
4 changed files with 15 additions and 15 deletions

View File

@@ -125,7 +125,7 @@ inline void loadHeader(u32 Cookie, const void *Ptr,
*NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader);
if (UNLIKELY(NewUnpackedHeader->Checksum !=
computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader)))
reportHeaderCorruption(NewUnpackedHeader, const_cast<void *>(Ptr));
reportHeaderCorruption(NewUnpackedHeader, Ptr);
}
inline bool isValid(u32 Cookie, const void *Ptr,

View File

@@ -775,7 +775,7 @@ public:
// Getting the alloc size of a chunk only makes sense if it's allocated.
if (UNLIKELY(Header.State != Chunk::State::Allocated))
reportInvalidChunkState(AllocatorAction::Sizing, const_cast<void *>(Ptr));
reportInvalidChunkState(AllocatorAction::Sizing, Ptr);
return getSize(Ptr, &Header);
}

View File

@@ -66,17 +66,16 @@ void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {
// The checksum of a chunk header is invalid. This could be caused by an
// {over,under}write of the header, a pointer that is not an actual chunk.
void NORETURN reportHeaderCorruption(void *Header, void *Ptr) {
void NORETURN reportHeaderCorruption(void *Header, const void *Ptr) {
ScopedErrorReport Report;
Report.append("corrupted chunk header at address %p", Ptr);
if (*static_cast<Chunk::PackedHeader *>(Header) == 0U) {
// Header all zero, which could indicate that this might be a pointer that
// has been double freed but the memory has been released to the kernel.
Report.append(": chunk header is zero and might indicate memory corruption "
"or a double free\n",
Ptr);
"or a double free\n");
} else {
Report.append(": most likely due to memory corruption\n", Ptr);
Report.append(": most likely due to memory corruption\n");
}
}
@@ -131,13 +130,13 @@ static const char *stringifyAction(AllocatorAction Action) {
// The chunk is not in a state congruent with the operation we want to perform.
// This is usually the case with a double-free, a realloc of a freed pointer.
void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr) {
void NORETURN reportInvalidChunkState(AllocatorAction Action, const void *Ptr) {
ScopedErrorReport Report;
Report.append("invalid chunk state when %s address %p\n",
stringifyAction(Action), Ptr);
}
void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) {
void NORETURN reportMisalignedPointer(AllocatorAction Action, const void *Ptr) {
ScopedErrorReport Report;
Report.append("misaligned pointer when %s address %p\n",
stringifyAction(Action), Ptr);
@@ -145,7 +144,7 @@ void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) {
// The deallocation function used is at odds with the one used to allocate the
// chunk (eg: new[]/delete or malloc/delete, and so on).
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, const void *Ptr,
u8 TypeA, u8 TypeB) {
ScopedErrorReport Report;
Report.append("allocation type mismatch when %s address %p (%d vs %d)\n",
@@ -154,7 +153,7 @@ void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
// The size specified to the delete operator does not match the one that was
// passed to new when allocating the chunk.
void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size,
void NORETURN reportDeleteSizeMismatch(const void *Ptr, uptr Size,
uptr ExpectedSize) {
ScopedErrorReport Report;
Report.append(

View File

@@ -24,7 +24,7 @@ void NORETURN reportRawError(const char *Message);
void NORETURN reportInvalidFlag(const char *FlagType, const char *Value);
// Chunk header related errors.
void NORETURN reportHeaderCorruption(void *Header, void *Ptr);
void NORETURN reportHeaderCorruption(void *Header, const void *Ptr);
// Sanity checks related error.
void NORETURN reportSanityCheckError(const char *Field);
@@ -41,11 +41,12 @@ enum class AllocatorAction : u8 {
Reallocating,
Sizing,
};
void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr);
void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr);
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
void NORETURN reportInvalidChunkState(AllocatorAction Action, const void *Ptr);
void NORETURN reportMisalignedPointer(AllocatorAction Action, const void *Ptr);
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, const void *Ptr,
u8 TypeA, u8 TypeB);
void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size, uptr ExpectedSize);
void NORETURN reportDeleteSizeMismatch(const void *Ptr, uptr Size,
uptr ExpectedSize);
// C wrappers errors.
void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment);