1 /* 2 * Copyright (c) 1998, 2022, 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 "precompiled.hpp" 26 #include "classfile/vmSymbols.hpp" 27 #include "gc/shared/collectedHeap.hpp" 28 #include "gc/shared/oopStorage.inline.hpp" 29 #include "gc/shared/oopStorageSet.hpp" 30 #include "logging/log.hpp" 31 #include "memory/iterator.hpp" 32 #include "memory/universe.hpp" 33 #include "oops/access.inline.hpp" 34 #include "oops/oop.inline.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 = NULL; 53 OopStorage* JNIHandles::_weak_global_handles = NULL; 54 55 void jni_handles_init() { 56 JNIHandles::_global_handles = OopStorageSet::create_strong("JNI Global", mtInternal); 57 JNIHandles::_weak_global_handles = OopStorageSet::create_weak("JNI Weak", mtInternal); 58 } 59 60 jobject JNIHandles::make_local(oop obj) { 61 return make_local(JavaThread::current(), obj); 62 } 63 64 // Used by NewLocalRef which requires NULL on out-of-memory 65 jobject JNIHandles::make_local(JavaThread* thread, oop obj, AllocFailType alloc_failmode) { 66 if (obj == NULL) { 67 return NULL; // ignore null handles 68 } else { 69 assert(oopDesc::is_oop(obj), "not an oop"); 70 assert(!current_thread_in_native(), "must not be in native"); 71 return thread->active_handles()->allocate_handle(thread, obj, alloc_failmode); 72 } 73 } 74 75 static void report_handle_allocation_failure(AllocFailType alloc_failmode, 76 const char* handle_kind) { 77 if (alloc_failmode == AllocFailStrategy::EXIT_OOM) { 78 // Fake size value, since we don't know the min allocation size here. 79 vm_exit_out_of_memory(sizeof(oop), OOM_MALLOC_ERROR, 80 "Cannot create %s JNI handle", handle_kind); 81 } else { 82 assert(alloc_failmode == AllocFailStrategy::RETURN_NULL, "invariant"); 83 } 84 } 85 86 jobject JNIHandles::make_global(Handle obj, AllocFailType alloc_failmode) { 87 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 88 assert(!current_thread_in_native(), "must not be in native"); 89 jobject res = NULL; 90 if (!obj.is_null()) { 91 // ignore null handles 92 assert(oopDesc::is_oop(obj()), "not an oop"); 93 oop* ptr = global_handles()->allocate(); 94 // Return NULL on allocation failure. 95 if (ptr != NULL) { 96 assert(*ptr == NULL, "invariant"); 97 NativeAccess<>::oop_store(ptr, obj()); 98 res = reinterpret_cast<jobject>(ptr); 99 } else { 100 report_handle_allocation_failure(alloc_failmode, "global"); 101 } 102 } 103 104 return res; 105 } 106 107 jobject JNIHandles::make_weak_global(Handle obj, AllocFailType alloc_failmode) { 108 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 109 assert(!current_thread_in_native(), "must not be in native"); 110 jobject res = NULL; 111 if (!obj.is_null()) { 112 // ignore null handles 113 assert(oopDesc::is_oop(obj()), "not an oop"); 114 oop* ptr = weak_global_handles()->allocate(); 115 // Return NULL on allocation failure. 116 if (ptr != NULL) { 117 assert(*ptr == NULL, "invariant"); 118 NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(ptr, obj()); 119 char* tptr = reinterpret_cast<char*>(ptr) + weak_tag_value; 120 res = reinterpret_cast<jobject>(tptr); 121 } else { 122 report_handle_allocation_failure(alloc_failmode, "weak global"); 123 } 124 } 125 return res; 126 } 127 128 // Resolve some erroneous cases to NULL, rather than treating them as 129 // possibly unchecked errors. In particular, deleted handles are 130 // treated as NULL (though a deleted and later reallocated handle 131 // isn't detected). 132 oop JNIHandles::resolve_external_guard(jobject handle) { 133 oop result = NULL; 134 if (handle != NULL) { 135 result = resolve_impl<DECORATORS_NONE, true /* external_guard */>(handle); 136 } 137 return result; 138 } 139 140 bool JNIHandles::is_global_weak_cleared(jweak handle) { 141 assert(handle != NULL, "precondition"); 142 assert(is_jweak(handle), "not a weak handle"); 143 oop* oop_ptr = jweak_ptr(handle); 144 oop value = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(oop_ptr); 145 return value == NULL; 146 } 147 148 void JNIHandles::destroy_global(jobject handle) { 149 if (handle != NULL) { 150 assert(!is_jweak(handle), "wrong method for destroying jweak"); 151 oop* oop_ptr = jobject_ptr(handle); 152 NativeAccess<>::oop_store(oop_ptr, (oop)NULL); 153 global_handles()->release(oop_ptr); 154 } 155 } 156 157 158 void JNIHandles::destroy_weak_global(jobject handle) { 159 if (handle != NULL) { 160 assert(is_jweak(handle), "JNI handle not jweak"); 161 oop* oop_ptr = jweak_ptr(handle); 162 NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(oop_ptr, (oop)NULL); 163 weak_global_handles()->release(oop_ptr); 164 } 165 } 166 167 168 void JNIHandles::oops_do(OopClosure* f) { 169 global_handles()->oops_do(f); 170 } 171 172 173 void JNIHandles::weak_oops_do(OopClosure* f) { 174 weak_global_handles()->weak_oops_do(f); 175 } 176 177 bool JNIHandles::is_global_storage(const OopStorage* storage) { 178 return _global_handles == storage; 179 } 180 181 inline bool is_storage_handle(const OopStorage* storage, const oop* ptr) { 182 return storage->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY; 183 } 184 185 186 jobjectRefType JNIHandles::handle_type(JavaThread* thread, jobject handle) { 187 assert(handle != NULL, "precondition"); 188 jobjectRefType result = JNIInvalidRefType; 189 if (is_jweak(handle)) { 190 if (is_storage_handle(weak_global_handles(), jweak_ptr(handle))) { 191 result = JNIWeakGlobalRefType; 192 } 193 } else { 194 switch (global_handles()->allocation_status(jobject_ptr(handle))) { 195 case OopStorage::ALLOCATED_ENTRY: 196 result = JNIGlobalRefType; 197 break; 198 199 case OopStorage::UNALLOCATED_ENTRY: 200 break; // Invalid global handle 201 202 case OopStorage::INVALID_ENTRY: 203 // Not in global storage. Might be a local handle. 204 if (is_local_handle(thread, handle) || is_frame_handle(thread, handle)) { 205 result = JNILocalRefType; 206 } 207 break; 208 209 default: 210 ShouldNotReachHere(); 211 } 212 } 213 return result; 214 } 215 216 217 bool JNIHandles::is_local_handle(JavaThread* thread, jobject handle) { 218 assert(handle != NULL, "precondition"); 219 JNIHandleBlock* block = thread->active_handles(); 220 221 // Look back past possible native calls to jni_PushLocalFrame. 222 while (block != NULL) { 223 if (block->chain_contains(handle)) { 224 return true; 225 } 226 block = block->pop_frame_link(); 227 } 228 return false; 229 } 230 231 232 // Determine if the handle is somewhere in the current thread's stack. 233 // We easily can't isolate any particular stack frame the handle might 234 // come from, so we'll check the whole stack. 235 236 bool JNIHandles::is_frame_handle(JavaThread* thr, jobject handle) { 237 assert(handle != NULL, "precondition"); 238 // If there is no java frame, then this must be top level code, such 239 // as the java command executable, in which case, this type of handle 240 // is not permitted. 241 return (thr->has_last_Java_frame() && 242 thr->is_in_stack_range_incl((address)handle, (address)thr->last_Java_sp())); 243 } 244 245 246 bool JNIHandles::is_global_handle(jobject handle) { 247 assert(handle != NULL, "precondition"); 248 return !is_jweak(handle) && is_storage_handle(global_handles(), jobject_ptr(handle)); 249 } 250 251 252 bool JNIHandles::is_weak_global_handle(jobject handle) { 253 assert(handle != NULL, "precondition"); 254 return is_jweak(handle) && is_storage_handle(weak_global_handles(), jweak_ptr(handle)); 255 } 256 257 // We assume this is called at a safepoint: no lock is needed. 258 void JNIHandles::print_on(outputStream* st) { 259 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 260 261 st->print_cr("JNI global refs: " SIZE_FORMAT ", weak refs: " SIZE_FORMAT, 262 global_handles()->allocation_count(), 263 weak_global_handles()->allocation_count()); 264 st->cr(); 265 st->flush(); 266 } 267 268 void JNIHandles::print() { print_on(tty); } 269 270 class VerifyJNIHandles: public OopClosure { 271 public: 272 virtual void do_oop(oop* root) { 273 guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop"); 274 } 275 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } 276 }; 277 278 void JNIHandles::verify() { 279 VerifyJNIHandles verify_handle; 280 281 oops_do(&verify_handle); 282 weak_oops_do(&verify_handle); 283 } 284 285 // This method is implemented here to avoid circular includes between 286 // jniHandles.hpp and thread.hpp. 287 bool JNIHandles::current_thread_in_native() { 288 Thread* thread = Thread::current(); 289 return (thread->is_Java_thread() && 290 JavaThread::cast(thread)->thread_state() == _thread_in_native); 291 } 292 293 bool JNIHandles::is_same_object(jobject handle1, jobject handle2) { 294 oop obj1 = resolve_no_keepalive(handle1); 295 oop obj2 = resolve_no_keepalive(handle2); 296 297 bool ret = obj1 == obj2; 298 299 if (EnableValhalla) { 300 if (!ret && obj1 != NULL && obj2 != NULL && obj1->klass() == obj2->klass() && obj1->klass()->is_inline_klass()) { 301 // The two references are different, they are not null and they are both inline types, 302 // a full substitutability test is required, calling ValueObjectMethods.isSubstitutable() 303 // (similarly to InterpreterRuntime::is_substitutable) 304 JavaThread* THREAD = JavaThread::current(); 305 Handle ha(THREAD, obj1); 306 Handle hb(THREAD, obj2); 307 JavaValue result(T_BOOLEAN); 308 JavaCallArguments args; 309 args.push_oop(ha); 310 args.push_oop(hb); 311 methodHandle method(THREAD, Universe::is_substitutable_method()); 312 JavaCalls::call(&result, method, &args, THREAD); 313 if (HAS_PENDING_EXCEPTION) { 314 // Something really bad happened because isSubstitutable() should not throw exceptions 315 // If it is an error, just let it propagate 316 // If it is an exception, wrap it into an InternalError 317 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) { 318 Handle e(THREAD, PENDING_EXCEPTION); 319 CLEAR_PENDING_EXCEPTION; 320 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false); 321 } 322 } 323 ret = result.get_jboolean(); 324 } 325 } 326 327 return ret; 328 } 329 330 331 int JNIHandleBlock::_blocks_allocated = 0; 332 333 static inline bool is_tagged_free_list(uintptr_t value) { 334 return (value & 1u) != 0; 335 } 336 337 static inline uintptr_t tag_free_list(uintptr_t value) { 338 return value | 1u; 339 } 340 341 static inline uintptr_t untag_free_list(uintptr_t value) { 342 return value & ~(uintptr_t)1u; 343 } 344 345 // There is a freelist of handles running through the JNIHandleBlock 346 // with a tagged next pointer, distinguishing these next pointers from 347 // oops. The freelist handling currently relies on the size of oops 348 // being the same as a native pointer. If this ever changes, then 349 // this freelist handling must change too. 350 STATIC_ASSERT(sizeof(oop) == sizeof(uintptr_t)); 351 352 #ifdef ASSERT 353 void JNIHandleBlock::zap() { 354 // Zap block values 355 _top = 0; 356 for (int index = 0; index < block_size_in_oops; index++) { 357 // NOT using Access here; just bare clobbering to NULL, since the 358 // block no longer contains valid oops. 359 _handles[index] = 0; 360 } 361 } 362 #endif // ASSERT 363 364 JNIHandleBlock* JNIHandleBlock::allocate_block(JavaThread* thread, AllocFailType alloc_failmode) { 365 // The VM thread can allocate a handle block in behalf of another thread during a safepoint. 366 assert(thread == NULL || thread == Thread::current() || SafepointSynchronize::is_at_safepoint(), 367 "sanity check"); 368 JNIHandleBlock* block; 369 // Check the thread-local free list for a block so we don't 370 // have to acquire a mutex. 371 if (thread != NULL && thread->free_handle_block() != NULL) { 372 block = thread->free_handle_block(); 373 thread->set_free_handle_block(block->_next); 374 } else { 375 // Allocate new block 376 if (alloc_failmode == AllocFailStrategy::RETURN_NULL) { 377 block = new (std::nothrow) JNIHandleBlock(); 378 if (block == NULL) { 379 return NULL; 380 } 381 } else { 382 block = new JNIHandleBlock(); 383 } 384 Atomic::inc(&_blocks_allocated); 385 block->zap(); 386 } 387 block->_top = 0; 388 block->_next = NULL; 389 block->_pop_frame_link = NULL; 390 // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle 391 debug_only(block->_last = NULL); 392 debug_only(block->_free_list = NULL); 393 debug_only(block->_allocate_before_rebuild = -1); 394 return block; 395 } 396 397 398 void JNIHandleBlock::release_block(JNIHandleBlock* block, JavaThread* thread) { 399 assert(thread == NULL || thread == Thread::current(), "sanity check"); 400 JNIHandleBlock* pop_frame_link = block->pop_frame_link(); 401 // Put returned block at the beginning of the thread-local free list. 402 // Note that if thread == NULL, we use it as an implicit argument that 403 // we _don't_ want the block to be kept on the free_handle_block. 404 // See for instance JavaThread::exit(). 405 if (thread != NULL ) { 406 block->zap(); 407 JNIHandleBlock* freelist = thread->free_handle_block(); 408 block->_pop_frame_link = NULL; 409 thread->set_free_handle_block(block); 410 411 // Add original freelist to end of chain 412 if ( freelist != NULL ) { 413 while ( block->_next != NULL ) block = block->_next; 414 block->_next = freelist; 415 } 416 block = NULL; 417 } else { 418 DEBUG_ONLY(block->set_pop_frame_link(NULL)); 419 while (block != NULL) { 420 JNIHandleBlock* next = block->_next; 421 Atomic::dec(&_blocks_allocated); 422 assert(block->pop_frame_link() == NULL, "pop_frame_link should be NULL"); 423 delete block; 424 block = next; 425 } 426 } 427 if (pop_frame_link != NULL) { 428 // As a sanity check we release blocks pointed to by the pop_frame_link. 429 // This should never happen (only if PopLocalFrame is not called the 430 // correct number of times). 431 release_block(pop_frame_link, thread); 432 } 433 } 434 435 436 void JNIHandleBlock::oops_do(OopClosure* f) { 437 JNIHandleBlock* current_chain = this; 438 // Iterate over chain of blocks, followed by chains linked through the 439 // pop frame links. 440 while (current_chain != NULL) { 441 for (JNIHandleBlock* current = current_chain; current != NULL; 442 current = current->_next) { 443 assert(current == current_chain || current->pop_frame_link() == NULL, 444 "only blocks first in chain should have pop frame link set"); 445 for (int index = 0; index < current->_top; index++) { 446 uintptr_t* addr = &(current->_handles)[index]; 447 uintptr_t value = *addr; 448 // traverse heap pointers only, not deleted handles or free list 449 // pointers 450 if (value != 0 && !is_tagged_free_list(value)) { 451 oop* root = (oop*)addr; 452 f->do_oop(root); 453 } 454 } 455 // the next handle block is valid only if current block is full 456 if (current->_top < block_size_in_oops) { 457 break; 458 } 459 } 460 current_chain = current_chain->pop_frame_link(); 461 } 462 } 463 464 465 jobject JNIHandleBlock::allocate_handle(JavaThread* caller, oop obj, AllocFailType alloc_failmode) { 466 assert(Universe::heap()->is_in(obj), "sanity check"); 467 if (_top == 0) { 468 // This is the first allocation or the initial block got zapped when 469 // entering a native function. If we have any following blocks they are 470 // not valid anymore. 471 for (JNIHandleBlock* current = _next; current != NULL; 472 current = current->_next) { 473 assert(current->_last == NULL, "only first block should have _last set"); 474 assert(current->_free_list == NULL, 475 "only first block should have _free_list set"); 476 if (current->_top == 0) { 477 // All blocks after the first clear trailing block are already cleared. 478 #ifdef ASSERT 479 for (current = current->_next; current != NULL; current = current->_next) { 480 assert(current->_top == 0, "trailing blocks must already be cleared"); 481 } 482 #endif 483 break; 484 } 485 current->_top = 0; 486 current->zap(); 487 } 488 // Clear initial block 489 _free_list = NULL; 490 _allocate_before_rebuild = 0; 491 _last = this; 492 zap(); 493 } 494 495 // Try last block 496 if (_last->_top < block_size_in_oops) { 497 oop* handle = (oop*)&(_last->_handles)[_last->_top++]; 498 NativeAccess<IS_DEST_UNINITIALIZED>::oop_store(handle, obj); 499 return (jobject) handle; 500 } 501 502 // Try free list 503 if (_free_list != NULL) { 504 oop* handle = (oop*)_free_list; 505 _free_list = (uintptr_t*) untag_free_list(*_free_list); 506 NativeAccess<IS_DEST_UNINITIALIZED>::oop_store(handle, obj); 507 return (jobject) handle; 508 } 509 // Check if unused block follow last 510 if (_last->_next != NULL) { 511 // update last and retry 512 _last = _last->_next; 513 return allocate_handle(caller, obj, alloc_failmode); 514 } 515 516 // No space available, we have to rebuild free list or expand 517 if (_allocate_before_rebuild == 0) { 518 rebuild_free_list(); // updates _allocate_before_rebuild counter 519 } else { 520 _last->_next = JNIHandleBlock::allocate_block(caller, alloc_failmode); 521 if (_last->_next == NULL) { 522 return NULL; 523 } 524 _last = _last->_next; 525 _allocate_before_rebuild--; 526 } 527 return allocate_handle(caller, obj, alloc_failmode); // retry 528 } 529 530 void JNIHandleBlock::rebuild_free_list() { 531 assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking"); 532 int free = 0; 533 int blocks = 0; 534 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { 535 for (int index = 0; index < current->_top; index++) { 536 uintptr_t* handle = &(current->_handles)[index]; 537 if (*handle == 0) { 538 // this handle was cleared out by a delete call, reuse it 539 *handle = _free_list == NULL ? 0 : tag_free_list((uintptr_t)_free_list); 540 _free_list = handle; 541 free++; 542 } 543 } 544 // we should not rebuild free list if there are unused handles at the end 545 assert(current->_top == block_size_in_oops, "just checking"); 546 blocks++; 547 } 548 // Heuristic: if more than half of the handles are free we rebuild next time 549 // as well, otherwise we append a corresponding number of new blocks before 550 // attempting a free list rebuild again. 551 int total = blocks * block_size_in_oops; 552 int extra = total - 2*free; 553 if (extra > 0) { 554 // Not as many free handles as we would like - compute number of new blocks to append 555 _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops; 556 } 557 } 558 559 560 bool JNIHandleBlock::contains(jobject handle) const { 561 return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]); 562 } 563 564 565 bool JNIHandleBlock::chain_contains(jobject handle) const { 566 for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) { 567 if (current->contains(handle)) { 568 return true; 569 } 570 } 571 return false; 572 }