Skip to content

Commit 9bb96d5

Browse files
authored
Add Config::without_compiler (#12089)
* Add `Config::without_compiler` This provides us an allocation-free path for constructing a `Config`. Why not rely on builds when `cfg(not(any(feature = "cranelift", feature = "winch")))`? Because we need to test our various OOM-handling and allocation-free code paths in this workspace, and without some way to create a config without a compiler otherwise, cargo's feature resolver will enable those features in the workspace, enabling the compiler in the config, but we don't intend to make compiler configurations handle OOM. * Quiet unused mut warnings * Move default compiler flags into builder
1 parent 196f09f commit 9bb96d5

File tree

9 files changed

+232
-88
lines changed

9 files changed

+232
-88
lines changed

crates/cranelift/src/builder.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,28 @@ pub struct LinkOptions {
3939
}
4040

4141
pub fn builder(triple: Option<Triple>) -> Result<Box<dyn CompilerBuilder>> {
42-
Ok(Box::new(Builder {
42+
let mut builder = Builder {
4343
tunables: None,
4444
inner: IsaBuilder::new(triple, |triple| isa::lookup(triple).map_err(|e| e.into()))?,
4545
linkopts: LinkOptions::default(),
4646
cache_store: None,
4747
clif_dir: None,
4848
wmemcheck: false,
4949
emit_debug_checks: false,
50-
}))
50+
};
51+
52+
builder.set("enable_verifier", "false").unwrap();
53+
builder.set("opt_level", "speed").unwrap();
54+
55+
// When running under MIRI try to optimize for compile time of Wasm code
56+
// itself as much as possible. Disable optimizations by default and use the
57+
// fastest regalloc available to us.
58+
if cfg!(miri) {
59+
builder.set("opt_level", "none").unwrap();
60+
builder.set("regalloc_algorithm", "single_pass").unwrap();
61+
}
62+
63+
Ok(Box::new(builder))
5164
}
5265

5366
impl CompilerBuilder for Builder {

crates/fuzzing/tests/oom.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::alloc::{Layout, alloc};
2-
use wasmtime::Result;
2+
use wasmtime::{Config, Result};
33
use wasmtime_fuzzing::oom::{OomTest, OomTestAllocator};
44

55
#[global_allocator]
@@ -25,3 +25,12 @@ fn smoke_test_missed_oom() -> Result<()> {
2525
);
2626
Ok(())
2727
}
28+
29+
#[test]
30+
fn config_new() -> Result<()> {
31+
OomTest::new().test(|| {
32+
let mut config = Config::new();
33+
config.enable_compiler(false);
34+
Ok(())
35+
})
36+
}

crates/wasmtime/src/compile.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub(crate) fn build_module_artifacts<T: FinishedObject>(
7070
T,
7171
Option<(CompiledModuleInfo, CompiledFunctionsTable, ModuleTypes)>,
7272
)> {
73+
let compiler = engine.try_compiler()?;
7374
let tunables = engine.tunables();
7475

7576
// First a `ModuleEnvironment` is created which records type information
@@ -101,7 +102,7 @@ pub(crate) fn build_module_artifacts<T: FinishedObject>(
101102

102103
// Emplace all compiled functions into the object file with any other
103104
// sections associated with code as well.
104-
let mut object = engine.compiler().object(ObjectKind::Module)?;
105+
let mut object = compiler.object(ObjectKind::Module)?;
105106
// Insert `Engine` and type-level information into the compiled
106107
// artifact so if this module is deserialized later it contains all
107108
// information necessary.
@@ -111,7 +112,7 @@ pub(crate) fn build_module_artifacts<T: FinishedObject>(
111112
// They're only used during deserialization and not during runtime for
112113
// the module itself. Currently there's no need for that, however, so
113114
// it's left as an exercise for later.
114-
engine.append_compiler_info(&mut object);
115+
engine.append_compiler_info(&mut object)?;
115116
engine.append_bti(&mut object);
116117

117118
let (mut object, compilation_artifacts) = indices.link_and_append_code(
@@ -150,8 +151,8 @@ pub(crate) fn build_component_artifacts<T: FinishedObject>(
150151
CompiledComponentInfo, ComponentArtifacts, ComponentTypesBuilder,
151152
};
152153

154+
let compiler = engine.try_compiler()?;
153155
let tunables = engine.tunables();
154-
let compiler = engine.compiler();
155156

156157
let scope = ScopeVec::new();
157158
let mut validator = wasmparser::Validator::new_with_features(engine.features());
@@ -185,7 +186,7 @@ pub(crate) fn build_component_artifacts<T: FinishedObject>(
185186
}
186187

187188
let mut object = compiler.object(ObjectKind::Component)?;
188-
engine.append_compiler_info(&mut object);
189+
engine.append_compiler_info(&mut object)?;
189190
engine.append_bti(&mut object);
190191

191192
let (mut object, compilation_artifacts) = indices.link_and_append_code(
@@ -520,7 +521,7 @@ impl<'a> CompileInputs<'a> {
520521
/// Compile these `CompileInput`s (maybe in parallel) and return the
521522
/// resulting `UnlinkedCompileOutput`s.
522523
fn compile(self, engine: &Engine) -> Result<UnlinkedCompileOutputs<'a>> {
523-
let compiler = engine.compiler();
524+
let compiler = engine.try_compiler()?;
524525

525526
if self.inputs.len() > 0 && cfg!(miri) {
526527
bail!(
@@ -912,7 +913,7 @@ the use case.
912913
}
913914

914915
fn compile_required_builtins(engine: &Engine, raw_outputs: &mut Vec<CompileOutput>) -> Result<()> {
915-
let compiler = engine.compiler();
916+
let compiler = engine.try_compiler()?;
916917
let mut builtins = HashSet::new();
917918
let mut new_inputs: Vec<CompileInput<'_>> = Vec::new();
918919

@@ -1040,7 +1041,7 @@ impl FunctionIndices {
10401041
// The result is a vector parallel to `compiled_funcs` where
10411042
// `symbol_ids_and_locs[i]` is the symbol ID and function location of
10421043
// `compiled_funcs[i]`.
1043-
let compiler = engine.compiler();
1044+
let compiler = engine.try_compiler()?;
10441045
let tunables = engine.tunables();
10451046
let symbol_ids_and_locs = compiler.append_code(
10461047
&mut obj,

crates/wasmtime/src/compile/code_builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,10 +808,11 @@ pub struct HashedEngineCompileEnv<'a>(pub &'a Engine);
808808
impl std::hash::Hash for HashedEngineCompileEnv<'_> {
809809
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
810810
// Hash the compiler's state based on its target and configuration.
811-
let compiler = self.0.compiler();
812-
compiler.triple().hash(hasher);
813-
compiler.flags().hash(hasher);
814-
compiler.isa_flags().hash(hasher);
811+
if let Some(compiler) = self.0.compiler() {
812+
compiler.triple().hash(hasher);
813+
compiler.flags().hash(hasher);
814+
compiler.isa_flags().hash(hasher);
815+
}
815816

816817
// Hash configuration state read for compilation
817818
let config = self.0.config();

0 commit comments

Comments
 (0)