1 /* 2 * Copyright (c) 2022, 2026, 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 "cds/aotClassLinker.hpp" 26 #include "cds/aotClassLocation.hpp" 27 #include "cds/aotConstantPoolResolver.hpp" 28 #include "cds/aotLogging.hpp" 29 #include "cds/archiveBuilder.hpp" 30 #include "cds/archiveUtils.inline.hpp" 31 #include "cds/cdsConfig.hpp" 32 #include "cds/finalImageRecipes.hpp" 33 #include "cds/heapShared.hpp" 34 #include "cds/lambdaFormInvokers.inline.hpp" 35 #include "classfile/classLoader.hpp" 36 #include "classfile/dictionary.hpp" 37 #include "classfile/symbolTable.hpp" 38 #include "classfile/systemDictionary.hpp" 39 #include "classfile/systemDictionaryShared.hpp" 40 #include "classfile/vmClasses.hpp" 41 #include "interpreter/bytecodeStream.hpp" 42 #include "interpreter/interpreterRuntime.hpp" 43 #include "memory/resourceArea.hpp" 44 #include "oops/constantPool.inline.hpp" 45 #include "oops/fieldStreams.inline.hpp" 46 #include "oops/instanceKlass.hpp" 47 #include "oops/klass.inline.hpp" 48 #include "runtime/handles.inline.hpp" 49 #include "runtime/javaCalls.hpp" 50 #include "runtime/signature.hpp" 51 52 // Returns true if we CAN PROVE that cp_index will always resolve to 53 // the same information at both dump time and run time. This is a 54 // necessary (but not sufficient) condition for pre-resolving cp_index 55 // during CDS archive assembly. 56 bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int cp_index) { 57 assert(!is_in_archivebuilder_buffer(cp), "sanity"); 58 59 if (cp->tag_at(cp_index).is_klass()) { 60 // We require cp_index to be already resolved. This is fine for now, are we 61 // currently archive only CP entries that are already resolved. 62 Klass* resolved_klass = cp->resolved_klass_at(cp_index); 63 return resolved_klass != nullptr && is_class_resolution_deterministic(cp->pool_holder(), resolved_klass); 64 } else if (cp->tag_at(cp_index).is_invoke_dynamic()) { 65 return is_indy_resolution_deterministic(cp, cp_index); 66 } else if (cp->tag_at(cp_index).is_field() || 67 cp->tag_at(cp_index).is_method() || 68 cp->tag_at(cp_index).is_interface_method()) { 69 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index); 70 if (!cp->tag_at(klass_cp_index).is_klass()) { 71 // Not yet resolved 72 return false; 73 } 74 Klass* k = cp->resolved_klass_at(klass_cp_index); 75 if (!is_class_resolution_deterministic(cp->pool_holder(), k)) { 76 return false; 77 } 78 79 if (!k->is_instance_klass()) { 80 // TODO: support non instance klasses as well. 81 return false; 82 } 83 84 // Here, We don't check if this entry can actually be resolved to a valid Field/Method. 85 // This method should be called by the ConstantPool to check Fields/Methods that 86 // have already been successfully resolved. 87 return true; 88 } else { 89 return false; 90 } 91 } 92 93 bool AOTConstantPoolResolver::is_class_resolution_deterministic(InstanceKlass* cp_holder, Klass* resolved_class) { 94 assert(!is_in_archivebuilder_buffer(cp_holder), "sanity"); 95 assert(!is_in_archivebuilder_buffer(resolved_class), "sanity"); 96 assert_at_safepoint(); // try_add_candidate() is called below and requires to be at safepoint. 97 98 if (resolved_class->is_instance_klass()) { 99 InstanceKlass* ik = InstanceKlass::cast(resolved_class); 100 101 if (!ik->in_aot_cache() && SystemDictionaryShared::is_excluded_class(ik)) { 102 return false; 103 } 104 105 if (cp_holder->is_subtype_of(ik)) { 106 // All super types of ik will be resolved in ik->class_loader() before 107 // ik is defined in this loader, so it's safe to archive the resolved klass reference. 108 return true; 109 } 110 111 if (CDSConfig::is_dumping_aot_linked_classes()) { 112 // Need to call try_add_candidate instead of is_candidate, as this may be called 113 // before AOTClassLinker::add_candidates(). 114 if (AOTClassLinker::try_add_candidate(ik)) { 115 return true; 116 } else { 117 return false; 118 } 119 } else if (AOTClassLinker::is_vm_class(ik)) { 120 if (ik->class_loader() != cp_holder->class_loader()) { 121 // At runtime, cp_holder() may not be able to resolve to the same 122 // ik. For example, a different version of ik may be defined in 123 // cp->pool_holder()'s loader using MethodHandles.Lookup.defineClass(). 124 return false; 125 } else { 126 return true; 127 } 128 } else { 129 return false; 130 } 131 } else if (resolved_class->is_objArray_klass()) { 132 if (CDSConfig::is_dumping_dynamic_archive()) { 133 // This is difficult to handle. See JDK-8374639 134 return false; 135 } 136 Klass* elem = ObjArrayKlass::cast(resolved_class)->bottom_klass(); 137 if (elem->is_instance_klass()) { 138 return is_class_resolution_deterministic(cp_holder, InstanceKlass::cast(elem)); 139 } else if (elem->is_typeArray_klass()) { 140 return true; 141 } else { 142 return false; 143 } 144 } else if (resolved_class->is_typeArray_klass()) { 145 return true; 146 } else { 147 return false; 148 } 149 } 150 151 void AOTConstantPoolResolver::preresolve_string_cp_entries(InstanceKlass* ik, TRAPS) { 152 if (!ik->is_linked()) { 153 // The cp->resolved_referenced() array is not ready yet, so we can't call resolve_string(). 154 return; 155 } 156 constantPoolHandle cp(THREAD, ik->constants()); 157 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused 158 switch (cp->tag_at(cp_index).value()) { 159 case JVM_CONSTANT_String: 160 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings. 161 break; 162 } 163 } 164 165 // Normally, we don't want to archive any CP entries that were not resolved 166 // in the training run. Otherwise the AOT/JIT may inline too much code that has not 167 // been executed. 168 // 169 // However, we want to aggressively resolve all klass/field/method constants for 170 // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes, 171 // so that the compiler can inline through them. 172 if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) { 173 bool eager_resolve = false; 174 175 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) { 176 eager_resolve = true; 177 } 178 if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) { 179 eager_resolve = true; 180 } 181 182 if (eager_resolve) { 183 preresolve_class_cp_entries(THREAD, ik, nullptr); 184 preresolve_field_and_method_cp_entries(THREAD, ik, nullptr); 185 } 186 } 187 } 188 189 // This works only for the boot/platform/app loaders 190 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) { 191 HandleMark hm(current); 192 Handle h_loader(current, class_loader); 193 Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader); 194 if (k != nullptr) { 195 return k; 196 } 197 if (h_loader() == SystemDictionary::java_system_loader()) { 198 return find_loaded_class(current, SystemDictionary::java_platform_loader(), name); 199 } else if (h_loader() == SystemDictionary::java_platform_loader()) { 200 return find_loaded_class(current, nullptr, name); 201 } else { 202 assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p", 203 cast_from_oop<address>(h_loader()), 204 cast_from_oop<address>(SystemDictionary::java_system_loader()), 205 cast_from_oop<address>(SystemDictionary::java_platform_loader())); 206 } 207 208 return nullptr; 209 } 210 211 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, ConstantPool* cp, int class_cp_index) { 212 Symbol* name = cp->klass_name_at(class_cp_index); 213 return find_loaded_class(current, cp->pool_holder()->class_loader(), name); 214 } 215 216 #if INCLUDE_CDS_JAVA_HEAP 217 void AOTConstantPoolResolver::resolve_string(constantPoolHandle cp, int cp_index, TRAPS) { 218 if (CDSConfig::is_dumping_heap()) { 219 int cache_index = cp->cp_to_object_index(cp_index); 220 ConstantPool::string_at_impl(cp, cp_index, cache_index, CHECK); 221 } 222 } 223 #endif 224 225 void AOTConstantPoolResolver::preresolve_class_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) { 226 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) { 227 return; 228 } 229 230 JavaThread* THREAD = current; 231 constantPoolHandle cp(THREAD, ik->constants()); 232 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { 233 if (cp->tag_at(cp_index).value() == JVM_CONSTANT_UnresolvedClass) { 234 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) { 235 // This class was not resolved during trial run. Don't attempt to resolve it. Otherwise 236 // the compiler may generate less efficient code. 237 continue; 238 } 239 if (find_loaded_class(current, cp(), cp_index) == nullptr) { 240 // Do not resolve any class that has not been loaded yet 241 continue; 242 } 243 Klass* resolved_klass = cp->klass_at(cp_index, THREAD); 244 if (HAS_PENDING_EXCEPTION) { 245 CLEAR_PENDING_EXCEPTION; // just ignore 246 } else { 247 log_trace(aot, resolve)("Resolved class [%3d] %s -> %s", cp_index, ik->external_name(), 248 resolved_klass->external_name()); 249 } 250 } 251 } 252 } 253 254 void AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) { 255 JavaThread* THREAD = current; 256 constantPoolHandle cp(THREAD, ik->constants()); 257 if (cp->cache() == nullptr) { 258 return; 259 } 260 for (int i = 0; i < ik->methods()->length(); i++) { 261 Method* m = ik->methods()->at(i); 262 BytecodeStream bcs(methodHandle(THREAD, m)); 263 while (!bcs.is_last_bytecode()) { 264 bcs.next(); 265 Bytecodes::Code raw_bc = bcs.raw_code(); 266 switch (raw_bc) { 267 case Bytecodes::_getstatic: 268 case Bytecodes::_putstatic: 269 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD); 270 if (HAS_PENDING_EXCEPTION) { 271 CLEAR_PENDING_EXCEPTION; // just ignore 272 } 273 break; 274 case Bytecodes::_getfield: 275 // no-fast bytecode 276 case Bytecodes::_nofast_getfield: 277 // fast bytecodes 278 case Bytecodes::_fast_agetfield: 279 case Bytecodes::_fast_bgetfield: 280 case Bytecodes::_fast_cgetfield: 281 case Bytecodes::_fast_dgetfield: 282 case Bytecodes::_fast_fgetfield: 283 case Bytecodes::_fast_igetfield: 284 case Bytecodes::_fast_lgetfield: 285 case Bytecodes::_fast_sgetfield: 286 raw_bc = Bytecodes::_getfield; 287 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD); 288 if (HAS_PENDING_EXCEPTION) { 289 CLEAR_PENDING_EXCEPTION; // just ignore 290 } 291 break; 292 293 case Bytecodes::_putfield: 294 // no-fast bytecode 295 case Bytecodes::_nofast_putfield: 296 // fast bytecodes 297 case Bytecodes::_fast_aputfield: 298 case Bytecodes::_fast_bputfield: 299 case Bytecodes::_fast_zputfield: 300 case Bytecodes::_fast_cputfield: 301 case Bytecodes::_fast_dputfield: 302 case Bytecodes::_fast_fputfield: 303 case Bytecodes::_fast_iputfield: 304 case Bytecodes::_fast_lputfield: 305 case Bytecodes::_fast_sputfield: 306 raw_bc = Bytecodes::_putfield; 307 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD); 308 if (HAS_PENDING_EXCEPTION) { 309 CLEAR_PENDING_EXCEPTION; // just ignore 310 } 311 break; 312 case Bytecodes::_invokehandle: 313 case Bytecodes::_invokespecial: 314 case Bytecodes::_invokevirtual: 315 case Bytecodes::_invokeinterface: 316 case Bytecodes::_invokestatic: 317 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD); 318 if (HAS_PENDING_EXCEPTION) { 319 CLEAR_PENDING_EXCEPTION; // just ignore 320 } 321 break; 322 default: 323 break; 324 } 325 } 326 } 327 } 328 329 void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index, 330 GrowableArray<bool>* preresolve_list, TRAPS) { 331 methodHandle mh(THREAD, m); 332 constantPoolHandle cp(THREAD, ik->constants()); 333 HandleMark hm(THREAD); 334 int cp_index = cp->to_cp_index(raw_index, bc); 335 336 if (cp->is_resolved(raw_index, bc)) { 337 return; 338 } 339 340 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) { 341 // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise 342 // the compiler may generate less efficient code. 343 return; 344 } 345 346 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index); 347 if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) { 348 // Do not resolve any field/methods from a class that has not been loaded yet. 349 return; 350 } 351 352 Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK); 353 const char* is_static = ""; 354 355 switch (bc) { 356 case Bytecodes::_getstatic: 357 case Bytecodes::_putstatic: 358 if (!VM_Version::supports_fast_class_init_checks()) { 359 return; // Do not resolve since interpreter lacks fast clinit barriers support 360 } 361 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, ClassInitMode::dont_init, CHECK); 362 is_static = " *** static"; 363 break; 364 365 case Bytecodes::_getfield: 366 case Bytecodes::_putfield: 367 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, ClassInitMode::dont_init, CHECK); 368 break; 369 370 case Bytecodes::_invokestatic: 371 if (!VM_Version::supports_fast_class_init_checks()) { 372 return; // Do not resolve since interpreter lacks fast clinit barriers support 373 } 374 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK); 375 is_static = " *** static"; 376 break; 377 378 case Bytecodes::_invokevirtual: 379 case Bytecodes::_invokespecial: 380 case Bytecodes::_invokeinterface: 381 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK); 382 break; 383 384 case Bytecodes::_invokehandle: 385 if (CDSConfig::is_dumping_method_handles()) { 386 ResolvedMethodEntry* method_entry = cp->resolved_method_entry_at(raw_index); 387 int cp_index = method_entry->constant_pool_index(); 388 Symbol* sig = cp->uncached_signature_ref_at(cp_index); 389 Klass* k; 390 if (check_methodtype_signature(cp(), sig, &k, true)) { 391 InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK); 392 } 393 } 394 break; 395 396 default: 397 ShouldNotReachHere(); 398 } 399 400 if (log_is_enabled(Trace, aot, resolve)) { 401 ResourceMark rm(THREAD); 402 bool resolved = cp->is_resolved(raw_index, bc); 403 Symbol* name = cp->name_ref_at(raw_index, bc); 404 Symbol* signature = cp->signature_ref_at(raw_index, bc); 405 log_trace(aot, resolve)("%s %s [%3d] %s -> %s.%s:%s%s", 406 (resolved ? "Resolved" : "Failed to resolve"), 407 Bytecodes::name(bc), cp_index, ik->external_name(), 408 resolved_klass->external_name(), 409 name->as_C_string(), signature->as_C_string(), is_static); 410 } 411 } 412 413 void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) { 414 JavaThread* THREAD = current; 415 constantPoolHandle cp(THREAD, ik->constants()); 416 if (!CDSConfig::is_dumping_invokedynamic() || cp->cache() == nullptr) { 417 return; 418 } 419 420 assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for " 421 "regenerated LambdaForm Invoker classes, which should not have indys anyway."); 422 423 Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries(); 424 for (int i = 0; i < indy_entries->length(); i++) { 425 ResolvedIndyEntry* rie = indy_entries->adr_at(i); 426 int cp_index = rie->constant_pool_index(); 427 if (preresolve_list->at(cp_index) == true) { 428 if (!rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) { 429 InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD); 430 if (HAS_PENDING_EXCEPTION) { 431 CLEAR_PENDING_EXCEPTION; // just ignore 432 } 433 } 434 if (log_is_enabled(Trace, aot, resolve)) { 435 ResourceMark rm(THREAD); 436 log_trace(aot, resolve)("%s indy [%3d] %s", 437 rie->is_resolved() ? "Resolved" : "Failed to resolve", 438 cp_index, ik->external_name()); 439 } 440 } 441 } 442 } 443 444 // Check the MethodType signatures used by parameters to the indy BSMs. Make sure we don't 445 // use types that have been excluded, or else we might end up creating MethodTypes that cannot be stored 446 // in the AOT cache. 447 bool AOTConstantPoolResolver::check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret, bool is_invokehandle) { 448 ResourceMark rm; 449 for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { 450 if (ss.is_reference()) { 451 Symbol* type = ss.as_symbol(); 452 Klass* k = find_loaded_class(Thread::current(), cp->pool_holder()->class_loader(), type); 453 if (k == nullptr) { 454 return false; 455 } 456 457 if (SystemDictionaryShared::should_be_excluded(k)) { 458 if (log_is_enabled(Warning, aot, resolve)) { 459 ResourceMark rm; 460 log_warning(aot, resolve)("Cannot aot-resolve %s because %s is excluded", 461 is_invokehandle ? "invokehandle" : "Lambda proxy", 462 k->external_name()); 463 } 464 return false; 465 } 466 467 // cp->pool_holder() must be able to resolve k in production run 468 precond(CDSConfig::is_dumping_aot_linked_classes()); 469 precond(SystemDictionaryShared::is_builtin_loader(cp->pool_holder()->class_loader_data())); 470 precond(SystemDictionaryShared::is_builtin_loader(k->class_loader_data())); 471 472 if (ss.at_return_type() && return_type_ret != nullptr) { 473 *return_type_ret = k; 474 } 475 } 476 } 477 return true; 478 } 479 480 bool AOTConstantPoolResolver::check_lambda_metafactory_signature(ConstantPool* cp, Symbol* sig) { 481 Klass* k; 482 if (!check_methodtype_signature(cp, sig, &k)) { 483 return false; 484 } 485 486 // <k> is the interface type implemented by the lambda proxy 487 if (!k->is_interface()) { 488 // cp->pool_holder() doesn't look like a valid class generated by javac 489 return false; 490 } 491 492 493 // The linked lambda callsite has an instance of the interface implemented by this lambda. If this 494 // interface requires its <clinit> to be executed, then we must delay the execution to the production run 495 // as <clinit> can have side effects ==> exclude such cases. 496 InstanceKlass* intf = InstanceKlass::cast(k); 497 bool exclude = intf->interface_needs_clinit_execution_as_super(); 498 if (log_is_enabled(Debug, aot, resolve)) { 499 ResourceMark rm; 500 log_debug(aot, resolve)("%s aot-resolve Lambda proxy of interface type %s", 501 exclude ? "Cannot" : "Can", k->external_name()); 502 } 503 return !exclude; 504 } 505 506 bool AOTConstantPoolResolver::check_lambda_metafactory_methodtype_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i) { 507 int mt_index = cp->bsm_attribute_entry(bsms_attribute_index)->argument(arg_i); 508 if (!cp->tag_at(mt_index).is_method_type()) { 509 // malformed class? 510 return false; 511 } 512 513 Symbol* sig = cp->method_type_signature_at(mt_index); 514 if (log_is_enabled(Debug, aot, resolve)) { 515 ResourceMark rm; 516 log_debug(aot, resolve)("Checking MethodType for LambdaMetafactory BSM arg %d: %s", arg_i, sig->as_C_string()); 517 } 518 519 return check_methodtype_signature(cp, sig); 520 } 521 522 bool AOTConstantPoolResolver::check_lambda_metafactory_methodhandle_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i) { 523 int mh_index = cp->bsm_attribute_entry(bsms_attribute_index)->argument(arg_i); 524 if (!cp->tag_at(mh_index).is_method_handle()) { 525 // malformed class? 526 return false; 527 } 528 529 // klass and sigature of the method (no need to check the method name) 530 Symbol* sig = cp->method_handle_signature_ref_at(mh_index); 531 Symbol* klass_name = cp->klass_name_at(cp->method_handle_klass_index_at(mh_index)); 532 533 if (log_is_enabled(Debug, aot, resolve)) { 534 ResourceMark rm; 535 log_debug(aot, resolve)("Checking MethodType of MethodHandle for LambdaMetafactory BSM arg %d: %s", arg_i, sig->as_C_string()); 536 } 537 538 { 539 Klass* k = find_loaded_class(Thread::current(), cp->pool_holder()->class_loader(), klass_name); 540 if (k == nullptr) { 541 // Dumping AOT cache: all classes should have been loaded by FinalImageRecipes::load_all_classes(). k must have 542 // been a class that was excluded when FinalImageRecipes recorded all classes at the end of the training run. 543 // 544 // Dumping static CDS archive: all classes in the classlist have already been loaded, before we resolve 545 // constants. k must have been a class that was excluded when the classlist was written 546 // at the end of the training run. 547 if (log_is_enabled(Warning, aot, resolve)) { 548 ResourceMark rm; 549 log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is not loaded", klass_name->as_C_string()); 550 } 551 return false; 552 } 553 if (SystemDictionaryShared::should_be_excluded(k)) { 554 if (log_is_enabled(Warning, aot, resolve)) { 555 ResourceMark rm; 556 log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is excluded", k->external_name()); 557 } 558 return false; 559 } 560 561 // cp->pool_holder() must be able to resolve k in production run 562 precond(CDSConfig::is_dumping_aot_linked_classes()); 563 precond(SystemDictionaryShared::is_builtin_loader(cp->pool_holder()->class_loader_data())); 564 precond(SystemDictionaryShared::is_builtin_loader(k->class_loader_data())); 565 } 566 567 return check_methodtype_signature(cp, sig); 568 } 569 570 bool AOTConstantPoolResolver::is_indy_resolution_deterministic(ConstantPool* cp, int cp_index) { 571 assert(cp->tag_at(cp_index).is_invoke_dynamic(), "sanity"); 572 if (!CDSConfig::is_dumping_invokedynamic()) { 573 return false; 574 } 575 576 InstanceKlass* pool_holder = cp->pool_holder(); 577 if (!SystemDictionaryShared::is_builtin(pool_holder)) { 578 return false; 579 } 580 581 int bsm = cp->bootstrap_method_ref_index_at(cp_index); 582 int bsm_ref = cp->method_handle_index_at(bsm); 583 Symbol* bsm_name = cp->uncached_name_ref_at(bsm_ref); 584 Symbol* bsm_signature = cp->uncached_signature_ref_at(bsm_ref); 585 Symbol* bsm_klass = cp->klass_name_at(cp->uncached_klass_ref_index_at(bsm_ref)); 586 587 // We currently support only StringConcatFactory::makeConcatWithConstants() and LambdaMetafactory::metafactory() 588 // We should mark the allowed BSMs in the JDK code using a private annotation. 589 // See notes on RFE JDK-8342481. 590 591 if (bsm_klass->equals("java/lang/invoke/StringConcatFactory") && 592 bsm_name->equals("makeConcatWithConstants") && 593 bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;" 594 "Ljava/lang/String;" 595 "Ljava/lang/invoke/MethodType;" 596 "Ljava/lang/String;" 597 "[Ljava/lang/Object;" 598 ")Ljava/lang/invoke/CallSite;")) { 599 Symbol* factory_type_sig = cp->uncached_signature_ref_at(cp_index); 600 if (log_is_enabled(Debug, aot, resolve)) { 601 ResourceMark rm; 602 log_debug(aot, resolve)("Checking StringConcatFactory callsite signature [%d]: %s", cp_index, factory_type_sig->as_C_string()); 603 } 604 605 Klass* k; 606 if (!check_methodtype_signature(cp, factory_type_sig, &k)) { 607 return false; 608 } 609 if (k != vmClasses::String_klass()) { 610 // bad class file? 611 return false; 612 } 613 614 return true; 615 } 616 617 if (bsm_klass->equals("java/lang/invoke/LambdaMetafactory") && 618 bsm_name->equals("metafactory") && 619 bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;" 620 "Ljava/lang/String;" 621 "Ljava/lang/invoke/MethodType;" 622 "Ljava/lang/invoke/MethodType;" 623 "Ljava/lang/invoke/MethodHandle;" 624 "Ljava/lang/invoke/MethodType;" 625 ")Ljava/lang/invoke/CallSite;")) { 626 /* 627 * An indy callsite is associated with the following MethodType and MethodHandles: 628 * 629 * https://github.com/openjdk/jdk/blob/580eb62dc097efeb51c76b095c1404106859b673/src/java.base/share/classes/java/lang/invoke/LambdaMetafactory.java#L293-L309 630 * 631 * MethodType factoryType The expected signature of the {@code CallSite}. The 632 * parameter types represent the types of capture variables; 633 * the return type is the interface to implement. When 634 * used with {@code invokedynamic}, this is provided by 635 * the {@code NameAndType} of the {@code InvokeDynamic} 636 * 637 * MethodType interfaceMethodType Signature and return type of method to be 638 * implemented by the function object. 639 * 640 * MethodHandle implementation A direct method handle describing the implementation 641 * method which should be called (with suitable adaptation 642 * of argument types and return types, and with captured 643 * arguments prepended to the invocation arguments) at 644 * invocation time. 645 * 646 * MethodType dynamicMethodType The signature and return type that should 647 * be enforced dynamically at invocation time. 648 * In simple use cases this is the same as 649 * {@code interfaceMethodType}. 650 */ 651 Symbol* factory_type_sig = cp->uncached_signature_ref_at(cp_index); 652 if (log_is_enabled(Debug, aot, resolve)) { 653 ResourceMark rm; 654 log_debug(aot, resolve)("Checking lambda callsite signature [%d]: %s", cp_index, factory_type_sig->as_C_string()); 655 } 656 657 if (!check_lambda_metafactory_signature(cp, factory_type_sig)) { 658 return false; 659 } 660 661 int bsms_attribute_index = cp->bootstrap_methods_attribute_index(cp_index); 662 int arg_count = cp->bsm_attribute_entry(bsms_attribute_index)->argument_count(); 663 if (arg_count != 3) { 664 // Malformed class? 665 return false; 666 } 667 668 // interfaceMethodType 669 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 0)) { 670 return false; 671 } 672 673 // implementation 674 if (!check_lambda_metafactory_methodhandle_arg(cp, bsms_attribute_index, 1)) { 675 return false; 676 } 677 678 // dynamicMethodType 679 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) { 680 return false; 681 } 682 683 return true; 684 } 685 686 return false; 687 } 688 #ifdef ASSERT 689 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) { 690 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) { 691 return false; 692 } else { 693 return ArchiveBuilder::current()->is_in_buffer_space(p); 694 } 695 } 696 #endif 697 698 // Paranoid check -- if any field/method signature in <ik> is referencing a class that is known to be 699 // excluded, do not archive any reflection data in <ik>. 700 bool AOTConstantPoolResolver::check_signature_for_reflection_data(InstanceKlass* ik, Symbol* sig, bool is_method) { 701 precond(SafepointSynchronize::is_at_safepoint()); 702 ResourceMark rm; 703 for (SignatureStream ss(sig, is_method); !ss.is_done(); ss.next()) { 704 if (ss.is_reference()) { 705 Symbol* klass_name = ss.as_symbol(/*probe_only=*/true); 706 if (klass_name == nullptr) { 707 // This symbol has never been created during the assembly phase, so it can't 708 // possibly be referencing an excluded class. 709 continue; 710 } 711 if (Signature::is_array(klass_name)) { 712 SignatureStream ss2(klass_name, false); 713 ss2.skip_array_prefix(); 714 if (ss2.is_reference() && ss2.as_symbol(/*probe_only=*/true) == nullptr) { 715 // klass_name is an array klass that has not been loaded during the assembly phase, so it can't 716 // possibly be referencing an excluded class. 717 continue; 718 } 719 } 720 Klass* k = find_loaded_class(Thread::current(), ik->class_loader(), klass_name); 721 if (k == nullptr) { 722 // No class of this name has been loaded for this loader, so this name can't 723 // possibly be referencing an excluded class. 724 continue; 725 } 726 if (SystemDictionaryShared::should_be_excluded(k)) { 727 if (log_is_enabled(Warning, aot, resolve)) { 728 ResourceMark rm; 729 log_warning(aot, resolve)("Cannot archive reflection data in %s because it refers to excluded class %s", 730 ik->external_name(), k->external_name()); 731 } 732 return false; 733 } 734 } 735 } 736 return true; 737 } 738 739 bool AOTConstantPoolResolver::can_archive_reflection_data_declared_fields(InstanceKlass* ik) { 740 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { 741 if (!check_signature_for_reflection_data(ik, fs.signature(), /*is_method=*/false)) { 742 return false; 743 } 744 } 745 746 return true; 747 } 748 749 bool AOTConstantPoolResolver::can_archive_reflection_data_declared_methods(InstanceKlass* ik) { 750 Array<Method*>* methods = ik->methods(); 751 for (int i = 0; i < methods->length(); i++) { 752 Method* m = methods->at(i); 753 if (!check_signature_for_reflection_data(ik, m->signature(), /*is_method=*/true)) { 754 return false; 755 } 756 } 757 758 return true; 759 } 760 761 // This is called by AOT assembly phase to see if java_lang_Class::reflection_data(k) can be 762 // archived. 763 bool AOTConstantPoolResolver::can_archive_reflection_data(InstanceKlass* ik) { 764 // At this point, we have resolved the reflection data of *all* classes 765 // recorded by FinalImageRecipes. 766 // When we are in the AOT safepoint, we will archive the reflection 767 // data of <ik> *only if* it doesn't reference any excluded classes. 768 precond(SafepointSynchronize::is_at_safepoint()); 769 770 if (!can_archive_reflection_data_declared_fields(ik)) { 771 return false; 772 } 773 if (!can_archive_reflection_data_declared_methods(ik)) { 774 return false; 775 } 776 777 if (log_is_enabled(Info, aot, resolve)) { 778 ResourceMark rm; 779 log_info(aot, resolve)("Archived reflection data in %s", ik->external_name()); 780 } 781 782 return true; 783 } 784 785 int AOTConstantPoolResolver::class_reflection_data_flags(InstanceKlass* ik, TRAPS) { 786 assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be"); 787 788 HandleMark hm(THREAD); 789 JavaCallArguments args(Handle(THREAD, ik->java_mirror())); 790 JavaValue result(T_INT); 791 JavaCalls::call_special(&result, 792 vmClasses::Class_klass(), 793 vmSymbols::encodeReflectionData_name(), 794 vmSymbols::void_int_signature(), 795 &args, CHECK_0); 796 int flags = result.get_jint(); 797 aot_log_info(aot)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags); 798 return flags; 799 } 800 801 void AOTConstantPoolResolver::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) { 802 aot_log_info(aot)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags); 803 JavaThread* THREAD = current; // for exception macros 804 JavaCallArguments args(Handle(THREAD, ik->java_mirror())); 805 args.push_int(rd_flags); 806 JavaValue result(T_OBJECT); 807 JavaCalls::call_special(&result, 808 vmClasses::Class_klass(), 809 vmSymbols::generateReflectionData_name(), 810 vmSymbols::int_void_signature(), 811 &args, THREAD); 812 if (HAS_PENDING_EXCEPTION) { 813 Handle exc_handle(THREAD, PENDING_EXCEPTION); 814 CLEAR_PENDING_EXCEPTION; 815 816 log_warning(aot)("Exception during Class::generateReflectionData() call for %s", ik->external_name()); 817 LogStreamHandle(Debug, aot) log; 818 if (log.is_enabled()) { 819 java_lang_Throwable::print_stack_trace(exc_handle, &log); 820 } 821 } 822 } 823 824 Klass* AOTConstantPoolResolver::resolve_boot_class_or_fail(const char* class_name, TRAPS) { 825 Handle class_loader; 826 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name); 827 return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, true, THREAD); 828 } 829 830 void AOTConstantPoolResolver::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) { 831 if (interfaces->length() < 1) { 832 return; 833 } 834 if (CDSConfig::is_dumping_preimage_static_archive()) { 835 FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags); 836 } 837 } 838 839 void AOTConstantPoolResolver::init_dynamic_proxy_cache(TRAPS) { 840 static bool inited = false; 841 if (inited) { 842 return; 843 } 844 inited = true; 845 846 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK); 847 TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS"); 848 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V"); 849 850 JavaCallArguments args; 851 args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader())); 852 args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader())); 853 JavaValue result(T_VOID); 854 JavaCalls::call_static(&result, 855 klass, 856 method, 857 signature, 858 &args, CHECK); 859 } 860 861 862 void AOTConstantPoolResolver::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) { 863 if (!CDSConfig::is_dumping_dynamic_proxies()) { 864 return; 865 } 866 init_dynamic_proxy_cache(CHECK); 867 868 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK); 869 TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS"); 870 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;"); 871 872 JavaCallArguments args; 873 args.push_oop(Handle(THREAD, loader())); 874 args.push_oop(Handle(THREAD, proxy_name())); 875 args.push_oop(Handle(THREAD, interfaces())); 876 args.push_int(access_flags); 877 JavaValue result(T_OBJECT); 878 JavaCalls::call_static(&result, 879 klass, 880 method, 881 signature, 882 &args, CHECK); 883 884 // Assumptions: 885 // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified. 886 // All named modules are loaded from the system modules files. 887 // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed. 888 // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived. 889 // The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which 890 // uses a null ProtectionDomain for this class) 891 oop mirror = result.get_oop(); 892 assert(mirror != nullptr, "class must have been generated if not OOM"); 893 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror)); 894 if (ik->defined_by_boot_loader() || ik->defined_by_platform_loader()) { 895 assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported"); 896 ik->set_shared_classpath_index(0); 897 } else { 898 assert(ik->defined_by_app_loader(), "must be"); 899 ik->set_shared_classpath_index(AOTClassLocationConfig::dumptime()->app_cp_start_index()); 900 } 901 902 ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class(); 903 if (log_is_enabled(Info, cds, dynamic, proxy)) { 904 ResourceMark rm(THREAD); 905 stringStream ss; 906 const char* prefix = ""; 907 ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(), 908 ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index()); 909 objArrayOop intfs = (objArrayOop)interfaces(); 910 for (int i = 0; i < intfs->length(); i++) { 911 oop intf_mirror = intfs->obj_at(i); 912 ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name()); 913 prefix = ", "; 914 } 915 916 log_info(cds, dynamic, proxy)("%s", ss.freeze()); 917 } 918 } --- EOF ---