1 //===-- ubsan_handlers.cc -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Error logging entry points for the UBSan runtime.
12 //===----------------------------------------------------------------------===//
14 #include "ubsan_platform.h"
16 #include "ubsan_handlers.h"
17 #include "ubsan_diag.h"
19 #include "sanitizer_common/sanitizer_common.h"
21 using namespace __sanitizer;
22 using namespace __ubsan;
25 bool ignoreReport(SourceLocation SLoc, ReportOptions Opts, ErrorType ET) {
26 // We are not allowed to skip error report: if we are in unrecoverable
27 // handler, we have to terminate the program right now, and therefore
28 // have to print some diagnostic.
30 // Even if source location is disabled, it doesn't mean that we have
31 // already report an error to the user: some concurrently running
32 // thread could have acquired it, but not yet printed the report.
33 if (Opts.FromUnrecoverableHandler)
35 return SLoc.isDisabled() || IsPCSuppressed(ET, Opts.pc, SLoc.getFilename());
38 const char *TypeCheckKinds[] = {
39 "load of", "store to", "reference binding to", "member access within",
40 "member call on", "constructor call on", "downcast of", "downcast of",
41 "upcast of", "cast to virtual base of"};
44 static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
46 Location Loc = Data->Loc.acquire();
50 ET = ErrorType::NullPointerUse;
51 else if (Data->Alignment && (Pointer & (Data->Alignment - 1)))
52 ET = ErrorType::MisalignedPointerUse;
54 ET = ErrorType::InsufficientObjectSize;
56 // Use the SourceLocation from Data to track deduplication, even if it's
58 if (ignoreReport(Loc.getSourceLocation(), Opts, ET))
61 SymbolizedStackHolder FallbackLoc;
62 if (Data->Loc.isInvalid()) {
63 FallbackLoc.reset(getCallerLocation(Opts.pc));
67 ScopedReport R(Opts, Loc, ET);
70 case ErrorType::NullPointerUse:
71 Diag(Loc, DL_Error, "%0 null pointer of type %1")
72 << TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
74 case ErrorType::MisalignedPointerUse:
75 Diag(Loc, DL_Error, "%0 misaligned address %1 for type %3, "
76 "which requires %2 byte alignment")
77 << TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer
78 << Data->Alignment << Data->Type;
80 case ErrorType::InsufficientObjectSize:
81 Diag(Loc, DL_Error, "%0 address %1 with insufficient space "
82 "for an object of type %2")
83 << TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer << Data->Type;
86 UNREACHABLE("unexpected error type!");
90 Diag(Pointer, DL_Note, "pointer points here");
93 void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data,
94 ValueHandle Pointer) {
95 GET_REPORT_OPTIONS(false);
96 handleTypeMismatchImpl(Data, Pointer, Opts);
98 void __ubsan::__ubsan_handle_type_mismatch_abort(TypeMismatchData *Data,
99 ValueHandle Pointer) {
100 GET_REPORT_OPTIONS(true);
101 handleTypeMismatchImpl(Data, Pointer, Opts);
105 /// \brief Common diagnostic emission for various forms of integer overflow.
106 template <typename T>
107 static void handleIntegerOverflowImpl(OverflowData *Data, ValueHandle LHS,
108 const char *Operator, T RHS,
109 ReportOptions Opts) {
110 SourceLocation Loc = Data->Loc.acquire();
111 bool IsSigned = Data->Type.isSignedIntegerTy();
112 ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
113 : ErrorType::UnsignedIntegerOverflow;
115 if (ignoreReport(Loc, Opts, ET))
118 ScopedReport R(Opts, Loc, ET);
120 Diag(Loc, DL_Error, "%0 integer overflow: "
121 "%1 %2 %3 cannot be represented in type %4")
122 << (IsSigned ? "signed" : "unsigned")
123 << Value(Data->Type, LHS) << Operator << RHS << Data->Type;
126 #define UBSAN_OVERFLOW_HANDLER(handler_name, op, unrecoverable) \
127 void __ubsan::handler_name(OverflowData *Data, ValueHandle LHS, \
129 GET_REPORT_OPTIONS(unrecoverable); \
130 handleIntegerOverflowImpl(Data, LHS, op, Value(Data->Type, RHS), Opts); \
135 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow, "+", false)
136 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow_abort, "+", true)
137 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow, "-", false)
138 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow_abort, "-", true)
139 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow, "*", false)
140 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow_abort, "*", true)
142 static void handleNegateOverflowImpl(OverflowData *Data, ValueHandle OldVal,
143 ReportOptions Opts) {
144 SourceLocation Loc = Data->Loc.acquire();
145 bool IsSigned = Data->Type.isSignedIntegerTy();
146 ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
147 : ErrorType::UnsignedIntegerOverflow;
149 if (ignoreReport(Loc, Opts, ET))
152 ScopedReport R(Opts, Loc, ET);
156 "negation of %0 cannot be represented in type %1; "
157 "cast to an unsigned type to negate this value to itself")
158 << Value(Data->Type, OldVal) << Data->Type;
160 Diag(Loc, DL_Error, "negation of %0 cannot be represented in type %1")
161 << Value(Data->Type, OldVal) << Data->Type;
164 void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data,
165 ValueHandle OldVal) {
166 GET_REPORT_OPTIONS(false);
167 handleNegateOverflowImpl(Data, OldVal, Opts);
169 void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data,
170 ValueHandle OldVal) {
171 GET_REPORT_OPTIONS(true);
172 handleNegateOverflowImpl(Data, OldVal, Opts);
176 static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS,
177 ValueHandle RHS, ReportOptions Opts) {
178 SourceLocation Loc = Data->Loc.acquire();
179 Value LHSVal(Data->Type, LHS);
180 Value RHSVal(Data->Type, RHS);
183 if (RHSVal.isMinusOne())
184 ET = ErrorType::SignedIntegerOverflow;
185 else if (Data->Type.isIntegerTy())
186 ET = ErrorType::IntegerDivideByZero;
188 ET = ErrorType::FloatDivideByZero;
190 if (ignoreReport(Loc, Opts, ET))
193 ScopedReport R(Opts, Loc, ET);
196 case ErrorType::SignedIntegerOverflow:
197 Diag(Loc, DL_Error, "division of %0 by -1 cannot be represented in type %1")
198 << LHSVal << Data->Type;
201 Diag(Loc, DL_Error, "division by zero");
206 void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data,
207 ValueHandle LHS, ValueHandle RHS) {
208 GET_REPORT_OPTIONS(false);
209 handleDivremOverflowImpl(Data, LHS, RHS, Opts);
211 void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data,
214 GET_REPORT_OPTIONS(true);
215 handleDivremOverflowImpl(Data, LHS, RHS, Opts);
219 static void handleShiftOutOfBoundsImpl(ShiftOutOfBoundsData *Data,
220 ValueHandle LHS, ValueHandle RHS,
221 ReportOptions Opts) {
222 SourceLocation Loc = Data->Loc.acquire();
223 Value LHSVal(Data->LHSType, LHS);
224 Value RHSVal(Data->RHSType, RHS);
227 if (RHSVal.isNegative() ||
228 RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth())
229 ET = ErrorType::InvalidShiftExponent;
231 ET = ErrorType::InvalidShiftBase;
233 if (ignoreReport(Loc, Opts, ET))
236 ScopedReport R(Opts, Loc, ET);
238 if (ET == ErrorType::InvalidShiftExponent) {
239 if (RHSVal.isNegative())
240 Diag(Loc, DL_Error, "shift exponent %0 is negative") << RHSVal;
242 Diag(Loc, DL_Error, "shift exponent %0 is too large for %1-bit type %2")
243 << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType;
245 if (LHSVal.isNegative())
246 Diag(Loc, DL_Error, "left shift of negative value %0") << LHSVal;
249 "left shift of %0 by %1 places cannot be represented in type %2")
250 << LHSVal << RHSVal << Data->LHSType;
254 void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data,
257 GET_REPORT_OPTIONS(false);
258 handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
260 void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
261 ShiftOutOfBoundsData *Data,
264 GET_REPORT_OPTIONS(true);
265 handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
269 static void handleOutOfBoundsImpl(OutOfBoundsData *Data, ValueHandle Index,
270 ReportOptions Opts) {
271 SourceLocation Loc = Data->Loc.acquire();
272 ErrorType ET = ErrorType::OutOfBoundsIndex;
274 if (ignoreReport(Loc, Opts, ET))
277 ScopedReport R(Opts, Loc, ET);
279 Value IndexVal(Data->IndexType, Index);
280 Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
281 << IndexVal << Data->ArrayType;
284 void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
286 GET_REPORT_OPTIONS(false);
287 handleOutOfBoundsImpl(Data, Index, Opts);
289 void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
291 GET_REPORT_OPTIONS(true);
292 handleOutOfBoundsImpl(Data, Index, Opts);
296 static void handleBuiltinUnreachableImpl(UnreachableData *Data,
297 ReportOptions Opts) {
298 ScopedReport R(Opts, Data->Loc, ErrorType::UnreachableCall);
299 Diag(Data->Loc, DL_Error, "execution reached a __builtin_unreachable() call");
302 void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
303 GET_REPORT_OPTIONS(true);
304 handleBuiltinUnreachableImpl(Data, Opts);
308 static void handleMissingReturnImpl(UnreachableData *Data, ReportOptions Opts) {
309 ScopedReport R(Opts, Data->Loc, ErrorType::MissingReturn);
310 Diag(Data->Loc, DL_Error,
311 "execution reached the end of a value-returning function "
312 "without returning a value");
315 void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) {
316 GET_REPORT_OPTIONS(true);
317 handleMissingReturnImpl(Data, Opts);
321 static void handleVLABoundNotPositive(VLABoundData *Data, ValueHandle Bound,
322 ReportOptions Opts) {
323 SourceLocation Loc = Data->Loc.acquire();
324 ErrorType ET = ErrorType::NonPositiveVLAIndex;
326 if (ignoreReport(Loc, Opts, ET))
329 ScopedReport R(Opts, Loc, ET);
331 Diag(Loc, DL_Error, "variable length array bound evaluates to "
332 "non-positive value %0")
333 << Value(Data->Type, Bound);
336 void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data,
338 GET_REPORT_OPTIONS(false);
339 handleVLABoundNotPositive(Data, Bound, Opts);
341 void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data,
343 GET_REPORT_OPTIONS(true);
344 handleVLABoundNotPositive(Data, Bound, Opts);
348 static bool looksLikeFloatCastOverflowDataV1(void *Data) {
349 // First field is either a pointer to filename or a pointer to a
351 u8 *FilenameOrTypeDescriptor;
352 internal_memcpy(&FilenameOrTypeDescriptor, Data,
353 sizeof(FilenameOrTypeDescriptor));
355 // Heuristic: For float_cast_overflow, the TypeKind will be either TK_Integer
356 // (0x0), TK_Float (0x1) or TK_Unknown (0xff). If both types are known,
357 // adding both bytes will be 0 or 1 (for BE or LE). If it were a filename,
358 // adding two printable characters will not yield such a value. Otherwise,
359 // if one of them is 0xff, this is most likely TK_Unknown type descriptor.
360 u16 MaybeFromTypeKind =
361 FilenameOrTypeDescriptor[0] + FilenameOrTypeDescriptor[1];
362 return MaybeFromTypeKind < 2 || FilenameOrTypeDescriptor[0] == 0xff ||
363 FilenameOrTypeDescriptor[1] == 0xff;
366 static void handleFloatCastOverflow(void *DataPtr, ValueHandle From,
367 ReportOptions Opts) {
368 SymbolizedStackHolder CallerLoc;
370 const TypeDescriptor *FromType, *ToType;
371 ErrorType ET = ErrorType::FloatCastOverflow;
373 if (looksLikeFloatCastOverflowDataV1(DataPtr)) {
374 auto Data = reinterpret_cast<FloatCastOverflowData *>(DataPtr);
375 CallerLoc.reset(getCallerLocation(Opts.pc));
377 FromType = &Data->FromType;
378 ToType = &Data->ToType;
380 auto Data = reinterpret_cast<FloatCastOverflowDataV2 *>(DataPtr);
381 SourceLocation SLoc = Data->Loc.acquire();
382 if (ignoreReport(SLoc, Opts, ET))
385 FromType = &Data->FromType;
386 ToType = &Data->ToType;
389 ScopedReport R(Opts, Loc, ET);
392 "value %0 is outside the range of representable values of type %2")
393 << Value(*FromType, From) << *FromType << *ToType;
396 void __ubsan::__ubsan_handle_float_cast_overflow(void *Data, ValueHandle From) {
397 GET_REPORT_OPTIONS(false);
398 handleFloatCastOverflow(Data, From, Opts);
400 void __ubsan::__ubsan_handle_float_cast_overflow_abort(void *Data,
402 GET_REPORT_OPTIONS(true);
403 handleFloatCastOverflow(Data, From, Opts);
407 static void handleLoadInvalidValue(InvalidValueData *Data, ValueHandle Val,
408 ReportOptions Opts) {
409 SourceLocation Loc = Data->Loc.acquire();
410 // This check could be more precise if we used different handlers for
411 // -fsanitize=bool and -fsanitize=enum.
412 bool IsBool = (0 == internal_strcmp(Data->Type.getTypeName(), "'bool'"));
414 IsBool ? ErrorType::InvalidBoolLoad : ErrorType::InvalidEnumLoad;
416 if (ignoreReport(Loc, Opts, ET))
419 ScopedReport R(Opts, Loc, ET);
422 "load of value %0, which is not a valid value for type %1")
423 << Value(Data->Type, Val) << Data->Type;
426 void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data,
428 GET_REPORT_OPTIONS(false);
429 handleLoadInvalidValue(Data, Val, Opts);
431 void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data,
433 GET_REPORT_OPTIONS(true);
434 handleLoadInvalidValue(Data, Val, Opts);
438 static void handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
439 ValueHandle Function,
440 ReportOptions Opts) {
441 SourceLocation CallLoc = Data->Loc.acquire();
442 ErrorType ET = ErrorType::FunctionTypeMismatch;
444 if (ignoreReport(CallLoc, Opts, ET))
447 ScopedReport R(Opts, CallLoc, ET);
449 SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
450 const char *FName = FLoc.get()->info.function;
454 Diag(CallLoc, DL_Error,
455 "call to function %0 through pointer to incorrect function type %1")
456 << FName << Data->Type;
457 Diag(FLoc, DL_Note, "%0 defined here") << FName;
461 __ubsan::__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,
462 ValueHandle Function) {
463 GET_REPORT_OPTIONS(false);
464 handleFunctionTypeMismatch(Data, Function, Opts);
467 void __ubsan::__ubsan_handle_function_type_mismatch_abort(
468 FunctionTypeMismatchData *Data, ValueHandle Function) {
469 GET_REPORT_OPTIONS(true);
470 handleFunctionTypeMismatch(Data, Function, Opts);
474 static void handleNonNullReturn(NonNullReturnData *Data, ReportOptions Opts) {
475 SourceLocation Loc = Data->Loc.acquire();
476 ErrorType ET = ErrorType::InvalidNullReturn;
478 if (ignoreReport(Loc, Opts, ET))
481 ScopedReport R(Opts, Loc, ET);
483 Diag(Loc, DL_Error, "null pointer returned from function declared to never "
485 if (!Data->AttrLoc.isInvalid())
486 Diag(Data->AttrLoc, DL_Note, "returns_nonnull attribute specified here");
489 void __ubsan::__ubsan_handle_nonnull_return(NonNullReturnData *Data) {
490 GET_REPORT_OPTIONS(false);
491 handleNonNullReturn(Data, Opts);
494 void __ubsan::__ubsan_handle_nonnull_return_abort(NonNullReturnData *Data) {
495 GET_REPORT_OPTIONS(true);
496 handleNonNullReturn(Data, Opts);
500 static void handleNonNullArg(NonNullArgData *Data, ReportOptions Opts) {
501 SourceLocation Loc = Data->Loc.acquire();
502 ErrorType ET = ErrorType::InvalidNullArgument;
504 if (ignoreReport(Loc, Opts, ET))
507 ScopedReport R(Opts, Loc, ET);
509 Diag(Loc, DL_Error, "null pointer passed as argument %0, which is declared to "
510 "never be null") << Data->ArgIndex;
511 if (!Data->AttrLoc.isInvalid())
512 Diag(Data->AttrLoc, DL_Note, "nonnull attribute specified here");
515 void __ubsan::__ubsan_handle_nonnull_arg(NonNullArgData *Data) {
516 GET_REPORT_OPTIONS(false);
517 handleNonNullArg(Data, Opts);
520 void __ubsan::__ubsan_handle_nonnull_arg_abort(NonNullArgData *Data) {
521 GET_REPORT_OPTIONS(true);
522 handleNonNullArg(Data, Opts);
526 static void handleCFIBadIcall(CFIBadIcallData *Data, ValueHandle Function,
527 ReportOptions Opts) {
528 SourceLocation Loc = Data->Loc.acquire();
529 ErrorType ET = ErrorType::CFIBadType;
531 if (ignoreReport(Loc, Opts, ET))
534 ScopedReport R(Opts, Loc, ET);
536 Diag(Loc, DL_Error, "control flow integrity check for type %0 failed during "
537 "indirect function call")
540 SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
541 const char *FName = FLoc.get()->info.function;
544 Diag(FLoc, DL_Note, "%0 defined here") << FName;
547 void __ubsan::__ubsan_handle_cfi_bad_icall(CFIBadIcallData *Data,
548 ValueHandle Function) {
549 GET_REPORT_OPTIONS(false);
550 handleCFIBadIcall(Data, Function, Opts);
553 void __ubsan::__ubsan_handle_cfi_bad_icall_abort(CFIBadIcallData *Data,
554 ValueHandle Function) {
555 GET_REPORT_OPTIONS(true);
556 handleCFIBadIcall(Data, Function, Opts);
560 #endif // CAN_SANITIZE_UB