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