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