Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a WebAssembly API #2853

Open
2 of 5 tasks
turbolent opened this issue Oct 5, 2023 · 5 comments
Open
2 of 5 tasks

Add a WebAssembly API #2853

turbolent opened this issue Oct 5, 2023 · 5 comments

Comments

@turbolent
Copy link
Member

turbolent commented Oct 5, 2023

Issue to be solved

As discussed in https://forum.onflow.org/t/idea-wasm-execution-engine-in-cadence/5164, add an API to Cadence that allows running WebAssembly programs.

Suggested Solution

An MVP should be able to:

  • Instantiate a WebAssembly module, without imports (i.e. the WebAssembly module has no access to Cadence)
  • Support function exports, where argument and return value types are i32 or i64 (i.e., not f32 or f64)
  • Allow calling such function exports from Cadence, and returning the result back to Cadence
  • Properly meter execution and abort execution if a given limit is reached. This requires defining how WebAssembly execution is metered in terms of the Cadence transaction's computation metering.

#2760 is a PoC implementation of the above. Remaining tasks:

Tasks

@darkdrag00nv2
Copy link
Contributor

darkdrag00nv2 commented Feb 10, 2024

Started looking into the metering aspects. Here is a brain dump for now. Might have misunderstood a few things, please correct me.

Computation

As @turbolent wrote in the poc #2760, we need a way to know the remaining computations.

FVM has ComputationAvailable(kind, amount) which returns true/false if there is enough computation units for the given kind. I think we can introduce RemainingComputation(kind) which returns the computation units remaining for the kind (or -1 for unbounded). For the current use case, the kind will just be ComputationKindWebAssemblyFuel but there can be future use-cases as well.

https://github.com/onflow/flow-go/blob/51202b7a9850a9728576355860b3279bdc1fe5bc/fvm/meter/computation_meter.go#L100-L111

Once have this, we just need a way to convert computation units to wasmtime fuel. Probably just a configurable multiplier would be sufficient?


Memory

We can limit the memory use via following:

  1. Limit max stack size using SetMaxWasmStack

  2. Limit linear memory, tables etc. using the Limiter

https://github.com/bytecodealliance/wasmtime-go/blob/da91f80fb2bec90f982107c127be18cfa328fd8e/store.go#L275-L281

Corresponding C documentation: https://docs.wasmtime.dev/c-api/store_8h.html#a0d8cfde96e219dd23d7c65f3cac2e15a.

@turbolent
Copy link
Member Author

@darkdrag00nv2 I haven't looked into it myself yet, but your investigation looks good so far 👍

@darkdrag00nv2
Copy link
Contributor

darkdrag00nv2 commented Feb 15, 2024

For tracking memory used, seems like we can just use the Size(store) function: https://github.com/bytecodealliance/wasmtime-go/blob/da91f80fb2bec90f982107c127be18cfa328fd8e/memory.go#L71

No, it gives the size of a linear memory.

@turbolent
Copy link
Member Author

Opened onflow/flow-go#6215 to add a ComputationRemaining function to flow-go

@turbolent
Copy link
Member Author

@onflow/flow-cadence-execution @janezpodhostnik How could we define the fuel-to-FVM-computation-usage factor/weight? How did we do this for EVM?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants