target/i386: Clear s->x86_64_hregs when decoding LAHF/SAHF with a REX prefix - #2370
Open
unknown-1-0 wants to merge 1 commit into
Open
target/i386: Clear s->x86_64_hregs when decoding LAHF/SAHF with a REX prefix#2370unknown-1-0 wants to merge 1 commit into
s->x86_64_hregs when decoding LAHF/SAHF with a REX prefix#2370unknown-1-0 wants to merge 1 commit into
Conversation
Author
|
New tests were added between existing ones to prevent merge conflicts later. |
…EX prefix According to both Volume 2 of Intel 64 and IA-32 Architectures Software Developer's Manual and Volume 3 of AMD64 Architecture Programmer's Manual, the LAHF (0x9f) and SAHF (0x9e) instructions always access the AH register. The presence of a REX prefix in 64-bit mode does not alter this behavior, and uniform byte register addressing (SPL) must not be applied. The bug occurs because the translator blindly sets `s->x86_64_hregs` to `true` upon encountering any REX prefix. During translation of LAHF/SAHF, `gen_op_deposit_reg_v()` is called, which relies on the internal helper `byte_reg_is_xH()` to check whether to address a high byte register (AH). Since `s->x86_64_hregs` is `true`, `byte_reg_is_xH(s, 4)` returns `false`. This causes the translator to bypass the high-byte logic (`reg - 4` with an 8-bit shift) and instead maps index 4 directly to `tcg_ctx->cpu_regs[4]`, leading to SPL being accessed instead of AH. Fix this by ensuring `s->x86_64_hregs` remains `false` when decoding LAHF/SAHF. Signed-off-by: Andrey Polivoda <apolivodaa433@gmail.com>
unknown-1-0
force-pushed
the
i386-fix-lahf-sahf-with-rex
branch
from
July 22, 2026 16:20
1abcfa9 to
6cbb6c1
Compare
Author
|
Force-pushed to sign the commit |
This was referenced Jul 24, 2026
Open
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.
According to both Volume 2 of Intel 64 and IA-32 Architectures Software Developer's Manual and Volume 3 of AMD64 Architecture Programmer's Manual, the LAHF (0x9f) and SAHF (0x9e) instructions always access the AH register. The presence of a REX prefix in 64-bit mode does not alter this behavior, and uniform byte register addressing (SPL) must not be applied.
The bug occurs because the translator blindly sets
s->x86_64_hregstotrueupon encountering any REX prefix. During translation of LAHF/SAHF,gen_op_deposit_reg_v()is called, which relies on the internal helperbyte_reg_is_xH()to check whether to address a high byte register (AH). Sinces->x86_64_hregsistrue,byte_reg_is_xH(s, 4)returnsfalse. This causes the translator to bypass the high-byte logic (reg - 4with an 8-bit shift) and instead maps index 4 directly totcg_ctx->cpu_regs[4], leading to SPL being accessed instead of AH.Fix this by ensuring
s->x86_64_hregsremainsfalsewhen decoding LAHF/SAHF.