RFR: 8313874 - JNI NewWeakGlobalRef throws exception for null arg
Kim Barrett
kbarrett at openjdk.org
Tue Aug 8 12:40:33 UTC 2023
On Tue, 8 Aug 2023 12:05:12 GMT, David Holmes <dholmes at openjdk.org> wrote:
>> According to the JNI spec for NewWeakGlobalRef:
>>
>>> Returns NULL if obj refers to null, or if obj was a weak global reference, or if the VM runs out of memory. If the VM runs out of memory, an OutOfMemoryError will be thrown.
>>
>> The current behaviour is that `NewWeakGlobalRef(nullptr)` throws an OutOfMemoryError, which it should not.
>>
>> (Thanks @dholmes-ora for confirming the issue and suggesting a fix.)
>
> src/hotspot/share/prims/jni.cpp line 2879:
>
>> 2877: Handle ref_handle(thread, JNIHandles::resolve(ref));
>> 2878: jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL);
>> 2879: if (ret == nullptr && ref_handle != nullptr) {
>
> That isn't how you check a Handle for null content - you need `!ref_handle.is_null()`.
even better would be `ref_handle.not_null()`. Though I think the comparison does work.
> test/hotspot/jtreg/runtime/jni/NullJNIWeak/NullJNIWeak.java line 1:
>
>> 1: /*
>
> Is there not an existing test we can simply add the null case to?
Agree that adding this to some existing test might be preferable. Maybe another subtest (see main) in
runtime/jni/ReturnJNIWeak?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15188#discussion_r1287039034
PR Review Comment: https://git.openjdk.org/jdk/pull/15188#discussion_r1287049972
More information about the hotspot-dev
mailing list