From e2aaad14ca1be229a1ea189bccb3166dd35f4a52 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Fri, 13 Jun 2025 15:18:19 -0700 Subject: [PATCH 1/2] [scudo] Make report pointers const. 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. --- compiler-rt/lib/scudo/standalone/chunk.h | 2 +- compiler-rt/lib/scudo/standalone/combined.h | 2 +- compiler-rt/lib/scudo/standalone/report.cpp | 15 +++++++-------- compiler-rt/lib/scudo/standalone/report.h | 10 +++++----- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/chunk.h b/compiler-rt/lib/scudo/standalone/chunk.h index a1b8e723d4cb5..9da2dc57e71a1 100644 --- a/compiler-rt/lib/scudo/standalone/chunk.h +++ b/compiler-rt/lib/scudo/standalone/chunk.h @@ -125,7 +125,7 @@ inline void loadHeader(u32 Cookie, const void *Ptr, *NewUnpackedHeader = bit_cast(NewPackedHeader); if (UNLIKELY(NewUnpackedHeader->Checksum != computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader))) - reportHeaderCorruption(NewUnpackedHeader, const_cast(Ptr)); + reportHeaderCorruption(NewUnpackedHeader, Ptr); } inline bool isValid(u32 Cookie, const void *Ptr, diff --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h index 43655642843cb..87acdec2a3bac 100644 --- a/compiler-rt/lib/scudo/standalone/combined.h +++ b/compiler-rt/lib/scudo/standalone/combined.h @@ -775,7 +775,7 @@ class Allocator { // 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(Ptr)); + reportInvalidChunkState(AllocatorAction::Sizing, Ptr); return getSize(Ptr, &Header); } diff --git a/compiler-rt/lib/scudo/standalone/report.cpp b/compiler-rt/lib/scudo/standalone/report.cpp index 14a4066d37200..b97a74b078c2f 100644 --- a/compiler-rt/lib/scudo/standalone/report.cpp +++ b/compiler-rt/lib/scudo/standalone/report.cpp @@ -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(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( diff --git a/compiler-rt/lib/scudo/standalone/report.h b/compiler-rt/lib/scudo/standalone/report.h index c0214b51560e9..ef42f2063ef93 100644 --- a/compiler-rt/lib/scudo/standalone/report.h +++ b/compiler-rt/lib/scudo/standalone/report.h @@ -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,11 @@ 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); From e0863c4c2f0bf68bc425af02ef62ddb8ed7dc043 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Tue, 17 Jun 2025 18:00:37 -0700 Subject: [PATCH 2/2] Run clang-format. --- compiler-rt/lib/scudo/standalone/report.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/scudo/standalone/report.h b/compiler-rt/lib/scudo/standalone/report.h index ef42f2063ef93..c397dd3fc9c65 100644 --- a/compiler-rt/lib/scudo/standalone/report.h +++ b/compiler-rt/lib/scudo/standalone/report.h @@ -45,7 +45,8 @@ 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(const void *Ptr, uptr Size, uptr ExpectedSize); +void NORETURN reportDeleteSizeMismatch(const void *Ptr, uptr Size, + uptr ExpectedSize); // C wrappers errors. void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment);