1 /*
2 * Copyright (c) 2022, 2023, 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 "precompiled.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/classPrelinker.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "classfile/vmClasses.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/constantPool.inline.hpp"
33 #include "oops/instanceKlass.hpp"
34 #include "oops/klass.inline.hpp"
35 #include "runtime/handles.inline.hpp"
36
37 ClassPrelinker::ClassesTable* ClassPrelinker::_processed_classes = nullptr;
38 ClassPrelinker::ClassesTable* ClassPrelinker::_vm_classes = nullptr;
39
40 bool ClassPrelinker::is_vm_class(InstanceKlass* ik) {
41 return (_vm_classes->get(ik) != nullptr);
42 }
43
44 void ClassPrelinker::add_one_vm_class(InstanceKlass* ik) {
45 bool created;
46 _vm_classes->put_if_absent(ik, &created);
47 if (created) {
48 InstanceKlass* super = ik->java_super();
49 if (super != nullptr) {
50 add_one_vm_class(super);
51 }
52 Array<InstanceKlass*>* ifs = ik->local_interfaces();
53 for (int i = 0; i < ifs->length(); i++) {
54 add_one_vm_class(ifs->at(i));
55 }
56 }
57 }
58
59 void ClassPrelinker::initialize() {
60 assert(_vm_classes == nullptr, "must be");
61 _vm_classes = new (mtClass)ClassesTable();
62 _processed_classes = new (mtClass)ClassesTable();
63 for (auto id : EnumRange<vmClassID>{}) {
64 add_one_vm_class(vmClasses::klass_at(id));
65 }
66 }
67
68 void ClassPrelinker::dispose() {
69 assert(_vm_classes != nullptr, "must be");
70 delete _vm_classes;
71 delete _processed_classes;
72 _vm_classes = nullptr;
73 _processed_classes = nullptr;
74 }
75
76 bool ClassPrelinker::can_archive_resolved_klass(ConstantPool* cp, int cp_index) {
77 assert(!is_in_archivebuilder_buffer(cp), "sanity");
78 assert(cp->tag_at(cp_index).is_klass(), "must be resolved");
79
80 Klass* resolved_klass = cp->resolved_klass_at(cp_index);
81 assert(resolved_klass != nullptr, "must be");
82
83 return can_archive_resolved_klass(cp->pool_holder(), resolved_klass);
84 }
85
86 bool ClassPrelinker::can_archive_resolved_klass(InstanceKlass* cp_holder, Klass* resolved_klass) {
87 assert(!is_in_archivebuilder_buffer(cp_holder), "sanity");
88 assert(!is_in_archivebuilder_buffer(resolved_klass), "sanity");
89
90 if (resolved_klass->is_instance_klass()) {
91 InstanceKlass* ik = InstanceKlass::cast(resolved_klass);
92 if (is_vm_class(ik)) { // These are safe to resolve. See is_vm_class declaration.
93 assert(ik->is_shared_boot_class(), "vmClasses must be loaded by boot loader");
94 if (cp_holder->is_shared_boot_class()) {
95 // For now, do this for boot loader only. Other loaders
96 // must go through ConstantPool::klass_at_impl at runtime
97 // to put this class in their directory.
98
99 // TODO: we can support the platform and app loaders as well, if we
100 // preload the vmClasses into these two loaders during VM bootstrap.
101 return true;
102 }
103 }
104
105 if (cp_holder->is_subtype_of(ik)) {
106 // All super types of ik will be resolved in ik->class_loader() before
107 // ik is defined in this loader, so it's safe to archive the resolved klass reference.
108 return true;
109 }
110
111 // TODO -- allow objArray classes, too
112 }
113
114 return false;
115 }
116
117 void ClassPrelinker::dumptime_resolve_constants(InstanceKlass* ik, TRAPS) {
118 constantPoolHandle cp(THREAD, ik->constants());
119 if (cp->cache() == nullptr || cp->reference_map() == nullptr) {
120 // The cache may be null if the pool_holder klass fails verification
121 // at dump time due to missing dependencies.
122 return;
123 }
124
125 bool first_time;
126 _processed_classes->put_if_absent(ik, &first_time);
127 if (!first_time) {
128 // We have already resolved the constants in class, so no need to do it again.
129 return;
130 }
131
132 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
133 switch (cp->tag_at(cp_index).value()) {
134 case JVM_CONSTANT_UnresolvedClass:
135 maybe_resolve_class(cp, cp_index, CHECK);
136 break;
137
138 case JVM_CONSTANT_String:
139 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
140 break;
141 }
142 }
143 }
144
145 Klass* ClassPrelinker::find_loaded_class(JavaThread* THREAD, oop class_loader, Symbol* name) {
146 HandleMark hm(THREAD);
147 Handle h_loader(THREAD, class_loader);
148 Klass* k = SystemDictionary::find_instance_or_array_klass(THREAD, name,
149 h_loader,
150 Handle());
151 if (k != nullptr) {
152 return k;
153 }
154 if (class_loader == SystemDictionary::java_system_loader()) {
155 return find_loaded_class(THREAD, SystemDictionary::java_platform_loader(), name);
156 } else if (class_loader == SystemDictionary::java_platform_loader()) {
157 return find_loaded_class(THREAD, nullptr, name);
158 }
159
160 return nullptr;
161 }
162
163 Klass* ClassPrelinker::maybe_resolve_class(constantPoolHandle cp, int cp_index, TRAPS) {
164 assert(!is_in_archivebuilder_buffer(cp()), "sanity");
165 InstanceKlass* cp_holder = cp->pool_holder();
166 if (!cp_holder->is_shared_boot_class() &&
167 !cp_holder->is_shared_platform_class() &&
168 !cp_holder->is_shared_app_class()) {
169 // Don't trust custom loaders, as they may not be well-behaved
170 // when resolving classes.
171 return nullptr;
172 }
173
174 Symbol* name = cp->klass_name_at(cp_index);
175 Klass* resolved_klass = find_loaded_class(THREAD, cp_holder->class_loader(), name);
176 if (resolved_klass != nullptr && can_archive_resolved_klass(cp_holder, resolved_klass)) {
177 Klass* k = cp->klass_at(cp_index, CHECK_NULL); // Should fail only with OOM
178 assert(k == resolved_klass, "must be");
179 }
180
181 return resolved_klass;
182 }
183
184 #if INCLUDE_CDS_JAVA_HEAP
185 void ClassPrelinker::resolve_string(constantPoolHandle cp, int cp_index, TRAPS) {
186 if (CDSConfig::is_dumping_heap()) {
187 int cache_index = cp->cp_to_object_index(cp_index);
188 ConstantPool::string_at_impl(cp, cp_index, cache_index, CHECK);
189 }
190 }
191 #endif
192
193 #ifdef ASSERT
194 bool ClassPrelinker::is_in_archivebuilder_buffer(address p) {
195 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
196 return false;
197 } else {
198 return ArchiveBuilder::current()->is_in_buffer_space(p);
199 }
200 }
201 #endif
|
1 /*
2 * Copyright (c) 2022, 2024, 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 "precompiled.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveUtils.inline.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/classPrelinker.hpp"
30 #include "cds/classPreloader.hpp"
31 #include "cds/classListWriter.hpp"
32 #include "cds/finalImageRecipes.hpp"
33 #include "cds/heapShared.hpp"
34 #include "cds/lambdaFormInvokers.inline.hpp"
35 #include "cds/regeneratedClasses.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/bytecode.hpp"
44 #include "interpreter/bytecodeStream.hpp"
45 #include "interpreter/interpreterRuntime.hpp"
46 #include "interpreter/linkResolver.hpp"
47 #include "memory/oopFactory.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "oops/constantPool.inline.hpp"
50 #include "oops/fieldStreams.inline.hpp"
51 #include "oops/instanceKlass.hpp"
52 #include "oops/klass.inline.hpp"
53 #include "runtime/handles.inline.hpp"
54 #include "runtime/javaCalls.hpp"
55 #include "runtime/perfData.inline.hpp"
56 #include "runtime/timer.hpp"
57 #include "runtime/signature.hpp"
58
59 ClassPrelinker::ClassesTable* ClassPrelinker::_processed_classes = nullptr;
60
61 void ClassPrelinker::initialize() {
62 _processed_classes = new (mtClass)ClassesTable();
63 }
64
65 void ClassPrelinker::dispose() {
66 delete _processed_classes;
67 _processed_classes = nullptr;
68 }
69
70 // Returns true if we CAN PROVE that cp_index will always resolve to
71 // the same information at both dump time and run time. This is a
72 // necessary (but not sufficient) condition for pre-resolving cp_index
73 // during CDS archive assembly.
74 bool ClassPrelinker::is_resolution_deterministic(ConstantPool* cp, int cp_index) {
75 assert(!is_in_archivebuilder_buffer(cp), "sanity");
76
77 if (cp->tag_at(cp_index).is_klass()) {
78 // We require cp_index to be already resolved. This is fine for now, are we
79 // currently archive only CP entries that are already resolved.
80 Klass* resolved_klass = cp->resolved_klass_at(cp_index);
81 return resolved_klass != nullptr && is_class_resolution_deterministic(cp->pool_holder(), resolved_klass);
82 } else if (cp->tag_at(cp_index).is_invoke_dynamic()) {
83 return is_indy_resolution_deterministic(cp, cp_index);
84 } else if (cp->tag_at(cp_index).is_field() ||
85 cp->tag_at(cp_index).is_method() ||
86 cp->tag_at(cp_index).is_interface_method()) {
87 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
88 if (!cp->tag_at(klass_cp_index).is_klass()) {
89 // Not yet resolved
90 return false;
91 }
92 Klass* k = cp->resolved_klass_at(klass_cp_index);
93 if (!is_class_resolution_deterministic(cp->pool_holder(), k)) {
94 return false;
95 }
96
97 if (!k->is_instance_klass()) { // FIXME -- remove this check, and add a test case
98 // FIXME: is it valid to have a non-instance klass in method refs?
99 return false;
100 }
101
102 // Here, We don't check if this entry can actually be resolved to a valid Field/Method.
103 // This method should be called by the ConstantPool to check Fields/Methods that
104 // have already been successfully resolved.
105 return true;
106 } else {
107 return false;
108 }
109 }
110
111 bool ClassPrelinker::is_class_resolution_deterministic(InstanceKlass* cp_holder, Klass* resolved_class) {
112 assert(!is_in_archivebuilder_buffer(cp_holder), "sanity");
113 assert(!is_in_archivebuilder_buffer(resolved_class), "sanity");
114
115 if (resolved_class->is_instance_klass()) {
116 InstanceKlass* ik = InstanceKlass::cast(resolved_class);
117
118 if (!ik->is_shared() && SystemDictionaryShared::is_excluded_class(ik)) {
119 return false;
120 }
121
122 if (cp_holder->is_subtype_of(ik)) {
123 // All super types of ik will be resolved in ik->class_loader() before
124 // ik is defined in this loader, so it's safe to archive the resolved klass reference.
125 return true;
126 }
127
128 if (ClassPreloader::is_preloaded_class(ik)) {
129 if (cp_holder->is_shared_platform_class()) {
130 ClassPreloader::add_initiated_class(cp_holder, ik);
131 return true;
132 } else if (cp_holder->is_shared_app_class()) {
133 ClassPreloader::add_initiated_class(cp_holder, ik);
134 return true;
135 } else if (cp_holder->is_shared_boot_class()) {
136 assert(ik->class_loader() == nullptr, "a boot class can reference only boot classes");
137 return true;
138 } else if (cp_holder->is_hidden() && cp_holder->class_loader() == nullptr) { // FIXME -- use better checks!
139 return true;
140 }
141 }
142 } else if (resolved_class->is_objArray_klass()) {
143 Klass* elem = ObjArrayKlass::cast(resolved_class)->bottom_klass();
144 if (elem->is_instance_klass()) {
145 return is_class_resolution_deterministic(cp_holder, InstanceKlass::cast(elem));
146 } else if (elem->is_typeArray_klass()) {
147 return true;
148 }
149 } else if (resolved_class->is_typeArray_klass()) {
150 return true;
151 }
152
153 return false;
154 }
155
156 void ClassPrelinker::dumptime_resolve_constants(InstanceKlass* ik, TRAPS) {
157 if (!ik->is_linked()) {
158 return;
159 }
160 bool first_time;
161 _processed_classes->put_if_absent(ik, &first_time);
162 if (!first_time) {
163 // We have already resolved the constants in class, so no need to do it again.
164 return;
165 }
166
167 constantPoolHandle cp(THREAD, ik->constants());
168 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
169 if (cp->tag_at(cp_index).value() == JVM_CONSTANT_String) {
170 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
171 }
172 }
173
174 // Normally, we don't want to archive any CP entries that were not resolved
175 // in the training run. Otherwise the AOT/JIT may inline too much code that has not
176 // been executed.
177 //
178 // However, we want to aggressively resolve all klass/field/method constants for
179 // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes,
180 // so that the compiler can inline through them.
181 if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
182 bool eager_resolve = false;
183
184 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
185 eager_resolve = true;
186 }
187 if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) {
188 eager_resolve = true;
189 }
190
191 if (eager_resolve) {
192 preresolve_class_cp_entries(THREAD, ik, nullptr);
193 preresolve_field_and_method_cp_entries(THREAD, ik, nullptr);
194 }
195 }
196 }
197
198 // This works only for the boot/platform/app loaders
199 Klass* ClassPrelinker::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
200 HandleMark hm(current);
201 Handle h_loader(current, class_loader);
202 Klass* k = SystemDictionary::find_instance_or_array_klass(current, name,
203 h_loader,
204 Handle());
205 if (k != nullptr) {
206 return k;
207 }
208 if (h_loader() == SystemDictionary::java_system_loader()) {
209 return find_loaded_class(current, SystemDictionary::java_platform_loader(), name);
210 } else if (h_loader() == SystemDictionary::java_platform_loader()) {
211 return find_loaded_class(current, nullptr, name);
212 } else {
213 assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p",
214 cast_from_oop<address>(h_loader()),
215 cast_from_oop<address>(SystemDictionary::java_system_loader()),
216 cast_from_oop<address>(SystemDictionary::java_platform_loader()));
217 }
218
219 return nullptr;
220 }
221
222 Klass* ClassPrelinker::find_loaded_class(Thread* current, ConstantPool* cp, int class_cp_index) {
223 Symbol* name = cp->klass_name_at(class_cp_index);
224 return find_loaded_class(current, cp->pool_holder()->class_loader(), name);
225 }
226
227 #if INCLUDE_CDS_JAVA_HEAP
228 void ClassPrelinker::resolve_string(constantPoolHandle cp, int cp_index, TRAPS) {
229 if (CDSConfig::is_dumping_heap()) {
230 int cache_index = cp->cp_to_object_index(cp_index);
231 ConstantPool::string_at_impl(cp, cp_index, cache_index, CHECK);
232 }
233 }
234 #endif
235
236 void ClassPrelinker::preresolve_class_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
237 if (!PreloadSharedClasses) {
238 return;
239 }
240 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
241 return;
242 }
243
244 JavaThread* THREAD = current;
245 constantPoolHandle cp(THREAD, ik->constants());
246 for (int cp_index = 1; cp_index < cp->length(); cp_index++) {
247 if (cp->tag_at(cp_index).value() == JVM_CONSTANT_UnresolvedClass) {
248 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
249 // This class was not resolved during trial run. Don't attempt to resolve it. Otherwise
250 // the compiler may generate less efficient code.
251 continue;
252 }
253 if (find_loaded_class(current, cp(), cp_index) == nullptr) {
254 // Do not resolve any class that has not been loaded yet
255 continue;
256 }
257 Klass* resolved_klass = cp->klass_at(cp_index, THREAD);
258 if (HAS_PENDING_EXCEPTION) {
259 CLEAR_PENDING_EXCEPTION; // just ignore
260 } else {
261 log_trace(cds, resolve)("Resolved class [%3d] %s -> %s", cp_index, ik->external_name(),
262 resolved_klass->external_name());
263 }
264 }
265 }
266 }
267
268 void ClassPrelinker::preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
269 JavaThread* THREAD = current;
270 constantPoolHandle cp(THREAD, ik->constants());
271 if (cp->cache() == nullptr) {
272 return;
273 }
274 for (int i = 0; i < ik->methods()->length(); i++) {
275 Method* m = ik->methods()->at(i);
276 BytecodeStream bcs(methodHandle(THREAD, m));
277 while (!bcs.is_last_bytecode()) {
278 bcs.next();
279 Bytecodes::Code raw_bc = bcs.raw_code();
280 switch (raw_bc) {
281 case Bytecodes::_getstatic:
282 case Bytecodes::_putstatic:
283 case Bytecodes::_getfield:
284 case Bytecodes::_putfield:
285 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
286 if (HAS_PENDING_EXCEPTION) {
287 CLEAR_PENDING_EXCEPTION; // just ignore
288 }
289 break;
290 case Bytecodes::_invokehandle:
291 case Bytecodes::_invokespecial:
292 case Bytecodes::_invokevirtual:
293 case Bytecodes::_invokeinterface:
294 case Bytecodes::_invokestatic:
295 maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
296 if (HAS_PENDING_EXCEPTION) {
297 CLEAR_PENDING_EXCEPTION; // just ignore
298 }
299 break;
300 default:
301 break;
302 }
303 }
304 }
305 }
306
307 void ClassPrelinker::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
308 GrowableArray<bool>* preresolve_list, TRAPS) {
309 methodHandle mh(THREAD, m);
310 constantPoolHandle cp(THREAD, ik->constants());
311 HandleMark hm(THREAD);
312 int cp_index = cp->to_cp_index(raw_index, bc);
313
314 if (cp->is_resolved(raw_index, bc)) {
315 return;
316 }
317
318 if (preresolve_list != nullptr && preresolve_list->at(cp_index) == false) {
319 // This field wasn't resolved during the trial run. Don't attempt to resolve it. Otherwise
320 // the compiler may generate less efficient code.
321 return;
322 }
323
324 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
325 if (find_loaded_class(THREAD, cp(), klass_cp_index) == nullptr) {
326 // Do not resolve any field/methods from a class that has not been loaded yet.
327 return;
328 }
329
330 Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK);
331 const char* is_static = "";
332
333 switch (bc) {
334 case Bytecodes::_getstatic:
335 case Bytecodes::_putstatic:
336 if (!VM_Version::supports_fast_class_init_checks()) {
337 return; // Do not resolve since interpreter lacks fast clinit barriers support
338 }
339 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
340 is_static = " *** static";
341 break;
342 case Bytecodes::_getfield:
343 case Bytecodes::_putfield:
344 InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
345 break;
346
347 case Bytecodes::_invokestatic:
348 if (!VM_Version::supports_fast_class_init_checks()) {
349 return; // Do not resolve since interpreter lacks fast clinit barriers support
350 }
351 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, mh, cp, CHECK);
352 is_static = " *** static";
353 break;
354
355 case Bytecodes::_invokevirtual:
356 case Bytecodes::_invokespecial:
357 case Bytecodes::_invokeinterface:
358 InterpreterRuntime::cds_resolve_invoke(bc, raw_index, mh, cp, CHECK);
359 break;
360
361 case Bytecodes::_invokehandle:
362 InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK);
363 break;
364
365 default:
366 ShouldNotReachHere();
367 }
368
369 if (log_is_enabled(Trace, cds, resolve)) {
370 ResourceMark rm(THREAD);
371 bool resolved = cp->is_resolved(raw_index, bc);
372 Symbol* name = cp->name_ref_at(raw_index, bc);
373 Symbol* signature = cp->signature_ref_at(raw_index, bc);
374 log_trace(cds, resolve)("%s %s [%3d] %s -> %s.%s:%s%s",
375 (resolved ? "Resolved" : "Failed to resolve"),
376 Bytecodes::name(bc), cp_index, ik->external_name(),
377 resolved_klass->external_name(),
378 name->as_C_string(), signature->as_C_string(), is_static);
379 }
380 }
381
382 void ClassPrelinker::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
383 JavaThread* THREAD = current;
384 constantPoolHandle cp(THREAD, ik->constants());
385 if (!ArchiveInvokeDynamic || cp->cache() == nullptr) {
386 return;
387 }
388
389 assert(preresolve_list != nullptr, "preresolve_indy_cp_entries() should not be called for "
390 "regenerated LambdaForm Invoker classes, which should not have indys anyway.");
391
392 Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries();
393 for (int i = 0; i < indy_entries->length(); i++) {
394 ResolvedIndyEntry* rie = indy_entries->adr_at(i);
395 int cp_index = rie->constant_pool_index();
396 if (preresolve_list->at(cp_index) == true && !rie->is_resolved() && is_indy_resolution_deterministic(cp(), cp_index)) {
397 InterpreterRuntime::cds_resolve_invokedynamic(i, cp, THREAD);
398 if (HAS_PENDING_EXCEPTION) {
399 CLEAR_PENDING_EXCEPTION; // just ignore
400 }
401 }
402 }
403 }
404
405 static GrowableArrayCHeap<char*, mtClassShared>* _invokedynamic_filter = nullptr;
406
407 static bool has_clinit(InstanceKlass* ik) {
408 if (ik->class_initializer() != nullptr) {
409 return true;
410 }
411 InstanceKlass* super = ik->java_super();
412 if (super != nullptr && has_clinit(super)) {
413 return true;
414 }
415 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
416 int num_interfaces = interfaces->length();
417 for (int index = 0; index < num_interfaces; index++) {
418 InstanceKlass* intf = interfaces->at(index);
419 if (has_clinit(intf)) {
420 return true;
421 }
422 }
423 return false;
424 }
425
426 bool ClassPrelinker::is_indy_resolution_deterministic(ConstantPool* cp, int cp_index) {
427 assert(cp->tag_at(cp_index).is_invoke_dynamic(), "sanity");
428 if (!ArchiveInvokeDynamic || !HeapShared::can_write()) {
429 return false;
430 }
431
432 if (!SystemDictionaryShared::is_builtin(cp->pool_holder())) {
433 return false;
434 }
435
436 int bsm = cp->bootstrap_method_ref_index_at(cp_index);
437 int bsm_ref = cp->method_handle_index_at(bsm);
438 Symbol* bsm_name = cp->uncached_name_ref_at(bsm_ref);
439 Symbol* bsm_signature = cp->uncached_signature_ref_at(bsm_ref);
440 Symbol* bsm_klass = cp->klass_name_at(cp->uncached_klass_ref_index_at(bsm_ref));
441
442 // We currently support only string concat and LambdaMetafactory::metafactory()
443
444 if (bsm_klass->equals("java/lang/invoke/StringConcatFactory") &&
445 bsm_name->equals("makeConcatWithConstants")) {
446 return true;
447 }
448
449 if (bsm_klass->equals("java/lang/invoke/LambdaMetafactory") &&
450 ((bsm_name->equals("metafactory") && bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;")) ||
451 (bsm_name->equals("altMetafactory") && bsm_signature->equals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;")))) {
452 SignatureStream ss(cp->uncached_signature_ref_at(cp_index));
453 ss.skip_to_return_type();
454 Symbol* type = ss.as_symbol(); // This is the interface type implemented by the lambda proxy
455 InstanceKlass* holder = cp->pool_holder();
456 Klass* k = find_loaded_class(Thread::current(), holder->class_loader(), type);
457 if (k == nullptr) {
458 return false;
459 }
460 if (!k->is_interface()) {
461 // Might be a class not generated by javac
462 return false;
463 }
464
465 if (has_clinit(InstanceKlass::cast(k))) {
466 // We initialize the class of the archived lambda proxy at VM start-up, which will also initialize
467 // the interface that it implements. If that interface has a clinit method, we can potentially
468 // change program execution order. See test/hotspot/jtreg/runtime/cds/appcds/indy/IndyMiscTests.java
469 if (log_is_enabled(Debug, cds, resolve)) {
470 ResourceMark rm;
471 log_debug(cds, resolve)("Cannot resolve Lambda proxy of interface type %s", k->external_name());
472 }
473 return false;
474 }
475
476 return true;
477 }
478
479 return false;
480 }
481
482 #ifdef ASSERT
483 bool ClassPrelinker::is_in_archivebuilder_buffer(address p) {
484 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
485 return false;
486 } else {
487 return ArchiveBuilder::current()->is_in_buffer_space(p);
488 }
489 }
490 #endif
491
492 int ClassPrelinker::class_reflection_data_flags(InstanceKlass* ik, TRAPS) {
493 assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be");
494
495 HandleMark hm(THREAD);
496 JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
497 JavaValue result(T_INT);
498 JavaCalls::call_special(&result,
499 vmClasses::Class_klass(),
500 vmSymbols::encodeReflectionData_name(),
501 vmSymbols::void_int_signature(),
502 &args, CHECK_0);
503 int flags = result.get_jint();
504 log_info(cds)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags);
505 return flags;
506 }
507
508 void ClassPrelinker::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) {
509 log_info(cds)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags);
510 JavaThread* THREAD = current; // for exception macros
511 JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
512 args.push_int(rd_flags);
513 JavaValue result(T_OBJECT);
514 JavaCalls::call_special(&result,
515 vmClasses::Class_klass(),
516 vmSymbols::generateReflectionData_name(),
517 vmSymbols::int_void_signature(),
518 &args, THREAD);
519 if (HAS_PENDING_EXCEPTION) {
520 Handle exc_handle(THREAD, PENDING_EXCEPTION);
521 CLEAR_PENDING_EXCEPTION;
522
523 log_warning(cds)("Exception during Class::generateReflectionData() call for %s", ik->external_name());
524 LogStreamHandle(Debug, cds) log;
525 if (log.is_enabled()) {
526 java_lang_Throwable::print_stack_trace(exc_handle, &log);
527 }
528 }
529 }
530
531 Klass* ClassPrelinker::resolve_boot_class_or_fail(const char* class_name, TRAPS) {
532 Handle class_loader;
533 Handle protection_domain;
534 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
535 return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, protection_domain, true, THREAD);
536 }
537
538 void ClassPrelinker::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
539 if (interfaces->length() < 1) {
540 return;
541 }
542 if (ClassListWriter::is_enabled()) {
543 const char* loader_name = ArchiveUtils::builtin_loader_name_or_null(loader);
544 if (loader_name != nullptr) {
545 stringStream ss;
546 ss.print("%s %s %d %d", loader_name, proxy_name, access_flags, interfaces->length());
547 for (int i = 0; i < interfaces->length(); i++) {
548 oop mirror = interfaces->obj_at(i);
549 Klass* k = java_lang_Class::as_Klass(mirror);
550 ss.print(" %s", k->name()->as_C_string());
551 }
552 ClassListWriter w;
553 w.stream()->print_cr("@dynamic-proxy %s", ss.freeze());
554 }
555 }
556 if (CDSConfig::is_dumping_preimage_static_archive()) {
557 FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags);
558 }
559 }
560
561 void ClassPrelinker::init_dynamic_proxy_cache(TRAPS) {
562 static bool inited = false;
563 if (inited) {
564 return;
565 }
566 inited = true;
567
568 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK);
569 TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS");
570 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V");
571
572 JavaCallArguments args;
573 args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader()));
574 args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader()));
575 JavaValue result(T_VOID);
576 JavaCalls::call_static(&result,
577 klass,
578 method,
579 signature,
580 &args, CHECK);
581 }
582
583
584 void ClassPrelinker::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) {
585 if (!CDSConfig::is_dumping_dynamic_proxy() || !ArchiveDynamicProxies) {
586 return;
587 }
588 init_dynamic_proxy_cache(CHECK);
589
590 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK);
591 TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS");
592 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;");
593
594 JavaCallArguments args;
595 args.push_oop(Handle(THREAD, loader()));
596 args.push_oop(Handle(THREAD, proxy_name()));
597 args.push_oop(Handle(THREAD, interfaces()));
598 args.push_int(access_flags);
599 JavaValue result(T_OBJECT);
600 JavaCalls::call_static(&result,
601 klass,
602 method,
603 signature,
604 &args, CHECK);
605
606 // Assumptions:
607 // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified.
608 // All named modules are loaded from the system modules files.
609 // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed.
610 // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived.
611 // The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which
612 // uses a null ProtectionDomain for this class)
613 oop mirror = result.get_oop();
614 assert(mirror != nullptr, "class must have been generated if not OOM");
615 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
616 if (ik->is_shared_boot_class() || ik->is_shared_platform_class()) {
617 assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported");
618 ik->set_shared_classpath_index(0);
619 } else {
620 assert(ik->is_shared_app_class(), "must be");
621 ik->set_shared_classpath_index(ClassLoaderExt::app_class_paths_start_index());
622 }
623
624 ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
625 if (log_is_enabled(Info, cds, dynamic, proxy)) {
626 ResourceMark rm(THREAD);
627 stringStream ss;
628 const char* prefix = "";
629 ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(),
630 ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index());
631 objArrayOop intfs = (objArrayOop)interfaces();
632 for (int i = 0; i < intfs->length(); i++) {
633 oop intf_mirror = intfs->obj_at(i);
634 ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name());
635 prefix = ", ";
636 }
637
638 log_info(cds, dynamic, proxy)("%s", ss.freeze());
639 }
640 }
|