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