Skip to content

Metering for Bulk Memory Opcodes + Opcode Version - #79

Open
laurci wants to merge 36 commits into
rc/after-supernovafrom
mem-ops-wasm
Open

laurci wants to merge 36 commits into
rc/after-supernovafrom
mem-ops-wasm

Conversation

@laurci

@laurci laurci commented Jul 21, 2025 •

Copy link
Copy Markdown

Bulk memory opcodes and versioned opcode configuration

Summary

This PR adds support for WebAssembly bulk-memory operations and makes opcode configuration explicitly versioned across the Rust and C APIs.

  • Add OpcodeVersion and OpcodeConfig; V1 remains the default legacy opcode set, while V2 enables memory.copy and memory.fill.
  • Remove unsupported exception-handling, reference-type, and table-manipulation instructions from the V2 whitelist. memory.init and data.drop remain unsupported.
  • Meter bulk-memory operations in both the production Wasmer executor and the experimental executor with independent base and per-byte costs for copy and fill.
  • Refactor opcode-cost handling into dedicated shared structures and replace the former opcode-usage mechanism.
  • Harden the C ABI with explicit repr(C) mirrors for compilation options and opcode costs, and replace vm_exec_set_opcode_costs with version-aware vm_exec_set_opcode_config.
  • Regenerate VM hooks, remove the retired hook entries, and update the Rust toolchain to 1.98.

Gas accounting

For both memory.copy and memory.fill, the charged gas is:

base_cost + (byte_count * per_byte_cost)

The base cost applies to zero-byte operations as well. The instrumentation preserves the size operand on the WebAssembly stack.

Compatibility

  • Existing users remain on OpcodeVersion::V1 by default and cannot instantiate modules that use bulk-memory operations.
  • Consumers that need memory.copy or memory.fill must configure OpcodeVersion::V2 with the corresponding opcode costs.
  • C API consumers must migrate from vm_exec_set_opcode_costs to vm_exec_set_opcode_config, passing the opcode-version code and the ABI-safe opcode-cost structure.

Tests

  • Add production and experimental executor tests covering base costs, per-byte costs, zero-byte operations, and preservation of a local size operand.
  • Add an opcode-version test confirming that a memory.copy module is rejected by V1 and accepted by V2.

@andrei-marinica
andrei-marinica marked this pull request as draft July 21, 2025 11:40
@laurci
laurci changed the base branch from rc/v1.7.2 to rc/after-supernova August 20, 2025 07:27
@laurci
laurci marked this pull request as ready for review August 20, 2025 07:27
Comment thread vm-executor-wasmer/src/wasmer_metering_helpers.rs Outdated
Comment thread vm-executor-wasmer/src/wasmer_metering_helpers.rs Outdated
Comment thread vm-executor/src/opcode_check_used.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds metering support for bulk memory operations (MemoryCopy and MemoryFill) in WebAssembly through an opcode versioning mechanism. The implementation introduces V2 opcodes that include these bulk memory operations, while V1 maintains backward compatibility.

Changes:

  • Introduced opcode versioning (V1/V2) to enable/disable bulk memory operations
  • Added metering for MemoryCopy and MemoryFill with per-byte cost calculation
  • Refactored API from set_opcode_cost to set_opcode_config to include version information

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
vm-executor/src/opcode_version.rs New enum defining opcode versions V1 and V2 with i32 conversion
vm-executor/src/opcode_config.rs New struct combining opcode version and cost configuration
vm-executor/src/opcode_cost.rs Added cost fields for MemoryCopy and MemoryFill operations
vm-executor/src/lib.rs Exported new OpcodeConfig and OpcodeVersion types
vm-executor/src/executor.rs Updated trait method from set_opcode_cost to set_opcode_config
vm-executor-wasmer/src/wasmer_opcode_cost.rs Split opcode cost logic into V1 and V2 functions, added bulk memory ops to V2
vm-executor-wasmer/src/wasmer_metering.rs Added bulk memory metering with size backup global and per-byte cost injection
vm-executor-wasmer/src/wasmer_helpers.rs Added helper to identify supported bulk memory operators
vm-executor-wasmer/src/wasmer_instance.rs Updated to use OpcodeConfig instead of OpcodeCost
vm-executor-wasmer/src/wasmer_executor.rs Updated executor to use OpcodeConfig with improved error handling
vm-executor-wasmer/src/lib.rs Renamed metering_helpers to opcode_cost module
vm-executor-wasmer/tests/opcode_version_test.rs Added test verifying V1 rejects and V2 accepts bulk memory ops
vm-executor-wasmer/tests/memory-copy.wat Test WAT file demonstrating memory.copy operation
vm-executor-wasmer/Cargo.toml Added std feature to log dependency
vm-executor-experimental/* Updated to use OpcodeConfig (but missing bulk memory metering)
c-api/src/capi_metering.rs Updated C API to accept opcode version parameter
c-api/libvmexeccapi.h Updated C header with new function signature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread vm-executor-wasmer/src/wasmer_helpers.rs Outdated
Comment thread vm-executor/src/opcode_version.rs
Comment thread vm-executor/src/executor.rs
Comment thread c-api/src/capi_metering.rs Outdated
Comment thread vm-executor-wasmer/src/wasmer_metering.rs Outdated
Comment thread c-api/src/capi_metering.rs Outdated
Comment thread c-api/libvmexeccapi.h Outdated
Comment thread vm-executor-wasmer/src/wasmer_metering.rs Outdated
Comment thread vm-executor-experimental/src/middlewares/we_metering.rs Outdated
Comment thread vm-executor-wasmer/tests/opcode_version_test.rs

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Critical metering correctness, cache enforcement, import compatibility, and public ABI regressions remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 8 High severity

Open (8)

Comment thread c-api/libvmexeccapi.h
Comment thread c-api/libvmexeccapi.h Outdated
Comment thread c-api/src/capi_vm_hook_pointers.rs
Comment thread vm-executor-wasmer/src/wasmer_imports.rs
Comment thread vm-executor-wasmer/src/wasmer_instance.rs
Comment thread vm-executor-wasmer/src/wasmer_metering.rs
Comment thread vm-executor-wasmer/src/wasmer_metering.rs
Comment thread vm-executor/src/vm_hooks.rs
@andrei-marinica andrei-marinica changed the title add metering for bulk memory opcodes Metering for Bulk Memory Opcodes + Opcode Version Sep 28, 2026
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.

4 participants