1 /* 2 * Copyright (c) 1998, 2018, 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 "gc/shared/oopStorage.inline.hpp" 27 #include "logging/log.hpp" 28 #include "memory/iterator.hpp" 29 #include "oops/oop.inline.hpp" 30 #include "runtime/jniHandles.inline.hpp" 31 #include "runtime/mutexLocker.hpp" 32 #include "runtime/thread.inline.hpp" 33 #include "trace/traceMacros.hpp" 34 #include "utilities/align.hpp" 35 #include "utilities/debug.hpp" 36 #if INCLUDE_ALL_GCS 37 #include "gc/g1/g1BarrierSet.hpp" 38 #endif 39 40 OopStorage* JNIHandles::_global_handles = NULL; 41 OopStorage* JNIHandles::_weak_global_handles = NULL; 42 43 44 jobject JNIHandles::make_local(oop obj) { 45 if (obj == NULL) { 46 return NULL; // ignore null handles 47 } else { 48 Thread* thread = Thread::current(); 49 assert(oopDesc::is_oop(obj), "not an oop"); 50 assert(!current_thread_in_native(), "must not be in native"); 51 return thread->active_handles()->allocate_handle(obj); 52 } 53 } 54 55 56 // optimized versions 57 58 jobject JNIHandles::make_local(Thread* thread, oop obj) { 59 if (obj == NULL) { 60 return NULL; // ignore null handles 61 } else { 62 assert(oopDesc::is_oop(obj), "not an oop"); 63 assert(thread->is_Java_thread(), "not a Java thread"); 64 assert(!current_thread_in_native(), "must not be in native"); 65 return thread->active_handles()->allocate_handle(obj); 66 } 67 } 68 69 70 jobject JNIHandles::make_local(JNIEnv* env, oop obj) { 71 if (obj == NULL) { 72 return NULL; // ignore null handles 73 } else { 74 JavaThread* thread = JavaThread::thread_from_jni_environment(env); 75 assert(oopDesc::is_oop(obj), "not an oop"); 76 assert(!current_thread_in_native(), "must not be in native"); 77 return thread->active_handles()->allocate_handle(obj); 78 } 79 } 80 81 82 static void report_handle_allocation_failure(AllocFailType alloc_failmode, 83 const char* handle_kind) { 84 if (alloc_failmode == AllocFailStrategy::EXIT_OOM) { 85 // Fake size value, since we don't know the min allocation size here. 86 vm_exit_out_of_memory(sizeof(oop), OOM_MALLOC_ERROR, 87 "Cannot create %s JNI handle", handle_kind); 88 } else { 89 assert(alloc_failmode == AllocFailStrategy::RETURN_NULL, "invariant"); 90 } 91 } 92 93 jobject JNIHandles::make_global(Handle obj, AllocFailType alloc_failmode) { 94 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 95 assert(!current_thread_in_native(), "must not be in native"); 96 jobject res = NULL; 97 if (!obj.is_null()) { 98 // ignore null handles 99 assert(oopDesc::is_oop(obj()), "not an oop"); 100 oop* ptr = _global_handles->allocate(); 101 // Return NULL on allocation failure. 102 if (ptr != NULL) { 103 *ptr = obj(); 104 res = reinterpret_cast<jobject>(ptr); 105 } else { 106 report_handle_allocation_failure(alloc_failmode, "global"); 107 } 108 } else { 109 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 110 } 111 112 return res; 113 } 114 115 116 jobject JNIHandles::make_weak_global(Handle obj, AllocFailType alloc_failmode) { 117 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 118 assert(!current_thread_in_native(), "must not be in native"); 119 jobject res = NULL; 120 if (!obj.is_null()) { 121 // ignore null handles 122 assert(oopDesc::is_oop(obj()), "not an oop"); 123 oop* ptr = _weak_global_handles->allocate(); 124 // Return NULL on allocation failure. 125 if (ptr != NULL) { 126 *ptr = obj(); 127 char* tptr = reinterpret_cast<char*>(ptr) + weak_tag_value; 128 res = reinterpret_cast<jobject>(tptr); 129 } else { 130 report_handle_allocation_failure(alloc_failmode, "weak global"); 131 } 132 } else { 133 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 134 } 135 return res; 136 } 137 138 // Resolve some erroneous cases to NULL, rather than treating them as 139 // possibly unchecked errors. In particular, deleted handles are 140 // treated as NULL (though a deleted and later reallocated handle 141 // isn't detected). 142 oop JNIHandles::resolve_external_guard(jobject handle) { 143 oop result = NULL; 144 if (handle != NULL) { 145 result = resolve_impl<true /* external_guard */ >(handle); 146 } 147 return result; 148 } 149 150 oop JNIHandles::resolve_jweak(jweak handle) { 151 assert(handle != NULL, "precondition"); 152 assert(is_jweak(handle), "precondition"); 153 #if INCLUDE_ALL_GCS 154 if (UseZGC) { 155 oop* ref_addr = jweak_ref_addr(handle); 156 return RootAccess<ON_PHANTOM_OOP_REF>::oop_load(ref_addr); 157 } 158 #endif 159 160 oop result = jweak_ref(handle); 161 #if INCLUDE_ALL_GCS 162 if (result != NULL && UseG1GC) { 163 G1BarrierSet::enqueue(result); 164 } 165 #endif // INCLUDE_ALL_GCS 166 return result; 167 } 168 169 bool JNIHandles::is_global_weak_cleared(jweak handle) { 170 assert(handle != NULL, "precondition"); 171 assert(is_jweak(handle), "not a weak handle"); 172 return jweak_ref(handle) == NULL; 173 } 174 175 void JNIHandles::destroy_global(jobject handle) { 176 if (handle != NULL) { 177 assert(!is_jweak(handle), "wrong method for detroying jweak"); 178 jobject_ref(handle) = NULL; 179 _global_handles->release(&jobject_ref(handle)); 180 } 181 } 182 183 184 void JNIHandles::destroy_weak_global(jobject handle) { 185 if (handle != NULL) { 186 assert(is_jweak(handle), "JNI handle not jweak"); 187 jweak_ref(handle) = NULL; 188 _weak_global_handles->release(&jweak_ref(handle)); 189 } 190 } 191 192 193 void JNIHandles::oops_do(OopClosure* f) { 194 _global_handles->oops_do(f); 195 } 196 197 198 void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { 199 _weak_global_handles->weak_oops_do(is_alive, f); 200 } 201 202 203 void JNIHandles::weak_oops_do(OopClosure* f) { 204 _weak_global_handles->weak_oops_do(f); 205 } 206 207 208 OopStorage* JNIHandles::weak_global_handles() { 209 return _weak_global_handles; 210 } 211 212 213 void JNIHandles::initialize() { 214 _global_handles = new OopStorage("JNI Global", 215 JNIGlobalAlloc_lock, 216 JNIGlobalActive_lock); 217 _weak_global_handles = new OopStorage("JNI Weak", 218 JNIWeakAlloc_lock, 219 JNIWeakActive_lock); 220 } 221 222 223 inline bool is_storage_handle(const OopStorage* storage, const oop* ptr) { 224 return storage->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY; 225 } 226 227 228 jobjectRefType JNIHandles::handle_type(Thread* thread, jobject handle) { 229 assert(handle != NULL, "precondition"); 230 jobjectRefType result = JNIInvalidRefType; 231 if (is_jweak(handle)) { 232 if (is_storage_handle(_weak_global_handles, &jweak_ref(handle))) { 233 result = JNIWeakGlobalRefType; 234 } 235 } else { 236 switch (_global_handles->allocation_status(&jobject_ref(handle))) { 237 case OopStorage::ALLOCATED_ENTRY: 238 result = JNIGlobalRefType; 239 break; 240 241 case OopStorage::UNALLOCATED_ENTRY: 242 break; // Invalid global handle 243 244 case OopStorage::INVALID_ENTRY: 245 // Not in global storage. Might be a local handle. 246 if (is_local_handle(thread, handle) || 247 (thread->is_Java_thread() && 248 is_frame_handle((JavaThread*)thread, handle))) { 249 result = JNILocalRefType; 250 } 251 break; 252 253 default: 254 ShouldNotReachHere(); 255 } 256 } 257 return result; 258 } 259 260 261 bool JNIHandles::is_local_handle(Thread* thread, jobject handle) { 262 assert(handle != NULL, "precondition"); 263 JNIHandleBlock* block = thread->active_handles(); 264 265 // Look back past possible native calls to jni_PushLocalFrame. 266 while (block != NULL) { 267 if (block->chain_contains(handle)) { 268 return true; 269 } 270 block = block->pop_frame_link(); 271 } 272 return false; 273 } 274 275 276 // Determine if the handle is somewhere in the current thread's stack. 277 // We easily can't isolate any particular stack frame the handle might 278 // come from, so we'll check the whole stack. 279 280 bool JNIHandles::is_frame_handle(JavaThread* thr, jobject handle) { 281 assert(handle != NULL, "precondition"); 282 // If there is no java frame, then this must be top level code, such 283 // as the java command executable, in which case, this type of handle 284 // is not permitted. 285 return (thr->has_last_Java_frame() && 286 (void*)handle < (void*)thr->stack_base() && 287 (void*)handle >= (void*)thr->last_Java_sp()); 288 } 289 290 291 bool JNIHandles::is_global_handle(jobject handle) { 292 assert(handle != NULL, "precondition"); 293 return !is_jweak(handle) && is_storage_handle(_global_handles, &jobject_ref(handle)); 294 } 295 296 297 bool JNIHandles::is_weak_global_handle(jobject handle) { 298 assert(handle != NULL, "precondition"); 299 return is_jweak(handle) && is_storage_handle(_weak_global_handles, &jweak_ref(handle)); 300 } 301 302 size_t JNIHandles::global_handle_memory_usage() { 303 return _global_handles->total_memory_usage(); 304 } 305 306 size_t JNIHandles::weak_global_handle_memory_usage() { 307 return _weak_global_handles->total_memory_usage(); 308 } 309 310 311 // We assume this is called at a safepoint: no lock is needed. 312 void JNIHandles::print_on(outputStream* st) { 313 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 314 assert(_global_handles != NULL && _weak_global_handles != NULL, 315 "JNIHandles not initialized"); 316 317 st->print_cr("JNI global refs: " SIZE_FORMAT ", weak refs: " SIZE_FORMAT, 318 _global_handles->allocation_count(), 319 _weak_global_handles->allocation_count()); 320 st->cr(); 321 st->flush(); 322 } 323 324 class VerifyJNIHandles: public OopClosure { 325 public: 326 virtual void do_oop(oop* root) { 327 (*root)->verify(); 328 } 329 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } 330 }; 331 332 void JNIHandles::verify() { 333 VerifyJNIHandles verify_handle; 334 335 oops_do(&verify_handle); 336 if (!UseZGC) { 337 weak_oops_do(&verify_handle); 338 } 339 } 340 341 // This method is implemented here to avoid circular includes between 342 // jniHandles.hpp and thread.hpp. 343 bool JNIHandles::current_thread_in_native() { 344 Thread* thread = Thread::current(); 345 return (thread->is_Java_thread() && 346 JavaThread::current()->thread_state() == _thread_in_native); 347 } 348 349 350 void jni_handles_init() { 351 JNIHandles::initialize(); 352 } 353 354 355 int JNIHandleBlock::_blocks_allocated = 0; 356 JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; 357 #ifndef PRODUCT 358 JNIHandleBlock* JNIHandleBlock::_block_list = NULL; 359 #endif 360 361 362 #ifdef ASSERT 363 void JNIHandleBlock::zap() { 364 // Zap block values 365 _top = 0; 366 for (int index = 0; index < block_size_in_oops; index++) { 367 _handles[index] = NULL; 368 } 369 } 370 #endif // ASSERT 371 372 JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) { 373 assert(thread == NULL || thread == Thread::current(), "sanity check"); 374 JNIHandleBlock* block; 375 // Check the thread-local free list for a block so we don't 376 // have to acquire a mutex. 377 if (thread != NULL && thread->free_handle_block() != NULL) { 378 block = thread->free_handle_block(); 379 thread->set_free_handle_block(block->_next); 380 } 381 else { 382 // locking with safepoint checking introduces a potential deadlock: 383 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock 384 // - another would hold Threads_lock (jni_AttachCurrentThread) and then 385 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) 386 MutexLockerEx ml(JNIHandleBlockFreeList_lock, 387 Mutex::_no_safepoint_check_flag); 388 if (_block_free_list == NULL) { 389 // Allocate new block 390 block = new JNIHandleBlock(); 391 _blocks_allocated++; 392 block->zap(); 393 #ifndef PRODUCT 394 // Link new block to list of all allocated blocks 395 block->_block_list_link = _block_list; 396 _block_list = block; 397 #endif 398 } else { 399 // Get block from free list 400 block = _block_free_list; 401 _block_free_list = _block_free_list->_next; 402 } 403 } 404 block->_top = 0; 405 block->_next = NULL; 406 block->_pop_frame_link = NULL; 407 block->_planned_capacity = block_size_in_oops; 408 // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle 409 debug_only(block->_last = NULL); 410 debug_only(block->_free_list = NULL); 411 debug_only(block->_allocate_before_rebuild = -1); 412 return block; 413 } 414 415 416 void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) { 417 assert(thread == NULL || thread == Thread::current(), "sanity check"); 418 JNIHandleBlock* pop_frame_link = block->pop_frame_link(); 419 // Put returned block at the beginning of the thread-local free list. 420 // Note that if thread == NULL, we use it as an implicit argument that 421 // we _don't_ want the block to be kept on the free_handle_block. 422 // See for instance JavaThread::exit(). 423 if (thread != NULL ) { 424 block->zap(); 425 JNIHandleBlock* freelist = thread->free_handle_block(); 426 block->_pop_frame_link = NULL; 427 thread->set_free_handle_block(block); 428 429 // Add original freelist to end of chain 430 if ( freelist != NULL ) { 431 while ( block->_next != NULL ) block = block->_next; 432 block->_next = freelist; 433 } 434 block = NULL; 435 } 436 if (block != NULL) { 437 // Return blocks to free list 438 // locking with safepoint checking introduces a potential deadlock: 439 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock 440 // - another would hold Threads_lock (jni_AttachCurrentThread) and then 441 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) 442 MutexLockerEx ml(JNIHandleBlockFreeList_lock, 443 Mutex::_no_safepoint_check_flag); 444 while (block != NULL) { 445 block->zap(); 446 JNIHandleBlock* next = block->_next; 447 block->_next = _block_free_list; 448 _block_free_list = block; 449 block = next; 450 } 451 } 452 if (pop_frame_link != NULL) { 453 // As a sanity check we release blocks pointed to by the pop_frame_link. 454 // This should never happen (only if PopLocalFrame is not called the 455 // correct number of times). 456 release_block(pop_frame_link, thread); 457 } 458 } 459 460 461 void JNIHandleBlock::oops_do(OopClosure* f) { 462 JNIHandleBlock* current_chain = this; 463 // Iterate over chain of blocks, followed by chains linked through the 464 // pop frame links. 465 while (current_chain != NULL) { 466 for (JNIHandleBlock* current = current_chain; current != NULL; 467 current = current->_next) { 468 assert(current == current_chain || current->pop_frame_link() == NULL, 469 "only blocks first in chain should have pop frame link set"); 470 for (int index = 0; index < current->_top; index++) { 471 oop* root = &(current->_handles)[index]; 472 oop value = *root; 473 // traverse heap pointers only, not deleted handles or free list 474 // pointers 475 if (value != NULL && Universe::heap()->is_in_reserved(value)) { 476 f->do_oop(root); 477 } 478 } 479 // the next handle block is valid only if current block is full 480 if (current->_top < block_size_in_oops) { 481 break; 482 } 483 } 484 current_chain = current_chain->pop_frame_link(); 485 } 486 } 487 488 489 jobject JNIHandleBlock::allocate_handle(oop obj) { 490 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); 491 if (_top == 0) { 492 // This is the first allocation or the initial block got zapped when 493 // entering a native function. If we have any following blocks they are 494 // not valid anymore. 495 for (JNIHandleBlock* current = _next; current != NULL; 496 current = current->_next) { 497 assert(current->_last == NULL, "only first block should have _last set"); 498 assert(current->_free_list == NULL, 499 "only first block should have _free_list set"); 500 if (current->_top == 0) { 501 // All blocks after the first clear trailing block are already cleared. 502 #ifdef ASSERT 503 for (current = current->_next; current != NULL; current = current->_next) { 504 assert(current->_top == 0, "trailing blocks must already be cleared"); 505 } 506 #endif 507 break; 508 } 509 current->_top = 0; 510 current->zap(); 511 } 512 // Clear initial block 513 _free_list = NULL; 514 _allocate_before_rebuild = 0; 515 _last = this; 516 zap(); 517 } 518 519 // Try last block 520 if (_last->_top < block_size_in_oops) { 521 oop* handle = &(_last->_handles)[_last->_top++]; 522 *handle = obj; 523 return (jobject) handle; 524 } 525 526 // Try free list 527 if (_free_list != NULL) { 528 oop* handle = _free_list; 529 _free_list = (oop*) *_free_list; 530 *handle = obj; 531 return (jobject) handle; 532 } 533 // Check if unused block follow last 534 if (_last->_next != NULL) { 535 // update last and retry 536 _last = _last->_next; 537 return allocate_handle(obj); 538 } 539 540 // No space available, we have to rebuild free list or expand 541 if (_allocate_before_rebuild == 0) { 542 rebuild_free_list(); // updates _allocate_before_rebuild counter 543 } else { 544 // Append new block 545 Thread* thread = Thread::current(); 546 Handle obj_handle(thread, obj); 547 // This can block, so we need to preserve obj across call. 548 _last->_next = JNIHandleBlock::allocate_block(thread); 549 _last = _last->_next; 550 _allocate_before_rebuild--; 551 obj = obj_handle(); 552 } 553 return allocate_handle(obj); // retry 554 } 555 556 void JNIHandleBlock::rebuild_free_list() { 557 assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking"); 558 int free = 0; 559 int blocks = 0; 560 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { 561 for (int index = 0; index < current->_top; index++) { 562 oop* handle = &(current->_handles)[index]; 563 if (*handle == NULL) { 564 // this handle was cleared out by a delete call, reuse it 565 *handle = (oop) _free_list; 566 _free_list = handle; 567 free++; 568 } 569 } 570 // we should not rebuild free list if there are unused handles at the end 571 assert(current->_top == block_size_in_oops, "just checking"); 572 blocks++; 573 } 574 // Heuristic: if more than half of the handles are free we rebuild next time 575 // as well, otherwise we append a corresponding number of new blocks before 576 // attempting a free list rebuild again. 577 int total = blocks * block_size_in_oops; 578 int extra = total - 2*free; 579 if (extra > 0) { 580 // Not as many free handles as we would like - compute number of new blocks to append 581 _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops; 582 } 583 } 584 585 586 bool JNIHandleBlock::contains(jobject handle) const { 587 return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]); 588 } 589 590 591 bool JNIHandleBlock::chain_contains(jobject handle) const { 592 for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) { 593 if (current->contains(handle)) { 594 return true; 595 } 596 } 597 return false; 598 } 599 600 601 size_t JNIHandleBlock::length() const { 602 size_t result = 1; 603 for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) { 604 result++; 605 } 606 return result; 607 } 608 609 class CountJNIHandleClosure: public OopClosure { 610 private: 611 int _count; 612 public: 613 CountJNIHandleClosure(): _count(0) {} 614 virtual void do_oop(oop* ooph) { _count++; } 615 virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); } 616 int count() { return _count; } 617 }; 618 619 const size_t JNIHandleBlock::get_number_of_live_handles() { 620 CountJNIHandleClosure counter; 621 oops_do(&counter); 622 return counter.count(); 623 } 624 625 // This method is not thread-safe, i.e., must be called while holding a lock on the 626 // structure. 627 size_t JNIHandleBlock::memory_usage() const { 628 return length() * sizeof(JNIHandleBlock); 629 } 630 631 632 #ifndef PRODUCT 633 634 bool JNIHandles::is_local_handle(jobject handle) { 635 return JNIHandleBlock::any_contains(handle); 636 } 637 638 bool JNIHandleBlock::any_contains(jobject handle) { 639 assert(handle != NULL, "precondition"); 640 for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) { 641 if (current->contains(handle)) { 642 return true; 643 } 644 } 645 return false; 646 } 647 648 void JNIHandleBlock::print_statistics() { 649 int used_blocks = 0; 650 int free_blocks = 0; 651 int used_handles = 0; 652 int free_handles = 0; 653 JNIHandleBlock* block = _block_list; 654 while (block != NULL) { 655 if (block->_top > 0) { 656 used_blocks++; 657 } else { 658 free_blocks++; 659 } 660 used_handles += block->_top; 661 free_handles += (block_size_in_oops - block->_top); 662 block = block->_block_list_link; 663 } 664 tty->print_cr("JNIHandleBlocks statistics"); 665 tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks); 666 tty->print_cr("- blocks in use: %d", used_blocks); 667 tty->print_cr("- blocks free: %d", free_blocks); 668 tty->print_cr("- handles in use: %d", used_handles); 669 tty->print_cr("- handles free: %d", free_handles); 670 } 671 672 #endif