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