Skip to content

Commit b029cd8

Browse files
committed
Refactored the code.
1 parent e26ffe0 commit b029cd8

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ static char *append_hex(uintptr_t d, char *buf, const char *end) {
3939
return buf;
4040
}
4141

42-
#if defined(__ANDROID__)
43-
extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
44-
static void abort_with_message(const char *msg) {
45-
if (&android_set_abort_message) android_set_abort_message(msg);
46-
abort();
42+
static void format_msg(const char *kind, uintptr_t caller, char *buf,
43+
const char *end) {
44+
buf = append_str(MSG_PREFIX, buf, end);
45+
buf = append_str(kind, buf, end);
46+
buf = append_str(MSG_SUFFIX, buf, end);
47+
buf = append_hex(caller, buf, end);
48+
buf = append_str("\n", buf, end);
49+
if (buf == end) --buf; // Make sure we don't cause a buffer overflow.
50+
*buf = '\0';
4751
}
48-
#else
49-
static void abort_with_message(const char *) { abort(); }
50-
#endif
5152

52-
static void report_error(const char *msg, uintptr_t caller, int abort) {
53+
static void report_error(const char *kind, uintptr_t caller) {
5354
if (caller == 0)
5455
return;
5556
while (true) {
@@ -77,21 +78,24 @@ static void report_error(const char *msg, uintptr_t caller, int abort) {
7778
}
7879
__sanitizer::atomic_store_relaxed(&caller_pcs[sz], caller);
7980

80-
char msg_buf[128] = MSG_PREFIX;
81-
const char *end = msg_buf + sizeof(msg_buf);
82-
char *p = append_str(msg, msg_buf + sizeof(MSG_PREFIX) - 1, end);
83-
p = append_str(MSG_SUFFIX, p, end);
84-
p = append_hex(caller, p, end);
85-
if (p < end) *p++ = '\n';
86-
87-
// Zero terminate.
88-
if (p == end) --p;
89-
*p = '\0';
81+
char msg_buf[128];
82+
format_msg(kind, caller, msg_buf, msg_buf + sizeof(msg_buf));
9083
message(msg_buf);
91-
if (abort) abort_with_message(msg_buf); \
9284
}
9385
}
9486

87+
#if defined(__ANDROID__)
88+
extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
89+
static void abort_with_message(const char *kind, uintptr_t caller) {
90+
char msg_buf[128];
91+
format_msg(kind, caller, msg_buf, msg_buf + sizeof(msg_buf));
92+
if (&android_set_abort_message) android_set_abort_message(msg_buf);
93+
abort();
94+
}
95+
#else
96+
static void abort_with_message(const char *kind, uintptr_t caller) { abort(); }
97+
#endif
98+
9599
#if SANITIZER_DEBUG
96100
namespace __sanitizer {
97101
// The DCHECK macro needs this symbol to be defined.
@@ -107,19 +111,21 @@ void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {
107111

108112
#define INTERFACE extern "C" __attribute__((visibility("default")))
109113

110-
#define HANDLER_RECOVER(name, msg) \
111-
INTERFACE void __ubsan_handle_##name##_minimal() { \
112-
report_error(msg, GET_CALLER_PC(), 0); \
114+
#define HANDLER_RECOVER(name, kind) \
115+
INTERFACE void __ubsan_handle_##name##_minimal() { \
116+
report_error(kind, GET_CALLER_PC()); \
113117
}
114118

115-
#define HANDLER_NORECOVER(name, msg) \
116-
INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
117-
report_error(msg, GET_CALLER_PC(), 1); \
119+
#define HANDLER_NORECOVER(name, kind) \
120+
INTERFACE void __ubsan_handle_##name##_minimal_abort() { \
121+
uintptr_t caller = GET_CALLER_PC(); \
122+
report_error(kind, caller); \
123+
abort_with_message(kind, caller); \
118124
}
119125

120-
#define HANDLER(name, msg) \
121-
HANDLER_RECOVER(name, msg) \
122-
HANDLER_NORECOVER(name, msg)
126+
#define HANDLER(name, kind) \
127+
HANDLER_RECOVER(name, kind) \
128+
HANDLER_NORECOVER(name, kind)
123129

124130
HANDLER(type_mismatch, "type-mismatch")
125131
HANDLER(alignment_assumption, "alignment-assumption")

0 commit comments

Comments
 (0)