Skip to content

Commit a2f9166

Browse files
author
David Holmes
committed
8379007: [lworld] JNI NewWeakGlobalRef should throw IdentityException for value types
Reviewed-by: liach, heidinga
1 parent 9c53e9a commit a2f9166

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/hotspot/share/prims/jni.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,7 +2944,16 @@ JNI_END
29442944

29452945
JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
29462946
HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref);
2947+
29472948
Handle ref_handle(thread, JNIHandles::resolve(ref));
2949+
2950+
if (!ref_handle.is_null() && ref_handle->klass()->is_inline_klass()) {
2951+
ResourceMark rm(THREAD);
2952+
stringStream ss;
2953+
ss.print("%s is not an identity class", ref_handle->klass()->external_name());
2954+
THROW_MSG_(vmSymbols::java_lang_IdentityException(), ss.as_string(), nullptr);
2955+
}
2956+
29482957
jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL);
29492958
if (ret == nullptr && ref_handle.not_null()) {
29502959
THROW_OOP_(Universe::out_of_memory_error_c_heap(), nullptr);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package runtime.valhalla.inlinetypes;
25+
26+
import jdk.test.lib.Asserts;
27+
28+
/*
29+
* @test
30+
* @summary Test JNI NewWeakGlobalRef with inline types
31+
* @library /test/lib
32+
* @enablePreview
33+
* @run main/othervm/native --enable-native-access=ALL-UNNAMED
34+
* runtime.valhalla.inlinetypes.TestJNINewWeakGlobalRef
35+
*/
36+
public class TestJNINewWeakGlobalRef {
37+
38+
static value class Value {
39+
int i;
40+
41+
public Value(int i) {
42+
this.i = i;
43+
}
44+
}
45+
46+
native static void newWeakGlobalRef(Object target);
47+
48+
static {
49+
System.loadLibrary("JNINewWeakGlobalRef");
50+
}
51+
52+
public static void main(String[] args) {
53+
Value v = new Value(1);
54+
try {
55+
newWeakGlobalRef(v);
56+
Asserts.fail("Should have thrown IdentityException");
57+
}
58+
catch (IdentityException i) {
59+
Asserts.assertTrue(i.getMessage().contains("TestJNINewWeakGlobalRef$Value is not an identity class"),
60+
"Wrong identity exception: " + i);
61+
}
62+
63+
// Sanity check that passing null doesn't cause an error.
64+
newWeakGlobalRef(null);
65+
}
66+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
#include <jni.h>
25+
26+
JNIEXPORT void JNICALL
27+
Java_runtime_valhalla_inlinetypes_TestJNINewWeakGlobalRef_newWeakGlobalRef(JNIEnv* env, jclass klass, jobject target) {
28+
(*env)->NewWeakGlobalRef(env, target);
29+
}

0 commit comments

Comments
 (0)