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