8000 arm64: lift CFINV, XAFLAG and AXFLAG by DORA-B · Pull Request #109 · angr/vex · GitHub
[go: up one dir, main page]

Skip to content

arm64: lift CFINV, XAFLAG and AXFLAG - #109

Open
DORA-B wants to merge 2 commits into
angr:masterfrom
DORA-B:fix/arm64-flagm-nzcv
Open

DORA-B wants to merge 2 commits into
angr:masterfrom
DORA-B:fix/arm64-flagm-nzcv

Conversation

@DORA-B
@DORA-B DORA-B commented Nov 30, 2026
Copy link
Copy Markdown

Summary

If I clear NZCV and execute cfinv, the next b.cs should take the branch. With VEX fd41c675 and PyVEX 6a81d15f, carry stays clear. Reading NZCV immediately after xaflag and axflag shows the same problem. Both instructions leave the old flags in place.

Architectural definition

Arm’s A64 instruction reference (2025-09 release) gives the following rules. Each expression uses the flags before the instruction executes.

Instruction What Arm specifies Reference
CFINV (FlagM) Invert C. N, Z and V keep their values. Arm, CFINV, p. 119
XAFLAG (FlagM2) Convert external comparison flags to Arm format. The new N, Z, C and V are (NOT C) AND (NOT Z), Z AND C, C OR Z and (NOT C) AND Z, respectively. Arm, XAFLAG, p. 888
AXFLAG (FlagM2) Convert Arm comparison flags to external format. Set Z to Z OR V and C to C AND (NOT V). Clear N and V. Arm, AXFLAG, p. 53

Reproducer

Here is a standalone example using angr. Each case writes the starting flags with msr nzcv, x0, executes the instruction, then reads the flags back with mrs x3, nzcv. N, Z, C and V occupy bits 31 through 28.

import angr

# Mnemonic, instruction word, initial NZCV, expected NZCV
cases = [
    ("cfinv",  0xd500401f, 0x00000000, 0x20000000),  # clear carry becomes set
    ("xaflag", 0xd500403f, 0x00000000, 0x80000000),  # C=0 and Z=0 give N=1
    ("axflag", 0xd500405f, 0x10000000, 0x40000000),  # V=1 gives Z=1 and clears V
]

for name, instruction, initial, expected in cases:
    words = (0xd51b4200, instruction, 0xd53b4203)
    code = b"".join(word.to_bytes(4, "little") for word in words)
    project = angr.load_shellcode(code, arch="aarch64", load_address=0x1000)
    state = project.factory.blank_state(addr=0x1000)
    state.regs.x0 = initial
    for _ in words:
        state, = state.step(num_inst=1).flat_successors
    actual = state.solver.eval_one(state.regs.x3)
    print(f"{name}: got {actual:#010x}, expected {expected:#010x}")

I get this output on master.

cfinv: got 0x00000000, expected 0x20000000
xaflag: got 0x00000000, expected 0x80000000
axflag: got 0x10000000, expected 0x40000000

Root cause

The VEX AArch64 decoder returns NoDecode for all three instruction words. PyVEX then accepts them through its generic Instruction_MSR fallback. That handler advances the PC without emitting a flag write, so the following mrs x3, nzcv reads the previous value. PyVEX fallback at the tested revision

Fix

Decode the three instructions in guest_arm64_toIR.c. Read NZCV with mk_arm64g_calculate_flags_nzcv(), apply the operation to those input bits, and write the resulting flags with setFlags_COPY. Reading all four flags first matters for AXFLAG, where the incoming V contributes to the new Z before V is cleared. The patch models execution with the corresponding architectural feature present.

Validation

With the patch, the reproducer reads back 0x20000000, 0x80000000 and 0x40000000, respectively. I repeated the sequence with every possible starting NZCV value. I also replaced the initial msr with cmp to check flags produced by arithmetic, including a signed overflow.

8000

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.

1 participant

0