1 /* 2 * Copyright (c) 1999, 2018, 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 "jvmci/jvmciEnv.hpp" 27 #include "classfile/javaAssertions.hpp" 28 #include "classfile/systemDictionary.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "code/codeCache.hpp" 31 #include "code/scopeDesc.hpp" 32 #include "compiler/compileBroker.hpp" 33 #include "compiler/compileLog.hpp" 34 #include "compiler/compilerOracle.hpp" 35 #include "interpreter/linkResolver.hpp" 36 #include "memory/allocation.inline.hpp" 37 #include "memory/oopFactory.hpp" 38 #include "memory/resourceArea.hpp" 39 #include "memory/universe.hpp" 40 #include "oops/constantPool.inline.hpp" 41 #include "oops/cpCache.inline.hpp" 42 #include "oops/method.inline.hpp" 43 #include "oops/methodData.hpp" 44 #include "oops/objArrayKlass.hpp" 45 #include "oops/oop.inline.hpp" 46 #include "prims/jvmtiExport.hpp" 47 #include "runtime/fieldDescriptor.inline.hpp" 48 #include "runtime/handles.inline.hpp" 49 #include "runtime/init.hpp" 50 #include "runtime/reflection.hpp" 51 #include "runtime/sharedRuntime.hpp" 52 #include "runtime/sweeper.hpp" 53 #include "utilities/dtrace.hpp" 54 #include "jvmci/jvmciRuntime.hpp" 55 #include "jvmci/jvmciJavaClasses.hpp" 56 57 JVMCIEnv::JVMCIEnv(CompileTask* task, int system_dictionary_modification_counter): 58 _task(task), 59 _system_dictionary_modification_counter(system_dictionary_modification_counter), 60 _failure_reason(NULL), 61 _retryable(true) 62 { 63 // Get Jvmti capabilities under lock to get consistent values. 64 MutexLocker mu(JvmtiThreadState_lock); 65 _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint(); 66 _jvmti_can_access_local_variables = JvmtiExport::can_access_local_variables(); 67 _jvmti_can_post_on_exceptions = JvmtiExport::can_post_on_exceptions(); 68 } 69 70 // ------------------------------------------------------------------ 71 // Note: the logic of this method should mirror the logic of 72 // constantPoolOopDesc::verify_constant_pool_resolve. 73 bool JVMCIEnv::check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass) { 74 if (accessing_klass->is_objArray_klass()) { 75 accessing_klass = ObjArrayKlass::cast(accessing_klass)->bottom_klass(); 76 } 77 if (!accessing_klass->is_instance_klass()) { 78 return true; 79 } 80 81 if (resolved_klass->is_objArray_klass()) { 82 // Find the element klass, if this is an array. 83 resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass(); 84 } 85 if (resolved_klass->is_instance_klass()) { 86 Reflection::VerifyClassAccessResults result = 87 Reflection::verify_class_access(accessing_klass, InstanceKlass::cast(resolved_klass), true); 88 return result == Reflection::ACCESS_OK; 89 } 90 return true; 91 } 92 93 // ------------------------------------------------------------------ 94 Klass* JVMCIEnv::get_klass_by_name_impl(Klass* accessing_klass, 95 const constantPoolHandle& cpool, 96 Symbol* sym, 97 bool require_local) { 98 JVMCI_EXCEPTION_CONTEXT; 99 100 // Now we need to check the SystemDictionary 101 if (sym->char_at(0) == 'L' && 102 sym->char_at(sym->utf8_length()-1) == ';') { 103 // This is a name from a signature. Strip off the trimmings. 104 // Call recursive to keep scope of strippedsym. 105 TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1, 106 sym->utf8_length()-2, 107 CHECK_NULL); 108 return get_klass_by_name_impl(accessing_klass, cpool, strippedsym, require_local); 109 } 110 111 Handle loader(THREAD, (oop)NULL); 112 Handle domain(THREAD, (oop)NULL); 113 if (accessing_klass != NULL) { 114 loader = Handle(THREAD, accessing_klass->class_loader()); 115 domain = Handle(THREAD, accessing_klass->protection_domain()); 116 } 117 118 Klass* found_klass = NULL; 119 { 120 ttyUnlocker ttyul; // release tty lock to avoid ordering problems 121 MutexLocker ml(Compile_lock); 122 if (!require_local) { 123 found_klass = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader, CHECK_NULL); 124 } else { 125 found_klass = SystemDictionary::find_instance_or_array_klass(sym, loader, domain, CHECK_NULL); 126 } 127 } 128 129 // If we fail to find an array klass, look again for its element type. 130 // The element type may be available either locally or via constraints. 131 // In either case, if we can find the element type in the system dictionary, 132 // we must build an array type around it. The CI requires array klasses 133 // to be loaded if their element klasses are loaded, except when memory 134 // is exhausted. 135 if (sym->char_at(0) == '[' && 136 (sym->char_at(1) == '[' || sym->char_at(1) == 'L')) { 137 // We have an unloaded array. 138 // Build it on the fly if the element class exists. 139 TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1, 140 sym->utf8_length()-1, 141 CHECK_NULL); 142 143 // Get element Klass recursively. 144 Klass* elem_klass = 145 get_klass_by_name_impl(accessing_klass, 146 cpool, 147 elem_sym, 148 require_local); 149 if (elem_klass != NULL) { 150 // Now make an array for it 151 return elem_klass->array_klass(THREAD); 152 } 153 } 154 155 if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) { 156 // Look inside the constant pool for pre-resolved class entries. 157 for (int i = cpool->length() - 1; i >= 1; i--) { 158 if (cpool->tag_at(i).is_klass()) { 159 Klass* kls = cpool->resolved_klass_at(i); 160 if (kls->name() == sym) { 161 return kls; 162 } 163 } 164 } 165 } 166 167 return found_klass; 168 } 169 170 // ------------------------------------------------------------------ 171 Klass* JVMCIEnv::get_klass_by_name(Klass* accessing_klass, 172 Symbol* klass_name, 173 bool require_local) { 174 ResourceMark rm; 175 constantPoolHandle cpool; 176 return get_klass_by_name_impl(accessing_klass, 177 cpool, 178 klass_name, 179 require_local); 180 } 181 182 // ------------------------------------------------------------------ 183 // Implementation of get_klass_by_index. 184 Klass* JVMCIEnv::get_klass_by_index_impl(const constantPoolHandle& cpool, 185 int index, 186 bool& is_accessible, 187 Klass* accessor) { 188 JVMCI_EXCEPTION_CONTEXT; 189 Klass* klass = ConstantPool::klass_at_if_loaded(cpool, index); 190 Symbol* klass_name = NULL; 191 if (klass == NULL) { 192 klass_name = cpool->klass_name_at(index); 193 } 194 195 if (klass == NULL) { 196 // Not found in constant pool. Use the name to do the lookup. 197 Klass* k = get_klass_by_name_impl(accessor, 198 cpool, 199 klass_name, 200 false); 201 // Calculate accessibility the hard way. 202 if (k == NULL) { 203 is_accessible = false; 204 } else if (k->class_loader() != accessor->class_loader() && 205 get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) { 206 // Loaded only remotely. Not linked yet. 207 is_accessible = false; 208 } else { 209 // Linked locally, and we must also check public/private, etc. 210 is_accessible = check_klass_accessibility(accessor, k); 211 } 212 if (!is_accessible) { 213 return NULL; 214 } 215 return k; 216 } 217 218 // It is known to be accessible, since it was found in the constant pool. 219 is_accessible = true; 220 return klass; 221 } 222 223 // ------------------------------------------------------------------ 224 // Get a klass from the constant pool. 225 Klass* JVMCIEnv::get_klass_by_index(const constantPoolHandle& cpool, 226 int index, 227 bool& is_accessible, 228 Klass* accessor) { 229 ResourceMark rm; 230 return get_klass_by_index_impl(cpool, index, is_accessible, accessor); 231 } 232 233 // ------------------------------------------------------------------ 234 // Implementation of get_field_by_index. 235 // 236 // Implementation note: the results of field lookups are cached 237 // in the accessor klass. 238 void JVMCIEnv::get_field_by_index_impl(InstanceKlass* klass, fieldDescriptor& field_desc, 239 int index) { 240 JVMCI_EXCEPTION_CONTEXT; 241 242 assert(klass->is_linked(), "must be linked before using its constant-pool"); 243 244 constantPoolHandle cpool(thread, klass->constants()); 245 246 // Get the field's name, signature, and type. 247 Symbol* name = cpool->name_ref_at(index); 248 249 int nt_index = cpool->name_and_type_ref_index_at(index); 250 int sig_index = cpool->signature_ref_index_at(nt_index); 251 Symbol* signature = cpool->symbol_at(sig_index); 252 253 // Get the field's declared holder. 254 int holder_index = cpool->klass_ref_index_at(index); 255 bool holder_is_accessible; 256 Klass* declared_holder = get_klass_by_index(cpool, holder_index, 257 holder_is_accessible, 258 klass); 259 260 // The declared holder of this field may not have been loaded. 261 // Bail out with partial field information. 262 if (!holder_is_accessible) { 263 return; 264 } 265 266 267 // Perform the field lookup. 268 Klass* canonical_holder = 269 InstanceKlass::cast(declared_holder)->find_field(name, signature, &field_desc); 270 if (canonical_holder == NULL) { 271 return; 272 } 273 274 assert(canonical_holder == field_desc.field_holder(), "just checking"); 275 } 276 277 // ------------------------------------------------------------------ 278 // Get a field by index from a klass's constant pool. 279 void JVMCIEnv::get_field_by_index(InstanceKlass* accessor, fieldDescriptor& fd, int index) { 280 ResourceMark rm; 281 return get_field_by_index_impl(accessor, fd, index); 282 } 283 284 // ------------------------------------------------------------------ 285 // Perform an appropriate method lookup based on accessor, holder, 286 // name, signature, and bytecode. 287 methodHandle JVMCIEnv::lookup_method(InstanceKlass* accessor, 288 Klass* holder, 289 Symbol* name, 290 Symbol* sig, 291 Bytecodes::Code bc, 292 constantTag tag) { 293 // Accessibility checks are performed in JVMCIEnv::get_method_by_index_impl(). 294 assert(check_klass_accessibility(accessor, holder), "holder not accessible"); 295 296 methodHandle dest_method; 297 LinkInfo link_info(holder, name, sig, accessor, LinkInfo::needs_access_check, tag); 298 switch (bc) { 299 case Bytecodes::_invokestatic: 300 dest_method = 301 LinkResolver::resolve_static_call_or_null(link_info); 302 break; 303 case Bytecodes::_invokespecial: 304 dest_method = 305 LinkResolver::resolve_special_call_or_null(link_info); 306 break; 307 case Bytecodes::_invokeinterface: 308 dest_method = 309 LinkResolver::linktime_resolve_interface_method_or_null(link_info); 310 break; 311 case Bytecodes::_invokevirtual: 312 dest_method = 313 LinkResolver::linktime_resolve_virtual_method_or_null(link_info); 314 break; 315 default: ShouldNotReachHere(); 316 } 317 318 return dest_method; 319 } 320 321 322 // ------------------------------------------------------------------ 323 methodHandle JVMCIEnv::get_method_by_index_impl(const constantPoolHandle& cpool, 324 int index, Bytecodes::Code bc, 325 InstanceKlass* accessor) { 326 if (bc == Bytecodes::_invokedynamic) { 327 ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index); 328 bool is_resolved = !cpce->is_f1_null(); 329 if (is_resolved) { 330 // Get the invoker Method* from the constant pool. 331 // (The appendix argument, if any, will be noted in the method's signature.) 332 Method* adapter = cpce->f1_as_method(); 333 return methodHandle(adapter); 334 } 335 336 return NULL; 337 } 338 339 int holder_index = cpool->klass_ref_index_at(index); 340 bool holder_is_accessible; 341 Klass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor); 342 343 // Get the method's name and signature. 344 Symbol* name_sym = cpool->name_ref_at(index); 345 Symbol* sig_sym = cpool->signature_ref_at(index); 346 347 if (cpool->has_preresolution() 348 || ((holder == SystemDictionary::MethodHandle_klass() || holder == SystemDictionary::VarHandle_klass()) && 349 MethodHandles::is_signature_polymorphic_name(holder, name_sym))) { 350 // Short-circuit lookups for JSR 292-related call sites. 351 // That is, do not rely only on name-based lookups, because they may fail 352 // if the names are not resolvable in the boot class loader (7056328). 353 switch (bc) { 354 case Bytecodes::_invokevirtual: 355 case Bytecodes::_invokeinterface: 356 case Bytecodes::_invokespecial: 357 case Bytecodes::_invokestatic: 358 { 359 Method* m = ConstantPool::method_at_if_loaded(cpool, index); 360 if (m != NULL) { 361 return m; 362 } 363 } 364 break; 365 default: 366 break; 367 } 368 } 369 370 if (holder_is_accessible) { // Our declared holder is loaded. 371 constantTag tag = cpool->tag_ref_at(index); 372 methodHandle m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag); 373 if (!m.is_null()) { 374 // We found the method. 375 return m; 376 } 377 } 378 379 // Either the declared holder was not loaded, or the method could 380 // not be found. 381 382 return NULL; 383 } 384 385 // ------------------------------------------------------------------ 386 InstanceKlass* JVMCIEnv::get_instance_klass_for_declared_method_holder(Klass* method_holder) { 387 // For the case of <array>.clone(), the method holder can be an ArrayKlass* 388 // instead of an InstanceKlass*. For that case simply pretend that the 389 // declared holder is Object.clone since that's where the call will bottom out. 390 if (method_holder->is_instance_klass()) { 391 return InstanceKlass::cast(method_holder); 392 } else if (method_holder->is_array_klass()) { 393 return SystemDictionary::Object_klass(); 394 } else { 395 ShouldNotReachHere(); 396 } 397 return NULL; 398 } 399 400 401 // ------------------------------------------------------------------ 402 methodHandle JVMCIEnv::get_method_by_index(const constantPoolHandle& cpool, 403 int index, Bytecodes::Code bc, 404 InstanceKlass* accessor) { 405 ResourceMark rm; 406 return get_method_by_index_impl(cpool, index, bc, accessor); 407 } 408 409 // ------------------------------------------------------------------ 410 // Check for changes to the system dictionary during compilation 411 // class loads, evolution, breakpoints 412 JVMCIEnv::CodeInstallResult JVMCIEnv::validate_compile_task_dependencies(Dependencies* dependencies, Handle compiled_code, 413 JVMCIEnv* env, char** failure_detail) { 414 // If JVMTI capabilities were enabled during compile, the compilation is invalidated. 415 if (env != NULL) { 416 if (!env->_jvmti_can_hotswap_or_post_breakpoint && JvmtiExport::can_hotswap_or_post_breakpoint()) { 417 *failure_detail = (char*) "Hotswapping or breakpointing was enabled during compilation"; 418 return JVMCIEnv::dependencies_failed; 419 } 420 } 421 422 // Dependencies must be checked when the system dictionary changes 423 // or if we don't know whether it has changed (i.e., env == NULL). 424 bool counter_changed = env == NULL || env->_system_dictionary_modification_counter != SystemDictionary::number_of_modifications(); 425 CompileTask* task = env == NULL ? NULL : env->task(); 426 Dependencies::DepType result = dependencies->validate_dependencies(task, counter_changed, failure_detail); 427 if (result == Dependencies::end_marker) { 428 return JVMCIEnv::ok; 429 } 430 431 if (!Dependencies::is_klass_type(result) || counter_changed) { 432 return JVMCIEnv::dependencies_failed; 433 } 434 // The dependencies were invalid at the time of installation 435 // without any intervening modification of the system 436 // dictionary. That means they were invalidly constructed. 437 return JVMCIEnv::dependencies_invalid; 438 } 439 440 // ------------------------------------------------------------------ 441 JVMCIEnv::CodeInstallResult JVMCIEnv::register_method( 442 const methodHandle& method, 443 nmethod*& nm, 444 int entry_bci, 445 CodeOffsets* offsets, 446 int orig_pc_offset, 447 CodeBuffer* code_buffer, 448 int frame_words, 449 OopMapSet* oop_map_set, 450 ExceptionHandlerTable* handler_table, 451 AbstractCompiler* compiler, 452 DebugInformationRecorder* debug_info, 453 Dependencies* dependencies, 454 JVMCIEnv* env, 455 int compile_id, 456 bool has_unsafe_access, 457 bool has_wide_vector, 458 Handle installed_code, 459 Handle compiled_code, 460 Handle speculation_log) { 461 JVMCI_EXCEPTION_CONTEXT; 462 nm = NULL; 463 int comp_level = CompLevel_full_optimization; 464 char* failure_detail = NULL; 465 JVMCIEnv::CodeInstallResult result; 466 { 467 // To prevent compile queue updates. 468 MutexLocker locker(MethodCompileQueue_lock, THREAD); 469 470 // Prevent SystemDictionary::add_to_hierarchy from running 471 // and invalidating our dependencies until we install this method. 472 MutexLocker ml(Compile_lock); 473 474 // Encode the dependencies now, so we can check them right away. 475 dependencies->encode_content_bytes(); 476 477 // Record the dependencies for the current compile in the log 478 if (LogCompilation) { 479 for (Dependencies::DepStream deps(dependencies); deps.next(); ) { 480 deps.log_dependency(); 481 } 482 } 483 484 // Check for {class loads, evolution, breakpoints} during compilation 485 result = validate_compile_task_dependencies(dependencies, compiled_code, env, &failure_detail); 486 if (result != JVMCIEnv::ok) { 487 // While not a true deoptimization, it is a preemptive decompile. 488 MethodData* mdp = method()->method_data(); 489 if (mdp != NULL) { 490 mdp->inc_decompile_count(); 491 #ifdef ASSERT 492 if (mdp->decompile_count() > (uint)PerMethodRecompilationCutoff) { 493 ResourceMark m; 494 tty->print_cr("WARN: endless recompilation of %s. Method was set to not compilable.", method()->name_and_sig_as_C_string()); 495 } 496 #endif 497 } 498 499 // All buffers in the CodeBuffer are allocated in the CodeCache. 500 // If the code buffer is created on each compile attempt 501 // as in C2, then it must be freed. 502 //code_buffer->free_blob(); 503 } else { 504 ImplicitExceptionTable implicit_tbl; 505 nm = nmethod::new_nmethod(method, 506 compile_id, 507 entry_bci, 508 offsets, 509 orig_pc_offset, 510 debug_info, dependencies, code_buffer, 511 frame_words, oop_map_set, 512 handler_table, &implicit_tbl, 513 compiler, comp_level, 514 JNIHandles::make_weak_global(installed_code), 515 JNIHandles::make_weak_global(speculation_log)); 516 517 // Free codeBlobs 518 //code_buffer->free_blob(); 519 if (nm == NULL) { 520 // The CodeCache is full. Print out warning and disable compilation. 521 { 522 MutexUnlocker ml(Compile_lock); 523 MutexUnlocker locker(MethodCompileQueue_lock); 524 CompileBroker::handle_full_code_cache(CodeCache::get_code_blob_type(comp_level)); 525 } 526 } else { 527 nm->set_has_unsafe_access(has_unsafe_access); 528 nm->set_has_wide_vectors(has_wide_vector); 529 530 // Record successful registration. 531 // (Put nm into the task handle *before* publishing to the Java heap.) 532 CompileTask* task = env == NULL ? NULL : env->task(); 533 if (task != NULL) { 534 task->set_code(nm); 535 } 536 537 if (installed_code->is_a(HotSpotNmethod::klass()) && HotSpotNmethod::isDefault(installed_code())) { 538 if (entry_bci == InvocationEntryBci) { 539 if (TieredCompilation) { 540 // If there is an old version we're done with it 541 CompiledMethod* old = method->code(); 542 if (TraceMethodReplacement && old != NULL) { 543 ResourceMark rm; 544 char *method_name = method->name_and_sig_as_C_string(); 545 tty->print_cr("Replacing method %s", method_name); 546 } 547 if (old != NULL ) { 548 old->make_not_entrant(); 549 } 550 } 551 if (TraceNMethodInstalls) { 552 ResourceMark rm; 553 char *method_name = method->name_and_sig_as_C_string(); 554 ttyLocker ttyl; 555 tty->print_cr("Installing method (%d) %s [entry point: %p]", 556 comp_level, 557 method_name, nm->entry_point()); 558 } 559 // Allow the code to be executed 560 method->set_code(method, nm); 561 } else { 562 if (TraceNMethodInstalls ) { 563 ResourceMark rm; 564 char *method_name = method->name_and_sig_as_C_string(); 565 ttyLocker ttyl; 566 tty->print_cr("Installing osr method (%d) %s @ %d", 567 comp_level, 568 method_name, 569 entry_bci); 570 } 571 InstanceKlass::cast(method->method_holder())->add_osr_nmethod(nm); 572 } 573 } 574 nm->make_in_use(); 575 } 576 result = nm != NULL ? JVMCIEnv::ok :JVMCIEnv::cache_full; 577 } 578 } 579 580 // String creation must be done outside lock 581 if (failure_detail != NULL) { 582 // A failure to allocate the string is silently ignored. 583 Handle message = java_lang_String::create_from_str(failure_detail, THREAD); 584 HotSpotCompiledNmethod::set_installationFailureMessage(compiled_code, message()); 585 } 586 587 // JVMTI -- compiled method notification (must be done outside lock) 588 if (nm != NULL) { 589 nm->post_compiled_method_load_event(); 590 591 if (env == NULL) { 592 // This compile didn't come through the CompileBroker so perform the printing here 593 DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, compiler); 594 nm->maybe_print_nmethod(directive); 595 DirectivesStack::release(directive); 596 } 597 } 598 599 return result; 600 }