Improve wasmtime error reporting (#11856)

* Improve `wasmtime` error reporting

* cargo fmt
This commit is contained in:
Koute
2022-07-18 20:51:38 +09:00
committed by GitHub
parent 20abd5dc24
commit 8f6ba2e75e
3 changed files with 17 additions and 16 deletions
@@ -112,7 +112,7 @@ impl<'a, 'b> sp_wasm_interface::HostFunctionRegistry for Registry<'a, 'b> {
if self.pending_func_imports.remove(fn_name).is_some() { if self.pending_func_imports.remove(fn_name).is_some() {
self.linker.func_wrap("env", fn_name, func).map_err(|error| { self.linker.func_wrap("env", fn_name, func).map_err(|error| {
WasmError::Other(format!( WasmError::Other(format!(
"failed to register host function '{}' with the WASM linker: {}", "failed to register host function '{}' with the WASM linker: {:#}",
fn_name, error fn_name, error
)) ))
})?; })?;
@@ -186,9 +186,10 @@ impl InstanceWrapper {
) -> Result<Self> { ) -> Result<Self> {
let mut store = create_store(engine, max_memory_size); let mut store = create_store(engine, max_memory_size);
let instance = instance_pre.instantiate(&mut store).map_err(|error| { let instance = instance_pre.instantiate(&mut store).map_err(|error| {
WasmError::Other( WasmError::Other(format!(
format!("failed to instantiate a new WASM module instance: {}", error,), "failed to instantiate a new WASM module instance: {:#}",
) error,
))
})?; })?;
let memory = get_linear_memory(&instance, &mut store)?; let memory = get_linear_memory(&instance, &mut store)?;
@@ -257,12 +257,12 @@ fn setup_wasmtime_caching(
let wasmtime_cache_root = cache_path.join("wasmtime"); let wasmtime_cache_root = cache_path.join("wasmtime");
fs::create_dir_all(&wasmtime_cache_root) fs::create_dir_all(&wasmtime_cache_root)
.map_err(|err| format!("cannot create the dirs to cache: {:?}", err))?; .map_err(|err| format!("cannot create the dirs to cache: {}", err))?;
// Canonicalize the path after creating the directories. // Canonicalize the path after creating the directories.
let wasmtime_cache_root = wasmtime_cache_root let wasmtime_cache_root = wasmtime_cache_root
.canonicalize() .canonicalize()
.map_err(|err| format!("failed to canonicalize the path: {:?}", err))?; .map_err(|err| format!("failed to canonicalize the path: {}", err))?;
// Write the cache config file // Write the cache config file
let cache_config_path = wasmtime_cache_root.join("cache-config.toml"); let cache_config_path = wasmtime_cache_root.join("cache-config.toml");
@@ -275,11 +275,11 @@ directory = \"{cache_dir}\"
cache_dir = wasmtime_cache_root.display() cache_dir = wasmtime_cache_root.display()
); );
fs::write(&cache_config_path, config_content) fs::write(&cache_config_path, config_content)
.map_err(|err| format!("cannot write the cache config: {:?}", err))?; .map_err(|err| format!("cannot write the cache config: {}", err))?;
config config
.cache_config_load(cache_config_path) .cache_config_load(cache_config_path)
.map_err(|err| format!("failed to parse the config: {:?}", err))?; .map_err(|err| format!("failed to parse the config: {:#}", err))?;
Ok(()) Ok(())
} }
@@ -304,14 +304,14 @@ fn common_config(semantics: &Semantics) -> std::result::Result<wasmtime::Config,
}; };
config config
.profiler(profiler) .profiler(profiler)
.map_err(|e| WasmError::Instantiation(format!("fail to set profiler: {}", e)))?; .map_err(|e| WasmError::Instantiation(format!("fail to set profiler: {:#}", e)))?;
if let Some(DeterministicStackLimit { native_stack_max, .. }) = if let Some(DeterministicStackLimit { native_stack_max, .. }) =
semantics.deterministic_stack_limit semantics.deterministic_stack_limit
{ {
config config
.max_wasm_stack(native_stack_max as usize) .max_wasm_stack(native_stack_max as usize)
.map_err(|e| WasmError::Other(format!("cannot set max wasm stack: {}", e)))?; .map_err(|e| WasmError::Other(format!("cannot set max wasm stack: {:#}", e)))?;
} }
config.parallel_compilation(semantics.parallel_compilation); config.parallel_compilation(semantics.parallel_compilation);
@@ -618,7 +618,7 @@ where
} }
let engine = Engine::new(&wasmtime_config) let engine = Engine::new(&wasmtime_config)
.map_err(|e| WasmError::Other(format!("cannot create the wasmtime engine: {}", e)))?; .map_err(|e| WasmError::Other(format!("cannot create the wasmtime engine: {:#}", e)))?;
let (module, instantiation_strategy) = match code_supply_mode { let (module, instantiation_strategy) = match code_supply_mode {
CodeSupplyMode::Fresh(blob) => { CodeSupplyMode::Fresh(blob) => {
@@ -626,7 +626,7 @@ where
let serialized_blob = blob.clone().serialize(); let serialized_blob = blob.clone().serialize();
let module = wasmtime::Module::new(&engine, &serialized_blob) let module = wasmtime::Module::new(&engine, &serialized_blob)
.map_err(|e| WasmError::Other(format!("cannot create module: {}", e)))?; .map_err(|e| WasmError::Other(format!("cannot create module: {:#}", e)))?;
match config.semantics.instantiation_strategy { match config.semantics.instantiation_strategy {
InstantiationStrategy::LegacyInstanceReuse => { InstantiationStrategy::LegacyInstanceReuse => {
@@ -664,7 +664,7 @@ where
// //
// See [`create_runtime_from_artifact`] for more details. // See [`create_runtime_from_artifact`] for more details.
let module = wasmtime::Module::deserialize_file(&engine, compiled_artifact_path) let module = wasmtime::Module::deserialize_file(&engine, compiled_artifact_path)
.map_err(|e| WasmError::Other(format!("cannot deserialize module: {}", e)))?; .map_err(|e| WasmError::Other(format!("cannot deserialize module: {:#}", e)))?;
(module, InternalInstantiationStrategy::Builtin) (module, InternalInstantiationStrategy::Builtin)
}, },
@@ -677,7 +677,7 @@ where
crate::instance_wrapper::create_store(module.engine(), config.semantics.max_memory_size); crate::instance_wrapper::create_store(module.engine(), config.semantics.max_memory_size);
let instance_pre = linker let instance_pre = linker
.instantiate_pre(&mut store, &module) .instantiate_pre(&mut store, &module)
.map_err(|e| WasmError::Other(format!("cannot preinstantiate module: {}", e)))?; .map_err(|e| WasmError::Other(format!("cannot preinstantiate module: {:#}", e)))?;
Ok(WasmtimeRuntime { Ok(WasmtimeRuntime {
engine, engine,
@@ -729,11 +729,11 @@ pub fn prepare_runtime_artifact(
let blob = prepare_blob_for_compilation(blob, &semantics)?; let blob = prepare_blob_for_compilation(blob, &semantics)?;
let engine = Engine::new(&common_config(&semantics)?) let engine = Engine::new(&common_config(&semantics)?)
.map_err(|e| WasmError::Other(format!("cannot create the engine: {}", e)))?; .map_err(|e| WasmError::Other(format!("cannot create the engine: {:#}", e)))?;
engine engine
.precompile_module(&blob.serialize()) .precompile_module(&blob.serialize())
.map_err(|e| WasmError::Other(format!("cannot precompile module: {}", e))) .map_err(|e| WasmError::Other(format!("cannot precompile module: {:#}", e)))
} }
fn perform_call( fn perform_call(