Skip to content

8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()#25324

Closed
minborg wants to merge 14 commits into
openjdk:masterfrom
minborg:jdk-segment-access
Closed

8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()#25324
minborg wants to merge 14 commits into
openjdk:masterfrom
minborg:jdk-segment-access

Conversation

@minborg
Copy link
Copy Markdown
Contributor

@minborg minborg commented May 20, 2025

This PR proposes to use JavaNioAccess::getBufferAdress rather than DirectBuffer::address so that Buffer instances backed by MemorySegment instances can be used in classes that were not covered by #25321

This PR passes tier1, tier2, and tier3 tests on multiple platforms and configurations.


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

Issue

  • JDK-8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address() (Bug - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25324

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link
Copy Markdown

bridgekeeper Bot commented May 20, 2025

👋 Welcome back pminborg! 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
Copy Markdown

openjdk Bot commented May 20, 2025

@minborg 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:

8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()

Reviewed-by: alanb, valeriep

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 1 new commit pushed to the master branch:

  • 72a3022: 8357842: PandocFilter misses copyright header

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
Copy link
Copy Markdown

openjdk Bot commented May 20, 2025

@minborg The following labels will be automatically applied to this pull request:

  • net
  • nio
  • security

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

@openjdk openjdk Bot added security security-dev@openjdk.org nio nio-dev@openjdk.org net net-dev@openjdk.org labels May 20, 2025
}

boolean pending = false;
NIO_ACCESS.acquireSession(buf);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here, we acquire the session after we have obtained the address. This is safe as we do not touch the segment before it is acquired. If a segment is deallocated before we try to acquire the session, an exception will be thrown.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there documentation on when sessions should be acquired/released? Is this only for when using MemorySegment? In security area, the bytes are passed to the JNI code which calls the native library to process the bytes and I wonder if sessions should be acquired/released for these.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Never mind, I see that there are already acquireSession/releaseSession calls when the ByteBuffer objects are accessed.

@minborg minborg marked this pull request as ready for review May 20, 2025 12:01
@openjdk openjdk Bot added the rfr Pull request is ready for review label May 20, 2025
@mlbridge
Copy link
Copy Markdown

mlbridge Bot commented May 20, 2025

@AlanBateman
Copy link
Copy Markdown
Contributor

We need to audit the tests for the APIs that take a byte buffer to ensure that we have tests to exercise these APIs with buffers that are views on a memory segment. That would help identify any bugs.

The temporary buffer cache is internal so I think better to separate from this JBS issue and PR as these cases cannot be views on a memory segment. Future work to re-implement the temporary buffer cache can re-visit this.

@AlanBateman
Copy link
Copy Markdown
Contributor

AlanBateman commented May 20, 2025

A question for folks on security-dev. Are there tests for Cipher.doFinal, CipherSpi.engineUpdate, etc. that exercises cases where the ByteBuffer obtained from a memory segment?

