Skip to content

Instantly share code, notes, and snippets.

urwasm: WebAssembly interpreter suite on Urbit


WebAssembly is a low-level language for a portable virtual machine. Wasm is designed to be a compilation target for a variety of programming languages and its design is hardware independent and relatively simple, making its support ubiquitous in modern browsers. Its simple design made it a perfect first candidate for a first emulator of an conventional computational system on a novel functional computer: Urbit. In this paper I discuss the current state of urwasm project and some technical details, as well as describe the strategy to jet the interpreter of a state machine in a functional environment.


Introduction

Part 1

In this gist I will describe Lia interpreter model in a greater detail. As an addendum to the first part, I'd like to point out yet another advantage of jetting another interpreter on top of Wasm that would take a list of actions to be performed, as opposed to jetting just the invocation gate.

Both in Vere and Ares, at least right now, a jetting function that is called instead of performing Nock 9 on a jetted core requires that core must not be modified. Therefore, the jetting function must only read from the input core, allocating memory for the results.

Since ++invoke gate has module store, including linear memory as its input and output, any Wasm function invocation from Urbit would require copying the entire store. It might be a prohibitive overhead for some memory-heavy applications of Wasm, like emulating x86, and something to keep in mind for smaller cases.

On the other hand, interacting with an instantiated module in the c

In this gist I will go through several strategies for jetting in urwasm, delineate their strengths and weaknesses and explain my motivation for choosing a particular strategy (Lia interpreter). I will then draw a rough sketch of the jetted function specification and the implementation of its jet.

urwasm

urwasm is a project which aims at executing WebAssembly modules within an Urbit instance. Determinism is the key feature of Urbit computer, and in addition to that I want Wasm execution to be as fast as possible. Currently, a Wasm interpreter in Hoon is being developed, intended to be a complete Wasm runtime. However, it would not be a practical one due to its poor performance. To go from a merely complete interpreter to a practical Wasm runtime I will have to jet it.

Jetting

To jet the interpreter, I would have to pair a function written in Hoon with a code in C that must be extensionally equivalent to the Hoon code. In that sense function definition in Hoon would act as a formal mathematical specif

Mass update report

Introduction

A few months ago I started working on mass modernization project to learn about Arvo and Vere and to contribute to the core development. Since then I had some success working on WebAssembly interpreter, and I decided that it would be wise to focus my attention on that project instead.

I managed to achieve the first milestone outlined in the grant proposal. To ensure my efforts don't go in vain I will describe what I achieved and learned while working on that milestone.

Target audience

-mass test

Below are instructions to run -mass from this project:

  1. Clone my versions of vere and urbit repos (mind the branches, modernize-mass and mass-thread respectively)
  2. Build vere binary
  3. Boot a fakezod:
./vere/bazel-bin/pkg/vere/urbit -F zod -B urbit/bin/brass.pill -A urbit/pkg/arvo
  1. Run -mass in dojo, After the usual printfs, you should get something like:
@Quodss
Quodss / urwasm-todo.md
Last active November 2, 2023 15:10
urwasm TODO

Interpreter in Hoon

  • Change memory model: random access via (map page-number=@ page-array=@)
  • Revisit the model of block/loop/if and call/call_indirect execution: it appears that the current model causes unnecessary copies of the entire membuffer, slowing down the computation. So instead of cloning the core, interpreting an expression and back-propagating changes in the global state, I need to call an evaluating arm that will edit the global state in-place
  • Cache function fetching:
    • Have a gate fetch-operation within apply-instruction that takes term of an instruction as an argument and returns a number of operands to be consumed and a gate to be applied to the immediate arguments and consumed operands
    • Cache that gate
    • Fetching gate and operation gates must be defined outside of hwasm core
    • Use four letters for operators (shoudld speed things up since 4 letter term is a direct atom)
  • Floating point: verify roundings and such (iirc they are different in Wasm spec an