From aa72fdab644c265d1a72e6ef2658c2b64194707b Mon Sep 17 00:00:00 2001 From: Valery Antopol Date: Tue, 28 Jun 2022 18:40:19 +0300 Subject: [PATCH] Add memory leak temporary mitigation (#42) --- Cargo.lock | 2 +- example/index.ts | 8 ++++---- service/Cargo.toml | 2 +- service/src/main.rs | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c8e204ea..0fe71ddb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2672,7 +2672,7 @@ dependencies = [ [[package]] name = "trust-graph-wasm" -version = "0.3.0" +version = "0.3.1" dependencies = [ "anyhow", "bincode", diff --git a/example/index.ts b/example/index.ts index 085dd697..ffa3a615 100644 --- a/example/index.ts +++ b/example/index.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -import {trusted_computation} from "./generated/computation"; +import { trusted_computation } from "./generated/computation"; import * as tg from "./generated/export"; -import {Fluence, FluencePeer, KeyPair} from "@fluencelabs/fluence"; -import {krasnodar, Node, testNet, stage} from "@fluencelabs/fluence-network-environment"; +import { Fluence, FluencePeer, KeyPair } from "@fluencelabs/fluence"; +import { krasnodar, Node, testNet, stage } from "@fluencelabs/fluence-network-environment"; import assert from "assert"; const bs58 = require('bs58'); @@ -92,7 +92,7 @@ async function main() { let builtins_keypair = await KeyPair.fromEd25519SK(sk); let relay = local[0]; - await Fluence.start({ connectTo: relay, KeyPair: builtins_keypair}); + await Fluence.start({ connectTo: relay, KeyPair: builtins_keypair }); console.log( "📗 created a fluence peer %s with relay %s", Fluence.getStatus().peerId, diff --git a/service/Cargo.toml b/service/Cargo.toml index 3413fda0..0f65c417 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trust-graph-wasm" -version = "0.3.0" +version = "0.3.1" authors = ["Fluence Labs"] edition = "2018" description = "trust graph wasm" diff --git a/service/src/main.rs b/service/src/main.rs index d4e2c575..cdce3e8f 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -13,10 +13,31 @@ mod results; mod service_api; mod storage_impl; mod tests; +/* + _initialize function that calls __wasm_call_ctors is required to mitigade 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" { + pub fn __wasm_call_ctors(); +} + +#[no_mangle] +fn _initialize() { + unsafe { + __wasm_call_ctors(); + } +} +//------------------------------ pub static TRUSTED_TIMESTAMP: (&str, &str) = ("peer", "timestamp_sec"); pub fn main() { + _initialize(); // As __wasm_call_ctors still does necessary work, we call it at the start of the module WasmLoggerBuilder::new() .with_log_level(log::LevelFilter::Trace) .build()