Skip to content

Commit 8d90b3a

Browse files
authored
Fix simd usage in Wasmi's C-API (#1676)
fix `simd` usage in Wasmi's C-API Wasmi implemented `simd` support superficially while the official C-API does not. This caused `wasm_val_t` and `wasm_valtype_t` types to be misaligned which caused issues.
1 parent 238a070 commit 8d90b3a

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

crates/c_api/src/types/val.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ pub enum wasm_valkind_t {
3434
///
3535
/// Wraps [`ValType::F64`].
3636
WASM_F64 = 3,
37-
/// A Wasm `v128` value type.
38-
///
39-
/// Wraps [`ValType::V128`].
40-
WASM_V128 = 4,
4137
/// A Wasm external reference type.
4238
///
4339
/// Wraps [`ValType::ExternRef`].
@@ -71,7 +67,6 @@ pub(crate) fn into_valtype(kind: wasm_valkind_t) -> ValType {
7167
wasm_valkind_t::WASM_I64 => ValType::I64,
7268
wasm_valkind_t::WASM_F32 => ValType::F32,
7369
wasm_valkind_t::WASM_F64 => ValType::F64,
74-
wasm_valkind_t::WASM_V128 => ValType::V128,
7570
wasm_valkind_t::WASM_EXTERNREF => ValType::ExternRef,
7671
wasm_valkind_t::WASM_FUNCREF => ValType::FuncRef,
7772
}
@@ -84,8 +79,8 @@ pub(crate) fn from_valtype(ty: &ValType) -> wasm_valkind_t {
8479
ValType::I64 => wasm_valkind_t::WASM_I64,
8580
ValType::F32 => wasm_valkind_t::WASM_F32,
8681
ValType::F64 => wasm_valkind_t::WASM_F64,
87-
ValType::V128 => wasm_valkind_t::WASM_V128,
8882
ValType::ExternRef => wasm_valkind_t::WASM_EXTERNREF,
8983
ValType::FuncRef => wasm_valkind_t::WASM_FUNCREF,
84+
unsupported => ::core::panic!("c-api: unsupported `valtype` found: {unsupported:?}"),
9085
}
9186
}

crates/c_api/src/val.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
};
1010
use alloc::boxed::Box;
1111
use core::{mem::MaybeUninit, ptr};
12-
use wasmi::{Func, Ref, Val, ValType, F32, F64, V128};
12+
use wasmi::{Func, Ref, Val, ValType, F32, F64};
1313

1414
/// A Wasm value.
1515
///
@@ -38,8 +38,6 @@ pub union wasm_val_union {
3838
pub f32: f32,
3939
/// A Wasm 64-bit float.
4040
pub f64: f64,
41-
/// A Wasm `v128` value.
42-
pub v128: u128,
4341
/// A Wasm referenced object.
4442
pub ref_: *mut wasm_ref_t,
4543
}
@@ -99,12 +97,9 @@ impl From<Val> for wasm_val_t {
9997
u64: value.to_bits(),
10098
},
10199
},
102-
Val::V128(value) => Self {
103-
kind: from_valtype(&ValType::V128),
104-
of: wasm_val_union {
105-
v128: value.as_u128(),
106-
},
107-
},
100+
Val::V128(value) => core::panic!(
101+
"`wasm_val_t`: creating a `wasm_val_t` from an `v128` value is not supported but found: {value:?}"
102+
),
108103
Val::FuncRef(funcref) => Self {
109104
kind: from_valtype(&ValType::FuncRef),
110105
of: wasm_val_union {
@@ -132,17 +127,16 @@ impl wasm_val_t {
132127
///
133128
/// This effectively clones the [`wasm_val_t`] if necessary.
134129
pub fn to_val(&self) -> Val {
135-
match into_valtype(self.kind) {
136-
ValType::I32 => Val::from(unsafe { self.of.i32 }),
137-
ValType::I64 => Val::from(unsafe { self.of.i64 }),
138-
ValType::F32 => Val::from(F32::from(unsafe { self.of.f32 })),
139-
ValType::F64 => Val::from(F64::from(unsafe { self.of.f64 })),
140-
ValType::V128 => Val::from(V128::from(unsafe { self.of.v128 })),
141-
ValType::FuncRef => match unsafe { self.of.ref_ }.is_null() {
130+
match self.kind {
131+
wasm_valkind_t::WASM_I32 => Val::from(unsafe { self.of.i32 }),
132+
wasm_valkind_t::WASM_I64 => Val::from(unsafe { self.of.i64 }),
133+
wasm_valkind_t::WASM_F32 => Val::from(F32::from(unsafe { self.of.f32 })),
134+
wasm_valkind_t::WASM_F64 => Val::from(F64::from(unsafe { self.of.f64 })),
135+
wasm_valkind_t::WASM_FUNCREF => match unsafe { self.of.ref_ }.is_null() {
142136
true => Val::FuncRef(<Ref<Func>>::Null),
143137
false => ref_to_val(unsafe { &*self.of.ref_ }),
144138
},
145-
ValType::ExternRef => {
139+
wasm_valkind_t::WASM_EXTERNREF => {
146140
core::unreachable!("`wasm_val_t`: cannot contain non-function reference values")
147141
}
148142
}

0 commit comments

Comments
 (0)