1 /* 2 * Copyright (c) 2012, 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 #ifndef SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP 25 #define SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP 26 27 #include "code/nmethod.hpp" 28 #include "jvmci/jvmci.hpp" 29 #include "jvmci/jvmciExceptions.hpp" 30 #include "jvmci/jvmciObject.hpp" 31 32 class JVMCIEnv; 33 class JVMCICompiler; 34 class JVMCICompileState; 35 class MetadataHandles; 36 37 // Encapsulates the JVMCI metadata for an nmethod. 38 // JVMCINMethodData objects are inlined into nmethods 39 // at nmethod::_jvmci_data_offset. 40 class JVMCINMethodData { 41 // Index for the HotSpotNmethod mirror in the nmethod's oops table. 42 // This is -1 if there is no mirror in the oops table. 43 int _nmethod_mirror_index; 44 45 // Is HotSpotNmethod.name non-null? If so, the value is 46 // embedded in the end of this object. 47 bool _has_name; 48 49 // Address of the failed speculations list to which a speculation 50 // is appended when it causes a deoptimization. 51 FailedSpeculation** _failed_speculations; 52 53 public: 54 // Computes the size of a JVMCINMethodData object 55 static int compute_size(const char* nmethod_mirror_name) { 56 int size = sizeof(JVMCINMethodData); 57 if (nmethod_mirror_name != NULL) { 58 size += (int) strlen(nmethod_mirror_name) + 1; 59 } 60 return size; 61 } 62 63 void initialize(int nmethod_mirror_index, 64 const char* name, 65 FailedSpeculation** failed_speculations); 66 67 // Adds `speculation` to the failed speculations list. 68 void add_failed_speculation(nmethod* nm, jlong speculation); 69 70 // Gets the JVMCI name of the nmethod (which may be NULL). 71 const char* name() { return _has_name ? (char*)(((address) this) + sizeof(JVMCINMethodData)) : NULL; } 72 73 // Clears the HotSpotNmethod.address field in the mirror. If nm 74 // is dead, the HotSpotNmethod.entryPoint field is also cleared. 75 void invalidate_nmethod_mirror(nmethod* nm); 76 77 // Gets the mirror from nm's oops table. 78 oop get_nmethod_mirror(nmethod* nm, bool phantom_ref); 79 80 // Sets the mirror in nm's oops table. 81 void set_nmethod_mirror(nmethod* nm, oop mirror); 82 83 // Clears the mirror in nm's oops table. 84 void clear_nmethod_mirror(nmethod* nm); 85 }; 86 87 // A top level class that represents an initialized JVMCI runtime. 88 // There is one instance of this class per HotSpotJVMCIRuntime object. 89 class JVMCIRuntime: public CHeapObj<mtJVMCI> { 90 friend class JVMCI; 91 public: 92 // Constants describing whether JVMCI wants to be able to adjust the compilation 93 // level selected for a method by the VM compilation policy and if so, based on 94 // what information about the method being schedule for compilation. 95 enum CompLevelAdjustment { 96 none = 0, // no adjustment 97 by_holder = 1, // adjust based on declaring class of method 98 by_full_signature = 2 // adjust based on declaring class, name and signature of method 99 }; 100 101 private: 102 103 enum InitState { 104 uninitialized, 105 being_initialized, 106 fully_initialized 107 }; 108 109 // Initialization state of this JVMCIRuntime. 110 InitState _init_state; 111 112 JVMCIObject _HotSpotJVMCIRuntime_instance; 113 114 // Result of calling JNI_CreateJavaVM in the JVMCI shared library. 115 // Must only be modified under JVMCI_lock. 116 volatile JavaVM* _shared_library_javavm; 117 118 // The HotSpot heap based runtime will have an id of -1 and the 119 // JVMCI shared library runtime will have an id of 0. 120 int _id; 121 122 // Handles to objects in the HotSpot heap. 123 OopStorage* _object_handles; 124 125 // Handles to Metadata objects. 126 MetadataHandles* _metadata_handles; 127 128 JVMCIObject create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS); 129 130 // Implementation methods for loading and constant pool access. 131 static Klass* get_klass_by_name_impl(Klass*& accessing_klass, 132 const constantPoolHandle& cpool, 133 Symbol* klass_name, 134 bool require_local); 135 static Klass* get_klass_by_index_impl(const constantPoolHandle& cpool, 136 int klass_index, 137 bool& is_accessible, 138 Klass* loading_klass); 139 static void get_field_by_index_impl(InstanceKlass* loading_klass, fieldDescriptor& fd, 140 int field_index); 141 static methodHandle get_method_by_index_impl(const constantPoolHandle& cpool, 142 int method_index, Bytecodes::Code bc, 143 InstanceKlass* loading_klass); 144 145 // Helper methods 146 static bool check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass); 147 static methodHandle lookup_method(InstanceKlass* accessor, 148 Klass* holder, 149 Symbol* name, 150 Symbol* sig, 151 Bytecodes::Code bc, 152 constantTag tag); 153 154 static OopStorage* create_object_handles(int id); 155 156 public: 157 JVMCIRuntime(int id); 158 159 int id() const { return _id; } 160 161 // Ensures that a JVMCI shared library JavaVM exists for this runtime. 162 // If the JavaVM was created by this call, then the thread-local JNI 163 // interface pointer for the JavaVM is returned otherwise NULL is returned. 164 JNIEnv* init_shared_library_javavm(); 165 166 // Determines if the JVMCI shared library JavaVM exists for this runtime. 167 bool has_shared_library_javavm() { return _shared_library_javavm != NULL; } 168 169 // Copies info about the JVMCI shared library JavaVM associated with this 170 // runtime into `info` as follows: 171 // { 172 // javaVM, // the {@code JavaVM*} value 173 // javaVM->functions->reserved0, 174 // javaVM->functions->reserved1, 175 // javaVM->functions->reserved2 176 // } 177 void init_JavaVM_info(jlongArray info, JVMCI_TRAPS); 178 179 // Wrappers for calling Invocation Interface functions on the 180 // JVMCI shared library JavaVM associated with this runtime. 181 // These wrappers ensure all required thread state transitions are performed. 182 jint AttachCurrentThread(JavaThread* thread, void **penv, void *args); 183 jint AttachCurrentThreadAsDaemon(JavaThread* thread, void **penv, void *args); 184 jint DetachCurrentThread(JavaThread* thread); 185 jint GetEnv(JavaThread* thread, void **penv, jint version); 186 187 // Compute offsets and construct any state required before executing JVMCI code. 188 void initialize(JVMCIEnv* jvmciEnv); 189 190 // Allocation and management of JNI global object handles. 191 jobject make_global(const Handle& obj); 192 void destroy_global(jobject handle); 193 bool is_global_handle(jobject handle); 194 195 // Allocation and management of matadata handles. 196 jmetadata allocate_handle(const methodHandle& handle); 197 jmetadata allocate_handle(const constantPoolHandle& handle); 198 void release_handle(jmetadata handle); 199 200 // Gets the HotSpotJVMCIRuntime instance for this runtime, 201 // initializing it first if necessary. 202 JVMCIObject get_HotSpotJVMCIRuntime(JVMCI_TRAPS); 203 204 bool is_HotSpotJVMCIRuntime_initialized() { 205 return _HotSpotJVMCIRuntime_instance.is_non_null(); 206 } 207 208 // Gets the current HotSpotJVMCIRuntime instance for this runtime which 209 // may be a "null" JVMCIObject value. 210 JVMCIObject probe_HotSpotJVMCIRuntime() { 211 return _HotSpotJVMCIRuntime_instance; 212 } 213 214 // Trigger initialization of HotSpotJVMCIRuntime through JVMCI.getRuntime() 215 void initialize_JVMCI(JVMCI_TRAPS); 216 217 // Explicitly initialize HotSpotJVMCIRuntime itself 218 void initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS); 219 220 void call_getCompiler(TRAPS); 221 222 // Shuts down this runtime by calling HotSpotJVMCIRuntime.shutdown(). 223 void shutdown(); 224 225 void bootstrap_finished(TRAPS); 226 227 // Look up a klass by name from a particular class loader (the accessor's). 228 // If require_local, result must be defined in that class loader, or NULL. 229 // If !require_local, a result from remote class loader may be reported, 230 // if sufficient class loader constraints exist such that initiating 231 // a class loading request from the given loader is bound to return 232 // the class defined in the remote loader (or throw an error). 233 // 234 // Return an unloaded klass if !require_local and no class at all is found. 235 // 236 // The CI treats a klass as loaded if it is consistently defined in 237 // another loader, even if it hasn't yet been loaded in all loaders 238 // that could potentially see it via delegation. 239 static Klass* get_klass_by_name(Klass* accessing_klass, 240 Symbol* klass_name, 241 bool require_local); 242 243 // Constant pool access. 244 static Klass* get_klass_by_index(const constantPoolHandle& cpool, 245 int klass_index, 246 bool& is_accessible, 247 Klass* loading_klass); 248 static void get_field_by_index(InstanceKlass* loading_klass, fieldDescriptor& fd, 249 int field_index); 250 static methodHandle get_method_by_index(const constantPoolHandle& cpool, 251 int method_index, Bytecodes::Code bc, 252 InstanceKlass* loading_klass); 253 254 // converts the Klass* representing the holder of a method into a 255 // InstanceKlass*. This is needed since the holder of a method in 256 // the bytecodes could be an array type. Basically this converts 257 // array types into java/lang/Object and other types stay as they are. 258 static InstanceKlass* get_instance_klass_for_declared_method_holder(Klass* klass); 259 260 // Helper routine for determining the validity of a compilation 261 // with respect to concurrent class loading. 262 static JVMCI::CodeInstallResult validate_compile_task_dependencies(Dependencies* target, JVMCICompileState* task, char** failure_detail); 263 264 // Compiles `target` with the JVMCI compiler. 265 void compile_method(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, const methodHandle& target, int entry_bci); 266 267 // Register the result of a compilation. 268 JVMCI::CodeInstallResult register_method(JVMCIEnv* JVMCIENV, 269 const methodHandle& target, 270 nmethod*& nm, 271 int entry_bci, 272 CodeOffsets* offsets, 273 int orig_pc_offset, 274 CodeBuffer* code_buffer, 275 int frame_words, 276 OopMapSet* oop_map_set, 277 ExceptionHandlerTable* handler_table, 278 ImplicitExceptionTable* implicit_exception_table, 279 AbstractCompiler* compiler, 280 DebugInformationRecorder* debug_info, 281 Dependencies* dependencies, 282 int compile_id, 283 bool has_unsafe_access, 284 bool has_wide_vector, 285 JVMCIObject compiled_code, 286 JVMCIObject nmethod_mirror, 287 FailedSpeculation** failed_speculations, 288 char* speculations, 289 int speculations_len); 290 291 // Exits the VM due to an unexpected exception. 292 static void exit_on_pending_exception(JVMCIEnv* JVMCIENV, const char* message); 293 294 static void describe_pending_hotspot_exception(JavaThread* THREAD, bool clear); 295 296 #define CHECK_EXIT THREAD); \ 297 if (HAS_PENDING_EXCEPTION) { \ 298 char buf[256]; \ 299 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \ 300 JVMCIRuntime::exit_on_pending_exception(NULL, buf); \ 301 return; \ 302 } \ 303 (void)(0 304 305 #define CHECK_EXIT_(v) THREAD); \ 306 if (HAS_PENDING_EXCEPTION) { \ 307 char buf[256]; \ 308 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \ 309 JVMCIRuntime::exit_on_pending_exception(NULL, buf); \ 310 return v; \ 311 } \ 312 (void)(0 313 314 #define JVMCI_CHECK_EXIT JVMCIENV); \ 315 if (JVMCIENV->has_pending_exception()) { \ 316 char buf[256]; \ 317 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \ 318 JVMCIRuntime::exit_on_pending_exception(JVMCIENV, buf); \ 319 return; \ 320 } \ 321 (void)(0 322 323 #define JVMCI_CHECK_EXIT_(result) JVMCIENV); \ 324 if (JVMCIENV->has_pending_exception()) { \ 325 char buf[256]; \ 326 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \ 327 JVMCIRuntime::exit_on_pending_exception(JVMCIENV, buf); \ 328 return result; \ 329 } \ 330 (void)(0 331 332 static BasicType kindToBasicType(const Handle& kind, TRAPS); 333 334 static void new_instance_common(JavaThread* thread, Klass* klass, bool null_on_fail); 335 static void new_array_common(JavaThread* thread, Klass* klass, jint length, bool null_on_fail); 336 static void new_multi_array_common(JavaThread* thread, Klass* klass, int rank, jint* dims, bool null_on_fail); 337 static void dynamic_new_array_common(JavaThread* thread, oopDesc* element_mirror, jint length, bool null_on_fail); 338 static void dynamic_new_instance_common(JavaThread* thread, oopDesc* type_mirror, bool null_on_fail); 339 340 // The following routines are called from compiled JVMCI code 341 342 // When allocation fails, these stubs: 343 // 1. Exercise -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError handling and also 344 // post a JVMTI_EVENT_RESOURCE_EXHAUSTED event if the failure is an OutOfMemroyError 345 // 2. Return NULL with a pending exception. 346 // Compiled code must ensure these stubs are not called twice for the same allocation 347 // site due to the non-repeatable side effects in the case of OOME. 348 static void new_instance(JavaThread* thread, Klass* klass) { new_instance_common(thread, klass, false); } 349 static void new_array(JavaThread* thread, Klass* klass, jint length) { new_array_common(thread, klass, length, false); } 350 static void new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims) { new_multi_array_common(thread, klass, rank, dims, false); } 351 static void dynamic_new_array(JavaThread* thread, oopDesc* element_mirror, jint length) { dynamic_new_array_common(thread, element_mirror, length, false); } 352 static void dynamic_new_instance(JavaThread* thread, oopDesc* type_mirror) { dynamic_new_instance_common(thread, type_mirror, false); } 353 354 // When allocation fails, these stubs return NULL and have no pending exception. Compiled code 355 // can use these stubs if a failed allocation will be retried (e.g., by deoptimizing and 356 // re-executing in the interpreter). 357 static void new_instance_or_null(JavaThread* thread, Klass* klass) { new_instance_common(thread, klass, true); } 358 static void new_array_or_null(JavaThread* thread, Klass* klass, jint length) { new_array_common(thread, klass, length, true); } 359 static void new_multi_array_or_null(JavaThread* thread, Klass* klass, int rank, jint* dims) { new_multi_array_common(thread, klass, rank, dims, true); } 360 static void dynamic_new_array_or_null(JavaThread* thread, oopDesc* element_mirror, jint length) { dynamic_new_array_common(thread, element_mirror, length, true); } 361 static void dynamic_new_instance_or_null(JavaThread* thread, oopDesc* type_mirror) { dynamic_new_instance_common(thread, type_mirror, true); } 362 363 static jboolean thread_is_interrupted(JavaThread* thread, oopDesc* obj, jboolean clear_interrupted); 364 static void vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3); 365 static jint identity_hash_code(JavaThread* thread, oopDesc* obj); 366 static address exception_handler_for_pc(JavaThread* thread); 367 static void monitorenter(JavaThread* thread, oopDesc* obj, BasicLock* lock); 368 static void monitorexit (JavaThread* thread, oopDesc* obj, BasicLock* lock); 369 static jboolean object_notify(JavaThread* thread, oopDesc* obj); 370 static jboolean object_notifyAll(JavaThread* thread, oopDesc* obj); 371 static void vm_error(JavaThread* thread, jlong where, jlong format, jlong value); 372 static oopDesc* load_and_clear_exception(JavaThread* thread); 373 static void log_printf(JavaThread* thread, const char* format, jlong v1, jlong v2, jlong v3); 374 static void log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline); 375 // Print the passed in object, optionally followed by a newline. If 376 // as_string is true and the object is a java.lang.String then it 377 // printed as a string, otherwise the type of the object is printed 378 // followed by its address. 379 static void log_object(JavaThread* thread, oopDesc* object, bool as_string, bool newline); 380 #if INCLUDE_G1GC 381 static void write_barrier_pre(JavaThread* thread, oopDesc* obj); 382 static void write_barrier_post(JavaThread* thread, void* card); 383 #endif 384 static jboolean validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child); 385 386 // used to throw exceptions from compiled JVMCI code 387 static int throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message); 388 // helper methods to throw exception with complex messages 389 static int throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass); 390 static int throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass); 391 392 // Test only function 393 static jint test_deoptimize_call_int(JavaThread* thread, int value); 394 395 // Emits the following on tty: 396 // "JVMCITrace-" <level> "[" <current thread name> "]:" <padding of width `level`> 397 // Returns true. 398 static bool trace_prefix(int level); 399 }; 400 401 // Tracing macros. 402 403 #define IF_TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1)) ; else 404 #define IF_TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2)) ; else 405 #define IF_TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3)) ; else 406 #define IF_TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4)) ; else 407 #define IF_TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5)) ; else 408 409 #define TRACE_jvmci_(n) if (!(JVMCITraceLevel >= n && JVMCIRuntime::trace_prefix(n))) ; else tty->print_cr 410 #define TRACE_jvmci_1 TRACE_jvmci_(1) 411 #define TRACE_jvmci_2 TRACE_jvmci_(2) 412 #define TRACE_jvmci_3 TRACE_jvmci_(3) 413 #define TRACE_jvmci_4 TRACE_jvmci_(4) 414 #define TRACE_jvmci_5 TRACE_jvmci_(5) 415 416 #endif // SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP