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