Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Refactor resource and error handling in wasm (#6074)
Browse files Browse the repository at this point in the history
* Refactor resource and error handling in wasm

* Fixes based on review
  • Loading branch information
0x7CFE committed May 20, 2020
1 parent 4269e23 commit a67dcfb
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions client/executor/common/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,33 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
.supervisor_externals
.allocate_memory(invoke_args_len)
.map_err(|_| trap("Can't allocate memory in supervisor for the arguments"))?;
self

let deallocate = |this: &mut GuestExternals<FE>, ptr, fail_msg| {
this
.supervisor_externals
.deallocate_memory(ptr)
.map_err(|_| trap(fail_msg))
};

if self
.supervisor_externals
.write_memory(invoke_args_ptr, &invoke_args_data)
.map_err(|_| trap("Can't write invoke args into memory"))?;
.is_err()
{
deallocate(self, invoke_args_ptr, "Failed dealloction after failed write of invoke arguments")?;
return Err(trap("Can't write invoke args into memory"));
}

let result = self.supervisor_externals.invoke(
&self.sandbox_instance.dispatch_thunk,
invoke_args_ptr,
invoke_args_len,
state,
func_idx,
)?;
self
.supervisor_externals
.deallocate_memory(invoke_args_ptr)
.map_err(|_| trap("Can't deallocate memory for dispatch thunk's invoke arguments"))?;
);

deallocate(self, invoke_args_ptr, "Can't deallocate memory for dispatch thunk's invoke arguments")?;
let result = result?;

// dispatch_thunk returns pointer to serialized arguments.
// Unpack pointer and len of the serialized result data.
Expand All @@ -265,12 +277,11 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {

let serialized_result_val = self.supervisor_externals
.read_memory(serialized_result_val_ptr, serialized_result_val_len)
.map_err(|_| trap("Can't read the serialized result from dispatch thunk"))?;
self.supervisor_externals
.deallocate_memory(serialized_result_val_ptr)
.map_err(|_| trap("Can't deallocate memory for dispatch thunk's result"))?;
.map_err(|_| trap("Can't read the serialized result from dispatch thunk"));

deserialize_result(&serialized_result_val)
deallocate(self, serialized_result_val_ptr, "Can't deallocate memory for dispatch thunk's result")
.and_then(|_| serialized_result_val)
.and_then(|serialized_result_val| deserialize_result(&serialized_result_val))
}
}

Expand Down

0 comments on commit a67dcfb

Please sign in to comment.