1 /* 2 * Copyright (c) 2022, 2025, 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/classListWriter.hpp" 33 #include "cds/finalImageRecipes.hpp" 34 #include "cds/heapShared.hpp" 35 #include "cds/lambdaFormInvokers.inline.hpp" 36 #include "classfile/classLoader.hpp" 37 #include "classfile/classLoaderExt.hpp" 38 #include "classfile/dictionary.hpp" 39 #include "classfile/symbolTable.hpp" 40 #include "classfile/systemDictionary.hpp" 41 #include "classfile/systemDictionaryShared.hpp" 42 #include "classfile/vmClasses.hpp" 43 #include "interpreter/bytecodeStream.hpp" 44 #include "interpreter/interpreterRuntime.hpp" 45 #include "memory/resourceArea.hpp" 46 #include "oops/constantPool.inline.hpp" 47 #include "oops/instanceKlass.hpp" 48 #include "oops/klass.inline.hpp" 49 #include "runtime/handles.inline.hpp" 50 #include "runtime/javaCalls.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 97 if (resolved_class->is_instance_klass()) { 98 InstanceKlass* ik = InstanceKlass::cast(resolved_class); 99 100 if (!ik->is_shared() && SystemDictionaryShared::is_excluded_class(ik)) { 101 return false; 102 } 103 104 if (cp_holder->is_subtype_of(ik)) { 105 // All super types of ik will be resolved in ik->class_loader() before 106 // ik is defined in this loader, so it's safe to archive the resolved klass reference. 107 return true; 108 } 109 110 if (CDSConfig::is_dumping_aot_linked_classes()) { 111 // Need to call try_add_candidate instead of is_candidate, as this may be called 112 // before AOTClassLinker::add_candidates(). 113 if (AOTClassLinker::try_add_candidate(ik)) { 114 return true; 115 } else { 116 return false; 117 } 118 } else if (AOTClassLinker::is_vm_class(ik)) { 119 if (ik->class_loader() != cp_holder->class_loader()) { 120 // At runtime, cp_holder() may not be able to resolve to the same 121 // ik. For example, a different version of ik may be defined in 122 // cp->pool_holder()'s loader using MethodHandles.Lookup.defineClass(). 123 return false; 124 } else { 125 return true; 126 } 127 } else { 128 return false; 129 } 130 } else if (resolved_class->is_objArray_klass()) { 131 Klass* elem = ObjArrayKlass::cast(resolved_class)->bottom_klass(); 132 if (elem->is_instance_klass()) { 133 return is_class_resolution_deterministic(cp_holder, InstanceKlass::cast(elem)); 134 } else if (elem->is_typeArray_klass()) { 135 return true; 136 } else { 137 return false; 138 } 139 } else if (resolved_class->is_typeArray_klass()) { 140 return true; 141 } else { 142 return false; 143 } 144 } 145 146 void AOTConstantPoolResolver::preresolve_string_cp_entries(InstanceKlass* ik, TRAPS) { 147 if (!ik->is_linked()) { 148 // The cp->resolved_referenced() array is not ready yet, so we can't call resolve_string(). 149 return; 150 } 151 constantPoolHandle cp(THREAD, ik->constants()); 152 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused 153 switch (cp->tag_at(cp_index).value()) { 154 case JVM_CONSTANT_String: 155 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings. 156 break; 157 } 158 } 159 160 // Normally, we don't want to archive any CP entries that were not resolved 161 // in the training run. Otherwise the AOT/JIT may inline too much code that has not 162 // been executed. 163 // 164 // However, we want to aggressively resolve all klass/field/method constants for 165 // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes, 166 // so that the compiler can inline through them. 167 if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) { 168 bool eager_resolve = false; 169 170 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) { 171 eager_resolve = true; 172 } 173 if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) { 174 eager_resolve = true; 175 } 176 177 if (eager_resolve) { 178 preresolve_class_cp_entries(THREAD, ik, nullptr); 179 preresolve_field_and_method_cp_entries(THREAD, ik, nullptr); 180 } 181 } 182 } 183 184 // This works only for the boot/platform/app loaders 185 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) { 186 HandleMark hm(current); 187 Handle h_loader(current, class_loader); 188 Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader); 189 if (k != nullptr) { 190 return k; 191 } 192 if (h_loader() == SystemDictionary::java_system_loader()) { 193 return find_loaded_class(current, SystemDictionary::java_platform_loader(), name); 194 } else if (h_loader() == SystemDictionary::java_platform_loader()) { 195 return find_loaded_class(current, nullptr, name); 196 } else { 197 assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p", 198 cast_from_oop<address>(h_loader()), 199 cast_from_oop<address>(SystemDictionary::java_system_loader()), 200 cast_from_oop<address>(SystemDictionary::java_platform_loader())); 201 } 202 203 return nullptr; 204 } 205 206 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, ConstantPool* cp, int class_cp_index) { 207 Symbol* name = cp->klass_name_at(class_cp_index); 208 return find_loaded_class(current, cp->pool_holder()->class_loader(), name); 209 } 210 211 #if INCLUDE_CDS_JAVA_HEAP 212 void AOTConstantPoolResolver::resolve_string(constantPoolHandle cp, int cp_index, TRAPS) { 213 if (CDSConfig::is_dumping_heap()) { 214 int cache_index = cp->cp_to_object_index(cp_index); 215 ConstantPool::string_at_impl(cp, cp_index, cache_index, CHECK); 216 } 217 } 218 #endif 219 220 void AOTConstantPoolResolver::preresolve_class_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) { 221 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) { 222 return; 223 } 224 225 JavaThread* THREAD = current; 226 constantPoolHandle cp(THREAD, ik->constants()); 227 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { 228 if (cp->tag_at(cp_index).value() == JVM_CONSTANT_UnresolvedClass) { 229 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) { 230 // This class was not resolved during trial run. Don't attempt to resolve it. Otherwise 231 // the compiler may generate less efficient code. 232 continue; 233 } 234 if (find_loaded_class(current, cp(), cp_index) == nullptr) { 235 // Do not resolve any class that has not been loaded yet 236 continue; 237 } 238 Klass* resolved_klass = cp->klass_at(cp_index, THREAD); 239 if (HAS_PENDING_EXCEPTION) { 240 CLEAR_PENDING_EXCEPTION; // just ignore 241 } else { 242 log_trace(aot, resolve)("Resolved class [%3d] %s -> %s", cp_index, ik->external_name(), 243 resolved_klass->external_name()); 244 } 245 } 246 } 247 } 248 249 void AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) { 250 JavaThread* THREAD = current; 251 constantPoolHandle cp(THREAD, ik->constants()); 252 if (cp->cache() == nullptr) { 253 return; 254 } 255 for (int i = 0; i < ik->methods()->length(); i++) { 256 Method* m = ik->methods()->at(i); 257 BytecodeStream bcs(methodHandle(THREAD, m)); 258 while (!bcs.is_last_bytecode()) { 259 bcs.next(); 260 Bytecodes::Code raw_bc = bcs.raw_code(); 261 switch (raw_bc) { 262 case Bytecodes::_getstatic: 263 case Bytecodes::_putstatic: 264 case Bytecodes::_getfield: 265 case Bytecodes::_putfield: 266 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD); 267 if (HAS_PENDING_EXCEPTION) { 268 CLEAR_PENDING_EXCEPTION; // just ignore 269 } 270 break; 271 case Bytecodes::_invokehandle: 272 case Bytecodes::_invokespecial: 273 case Bytecodes::_invokevirtual: 274 case Bytecodes::_invokeinterface: 275 case Bytecodes::_invokestatic: 276 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD); 277 if (HAS_PENDING_EXCEPTION) { 278 CLEAR_PENDING_EXCEPTION; // just ignore 279 } 280 break; 281 default: 282 break; 283 } 284 } 285 } 286 } 287 288 void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index, 289 GrowableArray<bool>* preresolve_list, TRAPS) { 290 methodHandle mh(THREAD, m); 291 constantPoolHandle cp(THREAD, ik->constants()); 292 HandleMark hm(THREAD); 293 int cp_index = cp->to_cp_index(raw_index, bc); 294 295 if (cp->is_resolved(raw_index, bc)) { 296 return; 297 } 298 299 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) { 300 // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise 301 // the compiler may generate less efficient code. 302 return; 303 } 304 305 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index); 306 if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) { 307 // Do not resolve any field/methods from a class that has not been loaded yet. 308 return; 309 } 310 311 Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK); 312 const char* is_static = ""; 313 314 switch (bc) { 315 case Bytecodes::_getstatic: 316 case Bytecodes::_putstatic: 317 if (!VM_Version::supports_fast_class_init_checks()) { 318 return; // Do not resolve since interpreter lacks fast clinit barriers support 319 } 320 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK); 321 is_static = " *** static"; 322 break; 323 case Bytecodes::_getfield: 324 case Bytecodes::_putfield: 325 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK); 326 break; 327 328 case Bytecodes::_invokestatic: 329 if (!VM_Version::supports_fast_class_init_checks()) { 330 return; // Do not resolve since interpreter lacks fast clinit barriers support 331 } 332 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK); 333 is_static = " *** static"; 334 break; 335 336 case Bytecodes::_invokevirtual: 337 case Bytecodes::_invokespecial: 338 case Bytecodes::_invokeinterface: 339 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK); 340 break; 341 342 case Bytecodes::_invokehandle: 343 InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK); 344 break; 345 346 default: 347 ShouldNotReachHere(); 348 } 349 350 if (log_is_enabled(Trace, aot, resolve)) { 351 ResourceMark rm(THREAD); 352 bool resolved = cp->is_resolved(raw_index, bc); 353 Symbol* name = cp->name_ref_at(raw_index, bc); 354 Symbol* signature = cp->signature_ref_at(raw_index, bc); 355 log_trace(aot, resolve)("%s %s [%3d] %s -> %s.%s:%s%s", 356 (resolved ? "Resolved" : "Failed to resolve"), 357 Bytecodes::name(bc), cp_index, ik->external_name(), 358 resolved_klass->external_name(), 359 name->as_C_string(), signature->as_C_string(), is_static); 360 } 361 } 362 363 void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) { 364 JavaThread* THREAD = current; 365 constantPoolHandle cp(THREAD, ik->constants()); 366 if (!CDSConfig::is_dumping_invokedynamic() || cp->cache() == nullptr) { 367 return; 368 } 369 370 assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for " 371 "regenerated LambdaForm Invoker classes, which should not have indys anyway."); 372 373 Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries(); 374 for (int i = 0; i < indy_entries->length(); i++) { 375 ResolvedIndyEntry* rie = indy_entries->adr_at(i); 376 int cp_index = rie->constant_pool_index(); 377 if (preresolve_list->at(cp_index) == true) { 378 if (!rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) { 379 InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD); 380 if (HAS_PENDING_EXCEPTION) { 381 CLEAR_PENDING_EXCEPTION; // just ignore 382 } 383 } 384 if (log_is_enabled(Trace, aot, resolve)) { 385 ResourceMark rm(THREAD); 386 log_trace(aot, resolve)("%s indy [%3d] %s", 387 rie->is_resolved() ? "Resolved" : "Failed to resolve", 388 cp_index, ik->external_name()); 389 } 390 } 391 } 392 } 393 394 // Check the MethodType signatures used by parameters to the indy BSMs. Make sure we don't 395 // use types that have been excluded, or else we might end up creating MethodTypes that cannot be stored 396 // in the AOT cache. 397 bool AOTConstantPoolResolver::check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret) { 398 ResourceMark rm; 399 for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { 400 if (ss.is_reference()) { 401 Symbol* type = ss.as_symbol(); 402 Klass* k = find_loaded_class(Thread::current(), cp->pool_holder()->class_loader(), type); 403 if (k == nullptr) { 404 return false; 405 } 406 407 if (SystemDictionaryShared::should_be_excluded(k)) { 408 if (log_is_enabled(Warning, aot, resolve)) { 409 ResourceMark rm; 410 log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is excluded", k->external_name()); 411 } 412 return false; 413 } 414 415 if (ss.at_return_type() && return_type_ret != nullptr) { 416 *return_type_ret = k; 417 } 418 } 419 } 420 return true; 421 } 422 423 bool AOTConstantPoolResolver::check_lambda_metafactory_signature(ConstantPool* cp, Symbol* sig) { 424 Klass* k; 425 if (!check_methodtype_signature(cp, sig, &k)) { 426 return false; 427 } 428 429 // <k> is the interface type implemented by the lambda proxy 430 if (!k->is_interface()) { 431 // cp->pool_holder() doesn't look like a valid class generated by javac 432 return false; 433 } 434 435 436 // The linked lambda callsite has an instance of the interface implemented by this lambda. If this 437 // interface requires its <clinit> to be executed, then we must delay the execution to the production run 438 // as <clinit> can have side effects ==> exclude such cases. 439 InstanceKlass* intf = InstanceKlass::cast(k); 440 bool exclude = intf->interface_needs_clinit_execution_as_super(); 441 if (log_is_enabled(Debug, aot, resolve)) { 442 ResourceMark rm; 443 log_debug(aot, resolve)("%s aot-resolve Lambda proxy of interface type %s", 444 exclude ? "Cannot" : "Can", k->external_name()); 445 } 446 return !exclude; 447 } 448 449 bool AOTConstantPoolResolver::check_lambda_metafactory_methodtype_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i) { 450 int mt_index = cp->operand_argument_index_at(bsms_attribute_index, arg_i); 451 if (!cp->tag_at(mt_index).is_method_type()) { 452 // malformed class? 453 return false; 454 } 455 456 Symbol* sig = cp->method_type_signature_at(mt_index); 457 if (log_is_enabled(Debug, aot, resolve)) { 458 ResourceMark rm; 459 log_debug(aot, resolve)("Checking MethodType for LambdaMetafactory BSM arg %d: %s", arg_i, sig->as_C_string()); 460 } 461 462 return check_methodtype_signature(cp, sig); 463 } 464 465 bool AOTConstantPoolResolver::check_lambda_metafactory_methodhandle_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i) { 466 int mh_index = cp->operand_argument_index_at(bsms_attribute_index, arg_i); 467 if (!cp->tag_at(mh_index).is_method_handle()) { 468 // malformed class? 469 return false; 470 } 471 472 Symbol* sig = cp->method_handle_signature_ref_at(mh_index); 473 if (log_is_enabled(Debug, aot, resolve)) { 474 ResourceMark rm; 475 log_debug(aot, resolve)("Checking MethodType of MethodHandle for LambdaMetafactory BSM arg %d: %s", arg_i, sig->as_C_string()); 476 } 477 return check_methodtype_signature(cp, sig); 478 } 479 480 bool AOTConstantPoolResolver::is_indy_resolution_deterministic(ConstantPool* cp, int cp_index) { 481 assert(cp->tag_at(cp_index).is_invoke_dynamic(), "sanity"); 482 if (!CDSConfig::is_dumping_invokedynamic()) { 483 return false; 484 } 485 486 InstanceKlass* pool_holder = cp->pool_holder(); 487 if (!SystemDictionaryShared::is_builtin(pool_holder)) { 488 return false; 489 } 490 491 int bsm = cp->bootstrap_method_ref_index_at(cp_index); 492 int bsm_ref = cp->method_handle_index_at(bsm); 493 Symbol* bsm_name = cp->uncached_name_ref_at(bsm_ref); 494 Symbol* bsm_signature = cp->uncached_signature_ref_at(bsm_ref); 495 Symbol* bsm_klass = cp->klass_name_at(cp->uncached_klass_ref_index_at(bsm_ref)); 496 497 // We currently support only StringConcatFactory::makeConcatWithConstants() and LambdaMetafactory::metafactory() 498 // We should mark the allowed BSMs in the JDK code using a private annotation. 499 // See notes on RFE JDK-8342481. 500 501 if (bsm_klass->equals("java/lang/invoke/StringConcatFactory") && 502 bsm_name->equals("makeConcatWithConstants") && 503 bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;" 504 "Ljava/lang/String;" 505 "Ljava/lang/invoke/MethodType;" 506 "Ljava/lang/String;" 507 "[Ljava/lang/Object;" 508 ")Ljava/lang/invoke/CallSite;")) { 509 Symbol* factory_type_sig = cp->uncached_signature_ref_at(cp_index); 510 if (log_is_enabled(Debug, aot, resolve)) { 511 ResourceMark rm; 512 log_debug(aot, resolve)("Checking StringConcatFactory callsite signature [%d]: %s", cp_index, factory_type_sig->as_C_string()); 513 } 514 515 Klass* k; 516 if (!check_methodtype_signature(cp, factory_type_sig, &k)) { 517 return false; 518 } 519 if (k != vmClasses::String_klass()) { 520 // bad class file? 521 return false; 522 } 523 524 return true; 525 } 526 527 if (bsm_klass->equals("java/lang/invoke/LambdaMetafactory") && 528 bsm_name->equals("metafactory") && 529 bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;" 530 "Ljava/lang/String;" 531 "Ljava/lang/invoke/MethodType;" 532 "Ljava/lang/invoke/MethodType;" 533 "Ljava/lang/invoke/MethodHandle;" 534 "Ljava/lang/invoke/MethodType;" 535 ")Ljava/lang/invoke/CallSite;")) { 536 /* 537 * An indy callsite is associated with the following MethodType and MethodHandles: 538 * 539 * https://github.com/openjdk/jdk/blob/580eb62dc097efeb51c76b095c1404106859b673/src/java.base/share/classes/java/lang/invoke/LambdaMetafactory.java#L293-L309 540 * 541 * MethodType factoryType The expected signature of the {@code CallSite}. The 542 * parameter types represent the types of capture variables; 543 * the return type is the interface to implement. When 544 * used with {@code invokedynamic}, this is provided by 545 * the {@code NameAndType} of the {@code InvokeDynamic} 546 * 547 * MethodType interfaceMethodType Signature and return type of method to be 548 * implemented by the function object. 549 * 550 * MethodHandle implementation A direct method handle describing the implementation 551 * method which should be called (with suitable adaptation 552 * of argument types and return types, and with captured 553 * arguments prepended to the invocation arguments) at 554 * invocation time. 555 * 556 * MethodType dynamicMethodType The signature and return type that should 557 * be enforced dynamically at invocation time. 558 * In simple use cases this is the same as 559 * {@code interfaceMethodType}. 560 */ 561 Symbol* factory_type_sig = cp->uncached_signature_ref_at(cp_index); 562 if (log_is_enabled(Debug, aot, resolve)) { 563 ResourceMark rm; 564 log_debug(aot, resolve)("Checking lambda callsite signature [%d]: %s", cp_index, factory_type_sig->as_C_string()); 565 } 566 567 if (!check_lambda_metafactory_signature(cp, factory_type_sig)) { 568 return false; 569 } 570 571 int bsms_attribute_index = cp->bootstrap_methods_attribute_index(cp_index); 572 int arg_count = cp->operand_argument_count_at(bsms_attribute_index); 573 if (arg_count != 3) { 574 // Malformed class? 575 return false; 576 } 577 578 // interfaceMethodType 579 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 0)) { 580 return false; 581 } 582 583 // implementation 584 if (!check_lambda_metafactory_methodhandle_arg(cp, bsms_attribute_index, 1)) { 585 return false; 586 } 587 588 // dynamicMethodType 589 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) { 590 return false; 591 } 592 593 return true; 594 } 595 596 return false; 597 } 598 #ifdef ASSERT 599 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) { 600 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) { 601 return false; 602 } else { 603 return ArchiveBuilder::current()->is_in_buffer_space(p); 604 } 605 } 606 #endif 607 608 int AOTConstantPoolResolver::class_reflection_data_flags(InstanceKlass* ik, TRAPS) { 609 assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be"); 610 611 HandleMark hm(THREAD); 612 JavaCallArguments args(Handle(THREAD, ik->java_mirror())); 613 JavaValue result(T_INT); 614 JavaCalls::call_special(&result, 615 vmClasses::Class_klass(), 616 vmSymbols::encodeReflectionData_name(), 617 vmSymbols::void_int_signature(), 618 &args, CHECK_0); 619 int flags = result.get_jint(); 620 aot_log_info(aot)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags); 621 return flags; 622 } 623 624 void AOTConstantPoolResolver::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) { 625 aot_log_info(aot)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags); 626 JavaThread* THREAD = current; // for exception macros 627 JavaCallArguments args(Handle(THREAD, ik->java_mirror())); 628 args.push_int(rd_flags); 629 JavaValue result(T_OBJECT); 630 JavaCalls::call_special(&result, 631 vmClasses::Class_klass(), 632 vmSymbols::generateReflectionData_name(), 633 vmSymbols::int_void_signature(), 634 &args, THREAD); 635 if (HAS_PENDING_EXCEPTION) { 636 Handle exc_handle(THREAD, PENDING_EXCEPTION); 637 CLEAR_PENDING_EXCEPTION; 638 639 log_warning(aot)("Exception during Class::generateReflectionData() call for %s", ik->external_name()); 640 LogStreamHandle(Debug, aot) log; 641 if (log.is_enabled()) { 642 java_lang_Throwable::print_stack_trace(exc_handle, &log); 643 } 644 } 645 } 646 647 Klass* AOTConstantPoolResolver::resolve_boot_class_or_fail(const char* class_name, TRAPS) { 648 Handle class_loader; 649 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name); 650 return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, true, THREAD); 651 } 652 653 void AOTConstantPoolResolver::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) { 654 if (interfaces->length() < 1) { 655 return; 656 } 657 if (ClassListWriter::is_enabled()) { 658 const char* loader_name = ArchiveUtils::builtin_loader_name_or_null(loader); 659 if (loader_name != nullptr) { 660 stringStream ss; 661 ss.print("%s %s %d %d", loader_name, proxy_name, access_flags, interfaces->length()); 662 for (int i = 0; i < interfaces->length(); i++) { 663 oop mirror = interfaces->obj_at(i); 664 Klass* k = java_lang_Class::as_Klass(mirror); 665 ss.print(" %s", k->name()->as_C_string()); 666 } 667 ClassListWriter w; // This locks ClassListFile_lock 668 w.stream()->print_cr("@dynamic-proxy %s", ss.freeze()); 669 } 670 } 671 if (CDSConfig::is_dumping_preimage_static_archive()) { 672 FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags); 673 } 674 } 675 676 void AOTConstantPoolResolver::init_dynamic_proxy_cache(TRAPS) { 677 static bool inited = false; 678 if (inited) { 679 return; 680 } 681 inited = true; 682 683 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK); 684 TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS"); 685 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V"); 686 687 JavaCallArguments args; 688 args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader())); 689 args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader())); 690 JavaValue result(T_VOID); 691 JavaCalls::call_static(&result, 692 klass, 693 method, 694 signature, 695 &args, CHECK); 696 } 697 698 699 void AOTConstantPoolResolver::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) { 700 if (!CDSConfig::is_dumping_dynamic_proxies()) { 701 return; 702 } 703 init_dynamic_proxy_cache(CHECK); 704 705 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK); 706 TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS"); 707 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;"); 708 709 JavaCallArguments args; 710 args.push_oop(Handle(THREAD, loader())); 711 args.push_oop(Handle(THREAD, proxy_name())); 712 args.push_oop(Handle(THREAD, interfaces())); 713 args.push_int(access_flags); 714 JavaValue result(T_OBJECT); 715 JavaCalls::call_static(&result, 716 klass, 717 method, 718 signature, 719 &args, CHECK); 720 721 // Assumptions: 722 // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified. 723 // All named modules are loaded from the system modules files. 724 // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed. 725 // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived. 726 // The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which 727 // uses a null ProtectionDomain for this class) 728 oop mirror = result.get_oop(); 729 assert(mirror != nullptr, "class must have been generated if not OOM"); 730 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror)); 731 if (ik->defined_by_boot_loader() || ik->defined_by_platform_loader()) { 732 assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported"); 733 ik->set_shared_classpath_index(0); 734 } else { 735 assert(ik->defined_by_app_loader(), "must be"); 736 ik->set_shared_classpath_index(AOTClassLocationConfig::dumptime()->app_cp_start_index()); 737 } 738 739 ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class(); 740 if (log_is_enabled(Info, cds, dynamic, proxy)) { 741 ResourceMark rm(THREAD); 742 stringStream ss; 743 const char* prefix = ""; 744 ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(), 745 ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index()); 746 objArrayOop intfs = (objArrayOop)interfaces(); 747 for (int i = 0; i < intfs->length(); i++) { 748 oop intf_mirror = intfs->obj_at(i); 749 ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name()); 750 prefix = ", "; 751 } 752 753 log_info(cds, dynamic, proxy)("%s", ss.freeze()); 754 } 755 }