querycert / qcert

Compilation and Verification of Data-Centric Languages
https://querycert.github.io/
Apache License 2.0
56 stars 9 forks source link

WASM: shipping the runtime module #154

Closed pkel closed 3 years ago

pkel commented 3 years ago

Compiled-to-wasm contracts depend on a runtime.wasm module for execution. This module is implemented in another language (currently AssemblyScript, see /runtimes/assemblyscript). The module has to be shipped as part of the qcert opam package.

Currently, we use a dune to

  1. build runtime.wasm in the runtime tree
  2. copy runtime.wasm to the compiler tree
  3. translate the binary file to a OCaml hex-encoded string in wasm_runtime.ml
  4. (after compilation) read the binary wasm module from Wasm_runtime.runtime_wasm OCaml module

The rules looks like this

(rule
  (target runtime.wasm)
  (deps ../runtimes/assemblyscript/assembly/index.ts)
  (mode (promote (into wasm)))
  (action
    (progn
      (system "cd ../../../runtimes/assemblyscript && npm run asbuild:untouched")
      (with-stdout-to runtime.wasm
        (system "cat ../../../runtimes/assemblyscript/build/untouched.wasm"))
    )
  )
)

(rule
  (target wasm_runtime.ml)
  (deps runtime.wasm)
  (action
    (with-stdout-to wasm_runtime.ml
      (progn (echo "let runtime_wasm = \"")
             (run ../tools/binary_to_string.exe runtime.wasm)
             (echo "\"\n")
      )
    )
  )
)

Practical, but not particularly nice.

Dune (recently?!) added support for shipping additional files and locate/load them at runtime. It's marked experimental in the dune documentation. As soon as there is a stable API, we might consider replacing the above mechanism with the new.

pkel commented 3 years ago

The first rule navigates the file system with relative paths. This does not work if qcert is symlink into another project.

Additionally, the use of npm breaks opam build.

Idea: move the two rules into the assemblyscript dir, create the ml file there, add dune file and create library. Dune will figure out the locations for us

jeromesimeon commented 3 years ago

For reference, error when doing opam install .

### output ###
# Use Stdlib instead.
# [...]
# If you need to stay compatible with OCaml < 4.07, you can use the 
# stdlib-shims library: https://github.com/ocaml/stdlib-shims
File "compiler/dune", line 34, characters 0-253:
# 34 | (rule
# 35 |   (target wasm_runtime.ml)
# 36 |   (deps runtime.wasm)
# ....43 |     )
# 44 |   )
# 45 | )
# Error: No rule found for compiler/runtime.wasm
pkel commented 3 years ago

Progress in 48bfd7f.