Skip to content

[AUTOMATED] Define fixed signed ARM shifts in emitted C - #1013

Merged
mahaloz merged 4 commits into
Noelo-Lab:mainfrom
mgostIH:fix/signed-left-shift-upstream
Oct 12, 2026
Merged

mahaloz merged 4 commits into
Noelo-Lab:mainfrom
mgostIH:fix/signed-left-shift-upstream

Conversation

@mgostIH

@mgostIH mgostIH commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

A fixed ARM LSL #2 on a signed parameter emits a C11 shift that is undefined for negative values and positive overflow. Converting the result afterward does not help:

printf '\000\001\240\341\036\377\057\341' > /tmp/lsl.bin
printf 'function 0x10000-0x10008=ShiftLeftTwo\nprototype 0x10000 unsigned int ShiftLeftTwo(int value)\n' > /tmp/lsl.kuna
kuna decompile-all /tmp/lsl.bin --raw-image --base 0x10000 --entry 0x10000 \
  --target ARM:LE:32:v5t:default --isa arm --mode reliable --jobs 1 \
  --assert @/tmp/lsl.kuna --assert-strict

The unpatched output is:

uint4 ShiftLeftTwo(int4 value)
{
  return value << 2;
}

Fix

  • Convert signed operands before fixed-width ARM left shifts and restore signed results where later operations need signed semantics.
  • Evaluate each operand once, and leave unsigned shifts and variable-count guards unchanged.

Tests

  • arm_constant_shift:: passes. Its authored probes compile and run the emitted C with GCC and Clang at -O0 and -O2, including UBSan.

mgostIH and others added 4 commits October 11, 2026 10:54
…constshift)

The conversion makes the shift defined C, but it rewrites the sign-bit test
firmware uses for nearly every register flag check: `x << 0x18 < 0` becomes
`(int)((unsigned int)x << 0x18) < 0`. On three libopencm3 examples it changed
11 functions, each only by that cast pair. Keep the shorter spelling by default
and make the defined form an explicit per-run choice:

- New P9 option `armconstshift` (default off, not in the aggressive preset).
- The stage test is two-pass: off keeps the shift as written, on pins the casts.
- The CLI round-trip test opts in; the interwork and soft-float expectations
  return to main's text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011d7uHgQPRpFwdybmGwWHtw
@mahaloz
mahaloz merged commit b52d9fa into Noelo-Lab:main Oct 12, 2026
2 checks passed
@mahaloz

mahaloz commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Merged with one change on top (39fb862): the conversion now runs only under the new option armconstshift (default off, not in the aggressive preset).

Firmware tests nearly every register flag with a sign-bit shift, so by default it rewrote x << 0x18 < 0 as (int)((unsigned int)x << 0x18) < 0 throughout: 11 functions on three libopencm3 examples changed only by that cast pair. The defined form stays available with --option armconstshift on, and the stage test is now two-pass (off keeps the shift as written, on pins the conversion).

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.

2 participants