Remove usage of CompilationStrategy from Config (#764)

* Remove usage of `CompilationStrategy` from `Config`

This commit removes the public API usage of the internal
`CompilationStrategy` enumeration from the `Config` type in the
`wasmtime` crate. To do this the `enum` was copied locally into the
crate and renamed `Strategy`. The high-level description of this change
is:

* The `Config::strategy` method now takes a locally-defined `Strategy`
  enumeration instead of an internal type.

* The contents of `Strategy` are always the same, not relying on Cargo
  features to indicate which variants are present. This avoids
  unnecessary downstream `#[cfg]`.

* A `lightbeam` feature was added to the `wasmtime` crate itself to
  lightbeam compilation support.

* The `Config::strategy` method is now fallible. It returns a runtime
  error if support for the selected strategy wasn't compiled in.

* The `Strategy` enum is listed as `#[non_exhaustive]` so we can safely
  add variants over time to it.

This reduces the public crate dependencies of the `wasmtime` crate
itself, removing the need to reach into internal crates even more!

cc #708

* Fix fuzz targets

* Update nightly used to build releases

* Run rustfmt
This commit is contained in:
Alex Crichton
2020-01-06 18:08:13 -06:00
committed by GitHub
parent 787f50e107
commit 7474633cca
24 changed files with 173 additions and 153 deletions

View File

@@ -36,3 +36,9 @@ wat = "1.0"
[badges]
maintenance = { status = "actively-developed" }
[features]
# Enables experimental support for the lightbeam codegen backend, an alternative
# to cranelift. Requires Nightly Rust currently, and this is not enabled by
# default.
lightbeam = ["wasmtime-jit/lightbeam"]

View File

@@ -27,7 +27,7 @@ pub use crate::externals::*;
pub use crate::instance::Instance;
pub use crate::module::Module;
pub use crate::r#ref::{AnyRef, HostInfo, HostRef};
pub use crate::runtime::{Config, Engine, Store};
pub use crate::runtime::{Config, Engine, Store, Strategy};
pub use crate::trap::{FrameInfo, Trap, TrapInfo};
pub use crate::types::*;
pub use crate::values::*;

View File

@@ -1,4 +1,5 @@
use crate::context::Context;
use anyhow::Result;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
@@ -158,15 +159,29 @@ impl Config {
self
}
/// Configures the compilation `strategy` provided, indicating which
/// backend will be used for compiling WebAssembly to native code.
/// Configures which compilation strategy will be used for wasm modules.
///
/// Currently the primary strategies are with cranelift (an optimizing
/// compiler) or lightbeam (a fast single-pass JIT which produces code
/// quickly).
pub fn strategy(&mut self, strategy: CompilationStrategy) -> &mut Self {
self.strategy = strategy;
self
/// This method can be used to configure which compiler is used for wasm
/// modules, and for more documentation consult the [`Strategy`] enumeration
/// and its documentation.
///
/// # Errors
///
/// Some compilation strategies require compile-time options of `wasmtime`
/// itself to be set, but if they're not set and the strategy is specified
/// here then an error will be returned.
pub fn strategy(&mut self, strategy: Strategy) -> Result<&mut Self> {
self.strategy = match strategy {
Strategy::Auto => CompilationStrategy::Auto,
Strategy::Cranelift => CompilationStrategy::Cranelift,
#[cfg(feature = "lightbeam")]
Strategy::Lightbeam => CompilationStrategy::Lightbeam,
#[cfg(not(feature = "lightbeam"))]
Strategy::Lightbeam => {
anyhow::bail!("lightbeam compilation strategy wasn't enabled at compile time");
}
};
Ok(self)
}
}
@@ -176,6 +191,30 @@ impl Default for Config {
}
}
/// Possible Compilation strategies for a wasm module.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum Strategy {
/// An indicator that the compilation strategy should be automatically
/// selected.
///
/// This is generally what you want for most projects and indicates that the
/// `wasmtime` crate itself should make the decision about what the best
/// code generator for a wasm module is.
///
/// Currently this always defaults to Cranelift, but the default value will
/// change over time.
Auto,
/// Currently the default backend, Cranelift aims to be a reasonably fast
/// code generator which generates high quality machine code.
Cranelift,
/// A single-pass code generator that is faster than Cranelift but doesn't
/// produce as high-quality code.
Lightbeam,
}
// Engine
/// An `Engine` which is a global context for compilation and management of wasm

View File

@@ -12,6 +12,7 @@ readme = "README.md"
edition = "2018"
[dependencies]
anyhow = "1.0"
cranelift-codegen = { version = "0.52.0", features = ["enable-serde"] }
cranelift-entity = { version = "0.52.0", features = ["enable-serde"] }
cranelift-wasm = { version = "0.52.0", features = ["enable-serde"] }

View File

