RFR: 8299089: Instrument global jni handles with tag to make them distinguishable

Axel Boldt-Christmas aboldtch at openjdk.org
Tue Jan 10 07:57:52 UTC 2023


On Mon, 9 Jan 2023 19:28:50 GMT, Albert Mingkun Yang <ayang at openjdk.org> wrote:

>> Weak global jni handles are tagged so the GC can distinguish them when resolving the object. Today there is no cheap way of distinguishing global jni handles from local jni handles. For generational ZGC the OopStorage handles and the thread local handles semantical difference requires the handles to be distinguishable. 
>> 
>> This enhancements instruments the jni handles with a global tag similarly to the jweak tag. 
>> 
>> Note: 
>>  * the s390 implementation has minimal changes and requires improvement.
>>  * There is room for enchantment here to create the same abstraction that ppc uses for all platforms, i.e. move the resolve_jobject from the MacroAssembler to the BarrierSetAssembler which allows for more optimised code for GCs that can treat local and global handles the same. 
>> 
>> Testing: GHA. Oracle supported platforms tier1-3. Will run higher tiers. Has also been tested on the generational branch of ZGC for over three months. Requires testing on non Oracle platforms.
>
> src/hotspot/share/runtime/jniHandles.cpp line 250:
> 
>> 248: bool JNIHandles::is_global_handle(jobject handle) {
>> 249:   assert(handle != NULL, "precondition");
>> 250:   return is_global_tagged(handle) && !is_jweak_tagged(handle) && is_storage_handle(global_handles(), global_ptr(handle));
> 
> Can a handle be both global and jweak tagged? Can a non-global ref be global-tagged?
> 
> Seems to me the `is_global_tagged` check alone is enough here.

All the tags are mutually exclusive.
_Note there is a minor difference in naming here. We just call them local, global or [j]weak. But in the jni.h and the API documentation jweak is also known as a Weak Global Ref or JNIWeakGlobalRefType, and global is a Global ref or JNIGlobalRefType. But they are still mutually exclusive and it is just the naming that both use the word global._

An alternative implementation would be to have the two bits mean global/local and strong/weak with the local weak combination being disallowed. I think the only benefit would be that it would fit more with the API naming that a global weak is global and weak tagged, a global is global tagged and local has no tags. 
But it would confuse the internal meaning of `global_handle`, does it include global weak or not? I think there is probably a better way of naming all these, but I also think the proposed change is the least disruptive to established nomenclature.

`is_global_tagged` is enough if you only look at the hotspot code. But we will get handles from the JNI external interface, and we want to check that the storage is correct in conjunction with checking that the tags are correct. (I think this is the idea at least. Similarly how before this change `is_weak_global_handle` both checked the tag and the storage, while just checking the tag would have been enough).

Looking at the code there is probably room for improvement here, as the `checked_jni_Delete*` which uses the `is_*_handle` functions also uses `jniCheck::validate_object` which only checks the tags (not the storage) before trying to look at the contents. So I am unsure what happens if the input handle has a bad tag/storage.

-------------

PR: https://git.openjdk.org/jdk/pull/11740


More information about the hotspot-dev mailing list