2017-11-11 10:32:02 +08:00
|
|
|
#include "sanitizer_common/sanitizer_atomic.h"
|
|
|
|
|
2017-08-30 04:03:51 +08:00
|
|
|
#include <stdlib.h>
|
2017-11-11 10:32:02 +08:00
|
|
|
#include <stdint.h>
|
2017-08-30 04:03:51 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-12-16 01:30:50 +08:00
|
|
|
#ifdef KERNEL_USE
|
|
|
|
extern "C" void ubsan_message(const char *msg);
|
|
|
|
static void message(const char *msg) { ubsan_message(msg); }
|
|
|
|
#else
|
2017-08-30 04:03:51 +08:00
|
|
|
static void message(const char *msg) {
|
2020-10-13 00:57:12 +08:00
|
|
|
(void)write(2, msg, strlen(msg));
|
2017-08-30 04:03:51 +08:00
|
|
|
}
|
2017-12-16 01:30:50 +08:00
|
|
|
#endif
|
2017-08-30 04:03:51 +08:00
|
|
|
|
|
|
|
static const int kMaxCallerPcs = 20;
|
2017-11-11 10:32:02 +08:00
|
|
|
static __sanitizer::atomic_uintptr_t caller_pcs[kMaxCallerPcs];
|
2017-08-30 04:03:51 +08:00
|
|
|
// Number of elements in caller_pcs. A special value of kMaxCallerPcs + 1 means
|
|
|
|
// that "too many errors" has already been reported.
|
2017-11-11 10:32:02 +08:00
|
|
|
static __sanitizer::atomic_uint32_t caller_pcs_sz;
|
2017-08-30 04:03:51 +08:00
|
|
|
|
2017-11-11 10:32:02 +08:00
|
|
|
__attribute__((noinline)) static bool report_this_error(void *caller_p) {
|
|
|
|
uintptr_t caller = reinterpret_cast<uintptr_t>(caller_p);
|
|
|
|
if (caller == 0) return false;
|
2017-08-30 04:03:51 +08:00
|
|
|
while (true) {
|
2017-11-11 10:32:02 +08:00
|
|
|
unsigned sz = __sanitizer::atomic_load_relaxed(&caller_pcs_sz);
|
|
|
|
if (sz > kMaxCallerPcs) return false; // early exit
|
2017-08-30 04:03:51 +08:00
|
|
|
// when sz==kMaxCallerPcs print "too many errors", but only when cmpxchg
|
|
|
|
// succeeds in order to not print it multiple times.
|
|
|
|
if (sz > 0 && sz < kMaxCallerPcs) {
|
2017-11-11 10:32:02 +08:00
|
|
|
uintptr_t p;
|
|
|
|
for (unsigned i = 0; i < sz; ++i) {
|
|
|
|
p = __sanitizer::atomic_load_relaxed(&caller_pcs[i]);
|
|
|
|
if (p == 0) break; // Concurrent update.
|
2017-08-30 04:03:51 +08:00
|
|
|
if (p == caller) return false;
|
|
|
|
}
|
2017-11-11 10:32:02 +08:00
|
|
|
if (p == 0) continue; // FIXME: yield?
|
2017-08-30 04:03:51 +08:00
|
|
|
}
|
|
|
|
|
2017-11-11 10:32:02 +08:00
|
|
|
if (!__sanitizer::atomic_compare_exchange_strong(
|
|
|
|
&caller_pcs_sz, &sz, sz + 1, __sanitizer::memory_order_seq_cst))
|
|
|
|
continue; // Concurrent update! Try again from the start.
|
2017-08-30 04:03:51 +08:00
|
|
|
|
|
|
|
if (sz == kMaxCallerPcs) {
|
|
|
|
message("ubsan: too many errors\n");
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-11 10:32:02 +08:00
|
|
|
__sanitizer::atomic_store_relaxed(&caller_pcs[sz], caller);
|
2017-08-30 04:03:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__ANDROID__)
|
2017-09-12 07:27:58 +08:00
|
|
|
extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
|
2017-08-30 04:03:51 +08:00
|
|
|
static void abort_with_message(const char *msg) {
|
2017-09-12 07:27:58 +08:00
|
|
|
if (&android_set_abort_message) android_set_abort_message(msg);
|
2017-08-30 04:03:51 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void abort_with_message(const char *) { abort(); }
|
|
|
|
#endif
|
|
|
|
|
2018-02-24 21:14:44 +08:00
|
|
|
#if SANITIZER_DEBUG
|
|
|
|
namespace __sanitizer {
|
|
|
|
// The DCHECK macro needs this symbol to be defined.
|
|
|
|
void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {
|
|
|
|
message("Sanitizer CHECK failed: ");
|
|
|
|
message(file);
|
|
|
|
message(":?? : "); // FIXME: Show line number.
|
|
|
|
message(cond);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} // namespace __sanitizer
|
|
|
|
#endif
|
|
|
|
|
2017-08-30 04:03:51 +08:00
|
|
|
#define INTERFACE extern "C" __attribute__((visibility("default")))
|
|
|
|
|
|
|
|
// FIXME: add caller pc to the error message (possibly as "ubsan: error-type
|
|
|
|
// @1234ABCD").
|
2017-09-19 14:46:36 +08:00
|
|
|
#define HANDLER_RECOVER(name, msg) \
|
2017-08-30 04:03:51 +08:00
|
|
|
INTERFACE void __ubsan_handle_##name##_minimal() { \
|
|
|
|
if (!report_this_error(__builtin_return_address(0))) return; \
|
|
|
|
message("ubsan: " msg "\n"); \
|
2017-09-19 14:46:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define HANDLER_NORECOVER(name, msg) \
|
2017-08-30 04:03:51 +08:00
|
|
|
INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
|
|
|
|
message("ubsan: " msg "\n"); \
|
|
|
|
abort_with_message("ubsan: " msg); \
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:46:36 +08:00
|
|
|
#define HANDLER(name, msg) \
|
|
|
|
HANDLER_RECOVER(name, msg) \
|
|
|
|
HANDLER_NORECOVER(name, msg)
|
|
|
|
|
2017-08-30 04:03:51 +08:00
|
|
|
HANDLER(type_mismatch, "type-mismatch")
|
[compiler-rt][UBSan] Sanitization for alignment assumptions.
Summary:
This is the compiler-rt part.
The clang part is D54589.
This is a second commit, the original one was r351106,
which was mass-reverted in r351159 because 2 compiler-rt tests were failing.
Now, i have fundamentally changed the testing approach:
i malloc a few bytes, intentionally mis-align the pointer
(increment it by one), and check that. Also, i have decreased
the expected alignment. This hopefully should be enough to pacify
all the bots. If not, i guess i might just drop the two 'bad' tests.
Reviewers: filcab, vsk, #sanitizers, vitalybuka, rsmith, morehouse
Reviewed By: morehouse
Subscribers: rjmccall, krytarowski, rsmith, kcc, srhines, kubamracek, dberris, llvm-commits
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D54590
llvm-svn: 351178
2019-01-15 17:44:27 +08:00
|
|
|
HANDLER(alignment_assumption, "alignment-assumption")
|
2017-08-30 04:03:51 +08:00
|
|
|
HANDLER(add_overflow, "add-overflow")
|
|
|
|
HANDLER(sub_overflow, "sub-overflow")
|
|
|
|
HANDLER(mul_overflow, "mul-overflow")
|
|
|
|
HANDLER(negate_overflow, "negate-overflow")
|
|
|
|
HANDLER(divrem_overflow, "divrem-overflow")
|
|
|
|
HANDLER(shift_out_of_bounds, "shift-out-of-bounds")
|
|
|
|
HANDLER(out_of_bounds, "out-of-bounds")
|
2017-09-19 14:46:36 +08:00
|
|
|
HANDLER_RECOVER(builtin_unreachable, "builtin-unreachable")
|
|
|
|
HANDLER_RECOVER(missing_return, "missing-return")
|
2017-08-30 04:03:51 +08:00
|
|
|
HANDLER(vla_bound_not_positive, "vla-bound-not-positive")
|
|
|
|
HANDLER(float_cast_overflow, "float-cast-overflow")
|
|
|
|
HANDLER(load_invalid_value, "load-invalid-value")
|
|
|
|
HANDLER(invalid_builtin, "invalid-builtin")
|
2019-12-14 04:59:40 +08:00
|
|
|
HANDLER(invalid_objc_cast, "invalid-objc-cast")
|
2017-08-30 04:03:51 +08:00
|
|
|
HANDLER(function_type_mismatch, "function-type-mismatch")
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
HANDLER(implicit_conversion, "implicit-conversion")
|
2017-08-30 04:03:51 +08:00
|
|
|
HANDLER(nonnull_arg, "nonnull-arg")
|
2017-09-19 14:46:36 +08:00
|
|
|
HANDLER(nonnull_return, "nonnull-return")
|
2017-08-30 04:03:51 +08:00
|
|
|
HANDLER(nullability_arg, "nullability-arg")
|
2017-09-19 14:46:36 +08:00
|
|
|
HANDLER(nullability_return, "nullability-return")
|
2017-08-30 04:03:51 +08:00
|
|
|
HANDLER(pointer_overflow, "pointer-overflow")
|
|
|
|
HANDLER(cfi_check_fail, "cfi-check-fail")
|