RFR: 8338731: MemoryLayout::offsetHandle can return a negative offset [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Aug 27 14:45:37 UTC 2024


> When working on startup improvements, I noticed that the method handle returned by `MemoryLayout::offsetHandle` can overflow if the client calls the handle with a base offset that is too big.
> 
> In other similar situations, the layout API always fails with `ArithmeticException` (see `MemoryLayout::scale`), so we should do the same here.
> 
> The fix is to use a `Math::addExact(long, long)` for the outermost add operation in the computation of the offset method handle. That outermost computation in fact is the only one that can overflow: it is an addition between a user-provided base offset `B` and a layout offset `L`. `L` is guaranteed not to overflow, by construction (as `L` is derived from a layout path). But `B` + `L` might overflow, so the new logic checks for that.

Maurizio Cimadamore has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains three commits:

 - Merge branch 'master' into offset_overflow
 - Merge branch 'master' into offset_overflow
 - Initial push

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

Changes: https://git.openjdk.org/jdk/pull/20662/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=20662&range=01
  Stats: 23 lines in 3 files changed: 14 ins; 1 del; 8 mod
  Patch: https://git.openjdk.org/jdk/pull/20662.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/20662/head:pull/20662

PR: https://git.openjdk.org/jdk/pull/20662


More information about the core-libs-dev mailing list