Skip to content

jit: restore all callee-saved registers in conditional block epilogues - #105

Merged
GloriousTacoo merged 2 commits into
pound-emu:mainfrom
GabrielGonHo:jit/fix-conditional-block-epilogue
Oct 28, 2026
Merged

GloriousTacoo merged 2 commits into
pound-emu:mainfrom
GabrielGonHo:jit/fix-conditional-block-epilogue

Conversation

@GabrielGonHo

Copy link
Copy Markdown
Contributor

Description

The function prologue in �al_tier1_compiler_translate() pushes five callee-saved registers (RBP, R12, R13, R14, R15), but two epilogues only popped one of them:

  • erminate_block_conditional() — the taken branch epilogue popped only RBP.
  • ranslate_b_cond(), case 0xE (AL, always) — same single RBP pop.

The taken path therefore returned with RSP 32 bytes below where the caller expects it, and
et consumed a saved register instead of the return address. The first taken conditional branch (and every AL-flag branch) crashed. The fallthrough epilogue and erminate_block() already pop all five, so this was an oversight in the two taken paths.

Both epilogues now pop R15, R14, R13, R12, then RBP.

A second issue in erminate_block_conditional(): lush_dirty_registers() was called three times without ever clearing compiler->is_dirty, so the second and third calls re-pushed and re-emitted the same stores (the second batch landed after the fallthrough
et, and the jcc offset only accounted for one batch). is_dirty is now cleared after the first flush, making the later flushes no-ops and the jcc offset exact.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Testing

  • Compiled src/backend/x86/bal_x86_tier1_compiler.c with Clang (zig cc) on the Linux (x86_64-linux-gnu) and Windows (x86_64-windows-gnu) targets using the project's warning set: no warnings or errors.
  • The unit test suite can't be built on this machine (Linux toolchain not available). The defect is observable by compiling any ARM64 block containing a taken conditional branch and executing it: the original code traps on the
    et of the taken path.

GabrielGonHo and others added 2 commits October 25, 2026 16:55
terminate_block_conditional() and the AL (always) case of translate_b_cond() emitted a taken-branch epilogue that popped only RBP, while the function prologue pushes RBP, R12, R13, R14 and R15. The taken path therefore returned with RSP 32 bytes below where the caller expects it, and ret consumed a saved register instead of the return address (crash on the first taken conditional branch, and on every AL-flag branch).

Both epilogues now pop R15, R14, R13, R12 and then RBP. is_dirty is also cleared after the first register flush so the second flush does not re-emit the same stores, keeping the jcc offset exact.

Signed-off-by: GabrielGonHo <195778548+GabrielGonHo@users.noreply.github.com>
@GloriousTacoo
GloriousTacoo merged commit de07fca into pound-emu:main Oct 28, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants