ubsan_minimal_handlers.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "sanitizer_common/sanitizer_atomic.h"
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #ifdef KERNEL_USE
  7. extern "C" void ubsan_message(const char *msg);
  8. static void message(const char *msg) { ubsan_message(msg); }
  9. #else
  10. static void message(const char *msg) {
  11. (void)write(2, msg, strlen(msg));
  12. }
  13. #endif
  14. static const int kMaxCallerPcs = 20;
  15. static __sanitizer::atomic_uintptr_t caller_pcs[kMaxCallerPcs];
  16. // Number of elements in caller_pcs. A special value of kMaxCallerPcs + 1 means
  17. // that "too many errors" has already been reported.
  18. static __sanitizer::atomic_uint32_t caller_pcs_sz;
  19. __attribute__((noinline)) static bool report_this_error(void *caller_p) {
  20. uintptr_t caller = reinterpret_cast<uintptr_t>(caller_p);
  21. if (caller == 0) return false;
  22. while (true) {
  23. unsigned sz = __sanitizer::atomic_load_relaxed(&caller_pcs_sz);
  24. if (sz > kMaxCallerPcs) return false; // early exit
  25. // when sz==kMaxCallerPcs print "too many errors", but only when cmpxchg
  26. // succeeds in order to not print it multiple times.
  27. if (sz > 0 && sz < kMaxCallerPcs) {
  28. uintptr_t p;
  29. for (unsigned i = 0; i < sz; ++i) {
  30. p = __sanitizer::atomic_load_relaxed(&caller_pcs[i]);
  31. if (p == 0) break; // Concurrent update.
  32. if (p == caller) return false;
  33. }
  34. if (p == 0) continue; // FIXME: yield?
  35. }
  36. if (!__sanitizer::atomic_compare_exchange_strong(
  37. &caller_pcs_sz, &sz, sz + 1, __sanitizer::memory_order_seq_cst))
  38. continue; // Concurrent update! Try again from the start.
  39. if (sz == kMaxCallerPcs) {
  40. message("ubsan: too many errors\n");
  41. return false;
  42. }
  43. __sanitizer::atomic_store_relaxed(&caller_pcs[sz], caller);
  44. return true;
  45. }
  46. }
  47. #if defined(__ANDROID__)
  48. extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
  49. static void abort_with_message(const char *msg) {
  50. if (&android_set_abort_message) android_set_abort_message(msg);
  51. abort();
  52. }
  53. #else
  54. static void abort_with_message(const char *) { abort(); }
  55. #endif
  56. #if SANITIZER_DEBUG
  57. namespace __sanitizer {
  58. // The DCHECK macro needs this symbol to be defined.
  59. void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {
  60. message("Sanitizer CHECK failed: ");
  61. message(file);
  62. message(":?? : "); // FIXME: Show line number.
  63. message(cond);
  64. abort();
  65. }
  66. } // namespace __sanitizer
  67. #endif
  68. #define INTERFACE extern "C" __attribute__((visibility("default")))
  69. // FIXME: add caller pc to the error message (possibly as "ubsan: error-type
  70. // @1234ABCD").
  71. #define HANDLER_RECOVER(name, msg) \
  72. INTERFACE void __ubsan_handle_##name##_minimal() { \
  73. if (!report_this_error(__builtin_return_address(0))) return; \
  74. message("ubsan: " msg "\n"); \
  75. }
  76. #define HANDLER_NORECOVER(name, msg) \
  77. INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
  78. message("ubsan: " msg "\n"); \
  79. abort_with_message("ubsan: " msg); \
  80. }
  81. #define HANDLER(name, msg) \
  82. HANDLER_RECOVER(name, msg) \
  83. HANDLER_NORECOVER(name, msg)
  84. HANDLER(type_mismatch, "type-mismatch")
  85. HANDLER(alignment_assumption, "alignment-assumption")
  86. HANDLER(add_overflow, "add-overflow")
  87. HANDLER(sub_overflow, "sub-overflow")
  88. HANDLER(mul_overflow, "mul-overflow")
  89. HANDLER(negate_overflow, "negate-overflow")
  90. HANDLER(divrem_overflow, "divrem-overflow")
  91. HANDLER(shift_out_of_bounds, "shift-out-of-bounds")
  92. HANDLER(out_of_bounds, "out-of-bounds")
  93. HANDLER_RECOVER(builtin_unreachable, "builtin-unreachable")
  94. HANDLER_RECOVER(missing_return, "missing-return")
  95. HANDLER(vla_bound_not_positive, "vla-bound-not-positive")
  96. HANDLER(float_cast_overflow, "float-cast-overflow")
  97. HANDLER(load_invalid_value, "load-invalid-value")
  98. HANDLER(invalid_builtin, "invalid-builtin")
  99. HANDLER(invalid_objc_cast, "invalid-objc-cast")
  100. HANDLER(function_type_mismatch, "function-type-mismatch")
  101. HANDLER(implicit_conversion, "implicit-conversion")
  102. HANDLER(nonnull_arg, "nonnull-arg")
  103. HANDLER(nonnull_return, "nonnull-return")
  104. HANDLER(nullability_arg, "nullability-arg")
  105. HANDLER(nullability_return, "nullability-return")
  106. HANDLER(pointer_overflow, "pointer-overflow")
  107. HANDLER(cfi_check_fail, "cfi-check-fail")