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/aotConstantPoolResolver.hpp"
 27 #include "cds/archiveBuilder.hpp"
 28 #include "cds/cdsConfig.hpp"
 29 #include "classfile/systemDictionary.hpp"
 30 #include "classfile/systemDictionaryShared.hpp"
 31 #include "classfile/vmClasses.hpp"
 32 #include "interpreter/bytecodeStream.hpp"
 33 #include "interpreter/interpreterRuntime.hpp"
 34 #include "memory/resourceArea.hpp"
 35 #include "oops/constantPool.inline.hpp"
 36 #include "oops/instanceKlass.hpp"
 37 #include "oops/klass.inline.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 
 40 // Returns true if we CAN PROVE that cp_index will always resolve to
 41 // the same information at both dump time and run time. This is a
 42 // necessary (but not sufficient) condition for pre-resolving cp_index
 43 // during CDS archive assembly.
 44 bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int cp_index) {
 45   assert(!is_in_archivebuilder_buffer(cp), "sanity");
 46 
 47   if (cp->tag_at(cp_index).is_klass()) {
 48     // We require cp_index to be already resolved. This is fine for now, are we
 49     // currently archive only CP entries that are already resolved.
 50     Klass* resolved_klass = cp->resolved_klass_at(cp_index);
 51     return resolved_klass != nullptr && is_class_resolution_deterministic(cp->pool_holder(), resolved_klass);
 52   } else if (cp->tag_at(cp_index).is_invoke_dynamic()) {
 53     return is_indy_resolution_deterministic(cp, cp_index);
 54   } else if (cp->tag_at(cp_index).is_field() ||
 55              cp->tag_at(cp_index).is_method() ||
 56              cp->tag_at(cp_index).is_interface_method()) {
 57     int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
 58     if (!cp->tag_at(klass_cp_index).is_klass()) {
 59       // Not yet resolved
 60       return false;
 61     }
 62     Klass* k = cp->resolved_klass_at(klass_cp_index);
 63     if (!is_class_resolution_deterministic(cp->pool_holder(), k)) {
 64       return false;
 65     }
 66 
 67     if (!k->is_instance_klass()) {
 68       // TODO: support non instance klasses as well.
 69       return false;
 70     }
 71 
 72     // Here, We don't check if this entry can actually be resolved to a valid Field/Method.
 73     // This method should be called by the ConstantPool to check Fields/Methods that
 74     // have already been successfully resolved.
 75     return true;
 76   } else {
 77     return false;
 78   }
 79 }
 80 
 81 bool AOTConstantPoolResolver::is_class_resolution_deterministic(InstanceKlass* cp_holder, Klass* resolved_class) {
 82   assert(!is_in_archivebuilder_buffer(cp_holder), "sanity");
 83   assert(!is_in_archivebuilder_buffer(resolved_class), "sanity");
 84   assert_at_safepoint(); // try_add_candidate() is called below and requires to be at safepoint.
 85 
 86   if (resolved_class->is_instance_klass()) {
 87     InstanceKlass* ik = InstanceKlass::cast(resolved_class);
 88 
 89     if (!ik->in_aot_cache() && SystemDictionaryShared::is_excluded_class(ik)) {
 90       return false;
 91     }
 92 
 93     if (cp_holder->is_subtype_of(ik)) {
 94       // All super types of ik will be resolved in ik->class_loader() before
 95       // ik is defined in this loader, so it's safe to archive the resolved klass reference.
 96       return true;
 97     }
 98 
 99     if (CDSConfig::is_dumping_aot_linked_classes()) {
100       // Need to call try_add_candidate instead of is_candidate, as this may be called
101       // before AOTClassLinker::add_candidates().
102       if (AOTClassLinker::try_add_candidate(ik)) {
103         return true;
104       } else {
105         return false;
106       }
107     } else if (AOTClassLinker::is_vm_class(ik)) {
108       if (ik->class_loader() != cp_holder->class_loader()) {
109         // At runtime, cp_holder() may not be able to resolve to the same
110         // ik. For example, a different version of ik may be defined in
111         // cp->pool_holder()'s loader using MethodHandles.Lookup.defineClass().
112         return false;
113       } else {
114         return true;
115       }
116     } else {
117       return false;
118     }
119   } else if (resolved_class->is_objArray_klass()) {
120     if (CDSConfig::is_dumping_dynamic_archive()) {
121       // This is difficult to handle. See JDK-8374639
122       return false;
123     }
124     Klass* elem = ObjArrayKlass::cast(resolved_class)->bottom_klass();
125     if (elem->is_instance_klass()) {
126       return is_class_resolution_deterministic(cp_holder, InstanceKlass::cast(elem));
127     } else if (elem->is_typeArray_klass()) {
128       return true;
129     } else {
130       return false;
131     }
132   } else if (resolved_class->is_typeArray_klass()) {
133     return true;
134   } else {
135     return false;
136   }
137 }
138 
139 void AOTConstantPoolResolver::preresolve_string_cp_entries(InstanceKlass* ik, TRAPS) {
140   if (!ik->is_linked()) {
141     // The cp->resolved_referenced() array is not ready yet, so we can't call resolve_string().
142     return;
143   }
144   constantPoolHandle cp(THREAD, ik->constants());
145   for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
146     switch (cp->tag_at(cp_index).value()) {
147     case JVM_CONSTANT_String:
148       resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
149       break;
150     }
151   }
152 }
153 
154 // This works only for the boot/platform/app loaders
155 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
156   HandleMark hm(current);
157   Handle h_loader(current, class_loader);
158   Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader);
159   if (k != nullptr) {
160     return k;
161   }
162   if (h_loader() == SystemDictionary::java_system_loader()) {
163     return find_loaded_class(current, SystemDictionary::java_platform_loader(), name);
164   } else if (h_loader() == SystemDictionary::java_platform_loader()) {
165     return find_loaded_class(current, nullptr, name);
166   } else {
167     assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p",
168            cast_from_oop<address>(h_loader()),
169            cast_from_oop<address>(SystemDictionary::java_system_loader()),
170            cast_from_oop<address>(SystemDictionary::java_platform_loader()));
171   }
172 
173   return nullptr;
174 }
175 
176 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, ConstantPool* cp, int class_cp_index) {
177   Symbol* name = cp->klass_name_at(class_cp_index);
178   return find_loaded_class(current, cp->pool_holder()->class_loader(), name);
179 }
180 
181 #if INCLUDE_CDS_JAVA_HEAP
182 void AOTConstantPoolResolver::resolve_string(constantPoolHandle cp, int cp_index, TRAPS) {
183   if (CDSConfig::is_dumping_heap()) {
184     int cache_index = cp->cp_to_object_index(cp_index);
185     ConstantPool::string_at_impl(cp, cp_index, cache_index, CHECK);
186   }
187 }
188 #endif
189 
190 void AOTConstantPoolResolver::preresolve_class_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
191   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
192     return;
193   }
194 
195   JavaThread* THREAD = current;
196   constantPoolHandle cp(THREAD, ik->constants());
197   for (int cp_index = 1; cp_index < cp->length(); cp_index++) {
198     if (cp->tag_at(cp_index).value() == JVM_CONSTANT_UnresolvedClass) {
199       if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
200         // This class was not resolved during trial run. Don't attempt to resolve it. Otherwise
201         // the compiler may generate less efficient code.
202         continue;
203       }
204       if (find_loaded_class(current, cp(), cp_index) == nullptr) {
205         // Do not resolve any class that has not been loaded yet
206         continue;
207       }
208       Klass* resolved_klass = cp->klass_at(cp_index, THREAD);
209       if (HAS_PENDING_EXCEPTION) {
210         CLEAR_PENDING_EXCEPTION; // just ignore
211       } else {
212         log_trace(aot, resolve)("Resolved class  [%3d] %s -> %s", cp_index, ik->external_name(),
213                                 resolved_klass->external_name());
214       }
215     }
216   }
217 }
218 
219 void AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
220   JavaThread* THREAD = current;
221   constantPoolHandle cp(THREAD, ik->constants());
222   if (cp->cache() == nullptr) {
223     return;
224   }
225   for (int i = 0; i < ik->methods()->length(); i++) {
226     Method* m = ik->methods()->at(i);
227     BytecodeStream bcs(methodHandle(THREAD, m));
228     while (!bcs.is_last_bytecode()) {
229       bcs.next();
230       Bytecodes::Code raw_bc = bcs.raw_code();
231       switch (raw_bc) {
232       case Bytecodes::_getstatic:
233       case Bytecodes::_putstatic:
234         maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
235         if (HAS_PENDING_EXCEPTION) {
236           CLEAR_PENDING_EXCEPTION; // just ignore
237         }
238         break;
239       case Bytecodes::_getfield:
240       // no-fast bytecode
241       case Bytecodes::_nofast_getfield:
242       // fast bytecodes
243       case Bytecodes::_fast_agetfield:
244       case Bytecodes::_fast_bgetfield:
245       case Bytecodes::_fast_cgetfield:
246       case Bytecodes::_fast_dgetfield:
247       case Bytecodes::_fast_fgetfield:
248       case Bytecodes::_fast_igetfield:
249       case Bytecodes::_fast_lgetfield:
250       case Bytecodes::_fast_sgetfield:
251         raw_bc = Bytecodes::_getfield;
252         maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
253         if (HAS_PENDING_EXCEPTION) {
254           CLEAR_PENDING_EXCEPTION; // just ignore
255         }
256         break;
257 
258       case Bytecodes::_putfield:
259       // no-fast bytecode
260       case Bytecodes::_nofast_putfield:
261       // fast bytecodes
262       case Bytecodes::_fast_aputfield:
263       case Bytecodes::_fast_bputfield:
264       case Bytecodes::_fast_zputfield:
265       case Bytecodes::_fast_cputfield:
266       case Bytecodes::_fast_dputfield:
267       case Bytecodes::_fast_fputfield:
268       case Bytecodes::_fast_iputfield:
269       case Bytecodes::_fast_lputfield:
270       case Bytecodes::_fast_sputfield:
271         raw_bc = Bytecodes::_putfield;
272         maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
273         if (HAS_PENDING_EXCEPTION) {
274           CLEAR_PENDING_EXCEPTION; // just ignore
275         }
276         break;
277       case Bytecodes::_invokehandle:
278       case Bytecodes::_invokespecial:
279       case Bytecodes::_invokevirtual:
280       case Bytecodes::_invokeinterface:
281       case Bytecodes::_invokestatic:
282         maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
283         if (HAS_PENDING_EXCEPTION) {
284           CLEAR_PENDING_EXCEPTION; // just ignore
285         }
286         break;
287       default:
288         break;
289       }
290     }
291   }
292 }
293 
294 void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
295                                            GrowableArray<bool>* preresolve_list, TRAPS) {
296   methodHandle mh(THREAD, m);
297   constantPoolHandle cp(THREAD, ik->constants());
298   HandleMark hm(THREAD);
299   int cp_index = cp->to_cp_index(raw_index, bc);
300 
301   if (cp->is_resolved(raw_index, bc)) {
302     return;
303   }
304 
305   if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
306     // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise
307     // the compiler may generate less efficient code.
308     return;
309   }
310 
311   int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
312   if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) {
313     // Do not resolve any field/methods from a class that has not been loaded yet.
314     return;
315   }
316 
317   Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK);
318   const char* is_static = "";
319 
320   switch (bc) {
321   case Bytecodes::_getstatic:
322   case Bytecodes::_putstatic:
323     if (!VM_Version::supports_fast_class_init_checks()) {
324       return; // Do not resolve since interpreter lacks fast clinit barriers support
325     }
326     InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, ClassInitMode::dont_init, CHECK);
327     is_static = " *** static";
328     break;
329 
330   case Bytecodes::_getfield:
331   case Bytecodes::_putfield:
332     InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, ClassInitMode::dont_init, CHECK);
333     break;
334 
335   case Bytecodes::_invokestatic:
336     if (!VM_Version::supports_fast_class_init_checks()) {
337       return; // Do not resolve since interpreter lacks fast clinit barriers support
338     }
339     InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
340     is_static = " *** static";
341     break;
342 
343   case Bytecodes::_invokevirtual:
344   case Bytecodes::_invokespecial:
345   case Bytecodes::_invokeinterface:
346     InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
347     break;
348 
349   case Bytecodes::_invokehandle:
350     if (CDSConfig::is_dumping_method_handles()) {
351       ResolvedMethodEntry* method_entry = cp->resolved_method_entry_at(raw_index);
352       int cp_index = method_entry->constant_pool_index();
353       Symbol* sig = cp->uncached_signature_ref_at(cp_index);
354       Klass* k;
355       if (check_methodtype_signature(cp(), sig, &k, true)) {
356         InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK);
357       }
358     }
359     break;
360 
361   default:
362     ShouldNotReachHere();
363   }
364 
365   if (log_is_enabled(Trace, aot, resolve)) {
366     ResourceMark rm(THREAD);
367     bool resolved = cp->is_resolved(raw_index, bc);
368     Symbol* name = cp->name_ref_at(raw_index, bc);
369     Symbol* signature = cp->signature_ref_at(raw_index, bc);
370     log_trace(aot, resolve)("%s %s [%3d] %s -> %s.%s:%s%s",
371                             (resolved ? "Resolved" : "Failed to resolve"),
372                             Bytecodes::name(bc), cp_index, ik->external_name(),
373                             resolved_klass->external_name(),
374                             name->as_C_string(), signature->as_C_string(), is_static);
375   }
376 }
377 
378 void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
379   JavaThread* THREAD = current;
380   constantPoolHandle cp(THREAD, ik->constants());
381   if (!CDSConfig::is_dumping_invokedynamic() || cp->cache() == nullptr) {
382     return;
383   }
384 
385   assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for "
386          "regenerated LambdaForm Invoker classes, which should not have indys anyway.");
387 
388   Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries();
389   for (int i = 0; i < indy_entries->length(); i++) {
390     ResolvedIndyEntry* rie = indy_entries->adr_at(i);
391     int cp_index = rie->constant_pool_index();
392     if (preresolve_list->at(cp_index) == true) {
393       if (!rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) {
394         InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD);
395         if (HAS_PENDING_EXCEPTION) {
396           CLEAR_PENDING_EXCEPTION; // just ignore
397         }
398       }
399       if (log_is_enabled(Trace, aot, resolve)) {
400         ResourceMark rm(THREAD);
401         log_trace(aot, resolve)("%s indy   [%3d] %s",
402                                 rie->is_resolved() ? "Resolved" : "Failed to resolve",
403                                 cp_index, ik->external_name());
404       }
405     }
406   }
407 }
408 
409 // Check the MethodType signatures used by parameters to the indy BSMs. Make sure we don't
410 // use types that have been excluded, or else we might end up creating MethodTypes that cannot be stored
411 // in the AOT cache.
412 bool AOTConstantPoolResolver::check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret, bool is_invokehandle) {
413   ResourceMark rm;
414   for (SignatureStream ss(sig); !ss.is_done(); ss.next()) {
415     if (ss.is_reference()) {
416       Symbol* type = ss.as_symbol();
417       Klass* k = find_loaded_class(Thread::current(), cp->pool_holder()->class_loader(), type);
418       if (k == nullptr) {
419         return false;
420       }
421 
422       if (SystemDictionaryShared::should_be_excluded(k)) {
423         if (log_is_enabled(Warning, aot, resolve)) {
424           ResourceMark rm;
425           log_warning(aot, resolve)("Cannot aot-resolve %s because %s is excluded",
426                                     is_invokehandle ? "invokehandle" : "Lambda proxy",
427                                     k->external_name());
428         }
429         return false;
430       }
431 
432       // cp->pool_holder() must be able to resolve k in production run
433       precond(CDSConfig::is_dumping_aot_linked_classes());
434       precond(SystemDictionaryShared::is_builtin_loader(cp->pool_holder()->class_loader_data()));
435       precond(SystemDictionaryShared::is_builtin_loader(k->class_loader_data()));
436 
437       if (ss.at_return_type() && return_type_ret != nullptr) {
438         *return_type_ret = k;
439       }
440     }
441   }
442   return true;
443 }
444 
445 bool AOTConstantPoolResolver::check_lambda_metafactory_signature(ConstantPool* cp, Symbol* sig) {
446   Klass* k;
447   if (!check_methodtype_signature(cp, sig, &k)) {
448     return false;
449   }
450 
451   // <k> is the interface type implemented by the lambda proxy
452   if (!k->is_interface()) {
453     // cp->pool_holder() doesn't look like a valid class generated by javac
454     return false;
455   }
456 
457 
458   // The linked lambda callsite has an instance of the interface implemented by this lambda. If this
459   // interface requires its <clinit> to be executed, then we must delay the execution to the production run
460   // as <clinit> can have side effects ==> exclude such cases.
461   InstanceKlass* intf = InstanceKlass::cast(k);
462   bool exclude = intf->interface_needs_clinit_execution_as_super();
463   if (log_is_enabled(Debug, aot, resolve)) {
464     ResourceMark rm;
465     log_debug(aot, resolve)("%s aot-resolve Lambda proxy of interface type %s",
466                             exclude ? "Cannot" : "Can", k->external_name());
467   }
468   return !exclude;
469 }
470 
471 bool AOTConstantPoolResolver::check_lambda_metafactory_methodtype_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i) {
472   int mt_index = cp->bsm_attribute_entry(bsms_attribute_index)->argument(arg_i);
473   if (!cp->tag_at(mt_index).is_method_type()) {
474     // malformed class?
475     return false;
476   }
477 
478   Symbol* sig = cp->method_type_signature_at(mt_index);
479   if (log_is_enabled(Debug, aot, resolve)) {
480     ResourceMark rm;
481     log_debug(aot, resolve)("Checking MethodType for LambdaMetafactory BSM arg %d: %s", arg_i, sig->as_C_string());
482   }
483 
484   return check_methodtype_signature(cp, sig);
485 }
486 
487 bool AOTConstantPoolResolver::check_lambda_metafactory_methodhandle_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i) {
488   int mh_index = cp->bsm_attribute_entry(bsms_attribute_index)->argument(arg_i);
489   if (!cp->tag_at(mh_index).is_method_handle()) {
490     // malformed class?
491     return false;
492   }
493 
494   // klass and sigature of the method (no need to check the method name)
495   Symbol* sig = cp->method_handle_signature_ref_at(mh_index);
496   Symbol* klass_name = cp->klass_name_at(cp->method_handle_klass_index_at(mh_index));
497 
498   if (log_is_enabled(Debug, aot, resolve)) {
499     ResourceMark rm;
500     log_debug(aot, resolve)("Checking MethodType of MethodHandle for LambdaMetafactory BSM arg %d: %s", arg_i, sig->as_C_string());
501   }
502 
503   {
504     Klass* k = find_loaded_class(Thread::current(), cp->pool_holder()->class_loader(), klass_name);
505     if (k == nullptr) {
506       // Dumping AOT cache: all classes should have been loaded by FinalImageRecipes::load_all_classes(). k must have
507       // been a class that was excluded when FinalImageRecipes recorded all classes at the end of the training run.
508       //
509       // Dumping static CDS archive: all classes in the classlist have already been loaded, before we resolve
510       // constants. k must have been a class that was excluded when the classlist was written
511       // at the end of the training run.
512       if (log_is_enabled(Warning, aot, resolve)) {
513         ResourceMark rm;
514         log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is not loaded", klass_name->as_C_string());
515       }
516       return false;
517     }
518     if (SystemDictionaryShared::should_be_excluded(k)) {
519       if (log_is_enabled(Warning, aot, resolve)) {
520         ResourceMark rm;
521         log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is excluded", k->external_name());
522       }
523       return false;
524     }
525 
526     // cp->pool_holder() must be able to resolve k in production run
527     precond(CDSConfig::is_dumping_aot_linked_classes());
528     precond(SystemDictionaryShared::is_builtin_loader(cp->pool_holder()->class_loader_data()));
529     precond(SystemDictionaryShared::is_builtin_loader(k->class_loader_data()));
530   }
531 
532   return check_methodtype_signature(cp, sig);
533 }
534 
535 bool AOTConstantPoolResolver::is_indy_resolution_deterministic(ConstantPool* cp, int cp_index) {
536   assert(cp->tag_at(cp_index).is_invoke_dynamic(), "sanity");
537   if (!CDSConfig::is_dumping_invokedynamic()) {
538     return false;
539   }
540 
541   InstanceKlass* pool_holder = cp->pool_holder();
542   if (!SystemDictionaryShared::is_builtin(pool_holder)) {
543     return false;
544   }
545 
546   int bsm = cp->bootstrap_method_ref_index_at(cp_index);
547   int bsm_ref = cp->method_handle_index_at(bsm);
548   Symbol* bsm_name = cp->uncached_name_ref_at(bsm_ref);
549   Symbol* bsm_signature = cp->uncached_signature_ref_at(bsm_ref);
550   Symbol* bsm_klass = cp->klass_name_at(cp->uncached_klass_ref_index_at(bsm_ref));
551 
552   // We currently support only StringConcatFactory::makeConcatWithConstants() and LambdaMetafactory::metafactory()
553   // We should mark the allowed BSMs in the JDK code using a private annotation.
554   // See notes on RFE JDK-8342481.
555 
556   if (bsm_klass->equals("java/lang/invoke/StringConcatFactory") &&
557       bsm_name->equals("makeConcatWithConstants") &&
558       bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;"
559                              "Ljava/lang/String;"
560                              "Ljava/lang/invoke/MethodType;"
561                              "Ljava/lang/String;"
562                              "[Ljava/lang/Object;"
563                             ")Ljava/lang/invoke/CallSite;")) {
564     Symbol* factory_type_sig = cp->uncached_signature_ref_at(cp_index);
565     if (log_is_enabled(Debug, aot, resolve)) {
566       ResourceMark rm;
567       log_debug(aot, resolve)("Checking StringConcatFactory callsite signature [%d]: %s", cp_index, factory_type_sig->as_C_string());
568     }
569 
570     Klass* k;
571     if (!check_methodtype_signature(cp, factory_type_sig, &k)) {
572       return false;
573     }
574     if (k != vmClasses::String_klass()) {
575       // bad class file?
576       return false;
577     }
578 
579     return true;
580   }
581 
582   if (bsm_klass->equals("java/lang/invoke/LambdaMetafactory") &&
583       bsm_name->equals("metafactory") &&
584       bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;"
585                              "Ljava/lang/String;"
586                              "Ljava/lang/invoke/MethodType;"
587                              "Ljava/lang/invoke/MethodType;"
588                              "Ljava/lang/invoke/MethodHandle;"
589                              "Ljava/lang/invoke/MethodType;"
590                             ")Ljava/lang/invoke/CallSite;")) {
591     /*
592      * An indy callsite is associated with the following MethodType and MethodHandles:
593      *
594      * https://github.com/openjdk/jdk/blob/580eb62dc097efeb51c76b095c1404106859b673/src/java.base/share/classes/java/lang/invoke/LambdaMetafactory.java#L293-L309
595      *
596      * MethodType factoryType         The expected signature of the {@code CallSite}.  The
597      *                                parameter types represent the types of capture variables;
598      *                                the return type is the interface to implement.   When
599      *                                used with {@code invokedynamic}, this is provided by
600      *                                the {@code NameAndType} of the {@code InvokeDynamic}
601      *
602      * MethodType interfaceMethodType Signature and return type of method to be
603      *                                implemented by the function object.
604      *
605      * MethodHandle implementation    A direct method handle describing the implementation
606      *                                method which should be called (with suitable adaptation
607      *                                of argument types and return types, and with captured
608      *                                arguments prepended to the invocation arguments) at
609      *                                invocation time.
610      *
611      * MethodType dynamicMethodType   The signature and return type that should
612      *                                be enforced dynamically at invocation time.
613      *                                In simple use cases this is the same as
614      *                                {@code interfaceMethodType}.
615      */
616     Symbol* factory_type_sig = cp->uncached_signature_ref_at(cp_index);
617     if (log_is_enabled(Debug, aot, resolve)) {
618       ResourceMark rm;
619       log_debug(aot, resolve)("Checking lambda callsite signature [%d]: %s", cp_index, factory_type_sig->as_C_string());
620     }
621 
622     if (!check_lambda_metafactory_signature(cp, factory_type_sig)) {
623       return false;
624     }
625 
626     int bsms_attribute_index = cp->bootstrap_methods_attribute_index(cp_index);
627     int arg_count = cp->bsm_attribute_entry(bsms_attribute_index)->argument_count();
628     if (arg_count != 3) {
629       // Malformed class?
630       return false;
631     }
632 
633     // interfaceMethodType
634     if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 0)) {
635       return false;
636     }
637 
638     // implementation
639     if (!check_lambda_metafactory_methodhandle_arg(cp, bsms_attribute_index, 1)) {
640       return false;
641     }
642 
643     // dynamicMethodType
644     if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
645       return false;
646     }
647 
648     return true;
649   }
650 
651   return false;
652 }
653 #ifdef ASSERT
654 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
655   if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
656     return false;
657   } else {
658     return ArchiveBuilder::current()->is_in_buffer_space(p);
659   }
660 }
661 #endif