Skip to content

Commit 9818015

Browse files
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").
1 parent e882aec commit 9818015

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

barretenberg/cpp/scripts/wasmtime.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -eu
55
export WASMTIME_BACKTRACE_DETAILS=1
66
exec wasmtime run \
77
-Wthreads=y \
8+
-Wunknown-imports-trap=y \
89
-Sthreads=y \
910
${HARDWARE_CONCURRENCY:+--env HARDWARE_CONCURRENCY} \
1011
--env HOME \

barretenberg/cpp/src/barretenberg/env/throw_or_abort_impl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
// In WASM builds, throw_or_abort_impl is provided by the JavaScript environment
2+
// (declared as WASM_IMPORT in throw_or_abort_impl.hpp). The JS implementation
3+
// throws a catchable JS Error with the error message.
4+
//
5+
// For non-JS WASM runtimes (wasmtime), the import must also be provided.
6+
// See barretenberg/cpp/scripts/wasmtime.sh for the wasmtime configuration.
7+
//
8+
// We guard out the C++ definition so the JS import is used instead of being
9+
// shadowed by the local definition (which would call std::abort).
10+
#ifndef __wasm__
11+
112
#include "barretenberg/common/log.hpp"
213
#include "barretenberg/common/wasm_export.hpp"
314
#include <stdexcept>
@@ -27,3 +38,5 @@ WASM_EXPORT void throw_or_abort_impl [[noreturn]] (const char* err)
2738
abort_with_message(err);
2839
#endif
2940
}
41+
42+
#endif // !__wasm__

0 commit comments

Comments
 (0)