Skip to content

Commit

Permalink
Maintain sufficient allocated space in the vector
Browse files Browse the repository at this point in the history
`wasm_runtime_call_wasm()`` is called with `argv`
used to pass parameters, and the callee reuses
`argv` to store the results. Therefore, `argv`
must be long enough to hold the results.

Typically, a vector's length changes with
operations, but in our scenario, `append()`
operation occurs outside of our control. If there
are more results than parameters, the vector will
end up with an incorrect length unless we
proactively manage it.

Update Dockerfile and devcontainer configuration
  • Loading branch information
lum1n0us committed Jan 23, 2025
1 parent 976251e commit 6acbc18
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ RUN cd /tmp \
&& chmod a+x llvm.sh \
&& ./llvm.sh 15

RUN ln -sf /usr/bin/lldb-15 /usr/bin/lldb
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
},
"vscode": {
"extensions": [
"bungcip.better-toml",
"dtsvet.vscode-wasm",
"fill-labs.dependi",
"rust-lang.rust-analyzer",
"streetsidesoftware.code-spell-checker",
"tamasfe.even-better-toml",
"vadimcn.vscode-lldb"
],
}
Expand Down
22 changes: 18 additions & 4 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use std::{ffi::CString, marker::PhantomData};
use wamr_sys::{
wasm_exec_env_t, wasm_func_get_result_count, wasm_func_get_result_types, wasm_function_inst_t,
wasm_runtime_call_wasm, wasm_runtime_get_exception, wasm_runtime_get_exec_env_singleton,
wasm_exec_env_t, wasm_func_get_param_count, wasm_func_get_result_count,
wasm_func_get_result_types, wasm_function_inst_t, wasm_runtime_call_wasm,
wasm_runtime_get_exception, wasm_runtime_get_exec_env_singleton,
wasm_runtime_get_wasi_exit_code, wasm_runtime_lookup_function, wasm_valkind_enum_WASM_F32,
wasm_valkind_enum_WASM_F64, wasm_valkind_enum_WASM_I32, wasm_valkind_enum_WASM_I64,
wasm_valkind_t,
Expand Down Expand Up @@ -87,19 +88,32 @@ impl<'instance> Function<'instance> {
instance: &'instance Instance<'instance>,
params: &Vec<WasmValue>,
) -> Result<WasmValue, RuntimeError> {
let param_count =
unsafe { wasm_func_get_param_count(self.function, instance.get_inner_instance()) };
if param_count > params.len() as u32 {
return Err(RuntimeError::ExecutionError(ExecError {
message: "invalid parameters".to_string(),
exit_code: 0xff,
}));
}

// params -> Vec<u32>
let mut argv = Vec::new();
for p in params {
argv.append(&mut p.encode());
}

let argc = params.len();
// Maintain sufficient allocated space in the vector rather than just declaring its capacity.
let result_count =
unsafe { wasm_func_get_result_count(self.function, instance.get_inner_instance()) };
argv.resize(std::cmp::max(param_count, result_count) as usize, 0);

let call_result: bool;
unsafe {
let exec_env: wasm_exec_env_t =
wasm_runtime_get_exec_env_singleton(instance.get_inner_instance());
call_result =
wasm_runtime_call_wasm(exec_env, self.function, argc as u32, argv.as_mut_ptr());
wasm_runtime_call_wasm(exec_env, self.function, param_count, argv.as_mut_ptr());
};

if !call_result {
Expand Down

0 comments on commit 6acbc18

Please sign in to comment.