-
Notifications
You must be signed in to change notification settings - Fork 5.8k
8299684: (bf) JNI direct buffer functions with large capacity behave unexpectedly #11873
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
Conversation
👋 Welcome back bpb! A progress list of the required criteria for merging this PR into |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requires an update to the JNI spec and therefore a CSR?
src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
Outdated
Show resolved
Hide resolved
Yes, I think both. |
… up a capacity which overflows int range
I think it will look strange to update the JNI spec to say that although the function takes a jlong as the requested capacity, it will throw IAE if the capacity is larger than a jint! The mismatch between the JNI and Java code made me dig into the history here to see what JSR-51 had to say about this, but unfortunately it didn't say much. What I did find in https://bugs.openjdk.org/browse/JDK-4496703 was a very definitive comment about only supporting 32-bit (ie int) based direct buffers, but with a glimmer of hope
So perhaps the JNI side was prepared for this unrealized future? But even so it should have specified what happens if the jlong capacity exceeds the value of a jint I thought perhaps a special OutOfMemoryError (akin to the "Requested array size exceeds VM limit" OOME) could be thrown - and that would not require a JNI spec change - but it is a stretch. Though also note that if this requires a JNI spec change then it cannot be backported without jumping through some serious JCP Maintenance Release hoops. |
I checked ancient history and the issue goes back to JDK 1.4 (when JSR-51 defined this API). It's surprising that it hasn't been reported/noticed before now. Yes, this will need an update to the JNI spec to say that it throws if capacity is negative or greater than Integer.MAX_VALUE. It will look a bit strange in the spec when the parameter is jlong but we can't change it. As regards what to throw then the current proposal to throw IAE seems okay as the function already throws it for negative values that are >= Integer.MIN_VALUE, e.g.
The compatibility impact of the change should be minimal, at least I can't imagine anyone depending on the current broken behavior. For the change, clampCapacity only needs to be called once, not twice. The exception message is a bit strange. If cap is < Integer.MIN_VALUE then it would be better to have it consistent with the existing message above. Also "in upcall from JNI NewDirectByteBuffer" should be better if it didn't include "in upcall". We will need to add a test for this and decide where to place it. It seems there aren't any tests for these JNI functions in hotspot//jtreg/runtime/jni, maybe we start a new locations for tests in test/jdk/java/nio/jni. |
/csr |
@AlanBateman has indicated that a compatibility and specification (CSR) request is needed for this pull request. @bplb please create a CSR request for issue JDK-8299684 with the correct fix version. This pull request cannot be integrated until the CSR request is approved. |
src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
Outdated
Show resolved
Hide resolved
src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
Outdated
Show resolved
Hide resolved
src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
Outdated
Show resolved
Hide resolved
I agree with David that it would be clearer and simpler to check if capacity is negative or greater than Integer.MAX_VALUE. There other helper methods called from this code and they are named checkXXX so it would be good to keep that consistent if you can. We don't have other tests for this JNI function in the test tree so having the new tests be a complete unit test would be good. This means checking the position, limit, and capacity of the ByteBuffer and testing that it's a direct buffer. If you make it a JUnit test then you'll be able to paramatrized test with value sources, and use the assertions API when checking each of the buffer properties. For the native code used by the test then it would be clearer if allocBigBuffer were renamed to newDirectByteBuffer. Also I think you'll need to check the value from malloc in case it fails. You'll need another native function to free the memory and have the tests invoke the free in a finally block so that memory doesn't accumulate as the test cycles through the different buffer sizes. Right now, I assume the tests don't run on 32-bit as it mallocs two 2Gb areas. Once the test is re-worded then you can get a sense as to how much memory it needs and whether you need a @requires in the test description to ensue that the test system has sufficient memory. |
All points addressed in d2386a4 except converting to a JUnit test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updates look good - thanks. A few minor nits.
src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
Outdated
Show resolved
Hide resolved
src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The update looks quite good and maybe some day this can be converted from a psvm test to a junit test.
|
It sounds like the suggested way forward is to use junit for new tests and for extra credit convert existing testng tests when/if they need updated |
Converted to a JUnit test in 7f99fae.
Added @requires constraining to 64-bit arch and memory at least 8GB in 7f99fae. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test looks much better now but I think can be improved a bit more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks okay. If you are doing any further edits then the try-finally OOME can be removed from the illegalCapacities test.
@bplb 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:
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 277 new commits pushed to the
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing further from me. Thanks
/integrate |
Going to push as commit a56598f.
Your commit was automatically rebased without conflicts. |
This PR is causing causing x86 build failures in GitHub actions. I filed https://bugs.openjdk.org/browse/JDK-8300942 |
PR #12157 has been created to address this problem. |
Remove cast in
JNI::NewDirectByteBuffer
oflong
capacity toint
, modify the constructor in question to accept along
capacity, and verify in the constructor that the capacity does not overflowint
range, throwing IAE If it does.Progress
Issues
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/11873/head:pull/11873
$ git checkout pull/11873
Update a local copy of the PR:
$ git checkout pull/11873
$ git pull https://git.openjdk.org/jdk pull/11873/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 11873
View PR using the GUI difftool:
$ git pr show -t 11873
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/11873.diff