Skip to content

Commit

Permalink
chore: Examine the arity of the called function
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Aug 13, 2024
1 parent b8f7c9e commit 6fef09a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/vm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,18 @@ impl<A: BackendApi, S: Storage, Q: Querier> Environment<A, S, Q> {
&self,
store: &mut impl AsStoreMut,
name: &str,
args: &[Value],
args: &Vec<Value>,
) -> VmResult<Box<[Value]>> {
// Clone function before calling it to avoid dead locks
let func = self.with_wasmer_instance(|instance| {
let func = instance.exports.get_function(name)?;
Ok(func.clone())
})?;
let function_arity = func.param_arity(store);
if args.len() != function_arity {
// TODO tkulik: when return error, try to call it with different args vector
return Err(VmError::func_arity_mismatch(function_arity));
};
self.increment_call_depth()?;
let res = func.call(store, args).map_err(|runtime_err| -> VmError {
self.with_wasmer_instance::<_, Never>(|instance| {
Expand All @@ -293,7 +298,7 @@ impl<A: BackendApi, S: Storage, Q: Querier> Environment<A, S, Q> {
name: &str,
args: &[Value],
) -> VmResult<()> {
let result = self.call_function(store, name, args)?;
let result = self.call_function(store, name, &args.to_vec())?;
let expected = 0;
let actual = result.len();
if actual != expected {
Expand All @@ -308,7 +313,7 @@ impl<A: BackendApi, S: Storage, Q: Querier> Environment<A, S, Q> {
name: &str,
args: &[Value],
) -> VmResult<Value> {
let result = self.call_function(store, name, args)?;
let result = self.call_function(store, name, &args.to_vec())?;
let expected = 1;
let actual = result.len();
if actual != expected {
Expand Down Expand Up @@ -792,7 +797,7 @@ mod tests {
leave_default_data(&env);

let result = env
.call_function(&mut store, "allocate", &[10u32.into()])
.call_function(&mut store, "allocate", &vec![10u32.into()])
.unwrap();
let ptr = ref_to_u32(&result[0]).unwrap();
assert!(ptr > 0);
Expand All @@ -806,7 +811,7 @@ mod tests {
// Clear context's wasmer_instance
env.set_wasmer_instance(None);

let res = env.call_function(&mut store, "allocate", &[]);
let res = env.call_function(&mut store, "allocate", &vec![]);
match res.unwrap_err() {
VmError::UninitializedContextData { kind, .. } => assert_eq!(kind, "wasmer_instance"),
err => panic!("Unexpected error: {err:?}"),
Expand All @@ -818,7 +823,7 @@ mod tests {
let (env, mut store, _instance) = make_instance(TESTING_GAS_LIMIT);
leave_default_data(&env);

let res = env.call_function(&mut store, "doesnt_exist", &[]);
let res = env.call_function(&mut store, "doesnt_exist", &vec![]);
match res.unwrap_err() {
VmError::ResolveErr { msg, .. } => {
assert_eq!(msg, "Could not get export: Missing export doesnt_exist");
Expand Down
6 changes: 6 additions & 0 deletions packages/vm/src/errors/vm_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub enum VmError {
WriteAccessDenied { backtrace: BT },
#[error("Maximum call depth exceeded.")]
MaxCallDepthExceeded { backtrace: BT },
#[error("The called function args arity mismatch, expected {} args.", actual_arity)]
WrongFunctionArity { actual_arity: usize, backtrace: BT },
}

impl VmError {
Expand Down Expand Up @@ -242,6 +244,10 @@ impl VmError {
backtrace: BT::capture(),
}
}

pub(crate) fn func_arity_mismatch(actual_arity: usize) -> Self {
VmError::WrongFunctionArity { actual_arity, backtrace: BT::capture(), }
}
}

impl_from_err!(CommunicationError, VmError, VmError::CommunicationErr);
Expand Down

0 comments on commit 6fef09a

Please sign in to comment.