Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Update wasmtime to 0.29.0 #9552

Merged
19 commits merged into from
Sep 29, 2021
Merged
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
207 changes: 94 additions & 113 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions client/executor/common/src/runtime_blob/globals_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ pub trait InstanceGlobals {
/// Get a handle to a global by it's export name.
///
/// The requested export is must exist in the exported list, and it should be a mutable global.
fn get_global(&self, export_name: &str) -> Self::Global;
fn get_global(&mut self, export_name: &str) -> Self::Global;
/// Get the current value of the global.
fn get_global_value(&self, global: &Self::Global) -> sp_wasm_interface::Value;
fn get_global_value(&mut self, global: &Self::Global) -> sp_wasm_interface::Value;
/// Update the current value of the global.
///
/// The global behind the handle is guaranteed to be mutable and the value to be the same type
/// as the global.
fn set_global_value(&self, global: &Self::Global, value: sp_wasm_interface::Value);
fn set_global_value(&mut self, global: &Self::Global, value: sp_wasm_interface::Value);
}

/// A set of exposed mutable globals.
Expand Down Expand Up @@ -79,7 +79,10 @@ impl<Global> GlobalsSnapshot<Global> {
///
/// This function panics if the instance doesn't correspond to the module from which the
/// [`ExposedMutableGlobalsSet`] was collected.
pub fn take<Instance>(mutable_globals: &ExposedMutableGlobalsSet, instance: &Instance) -> Self
pub fn take<Instance>(
mutable_globals: &ExposedMutableGlobalsSet,
instance: &mut Instance,
) -> Self
where
Instance: InstanceGlobals<Global = Global>,
{
Expand All @@ -98,7 +101,7 @@ impl<Global> GlobalsSnapshot<Global> {
/// Apply the snapshot to the given instance.
///
/// This instance must be the same that was used for creation of this snapshot.
pub fn apply<Instance>(&self, instance: &Instance)
pub fn apply<Instance>(&self, instance: &mut Instance)
where
Instance: InstanceGlobals<Global = Global>,
{
Expand Down
2 changes: 1 addition & 1 deletion client/executor/common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub mod wasmer {
let range = checked_range(dest_addr.into(), source.len(), destination.len())
.ok_or_else(|| Error::Other("memory write is out of bounds".into()))?;

&mut destination[range].copy_from_slice(source);
destination[range].copy_from_slice(source);
Ok(())
}
}
Expand Down
6 changes: 3 additions & 3 deletions client/executor/common/src/wasm_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ pub trait WasmInstance: Send {
/// Before execution, instance is reset.
///
/// Returns the encoded result on success.
fn call(&self, method: InvokeMethod, data: &[u8]) -> Result<Vec<u8>, Error>;
fn call(&mut self, method: InvokeMethod, data: &[u8]) -> Result<Vec<u8>, Error>;

/// Call an exported method on this WASM instance.
///
/// Before execution, instance is reset.
///
/// Returns the encoded result on success.
fn call_export(&self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
fn call_export(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
self.call(method.into(), data)
}

/// Get the value from a global with the given `name`.
///
/// This method is only suitable for getting immutable globals.
fn get_global_const(&self, name: &str) -> Result<Option<Value>, Error>;
fn get_global_const(&mut self, name: &str) -> Result<Option<Value>, Error>;

/// **Testing Only**. This function returns the base address of the linear memory.
///
Expand Down
2 changes: 1 addition & 1 deletion client/executor/src/integration_tests/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn memory_consumption_compiled() {

let runtime = mk_test_runtime(WasmExecutionMethod::Compiled, 1024);

let instance = runtime.new_instance().unwrap();
let mut instance = runtime.new_instance().unwrap();
let heap_base = instance
.get_global_const("__heap_base")
.expect("`__heap_base` is valid")
Expand Down
8 changes: 4 additions & 4 deletions client/executor/src/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ test_wasm_execution!(returns_mutable_static);
fn returns_mutable_static(wasm_method: WasmExecutionMethod) {
let runtime = mk_test_runtime(wasm_method, 1024);

let instance = runtime.new_instance().unwrap();
let mut instance = runtime.new_instance().unwrap();
let res = instance.call_export("returns_mutable_static", &[0]).unwrap();
assert_eq!(33, u64::decode(&mut &res[..]).unwrap());

Expand All @@ -482,7 +482,7 @@ test_wasm_execution!(returns_mutable_static_bss);
fn returns_mutable_static_bss(wasm_method: WasmExecutionMethod) {
let runtime = mk_test_runtime(wasm_method, 1024);

let instance = runtime.new_instance().unwrap();
let mut instance = runtime.new_instance().unwrap();
let res = instance.call_export("returns_mutable_static_bss", &[0]).unwrap();
assert_eq!(1, u64::decode(&mut &res[..]).unwrap());

Expand All @@ -508,7 +508,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
const REQUIRED_MEMORY_PAGES: u64 = 32;

let runtime = mk_test_runtime(wasm_method, REQUIRED_MEMORY_PAGES);
let instance = runtime.new_instance().unwrap();
let mut instance = runtime.new_instance().unwrap();

// On the first invocation we allocate approx. 768KB (75%) of stack and then trap.
let res = instance.call_export("allocates_huge_stack_array", &true.encode());
Expand All @@ -522,7 +522,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
test_wasm_execution!(interpreted_only heap_is_reset_between_calls);
fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
let runtime = mk_test_runtime(wasm_method, 1024);
let instance = runtime.new_instance().unwrap();
let mut instance = runtime.new_instance().unwrap();

let heap_base = instance
.get_global_const("__heap_base")
Expand Down
10 changes: 5 additions & 5 deletions client/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl WasmExecutor {
where
F: FnOnce(
AssertUnwindSafe<&Arc<dyn WasmModule>>,
AssertUnwindSafe<&dyn WasmInstance>,
AssertUnwindSafe<&mut dyn WasmInstance>,
Option<&RuntimeVersion>,
AssertUnwindSafe<&mut dyn Externalities>,
) -> Result<Result<R>>,
Expand Down Expand Up @@ -217,7 +217,7 @@ impl WasmExecutor {
.new_instance()
.map_err(|e| format!("Failed to create instance: {:?}", e))?;

let instance = AssertUnwindSafe(instance);
let mut instance = AssertUnwindSafe(instance);
let mut ext = AssertUnwindSafe(ext);
let module = AssertUnwindSafe(module);

Expand Down Expand Up @@ -283,7 +283,7 @@ impl CodeExecutor for WasmExecutor {
runtime_code,
ext,
false,
|module, instance, _onchain_version, mut ext| {
|module, mut instance, _onchain_version, mut ext| {
with_externalities_safe(&mut **ext, move || {
preregister_builtin_ext(module.clone());
instance.call_export(method, data).map(NativeOrEncoded::Encoded)
Expand Down Expand Up @@ -438,7 +438,7 @@ impl RuntimeSpawn for RuntimeInstanceSpawn {
// pool of instances should be used.
//
// https://github.com/paritytech/substrate/issues/7354
let instance =
let mut instance =
module.new_instance().expect("Failed to create new instance from module");

instance
Expand Down Expand Up @@ -525,7 +525,7 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeElseWasmExecut
runtime_code,
ext,
false,
|module, instance, onchain_version, mut ext| {
|module, mut instance, onchain_version, mut ext| {
let onchain_version =
onchain_version.ok_or_else(|| Error::ApiError("Unknown version".into()))?;

Expand Down
12 changes: 6 additions & 6 deletions client/executor/src/wasm_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl VersionedRuntime {
where
F: FnOnce(
&Arc<dyn WasmModule>,
&dyn WasmInstance,
&mut dyn WasmInstance,
Option<&RuntimeVersion>,
&mut dyn Externalities,
) -> Result<R, Error>,
Expand All @@ -90,12 +90,12 @@ impl VersionedRuntime {

match instance {
Some((index, mut locked)) => {
let (instance, new_inst) = locked
let (mut instance, new_inst) = locked
.take()
.map(|r| Ok((r, false)))
.unwrap_or_else(|| self.module.new_instance().map(|i| (i, true)))?;

let result = f(&self.module, &*instance, self.version.as_ref(), ext);
let result = f(&self.module, &mut *instance, self.version.as_ref(), ext);
if let Err(e) = &result {
if new_inst {
log::warn!(
Expand Down Expand Up @@ -129,9 +129,9 @@ impl VersionedRuntime {
log::warn!(target: "wasm-runtime", "Ran out of free WASM instances");

// Allocate a new instance
let instance = self.module.new_instance()?;
let mut instance = self.module.new_instance()?;

f(&self.module, &*instance, self.version.as_ref(), ext)
f(&self.module, &mut *instance, self.version.as_ref(), ext)
},
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ impl RuntimeCache {
where
F: FnOnce(
&Arc<dyn WasmModule>,
&dyn WasmInstance,
&mut dyn WasmInstance,
Option<&RuntimeVersion>,
&mut dyn Externalities,
) -> Result<R, Error>,
Expand Down
4 changes: 2 additions & 2 deletions client/executor/wasmi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ pub struct WasmiInstance {
unsafe impl Send for WasmiInstance {}

impl WasmInstance for WasmiInstance {
fn call(&self, method: InvokeMethod, data: &[u8]) -> Result<Vec<u8>, Error> {
fn call(&mut self, method: InvokeMethod, data: &[u8]) -> Result<Vec<u8>, Error> {
// We reuse a single wasm instance for multiple calls and a previous call (if any)
// altered the state. Therefore, we need to restore the instance to original state.

Expand Down Expand Up @@ -767,7 +767,7 @@ impl WasmInstance for WasmiInstance {
)
}

fn get_global_const(&self, name: &str) -> Result<Option<sp_wasm_interface::Value>, Error> {
fn get_global_const(&mut self, name: &str) -> Result<Option<sp_wasm_interface::Value>, Error> {
match self.instance.export_by_name(name) {
Some(global) => Ok(Some(
global
Expand Down
6 changes: 1 addition & 5 deletions client/executor/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ targets = ["x86_64-unknown-linux-gnu"]
libc = "0.2.90"
cfg-if = "1.0"
log = "0.4.8"
scoped-tls = "1.0"
parity-wasm = "0.42.0"
codec = { package = "parity-scale-codec", version = "2.0.0" }
sc-executor-common = { version = "0.10.0-dev", path = "../common" }
sp-wasm-interface = { version = "4.0.0-dev", path = "../../../primitives/wasm-interface" }
sp-runtime-interface = { version = "4.0.0-dev", path = "../../../primitives/runtime-interface" }
sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" }
sc-allocator = { version = "4.0.0-dev", path = "../../allocator" }
wasmtime = { version = "0.27.0", default-features = false, features = [
"cache",
"parallel-compilation",
] }
wasmtime = { version = "0.29.0", default-features = false, features = ["cache", "parallel-compilation"] }

[dev-dependencies]
sc-runtime-test = { version = "2.0.0", path = "../runtime-test" }
Expand Down
Loading