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