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 AOTConstantPoolResolver::ClassesTable* AOTConstantPoolResolver::_processed_classes = nullptr;
41
42 void AOTConstantPoolResolver::initialize() {
43 assert(_processed_classes == nullptr, "must be");
44 _processed_classes = new (mtClass)ClassesTable();
45 }
46
47 void AOTConstantPoolResolver::dispose() {
48 assert(_processed_classes != nullptr, "must be");
49 delete _processed_classes;
50 _processed_classes = nullptr;
51 }
52
53 // Returns true if we CAN PROVE that cp_index will always resolve to
54 // the same information at both dump time and run time. This is a
55 // necessary (but not sufficient) condition for pre-resolving cp_index
56 // during CDS archive assembly.
57 bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int cp_index) {
58 assert(!is_in_archivebuilder_buffer(cp), "sanity");
146
147 void AOTConstantPoolResolver::dumptime_resolve_constants(InstanceKlass* ik, TRAPS) {
148 if (!ik->is_linked()) {
149 return;
150 }
151 bool first_time;
152 _processed_classes->put_if_absent(ik, &first_time);
153 if (!first_time) {
154 // We have already resolved the constants in class, so no need to do it again.
155 return;
156 }
157
158 constantPoolHandle cp(THREAD, ik->constants());
159 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
160 switch (cp->tag_at(cp_index).value()) {
161 case JVM_CONSTANT_String:
162 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
163 break;
164 }
165 }
166 }
167
168 // This works only for the boot/platform/app loaders
169 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
170 HandleMark hm(current);
171 Handle h_loader(current, class_loader);
172 Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader);
173 if (k != nullptr) {
174 return k;
175 }
176 if (h_loader() == SystemDictionary::java_system_loader()) {
177 return find_loaded_class(current, SystemDictionary::java_platform_loader(), name);
178 } else if (h_loader() == SystemDictionary::java_platform_loader()) {
179 return find_loaded_class(current, nullptr, name);
180 } else {
181 assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p",
182 cast_from_oop<address>(h_loader()),
183 cast_from_oop<address>(SystemDictionary::java_system_loader()),
184 cast_from_oop<address>(SystemDictionary::java_platform_loader()));
185 }
226 log_trace(cds, resolve)("Resolved class [%3d] %s -> %s", cp_index, ik->external_name(),
227 resolved_klass->external_name());
228 }
229 }
230 }
231 }
232
233 void AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
234 JavaThread* THREAD = current;
235 constantPoolHandle cp(THREAD, ik->constants());
236 if (cp->cache() == nullptr) {
237 return;
238 }
239 for (int i = 0; i < ik->methods()->length(); i++) {
240 Method* m = ik->methods()->at(i);
241 BytecodeStream bcs(methodHandle(THREAD, m));
242 while (!bcs.is_last_bytecode()) {
243 bcs.next();
244 Bytecodes::Code raw_bc = bcs.raw_code();
245 switch (raw_bc) {
246 case Bytecodes::_getfield:
247 case Bytecodes::_putfield:
248 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
249 if (HAS_PENDING_EXCEPTION) {
250 CLEAR_PENDING_EXCEPTION; // just ignore
251 }
252 break;
253 case Bytecodes::_invokehandle:
254 case Bytecodes::_invokespecial:
255 case Bytecodes::_invokevirtual:
256 case Bytecodes::_invokeinterface:
257 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
258 if (HAS_PENDING_EXCEPTION) {
259 CLEAR_PENDING_EXCEPTION; // just ignore
260 }
261 break;
262 default:
263 break;
264 }
265 }
266 }
267 }
268
269 void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
270 GrowableArray<bool>* preresolve_list, TRAPS) {
271 methodHandle mh(THREAD, m);
272 constantPoolHandle cp(THREAD, ik->constants());
273 HandleMark hm(THREAD);
274 int cp_index = cp->to_cp_index(raw_index, bc);
275
276 if (cp->is_resolved(raw_index, bc)) {
277 return;
278 }
279
280 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
281 // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise
282 // the compiler may generate less efficient code.
283 return;
284 }
285
286 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
287 if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) {
288 // Do not resolve any field/methods from a class that has not been loaded yet.
289 return;
290 }
291
292 Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK);
293
294 switch (bc) {
295 case Bytecodes::_getfield:
296 case Bytecodes::_putfield:
297 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
298 break;
299
300 case Bytecodes::_invokevirtual:
301 case Bytecodes::_invokespecial:
302 case Bytecodes::_invokeinterface:
303 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
304 break;
305
306 case Bytecodes::_invokehandle:
307 InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK);
308 break;
309
310 default:
311 ShouldNotReachHere();
312 }
313
314 if (log_is_enabled(Trace, cds, resolve)) {
315 ResourceMark rm(THREAD);
316 bool resolved = cp->is_resolved(raw_index, bc);
317 Symbol* name = cp->name_ref_at(raw_index, bc);
318 Symbol* signature = cp->signature_ref_at(raw_index, bc);
319 log_trace(cds, resolve)("%s %s [%3d] %s -> %s.%s:%s",
320 (resolved ? "Resolved" : "Failed to resolve"),
321 Bytecodes::name(bc), cp_index, ik->external_name(),
322 resolved_klass->external_name(),
323 name->as_C_string(), signature->as_C_string());
324 }
325 }
326
327 void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
328 JavaThread* THREAD = current;
329 constantPoolHandle cp(THREAD, ik->constants());
330 if (!CDSConfig::is_dumping_invokedynamic() || cp->cache() == nullptr) {
331 return;
332 }
333
334 assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for "
335 "regenerated LambdaForm Invoker classes, which should not have indys anyway.");
336
337 Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries();
338 for (int i = 0; i < indy_entries->length(); i++) {
339 ResolvedIndyEntry* rie = indy_entries->adr_at(i);
340 int cp_index = rie->constant_pool_index();
341 if (preresolve_list->at(cp_index) == true) {
342 if (!rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) {
343 InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD);
551
552 // dynamicMethodType
553 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
554 return false;
555 }
556
557 return true;
558 }
559
560 return false;
561 }
562 #ifdef ASSERT
563 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
564 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
565 return false;
566 } else {
567 return ArchiveBuilder::current()->is_in_buffer_space(p);
568 }
569 }
570 #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/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");
157
158 void AOTConstantPoolResolver::dumptime_resolve_constants(InstanceKlass* ik, TRAPS) {
159 if (!ik->is_linked()) {
160 return;
161 }
162 bool first_time;
163 _processed_classes->put_if_absent(ik, &first_time);
164 if (!first_time) {
165 // We have already resolved the constants in class, so no need to do it again.
166 return;
167 }
168
169 constantPoolHandle cp(THREAD, ik->constants());
170 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
171 switch (cp->tag_at(cp_index).value()) {
172 case JVM_CONSTANT_String:
173 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
174 break;
175 }
176 }
177
178 // Normally, we don't want to archive any CP entries that were not resolved
179 // in the training run. Otherwise the AOT/JIT may inline too much code that has not
180 // been executed.
181 //
182 // However, we want to aggressively resolve all klass/field/method constants for
183 // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes,
184 // so that the compiler can inline through them.
185 if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
186 bool eager_resolve = false;
187
188 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
189 eager_resolve = true;
190 }
191 if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) {
192 eager_resolve = true;
193 }
194
195 if (eager_resolve) {
196 preresolve_class_cp_entries(THREAD, ik, nullptr);
197 preresolve_field_and_method_cp_entries(THREAD, ik, nullptr);
198 }
199 }
200 }
201
202 // This works only for the boot/platform/app loaders
203 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
204 HandleMark hm(current);
205 Handle h_loader(current, class_loader);
206 Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader);
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 }
260 log_trace(cds, resolve)("Resolved class [%3d] %s -> %s", cp_index, ik->external_name(),
261 resolved_klass->external_name());
262 }
263 }
264 }
265 }
266
267 void AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
268 JavaThread* THREAD = current;
269 constantPoolHandle cp(THREAD, ik->constants());
270 if (cp->cache() == nullptr) {
271 return;
272 }
273 for (int i = 0; i < ik->methods()->length(); i++) {
274 Method* m = ik->methods()->at(i);
275 BytecodeStream bcs(methodHandle(THREAD, m));
276 while (!bcs.is_last_bytecode()) {
277 bcs.next();
278 Bytecodes::Code raw_bc = bcs.raw_code();
279 switch (raw_bc) {
280 case Bytecodes::_getstatic:
281 case Bytecodes::_putstatic:
282 case Bytecodes::_getfield:
283 case Bytecodes::_putfield:
284 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
285 if (HAS_PENDING_EXCEPTION) {
286 CLEAR_PENDING_EXCEPTION; // just ignore
287 }
288 break;
289 case Bytecodes::_invokehandle:
290 case Bytecodes::_invokespecial:
291 case Bytecodes::_invokevirtual:
292 case Bytecodes::_invokeinterface:
293 case Bytecodes::_invokestatic:
294 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
295 if (HAS_PENDING_EXCEPTION) {
296 CLEAR_PENDING_EXCEPTION; // just ignore
297 }
298 break;
299 default:
300 break;
301 }
302 }
303 }
304 }
305
306 void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
307 GrowableArray<bool>* preresolve_list, TRAPS) {
308 methodHandle mh(THREAD, m);
309 constantPoolHandle cp(THREAD, ik->constants());
310 HandleMark hm(THREAD);
311 int cp_index = cp->to_cp_index(raw_index, bc);
312
313 if (cp->is_resolved(raw_index, bc)) {
314 return;
315 }
316
317 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
318 // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise
319 // the compiler may generate less efficient code.
320 return;
321 }
322
323 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
324 if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) {
325 // Do not resolve any field/methods from a class that has not been loaded yet.
326 return;
327 }
328
329 Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK);
330 const char* is_static = "";
331
332 switch (bc) {
333 case Bytecodes::_getstatic:
334 case Bytecodes::_putstatic:
335 if (!VM_Version::supports_fast_class_init_checks()) {
336 return; // Do not resolve since interpreter lacks fast clinit barriers support
337 }
338 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
339 is_static = " *** static";
340 break;
341 case Bytecodes::_getfield:
342 case Bytecodes::_putfield:
343 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
344 break;
345
346 case Bytecodes::_invokestatic:
347 if (!VM_Version::supports_fast_class_init_checks()) {
348 return; // Do not resolve since interpreter lacks fast clinit barriers support
349 }
350 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
351 is_static = " *** static";
352 break;
353
354 case Bytecodes::_invokevirtual:
355 case Bytecodes::_invokespecial:
356 case Bytecodes::_invokeinterface:
357 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
358 break;
359
360 case Bytecodes::_invokehandle:
361 InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK);
362 break;
363
364 default:
365 ShouldNotReachHere();
366 }
367
368 if (log_is_enabled(Trace, cds, resolve)) {
369 ResourceMark rm(THREAD);
370 bool resolved = cp->is_resolved(raw_index, bc);
371 Symbol* name = cp->name_ref_at(raw_index, bc);
372 Symbol* signature = cp->signature_ref_at(raw_index, bc);
373 log_trace(cds, resolve)("%s %s [%3d] %s -> %s.%s:%s%s",
374 (resolved ? "Resolved" : "Failed to resolve"),
375 Bytecodes::name(bc), cp_index, ik->external_name(),
376 resolved_klass->external_name(),
377 name->as_C_string(), signature->as_C_string(), is_static);
378 }
379 }
380
381 void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
382 JavaThread* THREAD = current;
383 constantPoolHandle cp(THREAD, ik->constants());
384 if (!CDSConfig::is_dumping_invokedynamic() || cp->cache() == nullptr) {
385 return;
386 }
387
388 assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for "
389 "regenerated LambdaForm Invoker classes, which should not have indys anyway.");
390
391 Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries();
392 for (int i = 0; i < indy_entries->length(); i++) {
393 ResolvedIndyEntry* rie = indy_entries->adr_at(i);
394 int cp_index = rie->constant_pool_index();
395 if (preresolve_list->at(cp_index) == true) {
396 if (!rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) {
397 InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD);
605
606 // dynamicMethodType
607 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
608 return false;
609 }
610
611 return true;
612 }
613
614 return false;
615 }
616 #ifdef ASSERT
617 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
618 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
619 return false;
620 } else {
621 return ArchiveBuilder::current()->is_in_buffer_space(p);
622 }
623 }
624 #endif
625
626 int AOTConstantPoolResolver::class_reflection_data_flags(InstanceKlass* ik, TRAPS) {
627 assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be");
628
629 HandleMark hm(THREAD);
630 JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
631 JavaValue result(T_INT);
632 JavaCalls::call_special(&result,
633 vmClasses::Class_klass(),
634 vmSymbols::encodeReflectionData_name(),
635 vmSymbols::void_int_signature(),
636 &args, CHECK_0);
637 int flags = result.get_jint();
638 log_info(cds)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags);
639 return flags;
640 }
641
642 void AOTConstantPoolResolver::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) {
643 log_info(cds)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags);
644 JavaThread* THREAD = current; // for exception macros
645 JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
646 args.push_int(rd_flags);
647 JavaValue result(T_OBJECT);
648 JavaCalls::call_special(&result,
649 vmClasses::Class_klass(),
650 vmSymbols::generateReflectionData_name(),
651 vmSymbols::int_void_signature(),
652 &args, THREAD);
653 if (HAS_PENDING_EXCEPTION) {
654 Handle exc_handle(THREAD, PENDING_EXCEPTION);
655 CLEAR_PENDING_EXCEPTION;
656
657 log_warning(cds)("Exception during Class::generateReflectionData() call for %s", ik->external_name());
658 LogStreamHandle(Debug, cds) log;
659 if (log.is_enabled()) {
660 java_lang_Throwable::print_stack_trace(exc_handle, &log);
661 }
662 }
663 }
664
665 Klass* AOTConstantPoolResolver::resolve_boot_class_or_fail(const char* class_name, TRAPS) {
666 Handle class_loader;
667 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
668 return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, true, THREAD);
669 }
670
671 void AOTConstantPoolResolver::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
672 if (interfaces->length() < 1) {
673 return;
674 }
675 if (ClassListWriter::is_enabled()) {
676 const char* loader_name = ArchiveUtils::builtin_loader_name_or_null(loader);
677 if (loader_name != nullptr) {
678 stringStream ss;
679 ss.print("%s %s %d %d", loader_name, proxy_name, access_flags, interfaces->length());
680 for (int i = 0; i < interfaces->length(); i++) {
681 oop mirror = interfaces->obj_at(i);
682 Klass* k = java_lang_Class::as_Klass(mirror);
683 ss.print(" %s", k->name()->as_C_string());
684 }
685 ClassListWriter w;
686 w.stream()->print_cr("@dynamic-proxy %s", ss.freeze());
687 }
688 }
689 if (CDSConfig::is_dumping_preimage_static_archive()) {
690 FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags);
691 }
692 }
693
694 void AOTConstantPoolResolver::init_dynamic_proxy_cache(TRAPS) {
695 static bool inited = false;
696 if (inited) {
697 return;
698 }
699 inited = true;
700
701 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK);
702 TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS");
703 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V");
704
705 JavaCallArguments args;
706 args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader()));
707 args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader()));
708 JavaValue result(T_VOID);
709 JavaCalls::call_static(&result,
710 klass,
711 method,
712 signature,
713 &args, CHECK);
714 }
715
716
717 void AOTConstantPoolResolver::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) {
718 if (!CDSConfig::is_dumping_dynamic_proxies()) {
719 return;
720 }
721 init_dynamic_proxy_cache(CHECK);
722
723 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK);
724 TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS");
725 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;");
726
727 JavaCallArguments args;
728 args.push_oop(Handle(THREAD, loader()));
729 args.push_oop(Handle(THREAD, proxy_name()));
730 args.push_oop(Handle(THREAD, interfaces()));
731 args.push_int(access_flags);
732 JavaValue result(T_OBJECT);
733 JavaCalls::call_static(&result,
734 klass,
735 method,
736 signature,
737 &args, CHECK);
738
739 // Assumptions:
740 // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified.
741 // All named modules are loaded from the system modules files.
742 // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed.
743 // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived.
744 // The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which
745 // uses a null ProtectionDomain for this class)
746 oop mirror = result.get_oop();
747 assert(mirror != nullptr, "class must have been generated if not OOM");
748 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
749 if (ik->is_shared_boot_class() || ik->is_shared_platform_class()) {
750 assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported");
751 ik->set_shared_classpath_index(0);
752 } else {
753 assert(ik->is_shared_app_class(), "must be");
754 ik->set_shared_classpath_index(AOTClassLocationConfig::dumptime()->app_cp_start_index());
755 }
756
757 ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
758 if (log_is_enabled(Info, cds, dynamic, proxy)) {
759 ResourceMark rm(THREAD);
760 stringStream ss;
761 const char* prefix = "";
762 ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(),
763 ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index());
764 objArrayOop intfs = (objArrayOop)interfaces();
765 for (int i = 0; i < intfs->length(); i++) {
766 oop intf_mirror = intfs->obj_at(i);
767 ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name());
768 prefix = ", ";
769 }
770
771 log_info(cds, dynamic, proxy)("%s", ss.freeze());
772 }
773 }
|