feat(runtime-c-api) Avoid undefined behavior with user-given version.

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.
This commit is contained in:
Ivan Enderlin
2019-12-04 14:33:10 +01:00
parent b9851f26d4
commit 4fe8286b81
4 changed files with 15 additions and 5 deletions

View File

@ -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::{

View File

@ -14,6 +14,16 @@ pub enum Version {
Snapshot1,
}
impl From<c_uchar> 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,