blob: fde7f28c8f55da7b321b9f3899dbde3691d66856 [file] [edit]
From: rive
Subject: [PATCH 0064] touch only the script stack a call uses
An exec env zeroed its whole wasm stack when it was made, 512 KB for each
of our script VMs, so every file that ran scripts paid that much resident
memory up front, though calls rarely reach a fraction of it. Nothing reads
the stack before writing it: every interpreter frame sets the fields it
reads when pushed and zeroes its own locals, and compiled code keeps no
frames there unless frame bookkeeping is on. Calls reuse the stack without
zeroing it again anyway, so only an exec env's first call ever saw zeroes,
and call stack dumps already read whatever earlier calls left in a frame.
Only the exec env's header is zeroed now, and the stack's pages stay
untouched until a call reaches them. GC and debugger builds, which scan
frames' slots, keep zeroing all of it.
diff --git a/core/iwasm/common/wasm_exec_env.c b/core/iwasm/common/wasm_exec_env.c
index 4775295..49e0e99 100644
--- a/core/iwasm/common/wasm_exec_env.c
+++ b/core/iwasm/common/wasm_exec_env.c
@@ -38,7 +38,14 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
|| !(exec_env = wasm_runtime_malloc((uint32)total_size)))
return NULL;
+ /* rive patch: frames write what they read and calls reuse the stack
+ unzeroed, so it stays untouched until used; GC and debugger builds
+ still zero it. */
+#if WASM_ENABLE_GC != 0 || WASM_ENABLE_DEBUG_INTERP != 0
memset(exec_env, 0, (uint32)total_size);
+#else
+ memset(exec_env, 0, offsetof(WASMExecEnv, wasm_stack_u.bottom));
+#endif
#if WASM_ENABLE_AOT != 0
if (!(exec_env->argv_buf = wasm_runtime_malloc(sizeof(uint32) * 64))) {