Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasmer 3.0.0-beta PoC2 (Simon) #1467

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 257 additions & 50 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ serde = { version = "1.0.103", default-features = false, features = ["derive", "
serde_json = "1.0"
sha2 = "0.10.3"
thiserror = "1.0"
wasmer = { version = "=2.3.0", default-features = false, features = ["cranelift", "universal", "singlepass"] }
wasmer-middlewares = "=2.3.0"
wasmer = { version = "=3.0.0-beta", default-features = false, features = ["cranelift", "singlepass"] }
wasmer-middlewares = "=3.0.0-beta"
loupe = "0.1.3"

# Wasmer git/local (used for quick local debugging or patching)
Expand Down
27 changes: 20 additions & 7 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use wasmer::Store;

use crate::backend::{Backend, BackendApi, Querier, Storage};
use crate::capabilities::required_capabilities_from_module;
use crate::checksum::Checksum;
Expand Down Expand Up @@ -154,7 +156,7 @@ where

pub fn save_wasm(&self, wasm: &[u8]) -> VmResult<Checksum> {
check_wasm(wasm, &self.available_capabilities)?;
let module = compile(wasm, None, &[])?;
let (module, _store) = compile(wasm, None, &[])?;

let mut cache = self.inner.lock().unwrap();
let checksum = save_wasm_to_disk(&cache.wasm_path, wasm)?;
Expand Down Expand Up @@ -219,18 +221,18 @@ where
let store = make_runtime_store(Some(cache.instance_memory_limit));
if let Some(module) = cache.fs_cache.load(checksum, &store)? {
cache.stats.hits_fs_cache += 1;
let module_size = loupe::size_of_val(&module);
let module_size = 1;
return cache
.pinned_memory_cache
.store(checksum, module, module_size);
}

// Re-compile from original Wasm bytecode
let code = self.load_wasm_with_path(&cache.wasm_path, checksum)?;
let module = compile(&code, Some(cache.instance_memory_limit), &[])?;
let (module, _store) = compile(&code, Some(cache.instance_memory_limit), &[])?;
// Store into the fs cache too
cache.fs_cache.store(checksum, &module)?;
let module_size = loupe::size_of_val(&module);
let module_size = 1;
cache
.pinned_memory_cache
.store(checksum, module, module_size)
Expand All @@ -257,8 +259,10 @@ where
backend: Backend<A, S, Q>,
options: InstanceOptions,
) -> VmResult<Instance<A, S, Q>> {
let store = Store::default();
let module = self.get_module(checksum)?;
let instance = Instance::from_module(
store,
&module,
backend,
options.gas_limit,
Expand Down Expand Up @@ -290,7 +294,7 @@ where
let store = make_runtime_store(Some(cache.instance_memory_limit));
if let Some(module) = cache.fs_cache.load(checksum, &store)? {
cache.stats.hits_fs_cache += 1;
let module_size = loupe::size_of_val(&module);
let module_size = 1;
cache
.memory_cache
.store(checksum, module.clone(), module_size)?;
Expand All @@ -304,9 +308,9 @@ where
// stored the old module format.
let wasm = self.load_wasm_with_path(&cache.wasm_path, checksum)?;
cache.stats.misses += 1;
let module = compile(&wasm, Some(cache.instance_memory_limit), &[])?;
let (module, _store) = compile(&wasm, Some(cache.instance_memory_limit), &[])?;
cache.fs_cache.store(checksum, &module)?;
let module_size = loupe::size_of_val(&module);
let module_size = 1;
cache
.memory_cache
.store(checksum, module.clone(), module_size)?;
Expand Down Expand Up @@ -376,6 +380,7 @@ mod tests {
use std::fs::OpenOptions;
use std::io::Write;
use tempfile::TempDir;
use wasmer::Store;

const TESTING_GAS_LIMIT: u64 = 500_000_000_000; // ~0.5ms
const TESTING_MEMORY_LIMIT: Size = Size::mebi(16);
Expand Down Expand Up @@ -576,6 +581,8 @@ mod tests {

#[test]
fn get_instance_finds_cached_modules_and_stores_to_memory() {
let store = Store::default();

let cache = unsafe { Cache::new(make_testing_options()).unwrap() };
let checksum = cache.save_wasm(CONTRACT).unwrap();
let backend1 = mock_backend(&[]);
Expand Down Expand Up @@ -796,6 +803,8 @@ mod tests {

#[test]
fn use_multiple_cached_instances_of_same_contract() {
let store = Store::default();

let cache = unsafe { Cache::new(make_testing_options()).unwrap() };
let checksum = cache.save_wasm(CONTRACT).unwrap();

Expand Down Expand Up @@ -850,6 +859,8 @@ mod tests {

#[test]
fn resets_gas_when_reusing_instance() {
let store = Store::default();

let cache = unsafe { Cache::new(make_testing_options()).unwrap() };
let checksum = cache.save_wasm(CONTRACT).unwrap();

Expand Down Expand Up @@ -1007,6 +1018,8 @@ mod tests {

#[test]
fn pin_unpin_works() {
let store = Store::default();

let cache = unsafe { Cache::new(make_testing_options()).unwrap() };
let checksum = cache.save_wasm(CONTRACT).unwrap();

Expand Down
38 changes: 23 additions & 15 deletions packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::de::DeserializeOwned;
use wasmer::Val;
use wasmer::Value;

use cosmwasm_std::{ContractResult, CustomMsg, Env, MessageInfo, QueryResponse, Reply, Response};
#[cfg(feature = "stargate")]
Expand Down Expand Up @@ -227,7 +227,7 @@ where
{
let env = to_vec(env)?;
let msg = to_vec(msg)?;
let data = call_ibc_channel_open_raw(instance, &env, &msg)?;
let data = call_ibc_channel_open_raw(store, instance, &env, &msg)?;
let result: ContractResult<Option<Ibc3ChannelOpenResponse>> =
from_slice(&data, deserialization_limits::RESULT_IBC_CHANNEL_OPEN)?;
Ok(result)
Expand All @@ -247,7 +247,7 @@ where
{
let env = to_vec(env)?;
let msg = to_vec(msg)?;
let data = call_ibc_channel_connect_raw(instance, &env, &msg)?;
let data = call_ibc_channel_connect_raw(store, instance, &env, &msg)?;
let result = from_slice(&data, deserialization_limits::RESULT_IBC_CHANNEL_CONNECT)?;
Ok(result)
}
Expand All @@ -266,7 +266,7 @@ where
{
let env = to_vec(env)?;
let msg = to_vec(msg)?;
let data = call_ibc_channel_close_raw(instance, &env, &msg)?;
let data = call_ibc_channel_close_raw(store, instance, &env, &msg)?;
let result = from_slice(&data, deserialization_limits::RESULT_IBC_CHANNEL_CLOSE)?;
Ok(result)
}
Expand All @@ -285,7 +285,7 @@ where
{
let env = to_vec(env)?;
let msg = to_vec(msg)?;
let data = call_ibc_packet_receive_raw(instance, &env, &msg)?;
let data = call_ibc_packet_receive_raw(store, instance, &env, &msg)?;
let result = from_slice(&data, deserialization_limits::RESULT_IBC_PACKET_RECEIVE)?;
Ok(result)
}
Expand All @@ -304,7 +304,7 @@ where
{
let env = to_vec(env)?;
let msg = to_vec(msg)?;
let data = call_ibc_packet_ack_raw(instance, &env, &msg)?;
let data = call_ibc_packet_ack_raw(store, instance, &env, &msg)?;
let result = from_slice(&data, deserialization_limits::RESULT_IBC_PACKET_ACK)?;
Ok(result)
}
Expand Down Expand Up @@ -574,7 +574,7 @@ where
S: Storage + 'static,
Q: Querier + 'static,
{
let mut arg_region_ptrs = Vec::<Val>::with_capacity(args.len());
let mut arg_region_ptrs = Vec::<Value>::with_capacity(args.len());
for arg in args {
let region_ptr = instance.allocate(arg.len())?;
instance.write_memory(region_ptr, arg)?;
Expand All @@ -593,11 +593,13 @@ mod tests {
use super::*;
use crate::testing::{mock_env, mock_info, mock_instance};
use cosmwasm_std::{coins, Empty};
use wasmer::Store;

static CONTRACT: &[u8] = include_bytes!("../testdata/hackatom.wasm");

#[test]
fn call_instantiate_works() {
let store = Store::default();
let mut instance = mock_instance(CONTRACT, &[]);

// init
Expand All @@ -610,6 +612,7 @@ mod tests {

#[test]
fn call_execute_works() {
let store = Store::default();
let mut instance = mock_instance(CONTRACT, &[]);

// init
Expand Down Expand Up @@ -695,7 +698,7 @@ mod tests {
// init
let info = mock_info("creator", &[]);
let msg = br#"{"reflect_code_id":77}"#;
call_instantiate::<_, _, _, Empty>(instance, &mock_env(), &info, msg)
call_instantiate::<_, _, _, Empty>(&mut store, instance, &mock_env(), &info, msg)
.unwrap()
.unwrap();
// first we try to open with a valid handshake
Expand Down Expand Up @@ -733,7 +736,7 @@ mod tests {
data: None,
}),
};
call_reply::<_, _, _, Empty>(instance, &mock_env(), &response).unwrap();
call_reply::<_, _, _, Empty>(&mut store, instance, &mock_env(), &response).unwrap();
}
const CHANNEL_ID: &str = "channel-123";
const ACCOUNT: &str = "account-456";
Expand All @@ -748,17 +751,22 @@ mod tests {
setup(&mut instance, CHANNEL_ID, ACCOUNT);
let handshake_close =
mock_ibc_channel_close_init(CHANNEL_ID, IbcOrder::Ordered, IBC_VERSION);
call_ibc_channel_close::<_, _, _, Empty>(&mut instance, &mock_env(), &handshake_close)
.unwrap()
.unwrap();
call_ibc_channel_close::<_, _, _, Empty>(
&mut store,
&mut instance,
&mock_env(),
&handshake_close,
)
.unwrap()
.unwrap();
}
#[test]
fn call_ibc_packet_ack_works() {
let mut instance = mock_instance(CONTRACT, &[]);
setup(&mut instance, CHANNEL_ID, ACCOUNT);
let ack = IbcAcknowledgement::new(br#"{}"#);
let msg = mock_ibc_packet_ack(CHANNEL_ID, br#"{}"#, ack).unwrap();
call_ibc_packet_ack::<_, _, _, Empty>(&mut instance, &mock_env(), &msg)
call_ibc_packet_ack::<_, _, _, Empty>(&mut store, &mut instance, &mock_env(), &msg)
.unwrap()
.unwrap();
}
Expand All @@ -767,7 +775,7 @@ mod tests {
let mut instance = mock_instance(CONTRACT, &[]);
setup(&mut instance, CHANNEL_ID, ACCOUNT);
let msg = mock_ibc_packet_timeout(CHANNEL_ID, br#"{}"#).unwrap();
call_ibc_packet_timeout::<_, _, _, Empty>(&mut instance, &mock_env(), &msg)
call_ibc_packet_timeout::<_, _, _, Empty>(&mut store, &mut instance, &mock_env(), &msg)
.unwrap()
.unwrap();
}
Expand All @@ -777,7 +785,7 @@ mod tests {
setup(&mut instance, CHANNEL_ID, ACCOUNT);
let who_am_i = br#"{"who_am_i":{}}"#;
let msg = mock_ibc_packet_recv(CHANNEL_ID, who_am_i).unwrap();
call_ibc_packet_receive::<_, _, _, Empty>(&mut instance, &mock_env(), &msg)
call_ibc_packet_receive::<_, _, _, Empty>(&mut store, &mut instance, &mock_env(), &msg)
.unwrap()
.unwrap();
}
Expand Down
Loading