1 /*
2 * Copyright (c) 1998, 2025, 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
25 #include "gc/shared/collectedHeap.hpp"
26 #include "gc/shared/oopStorage.inline.hpp"
27 #include "gc/shared/oopStorageSet.hpp"
28 #include "logging/log.hpp"
29 #include "memory/iterator.hpp"
30 #include "memory/universe.hpp"
31 #include "oops/access.inline.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/handles.inline.hpp"
34 #include "runtime/javaThread.inline.hpp"
35 #include "runtime/jniHandles.inline.hpp"
36 #include "runtime/mutexLocker.hpp"
37 #include "utilities/align.hpp"
38 #include "utilities/debug.hpp"
39
40 OopStorage* JNIHandles::global_handles() {
41 return _global_handles;
42 }
43
44 OopStorage* JNIHandles::weak_global_handles() {
45 return _weak_global_handles;
46 }
47
48 // Serviceability agent support.
49 OopStorage* JNIHandles::_global_handles = nullptr;
50 OopStorage* JNIHandles::_weak_global_handles = nullptr;
51
52 void jni_handles_init() {
53 JNIHandles::_global_handles = OopStorageSet::create_strong("JNI Global", mtInternal);
265 guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop");
266 }
267 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
268 };
269
270 void JNIHandles::verify() {
271 VerifyJNIHandles verify_handle;
272
273 oops_do(&verify_handle);
274 weak_oops_do(&verify_handle);
275 }
276
277 // This method is implemented here to avoid circular includes between
278 // jniHandles.hpp and thread.hpp.
279 bool JNIHandles::current_thread_in_native() {
280 Thread* thread = Thread::current();
281 return (thread->is_Java_thread() &&
282 JavaThread::cast(thread)->thread_state() == _thread_in_native);
283 }
284
285 int JNIHandleBlock::_blocks_allocated = 0;
286
287 static inline bool is_tagged_free_list(uintptr_t value) {
288 return (value & 1u) != 0;
289 }
290
291 static inline uintptr_t tag_free_list(uintptr_t value) {
292 return value | 1u;
293 }
294
295 static inline uintptr_t untag_free_list(uintptr_t value) {
296 return value & ~(uintptr_t)1u;
297 }
298
299 // There is a freelist of handles running through the JNIHandleBlock
300 // with a tagged next pointer, distinguishing these next pointers from
301 // oops. The freelist handling currently relies on the size of oops
302 // being the same as a native pointer. If this ever changes, then
303 // this freelist handling must change too.
304 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t));
|
1 /*
2 * Copyright (c) 1998, 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
25 #include "classfile/vmSymbols.hpp"
26 #include "gc/shared/collectedHeap.hpp"
27 #include "gc/shared/oopStorage.inline.hpp"
28 #include "gc/shared/oopStorageSet.hpp"
29 #include "logging/log.hpp"
30 #include "memory/iterator.hpp"
31 #include "memory/universe.hpp"
32 #include "oops/access.inline.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "runtime/arguments.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/javaCalls.hpp"
37 #include "runtime/javaThread.inline.hpp"
38 #include "runtime/jniHandles.inline.hpp"
39 #include "runtime/mutexLocker.hpp"
40 #include "utilities/align.hpp"
41 #include "utilities/debug.hpp"
42
43 OopStorage* JNIHandles::global_handles() {
44 return _global_handles;
45 }
46
47 OopStorage* JNIHandles::weak_global_handles() {
48 return _weak_global_handles;
49 }
50
51 // Serviceability agent support.
52 OopStorage* JNIHandles::_global_handles = nullptr;
53 OopStorage* JNIHandles::_weak_global_handles = nullptr;
54
55 void jni_handles_init() {
56 JNIHandles::_global_handles = OopStorageSet::create_strong("JNI Global", mtInternal);
268 guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop");
269 }
270 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
271 };
272
273 void JNIHandles::verify() {
274 VerifyJNIHandles verify_handle;
275
276 oops_do(&verify_handle);
277 weak_oops_do(&verify_handle);
278 }
279
280 // This method is implemented here to avoid circular includes between
281 // jniHandles.hpp and thread.hpp.
282 bool JNIHandles::current_thread_in_native() {
283 Thread* thread = Thread::current();
284 return (thread->is_Java_thread() &&
285 JavaThread::cast(thread)->thread_state() == _thread_in_native);
286 }
287
288 bool JNIHandles::is_same_object(jobject handle1, jobject handle2) {
289 oop obj1 = resolve_no_keepalive(handle1);
290 oop obj2 = resolve_no_keepalive(handle2);
291
292 bool ret = obj1 == obj2;
293
294 if (!ret && Arguments::is_valhalla_enabled()) {
295 if (obj1 != nullptr && obj2 != nullptr &&
296 obj1->klass() == obj2->klass() && obj1->klass()->is_inline_klass()) {
297 // The two references are different, they are not null and they are both inline types,
298 // a full substitutability test is required, calling ValueObjectMethods.isSubstitutable()
299 // (similarly to InterpreterRuntime::is_substitutable).
300 // The jobjects must be re-resolved as the no-keepalive variants are not safe to use
301 // with the Java upcall.
302 JavaThread* THREAD = JavaThread::current();
303 Handle ha(THREAD, resolve_non_null(handle1));
304 Handle hb(THREAD, resolve_non_null(handle2));
305 JavaValue result(T_BOOLEAN);
306 JavaCallArguments args;
307 args.push_oop(ha);
308 args.push_oop(hb);
309 methodHandle method(THREAD, Universe::is_substitutable_method());
310 JavaCalls::call(&result, method, &args, THREAD);
311 Exceptions::wrap_exception_in_internal_error("Internal error in substitutability test", CHECK_false);
312
313 ret = result.get_jboolean();
314 }
315 }
316
317 return ret;
318 }
319
320
321 int JNIHandleBlock::_blocks_allocated = 0;
322
323 static inline bool is_tagged_free_list(uintptr_t value) {
324 return (value & 1u) != 0;
325 }
326
327 static inline uintptr_t tag_free_list(uintptr_t value) {
328 return value | 1u;
329 }
330
331 static inline uintptr_t untag_free_list(uintptr_t value) {
332 return value & ~(uintptr_t)1u;
333 }
334
335 // There is a freelist of handles running through the JNIHandleBlock
336 // with a tagged next pointer, distinguishing these next pointers from
337 // oops. The freelist handling currently relies on the size of oops
338 // being the same as a native pointer. If this ever changes, then
339 // this freelist handling must change too.
340 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t));
|