Skip to content

Pass unions by value with the C ABI's register classes - #10

Draft
aminmansuri wants to merge 10 commits into
masterfrom
fix-union-by-value-abi-master
Draft

aminmansuri wants to merge 10 commits into
masterfrom
fix-union-by-value-abi-master

Conversation

@aminmansuri

Copy link
Copy Markdown
Owner

Makes JRuby pass C unions by value the way a C compiler does.

Before, every 8-byte union went in an integer register, so a union of floats came back as garbage and a callback receiving one crashed the JVM.

Now the union's members decide the register class, on x86_64 and ARM64.

union_spec.rb and UnionTest.c match the ffi gem's March 2026 versions
without its JRuby skips; union_by_value_spec.rb adds mixed-class,
nested, oversize and callback shapes.
Pick the libffi filler from the union's leaves: the float type for a
homogeneous floating aggregate, per-eightbyte SSE/INTEGER cells on
SysV x86_64, an integer of the union's alignment otherwise.
A 4-aligned union whose cells differ in class, nested at offset 4, takes
the enclosing struct's eightbytes: (INTEGER, SSE) on SysV x86_64.
Argument, return and callback forms.
libffi merges the cells into eightbytes at the union's offset inside an
enclosing struct; a per-cell class is exact there and unchanged for a
union passed on its own.
No behaviour change: the one-argument newUnion hands the host platform
to the overload, which tests can call with any CPU and OS.
A union of one floating type is a floating-point aggregate on AArch64,
ARM, PPC64 ELFv2 and x86_64; riscv64, loongarch64 and s390x pass every
union in integer registers, so the integer filler must stay there.
riscv64, loongarch64 and s390x pass unions in integer registers while
libffi would put a struct of doubles into FP registers; keep the integer
filler there and the float one on AArch64, ARM, PPC64 ELFv2 and x86_64.
JRuby's FFI rejects :long_double layout members, so no union has a long
double leaf or an alignment of 16; the integer filler list is now what its name says.
master names riscv64 as a CPU of its own (jruby-10.0 reports it as UNKNOWN);
like s390x and loongarch64 it must keep the integer filler.
A failing assertion printed the jffi Type objects by identity; it now
prints the cell names, so a wrong filler reads as SINT64 versus DOUBLE.
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