Opt-out from fast instance reuse and foundation for other refactorings (#8394)

* Establish the runtime_blob module

Seed it with the existing contents of the `util` module.

* Port wasmtime mutable globals instrumentation into runtime blob APIs

* Opt-out from fast instance reuse

* Minor clean up

* Spaces

* Docs clean up

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Factor out the expects

* Fix the suggestion

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Sergei Shulepov
2021-04-06 19:21:34 +03:00
committed by GitHub
parent 6a8c6b2b0a
commit b2a6ad0553
15 changed files with 592 additions and 285 deletions
@@ -25,53 +25,11 @@ use crate::imports::Imports;
use std::{slice, marker};
use sc_executor_common::{
error::{Error, Result},
util::{WasmModuleInfo, DataSegmentsSnapshot},
runtime_blob,
wasm_runtime::InvokeMethod,
};
use sp_wasm_interface::{Pointer, WordSize, Value};
use wasmtime::{Engine, Instance, Module, Memory, Table, Val, Func, Extern, Global, Store};
use parity_wasm::elements;
mod globals_snapshot;
pub use globals_snapshot::GlobalsSnapshot;
pub struct ModuleWrapper {
module: Module,
data_segments_snapshot: DataSegmentsSnapshot,
}
impl ModuleWrapper {
pub fn new(engine: &Engine, code: &[u8]) -> Result<Self> {
let mut raw_module: elements::Module = elements::deserialize_buffer(code)
.map_err(|e| Error::from(format!("cannot decode module: {}", e)))?;
pwasm_utils::export_mutable_globals(&mut raw_module, "exported_internal_global");
let instrumented_code = elements::serialize(raw_module)
.map_err(|e| Error::from(format!("cannot encode module: {}", e)))?;
let module = Module::new(engine, &instrumented_code)
.map_err(|e| Error::from(format!("cannot create module: {}", e)))?;
let module_info = WasmModuleInfo::new(code)
.ok_or_else(|| Error::from("cannot deserialize module".to_string()))?;
let data_segments_snapshot = DataSegmentsSnapshot::take(&module_info)
.map_err(|e| Error::from(format!("cannot take data segments snapshot: {}", e)))?;
Ok(Self {
module,
data_segments_snapshot,
})
}
pub fn module(&self) -> &Module {
&self.module
}
pub fn data_segments_snapshot(&self) -> &DataSegmentsSnapshot {
&self.data_segments_snapshot
}
}
use wasmtime::{Instance, Module, Memory, Table, Val, Func, Extern, Global, Store};
/// Invoked entrypoint format.
pub enum EntryPointType {
@@ -197,8 +155,8 @@ fn extern_func(extern_: &Extern) -> Option<&Func> {
impl InstanceWrapper {
/// Create a new instance wrapper from the given wasm module.
pub fn new(store: &Store, module_wrapper: &ModuleWrapper, imports: &Imports, heap_pages: u32) -> Result<Self> {
let instance = Instance::new(store, &module_wrapper.module, &imports.externs)
pub fn new(store: &Store, module: &Module, imports: &Imports, heap_pages: u32) -> Result<Self> {
let instance = Instance::new(store, module, &imports.externs)
.map_err(|e| Error::from(format!("cannot instantiate: {}", e)))?;
let memory = match imports.memory_import_index {
@@ -462,3 +420,23 @@ impl InstanceWrapper {
}
}
}
impl runtime_blob::InstanceGlobals for InstanceWrapper {
type Global = wasmtime::Global;
fn get_global(&self, export_name: &str) -> Self::Global {
self.instance
.get_global(export_name)
.expect("get_global is guaranteed to be called with an export name of a global; qed")
}
fn get_global_value(&self, global: &Self::Global) -> Value {
util::from_wasmtime_val(global.get())
}
fn set_global_value(&self, global: &Self::Global, value: Value) {
global.set(util::into_wasmtime_val(value)).expect(
"the value is guaranteed to be of the same value; the global is guaranteed to be mutable; qed",
);
}
}