From a9e446b5cdb2e2ffc1c54b385b7eee5d41b53dd3 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 21 Nov 2019 10:51:04 +0100 Subject: [PATCH 1/3] fix(runtime-core) Fix a panic when generating globals. Fix https://github.com/wasmerio/wasmer/issues/979. When we try to get a global that doesn't exist, a panic is generated. This patch just skip that path, and let a proper error be generated later. With this patch, we get: ```sh $ cargo run -- run panic_index_oob_all_backends.wasm Error: ExportNotFound { name: "main" } ``` which is kind of the expected behavior in such situation. --- lib/runtime-core/src/backing.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index c88cb953dfb..c3118cfb00f 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -450,6 +450,11 @@ impl LocalBacking { let value = match &global_init.init { Initializer::Const(value) => value.clone(), Initializer::GetGlobal(import_global_index) => { + // Skip if the global hasn't been initialized properly. + if imports.globals.len() <= import_global_index.index() { + continue; + } + imports.globals[*import_global_index].get() } }; From b1f58bded0925343bd322f5ab60e4b1515f12cea Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 21 Nov 2019 10:57:52 +0100 Subject: [PATCH 2/3] fix(runtime-core) Improve error message when globals are corrupted. Before this patch: ``` $ cargo run -- run panic_index_oob_all_backends.wasm Error: ExportNotFound { name: "main" } ``` With this patch: ```sh $ cargo run -- run panic_index_oob_all_backends.wasm Error: Can't instantiate module: LinkError([Generic { message: "Trying to read the `0` global that isn\'t properly initialized." }]) ``` --- lib/runtime-core/src/backing.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index c3118cfb00f..1b50abaf6ab 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -77,7 +77,7 @@ impl LocalBacking { } }; let mut tables = Self::generate_tables(module); - let mut globals = Self::generate_globals(module, imports); + let mut globals = Self::generate_globals(module, imports)?; // Ensure all initializers are valid before running finalizers Self::validate_memories(module, imports)?; @@ -443,16 +443,20 @@ impl LocalBacking { fn generate_globals( module: &ModuleInner, imports: &ImportBacking, - ) -> BoxedMap { + ) -> LinkResult> { let mut globals = Map::with_capacity(module.info.globals.len()); for (_, global_init) in module.info.globals.iter() { let value = match &global_init.init { Initializer::Const(value) => value.clone(), Initializer::GetGlobal(import_global_index) => { - // Skip if the global hasn't been initialized properly. if imports.globals.len() <= import_global_index.index() { - continue; + return Err(vec![LinkError::Generic { + message: format!( + "Trying to read the `{:?}` global that is not properly initialized.", + import_global_index.index() + ), + }]); } imports.globals[*import_global_index].get() @@ -468,7 +472,7 @@ impl LocalBacking { globals.push(global); } - globals.into_boxed_map() + Ok(globals.into_boxed_map()) } fn finalize_globals( From 7313672d96c6b5fb5bfd97cdd2f69246ca63d359 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 21 Nov 2019 11:03:53 +0100 Subject: [PATCH 3/3] doc(changelog) Add #995. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7b0088dc52..2696d054670 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## **[Unreleased]** +- [#995](https://github.com/wasmerio/wasmer/pull/995) Detect when a global is read without being initialized (emit a proper error instead of panicking) - [#992](https://github.com/wasmerio/wasmer/pull/992) Updates WAPM version to 0.4.1, fix arguments issue introduced in #990 - [#990](https://github.com/wasmerio/wasmer/pull/990) Default wasmer CLI to `run`. Wasmer will now attempt to parse unrecognized command line options as if they were applied to the run command: `wasmer mywasm.wasm --dir=.` now works! - [#987](https://github.com/wasmerio/wasmer/pull/987) Fix `runtime-c-api` header files when compiled by gnuc.