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