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/archiveHeapWriter.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveBuilder.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classPrelinker.hpp"
31 #include "cds/heapShared.hpp"
32 #include "classfile/classLoaderData.hpp"
33 #include "classfile/javaClasses.inline.hpp"
34 #include "classfile/metadataOnStackMark.hpp"
35 #include "classfile/stringTable.hpp"
36 #include "classfile/systemDictionary.hpp"
37 #include "classfile/vmClasses.hpp"
38 #include "classfile/vmSymbols.hpp"
39 #include "code/codeCache.hpp"
40 #include "interpreter/bootstrapInfo.hpp"
41 #include "interpreter/linkResolver.hpp"
42 #include "jvm.h"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "memory/allocation.inline.hpp"
46 #include "memory/metadataFactory.hpp"
47 #include "memory/metaspaceClosure.hpp"
48 #include "memory/oopFactory.hpp"
49 #include "memory/resourceArea.hpp"
50 #include "memory/universe.hpp"
51 #include "oops/array.hpp"
52 #include "oops/constantPool.inline.hpp"
53 #include "oops/cpCache.inline.hpp"
54 #include "oops/instanceKlass.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/objArrayKlass.hpp"
57 #include "oops/objArrayOop.inline.hpp"
58 #include "oops/oop.inline.hpp"
59 #include "oops/typeArrayOop.inline.hpp"
60 #include "prims/jvmtiExport.hpp"
61 #include "runtime/atomic.hpp"
62 #include "runtime/handles.inline.hpp"
63 #include "runtime/init.hpp"
64 #include "runtime/javaCalls.hpp"
65 #include "runtime/javaThread.hpp"
66 #include "runtime/signature.hpp"
67 #include "runtime/vframe.inline.hpp"
68 #include "utilities/checkedCast.hpp"
69 #include "utilities/copy.hpp"
70
71 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
72 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
73 int size = ConstantPool::size(length);
74 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
75 }
76
77 void ConstantPool::copy_fields(const ConstantPool* orig) {
78 // Preserve dynamic constant information from the original pool
79 if (orig->has_dynamic_constant()) {
80 set_has_dynamic_constant();
81 }
82
83 set_major_version(orig->major_version());
84 set_minor_version(orig->minor_version());
85
281 }
282
283 #if INCLUDE_CDS_JAVA_HEAP
284 // Returns the _resolved_reference array after removing unarchivable items from it.
285 // Returns null if this class is not supported, or _resolved_reference doesn't exist.
286 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() {
287 if (_cache == nullptr) {
288 return nullptr; // nothing to do
289 }
290
291 InstanceKlass *ik = pool_holder();
292 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
293 ik->is_shared_app_class())) {
294 // Archiving resolved references for classes from non-builtin loaders
295 // is not yet supported.
296 return nullptr;
297 }
298
299 objArrayOop rr = resolved_references();
300 if (rr != nullptr) {
301 ConstantPool* orig_pool = ArchiveBuilder::current()->get_source_addr(this);
302 objArrayOop scratch_rr = HeapShared::scratch_resolved_references(orig_pool);
303 Array<u2>* ref_map = reference_map();
304 int ref_map_len = ref_map == nullptr ? 0 : ref_map->length();
305 int rr_len = rr->length();
306 for (int i = 0; i < rr_len; i++) {
307 oop obj = rr->obj_at(i);
308 scratch_rr->obj_at_put(i, nullptr);
309 if (obj != nullptr && i < ref_map_len) {
310 int index = object_to_cp_index(i);
311 if (tag_at(index).is_string()) {
312 assert(java_lang_String::is_instance(obj), "must be");
313 if (!ArchiveHeapWriter::is_string_too_large_to_archive(obj)) {
314 scratch_rr->obj_at_put(i, obj);
315 }
316 }
317 }
318 }
319 return scratch_rr;
320 }
321 return rr;
322 }
323
324 void ConstantPool::add_dumped_interned_strings() {
325 objArrayOop rr = resolved_references();
326 if (rr != nullptr) {
327 int rr_len = rr->length();
328 for (int i = 0; i < rr_len; i++) {
329 oop p = rr->obj_at(i);
330 if (java_lang_String::is_instance(p) &&
331 !ArchiveHeapWriter::is_string_too_large_to_archive(p)) {
332 HeapShared::add_to_dumped_interned_strings(p);
333 }
334 }
335 }
336 }
337 #endif
338
339 #if INCLUDE_CDS
340 // CDS support. Create a new resolved_references array.
341 void ConstantPool::restore_unshareable_info(TRAPS) {
342 if (!_pool_holder->is_linked() && !_pool_holder->is_rewritten()) {
343 return;
344 }
345 assert(is_constantPool(), "ensure C++ vtable is restored");
346 assert(on_stack(), "should always be set for shared constant pools");
347 assert(is_shared(), "should always be set for shared constant pools");
348 assert(_cache != nullptr, "constant pool _cache should not be null");
349
350 // Only create the new resolved references array if it hasn't been attempted before
351 if (resolved_references() != nullptr) return;
352
353 if (vmClasses::Object_klass_loaded()) {
354 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
355 #if INCLUDE_CDS_JAVA_HEAP
356 if (ArchiveHeapLoader::is_in_use() &&
357 _cache->archived_references() != nullptr) {
358 oop archived = _cache->archived_references();
359 // Create handle for the archived resolved reference array object
360 HandleMark hm(THREAD);
361 Handle refs_handle(THREAD, archived);
362 set_resolved_references(loader_data->add_handle(refs_handle));
363 _cache->clear_archived_references();
364 } else
365 #endif
366 {
367 // No mapped archived resolved reference array
368 // Recreate the object array and add to ClassLoaderData.
369 int map_length = resolved_reference_length();
370 if (map_length > 0) {
371 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
372 HandleMark hm(THREAD);
373 Handle refs_handle(THREAD, stom); // must handleize.
374 set_resolved_references(loader_data->add_handle(refs_handle));
375 }
376 }
377 }
378 }
379
380 void ConstantPool::remove_unshareable_info() {
381 // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
382 // The _on_stack flag is used to prevent ConstantPools from deallocation during
383 // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
384 // we always set _on_stack to true to avoid having to change _flags during runtime.
385 _flags |= (_on_stack | _is_shared);
386
387 if (!_pool_holder->is_linked() && !_pool_holder->verified_at_dump_time()) {
388 return;
389 }
390 // Resolved references are not in the shared archive.
391 // Save the length for restoration. It is not necessarily the same length
392 // as reference_map.length() if invokedynamic is saved. It is needed when
393 // re-creating the resolved reference array if archived heap data cannot be map
394 // at runtime.
395 set_resolved_reference_length(
396 resolved_references() != nullptr ? resolved_references()->length() : 0);
397 set_resolved_references(OopHandle());
398
399 bool archived = false;
400 for (int cp_index = 1; cp_index < length(); cp_index++) { // cp_index 0 is unused
401 switch (tag_at(cp_index).value()) {
402 case JVM_CONSTANT_UnresolvedClassInError:
403 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
404 break;
405 case JVM_CONSTANT_MethodHandleInError:
406 tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
407 break;
408 case JVM_CONSTANT_MethodTypeInError:
409 tag_at_put(cp_index, JVM_CONSTANT_MethodType);
410 break;
411 case JVM_CONSTANT_DynamicInError:
412 tag_at_put(cp_index, JVM_CONSTANT_Dynamic);
413 break;
414 case JVM_CONSTANT_Class:
415 archived = maybe_archive_resolved_klass_at(cp_index);
416 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(archived);
417 break;
418 }
419 }
420
421 if (cache() != nullptr) {
422 // cache() is null if this class is not yet linked.
423 cache()->remove_unshareable_info();
424 }
425 }
426
427 bool ConstantPool::maybe_archive_resolved_klass_at(int cp_index) {
428 assert(ArchiveBuilder::current()->is_in_buffer_space(this), "must be");
429 assert(tag_at(cp_index).is_klass(), "must be resolved");
430
431 if (pool_holder()->is_hidden() && cp_index == pool_holder()->this_class_index()) {
432 // All references to a hidden class's own field/methods are through this
433 // index, which was resolved in ClassFileParser::fill_instance_klass. We
434 // must preserve it.
435 return true;
436 }
437
438 CPKlassSlot kslot = klass_slot_at(cp_index);
439 int resolved_klass_index = kslot.resolved_klass_index();
440 Klass* k = resolved_klasses()->at(resolved_klass_index);
441 // k could be null if the referenced class has been excluded via
442 // SystemDictionaryShared::is_excluded_class().
443
444 if (k != nullptr) {
445 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
446 if (ClassPrelinker::can_archive_resolved_klass(src_cp, cp_index)) {
447 if (log_is_enabled(Debug, cds, resolve)) {
448 ResourceMark rm;
449 log_debug(cds, resolve)("Resolved klass CP entry [%d]: %s => %s", cp_index,
450 pool_holder()->external_name(), k->external_name());
451 }
452 return true;
453 }
454 }
455
456 // This referenced class cannot be archived. Revert the tag to UnresolvedClass,
457 // so that the proper class loading and initialization can happen at runtime.
458 resolved_klasses()->at_put(resolved_klass_index, nullptr);
459 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
460 return false;
461 }
462 #endif // INCLUDE_CDS
463
464 int ConstantPool::cp_to_object_index(int cp_index) {
465 // this is harder don't do this so much.
466 int i = reference_map()->find(checked_cast<u2>(cp_index));
467 // We might not find the index for jsr292 call.
468 return (i < 0) ? _no_index_sentinel : i;
469 }
470
471 void ConstantPool::string_at_put(int obj_index, oop str) {
472 oop result = set_resolved_reference_at(obj_index, str);
473 assert(result == nullptr || result == str, "Only set once or to the same string.");
474 }
475
476 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
477 ResourceMark rm;
478 int line_number = -1;
479 const char * source_file = nullptr;
480 if (JavaThread::current()->has_last_Java_frame()) {
682
683 // Translate index, which could be CPCache index or Indy index, to a constant pool index
684 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
685 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
686 switch(code) {
687 case Bytecodes::_invokedynamic:
688 return invokedynamic_bootstrap_ref_index_at(index);
689 case Bytecodes::_getfield:
690 case Bytecodes::_getstatic:
691 case Bytecodes::_putfield:
692 case Bytecodes::_putstatic:
693 return resolved_field_entry_at(index)->constant_pool_index();
694 case Bytecodes::_invokeinterface:
695 case Bytecodes::_invokehandle:
696 case Bytecodes::_invokespecial:
697 case Bytecodes::_invokestatic:
698 case Bytecodes::_invokevirtual:
699 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
700 return resolved_method_entry_at(index)->constant_pool_index();
701 default:
702 tty->print_cr("Unexpected bytecode: %d", code);
703 ShouldNotReachHere(); // All cases should have been handled
704 return -1;
705 }
706 }
707
708 u2 ConstantPool::uncached_name_and_type_ref_index_at(int cp_index) {
709 if (tag_at(cp_index).has_bootstrap()) {
710 u2 pool_index = bootstrap_name_and_type_ref_index_at(cp_index);
711 assert(tag_at(pool_index).is_name_and_type(), "");
712 return pool_index;
713 }
714 assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool");
715 assert(!tag_at(cp_index).has_bootstrap(), "Must be handled above");
716 jint ref_index = *int_at_addr(cp_index);
717 return extract_high_short_from_int(ref_index);
718 }
719
720 u2 ConstantPool::name_and_type_ref_index_at(int index, Bytecodes::Code code) {
721 return uncached_name_and_type_ref_index_at(to_cp_index(index, code));
722 }
723
724 constantTag ConstantPool::tag_ref_at(int which, Bytecodes::Code code) {
997 (*status_return) = false;
998 return nullptr;
999 }
1000 // from now on there is either success or an OOME
1001 (*status_return) = true;
1002 }
1003
1004 switch (tag.value()) {
1005
1006 case JVM_CONSTANT_UnresolvedClass:
1007 case JVM_CONSTANT_Class:
1008 {
1009 assert(cache_index == _no_index_sentinel, "should not have been set");
1010 Klass* resolved = klass_at_impl(this_cp, cp_index, CHECK_NULL);
1011 // ldc wants the java mirror.
1012 result_oop = resolved->java_mirror();
1013 break;
1014 }
1015
1016 case JVM_CONSTANT_Dynamic:
1017 {
1018 // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1019 BootstrapInfo bootstrap_specifier(this_cp, cp_index);
1020
1021 // The initial step in resolving an unresolved symbolic reference to a
1022 // dynamically-computed constant is to resolve the symbolic reference to a
1023 // method handle which will be the bootstrap method for the dynamically-computed
1024 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1025 // method fails, then a MethodHandleInError is stored at the corresponding
1026 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1027 // set a DynamicConstantInError here since any subsequent use of this
1028 // bootstrap method will encounter the resolution of MethodHandleInError.
1029 // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1030 // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1031 // for the bootstrap_specifier created above.
1032 SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1033 Exceptions::wrap_dynamic_exception(/* is_indy */ false, THREAD);
1034 if (HAS_PENDING_EXCEPTION) {
1035 // Resolution failure of the dynamically-computed constant, save_and_throw_exception
1036 // will check for a LinkageError and store a DynamicConstantInError.
1037 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1055 // but do not save a DynamicInError resolution result.
1056 // See section 5.4.3 of the VM spec.
1057 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail);
1058 }
1059 }
1060
1061 LogTarget(Debug, methodhandles, condy) lt_condy;
1062 if (lt_condy.is_enabled()) {
1063 LogStream ls(lt_condy);
1064 bootstrap_specifier.print_msg_on(&ls, "resolve_constant_at_impl");
1065 }
1066 break;
1067 }
1068
1069 case JVM_CONSTANT_String:
1070 assert(cache_index != _no_index_sentinel, "should have been set");
1071 result_oop = string_at_impl(this_cp, cp_index, cache_index, CHECK_NULL);
1072 break;
1073
1074 case JVM_CONSTANT_MethodHandle:
1075 {
1076 int ref_kind = this_cp->method_handle_ref_kind_at(cp_index);
1077 int callee_index = this_cp->method_handle_klass_index_at(cp_index);
1078 Symbol* name = this_cp->method_handle_name_ref_at(cp_index);
1079 Symbol* signature = this_cp->method_handle_signature_ref_at(cp_index);
1080 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(cp_index));
1081 { ResourceMark rm(THREAD);
1082 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
1083 ref_kind, cp_index, this_cp->method_handle_index_at(cp_index),
1084 callee_index, name->as_C_string(), signature->as_C_string());
1085 }
1086
1087 Klass* callee = klass_at_impl(this_cp, callee_index, THREAD);
1088 if (HAS_PENDING_EXCEPTION) {
1089 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1090 }
1091
1092 // Check constant pool method consistency
1093 if ((callee->is_interface() && m_tag.is_method()) ||
1094 (!callee->is_interface() && m_tag.is_interface_method())) {
1095 ResourceMark rm(THREAD);
1103 cp_index,
1104 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
1105 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
1106 Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IncompatibleClassChangeError(), "%s", ss.as_string());
1107 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1108 }
1109
1110 Klass* klass = this_cp->pool_holder();
1111 HandleMark hm(THREAD);
1112 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
1113 callee, name, signature,
1114 THREAD);
1115 if (HAS_PENDING_EXCEPTION) {
1116 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1117 }
1118 result_oop = value();
1119 break;
1120 }
1121
1122 case JVM_CONSTANT_MethodType:
1123 {
1124 Symbol* signature = this_cp->method_type_signature_at(cp_index);
1125 { ResourceMark rm(THREAD);
1126 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
1127 cp_index, this_cp->method_type_index_at(cp_index),
1128 signature->as_C_string());
1129 }
1130 Klass* klass = this_cp->pool_holder();
1131 HandleMark hm(THREAD);
1132 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
1133 result_oop = value();
1134 if (HAS_PENDING_EXCEPTION) {
1135 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1136 }
1137 break;
1138 }
1139
1140 case JVM_CONSTANT_Integer:
1141 assert(cache_index == _no_index_sentinel, "should not have been set");
1142 prim_value.i = this_cp->int_at(cp_index);
1143 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
|
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/archiveHeapWriter.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveBuilder.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classPrelinker.hpp"
31 #include "cds/lambdaFormInvokers.inline.hpp"
32 #include "cds/heapShared.hpp"
33 #include "classfile/classLoader.hpp"
34 #include "classfile/classLoaderData.hpp"
35 #include "classfile/javaClasses.inline.hpp"
36 #include "classfile/metadataOnStackMark.hpp"
37 #include "classfile/stringTable.hpp"
38 #include "classfile/systemDictionary.hpp"
39 #include "classfile/systemDictionaryShared.hpp"
40 #include "classfile/vmClasses.hpp"
41 #include "classfile/vmSymbols.hpp"
42 #include "code/codeCache.hpp"
43 #include "interpreter/bootstrapInfo.hpp"
44 #include "interpreter/linkResolver.hpp"
45 #include "jvm.h"
46 #include "logging/log.hpp"
47 #include "logging/logStream.hpp"
48 #include "memory/allocation.inline.hpp"
49 #include "memory/metadataFactory.hpp"
50 #include "memory/metaspaceClosure.hpp"
51 #include "memory/oopFactory.hpp"
52 #include "memory/resourceArea.hpp"
53 #include "memory/universe.hpp"
54 #include "oops/array.hpp"
55 #include "oops/constantPool.inline.hpp"
56 #include "oops/cpCache.inline.hpp"
57 #include "oops/instanceKlass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/typeArrayOop.inline.hpp"
63 #include "prims/jvmtiExport.hpp"
64 #include "runtime/atomic.hpp"
65 //#include "runtime/fieldDescriptor.inline.hpp"
66 #include "runtime/handles.inline.hpp"
67 #include "runtime/init.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/javaThread.inline.hpp"
70 #include "runtime/perfData.hpp"
71 #include "runtime/signature.hpp"
72 #include "runtime/vframe.inline.hpp"
73 #include "utilities/checkedCast.hpp"
74 #include "utilities/copy.hpp"
75
76 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
77 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
78 int size = ConstantPool::size(length);
79 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
80 }
81
82 void ConstantPool::copy_fields(const ConstantPool* orig) {
83 // Preserve dynamic constant information from the original pool
84 if (orig->has_dynamic_constant()) {
85 set_has_dynamic_constant();
86 }
87
88 set_major_version(orig->major_version());
89 set_minor_version(orig->minor_version());
90
286 }
287
288 #if INCLUDE_CDS_JAVA_HEAP
289 // Returns the _resolved_reference array after removing unarchivable items from it.
290 // Returns null if this class is not supported, or _resolved_reference doesn't exist.
291 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() {
292 if (_cache == nullptr) {
293 return nullptr; // nothing to do
294 }
295
296 InstanceKlass *ik = pool_holder();
297 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
298 ik->is_shared_app_class())) {
299 // Archiving resolved references for classes from non-builtin loaders
300 // is not yet supported.
301 return nullptr;
302 }
303
304 objArrayOop rr = resolved_references();
305 if (rr != nullptr) {
306 ResourceMark rm;
307 int rr_len = rr->length();
308 GrowableArray<bool> keep_resolved_refs(rr_len, rr_len, false);
309 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
310
311 if (cache() != nullptr && ArchiveInvokeDynamic) {
312 Array<ResolvedIndyEntry>* indy_entries = cache()->resolved_indy_entries();
313 if (indy_entries != nullptr) {
314 for (int i = 0; i < indy_entries->length(); i++) {
315 ResolvedIndyEntry *rie = indy_entries->adr_at(i);
316 if (rie->is_resolved() && ClassPrelinker::is_resolution_deterministic(src_cp, rie->constant_pool_index())) {
317 int rr_index = rie->resolved_references_index();
318 keep_resolved_refs.at_put(rr_index, true);
319 }
320 }
321 }
322
323 Array<ResolvedMethodEntry>* method_entries = cache()->resolved_method_entries();
324 if (method_entries != nullptr) {
325 for (int i = 0; i < method_entries->length(); i++) {
326 ResolvedMethodEntry* rme = method_entries->adr_at(i);
327 if (rme->is_resolved(Bytecodes::_invokehandle) && rme->has_appendix() &&
328 can_archive_resolved_method(rme)) {
329 int rr_index = rme->resolved_references_index();
330 keep_resolved_refs.at_put(rr_index, true);
331 }
332 }
333 }
334 }
335
336 objArrayOop scratch_rr = HeapShared::scratch_resolved_references(src_cp);
337 Array<u2>* ref_map = reference_map();
338 int ref_map_len = ref_map == nullptr ? 0 : ref_map->length();
339 for (int i = 0; i < rr_len; i++) {
340 oop obj = rr->obj_at(i);
341 scratch_rr->obj_at_put(i, nullptr);
342 if (obj != nullptr) {
343 if (i < ref_map_len) {
344 int index = object_to_cp_index(i);
345 if (tag_at(index).is_string()) {
346 assert(java_lang_String::is_instance(obj), "must be");
347 if (!ArchiveHeapWriter::is_string_too_large_to_archive(obj)) {
348 scratch_rr->obj_at_put(i, obj);
349 }
350 }
351 } else if (keep_resolved_refs.at(i)) {
352 scratch_rr->obj_at_put(i, obj);
353 }
354 }
355 }
356 return scratch_rr;
357 }
358 return rr;
359 }
360
361 void ConstantPool::add_dumped_interned_strings() {
362 objArrayOop rr = resolved_references();
363 if (rr != nullptr) {
364 int rr_len = rr->length();
365 for (int i = 0; i < rr_len; i++) {
366 oop p = rr->obj_at(i);
367 if (java_lang_String::is_instance(p) &&
368 !ArchiveHeapWriter::is_string_too_large_to_archive(p)) {
369 HeapShared::add_to_dumped_interned_strings(p);
370 }
371 }
372 }
373 }
374 #endif
375
376 #if INCLUDE_CDS
377 // CDS support. Create a new resolved_references array.
378 void ConstantPool::restore_unshareable_info(TRAPS) {
379 if (!_pool_holder->is_linked() && !_pool_holder->is_rewritten()) {
380 return;
381 }
382 assert(is_constantPool(), "ensure C++ vtable is restored");
383 assert(on_stack(), "should always be set for shared constant pools");
384 assert(is_shared(), "should always be set for shared constant pools");
385 if (is_for_method_handle_intrinsic()) {
386 return;
387 }
388 assert(_cache != nullptr, "constant pool _cache should not be null");
389
390 // Only create the new resolved references array if it hasn't been attempted before
391 if (resolved_references() != nullptr) return;
392
393 if (vmClasses::Object_klass_loaded()) {
394 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
395 #if INCLUDE_CDS_JAVA_HEAP
396 if (ArchiveHeapLoader::is_in_use() &&
397 _cache->archived_references() != nullptr) {
398 oop archived = _cache->archived_references();
399 // Create handle for the archived resolved reference array object
400 HandleMark hm(THREAD);
401 Handle refs_handle(THREAD, archived);
402 set_resolved_references(loader_data->add_handle(refs_handle));
403 _cache->clear_archived_references();
404 } else
405 #endif
406 {
407 // No mapped archived resolved reference array
408 // Recreate the object array and add to ClassLoaderData.
409 int map_length = resolved_reference_length();
410 if (map_length > 0) {
411 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
412 HandleMark hm(THREAD);
413 Handle refs_handle(THREAD, stom); // must handleize.
414 set_resolved_references(loader_data->add_handle(refs_handle));
415 }
416 }
417 }
418
419 if (CDSConfig::is_dumping_final_static_archive() && resolved_references() != nullptr) {
420 objArrayOop scratch_references = oopFactory::new_objArray(vmClasses::Object_klass(), resolved_references()->length(), CHECK);
421 HeapShared::add_scratch_resolved_references(this, scratch_references);
422 }
423 }
424
425 void ConstantPool::remove_unshareable_info() {
426 // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
427 // The _on_stack flag is used to prevent ConstantPools from deallocation during
428 // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
429 // we always set _on_stack to true to avoid having to change _flags during runtime.
430 _flags |= (_on_stack | _is_shared);
431
432 if (!ArchiveBuilder::current()->get_source_addr(_pool_holder)->is_linked()) {
433 return;
434 }
435
436 if (is_for_method_handle_intrinsic()) {
437 // This CP was created by Method::make_method_handle_intrinsic() and has nothing
438 // that need to be removed/restored. It has no cpCache since the intrinsic methods
439 // don't have any bytecodes.
440 assert(cache() == NULL, "must not have cpCache");
441 return;
442 }
443
444 // Resolved references are not in the shared archive.
445 // Save the length for restoration. It is not necessarily the same length
446 // as reference_map.length() if invokedynamic is saved. It is needed when
447 // re-creating the resolved reference array if archived heap data cannot be map
448 // at runtime.
449 set_resolved_reference_length(
450 resolved_references() != nullptr ? resolved_references()->length() : 0);
451 set_resolved_references(OopHandle());
452
453 remove_unshareable_entries();
454 }
455
456 static const char* get_type(Klass* k) {
457 const char* type;
458 Klass* src_k;
459 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
460 src_k = ArchiveBuilder::current()->get_source_addr(k);
461 } else {
462 src_k = k;
463 }
464
465 if (src_k->is_objArray_klass()) {
466 src_k = ObjArrayKlass::cast(src_k)->bottom_klass();
467 assert(!src_k->is_objArray_klass(), "sanity");
468 }
469
470 if (src_k->is_typeArray_klass()) {
471 type = "prim";
472 } else {
473 InstanceKlass* src_ik = InstanceKlass::cast(src_k);
474 oop loader = src_ik->class_loader();
475 if (loader == nullptr) {
476 type = "boot";
477 } else if (loader == SystemDictionary::java_platform_loader()) {
478 type = "plat";
479 } else if (loader == SystemDictionary::java_system_loader()) {
480 type = "app";
481 } else {
482 type = "unreg";
483 }
484 }
485
486 return type;
487 }
488
489 void ConstantPool::remove_unshareable_entries() {
490 ResourceMark rm;
491 log_info(cds, resolve)("Archiving CP entries for %s", pool_holder()->name()->as_C_string());
492 for (int cp_index = 1; cp_index < length(); cp_index++) { // cp_index 0 is unused
493 int cp_tag = tag_at(cp_index).value();
494 switch (cp_tag) {
495 case JVM_CONSTANT_UnresolvedClass:
496 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false);
497 break;
498 case JVM_CONSTANT_UnresolvedClassInError:
499 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
500 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false);
501 break;
502 case JVM_CONSTANT_MethodHandleInError:
503 tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
504 break;
505 case JVM_CONSTANT_MethodTypeInError:
506 tag_at_put(cp_index, JVM_CONSTANT_MethodType);
507 break;
508 case JVM_CONSTANT_DynamicInError:
509 tag_at_put(cp_index, JVM_CONSTANT_Dynamic);
510 break;
511 case JVM_CONSTANT_Class:
512 remove_resolved_klass_if_non_deterministic(cp_index);
513 break;
514 default:
515 break;
516 }
517 }
518
519 if (cache() != nullptr) {
520 // cache() is null if this class is not yet linked.
521 remove_resolved_field_entries_if_non_deterministic();
522 remove_resolved_method_entries_if_non_deterministic();
523 remove_resolved_indy_entries_if_non_deterministic();
524 }
525 }
526
527 void ConstantPool::remove_resolved_klass_if_non_deterministic(int cp_index) {
528 assert(ArchiveBuilder::current()->is_in_buffer_space(this), "must be");
529 assert(tag_at(cp_index).is_klass(), "must be resolved");
530
531 // k could be null if the referenced class has been excluded via
532 // SystemDictionaryShared::is_excluded_class().
533 Klass* k = resolved_klass_at(cp_index);
534 bool revert = true;
535 if (k != nullptr) {
536 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
537 if (ClassPrelinker::is_resolution_deterministic(src_cp, cp_index)) {
538 revert = false;
539 }
540 }
541
542 if (revert) {
543 // This resolved klass entry cannot be archived. Revert the tag to UnresolvedClass,
544 // so that it will be resolved at runtime.
545 int resolved_klass_index = klass_slot_at(cp_index).resolved_klass_index();
546 resolved_klasses()->at_put(resolved_klass_index, nullptr);
547 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
548 }
549
550 LogStreamHandle(Trace, cds, resolve) log;
551 if (log.is_enabled()) {
552 ResourceMark rm;
553 log.print("%s klass CP entry [%3d]: %s %s",
554 (revert ? "reverted" : "archived"),
555 cp_index, pool_holder()->name()->as_C_string(), get_type(pool_holder()));
556 if (revert) {
557 Symbol* name = klass_name_at(cp_index);
558 log.print(" \"%s\"", name->as_C_string());
559 } else {
560 log.print(" => %s %s%s", k->name()->as_C_string(), get_type(k),
561 (!k->is_instance_klass() || pool_holder()->is_subtype_of(k)) ? "" : " (not supertype)");
562 }
563 }
564 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(!revert);
565 }
566
567 void ConstantPool::remove_resolved_field_entries_if_non_deterministic() {
568 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
569 Array<ResolvedFieldEntry>* field_entries = cache()->resolved_field_entries();
570 if (field_entries != nullptr) {
571 for (int i = 0; i < field_entries->length(); i++) {
572 ResolvedFieldEntry* rfi = field_entries->adr_at(i);
573 int cp_index = rfi->constant_pool_index();
574 bool archived = false;
575 bool resolved = rfi->is_resolved(Bytecodes::_getstatic) ||
576 rfi->is_resolved(Bytecodes::_putstatic) ||
577 rfi->is_resolved(Bytecodes::_putfield) ||
578 rfi->is_resolved(Bytecodes::_putfield);
579 if (resolved && ClassPrelinker::is_resolution_deterministic(src_cp, cp_index)) {
580 rfi->mark_and_relocate();
581 archived = true;
582 } else {
583 rfi->remove_unshareable_info();
584 }
585 if (resolved) {
586 LogStreamHandle(Trace, cds, resolve) log;
587 if (log.is_enabled()) {
588 ResourceMark rm;
589 int klass_cp_index = uncached_klass_ref_index_at(cp_index);
590 Symbol* klass_name = klass_name_at(klass_cp_index);
591 Symbol* name = uncached_name_ref_at(cp_index);
592 Symbol* signature = uncached_signature_ref_at(cp_index);
593 log.print("%s field CP entry [%3d]: %s %s %s.%s:%s",
594 (archived ? "archived" : "excluded"),
595 cp_index,
596 pool_holder()->name()->as_C_string(),
597 (archived ? "=>" : " "),
598 klass_name->as_C_string(), name->as_C_string(), signature->as_C_string());
599 }
600 }
601 ArchiveBuilder::alloc_stats()->record_field_cp_entry(archived, resolved && !archived);
602 }
603 }
604 }
605
606 void ConstantPool::remove_resolved_method_entries_if_non_deterministic() {
607 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
608 Array<ResolvedMethodEntry>* method_entries = cache()->resolved_method_entries();
609 if (method_entries != nullptr) {
610 for (int i = 0; i < method_entries->length(); i++) {
611 ResolvedMethodEntry* rme = method_entries->adr_at(i);
612 int cp_index = rme->constant_pool_index();
613 bool archived = false;
614 bool resolved = rme->is_resolved(Bytecodes::_invokevirtual) ||
615 rme->is_resolved(Bytecodes::_invokespecial) ||
616 rme->is_resolved(Bytecodes::_invokestatic) ||
617 rme->is_resolved(Bytecodes::_invokeinterface) ||
618 rme->is_resolved(Bytecodes::_invokehandle);
619 if (resolved && can_archive_resolved_method(rme)) {
620 rme->mark_and_relocate(src_cp);
621 archived = true;
622 } else {
623 rme->remove_unshareable_info();
624 }
625 if (resolved) {
626 LogStreamHandle(Trace, cds, resolve) log;
627 if (log.is_enabled()) {
628 ResourceMark rm;
629 int klass_cp_index = uncached_klass_ref_index_at(cp_index);
630 Symbol* klass_name = klass_name_at(klass_cp_index);
631 Symbol* name = uncached_name_ref_at(cp_index);
632 Symbol* signature = uncached_signature_ref_at(cp_index);
633 log.print("%s%s method CP entry [%3d]: %s %s.%s:%s",
634 (archived ? "archived" : "excluded"),
635 (rme->is_resolved(Bytecodes::_invokeinterface) ? " interface" : ""),
636 cp_index,
637 pool_holder()->name()->as_C_string(),
638 klass_name->as_C_string(), name->as_C_string(), signature->as_C_string());
639 if (archived) {
640 Klass* resolved_klass = resolved_klass_at(klass_cp_index);
641 log.print(" => %s%s",
642 resolved_klass->name()->as_C_string(),
643 (rme->is_resolved(Bytecodes::_invokestatic) ? " *** static" : ""));
644 }
645 }
646 }
647 ArchiveBuilder::alloc_stats()->record_method_cp_entry(archived, resolved && !archived);
648 }
649 }
650 }
651
652 void ConstantPool::remove_resolved_indy_entries_if_non_deterministic() {
653 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
654 Array<ResolvedIndyEntry>* indy_entries = cache()->resolved_indy_entries();
655 if (indy_entries != nullptr) {
656 for (int i = 0; i < indy_entries->length(); i++) {
657 ResolvedIndyEntry* rei = indy_entries->adr_at(i);
658 int cp_index = rei->constant_pool_index();
659 bool archived = false;
660 bool resolved = rei->is_resolved();
661 if (resolved && ClassPrelinker::is_resolution_deterministic(src_cp, cp_index)) {
662 rei->mark_and_relocate();
663 archived = true;
664 } else {
665 rei->remove_unshareable_info();
666 }
667 if (resolved) {
668 LogStreamHandle(Trace, cds, resolve) log;
669 if (log.is_enabled()) {
670 ResourceMark rm;
671 int bsm = bootstrap_method_ref_index_at(cp_index);
672 int bsm_ref = method_handle_index_at(bsm);
673 Symbol* bsm_name = uncached_name_ref_at(bsm_ref);
674 Symbol* bsm_signature = uncached_signature_ref_at(bsm_ref);
675 Symbol* bsm_klass = klass_name_at(uncached_klass_ref_index_at(bsm_ref));
676 log.print("%s indy CP entry [%3d]: %s (%d)",
677 (archived ? "archived" : "excluded"),
678 cp_index, pool_holder()->name()->as_C_string(), i);
679 log.print(" %s %s.%s:%s", (archived ? "=>" : " "), bsm_klass->as_C_string(), bsm_name->as_C_string(), bsm_signature->as_C_string());
680 }
681 }
682 ArchiveBuilder::alloc_stats()->record_indy_cp_entry(archived, resolved && !archived);
683 }
684 }
685 }
686
687 bool ConstantPool::can_archive_invokehandle(ResolvedMethodEntry* rme) {
688 assert(rme->is_resolved(Bytecodes::_invokehandle), "sanity");
689
690 int cp_index = rme->constant_pool_index();
691 int klass_cp_index = uncached_klass_ref_index_at(cp_index);
692 Klass* resolved_klass = resolved_klass_at(klass_cp_index);
693 if (!resolved_klass->is_instance_klass()) {
694 // FIXME: can this ever happen?
695 return false;
696 }
697 // FIXME -- any class referenced by the archived CP entries should be added to ArchiveBuilder::classes, or should be
698 // filtered out.
699 return true;
700 }
701
702 bool ConstantPool::can_archive_resolved_method(ResolvedMethodEntry* method_entry) {
703 if (!(pool_holder()->is_shared_boot_class() || pool_holder()->is_shared_platform_class() ||
704 pool_holder()->is_shared_app_class())) {
705 // Archiving resolved cp entries for classes from non-builtin loaders
706 // is not yet supported.
707 return false;
708 }
709
710 if (CDSConfig::is_dumping_dynamic_archive()) {
711 // InstanceKlass::methods() is has been resorted. We need to
712 // update the vtable_index in method_entry (not implemented)
713 return false;
714 }
715
716 if (!method_entry->is_resolved(Bytecodes::_invokevirtual)) {
717 if (method_entry->method() == nullptr) {
718 return false;
719 }
720 if (method_entry->method()->is_continuation_native_intrinsic()) {
721 return false; // FIXME: corresponding stub is generated on demand during method resolution (see LinkResolver::resolve_static_call).
722 }
723 }
724
725 int cp_index = method_entry->constant_pool_index();
726 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
727 assert(src_cp->tag_at(cp_index).is_method() || src_cp->tag_at(cp_index).is_interface_method(), "sanity");
728
729 if (!ClassPrelinker::is_resolution_deterministic(src_cp, cp_index)) {
730 return false;
731 }
732
733 if (method_entry->is_resolved(Bytecodes::_invokehandle)) {
734 if (!ArchiveInvokeDynamic) {
735 // invokehandle depends on archived MethodType and LambdaForms.
736 return false;
737 } else if (!can_archive_invokehandle(method_entry)) {
738 return false;
739 }
740 } else if (method_entry->is_resolved(Bytecodes::_invokestatic) ||
741 method_entry->is_resolved(Bytecodes::_invokeinterface) ||
742 method_entry->is_resolved(Bytecodes::_invokevirtual) ||
743 method_entry->is_resolved(Bytecodes::_invokespecial)) {
744 // OK
745 } else {
746 return false; // just to be safe.
747 }
748
749 return true;
750 }
751 #endif // INCLUDE_CDS
752
753 int ConstantPool::cp_to_object_index(int cp_index) {
754 // this is harder don't do this so much.
755 int i = reference_map()->find(checked_cast<u2>(cp_index));
756 // We might not find the index for jsr292 call.
757 return (i < 0) ? _no_index_sentinel : i;
758 }
759
760 void ConstantPool::string_at_put(int obj_index, oop str) {
761 oop result = set_resolved_reference_at(obj_index, str);
762 assert(result == nullptr || result == str, "Only set once or to the same string.");
763 }
764
765 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
766 ResourceMark rm;
767 int line_number = -1;
768 const char * source_file = nullptr;
769 if (JavaThread::current()->has_last_Java_frame()) {
971
972 // Translate index, which could be CPCache index or Indy index, to a constant pool index
973 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
974 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
975 switch(code) {
976 case Bytecodes::_invokedynamic:
977 return invokedynamic_bootstrap_ref_index_at(index);
978 case Bytecodes::_getfield:
979 case Bytecodes::_getstatic:
980 case Bytecodes::_putfield:
981 case Bytecodes::_putstatic:
982 return resolved_field_entry_at(index)->constant_pool_index();
983 case Bytecodes::_invokeinterface:
984 case Bytecodes::_invokehandle:
985 case Bytecodes::_invokespecial:
986 case Bytecodes::_invokestatic:
987 case Bytecodes::_invokevirtual:
988 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
989 return resolved_method_entry_at(index)->constant_pool_index();
990 default:
991 fatal("Unexpected bytecode: %s", Bytecodes::name(code));
992 }
993 }
994
995 bool ConstantPool::is_resolved(int index, Bytecodes::Code code) {
996 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
997 switch(code) {
998 case Bytecodes::_invokedynamic:
999 return resolved_indy_entry_at(index)->is_resolved();
1000
1001 case Bytecodes::_getfield:
1002 case Bytecodes::_getstatic:
1003 case Bytecodes::_putfield:
1004 case Bytecodes::_putstatic:
1005 return resolved_field_entry_at(index)->is_resolved(code);
1006
1007 case Bytecodes::_invokeinterface:
1008 case Bytecodes::_invokehandle:
1009 case Bytecodes::_invokespecial:
1010 case Bytecodes::_invokestatic:
1011 case Bytecodes::_invokevirtual:
1012 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
1013 return resolved_method_entry_at(index)->is_resolved(code);
1014
1015 default:
1016 fatal("Unexpected bytecode: %s", Bytecodes::name(code));
1017 }
1018 }
1019
1020 u2 ConstantPool::uncached_name_and_type_ref_index_at(int cp_index) {
1021 if (tag_at(cp_index).has_bootstrap()) {
1022 u2 pool_index = bootstrap_name_and_type_ref_index_at(cp_index);
1023 assert(tag_at(pool_index).is_name_and_type(), "");
1024 return pool_index;
1025 }
1026 assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool");
1027 assert(!tag_at(cp_index).has_bootstrap(), "Must be handled above");
1028 jint ref_index = *int_at_addr(cp_index);
1029 return extract_high_short_from_int(ref_index);
1030 }
1031
1032 u2 ConstantPool::name_and_type_ref_index_at(int index, Bytecodes::Code code) {
1033 return uncached_name_and_type_ref_index_at(to_cp_index(index, code));
1034 }
1035
1036 constantTag ConstantPool::tag_ref_at(int which, Bytecodes::Code code) {
1309 (*status_return) = false;
1310 return nullptr;
1311 }
1312 // from now on there is either success or an OOME
1313 (*status_return) = true;
1314 }
1315
1316 switch (tag.value()) {
1317
1318 case JVM_CONSTANT_UnresolvedClass:
1319 case JVM_CONSTANT_Class:
1320 {
1321 assert(cache_index == _no_index_sentinel, "should not have been set");
1322 Klass* resolved = klass_at_impl(this_cp, cp_index, CHECK_NULL);
1323 // ldc wants the java mirror.
1324 result_oop = resolved->java_mirror();
1325 break;
1326 }
1327
1328 case JVM_CONSTANT_Dynamic:
1329 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokedynamic_time(),
1330 ClassLoader::perf_resolve_invokedynamic_count(),
1331 THREAD->class_being_initialized() == nullptr);
1332 // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1333 BootstrapInfo bootstrap_specifier(this_cp, cp_index);
1334
1335 // The initial step in resolving an unresolved symbolic reference to a
1336 // dynamically-computed constant is to resolve the symbolic reference to a
1337 // method handle which will be the bootstrap method for the dynamically-computed
1338 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1339 // method fails, then a MethodHandleInError is stored at the corresponding
1340 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1341 // set a DynamicConstantInError here since any subsequent use of this
1342 // bootstrap method will encounter the resolution of MethodHandleInError.
1343 // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1344 // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1345 // for the bootstrap_specifier created above.
1346 SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1347 Exceptions::wrap_dynamic_exception(/* is_indy */ false, THREAD);
1348 if (HAS_PENDING_EXCEPTION) {
1349 // Resolution failure of the dynamically-computed constant, save_and_throw_exception
1350 // will check for a LinkageError and store a DynamicConstantInError.
1351 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1369 // but do not save a DynamicInError resolution result.
1370 // See section 5.4.3 of the VM spec.
1371 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail);
1372 }
1373 }
1374
1375 LogTarget(Debug, methodhandles, condy) lt_condy;
1376 if (lt_condy.is_enabled()) {
1377 LogStream ls(lt_condy);
1378 bootstrap_specifier.print_msg_on(&ls, "resolve_constant_at_impl");
1379 }
1380 break;
1381 }
1382
1383 case JVM_CONSTANT_String:
1384 assert(cache_index != _no_index_sentinel, "should have been set");
1385 result_oop = string_at_impl(this_cp, cp_index, cache_index, CHECK_NULL);
1386 break;
1387
1388 case JVM_CONSTANT_MethodHandle:
1389 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_handle_time(),
1390 ClassLoader::perf_resolve_method_handle_count(),
1391 THREAD->class_being_initialized() == nullptr);
1392 int ref_kind = this_cp->method_handle_ref_kind_at(cp_index);
1393 int callee_index = this_cp->method_handle_klass_index_at(cp_index);
1394 Symbol* name = this_cp->method_handle_name_ref_at(cp_index);
1395 Symbol* signature = this_cp->method_handle_signature_ref_at(cp_index);
1396 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(cp_index));
1397 { ResourceMark rm(THREAD);
1398 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
1399 ref_kind, cp_index, this_cp->method_handle_index_at(cp_index),
1400 callee_index, name->as_C_string(), signature->as_C_string());
1401 }
1402
1403 Klass* callee = klass_at_impl(this_cp, callee_index, THREAD);
1404 if (HAS_PENDING_EXCEPTION) {
1405 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1406 }
1407
1408 // Check constant pool method consistency
1409 if ((callee->is_interface() && m_tag.is_method()) ||
1410 (!callee->is_interface() && m_tag.is_interface_method())) {
1411 ResourceMark rm(THREAD);
1419 cp_index,
1420 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
1421 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
1422 Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IncompatibleClassChangeError(), "%s", ss.as_string());
1423 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1424 }
1425
1426 Klass* klass = this_cp->pool_holder();
1427 HandleMark hm(THREAD);
1428 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
1429 callee, name, signature,
1430 THREAD);
1431 if (HAS_PENDING_EXCEPTION) {
1432 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1433 }
1434 result_oop = value();
1435 break;
1436 }
1437
1438 case JVM_CONSTANT_MethodType:
1439 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_type_time(),
1440 ClassLoader::perf_resolve_method_type_count(),
1441 THREAD->class_being_initialized() == nullptr);
1442 Symbol* signature = this_cp->method_type_signature_at(cp_index);
1443 { ResourceMark rm(THREAD);
1444 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
1445 cp_index, this_cp->method_type_index_at(cp_index),
1446 signature->as_C_string());
1447 }
1448 Klass* klass = this_cp->pool_holder();
1449 HandleMark hm(THREAD);
1450 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
1451 result_oop = value();
1452 if (HAS_PENDING_EXCEPTION) {
1453 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1454 }
1455 break;
1456 }
1457
1458 case JVM_CONSTANT_Integer:
1459 assert(cache_index == _no_index_sentinel, "should not have been set");
1460 prim_value.i = this_cp->int_at(cp_index);
1461 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
|