ubsan_handlers.cc revision 80af605afd0e92a2a128c81898f647207f384e08
1//===-- ubsan_handlers.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// Error logging entry points for the UBSan runtime. 11// 12//===----------------------------------------------------------------------===// 13 14#include "ubsan_handlers.h" 15#include "ubsan_diag.h" 16 17#include "sanitizer_common/sanitizer_common.h" 18 19using namespace __sanitizer; 20using namespace __ubsan; 21 22namespace __ubsan { 23 const char *TypeCheckKinds[] = { 24 "load of", "store to", "reference binding to", "member access within", 25 "member call on", "constructor call on" 26 }; 27} 28 29void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data, 30 ValueHandle Pointer) { 31 if (!Pointer) 32 Diag(Data->Loc, "%0 null pointer of type %1") 33 << TypeCheckKinds[Data->TypeCheckKind] << Data->Type; 34 else if (Data->Alignment && (Pointer & (Data->Alignment - 1))) 35 Diag(Data->Loc, "%0 misaligned address %1 for type %3, " 36 "which requires %2 byte alignment") 37 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer 38 << Data->Alignment << Data->Type; 39 else 40 Diag(Data->Loc, "%0 address %1 with insufficient space " 41 "for an object of type %2") 42 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type; 43 Die(); 44} 45 46/// \brief Common diagnostic emission for various forms of integer overflow. 47template<typename T> static void HandleIntegerOverflow(OverflowData *Data, 48 ValueHandle LHS, 49 const char *Operator, 50 T RHS) { 51 Diag(Data->Loc, "%0 integer overflow: " 52 "%1 %2 %3 cannot be represented in type %4") 53 << (Data->Type.isSignedIntegerTy() ? "signed" : "unsigned") 54 << Value(Data->Type, LHS) << Operator << RHS << Data->Type; 55 Die(); 56} 57 58void __ubsan::__ubsan_handle_add_overflow(OverflowData *Data, 59 ValueHandle LHS, ValueHandle RHS) { 60 HandleIntegerOverflow(Data, LHS, "+", Value(Data->Type, RHS)); 61} 62 63void __ubsan::__ubsan_handle_sub_overflow(OverflowData *Data, 64 ValueHandle LHS, ValueHandle RHS) { 65 HandleIntegerOverflow(Data, LHS, "-", Value(Data->Type, RHS)); 66} 67 68void __ubsan::__ubsan_handle_mul_overflow(OverflowData *Data, 69 ValueHandle LHS, ValueHandle RHS) { 70 HandleIntegerOverflow(Data, LHS, "*", Value(Data->Type, RHS)); 71} 72 73void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data, 74 ValueHandle OldVal) { 75 Diag(Data->Loc, "negation of %0 cannot be represented in type %1; " 76 "cast to an unsigned type to negate this value to itself") 77 << Value(Data->Type, OldVal) << Data->Type; 78 Die(); 79} 80 81void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data, 82 ValueHandle LHS, ValueHandle RHS) { 83 Value LHSVal(Data->Type, LHS); 84 Value RHSVal(Data->Type, RHS); 85 if (RHSVal.isMinusOne()) 86 Diag(Data->Loc, "division of %0 by -1 cannot be represented in type %1") 87 << LHSVal << Data->Type; 88 else 89 Diag(Data->Loc, "division by zero"); 90 Die(); 91} 92 93void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data, 94 ValueHandle LHS, 95 ValueHandle RHS) { 96 Value LHSVal(Data->LHSType, LHS); 97 Value RHSVal(Data->RHSType, RHS); 98 if (RHSVal.isNegative()) 99 Diag(Data->Loc, "shift exponent %0 is negative") << RHSVal; 100 else if (RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth()) 101 Diag(Data->Loc, "shift exponent %0 is too large for %1-bit type %2") 102 << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType; 103 else if (LHSVal.isNegative()) 104 Diag(Data->Loc, "left shift of negative value %0") << LHSVal; 105 else 106 Diag(Data->Loc, "left shift of %0 by %1 places cannot be represented " 107 "in type %2") << LHSVal << RHSVal << Data->LHSType; 108 Die(); 109} 110 111void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) { 112 Diag(Data->Loc, "execution reached a __builtin_unreachable() call"); 113 Die(); 114} 115 116void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) { 117 Diag(Data->Loc, "execution reached the end of a value-returning function " 118 "without returning a value"); 119 Die(); 120} 121 122void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data, 123 ValueHandle Bound) { 124 Diag(Data->Loc, "variable length array bound evaluates to " 125 "non-positive value %0") 126 << Value(Data->Type, Bound); 127 Die(); 128} 129 130void __ubsan::__ubsan_handle_float_cast_overflow(FloatCastOverflowData *Data, 131 ValueHandle From) { 132 Diag(SourceLocation(), "value %0 is outside the range of representable " 133 "values of type %2") 134 << Value(Data->FromType, From) << Data->FromType << Data->ToType; 135 Die(); 136} 137