1 /*
   2  * Copyright (c) 1998, 2019, 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/systemDictionary.hpp"
  27 #include "oops/oop.inline.hpp"
  28 #include "prims/jvmtiExport.hpp"
  29 #include "runtime/jniHandles.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 
  33 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  34 
  35 JNIHandleBlock* JNIHandles::_global_handles       = NULL;
  36 JNIHandleBlock* JNIHandles::_weak_global_handles  = NULL;
  37 oop             JNIHandles::_deleted_handle       = NULL;
  38 
  39 
  40 jobject JNIHandles::make_local(oop obj) {
  41   if (obj == NULL) {
  42     return NULL;                // ignore null handles
  43   } else {
  44     Thread* thread = Thread::current();
  45     assert(Universe::heap()->is_in_reserved(obj), "sanity check");
  46     return thread->active_handles()->allocate_handle(obj);
  47   }
  48 }
  49 
  50 
  51 // optimized versions
  52 
  53 jobject JNIHandles::make_local(Thread* thread, oop obj) {
  54   if (obj == NULL) {
  55     return NULL;                // ignore null handles
  56   } else {
  57     assert(Universe::heap()->is_in_reserved(obj), "sanity check");
  58     return thread->active_handles()->allocate_handle(obj);
  59   }
  60 }
  61 
  62 
  63 jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
  64   if (obj == NULL) {
  65     return NULL;                // ignore null handles
  66   } else {
  67     JavaThread* thread = JavaThread::thread_from_jni_environment(env);
  68     assert(Universe::heap()->is_in_reserved(obj), "sanity check");
  69     return thread->active_handles()->allocate_handle(obj);
  70   }
  71 }
  72 
  73 
  74 jobject JNIHandles::make_global(Handle obj) {
  75   assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
  76   jobject res = NULL;
  77   if (!obj.is_null()) {
  78     // ignore null handles
  79     MutexLocker ml(JNIGlobalHandle_lock);
  80     assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
  81     res = _global_handles->allocate_handle(obj());
  82   } else {
  83     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
  84   }
  85 
  86   return res;
  87 }
  88 
  89 
  90 jobject JNIHandles::make_weak_global(Handle obj) {
  91   assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
  92   jobject res = NULL;
  93   if (!obj.is_null()) {
  94     // ignore null handles
  95     MutexLocker ml(JNIGlobalHandle_lock);
  96     assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
  97     res = _weak_global_handles->allocate_handle(obj());
  98   } else {
  99     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
 100   }
 101   return res;
 102 }
 103 
 104 
 105 void JNIHandles::destroy_global(jobject handle) {
 106   if (handle != NULL) {
 107     assert(is_global_handle(handle), "Invalid delete of global JNI handle");
 108     *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
 109   }
 110 }
 111 
 112 
 113 void JNIHandles::destroy_weak_global(jobject handle) {
 114   if (handle != NULL) {
 115     assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle");
 116     *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
 117   }
 118 }
 119 
 120 
 121 void JNIHandles::oops_do(OopClosure* f) {
 122   f->do_oop(&_deleted_handle);
 123   _global_handles->oops_do(f);
 124 }
 125 
 126 
 127 void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
 128   _weak_global_handles->weak_oops_do(is_alive, f);
 129 }
 130 
 131 
 132 void JNIHandles::initialize() {
 133   _global_handles      = JNIHandleBlock::allocate_block();
 134   _weak_global_handles = JNIHandleBlock::allocate_block();
 135   EXCEPTION_MARK;
 136   // We will never reach the CATCH below since Exceptions::_throw will cause
 137   // the VM to exit if an exception is thrown during initialization
 138   Klass* k      = SystemDictionary::Object_klass();
 139   _deleted_handle = InstanceKlass::cast(k)->allocate_instance(CATCH);
 140 }
 141 
 142 
 143 bool JNIHandles::is_local_handle(Thread* thread, jobject handle) {
 144   JNIHandleBlock* block = thread->active_handles();
 145 
 146   // Look back past possible native calls to jni_PushLocalFrame.
 147   while (block != NULL) {
 148     if (block->chain_contains(handle)) {
 149       return true;
 150     }
 151     block = block->pop_frame_link();
 152   }
 153   return false;
 154 }
 155 
 156 
 157 // Determine if the handle is somewhere in the current thread's stack.
 158 // We easily can't isolate any particular stack frame the handle might
 159 // come from, so we'll check the whole stack.
 160 
 161 bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) {
 162   // If there is no java frame, then this must be top level code, such
 163   // as the java command executable, in which case, this type of handle
 164   // is not permitted.
 165   return (thr->has_last_Java_frame() &&
 166          (void*)obj < (void*)thr->stack_base() &&
 167          (void*)obj >= (void*)thr->last_Java_sp());
 168 }
 169 
 170 
 171 bool JNIHandles::is_global_handle(jobject handle) {
 172   return _global_handles->chain_contains(handle);
 173 }
 174 
 175 
 176 bool JNIHandles::is_weak_global_handle(jobject handle) {
 177   return _weak_global_handles->chain_contains(handle);
 178 }
 179 
 180 long JNIHandles::global_handle_memory_usage() {
 181   return _global_handles->memory_usage();
 182 }
 183 
 184 long JNIHandles::weak_global_handle_memory_usage() {
 185   return _weak_global_handles->memory_usage();
 186 }
 187 
 188 
 189 class AlwaysAliveClosure: public BoolObjectClosure {
 190 public:
 191   bool do_object_b(oop obj) { return true; }
 192 };
 193 
 194 class CountHandleClosure: public OopClosure {
 195 private:
 196   int _count;
 197 public:
 198   CountHandleClosure(): _count(0) {}
 199   virtual void do_oop(oop* ooph) {
 200     if (*ooph != JNIHandles::deleted_handle()) {
 201       _count++;
 202     }
 203   }
 204   virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
 205   int count() { return _count; }
 206 };
 207 
 208 // We assume this is called at a safepoint: no lock is needed.
 209 void JNIHandles::print_on(outputStream* st) {
 210   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 211   assert(_global_handles != NULL && _weak_global_handles != NULL,
 212          "JNIHandles not initialized");
 213 
 214   CountHandleClosure global_handle_count;
 215   AlwaysAliveClosure always_alive;
 216   oops_do(&global_handle_count);
 217   weak_oops_do(&always_alive, &global_handle_count);
 218 
 219   st->print_cr("JNI global references: %d", global_handle_count.count());
 220   st->cr();
 221   st->flush();
 222 }
 223 
 224 class VerifyHandleClosure: public OopClosure {
 225 public:
 226   virtual void do_oop(oop* root) {
 227     (*root)->verify();
 228   }
 229   virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
 230 };
 231 
 232 void JNIHandles::verify() {
 233   VerifyHandleClosure verify_handle;
 234   AlwaysAliveClosure always_alive;
 235 
 236   oops_do(&verify_handle);
 237   weak_oops_do(&always_alive, &verify_handle);
 238 }
 239 
 240 
 241 
 242 void jni_handles_init() {
 243   JNIHandles::initialize();
 244 }
 245 
 246 
 247 int             JNIHandleBlock::_blocks_allocated     = 0;
 248 JNIHandleBlock* JNIHandleBlock::_block_free_list      = NULL;
 249 #ifndef PRODUCT
 250 JNIHandleBlock* JNIHandleBlock::_block_list           = NULL;
 251 #endif
 252 
 253 
 254 void JNIHandleBlock::zap() {
 255   // Zap block values
 256   _top  = 0;
 257   for (int index = 0; index < block_size_in_oops; index++) {
 258     _handles[index] = badJNIHandle;
 259   }
 260 }
 261 
 262 JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread)  {
 263   assert(thread == NULL || thread == Thread::current(), "sanity check");
 264   JNIHandleBlock* block;
 265   // Check the thread-local free list for a block so we don't
 266   // have to acquire a mutex.
 267   if (thread != NULL && thread->free_handle_block() != NULL) {
 268     block = thread->free_handle_block();
 269     thread->set_free_handle_block(block->_next);
 270   }
 271   else {
 272     // locking with safepoint checking introduces a potential deadlock:
 273     // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
 274     // - another would hold Threads_lock (jni_AttachCurrentThread) and then
 275     //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
 276     MutexLockerEx ml(JNIHandleBlockFreeList_lock,
 277                      Mutex::_no_safepoint_check_flag);
 278     if (_block_free_list == NULL) {
 279       // Allocate new block
 280       block = new JNIHandleBlock();
 281       _blocks_allocated++;
 282       if (TraceJNIHandleAllocation) {
 283         tty->print_cr("JNIHandleBlock " INTPTR_FORMAT " allocated (%d total blocks)",
 284                       block, _blocks_allocated);
 285       }
 286       if (ZapJNIHandleArea) block->zap();
 287       #ifndef PRODUCT
 288       // Link new block to list of all allocated blocks
 289       block->_block_list_link = _block_list;
 290       _block_list = block;
 291       #endif
 292     } else {
 293       // Get block from free list
 294       block = _block_free_list;
 295       _block_free_list = _block_free_list->_next;
 296     }
 297   }
 298   block->_top  = 0;
 299   block->_next = NULL;
 300   block->_pop_frame_link = NULL;
 301   block->_planned_capacity = block_size_in_oops;
 302   // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle
 303   debug_only(block->_last = NULL);
 304   debug_only(block->_free_list = NULL);
 305   debug_only(block->_allocate_before_rebuild = -1);
 306   return block;
 307 }
 308 
 309 
 310 void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) {
 311   assert(thread == NULL || thread == Thread::current(), "sanity check");
 312   JNIHandleBlock* pop_frame_link = block->pop_frame_link();
 313   // Put returned block at the beginning of the thread-local free list.
 314   // Note that if thread == NULL, we use it as an implicit argument that
 315   // we _don't_ want the block to be kept on the free_handle_block.
 316   // See for instance JavaThread::exit().
 317   if (thread != NULL ) {
 318     if (ZapJNIHandleArea) block->zap();
 319     JNIHandleBlock* freelist = thread->free_handle_block();
 320     block->_pop_frame_link = NULL;
 321     thread->set_free_handle_block(block);
 322 
 323     // Add original freelist to end of chain
 324     if ( freelist != NULL ) {
 325       while ( block->_next != NULL ) block = block->_next;
 326       block->_next = freelist;
 327     }
 328     block = NULL;
 329   }
 330   if (block != NULL) {
 331     // Return blocks to free list
 332     // locking with safepoint checking introduces a potential deadlock:
 333     // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
 334     // - another would hold Threads_lock (jni_AttachCurrentThread) and then
 335     //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
 336     MutexLockerEx ml(JNIHandleBlockFreeList_lock,
 337                      Mutex::_no_safepoint_check_flag);
 338     while (block != NULL) {
 339       if (ZapJNIHandleArea) block->zap();
 340       JNIHandleBlock* next = block->_next;
 341       block->_next = _block_free_list;
 342       _block_free_list = block;
 343       block = next;
 344     }
 345   }
 346   if (pop_frame_link != NULL) {
 347     // As a sanity check we release blocks pointed to by the pop_frame_link.
 348     // This should never happen (only if PopLocalFrame is not called the
 349     // correct number of times).
 350     release_block(pop_frame_link, thread);
 351   }
 352 }
 353 
 354 
 355 void JNIHandleBlock::oops_do(OopClosure* f) {
 356   JNIHandleBlock* current_chain = this;
 357   // Iterate over chain of blocks, followed by chains linked through the
 358   // pop frame links.
 359   while (current_chain != NULL) {
 360     for (JNIHandleBlock* current = current_chain; current != NULL;
 361          current = current->_next) {
 362       assert(current == current_chain || current->pop_frame_link() == NULL,
 363         "only blocks first in chain should have pop frame link set");
 364       for (int index = 0; index < current->_top; index++) {
 365         oop* root = &(current->_handles)[index];
 366         oop value = *root;
 367         // traverse heap pointers only, not deleted handles or free list
 368         // pointers
 369         if (value != NULL && Universe::heap()->is_in_reserved(value)) {
 370           f->do_oop(root);
 371         }
 372       }
 373       // the next handle block is valid only if current block is full
 374       if (current->_top < block_size_in_oops) {
 375         break;
 376       }
 377     }
 378     current_chain = current_chain->pop_frame_link();
 379   }
 380 }
 381 
 382 
 383 void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive,
 384                                   OopClosure* f) {
 385   for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
 386     assert(current->pop_frame_link() == NULL,
 387       "blocks holding weak global JNI handles should not have pop frame link set");
 388     for (int index = 0; index < current->_top; index++) {
 389       oop* root = &(current->_handles)[index];
 390       oop value = *root;
 391       // traverse heap pointers only, not deleted handles or free list pointers
 392       if (value != NULL && Universe::heap()->is_in_reserved(value)) {
 393         if (is_alive->do_object_b(value)) {
 394           // The weakly referenced object is alive, update pointer
 395           f->do_oop(root);
 396         } else {
 397           // The weakly referenced object is not alive, clear the reference by storing NULL
 398           if (TraceReferenceGC) {
 399             tty->print_cr("Clearing JNI weak reference (" INTPTR_FORMAT ")", root);
 400           }
 401           *root = NULL;
 402         }
 403       }
 404     }
 405     // the next handle block is valid only if current block is full
 406     if (current->_top < block_size_in_oops) {
 407       break;
 408     }
 409   }
 410 
 411   /*
 412    * JVMTI data structures may also contain weak oops.  The iteration of them
 413    * is placed here so that we don't need to add it to each of the collectors.
 414    */
 415   JvmtiExport::weak_oops_do(is_alive, f);
 416   TRACE_WEAK_OOPS_DO(is_alive, f);
 417 }
 418 
 419 
 420 jobject JNIHandleBlock::allocate_handle(oop obj) {
 421   assert(Universe::heap()->is_in_reserved(obj), "sanity check");
 422   if (_top == 0) {
 423     // This is the first allocation or the initial block got zapped when
 424     // entering a native function. If we have any following blocks they are
 425     // not valid anymore.
 426     for (JNIHandleBlock* current = _next; current != NULL;
 427          current = current->_next) {
 428       assert(current->_last == NULL, "only first block should have _last set");
 429       assert(current->_free_list == NULL,
 430              "only first block should have _free_list set");
 431       current->_top = 0;
 432       if (ZapJNIHandleArea) current->zap();
 433     }
 434     // Clear initial block
 435     _free_list = NULL;
 436     _allocate_before_rebuild = 0;
 437     _last = this;
 438     if (ZapJNIHandleArea) zap();
 439   }
 440 
 441   // Try last block
 442   if (_last->_top < block_size_in_oops) {
 443     oop* handle = &(_last->_handles)[_last->_top++];
 444     *handle = obj;
 445     return (jobject) handle;
 446   }
 447 
 448   // Try free list
 449   if (_free_list != NULL) {
 450     oop* handle = _free_list;
 451     _free_list = (oop*) *_free_list;
 452     *handle = obj;
 453     return (jobject) handle;
 454   }
 455   // Check if unused block follow last
 456   if (_last->_next != NULL) {
 457     // update last and retry
 458     _last = _last->_next;
 459     return allocate_handle(obj);
 460   }
 461 
 462   // No space available, we have to rebuild free list or expand
 463   if (_allocate_before_rebuild == 0) {
 464       rebuild_free_list();        // updates _allocate_before_rebuild counter
 465   } else {
 466     // Append new block
 467     Thread* thread = Thread::current();
 468     Handle obj_handle(thread, obj);
 469     // This can block, so we need to preserve obj accross call.
 470     _last->_next = JNIHandleBlock::allocate_block(thread);
 471     _last = _last->_next;
 472     _allocate_before_rebuild--;
 473     obj = obj_handle();
 474   }
 475   return allocate_handle(obj);  // retry
 476 }
 477 
 478 
 479 void JNIHandleBlock::rebuild_free_list() {
 480   assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking");
 481   int free = 0;
 482   int blocks = 0;
 483   for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
 484     for (int index = 0; index < current->_top; index++) {
 485       oop* handle = &(current->_handles)[index];
 486       if (*handle ==  JNIHandles::deleted_handle()) {
 487         // this handle was cleared out by a delete call, reuse it
 488         *handle = (oop) _free_list;
 489         _free_list = handle;
 490         free++;
 491       }
 492     }
 493     // we should not rebuild free list if there are unused handles at the end
 494     assert(current->_top == block_size_in_oops, "just checking");
 495     blocks++;
 496   }
 497   // Heuristic: if more than half of the handles are free we rebuild next time
 498   // as well, otherwise we append a corresponding number of new blocks before
 499   // attempting a free list rebuild again.
 500   int total = blocks * block_size_in_oops;
 501   int extra = total - 2*free;
 502   if (extra > 0) {
 503     // Not as many free handles as we would like - compute number of new blocks to append
 504     _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops;
 505   }
 506   if (TraceJNIHandleAllocation) {
 507     tty->print_cr("Rebuild free list JNIHandleBlock " INTPTR_FORMAT " blocks=%d used=%d free=%d add=%d",
 508       this, blocks, total-free, free, _allocate_before_rebuild);
 509   }
 510 }
 511 
 512 
 513 bool JNIHandleBlock::contains(jobject handle) const {
 514   return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]);
 515 }
 516 
 517 
 518 bool JNIHandleBlock::chain_contains(jobject handle) const {
 519   for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) {
 520     if (current->contains(handle)) {
 521       return true;
 522     }
 523   }
 524   return false;
 525 }
 526 
 527 
 528 int JNIHandleBlock::length() const {
 529   int result = 1;
 530   for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) {
 531     result++;
 532   }
 533   return result;
 534 }
 535 
 536 const size_t JNIHandleBlock::get_number_of_live_handles() {
 537   CountHandleClosure counter;
 538   oops_do(&counter);
 539   return counter.count();
 540 }
 541 
 542 // This method is not thread-safe, i.e., must be called whule holding a lock on the
 543 // structure.
 544 long JNIHandleBlock::memory_usage() const {
 545   return length() * sizeof(JNIHandleBlock);
 546 }
 547 
 548 
 549 #ifndef PRODUCT
 550 
 551 bool JNIHandleBlock::any_contains(jobject handle) {
 552   for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) {
 553     if (current->contains(handle)) {
 554       return true;
 555     }
 556   }
 557   return false;
 558 }
 559 
 560 void JNIHandleBlock::print_statistics() {
 561   int used_blocks = 0;
 562   int free_blocks = 0;
 563   int used_handles = 0;
 564   int free_handles = 0;
 565   JNIHandleBlock* block = _block_list;
 566   while (block != NULL) {
 567     if (block->_top > 0) {
 568       used_blocks++;
 569     } else {
 570       free_blocks++;
 571     }
 572     used_handles += block->_top;
 573     free_handles += (block_size_in_oops - block->_top);
 574     block = block->_block_list_link;
 575   }
 576   tty->print_cr("JNIHandleBlocks statistics");
 577   tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks);
 578   tty->print_cr("- blocks in use:    %d", used_blocks);
 579   tty->print_cr("- blocks free:      %d", free_blocks);
 580   tty->print_cr("- handles in use:   %d", used_handles);
 581   tty->print_cr("- handles free:     %d", free_handles);
 582 }
 583 
 584 #endif