1 /*
   2  * Copyright (c) 1998, 2017, 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 "memory/allocation.hpp"
  29 #include "runtime/handles.hpp"
  30 
  31 class JNIHandleBlock;
  32 
  33 
  34 // Interface for creating and resolving local/global JNI handles
  35 
  36 class JNIHandles : AllStatic {
  37   friend class VMStructs;
  38  private:
  39   static JNIHandleBlock* _global_handles;             // First global handle block
  40   static JNIHandleBlock* _weak_global_handles;        // First weak global handle block
  41   static oop _deleted_handle;                         // Sentinel marking deleted handles
  42 
  43   inline static bool is_jweak(jobject handle);
  44   inline static oop& jobject_ref(jobject handle); // NOT jweak!
  45   inline static oop& jweak_ref(jobject handle);
  46 
  47   template<bool external_guard> inline static oop guard_value(oop value);
  48   template<bool external_guard> inline static oop resolve_impl(jobject handle);
  49   template<bool external_guard> static oop resolve_jweak(jweak handle);
  50 




  51  public:
  52   // Low tag bit in jobject used to distinguish a jweak.  jweak is
  53   // type equivalent to jobject, but there are places where we need to
  54   // be able to distinguish jweak values from other jobjects, and
  55   // is_weak_global_handle is unsuitable for performance reasons.  To
  56   // provide such a test we add weak_tag_value to the (aligned) byte
  57   // address designated by the jobject to produce the corresponding
  58   // jweak.  Accessing the value of a jobject must account for it
  59   // being a possibly offset jweak.
  60   static const uintptr_t weak_tag_size = 1;
  61   static const uintptr_t weak_tag_alignment = (1u << weak_tag_size);
  62   static const uintptr_t weak_tag_mask = weak_tag_alignment - 1;
  63   static const int weak_tag_value = 1;
  64 
  65   // Resolve handle into oop
  66   inline static oop resolve(jobject handle);
  67   // Resolve externally provided handle into oop with some guards
  68   inline static oop resolve_external_guard(jobject handle);
  69   // Resolve handle into oop, result guaranteed not to be null
  70   inline static oop resolve_non_null(jobject handle);
  71 
  72   // Local handles
  73   static jobject make_local(oop obj);
  74   static jobject make_local(JNIEnv* env, oop obj);    // Fast version when env is known
  75   static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known
  76   inline static void destroy_local(jobject handle);
  77 
  78   // Global handles
  79   static jobject make_global(Handle  obj);
  80   static void destroy_global(jobject handle);
  81 
  82   // Weak global handles
  83   static jobject make_weak_global(Handle obj);
  84   static void destroy_weak_global(jobject handle);
  85   static bool is_global_weak_cleared(jweak handle); // Test jweak without resolution
  86 
  87   // Sentinel marking deleted handles in block. Note that we cannot store NULL as
  88   // the sentinel, since clearing weak global JNI refs are done by storing NULL in
  89   // the handle. The handle may not be reused before destroy_weak_global is called.
  90   static oop deleted_handle()   { return _deleted_handle; }
  91 
  92   // Initialization
  93   static void initialize();
  94 
  95   // Debugging
  96   static void print_on(outputStream* st);
  97   static void print()           { print_on(tty); }
  98   static void verify();
  99   static bool is_local_handle(Thread* thread, jobject handle);
 100   static bool is_frame_handle(JavaThread* thr, jobject obj);
 101   static bool is_global_handle(jobject handle);
 102   static bool is_weak_global_handle(jobject handle);
 103   static long global_handle_memory_usage();
 104   static long weak_global_handle_memory_usage();
 105 
 106   // Garbage collection support(global handles only, local handles are traversed from thread)
 107   // Traversal of regular global handles
 108   static void oops_do(OopClosure* f);
 109   // Traversal of weak global handles. Unreachable oops are cleared.
 110   static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
 111   // Traversal of weak global handles.
 112   static void weak_oops_do(OopClosure* f);
 113 };
 114 
 115 
 116 
 117 // JNI handle blocks holding local/global JNI handles
 118 
 119 class JNIHandleBlock : public CHeapObj<mtInternal> {
 120   friend class VMStructs;
 121   friend class CppInterpreter;
 122 
 123  private:
 124   enum SomeConstants {
 125     block_size_in_oops  = 32                    // Number of handles per handle block
 126   };
 127 
 128   oop             _handles[block_size_in_oops]; // The handles
 129   int             _top;                         // Index of next unused handle
 130   JNIHandleBlock* _next;                        // Link to next block
 131 
 132   // The following instance variables are only used by the first block in a chain.
 133   // Having two types of blocks complicates the code and the space overhead in negligible.
 134   JNIHandleBlock* _last;                        // Last block in use
 135   JNIHandleBlock* _pop_frame_link;              // Block to restore on PopLocalFrame call
 136   oop*            _free_list;                   // Handle free list
 137   int             _allocate_before_rebuild;     // Number of blocks to allocate before rebuilding free list
 138 
 139   // Check JNI, "planned capacity" for current frame (or push/ensure)
 140   size_t          _planned_capacity;
 141 
 142   #ifndef PRODUCT
 143   JNIHandleBlock* _block_list_link;             // Link for list below
 144   static JNIHandleBlock* _block_list;           // List of all allocated blocks (for debugging only)
 145   #endif
 146 
 147   static JNIHandleBlock* _block_free_list;      // Free list of currently unused blocks
 148   static int      _blocks_allocated;            // For debugging/printing
 149 
 150   // Fill block with bad_handle values
 151   void zap() NOT_DEBUG_RETURN;
 152 
 153   // Free list computation
 154   void rebuild_free_list();
 155 
 156   // No more handles in the both the current and following blocks
 157   void clear() { _top = 0; }
 158 
 159  public:
 160   // Handle allocation
 161   jobject allocate_handle(oop obj);
 162 
 163   // Release Handle
 164   void release_handle(jobject);
 165 
 166   // Block allocation and block free list management
 167   static JNIHandleBlock* allocate_block(Thread* thread = NULL);
 168   static void release_block(JNIHandleBlock* block, Thread* thread = NULL);
 169 
 170   // JNI PushLocalFrame/PopLocalFrame support
 171   JNIHandleBlock* pop_frame_link() const          { return _pop_frame_link; }
 172   void set_pop_frame_link(JNIHandleBlock* block)  { _pop_frame_link = block; }
 173 
 174   // Stub generator support
 175   static int top_offset_in_bytes()                { return offset_of(JNIHandleBlock, _top); }
 176 
 177   // Garbage collection support
 178   // Traversal of regular handles
 179   void oops_do(OopClosure* f);
 180   // Traversal of weak handles. Unreachable oops are cleared.
 181   void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
 182 
 183   // Checked JNI support
 184   void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; }
 185   const size_t get_planned_capacity() { return _planned_capacity; }
 186   const size_t get_number_of_live_handles();
 187 
 188   // Debugging
 189   bool chain_contains(jobject handle) const;    // Does this block or following blocks contain handle
 190   bool contains(jobject handle) const;          // Does this block contain handle
 191   int length() const;                           // Length of chain starting with this block
 192   long memory_usage() const;
 193   #ifndef PRODUCT
 194   static bool any_contains(jobject handle);     // Does any block currently in use contain handle
 195   static void print_statistics();
 196   #endif
 197 };
 198 
 199 inline bool JNIHandles::is_jweak(jobject handle) {
 200   STATIC_ASSERT(weak_tag_size == 1);
 201   STATIC_ASSERT(weak_tag_value == 1);
 202   return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;
 203 }
 204 
 205 inline oop& JNIHandles::jobject_ref(jobject handle) {
 206   assert(!is_jweak(handle), "precondition");
 207   return *reinterpret_cast<oop*>(handle);
 208 }
 209 
 210 inline oop& JNIHandles::jweak_ref(jobject handle) {
 211   assert(is_jweak(handle), "precondition");
 212   char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;
 213   return *reinterpret_cast<oop*>(ptr);
 214 }
 215 
 216 // external_guard is true if called from resolve_external_guard.
 217 // Treat deleted (and possibly zapped) as NULL for external_guard,
 218 // else as (asserted) error.
 219 template<bool external_guard>
 220 inline oop JNIHandles::guard_value(oop value) {
 221   if (!external_guard) {
 222     assert(value != deleted_handle(), "Used a deleted global handle");
 223   } else if (value == deleted_handle()) {
 224     value = NULL;
 225   }
 226   return value;
 227 }
 228 
 229 // external_guard is true if called from resolve_external_guard.
 230 template<bool external_guard>
 231 inline oop JNIHandles::resolve_impl(jobject handle) {
 232   assert(handle != NULL, "precondition");

 233   oop result;
 234   if (is_jweak(handle)) {       // Unlikely
 235     result = resolve_jweak<external_guard>(handle);
 236   } else {
 237     result = jobject_ref(handle);
 238     // Construction of jobjects canonicalize a null value into a null
 239     // jobject, so for non-jweak the pointee should never be null.
 240     assert(external_guard || result != NULL,
 241            "Invalid value read from jni handle");
 242     result = guard_value<external_guard>(result);
 243   }
 244   return result;
 245 }
 246 
 247 inline oop JNIHandles::resolve(jobject handle) {
 248   oop result = NULL;
 249   if (handle != NULL) {
 250     result = resolve_impl<false /* external_guard */ >(handle);
 251   }
 252   return result;
 253 }
 254 
 255 // Resolve some erroneous cases to NULL, rather than treating them as
 256 // possibly unchecked errors.  In particular, deleted handles are
 257 // treated as NULL (though a deleted and later reallocated handle
 258 // isn't detected).
 259 inline oop JNIHandles::resolve_external_guard(jobject handle) {
 260   oop result = NULL;
 261   if (handle != NULL) {
 262     result = resolve_impl<true /* external_guard */ >(handle);
 263   }
 264   return result;
 265 }
 266 
 267 inline oop JNIHandles::resolve_non_null(jobject handle) {
 268   assert(handle != NULL, "JNI handle should not be null");
 269   oop result = resolve_impl<false /* external_guard */ >(handle);
 270   assert(result != NULL, "NULL read from jni handle");
 271   return result;
 272 }
 273 
 274 inline void JNIHandles::destroy_local(jobject handle) {
 275   if (handle != NULL) {
 276     jobject_ref(handle) = deleted_handle();
 277   }
 278 }
 279 
 280 #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP
--- EOF ---