Skip to content

Commit a2cee05

Browse files
authored
[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.
1 parent 0fa373c commit a2cee05

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ inline void loadHeader(u32 Cookie, const void *Ptr,
125125
*NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader);
126126
if (UNLIKELY(NewUnpackedHeader->Checksum !=
127127
computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader)))
128-
reportHeaderCorruption(NewUnpackedHeader, const_cast<void *>(Ptr));
128+
reportHeaderCorruption(NewUnpackedHeader, Ptr);
129129
}
130130

131131
inline bool isValid(u32 Cookie, const void *Ptr,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ class Allocator {
775775

776776
// Getting the alloc size of a chunk only makes sense if it's allocated.
777777
if (UNLIKELY(Header.State != Chunk::State::Allocated))
778-
reportInvalidChunkState(AllocatorAction::Sizing, const_cast<void *>(Ptr));
778+
reportInvalidChunkState(AllocatorAction::Sizing, Ptr);
779779

780780
return getSize(Ptr, &Header);
781781
}

compiler-rt/lib/scudo/standalone/report.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,16 @@ void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {
6666

6767
// The checksum of a chunk header is invalid. This could be caused by an
6868
// {over,under}write of the header, a pointer that is not an actual chunk.
69-
void NORETURN reportHeaderCorruption(void *Header, void *Ptr) {
69+
void NORETURN reportHeaderCorruption(void *Header, const void *Ptr) {
7070
ScopedErrorReport Report;
7171
Report.append("corrupted chunk header at address %p", Ptr);
7272
if (*static_cast<Chunk::PackedHeader *>(Header) == 0U) {
7373
// Header all zero, which could indicate that this might be a pointer that
7474
// has been double freed but the memory has been released to the kernel.
7575
Report.append(": chunk header is zero and might indicate memory corruption "
76-
"or a double free\n",
77-
Ptr);
76+
"or a double free\n");
7877
} else {
79-
Report.append(": most likely due to memory corruption\n", Ptr);
78+
Report.append(": most likely due to memory corruption\n");
8079
}
8180
}
8281

@@ -131,21 +130,21 @@ static const char *stringifyAction(AllocatorAction Action) {
131130

132131
// The chunk is not in a state congruent with the operation we want to perform.
133132
// This is usually the case with a double-free, a realloc of a freed pointer.
134-
void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr) {
133+
void NORETURN reportInvalidChunkState(AllocatorAction Action, const void *Ptr) {
135134
ScopedErrorReport Report;
136135
Report.append("invalid chunk state when %s address %p\n",
137136
stringifyAction(Action), Ptr);
138137
}
139138

140-
void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) {
139+
void NORETURN reportMisalignedPointer(AllocatorAction Action, const void *Ptr) {
141140
ScopedErrorReport Report;
142141
Report.append("misaligned pointer when %s address %p\n",
143142
stringifyAction(Action), Ptr);
144143
}
145144

146145
// The deallocation function used is at odds with the one used to allocate the
147146
// chunk (eg: new[]/delete or malloc/delete, and so on).
148-
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
147+
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, const void *Ptr,
149148
u8 TypeA, u8 TypeB) {
150149
ScopedErrorReport Report;
151150
Report.append("allocation type mismatch when %s address %p (%d vs %d)\n",
@@ -154,7 +153,7 @@ void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
154153

155154
// The size specified to the delete operator does not match the one that was
156155
// passed to new when allocating the chunk.
157-
void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size,
156+
void NORETURN reportDeleteSizeMismatch(const void *Ptr, uptr Size,
158157
uptr ExpectedSize) {
159158
ScopedErrorReport Report;
160159
Report.append(

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void NORETURN reportRawError(const char *Message);
2424
void NORETURN reportInvalidFlag(const char *FlagType, const char *Value);
2525

2626
// Chunk header related errors.
27-
void NORETURN reportHeaderCorruption(void *Header, void *Ptr);
27+
void NORETURN reportHeaderCorruption(void *Header, const void *Ptr);
2828

2929
// Sanity checks related error.
3030
void NORETURN reportSanityCheckError(const char *Field);
@@ -41,11 +41,12 @@ enum class AllocatorAction : u8 {
4141
Reallocating,
4242
Sizing,
4343
};
44-
void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr);
45-
void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr);
46-
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
44+
void NORETURN reportInvalidChunkState(AllocatorAction Action, const void *Ptr);
45+
void NORETURN reportMisalignedPointer(AllocatorAction Action, const void *Ptr);
46+
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, const void *Ptr,
4747
u8 TypeA, u8 TypeB);
48-
void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size, uptr ExpectedSize);
48+
void NORETURN reportDeleteSizeMismatch(const void *Ptr, uptr Size,
49+
uptr ExpectedSize);
4950

5051
// C wrappers errors.
5152
void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment);

0 commit comments

Comments
 (0)