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));
|
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 (Arguments::is_valhalla_enabled()) {
295 if (!ret && obj1 != nullptr && obj2 != nullptr && obj1->klass() == obj2->klass() && obj1->klass()->is_inline_klass()) {
296 // The two references are different, they are not null and they are both inline types,
297 // a full substitutability test is required, calling ValueObjectMethods.isSubstitutable()
298 // (similarly to InterpreterRuntime::is_substitutable)
299 JavaThread* THREAD = JavaThread::current();
300 Handle ha(THREAD, obj1);
301 Handle hb(THREAD, obj2);
302 JavaValue result(T_BOOLEAN);
303 JavaCallArguments args;
304 args.push_oop(ha);
305 args.push_oop(hb);
306 methodHandle method(THREAD, Universe::is_substitutable_method());
307 JavaCalls::call(&result, method, &args, THREAD);
308 if (HAS_PENDING_EXCEPTION) {
309 // Something really bad happened because isSubstitutable() should not throw exceptions
310 // If it is an error, just let it propagate
311 // If it is an exception, wrap it into an InternalError
312 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
313 Handle e(THREAD, PENDING_EXCEPTION);
314 CLEAR_PENDING_EXCEPTION;
315 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
316 }
317 }
318 ret = result.get_jboolean();
319 }
320 }
321
322 return ret;
323 }
324
325
326 int JNIHandleBlock::_blocks_allocated = 0;
327
328 static inline bool is_tagged_free_list(uintptr_t value) {
329 return (value & 1u) != 0;
330 }
331
332 static inline uintptr_t tag_free_list(uintptr_t value) {
333 return value | 1u;
334 }
335
336 static inline uintptr_t untag_free_list(uintptr_t value) {
337 return value & ~(uintptr_t)1u;
338 }
339
340 // There is a freelist of handles running through the JNIHandleBlock
341 // with a tagged next pointer, distinguishing these next pointers from
342 // oops. The freelist handling currently relies on the size of oops
343 // being the same as a native pointer. If this ever changes, then
344 // this freelist handling must change too.
345 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t));
|