From 9818015b27a9ed188684676d9f71df897f29dc6e Mon Sep 17 00:00:00 2001 From: Jonathan Hao Date: Thu, 2 Apr 2026 16:48:27 +0000 Subject: [PATCH] finding 2: use JS-provided throw_or_abort_impl for WASM error recovery In WASM builds, BB_NO_EXCEPTIONS is defined. The C++ definition of throw_or_abort_impl calls std::abort(), killing the WASM process on any error. Meanwhile, the header declares it as WASM_IMPORT from JS, where it throws a catchable Error. But the local C++ definition shadows the import. Fix: guard out the C++ definition with #ifndef __wasm__ so the JS import is used in WASM builds. For wasmtime (non-JS WASM runtime used in CI tests), add -Wunknown-imports-trap=y so the import is stubbed with a trap (correct behavior: errors in wasmtime should still abort). Before: any throw_or_abort in WASM kills the process, PXE crashes. After: the JS caller gets a catchable Error("actual error message"). --- barretenberg/cpp/scripts/wasmtime.sh | 1 + .../src/barretenberg/env/throw_or_abort_impl.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/barretenberg/cpp/scripts/wasmtime.sh b/barretenberg/cpp/scripts/wasmtime.sh index 3ad657d5798d..3e4032043ab8 100755 --- a/barretenberg/cpp/scripts/wasmtime.sh +++ b/barretenberg/cpp/scripts/wasmtime.sh @@ -5,6 +5,7 @@ set -eu export WASMTIME_BACKTRACE_DETAILS=1 exec wasmtime run \ -Wthreads=y \ + -Wunknown-imports-trap=y \ -Sthreads=y \ ${HARDWARE_CONCURRENCY:+--env HARDWARE_CONCURRENCY} \ --env HOME \ diff --git a/barretenberg/cpp/src/barretenberg/env/throw_or_abort_impl.cpp b/barretenberg/cpp/src/barretenberg/env/throw_or_abort_impl.cpp index 2cc76037a1f5..fdff0104f6e3 100644 --- a/barretenberg/cpp/src/barretenberg/env/throw_or_abort_impl.cpp +++ b/barretenberg/cpp/src/barretenberg/env/throw_or_abort_impl.cpp @@ -1,3 +1,14 @@ +// In WASM builds, throw_or_abort_impl is provided by the JavaScript environment +// (declared as WASM_IMPORT in throw_or_abort_impl.hpp). The JS implementation +// throws a catchable JS Error with the error message. +// +// For non-JS WASM runtimes (wasmtime), the import must also be provided. +// See barretenberg/cpp/scripts/wasmtime.sh for the wasmtime configuration. +// +// We guard out the C++ definition so the JS import is used instead of being +// shadowed by the local definition (which would call std::abort). +#ifndef __wasm__ + #include "barretenberg/common/log.hpp" #include "barretenberg/common/wasm_export.hpp" #include @@ -27,3 +38,5 @@ WASM_EXPORT void throw_or_abort_impl [[noreturn]] (const char* err) abort_with_message(err); #endif } + +#endif // !__wasm__