Skip to content

Commit cc94242

Browse files
committed
Make VM compile (no iterator or tests)
1 parent cadafc5 commit cc94242

File tree

13 files changed

+623
-703
lines changed

13 files changed

+623
-703
lines changed

Cargo.lock

+3-76
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/vm/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ staking = ["cosmwasm-std/staking"]
3434
cosmwasm-std = { path = "../std", version = "0.10.1" }
3535
serde_json = "1.0"
3636
wasmer = { path = "../../../wasmer/lib/api" }
37+
wasmer-compiler = { path = "../../../wasmer/lib/compiler" }
3738
wasmer-compiler-cranelift = { path = "../../../wasmer/lib/compiler-cranelift", optional = true }
3839
wasmer-compiler-singlepass = { path = "../../../wasmer/lib/compiler-singlepass", optional = true }
39-
wasmer-runtime-core = { path = "../../../wasmer/lib/deprecated/runtime-core" }
40+
wasmer-engine = { path = "../../../wasmer/lib/engine" }
41+
wasmer-engine-jit = { path = "../../../wasmer/lib/engine-jit" }
4042
wasmer-types = { path = "../../../wasmer/lib/wasmer-types" }
4143
schemars = "0.7"
4244
serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] }

packages/vm/src/backends/cranelift.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![cfg(any(feature = "cranelift", feature = "default-cranelift"))]
22

3-
use wasmer_clif_backend::CraneliftCompiler;
4-
use wasmer_runtime_core::{
5-
backend::Compiler, backend::CompilerConfig, compile_with_config, module::Module, vm::Ctx,
6-
};
3+
use wasmer::Module;
4+
use wasmer_compiler_cranelift::Cranelift;
5+
use wasmer_engine_jit::JIT;
76

87
use crate::errors::VmResult;
98

@@ -14,22 +13,21 @@ pub fn compile(code: &[u8]) -> VmResult<Module> {
1413
enable_verification: false, // As discussed in https://github.com/CosmWasm/cosmwasm/issues/155
1514
..Default::default()
1615
};
17-
let module = compile_with_config(code, compiler().as_ref(), config)?;
16+
let compiler = Cranelift::default();
17+
let engine = JIT::new(&mut compiler).engine();
18+
let store = Store::new(&engine);
19+
let module = Module::new(&store, code)?;
1820
Ok(module)
1921
}
2022

21-
pub fn compiler() -> Box<dyn Compiler> {
22-
Box::new(CraneliftCompiler::new())
23-
}
24-
2523
pub fn backend() -> &'static str {
2624
"cranelift"
2725
}
2826

2927
/// Set the amount of gas units that can be used in the context.
30-
pub fn set_gas_left(_ctx: &mut Ctx, _amount: u64) {}
28+
pub fn set_gas_left<S: Storage, Q: Querier>(_env: &mut Env<S, Q>, _amount: u64) {}
3129

