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

8291736: find_method_handle_intrinsic leaks Method* #9983

Closed
wants to merge 4 commits into from

Conversation

coleenp
Copy link
Contributor

@coleenp coleenp commented Aug 23, 2022

As part of the SymbolPropertyTable conversion, we noticed that threads could race to add the Method entry to the table, and the loser wasn't deleted. This change locks the InvokeMethodTable_lock through the Method creation so that it's not leaked. See bug for details, but this was performance tested with our general suite of performance tests to show no significant differences.
Also tested with tier1-3, and previously 4-7 with SymbolPropertyTable conversion patch.


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-8291736: find_method_handle_intrinsic leaks Method*

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 9983

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 23, 2022

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

openjdk bot commented Aug 23, 2022

@coleenp 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 Aug 23, 2022
@mlbridge
Copy link

mlbridge bot commented Aug 23, 2022

Webrevs

Copy link
Member

@hseigel hseigel left a comment

Choose a reason for hiding this comment

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

The change looks good.
Thanks, Harold

@openjdk
Copy link

openjdk bot commented Aug 24, 2022

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

8291736: find_method_handle_intrinsic leaks Method*

Reviewed-by: hseigel, iklam, dholmes

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

  • 5757e21: 8292385: assert(ctrl == kit.control()) failed: Control flow was added although the intrinsic bailed out
  • 3993a1f: 8292067: Convert test/sun/management/jmxremote/bootstrap shell tests to java version
  • 83a3408: 8293315: Add back logging for Placeholders
  • b6477fd: 8293288: bootcycle build failure after JDK-8173605
  • 0c6094e: 8293188: x86_64: Introduce stubGenerator_x86_64.hpp
  • 2baeebb: 8293006: sun/tools/jhsdb/JStackStressTest.java fails with "UnalignedAddressException: 8baadbabe"
  • da99e3e: 8289400: Improve com/sun/jdi/TestScaffold error reporting
  • 77e21c5: 8290529: C2: assert(BoolTest(btest).is_canonical()) failure
  • fcc0cf9: 8292375: Convert ProtectionDomainCacheTable to ResourceHashtable
  • 6fc58b8: 8293207: Add assert to JVM_ReferenceRefersTo to clarify its API
  • ... and 119 more: https://git.openjdk.org/jdk/compare/d24b7b7026cf85f1aecf44f60819762872cfd5c1...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 Aug 24, 2022
@coleenp
Copy link
Contributor Author

coleenp commented Aug 24, 2022

Thanks, Harold!

Copy link
Member

@iklam iklam left a comment

Choose a reason for hiding this comment

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

This looks fine. Now we are holding a lock across the call to make_method_handle_intrinsic, but that doesn't seem to do anything complicated (luckily it's not calling back into Java), so we should be OK.

const int iid_as_int = vmIntrinsics::as_int(iid);
assert(MethodHandles::is_signature_polymorphic(iid) &&
MethodHandles::is_signature_polymorphic_intrinsic(iid) &&
iid != vmIntrinsics::_invokeGeneric,
"must be a known MH intrinsic iid=%d: %s", iid_as_int, vmIntrinsics::name_at(iid));

Method** met;
MutexLocker ml(THREAD, InvokeMethodTable_lock);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you can hold the Mutex across the entire method as the exception code below will go to Java.

Copy link
Contributor Author

@coleenp coleenp Aug 25, 2022

Choose a reason for hiding this comment

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

This call does not go to Java. The other table does - the invoke_method_type_table. This just creates an intrinsic method and an adapter to go with it from the AdapterHandlerLibrary.

Copy link
Member

Choose a reason for hiding this comment

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

Line 2087 can throw an exception. If you want to hold the mutex across the entire body you need to throw the exception after releasing the lock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I didn't read what you actually wrote (just what I thought you wrote). Line 2080 can also throw an exception for metaspace allocation failure. I don't believe that we always do this outside a lock. This is a subtle interaction that only you noticed. I'm not sure what to do with the metaspace allocation failure. Moving throwing the other exception outside the lock uglifies the logic but isn't hard to do.

Copy link
Member

Choose a reason for hiding this comment

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

This is a subtle interaction that only you noticed.

You mean in this review? The original code clearly knew this had to be done outside the lock:

http://hg.openjdk.java.net/jdk7/jdk7/hotspot/rev/e5b0439ef4ae#l33.106

Now perhaps the current code is actually safe (as @iklam suggests) because it can't actually invoke any Java code? But that is something we would need to establish. In any case it seems an anti-pattern to allow any kind of use of CHECK from inside a locked region when we "know" we always have to throw exceptions outside of locked regions.

