Path: blob/main/contrib/llvm-project/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
35235 views
#include "sanitizer_common/sanitizer_atomic.h"12#include <stdlib.h>3#include <stdint.h>4#include <string.h>5#include <unistd.h>67#ifdef KERNEL_USE8extern "C" void ubsan_message(const char *msg);9static void message(const char *msg) { ubsan_message(msg); }10#else11static void message(const char *msg) {12(void)write(2, msg, strlen(msg));13}14#endif1516static const int kMaxCallerPcs = 20;17static __sanitizer::atomic_uintptr_t caller_pcs[kMaxCallerPcs];18// Number of elements in caller_pcs. A special value of kMaxCallerPcs + 1 means19// that "too many errors" has already been reported.20static __sanitizer::atomic_uint32_t caller_pcs_sz;2122__attribute__((noinline)) static bool report_this_error(uintptr_t caller) {23if (caller == 0)24return false;25while (true) {26unsigned sz = __sanitizer::atomic_load_relaxed(&caller_pcs_sz);27if (sz > kMaxCallerPcs) return false; // early exit28// when sz==kMaxCallerPcs print "too many errors", but only when cmpxchg29// succeeds in order to not print it multiple times.30if (sz > 0 && sz < kMaxCallerPcs) {31uintptr_t p;32for (unsigned i = 0; i < sz; ++i) {33p = __sanitizer::atomic_load_relaxed(&caller_pcs[i]);34if (p == 0) break; // Concurrent update.35if (p == caller) return false;36}37if (p == 0) continue; // FIXME: yield?38}3940if (!__sanitizer::atomic_compare_exchange_strong(41&caller_pcs_sz, &sz, sz + 1, __sanitizer::memory_order_seq_cst))42continue; // Concurrent update! Try again from the start.4344if (sz == kMaxCallerPcs) {45message("ubsan: too many errors\n");46return false;47}48__sanitizer::atomic_store_relaxed(&caller_pcs[sz], caller);49return true;50}51}5253__attribute__((noinline)) static void decorate_msg(char *buf,54uintptr_t caller) {55// print the address by nibbles56for (unsigned shift = sizeof(uintptr_t) * 8; shift;) {57shift -= 4;58unsigned nibble = (caller >> shift) & 0xf;59*(buf++) = nibble < 10 ? nibble + '0' : nibble - 10 + 'a';60}61// finish the message62buf[0] = '\n';63buf[1] = '\0';64}6566#if defined(__ANDROID__)67extern "C" __attribute__((weak)) void android_set_abort_message(const char *);68static void abort_with_message(const char *msg) {69if (&android_set_abort_message) android_set_abort_message(msg);70abort();71}72#else73static void abort_with_message(const char *) { abort(); }74#endif7576#if SANITIZER_DEBUG77namespace __sanitizer {78// The DCHECK macro needs this symbol to be defined.79void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {80message("Sanitizer CHECK failed: ");81message(file);82message(":?? : "); // FIXME: Show line number.83message(cond);84abort();85}86} // namespace __sanitizer87#endif8889#define INTERFACE extern "C" __attribute__((visibility("default")))9091// How many chars we need to reserve to print an address.92constexpr unsigned kAddrBuf = SANITIZER_WORDSIZE / 4;93#define MSG_TMPL(msg) "ubsan: " msg " by 0x"94#define MSG_TMPL_END(buf, msg) (buf + sizeof(MSG_TMPL(msg)) - 1)95// Reserve an additional byte for '\n'.96#define MSG_BUF_LEN(msg) (sizeof(MSG_TMPL(msg)) + kAddrBuf + 1)9798#define HANDLER_RECOVER(name, msg) \99INTERFACE void __ubsan_handle_##name##_minimal() { \100uintptr_t caller = GET_CALLER_PC(); \101if (!report_this_error(caller)) return; \102char msg_buf[MSG_BUF_LEN(msg)] = MSG_TMPL(msg); \103decorate_msg(MSG_TMPL_END(msg_buf, msg), caller); \104message(msg_buf); \105}106107#define HANDLER_NORECOVER(name, msg) \108INTERFACE void __ubsan_handle_##name##_minimal_abort() { \109char msg_buf[MSG_BUF_LEN(msg)] = MSG_TMPL(msg); \110decorate_msg(MSG_TMPL_END(msg_buf, msg), GET_CALLER_PC()); \111message(msg_buf); \112abort_with_message(msg_buf); \113}114115#define HANDLER(name, msg) \116HANDLER_RECOVER(name, msg) \117HANDLER_NORECOVER(name, msg)118119HANDLER(type_mismatch, "type-mismatch")120HANDLER(alignment_assumption, "alignment-assumption")121HANDLER(add_overflow, "add-overflow")122HANDLER(sub_overflow, "sub-overflow")123HANDLER(mul_overflow, "mul-overflow")124HANDLER(negate_overflow, "negate-overflow")125HANDLER(divrem_overflow, "divrem-overflow")126HANDLER(shift_out_of_bounds, "shift-out-of-bounds")127HANDLER(out_of_bounds, "out-of-bounds")128HANDLER_RECOVER(builtin_unreachable, "builtin-unreachable")129HANDLER_RECOVER(missing_return, "missing-return")130HANDLER(vla_bound_not_positive, "vla-bound-not-positive")131HANDLER(float_cast_overflow, "float-cast-overflow")132HANDLER(load_invalid_value, "load-invalid-value")133HANDLER(invalid_builtin, "invalid-builtin")134HANDLER(invalid_objc_cast, "invalid-objc-cast")135HANDLER(function_type_mismatch, "function-type-mismatch")136HANDLER(implicit_conversion, "implicit-conversion")137HANDLER(nonnull_arg, "nonnull-arg")138HANDLER(nonnull_return, "nonnull-return")139HANDLER(nullability_arg, "nullability-arg")140HANDLER(nullability_return, "nullability-return")141HANDLER(pointer_overflow, "pointer-overflow")142HANDLER(cfi_check_fail, "cfi-check-fail")143144145