[BUG PROPOSAL]: C++ code that calls JNI_CreateJavaVM can be exited by java

Adam Farley8 adam.farley at uk.ibm.com
Wed Aug 30 16:57:26 UTC 2017

Hi All,

I've included the full text of my reply in-line below.

A summary is: I continue to support the idea of a new return code on the 
that, when the VM is nonusable but no error has occurred, we have no 

Right now we can:
- Report an error that has not occurred.
- Die and take the user's code with us (except for any exit hook code).
- Return a JNI_OK, and allow the user's next action to fail.

I think that if VM developers concur that the correct action is to leave 
the VM
in a nonusable state, but to not throw an error, that this RC gives us a 
better option
than exit(0).

- Adam

P.S. Apologies for the delay. I was on vacation. :)

> Hi Alan, David, and Tom,
>> First, thanks again for your efforts on this. As a new guy to OpenJDK
>> contributions, it means a lot to see so much progress on this so 
>> quickly. :)
>All I see is discussion :) Progress would be something else entirely.

True. :)

>>  >On 24/08/2017 07:33, David Holmes wrote:
>>  >> Hi Adam,
>>  >>
>>  >> cc'ing hotspot runtime dev as runtime own JNI and the invocation 
>>  >> and some of the problematic code resides in the VM.
>>  >Yeah, the hotspot mailing list would be a better place to discuss 
>>  >as there are several issues here and several places where HotSpot 
>>  >the process when initialization fails. It's a long standing issue 
>>  >back 15+ years) that I think is partly because it's not easy to 
>>  >all resources and cleanup before CreateJavaVM returns with an error.
>>  >
>> According to the JNI spec, it is not possible (yet) to create a second 
>> in the same thread as the first.
>> There is also a bug (dup'd against another bug I don't have the access 
>> which states that even a successful VM creation+destruction won't 
>> a second VM to be created.
>> https://bugs.openjdk.java.net/browse/JDK-4712793
>> Both of these seem to imply that making a new VM after a failed 
>> (in the same thread) is unsupported behaviour.
>> So is it important to release all resources and cleanup, given that we 
>> won't
>> be trying to create a new VM in this thread? By "important" I mean 
>> important than exiting with a return code and allowing the user's code 
>> to finish".
>Okay, so if there is no intention of attempting to reload the jvm again, 
>I'm unclear what the purpose of the hosting process actually is. To me 
>it is either a customer launcher - in which case the exit calls are 
>"harmless" (and atexit handlers could be used if the process has its own 
>clean up) - or it's something multi-purpose part of which is to launch a 
>VM. In the latter case given the inability to reload a VM, and assuming 
>the process does not what it's java launching powers to be removed, then 
>the only real option is to filter out the problematic arguments and 
>either ignore them or exec a separate process to handle them.

My assumption is that the user's code may be doing many things, of which
the Java work is only a part. I'm trying not to be too specific here, as I 
know what the user is trying to do, nor what they want their code to do if
Java returns an error. I think we should tell the user what has happened, 
allow them to act on the information. 

Right now the VM developers don't have that option. They don't have a 
to tell the user that the VM is not in a usable state, but had found no 
errors. Therefore 
the VM *must* call exit(0) to indicate "pass", but also to prevent the 
user trying to do
anything with the unusable VM. 

I would give them that option. If they can return an RC, they should have 
one available 
that fits this scenario.

By providing this negative return code both within and without the VM, we 
can give future
VM-upgrade projects the option to indicate an unusable VM with no error, 
the need for them to call exit(0) when the VM is unusable despite no error 

Also, in regards to the example option: I agree that this option should 
really be filtered
out before we get to the exit(0)-slash-JNI_SILENT_EXIT RC. Perhaps we 
could abstract
the "is this a help option" logic into a shared function, and tie that 
into the unrecognised
options logic?

>>  >>
>>  >> This specific case seems like a bug to me as the logic is assuming 
>>  >> is only ever called by a launcher which it is okay to terminate.
>>  >> Though to be honest the very existence of the "help" option seems 
>>  >> me somewhat misguided in a hosted-VM environment. That said, I see
>>  >> unified logging in 9 also added a terminating "help" option <sigh>.
>>  >The agent "help" option case is tricky and would likely need an 
>>  >to the JVM TI spec and the Agent_OnLoad return value.
>>  >
>> To clarify, the agent "help" option is only an example of this problem.
>> There are 19 locations both within and without hotspot that call 
>> directly, plus more places where exit is passed a variable that can be
>> 0 (e.g. the aforementioned agent "help", which calls the forceExit 
>> with an argument of 0, which calls exit(arg) in turn).
>> I understand that your comment was intended as an effort to effect a 
>> for this specific instance of the problem. I wanted to make sure we 
>> sight of the wider problem, as ideally we'd come up with an ideal 
>> that could be applied to all cases.
>The fact there are numerous potential process termination points in the 
>VM and JDK native code, is something we just have to live with. I'm only 
>considering these kind of "report and terminate" flags to be the problem 
>cases that should be handled better.

A fair statement. I posit that simply having this option available could 
the need for further exit(0)'s in the future.

Though I'm certainly not ruling out an entrepreneurial VM developer fixing
these issues in the future. I'm simply agreeing that resolving all of 
issues are outside of this proposal's scope. 

>> My thought on this was a unique return code that tells the user's code
>> that the VM is not in a usable state, but that no error has occurred. 
>> should be a negative code (so the usual x<0 check will prevent the 
>> code from using the VM), but it shouldn't be one of the existing JNI 
>> all of which seem to indicate either:
>> a) The VM is fine and can be used (0).
>> or
>> b) The VM is not fine because an error occurred (-1 to -6).
>> Ideally we need a c) The VM is not fine, but no error has occurred.
>It's somewhat debatable how to classify the case where you ask the VM to 
>load and then perform a one-off action that effectively succeeds but 
>leaves the VM unusable. Again ideally, to me, the VM would never do that 
>- such actions would occur as part of VM initialization, the VM would be 
>usable, but the launcher would do the termination because that is how 
>the flag is specified. But that is non-trivial to untangle.


>> Or is there another solution to the exit(0) problem? Other than putting 

>> a copy of the rest of your code on the exit hook, I mean.
>>  >
>>  >>
>>  >> Options processed by the VM will be recognized, while options
>>  >> processed by the Java launcher will not be. "-version", "-X", 
>>  >> and numerous others are launcher options. Pure VM options are -XX
>>  >> options, but the VM also processes some -X flags and, as a result 
>>  >> jigsaw, now also processes a bunch of module-related flags that are
>>  >> simple --foo options.
>>  >Right because these options need to passed to CreateJavaVM as they 
>>  >used when initializing the VM. Using system properties would just 
>>  >the issues of past (e.g. java.class.path) and require documenting a 
>>  >of system properties (which is complicated at repeating options).
>>  >
>>  >-Alan
Unless stated otherwise above:
IBM United Kingdom Limited - Registered in England and Wales with number 
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU

More information about the core-libs-dev mailing list