Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3900,18 +3900,8 @@ void CodeGenFunction::EmitCheck(

// Clear arguments for the MinimalRuntime handler.
if (CGM.getCodeGenOpts().SanitizeMinimalRuntime) {
switch (CheckHandler) {
case SanitizerHandler::TypeMismatch:
// Pass value pointer only. It adds minimal overhead.
StaticArgs = {};
assert(DynamicArgs.size() == 1);
break;
default:
// No arguments for other checks.
StaticArgs = {};
DynamicArgs = {};
break;
}
StaticArgs = {};
DynamicArgs = {};
}

// Handler functions take an i8* pointing to the (handler-specific) static
Expand Down
52 changes: 13 additions & 39 deletions compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,20 @@ static char *append_hex(uintptr_t d, char *buf, const char *end) {
return buf;
}

static void format_msg(const char *kind, uintptr_t caller,
const uintptr_t *address, char *buf, const char *end) {
static void format_msg(const char *kind, uintptr_t caller, char *buf,
const char *end) {
buf = append_str("ubsan: ", buf, end);
buf = append_str(kind, buf, end);
buf = append_str(" by 0x", buf, end);
buf = append_hex(caller, buf, end);
if (address) {
buf = append_str(" address 0x", buf, end);
buf = append_hex(*address, buf, end);
}
buf = append_str("\n", buf, end);
if (buf == end)
--buf; // Make sure we don't cause a buffer overflow.
*buf = '\0';
}

SANITIZER_INTERFACE_WEAK_DEF(void, __ubsan_report_error, const char *kind,
uintptr_t caller, const uintptr_t *address) {
uintptr_t caller) {
if (caller == 0)
return;
while (true) {
Expand Down Expand Up @@ -84,32 +80,28 @@ SANITIZER_INTERFACE_WEAK_DEF(void, __ubsan_report_error, const char *kind,
__sanitizer::atomic_store_relaxed(&caller_pcs[sz], caller);

char msg_buf[128];
format_msg(kind, caller, address, msg_buf, msg_buf + sizeof(msg_buf));
format_msg(kind, caller, msg_buf, msg_buf + sizeof(msg_buf));
message(msg_buf);
}
}

SANITIZER_INTERFACE_WEAK_DEF(void, __ubsan_report_error_fatal, const char *kind,
uintptr_t caller, const uintptr_t *address) {
uintptr_t caller) {
// Use another handlers, in case it's already overriden.
__ubsan_report_error(kind, caller, address);
__ubsan_report_error(kind, caller);
}

#if defined(__ANDROID__)
extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
static void abort_with_message(const char *kind, uintptr_t caller,
const uintptr_t *address) {
static void abort_with_message(const char *kind, uintptr_t caller) {
char msg_buf[128];
format_msg(kind, caller, address, msg_buf, msg_buf + sizeof(msg_buf));
format_msg(kind, caller, msg_buf, msg_buf + sizeof(msg_buf));
if (&android_set_abort_message)
android_set_abort_message(msg_buf);
abort();
}
#else
static void abort_with_message(const char *kind, uintptr_t caller,
const uintptr_t *address) {
abort();
}
static void abort_with_message(const char *kind, uintptr_t caller) { abort(); }
#endif

#if SANITIZER_DEBUG
Expand All @@ -129,39 +121,21 @@ void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {

#define HANDLER_RECOVER(name, kind) \
INTERFACE void __ubsan_handle_##name##_minimal() { \
__ubsan_report_error(kind, GET_CALLER_PC(), nullptr); \
__ubsan_report_error(kind, GET_CALLER_PC()); \
}

#define HANDLER_NORECOVER(name, kind) \
INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
uintptr_t caller = GET_CALLER_PC(); \
__ubsan_report_error_fatal(kind, caller, nullptr); \
abort_with_message(kind, caller, nullptr); \
__ubsan_report_error_fatal(kind, caller); \
abort_with_message(kind, caller); \
}

#define HANDLER(name, kind) \
HANDLER_RECOVER(name, kind) \
HANDLER_NORECOVER(name, kind)

#define HANDLER_RECOVER_PTR(name, kind) \
INTERFACE void __ubsan_handle_##name##_minimal(const uintptr_t address) { \
__ubsan_report_error(kind, GET_CALLER_PC(), &address); \
}

#define HANDLER_NORECOVER_PTR(name, kind) \
INTERFACE void __ubsan_handle_##name##_minimal_abort( \
const uintptr_t address) { \
uintptr_t caller = GET_CALLER_PC(); \
__ubsan_report_error_fatal(kind, caller, &address); \
abort_with_message(kind, caller, &address); \
}

// A version of a handler that takes a pointer to a value.
#define HANDLER_PTR(name, kind) \
HANDLER_RECOVER_PTR(name, kind) \
HANDLER_NORECOVER_PTR(name, kind)

HANDLER_PTR(type_mismatch, "type-mismatch")
HANDLER(type_mismatch, "type-mismatch")
HANDLER(alignment_assumption, "alignment-assumption")
HANDLER(add_overflow, "add-overflow")
HANDLER(sub_overflow, "sub-overflow")
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/ubsan_minimal/TestCases/misalignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int *t;
int main() {
int r;
t = (int *)(((char *)&r) + 1);
// CHECK: ubsan: type-mismatch by 0x{{[[:xdigit:]]+}} address 0x{{[[:xdigit:]]+$}}
// CHECK: ubsan: type-mismatch by 0x{{[[:xdigit:]]+}}
// CHECK-NOT: type-mismatch
f(*t);
}
2 changes: 1 addition & 1 deletion compiler-rt/test/ubsan_minimal/TestCases/null.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void f(int &n) {}
int *t;

int main() {
// CHECK: ubsan: type-mismatch by 0x{{[[:xdigit:]]+}} address 0x{{[[:xdigit:]]+$}}
// CHECK: ubsan: type-mismatch by 0x{{[[:xdigit:]]+}}
// CHECK-NOT: type-mismatch
f(*t);
}