Function wasmer_c_api::wasm_c_api::module::wasm_module_deserialize
source · #[no_mangle]
pub unsafe extern "C" fn wasm_module_deserialize(
store: &wasm_store_t,
bytes: Option<&wasm_byte_vec_t>,
) -> Option<NonNull<wasm_module_t>>
Expand description
Deserializes a serialized module binary into a wasm_module_t
.
Note: the module has to be serialized before with the
wasm_module_serialize
function.
§Safety
This function is inherently unsafe as the provided bytes:
- Are going to be deserialized directly into Rust and C objects,
- Contains the function assembly bodies and, if intercepted, a malicious actor could inject code into executable memory.
And as such, the wasm_module_deserialize
method is unsafe.
§Example
int main() {
// Create the engine and the store.
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
// Create a WebAssembly module from a WAT definition.
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(
&wat,
"(module\n"
" (func (export \"function\") (param i32 i64))\n"
" (global (export \"global\") i32 (i32.const 7))\n"
" (table (export \"table\") 0 funcref)\n"
" (memory (export \"memory\") 1))"
);
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
// Create the module.
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
// Serialize the module into bytes.
wasm_byte_vec_t serialized_module;
wasm_module_serialize(module, &serialized_module);
assert(serialized_module.size > 0);
// Free the module.
wasm_module_delete(module);
// Deserialize the serialized module. Note that the store must
// be the same as the one used to serialize.
wasm_module_t* deserialized_module = wasm_module_deserialize(
store,
&serialized_module
);
wasm_byte_vec_delete(&serialized_module);
assert(deserialized_module);
// Check we have our 4 export types.
wasm_exporttype_vec_t export_types;
wasm_module_exports(deserialized_module, &export_types);
assert(export_types.size == 4);
// Free everything.
wasm_exporttype_vec_delete(&export_types);
wasm_module_delete(deserialized_module);
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}