Comment thread src/java.base/share/classes/sun/nio/ch/Util.java Outdated
ioCache.remove(overlapped);

} finally {
NIO_ACCESS.releaseSession(buf);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I thin the change to WindowsAsynchronousFileChannel will need more than one reviewer as the I/O operation may complete on a different thread to the one that it was initiated on.

Comment thread src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Digest.java Outdated
Comment thread src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java Outdated
@minborg
Copy link
Copy Markdown
Contributor Author

minborg commented May 20, 2025

/reviewers 2

@openjdk
Copy link
Copy Markdown

openjdk Bot commented May 20, 2025

@minborg
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

Comment thread src/java.base/share/classes/com/sun/crypto/provider/GaloisCounterMode.java Outdated
Comment thread src/java.base/share/classes/com/sun/crypto/provider/GaloisCounterMode.java Outdated
Comment thread test/jdk/java/nio/channels/AsynchronousFileChannel/Basic.java
Comment thread src/java.base/share/classes/sun/nio/ch/Util.java Outdated
Comment thread src/java.base/unix/classes/sun/nio/fs/UnixUserDefinedFileAttributeView.java Outdated
@valeriepeng
Copy link
Copy Markdown
Contributor

A question for folks on security-dev. Are there tests for Cipher.doFinal, CipherSpi.engineUpdate, etc. that exercises cases where the ByteBuffer obtained from a memory segment?

I don't find any. We'd have to update them to cover the memory segment usage. Judging from the changed sources, general Cipher and SunPKCS11-specific tests update would be needed.

@AlanBateman
Copy link
Copy Markdown
Contributor

A question for folks on security-dev. Are there tests for Cipher.doFinal, CipherSpi.engineUpdate, etc. that exercises cases where the ByteBuffer obtained from a memory segment?

I don't find any. We'd have to update them to cover the memory segment usage. Judging from the changed sources, general Cipher and SunPKCS11-specific tests update would be needed.

Thanks for checking. We need to create an issue in JBS for this. It may be a corner case to use these APIs and SPI with buffers that are views on a memory segment but it will happen at some point and would be good to ensure that it is covered by tests.

@minborg
Copy link
Copy Markdown
Contributor Author

minborg commented May 21, 2025

A question for folks on security-dev. Are there tests for Cipher.doFinal, CipherSpi.engineUpdate, etc. that exercises cases where the ByteBuffer obtained from a memory segment?

I don't find any. We'd have to update them to cover the memory segment usage. Judging from the changed sources, general Cipher and SunPKCS11-specific tests update would be needed.

Thanks for checking. We need to create an issue in JBS for this. It may be a corner case to use these APIs and SPI with buffers that are views on a memory segment but it will happen at some point and would be good to ensure that it is covered by tests.

I've created https://bugs.openjdk.org/browse/JDK-8357466. There are some missing fields, I hope someone can fill them in.

Copy link
Copy Markdown
Contributor

@valeriepeng valeriepeng left a comment

Choose a reason for hiding this comment

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

Security source changes look fine.

Comment thread src/java.base/unix/classes/sun/nio/fs/UnixUserDefinedFileAttributeView.java Outdated
Comment thread src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java Outdated
}

boolean pending = false;
IOUtil.acquireScope(buf, true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would you mind checking the use of acquireScope in WindowsAsynchronousSocketChannelImpl? From a quick look I'm wondering why it doesn't call the 2-arg acquireScope with async=true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

WASCI is using acquireScopes (plural) and this delegates to aquireScope(.., async=true). So, looks good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good, thanks for checking.

Comment thread test/jdk/java/nio/channels/AsynchronousFileChannel/Basic.java
ioCache.remove(overlapped);

} finally {
IOUtil.releaseScope(buf);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we can release here when there is an I/O pending. I suspect it will need to go into releaseBufferIfSubstituted.

TBH, I think the change to Windows implementation of AsynchronousFileChannel are going to take more eyes and significant testing. What would you think about dropping it from this PR and creating a separate JBS issue as this is going to require more cycles that everything else in this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@minborg
Copy link
Copy Markdown
Contributor Author

minborg commented May 27, 2025

/reviewers 1

I am reverting to 1 reviewer, as we have removed the asynchronous Windows class.

@openjdk
Copy link
Copy Markdown

openjdk Bot commented May 27, 2025

@minborg
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 1 (with at least 1 Reviewer).

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label May 27, 2025
@openjdk openjdk Bot removed the ready Pull request is ready to be integrated label May 27, 2025
@openjdk openjdk Bot added the ready Pull request is ready to be integrated label May 27, 2025
@minborg
Copy link
Copy Markdown
Contributor Author

minborg commented May 27, 2025

/integrate

@openjdk
Copy link
Copy Markdown

openjdk Bot commented May 27, 2025

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

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label May 27, 2025
@openjdk openjdk Bot closed this May 27, 2025
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 27, 2025
@openjdk
Copy link
Copy Markdown

openjdk Bot commented May 27, 2025

@minborg Pushed as commit d4b923d.

💡 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

integrated Pull request has been integrated net net-dev@openjdk.org nio nio-dev@openjdk.org security security-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

4 participants