blob: 5d82e9afcd09ef96de5101df31c650174b0ac3ae [file] [log] [blame]
Richard Smitheda8bd02012-10-25 02:07:02 +00001//===-- ubsan_handlers.cc -------------------------------------------------===//
Richard Smith6ebe4512012-10-09 19:34:32 +00002//
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
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070014#include "ubsan_platform.h"
15#if CAN_SANITIZE_UB
Richard Smith6ebe4512012-10-09 19:34:32 +000016#include "ubsan_handlers.h"
17#include "ubsan_diag.h"
18
19#include "sanitizer_common/sanitizer_common.h"
20
21using namespace __sanitizer;
22using namespace __ubsan;
23
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080024namespace __ubsan {
25bool 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.
29 //
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)
34 return false;
35 return SLoc.isDisabled() || IsPCSuppressed(ET, Opts.pc, SLoc.getFilename());
Stephen Hines6d186232014-11-26 17:56:19 -080036}
37
Stephen Hines6d186232014-11-26 17:56:19 -080038const char *TypeCheckKinds[] = {
Richard Smith6ebe4512012-10-09 19:34:32 +000039 "load of", "store to", "reference binding to", "member access within",
Stephen Hines6d186232014-11-26 17:56:19 -080040 "member call on", "constructor call on", "downcast of", "downcast of",
41 "upcast of", "cast to virtual base of"};
Richard Smitheda8bd02012-10-25 02:07:02 +000042}
43
Richard Smith5f116492012-12-18 04:23:18 +000044static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
Stephen Hines86277eb2015-03-23 12:06:32 -070045 ReportOptions Opts) {
Will Dietz2af552f2013-01-09 03:40:03 +000046 Location Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080047
48 ErrorType ET;
49 if (!Pointer)
50 ET = ErrorType::NullPointerUse;
51 else if (Data->Alignment && (Pointer & (Data->Alignment - 1)))
52 ET = ErrorType::MisalignedPointerUse;
53 else
54 ET = ErrorType::InsufficientObjectSize;
55
56 // Use the SourceLocation from Data to track deduplication, even if it's
57 // invalid.
58 if (ignoreReport(Loc.getSourceLocation(), Opts, ET))
Will Dietz2af552f2013-01-09 03:40:03 +000059 return;
Stephen Hines6d186232014-11-26 17:56:19 -080060
Stephen Hines86277eb2015-03-23 12:06:32 -070061 SymbolizedStackHolder FallbackLoc;
62 if (Data->Loc.isInvalid()) {
63 FallbackLoc.reset(getCallerLocation(Opts.pc));
Richard Smith5f116492012-12-18 04:23:18 +000064 Loc = FallbackLoc;
Stephen Hines86277eb2015-03-23 12:06:32 -070065 }
Richard Smith5f116492012-12-18 04:23:18 +000066
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080067 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -080068
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080069 switch (ET) {
70 case ErrorType::NullPointerUse:
Richard Smith25ee97f2012-12-18 06:30:32 +000071 Diag(Loc, DL_Error, "%0 null pointer of type %1")
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080072 << TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
73 break;
74 case ErrorType::MisalignedPointerUse:
Richard Smith25ee97f2012-12-18 06:30:32 +000075 Diag(Loc, DL_Error, "%0 misaligned address %1 for type %3, "
76 "which requires %2 byte alignment")
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080077 << TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer
78 << Data->Alignment << Data->Type;
79 break;
80 case ErrorType::InsufficientObjectSize:
Richard Smith25ee97f2012-12-18 06:30:32 +000081 Diag(Loc, DL_Error, "%0 address %1 with insufficient space "
82 "for an object of type %2")
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080083 << TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer << Data->Type;
84 break;
85 default:
86 UNREACHABLE("unexpected error type!");
87 }
88
Richard Smith25ee97f2012-12-18 06:30:32 +000089 if (Pointer)
90 Diag(Pointer, DL_Note, "pointer points here");
Will Dietza82a5d32012-12-02 19:47:29 +000091}
Stephen Hines6d186232014-11-26 17:56:19 -080092
Richard Smith5f116492012-12-18 04:23:18 +000093void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data,
94 ValueHandle Pointer) {
Stephen Hines6d186232014-11-26 17:56:19 -080095 GET_REPORT_OPTIONS(false);
Stephen Hines86277eb2015-03-23 12:06:32 -070096 handleTypeMismatchImpl(Data, Pointer, Opts);
Richard Smith5f116492012-12-18 04:23:18 +000097}
Will Dietza82a5d32012-12-02 19:47:29 +000098void __ubsan::__ubsan_handle_type_mismatch_abort(TypeMismatchData *Data,
Richard Smith5f116492012-12-18 04:23:18 +000099 ValueHandle Pointer) {
Stephen Hines6d186232014-11-26 17:56:19 -0800100 GET_REPORT_OPTIONS(true);
Stephen Hines86277eb2015-03-23 12:06:32 -0700101 handleTypeMismatchImpl(Data, Pointer, Opts);
Richard Smith6ebe4512012-10-09 19:34:32 +0000102 Die();
103}
104
Will Dietz80af6052012-11-27 15:01:43 +0000105/// \brief Common diagnostic emission for various forms of integer overflow.
Stephen Hines6d186232014-11-26 17:56:19 -0800106template <typename T>
107static void handleIntegerOverflowImpl(OverflowData *Data, ValueHandle LHS,
108 const char *Operator, T RHS,
109 ReportOptions Opts) {
Will Dietz2af552f2013-01-09 03:40:03 +0000110 SourceLocation Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800111 bool IsSigned = Data->Type.isSignedIntegerTy();
112 ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
113 : ErrorType::UnsignedIntegerOverflow;
114
115 if (ignoreReport(Loc, Opts, ET))
Will Dietz2af552f2013-01-09 03:40:03 +0000116 return;
117
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800118 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800119
Will Dietz2af552f2013-01-09 03:40:03 +0000120 Diag(Loc, DL_Error, "%0 integer overflow: "
121 "%1 %2 %3 cannot be represented in type %4")
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800122 << (IsSigned ? "signed" : "unsigned")
Richard Smith6ebe4512012-10-09 19:34:32 +0000123 << Value(Data->Type, LHS) << Operator << RHS << Data->Type;
Richard Smith6ebe4512012-10-09 19:34:32 +0000124}
125
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800126#define UBSAN_OVERFLOW_HANDLER(handler_name, op, unrecoverable) \
Stephen Hines6d186232014-11-26 17:56:19 -0800127 void __ubsan::handler_name(OverflowData *Data, ValueHandle LHS, \
128 ValueHandle RHS) { \
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800129 GET_REPORT_OPTIONS(unrecoverable); \
Stephen Hines6d186232014-11-26 17:56:19 -0800130 handleIntegerOverflowImpl(Data, LHS, op, Value(Data->Type, RHS), Opts); \
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800131 if (unrecoverable) \
132 Die(); \
Stephen Hines6d186232014-11-26 17:56:19 -0800133 }
Richard Smith6ebe4512012-10-09 19:34:32 +0000134
Stephen Hines6d186232014-11-26 17:56:19 -0800135UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow, "+", false)
136UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow_abort, "+", true)
137UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow, "-", false)
138UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow_abort, "-", true)
139UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow, "*", false)
140UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow_abort, "*", true)
Richard Smith6ebe4512012-10-09 19:34:32 +0000141
Stephen Hines6d186232014-11-26 17:56:19 -0800142static void handleNegateOverflowImpl(OverflowData *Data, ValueHandle OldVal,
143 ReportOptions Opts) {
Will Dietz2af552f2013-01-09 03:40:03 +0000144 SourceLocation Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800145 bool IsSigned = Data->Type.isSignedIntegerTy();
146 ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
147 : ErrorType::UnsignedIntegerOverflow;
148
149 if (ignoreReport(Loc, Opts, ET))
Will Dietz2af552f2013-01-09 03:40:03 +0000150 return;
151
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800152 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800153
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800154 if (IsSigned)
Will Dietz2af552f2013-01-09 03:40:03 +0000155 Diag(Loc, DL_Error,
Will Dietzf359dea2012-12-31 06:36:44 +0000156 "negation of %0 cannot be represented in type %1; "
157 "cast to an unsigned type to negate this value to itself")
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800158 << Value(Data->Type, OldVal) << Data->Type;
Will Dietzf359dea2012-12-31 06:36:44 +0000159 else
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800160 Diag(Loc, DL_Error, "negation of %0 cannot be represented in type %1")
161 << Value(Data->Type, OldVal) << Data->Type;
Will Dietza82a5d32012-12-02 19:47:29 +0000162}
Stephen Hines6d186232014-11-26 17:56:19 -0800163
164void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data,
165 ValueHandle OldVal) {
166 GET_REPORT_OPTIONS(false);
167 handleNegateOverflowImpl(Data, OldVal, Opts);
168}
Will Dietza82a5d32012-12-02 19:47:29 +0000169void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data,
170 ValueHandle OldVal) {
Stephen Hines6d186232014-11-26 17:56:19 -0800171 GET_REPORT_OPTIONS(true);
172 handleNegateOverflowImpl(Data, OldVal, Opts);
Richard Smith6ebe4512012-10-09 19:34:32 +0000173 Die();
174}
175
Stephen Hines6d186232014-11-26 17:56:19 -0800176static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS,
177 ValueHandle RHS, ReportOptions Opts) {
Will Dietz2af552f2013-01-09 03:40:03 +0000178 SourceLocation Loc = Data->Loc.acquire();
Richard Smith6ebe4512012-10-09 19:34:32 +0000179 Value LHSVal(Data->Type, LHS);
180 Value RHSVal(Data->Type, RHS);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800181
182 ErrorType ET;
Richard Smith6ebe4512012-10-09 19:34:32 +0000183 if (RHSVal.isMinusOne())
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800184 ET = ErrorType::SignedIntegerOverflow;
185 else if (Data->Type.isIntegerTy())
186 ET = ErrorType::IntegerDivideByZero;
Richard Smith6ebe4512012-10-09 19:34:32 +0000187 else
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800188 ET = ErrorType::FloatDivideByZero;
189
190 if (ignoreReport(Loc, Opts, ET))
191 return;
192
193 ScopedReport R(Opts, Loc, ET);
194
195 switch (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;
199 break;
200 default:
Will Dietz2af552f2013-01-09 03:40:03 +0000201 Diag(Loc, DL_Error, "division by zero");
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800202 break;
203 }
Will Dietza82a5d32012-12-02 19:47:29 +0000204}
Stephen Hines6d186232014-11-26 17:56:19 -0800205
206void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data,
207 ValueHandle LHS, ValueHandle RHS) {
208 GET_REPORT_OPTIONS(false);
209 handleDivremOverflowImpl(Data, LHS, RHS, Opts);
210}
Will Dietza82a5d32012-12-02 19:47:29 +0000211void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data,
212 ValueHandle LHS,
213 ValueHandle RHS) {
Stephen Hines6d186232014-11-26 17:56:19 -0800214 GET_REPORT_OPTIONS(true);
215 handleDivremOverflowImpl(Data, LHS, RHS, Opts);
Richard Smith6ebe4512012-10-09 19:34:32 +0000216 Die();
217}
218
Stephen Hines6d186232014-11-26 17:56:19 -0800219static void handleShiftOutOfBoundsImpl(ShiftOutOfBoundsData *Data,
220 ValueHandle LHS, ValueHandle RHS,
221 ReportOptions Opts) {
Will Dietz2af552f2013-01-09 03:40:03 +0000222 SourceLocation Loc = Data->Loc.acquire();
Richard Smith6ebe4512012-10-09 19:34:32 +0000223 Value LHSVal(Data->LHSType, LHS);
224 Value RHSVal(Data->RHSType, RHS);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800225
226 ErrorType ET;
227 if (RHSVal.isNegative() ||
228 RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth())
229 ET = ErrorType::InvalidShiftExponent;
Richard Smith6ebe4512012-10-09 19:34:32 +0000230 else
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800231 ET = ErrorType::InvalidShiftBase;
232
233 if (ignoreReport(Loc, Opts, ET))
234 return;
235
236 ScopedReport R(Opts, Loc, ET);
237
238 if (ET == ErrorType::InvalidShiftExponent) {
239 if (RHSVal.isNegative())
240 Diag(Loc, DL_Error, "shift exponent %0 is negative") << RHSVal;
241 else
242 Diag(Loc, DL_Error, "shift exponent %0 is too large for %1-bit type %2")
243 << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType;
244 } else {
245 if (LHSVal.isNegative())
246 Diag(Loc, DL_Error, "left shift of negative value %0") << LHSVal;
247 else
248 Diag(Loc, DL_Error,
249 "left shift of %0 by %1 places cannot be represented in type %2")
250 << LHSVal << RHSVal << Data->LHSType;
251 }
Will Dietza82a5d32012-12-02 19:47:29 +0000252}
Stephen Hines6d186232014-11-26 17:56:19 -0800253
254void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data,
255 ValueHandle LHS,
256 ValueHandle RHS) {
257 GET_REPORT_OPTIONS(false);
258 handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
259}
Will Dietza82a5d32012-12-02 19:47:29 +0000260void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
261 ShiftOutOfBoundsData *Data,
262 ValueHandle LHS,
263 ValueHandle RHS) {
Stephen Hines6d186232014-11-26 17:56:19 -0800264 GET_REPORT_OPTIONS(true);
265 handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
Richard Smith6ebe4512012-10-09 19:34:32 +0000266 Die();
267}
268
Stephen Hines6d186232014-11-26 17:56:19 -0800269static void handleOutOfBoundsImpl(OutOfBoundsData *Data, ValueHandle Index,
270 ReportOptions Opts) {
Richard Smitha0b1e212013-02-23 02:40:07 +0000271 SourceLocation Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800272 ErrorType ET = ErrorType::OutOfBoundsIndex;
273
274 if (ignoreReport(Loc, Opts, ET))
Richard Smitha0b1e212013-02-23 02:40:07 +0000275 return;
276
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800277 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800278
Richard Smitha0b1e212013-02-23 02:40:07 +0000279 Value IndexVal(Data->IndexType, Index);
280 Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
281 << IndexVal << Data->ArrayType;
282}
Stephen Hines6d186232014-11-26 17:56:19 -0800283
284void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
285 ValueHandle Index) {
286 GET_REPORT_OPTIONS(false);
287 handleOutOfBoundsImpl(Data, Index, Opts);
288}
Richard Smitha0b1e212013-02-23 02:40:07 +0000289void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
290 ValueHandle Index) {
Stephen Hines6d186232014-11-26 17:56:19 -0800291 GET_REPORT_OPTIONS(true);
292 handleOutOfBoundsImpl(Data, Index, Opts);
Richard Smitha0b1e212013-02-23 02:40:07 +0000293 Die();
294}
295
Stephen Hines6d186232014-11-26 17:56:19 -0800296static void handleBuiltinUnreachableImpl(UnreachableData *Data,
297 ReportOptions Opts) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800298 ScopedReport R(Opts, Data->Loc, ErrorType::UnreachableCall);
Stephen Hines6d186232014-11-26 17:56:19 -0800299 Diag(Data->Loc, DL_Error, "execution reached a __builtin_unreachable() call");
300}
301
Richard Smith6ebe4512012-10-09 19:34:32 +0000302void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
Stephen Hines6d186232014-11-26 17:56:19 -0800303 GET_REPORT_OPTIONS(true);
304 handleBuiltinUnreachableImpl(Data, Opts);
Richard Smith6ebe4512012-10-09 19:34:32 +0000305 Die();
306}
307
Stephen Hines6d186232014-11-26 17:56:19 -0800308static void handleMissingReturnImpl(UnreachableData *Data, ReportOptions Opts) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800309 ScopedReport R(Opts, Data->Loc, ErrorType::MissingReturn);
Richard Smith25ee97f2012-12-18 06:30:32 +0000310 Diag(Data->Loc, DL_Error,
311 "execution reached the end of a value-returning function "
312 "without returning a value");
Stephen Hines6d186232014-11-26 17:56:19 -0800313}
314
315void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) {
316 GET_REPORT_OPTIONS(true);
317 handleMissingReturnImpl(Data, Opts);
Richard Smith6ebe4512012-10-09 19:34:32 +0000318 Die();
319}
Richard Smithb04caf12012-10-10 01:10:59 +0000320
Stephen Hines6d186232014-11-26 17:56:19 -0800321static void handleVLABoundNotPositive(VLABoundData *Data, ValueHandle Bound,
322 ReportOptions Opts) {
Will Dietz2af552f2013-01-09 03:40:03 +0000323 SourceLocation Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800324 ErrorType ET = ErrorType::NonPositiveVLAIndex;
325
326 if (ignoreReport(Loc, Opts, ET))
Will Dietz2af552f2013-01-09 03:40:03 +0000327 return;
328
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800329 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800330
Will Dietz2af552f2013-01-09 03:40:03 +0000331 Diag(Loc, DL_Error, "variable length array bound evaluates to "
332 "non-positive value %0")
Richard Smithb04caf12012-10-10 01:10:59 +0000333 << Value(Data->Type, Bound);
Will Dietza82a5d32012-12-02 19:47:29 +0000334}
Stephen Hines6d186232014-11-26 17:56:19 -0800335
336void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data,
337 ValueHandle Bound) {
338 GET_REPORT_OPTIONS(false);
339 handleVLABoundNotPositive(Data, Bound, Opts);
340}
Will Dietza82a5d32012-12-02 19:47:29 +0000341void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data,
Stephen Hines6d186232014-11-26 17:56:19 -0800342 ValueHandle Bound) {
343 GET_REPORT_OPTIONS(true);
344 handleVLABoundNotPositive(Data, Bound, Opts);
Richard Smithb04caf12012-10-10 01:10:59 +0000345 Die();
346}
Richard Smith58561702012-10-12 22:57:15 +0000347
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800348static bool looksLikeFloatCastOverflowDataV1(void *Data) {
349 // First field is either a pointer to filename or a pointer to a
350 // TypeDescriptor.
351 u8 *FilenameOrTypeDescriptor;
352 internal_memcpy(&FilenameOrTypeDescriptor, Data,
353 sizeof(FilenameOrTypeDescriptor));
354
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;
364}
365
366static void handleFloatCastOverflow(void *DataPtr, ValueHandle From,
367 ReportOptions Opts) {
368 SymbolizedStackHolder CallerLoc;
369 Location Loc;
370 const TypeDescriptor *FromType, *ToType;
371 ErrorType ET = ErrorType::FloatCastOverflow;
372
373 if (looksLikeFloatCastOverflowDataV1(DataPtr)) {
374 auto Data = reinterpret_cast<FloatCastOverflowData *>(DataPtr);
375 CallerLoc.reset(getCallerLocation(Opts.pc));
376 Loc = CallerLoc;
377 FromType = &Data->FromType;
378 ToType = &Data->ToType;
379 } else {
380 auto Data = reinterpret_cast<FloatCastOverflowDataV2 *>(DataPtr);
381 SourceLocation SLoc = Data->Loc.acquire();
382 if (ignoreReport(SLoc, Opts, ET))
383 return;
384 Loc = SLoc;
385 FromType = &Data->FromType;
386 ToType = &Data->ToType;
387 }
388
389 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800390
391 Diag(Loc, DL_Error,
392 "value %0 is outside the range of representable values of type %2")
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800393 << Value(*FromType, From) << *FromType << *ToType;
Stephen Hines6d186232014-11-26 17:56:19 -0800394}
Richard Smith5f116492012-12-18 04:23:18 +0000395
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800396void __ubsan::__ubsan_handle_float_cast_overflow(void *Data, ValueHandle From) {
Stephen Hines6d186232014-11-26 17:56:19 -0800397 GET_REPORT_OPTIONS(false);
398 handleFloatCastOverflow(Data, From, Opts);
Will Dietza82a5d32012-12-02 19:47:29 +0000399}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800400void __ubsan::__ubsan_handle_float_cast_overflow_abort(void *Data,
401 ValueHandle From) {
Stephen Hines6d186232014-11-26 17:56:19 -0800402 GET_REPORT_OPTIONS(true);
403 handleFloatCastOverflow(Data, From, Opts);
Richard Smith58561702012-10-12 22:57:15 +0000404 Die();
405}
Richard Smithf2d77d02012-12-13 07:00:14 +0000406
Stephen Hines6d186232014-11-26 17:56:19 -0800407static void handleLoadInvalidValue(InvalidValueData *Data, ValueHandle Val,
408 ReportOptions Opts) {
Nick Lewyckyd1bf52e2013-10-02 02:29:47 +0000409 SourceLocation Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800410 // 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'"));
413 ErrorType ET =
414 IsBool ? ErrorType::InvalidBoolLoad : ErrorType::InvalidEnumLoad;
415
416 if (ignoreReport(Loc, Opts, ET))
Nick Lewyckyd1bf52e2013-10-02 02:29:47 +0000417 return;
418
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800419 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800420
Nick Lewyckyd1bf52e2013-10-02 02:29:47 +0000421 Diag(Loc, DL_Error,
Richard Smith25ee97f2012-12-18 06:30:32 +0000422 "load of value %0, which is not a valid value for type %1")
Richard Smithf2d77d02012-12-13 07:00:14 +0000423 << Value(Data->Type, Val) << Data->Type;
424}
Stephen Hines6d186232014-11-26 17:56:19 -0800425
426void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data,
427 ValueHandle Val) {
428 GET_REPORT_OPTIONS(false);
429 handleLoadInvalidValue(Data, Val, Opts);
430}
Richard Smithf2d77d02012-12-13 07:00:14 +0000431void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data,
432 ValueHandle Val) {
Stephen Hines6d186232014-11-26 17:56:19 -0800433 GET_REPORT_OPTIONS(true);
434 handleLoadInvalidValue(Data, Val, Opts);
Richard Smithf2d77d02012-12-13 07:00:14 +0000435 Die();
436}
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000437
Stephen Hines6d186232014-11-26 17:56:19 -0800438static void handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
439 ValueHandle Function,
440 ReportOptions Opts) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700441 SourceLocation CallLoc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800442 ErrorType ET = ErrorType::FunctionTypeMismatch;
443
444 if (ignoreReport(CallLoc, Opts, ET))
Stephen Hines86277eb2015-03-23 12:06:32 -0700445 return;
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000446
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800447 ScopedReport R(Opts, CallLoc, ET);
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000448
Stephen Hines86277eb2015-03-23 12:06:32 -0700449 SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
450 const char *FName = FLoc.get()->info.function;
451 if (!FName)
452 FName = "(unknown)";
Stephen Hines6d186232014-11-26 17:56:19 -0800453
Stephen Hines86277eb2015-03-23 12:06:32 -0700454 Diag(CallLoc, DL_Error,
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000455 "call to function %0 through pointer to incorrect function type %1")
Stephen Hines86277eb2015-03-23 12:06:32 -0700456 << FName << Data->Type;
457 Diag(FLoc, DL_Note, "%0 defined here") << FName;
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000458}
459
Stephen Hines6d186232014-11-26 17:56:19 -0800460void
461__ubsan::__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,
462 ValueHandle Function) {
463 GET_REPORT_OPTIONS(false);
464 handleFunctionTypeMismatch(Data, Function, Opts);
465}
466
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000467void __ubsan::__ubsan_handle_function_type_mismatch_abort(
Stephen Hines6d186232014-11-26 17:56:19 -0800468 FunctionTypeMismatchData *Data, ValueHandle Function) {
469 GET_REPORT_OPTIONS(true);
470 handleFunctionTypeMismatch(Data, Function, Opts);
471 Die();
472}
473
474static void handleNonNullReturn(NonNullReturnData *Data, ReportOptions Opts) {
475 SourceLocation Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800476 ErrorType ET = ErrorType::InvalidNullReturn;
477
478 if (ignoreReport(Loc, Opts, ET))
Stephen Hines6d186232014-11-26 17:56:19 -0800479 return;
480
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800481 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800482
483 Diag(Loc, DL_Error, "null pointer returned from function declared to never "
484 "return null");
485 if (!Data->AttrLoc.isInvalid())
486 Diag(Data->AttrLoc, DL_Note, "returns_nonnull attribute specified here");
487}
488
489void __ubsan::__ubsan_handle_nonnull_return(NonNullReturnData *Data) {
490 GET_REPORT_OPTIONS(false);
491 handleNonNullReturn(Data, Opts);
492}
493
494void __ubsan::__ubsan_handle_nonnull_return_abort(NonNullReturnData *Data) {
495 GET_REPORT_OPTIONS(true);
496 handleNonNullReturn(Data, Opts);
497 Die();
498}
499
500static void handleNonNullArg(NonNullArgData *Data, ReportOptions Opts) {
501 SourceLocation Loc = Data->Loc.acquire();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800502 ErrorType ET = ErrorType::InvalidNullArgument;
503
504 if (ignoreReport(Loc, Opts, ET))
Stephen Hines6d186232014-11-26 17:56:19 -0800505 return;
506
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800507 ScopedReport R(Opts, Loc, ET);
Stephen Hines6d186232014-11-26 17:56:19 -0800508
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");
513}
514
515void __ubsan::__ubsan_handle_nonnull_arg(NonNullArgData *Data) {
516 GET_REPORT_OPTIONS(false);
517 handleNonNullArg(Data, Opts);
518}
519
520void __ubsan::__ubsan_handle_nonnull_arg_abort(NonNullArgData *Data) {
521 GET_REPORT_OPTIONS(true);
522 handleNonNullArg(Data, Opts);
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000523 Die();
524}
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700525
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800526static void handleCFIBadIcall(CFIBadIcallData *Data, ValueHandle Function,
527 ReportOptions Opts) {
528 SourceLocation Loc = Data->Loc.acquire();
529 ErrorType ET = ErrorType::CFIBadType;
530
531 if (ignoreReport(Loc, Opts, ET))
532 return;
533
534 ScopedReport R(Opts, Loc, ET);
535
536 Diag(Loc, DL_Error, "control flow integrity check for type %0 failed during "
537 "indirect function call")
538 << Data->Type;
539
540 SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
541 const char *FName = FLoc.get()->info.function;
542 if (!FName)
543 FName = "(unknown)";
544 Diag(FLoc, DL_Note, "%0 defined here") << FName;
545}
546
547void __ubsan::__ubsan_handle_cfi_bad_icall(CFIBadIcallData *Data,
548 ValueHandle Function) {
549 GET_REPORT_OPTIONS(false);
550 handleCFIBadIcall(Data, Function, Opts);
551}
552
553void __ubsan::__ubsan_handle_cfi_bad_icall_abort(CFIBadIcallData *Data,
554 ValueHandle Function) {
555 GET_REPORT_OPTIONS(true);
556 handleCFIBadIcall(Data, Function, Opts);
557 Die();
558}
559
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700560#endif // CAN_SANITIZE_UB