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