Repository navigation
Fix x86-64 JIT operand-stack overflow (segfault on deep-push programs) - #9
Merged
Merged
Conversation
The x86-64 JIT tracked the operand stack in a fixed stack_regs[16] array and incremented stack_depth on every PUSH with no bounds check, so a program with many consecutive PUSH instructions overflowed the array during compilation, corrupting the adjacent next_reg/labels fields and crashing in free(). Add a two-slot headroom guard at the top of the compile loop that cleans up and returns NULL (falling back to the bounds-checked interpreter, which enforces VFM_ERROR_STACK_OVERFLOW correctly) when a program's depth exceeds what the JIT can represent. The ARM64 JIT keeps its operand stack in memory and is unaffected. Caught by the new Ubuntu CI; verified on x86-64 (Docker) and arm64.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
test_stack_overflowsegfaulted on x86-64/Linux (passed on arm64). Root cause is a memory-corruption bug in the x86-64 JIT, not the interpreter:vfm_jit_compile_x86_64tracks the operand stack in a fixedstack_regs[16]array and doesstack_regs[stack_depth++] = regon every PUSH with no bounds check. A program with 30 consecutive PUSH instructions overflows the array during compilation, clobbers the adjacentnext_reg/labelsfields, then crashes infree(jit.labels).arm64 is unaffected because its JIT keeps the operand stack in memory rather than in this register-map array.
Fix
A headroom guard at the top of the compile loop: if compiling the next instruction (widest pushes two slots) would exceed
stack_regs, clean up (free(labels)+munmap) and returnNULL, falling back to the bounds-checked interpreter — which already enforcesVFM_ERROR_STACK_OVERFLOWat runtime (exactly what the test expects). One comparison per instruction at compile time only; no effect on emitted code or fast paths.Verification
gcc:latest, linux/amd64):make clean && make && make test-> 14/14 passed (was SEGV).Both on the default portable (non-NATIVE) build.