Skip to content

feat: add stacks-wasm-host with wasmi and deploy steps 1–4 - #7929

Open
brice-stacks wants to merge 5 commits into
stacks-network:mainfrom
brice-stacks:feat/stacks-wasm-host
Open

brice-stacks wants to merge 5 commits into
stacks-network:mainfrom
brice-stacks:feat/stacks-wasm-host

Conversation

@brice-stacks

Copy link
Copy Markdown
Contributor

Description

Note

Stacked on #7905 (feat/stacks-abi-interface), which adds DeployRejection to stacks-abi. Review only the last two commits here until that merges.

Adds crates/stacks-wasm-host, the runtime for Wasm contracts under the chain ABI, with the engine and the first four deploy validation steps. The crate has no node dependency and builds for the browser with wasm-web. Nothing in the node calls it yet.

Engine

  • wasmi 2.0.4 behind a small Engine trait (compile only for now), so an engine change stays local to engine.rs.
  • Every wasmi::Config option available under our cargo features is set explicitly, so a wasmi default cannot change consensus: fuel on, CompilationMode::Eager, exactly the profile's proposals (mutable globals, sign extension, multi-value, bulk memory), floats off, and stack limits set 8× above the ABI's stack bound as a safety net.
  • wasmi and wasmparser are pinned to exact versions in the workspace. wasmparser is =0.228.0, not the latest, because that is the version wasmi 2.0.4 validates with; using it keeps one validator in the build.
  • compile creates a fresh wasmi::Engine per module. A wasmi engine never frees the code it has translated, so one shared engine would grow with every deploy. With one per module, dropping the compiled module frees its code. Creating an engine measured at about 72 ns.

Deploy steps 1–4 (deploy::validate)

  1. Module size within the limit.
  2. wasmparser validate_all under the profile's features.
  3. Profile: no start, the table limit, one defined memory with a bounded maximum exported as memory, plus the limits below.
  4. Every import is a function from stacks-abi-v1 with the name and type the host-function table gives.

A Rejection maps to its stacks_abi::deploy::DeployRejection reason (module-size, invalid-wasm, profile, imports) and carries a diagnostic detail truncated to 128 bytes.

New limits in stacks-abi

Modules come from arbitrary users, and deploy is charged by module size, so validating and compiling a module must cost in proportion to its bytes. Without limits, a 1 MiB module that passed steps 1–4 could take about 7 s to validate and compile, or retain 4–6.5 GB once compiled. These are enforced as profile; all values are provisional:

Limit Value
Types per module 4,096
Type arity (parameters, and results) 32
Functions per module, imports included 10,000
Declared locals per function 1,024
Block nesting depth 1,024
Control-flow values per module 1,000,000

Control-flow values are the values that blocks and branches carry: parameters plus results of each block/loop/if, the label arity of each branch target, and the function's results for each return. The exact definition is in the Limits::control_values doc. Code whose blocks and branches carry no values counts zero.

Applicable issues

Additional info (benefits, drawbacks, caveats)

Important

Two rejection reasons differ from what the issue text and the design suggest. Reasons are consensus data, so please check these.

  • Type limits are checked before validation. The type count and arity are judged on the type section as the parser reads it, before validate_all. A module over either is profile even if it is otherwise invalid. This is deliberate: validating a body costs time in proportion to the arity of the types it uses, so checking after validation let a rejected module cost up to 3.5 s.
  • A second table or memory is invalid-wasm, not profile. The validator rejects it at step 2 because multi-memory and reference types are off.

Note

Step 2 accepts exactly what wasmparser 0.228.0 accepts with floats off. That includes ten float-consuming conversions (i32/i64.trunc_f32/f64_s/u, i32.reinterpret_f32, i64.reinterpret_f64) in unreachable code, where they can never execute. A test pins this, so a wasmparser upgrade that changes it fails.

Cost at the limits, for a 1 MiB module (release build, Apple silicon; a typical dense module takes about 15 ms to validate and compile and retains 3.6 MB):

Case Measured
Worst accepted, time about 167 ms (11×), from calls to 32-parameter, 32-result functions
Worst accepted, memory retained about 68 MB, with the whole control-flow budget spent on br_if to 32-wide labels
Worst rejected, time about 122 ms

Known gaps, left for follow-up:

  • A tiny invalid module can make wasmparser reserve up to 69 MiB from a declared section count before it is rejected. The memory is transient.
  • wasmi has limits of its own that steps 1–4 do not imply. For example, a function with 1,024 locals and 63,488 pushes passes steps 1–4 and fails compile. Step 7 will report these as reason engine.
  • operator_cost and fuel_cost are set to wasmi 2.0.4's built-in values as placeholders until the fuel schedule supplies them.

Checklist

  • Test coverage for new or modified code paths
  • For new Clarity features or consensus changes, add property tests (see
    docs/property-testing.md)
  • Changelog fragment(s) or "no changelog" label added (see
    changelog.d/README.md). If this PR breaks
    anything for node operators or users, or requires them to manually do
    anything (such as adjust a setting), use the breaking category.
  • Required documentation changes (e.g.,
    rpc/openapi.yaml for RPC endpoints,
    event-dispatcher.md for new events)
  • New clarity functions have corresponding PR in clarity-benchmarking repo

Define the host interface of the chain ABI as one table: for each of the
66 host functions its group, parameters in the kinds of ABI 6.1, result,
status codes and charge formula. Deploy validation, linking, the fuel
schedule and the tools derive from it.

Tests check the Wasm types against ABI 6.1 and the rules every signature
and charge formula must keep.
Add the parts of a deploy that need no Wasm parser:

- The `abi.interface` section (ABI 4.1): encode, decode and validation,
  accepting exactly the one encoding of an interface, and its hash,
  `sha512/256` of the bytes.
- The rules for a module's custom sections (ABI 3.4).
- The reasons a deploy is rejected, each with the string a receipt
  carries.

Vectors cover every rejection rule of ABI 4.1 and 3.4.
Add `crates/stacks-wasm-host`, the runtime for Wasm contracts under the
chain ABI. It has no node dependency and builds for the browser with the
`wasm-web` feature.

The engine is wasmi 2.0.4 behind a small `Engine` trait, with every
`Config` option set explicitly so a wasmi default cannot change
consensus: fuel on, eager compilation, the proposals of the profile and
no others, and stack limits far above the ABI's stack bound. Each
compiled module gets its own wasmi engine, because an engine never
frees the code it has translated. wasmi and wasmparser are pinned to
exact versions in the workspace; wasmparser 0.228.0 is the version
wasmi itself validates with.

`deploy::validate` runs the first four deploy steps in order: the module
size limit, wasmparser validation under the profile's features, the
profile rules (no `start`, the table limit, one defined memory with a
bounded maximum exported as `memory`), and the import check against the
host-function table. A rejection names its reason and carries a bounded
diagnostic detail.

Add limits to `stacks-abi` that keep the cost of validating and
compiling a module in proportion to its size, enforced at deploy as
`profile`: types per module, type arity, functions per module, locals
per function, block nesting depth, and control-flow values per module.
The type count and arity are judged on the type section before the
module is validated, so a module over them is rejected without
validating its bodies.

Closes stacks-network#7879
`Rejection::reason()` returned the receipt strings directly because the
reason enum did not exist yet. `stacks-abi` now defines
`DeployRejection`, so return that and let it own the strings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clarity 8] Host: crate, engine trait, wasmi configuration, deploy steps 1–4

1 participant