Skip to content

Commit

Permalink
native: try to catch native function crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuyu committed Dec 27, 2024
1 parent 70d3164 commit 68d53b3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
37 changes: 37 additions & 0 deletions native/abnativefunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,43 @@ void disable_logger() {
logger = reinterpret_cast<Logger *>(new NullLogger());
}

void autobuild_crash_handler(int sig, siginfo_t *info, void *ucontext) {
auto *log = reinterpret_cast<BaseLogger *>(logger);
if (log) {
Diagnostic diagnostic{};
diagnostic.code = sig;
diagnostic.level = LogLevel::Critical;
void *addr = nullptr;
switch (sig) {
case SIGSEGV:
case SIGILL:
case SIGFPE:
case SIGBUS:
addr = info ? info->si_addr : nullptr;
break;
default:
break;
}
diagnostic.frames.push_back(DiagnosticFrame{
.file = "<native code>",
.function = "<native function>",
.line = 0,
});
log->logDiagnostic(diagnostic);
log->error(fmt::format("Got signal {0} at address {1}", sig, addr));
log->logException("autobuild received signal: " +
std::string(strsignal(sig)));
}
exit(1);
}

void setup_crash_handler() {
struct sigaction action{};
action.sa_sigaction = autobuild_crash_handler;
action.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &action, nullptr);
}

void set_custom_arch(const char *arch) {
get_logger()->info(
fmt::format("Overriding target architecture to {0}", arch));
Expand Down
1 change: 1 addition & 0 deletions native/abnativefunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ int start_proc_00();
int dump_defines();
void disable_logger();
void set_custom_arch(const char *arch);
void setup_crash_handler();

#ifdef __cplusplus
} // extern "C"
Expand Down
1 change: 1 addition & 0 deletions native/autobuild.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ int autobuild_builtin(WORD_LIST *list) {
/* Called when `template' is enabled and loaded from the shared object. If this
function returns 0, the load fails. */
int autobuild_builtin_load(char *name) {
setup_crash_handler();
if (register_builtin_variables() != 0) {
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion native/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ void ColorfulLogger::logDiagnostic(Diagnostic diagnostic) {
// first call
buffer += fmt::format("\e[0mIn file included from \e[1m{0}:{1}\n",
frame.file, frame.line);
continue;
if (diagnostic.frames.size() > 1)
continue;
}
if ((it + 1) == diagnostic.frames.rend()) {
// last call
Expand Down

0 comments on commit 68d53b3

Please sign in to comment.