Skip to content

Commit 4de984d

Browse files
finding 2: use JS-provided throw_or_abort_impl for WASM error recovery
In WASM builds, BB_NO_EXCEPTIONS is defined which routes throw_or_abort to std::abort(), killing the WASM process on any error with no recovery. The fix: don't compile the C++ definition of throw_or_abort_impl for WASM builds (#ifndef __wasm__). The header already declares it as a WASM_IMPORT from the JS environment, where it throws a catchable JS Error. With the competing C++ definition removed, the JS import is used, and errors propagate as JS exceptions back to the TS caller instead of aborting the process.
1 parent e882aec commit 4de984d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

barretenberg/cpp/src/barretenberg/bbapi/c_bind.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ BBApiRequest global_request;
1818
/**
1919
* @brief Main API function that processes commands and returns responses
2020
*
21+
* In native builds, exceptions are caught and converted to ErrorResponse.
22+
* In WASM builds (BB_NO_EXCEPTIONS), throw_or_abort_impl is provided by JS
23+
* and throws a catchable JS Error, so no C++ recovery boundary is needed.
24+
*
2125
* @param command The command to execute
2226
* @return CommandResponse The response from executing the command
2327
*/
2428
CommandResponse bbapi(Command&& command)
2529
{
2630
#ifndef BB_NO_EXCEPTIONS
2731
try {
28-
#endif
29-
// Execute the command using the global request and return the response
3032
return execute(global_request, std::move(command));
31-
#ifndef BB_NO_EXCEPTIONS
3233
} catch (const std::exception& e) {
3334
return ErrorResponse{ .message = e.what() };
3435
}
36+
#else
37+
return execute(global_request, std::move(command));
3538
#endif
3639
}
3740

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
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 throws
3+
// a catchable JS Error, giving the caller a clean exception instead of aborting.
4+
// We must not define a competing C++ implementation here for WASM, otherwise the
5+
// linker uses the local definition (which calls std::abort) instead of the JS import.
6+
#ifndef __wasm__
7+
18
#include "barretenberg/common/log.hpp"
29
#include "barretenberg/common/wasm_export.hpp"
310
#include <stdexcept>
@@ -27,3 +34,5 @@ WASM_EXPORT void throw_or_abort_impl [[noreturn]] (const char* err)
2734
abort_with_message(err);
2835
#endif
2936
}
37+
38+
#endif // !__wasm__

0 commit comments

Comments
 (0)