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_handlers.h"
15 #include "ubsan_diag.h"
17 #include "sanitizer_common/sanitizer_common.h"
19 using namespace __sanitizer;
20 using namespace __ubsan;
22 static bool ignoreReport(SourceLocation SLoc, ReportOptions Opts) {
23 // If source location is already acquired, we don't need to print an error
24 // report for the second time. However, if we're in an unrecoverable handler,
25 // it's possible that location was required by concurrently running thread.
26 // In this case, we should continue the execution to ensure that any of
27 // threads will grab the report mutex and print the report before
28 // crashing the program.
29 return SLoc.isDisabled() && !Opts.DieAfterReport;
33 const char *TypeCheckKinds[] = {
34 "load of", "store to", "reference binding to", "member access within",
35 "member call on", "constructor call on", "downcast of", "downcast of",
36 "upcast of", "cast to virtual base of"};
39 static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
40 Location FallbackLoc, ReportOptions Opts) {
41 Location Loc = Data->Loc.acquire();
42 // Use the SourceLocation from Data to track deduplication, even if 'invalid'
43 if (ignoreReport(Loc.getSourceLocation(), Opts))
46 if (Data->Loc.isInvalid())
49 ScopedReport R(Opts, Loc);
52 Diag(Loc, DL_Error, "%0 null pointer of type %1")
53 << TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
54 else if (Data->Alignment && (Pointer & (Data->Alignment - 1)))
55 Diag(Loc, DL_Error, "%0 misaligned address %1 for type %3, "
56 "which requires %2 byte alignment")
57 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer
58 << Data->Alignment << Data->Type;
60 Diag(Loc, DL_Error, "%0 address %1 with insufficient space "
61 "for an object of type %2")
62 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
64 Diag(Pointer, DL_Note, "pointer points here");
67 void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data,
68 ValueHandle Pointer) {
69 GET_REPORT_OPTIONS(false);
70 handleTypeMismatchImpl(Data, Pointer, getCallerLocation(), Opts);
72 void __ubsan::__ubsan_handle_type_mismatch_abort(TypeMismatchData *Data,
73 ValueHandle Pointer) {
74 GET_REPORT_OPTIONS(true);
75 handleTypeMismatchImpl(Data, Pointer, getCallerLocation(), Opts);
79 /// \brief Common diagnostic emission for various forms of integer overflow.
81 static void handleIntegerOverflowImpl(OverflowData *Data, ValueHandle LHS,
82 const char *Operator, T RHS,
84 SourceLocation Loc = Data->Loc.acquire();
85 if (ignoreReport(Loc, Opts))
88 ScopedReport R(Opts, Loc);
90 Diag(Loc, DL_Error, "%0 integer overflow: "
91 "%1 %2 %3 cannot be represented in type %4")
92 << (Data->Type.isSignedIntegerTy() ? "signed" : "unsigned")
93 << Value(Data->Type, LHS) << Operator << RHS << Data->Type;
96 #define UBSAN_OVERFLOW_HANDLER(handler_name, op, abort) \
97 void __ubsan::handler_name(OverflowData *Data, ValueHandle LHS, \
99 GET_REPORT_OPTIONS(abort); \
100 handleIntegerOverflowImpl(Data, LHS, op, Value(Data->Type, RHS), Opts); \
104 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow, "+", false)
105 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow_abort, "+", true)
106 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow, "-", false)
107 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow_abort, "-", true)
108 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow, "*", false)
109 UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow_abort, "*", true)
111 static void handleNegateOverflowImpl(OverflowData *Data, ValueHandle OldVal,
112 ReportOptions Opts) {
113 SourceLocation Loc = Data->Loc.acquire();
114 if (ignoreReport(Loc, Opts))
117 ScopedReport R(Opts, Loc);
119 if (Data->Type.isSignedIntegerTy())
121 "negation of %0 cannot be represented in type %1; "
122 "cast to an unsigned type to negate this value to itself")
123 << Value(Data->Type, OldVal) << Data->Type;
126 "negation of %0 cannot be represented in type %1")
127 << Value(Data->Type, OldVal) << Data->Type;
130 void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data,
131 ValueHandle OldVal) {
132 GET_REPORT_OPTIONS(false);
133 handleNegateOverflowImpl(Data, OldVal, Opts);
135 void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data,
136 ValueHandle OldVal) {
137 GET_REPORT_OPTIONS(true);
138 handleNegateOverflowImpl(Data, OldVal, Opts);
142 static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS,
143 ValueHandle RHS, ReportOptions Opts) {
144 SourceLocation Loc = Data->Loc.acquire();
145 if (ignoreReport(Loc, Opts))
148 ScopedReport R(Opts, Loc);
150 Value LHSVal(Data->Type, LHS);
151 Value RHSVal(Data->Type, RHS);
152 if (RHSVal.isMinusOne())
154 "division of %0 by -1 cannot be represented in type %1")
155 << LHSVal << Data->Type;
157 Diag(Loc, DL_Error, "division by zero");
160 void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data,
161 ValueHandle LHS, ValueHandle RHS) {
162 GET_REPORT_OPTIONS(false);
163 handleDivremOverflowImpl(Data, LHS, RHS, Opts);
165 void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data,
168 GET_REPORT_OPTIONS(true);
169 handleDivremOverflowImpl(Data, LHS, RHS, Opts);
173 static void handleShiftOutOfBoundsImpl(ShiftOutOfBoundsData *Data,
174 ValueHandle LHS, ValueHandle RHS,
175 ReportOptions Opts) {
176 SourceLocation Loc = Data->Loc.acquire();
177 if (ignoreReport(Loc, Opts))
180 ScopedReport R(Opts, Loc);
182 Value LHSVal(Data->LHSType, LHS);
183 Value RHSVal(Data->RHSType, RHS);
184 if (RHSVal.isNegative())
185 Diag(Loc, DL_Error, "shift exponent %0 is negative") << RHSVal;
186 else if (RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth())
188 "shift exponent %0 is too large for %1-bit type %2")
189 << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType;
190 else if (LHSVal.isNegative())
191 Diag(Loc, DL_Error, "left shift of negative value %0") << LHSVal;
194 "left shift of %0 by %1 places cannot be represented in type %2")
195 << LHSVal << RHSVal << Data->LHSType;
198 void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data,
201 GET_REPORT_OPTIONS(false);
202 handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
204 void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
205 ShiftOutOfBoundsData *Data,
208 GET_REPORT_OPTIONS(true);
209 handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
213 static void handleOutOfBoundsImpl(OutOfBoundsData *Data, ValueHandle Index,
214 ReportOptions Opts) {
215 SourceLocation Loc = Data->Loc.acquire();
216 if (ignoreReport(Loc, Opts))
219 ScopedReport R(Opts, Loc);
221 Value IndexVal(Data->IndexType, Index);
222 Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
223 << IndexVal << Data->ArrayType;
226 void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
228 GET_REPORT_OPTIONS(false);
229 handleOutOfBoundsImpl(Data, Index, Opts);
231 void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
233 GET_REPORT_OPTIONS(true);
234 handleOutOfBoundsImpl(Data, Index, Opts);
238 static void handleBuiltinUnreachableImpl(UnreachableData *Data,
239 ReportOptions Opts) {
240 ScopedReport R(Opts, Data->Loc);
241 Diag(Data->Loc, DL_Error, "execution reached a __builtin_unreachable() call");
244 void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
245 GET_REPORT_OPTIONS(true);
246 handleBuiltinUnreachableImpl(Data, Opts);
250 static void handleMissingReturnImpl(UnreachableData *Data, ReportOptions Opts) {
251 ScopedReport R(Opts, Data->Loc);
252 Diag(Data->Loc, DL_Error,
253 "execution reached the end of a value-returning function "
254 "without returning a value");
257 void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) {
258 GET_REPORT_OPTIONS(true);
259 handleMissingReturnImpl(Data, Opts);
263 static void handleVLABoundNotPositive(VLABoundData *Data, ValueHandle Bound,
264 ReportOptions Opts) {
265 SourceLocation Loc = Data->Loc.acquire();
266 if (ignoreReport(Loc, Opts))
269 ScopedReport R(Opts, Loc);
271 Diag(Loc, DL_Error, "variable length array bound evaluates to "
272 "non-positive value %0")
273 << Value(Data->Type, Bound);
276 void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data,
278 GET_REPORT_OPTIONS(false);
279 handleVLABoundNotPositive(Data, Bound, Opts);
281 void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data,
283 GET_REPORT_OPTIONS(true);
284 handleVLABoundNotPositive(Data, Bound, Opts);
288 static void handleFloatCastOverflow(FloatCastOverflowData *Data,
289 ValueHandle From, ReportOptions Opts) {
290 // TODO: Add deduplication once a SourceLocation is generated for this check.
291 Location Loc = getCallerLocation();
292 ScopedReport R(Opts, Loc);
295 "value %0 is outside the range of representable values of type %2")
296 << Value(Data->FromType, From) << Data->FromType << Data->ToType;
299 void __ubsan::__ubsan_handle_float_cast_overflow(FloatCastOverflowData *Data,
301 GET_REPORT_OPTIONS(false);
302 handleFloatCastOverflow(Data, From, Opts);
305 __ubsan::__ubsan_handle_float_cast_overflow_abort(FloatCastOverflowData *Data,
307 GET_REPORT_OPTIONS(true);
308 handleFloatCastOverflow(Data, From, Opts);
312 static void handleLoadInvalidValue(InvalidValueData *Data, ValueHandle Val,
313 ReportOptions Opts) {
314 SourceLocation Loc = Data->Loc.acquire();
315 if (ignoreReport(Loc, Opts))
318 ScopedReport R(Opts, Loc);
321 "load of value %0, which is not a valid value for type %1")
322 << Value(Data->Type, Val) << Data->Type;
325 void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data,
327 GET_REPORT_OPTIONS(false);
328 handleLoadInvalidValue(Data, Val, Opts);
330 void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data,
332 GET_REPORT_OPTIONS(true);
333 handleLoadInvalidValue(Data, Val, Opts);
337 static void handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
338 ValueHandle Function,
339 ReportOptions Opts) {
340 const char *FName = "(unknown)";
342 Location Loc = getFunctionLocation(Function, &FName);
344 ScopedReport R(Opts, Loc);
346 Diag(Data->Loc, DL_Error,
347 "call to function %0 through pointer to incorrect function type %1")
348 << FName << Data->Type;
349 Diag(Loc, DL_Note, "%0 defined here") << FName;
353 __ubsan::__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,
354 ValueHandle Function) {
355 GET_REPORT_OPTIONS(false);
356 handleFunctionTypeMismatch(Data, Function, Opts);
359 void __ubsan::__ubsan_handle_function_type_mismatch_abort(
360 FunctionTypeMismatchData *Data, ValueHandle Function) {
361 GET_REPORT_OPTIONS(true);
362 handleFunctionTypeMismatch(Data, Function, Opts);
366 static void handleNonNullReturn(NonNullReturnData *Data, ReportOptions Opts) {
367 SourceLocation Loc = Data->Loc.acquire();
368 if (ignoreReport(Loc, Opts))
371 ScopedReport R(Opts, Loc);
373 Diag(Loc, DL_Error, "null pointer returned from function declared to never "
375 if (!Data->AttrLoc.isInvalid())
376 Diag(Data->AttrLoc, DL_Note, "returns_nonnull attribute specified here");
379 void __ubsan::__ubsan_handle_nonnull_return(NonNullReturnData *Data) {
380 GET_REPORT_OPTIONS(false);
381 handleNonNullReturn(Data, Opts);
384 void __ubsan::__ubsan_handle_nonnull_return_abort(NonNullReturnData *Data) {
385 GET_REPORT_OPTIONS(true);
386 handleNonNullReturn(Data, Opts);
390 static void handleNonNullArg(NonNullArgData *Data, ReportOptions Opts) {
391 SourceLocation Loc = Data->Loc.acquire();
392 if (ignoreReport(Loc, Opts))
395 ScopedReport R(Opts, Loc);
397 Diag(Loc, DL_Error, "null pointer passed as argument %0, which is declared to "
398 "never be null") << Data->ArgIndex;
399 if (!Data->AttrLoc.isInvalid())
400 Diag(Data->AttrLoc, DL_Note, "nonnull attribute specified here");
403 void __ubsan::__ubsan_handle_nonnull_arg(NonNullArgData *Data) {
404 GET_REPORT_OPTIONS(false);
405 handleNonNullArg(Data, Opts);
408 void __ubsan::__ubsan_handle_nonnull_arg_abort(NonNullArgData *Data) {
409 GET_REPORT_OPTIONS(true);
410 handleNonNullArg(Data, Opts);