diff options
| author | d7ommy <abnalstlll@gmail.com> | 2026-11-26 02:54:28 +0300 |
|---|---|---|
| committer | d7ommy <abnalstlll@gmail.com> | 2026-11-26 02:54:28 +0300 |
| commit | 5ce5769d48980b8d89baa30a423256de9ce8dcf0 (patch) | |
| tree | a40692088f0d0fe0b8ce8c59229aa3dcffa52f5c /runtime/src/pe_loader.cpp | |
| parent | 33bd07cf86bb42236635c27341359b3fe841d1d5 (diff) | |
x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY)
In x86-64, shifts and rotates (shl, shr, sar, rol, ror) leave all flags
untouched when the count is zero.
In tools/x64_recomp.py:
1. Immediate count 0 shifts were classified with flags_w = FLAG_WRITERS[mnem],
causing FunctionTranslator to treat them as flag producers in arm_path,
while emitting 0 instructions. Subsequent condition checks (e.g. setc/jcc)
therefore checked ADD_KIND condition 'cs' against the preceding SUB_KIND
instruction (which set C == 0 on borrow), inverting the carry flag.
2. Variable count shifts (cl) skipped execution on cbz(T1, skip) when cl == 0,
leaving ARM NZCV in the preceding SUB_KIND instruction's polarity
(C == 0 for borrow), while downstream consumers expected ADD_KIND polarity
(C == 1 for borrow), inverting the carry flag.
Fix:
1. In _classify(), mark shifts and rotates with immediate count 0 (including
masked mod 32/64 zero counts) with flags_w = '' and flags_r = '' so they
are not treated as flag writers.
2. In _flag_analysis(), track the nearest reaching carry writer before variable
shifts in the basic block (shift_prev_c).
3. In _emit_shift(), on the cl == 0 path when carry is needed:
- If the preceding carry producer was in SUB_KIND, invert NZCV bit 29
(C := !C) to convert subtraction borrow polarity into ADD_KIND carry
polarity.
- If there was no preceding carry producer in the block, restore CF from
CTX_EFLAGS into NZCV bit 29.
4. Move _maybe_mkflags(ins) after the skip label so that materialized flag
blocks preserve valid flags on both zero and non-zero count paths.
5. Add unit tests in tests/test_x64_recomp.py.
AI-assisted (Claude Code)
Diffstat (limited to 'runtime/src/pe_loader.cpp')
0 files changed, 0 insertions, 0 deletions