filecoin-project / ref-fvm

Reference implementation of the Filecoin Virtual Machine
https://fvm.filecoin.io/
Other
384 stars 139 forks source link

Investigate using a wasm interpreter for M2.2 #1775

Open Stebalien opened 1 year ago

Stebalien commented 1 year ago

E.g., https://github.com/paritytech/wasmi.

AOT compilation can be a bit problematic for untrusted smart contracts:

  1. The runtime of wasm "blocks" will depend on how they compile to native code. There are concerns that a malicious actor could come up with pathological wasm bytecode that has a slow execution time with respect to the gas charged by our gas model.
  2. AOT compilation is expensive and time consuming. At the very least, it'll need to be asynchronous (and parallel).
  3. AOT compilation is architecture dependent so it can't happen inside an actor. That makes it difficult to account for in the gas model and potentially opens us up to compiler bombs.

The alternative is interpretation. Ideally, a wasm interpreter inside wasm (same as we run an EVM interpreter inside wasm). The obvious downsides are:

  1. A wasm interpreter will be significantly slower than AOT compilation. However, it should still be significantly faster than the EVM.
  2. Running the wasm interpreter inside wasm will add even more overhead. But it also adds quite a bit of security and may allow us to remove some runtime checks from the interpreter.

It may also be possible to default to interpretation, upgrading to AOT/native once the network has compiled the actor to native and performed some validation.

Stebalien commented 1 year ago

Looking at other networks:

Stebalien commented 1 year ago

Ok, it looks like winch (wasmtime's single pass compiler) is making good progress (https://www.youtube.com/watch?v=bj8SUKUg0BQ). If we go this way:

Stebalien commented 1 year ago

Discussion https://github.com/filecoin-project/FIPs/discussions/779