Skip to content

[AUTOMATED] Keep a call argument whose add or sub also sets the flags a branch tests - #2148

Open
kanonl125 wants to merge 1 commit into
Noelo-Lab:mainfrom
kanonl125:flag-use-call-argument
Open

kanonl125 wants to merge 1 commit into
Noelo-Lab:mainfrom
kanonl125:flag-use-call-argument

Conversation

@kanonl125

Copy link
Copy Markdown
Contributor

On x86, a call loses its last argument when the sub or add that computes the argument also sets the flags a branch before the call tests.

cat > r.c <<'EOF'
typedef long (*rd_t)(int, void *, unsigned long);
rd_t g_read;
__attribute__((noinline)) long net_read(int fd, char *buf, unsigned long n) {
    unsigned long got = 0;
    while (got < n) {
        long r = g_read(fd, buf + got, n - got);
        if (r <= 0) return r;
        got += (unsigned long)r;
    }
    return (long)got;
}
int main(void) { return 0; }
EOF
zig cc -target x86_64-linux-gnu -O2 -Wl,--export-dynamic r.c -o r   # mov rdx,r14; sub rdx,rbx; jbe out; ...; call [r13]
kuna decompile r net_read
    v1 = (*g_read)(fd,&buf[v2]);

The AArch64 build (subs x2,x19,x20; b.ls out; ...; blr x8) prints (fd,&buf[v3],n - v3).

  • x86's SLEIGH sets the zero flag from the destination register itself (resultflags(rdx): ZF = rdx == 0).
  • So only_op_use walks from rdx into ZF, the BOOL_OR with CF, and the CBRANCH, and rejects the trial. No later argument fills the hole.
  • AArch64 computes subs into a temporary, sets the flags from it, and copies it into x2, so x2's only use is the call.

Fix

p4_calls/kuna_resultflags.rs (always on) scores these arguments as AArch64's lifting already does. While a CALL or CALLIND argument is scored, and the value is the result of an add, sub, inc or dec, the walk skips a comparison against a constant when both hold:

  • The instruction that writes the value makes the comparison, of that value or of the view of it the instruction takes. For example, sub edx,ecx reads edx back out of the rdx heritage rebuilds.
  • The flag reaches only CBRANCH conditions, through boolean ops, copies, merges and INDIRECTs.

These are still uses:

  • A logical op. x86 has no flags-only or or xor, so a combined test (xor ecx,0x59; or ecx,eax; jne) writes a register it never reads again. clang also tests a bit with mov ecx,eax; and ecx,0x100000; jne. Admitted, they would give sqlite3's sqlite3DbMallocRawNN, sqlite3BeginWriteOperation and sqlite3ErrorMsg calls arguments those functions do not take. The flags-only form of add and sub is cmp, so for those a written register is a wanted value.
  • A later instruction's flags. This includes cmp and test, which write no register.
  • A flag kept as data (sete).
  • A value that goes round into its own instruction again, such as a loop counter sub rdx,1; jne loop left in rdx at a call after the loop. AArch64's walk rejects it on the next iteration's temporary.

A RETURN's trials are scored as before.

    v1 = (*g_read)(fd,&buf[v2],n - v2);

It is a judgement. A hand-written sub rdx,rbx; jbe whose rdx is dead gives an unknown callee rdx as an argument, as AArch64 already does for subs. Once the difference is an argument it has two uses, so the guard prints as AArch64 prints it, if (n < v2 || !(n - v2)), where it was if (n <= v2).

Tests

  • tests/stages/kuna-resultflags.xml, clang -O2 x86-64 bytes:
    • The loop above, a tail call sub rdx,rcx; jne, and its 32-bit twin pass the length. All three fail on main.
    • Three controls print as on main: a loaded value tested by a separate test, a flag stored by sete, and a hand-assembled counter sub rdx,1; jne before a tail call. Without the loop check the counter would print as (*a2)(a3,a1,0).
  • kuna-calleereadarg.xml's Perf/actionpool cursor #8 pinned g3(1) for clang's g3(1, 0, x), whose add edx,2 sets the flags its je reads.
    • It now pins g3(1,a1,*a0 + 2), which AArch64's adds w2,w8,#2 already prints for the same source. The gap holds the caller's q because clang leaves esi alone.
    • The control that Perf/actionpool cursor #8 was, a gap that only the caller's incoming register would fill is no evidence, is now Fix test stages #9. It runs on a new k1t that computes x gcc's way (lea edx,[rax+2]; test edx,edx) and prints g3(2) with and without the change.
  • Unit tests in kuna_resultflags cover the flag shapes, the 32-bit PIECE heritage builds, an or, a loop counter, and a RETURN.
  • No other datatest or stage assertion moves.
  • decompile-all --jobs 1 over 1,540 functions of an -O2 x86-64 sqlite3 build changes one: unixMapfile's osMunmap(pReq + nReuse, pFd->mmapSizeActual - nReuse) gains the length it lacked. It retires 323.66 billion instructions with the change against 323.95 billion on main, the mean of three runs each, which vary by up to 0.13%.

Model: anthropic/claude-opus-5-5

[AUTOMATED] x86 SLEIGH sets an arithmetic instruction's zero flag from
the destination register itself, so when the sub that computes a call's
length also guards the call (mov rdx,r14; sub rdx,rbx; jbe; ...; call),
only_op_use walked from rdx into ZF, the BOOL_OR with CF and the
CBRANCH, and rejected the trial: read(fd, buf + got, n - got) printed
as (*g_read)(fd,&buf[v2]). AArch64 computes subs into a temporary, sets
the flags from it and copies it into x2, and kept the length.

kuna_resultflags scores these as AArch64's lifting does. While a CALL or
CALLIND argument that an add, sub, inc or dec defines is scored, the
walk skips a comparison against a constant that instruction makes, of
the value or of the view of it the instruction takes (a 32-bit sub reads
edx back out of the rdx heritage rebuilds), when its flag reaches only
CBRANCH conditions. A logical op's register is no such evidence: x86
has no flags-only or or xor, and clang tests a bit with mov ecx,eax;
and ecx,0x100000; jne. A later instruction's flags (cmp, test), a flag
kept as data, a value that goes round into its own instruction again
(a loop counter), and a RETURN's trials are scored as before.

tests/stages/kuna-resultflags.xml pins the loop, a tail call and its
32-bit twin, with a test, a sete and a loop counter as controls. kuna-calleereadarg.xml
Noelo-Lab#8 now pins g3(1,a1,*a0 + 2) for clang's add edx,2, as AArch64's adds
already printed; its old control moves to Noelo-Lab#9 on a new k1t that tests x
with a separate test instruction.

This branch has not been deployed

No deployments
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