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