ubsan_minimal_handlers.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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(uintptr_t caller) {
  20. if (caller == 0)
  21. 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. __attribute__((noinline)) static void decorate_msg(char *buf,
  48. uintptr_t caller) {
  49. // print the address by nibbles
  50. for (unsigned shift = sizeof(uintptr_t) * 8; shift;) {
  51. shift -= 4;
  52. unsigned nibble = (caller >> shift) & 0xf;
  53. *(buf++) = nibble < 10 ? nibble + '0' : nibble - 10 + 'a';
  54. }
  55. // finish the message
  56. buf[0] = '\n';
  57. buf[1] = '\0';
  58. }
  59. #if defined(__ANDROID__)
  60. extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
  61. static void abort_with_message(const char *msg) {
  62. if (&android_set_abort_message) android_set_abort_message(msg);
  63. abort();
  64. }
  65. #else
  66. static void abort_with_message(const char *) { abort(); }
  67. #endif
  68. #if SANITIZER_DEBUG
  69. namespace __sanitizer {
  70. // The DCHECK macro needs this symbol to be defined.
  71. void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {
  72. message("Sanitizer CHECK failed: ");
  73. message(file);
  74. message(":?? : "); // FIXME: Show line number.
  75. message(cond);
  76. abort();
  77. }
  78. } // namespace __sanitizer
  79. #endif
  80. #define INTERFACE extern "C" __attribute__((visibility("default")))
  81. // How many chars we need to reserve to print an address.
  82. constexpr unsigned kAddrBuf = SANITIZER_WORDSIZE / 4;
  83. #define MSG_TMPL(msg) "ubsan: " msg " by 0x"
  84. #define MSG_TMPL_END(buf, msg) (buf + sizeof(MSG_TMPL(msg)) - 1)
  85. // Reserve an additional byte for '\n'.
  86. #define MSG_BUF_LEN(msg) (sizeof(MSG_TMPL(msg)) + kAddrBuf + 1)
  87. #define HANDLER_RECOVER(name, msg) \
  88. INTERFACE void __ubsan_handle_##name##_minimal() { \
  89. uintptr_t caller = GET_CALLER_PC(); \
  90. if (!report_this_error(caller)) return; \
  91. char msg_buf[MSG_BUF_LEN(msg)] = MSG_TMPL(msg); \
  92. decorate_msg(MSG_TMPL_END(msg_buf, msg), caller); \
  93. message(msg_buf); \
  94. }
  95. #define HANDLER_NORECOVER(name, msg) \
  96. INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
  97. char msg_buf[MSG_BUF_LEN(msg)] = MSG_TMPL(msg); \
  98. decorate_msg(MSG_TMPL_END(msg_buf, msg), GET_CALLER_PC()); \
  99. message(msg_buf); \
  100. abort_with_message(msg_buf); \
  101. }
  102. #define HANDLER(name, msg) \
  103. HANDLER_RECOVER(name, msg) \
  104. HANDLER_NORECOVER(name, msg)
  105. HANDLER(type_mismatch, "type-mismatch")
  106. HANDLER(alignment_assumption, "alignment-assumption")
  107. HANDLER(add_overflow, "add-overflow")
  108. HANDLER(sub_overflow, "sub-overflow")
  109. HANDLER(mul_overflow, "mul-overflow")
  110. HANDLER(negate_overflow, "negate-overflow")
  111. HANDLER(divrem_overflow, "divrem-overflow")
  112. HANDLER(shift_out_of_bounds, "shift-out-of-bounds")
  113. HANDLER(out_of_bounds, "out-of-bounds")
  114. HANDLER_RECOVER(builtin_unreachable, "builtin-unreachable")
  115. HANDLER_RECOVER(missing_return, "missing-return")
  116. HANDLER(vla_bound_not_positive, "vla-bound-not-positive")
  117. HANDLER(float_cast_overflow, "float-cast-overflow")
  118. HANDLER(load_invalid_value, "load-invalid-value")
  119. HANDLER(invalid_builtin, "invalid-builtin")
  120. HANDLER(invalid_objc_cast, "invalid-objc-cast")
  121. HANDLER(function_type_mismatch, "function-type-mismatch")
  122. HANDLER(implicit_conversion, "implicit-conversion")
  123. HANDLER(nonnull_arg, "nonnull-arg")
  124. HANDLER(nonnull_return, "nonnull-return")
  125. HANDLER(nullability_arg, "nullability-arg")
  126. HANDLER(nullability_return, "nullability-return")
  127. HANDLER(pointer_overflow, "pointer-overflow")
  128. HANDLER(cfi_check_fail, "cfi-check-fail")