ref-fvm icon indicating copy to clipboard operation
ref-fvm copied to clipboard

Investigation: Using Wasm Reference Types

Open Stebalien opened this issue 4 years ago • 1 comments

In addition to ints/floats, wasm now has a special "externref" type for referencing objects outside of the VM.

Possible use-cases:

  • IPLD objects/blocks. This could be safer (and likely more performant) than using integers.
  • Cross-module calling. E.g., a bigint library could return pointers to bigints as "externrefs" and know that the caller can't forge these references. This would need a bit of help from the runtime but it should be possible.
  • Closures?

Pros:

  • Unforgable: These could be great as capabilities. Except actually storing them in IPLD could get... tricky.
  • Opaque: There's no way we'll introduce non-determinism into modules by passing handles.
  • Garbage collected: If the actor has a reference, the object is live.
  • They seem to be the "future" for referencing host objects.

Cons:

  • Garbage collected: Wasmtime's garbage collector is deterministic, at least, but it's still complex. Luckily, any non-determinism would be in timing, and would not be visible to the Actor itself. I expect a "minimal" VM would just let all references live forever, and we'd probably be fine with that.
  • No way to make references affine. For capabilities, being able to say "there exists at most one copy" of a capability would be nice.
  • Cannot be stored in linear memory, they'd need to be stored in a WASM table and used indirectly. The language's compiler would need to be able to handle this.
  • They're very new.

Conclusion: For now, I'm going to shelve this as it seems cool but too experimental. But I'm documenting it here in case we want to revisit it.

References:

  • https://fitzgeraldnick.com/2020/08/27/reference-types-in-wasmtime.html
  • https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md

Stebalien avatar Oct 18 '21 23:10 Stebalien

Meta: @raulk would you prefer to put stuff like this in issues (easier to discuss) or in documents (easier to track over time).

Stebalien avatar Oct 18 '21 23:10 Stebalien