#include "report.h"
#include "atomic_helpers.h"
#include "string_utils.h"
#include <stdarg.h>
namespace scudo {
class ScopedErrorReport {
public:
ScopedErrorReport() : Message() { Message.append("Scudo ERROR: "); }
void append(const char *Format, ...) {
va_list Args;
va_start(Args, Format);
Message.append(Format, Args);
va_end(Args);
}
NORETURN ~ScopedErrorReport() {
outputRaw(Message.data());
setAbortMessage(Message.data());
die();
}
private:
ScopedString Message;
};
inline void NORETURN trap() { __builtin_trap(); }
void NORETURN reportCheckFailed(const char *File, int Line,
const char *Condition, u64 Value1, u64 Value2) {
static atomic_u32 NumberOfCalls;
if (atomic_fetch_add(&NumberOfCalls, 1, memory_order_relaxed) > 2) {
trap();
}
ScopedErrorReport Report;
Report.append("CHECK failed @ %s:%d %s ((u64)op1=%llu, (u64)op2=%llu)\n",
File, Line, Condition, Value1, Value2);
}
void NORETURN reportError(const char *Message) {
ScopedErrorReport Report;
Report.append("%s\n", Message);
}
void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {
ScopedErrorReport Report;
Report.append("invalid value for %s option: '%s'\n", FlagType, Value);
}
void NORETURN reportHeaderCorruption(void *Ptr) {
ScopedErrorReport Report;
Report.append("corrupted chunk header at address %p\n", Ptr);
}
void NORETURN reportHeaderRace(void *Ptr) {
ScopedErrorReport Report;
Report.append("race on chunk header at address %p\n", Ptr);
}
void NORETURN reportSanityCheckError(const char *Field) {
ScopedErrorReport Report;
Report.append("maximum possible %s doesn't fit in header\n", Field);
}
void NORETURN reportAlignmentTooBig(uptr Alignment, uptr MaxAlignment) {
ScopedErrorReport Report;
Report.append("invalid allocation alignment: %zu exceeds maximum supported "
"alignment of %zu\n",
Alignment, MaxAlignment);
}
void NORETURN reportAllocationSizeTooBig(uptr UserSize, uptr TotalSize,
uptr MaxSize) {
ScopedErrorReport Report;
Report.append("requested allocation size %zu (%zu after adjustments) exceeds "
"maximum supported size of %zu\n",
UserSize, TotalSize, MaxSize);
}
void NORETURN reportOutOfMemory(uptr RequestedSize) {
ScopedErrorReport Report;
Report.append("out of memory trying to allocate %zu bytes\n", RequestedSize);
}
static const char *stringifyAction(AllocatorAction Action) {
switch (Action) {
case AllocatorAction::Recycling:
return "recycling";
case AllocatorAction::Deallocating:
return "deallocating";
case AllocatorAction::Reallocating:
return "reallocating";
case AllocatorAction::Sizing:
return "sizing";
}
return "<invalid action>";
}
void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr) {
ScopedErrorReport Report;
Report.append("invalid chunk state when %s address %p\n",
stringifyAction(Action), Ptr);
}
void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) {
ScopedErrorReport Report;
Report.append("misaligned pointer when %s address %p\n",
stringifyAction(Action), Ptr);
}
void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
u8 TypeA, u8 TypeB) {
ScopedErrorReport Report;
Report.append("allocation type mismatch when %s address %p (%d vs %d)\n",
stringifyAction(Action), Ptr, TypeA, TypeB);
}
void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size,
uptr ExpectedSize) {
ScopedErrorReport Report;
Report.append(
"invalid sized delete when deallocating address %p (%zu vs %zu)\n", Ptr,
Size, ExpectedSize);
}
void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment) {
ScopedErrorReport Report;
Report.append(
"invalid allocation alignment: %zu, alignment must be a power of two\n",
Alignment);
}
void NORETURN reportCallocOverflow(uptr Count, uptr Size) {
ScopedErrorReport Report;
Report.append("calloc parameters overflow: count * size (%zu * %zu) cannot "
"be represented with type size_t\n",
Count, Size);
}
void NORETURN reportInvalidPosixMemalignAlignment(uptr Alignment) {
ScopedErrorReport Report;
Report.append(
"invalid alignment requested in posix_memalign: %zu, alignment must be a "
"power of two and a multiple of sizeof(void *) == %zu\n",
Alignment, sizeof(void *));
}
void NORETURN reportPvallocOverflow(uptr Size) {
ScopedErrorReport Report;
Report.append("pvalloc parameters overflow: size %zu rounded up to system "
"page size %zu cannot be represented in type size_t\n",
Size, getPageSizeCached());
}
void NORETURN reportInvalidAlignedAllocAlignment(uptr Alignment, uptr Size) {
ScopedErrorReport Report;
Report.append("invalid alignment requested in aligned_alloc: %zu, alignment "
"must be a power of two and the requested size %zu must be a "
"multiple of alignment\n",
Alignment, Size);
}
}