From fdc3d5107aa100074645da72b8431042dbbe594d Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 15:58:23 +0100 Subject: [PATCH 01/15] doc(runtime-c-api) Suggest to test in release mode. --- lib/runtime-c-api/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime-c-api/README.md b/lib/runtime-c-api/README.md index 06e545bf739..39e2c21a9fa 100644 --- a/lib/runtime-c-api/README.md +++ b/lib/runtime-c-api/README.md @@ -112,7 +112,7 @@ rebuild in release mode for the tests to see the changes. The tests can be run via `cargo test`, such as: ```sh -$ cargo test -- --nocapture +$ cargo test --release -- --nocapture ``` To run tests manually, enter the `lib/runtime-c-api/tests` directory From ca4a1b41a66fbfbcd5466826a47a19e8aab74369 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 15:59:20 +0100 Subject: [PATCH 02/15] feat(runtime-c-api) Ability to generate `ImportObject` for a specific WASI version. This patch introduces 2 new functions: * `wasmer_wasi_generate_import_object_for_version` and * `wasmer_wasi_get_version`. It mimics the current API provided by `wasmer_wasi`, nothing fancy here. It's just a regular port to C/C++. Because `wasmer_wasi::get_wasi_version` returns an option, and in order to simplify the C/C++ API, `wasmer_wasi_get_version` can return `Version::Unknown` in case of an error. It's up to the user to check the version is valid (i.e. not unknown). --- lib/runtime-c-api/src/import/wasi.rs | 91 ++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 12 deletions(-) diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index 3df3c7f700b..ec362fdf2ec 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -1,6 +1,18 @@ use super::*; use crate::get_slice_checked; -use std::path::PathBuf; +use std::{path::PathBuf, ptr, str}; +use wasmer_wasi as wasi; + +#[derive(Debug)] +#[repr(u8)] +pub enum Version { + /// Version cannot be detected or is unknown. + Unknown, + /// `wasi_unstable`. + Snapshot0, + /// `wasi_snapshot_preview1`. + Snapshot1, +} /// Opens a directory that's visible to the WASI module as `alias` but /// is backed by the host file at `host_file_path` @@ -14,9 +26,9 @@ pub struct wasmer_wasi_map_dir_entry_t { impl wasmer_wasi_map_dir_entry_t { /// Converts the data into owned, Rust types - pub unsafe fn as_tuple(&self) -> Result<(String, PathBuf), std::str::Utf8Error> { + pub unsafe fn as_tuple(&self) -> Result<(String, PathBuf), str::Utf8Error> { let alias = self.alias.as_str()?.to_owned(); - let host_path = std::path::PathBuf::from(self.host_file_path.as_str()?); + let host_path = PathBuf::from(self.host_file_path.as_str()?); Ok((alias, host_path)) } @@ -44,21 +56,74 @@ pub unsafe extern "C" fn wasmer_wasi_generate_import_object( let mapped_dir_list = get_slice_checked(mapped_dirs, mapped_dirs_len as usize); wasmer_wasi_generate_import_object_inner( + Version::Snapshot1, arg_list, env_list, preopened_file_list, mapped_dir_list, ) - .unwrap_or(std::ptr::null_mut()) + .unwrap_or(ptr::null_mut()) +} + +/// Creates a WASI import object. +/// +/// This function is similar to `wasmer_wasi_generate_import_object` +/// except that the first argument describes the WASI version. +#[no_mangle] +pub unsafe extern "C" fn wasmer_wasi_generate_import_object_for_version( + version: Version, + args: *const wasmer_byte_array, + args_len: c_uint, + envs: *const wasmer_byte_array, + envs_len: c_uint, + preopened_files: *const wasmer_byte_array, + preopened_files_len: c_uint, + mapped_dirs: *const wasmer_wasi_map_dir_entry_t, + mapped_dirs_len: c_uint, +) -> *mut wasmer_import_object_t { + let arg_list = get_slice_checked(args, args_len as usize); + let env_list = get_slice_checked(envs, envs_len as usize); + let preopened_file_list = get_slice_checked(preopened_files, preopened_files_len as usize); + let mapped_dir_list = get_slice_checked(mapped_dirs, mapped_dirs_len as usize); + + wasmer_wasi_generate_import_object_inner( + version, + arg_list, + env_list, + preopened_file_list, + mapped_dir_list, + ) + .unwrap_or(ptr::null_mut()) +} + +/// Find the version of WASI used by the module. +/// +/// In case of error, the returned version is `Version::Unknown`. +#[no_mangle] +pub unsafe extern "C" fn wasmer_wasi_get_version(module: *const wasmer_module_t) -> Version { + if module.is_null() { + return Version::Unknown; + } + + let module = &*(module as *const Module); + + match wasi::get_wasi_version(module) { + Some(version) => match version { + wasi::WasiVersion::Snapshot0 => Version::Snapshot0, + wasi::WasiVersion::Snapshot1 => Version::Snapshot1, + }, + None => Version::Unknown, + } } /// Inner function that wraps error handling fn wasmer_wasi_generate_import_object_inner( + version: Version, arg_list: &[wasmer_byte_array], env_list: &[wasmer_byte_array], preopened_file_list: &[wasmer_byte_array], mapped_dir_list: &[wasmer_wasi_map_dir_entry_t], -) -> Result<*mut wasmer_import_object_t, std::str::Utf8Error> { +) -> Result<*mut wasmer_import_object_t, str::Utf8Error> { let arg_vec = arg_list.iter().map(|arg| unsafe { arg.as_vec() }).collect(); let env_vec = env_list .iter() @@ -73,7 +138,14 @@ fn wasmer_wasi_generate_import_object_inner( .map(|entry| unsafe { entry.as_tuple() }) .collect::, _>>()?; - let import_object = Box::new(wasmer_wasi::generate_import_object( + let version = match version { + Version::Snapshot0 => wasi::WasiVersion::Snapshot0, + Version::Snapshot1 => wasi::WasiVersion::Snapshot1, + _ => panic!(format!("Version {:?} is invalid.", version)), + }; + + let import_object = Box::new(wasi::generate_import_object_for_version( + version, arg_vec, env_vec, po_file_vec, @@ -90,12 +162,7 @@ fn wasmer_wasi_generate_import_object_inner( #[no_mangle] pub unsafe extern "C" fn wasmer_wasi_generate_default_import_object() -> *mut wasmer_import_object_t { - let import_object = Box::new(wasmer_wasi::generate_import_object( - vec![], - vec![], - vec![], - vec![], - )); + let import_object = Box::new(wasi::generate_import_object(vec![], vec![], vec![], vec![])); Box::into_raw(import_object) as *mut wasmer_import_object_t } From e2c353b926ad4e46a8b3a10289188ca021501255 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 16:05:01 +0100 Subject: [PATCH 03/15] chore(runtime-c-api) Update C/C++ headers. --- lib/runtime-c-api/wasmer.h | 39 +++++++++++++++++++++++++++++++++++++ lib/runtime-c-api/wasmer.hh | 28 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 40a265cff3b..672a1704cd5 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -25,6 +25,22 @@ #include #include +enum Version { + /** + * Version cannot be detected or is unknown. + */ + Unknown, + /** + * `wasi_unstable`. + */ + Snapshot0, + /** + * `wasi_snapshot_preview1`. + */ + Snapshot1, +}; +typedef uint8_t Version; + /** * List of export/import kinds. */ @@ -890,4 +906,27 @@ wasmer_import_object_t *wasmer_wasi_generate_import_object(const wasmer_byte_arr const wasmer_wasi_map_dir_entry_t *mapped_dirs, unsigned int mapped_dirs_len); +/** + * Creates a WASI import object. + * + * This function is similar to `wasmer_wasi_generate_import_object` + * except that the first argument describes the WASI version. + */ +wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(Version version, + const wasmer_byte_array *args, + unsigned int args_len, + const wasmer_byte_array *envs, + unsigned int envs_len, + const wasmer_byte_array *preopened_files, + unsigned int preopened_files_len, + const wasmer_wasi_map_dir_entry_t *mapped_dirs, + unsigned int mapped_dirs_len); + +/** + * Find the version of WASI used by the module. + * + * In case of error, the returned version is `Version::Unknown`. + */ +Version wasmer_wasi_get_version(const wasmer_module_t *module); + #endif /* WASMER_H */ diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index b437edd4be0..d293c817ebc 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -25,6 +25,15 @@ #include #include +enum class Version : uint8_t { + /// Version cannot be detected or is unknown. + Unknown, + /// `wasi_unstable`. + Snapshot0, + /// `wasi_snapshot_preview1`. + Snapshot1, +}; + /// List of export/import kinds. enum class wasmer_import_export_kind : uint32_t { WASM_FUNCTION = 0, @@ -702,6 +711,25 @@ wasmer_import_object_t *wasmer_wasi_generate_import_object(const wasmer_byte_arr const wasmer_wasi_map_dir_entry_t *mapped_dirs, unsigned int mapped_dirs_len); +/// Creates a WASI import object. +/// +/// This function is similar to `wasmer_wasi_generate_import_object` +/// except that the first argument describes the WASI version. +wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(Version version, + const wasmer_byte_array *args, + unsigned int args_len, + const wasmer_byte_array *envs, + unsigned int envs_len, + const wasmer_byte_array *preopened_files, + unsigned int preopened_files_len, + const wasmer_wasi_map_dir_entry_t *mapped_dirs, + unsigned int mapped_dirs_len); + +/// Find the version of WASI used by the module. +/// +/// In case of error, the returned version is `Version::Unknown`. +Version wasmer_wasi_get_version(const wasmer_module_t *module); + } // extern "C" #endif // WASMER_H From 7fb934f5d09452935d4e9628a5b634e8ad6f03c5 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 16:05:34 +0100 Subject: [PATCH 04/15] chore(runtime-c-api) Fix CS in `CMakeLists.txt`. --- lib/runtime-c-api/tests/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/runtime-c-api/tests/CMakeLists.txt b/lib/runtime-c-api/tests/CMakeLists.txt index e4a573e6386..20f7dea13bd 100644 --- a/lib/runtime-c-api/tests/CMakeLists.txt +++ b/lib/runtime-c-api/tests/CMakeLists.txt @@ -19,12 +19,12 @@ add_executable(test-context test-context.c) add_executable(test-module-import-instantiate test-module-import-instantiate.c) if (DEFINED WASI_TESTS) - add_executable(test-wasi-import-object test-wasi-import-object.c) + add_executable(test-wasi-import-object test-wasi-import-object.c) endif() find_library( - WASMER_LIB NAMES libwasmer_runtime_c_api.dylib libwasmer_runtime_c_api.so wasmer_runtime_c_api.dll - PATHS ${CMAKE_SOURCE_DIR}/../../../target/release/ + WASMER_LIB NAMES libwasmer_runtime_c_api.dylib libwasmer_runtime_c_api.so wasmer_runtime_c_api.dll + PATHS ${CMAKE_SOURCE_DIR}/../../../target/release/ ) if(NOT WASMER_LIB) From 0391ade76f0d1a7abd8099279154ecc256a320a1 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 16:07:20 +0100 Subject: [PATCH 05/15] chore(git) Ignore the object file `test-import-object`. --- lib/runtime-c-api/tests/.gitignore | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/runtime-c-api/tests/.gitignore b/lib/runtime-c-api/tests/.gitignore index b64fa610c7e..cbc7f3b1fcf 100644 --- a/lib/runtime-c-api/tests/.gitignore +++ b/lib/runtime-c-api/tests/.gitignore @@ -10,20 +10,20 @@ compile_commands.json CTestTestfile.cmake _deps rust-build +test-context test-exported-memory test-exports test-globals test-import-function +test-import-object test-imports test-instantiate test-memory test-module test-module-exports +test-module-import-instantiate test-module-imports test-module-serialize test-tables test-validate -test-context -test-module-import-instantiate -test-wasi-import-object - +test-wasi-import-object \ No newline at end of file From 912713f88fc51122627a65f05cfbdb0566f9e218 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 16:09:14 +0100 Subject: [PATCH 06/15] test(runtime-c-api) Test the new WASI version API. This patch updates `test-wasi-import-object` to test the new `wasmer_wasi_get_version` & `wasmer_wasi_generate_import_object_for_version` functions, and the new `Version` type. --- .../tests/test-wasi-import-object.c | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/runtime-c-api/tests/test-wasi-import-object.c b/lib/runtime-c-api/tests/test-wasi-import-object.c index bd1478dc137..da94849963e 100644 --- a/lib/runtime-c-api/tests/test-wasi-import-object.c +++ b/lib/runtime-c-api/tests/test-wasi-import-object.c @@ -28,9 +28,9 @@ void print_wasmer_error() // helper function to print byte array to stdout void print_byte_array(wasmer_byte_array *arr) { - for (int i = 0; i < arr->bytes_len; ++i) { - putchar(arr->bytes[i]); - } + for (int i = 0; i < arr->bytes_len; ++i) { + putchar(arr->bytes[i]); + } } int main() @@ -164,20 +164,7 @@ int main() }; int mapped_dir_len = sizeof(mapped_dirs) / sizeof(mapped_dirs[0]); - // Create the WASI import object - wasmer_import_object_t *import_object = - wasmer_wasi_generate_import_object(args, wasi_argc, - envs, wasi_env_len, - NULL, 0, - mapped_dirs, mapped_dir_len); - - // Create our imports - wasmer_import_t imports[] = {func_import, global_import, memory_import, table_import}; - int imports_len = sizeof(imports) / sizeof(imports[0]); - // Add our imports to the import object - wasmer_import_object_extend(import_object, imports, imports_len); - - // Read the wasm file bytes + // Read the Wasm file bytes. FILE *file = fopen("assets/extended_wasi.wasm", "r"); assert(file); fseek(file, 0, SEEK_END); @@ -191,16 +178,39 @@ int main() // Compile the WebAssembly module wasmer_result_t compile_result = wasmer_compile(&module, bytes, len); printf("Compile result: %d\n", compile_result); + if (compile_result != WASMER_OK) { print_wasmer_error(); } + assert(compile_result == WASMER_OK); + // Detect the WASI version if any. This step is not mandatory, we + // use it to test the WASI version API. + Version wasi_version = wasmer_wasi_get_version(module); + + printf("WASI version: %d\n", wasi_version); + + // Create the WASI import object + wasmer_import_object_t *import_object = + wasmer_wasi_generate_import_object_for_version(wasi_version, + args, wasi_argc, + envs, wasi_env_len, + NULL, 0, + mapped_dirs, mapped_dir_len); + + // Create our imports + wasmer_import_t imports[] = {func_import, global_import, memory_import, table_import}; + int imports_len = sizeof(imports) / sizeof(imports[0]); + // Add our imports to the import object + wasmer_import_object_extend(import_object, imports, imports_len); + // Instantiatoe the module with our import_object wasmer_instance_t *instance = NULL; wasmer_result_t instantiate_result = wasmer_module_import_instantiate(&instance, module, import_object); printf("Instantiate result: %d\n", instantiate_result); + if (instantiate_result != WASMER_OK) { print_wasmer_error(); From b9851f26d45de4a0e9fd9c8af43716257c76a9f5 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 2 Dec 2019 16:19:19 +0100 Subject: [PATCH 07/15] doc(changelog) Add #1030. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 266dd165d34..b339615c989 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - [#1028](https://github.com/wasmerio/wasmer/pull/1028) Introduce strict/non-strict modes for `get_wasi_version` - [#1029](https://github.com/wasmerio/wasmer/pull/1029) Add the “floating” `WasiVersion::Latest` version. +- [#1030](https://github.com/wasmerio/wasmer/pull/1030) Ability to generate `ImportObject` for a specific version WASI version with the C API. +- [#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. - [#1022](https://github.com/wasmerio/wasmer/pull/1022) Add caching support for Singlepass backend. From 4fe8286b8197ebc4174553343aebae1930241157 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Dec 2019 14:33:10 +0100 Subject: [PATCH 08/15] feat(runtime-c-api) Avoid undefined behavior with user-given version. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the version has type `Version`, we expect the user to give a valid `Version` variant. Since the `Version` is basically a `uint8_t`, the user is able to pass everything she wants, which can create an undefined behavior on the Rust side. To avoid such situation, the version has now type `c_uchar` (`unsigned char` or `uint8_t` on C side —on most platforms). Then the `From` trait is implemented on `Version`. In case the value is unbound, `Version::Unknown` is returned. --- lib/runtime-c-api/src/import/mod.rs | 2 +- lib/runtime-c-api/src/import/wasi.rs | 14 ++++++++++++-- lib/runtime-c-api/wasmer.h | 2 +- lib/runtime-c-api/wasmer.hh | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/runtime-c-api/src/import/mod.rs b/lib/runtime-c-api/src/import/mod.rs index 5b44eb2b557..2bca1e3902c 100644 --- a/lib/runtime-c-api/src/import/mod.rs +++ b/lib/runtime-c-api/src/import/mod.rs @@ -8,7 +8,7 @@ use crate::{ value::wasmer_value_tag, wasmer_byte_array, wasmer_result_t, }; -use libc::c_uint; +use libc::{c_uchar, c_uint}; use std::{convert::TryFrom, ffi::c_void, ptr, slice, sync::Arc}; use wasmer_runtime::{Global, Memory, Module, Table}; use wasmer_runtime_core::{ diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index ec362fdf2ec..2f105b1a1d3 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -14,6 +14,16 @@ pub enum Version { Snapshot1, } +impl From for Version { + fn from(value: c_uchar) -> Self { + match value { + 0 => Self::Snapshot0, + 1 => Self::Snapshot1, + _ => Self::Unknown, + } + } +} + /// Opens a directory that's visible to the WASI module as `alias` but /// is backed by the host file at `host_file_path` #[repr(C)] @@ -71,7 +81,7 @@ pub unsafe extern "C" fn wasmer_wasi_generate_import_object( /// except that the first argument describes the WASI version. #[no_mangle] pub unsafe extern "C" fn wasmer_wasi_generate_import_object_for_version( - version: Version, + version: c_uchar, args: *const wasmer_byte_array, args_len: c_uint, envs: *const wasmer_byte_array, @@ -87,7 +97,7 @@ pub unsafe extern "C" fn wasmer_wasi_generate_import_object_for_version( let mapped_dir_list = get_slice_checked(mapped_dirs, mapped_dirs_len as usize); wasmer_wasi_generate_import_object_inner( - version, + version.into(), arg_list, env_list, preopened_file_list, diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 672a1704cd5..09d716e8ab5 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -912,7 +912,7 @@ wasmer_import_object_t *wasmer_wasi_generate_import_object(const wasmer_byte_arr * This function is similar to `wasmer_wasi_generate_import_object` * except that the first argument describes the WASI version. */ -wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(Version version, +wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(unsigned char version, const wasmer_byte_array *args, unsigned int args_len, const wasmer_byte_array *envs, diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index d293c817ebc..aac8daec312 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -715,7 +715,7 @@ wasmer_import_object_t *wasmer_wasi_generate_import_object(const wasmer_byte_arr /// /// This function is similar to `wasmer_wasi_generate_import_object` /// except that the first argument describes the WASI version. -wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(Version version, +wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(unsigned char version, const wasmer_byte_array *args, unsigned int args_len, const wasmer_byte_array *envs, From 0c5021484f680db913689bf6a720040fe319c32f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Dec 2019 14:37:13 +0100 Subject: [PATCH 09/15] fix(runtime-c-api) Use `get_wasi_version` in non-strict mode. --- lib/runtime-c-api/src/import/wasi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index 2f105b1a1d3..9b63a96a1f6 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -117,7 +117,7 @@ pub unsafe extern "C" fn wasmer_wasi_get_version(module: *const wasmer_module_t) let module = &*(module as *const Module); - match wasi::get_wasi_version(module) { + match wasi::get_wasi_version(module, false) { Some(version) => match version { wasi::WasiVersion::Snapshot0 => Version::Snapshot0, wasi::WasiVersion::Snapshot1 => Version::Snapshot1, From 90f3c894c110cbe27c69494ac8c64347ae19fa14 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Dec 2019 14:38:54 +0100 Subject: [PATCH 10/15] doc(runtime-c-api) Improve documentation of `wasmer_wasi_generate_import_object_for_version`. --- lib/runtime-c-api/src/import/wasi.rs | 4 +++- lib/runtime-c-api/wasmer.h | 4 +++- lib/runtime-c-api/wasmer.hh | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index 9b63a96a1f6..46caab24880 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -75,10 +75,12 @@ pub unsafe extern "C" fn wasmer_wasi_generate_import_object( .unwrap_or(ptr::null_mut()) } -/// Creates a WASI import object. +/// Creates a WASI import object for a specific version. /// /// This function is similar to `wasmer_wasi_generate_import_object` /// except that the first argument describes the WASI version. +/// +/// The version is expected to be of kind `Version`. #[no_mangle] pub unsafe extern "C" fn wasmer_wasi_generate_import_object_for_version( version: c_uchar, diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 09d716e8ab5..282a6ee05d4 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -907,10 +907,12 @@ wasmer_import_object_t *wasmer_wasi_generate_import_object(const wasmer_byte_arr unsigned int mapped_dirs_len); /** - * Creates a WASI import object. + * Creates a WASI import object for a specific version. * * This function is similar to `wasmer_wasi_generate_import_object` * except that the first argument describes the WASI version. + * + * The version is expected to be of kind `Version`. */ wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(unsigned char version, const wasmer_byte_array *args, diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index aac8daec312..a78780118d0 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -711,10 +711,12 @@ wasmer_import_object_t *wasmer_wasi_generate_import_object(const wasmer_byte_arr const wasmer_wasi_map_dir_entry_t *mapped_dirs, unsigned int mapped_dirs_len); -/// Creates a WASI import object. +/// Creates a WASI import object for a specific version. /// /// This function is similar to `wasmer_wasi_generate_import_object` /// except that the first argument describes the WASI version. +/// +/// The version is expected to be of kind `Version`. wasmer_import_object_t *wasmer_wasi_generate_import_object_for_version(unsigned char version, const wasmer_byte_array *args, unsigned int args_len, From 345511a4f9800e63611ec8ffbac3fa08c73884b2 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Dec 2019 14:48:12 +0100 Subject: [PATCH 11/15] fix(runtime-c-api) Fix `From for Version`. 0 matches to `Unknown`, 1 matches to `Snapshot0` and 2 matches to `Snapshot1`. --- lib/runtime-c-api/src/import/wasi.rs | 24 ++++++++++++++++++------ lib/runtime-c-api/wasmer.h | 6 +++--- lib/runtime-c-api/wasmer.hh | 6 +++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index 46caab24880..2766cb83cba 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -3,22 +3,22 @@ use crate::get_slice_checked; use std::{path::PathBuf, ptr, str}; use wasmer_wasi as wasi; -#[derive(Debug)] +#[derive(Debug, PartialEq)] #[repr(u8)] pub enum Version { /// Version cannot be detected or is unknown. - Unknown, + Unknown = 0, /// `wasi_unstable`. - Snapshot0, + Snapshot0 = 1, /// `wasi_snapshot_preview1`. - Snapshot1, + Snapshot1 = 2, } impl From for Version { fn from(value: c_uchar) -> Self { match value { - 0 => Self::Snapshot0, - 1 => Self::Snapshot1, + 1 => Self::Snapshot0, + 2 => Self::Snapshot1, _ => Self::Unknown, } } @@ -178,3 +178,15 @@ pub unsafe extern "C" fn wasmer_wasi_generate_default_import_object() -> *mut wa Box::into_raw(import_object) as *mut wasmer_import_object_t } + +#[cfg(test)] +mod tests { + use super::Version; + + #[test] + fn test_versions_from_uint() { + assert_eq!(Version::Unknown, 0.into()); + assert_eq!(Version::Snapshot0, 1.into()); + assert_eq!(Version::Snapshot1, 2.into()); + } +} diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 282a6ee05d4..1440b16dcd9 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -29,15 +29,15 @@ enum Version { /** * Version cannot be detected or is unknown. */ - Unknown, + Unknown = 0, /** * `wasi_unstable`. */ - Snapshot0, + Snapshot0 = 1, /** * `wasi_snapshot_preview1`. */ - Snapshot1, + Snapshot1 = 2, }; typedef uint8_t Version; diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index a78780118d0..5366ee88d41 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -27,11 +27,11 @@ enum class Version : uint8_t { /// Version cannot be detected or is unknown. - Unknown, + Unknown = 0, /// `wasi_unstable`. - Snapshot0, + Snapshot0 = 1, /// `wasi_snapshot_preview1`. - Snapshot1, + Snapshot1 = 2, }; /// List of export/import kinds. From 4fc6adf9c28bd387fe7f394a0cae3b1d0d303106 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Dec 2019 14:51:29 +0100 Subject: [PATCH 12/15] chore(runtime-c-api) Remove a useless `format!`. --- lib/runtime-c-api/src/import/wasi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index 2766cb83cba..6d7e6a52352 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -153,7 +153,7 @@ fn wasmer_wasi_generate_import_object_inner( let version = match version { Version::Snapshot0 => wasi::WasiVersion::Snapshot0, Version::Snapshot1 => wasi::WasiVersion::Snapshot1, - _ => panic!(format!("Version {:?} is invalid.", version)), + _ => panic!("Version {:?} is invalid.", version), }; let import_object = Box::new(wasi::generate_import_object_for_version( From f0f0657264bfce83d7ffc17ab7884a3ba1c511aa Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Dec 2019 14:52:58 +0100 Subject: [PATCH 13/15] doc(changelog) Resolve merge issues. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b339615c989..fc2ad7754ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - [#1028](https://github.com/wasmerio/wasmer/pull/1028) Introduce strict/non-strict modes for `get_wasi_version` - [#1029](https://github.com/wasmerio/wasmer/pull/1029) Add the “floating” `WasiVersion::Latest` version. - [#1030](https://github.com/wasmerio/wasmer/pull/1030) Ability to generate `ImportObject` for a specific version WASI version with the C API. -- [#1028](https://github.com/wasmerio/wasmer/pull/1028) Fix `get_wasi_version` when a module has multiple import namespaces. +- [#1028](https://github.com/wasmerio/wasmer/pull/1028) Introduce strict/non-strict modes for `get_wasi_version` - [#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. - [#1022](https://github.com/wasmerio/wasmer/pull/1022) Add caching support for Singlepass backend. From 4ef799f23d6f86e2533cd254374cb39320faddeb Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 4 Dec 2019 15:34:56 +0100 Subject: [PATCH 14/15] feat(runtime-c-api) Support `WasiVersion::Latest`. --- CHANGELOG.md | 2 +- lib/runtime-c-api/src/import/wasi.rs | 24 +++++++++++++++++------- lib/runtime-c-api/wasmer.h | 9 +++++++-- lib/runtime-c-api/wasmer.hh | 7 +++++-- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc2ad7754ea..495361d2876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,9 @@ ## **[Unreleased]** +- [#1030](https://github.com/wasmerio/wasmer/pull/1030) Ability to generate `ImportObject` for a specific version WASI version with the C API. - [#1028](https://github.com/wasmerio/wasmer/pull/1028) Introduce strict/non-strict modes for `get_wasi_version` - [#1029](https://github.com/wasmerio/wasmer/pull/1029) Add the “floating” `WasiVersion::Latest` version. -- [#1030](https://github.com/wasmerio/wasmer/pull/1030) Ability to generate `ImportObject` for a specific version WASI version with the C API. - [#1028](https://github.com/wasmerio/wasmer/pull/1028) Introduce strict/non-strict modes for `get_wasi_version` - [#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. diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index 6d7e6a52352..0ff833a61f5 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -8,17 +8,24 @@ use wasmer_wasi as wasi; pub enum Version { /// Version cannot be detected or is unknown. Unknown = 0, + + /// Latest version. See `wasmer_wasi::WasiVersion::Latest` to + /// leran more. + Latest = 1, + /// `wasi_unstable`. - Snapshot0 = 1, + Snapshot0 = 2, + /// `wasi_snapshot_preview1`. - Snapshot1 = 2, + Snapshot1 = 3, } impl From for Version { fn from(value: c_uchar) -> Self { match value { - 1 => Self::Snapshot0, - 2 => Self::Snapshot1, + 1 => Self::Latest, + 2 => Self::Snapshot0, + 3 => Self::Snapshot1, _ => Self::Unknown, } } @@ -66,7 +73,7 @@ pub unsafe extern "C" fn wasmer_wasi_generate_import_object( let mapped_dir_list = get_slice_checked(mapped_dirs, mapped_dirs_len as usize); wasmer_wasi_generate_import_object_inner( - Version::Snapshot1, + Version::Latest, arg_list, env_list, preopened_file_list, @@ -123,6 +130,7 @@ pub unsafe extern "C" fn wasmer_wasi_get_version(module: *const wasmer_module_t) Some(version) => match version { wasi::WasiVersion::Snapshot0 => Version::Snapshot0, wasi::WasiVersion::Snapshot1 => Version::Snapshot1, + wasi::WasiVersion::Latest => Version::Latest, }, None => Version::Unknown, } @@ -151,6 +159,7 @@ fn wasmer_wasi_generate_import_object_inner( .collect::, _>>()?; let version = match version { + Version::Latest => wasi::WasiVersion::Latest, Version::Snapshot0 => wasi::WasiVersion::Snapshot0, Version::Snapshot1 => wasi::WasiVersion::Snapshot1, _ => panic!("Version {:?} is invalid.", version), @@ -186,7 +195,8 @@ mod tests { #[test] fn test_versions_from_uint() { assert_eq!(Version::Unknown, 0.into()); - assert_eq!(Version::Snapshot0, 1.into()); - assert_eq!(Version::Snapshot1, 2.into()); + assert_eq!(Version::Latest, 1.into()); + assert_eq!(Version::Snapshot0, 2.into()); + assert_eq!(Version::Snapshot1, 3.into()); } } diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 1440b16dcd9..de921e3b04d 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -30,14 +30,19 @@ enum Version { * Version cannot be detected or is unknown. */ Unknown = 0, + /** + * Latest version. See `wasmer_wasi::WasiVersion::Latest` to + * leran more. + */ + Latest = 1, /** * `wasi_unstable`. */ - Snapshot0 = 1, + Snapshot0 = 2, /** * `wasi_snapshot_preview1`. */ - Snapshot1 = 2, + Snapshot1 = 3, }; typedef uint8_t Version; diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index 5366ee88d41..09b58f86100 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -28,10 +28,13 @@ enum class Version : uint8_t { /// Version cannot be detected or is unknown. Unknown = 0, + /// Latest version. See `wasmer_wasi::WasiVersion::Latest` to + /// leran more. + Latest = 1, /// `wasi_unstable`. - Snapshot0 = 1, + Snapshot0 = 2, /// `wasi_snapshot_preview1`. - Snapshot1 = 2, + Snapshot1 = 3, }; /// List of export/import kinds. From 45e4081e4b71573d453614b20ecda1b200d82306 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 6 Dec 2019 15:02:48 +0100 Subject: [PATCH 15/15] doc(changelog) Remove a duplication. --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 495361d2876..60ff42f8b9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,6 @@ - [#1030](https://github.com/wasmerio/wasmer/pull/1030) Ability to generate `ImportObject` for a specific version WASI version with the C API. - [#1028](https://github.com/wasmerio/wasmer/pull/1028) Introduce strict/non-strict modes for `get_wasi_version` - [#1029](https://github.com/wasmerio/wasmer/pull/1029) Add the “floating” `WasiVersion::Latest` version. -- [#1028](https://github.com/wasmerio/wasmer/pull/1028) Introduce strict/non-strict modes for `get_wasi_version` - [#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. - [#1022](https://github.com/wasmerio/wasmer/pull/1022) Add caching support for Singlepass backend.