Skip to content

[AUTOMATED] Keep x86 stack arguments a later direct stack store sits below - #1589

Open
thotypous wants to merge 1 commit into
Noelo-Lab:mainfrom
thotypous:fix/x86-direct-access-alias
Open

thotypous wants to merge 1 commit into
Noelo-Lab:mainfrom
thotypous:fix/x86-direct-access-alias

Conversation

@thotypous

Copy link
Copy Markdown
Contributor

Problem

When a function saves %esp in a frame slot around an alloca/VLA and restores it from that slot, an earlier call loses a stack argument. This independent example (clang -O0 shape: arguments stored through a copy of %esp) makes two calls, then allocates, restores %esp from the slot, and calls again:

python3 -c "from pathlib import Path; Path('/tmp/x86-directalias.bin').write_bytes(bytes.fromhex('5589e583ec188b4d0883ec1089e0894804c70001000000e8e400000083c4108b4d0883ec3089e0894804c7402008000000c70000000000e8c400000083c4308b4d0883ec10890c24e8b300000083c41089ec5dc390909090909090909090909090909090909090909090909090909090909090909090909090909090909090905589e583ec188b4d0883ec1089e0894804c70001000000e86400000083c4108b4d0883ec3089e0894804c7402008000000c70000000000e84400000083c43089e1894df48b550829d489e0c600078b45f489c48b4d0883ec10890c24e81f00000083c41089ec5dc3909090909090909090909090909090909090909090909090c3909090909090909090909090909090'))"
kuna decompile /tmp/x86-directalias.bin 0x100080 --raw-image \
  --target x86:LE:32:default:gcc --base 0x100000 --assert-strict \
  --assert 'prototype 0x100100 unsigned int sub_100100(unsigned int count,...)' \
  --assert 'prototype 0x100080 void sub_100080(unsigned int n)'

Before the fix the first call loses n:

  sub_100100(1);
  sub_100100(0,n);

Compiled and run against a sub_100100 that records its variadic arguments, the first call receives count = 1 and no second argument.

Cause

checkInputTrialUse drops a stack trial when the frame's alias boundary reaches it: some pointer formed from the stack pointer sits at or below the slot, so anything above it may be reached. Every LOAD/STORE through a stack pointer counts as such a pointer until heritage turns it into a stack variable. A stack pointer restored from the frame slot it was saved in only becomes a known constant offset a pass later, so when call inputs are scored the last call's own argument store is still a pointer store at -0x2c, and the first call's argument at -0x28 is "above" it. One pass later the store is converted and the pointer is gone, but the trial verdict is final.

Fix

p4_calls/kuna_directalias.rs: a gathered pointer that is the stack-pointer input plus constants, through copies and frame slots only, and that is used solely as the address of LOADs and STOREs, touches exactly [offset, offset + size). It aliases a trial only when those bytes overlap it. Every other pointer, including any indexed access, keeps the upstream boundary (AliasChecker::has_local_alias_among draws it from them). No option: the previous output drops an argument the program passes.

After the fix:

  sub_100100(1,n);
  sub_100100(0,n);

Tests

  • Stage test kuna-x86-directalias.xml: the same function without the allocation (unchanged control) and with it.
  • Emitted C compiled with clang for i686-linux-gnu and run under qemu-i386 against a recording variadic callee: before, the first call receives no n at -O0 and -O2; after, (1, n) at both.
  • Debian corpus (decompile-all, 113 executables, 23,142 functions across amd64, arm64, armhf, i386, ppc32, ppc64, riscv32, riscv64, sparc32 and sparc64; a second base run showed no nondeterministic function): no function changes.
  • Full public decbench corpus (770 binaries / 96,103 requested functions; 663 x86-64 ELF, 95 ARM ELF, 12 x86 PE): base and this PR recover the same 95,698 functions with the same 405 gaps; decompiled C, JSON payloads and coverage are identical, as are GED (90,305 scored, 38,454 perfect) and type-match (87,892 scored, 9,050 perfect).
  • Byte-match over all 770 binaries (native GCC, arm-none-eabi-gcc, i686-w64-mingw32-gcc; every recompiler available): no change from this PR. Each arm scores 95,698 functions, 80,402 recompilable, 15,296 not, 5,836 perfect, 0 metric errors. The default run reported 5 per-function score differences, but the decompiled C is byte-identical between arms for every binary, so they cannot come from this change. They come from the scorer's wall-clock diff timeout on large functions; re-scored with that timeout disabled, every pair has byte-identical original and recompiled machine code and equal scores.
  • CI command set: datatests 675/675, stages 4343/4343 (baseline re-recorded: adds this test's three assertions, removes none), tooling, CLI probes, workspace, kuna-ghidra including ignored, lint, spec, catalog, options and counters all pass. The WASI build step needs git history and could not run in the cluster's export.

…below

checkInputTrialUse drops a stack trial when the frame's alias boundary reaches it: some
pointer formed from the stack pointer sits at or below the slot. A LOAD/STORE through such
a pointer counts until heritage turns it into a stack variable, and a pointer whose offset
only becomes known late (a stack pointer restored from the frame slot it was saved in after
an alloca) is still one when call inputs are scored. The call after the restore stores its
own argument through it, below the earlier calls' arguments, and those were dropped
(reg(1) printed for reg(1, n)).

kuna_directalias: a pointer that is the stack-pointer input plus constants, through copies
and frame slots only, and is used solely as the address of LOADs and STOREs, aliases a trial
only when the bytes it touches overlap it (AliasChecker::has_local_alias_among draws the
boundary from the other pointers). Indexed pointers keep the upstream boundary.

Stage test kuna-x86-directalias; Debian corpus (23,142 functions, 10 architectures) and the
public decbench corpus (95,698 functions) are unchanged.

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