RFR: 8313874 - JNI NewWeakGlobalRef throws exception for null arg

David Holmes dholmes at openjdk.org
Tue Aug 8 12:08:32 UTC 2023


On Tue, 8 Aug 2023 10:40:30 GMT, Oli Gillespie <ogillespie 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.)

Changes requested by dholmes (Reviewer).

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()`.

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?

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

PR Review: https://git.openjdk.org/jdk/pull/15188#pullrequestreview-1567143282
PR Review Comment: https://git.openjdk.org/jdk/pull/15188#discussion_r1287020371
PR Review Comment: https://git.openjdk.org/jdk/pull/15188#discussion_r1287020998


More information about the hotspot-dev mailing list