From 7f3680c27a3f7e4c41e85ee5e4d289bfd921ea87 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 15:36:45 +0100 Subject: [PATCH 1/2] fix(wasi) `get_wasi_version` is broken with multiple namespaces. If a module has multiple import namespaces, `get_wasi_version` is broken because it assumes a module must only have a single namespace. This patch fixes it by a slower `get_wasi_version` function, but a correct one. As soon as the `wasi_unstable` or `wasi_snapshot_preview1` namespace is met, `get_wasi_version` maps it to the respective `WasiVersion` variant. It assumes however that a module must hold a unique WASI version. --- lib/wasi/src/utils.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/wasi/src/utils.rs b/lib/wasi/src/utils.rs index b19a404884b..e7c24aff48f 100644 --- a/lib/wasi/src/utils.rs +++ b/lib/wasi/src/utils.rs @@ -18,24 +18,19 @@ pub enum WasiVersion { /// Detect the version of WASI being used from the namespace pub fn get_wasi_version(module: &Module) -> Option { - let mut import_iter = module + let namespace_table = &module.info().namespace_table; + + module .info() .imported_functions .iter() - .map(|(_, import_name)| import_name.namespace_index); + .find_map(|(_, import_name)| { + let namespace_index = import_name.namespace_index; - // returns None if empty - let first = import_iter.next()?; - if import_iter.all(|idx| idx == first) { - // once we know that all the namespaces are the same, we can use it to - // detect which version of WASI this is - match module.info().namespace_table.get(first) { - "wasi_unstable" => Some(WasiVersion::Snapshot0), - "wasi_snapshot_preview1" => Some(WasiVersion::Snapshot1), - _ => None, - } - } else { - // not all funcs have the same namespace, therefore it's not WASI - None - } + match namespace_table.get(namespace_index) { + "wasi_unstable" => Some(WasiVersion::Snapshot0), + "wasi_snapshot_preview1" => Some(WasiVersion::Snapshot1), + _ => None, + } + }) } From 8df0591ee57ef8bacbacbcb13a6c41129129ede3 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 15:41:46 +0100 Subject: [PATCH 2/2] doc(changelog) Add #1028. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1de67bf893..79f9da5aa5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## **[Unreleased]** +- [#1028](https://github.com/wasmerio/wasmer/pull/1028) Fix `get_wasi_version` when a module has multiple import namespaces. - [#1006](https://github.com/wasmerio/wasmer/pull/1006) Fix minor panic issue when `wasmer::compile_with` called with llvm backend - [#1009](https://github.com/wasmerio/wasmer/pull/1009) Enable LLVM verifier for all tests, add new llvm-backend-tests crate. - [#1004](https://github.com/wasmerio/wasmer/pull/1004) Add the Auto backend to enable to adapt backend usage depending on wasm file executed.