Files
rea/scripts
Entrpiandmorluto d8aadcc69b feat(ghidra): admit RISC-V ilp32f ELF with a derived calling convention (#8401)
* feat(ghidra): record the RISC-V float-ABI calling-convention decision

Decision D3 (#7477), provisional: admit ELF32 soft-float (ilp32) RISC-V
images through a lane that keeps the pinned RISCV:LE:32:default language and
corrects the calling convention in the bridge.

This commit is the contract only; admission is unchanged.

- The domain owns the e_flags float-ABI vocabulary and parses
  .riscv.attributes and Tag_RISCV_arch into a declared, absent or
  unavailable ISA declaration.
- The Ghidra profile owns the lane table (class and float ABI), lane ids
  built from fields, the ilp32-prototype-v1 policy and its __rea_ilp32
  prototype, and the extensions Ghidra 12.1.4 decodes with p-code.
- The session handshake accepts the bridge's calling-convention report.
- docs/ghidra-riscv.md keeps the decision record and per-ABI policies for
  ilp32 and ilp32f; a test keeps it in step with the tables.
- tests/conformance/c/riscv-float-abi.c is the source the policies were
  measured on.

* feat(ghidra): admit RISC-V ilp32 ELF with a derived calling convention

ESP32-C2, C3, C5, C6, C61, H2 and H21 builds are ELF32 soft-float (ilp32)
images, which the RISC-V lane refused at its double-float check. Ghidra
12.1.4's generic RV32 compiler specification models ilp32d, so its stock
prototype would read float and double arguments from fa0-fa7.

- Target resolution reads the one SHT_RISCV_ATTRIBUTES section through the
  same bounded ELF declaration reader as range validation, and records
  Tag_RISCV_arch as declared, absent or unavailable.
- Admission selects a lane from the class and float-ABI table. The soft-float
  lane also requires an RV32I Tag_RISCV_arch that declares only extensions
  Ghidra decodes with p-code; the refusal names any others (Zcb, Zcmp, Zcmt,
  Zb*, xesp*).
- Soft-float profiles commit float_abi, arch, riscv_calling_convention
  ilp32-prototype-v1 and the lane elf32-exec-le-soft-float-generic-v1.
  Double-float profiles are unchanged.
- ReaGhidraRiscvCallingConvention.java runs after analysis. It derives
  __rea_ilp32 from the default prototype without floating-point entries and
  rules, with fs0-fs11 killed by calls, self-checks its storage and assigns
  it to every non-thunk function, external function and function
  definition. Its report is written last.
- The provider client requires that report exactly when the profile commits
  the policy, before any operation, and keeps it as session limitations and
  caller-project history.

ELF32 single-float (ilp32f), quad-float and ELF64 soft- or single-float
images stay refused, now with a reason built from the lane table.

* test(ghidra): verify RISC-V ilp32 against real Ghidra

verify:ghidra:riscv now also builds tests/conformance/c/riscv-float-abi.c
with ESP32-C3's -march=rv32imc_zicsr_zifencei -mabi=ilp32, typed with DWARF
and linked again with --strip-all. Every allocated section of the stripped
image must keep its address and bytes.

- riscv-checks.mjs holds the ilp32 storage of each fixture function together
  with the words the compiler's call site writes there. An LLVM objdump
  reading of the caller must find every word at its stated location: an
  unaligned named double, the a7/stack split, two floats in an integer pair,
  a large structure by reference, aligned variadic pairs and an indirect
  call.
- Through CLI and stdio MCP, the soft-float builds must report the soft-float
  lane and ilp32-prototype-v1, __rea_ilp32 with exactly that storage (one
  integer word per location when stripped), caller pseudocode that passes the
  source constants and names no floating-point register, the prototype
  outcome in retained Evidence, and decoded fence.i and csrr.
- The RV32 double-float image relabelled as soft-float, refused before, is
  admitted through the new lane. RV64 soft-float, single-float and unsupported
  bits are refused with their reasons.
- riscv-checks.test.ts runs the checks offline against recorded LLVM and
  Ghidra output, including Ghidra's stock ilp32d prototypes as a negative
  control.

* fix(ghidra): pass RISC-V ilp32 values over 8 bytes by reference

The psABI passes any value wider than 2 x XLEN by reference, scalars
included. __rea_ilp32 converted only structures and unions over 8 bytes to
pointers, so a long double (16 bytes) was joined into a1-a4. Measured on
Ghidra 12.1.4, rea_riscv_abi_quad(long double value, int sel) read value
from a4:4,a3:4,a2:4,a1:4 and sel from a5, where clang passes a pointer in
a1 and sel in a2.

- ReaGhidraRiscvCallingConvention.java now has one derivation driven by a
  per-policy row (floating-point entry size, preserved floating-point
  registers). Every policy inserts a rule before the input list's final
  join that converts any value of 9 bytes or more to a pointer; a return
  that size already becomes a caller buffer. The self-check requires a
  16-byte value to pass by reference.
- The fixture gains rea_riscv_abi_quad. The call-site oracle now checks the
  words the by-reference pointers point at, for the large structure and the
  long double, and the recorded data comes from an LLVM 19 build.
- docs/ghidra-riscv.md: the decision record and the ilp32 policy row.

ilp32-prototype-v1 keeps its id: no release carries it yet.

* feat(ghidra): record the RISC-V ilp32f calling-convention decision

Decision D3 (#7477), provisional, gains its ilp32f row: ELF32 images that
declare the single-float ABI are admitted through the lane
elf32-exec-le-single-float-generic-v1 with policy ilp32f-prototype-v1 and
the __rea_ilp32f prototype, keeping the pinned RISCV:LE:32:default language.

- The lane table gains the row, and admission requires the F extension in
  Tag_RISCV_arch beside the decoded-extension check.
- docs/ghidra-riscv.md records how the one-float-one-integer rule is
  realised: the shared derivation's row limits float entries to 4 bytes,
  keeps fs0-fs11 preserved and adds Ghidra 12.1.4's join_dual_class with
  fillalternate for structures of up to 8 bytes. The prototype is the only
  thing that assigns storage: a check of every typed function and
  function-pointer type against the psABI refuses the image when one uses a
  shape the prototype misplaces (a float with two shorts, a pointer or a
  union, _Float16, packed or over-aligned float structures). float _Complex,
  which Ghidra imports as a qword, stays unmodelled. Custom storage for typed
  functions and an unverified-signature admission are recorded alternatives.
- The session report for ilp32f can carry the bridge's refusal instead of
  the outcome; the handshake turns it into the user-facing reason.
- tests/conformance/c/riscv-float-abi.c gains single-float-only shapes and a
  refused variant; the ilp32 build's code is unchanged.

Until the bridge implements the policy (next commit), its post-analysis
script rejects the new policy id and REA refuses ilp32f sessions.

* feat(ghidra): apply the RISC-V ilp32f calling convention in the bridge

ReaGhidraRiscvCallingConvention.java gains ilp32f-prototype-v1 as a row of
its one derivation: float entries limited to 4 bytes, fs0-fs11 preserved,
and structures that Ghidra's dual-class join splits between register files.
The ilp32 row and its checks are unchanged.

- For that row the derivation inserts, before the input and output lists'
  final join, a rule giving structures of up to 8 bytes join_dual_class
  with fillalternate. The shared rule still passes values of 9 bytes or
  more by reference.
- The self-check also pins the storage of a float, double, two-float and
  float-and-int signature, and requires fs0-fs11 preserved.
- After assigning the prototype to every function and function definition,
  the script compares the storage class it gives each signature type, as a
  first argument or return, with the psABI's from the flattened fields. It
  checks every typed function and every definition a pointer type refers
  to. When any signature uses a shape the prototype misplaces, the report
  carries a refusal naming up to eight signatures instead of the outcome,
  and REA refuses the session with that reason.

The launcher test covers both policy ids.

* test(ghidra): verify RISC-V ilp32f against real Ghidra

verify:ghidra:riscv now also builds tests/conformance/c/riscv-float-abi.c
with ESP32-H4's -march=rv32imafc_zicsr_zifencei -mabi=ilp32f, typed with
DWARF and linked again with --strip-all, and a typed build with
REA_RISCV_ABI_UNMODELLED that the bridge must refuse.

- riscv-checks.mjs holds per-ABI tables: the ilp32 and ilp32f storage of
  each fixture function with the words its caller's call site writes there
  (and, for by-reference values, the words the pointer points at), the
  callers, and the indirect calls. Stripped builds record where Ghidra's
  inference differs from one word per typed location.
- The objdump call-site oracle now follows fa registers (flw, fsw, fmv.w.x)
  and tracks stack memory per byte, so word loads see byte and halfword
  stores, across both callers.
- Through CLI and stdio MCP the single-float builds must report the lane,
  ilp32f-prototype-v1 and __rea_ilp32f with the table's storage, including
  float-and-integer structures both ways round, float registers running
  out, long double by reference and an indirect float-and-integer call.
- riscv-isa-probe.S adds fmv.w.x and fmv.x.w when the build has F.
- The unmodelled build opens, then its first analysis call over MCP and the
  CLI fails with the bridge's reason naming its three signatures: a
  function passing a three-field structure, a _Float16 function and a
  function-pointer type passing the three-field structure.
- Header mutations: RV32 double-float relabelled single-float is admitted;
  the soft-float image relabelled single-float lacks F and is refused; RV64
  lp64f and high bits are refused.

* test(ghidra): check RISC-V lane limitations by their facts

The limitation tests froze a full explanatory sentence, the note count and
array positions, so a harmless rewording failed them: the pattern Greptile
flagged on #7486. Assert the facts the notes must carry instead: the pinned
language, the static-linkage boundary, one note naming the declared ABI,
the ABI the pinned language models and the prototype REA applies, and the
declared ISA.

* test(ghidra): check the ilp32f limitations by their facts

Like the ilp32 tests, the ilp32f test froze its explanatory sentence, the
note count and array positions. Assert one note naming the declared ilp32f
ABI, the modelled ilp32d and the applied `__rea_ilp32f` prototype, and the
declared ISA.

* test(ghidra): check return storage in stripped RISC-V ilp32 builds

The stripped branch of assertRiscvIlp32Storage compared parameter storage
only, so an inferred return that lost the high word of a register pair,
such as rea_riscv_abi_wide at a0:4 instead of a1:4,a0:4, still passed.

Stripped returns must now use the typed build's storage, or none when the
result goes through caller memory. Recovered type names are still not
compared: a test accepts other type names and refuses the dropped high word.

* docs(ghidra): record decision D3 as decided in #7477

* fix(ghidra): flatten RISC-V arrays and bit-fields as the psABI does

The ilp32f signature check flattened a by-value type differently from
GCC and Clang in two shapes. A nonzero array of empty C++ classes added
no fields, so struct { E e[1]; char c; float f; } looked like a char and
a float, and the policy admitted a float-and-integer split for a value
both compilers pass in two integer registers. A bit-field was bounded by
its declared type, so a float beside unsigned long long b : 1 used the
integer convention, and the image was refused although __rea_ilp32f
places it as the compilers do. An array element with no fields now makes
the array use the integer convention, and a bit-field counts by its
width.

Arrays are also classified once per element type, so an array of empty
structures no longer costs one visit per element, and a refusal keeps
its counts and at most eight shortened examples instead of every
description.

The unmodelled build gains a C++ source with the empty-class array,
which the refusal must name, and a bit-field function it must not.

* fix(ghidra): refuse flexible array members in ilp32f signatures

A float beside a flexible array member flattened to a single float, so
the policy admitted fa0 for a value Clang passes in a0. Ghidra imports
the member as a trailing array without elements, which the walk skipped
as a zero-length member. A trailing array without elements now makes the
value use the integer convention. Ghidra imports a trailing [0] the same
way, and Clang passes that in fa0, so such a structure fails closed too.
The unmodelled build gains a flexible-array function the refusal must
name.

* docs(ghidra): state that hidden-reference C++ classes are not modelled

Compilers pass a C++ class with a non-trivial copy constructor or
destructor through a hidden pointer, but Ghidra 12.1.4 keeps only its
fields, and GCC 14.2 writes no DW_AT_calling_convention, so the ilp32f
check cannot detect such a signature. Sessions now state this as a
limitation, and the guide lists it as not modelled.

* fix(ghidra): refuse ilp32f shapes and D hardware the prototype misplaces

A flexible array that starts inside a structure's padding passed the
offset check, so its float took fa0 where GCC and Clang use a0. The
position among the members now decides. A bare array in a signature can
only be a GNU vector, which uses the integer convention, so the policy
stops flattening it.

With D the ABI preserves only the low 32 bits of fs0-fs11 and Ghidra's
model preserves all 64, so the lane refuses d and zcd.

Ghidra's import erases float _Complex, a vector inside a structure and
_Atomic, so ilp32f sessions now state those limits.

* fix(ghidra): refuse ilp32f values Ghidra left untyped

Ghidra's DWARF import gives an encoding it does not map an untyped
byte, word, dword or qword, so float _Complex, which takes two float
registers, showed as an integer in a0 and a1. The policy cannot know
such a value's class, so it now refuses any value of up to 8 bytes that
holds one, alone or inside a structure. The session limitation no
longer lists complex values.

* docs(ghidra): match the RISC-V verification notes to the lane

* fix(ghidra): refuse padded untyped RISC-V values

An over-aligned structure of one float _Complex is 16 bytes, so the
8-byte guard let it fall through to a reference, although GCC and Clang
pass it in fa0 and fa1. Decide on the bytes the fields hold, which is
what the float convention limits, not on the padded length.

* fix(ghidra): walk each RISC-V field type once

The held-bytes and untyped checks revisited a nested type for every
member, so a chain of structures that each hold two of the last one
cost about 2^29 calls before any size check. One cached walk now
answers both, once per type.

* fix(ghidra): refuse ilp32f values whose bytes may hide a bit-field

DWARF omits unnamed bit-fields of nonzero width, which the psABI counts:
Clang 19 passes struct { float f; unsigned : 1; } in fa0 and a0, and a
following int in a1, while Ghidra sees one float in 8 bytes. A value
with a float whose structure has bytes that no member and no natural
alignment padding explain now has an unknown class, so the policy
refuses the image. Over-aligned structures of up to 8 bytes are refused
too. verify:ghidra:riscv's unmodelled build passes such a structure.

* perf(ghidra): cache the ilp32f layout checks per type, document padding

unexplainedBytes and naturalAlignment recomputed nested types on every
chain, so a deeper nesting cost quadratically more. Both now keep one
result per stripped DataType, as held() does.

The guide now states that an unnamed bit-field in natural padding, as in
struct { char c; unsigned : 1; float f; }, leaves no DWARF trace: Clang 19
passes it in a0 and a1 but the lane admits it with the float in fa0.
Refusing every padded float value would also refuse common layouts.

* fix(ghidra): refuse ilp32f union parameters that may be transparent

A transparent union passes as its first member, but DWARF does not record
the attribute: Clang 19 passes union { struct { float f; } s; unsigned u; }
with transparent_union in fa0, while Ghidra and the psABI class see an
ordinary union in a0, so the lane admitted wrong storage. A union parameter
whose first member would take a float register now refuses the policy,
unless Clang would ignore the attribute: a floating-point first member, or
another member of a different size or alignment. Returns are unaffected.
verify:ghidra:riscv's unmodelled build passes one such union and three
ordinary ones.

* fix(ghidra): refuse ilp32f float parameters of old-style C definitions

Callers of an old-style (K&R) definition such as float f(x) float x;
promote the argument to double: Clang 19 passes it in integer registers
and narrows it with __truncdfsf2. DWARF still declares a float and only
omits DW_AT_prototyped, which Ghidra 12.1.4's DWARF import drops, so the
lane placed it in fa0. When a typed signature has a float parameter, the
script now reads DWARF again for C subprograms without DW_AT_prototyped
and refuses the policy for their float parameters. C++ and other
languages never mark the attribute and are not checked.
verify:ghidra:riscv's unmodelled build passes one such definition.

* fix(ghidra): admit a direct C++ empty-class member in an ilp32f structure

Ghidra imports a C++ empty class as a one-byte structure without
components, so the unexplained-bytes check counted its byte as an omitted
field. A structure such as `struct S { E e; char c; float f; }` was then
UNKNOWN and refused the whole ilp32f image, although GCC 14.2 (ESP-IDF)
and Clang 21.1.8 ignore the member when flattening and pass S in a0 and
fa0, the storage the derived float-and-integer rule gives it.

The check now treats a one-byte structure without components as that
storage. A nonzero array of empty classes still uses the integer
convention and stays refused. The unmodelled fixture gains
rea_riscv_abi_member, which the real-Ghidra verifier requires to be
admitted, and the guide names the shape.

* fix(ghidra): refuse RISC-V functions compiled from a language without the C psABI

Both RISC-V calling-convention policies apply a C psABI prototype to every
function, so code from a language with another calling convention, such as
Rust's default ABI, was silently given the wrong storage. Before either
policy installs its prototype, the bridge now reads the image's DWARF once
and refuses the image when a compile unit outside C, C++ and assembly, or
one without DW_AT_language, defines a function with code, naming each
language. The same pass collects the old-style C definitions the ilp32f
check needs, which were read lazily before. This also changes main's ilp32
policy, which had no refusal path. Code that no DWARF compile unit
describes cannot be checked, and every session that applies a policy now
states that limitation.

* test(verify): refuse RISC-V fixtures whose C compile units are labelled Rust

The real RISC-V lane rewrites the DW_AT_language of each C compile unit in
the typed soft- and single-float builds to Rust, with a bounds-checked
reader of the units' abbreviations, and checks that each policy refuses the
image and names the language. The unlabelled builds are the controls.

* fix(ghidra): refuse ilp32f values whose register fields do not start their parts

The psABI puts each flattened field of a structure passed in float
registers at bit 0 of its own register, while __rea_ilp32f's dual-class
join gives each register a 4-byte part of the value as laid out in memory.
Both agree only when every such field starts its part. A C++ empty member
keeps its byte although the psABI ignores it as a field, so the char of
struct { E e; char c; float f; } passes in bits 0-7 of a0 while the
prototype reads bits 8-15; the policy compared only storage classes, which
match, and admitted it. Flattening now records each field's bit position,
and a value whose float-register fields do not each start their own part
is refused, which also covers packed layouts and bit-fields above bit 0.
The unmodelled fixture's caller now shows where the compiler puts that char.

* docs(ghidra): keep RISC-V guide focused on ABI rules and compatibility

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-24 17:02:19 +00:00
..