Skip to content

Commit ec00493

Browse files
jimpoVeykril
authored andcommitted
Remove unnecessary memory allocation when validating function signature
1 parent bd4ead7 commit ec00493

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

Diff for: src/function.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use alloc::vec::Vec;
21
use core::cmp::{Eq, PartialEq};
32
use core::hash::{Hash, Hasher};
43
use core::marker::PhantomData;
@@ -29,9 +28,9 @@ impl<'cc> CallContext<'cc> {
2928
/// # Safety
3029
///
3130
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
32-
pub unsafe fn memory(&self) -> *const [u8] {
31+
pub fn memory(&self) -> *const [u8] {
3332
let mut memory_size = 0u32;
34-
let data = ffi::m3_GetMemory(self.runtime.as_ptr(), &mut memory_size as *mut u32, 0);
33+
let data = unsafe { ffi::m3_GetMemory(self.runtime.as_ptr(), &mut memory_size, 0) };
3534
ptr::slice_from_raw_parts(data, memory_size as usize)
3635
}
3736

@@ -40,9 +39,9 @@ impl<'cc> CallContext<'cc> {
4039
/// # Safety
4140
///
4241
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
43-
pub unsafe fn memory_mut(&self) -> *mut [u8] {
42+
pub fn memory_mut(&self) -> *mut [u8] {
4443
let mut memory_size = 0u32;
45-
let data = ffi::m3_GetMemory(self.runtime.as_ptr(), &mut memory_size as *mut u32, 0);
44+
let data = unsafe { ffi::m3_GetMemory(self.runtime.as_ptr(), &mut memory_size, 0) };
4645
ptr::slice_from_raw_parts_mut(data, memory_size as usize)
4746
}
4847
}
@@ -109,16 +108,21 @@ where
109108
{
110109
fn validate_sig(func: NNM3Function) -> bool {
111110
let num_args = unsafe { ffi::m3_GetArgCount(func.as_ptr()) };
112-
let num_rets = unsafe { ffi::m3_GetRetCount(func.as_ptr()) };
113111
let args = (0..num_args)
114-
.map(|i| unsafe { ffi::m3_GetArgType(func.as_ptr(), i) })
115-
.collect::<Vec<_>>();
116-
let rets = (0..num_rets)
117-
.map(|i| unsafe { ffi::m3_GetRetType(func.as_ptr(), i) })
118-
.collect::<Vec<_>>();
119-
return Args::validate_types(&args) &&
120-
((rets.len() == 0 && Ret::TYPE_INDEX == ffi::M3ValueType::c_m3Type_none) ||
121-
(rets.len() == 1 && Ret::TYPE_INDEX == rets[0]));
112+
.map(|i| unsafe { ffi::m3_GetArgType(func.as_ptr(), i) });
113+
if !Args::validate_types(args) {
114+
return false;
115+
}
116+
117+
let num_rets = unsafe { ffi::m3_GetRetCount(func.as_ptr()) };
118+
match num_rets {
119+
0 => Ret::TYPE_INDEX == ffi::M3ValueType::c_m3Type_none,
120+
1 => {
121+
let ret = unsafe { ffi::m3_GetRetType(func.as_ptr(), 0) };
122+
Ret::TYPE_INDEX == ret
123+
}
124+
_ => false,
125+
}
122126
}
123127

124128
#[inline]

Diff for: src/runtime.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ impl Runtime {
9898
/// # Safety
9999
///
100100
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
101-
pub unsafe fn memory(&self) -> *const [u8] {
101+
pub fn memory(&self) -> *const [u8] {
102102
let mut len: u32 = 0;
103-
let data = ffi::m3_GetMemory(self.as_ptr(), &mut len as *mut u32, 0);
103+
let data = unsafe { ffi::m3_GetMemory(self.as_ptr(), &mut len, 0) };
104104
ptr::slice_from_raw_parts(data, len as usize)
105105
}
106106

@@ -109,9 +109,9 @@ impl Runtime {
109109
/// # Safety
110110
///
111111
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
112-
pub unsafe fn memory_mut(&self) -> *mut [u8] {
112+
pub fn memory_mut(&self) -> *mut [u8] {
113113
let mut len: u32 = 0;
114-
let data = ffi::m3_GetMemory(self.as_ptr(), &mut len as *mut u32, 0);
114+
let data = unsafe { ffi::m3_GetMemory(self.as_ptr(), &mut len, 0) };
115115
ptr::slice_from_raw_parts_mut(data, len as usize)
116116
}
117117
}

Diff for: src/ty.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub trait WasmArgs {
3434
// required for closure linking
3535
unsafe fn pop_from_stack(stack: *mut u64) -> Self;
3636
#[doc(hidden)]
37-
fn validate_types(types: &[ffi::M3ValueType::Type]) -> bool;
37+
fn validate_types(types: impl Iterator<Item=ffi::M3ValueType::Type>) -> bool;
3838
#[doc(hidden)]
3939
fn sealed_() -> private::Seal;
4040
#[doc(hidden)]
@@ -198,8 +198,8 @@ impl WasmArgs for () {
198198
#[doc(hidden)]
199199
unsafe fn pop_from_stack(_: *mut u64) -> Self {}
200200
#[doc(hidden)]
201-
fn validate_types(types: &[ffi::M3ValueType::Type]) -> bool {
202-
types.is_empty()
201+
fn validate_types(mut types: impl Iterator<Item=ffi::M3ValueType::Type>) -> bool {
202+
types.next().is_none()
203203
}
204204
#[doc(hidden)]
205205
fn sealed_() -> private::Seal {
@@ -227,8 +227,9 @@ where
227227
WasmType::pop_from_stack(stack)
228228
}
229229
#[doc(hidden)]
230-
fn validate_types(types: &[ffi::M3ValueType::Type]) -> bool {
231-
types.len() == 1 && types[0] == T::TYPE_INDEX
230+
fn validate_types(mut types: impl Iterator<Item=ffi::M3ValueType::Type>) -> bool {
231+
types.next().map(|ty| ty == T::TYPE_INDEX).unwrap_or(false) &&
232+
types.next().is_none()
232233
}
233234
#[doc(hidden)]
234235
fn sealed_() -> private::Seal {
@@ -278,12 +279,11 @@ macro_rules! args_impl {
278279
)*)
279280
}
280281
#[doc(hidden)]
281-
fn validate_types(types: &[ffi::M3ValueType::Type]) -> bool {
282-
let mut ty_iter = types.iter();
282+
fn validate_types(mut types: impl Iterator<Item=ffi::M3ValueType::Type>) -> bool {
283283
$(
284-
ty_iter.next().map(|&ty| ty == $types::TYPE_INDEX).unwrap_or(false) &&
284+
types.next().map(|ty| ty == $types::TYPE_INDEX).unwrap_or(false) &&
285285
)*
286-
ty_iter.next().is_none()
286+
types.next().is_none()
287287
}
288288
#[doc(hidden)]
289289
fn sealed_() -> private::Seal { private::Seal }
@@ -309,53 +309,53 @@ mod tests {
309309
use super::*;
310310
#[test]
311311
fn test_validate_types_single() {
312-
assert!(f64::validate_types(&[
312+
assert!(f64::validate_types([
313313
ffi::M3ValueType::c_m3Type_f64,
314-
]));
314+
].iter().cloned()));
315315
}
316316

317317
#[test]
318318
fn test_validate_types_single_fail() {
319-
assert!(!f32::validate_types(&[
319+
assert!(!f32::validate_types([
320320
ffi::M3ValueType::c_m3Type_f64,
321-
]));
321+
].iter().cloned()));
322322
}
323323

324324
#[test]
325325
fn test_validate_types_double() {
326-
assert!(<(f64, u32)>::validate_types(&[
326+
assert!(<(f64, u32)>::validate_types([
327327
ffi::M3ValueType::c_m3Type_f64,
328328
ffi::M3ValueType::c_m3Type_i32,
329-
]));
329+
].iter().cloned()));
330330
}
331331

332332
#[test]
333333
fn test_validate_types_double_fail() {
334-
assert!(!<(f32, u64)>::validate_types(&[
334+
assert!(!<(f32, u64)>::validate_types([
335335
ffi::M3ValueType::c_m3Type_i64,
336336
ffi::M3ValueType::c_m3Type_f32,
337-
]));
337+
].iter().cloned()));
338338
}
339339

340340
#[test]
341341
fn test_validate_types_quintuple() {
342-
assert!(<(f64, u32, i32, i64, f32)>::validate_types(&[
342+
assert!(<(f64, u32, i32, i64, f32)>::validate_types([
343343
ffi::M3ValueType::c_m3Type_f64,
344344
ffi::M3ValueType::c_m3Type_i32,
345345
ffi::M3ValueType::c_m3Type_i32,
346346
ffi::M3ValueType::c_m3Type_i64,
347347
ffi::M3ValueType::c_m3Type_f32,
348-
]));
348+
].iter().cloned()));
349349
}
350350

351351
#[test]
352352
fn test_validate_types_quintuple_fail() {
353-
assert!(!<(f64, u32, i32, i64, f32)>::validate_types(&[
353+
assert!(!<(f64, u32, i32, i64, f32)>::validate_types([
354354
ffi::M3ValueType::c_m3Type_i32,
355355
ffi::M3ValueType::c_m3Type_i64,
356356
ffi::M3ValueType::c_m3Type_i32,
357357
ffi::M3ValueType::c_m3Type_f32,
358358
ffi::M3ValueType::c_m3Type_f64,
359-
]));
359+
].iter().cloned()));
360360
}
361361
}

0 commit comments

Comments
 (0)