From 8bd4aa57839c5cba3f0c7bdcab149e2d76589a95 Mon Sep 17 00:00:00 2001 From: Valery Antopol Date: Fri, 8 Jul 2022 17:29:26 +0300 Subject: [PATCH] Add a workaround for wasm memory leak (#282) * Add a workaround for wasm memory leak * fmt --- air-interpreter/src/marine.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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); }