From 86f466cdb02ea1a81c0d6bca96dcb1fc054e818d Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 8 May 2022 13:32:19 +0200 Subject: [PATCH] Simplified signature checking in wasm jumploader --- ableos/src/wasm_jumploader/host_functions.rs | 118 +++---------------- 1 file changed, 17 insertions(+), 101 deletions(-) diff --git a/ableos/src/wasm_jumploader/host_functions.rs b/ableos/src/wasm_jumploader/host_functions.rs index 04c50550..a4f4d44e 100644 --- a/ableos/src/wasm_jumploader/host_functions.rs +++ b/ableos/src/wasm_jumploader/host_functions.rs @@ -14,108 +14,24 @@ const SEND_SIGNAL_INDEX: usize = 1; pub struct HostExternals; impl HostExternals { fn check_signature(&self, index: usize, signature: &Signature) -> bool { - match index { - ADD_FUNC_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } + let (params, ret_ty): (&[_], _) = match index { + ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), + SEND_SIGNAL_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), + GET_TIME_INDEX => (&[], Some(ValueType::I32)), + GET_RANDOM_INDEX => (&[], Some(ValueType::I32)), + GET_INPUT_INDEX => (&[], Some(ValueType::I32)), + PRINT_CHAR => (&[ValueType::I32], None), + _ => return false, + }; - SEND_SIGNAL_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - GET_TIME_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - GET_RANDOM_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - GET_INPUT_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - PRINT_CHAR => { - let (params, ret_ty): (&[ValueType], Option) = (&[ValueType::I32], None); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - _ => false, + if params.len() != signature.params().len() || ret_ty != signature.return_type() { + false + } else { + params + .iter() + .zip(signature.params()) + .find(|(ty, param)| ty != param) + .is_none() } } }