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