< prev index next >

src/hotspot/share/cds/aotConstantPoolResolver.cpp

Print this page

  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()) {

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   }

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::_getfield:
228       case Bytecodes::_putfield:
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::_invokehandle:
235       case Bytecodes::_invokespecial:
236       case Bytecodes::_invokevirtual:
237       case Bytecodes::_invokeinterface:

238         maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
239         if (HAS_PENDING_EXCEPTION) {
240           CLEAR_PENDING_EXCEPTION; // just ignore
241         }
242         break;
243       default:
244         break;
245       }
246     }
247   }
248 }
249 
250 void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
251                                            GrowableArray<bool>* preresolve_list, TRAPS) {
252   methodHandle mh(THREAD, m);
253   constantPoolHandle cp(THREAD, ik->constants());
254   HandleMark hm(THREAD);
255   int cp_index = cp->to_cp_index(raw_index, bc);
256 
257   if (cp->is_resolved(raw_index, bc)) {
258     return;
259   }
260 
261   if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
262     // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise
263     // the compiler may generate less efficient code.
264     return;
265   }
266 
267   int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
268   if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) {
269     // Do not resolve any field/methods from a class that has not been loaded yet.
270     return;
271   }
272 
273   Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK);

274 
275   switch (bc) {








276   case Bytecodes::_getfield:
277   case Bytecodes::_putfield:
278     InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
279     break;
280 








281   case Bytecodes::_invokevirtual:
282   case Bytecodes::_invokespecial:
283   case Bytecodes::_invokeinterface:
284     InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
285     break;
286 
287   case Bytecodes::_invokehandle:
288     InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK);
289     break;
290 
291   default:
292     ShouldNotReachHere();
293   }
294 
295   if (log_is_enabled(Trace, aot, resolve)) {
296     ResourceMark rm(THREAD);
297     bool resolved = cp->is_resolved(raw_index, bc);
298     Symbol* name = cp->name_ref_at(raw_index, bc);
299     Symbol* signature = cp->signature_ref_at(raw_index, bc);
300     log_trace(aot, resolve)("%s %s [%3d] %s -> %s.%s:%s",
301                             (resolved ? "Resolved" : "Failed to resolve"),
302                             Bytecodes::name(bc), cp_index, ik->external_name(),
303                             resolved_klass->external_name(),
304                             name->as_C_string(), signature->as_C_string());
305   }
306 }
307 
308 void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
309   JavaThread* THREAD = current;
310   constantPoolHandle cp(THREAD, ik->constants());
311   if (!CDSConfig::is_dumping_invokedynamic() || cp->cache() == nullptr) {
312     return;
313   }
314 
315   assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for "
316          "regenerated LambdaForm Invoker classes, which should not have indys anyway.");
317 
318   Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries();
319   for (int i = 0; i < indy_entries->length(); i++) {
320     ResolvedIndyEntry* rie = indy_entries->adr_at(i);
321     int cp_index = rie->constant_pool_index();
322     if (preresolve_list->at(cp_index) == true) {
323       if (!rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) {
324         InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD);

532 
533     // dynamicMethodType
534     if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
535       return false;
536     }
537 
538     return true;
539   }
540 
541   return false;
542 }
543 #ifdef ASSERT
544 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
545   if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
546     return false;
547   } else {
548     return ArchiveBuilder::current()->is_in_buffer_space(p);
549   }
550 }
551 #endif






















































































































































  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/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 // Returns true if we CAN PROVE that cp_index will always resolve to
 52 // the same information at both dump time and run time. This is a
 53 // necessary (but not sufficient) condition for pre-resolving cp_index
 54 // during CDS archive assembly.
 55 bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int cp_index) {
 56   assert(!is_in_archivebuilder_buffer(cp), "sanity");
 57 
 58   if (cp->tag_at(cp_index).is_klass()) {
 59     // We require cp_index to be already resolved. This is fine for now, are we
 60     // currently archive only CP entries that are already resolved.
 61     Klass* resolved_klass = cp->resolved_klass_at(cp_index);
 62     return resolved_klass != nullptr && is_class_resolution_deterministic(cp->pool_holder(), resolved_klass);
 63   } else if (cp->tag_at(cp_index).is_invoke_dynamic()) {
 64     return is_indy_resolution_deterministic(cp, cp_index);
 65   } else if (cp->tag_at(cp_index).is_field() ||
 66              cp->tag_at(cp_index).is_method() ||
 67              cp->tag_at(cp_index).is_interface_method()) {
 68     int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
 69     if (!cp->tag_at(klass_cp_index).is_klass()) {

138   } else if (resolved_class->is_typeArray_klass()) {
139     return true;
140   } else {
141     return false;
142   }
143 }
144 
145 void AOTConstantPoolResolver::preresolve_string_cp_entries(InstanceKlass* ik, TRAPS) {
146   if (!ik->is_linked()) {
147     // The cp->resolved_referenced() array is not ready yet, so we can't call resolve_string().
148     return;
149   }
150   constantPoolHandle cp(THREAD, ik->constants());
151   for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
152     switch (cp->tag_at(cp_index).value()) {
153     case JVM_CONSTANT_String:
154       resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
155       break;
156     }
157   }
158 
159   // Normally, we don't want to archive any CP entries that were not resolved
160   // in the training run. Otherwise the AOT/JIT may inline too much code that has not
161   // been executed.
162   //
163   // However, we want to aggressively resolve all klass/field/method constants for
164   // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes,
165   // so that the compiler can inline through them.
166   if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
167     bool eager_resolve = false;
168 
169     if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
170       eager_resolve = true;
171     }
172     if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) {
173       eager_resolve = true;
174     }
175 
176     if (eager_resolve) {
177       preresolve_class_cp_entries(THREAD, ik, nullptr);
178       preresolve_field_and_method_cp_entries(THREAD, ik, nullptr);
179     }
180   }
181 }
182 
183 // This works only for the boot/platform/app loaders
184 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
185   HandleMark hm(current);
186   Handle h_loader(current, class_loader);
187   Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader);
188   if (k != nullptr) {
189     return k;
190   }
191   if (h_loader() == SystemDictionary::java_system_loader()) {
192     return find_loaded_class(current, SystemDictionary::java_platform_loader(), name);
193   } else if (h_loader() == SystemDictionary::java_platform_loader()) {
194     return find_loaded_class(current, nullptr, name);
195   } else {
196     assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p",
197            cast_from_oop<address>(h_loader()),
198            cast_from_oop<address>(SystemDictionary::java_system_loader()),
199            cast_from_oop<address>(SystemDictionary::java_platform_loader()));
200   }

241         log_trace(aot, resolve)("Resolved class  [%3d] %s -> %s", cp_index, ik->external_name(),
242                                 resolved_klass->external_name());
243       }
244     }
245   }
246 }
247 
248 void AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
249   JavaThread* THREAD = current;
250   constantPoolHandle cp(THREAD, ik->constants());
251   if (cp->cache() == nullptr) {
252     return;
253   }
254   for (int i = 0; i < ik->methods()->length(); i++) {
255     Method* m = ik->methods()->at(i);
256     BytecodeStream bcs(methodHandle(THREAD, m));
257     while (!bcs.is_last_bytecode()) {
258       bcs.next();
259       Bytecodes::Code raw_bc = bcs.raw_code();
260       switch (raw_bc) {
261       case Bytecodes::_getstatic:
262       case Bytecodes::_putstatic:
263       case Bytecodes::_getfield:
264       case Bytecodes::_putfield:
265         maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
266         if (HAS_PENDING_EXCEPTION) {
267           CLEAR_PENDING_EXCEPTION; // just ignore
268         }
269         break;
270       case Bytecodes::_invokehandle:
271       case Bytecodes::_invokespecial:
272       case Bytecodes::_invokevirtual:
273       case Bytecodes::_invokeinterface:
274       case Bytecodes::_invokestatic:
275         maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
276         if (HAS_PENDING_EXCEPTION) {
277           CLEAR_PENDING_EXCEPTION; // just ignore
278         }
279         break;
280       default:
281         break;
282       }
283     }
284   }
285 }
286 
287 void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
288                                            GrowableArray<bool>* preresolve_list, TRAPS) {
289   methodHandle mh(THREAD, m);
290   constantPoolHandle cp(THREAD, ik->constants());
291   HandleMark hm(THREAD);
292   int cp_index = cp->to_cp_index(raw_index, bc);
293 
294   if (cp->is_resolved(raw_index, bc)) {
295     return;
296   }
297 
298   if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
299     // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise
300     // the compiler may generate less efficient code.
301     return;
302   }
303 
304   int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
305   if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) {
306     // Do not resolve any field/methods from a class that has not been loaded yet.
307     return;
308   }
309 
310   Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK);
311   const char* is_static = "";
312 
313   switch (bc) {
314   case Bytecodes::_getstatic:
315   case Bytecodes::_putstatic:
316     if (!VM_Version::supports_fast_class_init_checks()) {
317       return; // Do not resolve since interpreter lacks fast clinit barriers support
318     }
319     InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
320     is_static = " *** static";
321     break;
322   case Bytecodes::_getfield:
323   case Bytecodes::_putfield:
324     InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
325     break;
326 
327   case Bytecodes::_invokestatic:
328     if (!VM_Version::supports_fast_class_init_checks()) {
329       return; // Do not resolve since interpreter lacks fast clinit barriers support
330     }
331     InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
332     is_static = " *** static";
333     break;
334 
335   case Bytecodes::_invokevirtual:
336   case Bytecodes::_invokespecial:
337   case Bytecodes::_invokeinterface:
338     InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
339     break;
340 
341   case Bytecodes::_invokehandle:
342     InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK);
343     break;
344 
345   default:
346     ShouldNotReachHere();
347   }
348 
349   if (log_is_enabled(Trace, aot, resolve)) {
350     ResourceMark rm(THREAD);
351     bool resolved = cp->is_resolved(raw_index, bc);
352     Symbol* name = cp->name_ref_at(raw_index, bc);
353     Symbol* signature = cp->signature_ref_at(raw_index, bc);
354     log_trace(aot, resolve)("%s %s [%3d] %s -> %s.%s:%s%s",
355                             (resolved ? "Resolved" : "Failed to resolve"),
356                             Bytecodes::name(bc), cp_index, ik->external_name(),
357                             resolved_klass->external_name(),
358                             name->as_C_string(), signature->as_C_string(), is_static);
359   }
360 }
361 
362 void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
363   JavaThread* THREAD = current;
364   constantPoolHandle cp(THREAD, ik->constants());
365   if (!CDSConfig::is_dumping_invokedynamic() || cp->cache() == nullptr) {
366     return;
367   }
368 
369   assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for "
370          "regenerated LambdaForm Invoker classes, which should not have indys anyway.");
371 
372   Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries();
373   for (int i = 0; i < indy_entries->length(); i++) {
374     ResolvedIndyEntry* rie = indy_entries->adr_at(i);
375     int cp_index = rie->constant_pool_index();
376     if (preresolve_list->at(cp_index) == true) {
377       if (!rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) {
378         InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD);

586 
587     // dynamicMethodType
588     if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
589       return false;
590     }
591 
592     return true;
593   }
594 
595   return false;
596 }
597 #ifdef ASSERT
598 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
599   if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
600     return false;
601   } else {
602     return ArchiveBuilder::current()->is_in_buffer_space(p);
603   }
604 }
605 #endif
606 
607 int AOTConstantPoolResolver::class_reflection_data_flags(InstanceKlass* ik, TRAPS) {
608   assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be");
609 
610   HandleMark hm(THREAD);
611   JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
612   JavaValue result(T_INT);
613   JavaCalls::call_special(&result,
614                           vmClasses::Class_klass(),
615                           vmSymbols::encodeReflectionData_name(),
616                           vmSymbols::void_int_signature(),
617                           &args, CHECK_0);
618   int flags = result.get_jint();
619   aot_log_info(aot)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags);
620   return flags;
621 }
622 
623 void AOTConstantPoolResolver::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) {
624   aot_log_info(aot)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags);
625   JavaThread* THREAD = current; // for exception macros
626   JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
627   args.push_int(rd_flags);
628   JavaValue result(T_OBJECT);
629   JavaCalls::call_special(&result,
630                           vmClasses::Class_klass(),
631                           vmSymbols::generateReflectionData_name(),
632                           vmSymbols::int_void_signature(),
633                           &args, THREAD);
634   if (HAS_PENDING_EXCEPTION) {
635     Handle exc_handle(THREAD, PENDING_EXCEPTION);
636     CLEAR_PENDING_EXCEPTION;
637 
638     log_warning(aot)("Exception during Class::generateReflectionData() call for %s", ik->external_name());
639     LogStreamHandle(Debug, aot) log;
640     if (log.is_enabled()) {
641       java_lang_Throwable::print_stack_trace(exc_handle, &log);
642     }
643   }
644 }
645 
646 Klass* AOTConstantPoolResolver::resolve_boot_class_or_fail(const char* class_name, TRAPS) {
647   Handle class_loader;
648   TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
649   return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, true, THREAD);
650 }
651 
652 void AOTConstantPoolResolver::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
653   if (interfaces->length() < 1) {
654     return;
655   }
656   if (ClassListWriter::is_enabled()) {
657     const char* loader_name = ArchiveUtils::builtin_loader_name_or_null(loader);
658     if (loader_name != nullptr) {
659       stringStream ss;
660       ss.print("%s %s %d %d", loader_name, proxy_name, access_flags, interfaces->length());
661       for (int i = 0; i < interfaces->length(); i++) {
662         oop mirror = interfaces->obj_at(i);
663         Klass* k = java_lang_Class::as_Klass(mirror);
664         ss.print(" %s", k->name()->as_C_string());
665       }
666       ClassListWriter w; // This locks ClassListFile_lock
667       w.stream()->print_cr("@dynamic-proxy %s", ss.freeze());
668     }
669   }
670   if (CDSConfig::is_dumping_preimage_static_archive()) {
671     FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags);
672   }
673 }
674 
675 void AOTConstantPoolResolver::init_dynamic_proxy_cache(TRAPS) {
676   static bool inited = false;
677   if (inited) {
678     return;
679   }
680   inited = true;
681 
682   Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK);
683   TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS");
684   TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V");
685 
686   JavaCallArguments args;
687   args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader()));
688   args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader()));
689   JavaValue result(T_VOID);
690   JavaCalls::call_static(&result,
691                          klass,
692                          method,
693                          signature,
694                          &args, CHECK);
695 }
696 
697 
698 void AOTConstantPoolResolver::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) {
699   if (!CDSConfig::is_dumping_dynamic_proxies()) {
700     return;
701   }
702   init_dynamic_proxy_cache(CHECK);
703 
704   Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK);
705   TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS");
706   TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;");
707 
708   JavaCallArguments args;
709   args.push_oop(Handle(THREAD, loader()));
710   args.push_oop(Handle(THREAD, proxy_name()));
711   args.push_oop(Handle(THREAD, interfaces()));
712   args.push_int(access_flags);
713   JavaValue result(T_OBJECT);
714   JavaCalls::call_static(&result,
715                          klass,
716                          method,
717                          signature,
718                          &args, CHECK);
719 
720   // Assumptions:
721   // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified.
722   // All named modules are loaded from the system modules files.
723   // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed.
724   // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived.
725   //       The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which
726   //       uses a null ProtectionDomain for this class)
727   oop mirror = result.get_oop();
728   assert(mirror != nullptr, "class must have been generated if not OOM");
729   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
730   if (ik->defined_by_boot_loader() || ik->defined_by_platform_loader()) {
731     assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported");
732     ik->set_shared_classpath_index(0);
733   } else {
734     assert(ik->defined_by_app_loader(), "must be");
735     ik->set_shared_classpath_index(AOTClassLocationConfig::dumptime()->app_cp_start_index());
736   }
737 
738   ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
739   if (log_is_enabled(Info, cds, dynamic, proxy)) {
740     ResourceMark rm(THREAD);
741     stringStream ss;
742     const char* prefix = "";
743     ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(),
744              ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index());
745     objArrayOop intfs = (objArrayOop)interfaces();
746     for (int i = 0; i < intfs->length(); i++) {
747       oop intf_mirror = intfs->obj_at(i);
748       ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name());
749       prefix = ", ";
750     }
751 
752     log_info(cds, dynamic, proxy)("%s", ss.freeze());
753   }
754 }
< prev index next >