Skip to content
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

8313874 - JNI NewWeakGlobalRef throws exception for null arg #15188

Closed
wants to merge 2 commits into from

Conversation

olivergillespie
Copy link
Contributor

@olivergillespie olivergillespie commented Aug 8, 2023

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.

(Thanks @dholmes-ora for confirming the issue and suggesting a fix.)


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-8313874: JNI NewWeakGlobalRef throws exception for null arg (Bug - P4)

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

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.
@bridgekeeper
Copy link

bridgekeeper bot commented Aug 8, 2023

👋 Welcome back ogillespie! 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 openjdk bot added the rfr Pull request is ready for review label Aug 8, 2023
@openjdk
Copy link

openjdk bot commented Aug 8, 2023

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

  • hotspot

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 hotspot hotspot-dev@openjdk.org label Aug 8, 2023
@mlbridge
Copy link

mlbridge bot commented Aug 8, 2023

Webrevs

@stefank
Copy link
Member

stefank commented Aug 8, 2023

or if obj was a weak global reference

There's no check to see if obj is a weak global reference in this function. Is that also an oversight or is the spec just weirdly phrased?

@@ -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) {
Copy link
Member

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().

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.

Copy link
Member

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.

Copy link
Member

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?

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?

@dholmes-ora
Copy link
Member

There's no check to see if obj is a weak global reference in this function.

I'm not even sure what that means. Are jweak and jobject interchangeable i.e we should detect:

jweak j = env->NewWeakGlobalRef(env, env->NewWeakGlobalRef(env, obj));

??

@stefank
Copy link
Member

stefank commented Aug 8, 2023

I don't see why env->NewWeakGlobalRef(env, env->NewWeakGlobalRef(env, obj)); should return NULL, but it sounds to me like that's what the spec asks for. My guess is that the spec is ill-worded.

You can pass jweaks as jobjects. JNIHandles::resolve will apply the correct GC barriers depending on the underlying handle. @kimbarrett implemented that support by adding tagged bits to the handles.

@@ -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) {

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.

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?

@stefank
Copy link
Member

stefank commented Aug 8, 2023

I see that the spec further down says:
obj was a weak global reference and has already been garbage collected

So, it is just the first obj was a weak global reference occurrence that is ill-worded.

Move test case to existing test, use .not_null() for null check.
@olivergillespie
Copy link
Contributor Author

Updated the null check and moved the test to an existing file, thanks.

Copy link

@kimbarrett kimbarrett left a 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.

@openjdk
Copy link

openjdk bot commented Aug 8, 2023

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

8313874: JNI NewWeakGlobalRef throws exception for null arg

Reviewed-by: dholmes, kbarrett, shade

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 master branch:

  • 83adaf5: 8313421: [JVMCI] avoid locking class loader in CompilerToVM.lookupType
  • 35b60f9: 8298095: Refine implSpec for SegmentAllocator
  • 6dba202: 8313670: Simplify shared lib name handling code in some tests
  • 8f28809: 8299790: os::print_hex_dump is racy
  • e080a0b: 8311508: ZGC: RAII use of IntelJccErratumAlignment
  • 242a2e6: 8308843: Generational ZGC: Remove gc/z/TestHighUsage.java
  • c822183: 8313768: Reduce interaction with volatile field in j.u.l.StreamHandler
  • cd16158: 8314075: Update JCov version for JDK 22
  • c307391: 8307184: Incorrect/inconsistent specification and implementation for Elements.getDocComment
  • 593ba2f: 8313693: Introduce an internal utility for the Damerau–Levenshtein distance calculation
  • ... and 25 more: https://git.openjdk.org/jdk/compare/a1115a7a39438438ec247743718cdc1ec59823d6...master

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 /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 8, 2023
Copy link
Member

@shipilev shipilev left a 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.

Copy link
Member

@dholmes-ora dholmes-ora left a 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.

@olivergillespie
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Aug 10, 2023
@openjdk
Copy link

openjdk bot commented Aug 10, 2023

@olivergillespie
Your change (at version 69ab3c4) is now ready to be sponsored by a Committer.

@shipilev
Copy link
Member

/sponsor

@openjdk
Copy link

openjdk bot commented Aug 10, 2023

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

  • 83adaf5: 8313421: [JVMCI] avoid locking class loader in CompilerToVM.lookupType
  • 35b60f9: 8298095: Refine implSpec for SegmentAllocator
  • 6dba202: 8313670: Simplify shared lib name handling code in some tests
  • 8f28809: 8299790: os::print_hex_dump is racy
  • e080a0b: 8311508: ZGC: RAII use of IntelJccErratumAlignment
  • 242a2e6: 8308843: Generational ZGC: Remove gc/z/TestHighUsage.java
  • c822183: 8313768: Reduce interaction with volatile field in j.u.l.StreamHandler
  • cd16158: 8314075: Update JCov version for JDK 22
  • c307391: 8307184: Incorrect/inconsistent specification and implementation for Elements.getDocComment
  • 593ba2f: 8313693: Introduce an internal utility for the Damerau–Levenshtein distance calculation
  • ... and 25 more: https://git.openjdk.org/jdk/compare/a1115a7a39438438ec247743718cdc1ec59823d6...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Aug 10, 2023

@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.

@olivergillespie olivergillespie deleted the 8313874 branch August 10, 2023 12:26
@olivergillespie
Copy link
Contributor Author

/backport jdk21u

@olivergillespie
Copy link
Contributor Author

/backport jdk17u

@openjdk
Copy link

openjdk bot commented Aug 23, 2023

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

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 028b3ae1 from the openjdk/jdk repository.

The commit being backported was authored by Oli Gillespie on 10 Aug 2023 and was reviewed by David Holmes, Kim Barrett and Aleksey Shipilev.

Thanks!

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:

$ git fetch https://github.com/openjdk-bots/jdk21u.git olivergillespie-backport-028b3ae1:olivergillespie-backport-028b3ae1
$ git checkout olivergillespie-backport-028b3ae1
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git olivergillespie-backport-028b3ae1

@openjdk
Copy link

openjdk bot commented Aug 23, 2023

@olivergillespie Could not automatically backport 028b3ae1 to openjdk/jdk17u due to conflicts in the following files:

  • src/hotspot/share/prims/jni.cpp

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.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk17u.git master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b olivergillespie-backport-028b3ae1

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk.git 028b3ae1b162bd8f7c340bfa6e9487ca83697955

# Backport the commit
$ git cherry-pick --no-commit 028b3ae1b162bd8f7c340bfa6e9487ca83697955
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 028b3ae1b162bd8f7c340bfa6e9487ca83697955'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk17u with the title Backport 028b3ae1b162bd8f7c340bfa6e9487ca83697955.

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