Skip to content

Commit 11cc041

Browse files
authored
Merge pull request #2055 from CosmWasm/aw/optional-cranelift
Allow disabling cranelift
2 parents 56a5946 + 716bfbf commit 11cc041

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

packages/vm/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ iterator = ["cosmwasm-std/iterator"]
1818
staking = ["cosmwasm-std/staking"]
1919
# this enables all stargate-related functionality, including the ibc entry points
2020
stargate = ["cosmwasm-std/stargate"]
21-
# Use cranelift backend instead of singlepass. This is required for development on Windows.
21+
# Use cranelift backend instead of singlepass
2222
cranelift = ["wasmer/cranelift"]
2323
# For heap profiling. Only used in the "heap_profiling" example.
2424
dhat-heap = ["dep:dhat"]
@@ -42,7 +42,7 @@ serde = { workspace = true }
4242
serde_json = "1.0.40"
4343
sha2 = "0.10.3"
4444
thiserror = "1.0.26"
45-
wasmer = { version = "=4.2.6", default-features = false, features = ["cranelift", "singlepass"] }
45+
wasmer = { version = "=4.2.6", default-features = false, features = ["singlepass"] }
4646
wasmer-middlewares = "=4.2.6"
4747
strum = { version = "0.25.0", default-features = false, features = ["derive"] }
4848
# For heap profiling. Only used in the "heap_profiling" example. This has to be a non-dev dependency

