Repository navigation
feat: add stacks-wasm-host with wasmi and deploy steps 1–4 - #7929
Open
brice-stacks wants to merge 5 commits into
Open
brice-stacks wants to merge 5 commits into
brice-stacks wants to merge 5 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Note
Stacked on #7905 (
feat/stacks-abi-interface), which addsDeployRejectiontostacks-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 withwasm-web. Nothing in the node calls it yet.Engine
Enginetrait (compileonly for now), so an engine change stays local toengine.rs.wasmi::Configoption 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.=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.compilecreates a freshwasmi::Engineper 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)wasmparservalidate_allunder the profile's features.start, the table limit, one defined memory with a bounded maximum exported asmemory, plus the limits below.stacks-abi-v1with the name and type the host-function table gives.A
Rejectionmaps to itsstacks_abi::deploy::DeployRejectionreason (module-size,invalid-wasm,profile,imports) and carries a diagnostic detail truncated to 128 bytes.New limits in
stacks-abiModules 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: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 eachreturn. The exact definition is in theLimits::control_valuesdoc. 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.
validate_all. A module over either isprofileeven 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.invalid-wasm, notprofile. 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):
br_ifto 32-wide labelsKnown gaps, left for follow-up:
compile. Step 7 will report these as reasonengine.operator_costandfuel_costare set to wasmi 2.0.4's built-in values as placeholders until the fuel schedule supplies them.Checklist
docs/property-testing.md)changelog.d/README.md). If this PR breaksanything for node operators or users, or requires them to manually do
anything (such as adjust a setting), use the breaking category.
rpc/openapi.yamlfor RPC endpoints,event-dispatcher.mdfor new events)clarity-benchmarkingrepo