ubsan_handlers.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //===-- ubsan_handlers.cc -------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Error logging entry points for the UBSan runtime.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "ubsan_handlers.h"
  12. #include "ubsan_diag.h"
  13. #include "sanitizer_common/sanitizer_common.h"
  14. using namespace __sanitizer;
  15. using namespace __ubsan;
  16. static bool ignoreReport(SourceLocation SLoc, ReportOptions Opts) {
  17. // If source location is already acquired, we don't need to print an error
  18. // report for the second time. However, if we're in an unrecoverable handler,
  19. // it's possible that location was required by concurrently running thread.
  20. // In this case, we should continue the execution to ensure that any of
  21. // threads will grab the report mutex and print the report before
  22. // crashing the program.
  23. return SLoc.isDisabled() && !Opts.DieAfterReport;
  24. }
  25. namespace __ubsan {
  26. const char *TypeCheckKinds[] = {
  27. "load of", "store to", "reference binding to", "member access within",
  28. "member call on", "constructor call on", "downcast of", "downcast of",
  29. "upcast of", "cast to virtual base of"};
  30. }
  31. static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
  32. Location FallbackLoc, ReportOptions Opts) {
  33. Location Loc = Data->Loc.acquire();
  34. // Use the SourceLocation from Data to track deduplication, even if 'invalid'
  35. if (ignoreReport(Loc.getSourceLocation(), Opts))
  36. return;
  37. if (Data->Loc.isInvalid())
  38. Loc = FallbackLoc;
  39. ScopedReport R(Opts, Loc);
  40. if (!Pointer)
  41. Diag(Loc, DL_Error, "%0 null pointer of type %1")
  42. << TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
  43. else if (Data->Alignment && (Pointer & (Data->Alignment - 1)))
  44. Diag(Loc, DL_Error, "%0 misaligned address %1 for type %3, "
  45. "which requires %2 byte alignment")
  46. << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer
  47. << Data->Alignment << Data->Type;
  48. else
  49. Diag(Loc, DL_Error, "%0 address %1 with insufficient space "
  50. "for an object of type %2")
  51. << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
  52. if (Pointer)
  53. Diag(Pointer, DL_Note, "pointer points here");
  54. }
  55. void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data,
  56. ValueHandle Pointer) {
  57. GET_REPORT_OPTIONS(false);
  58. handleTypeMismatchImpl(Data, Pointer, getCallerLocation(), Opts);
  59. }
  60. void __ubsan::__ubsan_handle_type_mismatch_abort(TypeMismatchData *Data,
  61. ValueHandle Pointer) {
  62. GET_REPORT_OPTIONS(true);
  63. handleTypeMismatchImpl(Data, Pointer, getCallerLocation(), Opts);
  64. Die();
  65. }
  66. /// \brief Common diagnostic emission for various forms of integer overflow.
  67. template <typename T>
  68. static void handleIntegerOverflowImpl(OverflowData *Data, ValueHandle LHS,
  69. const char *Operator, T RHS,
  70. ReportOptions Opts) {
  71. SourceLocation Loc = Data->Loc.acquire();
  72. if (ignoreReport(Loc, Opts))
  73. return;
  74. ScopedReport R(Opts, Loc);
  75. Diag(Loc, DL_Error, "%0 integer overflow: "
  76. "%1 %2 %3 cannot be represented in type %4")
  77. << (Data->Type.isSignedIntegerTy() ? "signed" : "unsigned")
  78. << Value(Data->Type, LHS) << Operator << RHS << Data->Type;
  79. }
  80. #define UBSAN_OVERFLOW_HANDLER(handler_name, op, abort) \
  81. void __ubsan::handler_name(OverflowData *Data, ValueHandle LHS, \
  82. ValueHandle RHS) { \
  83. GET_REPORT_OPTIONS(abort); \
  84. handleIntegerOverflowImpl(Data, LHS, op, Value(Data->Type, RHS), Opts); \
  85. if (abort) Die(); \
  86. }
  87. UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow, "+", false)
  88. UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow_abort, "+", true)
  89. UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow, "-", false)
  90. UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow_abort, "-", true)
  91. UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow, "*", false)
  92. UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow_abort, "*", true)
  93. static void handleNegateOverflowImpl(OverflowData *Data, ValueHandle OldVal,
  94. ReportOptions Opts) {
  95. SourceLocation Loc = Data->Loc.acquire();
  96. if (ignoreReport(Loc, Opts))
  97. return;
  98. ScopedReport R(Opts, Loc);
  99. if (Data->Type.isSignedIntegerTy())
  100. Diag(Loc, DL_Error,
  101. "negation of %0 cannot be represented in type %1; "
  102. "cast to an unsigned type to negate this value to itself")
  103. << Value(Data->Type, OldVal) << Data->Type;
  104. else
  105. Diag(Loc, DL_Error,
  106. "negation of %0 cannot be represented in type %1")
  107. << Value(Data->Type, OldVal) << Data->Type;
  108. }
  109. void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data,
  110. ValueHandle OldVal) {
  111. GET_REPORT_OPTIONS(false);
  112. handleNegateOverflowImpl(Data, OldVal, Opts);
  113. }
  114. void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data,
  115. ValueHandle OldVal) {
  116. GET_REPORT_OPTIONS(true);
  117. handleNegateOverflowImpl(Data, OldVal, Opts);
  118. Die();
  119. }
  120. static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS,
  121. ValueHandle RHS, ReportOptions Opts) {
  122. SourceLocation Loc = Data->Loc.acquire();
  123. if (ignoreReport(Loc, Opts))
  124. return;
  125. ScopedReport R(Opts, Loc);
  126. Value LHSVal(Data->Type, LHS);
  127. Value RHSVal(Data->Type, RHS);
  128. if (RHSVal.isMinusOne())
  129. Diag(Loc, DL_Error,
  130. "division of %0 by -1 cannot be represented in type %1")
  131. << LHSVal << Data->Type;
  132. else
  133. Diag(Loc, DL_Error, "division by zero");
  134. }
  135. void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data,
  136. ValueHandle LHS, ValueHandle RHS) {
  137. GET_REPORT_OPTIONS(false);
  138. handleDivremOverflowImpl(Data, LHS, RHS, Opts);
  139. }
  140. void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data,
  141. ValueHandle LHS,
  142. ValueHandle RHS) {
  143. GET_REPORT_OPTIONS(true);
  144. handleDivremOverflowImpl(Data, LHS, RHS, Opts);
  145. Die();
  146. }
  147. static void handleShiftOutOfBoundsImpl(ShiftOutOfBoundsData *Data,
  148. ValueHandle LHS, ValueHandle RHS,
  149. ReportOptions Opts) {
  150. SourceLocation Loc = Data->Loc.acquire();
  151. if (ignoreReport(Loc, Opts))
  152. return;
  153. ScopedReport R(Opts, Loc);
  154. Value LHSVal(Data->LHSType, LHS);
  155. Value RHSVal(Data->RHSType, RHS);
  156. if (RHSVal.isNegative())
  157. Diag(Loc, DL_Error, "shift exponent %0 is negative") << RHSVal;
  158. else if (RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth())
  159. Diag(Loc, DL_Error,
  160. "shift exponent %0 is too large for %1-bit type %2")
  161. << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType;
  162. else if (LHSVal.isNegative())
  163. Diag(Loc, DL_Error, "left shift of negative value %0") << LHSVal;
  164. else
  165. Diag(Loc, DL_Error,
  166. "left shift of %0 by %1 places cannot be represented in type %2")
  167. << LHSVal << RHSVal << Data->LHSType;
  168. }
  169. void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data,
  170. ValueHandle LHS,
  171. ValueHandle RHS) {
  172. GET_REPORT_OPTIONS(false);
  173. handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
  174. }
  175. void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
  176. ShiftOutOfBoundsData *Data,
  177. ValueHandle LHS,
  178. ValueHandle RHS) {
  179. GET_REPORT_OPTIONS(true);
  180. handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
  181. Die();
  182. }
  183. static void handleOutOfBoundsImpl(OutOfBoundsData *Data, ValueHandle Index,
  184. ReportOptions Opts) {
  185. SourceLocation Loc = Data->Loc.acquire();
  186. if (ignoreReport(Loc, Opts))
  187. return;
  188. ScopedReport R(Opts, Loc);
  189. Value IndexVal(Data->IndexType, Index);
  190. Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
  191. << IndexVal << Data->ArrayType;
  192. }
  193. void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
  194. ValueHandle Index) {
  195. GET_REPORT_OPTIONS(false);
  196. handleOutOfBoundsImpl(Data, Index, Opts);
  197. }
  198. void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
  199. ValueHandle Index) {
  200. GET_REPORT_OPTIONS(true);
  201. handleOutOfBoundsImpl(Data, Index, Opts);
  202. Die();
  203. }
  204. static void handleBuiltinUnreachableImpl(UnreachableData *Data,
  205. ReportOptions Opts) {
  206. ScopedReport R(Opts, Data->Loc);
  207. Diag(Data->Loc, DL_Error, "execution reached a __builtin_unreachable() call");
  208. }
  209. void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
  210. GET_REPORT_OPTIONS(true);
  211. handleBuiltinUnreachableImpl(Data, Opts);
  212. Die();
  213. }
  214. static void handleMissingReturnImpl(UnreachableData *Data, ReportOptions Opts) {
  215. ScopedReport R(Opts, Data->Loc);
  216. Diag(Data->Loc, DL_Error,
  217. "execution reached the end of a value-returning function "
  218. "without returning a value");
  219. }
  220. void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) {
  221. GET_REPORT_OPTIONS(true);
  222. handleMissingReturnImpl(Data, Opts);
  223. Die();
  224. }
  225. static void handleVLABoundNotPositive(VLABoundData *Data, ValueHandle Bound,
  226. ReportOptions Opts) {
  227. SourceLocation Loc = Data->Loc.acquire();
  228. if (ignoreReport(Loc, Opts))
  229. return;
  230. ScopedReport R(Opts, Loc);
  231. Diag(Loc, DL_Error, "variable length array bound evaluates to "
  232. "non-positive value %0")
  233. << Value(Data->Type, Bound);
  234. }
  235. void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data,
  236. ValueHandle Bound) {
  237. GET_REPORT_OPTIONS(false);
  238. handleVLABoundNotPositive(Data, Bound, Opts);
  239. }
  240. void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data,
  241. ValueHandle Bound) {
  242. GET_REPORT_OPTIONS(true);
  243. handleVLABoundNotPositive(Data, Bound, Opts);
  244. Die();
  245. }
  246. static void handleFloatCastOverflow(FloatCastOverflowData *Data,
  247. ValueHandle From, ReportOptions Opts) {
  248. // TODO: Add deduplication once a SourceLocation is generated for this check.
  249. Location Loc = getCallerLocation();
  250. ScopedReport R(Opts, Loc);
  251. Diag(Loc, DL_Error,
  252. "value %0 is outside the range of representable values of type %2")
  253. << Value(Data->FromType, From) << Data->FromType << Data->ToType;
  254. }
  255. void __ubsan::__ubsan_handle_float_cast_overflow(FloatCastOverflowData *Data,
  256. ValueHandle From) {
  257. GET_REPORT_OPTIONS(false);
  258. handleFloatCastOverflow(Data, From, Opts);
  259. }
  260. void
  261. __ubsan::__ubsan_handle_float_cast_overflow_abort(FloatCastOverflowData *Data,
  262. ValueHandle From) {
  263. GET_REPORT_OPTIONS(true);
  264. handleFloatCastOverflow(Data, From, Opts);
  265. Die();
  266. }
  267. static void handleLoadInvalidValue(InvalidValueData *Data, ValueHandle Val,
  268. ReportOptions Opts) {
  269. SourceLocation Loc = Data->Loc.acquire();
  270. if (ignoreReport(Loc, Opts))
  271. return;
  272. ScopedReport R(Opts, Loc);
  273. Diag(Loc, DL_Error,
  274. "load of value %0, which is not a valid value for type %1")
  275. << Value(Data->Type, Val) << Data->Type;
  276. }
  277. void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data,
  278. ValueHandle Val) {
  279. GET_REPORT_OPTIONS(false);
  280. handleLoadInvalidValue(Data, Val, Opts);
  281. }
  282. void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data,
  283. ValueHandle Val) {
  284. GET_REPORT_OPTIONS(true);
  285. handleLoadInvalidValue(Data, Val, Opts);
  286. Die();
  287. }
  288. static void handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
  289. ValueHandle Function,
  290. ReportOptions Opts) {
  291. const char *FName = "(unknown)";
  292. Location Loc = getFunctionLocation(Function, &FName);
  293. ScopedReport R(Opts, Loc);
  294. Diag(Data->Loc, DL_Error,
  295. "call to function %0 through pointer to incorrect function type %1")
  296. << FName << Data->Type;
  297. Diag(Loc, DL_Note, "%0 defined here") << FName;
  298. }
  299. void
  300. __ubsan::__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,
  301. ValueHandle Function) {
  302. GET_REPORT_OPTIONS(false);
  303. handleFunctionTypeMismatch(Data, Function, Opts);
  304. }
  305. void __ubsan::__ubsan_handle_function_type_mismatch_abort(
  306. FunctionTypeMismatchData *Data, ValueHandle Function) {
  307. GET_REPORT_OPTIONS(true);
  308. handleFunctionTypeMismatch(Data, Function, Opts);
  309. Die();
  310. }
  311. static void handleNonNullReturn(NonNullReturnData *Data, ReportOptions Opts) {
  312. SourceLocation Loc = Data->Loc.acquire();
  313. if (ignoreReport(Loc, Opts))
  314. return;
  315. ScopedReport R(Opts, Loc);
  316. Diag(Loc, DL_Error, "null pointer returned from function declared to never "
  317. "return null");
  318. if (!Data->AttrLoc.isInvalid())
  319. Diag(Data->AttrLoc, DL_Note, "returns_nonnull attribute specified here");
  320. }
  321. void __ubsan::__ubsan_handle_nonnull_return(NonNullReturnData *Data) {
  322. GET_REPORT_OPTIONS(false);
  323. handleNonNullReturn(Data, Opts);
  324. }
  325. void __ubsan::__ubsan_handle_nonnull_return_abort(NonNullReturnData *Data) {
  326. GET_REPORT_OPTIONS(true);
  327. handleNonNullReturn(Data, Opts);
  328. Die();
  329. }
  330. static void handleNonNullArg(NonNullArgData *Data, ReportOptions Opts) {
  331. SourceLocation Loc = Data->Loc.acquire();
  332. if (ignoreReport(Loc, Opts))
  333. return;
  334. ScopedReport R(Opts, Loc);
  335. Diag(Loc, DL_Error, "null pointer passed as argument %0, which is declared to "
  336. "never be null") << Data->ArgIndex;
  337. if (!Data->AttrLoc.isInvalid())
  338. Diag(Data->AttrLoc, DL_Note, "nonnull attribute specified here");
  339. }
  340. void __ubsan::__ubsan_handle_nonnull_arg(NonNullArgData *Data) {
  341. GET_REPORT_OPTIONS(false);
  342. handleNonNullArg(Data, Opts);
  343. }
  344. void __ubsan::__ubsan_handle_nonnull_arg_abort(NonNullArgData *Data) {
  345. GET_REPORT_OPTIONS(true);
  346. handleNonNullArg(Data, Opts);
  347. Die();
  348. }