From 1f7c59bf448f0c02dfce3f4995a0bc71ce1864b4 Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Mon, 20 Jan 2025 11:05:47 +0100 Subject: [PATCH] refactor: document changes --- lib/src/wasm/mod.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/src/wasm/mod.rs b/lib/src/wasm/mod.rs index c5621f11..3758d2a6 100644 --- a/lib/src/wasm/mod.rs +++ b/lib/src/wasm/mod.rs @@ -720,11 +720,29 @@ lazy_static! { config.native_unwind_info(false); config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); - config.memory_reservation(0x1000000); // 16MB + config.epoch_interruption(true); + + // 16MB should be enough for each WASM module. Each module needs a + // fixed amount of memory that is only a few KB long, plus a variable + // amount that depends on the number of rules and patterns (1 bit per + // rule and 1 bit per pattern). With 16MB there's enough space for + // millions of rules and patterns. By default, this is 4GB in 64-bits + // systems, which causes a reservation of 4GB of virtual address space + // (not physical RAM) per module (and therefore per Scanner). In some + // scenarios where virtual address space is limited (i.e: Docker + // instances) this is problematic. See: + // https://github.com/VirusTotal/yara-x/issues/292 + config.memory_reservation(0x1000000); + + // WASM memory won't grow, there's no need to allocate space for + // future grow. config.memory_reservation_for_growth(0); - config.guard_before_linear_memory(false); + + // As the memory can't grow, it won't move. By explicitly indicating + // this, modules can be compiled with static knowledge the base pointer + // of linear memory never changes to enable optimizations. config.memory_may_move(false); - config.epoch_interruption(true); + config }; pub(crate) static ref ENGINE: Engine = Engine::new(&CONFIG).unwrap();