[foreign-memaccess+abi] RFR: 8303017: Passing by-value structs whose size is not power of 2 doesn't work on all platforms

Jorn Vernee jvernee at openjdk.org
Thu Mar 2 17:40:19 UTC 2023


On Fri, 24 Feb 2023 21:08:50 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

> Fix passing of by-value structs whose size is not a power of 2.
> 
> The issue is that currently we try to do loads using a ValueLayout that is the size of the nearest power of two that can fit the struct (or part of it, if it is passed in multiple registers). For instance, for a struct of size 6, we try to load its value using `ValueLayout.OfLong`, which is size 8, and thus produce an out of bounds error. A similar issue applies to writes.
> 
> For the solution I've implemented in this patch, I've attached an explicit byte width to BufferLoads/Stores, which indicates the size of the value we want to load/store. The type that is produced by the binding is still the same. For example, loading a struct of size 6 is implemented as an `int` load and a `short` load, which are then combined into a `long`, instead of attempting to do a single `long` load (and failing). This allows us to avoid doing an out of bounds access.
> 
> I've added a new test that tests a bunch of structs with varying byte sizes being passed in registers and on the stack. Using a nested `char[]` to precisely tweak the byte size of each struct.

I've address review comments (https://github.com/openjdk/panama-foreign/pull/806/commits/b1ccd916913065d2a146b0d3c328cdc4401546c2)

I don't think there's a good way to test the BE behavior on LE platforms, unfortunately. It also made me realize that our linkers are porbably tied to a particular endianess, and we should/could check that the layouts found in a FunctionDescriptor have the expected byte order as well.

-------------

PR: https://git.openjdk.org/panama-foreign/pull/806


More information about the panama-dev mailing list