Skip to content

Commit d7662c1

Browse files
committed
feat: cut simulator over to generated bb-avm-sim IPC service
1 parent 6edf044 commit d7662c1

96 files changed

Lines changed: 1757 additions & 2998 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

barretenberg/cpp/src/barretenberg/avm/avm_execute.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,24 @@ namespace bb::avm {
1313
using namespace bb::avm2;
1414
using namespace bb::world_state;
1515

16-
// Global cancellation token for the currently active simulation.
17-
// Set before simulation starts, cleared after. SIGUSR1 handler reads this to cancel.
16+
// Cancellation for the single in-flight simulation. bb-avm-sim runs exactly one
17+
// simulation at a time; the SIGUSR1 handler (which may run on any thread) cancels
18+
// it through g_active_cancellation_token.
19+
//
20+
// The token is process-lifetime and never freed. A per-request token would let
21+
// the signal handler dereference a pointer to a token the completing request had
22+
// already freed (use-after-free), since the handler can run concurrently with the
23+
// request thread unwinding. g_active_cancellation_token points at this token only
24+
// while a simulation runs and is null otherwise, so a signal between simulations
25+
// is a safe no-op.
26+
//
27+
// NOTE: cancellation is process-scoped (a signal), not request-scoped. A signal
28+
// delivered late — after its target finished and the next simulation began on this
29+
// process — would cancel the wrong simulation. The pool runs one simulation per
30+
// process and signals only the in-flight one, so this isn't exercised today;
31+
// hardening it would need a request-scoped cancel channel rather than a signal.
32+
const avm2::simulation::CancellationTokenPtr g_sim_cancellation_token =
33+
std::make_shared<avm2::simulation::CancellationToken>();
1834
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
1935
std::atomic<avm2::simulation::CancellationToken*> g_active_cancellation_token{ nullptr };
2036

@@ -36,7 +52,10 @@ template <typename T> static T deserialize_from_msgpack(const std::vector<uint8_
3652
template <>
3753
void handle_simulate(AvmRequest& request, wire::AvmSimulate&& command, Responder<wire::AvmSimulateResponse> respond)
3854
{
39-
auto cancellation_token = std::make_shared<avm2::simulation::CancellationToken>();
55+
// Reuse the process-lifetime token (cleared of any prior cancellation) instead
56+
// of allocating a per-request one the signal handler could outlive.
57+
g_sim_cancellation_token->reset();
58+
const avm2::simulation::CancellationTokenPtr& cancellation_token = g_sim_cancellation_token;
4059
try {
4160
auto sim_inputs = deserialize_from_msgpack<AvmFastSimulationInputs>(command.inputs);
4261

barretenberg/cpp/src/barretenberg/nodejs_module/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR})
2727
add_library(nodejs_module SHARED ${SOURCE_FILES})
2828
set_target_properties(nodejs_module PROPERTIES PREFIX "" SUFFIX ".node")
2929
target_include_directories(nodejs_module PRIVATE ${NODE_API_HEADERS_DIR} ${NODE_ADDON_API_DIR})
30-
target_link_libraries(nodejs_module PRIVATE ipc ipc_runtime vm2_sim wsdb_ipc_merkle_db)
30+
target_link_libraries(nodejs_module PRIVATE ipc ipc_runtime lmdblib)
3131

3232
# On macOS, Node.js N-API symbols are provided by the runtime, not at link time
3333
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")

barretenberg/cpp/src/barretenberg/nodejs_module/avm_simulate/avm_simulate_napi.cpp

Lines changed: 0 additions & 429 deletions
This file was deleted.

barretenberg/cpp/src/barretenberg/nodejs_module/avm_simulate/avm_simulate_napi.hpp

Lines changed: 0 additions & 72 deletions
This file was deleted.

barretenberg/cpp/src/barretenberg/nodejs_module/avm_simulate/ts_callback_contract_db.cpp

Lines changed: 0 additions & 260 deletions
This file was deleted.

0 commit comments

Comments
 (0)