x86: fix xadd reg,reg writeback order for the same-register case - #85
Open
DORA-B wants to merge 1 commit into
Open
x86: fix xadd reg,reg writeback order for the same-register case#85DORA-B wants to merge 1 commit into
DORA-B wants to merge 1 commit into
Conversation
dis_xadd_G_E wrote E (the sum) before G (the old E value) in the reg-reg case. When the two operands are the same register (`xadd %eax,%eax`), the second write (G := old E) then clobbered the sum, so the register kept its original value instead of 2*eax. The Intel semantics are TEMP := SRC+DEST; SRC := DEST; DEST := TEMP, i.e. the destination write is last and must win. Swap the two writes so G (old E) is written first and E (the sum) last, matching the amd64 front-end (which already orders them G-then-E and is unaffected). For distinct registers the order is irrelevant, so only the degenerate same-register case changes.
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.
dis_xadd_G_E wrote E (the sum) before G (the old E value) in the reg-reg case. When the two operands are the same register (
xadd %eax,%eax), the second write (G := old E) then clobbered the sum, so the register kept its original value instead of 2*eax. The Intel semantics are TEMP := SRC+DEST; SRC := DEST; DEST := TEMP, i.e. the destination write is last and must win.Swap the two writes so G (old E) is written first and E (the sum) last, matching the amd64 front-end (which already orders them G-then-E and is unaffected). For distinct registers the order is irrelevant, so only the degenerate same-register case changes.
Impact
Silent wrong result (no decode error, no exception) for the degenerate
same-register form. Concretely
xadd %eax,%eaxleaves EAX unchanged instead ofdoubling it; any following use of EAX or a flag-consuming branch then diverges.
Distinct-register
xaddis unaffected (write order is irrelevant there).Reproduction (before the fix)
Root cause
dis_xadd_G_E, reg-reg case (epartIsReg(rm)):Both source reads are correctly latched into temps, so the flags and
tmpt1areright; only the order of the two writes is wrong. When
E == G, the finalG := old Ewins and the sum is lost. The amd64 front-end'sdis_xadd_G_Ealready writes G first, then E, and is unaffected.