1 /*
   2  * Copyright (c) 1998, 2014, 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 #ifndef SHARE_VM_RUNTIME_JNIHANDLES_HPP
  26 #define SHARE_VM_RUNTIME_JNIHANDLES_HPP
  27 
  28 #include "runtime/handles.hpp"
  29 #include "utilities/top.hpp"
  30 #include "oops/oop.hpp"
  31 
  32 class JNIHandleBlock;
  33 
  34 
  35 // Interface for creating and resolving local/global JNI handles
  36 
  37 class JNIHandles : AllStatic {
  38   friend class VMStructs;
  39  private:
  40   static JNIHandleBlock* _global_handles;             // First global handle block
  41   static JNIHandleBlock* _weak_global_handles;        // First weak global handle block
  42   static oop _deleted_handle;                         // Sentinel marking deleted handles
  43 
  44  public:
  45   // Resolve handle into oop
  46   inline static oop resolve(jobject handle);
  47   // Resolve externally provided handle into oop with some guards
  48   inline static oop resolve_external_guard(jobject handle);
  49   // Resolve handle into oop, result guaranteed not to be null
  50   inline static oop resolve_non_null(jobject handle);
  51 
  52   // Local handles
  53   static jobject make_local(oop obj);
  54   static jobject make_local(JNIEnv* env, oop obj);    // Fast version when env is known
  55   static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known
  56   inline static void destroy_local(jobject handle);
  57 
  58   // Global handles
  59   static jobject make_global(Handle  obj);
  60   static void destroy_global(jobject handle);
  61 
  62   // Weak global handles
  63   static jobject make_weak_global(Handle obj);
  64   static void destroy_weak_global(jobject handle);
  65 
  66   // Sentinel marking deleted handles in block. Note that we cannot store NULL as
  67   // the sentinel, since clearing weak global JNI refs are done by storing NULL in
  68   // the handle. The handle may not be reused before destroy_weak_global is called.
  69   static oop deleted_handle()   { return _deleted_handle; }
  70 
  71   // Initialization
  72   static void initialize();
  73 
  74   // Debugging
  75   static void print_on(outputStream* st);
  76   static void print()           { print_on(tty); }
  77   static void verify();
  78   static bool is_local_handle(Thread* thread, jobject handle);
  79   static bool is_frame_handle(JavaThread* thr, jobject obj);
  80   static bool is_global_handle(jobject handle);
  81   static bool is_weak_global_handle(jobject handle);
  82   static long global_handle_memory_usage();
  83   static long weak_global_handle_memory_usage();
  84 
  85   // Garbage collection support(global handles only, local handles are traversed from thread)
  86   // Traversal of regular global handles
  87   static void oops_do(OopClosure* f);
  88   // Traversal of weak global handles. Unreachable oops are cleared.
  89   static size_t weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
  90 };
  91 
  92 
  93 
  94 // JNI handle blocks holding local/global JNI handles
  95 
  96 class JNIHandleBlock : public CHeapObj<mtInternal> {
  97   friend class VMStructs;
  98   friend class CppInterpreter;
  99 
 100  private:
 101   enum SomeConstants {
 102     block_size_in_oops  = 32                    // Number of handles per handle block
 103   };
 104 
 105   oop             _handles[block_size_in_oops]; // The handles
 106   int             _top;                         // Index of next unused handle
 107   JNIHandleBlock* _next;                        // Link to next block
 108 
 109   // The following instance variables are only used by the first block in a chain.
 110   // Having two types of blocks complicates the code and the space overhead in negligible.
 111   JNIHandleBlock* _last;                        // Last block in use
 112   JNIHandleBlock* _pop_frame_link;              // Block to restore on PopLocalFrame call
 113   oop*            _free_list;                   // Handle free list
 114   int             _allocate_before_rebuild;     // Number of blocks to allocate before rebuilding free list
 115 
 116   // Check JNI, "planned capacity" for current frame (or push/ensure)
 117   size_t          _planned_capacity;
 118 
 119   #ifndef PRODUCT
 120   JNIHandleBlock* _block_list_link;             // Link for list below
 121   static JNIHandleBlock* _block_list;           // List of all allocated blocks (for debugging only)
 122   #endif
 123 
 124   static JNIHandleBlock* _block_free_list;      // Free list of currently unused blocks
 125   static int      _blocks_allocated;            // For debugging/printing
 126 
 127   // Fill block with bad_handle values
 128   void zap();
 129 
 130  protected:
 131   // No more handles in the both the current and following blocks
 132   void clear() { _top = 0; }
 133 
 134  private:
 135   // Free list computation
 136   void rebuild_free_list();
 137 
 138  public:
 139   // Handle allocation
 140   jobject allocate_handle(oop obj);
 141 
 142   // Block allocation and block free list management
 143   static JNIHandleBlock* allocate_block(Thread* thread = NULL);
 144   static void release_block(JNIHandleBlock* block, Thread* thread = NULL);
 145 
 146   // JNI PushLocalFrame/PopLocalFrame support
 147   JNIHandleBlock* pop_frame_link() const          { return _pop_frame_link; }
 148   void set_pop_frame_link(JNIHandleBlock* block)  { _pop_frame_link = block; }
 149 
 150   // Stub generator support
 151   static int top_offset_in_bytes()                { return offset_of(JNIHandleBlock, _top); }
 152 
 153   // Garbage collection support
 154   // Traversal of regular handles
 155   void oops_do(OopClosure* f);
 156   // Traversal of weak handles. Unreachable oops are cleared.
 157   size_t weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
 158 
 159   // Checked JNI support
 160   void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; }
 161   const size_t get_planned_capacity() { return _planned_capacity; }
 162   const size_t get_number_of_live_handles();
 163 
 164   // Debugging
 165   bool chain_contains(jobject handle) const;    // Does this block or following blocks contain handle
 166   bool contains(jobject handle) const;          // Does this block contain handle
 167   int length() const;                           // Length of chain starting with this block
 168   long memory_usage() const;
 169   #ifndef PRODUCT
 170   static bool any_contains(jobject handle);     // Does any block currently in use contain handle
 171   static void print_statistics();
 172   #endif
 173 };
 174 
 175 
 176 inline oop JNIHandles::resolve(jobject handle) {
 177   oop result = (handle == NULL ? (oop)NULL : *(oop*)handle);
 178   assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle");
 179   assert(result != badJNIHandle, "Pointing to zapped jni handle area");
 180   return result;
 181 };
 182 
 183 
 184 inline oop JNIHandles::resolve_external_guard(jobject handle) {
 185   if (handle == NULL) return NULL;
 186   oop result = *(oop*)handle;
 187   if (result == NULL || result == badJNIHandle) return NULL;
 188   return result;
 189 };
 190 
 191 
 192 inline oop JNIHandles::resolve_non_null(jobject handle) {
 193   assert(handle != NULL, "JNI handle should not be null");
 194   oop result = *(oop*)handle;
 195   assert(result != NULL, "Invalid value read from jni handle");
 196   assert(result != badJNIHandle, "Pointing to zapped jni handle area");
 197   // Don't let that private _deleted_handle object escape into the wild.
 198   assert(result != deleted_handle(), "Used a deleted global handle.");
 199   return result;
 200 };
 201 
 202 inline void JNIHandles::destroy_local(jobject handle) {
 203   if (handle != NULL) {
 204     *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
 205   }
 206 }
 207 
 208 #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP