fuzz: improve the spec interpreter (#4881)

* fuzz: improve the API of the `wasm-spec-interpreter` crate

This change addresses key parts of #4852 by improving the bindings to
the OCaml spec interpreter. The new API allows users to `instantiate` a
module, `interpret` named functions on that instance, and `export`
globals and memories from that instance. This currently leaves the
existing implementation ("instantiate and interpret the first function in
a module") present under a new name: `interpret_legacy`.

* fuzz: adapt the differential spec engine to the new API

This removes the legacy uses in the differential spec engine, replacing
them with the new `instantiate`-`interpret`-`export` API from the
`wasm-spec-interpreter` crate.

* fix: make instance access thread-safe

This changes the OCaml-side definition of the instance so that each
instance carries round a reference to a "global store" that's specific
to that instantiation. Because everything is updated by reference there
should be no visible behavioural change on the Rust side, apart from
everything suddenly being thread-safe (modulo the fact that access to
the OCaml runtime still needs to be locked). This fix will need to be
generalised slightly in future if we want to allow multiple modules to
be instantiated in the same store.

Co-authored-by: conrad-watt <cnrdwtt@gmail.com>
Co-authored-by: Alex Crichton <alex@alexcrichton.com>
This commit is contained in:
Andrew Brown
2022-09-12 14:23:03 -07:00
committed by GitHub
parent 024cad7e3d
commit c3f8415ac7
8 changed files with 443 additions and 147 deletions

View File

@@ -9,9 +9,9 @@
//! - when the tools are not available, this library will panic at runtime (see
//! `without_library` module).
/// Enumerate the kinds of Wasm values.
/// Enumerate the kinds of Wasm values the OCaml interpreter can handle.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
pub enum SpecValue {
I32(i32),
I64(i64),
F32(i32),
@@ -19,6 +19,19 @@ pub enum Value {
V128(Vec<u8>),
}
/// Represents a WebAssembly export from the OCaml interpreter side.
#[allow(dead_code)]
pub enum SpecExport {
Global(SpecValue),
Memory(Vec<u8>),
}
/// Represents a WebAssembly instance from the OCaml interpreter side.
pub struct SpecInstance {
#[cfg(feature = "has-libinterpret")]
repr: ocaml_interop::BoxRoot<SpecInstance>,
}
#[cfg(feature = "has-libinterpret")]
mod with_library;
#[cfg(feature = "has-libinterpret")]
@@ -33,6 +46,7 @@ pub use without_library::*;
#[cfg(all(fuzzing, not(feature = "has-libinterpret")))]
compile_error!("The OCaml library was not built.");
/// Check if the OCaml spec interpreter bindings will work.
pub fn support_compiled_in() -> bool {
cfg!(feature = "has-libinterpret")
}