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

8280422: thread_from_jni_environment can never return NULL #7193

Closed

Conversation

dholmes-ora
Copy link
Member

@dholmes-ora dholmes-ora commented Jan 23, 2022

Analysing the code and its use (see JBS issue for gory details) we can see that thread_from_jni_environment can never actually return NULL, so we change it to not appear to do so and thus keep static analysis tools happy. We also always validate the incoming JNIEnv for debug builds and scrap the unused VerifyJNIEnvThread flag.

Testing: tiers 1-5

Thanks,
David


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8280422: thread_from_jni_environment can never return NULL

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/7193/head:pull/7193
$ git checkout pull/7193

Update a local copy of the PR:
$ git checkout pull/7193
$ git pull https://git.openjdk.java.net/jdk pull/7193/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 7193

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/7193.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 23, 2022

👋 Welcome back dholmes! 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 Jan 23, 2022
@openjdk
Copy link

openjdk bot commented Jan 23, 2022

@dholmes-ora The following label will be automatically applied to this pull request:

  • hotspot-runtime

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-runtime hotspot-runtime-dev@openjdk.org label Jan 23, 2022
@mlbridge
Copy link

mlbridge bot commented Jan 23, 2022

Webrevs

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 sensible to me.

@openjdk
Copy link

openjdk bot commented Jan 24, 2022

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

8280422: thread_from_jni_environment can never return NULL

Reviewed-by: shade, kbarrett

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 33 new commits pushed to the master branch:

  • e307655: 8280403: RegEx: String.split can fail with NPE in Pattern.CharPredicate::match
  • 52ddbe2: Merge
  • ead9fec: 8280441: Missing "classpath exception" in several files from jdk.httpserver
  • 7d2ef9d: 8279179: Update nroff pages in JDK 18 before RC
  • 8e82d00: 8280492: Use cross-module syntax for cross-module links
  • a541666: 8280474: Garbage value passed to getLocaleInfoWrapper in HostLocaleProviderAdapter_md
  • a825a4a: 8279861: Clarify 'rect' parameters and description of paintTabBorder method in BasicTabbedPaneUI
  • 0b5c54b: 8279798: Javadoc for BasicTabbedPaneUI is inconsistent
  • acd9829: 8279794: Fix typos in BasicScrollBarUI: Laysouts a vertical scroll bar
  • dae2226: 8279795: Fix typo in BasicFileChooserUI: Constucts -> Constructs
  • ... and 23 more: https://git.openjdk.java.net/jdk/compare/35ee0f38c6d5e35fb50a059a189e032df7f7e7c5...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.

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

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 24, 2022
@dholmes-ora
Copy link
Member Author

Thanks for the review @shipilev !

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.

return NULL;
} else {
return thread_from_jni_env;
JavaThread* current = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));

Choose a reason for hiding this comment

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

[pre-existing] If I were writing this I would have used char* rather than intptr_t.

@dholmes-ora
Copy link
Member Author

Thanks @kimbarrett !

/integrate

@openjdk
Copy link

openjdk bot commented Jan 25, 2022

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

  • e307655: 8280403: RegEx: String.split can fail with NPE in Pattern.CharPredicate::match
  • 52ddbe2: Merge
  • ead9fec: 8280441: Missing "classpath exception" in several files from jdk.httpserver
  • 7d2ef9d: 8279179: Update nroff pages in JDK 18 before RC
  • 8e82d00: 8280492: Use cross-module syntax for cross-module links
  • a541666: 8280474: Garbage value passed to getLocaleInfoWrapper in HostLocaleProviderAdapter_md
  • a825a4a: 8279861: Clarify 'rect' parameters and description of paintTabBorder method in BasicTabbedPaneUI
  • 0b5c54b: 8279798: Javadoc for BasicTabbedPaneUI is inconsistent
  • acd9829: 8279794: Fix typos in BasicScrollBarUI: Laysouts a vertical scroll bar
  • dae2226: 8279795: Fix typo in BasicFileChooserUI: Constucts -> Constructs
  • ... and 23 more: https://git.openjdk.java.net/jdk/compare/35ee0f38c6d5e35fb50a059a189e032df7f7e7c5...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jan 25, 2022

@dholmes-ora Pushed as commit f35df5b.

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

@dholmes-ora dholmes-ora deleted the 8280422-thread-from-jni branch January 25, 2022 02:18
@parttimenerd
Copy link
Contributor

parttimenerd commented Feb 21, 2022

This seems to break AsyncGetCallTrace in some rare cases, as "thread_from_jni_environment can never actually return NULL" does not always hold.

I run into this rather often when running a profiler on the dacapo benchmark suite with an debug build of the JDK.

@parttimenerd
Copy link
Contributor

I would suggest to add a method for checking the termination status in cases where it might happen:

  // Returns whether current thread as indicated by the given JNIEnv
  // is terminated.
  // We don't assert it is Thread::current here as that is done at the
  // external JNI entry points where the JNIEnv is passed into the VM.
  static bool is_thread_from_jni_environment_terminated(JNIEnv* env) {
    JavaThread* current = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
    // We can't get here in a thread that has completed its execution and so
    // "is_terminated", but a thread is also considered terminated if the VM
    // has exited, so we have to check this and block in case this is a daemon
    // thread returning to the VM (the JNI DirectBuffer entry points rely on
    // this).
    return current->is_terminated();
  }

@dholmes-ora
Copy link
Member Author

@parttimenerd 👍

This seems to break AsyncGetCallTrace in some rare cases, as "thread_from_jni_environment can never actually return NULL" does not always hold.

Can you please provide further information. As a correctly used JNIEnv can never produce a NULL thread as the JNIEnv is a sub-object of the JavaThread instance.

@parttimenerd
Copy link
Contributor

AsyncGetCallTrace is implemented in forte.cpp:

void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
  JavaThread* thread;

  if (trace->env_id == NULL ||
    (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL ||
    thread->is_exiting()) {

    // bad env_id, thread has exited or thread is exiting
    trace->num_frames = ticks_thread_exit; // -8
    return;
  }

I'm going to create a separate issue with a PR for the discussion.

@dholmes-ora
Copy link
Member Author

dholmes-ora commented Feb 21, 2022

That condition should just be:

if (trace->env_id == NULL || (thread = JavaThread::thread_from_jni_environment(trace->env_id))->is_exiting()) {

@parttimenerd
Copy link
Contributor

That does not address the issue, as JavaThread::thread_from_jni_environment(trace->env_id) never returns in case is_terminating() returns true.

@dholmes-ora
Copy link
Member Author

Correct but this PR did not introduce that behaviour. I only changed the name of the local variable and added the comment.

// this).
if (current->is_terminated()) {
current->block_if_vm_exited();
ShouldNotReachHere();
Copy link
Contributor

Choose a reason for hiding this comment

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

This breaks AsyncGetCallTrace

Copy link
Member Author

Choose a reason for hiding this comment

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

If we reach that code then the VM aborts. It should be impossible to reach that statement and thus impossible to actually return NULL in the old code. Please elaborate under what conditions you think this code can be reached.

Copy link
Contributor

Choose a reason for hiding this comment

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

AsyncProfiler on a debug build of OpenJDK profiling the dacapo tomcat benchmark.

@parttimenerd
Copy link
Contributor

It seems to me that this commit introduces the ShouldNotReachHere() when before NULL would be returned.

@parttimenerd
Copy link
Contributor

I've created a PR with a fix at #7559

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

Successfully merging this pull request may close these issues.

5 participants