ubsan_handlers.cc revision 5f1164955fb28a9bcb826abc195aa2119feb0f97
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 29static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer, 30 Location FallbackLoc) { 31 Location Loc = Data->Loc; 32 if (Data->Loc.isInvalid()) 33 Loc = FallbackLoc; 34 35 if (!Pointer) 36 Diag(Loc, "%0 null pointer of type %1") 37 << TypeCheckKinds[Data->TypeCheckKind] << Data->Type; 38 else if (Data->Alignment && (Pointer & (Data->Alignment - 1))) 39 Diag(Loc, "%0 misaligned address %1 for type %3, " 40 "which requires %2 byte alignment") 41 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer 42 << Data->Alignment << Data->Type; 43 else 44 Diag(Loc, "%0 address %1 with insufficient space " 45 "for an object of type %2") 46 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type; 47} 48void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data, 49 ValueHandle Pointer) { 50 handleTypeMismatchImpl(Data, Pointer, getCallerLocation()); 51} 52void __ubsan::__ubsan_handle_type_mismatch_abort(TypeMismatchData *Data, 53 ValueHandle Pointer) { 54 handleTypeMismatchImpl(Data, Pointer, getCallerLocation()); 55 Die(); 56} 57 58/// \brief Common diagnostic emission for various forms of integer overflow. 59template<typename T> static void HandleIntegerOverflow(OverflowData *Data, 60 ValueHandle LHS, 61 const char *Operator, 62 T RHS) { 63 Diag(Data->Loc, "%0 integer overflow: " 64 "%1 %2 %3 cannot be represented in type %4") 65 << (Data->Type.isSignedIntegerTy() ? "signed" : "unsigned") 66 << Value(Data->Type, LHS) << Operator << RHS << Data->Type; 67} 68 69void __ubsan::__ubsan_handle_add_overflow(OverflowData *Data, 70 ValueHandle LHS, ValueHandle RHS) { 71 HandleIntegerOverflow(Data, LHS, "+", Value(Data->Type, RHS)); 72} 73void __ubsan::__ubsan_handle_add_overflow_abort(OverflowData *Data, 74 ValueHandle LHS, 75 ValueHandle RHS) { 76 __ubsan_handle_add_overflow(Data, LHS, RHS); 77 Die(); 78} 79 80void __ubsan::__ubsan_handle_sub_overflow(OverflowData *Data, 81 ValueHandle LHS, ValueHandle RHS) { 82 HandleIntegerOverflow(Data, LHS, "-", Value(Data->Type, RHS)); 83} 84void __ubsan::__ubsan_handle_sub_overflow_abort(OverflowData *Data, 85 ValueHandle LHS, 86 ValueHandle RHS) { 87 __ubsan_handle_sub_overflow(Data, LHS, RHS); 88 Die(); 89} 90 91void __ubsan::__ubsan_handle_mul_overflow(OverflowData *Data, 92 ValueHandle LHS, ValueHandle RHS) { 93 HandleIntegerOverflow(Data, LHS, "*", Value(Data->Type, RHS)); 94} 95void __ubsan::__ubsan_handle_mul_overflow_abort(OverflowData *Data, 96 ValueHandle LHS, 97 ValueHandle RHS) { 98 __ubsan_handle_mul_overflow(Data, LHS, RHS); 99 Die(); 100} 101 102void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data, 103 ValueHandle OldVal) { 104 Diag(Data->Loc, "negation of %0 cannot be represented in type %1; " 105 "cast to an unsigned type to negate this value to itself") 106 << Value(Data->Type, OldVal) << Data->Type; 107} 108void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data, 109 ValueHandle OldVal) { 110 __ubsan_handle_negate_overflow(Data, OldVal); 111 Die(); 112} 113 114void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data, 115 ValueHandle LHS, ValueHandle RHS) { 116 Value LHSVal(Data->Type, LHS); 117 Value RHSVal(Data->Type, RHS); 118 if (RHSVal.isMinusOne()) 119 Diag(Data->Loc, "division of %0 by -1 cannot be represented in type %1") 120 << LHSVal << Data->Type; 121 else 122 Diag(Data->Loc, "division by zero"); 123} 124void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data, 125 ValueHandle LHS, 126 ValueHandle RHS) { 127 __ubsan_handle_divrem_overflow(Data, LHS, RHS); 128 Die(); 129} 130 131void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data, 132 ValueHandle LHS, 133 ValueHandle RHS) { 134 Value LHSVal(Data->LHSType, LHS); 135 Value RHSVal(Data->RHSType, RHS); 136 if (RHSVal.isNegative()) 137 Diag(Data->Loc, "shift exponent %0 is negative") << RHSVal; 138 else if (RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth()) 139 Diag(Data->Loc, "shift exponent %0 is too large for %1-bit type %2") 140 << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType; 141 else if (LHSVal.isNegative()) 142 Diag(Data->Loc, "left shift of negative value %0") << LHSVal; 143 else 144 Diag(Data->Loc, "left shift of %0 by %1 places cannot be represented " 145 "in type %2") << LHSVal << RHSVal << Data->LHSType; 146} 147void __ubsan::__ubsan_handle_shift_out_of_bounds_abort( 148 ShiftOutOfBoundsData *Data, 149 ValueHandle LHS, 150 ValueHandle RHS) { 151 __ubsan_handle_shift_out_of_bounds(Data, LHS, RHS); 152 Die(); 153} 154 155void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) { 156 Diag(Data->Loc, "execution reached a __builtin_unreachable() call"); 157 Die(); 158} 159 160void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) { 161 Diag(Data->Loc, "execution reached the end of a value-returning function " 162 "without returning a value"); 163 Die(); 164} 165 166void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data, 167 ValueHandle Bound) { 168 Diag(Data->Loc, "variable length array bound evaluates to " 169 "non-positive value %0") 170 << Value(Data->Type, Bound); 171} 172void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data, 173 ValueHandle Bound) { 174 __ubsan_handle_vla_bound_not_positive(Data, Bound); 175 Die(); 176} 177 178 179void __ubsan::__ubsan_handle_float_cast_overflow(FloatCastOverflowData *Data, 180 ValueHandle From) { 181 Diag(getCallerLocation(), "value %0 is outside the range of representable " 182 "values of type %2") 183 << Value(Data->FromType, From) << Data->FromType << Data->ToType; 184} 185void __ubsan::__ubsan_handle_float_cast_overflow_abort( 186 FloatCastOverflowData *Data, 187 ValueHandle From) { 188 Diag(getCallerLocation(), "value %0 is outside the range of representable " 189 "values of type %2") 190 << Value(Data->FromType, From) << Data->FromType << Data->ToType; 191 Die(); 192} 193 194void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data, 195 ValueHandle Val) { 196 Diag(getCallerLocation(), "load of value %0, which is not a valid value for " 197 "type %1") 198 << Value(Data->Type, Val) << Data->Type; 199} 200void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data, 201 ValueHandle Val) { 202 Diag(getCallerLocation(), "load of value %0, which is not a valid value for " 203 "type %1") 204 << Value(Data->Type, Val) << Data->Type; 205 Die(); 206} 207