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