Skip to content

Commit

Permalink
Improve wasmtime error reporting (paritytech#11856)
Browse files Browse the repository at this point in the history
* Improve `wasmtime` error reporting

* cargo fmt
  • Loading branch information
koute authored and ark0f committed Feb 27, 2023
1 parent dbf6339 commit 53e7600
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion client/executor/wasmtime/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
self.linker.func_wrap("env", fn_name, func).map_err(|error| {
WasmError::Other(format!(
"failed to register host function '{}' with the WASM linker: {}",
"failed to register host function '{}' with the WASM linker: {:#}",
fn_name, error
))
})?;
Expand Down
7 changes: 4 additions & 3 deletions client/executor/wasmtime/src/instance_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ impl InstanceWrapper {
) -> Result<Self> {
let mut store = create_store(engine, max_memory_size);
let instance = instance_pre.instantiate(&mut store).map_err(|error| {
WasmError::Other(
format!("failed to instantiate a new WASM module instance: {}", error,),
)
WasmError::Other(format!(
"failed to instantiate a new WASM module instance: {:#}",
error,
))
})?;

let memory = get_linear_memory(&instance, &mut store)?;
Expand Down
24 changes: 12 additions & 12 deletions client/executor/wasmtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ fn setup_wasmtime_caching(

let wasmtime_cache_root = cache_path.join("wasmtime");
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.
let wasmtime_cache_root = wasmtime_cache_root
.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
let cache_config_path = wasmtime_cache_root.join("cache-config.toml");
Expand All @@ -275,11 +275,11 @@ directory = \"{cache_dir}\"
cache_dir = wasmtime_cache_root.display()
);
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
.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(())
}
Expand All @@ -304,14 +304,14 @@ fn common_config(semantics: &Semantics) -> std::result::Result<wasmtime::Config,
};
config
.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, .. }) =
semantics.deterministic_stack_limit
{
config
.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);
Expand Down Expand Up @@ -618,15 +618,15 @@ where
}

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 {
CodeSupplyMode::Fresh(blob) => {
let blob = prepare_blob_for_compilation(blob, &config.semantics)?;
let serialized_blob = blob.clone().serialize();

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 {
InstantiationStrategy::LegacyInstanceReuse => {
Expand Down Expand Up @@ -664,7 +664,7 @@ where
//
// See [`create_runtime_from_artifact`] for more details.
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)
},
Expand All @@ -677,7 +677,7 @@ where
crate::instance_wrapper::create_store(module.engine(), config.semantics.max_memory_size);
let instance_pre = linker
.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 {
engine,
Expand Down Expand Up @@ -729,11 +729,11 @@ pub fn prepare_runtime_artifact(
let blob = prepare_blob_for_compilation(blob, &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
.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(
Expand Down

0 comments on commit 53e7600

Please sign in to comment.