diff --git a/air-interpreter/src/marine.rs b/air-interpreter/src/marine.rs index 27d5f072..98fa249d 100644 --- a/air-interpreter/src/marine.rs +++ b/air-interpreter/src/marine.rs @@ -37,7 +37,29 @@ use marine_rs_sdk::module_manifest; module_manifest!(); +/* + _initialize function that calls __wasm_call_ctors is required to mitigate memory leak + that is described in https://github.com/WebAssembly/wasi-libc/issues/298. + + In short, without this code rust wraps every export function + with __wasm_call_ctors/__wasm_call_dtors calls. This causes memory leaks. When compiler sees + an explicit call to __wasm_call_ctors in _initialize function, it disables export wrapping. + + TODO: remove when updating to marine-rs-sdk with fix +*/ +extern "C" { + fn __wasm_call_ctors(); +} + +#[no_mangle] +pub fn _initialize() { + unsafe { + __wasm_call_ctors(); + } +} + pub fn main() { + _initialize(); // As __wasm_call_ctors still does necessary work, we call it at the start of the module. logger::init_logger(None); }