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 #ifndef SHARE_RUNTIME_JNIHANDLES_HPP 26 #define SHARE_RUNTIME_JNIHANDLES_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/handles.hpp" 30 31 class JavaThread; 32 class OopStorage; 33 class Thread; 34 35 // Interface for creating and resolving local/global JNI handles 36 37 class JNIHandles : AllStatic { 38 friend class VMStructs; 39 private: 40 // These are used by the serviceability agent. 41 static OopStorage* _global_handles; 42 static OopStorage* _weak_global_handles; 43 friend void jni_handles_init(); 44 45 static OopStorage* global_handles(); 46 static OopStorage* weak_global_handles(); 47 48 inline static bool is_jweak(jobject handle); 49 inline static oop* jobject_ptr(jobject handle); // NOT jweak! 50 inline static oop* jweak_ptr(jobject handle); 51 52 template <DecoratorSet decorators, bool external_guard> inline static oop resolve_impl(jobject handle); 53 54 // Resolve handle into oop, without keeping the object alive 55 inline static oop resolve_no_keepalive(jobject handle); 56 57 // This method is not inlined in order to avoid circular includes between 58 // this header file and thread.hpp. 59 static bool current_thread_in_native(); 60 61 public: 62 // Low tag bit in jobject used to distinguish a jweak. jweak is 63 // type equivalent to jobject, but there are places where we need to 64 // be able to distinguish jweak values from other jobjects, and 65 // is_weak_global_handle is unsuitable for performance reasons. To 66 // provide such a test we add weak_tag_value to the (aligned) byte 67 // address designated by the jobject to produce the corresponding 68 // jweak. Accessing the value of a jobject must account for it 69 // being a possibly offset jweak. 70 static const uintptr_t weak_tag_size = 1; 71 static const uintptr_t weak_tag_alignment = (1u << weak_tag_size); 72 static const uintptr_t weak_tag_mask = weak_tag_alignment - 1; 73 static const int weak_tag_value = 1; 74 75 // Resolve handle into oop 76 inline static oop resolve(jobject handle); 77 // Resolve handle into oop, result guaranteed not to be null 78 inline static oop resolve_non_null(jobject handle); 79 // Resolve externally provided handle into oop with some guards 80 static oop resolve_external_guard(jobject handle); 81 82 // Check for equality without keeping objects alive 83 static bool is_same_object(jobject handle1, jobject handle2); 84 85 // Local handles 86 static jobject make_local(oop obj); 87 static jobject make_local(Thread* thread, oop obj, // Faster version when current thread is known 88 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 89 inline static void destroy_local(jobject handle); 90 91 // Global handles 92 static jobject make_global(Handle obj, 93 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 94 static void destroy_global(jobject handle); 95 96 // Weak global handles 97 static jobject make_weak_global(Handle obj, 98 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 99 static void destroy_weak_global(jobject handle); 100 static bool is_global_weak_cleared(jweak handle); // Test jweak without resolution 101 102 // Initialization 103 static void initialize(); 104 105 // Debugging 106 static void print_on(outputStream* st); 107 static void print(); 108 static void verify(); 109 // The category predicates all require handle != NULL. 110 static bool is_local_handle(Thread* thread, jobject handle); 111 static bool is_frame_handle(JavaThread* thread, jobject handle); 112 static bool is_global_handle(jobject handle); 113 static bool is_weak_global_handle(jobject handle); 114 static size_t global_handle_memory_usage(); 115 static size_t weak_global_handle_memory_usage(); 116 117 #ifndef PRODUCT 118 // Is handle from any local block of any thread? 119 static bool is_local_handle(jobject handle); 120 #endif 121 122 // precondition: handle != NULL. 123 static jobjectRefType handle_type(Thread* thread, jobject handle); 124 125 // Garbage collection support(global handles only, local handles are traversed from thread) 126 // Traversal of regular global handles 127 static void oops_do(OopClosure* f); 128 // Traversal of weak global handles. Unreachable oops are cleared. 129 static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); 130 // Traversal of weak global handles. 131 static void weak_oops_do(OopClosure* f); 132 133 static bool is_global_storage(const OopStorage* storage); 134 }; 135 136 137 138 // JNI handle blocks holding local/global JNI handles 139 140 class JNIHandleBlock : public CHeapObj<mtInternal> { 141 friend class VMStructs; 142 friend class ZeroInterpreter; 143 144 private: 145 enum SomeConstants { 146 block_size_in_oops = 32 // Number of handles per handle block 147 }; 148 149 uintptr_t _handles[block_size_in_oops]; // The handles 150 int _top; // Index of next unused handle 151 JNIHandleBlock* _next; // Link to next block 152 153 // The following instance variables are only used by the first block in a chain. 154 // Having two types of blocks complicates the code and the space overhead in negligible. 155 JNIHandleBlock* _last; // Last block in use 156 JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call 157 uintptr_t* _free_list; // Handle free list 158 int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list 159 160 // Check JNI, "planned capacity" for current frame (or push/ensure) 161 size_t _planned_capacity; 162 163 #ifndef PRODUCT 164 JNIHandleBlock* _block_list_link; // Link for list below 165 static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only) 166 #endif 167 168 static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks 169 static int _blocks_allocated; // For debugging/printing 170 171 // Fill block with bad_handle values 172 void zap() NOT_DEBUG_RETURN; 173 174 // Free list computation 175 void rebuild_free_list(); 176 177 // No more handles in the both the current and following blocks 178 void clear() { _top = 0; } 179 180 public: 181 // Handle allocation 182 jobject allocate_handle(oop obj, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 183 184 // Block allocation and block free list management 185 static JNIHandleBlock* allocate_block(Thread* thread = NULL, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 186 static void release_block(JNIHandleBlock* block, Thread* thread = NULL); 187 188 // JNI PushLocalFrame/PopLocalFrame support 189 JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; } 190 void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; } 191 192 // Stub generator support 193 static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); } 194 195 // Garbage collection support 196 // Traversal of handles 197 void oops_do(OopClosure* f); 198 199 // Checked JNI support 200 void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; } 201 const size_t get_planned_capacity() { return _planned_capacity; } 202 const size_t get_number_of_live_handles(); 203 204 // Debugging 205 bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle 206 bool contains(jobject handle) const; // Does this block contain handle 207 size_t length() const; // Length of chain starting with this block 208 size_t memory_usage() const; 209 #ifndef PRODUCT 210 static bool any_contains(jobject handle); // Does any block currently in use contain handle 211 static void print_statistics(); 212 #endif 213 }; 214 215 #endif // SHARE_RUNTIME_JNIHANDLES_HPP