Skip to content
LogoLogo

C & WASM ABI

Both zig build native (zig-out/lib/libguillotine_mini.a) and zig build wasm (zig-out/bin/guillotine_mini.wasm) export the same 42 symbols from src/root_c.zig.

Calling conventions

  • Handles: Every function except evm_alloc, evm_free, evm_create, and evm_set_js_opcode_callbacks_enabled takes a nullable EvmHandle*. On NULL, void functions return immediately, boolean functions return false, and numeric functions return 0.
  • Empty buffers: Pass a valid byte pointer with length 0 for empty calldata or continuation data.
  • Byte order: Addresses are 20 raw bytes. All u256 values (value, slot, balance, chain_id, difficulty, prevrandao, base_fee, blob_base_fee) are 32-byte big-endian arrays.
  • usize width: 8 bytes on 64-bit native targets; 4 bytes (little-endian u32) on wasm32-wasi.
  • WASM reactor startup: Call _initialize() once (via WASI.initialize(instance) in Node 22+) before calling any evm_* export.
  • Buffer ownership: All input setters copy bytes into internal storage. Re-read exports.memory.buffer after every evm_alloc or evm_execute because linear memory may grow.

Memory management (WASM & FFI buffers)

uint8_t *evm_alloc(size_t len);
void     evm_free(uint8_t *ptr, size_t len);

evm_alloc(0) returns NULL. evm_free(NULL, len) is a no-op.

Lifecycle

EvmHandle *evm_create(const char *hardfork_name, size_t hardfork_len, uint8_t log_level);
void       evm_destroy(EvmHandle *handle);
  • hardfork_name is parsed case-insensitively ("frontier", "cancun", "prague", "osaka", "amsterdam", …). An empty or unrecognized name selects the configured default. The C wrapper uses Evm(.{}), whose Hardfork.DEFAULT is .OSAKA.
  • log_level: 0 = none, 1 = err, 2 = warn, 3 = info, 4 = debug.

Execution setup & run

bool evm_set_bytecode(EvmHandle *h, const uint8_t *bytecode, size_t bytecode_len);
 
bool evm_set_execution_context(
    EvmHandle *h,
    int64_t gas,
    const uint8_t caller[20],
    const uint8_t address[20],
    const uint8_t value[32],
    const uint8_t *calldata,
    size_t calldata_len
);
 
void evm_set_blockchain_context(
    EvmHandle *h,
    const uint8_t chain_id[32],
    uint64_t block_number,
    uint64_t block_timestamp,
    const uint8_t block_difficulty[32],
    const uint8_t block_prevrandao[32],
    const uint8_t block_coinbase[20],
    uint64_t block_gas_limit,
    const uint8_t block_base_fee[32],
    const uint8_t blob_base_fee[32]
);
 
bool evm_set_access_list_addresses(EvmHandle *h, const uint8_t *addresses, size_t count);
bool evm_set_access_list_storage_keys(
    EvmHandle *h,
    const uint8_t *addresses, // count * 20 packed bytes
    const uint8_t *slots,     // count * 32 packed big-endian bytes
    size_t count
);
bool evm_set_blob_hashes(EvmHandle *h, const uint8_t *hashes, size_t count);
 
bool evm_execute(EvmHandle *h);

evm_execute() runs Evm.callWithBytecodeOverride(): if evm_set_bytecode() set non-empty bytecode, it overrides the top-level target's code without mutating evm.code; otherwise it executes the code already stored at address via evm_set_code(). Returns result.success.

Tracing, call tracer & access list tracker

// EIP-3155 JSONL opcode trace (includes trailing TraceSummary line)
void   evm_enable_tracing(
    EvmHandle *h,
    bool enable_memory,
    bool disable_stack,
    bool disable_storage,
    bool enable_return_data
);
void   evm_disable_tracing(EvmHandle *h);
size_t evm_get_trace_json_len(EvmHandle *h);
size_t evm_get_trace_json(EvmHandle *h, uint8_t *buffer, size_t buffer_len);
 
// Geth callTracer JSON tree
void   evm_enable_call_tracer(EvmHandle *h, bool only_top_call, bool with_log);
size_t evm_get_call_trace_json_len(EvmHandle *h);
size_t evm_get_call_trace_json(EvmHandle *h, uint8_t *buffer, size_t buffer_len);
 
