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