From 503c701fd311f35bd37934b9fa7bcfd3dfc43e28 Mon Sep 17 00:00:00 2001 From: akorchyn Date: Mon, 16 Sep 2024 16:17:49 +0300 Subject: [PATCH 1/3] bugfix: re-use engine that was used during loading module to keep same state. --- runtime/near-vm-runner/src/wasmtime_runner.rs | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/runtime/near-vm-runner/src/wasmtime_runner.rs b/runtime/near-vm-runner/src/wasmtime_runner.rs index 82820cbb4c0..c67a86ab495 100644 --- a/runtime/near-vm-runner/src/wasmtime_runner.rs +++ b/runtime/near-vm-runner/src/wasmtime_runner.rs @@ -144,12 +144,11 @@ pub(crate) fn wasmtime_vm_hash() -> u64 { pub(crate) struct WasmtimeVM { config: Arc, - engine: wasmtime::Engine, } impl WasmtimeVM { pub(crate) fn new(config: Arc) -> Self { - Self { engine: get_engine(&default_wasmtime_config(&config)), config } + Self { config } } #[tracing::instrument(target = "vm", level = "debug", "WasmtimeVM::compile_uncached", skip_all)] @@ -157,7 +156,8 @@ impl WasmtimeVM { let start = std::time::Instant::now(); let prepared_code = prepare::prepare_contract(code.code(), &self.config, VMKind::Wasmtime) .map_err(CompilationError::PrepareError)?; - let serialized = self.engine.precompile_module(&prepared_code).map_err(|err| { + let engine = get_engine(&default_wasmtime_config(&self.config)); + let serialized = engine.precompile_module(&prepared_code).map_err(|err| { tracing::error!(?err, "wasmtime failed to compile the prepared code (this is defense-in-depth, the error was recovered from but should be reported to the developers)"); CompilationError::WasmtimeCompileError { msg: err.to_string() } }); @@ -206,8 +206,11 @@ impl WasmtimeVM { code.code().len() as u64, match self.compile_and_cache(&code, cache)? { Ok(serialized_module) => Ok(unsafe { - Module::deserialize(&self.engine, serialized_module) - .map_err(|err| VMRunnerError::LoadingError(err.to_string()))? + Module::deserialize( + &get_engine(&default_wasmtime_config(&self.config)), + serialized_module, + ) + .map_err(|err| VMRunnerError::LoadingError(err.to_string()))? }), Err(err) => Err(err), }, @@ -230,8 +233,11 @@ impl WasmtimeVM { // // There should definitely be some validation in near_vm to ensure // we load what we think we load. - let module = Module::deserialize(&self.engine, &serialized_module) - .map_err(|err| VMRunnerError::LoadingError(err.to_string()))?; + let module = Module::deserialize( + &get_engine(&default_wasmtime_config(&self.config)), + &serialized_module, + ) + .map_err(|err| VMRunnerError::LoadingError(err.to_string()))?; Ok(to_any((compiled_contract_info.wasm_bytes, Ok(module)))) } } @@ -327,7 +333,7 @@ impl crate::runner::VM for WasmtimeVM { } } - let mut store = Store::new(&self.engine, ()); + let mut store = Store::new(module.engine(), ()); let memory = WasmtimeMemory::new( &mut store, self.config.limit_config.initial_memory_pages, From 16d99658fe17a01dfdc763bbf17dfd26c409c7b1 Mon Sep 17 00:00:00 2001 From: akorchyn Date: Mon, 16 Sep 2024 18:03:29 +0300 Subject: [PATCH 2/3] reverted removing engine --- runtime/near-vm-runner/src/wasmtime_runner.rs | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/runtime/near-vm-runner/src/wasmtime_runner.rs b/runtime/near-vm-runner/src/wasmtime_runner.rs index c67a86ab495..2c94d5d696e 100644 --- a/runtime/near-vm-runner/src/wasmtime_runner.rs +++ b/runtime/near-vm-runner/src/wasmtime_runner.rs @@ -144,11 +144,12 @@ pub(crate) fn wasmtime_vm_hash() -> u64 { pub(crate) struct WasmtimeVM { config: Arc, + engine: Engine, } impl WasmtimeVM { pub(crate) fn new(config: Arc) -> Self { - Self { config } + Self { config, engine: get_engine(&default_wasmtime_config(&config)) } } #[tracing::instrument(target = "vm", level = "debug", "WasmtimeVM::compile_uncached", skip_all)] @@ -156,8 +157,7 @@ impl WasmtimeVM { let start = std::time::Instant::now(); let prepared_code = prepare::prepare_contract(code.code(), &self.config, VMKind::Wasmtime) .map_err(CompilationError::PrepareError)?; - let engine = get_engine(&default_wasmtime_config(&self.config)); - let serialized = engine.precompile_module(&prepared_code).map_err(|err| { + let serialized = self.engine.precompile_module(&prepared_code).map_err(|err| { tracing::error!(?err, "wasmtime failed to compile the prepared code (this is defense-in-depth, the error was recovered from but should be reported to the developers)"); CompilationError::WasmtimeCompileError { msg: err.to_string() } }); @@ -206,11 +206,8 @@ impl WasmtimeVM { code.code().len() as u64, match self.compile_and_cache(&code, cache)? { Ok(serialized_module) => Ok(unsafe { - Module::deserialize( - &get_engine(&default_wasmtime_config(&self.config)), - serialized_module, - ) - .map_err(|err| VMRunnerError::LoadingError(err.to_string()))? + Module::deserialize(&self.engine, serialized_module) + .map_err(|err| VMRunnerError::LoadingError(err.to_string()))? }), Err(err) => Err(err), }, @@ -233,11 +230,8 @@ impl WasmtimeVM { // // There should definitely be some validation in near_vm to ensure // we load what we think we load. - let module = Module::deserialize( - &get_engine(&default_wasmtime_config(&self.config)), - &serialized_module, - ) - .map_err(|err| VMRunnerError::LoadingError(err.to_string()))?; + let module = Module::deserialize(&self.engine, serialized_module) + .map_err(|err| VMRunnerError::LoadingError(err.to_string()))?; Ok(to_any((compiled_contract_info.wasm_bytes, Ok(module)))) } } From 960a4f22c3f976db2c7d15f862240067bb4d10f0 Mon Sep 17 00:00:00 2001 From: akorchyn Date: Mon, 16 Sep 2024 18:04:58 +0300 Subject: [PATCH 3/3] reverted removing engine --- runtime/near-vm-runner/src/wasmtime_runner.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/near-vm-runner/src/wasmtime_runner.rs b/runtime/near-vm-runner/src/wasmtime_runner.rs index 2c94d5d696e..a615c8d3aaa 100644 --- a/runtime/near-vm-runner/src/wasmtime_runner.rs +++ b/runtime/near-vm-runner/src/wasmtime_runner.rs @@ -144,12 +144,12 @@ pub(crate) fn wasmtime_vm_hash() -> u64 { pub(crate) struct WasmtimeVM { config: Arc, - engine: Engine, + engine: wasmtime::Engine, } impl WasmtimeVM { pub(crate) fn new(config: Arc) -> Self { - Self { config, engine: get_engine(&default_wasmtime_config(&config)) } + Self { engine: get_engine(&default_wasmtime_config(&config)), config } } #[tracing::instrument(target = "vm", level = "debug", "WasmtimeVM::compile_uncached", skip_all)] @@ -230,7 +230,7 @@ impl WasmtimeVM { // // There should definitely be some validation in near_vm to ensure // we load what we think we load. - let module = Module::deserialize(&self.engine, serialized_module) + let module = Module::deserialize(&self.engine, &serialized_module) .map_err(|err| VMRunnerError::LoadingError(err.to_string()))?; Ok(to_any((compiled_contract_info.wasm_bytes, Ok(module)))) }