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 bool has_scoped_access, 1036 int immediate_oops_patched) { 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 1094 if (failing()) { 1095 // While not a true deoptimization, it is a preemptive decompile. 1096 MethodData* mdo = method()->method_data(); 1097 if (mdo != nullptr && _inc_decompile_count_on_failure) { 1098 mdo->inc_decompile_count(); 1099 } 1100 1101 // All buffers in the CodeBuffer are allocated in the CodeCache. 1102 // If the code buffer is created on each compile attempt 1103 // as in C2, then it must be freed. 1104 code_buffer->free_blob(); 1105 return; 1106 } 1107 1108 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry"); 1109 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry"); 1110 1111 nm = nmethod::new_nmethod(method, 1112 compile_id(), 1113 entry_bci, 1114 offsets, 1115 orig_pc_offset, 1116 debug_info(), dependencies(), code_buffer, 1117 frame_words, oop_map_set, 1118 handler_table, inc_table, 1119 compiler, CompLevel(task()->comp_level())); 1120 1121 // Free codeBlobs 1122 code_buffer->free_blob(); 1123 1124 if (nm != nullptr) { 1125 nm->set_has_unsafe_access(has_unsafe_access); 1126 nm->set_has_wide_vectors(has_wide_vectors); 1127 nm->set_has_monitors(has_monitors); 1128 nm->set_has_scoped_access(has_scoped_access); 1129 assert(!method->is_synchronized() || nm->has_monitors(), ""); 1130 1131 if (entry_bci == InvocationEntryBci) { 1132 if (TieredCompilation) { 1133 // If there is an old version we're done with it 1134 nmethod* old = method->code(); 1135 if (TraceMethodReplacement && old != nullptr) { 1136 ResourceMark rm; 1137 char *method_name = method->name_and_sig_as_C_string(); 1138 tty->print_cr("Replacing method %s", method_name); 1139 } 1140 if (old != nullptr) { 1141 old->make_not_used(); 1142 } 1143 } 1144 1145 LogTarget(Info, nmethod, install) lt; 1146 if (lt.is_enabled()) { 1147 ResourceMark rm; 1148 char *method_name = method->name_and_sig_as_C_string(); 1149 lt.print("Installing method (%d) %s ", 1150 task()->comp_level(), method_name); 1151 } 1152 // Allow the code to be executed 1153 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag); 1154 if (nm->make_in_use()) { 1155 method->set_code(method, nm); 1156 } 1157 } else { 1158 LogTarget(Info, nmethod, install) lt; 1159 if (lt.is_enabled()) { 1160 ResourceMark rm; 1161 char *method_name = method->name_and_sig_as_C_string(); 1162 lt.print("Installing osr method (%d) %s @ %d", 1163 task()->comp_level(), method_name, entry_bci); 1164 } 1165 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag); 1166 if (nm->make_in_use()) { 1167 method->method_holder()->add_osr_nmethod(nm); 1168 } 1169 } 1170 } 1171 } 1172 1173 NoSafepointVerifier nsv; 1174 if (nm != nullptr) { 1175 // Compilation succeeded, post what we know about it 1176 nm->post_compiled_method(task()); 1177 task()->set_num_inlined_bytecodes(num_inlined_bytecodes()); 1178 } else { 1179 // The CodeCache is full. 1180 record_failure("code cache is full"); 1181 } 1182 1183 // safepoints are allowed again 1184 } 1185 1186 // ------------------------------------------------------------------ 1187 // ciEnv::find_system_klass 1188 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) { 1189 VM_ENTRY_MARK; 1190 return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false); 1191 } 1192 1193 // ------------------------------------------------------------------ 1194 // ciEnv::comp_level 1195 int ciEnv::comp_level() { 1196 if (task() == nullptr) return CompilationPolicy::highest_compile_level(); 1197 return task()->comp_level(); 1198 } 1199 1200 // ------------------------------------------------------------------ 1201 // ciEnv::compile_id 1202 int ciEnv::compile_id() { 1203 if (task() == nullptr) return 0; 1204 return task()->compile_id(); 1205 } 1206 1207 // ------------------------------------------------------------------ 1208 // ciEnv::notice_inlined_method() 1209 void ciEnv::notice_inlined_method(ciMethod* method) { 1210 _num_inlined_bytecodes += method->code_size_for_inlining(); 1211 } 1212 1213 // ------------------------------------------------------------------ 1214 // ciEnv::num_inlined_bytecodes() 1215 int ciEnv::num_inlined_bytecodes() const { 1216 return _num_inlined_bytecodes; 1217 } 1218 1219 // ------------------------------------------------------------------ 1220 // ciEnv::record_failure() 1221 void ciEnv::record_failure(const char* reason) { 1222 if (_failure_reason.get() == nullptr) { 1223 // Record the first failure reason. 1224 _failure_reason.set(reason); 1225 } 1226 } 1227 1228 void ciEnv::report_failure(const char* reason) { 1229 EventCompilationFailure event; 1230 if (event.should_commit()) { 1231 CompilerEvent::CompilationFailureEvent::post(event, compile_id(), reason); 1232 } 1233 } 1234 1235 // ------------------------------------------------------------------ 1236 // ciEnv::record_method_not_compilable() 1237 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) { 1238 int new_compilable = 1239 all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ; 1240 1241 // Only note transitions to a worse state 1242 if (new_compilable > _compilable) { 1243 if (log() != nullptr) { 1244 if (all_tiers) { 1245 log()->elem("method_not_compilable"); 1246 } else { 1247 log()->elem("method_not_compilable_at_tier level='%d'", 1248 current()->task()->comp_level()); 1249 } 1250 } 1251 _compilable = new_compilable; 1252 1253 // Reset failure reason; this one is more important. 1254 _failure_reason.clear(); 1255 record_failure(reason); 1256 } 1257 } 1258 1259 // ------------------------------------------------------------------ 1260 // ciEnv::record_out_of_memory_failure() 1261 void ciEnv::record_out_of_memory_failure() { 1262 // If memory is low, we stop compiling methods. 1263 record_method_not_compilable("out of memory"); 1264 } 1265 1266 ciInstance* ciEnv::unloaded_ciinstance() { 1267 GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();) 1268 } 1269 1270 // ------------------------------------------------------------------ 1271 // Replay support 1272 1273 1274 // Lookup location descriptor for the class, if any. 1275 // Returns false if not found. 1276 bool ciEnv::dyno_loc(const InstanceKlass* ik, const char *&loc) const { 1277 bool found = false; 1278 int pos = _dyno_klasses->find_sorted<const InstanceKlass*, klass_compare>(ik, found); 1279 if (!found) { 1280 return false; 1281 } 1282 loc = _dyno_locs->at(pos); 1283 return found; 1284 } 1285 1286 // Associate the current location descriptor with the given class and record for later lookup. 1287 void ciEnv::set_dyno_loc(const InstanceKlass* ik) { 1288 const char *loc = os::strdup(_dyno_name); 1289 bool found = false; 1290 int pos = _dyno_klasses->find_sorted<const InstanceKlass*, klass_compare>(ik, found); 1291 if (found) { 1292 _dyno_locs->at_put(pos, loc); 1293 } else { 1294 _dyno_klasses->insert_before(pos, ik); 1295 _dyno_locs->insert_before(pos, loc); 1296 } 1297 } 1298 1299 // Associate the current location descriptor with the given class and record for later lookup. 1300 // If it turns out that there are multiple locations for the given class, that conflict should 1301 // be handled here. Currently we choose the first location found. 1302 void ciEnv::record_best_dyno_loc(const InstanceKlass* ik) { 1303 if (!ik->is_hidden()) { 1304 return; 1305 } 1306 const char *loc0; 1307 if (!dyno_loc(ik, loc0)) { 1308 set_dyno_loc(ik); 1309 } 1310 } 1311 1312 // Look up the location descriptor for the given class and print it to the output stream. 1313 bool ciEnv::print_dyno_loc(outputStream* out, const InstanceKlass* ik) const { 1314 const char *loc; 1315 if (dyno_loc(ik, loc)) { 1316 out->print("%s", loc); 1317 return true; 1318 } else { 1319 return false; 1320 } 1321 } 1322 1323 // Look up the location descriptor for the given class and return it as a string. 1324 // Returns null if no location is found. 1325 const char *ciEnv::dyno_name(const InstanceKlass* ik) const { 1326 if (ik->is_hidden()) { 1327 stringStream ss; 1328 if (print_dyno_loc(&ss, ik)) { 1329 ss.print(" ;"); // add terminator 1330 const char* call_site = ss.as_string(); 1331 return call_site; 1332 } 1333 } 1334 return nullptr; 1335 } 1336 1337 // Look up the location descriptor for the given class and return it as a string. 1338 // Returns the class name as a fallback if no location is found. 1339 const char *ciEnv::replay_name(ciKlass* k) const { 1340 if (k->is_instance_klass()) { 1341 return replay_name(k->as_instance_klass()->get_instanceKlass()); 1342 } 1343 return k->name()->as_quoted_ascii(); 1344 } 1345 1346 // Look up the location descriptor for the given class and return it as a string. 1347 // Returns the class name as a fallback if no location is found. 1348 const char *ciEnv::replay_name(const InstanceKlass* ik) const { 1349 const char* name = dyno_name(ik); 1350 if (name != nullptr) { 1351 return name; 1352 } 1353 return ik->name()->as_quoted_ascii(); 1354 } 1355 1356 // Process a java.lang.invoke.MemberName object and record any dynamic locations. 1357 void ciEnv::record_member(Thread* thread, oop member) { 1358 assert(java_lang_invoke_MemberName::is_instance(member), "!"); 1359 // Check MemberName.clazz field 1360 oop clazz = java_lang_invoke_MemberName::clazz(member); 1361 if (clazz->klass()->is_instance_klass()) { 1362 RecordLocation fp(this, "clazz"); 1363 InstanceKlass* ik = InstanceKlass::cast(clazz->klass()); 1364 record_best_dyno_loc(ik); 1365 } 1366 // Check MemberName.method.vmtarget field 1367 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(member); 1368 if (vmtarget != nullptr) { 1369 RecordLocation fp2(this, "<vmtarget>"); 1370 InstanceKlass* ik = vmtarget->method_holder(); 1371 record_best_dyno_loc(ik); 1372 } 1373 } 1374 1375 // Read an object field. Lookup is done by name only. 1376 static inline oop obj_field(oop obj, const char* name) { 1377 return ciReplay::obj_field(obj, name); 1378 } 1379 1380 // Process a java.lang.invoke.LambdaForm object and record any dynamic locations. 1381 void ciEnv::record_lambdaform(Thread* thread, oop form) { 1382 assert(java_lang_invoke_LambdaForm::is_instance(form), "!"); 1383 1384 { 1385 // Check LambdaForm.vmentry field 1386 oop member = java_lang_invoke_LambdaForm::vmentry(form); 1387 RecordLocation fp0(this, "vmentry"); 1388 record_member(thread, member); 1389 } 1390 1391 // Check LambdaForm.names array 1392 objArrayOop names = (objArrayOop)obj_field(form, "names"); 1393 if (names != nullptr) { 1394 RecordLocation lp0(this, "names"); 1395 int len = names->length(); 1396 for (int i = 0; i < len; ++i) { 1397 oop name = names->obj_at(i); 1398 RecordLocation lp1(this, "%d", i); 1399 // Check LambdaForm.names[i].function field 1400 RecordLocation lp2(this, "function"); 1401 oop function = obj_field(name, "function"); 1402 if (function != nullptr) { 1403 // Check LambdaForm.names[i].function.member field 1404 oop member = obj_field(function, "member"); 1405 if (member != nullptr) { 1406 RecordLocation lp3(this, "member"); 1407 record_member(thread, member); 1408 } 1409 // Check LambdaForm.names[i].function.resolvedHandle field 1410 oop mh = obj_field(function, "resolvedHandle"); 1411 if (mh != nullptr) { 1412 RecordLocation lp3(this, "resolvedHandle"); 1413 record_mh(thread, mh); 1414 } 1415 // Check LambdaForm.names[i].function.invoker field 1416 oop invoker = obj_field(function, "invoker"); 1417 if (invoker != nullptr) { 1418 RecordLocation lp3(this, "invoker"); 1419 record_mh(thread, invoker); 1420 } 1421 } 1422 } 1423 } 1424 } 1425 1426 // Process a java.lang.invoke.MethodHandle object and record any dynamic locations. 1427 void ciEnv::record_mh(Thread* thread, oop mh) { 1428 { 1429 // Check MethodHandle.form field 1430 oop form = java_lang_invoke_MethodHandle::form(mh); 1431 RecordLocation fp(this, "form"); 1432 record_lambdaform(thread, form); 1433 } 1434 // Check DirectMethodHandle.member field 1435 if (java_lang_invoke_DirectMethodHandle::is_instance(mh)) { 1436 oop member = java_lang_invoke_DirectMethodHandle::member(mh); 1437 RecordLocation fp(this, "member"); 1438 record_member(thread, member); 1439 } else { 1440 // Check <MethodHandle subclass>.argL<n> fields 1441 // Probably BoundMethodHandle.Species_L*, but we only care if the field exists 1442 char arg_name[] = "argLXX"; 1443 int max_arg = 99; 1444 for (int index = 0; index <= max_arg; ++index) { 1445 jio_snprintf(arg_name, sizeof (arg_name), "argL%d", index); 1446 oop arg = obj_field(mh, arg_name); 1447 if (arg != nullptr) { 1448 RecordLocation fp(this, "%s", arg_name); 1449 if (arg->klass()->is_instance_klass()) { 1450 InstanceKlass* ik2 = InstanceKlass::cast(arg->klass()); 1451 record_best_dyno_loc(ik2); 1452 record_call_site_obj(thread, arg); 1453 } 1454 } else { 1455 break; 1456 } 1457 } 1458 } 1459 } 1460 1461 // Process an object found at an invokedynamic/invokehandle call site and record any dynamic locations. 1462 // Types currently supported are MethodHandle and CallSite. 1463 // The object is typically the "appendix" object, or Bootstrap Method (BSM) object. 1464 void ciEnv::record_call_site_obj(Thread* thread, oop obj) 1465 { 1466 if (obj != nullptr) { 1467 if (java_lang_invoke_MethodHandle::is_instance(obj)) { 1468 record_mh(thread, obj); 1469 } else if (java_lang_invoke_ConstantCallSite::is_instance(obj)) { 1470 oop target = java_lang_invoke_CallSite::target(obj); 1471 if (target->klass()->is_instance_klass()) { 1472 RecordLocation fp(this, "target"); 1473 InstanceKlass* ik = InstanceKlass::cast(target->klass()); 1474 record_best_dyno_loc(ik); 1475 } 1476 } 1477 } 1478 } 1479 1480 // Process an adapter Method* found at an invokedynamic/invokehandle call site and record any dynamic locations. 1481 void ciEnv::record_call_site_method(Thread* thread, Method* adapter) { 1482 InstanceKlass* holder = adapter->method_holder(); 1483 if (!holder->is_hidden()) { 1484 return; 1485 } 1486 RecordLocation fp(this, "<adapter>"); 1487 record_best_dyno_loc(holder); 1488 } 1489 1490 // Process an invokedynamic call site and record any dynamic locations. 1491 void ciEnv::process_invokedynamic(const constantPoolHandle &cp, int indy_index, JavaThread* thread) { 1492 ResolvedIndyEntry* indy_info = cp->resolved_indy_entry_at(indy_index); 1493 if (indy_info->method() != nullptr) { 1494 // process the adapter 1495 Method* adapter = indy_info->method(); 1496 record_call_site_method(thread, adapter); 1497 // process the appendix 1498 oop appendix = cp->resolved_reference_from_indy(indy_index); 1499 { 1500 RecordLocation fp(this, "<appendix>"); 1501 record_call_site_obj(thread, appendix); 1502 } 1503 // process the BSM 1504 int pool_index = indy_info->constant_pool_index(); 1505 BootstrapInfo bootstrap_specifier(cp, pool_index, indy_index); 1506 oop bsm = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), thread); 1507 { 1508 RecordLocation fp(this, "<bsm>"); 1509 record_call_site_obj(thread, bsm); 1510 } 1511 } 1512 } 1513 1514 // Process an invokehandle call site and record any dynamic locations. 1515 void ciEnv::process_invokehandle(const constantPoolHandle &cp, int index, JavaThread* thread) { 1516 const int holder_index = cp->klass_ref_index_at(index, Bytecodes::_invokehandle); 1517 if (!cp->tag_at(holder_index).is_klass()) { 1518 return; // not resolved 1519 } 1520 Klass* holder = ConstantPool::klass_at_if_loaded(cp, holder_index); 1521 Symbol* name = cp->name_ref_at(index, Bytecodes::_invokehandle); 1522 if (MethodHandles::is_signature_polymorphic_name(holder, name)) { 1523 ResolvedMethodEntry* method_entry = cp->resolved_method_entry_at(index); 1524 if (method_entry->is_resolved(Bytecodes::_invokehandle)) { 1525 // process the adapter 1526 Method* adapter = method_entry->method(); 1527 oop appendix = cp->cache()->appendix_if_resolved(method_entry); 1528 record_call_site_method(thread, adapter); 1529 // process the appendix 1530 { 1531 RecordLocation fp(this, "<appendix>"); 1532 record_call_site_obj(thread, appendix); 1533 } 1534 } 1535 } 1536 } 1537 1538 // Search the class hierarchy for dynamic classes reachable through dynamic call sites or 1539 // constant pool entries and record for future lookup. 1540 void ciEnv::find_dynamic_call_sites() { 1541 _dyno_klasses = new (arena()) GrowableArray<const InstanceKlass*>(arena(), 100, 0, nullptr); 1542 _dyno_locs = new (arena()) GrowableArray<const char *>(arena(), 100, 0, nullptr); 1543 1544 // Iterate over the class hierarchy 1545 for (ClassHierarchyIterator iter(vmClasses::Object_klass()); !iter.done(); iter.next()) { 1546 Klass* sub = iter.klass(); 1547 if (sub->is_instance_klass()) { 1548 InstanceKlass *isub = InstanceKlass::cast(sub); 1549 InstanceKlass* ik = isub; 1550 if (!ik->is_linked()) { 1551 continue; 1552 } 1553 if (ik->is_hidden()) { 1554 continue; 1555 } 1556 JavaThread* thread = JavaThread::current(); 1557 const constantPoolHandle pool(thread, ik->constants()); 1558 1559 // Look for invokedynamic/invokehandle call sites 1560 for (int i = 0; i < ik->methods()->length(); ++i) { 1561 Method* m = ik->methods()->at(i); 1562 1563 BytecodeStream bcs(methodHandle(thread, m)); 1564 while (!bcs.is_last_bytecode()) { 1565 Bytecodes::Code opcode = bcs.next(); 1566 opcode = bcs.raw_code(); 1567 switch (opcode) { 1568 case Bytecodes::_invokedynamic: 1569 case Bytecodes::_invokehandle: { 1570 RecordLocation fp(this, "@bci %s %s %s %d", 1571 ik->name()->as_quoted_ascii(), 1572 m->name()->as_quoted_ascii(), m->signature()->as_quoted_ascii(), 1573 bcs.bci()); 1574 if (opcode == Bytecodes::_invokedynamic) { 1575 int index = bcs.get_index_u4(); 1576 process_invokedynamic(pool, index, thread); 1577 } else { 1578 assert(opcode == Bytecodes::_invokehandle, "new switch label added?"); 1579 int cp_cache_index = bcs.get_index_u2(); 1580 process_invokehandle(pool, cp_cache_index, thread); 1581 } 1582 break; 1583 } 1584 default: 1585 break; 1586 } 1587 } 1588 } 1589 1590 // Look for MethodHandle constant pool entries 1591 RecordLocation fp(this, "@cpi %s", ik->name()->as_quoted_ascii()); 1592 int len = pool->length(); 1593 for (int i = 0; i < len; ++i) { 1594 if (pool->tag_at(i).is_method_handle()) { 1595 bool found_it; 1596 oop mh = pool->find_cached_constant_at(i, found_it, thread); 1597 if (mh != nullptr) { 1598 RecordLocation fp(this, "%d", i); 1599 record_mh(thread, mh); 1600 } 1601 } 1602 } 1603 } 1604 } 1605 } 1606 1607 void ciEnv::dump_compile_data(outputStream* out) { 1608 CompileTask* task = this->task(); 1609 if (task) { 1610 #ifdef COMPILER2 1611 if (ReplayReduce && compiler_data() != nullptr) { 1612 // Dump C2 "reduced" inlining data. 1613 ((Compile*)compiler_data())->dump_inline_data_reduced(out); 1614 } 1615 #endif 1616 Method* method = task->method(); 1617 int entry_bci = task->osr_bci(); 1618 int comp_level = task->comp_level(); 1619 out->print("compile "); 1620 get_method(method)->dump_name_as_ascii(out); 1621 out->print(" %d %d", entry_bci, comp_level); 1622 if (compiler_data() != nullptr) { 1623 if (is_c2_compile(comp_level)) { 1624 #ifdef COMPILER2 1625 // Dump C2 inlining data. 1626 ((Compile*)compiler_data())->dump_inline_data(out); 1627 #endif 1628 } else if (is_c1_compile(comp_level)) { 1629 #ifdef COMPILER1 1630 // Dump C1 inlining data. 1631 ((Compilation*)compiler_data())->dump_inline_data(out); 1632 #endif 1633 } 1634 } 1635 out->cr(); 1636 } 1637 } 1638 1639 // Called from VM error reporter, so be careful. 1640 // Don't safepoint or acquire any locks. 1641 // 1642 void ciEnv::dump_replay_data_helper(outputStream* out) { 1643 NoSafepointVerifier no_safepoint; 1644 ResourceMark rm; 1645 1646 dump_replay_data_version(out); 1647 #if INCLUDE_JVMTI 1648 out->print_cr("JvmtiExport can_access_local_variables %d", _jvmti_can_access_local_variables); 1649 out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint); 1650 out->print_cr("JvmtiExport can_post_on_exceptions %d", _jvmti_can_post_on_exceptions); 1651 #endif // INCLUDE_JVMTI 1652 1653 find_dynamic_call_sites(); 1654 1655 GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata(); 1656 out->print_cr("# %d ciObject found", objects->length()); 1657 1658 // The very first entry is the InstanceKlass of the root method of the current compilation in order to get the right 1659 // protection domain to load subsequent classes during replay compilation. 1660 ciInstanceKlass::dump_replay_instanceKlass(out, task()->method()->method_holder()); 1661 1662 for (int i = 0; i < objects->length(); i++) { 1663 objects->at(i)->dump_replay_data(out); 1664 } 1665 dump_compile_data(out); 1666 out->flush(); 1667 } 1668 1669 // Called from VM error reporter, so be careful. 1670 // Don't safepoint or acquire any locks. 1671 // 1672 void ciEnv::dump_replay_data_unsafe(outputStream* out) { 1673 GUARDED_VM_ENTRY( 1674 dump_replay_data_helper(out); 1675 ) 1676 } 1677 1678 void ciEnv::dump_replay_data(outputStream* out) { 1679 GUARDED_VM_ENTRY( 1680 MutexLocker ml(Compile_lock); 1681 dump_replay_data_helper(out); 1682 ) 1683 } 1684 1685 void ciEnv::dump_replay_data(int compile_id) { 1686 char buffer[64]; 1687 int ret = jio_snprintf(buffer, sizeof(buffer), "replay_pid%d_compid%d.log", os::current_process_id(), compile_id); 1688 if (ret > 0) { 1689 int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666); 1690 if (fd != -1) { 1691 FILE* replay_data_file = os::fdopen(fd, "w"); 1692 if (replay_data_file != nullptr) { 1693 fileStream replay_data_stream(replay_data_file, /*need_close=*/true); 1694 dump_replay_data(&replay_data_stream); 1695 tty->print_cr("# Compiler replay data is saved as: %s", buffer); 1696 } else { 1697 tty->print_cr("# Can't open file to dump replay data."); 1698 close(fd); 1699 } 1700 } 1701 } 1702 } 1703 1704 void ciEnv::dump_inline_data(int compile_id) { 1705 char buffer[64]; 1706 int ret = jio_snprintf(buffer, sizeof(buffer), "inline_pid%d_compid%d.log", os::current_process_id(), compile_id); 1707 if (ret > 0) { 1708 int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666); 1709 if (fd != -1) { 1710 FILE* inline_data_file = os::fdopen(fd, "w"); 1711 if (inline_data_file != nullptr) { 1712 fileStream replay_data_stream(inline_data_file, /*need_close=*/true); 1713 GUARDED_VM_ENTRY( 1714 MutexLocker ml(Compile_lock); 1715 dump_replay_data_version(&replay_data_stream); 1716 dump_compile_data(&replay_data_stream); 1717 ) 1718 replay_data_stream.flush(); 1719 tty->print("# Compiler inline data is saved as: "); 1720 tty->print_cr("%s", buffer); 1721 } else { 1722 tty->print_cr("# Can't open file to dump inline data."); 1723 close(fd); 1724 } 1725 } 1726 } 1727 } 1728 1729 void ciEnv::dump_replay_data_version(outputStream* out) { 1730 out->print_cr("version %d", REPLAY_VERSION); 1731 } --- EOF ---