3230
/// Get how many more gas units can be used in the context.
33-
pub fn get_gas_left(_ctx: &Ctx) -> u64 {
31+
pub fn get_gas_left<S: Storage, Q: Querier>(_env: &Env<S, Q>) -> u64 {
3432
FAKE_GAS_AVAILABLE
3533
}

packages/vm/src/backends/mod.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
pub mod cranelift;
22
pub mod singlepass;
33

4-
pub use wasmer_runtime_core::backend::Compiler;
5-
use wasmer_runtime_core::vm::Ctx;
6-
7-
pub fn compiler_for_backend(backend: &str) -> Option<Box<dyn Compiler>> {
8-
match backend {
9-
#[cfg(any(feature = "cranelift", feature = "default-cranelift"))]
10-
"cranelift" => Some(cranelift::compiler()),
11-
12-
#[cfg(any(feature = "singlepass", feature = "default-singlepass"))]
13-
"singlepass" => Some(singlepass::compiler()),
14-
15-
_ => None,
16-
}
17-
}
4+
use crate::context::Env;
5+
use crate::traits::{Querier, Storage};
186

197
#[derive(Debug)]
208
pub struct InsufficientGasLeft;
219

2210
/// Decreases gas left by the given amount.
2311
/// If the amount exceeds the available gas, the remaining gas is set to 0 and
2412
/// an InsufficientGasLeft error is returned.
25-
pub fn decrease_gas_left(ctx: &mut Ctx, amount: u64) -> Result<(), InsufficientGasLeft> {
26-
let remaining = get_gas_left(ctx);
13+
pub fn decrease_gas_left<S: Storage, Q: Querier>(
14+
env: &mut Env<S, Q>,
15+
amount: u64,
16+
) -> Result<(), InsufficientGasLeft> {
17+
let remaining = get_gas_left(env);
2718
if amount > remaining {
28-
set_gas_left(ctx, 0);
19+
set_gas_left(env, 0);
2920
Err(InsufficientGasLeft)
3021
} else {
31-
set_gas_left(ctx, remaining - amount);
22+
set_gas_left(env, remaining - amount);
3223
Ok(())
3324
}
3425
}
@@ -44,12 +35,12 @@ pub use singlepass::{backend, compile, get_gas_left, set_gas_left};
4435
mod test {
4536
use super::*;
4637
use wabt::wat2wasm;
47-
use wasmer_runtime_core::{imports, Instance as WasmerInstance};
38+
use wasmer::{imports, Instance as WasmerInstance};
4839

4940
fn instantiate(code: &[u8]) -> WasmerInstance {
5041
let module = compile(code).unwrap();
5142
let import_obj = imports! { "env" => {}, };
52-
module.instantiate(&import_obj).unwrap()
43+
WasmerInstance::new(&module, &import_obj).unwrap()
5344
}
5445

5546
#[test]

packages/vm/src/backends/singlepass.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#![cfg(any(feature = "singlepass", feature = "default-singlepass"))]
22

33
// use wasmer_middleware_common::metering;
4-
use wasmer_compiler_singlepass::{Singlepass, SinglepassCompiler};
5-
use wasmer_runtime_core::{
6-
backend::{Backend, Compiler},
7-
compile_with,
8-
module::Module,
9-
vm::Ctx,
10-
};
11-
12-
use crate::errors::{VmError, VmResult};
4+
use wasmer::{Module, Store};
5+
use wasmer_compiler_singlepass::Singlepass;
6+
use wasmer_engine_jit::JIT;
7+
8+
use crate::context::Env;
9+
use crate::errors::VmResult;
10+
use crate::traits::{Querier, Storage};
1311
// use crate::middleware::DeterministicMiddleware;
1412

1513
/// In Wasmer, the gas limit is set on modules during compilation and is included in the cached modules.
@@ -26,23 +24,22 @@ const MAX_GAS_LIMIT: u64 = u64::MAX / 2;
2624
const FAKE_GAS_AVAILABLE: u64 = 1_000_000;
2725

2826
pub fn compile(code: &[u8]) -> VmResult<Module> {
29-
compile_with(code, Backend::Auto).map_err(|err| VmError::compile_err(err.to_string()))
30-
}
31-
32-
pub fn compiler() -> Box<dyn Compiler> {
33-
let config = Singlepass::default();
34-
Box::new(SinglepassCompiler::new(&config))
27+
let compiler = Singlepass::default();
28+
let engine = JIT::new(&compiler).engine();
29+
let store = Store::new(&engine);
30+
let module = Module::new(&store, code)?;
31+
Ok(module)
3532
}
3633

3734
pub fn backend() -> &'static str {
3835
"singlepass"
3936
}
4037

4138
/// Set the amount of gas units that can be used in the context.
42-
pub fn set_gas_left(_ctx: &mut Ctx, _amount: u64) {}
39+
pub fn set_gas_left<S: Storage, Q: Querier>(_env: &mut Env<S, Q>, _amount: u64) {}
4340

4441
/// Get how many more gas units can be used in the context.
45-
pub fn get_gas_left(_ctx: &Ctx) -> u64 {
42+
pub fn get_gas_left<S: Storage, Q: Querier>(_env: &Env<S, Q>) -> u64 {
4643
FAKE_GAS_AVAILABLE
4744
}
4845

@@ -70,7 +67,7 @@ pub fn get_gas_left(_ctx: &Ctx) -> u64 {
7067
mod test {
7168
use super::*;
7269
use wabt::wat2wasm;
73-
use wasmer_runtime_core::{imports, Instance as WasmerInstance};
70+
use wasmer::{imports, Instance as WasmerInstance};
7471

7572
fn instantiate(code: &[u8]) -> WasmerInstance {
7673
let module = compile(code).unwrap();

packages/vm/src/calls.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use serde::de::DeserializeOwned;
22
use std::fmt;
33

44
use cosmwasm_std::{Env, HandleResult, InitResult, MigrateResult, QueryResult};
5+
use wasmer::Val;
56

67
use crate::errors::{VmError, VmResult};
7-
use crate::instance::{Func, Instance};
8+
use crate::instance::Instance;
89
use crate::serde::{from_slice, to_vec};
910
use crate::traits::{Api, Querier, Storage};
1011
use schemars::JsonSchema;
@@ -131,25 +132,14 @@ fn call_raw<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
131132
args: &[&[u8]],
132133
result_max_length: usize,
133134
) -> VmResult<Vec<u8>> {
134-
let mut arg_region_ptrs = Vec::<u32>::with_capacity(args.len());
135+
let mut arg_region_ptrs = Vec::<Val>::with_capacity(args.len());
135136
for arg in args {
136137
let region_ptr = instance.allocate(arg.len())?;
137138
instance.write_memory(region_ptr, arg)?;
138-
arg_region_ptrs.push(region_ptr);
139+
arg_region_ptrs.push(region_ptr.into());
139140
}
140-
141-
let res_region_ptr = match args.len() {
142-
1 => {
143-
let func: Func<u32, u32> = instance.func(name)?;
144-
func.call(arg_region_ptrs[0])?
145-
}
146-
2 => {
147-
let func: Func<(u32, u32), u32> = instance.func(name)?;
148-
func.call(arg_region_ptrs[0], arg_region_ptrs[1])?
149-
}
150-
_ => panic!("call_raw called with unsupported number of arguments"),
151-
};
152-
141+
let result = instance.call_function(name, &arg_region_ptrs)?;
142+
let res_region_ptr = result[0].unwrap_i32() as u32;
153143
let data = instance.read_memory(res_region_ptr, result_max_length)?;
154144
// free return value in wasm (arguments were freed in wasm code)
155145
instance.deallocate(res_region_ptr)?;

0 commit comments

Comments
 (0)