]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/ubsan/ubsan_handlers.cc
MFV of 279704,tzdata{2015a}
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / ubsan / ubsan_handlers.cc
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
19 using namespace __sanitizer;
20 using namespace __ubsan;
21
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;
30 }
31
32 namespace __ubsan {
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"};
37 }
38
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))
44     return;
45
46   if (Data->Loc.isInvalid())
47     Loc = FallbackLoc;
48
49   ScopedReport R(Opts, Loc);
50
51   if (!Pointer)
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;
59   else
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;
63   if (Pointer)
64     Diag(Pointer, DL_Note, "pointer points here");
65 }
66
67 void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data,
68                                            ValueHandle Pointer) {
69   GET_REPORT_OPTIONS(false);
70   handleTypeMismatchImpl(Data, Pointer, getCallerLocation(), Opts);
71 }
72 void __ubsan::__ubsan_handle_type_mismatch_abort(TypeMismatchData *Data,
73                                                  ValueHandle Pointer) {
74   GET_REPORT_OPTIONS(true);
75   handleTypeMismatchImpl(Data, Pointer, getCallerLocation(), Opts);
76   Die();
77 }
78
79 /// \brief Common diagnostic emission for various forms of integer overflow.
80 template <typename T>
81 static void handleIntegerOverflowImpl(OverflowData *Data, ValueHandle LHS,
82                                       const char *Operator, T RHS,
83                                       ReportOptions Opts) {
84   SourceLocation Loc = Data->Loc.acquire();
85   if (ignoreReport(Loc, Opts))
86     return;
87
88   ScopedReport R(Opts, Loc);
89
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;
94 }
95
96 #define UBSAN_OVERFLOW_HANDLER(handler_name, op, abort)                        \
97   void __ubsan::handler_name(OverflowData *Data, ValueHandle LHS,              \
98                              ValueHandle RHS) {                                \
99     GET_REPORT_OPTIONS(abort);                                                 \
100     handleIntegerOverflowImpl(Data, LHS, op, Value(Data->Type, RHS), Opts);    \
101     if (abort) Die();                                                          \
102   }
103
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)
110
111 static void handleNegateOverflowImpl(OverflowData *Data, ValueHandle OldVal,
112                                      ReportOptions Opts) {
113   SourceLocation Loc = Data->Loc.acquire();
114   if (ignoreReport(Loc, Opts))
115     return;
116
117   ScopedReport R(Opts, Loc);
118
119   if (Data->Type.isSignedIntegerTy())
120     Diag(Loc, DL_Error,
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;
124   else
125     Diag(Loc, DL_Error,
126          "negation of %0 cannot be represented in type %1")
127       << Value(Data->Type, OldVal) << Data->Type;
128 }
129
130 void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data,
131                                              ValueHandle OldVal) {
132   GET_REPORT_OPTIONS(false);
133   handleNegateOverflowImpl(Data, OldVal, Opts);
134 }
135 void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data,
136                                                     ValueHandle OldVal) {
137   GET_REPORT_OPTIONS(true);
138   handleNegateOverflowImpl(Data, OldVal, Opts);
139   Die();
140 }
141
142 static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS,
143                                      ValueHandle RHS, ReportOptions Opts) {
144   SourceLocation Loc = Data->Loc.acquire();
145   if (ignoreReport(Loc, Opts))
146     return;
147
148   ScopedReport R(Opts, Loc);
149
150   Value LHSVal(Data->Type, LHS);
151   Value RHSVal(Data->Type, RHS);
152   if (RHSVal.isMinusOne())
153     Diag(Loc, DL_Error,
154          "division of %0 by -1 cannot be represented in type %1")
155       << LHSVal << Data->Type;
156   else
157     Diag(Loc, DL_Error, "division by zero");
158 }
159
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);
164 }
165 void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data,
166                                                     ValueHandle LHS,
167                                                     ValueHandle RHS) {
168   GET_REPORT_OPTIONS(true);
169   handleDivremOverflowImpl(Data, LHS, RHS, Opts);
170   Die();
171 }
172
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))
178     return;
179
180   ScopedReport R(Opts, Loc);
181
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())
187     Diag(Loc, DL_Error,
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;
192   else
193     Diag(Loc, DL_Error,
194          "left shift of %0 by %1 places cannot be represented in type %2")
195       << LHSVal << RHSVal << Data->LHSType;
196 }
197
198 void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data,
199                                                  ValueHandle LHS,
200                                                  ValueHandle RHS) {
201   GET_REPORT_OPTIONS(false);
202   handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
203 }
204 void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
205                                                      ShiftOutOfBoundsData *Data,
206                                                      ValueHandle LHS,
207                                                      ValueHandle RHS) {
208   GET_REPORT_OPTIONS(true);
209   handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
210   Die();
211 }
212
213 static void handleOutOfBoundsImpl(OutOfBoundsData *Data, ValueHandle Index,
214                                   ReportOptions Opts) {
215   SourceLocation Loc = Data->Loc.acquire();
216   if (ignoreReport(Loc, Opts))
217     return;
218
219   ScopedReport R(Opts, Loc);
220
221   Value IndexVal(Data->IndexType, Index);
222   Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
223     << IndexVal << Data->ArrayType;
224 }
225
226 void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
227                                            ValueHandle Index) {
228   GET_REPORT_OPTIONS(false);
229   handleOutOfBoundsImpl(Data, Index, Opts);
230 }
231 void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
232                                                  ValueHandle Index) {
233   GET_REPORT_OPTIONS(true);
234   handleOutOfBoundsImpl(Data, Index, Opts);
235   Die();
236 }
237
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");
242 }
243
244 void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
245   GET_REPORT_OPTIONS(true);
246   handleBuiltinUnreachableImpl(Data, Opts);
247   Die();
248 }
249
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");
255 }
256
257 void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) {
258   GET_REPORT_OPTIONS(true);
259   handleMissingReturnImpl(Data, Opts);
260   Die();
261 }
262
263 static void handleVLABoundNotPositive(VLABoundData *Data, ValueHandle Bound,
264                                       ReportOptions Opts) {
265   SourceLocation Loc = Data->Loc.acquire();
266   if (ignoreReport(Loc, Opts))
267     return;
268
269   ScopedReport R(Opts, Loc);
270
271   Diag(Loc, DL_Error, "variable length array bound evaluates to "
272                       "non-positive value %0")
273     << Value(Data->Type, Bound);
274 }
275
276 void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data,
277                                                     ValueHandle Bound) {
278   GET_REPORT_OPTIONS(false);
279   handleVLABoundNotPositive(Data, Bound, Opts);
280 }
281 void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data,
282                                                           ValueHandle Bound) {
283   GET_REPORT_OPTIONS(true);
284   handleVLABoundNotPositive(Data, Bound, Opts);
285   Die();
286 }
287
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);
293
294   Diag(Loc, DL_Error,
295        "value %0 is outside the range of representable values of type %2")
296       << Value(Data->FromType, From) << Data->FromType << Data->ToType;
297 }
298
299 void __ubsan::__ubsan_handle_float_cast_overflow(FloatCastOverflowData *Data,
300                                                  ValueHandle From) {
301   GET_REPORT_OPTIONS(false);
302   handleFloatCastOverflow(Data, From, Opts);
303 }
304 void
305 __ubsan::__ubsan_handle_float_cast_overflow_abort(FloatCastOverflowData *Data,
306                                                   ValueHandle From) {
307   GET_REPORT_OPTIONS(true);
308   handleFloatCastOverflow(Data, From, Opts);
309   Die();
310 }
311
312 static void handleLoadInvalidValue(InvalidValueData *Data, ValueHandle Val,
313                                    ReportOptions Opts) {
314   SourceLocation Loc = Data->Loc.acquire();
315   if (ignoreReport(Loc, Opts))
316     return;
317
318   ScopedReport R(Opts, Loc);
319
320   Diag(Loc, DL_Error,
321        "load of value %0, which is not a valid value for type %1")
322     << Value(Data->Type, Val) << Data->Type;
323 }
324
325 void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data,
326                                                 ValueHandle Val) {
327   GET_REPORT_OPTIONS(false);
328   handleLoadInvalidValue(Data, Val, Opts);
329 }
330 void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data,
331                                                       ValueHandle Val) {
332   GET_REPORT_OPTIONS(true);
333   handleLoadInvalidValue(Data, Val, Opts);
334   Die();
335 }
336
337 static void handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
338                                        ValueHandle Function,
339                                        ReportOptions Opts) {
340   const char *FName = "(unknown)";
341
342   Location Loc = getFunctionLocation(Function, &FName);
343
344   ScopedReport R(Opts, Loc);
345
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;
350 }
351
352 void
353 __ubsan::__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,
354                                                ValueHandle Function) {
355   GET_REPORT_OPTIONS(false);
356   handleFunctionTypeMismatch(Data, Function, Opts);
357 }
358
359 void __ubsan::__ubsan_handle_function_type_mismatch_abort(
360     FunctionTypeMismatchData *Data, ValueHandle Function) {
361   GET_REPORT_OPTIONS(true);
362   handleFunctionTypeMismatch(Data, Function, Opts);
363   Die();
364 }
365
366 static void handleNonNullReturn(NonNullReturnData *Data, ReportOptions Opts) {
367   SourceLocation Loc = Data->Loc.acquire();
368   if (ignoreReport(Loc, Opts))
369     return;
370
371   ScopedReport R(Opts, Loc);
372
373   Diag(Loc, DL_Error, "null pointer returned from function declared to never "
374                       "return null");
375   if (!Data->AttrLoc.isInvalid())
376     Diag(Data->AttrLoc, DL_Note, "returns_nonnull attribute specified here");
377 }
378
379 void __ubsan::__ubsan_handle_nonnull_return(NonNullReturnData *Data) {
380   GET_REPORT_OPTIONS(false);
381   handleNonNullReturn(Data, Opts);
382 }
383
384 void __ubsan::__ubsan_handle_nonnull_return_abort(NonNullReturnData *Data) {
385   GET_REPORT_OPTIONS(true);
386   handleNonNullReturn(Data, Opts);
387   Die();
388 }
389
390 static void handleNonNullArg(NonNullArgData *Data, ReportOptions Opts) {
391   SourceLocation Loc = Data->Loc.acquire();
392   if (ignoreReport(Loc, Opts))
393     return;
394
395   ScopedReport R(Opts, Loc);
396
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");
401 }
402
403 void __ubsan::__ubsan_handle_nonnull_arg(NonNullArgData *Data) {
404   GET_REPORT_OPTIONS(false);
405   handleNonNullArg(Data, Opts);
406 }
407
408 void __ubsan::__ubsan_handle_nonnull_arg_abort(NonNullArgData *Data) {
409   GET_REPORT_OPTIONS(true);
410   handleNonNullArg(Data, Opts);
411   Die();
412 }