Skip to content

[AUTOMATED] fix(p2): an inline svc/ecall/syscall/sc returned the registers set up for it, not its result - #826

Merged
mahaloz merged 1 commit into
mainfrom
fix/svc-return-regs
Oct 2, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/svc-return-regs

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

The problem

kuna treats svc (ARM, AArch64), ecall (RISC-V), syscall (MIPS) and sc (PowerPC) as writing no register, so after the call a function seems to return or test whatever it put in the result register to set the call up. A Linux wrapper that moves its arguments into r0/r1 returns them as a pair:

python3 - <<'PY'
import struct
code = bytes.fromhex('80402de9 0120a0e1 0010a0e1 0200a0e1 0470a0e3 000000ef 8080bde8')
interp = b'/lib/ld-linux.so.3\0'
off = 0x90
size = off + len(code)
header = struct.pack('<16sHHIIIIIHHHHHH', b'\x7fELF\x01\x01\x01' + bytes(9), 2, 40, 1,
                     0x10000 + off, 52, 0, 0x5000200, 52, 32, 2, 0, 0, 0)
segs = struct.pack('<8I', 3, 116, 0x10000 + 116, 0x10000 + 116, len(interp), len(interp), 4, 1)
segs += struct.pack('<8I', 1, 0, 0x10000, 0x10000, size, size, 5, 0x1000)
open('svc.elf', 'wb').write((header + segs + interp).ljust(off, b'\0') + code)
PY
kuna decompile svc.elf 0x10090
unsigned long long sub_10090(unsigned int a0,unsigned int a1)
{
  software_interrupt(0);
  return CONCAT44(a0,a1);
}

With this change it is return software_interrupt(0,4,a1,a0,a1);. The same wrapper built for MIPS returns the syscall number it loaded into v0 (return 0xfa4;), and in glibc a test of the result (if (r0 < 0)) tests the argument instead.

The fix

  • New option syscallregs off|auto|on. When it acts, the CALLOTHER writes the result register (r0, x0, a0, v0, r3). It reads the number register, then the argument registers in order up to the first one the function does not write on every path to the call. A printed argument is always in its own position, and no undefined register becomes a parameter.
  • The default auto acts only on an ELF that says it is a program for an operating system: a Linux/BSD EI_OSABI, a PT_INTERP, a dynamic section naming a shared object, or an ABI-tag note. Bare-metal RTOS handlers use the same instruction for a privilege change or a yield and leave r0 alone. FreeRTOS's xPortRaisePrivilege returns the r0 = 0 it set before svc 2, and stays return !v1;. on acts on any image.
  • Return recovery treats the rewritten call as a call site, so an argument it reads is not also a returned register half.

Not fixed:

  • Relocatable objects, raw images and static musl/uClibc programs carry no OS marker, so they keep the old output unless --option syscallregs on is passed. That includes the .o in [AUTOMATED] An inline svc/ecall/sc whose arguments sit in the return registers comes back as a register-pair return #812.
  • Arguments a wrapper passes through untouched are not shown, since nothing says how many the kernel reads. The list stops at the first of them. Five glibc wrappers that pass their first argument through and write a later one (sigpending, pipe, symlink) still return a pair, now with the result as the low half.
  • The MIPS a3 and PowerPC cr0 error flags still read their pre-call values.

The tests

  • Stage tests tests/stages/kuna-syscallregs-{arm,aarch64,riscv,mips,ppc}.xml show the fix under on, and the old output under off and under auto on a bare byte chunk. kuna-syscallregs-cortexm.xml is the FreeRTOS function: unchanged by default, wrong if forced on. The ARM and RISC-V tests include a call where r1/a1 is written but r0/a0 is not, and a loop with scratch values in higher argument registers. Neither takes an argument position.
  • kuna-cli/tests/syscall_regs_cli.rs loads the ARM witness as an object with and without a GNU ABI-tag note. The default fixes it only with the note.
  • decompile-all, auto vs off, 25 stripped binaries (24,212 functions). The 12 without an OS marker are byte-identical: seven ARM firmware images, FreeRTOS RTOSDemo, p2im, a uClibc busybox and two RISC-V enclaves. So are two x86-64 Linux programs. In 10 non-x86 Linux images, 954 functions with a system call change, and 157 register pairs become single returns. 528 others change only in numbering, types or arguments taken from the wrappers they call. Datatests are unchanged (675/675).
  • Speed, decompile-all off vs auto, interleaved, min of 11 on a shared machine: ARM ld.so 24.10 s vs 24.26 s, MIPS ld.so 21.83 s vs 21.43 s, AArch64 ld.so 24.33 s vs 24.29 s.

Fixes #812

🤖 Generated with Claude Code

https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt

@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 2, 2026
@mahaloz
mahaloz force-pushed the fix/svc-return-regs branch from 29bfe1b to 2e65cb9 Compare October 2, 2026 20:22
…sters set up for it, not its result

New option syscallregs off|auto|on (default auto: only on an ELF the loader proves is OS user-space).
When it acts, the system-call user-op writes the ABI result register and reads the number register
plus the argument registers written on every path to the call.

Fixes #812

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz force-pushed the fix/svc-return-regs branch from 276d7ee to f6e0adf Compare October 2, 2026 22:14
@mahaloz
mahaloz merged commit 72d2e9b into main Oct 2, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/svc-return-regs branch October 2, 2026 22:31
mahaloz added a commit that referenced this pull request Oct 2, 2026
Both #826 and this branch moved the settable counts from 250 to 251, which
merged as one bump. Re-derived on a fresh build: 252 settables.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
mahaloz added a commit to phix33/kuna that referenced this pull request Oct 2, 2026
Brings in Noelo-Lab#826 (syscallregs). Counts re-derived on a fresh build:
252 settables, tiers 81/105/66, 251 catalog rows, corpus 388; the
stages baseline is main's plus the three STACK STORE keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
mahaloz added a commit that referenced this pull request Oct 3, 2026
Both #826 and this branch moved the settable counts from 250 to 251, which
merged as one bump. Re-derived on a fresh build: 252 settables.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AUTOMATED] An inline svc/ecall/sc whose arguments sit in the return registers comes back as a register-pair return

1 participant