[foreign-jextract] RFR: 8261578: jextract crashes with Crossing storage unit boundaries

Maurizio Cimadamore mcimadamore at openjdk.java.net
Mon Feb 15 12:24:03 UTC 2021


This patch overhauls the logic for bitfield detection, after discovering a number of related failures. The current logic attempts to find a unique container size for a bunch of adjacent bitfields (either 8, 16, 32 or 64 bits) and then allocates the bitfields into subsequent containers of same size.

The fact that the container size is decided once and for all at the beginning of parsing of the bitfield sequence is problematic, and breaks down in cases where containers are used only partially, or where padding is inserted.

To overcome these issues, I've now switched to a more flexible algorithm which, instead of fixing a container size before hands, just emits a new container whenever the cumulative offset of adjacent bitfield has reached a word boundary (either 8, 16, 32, 64 bits).

This makes the algorithm less strict, and less prone to crashes. However, since we're still reverse engineering bitfield information (as libclang doesn't give us that information), the solution will probably require more tweaks, especially in order to support packed structures, in which compilers emit bitfields which happily span across a word boundary.

For this reason, I did not included some of the examples listed in JDK-8261578 which make use of the `packed` attribute/pragma - that is unsupported anyway, see JDK-8259832. We should come back to these examples when support for pragma is added (for now I've added a link from the pragma JBS issue to this one, so that we'll not forget).

As for testing, I followed what other tests in this area did in the past - but I believe at some point we probably need to write better tests which use the jextract API to query bitfield offsets and sizes, because the tests, as currently written, do not assert much, other than that jextract is not failing badly.

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

Commit messages:
 - Merge branch 'foreign-jextract' into bitfields_flexible
 - Tweak bitfield processing logic to be more flexible

Changes: https://git.openjdk.java.net/panama-foreign/pull/451/files
 Webrev: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=451&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8261578
  Stats: 247 lines in 6 files changed: 213 ins; 22 del; 12 mod
  Patch: https://git.openjdk.java.net/panama-foreign/pull/451.diff
  Fetch: git fetch https://git.openjdk.java.net/panama-foreign pull/451/head:pull/451

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


More information about the panama-dev mailing list