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 (void)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(uintptr_t caller
) {
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 __attribute__((noinline
)) static void decorate_msg(char *buf
,
56 // print the address by nibbles
57 for (unsigned shift
= sizeof(uintptr_t) * 8; shift
;) {
59 unsigned nibble
= (caller
>> shift
) & 0xf;
60 *(buf
++) = nibble
< 10 ? nibble
+ '0' : nibble
- 10 + 'a';
67 #if defined(__ANDROID__)
68 extern "C" __attribute__((weak
)) void android_set_abort_message(const char *);
69 static void abort_with_message(const char *msg
) {
70 if (&android_set_abort_message
) android_set_abort_message(msg
);
74 static void abort_with_message(const char *) { abort(); }
78 namespace __sanitizer
{
79 // The DCHECK macro needs this symbol to be defined.
80 void NORETURN
CheckFailed(const char *file
, int, const char *cond
, u64
, u64
) {
81 message("Sanitizer CHECK failed: ");
83 message(":?? : "); // FIXME: Show line number.
87 } // namespace __sanitizer
90 #define INTERFACE extern "C" __attribute__((visibility("default")))
92 // How many chars we need to reserve to print an address.
93 constexpr unsigned kAddrBuf
= SANITIZER_WORDSIZE
/ 4;
94 #define MSG_TMPL(msg) "ubsan: " msg " by 0x"
95 #define MSG_TMPL_END(buf, msg) (buf + sizeof(MSG_TMPL(msg)) - 1)
96 // Reserve an additional byte for '\n'.
97 #define MSG_BUF_LEN(msg) (sizeof(MSG_TMPL(msg)) + kAddrBuf + 1)
99 #define HANDLER_RECOVER(name, msg) \
100 INTERFACE void __ubsan_handle_##name##_minimal() { \
101 uintptr_t caller = GET_CALLER_PC(); \
102 if (!report_this_error(caller)) return; \
103 char msg_buf[MSG_BUF_LEN(msg)] = MSG_TMPL(msg); \
104 decorate_msg(MSG_TMPL_END(msg_buf, msg), caller); \
108 #define HANDLER_NORECOVER(name, msg) \
109 INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
110 char msg_buf[MSG_BUF_LEN(msg)] = MSG_TMPL(msg); \
111 decorate_msg(MSG_TMPL_END(msg_buf, msg), GET_CALLER_PC()); \
113 abort_with_message(msg_buf); \
116 #define HANDLER(name, msg) \
117 HANDLER_RECOVER(name, msg) \
118 HANDLER_NORECOVER(name, msg)
120 HANDLER(type_mismatch
, "type-mismatch")
121 HANDLER(alignment_assumption
, "alignment-assumption")
122 HANDLER(add_overflow
, "add-overflow")
123 HANDLER(sub_overflow
, "sub-overflow")
124 HANDLER(mul_overflow
, "mul-overflow")
125 HANDLER(negate_overflow
, "negate-overflow")
126 HANDLER(divrem_overflow
, "divrem-overflow")
127 HANDLER(shift_out_of_bounds
, "shift-out-of-bounds")
128 HANDLER(out_of_bounds
, "out-of-bounds")
129 HANDLER_RECOVER(builtin_unreachable
, "builtin-unreachable")
130 HANDLER_RECOVER(missing_return
, "missing-return")
131 HANDLER(vla_bound_not_positive
, "vla-bound-not-positive")
132 HANDLER(float_cast_overflow
, "float-cast-overflow")
133 HANDLER(load_invalid_value
, "load-invalid-value")
134 HANDLER(invalid_builtin
, "invalid-builtin")
135 HANDLER(invalid_objc_cast
, "invalid-objc-cast")
136 HANDLER(function_type_mismatch
, "function-type-mismatch")
137 HANDLER(implicit_conversion
, "implicit-conversion")
138 HANDLER(nonnull_arg
, "nonnull-arg")
139 HANDLER(nonnull_return
, "nonnull-return")
140 HANDLER(nullability_arg
, "nullability-arg")
141 HANDLER(nullability_return
, "nullability-return")
142 HANDLER(pointer_overflow
, "pointer-overflow")
143 HANDLER(cfi_check_fail
, "cfi-check-fail")