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

Maurizio Cimadamore mcimadamore at openjdk.java.net
Mon Feb 21 15:48:53 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.

Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:

  Update src/java.base/share/classes/java/lang/foreign/ValueLayout.java
  
  Co-authored-by: Jorn Vernee <JornVernee at users.noreply.github.com>

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

Changes:
  - all: https://git.openjdk.java.net/panama-foreign/pull/647/files
  - new: https://git.openjdk.java.net/panama-foreign/pull/647/files/007f8e9b..6edf30cd

Webrevs:
 - full: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=647&range=01
 - incr: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=647&range=00-01

  Stats: 1 line in 1 file changed: 0 ins; 0 del; 1 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