2012-10-10 03:34:32 +08:00
|
|
|
//===-- ubsan_diag.cc -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Diagnostic reporting for the UBSan runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ubsan_diag.h"
|
2012-11-14 07:42:05 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2012-10-10 03:34:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace __ubsan;
|
|
|
|
|
|
|
|
Diag &Diag::operator<<(const TypeDescriptor &V) {
|
|
|
|
return AddArg(V.getTypeName());
|
|
|
|
}
|
|
|
|
|
|
|
|
Diag &Diag::operator<<(const Value &V) {
|
|
|
|
if (V.getType().isSignedIntegerTy())
|
|
|
|
AddArg(V.getSIntValue());
|
|
|
|
else if (V.getType().isUnsignedIntegerTy())
|
|
|
|
AddArg(V.getUIntValue());
|
2012-10-13 06:57:15 +08:00
|
|
|
else if (V.getType().isFloatTy())
|
|
|
|
AddArg(V.getFloatValue());
|
2012-10-10 03:34:32 +08:00
|
|
|
else
|
|
|
|
AddArg("<unknown>");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hexadecimal printing for numbers too large for fprintf to handle directly.
|
|
|
|
static void PrintHex(UIntMax Val) {
|
2012-10-13 10:30:10 +08:00
|
|
|
#if HAVE_INT128_T
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("0x%08x%08x%08x%08x",
|
2012-10-10 03:34:32 +08:00
|
|
|
(unsigned int)(Val >> 96),
|
|
|
|
(unsigned int)(Val >> 64),
|
|
|
|
(unsigned int)(Val >> 32),
|
|
|
|
(unsigned int)(Val));
|
|
|
|
#else
|
|
|
|
UNREACHABLE("long long smaller than 64 bits?");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Diag::~Diag() {
|
2012-11-14 07:42:05 +08:00
|
|
|
bool UseAnsiColor = PrintsToTty();
|
2012-10-10 03:34:32 +08:00
|
|
|
if (UseAnsiColor)
|
2012-11-14 07:42:05 +08:00
|
|
|
RawWrite("\033[1m");
|
2012-10-10 03:34:32 +08:00
|
|
|
if (Loc.isInvalid())
|
2012-11-14 07:42:05 +08:00
|
|
|
RawWrite("<unknown>:");
|
2012-10-10 03:34:32 +08:00
|
|
|
else {
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("%s:%d:", Loc.getFilename(), Loc.getLine());
|
2012-10-10 03:34:32 +08:00
|
|
|
if (Loc.getColumn())
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("%d:", Loc.getColumn());
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
if (UseAnsiColor)
|
2012-11-14 07:42:05 +08:00
|
|
|
RawWrite("\033[31m");
|
2012-12-03 02:43:33 +08:00
|
|
|
RawWrite(" runtime error: ");
|
2012-10-10 03:34:32 +08:00
|
|
|
if (UseAnsiColor)
|
2012-11-14 07:42:05 +08:00
|
|
|
RawWrite("\033[0;1m");
|
2012-10-10 03:34:32 +08:00
|
|
|
for (const char *Msg = Message; *Msg; ++Msg) {
|
2012-11-14 07:42:05 +08:00
|
|
|
if (*Msg != '%') {
|
|
|
|
char Buffer[64];
|
|
|
|
unsigned I;
|
|
|
|
for (I = 0; Msg[I] && Msg[I] != '%' && I != 63; ++I)
|
|
|
|
Buffer[I] = Msg[I];
|
|
|
|
Buffer[I] = '\0';
|
|
|
|
RawWrite(Buffer);
|
|
|
|
Msg += I - 1;
|
|
|
|
} else {
|
2012-10-10 03:34:32 +08:00
|
|
|
const Arg &A = Args[*++Msg - '0'];
|
|
|
|
switch (A.Kind) {
|
|
|
|
case AK_String:
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("%s", A.String);
|
2012-10-10 03:34:32 +08:00
|
|
|
break;
|
|
|
|
case AK_SInt:
|
|
|
|
// 'long long' is guaranteed to be at least 64 bits wide.
|
|
|
|
if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX)
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("%lld", (long long)A.SInt);
|
2012-10-10 03:34:32 +08:00
|
|
|
else
|
|
|
|
PrintHex(A.SInt);
|
|
|
|
break;
|
|
|
|
case AK_UInt:
|
|
|
|
if (A.UInt <= UINT64_MAX)
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("%llu", (unsigned long long)A.UInt);
|
2012-10-10 03:34:32 +08:00
|
|
|
else
|
|
|
|
PrintHex(A.UInt);
|
|
|
|
break;
|
2012-11-14 07:42:05 +08:00
|
|
|
case AK_Float: {
|
|
|
|
// FIXME: Support floating-point formatting in sanitizer_common's
|
|
|
|
// printf, and stop using snprintf here.
|
|
|
|
char Buffer[32];
|
|
|
|
snprintf(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float);
|
|
|
|
Printf("%s", Buffer);
|
2012-10-13 06:57:15 +08:00
|
|
|
break;
|
2012-11-14 07:42:05 +08:00
|
|
|
}
|
2012-10-10 03:34:32 +08:00
|
|
|
case AK_Pointer:
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("0x%zx", (uptr)A.Pointer);
|
2012-10-10 03:34:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 07:42:05 +08:00
|
|
|
RawWrite("\n");
|
2012-10-10 03:34:32 +08:00
|
|
|
if (UseAnsiColor)
|
2012-11-14 07:42:05 +08:00
|
|
|
Printf("\033[0m");
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|