@@ -1,6 +1,7 @@
//! Module for configuring the cache system.
use super::worker;
use anyhow::{anyhow, bail, Context, Result};
use directories::ProjectDirs;
use lazy_static::lazy_static;
use log::{debug, error, trace, warn};
@@ -137,32 +138,32 @@ pub fn init<P: AsRef<Path> + Debug>(
/// Creates a new configuration file at specified path, or default path if None is passed.
/// Fails if file already exists.
pub fn create_new_config<P: AsRef<Path> + Debug>(
config_file: Option<P>,
) -> Result<PathBuf, String> {
pub fn create_new_config<P: AsRef<Path> + Debug>(config_file: Option<P>) -> Result<PathBuf> {
trace!("Creating new config file, path: {:?}", config_file);
let config_file = config_file.as_ref().map_or_else(
|| DEFAULT_CONFIG_PATH.as_ref().map(|p| p.as_ref()),
|p| Ok(p.as_ref()),
)?;
let config_file = config_file
.as_ref()
.map_or_else(
|| DEFAULT_CONFIG_PATH.as_ref().map(|p| p.as_ref()),
|p| Ok(p.as_ref()),
)
.map_err(|s| anyhow!("{}", s))?;
if config_file.exists() {
return Err(format!(
bail!(
"Specified config file already exists! Path: {}",
config_file.display()
));
);
}
let parent_dir = config_file
.parent()
.ok_or_else(|| format!("Invalid cache config path: {}", config_file.display()))?;
.ok_or_else(|| anyhow!("Invalid cache config path: {}", config_file.display()))?;
fs::create_dir_all(parent_dir).map_err(|err| {
fs::create_dir_all(parent_dir).with_context(|| {
format!(
"Failed to create config directory, config path: {}, error: {}",
"Failed to create config directory, config path: {}",
config_file.display(),
err
)
})?;
@@ -175,11 +176,10 @@ pub fn create_new_config<P: AsRef<Path> + Debug>(
enabled = true
";
fs::write(&config_file, &content).map_err(|err| {
fs::write(&config_file, &content).with_context(|| {
format!(
"Failed to flush config to the disk, path: {}, msg: {}",
"Failed to flush config to the disk, path: {}",
config_file.display(),
err
)
})?;

View File

@@ -32,14 +32,15 @@ fn host_isa() -> Box<dyn isa::TargetIsa> {
/// Performs initial validation, and returns early if the Wasm is invalid.
///
/// You can control which compiler is used via passing a `CompilationStrategy`.
pub fn instantiate(wasm: &[u8], compilation_strategy: CompilationStrategy) {
pub fn instantiate(wasm: &[u8], strategy: Strategy) {
if wasmparser::validate(wasm, None).is_err() {
return;
}
let mut config = Config::new();
config.strategy(compilation_strategy);
config
.strategy(strategy)
.expect("failed to enable lightbeam");
let engine = Engine::new(&config);
let store = HostRef::new(Store::new(&engine));

View File

@@ -5,10 +5,11 @@
//! use the Wasm binary by including it via
//! `include_bytes!("./regressions/some-descriptive-name.wasm")`.
use wasmtime::Strategy;
use wasmtime_fuzzing::oracles;
#[test]
fn instantiate_empty_module() {
let data = wat::parse_str(include_str!("./regressions/empty.wat")).unwrap();
oracles::instantiate(&data, wasmtime_jit::CompilationStrategy::Auto);
oracles::instantiate(&data, Strategy::Auto);
}

View File

@@ -11,6 +11,7 @@ readme = "README.md"
edition = "2018"
[dependencies]
anyhow = "1.0"
wasmtime-environ = { path = "../environ" }
faerie = "0.13.0"
more-asserts = "0.2.1"

View File

@@ -1,3 +1,4 @@
use anyhow::Result;
use faerie::{Artifact, Decl};
use wasmtime_environ::DataInitializer;
@@ -6,10 +7,9 @@ pub fn declare_data_segment(
obj: &mut Artifact,
_data_initaliazer: &DataInitializer,
index: usize,
) -> Result<(), String> {
) -> Result<()> {
let name = format!("_memory_{}", index);
obj.declare(name, Decl::data())
.map_err(|err| format!("{}", err))?;
obj.declare(name, Decl::data())?;
Ok(())
}
@@ -18,9 +18,8 @@ pub fn emit_data_segment(
obj: &mut Artifact,
data_initaliazer: &DataInitializer,
index: usize,
) -> Result<(), String> {
) -> Result<()> {
let name = format!("_memory_{}", index);
obj.define(name, Vec::from(data_initaliazer.data))
.map_err(|err| format!("{}", err))?;
obj.define(name, Vec::from(data_initaliazer.data))?;
Ok(())
}

View File

@@ -1,3 +1,4 @@
use anyhow::Result;
use faerie::{Artifact, Decl, Link};
use wasmtime_environ::entity::EntityRef;
use wasmtime_environ::settings;
@@ -19,26 +20,23 @@ pub fn declare_functions(
obj: &mut Artifact,
module: &Module,
relocations: &Relocations,
) -> Result<(), String> {
) -> Result<()> {
for i in 0..module.imported_funcs.len() {
let string_name = format!("_wasm_function_{}", i);
obj.declare(string_name, Decl::function_import())
.map_err(|err| format!("{}", err))?;
obj.declare(string_name, Decl::function_import())?;
}
for (_, function_relocs) in relocations.iter() {
for r in function_relocs {
let special_import_name = get_reloc_target_special_import_name(r.reloc_target);
if let Some(special_import_name) = special_import_name {
obj.declare(special_import_name, Decl::function_import())
.map_err(|err| format!("{}", err))?;
obj.declare(special_import_name, Decl::function_import())?;
}
}
}
for (i, _function_relocs) in relocations.iter().rev() {
let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index());
obj.declare(string_name, Decl::function().global())
.map_err(|err| format!("{}", err))?;
obj.declare(string_name, Decl::function().global())?;
}
Ok(())
}
@@ -49,7 +47,7 @@ pub fn emit_functions(
module: &Module,
compilation: &Compilation,
relocations: &Relocations,
) -> Result<(), String> {
) -> Result<()> {
debug_assert!(
module.start_func.is_none()
|| module.start_func.unwrap().index() >= module.imported_funcs.len(),
@@ -66,8 +64,7 @@ pub fn emit_functions(
let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index());
obj.define(string_name, body.clone())
.map_err(|err| format!("{}", err))?;
obj.define(string_name, body.clone())?;
}
for (i, function_relocs) in relocations.iter() {
@@ -82,8 +79,7 @@ pub fn emit_functions(
from: &string_name,
to: &target_name,
at: r.offset as u64,
})
.map_err(|err| format!("{}", err))?;
})?;
}
RelocationTarget::Memory32Grow
| RelocationTarget::ImportedMemory32Grow
@@ -93,8 +89,7 @@ pub fn emit_functions(
from: &string_name,
to: get_reloc_target_special_import_name(r.reloc_target).expect("name"),
at: r.offset as u64,
})
.map_err(|err| format!("{}", err))?;
})?;
}
RelocationTarget::JumpTable(_, _) => {
// ignore relocations for jump tables

View File

@@ -2,6 +2,7 @@ use crate::context::layout_vmcontext;
use crate::data_segment::{declare_data_segment, emit_data_segment};
use crate::function::{declare_functions, emit_functions};
use crate::table::{declare_table, emit_table};
use anyhow::Result;
use faerie::{Artifact, Decl, Link};
use wasmtime_environ::isa::TargetFrontendConfig;
use wasmtime_environ::{Compilation, DataInitializer, Module, Relocations};
@@ -10,18 +11,16 @@ fn emit_vmcontext_init(
obj: &mut Artifact,
module: &Module,
target_config: &TargetFrontendConfig,
) -> Result<(), String> {
) -> Result<()> {
let (data, table_relocs) = layout_vmcontext(module, target_config);
obj.declare_with("_vmcontext_init", Decl::data().global(), data.to_vec())
.map_err(|err| format!("{}", err))?;
obj.declare_with("_vmcontext_init", Decl::data().global(), data.to_vec())?;
for reloc in table_relocs.iter() {
let target_name = format!("_table_{}", reloc.index);
obj.link(Link {
from: "_vmcontext_init",
to: &target_name,
at: reloc.offset as u64,
})
.map_err(|err| format!("{}", err))?;
})?;
}
Ok(())
}
@@ -35,7 +34,7 @@ pub fn emit_module(
relocations: &Relocations,
data_initializers: &[DataInitializer],
target_config: &TargetFrontendConfig,
) -> Result<(), String> {
) -> Result<()> {
declare_functions(obj, module, relocations)?;
for (i, initializer) in data_initializers.iter().enumerate() {

View File

@@ -1,18 +1,17 @@
use anyhow::Result;
use faerie::{Artifact, Decl};
/// Declares data segment symbol
pub fn declare_table(obj: &mut Artifact, index: usize) -> Result<(), String> {
pub fn declare_table(obj: &mut Artifact, index: usize) -> Result<()> {
let name = format!("_table_{}", index);
obj.declare(name, Decl::data())
.map_err(|err| format!("{}", err))?;
obj.declare(name, Decl::data())?;
Ok(())
}
/// Emit segment data and initialization location
pub fn emit_table(obj: &mut Artifact, index: usize) -> Result<(), String> {
pub fn emit_table(obj: &mut Artifact, index: usize) -> Result<()> {
let name = format!("_table_{}", index);
// FIXME: We need to initialize table using function symbols
obj.define(name, Vec::new())
.map_err(|err| format!("{}", err))?;
obj.define(name, Vec::new())?;
Ok(())
}