Skip to content

Commit 61635b8

Browse files
committed
Allow disabling cranelift
1 parent 6b7c60e commit 61635b8

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

packages/vm/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ staking = ["cosmwasm-std/staking"]
1919
# this enables all stargate-related functionality, including the ibc entry points
2020
stargate = ["cosmwasm-std/stargate"]
2121
# Use cranelift backend instead of singlepass. This is required for development on Windows.
22-
cranelift = ["wasmer/cranelift"]
22+
#
23+
# LEGACY: This doesn't do anything anymore. Cranelift is automatically pulled in on Windows.
24+
cranelift = []
2325
# For heap profiling. Only used in the "heap_profiling" example.
2426
dhat-heap = ["dep:dhat"]
2527

@@ -42,7 +44,7 @@ serde = { workspace = true }
4244
serde_json = "1.0.40"
4345
sha2 = "0.10.3"
4446
thiserror = "1.0.26"
45-
wasmer = { version = "=4.2.6", default-features = false, features = ["cranelift", "singlepass"] }
47+
wasmer = { version = "=4.2.6", default-features = false, features = ["singlepass"] }
4648
wasmer-middlewares = "=4.2.6"
4749
strum = { version = "0.25.0", default-features = false, features = ["derive"] }
4850
# For heap profiling. Only used in the "heap_profiling" example. This has to be a non-dev dependency
@@ -59,6 +61,9 @@ tracing = "0.1.32"
5961
# wasmer = { path = "../../../wasmer/lib/api", default-features = false, features = ["cranelift", "singlepass"] }
6062
# wasmer-middlewares = { path = "../../../wasmer/lib/middlewares" }
6163

64+
[target.'cfg(target_family = "windows")'.dependencies]
65+
wasmer = { version = "=4.2.6", default-features = false, features = ["cranelift"] }
66+
6267
[dev-dependencies]
6368
criterion = { version = "0.5.1", features = ["html_reports"] }
6469
glob = "0.3.1"

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");
@@ -201,7 +202,7 @@ mod tests {
201202
#[test]
202203
fn exported_function_names_works_for_wasmer_with_no_prefix() {
203204
let wasm = wat::parse_str(r#"(module)"#).unwrap();
204-
let compiler = Cranelift::default();
205+
let compiler = make_compiler_config();
205206
let store = Store::new(compiler);
206207
let module = wasmer::Module::new(&store, wasm).unwrap();
207208
let exports = module.exported_function_names(None);
@@ -219,7 +220,7 @@ mod tests {
219220
)"#,
220221
)
221222
.unwrap();
222-
let compiler = Cranelift::default();
223+
let compiler = make_compiler_config();
223224
let store = Store::new(compiler);
224225
let module = wasmer::Module::new(&store, wasm).unwrap();
225226
let exports = module.exported_function_names(None);
@@ -232,7 +233,7 @@ mod tests {
232233
#[test]
233234
fn exported_function_names_works_for_wasmer_with_prefix() {
234235
let wasm = wat::parse_str(r#"(module)"#).unwrap();
235-
let compiler = Cranelift::default();
236+
let compiler = make_compiler_config();
236237
let store = Store::new(compiler);
237238
let module = wasmer::Module::new(&store, wasm).unwrap();
238239
let exports = module.exported_function_names(Some("b"));
@@ -251,7 +252,7 @@ mod tests {
251252
)"#,
252253
)
253254
.unwrap();
254-
let compiler = Cranelift::default();
255+
let compiler = make_compiler_config();
255256
let store = Store::new(compiler);
256257
let module = wasmer::Module::new(&store, wasm).unwrap();
257258
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+
/// Creates the appropriate compiler config for the platform
30+
pub fn make_compiler_config() -> impl CompilerConfig + Into<Engine> {
31+
#[cfg(target_family = "windows")]
32+
return wasmer::Cranelift::new();
33+
#[cfg(not(target_family = "windows"))]
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)