ubsan_diag.cc revision 0ad23f7f860e27b8b9a889be665cfaea830503ce
1//===-- ubsan_diag.cc -----------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Diagnostic reporting for the UBSan runtime. 11// 12//===----------------------------------------------------------------------===// 13 14#include "ubsan_diag.h" 15#include "sanitizer_common/sanitizer_common.h" 16#include "sanitizer_common/sanitizer_libc.h" 17#include "sanitizer_common/sanitizer_stacktrace.h" 18#include "sanitizer_common/sanitizer_symbolizer.h" 19#include <stdio.h> 20 21using namespace __ubsan; 22 23Location __ubsan::getCallerLocation(uptr CallerLoc) { 24 if (!CallerLoc) 25 return Location(); 26 27 // Adjust to find the call instruction. 28 // FIXME: This is not portable. 29 --CallerLoc; 30 31 AddressInfo Info; 32 if (!SymbolizeCode(CallerLoc, &Info, 1) || !Info.module || !*Info.module) 33 return Location(CallerLoc); 34 35 if (!Info.function) 36 return ModuleLocation(Info.module, Info.module_offset); 37 38 return SourceLocation(Info.file, Info.line, Info.column); 39} 40 41Diag &Diag::operator<<(const TypeDescriptor &V) { 42 return AddArg(V.getTypeName()); 43} 44 45Diag &Diag::operator<<(const Value &V) { 46 if (V.getType().isSignedIntegerTy()) 47 AddArg(V.getSIntValue()); 48 else if (V.getType().isUnsignedIntegerTy()) 49 AddArg(V.getUIntValue()); 50 else if (V.getType().isFloatTy()) 51 AddArg(V.getFloatValue()); 52 else 53 AddArg("<unknown>"); 54 return *this; 55} 56 57/// Hexadecimal printing for numbers too large for Printf to handle directly. 58static void PrintHex(UIntMax Val) { 59#if HAVE_INT128_T 60 Printf("0x%08x%08x%08x%08x", 61 (unsigned int)(Val >> 96), 62 (unsigned int)(Val >> 64), 63 (unsigned int)(Val >> 32), 64 (unsigned int)(Val)); 65#else 66 UNREACHABLE("long long smaller than 64 bits?"); 67#endif 68} 69 70static void renderLocation(Location Loc) { 71 switch (Loc.getKind()) { 72 case Location::LK_Source: { 73 SourceLocation SLoc = Loc.getSourceLocation(); 74 if (SLoc.isInvalid()) 75 RawWrite("<unknown>:"); 76 else { 77 Printf("%s:%d:", SLoc.getFilename(), SLoc.getLine()); 78 if (SLoc.getColumn()) 79 Printf("%d:", SLoc.getColumn()); 80 } 81 break; 82 } 83 case Location::LK_Module: 84 Printf("%s:0x%zx:", Loc.getModuleLocation().getModuleName(), 85 Loc.getModuleLocation().getOffset()); 86 break; 87 case Location::LK_Memory: 88 Printf("0x%zx:", Loc.getMemoryLocation()); 89 break; 90 case Location::LK_Null: 91 RawWrite("<unknown>:"); 92 break; 93 } 94} 95 96// C++ demangling function, as required by Itanium C++ ABI. This is weak, 97// because we do not require a C++ ABI library to be linked to a program 98// using UBSan; if it's not present, we'll just print the string mangled. 99namespace __cxxabiv1 { 100 extern "C" char *__cxa_demangle(const char *mangled, char *buffer, 101 size_t *length, int *status) 102 __attribute__((weak)); 103} 104 105static void renderText(const char *Message, const Diag::Arg *Args) { 106 for (const char *Msg = Message; *Msg; ++Msg) { 107 if (*Msg != '%') { 108 char Buffer[64]; 109 unsigned I; 110 for (I = 0; Msg[I] && Msg[I] != '%' && I != 63; ++I) 111 Buffer[I] = Msg[I]; 112 Buffer[I] = '\0'; 113 RawWrite(Buffer); 114 Msg += I - 1; 115 } else { 116 const Diag::Arg &A = Args[*++Msg - '0']; 117 switch (A.Kind) { 118 case Diag::AK_String: 119 Printf("%s", A.String); 120 break; 121 case Diag::AK_Mangled: { 122 const char *String = 0; 123 // FIXME: __cxa_demangle aggressively insists on allocating memory. 124 // There's not much we can do about that, short of providing our 125 // own demangler (libc++abi's implementation could easily be made 126 // to not allocate). For now, we just call it anyway, and we leak 127 // the returned value. 128 if (__cxxabiv1::__cxa_demangle) 129 String = __cxxabiv1::__cxa_demangle(A.String, 0, 0, 0); 130 RawWrite("'"); 131 RawWrite(String ? String : A.String); 132 RawWrite("'"); 133 break; 134 } 135 case Diag::AK_SInt: 136 // 'long long' is guaranteed to be at least 64 bits wide. 137 if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX) 138 Printf("%lld", (long long)A.SInt); 139 else 140 PrintHex(A.SInt); 141 break; 142 case Diag::AK_UInt: 143 if (A.UInt <= UINT64_MAX) 144 Printf("%llu", (unsigned long long)A.UInt); 145 else 146 PrintHex(A.UInt); 147 break; 148 case Diag::AK_Float: { 149 // FIXME: Support floating-point formatting in sanitizer_common's 150 // printf, and stop using snprintf here. 151 char Buffer[32]; 152 snprintf(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float); 153 Printf("%s", Buffer); 154 break; 155 } 156 case Diag::AK_Pointer: 157 Printf("0x%zx", (uptr)A.Pointer); 158 break; 159 } 160 } 161 } 162} 163 164/// Find the earliest-starting range in Ranges which ends after Loc. 165static Range *upperBound(MemoryLocation Loc, Range *Ranges, 166 unsigned NumRanges) { 167 Range *Best = 0; 168 for (unsigned I = 0; I != NumRanges; ++I) 169 if (Ranges[I].getEnd().getMemoryLocation() > Loc && 170 (!Best || 171 Best->getStart().getMemoryLocation() > 172 Ranges[I].getStart().getMemoryLocation())) 173 Best = &Ranges[I]; 174 return Best; 175} 176 177/// Render a snippet of the address space near a location. 178static void renderMemorySnippet(MemoryLocation Loc, 179 Range *Ranges, unsigned NumRanges, 180 const Diag::Arg *Args) { 181 const unsigned BytesToShow = 32; 182 const unsigned MinBytesNearLoc = 4; 183 184 // Show at least the 8 bytes surrounding Loc. 185 MemoryLocation Min = Loc - MinBytesNearLoc, Max = Loc + MinBytesNearLoc; 186 for (unsigned I = 0; I < NumRanges; ++I) { 187 Min = __sanitizer::Min(Ranges[I].getStart().getMemoryLocation(), Min); 188 Max = __sanitizer::Max(Ranges[I].getEnd().getMemoryLocation(), Max); 189 } 190 191 // If we have too many interesting bytes, prefer to show bytes after Loc. 192 if (Max - Min > BytesToShow) 193 Min = __sanitizer::Min(Max - BytesToShow, Loc - MinBytesNearLoc); 194 Max = Min + BytesToShow; 195 196 // Emit data. 197 for (uptr P = Min; P != Max; ++P) { 198 // FIXME: Check that the address is readable before printing it. 199 unsigned char C = *reinterpret_cast<const unsigned char*>(P); 200 Printf("%s%02x", (P % 8 == 0) ? " " : " ", C); 201 } 202 RawWrite("\n"); 203 204 // Emit highlights. 205 Range *InRange = upperBound(Min, Ranges, NumRanges); 206 for (uptr P = Min; P != Max; ++P) { 207 char Pad = ' ', Byte = ' '; 208 if (InRange && InRange->getEnd().getMemoryLocation() == P) 209 InRange = upperBound(P, Ranges, NumRanges); 210 if (!InRange && P > Loc) 211 break; 212 if (InRange && InRange->getStart().getMemoryLocation() < P) 213 Pad = '~'; 214 if (InRange && InRange->getStart().getMemoryLocation() <= P) 215 Byte = '~'; 216 char Buffer[] = { Pad, Pad, P == Loc ? '^' : Byte, Byte, 0 }; 217 RawWrite((P % 8 == 0) ? Buffer : &Buffer[1]); 218 } 219 RawWrite("\n"); 220 221 // Go over the line again, and print names for the ranges. 222 InRange = 0; 223 unsigned Spaces = 0; 224 for (uptr P = Min; P != Max; ++P) { 225 if (!InRange || InRange->getEnd().getMemoryLocation() == P) 226 InRange = upperBound(P, Ranges, NumRanges); 227 if (!InRange) 228 break; 229 230 Spaces += (P % 8) == 0 ? 2 : 1; 231 232 if (InRange && InRange->getStart().getMemoryLocation() == P) { 233 while (Spaces--) 234 RawWrite(" "); 235 renderText(InRange->getText(), Args); 236 RawWrite("\n"); 237 // FIXME: We only support naming one range for now! 238 break; 239 } 240 241 Spaces += 2; 242 } 243 244 // FIXME: Print names for anything we can identify within the line: 245 // 246 // * If we can identify the memory itself as belonging to a particular 247 // global, stack variable, or dynamic allocation, then do so. 248 // 249 // * If we have a pointer-size, pointer-aligned range highlighted, 250 // determine whether the value of that range is a pointer to an 251 // entity which we can name, and if so, print that name. 252 // 253 // This needs an external symbolizer, or (preferably) ASan instrumentation. 254} 255 256Diag::~Diag() { 257 bool UseAnsiColor = PrintsToTty(); 258 if (UseAnsiColor) 259 RawWrite("\033[1m"); 260 261 renderLocation(Loc); 262 263 switch (Level) { 264 case DL_Error: 265 if (UseAnsiColor) 266 RawWrite("\033[31m"); 267 RawWrite(" runtime error: "); 268 if (UseAnsiColor) 269 RawWrite("\033[0;1m"); 270 break; 271 272 case DL_Note: 273 if (UseAnsiColor) 274 RawWrite("\033[30m"); 275 RawWrite(" note: "); 276 if (UseAnsiColor) 277 RawWrite("\033[0m"); 278 break; 279 } 280 281 renderText(Message, Args); 282 283 if (UseAnsiColor) 284 RawWrite("\033[0m"); 285 286 RawWrite("\n"); 287 288 if (Loc.isMemoryLocation()) 289 renderMemorySnippet(Loc.getMemoryLocation(), Ranges, NumRanges, Args); 290} 291