// EIP-2930 access list JSON array
void   evm_enable_access_list_tracker(EvmHandle *h);
size_t evm_get_access_list_json_len(EvmHandle *h);
size_t evm_get_access_list_json(EvmHandle *h, uint8_t *buffer, size_t buffer_len);
  • Configuration applies to subsequent evm_execute() and evm_call_ffi() calls.
  • When both evm_enable_call_tracer and evm_enable_access_list_tracker are enabled, events are fanned out through MuxInspector.
  • Serialized JSON payloads are lazily computed and cached per execution, then freed on the next execution or evm_destroy().
  • evm_get_*_json() copies min(payload_len, buffer_len) bytes and returns the number of bytes copied.

Result inspection

bool     evm_is_success(EvmHandle *h);
int64_t  evm_get_gas_remaining(EvmHandle *h);
int64_t  evm_get_gas_used(EvmHandle *h);
uint64_t evm_get_gas_refund(EvmHandle *h);
 
size_t   evm_get_output_len(EvmHandle *h);
size_t   evm_get_output(EvmHandle *h, uint8_t *buffer, size_t buffer_len);
 
size_t   evm_get_log_count(EvmHandle *h);
bool     evm_get_log(
    EvmHandle *h,
    size_t index,
    uint8_t address_out[20],
    size_t *topics_count_out,
    uint8_t topics_out[128],  // up to 4 * 32-byte big-endian topics
    size_t *data_len_out,
    uint8_t *data_out,
    size_t data_max_len
);
 
size_t   evm_get_storage_change_count(EvmHandle *h);
bool     evm_get_storage_change(
    EvmHandle *h,
    size_t index,
    uint8_t address_out[20],
    uint8_t slot_out[32],
    uint8_t value_out[32]
);

Direct account & storage state

bool evm_set_storage(EvmHandle *h, const uint8_t addr[20], const uint8_t slot[32], const uint8_t value[32]);
bool evm_get_storage(EvmHandle *h, const uint8_t addr[20], const uint8_t slot[32], uint8_t value_out[32]);
bool evm_set_balance(EvmHandle *h, const uint8_t addr[20], const uint8_t balance[32]);
bool evm_set_code(EvmHandle *h, const uint8_t addr[20], const uint8_t *code, size_t code_len);
bool evm_set_nonce(EvmHandle *h, const uint8_t addr[20], uint64_t nonce);

All four setters may be called before the first evm_execute() or evm_call_ffi(). evm_set_storage() records the slot via Evm.setStoragePrestate() so seeded values survive initTransactionState() (including with StorageInjector enabled) without being misreported as writes; evm_set_balance(), evm_set_code(), and evm_set_nonce() persist in the Evm's arena-backed state maps across calls.

Async state-injection protocol

typedef struct {
    uint8_t  output_type;      // 0=result, 1=storage, 2=balance, 3=code, 4=nonce, 5=ready_to_commit, 255=error
    uint8_t  address[20];
    uint8_t  slot[32];
    uint32_t json_len;         // inline bytes copied into json_data (up to 16384)
    uint8_t  json_data[16384];
} AsyncRequest;
 
bool   evm_enable_storage_injector(EvmHandle *h);
bool   evm_call_ffi(EvmHandle *h, AsyncRequest *request_out);
bool   evm_continue_ffi(
    EvmHandle *h,
    uint8_t continue_type,     // 1=storage(84B), 2=balance(52B), 3=code(20+N), 4=nonce(28B), 5=after_commit(0B)
    const uint8_t *data_ptr,
    size_t data_len,
    AsyncRequest *request_out
);
size_t evm_get_state_changes_len(EvmHandle *h);
size_t evm_get_state_changes(EvmHandle *h, uint8_t *buffer, size_t buffer_len);

evm_get_state_changes_len() returns the exact byte length of the full arena-allocated state-changes JSON when output_type == 5, allowing callers to fetch payloads larger than the 16,384-byte inline AsyncRequest.json_data buffer via evm_get_state_changes().

WASM host imports (env) & callback toggle

void evm_set_js_opcode_callbacks_enabled(bool enabled);

guillotine_mini.wasm imports wasi_snapshot_preview1 plus two functions from env:

ImportReturn 0Return non-zero
env.js_opcode_callback(opcode, frame_ptr)Run built-in Zig opcode handlerHandler consumed the opcode
env.js_precompile_callback(addr_ptr, in_ptr, in_len, gas_limit, out_len_ptr, out_ptr_ptr, gas_used_ptr)Run built-in Zig/Rust precompileJS handled the precompile

js_opcode_callback dispatch is opt-in and defaults to false to avoid per-opcode WASM/JS boundary overhead; call evm_set_js_opcode_callbacks_enabled(true) to enable it for the WASM instance (no-op on native builds).