Copy link
Contributor Author

@coleenp coleenp Aug 31, 2022

Choose a reason for hiding this comment

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

The problem with the original code was the ranking and multi-purpose use of the SystemDictionary_lock. Creating the adapters takes out a lock whose ranking is above SystemDictionary_lock. Also the tables (and lock for the tables) were shared with code that could call Java code for calling Java to create MethodType, so that code had to release the lock. Leaking the java.lang.invoke.MethodType is ok because it'll be GC'd.

We've leaked the Method* and adapter for many years now, maybe it's fine to keep leaking it and I should just close this and not try to fix it.

The case of throwing exceptions inside Mutex locked regions is something I thought we have all over the source code. I couldn't find any explicit calls, but the pattern of doing metaspace allocation inside of a MutexLocker is in enough places that adding an assert(!thread->owns_locks(), "must release all locks when possibly throwing exceptions"); during in the TRAPS version of Metaspace::allocate() fails immediately.

But it may be that the OOM for Metaspace allocation failure doesn't call into Java for constructing the object and this part is fine.

Copy link
Member

Choose a reason for hiding this comment

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

Right, as per our side-bar discussion, while the general rule is "no throwing exceptions while holding a VM mutex/monitor" it seems that it is safe to only throw OutOfMemoryError because Universe::gen_out_of_memory_error doesn't execute any Java code. It either uses one of the pre-allocated OOME instances (if available) and fills in the stacktrace, or else (or in case of secondary OOM) it uses the pre-allocated stackless singleton instance. This should be documented somewhere of course (and perhaps even programmatically verified somehow NoJavaCodeVerifier? :) )

Also whilst the lower-level code makes it somewhat clear that the only exception that can arise is the metaspace OOME, it is not at all clear just by looking at a callsite like:

methodHandle m = Method::make_method_handle_intrinsic(iid, signature, CHECK_NULL);

so perhaps we even need additional macros like CHECK_OOM to verify only OOME can be thrown? (Not in this PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment above the make_method_handle_intrinsic call about why it's ok to call inside a locked region, and agree that we need better checking for the ability to run Java code from all paths with possible exceptions. I'm not sure how to do that yet but we should file an RFE.

@dholmes-ora
Copy link
Member

Why not just use created to determine whether to delete the method to be thrown away?

@coleenp
Copy link
Contributor Author

coleenp commented Aug 25, 2022

Why not just use created to determine whether to delete the method to be thrown away?

I had a version of this that crashed because we create an adapter and a native wrapper so removing the Method* through add_to_deallocate_list lead to a dangling pointer from the code cache.
This seems simpler.

To elaborate further, this code creates a native wrapper nmethod for the Method created in make_method_handle_intrinsic, which has the Metadata for Method* in the nmethod. Removing the leaked Method (and associated ConstantPool, as each of these creates their own constant pool), also requires some way of removing the nmethod as well. Given that this code is owned by the GC, adding non-gc support for doing this seems like a very bad idea.

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.

No further comments from me.

@coleenp
Copy link
Contributor Author

coleenp commented Sep 6, 2022

Thanks David.
/integrate

@openjdk
Copy link

openjdk bot commented Sep 6, 2022

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

  • b2067e6: 8291725: Leftover marks when VM shutdown aborts bitmap clearing make mixed gc fail
  • 6a1e98c: 8293213: G1: Remove redundant assertion in G1RemSet::clean_card_before_refine
  • a92c1ff: 8287912: GTK L&F : Background of tree icons are red
  • 272745b: 8293340: Remove unused _code in {Zero,Template}InterpreterGenerator
  • 26f2a97: 8290561: Coalesce incubator-module warnings for single-file source-code programs
  • 8e22f2b: 8293361: GHA: dump config.log in case of configure failure
  • b17758a: 8247283: Enable the new conformant preprocessor option in Visual Studio
  • 1bed23a: 8293353: [BACKOUT] G1: Remove redundant is-marking-active checks in C1 barrier
  • 4955835: 8282434: Leading combining diacritic character in string renders incorrectly
  • ef20ffe: 8293159: Use try-with-resources in X11FontManager.registerFontDir
  • ... and 150 more: https://git.openjdk.org/jdk/compare/d24b7b7026cf85f1aecf44f60819762872cfd5c1...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Sep 6, 2022

@coleenp Pushed as commit c05015b.

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

@coleenp coleenp deleted the intrinsic-lock branch September 6, 2022 14:30
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.

4 participants