Skip to content

8338731: MemoryLayout::offsetHandle can return a negative offset #20662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

mcimadamore
Copy link
Contributor

@mcimadamore mcimadamore commented Aug 21, 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.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8338742 to be approved

Issues

  • JDK-8338731: MemoryLayout::offsetHandle can return a negative offset (Enhancement - P3)
  • JDK-8338742: MemoryLayout::offsetHandle can return a negative offset (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/20662/head:pull/20662
$ git checkout pull/20662

Update a local copy of the PR:
$ git checkout pull/20662
$ git pull https://git.openjdk.org/jdk.git pull/20662/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 20662

View PR using the GUI difftool:
$ git pr show -t 20662

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/20662.diff

Webrev

Link to Webrev Comment

@mcimadamore
Copy link
Contributor Author

/csr needed

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 21, 2024

👋 Welcome back mcimadamore! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 21, 2024

@mcimadamore This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8338731: MemoryLayout::offsetHandle can return a negative offset

Reviewed-by: pminborg, psandoz

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 10 new commits pushed to the master branch:

  • 8e88da0: 8338041: Keyboard Navigation of JTable, Ctrl Shift RIGHT/LEFT doesn't follow native action in GTK L&F
  • 449ca2c: 8337832: Optimize datetime toString
  • b1b4cd4: 8332158: [XWayland] test/jdk/java/awt/Mouse/EnterExitEvents/ResizingFrameTest.java
  • 284c3cd: 8336299: Improve GCLocker stall diagnostics
  • 2e96f15: 8338489: Typo in MemorySegment doc
  • 44d3a68: 8314124: RISC-V: implement Base64 intrinsic - decoding
  • daf2617: 8338929: Make Metaspace::deallocate space-aware
  • fa4ff78: 8338690: CompactNumberInstance.format incorrectly formats some numbers (few vs many)
  • 1ff5f8d: 8338440: Parallel: Improve fragmentation mitigation in Full GC
  • 0f66710: 8338939: Simplify processing of hidden class names

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch.
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added rfr Pull request is ready for review csr Pull request needs approved CSR before integration labels Aug 21, 2024
@openjdk
Copy link

openjdk bot commented Aug 21, 2024

@mcimadamore has indicated that a compatibility and specification (CSR) request is needed for this pull request.

@mcimadamore please create a CSR request for issue JDK-8338731 with the correct fix version. This pull request cannot be integrated until the CSR request is approved.

@openjdk
Copy link

openjdk bot commented Aug 21, 2024

@mcimadamore The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Aug 21, 2024
@mlbridge
Copy link

mlbridge bot commented Aug 21, 2024

Webrevs

Copy link
Contributor

@minborg minborg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

@openjdk
Copy link

openjdk bot commented Aug 23, 2024

@mcimadamore this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout offset_overflow
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Aug 23, 2024
@minborg
Copy link
Contributor

minborg commented Aug 23, 2024

Unrelated: I wonder if the performance of:

MH_ADD = lookup.findStatic(Long.class, "sum",
                    MethodType.methodType(long.class, long.class, long.class));
                    

and

MH_ADD = MethodHandles.publicLookup().findStatic(Long.class, "sum",
                    MethodType.methodType(long.class, long.class, long.class));

Differ?

@liach
Copy link
Member

liach commented Aug 23, 2024

They should have the same performance characteristics. They have different permissions for access checks, and since Long is exported public API and sum is public both will pass.

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label Aug 26, 2024
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Aug 27, 2024
Copy link
Contributor

@minborg minborg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge ok ;-)

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 28, 2024
@mcimadamore
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Aug 28, 2024

Going to push as commit 1ff9ac7.
Since your change was applied there have been 11 commits pushed to the master branch:

  • 2e174c6: 8338445: jdk.internal.loader.URLClassPath may leak JarFile instance when dealing with unexpected Class-Path entry in manifest
  • 8e88da0: 8338041: Keyboard Navigation of JTable, Ctrl Shift RIGHT/LEFT doesn't follow native action in GTK L&F
  • 449ca2c: 8337832: Optimize datetime toString
  • b1b4cd4: 8332158: [XWayland] test/jdk/java/awt/Mouse/EnterExitEvents/ResizingFrameTest.java
  • 284c3cd: 8336299: Improve GCLocker stall diagnostics
  • 2e96f15: 8338489: Typo in MemorySegment doc
  • 44d3a68: 8314124: RISC-V: implement Base64 intrinsic - decoding
  • daf2617: 8338929: Make Metaspace::deallocate space-aware
  • fa4ff78: 8338690: CompactNumberInstance.format incorrectly formats some numbers (few vs many)
  • 1ff5f8d: 8338440: Parallel: Improve fragmentation mitigation in Full GC
  • ... and 1 more: https://git.openjdk.org/jdk/compare/b25095b08e4d21b95177a5fa3fff3807b2cf81e0...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 28, 2024
@openjdk openjdk bot closed this Aug 28, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 28, 2024
@openjdk
Copy link

openjdk bot commented Aug 28, 2024

@mcimadamore Pushed as commit 1ff9ac7.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants