From b46ad342dc07ba7447fdc45b484190f1663a46f3 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Mon, 27 Apr 2026 23:49:05 -0700 Subject: [PATCH] try using native unwind.h instead of libunwind --- .github/workflows/build-platform.yml | 2 +- BUILDING.md | 2 +- meson/deps/meson.build | 11 +---- src/crash.c | 62 ++++++++++++++-------------- 4 files changed, 34 insertions(+), 43 deletions(-) diff --git a/.github/workflows/build-platform.yml b/.github/workflows/build-platform.yml index 3720248c..b6c02e87 100644 --- a/.github/workflows/build-platform.yml +++ b/.github/workflows/build-platform.yml @@ -82,7 +82,7 @@ jobs: run: | apk add --no-cache git clang lld llvm meson ninja cmake pkgconf curl npm nodejs \ musl-dev \ - util-linux-dev util-linux-static linux-headers libunwind-dev libunwind-static xz-static \ + util-linux-dev util-linux-static linux-headers libunwind-dev libunwind-static \ py3-tomli tar xz zstd jq bash git config --global --add safe.directory "$GITHUB_WORKSPACE" diff --git a/BUILDING.md b/BUILDING.md index d24898d4..ed20ae46 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -138,7 +138,7 @@ Installation via package manager: ```sh apk add clang lld llvm meson ninja cmake pkgconf nodejs npm \ musl-dev \ - util-linux-dev util-linux-static linux-headers libunwind-dev libunwind-static xz-static + util-linux-dev util-linux-static linux-headers libunwind-dev libunwind-static ``` You will also need Rust and Zig installed. The recommended approach: diff --git a/meson/deps/meson.build b/meson/deps/meson.build index 2a77d65d..0bb42dbb 100644 --- a/meson/deps/meson.build +++ b/meson/deps/meson.build @@ -51,15 +51,6 @@ else win_deps = [] endif -crash_deps = [] -if host_machine.system() == 'linux' and not cc.has_header('execinfo.h') - libunwind_dep = dependency('libunwind', required: false, static: is_static) - if libunwind_dep.found() and cc.has_header('libunwind.h', dependencies: libunwind_dep) - crash_deps += [libunwind_dep] - add_project_arguments('-DANT_CRASH_HAVE_LIBUNWIND', language: 'c') - endif -endif - deps_info = { 'lmdb': { 'dir': 'openldap-LMDB_0.9.33', @@ -243,7 +234,7 @@ ant_deps = [ uriparser_dep, utf8proc_dep, ssl_dep, crypto_dep, zlib_dep, brotli_common_dep, brotli_dec_dep, brotli_enc_dep, uthash_dep, lmdb_dep, wamr_dep, -] + win_deps + crash_deps +] + win_deps if get_option('jit') mir_dep = subproject('mir').get_variable('mir_dep') diff --git a/src/crash.c b/src/crash.c index 4c4af4b0..b369c286 100644 --- a/src/crash.c +++ b/src/crash.c @@ -72,12 +72,35 @@ bool ant_crash_is_internal_report(int argc, char **argv) { #include #endif -#ifdef ANT_CRASH_HAVE_LIBUNWIND -#define UNW_LOCAL_ONLY -#include +#if !defined(ANT_CRASH_HAVE_EXECINFO) && defined(__has_include) +#if __has_include() +#define ANT_CRASH_HAVE_UNWIND_BACKTRACE 1 +#include +#endif #endif #endif +#ifdef ANT_CRASH_HAVE_UNWIND_BACKTRACE +typedef struct { + uintptr_t *frames; + int frame_count; + int skip; +} crash_unwind_state_t; + +static _Unwind_Reason_Code collect_unwind_frame(struct _Unwind_Context *ctx, void *arg) { + crash_unwind_state_t *state = arg; + uintptr_t ip = (uintptr_t)_Unwind_GetIP(ctx); + if (!ip) return _URC_NO_REASON; + if (state->skip > 0) { + state->skip--; + return _URC_NO_REASON; + } + if (state->frame_count >= ANT_CRASH_FRAME_MAX) return _URC_END_OF_STACK; + state->frames[state->frame_count++] = ip; + return _URC_NO_REASON; +} +#endif + typedef struct { char *buf; size_t cap; @@ -970,34 +993,11 @@ static void crash_handler(int sig, siginfo_t *info, void *ucontext) { int skip = n > 1 ? 1 : 0; for (int i = skip; i < n && frame_count < ANT_CRASH_FRAME_MAX; i++) frames[frame_count++] = (uintptr_t)raw_frames[i]; -#elif defined(ANT_CRASH_HAVE_LIBUNWIND) - unw_cursor_t cursor; - bool cursor_ready = false; - bool include_current_frame = false; - -#ifdef UNW_INIT_SIGNAL_FRAME - if (ucontext && unw_init_local2(&cursor, (unw_context_t *)ucontext, UNW_INIT_SIGNAL_FRAME) == 0) { - cursor_ready = true; - include_current_frame = true; - } -#endif - - unw_context_t context; - if (!cursor_ready && unw_getcontext(&context) == 0 && unw_init_local(&cursor, &context) == 0) - cursor_ready = true; - - if (cursor_ready) { - if (include_current_frame) { - unw_word_t ip = 0; - if (unw_get_reg(&cursor, UNW_REG_IP, &ip) == 0 && ip) - frames[frame_count++] = (uintptr_t)ip; - } - while (frame_count < ANT_CRASH_FRAME_MAX && unw_step(&cursor) > 0) { - unw_word_t ip = 0; - if (unw_get_reg(&cursor, UNW_REG_IP, &ip) == 0 && ip) - frames[frame_count++] = (uintptr_t)ip; - } - } +#elif defined(ANT_CRASH_HAVE_UNWIND_BACKTRACE) + (void)ucontext; + crash_unwind_state_t state = { frames, 0, 1 }; + _Unwind_Backtrace(collect_unwind_frame, &state); + frame_count = state.frame_count; #endif uint64_t fault_addr = info ? (uint64_t)(uintptr_t)info->si_addr : 0; -- knotmirror 0.0.1