1 /* 2 * Copyright (c) 1999, 2023, 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 "ci/ciConstant.hpp" 27 #include "ci/ciEnv.hpp" 28 #include "ci/ciField.hpp" 29 #include "ci/ciInlineKlass.hpp" 30 #include "ci/ciInstance.hpp" 31 #include "ci/ciInstanceKlass.hpp" 32 #include "ci/ciMethod.hpp" 33 #include "ci/ciNullObject.hpp" 34 #include "ci/ciReplay.hpp" 35 #include "ci/ciSymbols.hpp" 36 #include "ci/ciUtilities.inline.hpp" 37 #include "classfile/javaClasses.hpp" 38 #include "classfile/javaClasses.inline.hpp" 39 #include "classfile/systemDictionary.hpp" 40 #include "classfile/vmClasses.hpp" 41 #include "classfile/vmSymbols.hpp" 42 #include "code/codeCache.hpp" 43 #include "code/scopeDesc.hpp" 44 #include "compiler/compilationLog.hpp" 45 #include "compiler/compilationPolicy.hpp" 46 #include "compiler/compileBroker.hpp" 47 #include "compiler/compilerEvent.hpp" 48 #include "compiler/compileLog.hpp" 49 #include "compiler/compileTask.hpp" 50 #include "compiler/disassembler.hpp" 51 #include "gc/shared/collectedHeap.inline.hpp" 52 #include "interpreter/bytecodeStream.hpp" 53 #include "interpreter/linkResolver.hpp" 54 #include "jfr/jfrEvents.hpp" 55 #include "jvm.h" 56 #include "logging/log.hpp" 57 #include "memory/allocation.inline.hpp" 58 #include "memory/oopFactory.hpp" 59 #include "memory/resourceArea.hpp" 60 #include "memory/universe.hpp" 61 #include "oops/constantPool.inline.hpp" 62 #include "oops/cpCache.inline.hpp" 63 #include "oops/method.inline.hpp" 64 #include "oops/methodData.hpp" 65 #include "oops/objArrayKlass.hpp" 66 #include "oops/objArrayOop.inline.hpp" 67 #include "oops/oop.inline.hpp" 68 #include "oops/symbolHandle.hpp" 69 #include "prims/jvmtiExport.hpp" 70 #include "prims/methodHandles.hpp" 71 #include "runtime/fieldDescriptor.inline.hpp" 72 #include "runtime/handles.inline.hpp" 73 #include "runtime/init.hpp" 74 #include "runtime/javaThread.hpp" 75 #include "runtime/jniHandles.inline.hpp" 76 #include "runtime/reflection.hpp" 77 #include "runtime/safepointVerifiers.hpp" 78 #include "runtime/sharedRuntime.hpp" 79 #include "utilities/dtrace.hpp" 80 #include "utilities/macros.hpp" 81 #ifdef COMPILER1 82 #include "c1/c1_Runtime1.hpp" 83 #endif 84 #ifdef COMPILER2 85 #include "opto/runtime.hpp" 86 #endif 87 88 // ciEnv 89 // 90 // This class is the top level broker for requests from the compiler 91 // to the VM. 92 93 ciObject* ciEnv::_null_object_instance; 94 95 #define VM_CLASS_DEFN(name, ignore_s) ciInstanceKlass* ciEnv::_##name = nullptr; 96 VM_CLASSES_DO(VM_CLASS_DEFN) 97 #undef VM_CLASS_DEFN 98 99 ciSymbol* ciEnv::_unloaded_cisymbol = nullptr; 100 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = nullptr; 101 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = nullptr; 102 103 jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = nullptr; 104 jobject ciEnv::_ArrayStoreException_handle = nullptr; 105 jobject ciEnv::_ClassCastException_handle = nullptr; 106 107 #ifndef PRODUCT 108 static bool firstEnv = true; 109 #endif /* PRODUCT */ 110 111 // ------------------------------------------------------------------ 112 // ciEnv::ciEnv 113 ciEnv::ciEnv(CompileTask* task) 114 : _ciEnv_arena(mtCompiler) { 115 VM_ENTRY_MARK; 116 117 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc. 118 thread->set_env(this); 119 assert(ciEnv::current() == this, "sanity"); 120 121 _oop_recorder = nullptr; 122 _debug_info = nullptr; 123 _dependencies = nullptr; 124 _failure_reason = nullptr; 125 _inc_decompile_count_on_failure = true; 126 _compilable = MethodCompilable; 127 _break_at_compile = false; 128 _compiler_data = nullptr; 129 #ifndef PRODUCT 130 assert(!firstEnv, "not initialized properly"); 131 #endif /* !PRODUCT */ 132 133 _num_inlined_bytecodes = 0; 134 assert(task == nullptr || thread->task() == task, "sanity"); 135 if (task != nullptr) { 136 task->mark_started(os::elapsed_counter()); 137 } 138 _task = task; 139 _log = nullptr; 140 141 // Temporary buffer for creating symbols and such. 142 _name_buffer = nullptr; 143 _name_buffer_len = 0; 144 145 _arena = &_ciEnv_arena; 146 _factory = new (_arena) ciObjectFactory(_arena, 128); 147 148 // Preload commonly referenced system ciObjects. 149 150 // During VM initialization, these instances have not yet been created. 151 // Assertions ensure that these instances are not accessed before 152 // their initialization. 153 154 assert(Universe::is_fully_initialized(), "should be complete"); 155 156 oop o = Universe::null_ptr_exception_instance(); 157 assert(o != nullptr, "should have been initialized"); 158 _NullPointerException_instance = get_object(o)->as_instance(); 159 o = Universe::arithmetic_exception_instance(); 160 assert(o != nullptr, "should have been initialized"); 161 _ArithmeticException_instance = get_object(o)->as_instance(); 162 163 _ArrayIndexOutOfBoundsException_instance = nullptr; 164 _ArrayStoreException_instance = nullptr; 165 _ClassCastException_instance = nullptr; 166 _the_null_string = nullptr; 167 _the_min_jint_string = nullptr; 168 169 _jvmti_redefinition_count = 0; 170 _jvmti_can_hotswap_or_post_breakpoint = false; 171 _jvmti_can_access_local_variables = false; 172 _jvmti_can_post_on_exceptions = false; 173 _jvmti_can_pop_frame = false; 174 175 _dyno_klasses = nullptr; 176 _dyno_locs = nullptr; 177 _dyno_name[0] = '\0'; 178 } 179 180 // Record components of a location descriptor string. Components are appended by the constructor and 181 // removed by the destructor, like a stack, so scope matters. These location descriptors are used to 182 // locate dynamic classes, and terminate at a Method* or oop field associated with dynamic/hidden class. 183 // 184 // Example use: 185 // 186 // { 187 // RecordLocation fp(this, "field1"); 188 // // location: "field1" 189 // { RecordLocation fp(this, " field2"); // location: "field1 field2" } 190 // // location: "field1" 191 // { RecordLocation fp(this, " field3"); // location: "field1 field3" } 192 // // location: "field1" 193 // } 194 // // location: "" 195 // 196 // Examples of actual locations 197 // @bci compiler/ciReplay/CiReplayBase$TestMain test (I)V 1 <appendix> argL0 ; 198 // // resolve invokedynamic at bci 1 of TestMain.test, then read field "argL0" from appendix 199 // @bci compiler/ciReplay/CiReplayBase$TestMain main ([Ljava/lang/String;)V 0 <appendix> form vmentry <vmtarget> ; 200 // // resolve invokedynamic at bci 0 of TestMain.main, then read field "form.vmentry.method.vmtarget" from appendix 201 // @cpi compiler/ciReplay/CiReplayBase$TestMain 56 form vmentry <vmtarget> ; 202 // // resolve MethodHandle at cpi 56 of TestMain, then read field "vmentry.method.vmtarget" from resolved MethodHandle 203 class RecordLocation { 204 private: 205 char* end; 206 207 ATTRIBUTE_PRINTF(3, 4) 208 void push(ciEnv* ci, const char* fmt, ...) { 209 va_list args; 210 va_start(args, fmt); 211 push_va(ci, fmt, args); 212 va_end(args); 213 } 214 215 public: 216 ATTRIBUTE_PRINTF(3, 0) 217 void push_va(ciEnv* ci, const char* fmt, va_list args) { 218 char *e = ci->_dyno_name + strlen(ci->_dyno_name); 219 char *m = ci->_dyno_name + ARRAY_SIZE(ci->_dyno_name) - 1; 220 os::vsnprintf(e, m - e, fmt, args); 221 assert(strlen(ci->_dyno_name) < (ARRAY_SIZE(ci->_dyno_name) - 1), "overflow"); 222 } 223 224 // append a new component 225 ATTRIBUTE_PRINTF(3, 4) 226 RecordLocation(ciEnv* ci, const char* fmt, ...) { 227 end = ci->_dyno_name + strlen(ci->_dyno_name); 228 va_list args; 229 va_start(args, fmt); 230 push(ci, " "); 231 push_va(ci, fmt, args); 232 va_end(args); 233 } 234 235 // reset to previous state 236 ~RecordLocation() { 237 *end = '\0'; 238 } 239 }; 240 241 ciEnv::ciEnv(Arena* arena) : _ciEnv_arena(mtCompiler) { 242 ASSERT_IN_VM; 243 244 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc. 245 CompilerThread* current_thread = CompilerThread::current(); 246 assert(current_thread->env() == nullptr, "must be"); 247 current_thread->set_env(this); 248 assert(ciEnv::current() == this, "sanity"); 249 250 _oop_recorder = nullptr; 251 _debug_info = nullptr; 252 _dependencies = nullptr; 253 _failure_reason = nullptr; 254 _inc_decompile_count_on_failure = true; 255 _compilable = MethodCompilable_never; 256 _break_at_compile = false; 257 _compiler_data = nullptr; 258 #ifndef PRODUCT 259 assert(firstEnv, "must be first"); 260 firstEnv = false; 261 #endif /* !PRODUCT */ 262 263 _num_inlined_bytecodes = 0; 264 _task = nullptr; 265 _log = nullptr; 266 267 // Temporary buffer for creating symbols and such. 268 _name_buffer = nullptr; 269 _name_buffer_len = 0; 270 271 _arena = arena; 272 _factory = new (_arena) ciObjectFactory(_arena, 128); 273 274 // Preload commonly referenced system ciObjects. 275 276 // During VM initialization, these instances have not yet been created. 277 // Assertions ensure that these instances are not accessed before 278 // their initialization. 279 280 assert(Universe::is_fully_initialized(), "must be"); 281 282 _NullPointerException_instance = nullptr; 283 _ArithmeticException_instance = nullptr; 284 _ArrayIndexOutOfBoundsException_instance = nullptr; 285 _ArrayStoreException_instance = nullptr; 286 _ClassCastException_instance = nullptr; 287 _the_null_string = nullptr; 288 _the_min_jint_string = nullptr; 289 290 _jvmti_redefinition_count = 0; 291 _jvmti_can_hotswap_or_post_breakpoint = false; 292 _jvmti_can_access_local_variables = false; 293 _jvmti_can_post_on_exceptions = false; 294 _jvmti_can_pop_frame = false; 295 296 _dyno_klasses = nullptr; 297 _dyno_locs = nullptr; 298 } 299 300 ciEnv::~ciEnv() { 301 GUARDED_VM_ENTRY( 302 CompilerThread* current_thread = CompilerThread::current(); 303 _factory->remove_symbols(); 304 // Need safepoint to clear the env on the thread. RedefineClasses might 305 // be reading it. 306 current_thread->set_env(nullptr); 307 ) 308 } 309 310 // ------------------------------------------------------------------ 311 // Cache Jvmti state 312 bool ciEnv::cache_jvmti_state() { 313 VM_ENTRY_MARK; 314 // Get Jvmti capabilities under lock to get consistent values. 315 MutexLocker mu(JvmtiThreadState_lock); 316 _jvmti_redefinition_count = JvmtiExport::redefinition_count(); 317 _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint(); 318 _jvmti_can_access_local_variables = JvmtiExport::can_access_local_variables(); 319 _jvmti_can_post_on_exceptions = JvmtiExport::can_post_on_exceptions(); 320 _jvmti_can_pop_frame = JvmtiExport::can_pop_frame(); 321 _jvmti_can_get_owned_monitor_info = JvmtiExport::can_get_owned_monitor_info(); 322 _jvmti_can_walk_any_space = JvmtiExport::can_walk_any_space(); 323 return _task != nullptr && _task->method()->is_old(); 324 } 325 326 bool ciEnv::jvmti_state_changed() const { 327 // Some classes were redefined 328 if (_jvmti_redefinition_count != JvmtiExport::redefinition_count()) { 329 return true; 330 } 331 332 if (!_jvmti_can_access_local_variables && 333 JvmtiExport::can_access_local_variables()) { 334 return true; 335 } 336 if (!_jvmti_can_hotswap_or_post_breakpoint && 337 JvmtiExport::can_hotswap_or_post_breakpoint()) { 338 return true; 339 } 340 if (!_jvmti_can_post_on_exceptions && 341 JvmtiExport::can_post_on_exceptions()) { 342 return true; 343 } 344 if (!_jvmti_can_pop_frame && 345 JvmtiExport::can_pop_frame()) { 346 return true; 347 } 348 if (!_jvmti_can_get_owned_monitor_info && 349 JvmtiExport::can_get_owned_monitor_info()) { 350 return true; 351 } 352 if (!_jvmti_can_walk_any_space && 353 JvmtiExport::can_walk_any_space()) { 354 return true; 355 } 356 357 return false; 358 } 359 360 // ------------------------------------------------------------------ 361 // Cache DTrace flags 362 void ciEnv::cache_dtrace_flags() { 363 // Need lock? 364 _dtrace_method_probes = DTraceMethodProbes; 365 _dtrace_alloc_probes = DTraceAllocProbes; 366 } 367 368 // ------------------------------------------------------------------ 369 // helper for lazy exception creation 370 ciInstance* ciEnv::get_or_create_exception(jobject& handle, Symbol* name) { 371 VM_ENTRY_MARK; 372 if (handle == nullptr) { 373 // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance. 374 InstanceKlass* ik = SystemDictionary::find_instance_klass(THREAD, name, Handle(), Handle()); 375 jobject objh = nullptr; 376 if (ik != nullptr) { 377 oop obj = ik->allocate_instance(THREAD); 378 if (!HAS_PENDING_EXCEPTION) 379 objh = JNIHandles::make_global(Handle(THREAD, obj)); 380 } 381 if (HAS_PENDING_EXCEPTION) { 382 CLEAR_PENDING_EXCEPTION; 383 } else { 384 handle = objh; 385 } 386 } 387 oop obj = JNIHandles::resolve(handle); 388 return obj == nullptr? nullptr: get_object(obj)->as_instance(); 389 } 390 391 ciInstanceKlass* ciEnv::get_box_klass_for_primitive_type(BasicType type) { 392 switch (type) { 393 case T_BOOLEAN: return Boolean_klass(); 394 case T_BYTE : return Byte_klass(); 395 case T_CHAR : return Character_klass(); 396 case T_SHORT : return Short_klass(); 397 case T_INT : return Integer_klass(); 398 case T_LONG : return Long_klass(); 399 case T_FLOAT : return Float_klass(); 400 case T_DOUBLE : return Double_klass(); 401 402 default: 403 assert(false, "not a primitive: %s", type2name(type)); 404 return nullptr; 405 } 406 } 407 408 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() { 409 if (_ArrayIndexOutOfBoundsException_instance == nullptr) { 410 _ArrayIndexOutOfBoundsException_instance 411 = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle, 412 vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); 413 } 414 return _ArrayIndexOutOfBoundsException_instance; 415 } 416 ciInstance* ciEnv::ArrayStoreException_instance() { 417 if (_ArrayStoreException_instance == nullptr) { 418 _ArrayStoreException_instance 419 = get_or_create_exception(_ArrayStoreException_handle, 420 vmSymbols::java_lang_ArrayStoreException()); 421 } 422 return _ArrayStoreException_instance; 423 } 424 ciInstance* ciEnv::ClassCastException_instance() { 425 if (_ClassCastException_instance == nullptr) { 426 _ClassCastException_instance 427 = get_or_create_exception(_ClassCastException_handle, 428 vmSymbols::java_lang_ClassCastException()); 429 } 430 return _ClassCastException_instance; 431 } 432 433 ciInstance* ciEnv::the_null_string() { 434 if (_the_null_string == nullptr) { 435 VM_ENTRY_MARK; 436 _the_null_string = get_object(Universe::the_null_string())->as_instance(); 437 } 438 return _the_null_string; 439 } 440 441 ciInstance* ciEnv::the_min_jint_string() { 442 if (_the_min_jint_string == nullptr) { 443 VM_ENTRY_MARK; 444 _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance(); 445 } 446 return _the_min_jint_string; 447 } 448 449 // ------------------------------------------------------------------ 450 // ciEnv::get_method_from_handle 451 ciMethod* ciEnv::get_method_from_handle(Method* method) { 452 VM_ENTRY_MARK; 453 return get_metadata(method)->as_method(); 454 } 455 456 // ------------------------------------------------------------------ 457 // ciEnv::check_klass_accessiblity 458 // 459 // Note: the logic of this method should mirror the logic of 460 // ConstantPool::verify_constant_pool_resolve. 461 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass, 462 Klass* resolved_klass) { 463 if (accessing_klass == nullptr || !accessing_klass->is_loaded()) { 464 return true; 465 } 466 if (accessing_klass->is_obj_array_klass()) { 467 accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass(); 468 } 469 if (!accessing_klass->is_instance_klass()) { 470 return true; 471 } 472 473 if (resolved_klass->is_objArray_klass()) { 474 // Find the element klass, if this is an array. 475 resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass(); 476 } 477 if (resolved_klass->is_instance_klass()) { 478 return (Reflection::verify_class_access(accessing_klass->get_Klass(), 479 InstanceKlass::cast(resolved_klass), 480 true) == Reflection::ACCESS_OK); 481 } 482 return true; 483 } 484 485 // ------------------------------------------------------------------ 486 // ciEnv::get_klass_by_name_impl 487 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass, 488 const constantPoolHandle& cpool, 489 ciSymbol* name, 490 bool require_local) { 491 ASSERT_IN_VM; 492 Thread* current = Thread::current(); 493 494 // Now we need to check the SystemDictionary 495 Symbol* sym = name->get_symbol(); 496 if (Signature::has_envelope(sym)) { 497 // This is a name from a signature. Strip off the trimmings. 498 // Call recursive to keep scope of strippedsym. 499 TempNewSymbol strippedsym = Signature::strip_envelope(sym); 500 ciSymbol* strippedname = get_symbol(strippedsym); 501 return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local); 502 } 503 504 // Check for prior unloaded klass. The SystemDictionary's answers 505 // can vary over time but the compiler needs consistency. 506 ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name); 507 if (unloaded_klass != nullptr) { 508 if (require_local) return nullptr; 509 return unloaded_klass; 510 } 511 512 Handle loader; 513 Handle domain; 514 if (accessing_klass != nullptr) { 515 loader = Handle(current, accessing_klass->loader()); 516 domain = Handle(current, accessing_klass->protection_domain()); 517 } 518 519 Klass* found_klass = require_local ? 520 SystemDictionary::find_instance_or_array_klass(current, sym, loader, domain) : 521 SystemDictionary::find_constrained_instance_or_array_klass(current, sym, loader); 522 523 // If we fail to find an array klass, look again for its element type. 524 // The element type may be available either locally or via constraints. 525 // In either case, if we can find the element type in the system dictionary, 526 // we must build an array type around it. The CI requires array klasses 527 // to be loaded if their element klasses are loaded, except when memory 528 // is exhausted. 529 if (Signature::is_array(sym) && 530 (sym->char_at(1) == JVM_SIGNATURE_ARRAY || 531 sym->char_at(1) == JVM_SIGNATURE_CLASS || 532 sym->char_at(1) == JVM_SIGNATURE_PRIMITIVE_OBJECT )) { 533 // We have an unloaded array. 534 // Build it on the fly if the element class exists. 535 SignatureStream ss(sym, false); 536 ss.skip_array_prefix(1); 537 // Get element ciKlass recursively. 538 ciKlass* elem_klass = 539 get_klass_by_name_impl(accessing_klass, 540 cpool, 541 get_symbol(ss.as_symbol()), 542 require_local); 543 if (elem_klass != nullptr && elem_klass->is_loaded()) { 544 // Now make an array for it 545 bool null_free_array = sym->is_Q_array_signature() && sym->char_at(1) == JVM_SIGNATURE_PRIMITIVE_OBJECT; 546 return ciArrayKlass::make(elem_klass, null_free_array); 547 } 548 } 549 550 if (found_klass == nullptr && !cpool.is_null() && cpool->has_preresolution()) { 551 // Look inside the constant pool for pre-resolved class entries. 552 for (int i = cpool->length() - 1; i >= 1; i--) { 553 if (cpool->tag_at(i).is_klass()) { 554 Klass* kls = cpool->resolved_klass_at(i); 555 if (kls->name() == sym) { 556 found_klass = kls; 557 break; 558 } 559 } 560 } 561 } 562 563 if (found_klass != nullptr) { 564 // Found it. Build a CI handle. 565 return get_klass(found_klass); 566 } 567 568 if (require_local) return nullptr; 569 570 // Not yet loaded into the VM, or not governed by loader constraints. 571 // Make a CI representative for it. 572 int i = 0; 573 while (sym->char_at(i) == JVM_SIGNATURE_ARRAY) { 574 i++; 575 } 576 if (i > 0 && sym->char_at(i) == JVM_SIGNATURE_PRIMITIVE_OBJECT) { 577 // An unloaded array class of inline types is an ObjArrayKlass, an 578 // unloaded inline type class is an InstanceKlass. For consistency, 579 // make the signature of the unloaded array of inline type use L 580 // rather than Q. 581 char* new_name = name_buffer(sym->utf8_length()+1); 582 strncpy(new_name, (char*)sym->base(), sym->utf8_length()); 583 new_name[i] = JVM_SIGNATURE_CLASS; 584 new_name[sym->utf8_length()] = '\0'; 585 return get_unloaded_klass(accessing_klass, ciSymbol::make(new_name)); 586 } 587 return get_unloaded_klass(accessing_klass, name); 588 } 589 590 // ------------------------------------------------------------------ 591 // ciEnv::get_klass_by_name 592 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass, 593 ciSymbol* klass_name, 594 bool require_local) { 595 GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass, 596 constantPoolHandle(), 597 klass_name, 598 require_local);) 599 } 600 601 // ------------------------------------------------------------------ 602 // ciEnv::get_klass_by_index_impl 603 // 604 // Implementation of get_klass_by_index. 605 ciKlass* ciEnv::get_klass_by_index_impl(const constantPoolHandle& cpool, 606 int index, 607 bool& is_accessible, 608 ciInstanceKlass* accessor) { 609 Klass* klass = nullptr; 610 Symbol* klass_name = nullptr; 611 612 if (cpool->tag_at(index).is_symbol()) { 613 klass_name = cpool->symbol_at(index); 614 } else { 615 // Check if it's resolved if it's not a symbol constant pool entry. 616 klass = ConstantPool::klass_at_if_loaded(cpool, index); 617 // Try to look it up by name. 618 if (klass == nullptr) { 619 klass_name = cpool->klass_name_at(index); 620 } 621 } 622 623 if (klass == nullptr) { 624 // Not found in constant pool. Use the name to do the lookup. 625 ciKlass* k = get_klass_by_name_impl(accessor, 626 cpool, 627 get_symbol(klass_name), 628 false); 629 // Calculate accessibility the hard way. 630 if (!k->is_loaded()) { 631 is_accessible = false; 632 } else if (k->loader() != accessor->loader() && 633 get_klass_by_name_impl(accessor, cpool, k->name(), true) == nullptr) { 634 // Loaded only remotely. Not linked yet. 635 is_accessible = false; 636 } else { 637 // Linked locally, and we must also check public/private, etc. 638 is_accessible = check_klass_accessibility(accessor, k->get_Klass()); 639 } 640 return k; 641 } 642 643 // Check for prior unloaded klass. The SystemDictionary's answers 644 // can vary over time but the compiler needs consistency. 645 ciSymbol* name = get_symbol(klass->name()); 646 ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name); 647 if (unloaded_klass != nullptr) { 648 is_accessible = false; 649 return unloaded_klass; 650 } 651 652 // It is known to be accessible, since it was found in the constant pool. 653 ciKlass* ciKlass = get_klass(klass); 654 is_accessible = true; 655 if (ReplayCompiles && ciKlass == _unloaded_ciinstance_klass) { 656 // Klass was unresolved at replay dump time and therefore not accessible. 657 is_accessible = false; 658 } 659 return ciKlass; 660 } 661 662 // ------------------------------------------------------------------ 663 // ciEnv::get_klass_by_index 664 // 665 // Get a klass from the constant pool. 666 ciKlass* ciEnv::get_klass_by_index(const constantPoolHandle& cpool, 667 int index, 668 bool& is_accessible, 669 ciInstanceKlass* accessor) { 670 GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);) 671 } 672 673 // ------------------------------------------------------------------ 674 // ciEnv::is_inline_klass 675 // 676 // Check if the klass is an inline klass. 677 bool ciEnv::has_Q_signature(const constantPoolHandle& cpool, int index) { 678 GUARDED_VM_ENTRY(return cpool->klass_name_at(index)->is_Q_signature();) 679 } 680 681 // ------------------------------------------------------------------ 682 // ciEnv::unbox_primitive_value 683 // 684 // Unbox a primitive and return it as a ciConstant. 685 ciConstant ciEnv::unbox_primitive_value(ciObject* cibox, BasicType expected_bt) { 686 jvalue value; 687 BasicType bt = java_lang_boxing_object::get_value(cibox->get_oop(), &value); 688 if (bt != expected_bt && expected_bt != T_ILLEGAL) { 689 assert(false, "type mismatch: %s vs %s", type2name(expected_bt), cibox->klass()->name()->as_klass_external_name()); 690 return ciConstant(); 691 } 692 switch (bt) { 693 case T_BOOLEAN: return ciConstant(bt, value.z); 694 case T_BYTE: return ciConstant(bt, value.b); 695 case T_SHORT: return ciConstant(bt, value.s); 696 case T_CHAR: return ciConstant(bt, value.c); 697 case T_INT: return ciConstant(bt, value.i); 698 case T_LONG: return ciConstant(value.j); 699 case T_FLOAT: return ciConstant(value.f); 700 case T_DOUBLE: return ciConstant(value.d); 701 702 default: 703 assert(false, "not a primitive type: %s", type2name(bt)); 704 return ciConstant(); 705 } 706 } 707 708 // ------------------------------------------------------------------ 709 // ciEnv::get_resolved_constant 710 // 711 ciConstant ciEnv::get_resolved_constant(const constantPoolHandle& cpool, int obj_index) { 712 assert(obj_index >= 0, ""); 713 oop obj = cpool->resolved_reference_at(obj_index); 714 if (obj == nullptr) { 715 // Unresolved constant. It is resolved when the corresponding slot contains a non-null reference. 716 // Null constant is represented as a sentinel (non-null) value. 717 return ciConstant(); 718 } else if (obj == Universe::the_null_sentinel()) { 719 return ciConstant(T_OBJECT, get_object(nullptr)); 720 } else { 721 ciObject* ciobj = get_object(obj); 722 if (ciobj->is_array()) { 723 return ciConstant(T_ARRAY, ciobj); 724 } else { 725 int cp_index = cpool->object_to_cp_index(obj_index); 726 BasicType bt = cpool->basic_type_for_constant_at(cp_index); 727 if (is_java_primitive(bt)) { 728 assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity"); 729 return unbox_primitive_value(ciobj, bt); 730 } else { 731 assert(ciobj->is_instance(), "should be an instance"); 732 return ciConstant(T_OBJECT, ciobj); 733 } 734 } 735 } 736 } 737 738 // ------------------------------------------------------------------ 739 // ciEnv::get_constant_by_index_impl 740 // 741 // Implementation of get_constant_by_index(). 742 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool, 743 int index, int obj_index, 744 ciInstanceKlass* accessor) { 745 if (obj_index >= 0) { 746 ciConstant con = get_resolved_constant(cpool, obj_index); 747 if (con.is_valid()) { 748 return con; 749 } 750 } 751 constantTag tag = cpool->tag_at(index); 752 if (tag.is_int()) { 753 return ciConstant(T_INT, (jint)cpool->int_at(index)); 754 } else if (tag.is_long()) { 755 return ciConstant((jlong)cpool->long_at(index)); 756 } else if (tag.is_float()) { 757 return ciConstant((jfloat)cpool->float_at(index)); 758 } else if (tag.is_double()) { 759 return ciConstant((jdouble)cpool->double_at(index)); 760 } else if (tag.is_string()) { 761 EXCEPTION_CONTEXT; 762 assert(obj_index >= 0, "should have an object index"); 763 oop string = cpool->string_at(index, obj_index, THREAD); 764 if (HAS_PENDING_EXCEPTION) { 765 CLEAR_PENDING_EXCEPTION; 766 record_out_of_memory_failure(); 767 return ciConstant(); 768 } 769 ciInstance* constant = get_object(string)->as_instance(); 770 return ciConstant(T_OBJECT, constant); 771 } else if (tag.is_unresolved_klass_in_error()) { 772 return ciConstant(T_OBJECT, get_unloaded_klass_mirror(nullptr)); 773 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 774 bool will_link; 775 ciKlass* klass = get_klass_by_index_impl(cpool, index, will_link, accessor); 776 ciInstance* mirror = (will_link ? klass->java_mirror() : get_unloaded_klass_mirror(klass)); 777 if (klass->is_loaded() && tag.is_Qdescriptor_klass()) { 778 return ciConstant(T_OBJECT, klass->as_inline_klass()->val_mirror()); 779 } else { 780 return ciConstant(T_OBJECT, mirror); 781 } 782 } else if (tag.is_method_type() || tag.is_method_type_in_error()) { 783 // must execute Java code to link this CP entry into cache[i].f1 784 assert(obj_index >= 0, "should have an object index"); 785 ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index)); 786 ciObject* ciobj = get_unloaded_method_type_constant(signature); 787 return ciConstant(T_OBJECT, ciobj); 788 } else if (tag.is_method_handle() || tag.is_method_handle_in_error()) { 789 // must execute Java code to link this CP entry into cache[i].f1 790 assert(obj_index >= 0, "should have an object index"); 791 bool ignore_will_link; 792 int ref_kind = cpool->method_handle_ref_kind_at(index); 793 int callee_index = cpool->method_handle_klass_index_at(index); 794 ciKlass* callee = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor); 795 ciSymbol* name = get_symbol(cpool->method_handle_name_ref_at(index)); 796 ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index)); 797 ciObject* ciobj = get_unloaded_method_handle_constant(callee, name, signature, ref_kind); 798 return ciConstant(T_OBJECT, ciobj); 799 } else if (tag.is_dynamic_constant() || tag.is_dynamic_constant_in_error()) { 800 assert(obj_index >= 0, "should have an object index"); 801 return ciConstant(T_OBJECT, unloaded_ciinstance()); // unresolved dynamic constant 802 } else { 803 assert(false, "unknown tag: %d (%s)", tag.value(), tag.internal_name()); 804 return ciConstant(); 805 } 806 } 807 808 // ------------------------------------------------------------------ 809 // ciEnv::get_constant_by_index 810 // 811 // Pull a constant out of the constant pool. How appropriate. 812 // 813 // Implementation note: this query is currently in no way cached. 814 ciConstant ciEnv::get_constant_by_index(const constantPoolHandle& cpool, 815 int pool_index, int cache_index, 816 ciInstanceKlass* accessor) { 817 GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);) 818 } 819 820 // ------------------------------------------------------------------ 821 // ciEnv::get_field_by_index_impl 822 // 823 // Implementation of get_field_by_index. 824 // 825 // Implementation note: the results of field lookups are cached 826 // in the accessor klass. 827 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor, 828 int index) { 829 ciConstantPoolCache* cache = accessor->field_cache(); 830 if (cache == nullptr) { 831 ciField* field = new (arena()) ciField(accessor, index); 832 return field; 833 } else { 834 ciField* field = (ciField*)cache->get(index); 835 if (field == nullptr) { 836 field = new (arena()) ciField(accessor, index); 837 cache->insert(index, field); 838 } 839 return field; 840 } 841 } 842 843 // ------------------------------------------------------------------ 844 // ciEnv::get_field_by_index 845 // 846 // Get a field by index from a klass's constant pool. 847 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor, 848 int index) { 849 GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);) 850 } 851 852 // ------------------------------------------------------------------ 853 // ciEnv::lookup_method 854 // 855 // Perform an appropriate method lookup based on accessor, holder, 856 // name, signature, and bytecode. 857 Method* ciEnv::lookup_method(ciInstanceKlass* accessor, 858 ciKlass* holder, 859 Symbol* name, 860 Symbol* sig, 861 Bytecodes::Code bc, 862 constantTag tag) { 863 InstanceKlass* accessor_klass = accessor->get_instanceKlass(); 864 Klass* holder_klass = holder->get_Klass(); 865 866 // Accessibility checks are performed in ciEnv::get_method_by_index_impl. 867 assert(check_klass_accessibility(accessor, holder_klass), "holder not accessible"); 868 869 LinkInfo link_info(holder_klass, name, sig, accessor_klass, 870 LinkInfo::AccessCheck::required, 871 LinkInfo::LoaderConstraintCheck::required, 872 tag); 873 switch (bc) { 874 case Bytecodes::_invokestatic: 875 return LinkResolver::resolve_static_call_or_null(link_info); 876 case Bytecodes::_invokespecial: 877 return LinkResolver::resolve_special_call_or_null(link_info); 878 case Bytecodes::_invokeinterface: 879 return LinkResolver::linktime_resolve_interface_method_or_null(link_info); 880 case Bytecodes::_invokevirtual: 881 return LinkResolver::linktime_resolve_virtual_method_or_null(link_info); 882 default: 883 fatal("Unhandled bytecode: %s", Bytecodes::name(bc)); 884 return nullptr; // silence compiler warnings 885 } 886 } 887 888 889 // ------------------------------------------------------------------ 890 // ciEnv::get_method_by_index_impl 891 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool, 892 int index, Bytecodes::Code bc, 893 ciInstanceKlass* accessor) { 894 assert(cpool.not_null(), "need constant pool"); 895 assert(accessor != nullptr, "need origin of access"); 896 if (bc == Bytecodes::_invokedynamic) { 897 // FIXME: code generation could allow for null (unlinked) call site 898 // The call site could be made patchable as follows: 899 // Load the appendix argument from the constant pool. 900 // Test the appendix argument and jump to a known deopt routine if it is null. 901 // Jump through a patchable call site, which is initially a deopt routine. 902 // Patch the call site to the nmethod entry point of the static compiled lambda form. 903 // As with other two-component call sites, both values must be independently verified. 904 int indy_index = cpool->decode_invokedynamic_index(index); 905 assert (indy_index >= 0, "should be"); 906 assert(indy_index < cpool->cache()->resolved_indy_entries_length(), "impossible"); 907 Method* adapter = cpool->resolved_indy_entry_at(indy_index)->method(); 908 // Resolved if the adapter is non null. 909 if (adapter != nullptr) { 910 return get_method(adapter); 911 } 912 913 // Fake a method that is equivalent to a declared method. 914 ciInstanceKlass* holder = get_instance_klass(vmClasses::MethodHandle_klass()); 915 ciSymbol* name = ciSymbols::invokeBasic_name(); 916 ciSymbol* signature = get_symbol(cpool->signature_ref_at(index)); 917 return get_unloaded_method(holder, name, signature, accessor); 918 } else { 919 const int holder_index = cpool->klass_ref_index_at(index); 920 bool holder_is_accessible; 921 ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor); 922 923 // Get the method's name and signature. 924 Symbol* name_sym = cpool->name_ref_at(index); 925 Symbol* sig_sym = cpool->signature_ref_at(index); 926 927 if (cpool->has_preresolution() 928 || ((holder == ciEnv::MethodHandle_klass() || holder == ciEnv::VarHandle_klass()) && 929 MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) { 930 // Short-circuit lookups for JSR 292-related call sites. 931 // That is, do not rely only on name-based lookups, because they may fail 932 // if the names are not resolvable in the boot class loader (7056328). 933 switch (bc) { 934 case Bytecodes::_invokevirtual: 935 case Bytecodes::_invokeinterface: 936 case Bytecodes::_invokespecial: 937 case Bytecodes::_invokestatic: 938 { 939 Method* m = ConstantPool::method_at_if_loaded(cpool, index); 940 if (m != nullptr) { 941 return get_method(m); 942 } 943 } 944 break; 945 default: 946 break; 947 } 948 } 949 950 if (holder_is_accessible) { // Our declared holder is loaded. 951 constantTag tag = cpool->tag_ref_at(index); 952 assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?"); 953 Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag); 954 if (m != nullptr && 955 (bc == Bytecodes::_invokestatic 956 ? m->method_holder()->is_not_initialized() 957 : !m->method_holder()->is_loaded())) { 958 m = nullptr; 959 } 960 if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) { 961 m = nullptr; 962 } 963 if (m != nullptr) { 964 // We found the method. 965 return get_method(m); 966 } 967 } 968 969 // Either the declared holder was not loaded, or the method could 970 // not be found. Create a dummy ciMethod to represent the failed 971 // lookup. 972 ciSymbol* name = get_symbol(name_sym); 973 ciSymbol* signature = get_symbol(sig_sym); 974 return get_unloaded_method(holder, name, signature, accessor); 975 } 976 } 977 978 979 // ------------------------------------------------------------------ 980 // ciEnv::get_instance_klass_for_declared_method_holder 981 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) { 982 // For the case of <array>.clone(), the method holder can be a ciArrayKlass 983 // instead of a ciInstanceKlass. For that case simply pretend that the 984 // declared holder is Object.clone since that's where the call will bottom out. 985 // A more correct fix would trickle out through many interfaces in CI, 986 // requiring ciInstanceKlass* to become ciKlass* and many more places would 987 // require checks to make sure the expected type was found. Given that this 988 // only occurs for clone() the more extensive fix seems like overkill so 989 // instead we simply smear the array type into Object. 990 guarantee(method_holder != nullptr, "no method holder"); 991 if (method_holder->is_instance_klass()) { 992 return method_holder->as_instance_klass(); 993 } else if (method_holder->is_array_klass()) { 994 return current()->Object_klass(); 995 } else { 996 ShouldNotReachHere(); 997 } 998 return nullptr; 999 } 1000 1001 1002 // ------------------------------------------------------------------ 1003 // ciEnv::get_method_by_index 1004 ciMethod* ciEnv::get_method_by_index(const constantPoolHandle& cpool, 1005 int index, Bytecodes::Code bc, 1006 ciInstanceKlass* accessor) { 1007 GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);) 1008 } 1009 1010 1011 // ------------------------------------------------------------------ 1012 // ciEnv::name_buffer 1013 char *ciEnv::name_buffer(int req_len) { 1014 if (_name_buffer_len < req_len) { 1015 if (_name_buffer == nullptr) { 1016 _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len); 1017 _name_buffer_len = req_len; 1018 } else { 1019 _name_buffer = 1020 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len); 1021 _name_buffer_len = req_len; 1022 } 1023 } 1024 return _name_buffer; 1025 } 1026 1027 // ------------------------------------------------------------------ 1028 // ciEnv::is_in_vm 1029 bool ciEnv::is_in_vm() { 1030 return JavaThread::current()->thread_state() == _thread_in_vm; 1031 } 1032 1033 // ------------------------------------------------------------------ 1034 // ciEnv::validate_compile_task_dependencies 1035 // 1036 // Check for changes during compilation (e.g. class loads, evolution, 1037 // breakpoints, call site invalidation). 1038 void ciEnv::validate_compile_task_dependencies(ciMethod* target) { 1039 if (failing()) return; // no need for further checks 1040 1041 Dependencies::DepType result = dependencies()->validate_dependencies(_task); 1042 if (result != Dependencies::end_marker) { 1043 if (result == Dependencies::call_site_target_value) { 1044 _inc_decompile_count_on_failure = false; 1045 record_failure("call site target change"); 1046 } else if (Dependencies::is_klass_type(result)) { 1047 record_failure("concurrent class loading"); 1048 } else { 1049 record_failure("invalid non-klass dependency"); 1050 } 1051 } 1052 } 1053 1054 // ------------------------------------------------------------------ 1055 // ciEnv::register_method 1056 void ciEnv::register_method(ciMethod* target, 1057 int entry_bci, 1058 CodeOffsets* offsets, 1059 int orig_pc_offset, 1060 CodeBuffer* code_buffer, 1061 int frame_words, 1062 OopMapSet* oop_map_set, 1063 ExceptionHandlerTable* handler_table, 1064 ImplicitExceptionTable* inc_table, 1065 AbstractCompiler* compiler, 1066 bool has_unsafe_access, 1067 bool has_wide_vectors, 1068 bool has_monitors, 1069 int immediate_oops_patched, 1070 RTMState rtm_state) { 1071 VM_ENTRY_MARK; 1072 nmethod* nm = nullptr; 1073 { 1074 methodHandle method(THREAD, target->get_Method()); 1075 1076 // We require method counters to store some method state (max compilation levels) required by the compilation policy. 1077 if (method->get_method_counters(THREAD) == nullptr) { 1078 record_failure("can't create method counters"); 1079 // All buffers in the CodeBuffer are allocated in the CodeCache. 1080 // If the code buffer is created on each compile attempt 1081 // as in C2, then it must be freed. 1082 code_buffer->free_blob(); 1083 return; 1084 } 1085 1086 // Check if memory should be freed before allocation 1087 CodeCache::gc_on_allocation(); 1088 1089 // To prevent compile queue updates. 1090 MutexLocker locker(THREAD, MethodCompileQueue_lock); 1091 1092 // Prevent InstanceKlass::add_to_hierarchy from running 1093 // and invalidating our dependencies until we install this method. 1094 // No safepoints are allowed. Otherwise, class redefinition can occur in between. 1095 MutexLocker ml(Compile_lock); 1096 NoSafepointVerifier nsv; 1097 1098 // Change in Jvmti state may invalidate compilation. 1099 if (!failing() && jvmti_state_changed()) { 1100 record_failure("Jvmti state change invalidated dependencies"); 1101 } 1102 1103 // Change in DTrace flags may invalidate compilation. 1104 if (!failing() && 1105 ( (!dtrace_method_probes() && DTraceMethodProbes) || 1106 (!dtrace_alloc_probes() && DTraceAllocProbes) )) { 1107 record_failure("DTrace flags change invalidated dependencies"); 1108 } 1109 1110 if (!failing() && target->needs_clinit_barrier() && 1111 target->holder()->is_in_error_state()) { 1112 record_failure("method holder is in error state"); 1113 } 1114 1115 if (!failing()) { 1116 if (log() != nullptr) { 1117 // Log the dependencies which this compilation declares. 1118 dependencies()->log_all_dependencies(); 1119 } 1120 1121 // Encode the dependencies now, so we can check them right away. 1122 dependencies()->encode_content_bytes(); 1123 1124 // Check for {class loads, evolution, breakpoints, ...} during compilation 1125 validate_compile_task_dependencies(target); 1126 } 1127 #if INCLUDE_RTM_OPT 1128 if (!failing() && (rtm_state != NoRTM) && 1129 (method()->method_data() != nullptr) && 1130 (method()->method_data()->rtm_state() != rtm_state)) { 1131 // Preemptive decompile if rtm state was changed. 1132 record_failure("RTM state change invalidated rtm code"); 1133 } 1134 #endif 1135 1136 if (failing()) { 1137 // While not a true deoptimization, it is a preemptive decompile. 1138 MethodData* mdo = method()->method_data(); 1139 if (mdo != nullptr && _inc_decompile_count_on_failure) { 1140 mdo->inc_decompile_count(); 1141 } 1142 1143 // All buffers in the CodeBuffer are allocated in the CodeCache. 1144 // If the code buffer is created on each compile attempt 1145 // as in C2, then it must be freed. 1146 code_buffer->free_blob(); 1147 return; 1148 } 1149 1150 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry"); 1151 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry"); 1152 1153 nm = nmethod::new_nmethod(method, 1154 compile_id(), 1155 entry_bci, 1156 offsets, 1157 orig_pc_offset, 1158 debug_info(), dependencies(), code_buffer, 1159 frame_words, oop_map_set, 1160 handler_table, inc_table, 1161 compiler, CompLevel(task()->comp_level())); 1162 1163 // Free codeBlobs 1164 code_buffer->free_blob(); 1165 1166 if (nm != nullptr) { 1167 nm->set_has_unsafe_access(has_unsafe_access); 1168 nm->set_has_wide_vectors(has_wide_vectors); 1169 nm->set_has_monitors(has_monitors); 1170 assert(!method->is_synchronized() || nm->has_monitors(), ""); 1171 #if INCLUDE_RTM_OPT 1172 nm->set_rtm_state(rtm_state); 1173 #endif 1174 1175 if (entry_bci == InvocationEntryBci) { 1176 if (TieredCompilation) { 1177 // If there is an old version we're done with it 1178 CompiledMethod* old = method->code(); 1179 if (TraceMethodReplacement && old != nullptr) { 1180 ResourceMark rm; 1181 char *method_name = method->name_and_sig_as_C_string(); 1182 tty->print_cr("Replacing method %s", method_name); 1183 } 1184 if (old != nullptr) { 1185 old->make_not_used(); 1186 } 1187 } 1188 1189 LogTarget(Info, nmethod, install) lt; 1190 if (lt.is_enabled()) { 1191 ResourceMark rm; 1192 char *method_name = method->name_and_sig_as_C_string(); 1193 lt.print("Installing method (%d) %s ", 1194 task()->comp_level(), method_name); 1195 } 1196 // Allow the code to be executed 1197 MutexLocker ml(CompiledMethod_lock, Mutex::_no_safepoint_check_flag); 1198 if (nm->make_in_use()) { 1199 method->set_code(method, nm); 1200 } 1201 } else { 1202 LogTarget(Info, nmethod, install) lt; 1203 if (lt.is_enabled()) { 1204 ResourceMark rm; 1205 char *method_name = method->name_and_sig_as_C_string(); 1206 lt.print("Installing osr method (%d) %s @ %d", 1207 task()->comp_level(), method_name, entry_bci); 1208 } 1209 MutexLocker ml(CompiledMethod_lock, Mutex::_no_safepoint_check_flag); 1210 if (nm->make_in_use()) { 1211 method->method_holder()->add_osr_nmethod(nm); 1212 } 1213 } 1214 } 1215 } 1216 1217 NoSafepointVerifier nsv; 1218 if (nm != nullptr) { 1219 // Compilation succeeded, post what we know about it 1220 nm->post_compiled_method(task()); 1221 task()->set_num_inlined_bytecodes(num_inlined_bytecodes()); 1222 } else { 1223 // The CodeCache is full. 1224 record_failure("code cache is full"); 1225 } 1226 1227 // safepoints are allowed again 1228 } 1229 1230 // ------------------------------------------------------------------ 1231 // ciEnv::find_system_klass 1232 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) { 1233 VM_ENTRY_MARK; 1234 return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false); 1235 } 1236 1237 // ------------------------------------------------------------------ 1238 // ciEnv::comp_level 1239 int ciEnv::comp_level() { 1240 if (task() == nullptr) return CompilationPolicy::highest_compile_level(); 1241 return task()->comp_level(); 1242 } 1243 1244 // ------------------------------------------------------------------ 1245 // ciEnv::compile_id 1246 int ciEnv::compile_id() { 1247 if (task() == nullptr) return 0; 1248 return task()->compile_id(); 1249 } 1250 1251 // ------------------------------------------------------------------ 1252 // ciEnv::notice_inlined_method() 1253 void ciEnv::notice_inlined_method(ciMethod* method) { 1254 _num_inlined_bytecodes += method->code_size_for_inlining(); 1255 } 1256 1257 // ------------------------------------------------------------------ 1258 // ciEnv::num_inlined_bytecodes() 1259 int ciEnv::num_inlined_bytecodes() const { 1260 return _num_inlined_bytecodes; 1261 } 1262 1263 // ------------------------------------------------------------------ 1264 // ciEnv::record_failure() 1265 void ciEnv::record_failure(const char* reason) { 1266 if (_failure_reason == nullptr) { 1267 // Record the first failure reason. 1268 _failure_reason = reason; 1269 } 1270 } 1271 1272 void ciEnv::report_failure(const char* reason) { 1273 EventCompilationFailure event; 1274 if (event.should_commit()) { 1275 CompilerEvent::CompilationFailureEvent::post(event, compile_id(), reason); 1276 } 1277 } 1278 1279 // ------------------------------------------------------------------ 1280 // ciEnv::record_method_not_compilable() 1281 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) { 1282 int new_compilable = 1283 all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ; 1284 1285 // Only note transitions to a worse state 1286 if (new_compilable > _compilable) { 1287 if (log() != nullptr) { 1288 if (all_tiers) { 1289 log()->elem("method_not_compilable"); 1290 } else { 1291 log()->elem("method_not_compilable_at_tier level='%d'", 1292 current()->task()->comp_level()); 1293 } 1294 } 1295 _compilable = new_compilable; 1296 1297 // Reset failure reason; this one is more important. 1298 _failure_reason = nullptr; 1299 record_failure(reason); 1300 } 1301 } 1302 1303 // ------------------------------------------------------------------ 1304 // ciEnv::record_out_of_memory_failure() 1305 void ciEnv::record_out_of_memory_failure() { 1306 // If memory is low, we stop compiling methods. 1307 record_method_not_compilable("out of memory"); 1308 } 1309 1310 ciInstance* ciEnv::unloaded_ciinstance() { 1311 GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();) 1312 } 1313 1314 // ------------------------------------------------------------------ 1315 // Replay support 1316 1317 1318 // Lookup location descriptor for the class, if any. 1319 // Returns false if not found. 1320 bool ciEnv::dyno_loc(const InstanceKlass* ik, const char *&loc) const { 1321 bool found = false; 1322 int pos = _dyno_klasses->find_sorted<const InstanceKlass*, klass_compare>(ik, found); 1323 if (!found) { 1324 return false; 1325 } 1326 loc = _dyno_locs->at(pos); 1327 return found; 1328 } 1329 1330 // Associate the current location descriptor with the given class and record for later lookup. 1331 void ciEnv::set_dyno_loc(const InstanceKlass* ik) { 1332 const char *loc = os::strdup(_dyno_name); 1333 bool found = false; 1334 int pos = _dyno_klasses->find_sorted<const InstanceKlass*, klass_compare>(ik, found); 1335 if (found) { 1336 _dyno_locs->at_put(pos, loc); 1337 } else { 1338 _dyno_klasses->insert_before(pos, ik); 1339 _dyno_locs->insert_before(pos, loc); 1340 } 1341 } 1342 1343 // Associate the current location descriptor with the given class and record for later lookup. 1344 // If it turns out that there are multiple locations for the given class, that conflict should 1345 // be handled here. Currently we choose the first location found. 1346 void ciEnv::record_best_dyno_loc(const InstanceKlass* ik) { 1347 if (!ik->is_hidden()) { 1348 return; 1349 } 1350 const char *loc0; 1351 if (dyno_loc(ik, loc0)) { 1352 // TODO: found multiple references, see if we can improve 1353 if (Verbose) { 1354 tty->print_cr("existing call site @ %s for %s", 1355 loc0, ik->external_name()); 1356 } 1357 } else { 1358 set_dyno_loc(ik); 1359 } 1360 } 1361 1362 // Look up the location descriptor for the given class and print it to the output stream. 1363 bool ciEnv::print_dyno_loc(outputStream* out, const InstanceKlass* ik) const { 1364 const char *loc; 1365 if (dyno_loc(ik, loc)) { 1366 out->print("%s", loc); 1367 return true; 1368 } else { 1369 return false; 1370 } 1371 } 1372 1373 // Look up the location descriptor for the given class and return it as a string. 1374 // Returns null if no location is found. 1375 const char *ciEnv::dyno_name(const InstanceKlass* ik) const { 1376 if (ik->is_hidden()) { 1377 stringStream ss; 1378 if (print_dyno_loc(&ss, ik)) { 1379 ss.print(" ;"); // add terminator 1380 const char* call_site = ss.as_string(); 1381 return call_site; 1382 } 1383 } 1384 return nullptr; 1385 } 1386 1387 // Look up the location descriptor for the given class and return it as a string. 1388 // Returns the class name as a fallback if no location is found. 1389 const char *ciEnv::replay_name(ciKlass* k) const { 1390 if (k->is_instance_klass()) { 1391 return replay_name(k->as_instance_klass()->get_instanceKlass()); 1392 } 1393 return k->name()->as_quoted_ascii(); 1394 } 1395 1396 // Look up the location descriptor for the given class and return it as a string. 1397 // Returns the class name as a fallback if no location is found. 1398 const char *ciEnv::replay_name(const InstanceKlass* ik) const { 1399 const char* name = dyno_name(ik); 1400 if (name != nullptr) { 1401 return name; 1402 } 1403 return ik->name()->as_quoted_ascii(); 1404 } 1405 1406 // Process a java.lang.invoke.MemberName object and record any dynamic locations. 1407 void ciEnv::record_member(Thread* thread, oop member) { 1408 assert(java_lang_invoke_MemberName::is_instance(member), "!"); 1409 // Check MemberName.clazz field 1410 oop clazz = java_lang_invoke_MemberName::clazz(member); 1411 if (clazz->klass()->is_instance_klass()) { 1412 RecordLocation fp(this, "clazz"); 1413 InstanceKlass* ik = InstanceKlass::cast(clazz->klass()); 1414 record_best_dyno_loc(ik); 1415 } 1416 // Check MemberName.method.vmtarget field 1417 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(member); 1418 if (vmtarget != nullptr) { 1419 RecordLocation fp2(this, "<vmtarget>"); 1420 InstanceKlass* ik = vmtarget->method_holder(); 1421 record_best_dyno_loc(ik); 1422 } 1423 } 1424 1425 // Read an object field. Lookup is done by name only. 1426 static inline oop obj_field(oop obj, const char* name) { 1427 return ciReplay::obj_field(obj, name); 1428 } 1429 1430 // Process a java.lang.invoke.LambdaForm object and record any dynamic locations. 1431 void ciEnv::record_lambdaform(Thread* thread, oop form) { 1432 assert(java_lang_invoke_LambdaForm::is_instance(form), "!"); 1433 1434 { 1435 // Check LambdaForm.vmentry field 1436 oop member = java_lang_invoke_LambdaForm::vmentry(form); 1437 RecordLocation fp0(this, "vmentry"); 1438 record_member(thread, member); 1439 } 1440 1441 // Check LambdaForm.names array 1442 objArrayOop names = (objArrayOop)obj_field(form, "names"); 1443 if (names != nullptr) { 1444 RecordLocation lp0(this, "names"); 1445 int len = names->length(); 1446 for (int i = 0; i < len; ++i) { 1447 oop name = names->obj_at(i); 1448 RecordLocation lp1(this, "%d", i); 1449 // Check LambdaForm.names[i].function field 1450 RecordLocation lp2(this, "function"); 1451 oop function = obj_field(name, "function"); 1452 if (function != nullptr) { 1453 // Check LambdaForm.names[i].function.member field 1454 oop member = obj_field(function, "member"); 1455 if (member != nullptr) { 1456 RecordLocation lp3(this, "member"); 1457 record_member(thread, member); 1458 } 1459 // Check LambdaForm.names[i].function.resolvedHandle field 1460 oop mh = obj_field(function, "resolvedHandle"); 1461 if (mh != nullptr) { 1462 RecordLocation lp3(this, "resolvedHandle"); 1463 record_mh(thread, mh); 1464 } 1465 // Check LambdaForm.names[i].function.invoker field 1466 oop invoker = obj_field(function, "invoker"); 1467 if (invoker != nullptr) { 1468 RecordLocation lp3(this, "invoker"); 1469 record_mh(thread, invoker); 1470 } 1471 } 1472 } 1473 } 1474 } 1475 1476 // Process a java.lang.invoke.MethodHandle object and record any dynamic locations. 1477 void ciEnv::record_mh(Thread* thread, oop mh) { 1478 { 1479 // Check MethodHandle.form field 1480 oop form = java_lang_invoke_MethodHandle::form(mh); 1481 RecordLocation fp(this, "form"); 1482 record_lambdaform(thread, form); 1483 } 1484 // Check DirectMethodHandle.member field 1485 if (java_lang_invoke_DirectMethodHandle::is_instance(mh)) { 1486 oop member = java_lang_invoke_DirectMethodHandle::member(mh); 1487 RecordLocation fp(this, "member"); 1488 record_member(thread, member); 1489 } else { 1490 // Check <MethodHandle subclass>.argL<n> fields 1491 // Probably BoundMethodHandle.Species_L*, but we only care if the field exists 1492 char arg_name[] = "argLXX"; 1493 int max_arg = 99; 1494 for (int index = 0; index <= max_arg; ++index) { 1495 jio_snprintf(arg_name, sizeof (arg_name), "argL%d", index); 1496 oop arg = obj_field(mh, arg_name); 1497 if (arg != nullptr) { 1498 RecordLocation fp(this, "%s", arg_name); 1499 if (arg->klass()->is_instance_klass()) { 1500 InstanceKlass* ik2 = InstanceKlass::cast(arg->klass()); 1501 record_best_dyno_loc(ik2); 1502 record_call_site_obj(thread, arg); 1503 } 1504 } else { 1505 break; 1506 } 1507 } 1508 } 1509 } 1510 1511 // Process an object found at an invokedynamic/invokehandle call site and record any dynamic locations. 1512 // Types currently supported are MethodHandle and CallSite. 1513 // The object is typically the "appendix" object, or Bootstrap Method (BSM) object. 1514 void ciEnv::record_call_site_obj(Thread* thread, oop obj) 1515 { 1516 if (obj != nullptr) { 1517 if (java_lang_invoke_MethodHandle::is_instance(obj)) { 1518 record_mh(thread, obj); 1519 } else if (java_lang_invoke_ConstantCallSite::is_instance(obj)) { 1520 oop target = java_lang_invoke_CallSite::target(obj); 1521 if (target->klass()->is_instance_klass()) { 1522 RecordLocation fp(this, "target"); 1523 InstanceKlass* ik = InstanceKlass::cast(target->klass()); 1524 record_best_dyno_loc(ik); 1525 } 1526 } 1527 } 1528 } 1529 1530 // Process an adapter Method* found at an invokedynamic/invokehandle call site and record any dynamic locations. 1531 void ciEnv::record_call_site_method(Thread* thread, Method* adapter) { 1532 InstanceKlass* holder = adapter->method_holder(); 1533 if (!holder->is_hidden()) { 1534 return; 1535 } 1536 RecordLocation fp(this, "<adapter>"); 1537 record_best_dyno_loc(holder); 1538 } 1539 1540 // Process an invokedynamic call site and record any dynamic locations. 1541 void ciEnv::process_invokedynamic(const constantPoolHandle &cp, int indy_index, JavaThread* thread) { 1542 int index = cp->decode_invokedynamic_index(indy_index); 1543 ResolvedIndyEntry* indy_info = cp->resolved_indy_entry_at(index); 1544 if (indy_info->method() != nullptr) { 1545 // process the adapter 1546 Method* adapter = indy_info->method(); 1547 record_call_site_method(thread, adapter); 1548 // process the appendix 1549 oop appendix = cp->resolved_reference_from_indy(index); 1550 { 1551 RecordLocation fp(this, "<appendix>"); 1552 record_call_site_obj(thread, appendix); 1553 } 1554 // process the BSM 1555 int pool_index = indy_info->constant_pool_index(); 1556 BootstrapInfo bootstrap_specifier(cp, pool_index, index); 1557 oop bsm = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), thread); 1558 { 1559 RecordLocation fp(this, "<bsm>"); 1560 record_call_site_obj(thread, bsm); 1561 } 1562 } 1563 } 1564 1565 // Process an invokehandle call site and record any dynamic locations. 1566 void ciEnv::process_invokehandle(const constantPoolHandle &cp, int index, JavaThread* thread) { 1567 const int holder_index = cp->klass_ref_index_at(index); 1568 if (!cp->tag_at(holder_index).is_klass()) { 1569 return; // not resolved 1570 } 1571 Klass* holder = ConstantPool::klass_at_if_loaded(cp, holder_index); 1572 Symbol* name = cp->name_ref_at(index); 1573 if (MethodHandles::is_signature_polymorphic_name(holder, name)) { 1574 ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index)); 1575 if (cp_cache_entry->is_resolved(Bytecodes::_invokehandle)) { 1576 // process the adapter 1577 Method* adapter = cp_cache_entry->f1_as_method(); 1578 oop appendix = cp_cache_entry->appendix_if_resolved(cp); 1579 record_call_site_method(thread, adapter); 1580 // process the appendix 1581 { 1582 RecordLocation fp(this, "<appendix>"); 1583 record_call_site_obj(thread, appendix); 1584 } 1585 } 1586 } 1587 } 1588 1589 // Search the class hierarchy for dynamic classes reachable through dynamic call sites or 1590 // constant pool entries and record for future lookup. 1591 void ciEnv::find_dynamic_call_sites() { 1592 _dyno_klasses = new (arena()) GrowableArray<const InstanceKlass*>(arena(), 100, 0, nullptr); 1593 _dyno_locs = new (arena()) GrowableArray<const char *>(arena(), 100, 0, nullptr); 1594 1595 // Iterate over the class hierarchy 1596 for (ClassHierarchyIterator iter(vmClasses::Object_klass()); !iter.done(); iter.next()) { 1597 Klass* sub = iter.klass(); 1598 if (sub->is_instance_klass()) { 1599 InstanceKlass *isub = InstanceKlass::cast(sub); 1600 InstanceKlass* ik = isub; 1601 if (!ik->is_linked()) { 1602 continue; 1603 } 1604 if (ik->is_hidden()) { 1605 continue; 1606 } 1607 JavaThread* thread = JavaThread::current(); 1608 const constantPoolHandle pool(thread, ik->constants()); 1609 1610 // Look for invokedynamic/invokehandle call sites 1611 for (int i = 0; i < ik->methods()->length(); ++i) { 1612 Method* m = ik->methods()->at(i); 1613 1614 BytecodeStream bcs(methodHandle(thread, m)); 1615 while (!bcs.is_last_bytecode()) { 1616 Bytecodes::Code opcode = bcs.next(); 1617 opcode = bcs.raw_code(); 1618 switch (opcode) { 1619 case Bytecodes::_invokedynamic: 1620 case Bytecodes::_invokehandle: { 1621 RecordLocation fp(this, "@bci %s %s %s %d", 1622 ik->name()->as_quoted_ascii(), 1623 m->name()->as_quoted_ascii(), m->signature()->as_quoted_ascii(), 1624 bcs.bci()); 1625 if (opcode == Bytecodes::_invokedynamic) { 1626 int index = bcs.get_index_u4(); 1627 process_invokedynamic(pool, index, thread); 1628 } else { 1629 assert(opcode == Bytecodes::_invokehandle, "new switch label added?"); 1630 int cp_cache_index = bcs.get_index_u2_cpcache(); 1631 process_invokehandle(pool, cp_cache_index, thread); 1632 } 1633 break; 1634 } 1635 default: 1636 break; 1637 } 1638 } 1639 } 1640 1641 // Look for MethodHandle constant pool entries 1642 RecordLocation fp(this, "@cpi %s", ik->name()->as_quoted_ascii()); 1643 int len = pool->length(); 1644 for (int i = 0; i < len; ++i) { 1645 if (pool->tag_at(i).is_method_handle()) { 1646 bool found_it; 1647 oop mh = pool->find_cached_constant_at(i, found_it, thread); 1648 if (mh != nullptr) { 1649 RecordLocation fp(this, "%d", i); 1650 record_mh(thread, mh); 1651 } 1652 } 1653 } 1654 } 1655 } 1656 } 1657 1658 void ciEnv::dump_compile_data(outputStream* out) { 1659 CompileTask* task = this->task(); 1660 if (task) { 1661 #ifdef COMPILER2 1662 if (ReplayReduce && compiler_data() != nullptr) { 1663 // Dump C2 "reduced" inlining data. 1664 ((Compile*)compiler_data())->dump_inline_data_reduced(out); 1665 } 1666 #endif 1667 Method* method = task->method(); 1668 int entry_bci = task->osr_bci(); 1669 int comp_level = task->comp_level(); 1670 out->print("compile "); 1671 get_method(method)->dump_name_as_ascii(out); 1672 out->print(" %d %d", entry_bci, comp_level); 1673 if (compiler_data() != nullptr) { 1674 if (is_c2_compile(comp_level)) { 1675 #ifdef COMPILER2 1676 // Dump C2 inlining data. 1677 ((Compile*)compiler_data())->dump_inline_data(out); 1678 #endif 1679 } else if (is_c1_compile(comp_level)) { 1680 #ifdef COMPILER1 1681 // Dump C1 inlining data. 1682 ((Compilation*)compiler_data())->dump_inline_data(out); 1683 #endif 1684 } 1685 } 1686 out->cr(); 1687 } 1688 } 1689 1690 // Called from VM error reporter, so be careful. 1691 // Don't safepoint or acquire any locks. 1692 // 1693 void ciEnv::dump_replay_data_helper(outputStream* out) { 1694 NoSafepointVerifier no_safepoint; 1695 ResourceMark rm; 1696 1697 out->print_cr("version %d", REPLAY_VERSION); 1698 #if INCLUDE_JVMTI 1699 out->print_cr("JvmtiExport can_access_local_variables %d", _jvmti_can_access_local_variables); 1700 out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint); 1701 out->print_cr("JvmtiExport can_post_on_exceptions %d", _jvmti_can_post_on_exceptions); 1702 #endif // INCLUDE_JVMTI 1703 1704 find_dynamic_call_sites(); 1705 1706 GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata(); 1707 out->print_cr("# %d ciObject found", objects->length()); 1708 1709 // The very first entry is the InstanceKlass of the root method of the current compilation in order to get the right 1710 // protection domain to load subsequent classes during replay compilation. 1711 ciInstanceKlass::dump_replay_instanceKlass(out, task()->method()->method_holder()); 1712 1713 for (int i = 0; i < objects->length(); i++) { 1714 objects->at(i)->dump_replay_data(out); 1715 } 1716 dump_compile_data(out); 1717 out->flush(); 1718 } 1719 1720 // Called from VM error reporter, so be careful. 1721 // Don't safepoint or acquire any locks. 1722 // 1723 void ciEnv::dump_replay_data_unsafe(outputStream* out) { 1724 GUARDED_VM_ENTRY( 1725 dump_replay_data_helper(out); 1726 ) 1727 } 1728 1729 void ciEnv::dump_replay_data(outputStream* out) { 1730 GUARDED_VM_ENTRY( 1731 MutexLocker ml(Compile_lock); 1732 dump_replay_data_helper(out); 1733 ) 1734 } 1735 1736 void ciEnv::dump_replay_data(int compile_id) { 1737 char buffer[64]; 1738 int ret = jio_snprintf(buffer, sizeof(buffer), "replay_pid%d_compid%d.log", os::current_process_id(), compile_id); 1739 if (ret > 0) { 1740 int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666); 1741 if (fd != -1) { 1742 FILE* replay_data_file = os::fdopen(fd, "w"); 1743 if (replay_data_file != nullptr) { 1744 fileStream replay_data_stream(replay_data_file, /*need_close=*/true); 1745 dump_replay_data(&replay_data_stream); 1746 tty->print_cr("# Compiler replay data is saved as: %s", buffer); 1747 } else { 1748 tty->print_cr("# Can't open file to dump replay data."); 1749 } 1750 } 1751 } 1752 } 1753 1754 void ciEnv::dump_inline_data(int compile_id) { 1755 char buffer[64]; 1756 int ret = jio_snprintf(buffer, sizeof(buffer), "inline_pid%d_compid%d.log", os::current_process_id(), compile_id); 1757 if (ret > 0) { 1758 int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666); 1759 if (fd != -1) { 1760 FILE* inline_data_file = os::fdopen(fd, "w"); 1761 if (inline_data_file != nullptr) { 1762 fileStream replay_data_stream(inline_data_file, /*need_close=*/true); 1763 GUARDED_VM_ENTRY( 1764 MutexLocker ml(Compile_lock); 1765 dump_compile_data(&replay_data_stream); 1766 ) 1767 replay_data_stream.flush(); 1768 tty->print("# Compiler inline data is saved as: "); 1769 tty->print_cr("%s", buffer); 1770 } else { 1771 tty->print_cr("# Can't open file to dump inline data."); 1772 } 1773 } 1774 } 1775 }