[foreign-preview] RFR: 8282069: Set correct alignment constraints on ValueLayout constants

Maurizio Cimadamore mcimadamore at openjdk.java.net
Fri Feb 18 18:22:37 UTC 2022


Now that we have a good understanding of how to enforce alignment checks for both off-heap _and_ heap segments, we are in a position to set the correct alignment constraints on the various `ValueLayout.JAVA_XYZ` constants.

The source changes are quite trivial; fixing the tests less so. The main issue is that some of the test are quite liberal in moving things in and out of `byte[]`. E.g. they create a `byte[]` and want to store a `JAVA_LONG` in it. If `JAVA_LONG` has the correct alignment (8 bytes) there's no way we can guarantee correctness and the code now fails (correctly).

Also, we now proprerly check alignment on bulk copy operations too - so bulk copy tests, which were ok before, now fail when correct alignment constraints are applied.

I've fixed some of the tests in simple ways - e.g. by using a `long[]` instead of a `byte[]`. But in some of the most complex tests I had to define unaligned layouts, as some of the tests are peeking and poking at memory in ways that is not compatible with aligned access (the byte buffer test is a good one, as byte buffer access is not aligned).

Also, I had to tweak some code in the `BindingSpecializer` class - some of the bindings can sometimes read multiple fields in a single shot - so e.g. if you have a struct like `[f32 f32]` a single `long` read is performed to read both fields. This means that we do a `JAVA_LONG` read, but with an underlying memory alignment of 4, which is not going to work in some cases (especially in upcalls, where we use an arena allocator to allocate temp buffers; the arena allocator can "pack" structs more than just using `malloc`). For this reason I had to change that code to use unaligned layouts as well.

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

Commit messages:
 - Fix spliterator test
 - Initial push

Changes: https://git.openjdk.java.net/panama-foreign/pull/647/files
 Webrev: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=647&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8282069
  Stats: 162 lines in 9 files changed: 41 ins; 20 del; 101 mod
  Patch: https://git.openjdk.java.net/panama-foreign/pull/647.diff
  Fetch: git fetch https://git.openjdk.java.net/panama-foreign pull/647/head:pull/647

PR: https://git.openjdk.java.net/panama-foreign/pull/647


More information about the panama-dev mailing list