-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Conversation
👋 Welcome back coleenp! 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.
The change looks good.
Thanks, Harold
@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:
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
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 |
Thanks, Harold! |
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.
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); |
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 don't think you can hold the Mutex across the entire method as the exception code below will go to Java.
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.
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.
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.
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.
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.
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.
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.
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.
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 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.
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.
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.)
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 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.
Why not just use |
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. 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. |
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.
No further comments from me.
Thanks David. |
Going to push as commit c05015b.
Your commit was automatically rebased without conflicts. |
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
Issue
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