packages/vm/src/static_analysis.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ impl ExportInfo for &wasmer::Module {
107107
mod tests {
108108
use std::str::FromStr;
109109

110+
use crate::wasm_backend::make_compiler_config;
110111
use crate::VmError;
111112

112113
use super::*;
113-
use wasmer::{Cranelift, Store};
114+
use wasmer::Store;
114115

115116
static CONTRACT: &[u8] = include_bytes!("../testdata/hackatom.wasm");
116117
static CORRUPTED: &[u8] = include_bytes!("../testdata/corrupted.wasm");
@@ -204,7 +205,7 @@ mod tests {
204205
#[test]
205206
fn exported_function_names_works_for_wasmer_with_no_prefix() {
206207
let wasm = wat::parse_str(r#"(module)"#).unwrap();
207-
let compiler = Cranelift::default();
208+
let compiler = make_compiler_config();
208209
let store = Store::new(compiler);
209210
let module = wasmer::Module::new(&store, wasm).unwrap();
210211
let exports = module.exported_function_names(None);
@@ -222,7 +223,7 @@ mod tests {
222223
)"#,
223224
)
224225
.unwrap();
225-
let compiler = Cranelift::default();
226+
let compiler = make_compiler_config();
226227
let store = Store::new(compiler);
227228
let module = wasmer::Module::new(&store, wasm).unwrap();
228229
let exports = module.exported_function_names(None);
@@ -235,7 +236,7 @@ mod tests {
235236
#[test]
236237
fn exported_function_names_works_for_wasmer_with_prefix() {
237238
let wasm = wat::parse_str(r#"(module)"#).unwrap();
238-
let compiler = Cranelift::default();
239+
let compiler = make_compiler_config();
239240
let store = Store::new(compiler);
240241
let module = wasmer::Module::new(&store, wasm).unwrap();
241242
let exports = module.exported_function_names(Some("b"));
@@ -254,7 +255,7 @@ mod tests {
254255
)"#,
255256
)
256257
.unwrap();
257-
let compiler = Cranelift::default();
258+
let compiler = make_compiler_config();
258259
let store = Store::new(compiler);
259260
let module = wasmer::Module::new(&store, wasm).unwrap();
260261
let exports = module.exported_function_names(Some("b"));

packages/vm/src/wasm_backend/engine.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use std::sync::Arc;
2-
#[cfg(feature = "cranelift")]
3-
use wasmer::Cranelift;
42
use wasmer::NativeEngineExt;
5-
#[cfg(not(feature = "cranelift"))]
6-
use wasmer::Singlepass;
73
use wasmer::{
84
sys::BaseTunables, wasmparser::Operator, CompilerConfig, Engine, Pages, Target, WASM_PAGE_SIZE,
95
};
@@ -30,6 +26,14 @@ fn cost(_operator: &Operator) -> u64 {
3026
150
3127
}
3228

29+
/// Use Cranelift as the compiler backend if the feature is enabled
30+
pub fn make_compiler_config() -> impl CompilerConfig + Into<Engine> {
31+
#[cfg(feature = "cranelift")]
32+
return wasmer::Cranelift::new();
33+
#[cfg(not(feature = "cranelift"))]
34+
wasmer::Singlepass::new()
35+
}
36+
3337
/// Creates an engine without a compiler.
3438
/// This is used to run modules compiled before.
3539
pub fn make_runtime_engine(memory_limit: Option<Size>) -> Engine {
@@ -48,16 +52,11 @@ pub fn make_compiling_engine(memory_limit: Option<Size>) -> Engine {
4852
let deterministic = Arc::new(Gatekeeper::default());
4953
let metering = Arc::new(Metering::new(gas_limit, cost));
5054

51-
#[cfg(feature = "cranelift")]
52-
let mut compiler = Cranelift::default();
53-
54-
#[cfg(not(feature = "cranelift"))]
55-
let mut compiler = Singlepass::default();
56-
55+
let mut compiler = make_compiler_config();
5756
compiler.canonicalize_nans(true);
5857
compiler.push_middleware(deterministic);
5958
compiler.push_middleware(metering);
60-
let mut engine = Engine::from(compiler);
59+
let mut engine: Engine = compiler.into();
6160
if let Some(limit) = memory_limit {
6261
let base = BaseTunables::for_target(&Target::default());
6362
let tunables = LimitingTunables::new(base, limit_to_pages(limit));

packages/vm/src/wasm_backend/gatekeeper.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,9 @@ impl FunctionMiddleware for FunctionGatekeeper {
746746
#[cfg(test)]
747747
mod tests {
748748
use super::*;
749+
use crate::wasm_backend::make_compiler_config;
749750
use std::sync::Arc;
750-
use wasmer::{CompilerConfig, Cranelift, Module, Store};
751+
use wasmer::{CompilerConfig, Module, Store};
751752

752753
#[test]
753754
fn valid_wasm_instance_sanity() {
@@ -764,7 +765,7 @@ mod tests {
764765
.unwrap();
765766

766767
let deterministic = Arc::new(Gatekeeper::default());
767-
let mut compiler = Cranelift::default();
768+
let mut compiler = make_compiler_config();
768769
compiler.push_middleware(deterministic);
769770
let store = Store::new(compiler);
770771
let result = Module::new(&store, wasm);
@@ -785,7 +786,7 @@ mod tests {
785786
.unwrap();
786787

787788
let deterministic = Arc::new(Gatekeeper::default());
788-
let mut compiler = Cranelift::default();
789+
let mut compiler = make_compiler_config();
789790
compiler.push_middleware(deterministic);
790791
let store = Store::new(compiler);
791792
let result = Module::new(&store, wasm);
@@ -809,7 +810,7 @@ mod tests {
809810
.unwrap();
810811

811812
let deterministic = Arc::new(Gatekeeper::default());
812-
let mut compiler = Cranelift::default();
813+
let mut compiler = make_compiler_config();
813814
compiler.push_middleware(deterministic);
814815
let store = Store::new(compiler);
815816
let result = Module::new(&store, wasm);

packages/vm/src/wasm_backend/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ mod engine;
33
mod gatekeeper;
44
mod limiting_tunables;
55

6+
#[cfg(test)]
7+
pub use engine::make_compiler_config;
8+
69
pub use compile::compile;
710
pub use engine::{make_compiling_engine, make_runtime_engine};

0 commit comments

Comments
 (0)