diff --git a/source/m3_env.c b/source/m3_env.c index 6700226..084dead 100644 --- a/source/m3_env.c +++ b/source/m3_env.c @@ -310,9 +310,7 @@ M3Result ResizeMemory (IM3Runtime io_runtime, u32 i_numPages) memory->mallocated->length = numPageBytes; memory->mallocated->runtime = io_runtime; - // TODO: track max function stack height and use this instead of hard-coded constant - d_m3Assert(io_runtime->numStackSlots > d_m3MaxFunctionStackHeight); - memory->mallocated->maxStack = (m3reg_t *) io_runtime->stack + (io_runtime->numStackSlots - d_m3MaxFunctionStackHeight); + memory->mallocated->maxStack = (m3reg_t *) io_runtime->stack + io_runtime->numStackSlots; m3log (runtime, "resized old: %p; mem: %p; end: %p; pages: %d", oldMallocated, memory->mallocated, memory->mallocated->end, memory->numPages); } diff --git a/source/m3_env.h b/source/m3_env.h index d177852..5fdb3bb 100644 --- a/source/m3_env.h +++ b/source/m3_env.h @@ -46,6 +46,8 @@ typedef struct M3Function IM3Operation callOp; pc_t compiled; + u32 maxStackSlots; + u32 numLocals; // not including args u32 numConstants; diff --git a/source/m3_exec.c b/source/m3_exec.c index e8069b1..040acd5 100644 --- a/source/m3_exec.c +++ b/source/m3_exec.c @@ -188,13 +188,14 @@ d_m3OpDef (Compile) d_m3OpDef (Entry) { + IM3Function function = immediate (IM3Function); + #if defined(d_m3SkipStackCheck) if (true) #else - if ((void *) _sp <= _mem->maxStack) + if ((void*)(_sp + function->maxStackSlots) < _mem->maxStack) #endif { - IM3Function function = immediate (IM3Function); function->hits++; m3log (exec, " enter %p > %s %s", _pc - 2, function->name ? function->name : ".unnamed", SPrintFunctionArgList (function, _sp)); u32 numLocals = function->numLocals; diff --git a/source/m3_parse.c b/source/m3_parse.c index 02ca49c..bb39d6d 100644 --- a/source/m3_parse.c +++ b/source/m3_parse.c @@ -331,6 +331,9 @@ _ (NormalizeType (& normalizedType, varType)); func->wasm = start; func->wasmEnd = i_bytes; func->numLocals = numLocalVars; + + // TODO: track max function stack height and use this instead of hard-coded constant + func->maxStackSlots = 4; } else _throw (m3Err_wasmSectionOverrun); }