Add a compile command to Wasmtime.
This commit adds a `compile` command to the Wasmtime CLI. The command can be used to Ahead-Of-Time (AOT) compile WebAssembly modules. With the `all-arch` feature enabled, AOT compilation can be performed for non-native architectures (i.e. cross-compilation). The `Module::compile` method has been added to perform AOT compilation. A few of the CLI flags relating to "on by default" Wasm features have been changed to be "--disable-XYZ" flags. A simple example of using the `wasmtime compile` command: ```text $ wasmtime compile input.wasm $ wasmtime input.cwasm ```
This commit is contained in:
@@ -10,7 +10,9 @@ pub mod ir {
|
||||
}
|
||||
|
||||
pub mod settings {
|
||||
pub use cranelift_codegen::settings::{builder, Builder, Configurable, Flags, SetError};
|
||||
pub use cranelift_codegen::settings::{
|
||||
builder, Builder, Configurable, Flags, OptLevel, SetError,
|
||||
};
|
||||
}
|
||||
|
||||
pub mod isa {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Tunable parameters for WebAssembly compilation.
|
||||
#[derive(Clone, Hash)]
|
||||
#[derive(Clone, Hash, Serialize, Deserialize)]
|
||||
pub struct Tunables {
|
||||
/// For static heaps, the size in wasm pages of the heap protected by bounds checking.
|
||||
pub static_memory_bound: u32,
|
||||
|
||||
@@ -46,6 +46,7 @@ lightbeam = ["wasmtime-lightbeam"]
|
||||
jitdump = ["wasmtime-profiling/jitdump"]
|
||||
vtune = ["wasmtime-profiling/vtune"]
|
||||
parallel-compilation = ["rayon"]
|
||||
all-arch = ["cranelift-codegen/all-arch"]
|
||||
|
||||
# Try the experimental, work-in-progress new x86_64 backend. This is not stable
|
||||
# as of June 2020.
|
||||
|
||||
@@ -312,7 +312,7 @@ impl CodeMemory {
|
||||
}
|
||||
}
|
||||
|
||||
// Register all unwind entiries for functions and trampolines.
|
||||
// Register all unwind entires for functions and trampolines.
|
||||
// TODO will `u32` type for start/len be enough for large code base.
|
||||
for i in unwind_info {
|
||||
match i {
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::object::{build_object, ObjectUnwindInfo};
|
||||
use object::write::Object;
|
||||
#[cfg(feature = "parallel-compilation")]
|
||||
use rayon::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::mem;
|
||||
use wasmparser::WasmFeatures;
|
||||
@@ -18,7 +19,7 @@ use wasmtime_environ::{
|
||||
};
|
||||
|
||||
/// Select which kind of compilation to use.
|
||||
#[derive(Copy, Clone, Debug, Hash)]
|
||||
#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
|
||||
pub enum CompilationStrategy {
|
||||
/// Let Wasmtime pick the strategy.
|
||||
Auto,
|
||||
@@ -108,6 +109,11 @@ impl Compiler {
|
||||
self.isa.as_ref()
|
||||
}
|
||||
|
||||
/// Return the compiler's strategy.
|
||||
pub fn strategy(&self) -> CompilationStrategy {
|
||||
self.strategy
|
||||
}
|
||||
|
||||
/// Return the target's frontend configuration settings.
|
||||
pub fn frontend_config(&self) -> TargetFrontendConfig {
|
||||
self.isa.frontend_config()
|
||||
|
||||
@@ -438,7 +438,7 @@ fn build_code_memory(
|
||||
isa: &dyn TargetIsa,
|
||||
obj: &[u8],
|
||||
module: &Module,
|
||||
unwind_info: &Box<[ObjectUnwindInfo]>,
|
||||
unwind_info: &[ObjectUnwindInfo],
|
||||
) -> Result<
|
||||
(
|
||||
CodeMemory,
|
||||
|
||||
@@ -79,3 +79,6 @@ async = ["wasmtime-fiber", "wasmtime-runtime/async"]
|
||||
|
||||
# Enables userfaultfd support in the runtime's pooling allocator when building on Linux
|
||||
uffd = ["wasmtime-runtime/uffd"]
|
||||
|
||||
# Enables support for all architectures in JIT and the `wasmtime compile` CLI command.
|
||||
all-arch = ["wasmtime-jit/all-arch"]
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::memory::MemoryCreator;
|
||||
use crate::trampoline::MemoryCreatorProxy;
|
||||
use crate::{func::HostFunc, Caller, FuncType, IntoFunc, Trap, Val, WasmRet, WasmTy};
|
||||
use anyhow::{bail, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
@@ -391,6 +392,20 @@ impl Config {
|
||||
/// Creates a new configuration object with the default configuration
|
||||
/// specified.
|
||||
pub fn new() -> Self {
|
||||
Self::new_with_isa_flags(native::builder())
|
||||
}
|
||||
|
||||
/// Creates a [`Config`] for the given target triple.
|
||||
///
|
||||
/// No CPU flags will be enabled for the config.
|
||||
pub fn for_target(target: &str) -> Result<Self> {
|
||||
use std::str::FromStr;
|
||||
Ok(Self::new_with_isa_flags(native::lookup(
|
||||
target_lexicon::Triple::from_str(target).map_err(|e| anyhow::anyhow!(e))?,
|
||||
)?))
|
||||
}
|
||||
|
||||
fn new_with_isa_flags(isa_flags: isa::Builder) -> Self {
|
||||
let mut flags = settings::builder();
|
||||
|
||||
// There are two possible traps for division, and this way
|
||||
@@ -414,10 +429,15 @@ impl Config {
|
||||
.set("enable_probestack", "false")
|
||||
.expect("should be valid flag");
|
||||
|
||||
// Reference types are enabled by default, so enable safepoints
|
||||
flags
|
||||
.set("enable_safepoints", "true")
|
||||
.expect("should be valid flag");
|
||||
|
||||
let mut ret = Self {
|
||||
tunables: Tunables::default(),
|
||||
flags,
|
||||
isa_flags: native::builder(),
|
||||
isa_flags,
|
||||
strategy: CompilationStrategy::Auto,
|
||||
#[cfg(feature = "cache")]
|
||||
cache_config: CacheConfig::new_cache_disabled(),
|
||||
@@ -898,6 +918,33 @@ impl Config {
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows setting a Cranelift boolean flag or preset. This allows
|
||||
/// fine-tuning of Cranelift settings.
|
||||
///
|
||||
/// Since Cranelift flags may be unstable, this method should not be considered to be stable
|
||||
/// either; other `Config` functions should be preferred for stability.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This is marked as unsafe, because setting the wrong flag might break invariants,
|
||||
/// resulting in execution hazards.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This method can fail if the flag's name does not exist.
|
||||
pub unsafe fn cranelift_flag_enable(&mut self, flag: &str) -> Result<&mut Self> {
|
||||
if let Err(err) = self.flags.enable(flag) {
|
||||
match err {
|
||||
SetError::BadName(_) => {
|
||||
// Try the target-specific flags.
|
||||
self.isa_flags.enable(flag)?;
|
||||
}
|
||||
_ => bail!(err),
|
||||
}
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Allows settings another Cranelift flag defined by a flag name and value. This allows
|
||||
/// fine-tuning of Cranelift settings.
|
||||
///
|
||||
@@ -1419,7 +1466,7 @@ pub enum Strategy {
|
||||
|
||||
/// Possible optimization levels for the Cranelift codegen backend.
|
||||
#[non_exhaustive]
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
||||
pub enum OptLevel {
|
||||
/// No optimizations performed, minimizes compilation time by disabling most
|
||||
/// optimizations.
|
||||
|
||||
@@ -175,6 +175,10 @@
|
||||
//! lock contention is hampering multithreading throughput. This feature is only
|
||||
//! supported on Linux and requires a Linux kernel version 4.11 or higher.
|
||||
//!
|
||||
//! * `all-arch` - Not enabled by default. This feature compiles in support for
|
||||
//! all architectures for both the JIT compiler and the `wasmtime compile` CLI
|
||||
//! command.
|
||||
//!
|
||||
//! ## Examples
|
||||
//!
|
||||
//! In addition to the examples below be sure to check out the [online embedding
|
||||
|
||||
@@ -2,9 +2,8 @@ use crate::types::{ExportType, ExternType, ImportType};
|
||||
use crate::{Engine, ModuleType};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use bincode::Options;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use wasmparser::Validator;
|
||||
@@ -14,6 +13,12 @@ use wasmtime_environ::entity::PrimaryMap;
|
||||
use wasmtime_environ::wasm::ModuleIndex;
|
||||
use wasmtime_jit::{CompilationArtifacts, CompiledModule, TypeTables};
|
||||
|
||||
mod serialization;
|
||||
|
||||
use serialization::SerializedModule;
|
||||
|
||||
const COMPILED_MODULE_HEADER: &[u8] = b"\0aot";
|
||||
|
||||
/// A compiled WebAssembly module, ready to be instantiated.
|
||||
///
|
||||
/// A `Module` is a compiled in-memory representation of an input WebAssembly
|
||||
@@ -30,7 +35,7 @@ use wasmtime_jit::{CompilationArtifacts, CompiledModule, TypeTables};
|
||||
/// compiling the original wasm module only once with a single [`Module`]
|
||||
/// instance.
|
||||
///
|
||||
/// The `Module` is threadsafe and safe to share accross threads.
|
||||
/// The `Module` is thread-safe and safe to share across threads.
|
||||
///
|
||||
/// ## Modules and `Clone`
|
||||
///
|
||||
@@ -103,75 +108,26 @@ struct ModuleInner {
|
||||
types: Arc<TypeTables>,
|
||||
}
|
||||
|
||||
/// A small helper struct which defines modules are serialized.
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct ModuleSerialized<'a> {
|
||||
/// All compiled artifacts neeeded by this module, where the last entry in
|
||||
/// this list is the artifacts for the module itself.
|
||||
artifacts: Vec<MyCow<'a, CompilationArtifacts>>,
|
||||
/// Closed-over module values that are also needed for this module.
|
||||
modules: Vec<ModuleSerialized<'a>>,
|
||||
/// The index into the list of type tables that are used for this module's
|
||||
/// type tables.
|
||||
type_tables: usize,
|
||||
}
|
||||
|
||||
// This is like `std::borrow::Cow` but it doesn't have a `Clone` bound on `T`
|
||||
enum MyCow<'a, T> {
|
||||
Borrowed(&'a T),
|
||||
Owned(T),
|
||||
}
|
||||
|
||||
impl<'a, T> MyCow<'a, T> {
|
||||
fn unwrap_owned(self) -> T {
|
||||
match self {
|
||||
MyCow::Owned(val) => val,
|
||||
MyCow::Borrowed(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Serialize> Serialize for MyCow<'a, T> {
|
||||
fn serialize<S>(&self, dst: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::ser::Serializer,
|
||||
{
|
||||
match self {
|
||||
MyCow::Borrowed(val) => val.serialize(dst),
|
||||
MyCow::Owned(val) => val.serialize(dst),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T: Deserialize<'a>> Deserialize<'a> for MyCow<'b, T> {
|
||||
fn deserialize<D>(src: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::de::Deserializer<'a>,
|
||||
{
|
||||
Ok(MyCow::Owned(T::deserialize(src)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Module {
|
||||
/// Creates a new WebAssembly `Module` from the given in-memory `bytes`.
|
||||
///
|
||||
/// The `bytes` provided must be in one of two formats:
|
||||
/// The `bytes` provided must be in one of three formats:
|
||||
///
|
||||
/// * It can be a [binary-encoded][binary] WebAssembly module. This
|
||||
/// is always supported.
|
||||
/// * It may also be a [text-encoded][text] instance of the WebAssembly
|
||||
/// text format. This is only supported when the `wat` feature of this
|
||||
/// crate is enabled. If this is supplied then the text format will be
|
||||
/// parsed before validation. Note that the `wat` feature is enabled by
|
||||
/// default.
|
||||
/// * A [binary-encoded][binary] WebAssembly module. This is always supported.
|
||||
/// * A [text-encoded][text] instance of the WebAssembly text format.
|
||||
/// This is only supported when the `wat` feature of this crate is enabled.
|
||||
/// If this is supplied then the text format will be parsed before validation.
|
||||
/// Note that the `wat` feature is enabled by default.
|
||||
/// * A module compiled with [`Module::compile`] or the `wasmtime compile` command.
|
||||
///
|
||||
/// The data for the wasm module must be loaded in-memory if it's present
|
||||
/// elsewhere, for example on disk. This requires that the entire binary is
|
||||
/// loaded into memory all at once, this API does not support streaming
|
||||
/// compilation of a module.
|
||||
///
|
||||
/// The WebAssembly binary will be decoded and validated. It will also be
|
||||
/// compiled according to the configuration of the provided `engine`.
|
||||
/// If the module has not been already been compiled, the WebAssembly binary will
|
||||
/// be decoded and validated. It will also be compiled according to the
|
||||
/// configuration of the provided `engine`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
@@ -184,7 +140,7 @@ impl Module {
|
||||
/// * Implementation-specific limits were exceeded with a valid binary (for
|
||||
/// example too many locals)
|
||||
/// * The wasm binary may use features that are not enabled in the
|
||||
/// configuration of `enging`
|
||||
/// configuration of `engine`
|
||||
/// * If the `wat` feature is enabled and the input is text, then it may be
|
||||
/// rejected if it fails to parse.
|
||||
///
|
||||
@@ -220,9 +176,14 @@ impl Module {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn new(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Module> {
|
||||
let bytes = bytes.as_ref();
|
||||
if bytes.starts_with(COMPILED_MODULE_HEADER) {
|
||||
return Self::deserialize(engine, &bytes[COMPILED_MODULE_HEADER.len()..]);
|
||||
}
|
||||
|
||||
#[cfg(feature = "wat")]
|
||||
let bytes = wat::parse_bytes(bytes.as_ref())?;
|
||||
Module::from_binary(engine, bytes.as_ref())
|
||||
let bytes = wat::parse_bytes(bytes)?;
|
||||
Self::from_binary(engine, &bytes)
|
||||
}
|
||||
|
||||
/// Creates a new WebAssembly `Module` from the given in-memory `binary`
|
||||
@@ -230,7 +191,7 @@ impl Module {
|
||||
///
|
||||
/// See [`Module::new`] for other details.
|
||||
pub fn new_with_name(engine: &Engine, bytes: impl AsRef<[u8]>, name: &str) -> Result<Module> {
|
||||
let mut module = Module::new(engine, bytes.as_ref())?;
|
||||
let mut module = Self::new(engine, bytes.as_ref())?;
|
||||
Arc::get_mut(&mut Arc::get_mut(&mut module.inner).unwrap().module)
|
||||
.unwrap()
|
||||
.module_mut()
|
||||
@@ -268,21 +229,20 @@ impl Module {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn from_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Module> {
|
||||
#[cfg(feature = "wat")]
|
||||
let wasm = wat::parse_file(file)?;
|
||||
#[cfg(not(feature = "wat"))]
|
||||
let wasm = std::fs::read(file)?;
|
||||
Module::new(engine, &wasm)
|
||||
Self::new(
|
||||
engine,
|
||||
&fs::read(file).with_context(|| "failed to read input file")?,
|
||||
)
|
||||
}
|
||||
|
||||
/// Creates a new WebAssembly `Module` from the given in-memory `binary`
|
||||
/// data.
|
||||
///
|
||||
/// This is similar to [`Module::new`] except that it requires that the
|
||||
/// `binary` input is a WebAssembly binary, the text format is not supported
|
||||
/// by this function. It's generally recommended to use [`Module::new`],
|
||||
/// but if it's required to not support the text format this function can be
|
||||
/// used instead.
|
||||
/// `binary` input is a WebAssembly binary or a compiled module, the
|
||||
/// text format is not supported by this function. It's generally
|
||||
/// recommended to use [`Module::new`], but if it's required to not
|
||||
/// support the text format this function can be used instead.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@@ -307,6 +267,10 @@ impl Module {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn from_binary(engine: &Engine, binary: &[u8]) -> Result<Module> {
|
||||
if binary.starts_with(COMPILED_MODULE_HEADER) {
|
||||
return Self::deserialize(engine, &binary[COMPILED_MODULE_HEADER.len()..]);
|
||||
}
|
||||
|
||||
const USE_PAGED_MEM_INIT: bool = cfg!(all(feature = "uffd", target_os = "linux"));
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
@@ -371,6 +335,43 @@ impl Module {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ahead-of-time (AOT) compiles a WebAssembly module.
|
||||
///
|
||||
/// The `bytes` provided must be in one of two formats:
|
||||
///
|
||||
/// * A [binary-encoded][binary] WebAssembly module. This is always supported.
|
||||
/// * A [text-encoded][text] instance of the WebAssembly text format.
|
||||
/// This is only supported when the `wat` feature of this crate is enabled.
|
||||
/// If this is supplied then the text format will be parsed before validation.
|
||||
/// Note that the `wat` feature is enabled by default.
|
||||
///
|
||||
/// See [`Module::new`] for errors that may be returned by this function.
|
||||
///
|
||||
/// [binary]: https://webassembly.github.io/spec/core/binary/index.html
|
||||
/// [text]: https://webassembly.github.io/spec/core/text/index.html
|
||||
pub fn compile(engine: &Engine, bytes: &[u8], mut output: impl Write) -> Result<()> {
|
||||
const USE_PAGED_MEM_INIT: bool = cfg!(all(feature = "uffd", target_os = "linux"));
|
||||
|
||||
if bytes.starts_with(COMPILED_MODULE_HEADER) {
|
||||
bail!("input is already a compiled module");
|
||||
}
|
||||
|
||||
#[cfg(feature = "wat")]
|
||||
let bytes = wat::parse_bytes(&bytes)?;
|
||||
|
||||
let (_, artifacts, types) =
|
||||
CompilationArtifacts::build(engine.compiler(), &bytes, USE_PAGED_MEM_INIT)?;
|
||||
|
||||
// Write a header that marks this as a compiled module
|
||||
output.write_all(COMPILED_MODULE_HEADER)?;
|
||||
bincode_options().serialize_into(
|
||||
output,
|
||||
&SerializedModule::from_artifacts(engine.compiler(), &artifacts, &types),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the type signature of this module.
|
||||
pub fn ty(&self) -> ModuleType {
|
||||
let mut sig = ModuleType::new();
|
||||
@@ -388,48 +389,13 @@ impl Module {
|
||||
sig
|
||||
}
|
||||
|
||||
/// Serialize compilation artifacts to the buffer. See also `deseriaize`.
|
||||
/// Serialize compilation artifacts to the buffer. See also `deserialize`.
|
||||
pub fn serialize(&self) -> Result<Vec<u8>> {
|
||||
let mut pushed = HashMap::new();
|
||||
let mut tables = Vec::new();
|
||||
let module = self.serialized_module(&mut pushed, &mut tables);
|
||||
let artifacts = (compiler_fingerprint(self.engine()), tables, module);
|
||||
let buffer = bincode_options().serialize(&artifacts)?;
|
||||
let mut buffer = Vec::new();
|
||||
bincode_options().serialize_into(&mut buffer, &SerializedModule::new(self))?;
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
fn serialized_module<'a>(
|
||||
&'a self,
|
||||
type_tables_pushed: &mut HashMap<usize, usize>,
|
||||
type_tables: &mut Vec<&'a TypeTables>,
|
||||
) -> ModuleSerialized<'a> {
|
||||
// Deduplicate `Arc<TypeTables>` using our two parameters to ensure we
|
||||
// serialize type tables as little as possible.
|
||||
let ptr = Arc::as_ptr(self.types());
|
||||
let type_tables_idx = *type_tables_pushed.entry(ptr as usize).or_insert_with(|| {
|
||||
type_tables.push(self.types());
|
||||
type_tables.len() - 1
|
||||
});
|
||||
ModuleSerialized {
|
||||
artifacts: self
|
||||
.inner
|
||||
.artifact_upvars
|
||||
.iter()
|
||||
.map(|i| MyCow::Borrowed(i.compilation_artifacts()))
|
||||
.chain(Some(MyCow::Borrowed(
|
||||
self.compiled_module().compilation_artifacts(),
|
||||
)))
|
||||
.collect(),
|
||||
modules: self
|
||||
.inner
|
||||
.module_upvars
|
||||
.iter()
|
||||
.map(|i| i.serialized_module(type_tables_pushed, type_tables))
|
||||
.collect(),
|
||||
type_tables: type_tables_idx,
|
||||
}
|
||||
}
|
||||
|
||||
/// Deserializes and creates a module from the compilation artifacts.
|
||||
/// The `serialize` saves the compilation artifacts along with the host
|
||||
/// fingerprint, which consists of target, compiler flags, and wasmtime
|
||||
@@ -437,49 +403,13 @@ impl Module {
|
||||
///
|
||||
/// The method will fail if fingerprints of current host and serialized
|
||||
/// one are different. The method does not verify the serialized artifacts
|
||||
/// for modifications or curruptions. All responsibily of signing and its
|
||||
/// for modifications or corruptions. All responsibly of signing and its
|
||||
/// verification falls on the embedder.
|
||||
pub fn deserialize(engine: &Engine, serialized: &[u8]) -> Result<Module> {
|
||||
let (fingerprint, types, serialized) = bincode_options()
|
||||
.deserialize::<(u64, Vec<TypeTables>, _)>(serialized)
|
||||
.context("Deserialize compilation artifacts")?;
|
||||
|
||||
if fingerprint != compiler_fingerprint(engine) {
|
||||
bail!("Incompatible compilation artifact");
|
||||
}
|
||||
|
||||
let types = types.into_iter().map(Arc::new).collect::<Vec<_>>();
|
||||
return mk(engine, &types, serialized);
|
||||
|
||||
fn mk(
|
||||
engine: &Engine,
|
||||
types: &Vec<Arc<TypeTables>>,
|
||||
module: ModuleSerialized<'_>,
|
||||
) -> Result<Module> {
|
||||
let mut artifacts = CompiledModule::from_artifacts_list(
|
||||
module
|
||||
.artifacts
|
||||
.into_iter()
|
||||
.map(|i| i.unwrap_owned())
|
||||
.collect(),
|
||||
engine.compiler().isa(),
|
||||
&*engine.config().profiler,
|
||||
)?;
|
||||
let inner = ModuleInner {
|
||||
engine: engine.clone(),
|
||||
types: types[module.type_tables].clone(),
|
||||
module: artifacts.pop().unwrap(),
|
||||
artifact_upvars: artifacts,
|
||||
module_upvars: module
|
||||
.modules
|
||||
.into_iter()
|
||||
.map(|m| mk(engine, types, m))
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
};
|
||||
Ok(Module {
|
||||
inner: Arc::new(inner),
|
||||
})
|
||||
}
|
||||
bincode_options()
|
||||
.deserialize::<SerializedModule<'_>>(serialized)
|
||||
.context("Deserialize compilation artifacts")?
|
||||
.into_module(engine)
|
||||
}
|
||||
|
||||
/// Creates a submodule `Module` value from the specified parameters.
|
||||
@@ -493,7 +423,7 @@ impl Module {
|
||||
/// the upvars array in the submodule to be created, and each element of
|
||||
/// this array is an index into this module's upvar array.
|
||||
/// * `module_upvars` - similar to `artifact_upvars` this is a mapping of
|
||||
/// how to create the e`module_upvars` of the submodule being created.
|
||||
/// how to create the `module_upvars` of the submodule being created.
|
||||
/// Each entry in this array is either an index into this module's own
|
||||
/// module upvars array or it's an index into `modules`, the list of
|
||||
/// modules so far for the instance where this submodule is being
|
||||
@@ -775,13 +705,6 @@ fn bincode_options() -> impl Options {
|
||||
bincode::DefaultOptions::new().with_varint_encoding()
|
||||
}
|
||||
|
||||
fn compiler_fingerprint(engine: &Engine) -> u64 {
|
||||
use std::hash::Hasher;
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
engine.compiler().hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
fn _assert_send_sync() {
|
||||
fn _assert<T: Send + Sync>() {}
|
||||
_assert::<Module>();
|
||||
|
||||
715
crates/wasmtime/src/module/serialization.rs
Normal file
715
crates/wasmtime/src/module/serialization.rs
Normal file
@@ -0,0 +1,715 @@
|
||||
//! Implements module serialization.
|
||||
|
||||
use super::ModuleInner;
|
||||
use crate::{Engine, Module, OptLevel};
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use wasmtime_environ::Tunables;
|
||||
use wasmtime_environ::{isa::TargetIsa, settings};
|
||||
use wasmtime_jit::{
|
||||
CompilationArtifacts, CompilationStrategy, CompiledModule, Compiler, TypeTables,
|
||||
};
|
||||
|
||||
// This exists because `wasmparser::WasmFeatures` isn't serializable
|
||||
#[derive(Hash, Debug, Copy, Clone, Serialize, Deserialize)]
|
||||
struct WasmFeatures {
|
||||
pub reference_types: bool,
|
||||
pub multi_value: bool,
|
||||
pub bulk_memory: bool,
|
||||
pub module_linking: bool,
|
||||
pub simd: bool,
|
||||
pub threads: bool,
|
||||
pub tail_call: bool,
|
||||
pub deterministic_only: bool,
|
||||
pub multi_memory: bool,
|
||||
pub exceptions: bool,
|
||||
pub memory64: bool,
|
||||
}
|
||||
|
||||
impl From<&wasmparser::WasmFeatures> for WasmFeatures {
|
||||
fn from(other: &wasmparser::WasmFeatures) -> Self {
|
||||
Self {
|
||||
reference_types: other.reference_types,
|
||||
multi_value: other.multi_value,
|
||||
bulk_memory: other.bulk_memory,
|
||||
module_linking: other.module_linking,
|
||||
simd: other.simd,
|
||||
threads: other.threads,
|
||||
tail_call: other.tail_call,
|
||||
deterministic_only: other.deterministic_only,
|
||||
multi_memory: other.multi_memory,
|
||||
exceptions: other.exceptions,
|
||||
memory64: other.memory64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This is like `std::borrow::Cow` but it doesn't have a `Clone` bound on `T`
|
||||
enum MyCow<'a, T> {
|
||||
Borrowed(&'a T),
|
||||
Owned(T),
|
||||
}
|
||||
|
||||
impl<'a, T> MyCow<'a, T> {
|
||||
fn unwrap_owned(self) -> T {
|
||||
match self {
|
||||
MyCow::Owned(val) => val,
|
||||
MyCow::Borrowed(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Serialize> Serialize for MyCow<'a, T> {
|
||||
fn serialize<S>(&self, dst: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::ser::Serializer,
|
||||
{
|
||||
match self {
|
||||
MyCow::Borrowed(val) => val.serialize(dst),
|
||||
MyCow::Owned(val) => val.serialize(dst),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T: Deserialize<'a>> Deserialize<'a> for MyCow<'b, T> {
|
||||
fn deserialize<D>(src: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::de::Deserializer<'a>,
|
||||
{
|
||||
Ok(MyCow::Owned(T::deserialize(src)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<settings::OptLevel> for OptLevel {
|
||||
fn from(level: settings::OptLevel) -> Self {
|
||||
match level {
|
||||
settings::OptLevel::Speed => OptLevel::Speed,
|
||||
settings::OptLevel::SpeedAndSize => OptLevel::SpeedAndSize,
|
||||
settings::OptLevel::None => OptLevel::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A small helper struct which defines modules are serialized.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct SerializedModuleData<'a> {
|
||||
/// All compiled artifacts needed by this module, where the last entry in
|
||||
/// this list is the artifacts for the module itself.
|
||||
artifacts: Vec<MyCow<'a, CompilationArtifacts>>,
|
||||
/// Closed-over module values that are also needed for this module.
|
||||
modules: Vec<SerializedModuleData<'a>>,
|
||||
/// The index into the list of type tables that are used for this module's
|
||||
/// type tables.
|
||||
type_tables: usize,
|
||||
}
|
||||
|
||||
impl<'a> SerializedModuleData<'a> {
|
||||
pub fn new(module: &'a Module) -> (Self, Vec<MyCow<'a, TypeTables>>) {
|
||||
let mut pushed = HashMap::new();
|
||||
let mut tables = Vec::new();
|
||||
return (module_data(module, &mut pushed, &mut tables), tables);
|
||||
|
||||
fn module_data<'a>(
|
||||
module: &'a Module,
|
||||
type_tables_pushed: &mut HashMap<usize, usize>,
|
||||
type_tables: &mut Vec<MyCow<'a, TypeTables>>,
|
||||
) -> SerializedModuleData<'a> {
|
||||
// Deduplicate `Arc<TypeTables>` using our two parameters to ensure we
|
||||
// serialize type tables as little as possible.
|
||||
let ptr = Arc::as_ptr(module.types());
|
||||
let type_tables_idx = *type_tables_pushed.entry(ptr as usize).or_insert_with(|| {
|
||||
type_tables.push(MyCow::Borrowed(module.types()));
|
||||
type_tables.len() - 1
|
||||
});
|
||||
SerializedModuleData {
|
||||
artifacts: module
|
||||
.inner
|
||||
.artifact_upvars
|
||||
.iter()
|
||||
.map(|i| MyCow::Borrowed(i.compilation_artifacts()))
|
||||
.chain(Some(MyCow::Borrowed(
|
||||
module.compiled_module().compilation_artifacts(),
|
||||
)))
|
||||
.collect(),
|
||||
modules: module
|
||||
.inner
|
||||
.module_upvars
|
||||
.iter()
|
||||
.map(|i| module_data(i, type_tables_pushed, type_tables))
|
||||
.collect(),
|
||||
type_tables: type_tables_idx,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SerializedModule<'a> {
|
||||
version: String,
|
||||
target: String,
|
||||
flags_hash: u64,
|
||||
// Record the opt level as it is the most common Cranelift flag users might change
|
||||
opt_level: OptLevel,
|
||||
isa_flags: Vec<String>,
|
||||
strategy: CompilationStrategy,
|
||||
tunables: Tunables,
|
||||
features: WasmFeatures,
|
||||
data: SerializedModuleData<'a>,
|
||||
tables: Vec<MyCow<'a, TypeTables>>,
|
||||
}
|
||||
|
||||
impl<'a> SerializedModule<'a> {
|
||||
pub fn new(module: &'a Module) -> Self {
|
||||
let (data, tables) = SerializedModuleData::new(module);
|
||||
Self::with_data(module.engine().compiler(), data, tables)
|
||||
}
|
||||
|
||||
pub fn from_artifacts(
|
||||
compiler: &Compiler,
|
||||
artifacts: &'a Vec<CompilationArtifacts>,
|
||||
types: &'a TypeTables,
|
||||
) -> Self {
|
||||
Self::with_data(
|
||||
compiler,
|
||||
SerializedModuleData {
|
||||
artifacts: artifacts.iter().map(MyCow::Borrowed).collect(),
|
||||
modules: Vec::new(),
|
||||
type_tables: 0,
|
||||
},
|
||||
vec![MyCow::Borrowed(types)],
|
||||
)
|
||||
}
|
||||
|
||||
fn with_data(
|
||||
compiler: &Compiler,
|
||||
data: SerializedModuleData<'a>,
|
||||
tables: Vec<MyCow<'a, TypeTables>>,
|
||||
) -> Self {
|
||||
let isa = compiler.isa();
|
||||
|
||||
Self {
|
||||
version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
target: isa.triple().to_string(),
|
||||
opt_level: isa.flags().opt_level().into(),
|
||||
flags_hash: Self::simple_hash(isa.flags()),
|
||||
isa_flags: isa.enabled_isa_flags(),
|
||||
strategy: compiler.strategy(),
|
||||
tunables: compiler.tunables().clone(),
|
||||
features: compiler.features().into(),
|
||||
data,
|
||||
tables,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_module(self, engine: &Engine) -> Result<Module> {
|
||||
let compiler = engine.compiler();
|
||||
let isa = compiler.isa();
|
||||
|
||||
self.check_version()?;
|
||||
self.check_triple(isa)?;
|
||||
self.check_isa_flags(isa)?;
|
||||
self.check_strategy(compiler)?;
|
||||
self.check_tunables(compiler)?;
|
||||
self.check_features(compiler)?;
|
||||
|
||||
// Check the flags last as they are the least helpful in terms of diagnostic message
|
||||
self.check_flags(isa)?;
|
||||
|
||||
let types = self
|
||||
.tables
|
||||
.into_iter()
|
||||
.map(|t| Arc::new(t.unwrap_owned()))
|
||||
.collect::<Vec<_>>();
|
||||
let module = mk(engine, &types, self.data)?;
|
||||
|
||||
// Validate the module can be used with the current allocator
|
||||
engine.allocator().validate(module.inner.module.module())?;
|
||||
|
||||
return Ok(module);
|
||||
|
||||
fn mk(
|
||||
engine: &Engine,
|
||||
types: &Vec<Arc<TypeTables>>,
|
||||
data: SerializedModuleData<'_>,
|
||||
) -> Result<Module> {
|
||||
let mut artifacts = CompiledModule::from_artifacts_list(
|
||||
data.artifacts
|
||||
.into_iter()
|
||||
.map(|i| i.unwrap_owned())
|
||||
.collect(),
|
||||
engine.compiler().isa(),
|
||||
&*engine.config().profiler,
|
||||
)?;
|
||||
let inner = ModuleInner {
|
||||
engine: engine.clone(),
|
||||
types: types[data.type_tables].clone(),
|
||||
module: artifacts.pop().unwrap(),
|
||||
artifact_upvars: artifacts,
|
||||
module_upvars: data
|
||||
.modules
|
||||
.into_iter()
|
||||
.map(|m| mk(engine, types, m))
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
};
|
||||
|
||||
Ok(Module {
|
||||
inner: Arc::new(inner),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn check_version(&self) -> Result<()> {
|
||||
if self.version != env!("CARGO_PKG_VERSION") {
|
||||
bail!(
|
||||
"Module was compiled with Wasmtime version '{}'",
|
||||
self.version
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_triple(&self, isa: &dyn TargetIsa) -> Result<()> {
|
||||
let triple = target_lexicon::Triple::from_str(&self.target).map_err(|e| anyhow!(e))?;
|
||||
|
||||
if triple.architecture != isa.triple().architecture {
|
||||
bail!(
|
||||
"Module was compiled for architecture '{}'",
|
||||
triple.architecture
|
||||
);
|
||||
}
|
||||
|
||||
if triple.operating_system != isa.triple().operating_system {
|
||||
bail!(
|
||||
"Module was compiled for operating system '{}'",
|
||||
triple.operating_system
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_flags(&self, isa: &dyn TargetIsa) -> Result<()> {
|
||||
let host_level = isa.flags().opt_level().into();
|
||||
if self.opt_level != host_level {
|
||||
bail!("Module was compiled with optimization level '{:?}' but '{:?}' is expected for the host", self.opt_level, host_level);
|
||||
}
|
||||
|
||||
if self.flags_hash != Self::simple_hash(isa.flags()) {
|
||||
bail!("Module was compiled with different Cranelift flags than the host");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_isa_flags(&self, isa: &dyn TargetIsa) -> Result<()> {
|
||||
for flag in &self.isa_flags {
|
||||
if !isa.is_flag_enabled(flag) {
|
||||
bail!("Host is missing CPU flag '{}'", flag);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_strategy(&self, compiler: &Compiler) -> Result<()> {
|
||||
#[allow(unreachable_patterns)]
|
||||
let matches = match (self.strategy, compiler.strategy()) {
|
||||
(CompilationStrategy::Auto, CompilationStrategy::Auto)
|
||||
| (CompilationStrategy::Auto, CompilationStrategy::Cranelift)
|
||||
| (CompilationStrategy::Cranelift, CompilationStrategy::Auto)
|
||||
| (CompilationStrategy::Cranelift, CompilationStrategy::Cranelift) => true,
|
||||
#[cfg(feature = "lightbeam")]
|
||||
(CompilationStrategy::Lightbeam, CompilationStrategy::Lightbeam) => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if !matches {
|
||||
bail!("Module was compiled with strategy '{:?}'", self.strategy);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_int<T: Eq + std::fmt::Display>(found: T, expected: T, feature: &str) -> Result<()> {
|
||||
if found == expected {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
bail!(
|
||||
"Module was compiled with a {} of '{}' but '{}' is expected for the host",
|
||||
feature,
|
||||
found,
|
||||
expected
|
||||
);
|
||||
}
|
||||
|
||||
fn check_bool(found: bool, expected: bool, feature: &str) -> Result<()> {
|
||||
if found == expected {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
bail!(
|
||||
"Module was compiled {} {} but it {} enabled for the host",
|
||||
if found { "with" } else { "without" },
|
||||
feature,
|
||||
if expected { "is" } else { "is not" }
|
||||
);
|
||||
}
|
||||
|
||||
fn simple_hash<T: Hash>(v: T) -> u64 {
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
v.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
fn check_tunables(&self, compiler: &Compiler) -> Result<()> {
|
||||
let other = compiler.tunables();
|
||||
|
||||
Self::check_int(
|
||||
self.tunables.static_memory_bound,
|
||||
other.static_memory_bound,
|
||||
"static memory bound",
|
||||
)?;
|
||||
Self::check_int(
|
||||
self.tunables.static_memory_offset_guard_size,
|
||||
other.static_memory_offset_guard_size,
|
||||
"static memory guard size",
|
||||
)?;
|
||||
Self::check_int(
|
||||
self.tunables.dynamic_memory_offset_guard_size,
|
||||
other.dynamic_memory_offset_guard_size,
|
||||
"dynamic memory guard size",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.tunables.generate_native_debuginfo,
|
||||
other.generate_native_debuginfo,
|
||||
"debug information support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.tunables.parse_wasm_debuginfo,
|
||||
other.parse_wasm_debuginfo,
|
||||
"WebAssembly backtrace support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.tunables.interruptable,
|
||||
other.interruptable,
|
||||
"interruption support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.tunables.consume_fuel,
|
||||
other.consume_fuel,
|
||||
"fuel support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.tunables.static_memory_bound_is_maximum,
|
||||
other.static_memory_bound_is_maximum,
|
||||
"pooling allocation support",
|
||||
)?;
|
||||
|
||||
// At this point, the hashes should match (if not we're missing a check)
|
||||
assert_eq!(
|
||||
Self::simple_hash(&self.tunables),
|
||||
Self::simple_hash(other),
|
||||
"unexpected hash difference"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_features(&self, compiler: &Compiler) -> Result<()> {
|
||||
let other = compiler.features();
|
||||
Self::check_bool(
|
||||
self.features.reference_types,
|
||||
other.reference_types,
|
||||
"WebAssembly reference types support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.multi_value,
|
||||
other.multi_value,
|
||||
"WebAssembly multi-value support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.bulk_memory,
|
||||
other.bulk_memory,
|
||||
"WebAssembly bulk memory support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.module_linking,
|
||||
other.module_linking,
|
||||
"WebAssembly module linking support",
|
||||
)?;
|
||||
Self::check_bool(self.features.simd, other.simd, "WebAssembly SIMD support")?;
|
||||
Self::check_bool(
|
||||
self.features.threads,
|
||||
other.threads,
|
||||
"WebAssembly threads support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.tail_call,
|
||||
other.tail_call,
|
||||
"WebAssembly tail-call support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.deterministic_only,
|
||||
other.deterministic_only,
|
||||
"WebAssembly deterministic-only support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.multi_memory,
|
||||
other.multi_memory,
|
||||
"WebAssembly multi-memory support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.exceptions,
|
||||
other.exceptions,
|
||||
"WebAssembly exceptions support",
|
||||
)?;
|
||||
Self::check_bool(
|
||||
self.features.memory64,
|
||||
other.memory64,
|
||||
"WebAssembly 64-bit memory support",
|
||||
)?;
|
||||
|
||||
// At this point, the hashes should match (if not we're missing a check)
|
||||
assert_eq!(
|
||||
Self::simple_hash(&self.features),
|
||||
Self::simple_hash(other),
|
||||
"unexpected hash difference"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::Config;
|
||||
|
||||
#[test]
|
||||
fn test_version_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.version = "0.0.1".to_string();
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled with Wasmtime version '0.0.1'"
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_architecture_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.target = "unknown-generic-linux".to_string();
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled for architecture 'unknown'",
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_os_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.target = format!(
|
||||
"{}-generic-unknown",
|
||||
target_lexicon::Triple::host().architecture
|
||||
);
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled for operating system 'unknown'",
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_opt_level_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.opt_level = OptLevel::None;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled with optimization level 'None' but 'Speed' is expected for the host",
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cranelift_flags_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.flags_hash += 1;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled with different Cranelift flags than the host",
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_isa_flags_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.isa_flags.push("not_a_flag".to_string());
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(e.to_string(), "Host is missing CPU flag 'not_a_flag'",),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "lightbeam")]
|
||||
#[test]
|
||||
fn test_compilation_strategy_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.strategy = CompilationStrategy::Lightbeam;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled with strategy 'Cranelift'",
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tunables_int_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.tunables.static_memory_offset_guard_size = 0;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(e.to_string(), "Module was compiled with a static memory guard size of '0' but '2147483648' is expected for the host"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tunables_bool_mismatch() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.interruptable(true);
|
||||
|
||||
let engine = Engine::new(&config)?;
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.tunables.interruptable = false;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled without interruption support but it is enabled for the host"
|
||||
),
|
||||
}
|
||||
|
||||
let mut config = Config::new();
|
||||
config.interruptable(false);
|
||||
|
||||
let engine = Engine::new(&config)?;
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.tunables.interruptable = true;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled with interruption support but it is not enabled for the host"
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_feature_mismatch() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.wasm_simd(true);
|
||||
|
||||
let engine = Engine::new(&config)?;
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.features.simd = false;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(e.to_string(), "Module was compiled without WebAssembly SIMD support but it is enabled for the host"),
|
||||
}
|
||||
|
||||
let mut config = Config::new();
|
||||
config.wasm_simd(false);
|
||||
|
||||
let engine = Engine::new(&config)?;
|
||||
let module = Module::new(&engine, "(module)")?;
|
||||
|
||||
let mut serialized = SerializedModule::new(&module);
|
||||
serialized.features.simd = true;
|
||||
|
||||
match serialized.into_module(&engine) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert_eq!(e.to_string(), "Module was compiled with WebAssembly SIMD support but it is not enabled for the host"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user