1 #include "sanitizer_common/sanitizer_atomic.h"
9 extern "C" void ubsan_message(const char *msg);
10 static void message(const char *msg) { ubsan_message(msg); }
12 static void message(const char *msg) {
13 write(2, msg, strlen(msg));
17 static const int kMaxCallerPcs = 20;
18 static __sanitizer::atomic_uintptr_t caller_pcs[kMaxCallerPcs];
19 // Number of elements in caller_pcs. A special value of kMaxCallerPcs + 1 means
20 // that "too many errors" has already been reported.
21 static __sanitizer::atomic_uint32_t caller_pcs_sz;
23 __attribute__((noinline)) static bool report_this_error(void *caller_p) {
24 uintptr_t caller = reinterpret_cast<uintptr_t>(caller_p);
25 if (caller == 0) return false;
27 unsigned sz = __sanitizer::atomic_load_relaxed(&caller_pcs_sz);
28 if (sz > kMaxCallerPcs) return false; // early exit
29 // when sz==kMaxCallerPcs print "too many errors", but only when cmpxchg
30 // succeeds in order to not print it multiple times.
31 if (sz > 0 && sz < kMaxCallerPcs) {
33 for (unsigned i = 0; i < sz; ++i) {
34 p = __sanitizer::atomic_load_relaxed(&caller_pcs[i]);
35 if (p == 0) break; // Concurrent update.
36 if (p == caller) return false;
38 if (p == 0) continue; // FIXME: yield?
41 if (!__sanitizer::atomic_compare_exchange_strong(
42 &caller_pcs_sz, &sz, sz + 1, __sanitizer::memory_order_seq_cst))
43 continue; // Concurrent update! Try again from the start.
45 if (sz == kMaxCallerPcs) {
46 message("ubsan: too many errors\n");
49 __sanitizer::atomic_store_relaxed(&caller_pcs[sz], caller);
54 #if defined(__ANDROID__)
55 extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
56 static void abort_with_message(const char *msg) {
57 if (&android_set_abort_message) android_set_abort_message(msg);
61 static void abort_with_message(const char *) { abort(); }
65 namespace __sanitizer {
66 // The DCHECK macro needs this symbol to be defined.
67 void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {
68 message("Sanitizer CHECK failed: ");
70 message(":?? : "); // FIXME: Show line number.
74 } // namespace __sanitizer
77 #define INTERFACE extern "C" __attribute__((visibility("default")))
79 // FIXME: add caller pc to the error message (possibly as "ubsan: error-type
81 #define HANDLER_RECOVER(name, msg) \
82 INTERFACE void __ubsan_handle_##name##_minimal() { \
83 if (!report_this_error(__builtin_return_address(0))) return; \
84 message("ubsan: " msg "\n"); \
87 #define HANDLER_NORECOVER(name, msg) \
88 INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
89 message("ubsan: " msg "\n"); \
90 abort_with_message("ubsan: " msg); \
93 #define HANDLER(name, msg) \
94 HANDLER_RECOVER(name, msg) \
95 HANDLER_NORECOVER(name, msg)
97 HANDLER(type_mismatch, "type-mismatch")
98 HANDLER(alignment_assumption, "alignment-assumption")
99 HANDLER(add_overflow, "add-overflow")
100 HANDLER(sub_overflow, "sub-overflow")
101 HANDLER(mul_overflow, "mul-overflow")
102 HANDLER(negate_overflow, "negate-overflow")
103 HANDLER(divrem_overflow, "divrem-overflow")
104 HANDLER(shift_out_of_bounds, "shift-out-of-bounds")
105 HANDLER(out_of_bounds, "out-of-bounds")
106 HANDLER_RECOVER(builtin_unreachable, "builtin-unreachable")
107 HANDLER_RECOVER(missing_return, "missing-return")
108 HANDLER(vla_bound_not_positive, "vla-bound-not-positive")
109 HANDLER(float_cast_overflow, "float-cast-overflow")
110 HANDLER(load_invalid_value, "load-invalid-value")
111 HANDLER(invalid_builtin, "invalid-builtin")
112 HANDLER(function_type_mismatch, "function-type-mismatch")
113 HANDLER(implicit_conversion, "implicit-conversion")
114 HANDLER(nonnull_arg, "nonnull-arg")
115 HANDLER(nonnull_return, "nonnull-return")
116 HANDLER(nullability_arg, "nullability-arg")
117 HANDLER(nullability_return, "nullability-return")
118 HANDLER(pointer_overflow, "pointer-overflow")
119 HANDLER(cfi_check_fail, "cfi-check-fail")