Skip to content

Commit

Permalink
Merge #2058
Browse files Browse the repository at this point in the history
2058: fix(c-api) `wasi_version_t` wasn't bound correctly to C r=Hywan a=Hywan

# Description

Because the enum variants weren't in uppercase _and_ because one variant (`InvalidVersion`) has a non-literal value, `cbindgen` was ignoring the variants. This patch fixes that. It's not a breaking changes since the WASI version constants weren't present in the `wasmer_wasm.h` file before.

Note: `u32::max_value()` is soft-deprecated, we should use `u32::MAX` instead. Notice that none of them works with `cbindgen`, so I've hardcoded the value of `u32::MAX` here.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
bors[bot] and Hywan authored Jan 27, 2021
2 parents cfc8f04 + 276537a commit 945fa81
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [#2056](https://github.com/wasmerio/wasmer/pull/2056) Change back to depend on the `enumset` crate instead of `wasmer_enumset`

### Fixed
- [#2058](https://github.com/wasmerio/wasmer/pull/2058) Expose WASI versions to C correctly.
- [#2044](https://github.com/wasmerio/wasmer/pull/2044) Do not build C headers on docs.rs.

## 1.0.1 - 2021-01-12
Expand Down
42 changes: 30 additions & 12 deletions lib/c-api/src/wasm_c_api/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,40 @@ fn read_inner(wasi_file: &mut Box<dyn WasiFile>, inner_buffer: &mut [u8]) -> isi
}
}

/// The version of WASI. This is determined by the imports namespace
/// string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
#[allow(non_camel_case_types)]
pub enum wasi_version_t {
Latest = 0,
Snapshot0 = 1,
Snapshot1 = 2,
InvalidVersion = u32::max_value(),
/// Latest version.
///
/// It's a “floating” version, i.e. it's an alias to the latest
/// version (for the moment, `Snapshot1`). Using this version is a
/// way to ensure that modules will run only if they come with the
/// latest WASI version (in case of security issues for instance),
/// by just updating the runtime.
///
/// Note that this version is never returned by an API. It is
/// provided only by the user.
LATEST = 0,

/// `wasi_unstable`.
SNAPSHOT0 = 1,

/// `wasi_snapshot_preview1`.
SNAPSHOT1 = 2,

/// An invalid version. It matches `u32` maximum value.
INVALID_VERSION = 4294967295, // u32::MAX,
}

impl From<WasiVersion> for wasi_version_t {
fn from(other: WasiVersion) -> Self {
match other {
WasiVersion::Snapshot0 => wasi_version_t::Snapshot0,
WasiVersion::Snapshot1 => wasi_version_t::Snapshot1,
WasiVersion::Latest => wasi_version_t::Latest,
WasiVersion::Snapshot0 => wasi_version_t::SNAPSHOT0,
WasiVersion::Snapshot1 => wasi_version_t::SNAPSHOT1,
WasiVersion::Latest => wasi_version_t::LATEST,
}
}
}
Expand All @@ -311,10 +329,10 @@ impl TryFrom<wasi_version_t> for WasiVersion {

fn try_from(other: wasi_version_t) -> Result<Self, Self::Error> {
Ok(match other {
wasi_version_t::Snapshot0 => WasiVersion::Snapshot0,
wasi_version_t::Snapshot1 => WasiVersion::Snapshot1,
wasi_version_t::Latest => WasiVersion::Latest,
wasi_version_t::InvalidVersion => return Err("Invalid WASI version cannot be used"),
wasi_version_t::SNAPSHOT0 => WasiVersion::Snapshot0,
wasi_version_t::SNAPSHOT1 => WasiVersion::Snapshot1,
wasi_version_t::LATEST => WasiVersion::Latest,
wasi_version_t::INVALID_VERSION => return Err("Invalid WASI version cannot be used"),
})
}
}
Expand All @@ -323,7 +341,7 @@ impl TryFrom<wasi_version_t> for WasiVersion {
pub unsafe extern "C" fn wasi_get_wasi_version(module: &wasm_module_t) -> wasi_version_t {
get_wasi_version(&module.inner, false)
.map(Into::into)
.unwrap_or(wasi_version_t::InvalidVersion)
.unwrap_or(wasi_version_t::INVALID_VERSION)
}

/// Takes ownership of `wasi_env_t`.
Expand Down
47 changes: 43 additions & 4 deletions lib/c-api/wasmer_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,49 @@
#include <stdlib.h>
#include "wasm.h"

#if defined(WASMER_WASI_ENABLED)
/**
* The version of WASI. This is determined by the imports namespace
* string.
*/
enum wasi_version_t {
#if defined(WASMER_WASI_ENABLED)
/**
* Latest version.
*
* It's a “floating” version, i.e. it's an alias to the latest
* version (for the moment, `Snapshot1`). Using this version is a
* way to ensure that modules will run only if they come with the
* latest WASI version (in case of security issues for instance),
* by just updating the runtime.
*
* Note that this version is never returned by an API. It is
* provided only by the user.
*/
LATEST = 0,
#endif
#if defined(WASMER_WASI_ENABLED)
/**
* `wasi_unstable`.
*/
SNAPSHOT0 = 1,
#endif
#if defined(WASMER_WASI_ENABLED)
/**
* `wasi_snapshot_preview1`.
*/
SNAPSHOT1 = 2,
#endif
#if defined(WASMER_WASI_ENABLED)
/**
* An invalid version. It matches `u32` maximum value.
*/
INVALID_VERSION = 4294967295,
#endif
};
typedef uint32_t wasi_version_t;
#endif

#if defined(WASMER_COMPILER_ENABLED)
/**
* Kind of compilers that can be used by the engines.
Expand Down Expand Up @@ -120,10 +163,6 @@ typedef struct wasi_config_t wasi_config_t;
typedef struct wasi_env_t wasi_env_t;
#endif

#if defined(WASMER_WASI_ENABLED)
typedef struct wasi_version_t wasi_version_t;
#endif

#if defined(WASMER_WASI_ENABLED)
void wasi_config_arg(wasi_config_t *config, const char *arg);
#endif
Expand Down
1 change: 1 addition & 0 deletions lib/wasi/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn is_wasi_module(module: &Module) -> bool {
pub enum WasiVersion {
/// `wasi_unstable`.
Snapshot0,

/// `wasi_snapshot_preview1`.
Snapshot1,

Expand Down

0 comments on commit 945fa81

Please sign in to comment.