Re: [RFC PATCH v2 27/30] tools/unwind: Add multi-arch symbolic instruction lifters for verify_cfi
From: bot+bpf-ci
Date: Wed Nov 25 2026 - 02:05:59 EST
> tools/unwind: Add multi-arch symbolic instruction lifters for verify_cfi
TL;DR:
- x86 PUSH64r/POP64r prefix tests likely catch memory forms, possibly
clobbering %rbp and causing false CFI mismatches
- arm64 sub/add sp, sp, xN probably matches wrong opcodes, leaving SP unknown
- RISC-V jal x0 may clobber ra; LDP W scale looks wrong
> diff --git a/tools/unwind/verify_insn_arch.cpp b/tools/unwind/verify_insn_arch.cpp
> --- /dev/null
> +++ b/tools/unwind/verify_insn_arch.cpp
[ ... ]
> + if (Desc.isCall()) {
> + for (int d = 0; d < 32; d++) {
> + if (AS.llvm_arch == Triple::x86_64) {
[ ... ]
> + } else if (AS.llvm_arch == Triple::riscv64) {
> + if (d == 0 || d == 2 || d == 3 || d == 4 ||
> + d == 8 || d == 9 || (d >= 18 && d <= 27))
> + continue;
> + st.regs[d] = {VK_UNKNOWN, 0};
On riscv64, `jal ra, sym` and `jalr ra, off(rs)` do not set isCall in their
MCInstrDesc, and stepInstructionRiscv() does not model JAL/JALR. Is this
riscv64 arm of the Desc.isCall() loop reachable for a real call?
In the later patch "tools/unwind: Add CFG instruction abstract execution
oracle to verify_cfi" (9528bbf0f915), verifyOracle2Instructions() sends
every RISC-V call around stepInstruction() and repeats the same register
list:
if (AS.llvm_arch == Triple::riscv64 && is_call) {
for (int d = 1; d < 32; d++) {
if (d == 2 || d == 3 || d == 4 ||
d == 8 || d == 9 ||
(d >= 18 && d <= 27))
continue;
next_st.regs[d] = {VK_UNKNOWN, 0};
}
} else {
stepInstruction(next_st, MI, OpName, Desc, MCRI, ...);
}
So the RISC-V caller-saved set is kept in two places that have to stay in
sync, and any other caller of clobberGenericDefs()/stepInstruction() leaves
a0-a7 and t0-t6 live across `jal ra`/`jalr ra`, contrary to the "ABI
caller-saved registers across call instructions" comment.
Could the normalized is_call be passed into stepInstruction() and
clobberGenericDefs() so the oracle can drop its copy, or could this arm be
removed with a note that the caller handles RISC-V calls?
[ ... ]
> + if (OpName.starts_with("PUSH64r")) {
> + int src = ctx.regD(0);
> + st.regs[7] = addSymImm(st.regs[7], -8);
> + st.store(st.regs[7], ctx.getRegVal(st, src));
> + return true;
> + }
> + if (OpName == "PUSHF64" || OpName.starts_with("PUSH64i") ||
> + OpName == "PUSH64rmm" || OpName == "PUSH64rmr") {
> + st.regs[7] = addSymImm(st.regs[7], -8);
> + st.store(st.regs[7], {VK_UNKNOWN, 0});
> + return true;
> + }
> + if (OpName.starts_with("POP64r")) {
> + int dst = ctx.regD(0);
> + SymVal loaded = st.load(st.regs[7]);
> + st.regs[7] = addSymImm(st.regs[7], 8);
> + if (dst != 7)
> + ctx.setRegVal(st, dst, loaded);
> + return true;
> + }
> + if (OpName == "POPF64" || OpName == "POP64rmm") {
> + st.regs[7] = addSymImm(st.regs[7], 8);
> + return true;
> + }
Don't starts_with("PUSH64r") and starts_with("POP64r") also match the memory
forms PUSH64rmm and POP64rmm? If so, the explicit "PUSH64rmm",
"PUSH64rmr" and "POP64rmm" tests below them can never be reached.
For those memory forms, MCInst operand 0 is the base register of the memory
reference, so ctx.regD(0) returns the address base rather than a data
register.
For `pushq -8(%r10)` (PUSH64rmm) this stores the symbolic value of %r10 into
the new stack slot, where the intended branch would store VK_UNKNOWN. That
is the DRAP sequence the function's kernel-doc says it models:
lea 8(%rsp), %r10
and $-64, %rsp
pushq -8(%r10)
The same happens for any `pushq disp(%rbp)` or `pushq disp(%rsp)`, such as
GCC pushing stack-passed arguments for calls with more than six arguments.
For `popq disp(%base)` (POP64rmm), ctx.setRegVal(st, base, loaded) overwrites
the base register's symbolic value with the popped value instead of storing
to memory. With base == %rbp, for example `pushf; popq -0x10(%rbp)` from
native_save_fl() with the "=rm" output constraint, %rbp becomes VK_UNKNOWN.
Every following row with an rbp-based CFA would then fail in
evalRowAgainstAbs() with cfa_clobbered_or_opaque or fp_recovery_mismatch for
a correct function.
Would testing exact names (OpName == "PUSH64r" || OpName == "PUSH64rmr", and
OpName == "POP64r" || OpName == "POP64rmr"), or moving the memory-form
branches ahead of the prefix tests, give the intended behaviour? No later
patch in the series (up to 0278f019673d) touches verify_insn_arch.cpp.
[ ... ]
> + if ((OpName == "ADDXrs" || OpName == "SUBXrs" ||
> + OpName == "ADDXrx" || OpName == "SUBXrx") &&
> + MI.getNumOperands() >= 4 && MI.getOperand(3).isImm()) {
[ ... ]
> + } else if (OpName.ends_with("rx") &&
> + ((imm_arg >> 3) == 3 || (imm_arg >> 3) == 7) &&
> + (imm_arg & 7) == 0) {
> + ok_shift = true;
> + }
Are these the right LLVM opcode names for `add/sub sp, sp, xN`? In LLVM's
AArch64 AddSub multiclass, ADDXrx/SUBXrx take a GPR32 Rm with the
UXTB..SXTW extends. The 64-bit Rm forms that allow only UXTX/SXTX are the
separate opcodes ADDXrx64/SUBXrx64, and the aliases used when Rd or Rn is SP
(`sub sp, sp, x12` == `sub sp, sp, x12, uxtx #0`) bind to those.
If so, the (imm_arg >> 3) == 3 || == 7 test can never be true for a decoded
ADDXrx/SUBXrx, so this sub-branch is dead. "SUBXrx64"/"ADDXrx64" match no
case, stepInstructionArm64() returns false, and clobberGenericDefs() sets
operand 0 (SP, DWARF 31) to VK_UNKNOWN.
This prologue shape is common with GCC when the adjustment is not a valid
add immediate but can be loaded with one mov:
mov x12, 4160
sub sp, sp, x12
.cfi_def_cfa_offset 4160
After that instruction sp is VK_UNKNOWN, and evalRowAgainstAbs() would report
cfa_clobbered_or_opaque for the SP-based CFA. Even with the CFA moved to
x29, the matching `add sp, sp, x12` / `ldp x29, x30, [sp], #N` epilogue
would still report a mismatch. The MOVZXi/MOVNXi/MOVKXi constant tracking
added for this case then has no consumer for SP.
Should this match "ADDXrx64"/"SUBXrx64" (and adjust the ends_with("rx")
test), with the current ADDXrx/SUBXrx names dropped or handled separately
with W-register extend semantics? No later patch in the series fixes this.
[ ... ]
> + auto scaleOf = [](StringRef N) -> int64_t {
> + if (N.contains("Q"))
> + return 16;
> + if (N.contains("X") || N.contains("D"))
> + return 8;
> + if (N.contains("W") || N.contains("S"))
> + return 4;
Doesn't N.contains("D") also match the "D" in the "LDP" mnemonic prefix?
scaleOf("LDPWpost") and scaleOf("LDPWpre") would then return 8 instead of 4.
The MC operand for LDP/STP is the raw signed imm7, so the scale is used
directly here:
ctx.setRegVal(st, wb,
addSymImm(addr,
MI.getOperand(4).getImm() * scaleOf(OpName)));
For example `ldp w19, w20, [sp], #8` would advance the abstract SP by 16 and
desynchronise it from the CFI, giving a spurious CFA mismatch for the rest
of the path. The STP W forms come out right only because "STP" contains
neither 'D' nor 'X'. Compiler-generated prologues and epilogues do not use
W-pair writeback on SP, so this mainly affects hand-written assembly.
Would selecting the scale from the register-class character after
"LDP"/"STP" (OpName[3]) avoid the substring ambiguity?
[ ... ]
> +void normalizeRiscvControlFlow(const MCInst &MI, StringRef OpName,
> + const MCRegisterInfo &MCRI,
> + bool &is_ret_or_ind_jump, bool &is_uncond_branch,
> + bool &is_call)
> +{
[ ... ]
> + if (OpName == "JAL") {
> + if (rd == 0) {
> + is_uncond_branch = true;
> + is_call = false;
normalizeRiscvControlFlow() clears is_call for an uncompressed `j` (JAL
x0), but doesn't stepInstruction() still see it as a call and clobber ra?
The caller verifyOracle2Instructions() added in 9528bbf0f915 starts from
is_call = Desc.isCall(), which is true for JAL since LLVM marks JAL/JALR
isCall = 1. After normalisation is_call is false, so the caller takes the
else branch and calls stepInstruction(). stepInstructionRiscv() has no
"JAL" case and returns false, so stepInstruction() falls back to:
if (!handled)
clobberGenericDefs(st, MI, Desc, MCRI, AS);
clobberGenericDefs() re-reads Desc.isCall(), which is still true for JAL,
and the riscv64 arm sets ra (x1), t0-t6 and a0-a7 to VK_UNKNOWN on every
plain unconditional jump. The state is then enqueued at the branch target.
If the RA rule there is DWARF_CFI_RULE_SAME, evalRowAgainstAbs() reads
st.regs[1] == VK_UNKNOWN and records a spurious ra_recovery_mismatch. RA is
SAME throughout leaf functions, and before the prologue or after the
epilogue in shrink-wrapped paths.
With RVC, `c.j` (C_J, not isCall) is unaffected, but an uncompressed `jal
x0` is still emitted when the target is outside the +/-2KB c.j range, and
for every jump in code built without the C extension. The real call paths
never reach this clobber because the caller handles riscv64 calls itself, so
the riscv64 isCall branch in clobberGenericDefs() only fires for JAL x0, the
one case that is not a call.
Could "JAL" with rd == x0 (and C_J) be modelled in stepInstructionRiscv() as
a no-op returning true, or could stepInstruction() take the normalised
is_call instead of using Desc.isCall()? No later patch in the series fixes
this.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45561209846