-
Notifications
You must be signed in to change notification settings - Fork 5.8k
8313874 - JNI NewWeakGlobalRef throws exception for null arg #15188
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
According to the JNI spec for NewWeakGlobalRef: > Returns NULL if obj refers to null, or if obj was a weak global reference, or if the VM runs out of memory. If the VM runs out of memory, an OutOfMemoryError will be thrown. The current behaviour is that `NewWeakGlobalRef(nullptr)` throws an OutOfMemoryError, which it should not.
👋 Welcome back ogillespie! A progress list of the required criteria for merging this PR into |
@olivergillespie The following label will be automatically applied to this pull request:
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. |
Webrevs
|
There's no check to see if |
src/hotspot/share/prims/jni.cpp
Outdated
@@ -2876,7 +2876,7 @@ JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref)) | |||
HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref); | |||
Handle ref_handle(thread, JNIHandles::resolve(ref)); | |||
jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL); | |||
if (ret == nullptr) { | |||
if (ret == nullptr && ref_handle != nullptr) { |
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.
That isn't how you check a Handle for null content - you need !ref_handle.is_null()
.
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.
even better would be ref_handle.not_null()
. Though I think the comparison does work.
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.
Yes it seems that some implicit Handle conversions were removed at some point which now makes direct comparison to nullptr
allowable - somewhat obsolescing the is_null()/not_null()
helper methods.
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.
Is there not an existing test we can simply add the null case to?
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.
Agree that adding this to some existing test might be preferable. Maybe another subtest (see main) in
runtime/jni/ReturnJNIWeak?
I'm not even sure what that means. Are
?? |
I don't see why You can pass |
src/hotspot/share/prims/jni.cpp
Outdated
@@ -2876,7 +2876,7 @@ JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref)) | |||
HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref); | |||
Handle ref_handle(thread, JNIHandles::resolve(ref)); | |||
jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL); | |||
if (ret == nullptr) { | |||
if (ret == nullptr && ref_handle != nullptr) { |
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.
even better would be ref_handle.not_null()
. Though I think the comparison does work.
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.
Agree that adding this to some existing test might be preferable. Maybe another subtest (see main) in
runtime/jni/ReturnJNIWeak?
I see that the spec further down says: So, it is just the first |
Move test case to existing test, use .not_null() for null check.
Updated the null check and moved the test to an existing file, thanks. |
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.
Looks good.
It's weird that NewWeakGlobalRef throws on OOM, but NewGlobalRef doesn't. But that's
what the spec says.
@olivergillespie 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 35 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. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@dholmes-ora, @kimbarrett, @shipilev) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
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.
Looks good to me.
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.
Looks good. Thanks.
I will file a follow up RFE to look at the spec issue @stefank pointed out.
/integrate |
@olivergillespie |
/sponsor |
Going to push as commit 028b3ae.
Your commit was automatically rebased without conflicts. |
@shipilev @olivergillespie Pushed as commit 028b3ae. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
/backport jdk21u |
/backport jdk17u |
@olivergillespie the backport was successfully created on the branch olivergillespie-backport-028b3ae1 in my personal fork of openjdk/jdk21u. To create a pull request with this backport targeting openjdk/jdk21u:master, just click the following link: The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:
If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u:
|
@olivergillespie Could not automatically backport
Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk17u. Note: these commands are just some suggestions and you can use other equivalent commands you know.
Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk17u with the title |
According to the JNI spec for NewWeakGlobalRef:
The current behaviour is that
NewWeakGlobalRef(nullptr)
throws an OutOfMemoryError, which it should not.(Thanks @dholmes-ora for confirming the issue and suggesting a fix.)
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/15188/head:pull/15188
$ git checkout pull/15188
Update a local copy of the PR:
$ git checkout pull/15188
$ git pull https://git.openjdk.org/jdk.git pull/15188/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 15188
View PR using the GUI difftool:
$ git pr show -t 15188
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/15188.diff
Webrev
Link to Webrev Comment