Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions src/wasm_runtime/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,39 @@ fn init_wasm_runtime() -> Result<Vec<u8>> {
Ok(get_flatbuffer_result::<i32>(0))
}

// Helper function to handle common module loading logic after module creation
fn instantiate_module(module: Module, engine: &Engine) -> Result<()> {
let linker = CUR_LINKER.lock();
let linker = linker.deref().as_ref().ok_or(HyperlightGuestError::new(
ErrorCode::GuestError,
"impossible: wasm runtime has no valid linker".to_string(),
))?;

let mut store = Store::new(engine, ());
let instance = linker.instantiate(&mut store, &module)?;

// Module must export malloc and free as these
// are used to marshal param/return values
if instance.get_func(&mut store, "malloc").is_none() {
Comment on lines +134 to +136
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only true if the module uses complex data types like string? I think simple args like int and such can be directly used on the stack

This rust example doesn't export malloc and seems to be working https://github.com/hyperlight-dev/hyperlight-wasm/blob/main/src/rust_wasm_samples/src/lib.rs

return Err(HyperlightGuestError::new(
ErrorCode::GuestError,
"WASM module must export 'malloc' function".to_string(),
));
}

if instance.get_func(&mut store, "free").is_none() {
return Err(HyperlightGuestError::new(
ErrorCode::GuestError,
"WASM module must export 'free' function".to_string(),
));
}

*CUR_MODULE.lock() = Some(module);
*CUR_STORE.lock() = Some(store);
*CUR_INSTANCE.lock() = Some(instance);
Ok(())
}

fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let (
ParameterValue::VecBytes(ref wasm_bytes),
Expand All @@ -130,19 +163,8 @@ fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
&function_call.parameters.as_ref().unwrap()[1],
&*CUR_ENGINE.lock(),
) {
let linker = CUR_LINKER.lock();
let linker = linker.deref().as_ref().ok_or(HyperlightGuestError::new(
ErrorCode::GuestError,
"impossible: wasm runtime has no valid linker".to_string(),
))?;

let module = unsafe { Module::deserialize(engine, wasm_bytes)? };
let mut store = Store::new(engine, ());
let instance = linker.instantiate(&mut store, &module)?;

*CUR_MODULE.lock() = Some(module);
*CUR_STORE.lock() = Some(store);
*CUR_INSTANCE.lock() = Some(instance);
instantiate_module(module, engine)?;
Ok(get_flatbuffer_result::<i32>(0))
} else {
Err(HyperlightGuestError::new(
Expand All @@ -158,19 +180,8 @@ fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
&function_call.parameters.as_ref().unwrap()[1],
&*CUR_ENGINE.lock(),
) {
let linker = CUR_LINKER.lock();
let linker = linker.deref().as_ref().ok_or(HyperlightGuestError::new(
ErrorCode::GuestError,
"impossible: wasm runtime has no valid linker".to_string(),
))?;

let module = unsafe { Module::deserialize_raw(engine, platform::map_buffer(*phys, *len))? };
let mut store = Store::new(engine, ());
let instance = linker.instantiate(&mut store, &module)?;

*CUR_MODULE.lock() = Some(module);
*CUR_STORE.lock() = Some(store);
*CUR_INSTANCE.lock() = Some(instance);
instantiate_module(module, engine)?;
Ok(get_flatbuffer_result::<()>(()))
} else {
Err(HyperlightGuestError::new(
Expand Down
Loading