< prev index next >

src/hotspot/share/oops/constantPool.cpp

Print this page

  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()) {
 481     // try to identify the method which called this function.

1000       (*status_return) = false;
1001       return nullptr;
1002     }
1003     // from now on there is either success or an OOME
1004     (*status_return) = true;
1005   }
1006 
1007   switch (tag.value()) {
1008 
1009   case JVM_CONSTANT_UnresolvedClass:
1010   case JVM_CONSTANT_Class:
1011     {
1012       assert(cache_index == _no_index_sentinel, "should not have been set");
1013       Klass* resolved = klass_at_impl(this_cp, cp_index, CHECK_NULL);
1014       // ldc wants the java mirror.
1015       result_oop = resolved->java_mirror();
1016       break;
1017     }
1018 
1019   case JVM_CONSTANT_Dynamic:
1020     {


1021       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1022       BootstrapInfo bootstrap_specifier(this_cp, cp_index);
1023 
1024       // The initial step in resolving an unresolved symbolic reference to a
1025       // dynamically-computed constant is to resolve the symbolic reference to a
1026       // method handle which will be the bootstrap method for the dynamically-computed
1027       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1028       // method fails, then a MethodHandleInError is stored at the corresponding
1029       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1030       // set a DynamicConstantInError here since any subsequent use of this
1031       // bootstrap method will encounter the resolution of MethodHandleInError.
1032       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1033       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1034       // for the bootstrap_specifier created above.
1035       SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1036       Exceptions::wrap_dynamic_exception(/* is_indy */ false, THREAD);
1037       if (HAS_PENDING_EXCEPTION) {
1038         // Resolution failure of the dynamically-computed constant, save_and_throw_exception
1039         // will check for a LinkageError and store a DynamicConstantInError.
1040         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);

1058           // but do not save a DynamicInError resolution result.
1059           // See section 5.4.3 of the VM spec.
1060           THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail);
1061         }
1062       }
1063 
1064       LogTarget(Debug, methodhandles, condy) lt_condy;
1065       if (lt_condy.is_enabled()) {
1066         LogStream ls(lt_condy);
1067         bootstrap_specifier.print_msg_on(&ls, "resolve_constant_at_impl");
1068       }
1069       break;
1070     }
1071 
1072   case JVM_CONSTANT_String:
1073     assert(cache_index != _no_index_sentinel, "should have been set");
1074     result_oop = string_at_impl(this_cp, cp_index, cache_index, CHECK_NULL);
1075     break;
1076 
1077   case JVM_CONSTANT_MethodHandle:
1078     {


1079       int ref_kind                 = this_cp->method_handle_ref_kind_at(cp_index);
1080       int callee_index             = this_cp->method_handle_klass_index_at(cp_index);
1081       Symbol*  name =      this_cp->method_handle_name_ref_at(cp_index);
1082       Symbol*  signature = this_cp->method_handle_signature_ref_at(cp_index);
1083       constantTag m_tag  = this_cp->tag_at(this_cp->method_handle_index_at(cp_index));
1084       { ResourceMark rm(THREAD);
1085         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
1086                               ref_kind, cp_index, this_cp->method_handle_index_at(cp_index),
1087                               callee_index, name->as_C_string(), signature->as_C_string());
1088       }
1089 
1090       Klass* callee = klass_at_impl(this_cp, callee_index, THREAD);
1091       if (HAS_PENDING_EXCEPTION) {
1092         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1093       }
1094 
1095       // Check constant pool method consistency
1096       if ((callee->is_interface() && m_tag.is_method()) ||
1097           (!callee->is_interface() && m_tag.is_interface_method())) {
1098         ResourceMark rm(THREAD);

1106                  cp_index,
1107                  callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
1108                  callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
1109         Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IncompatibleClassChangeError(), "%s", ss.as_string());
1110         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1111       }
1112 
1113       Klass* klass = this_cp->pool_holder();
1114       HandleMark hm(THREAD);
1115       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
1116                                                                    callee, name, signature,
1117                                                                    THREAD);
1118       if (HAS_PENDING_EXCEPTION) {
1119         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1120       }
1121       result_oop = value();
1122       break;
1123     }
1124 
1125   case JVM_CONSTANT_MethodType:
1126     {


1127       Symbol*  signature = this_cp->method_type_signature_at(cp_index);
1128       { ResourceMark rm(THREAD);
1129         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
1130                               cp_index, this_cp->method_type_index_at(cp_index),
1131                               signature->as_C_string());
1132       }
1133       Klass* klass = this_cp->pool_holder();
1134       HandleMark hm(THREAD);
1135       Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
1136       result_oop = value();
1137       if (HAS_PENDING_EXCEPTION) {
1138         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1139       }
1140       break;
1141     }
1142 
1143   case JVM_CONSTANT_Integer:
1144     assert(cache_index == _no_index_sentinel, "should not have been set");
1145     prim_value.i = this_cp->int_at(cp_index);
1146     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* orig_pool = 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_indy_archivable(orig_pool, 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(orig_pool);
 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   archive_entries();
 454 }
 455 
 456 void ConstantPool::archive_entries() {
 457   InstanceKlass* src_holder = ArchiveBuilder::current()->get_source_addr(pool_holder());
 458   assert(src_holder->is_linked(), "must be");
 459   ResourceMark rm;
 460   bool archived = false;
 461   bool preresolve = pool_holder()->is_shared_boot_class() || pool_holder()->is_shared_platform_class() ||
 462                     pool_holder()->is_shared_app_class();
 463   for (int cp_index = 1; cp_index < length(); cp_index++) { // cp_index 0 is unused
 464     int cp_tag = tag_at(cp_index).value();
 465     switch (cp_tag) {
 466     case JVM_CONSTANT_UnresolvedClass:
 467       ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false);
 468       break;
 469     case JVM_CONSTANT_UnresolvedClassInError:
 470       tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
 471       ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false);
 472       break;
 473     case JVM_CONSTANT_MethodHandleInError:
 474       tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
 475       break;
 476     case JVM_CONSTANT_MethodTypeInError:
 477       tag_at_put(cp_index, JVM_CONSTANT_MethodType);
 478       break;
 479     case JVM_CONSTANT_DynamicInError:
 480       tag_at_put(cp_index, JVM_CONSTANT_Dynamic);
 481       break;
 482     case JVM_CONSTANT_Class:
 483       if (preresolve) {
 484         archived = maybe_archive_resolved_klass_at(cp_index);
 485       } else {
 486         archived = false;
 487       }
 488       if (!archived) {
 489         // This referenced class cannot be archived. Revert the tag to UnresolvedClass,
 490         // so that the proper class loading and initialization can happen at runtime.
 491         int resolved_klass_index = klass_slot_at(cp_index).resolved_klass_index();
 492         resolved_klasses()->at_put(resolved_klass_index, nullptr);
 493         tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
 494       }
 495       ArchiveBuilder::alloc_stats()->record_klass_cp_entry(archived);
 496       break;
 497     default:
 498       break;
 499     }
 500   }
 501 
 502   if (cache() != nullptr) {
 503     // cache() is null if this class is not yet linked.
 504     cache()->remove_unshareable_info();
 505   }
 506 }
 507 
 508 static const char* get_type(Klass* k) {
 509   const char* type;
 510   Klass* src_k;
 511   if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
 512     src_k = ArchiveBuilder::current()->get_source_addr(k);
 513   } else {
 514     src_k = k;
 515   }
 516 
 517   if (src_k->is_objArray_klass()) {
 518     src_k = ObjArrayKlass::cast(src_k)->bottom_klass();
 519     assert(!src_k->is_objArray_klass(), "sanity");
 520   }
 521 
 522   if (src_k->is_typeArray_klass()) {
 523     type = "prim";
 524   } else {
 525     InstanceKlass* src_ik = InstanceKlass::cast(src_k);
 526     oop loader = src_ik->class_loader();
 527     if (loader == nullptr) {
 528       type = "boot";
 529     } else if (loader == SystemDictionary::java_platform_loader()) {
 530       type = "plat";
 531     } else if (loader == SystemDictionary::java_system_loader()) {
 532       type = "app ";
 533     } else {
 534       type = "bad ";
 535       assert(0, "shouldn't have resolved a type loaded by custom loader");
 536     }
 537   }
 538 
 539   return type;
 540 }
 541 
 542 bool ConstantPool::maybe_archive_resolved_klass_at(int cp_index) {
 543   assert(ArchiveBuilder::current()->is_in_buffer_space(this), "must be");
 544   assert(tag_at(cp_index).is_klass(), "must be resolved");
 545 
 546   if (pool_holder()->is_hidden() && cp_index == pool_holder()->this_class_index()) {
 547     // All references to a hidden class's own field/methods are through this
 548     // index, which was resolved in ClassFileParser::fill_instance_klass. We
 549     // must preserve it.
 550     return true;
 551   }
 552 
 553   CPKlassSlot kslot = klass_slot_at(cp_index);
 554   int resolved_klass_index = kslot.resolved_klass_index();
 555   Klass* k = resolved_klasses()->at(resolved_klass_index);
 556   // k could be null if the referenced class has been excluded via
 557   // SystemDictionaryShared::is_excluded_class().
 558 
 559   if (k != nullptr) {
 560     ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
 561     if (ClassPrelinker::can_archive_resolved_klass(src_cp, cp_index)) {
 562       if (log_is_enabled(Debug, cds, resolve)) {
 563         ResourceMark rm;
 564         log_debug(cds, resolve)("archived klass  CP entry [%3d]: %s %s => %s %s%s", cp_index,
 565                                 pool_holder()->name()->as_C_string(), get_type(pool_holder()),
 566                                 k->name()->as_C_string(), get_type(k),
 567                                 pool_holder()->is_subtype_of(k) ? "" : " (not supertype)");
 568       }
 569       return true;
 570     }
 571   }





 572   return false;
 573 }
 574 
 575 bool ConstantPool::can_archive_invokehandle(ResolvedMethodEntry* rme) {
 576   assert(rme->is_resolved(Bytecodes::_invokehandle), "sanity");
 577 
 578   int cp_index = rme->constant_pool_index();
 579   int klass_cp_index = uncached_klass_ref_index_at(cp_index);
 580   Klass* resolved_klass = resolved_klass_at(klass_cp_index);
 581   if (!resolved_klass->is_instance_klass()) {
 582     // FIXME: can this ever happen?
 583     return false;
 584   }
 585   // FIXME -- any class referenced by the archived CP entries should be added to ArchiveBuilder::classes, or should be
 586   // filtered out.
 587   return true;
 588 }
 589 
 590 bool ConstantPool::can_archive_resolved_method(ResolvedMethodEntry* method_entry) {
 591   if (pool_holder()->is_hidden()) { // Not sure how to handle this yet ...
 592     if (pool_holder()->name()->starts_with("java/lang/invoke/LambdaForm$")) {
 593       // Hmmm, walking on thin ice here, but maybe we are OK :-)
 594     } else {
 595       return false;
 596     }
 597   }
 598 
 599   if (!(pool_holder()->is_shared_boot_class() || pool_holder()->is_shared_platform_class() ||
 600         pool_holder()->is_shared_app_class())) {
 601     // Archiving resolved cp entries for classes from non-builtin loaders
 602     // is not yet supported.
 603     return false;
 604   }
 605 
 606   if (CDSConfig::is_dumping_dynamic_archive()) {
 607     // InstanceKlass::methods() is has been resorted. We need to
 608     // update the vtable_index in method_entry (not implemented)
 609     return false;
 610   }
 611 
 612   if (method_entry->method() == nullptr) {
 613     return false;
 614   }
 615   if (method_entry->method()->is_continuation_native_intrinsic()) {
 616     return false; // FIXME: corresponding stub is generated on demand during method resolution (see LinkResolver::resolve_static_call).
 617   }
 618 
 619   int cp_index = method_entry->constant_pool_index();
 620   ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
 621   if (!src_cp->tag_at(cp_index).is_method() &&
 622       !src_cp->tag_at(cp_index).is_interface_method()) {
 623     return false;
 624   }
 625 
 626   if (!ClassPrelinker::can_archive_resolved_method(src_cp, cp_index)) {
 627     return false;
 628   }
 629 
 630   int klass_cp_index = uncached_klass_ref_index_at(cp_index);
 631   if (!src_cp->tag_at(klass_cp_index).is_klass()) {
 632     return false;
 633   }
 634 
 635   Klass* resolved_klass = resolved_klass_at(klass_cp_index);
 636   if (!resolved_klass->is_instance_klass()) {
 637     // FIXME: is it valid to have a non-instance klass in method refs?
 638     return false;
 639   }
 640   const char* is_static = "";
 641   const char* is_interface = "";
 642   if (method_entry->is_resolved(Bytecodes::_invokehandle)) {
 643     if (!ArchiveInvokeDynamic) {
 644       // FIXME We don't dump the MethodType tables. This somehow breaks stuff. Why???
 645       return false;
 646     } else if (!can_archive_invokehandle(method_entry)) {
 647       return false;
 648     }
 649   } else if (method_entry->is_resolved(Bytecodes::_invokestatic)) {
 650     is_static = " *** static";
 651   } else if (method_entry->is_resolved(Bytecodes::_invokeinterface)) {
 652     is_interface = "interface";
 653   } else if (!method_entry->is_resolved(Bytecodes::_invokevirtual) &&
 654              !method_entry->is_resolved(Bytecodes::_invokespecial)) {
 655     return false;
 656   }
 657 
 658   if (resolved_klass->is_instance_klass()) {
 659     InstanceKlass* ik = InstanceKlass::cast(resolved_klass);
 660     if (SystemDictionaryShared::is_jfr_event_class(ik)) {
 661       // Some methods in JRF event klasses may be redefined.
 662       return false;
 663     }
 664     if (SystemDictionaryShared::has_been_redefined(ik)) {
 665       return false;
 666     }
 667   }
 668 
 669   if (log_is_enabled(Debug, cds, resolve)) {
 670     ResourceMark rm;
 671     Symbol* name = uncached_name_ref_at(cp_index);
 672     Symbol* signature = uncached_signature_ref_at(cp_index);
 673     log_debug(cds, resolve)("archived %s method CP entry [%3d]: %s => %s.%s:%s%s", is_interface, cp_index,
 674                             pool_holder()->name()->as_C_string(), resolved_klass->name()->as_C_string(),
 675                             name->as_C_string(), signature->as_C_string(), is_static);
 676   }
 677   return true;
 678 }
 679 #endif // INCLUDE_CDS
 680 
 681 int ConstantPool::cp_to_object_index(int cp_index) {
 682   // this is harder don't do this so much.
 683   int i = reference_map()->find(checked_cast<u2>(cp_index));
 684   // We might not find the index for jsr292 call.
 685   return (i < 0) ? _no_index_sentinel : i;
 686 }
 687 
 688 void ConstantPool::string_at_put(int obj_index, oop str) {
 689   oop result = set_resolved_reference_at(obj_index, str);
 690   assert(result == nullptr || result == str, "Only set once or to the same string.");
 691 }
 692 
 693 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 694   ResourceMark rm;
 695   int line_number = -1;
 696   const char * source_file = nullptr;
 697   if (JavaThread::current()->has_last_Java_frame()) {
 698     // try to identify the method which called this function.

1217       (*status_return) = false;
1218       return nullptr;
1219     }
1220     // from now on there is either success or an OOME
1221     (*status_return) = true;
1222   }
1223 
1224   switch (tag.value()) {
1225 
1226   case JVM_CONSTANT_UnresolvedClass:
1227   case JVM_CONSTANT_Class:
1228     {
1229       assert(cache_index == _no_index_sentinel, "should not have been set");
1230       Klass* resolved = klass_at_impl(this_cp, cp_index, CHECK_NULL);
1231       // ldc wants the java mirror.
1232       result_oop = resolved->java_mirror();
1233       break;
1234     }
1235 
1236   case JVM_CONSTANT_Dynamic:
1237     { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokedynamic_time(),
1238                                 ClassLoader::perf_resolve_invokedynamic_count(),
1239                                 THREAD->class_being_initialized() == nullptr);
1240       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1241       BootstrapInfo bootstrap_specifier(this_cp, cp_index);
1242 
1243       // The initial step in resolving an unresolved symbolic reference to a
1244       // dynamically-computed constant is to resolve the symbolic reference to a
1245       // method handle which will be the bootstrap method for the dynamically-computed
1246       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1247       // method fails, then a MethodHandleInError is stored at the corresponding
1248       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1249       // set a DynamicConstantInError here since any subsequent use of this
1250       // bootstrap method will encounter the resolution of MethodHandleInError.
1251       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1252       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1253       // for the bootstrap_specifier created above.
1254       SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1255       Exceptions::wrap_dynamic_exception(/* is_indy */ false, THREAD);
1256       if (HAS_PENDING_EXCEPTION) {
1257         // Resolution failure of the dynamically-computed constant, save_and_throw_exception
1258         // will check for a LinkageError and store a DynamicConstantInError.
1259         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);

1277           // but do not save a DynamicInError resolution result.
1278           // See section 5.4.3 of the VM spec.
1279           THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail);
1280         }
1281       }
1282 
1283       LogTarget(Debug, methodhandles, condy) lt_condy;
1284       if (lt_condy.is_enabled()) {
1285         LogStream ls(lt_condy);
1286         bootstrap_specifier.print_msg_on(&ls, "resolve_constant_at_impl");
1287       }
1288       break;
1289     }
1290 
1291   case JVM_CONSTANT_String:
1292     assert(cache_index != _no_index_sentinel, "should have been set");
1293     result_oop = string_at_impl(this_cp, cp_index, cache_index, CHECK_NULL);
1294     break;
1295 
1296   case JVM_CONSTANT_MethodHandle:
1297     { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_handle_time(),
1298                                 ClassLoader::perf_resolve_method_handle_count(),
1299                                 THREAD->class_being_initialized() == nullptr);
1300       int ref_kind                 = this_cp->method_handle_ref_kind_at(cp_index);
1301       int callee_index             = this_cp->method_handle_klass_index_at(cp_index);
1302       Symbol*  name =      this_cp->method_handle_name_ref_at(cp_index);
1303       Symbol*  signature = this_cp->method_handle_signature_ref_at(cp_index);
1304       constantTag m_tag  = this_cp->tag_at(this_cp->method_handle_index_at(cp_index));
1305       { ResourceMark rm(THREAD);
1306         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
1307                               ref_kind, cp_index, this_cp->method_handle_index_at(cp_index),
1308                               callee_index, name->as_C_string(), signature->as_C_string());
1309       }
1310 
1311       Klass* callee = klass_at_impl(this_cp, callee_index, THREAD);
1312       if (HAS_PENDING_EXCEPTION) {
1313         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1314       }
1315 
1316       // Check constant pool method consistency
1317       if ((callee->is_interface() && m_tag.is_method()) ||
1318           (!callee->is_interface() && m_tag.is_interface_method())) {
1319         ResourceMark rm(THREAD);

1327                  cp_index,
1328                  callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
1329                  callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
1330         Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IncompatibleClassChangeError(), "%s", ss.as_string());
1331         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1332       }
1333 
1334       Klass* klass = this_cp->pool_holder();
1335       HandleMark hm(THREAD);
1336       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
1337                                                                    callee, name, signature,
1338                                                                    THREAD);
1339       if (HAS_PENDING_EXCEPTION) {
1340         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1341       }
1342       result_oop = value();
1343       break;
1344     }
1345 
1346   case JVM_CONSTANT_MethodType:
1347     { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_type_time(),
1348                                 ClassLoader::perf_resolve_method_type_count(),
1349                                 THREAD->class_being_initialized() == nullptr);
1350       Symbol*  signature = this_cp->method_type_signature_at(cp_index);
1351       { ResourceMark rm(THREAD);
1352         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
1353                               cp_index, this_cp->method_type_index_at(cp_index),
1354                               signature->as_C_string());
1355       }
1356       Klass* klass = this_cp->pool_holder();
1357       HandleMark hm(THREAD);
1358       Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
1359       result_oop = value();
1360       if (HAS_PENDING_EXCEPTION) {
1361         save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1362       }
1363       break;
1364     }
1365 
1366   case JVM_CONSTANT_Integer:
1367     assert(cache_index == _no_index_sentinel, "should not have been set");
1368     prim_value.i = this_cp->int_at(cp_index);
1369     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
< prev index next >