src/share/vm/oops/constantPool.cpp

Mon, 05 May 2014 19:53:00 -0400

author
coleenp
date
Mon, 05 May 2014 19:53:00 -0400
changeset 9966
baf9f57c9b46
parent 9550
270570f695e0
child 9970
f614bd5c9561
permissions
-rw-r--r--

8023697: failed class resolution reports different class name in detail message for the first and subsequent times
Summary: Cache detail message when we cache exception for constant pool resolution.
Reviewed-by: acorn, twisti, jrose

     1 /*
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "classfile/classLoaderData.hpp"
    27 #include "classfile/javaClasses.hpp"
    28 #include "classfile/metadataOnStackMark.hpp"
    29 #include "classfile/symbolTable.hpp"
    30 #include "classfile/systemDictionary.hpp"
    31 #include "classfile/vmSymbols.hpp"
    32 #include "interpreter/linkResolver.hpp"
    33 #include "memory/heapInspection.hpp"
    34 #include "memory/metadataFactory.hpp"
    35 #include "memory/oopFactory.hpp"
    36 #include "oops/constantPool.hpp"
    37 #include "oops/instanceKlass.hpp"
    38 #include "oops/objArrayKlass.hpp"
    39 #include "runtime/fieldType.hpp"
    40 #include "runtime/init.hpp"
    41 #include "runtime/javaCalls.hpp"
    42 #include "runtime/signature.hpp"
    43 #include "runtime/vframe.hpp"
    45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    47 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
    48   // Tags are RW but comment below applies to tags also.
    49   Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
    51   int size = ConstantPool::size(length);
    53   // CDS considerations:
    54   // Allocate read-write but may be able to move to read-only at dumping time
    55   // if all the klasses are resolved.  The only other field that is writable is
    56   // the resolved_references array, which is recreated at startup time.
    57   // But that could be moved to InstanceKlass (although a pain to access from
    58   // assembly code).  Maybe it could be moved to the cpCache which is RW.
    59   return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
    60 }
    62 ConstantPool::ConstantPool(Array<u1>* tags) {
    63   set_length(tags->length());
    64   set_tags(NULL);
    65   set_cache(NULL);
    66   set_reference_map(NULL);
    67   set_resolved_references(NULL);
    68   set_operands(NULL);
    69   set_pool_holder(NULL);
    70   set_flags(0);
    72   // only set to non-zero if constant pool is merged by RedefineClasses
    73   set_version(0);
    74   set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
    76   // initialize tag array
    77   int length = tags->length();
    78   for (int index = 0; index < length; index++) {
    79     tags->at_put(index, JVM_CONSTANT_Invalid);
    80   }
    81   set_tags(tags);
    82 }
    84 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
    85   MetadataFactory::free_metadata(loader_data, cache());
    86   set_cache(NULL);
    87   MetadataFactory::free_array<u2>(loader_data, reference_map());
    88   set_reference_map(NULL);
    90   MetadataFactory::free_array<jushort>(loader_data, operands());
    91   set_operands(NULL);
    93   release_C_heap_structures();
    95   // free tag array
    96   MetadataFactory::free_array<u1>(loader_data, tags());
    97   set_tags(NULL);
    98 }
   100 void ConstantPool::release_C_heap_structures() {
   101   // walk constant pool and decrement symbol reference counts
   102   unreference_symbols();
   104   delete _lock;
   105   set_lock(NULL);
   106 }
   108 objArrayOop ConstantPool::resolved_references() const {
   109   return (objArrayOop)JNIHandles::resolve(_resolved_references);
   110 }
   112 // Called from outside constant pool resolution where a resolved_reference array
   113 // may not be present.
   114 objArrayOop ConstantPool::resolved_references_or_null() const {
   115   if (_cache == NULL) {
   116     return NULL;
   117   } else {
   118     return (objArrayOop)JNIHandles::resolve(_resolved_references);
   119   }
   120 }
   122 // Create resolved_references array and mapping array for original cp indexes
   123 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
   124 // to map it back for resolving and some unlikely miscellaneous uses.
   125 // The objects created by invokedynamic are appended to this list.
   126 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
   127                                                   intStack reference_map,
   128                                                   int constant_pool_map_length,
   129                                                   TRAPS) {
   130   // Initialized the resolved object cache.
   131   int map_length = reference_map.length();
   132   if (map_length > 0) {
   133     // Only need mapping back to constant pool entries.  The map isn't used for
   134     // invokedynamic resolved_reference entries.  For invokedynamic entries,
   135     // the constant pool cache index has the mapping back to both the constant
   136     // pool and to the resolved reference index.
   137     if (constant_pool_map_length > 0) {
   138       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
   140       for (int i = 0; i < constant_pool_map_length; i++) {
   141         int x = reference_map.at(i);
   142         assert(x == (int)(jushort) x, "klass index is too big");
   143         om->at_put(i, (jushort)x);
   144       }
   145       set_reference_map(om);
   146     }
   148     // Create Java array for holding resolved strings, methodHandles,
   149     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
   150     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
   151     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
   152     set_resolved_references(loader_data->add_handle(refs_handle));
   153   }
   154 }
   156 // CDS support. Create a new resolved_references array.
   157 void ConstantPool::restore_unshareable_info(TRAPS) {
   159   // Only create the new resolved references array and lock if it hasn't been
   160   // attempted before
   161   if (resolved_references() != NULL) return;
   163   // restore the C++ vtable from the shared archive
   164   restore_vtable();
   166   if (SystemDictionary::Object_klass_loaded()) {
   167     // Recreate the object array and add to ClassLoaderData.
   168     int map_length = resolved_reference_length();
   169     if (map_length > 0) {
   170       objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
   171       Handle refs_handle (THREAD, (oop)stom);  // must handleize.
   173       ClassLoaderData* loader_data = pool_holder()->class_loader_data();
   174       set_resolved_references(loader_data->add_handle(refs_handle));
   175     }
   177     // Also need to recreate the mutex.  Make sure this matches the constructor
   178     set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
   179   }
   180 }
   182 void ConstantPool::remove_unshareable_info() {
   183   // Resolved references are not in the shared archive.
   184   // Save the length for restoration.  It is not necessarily the same length
   185   // as reference_map.length() if invokedynamic is saved.
   186   set_resolved_reference_length(
   187     resolved_references() != NULL ? resolved_references()->length() : 0);
   188   set_resolved_references(NULL);
   189   set_lock(NULL);
   190 }
   192 int ConstantPool::cp_to_object_index(int cp_index) {
   193   // this is harder don't do this so much.
   194   int i = reference_map()->find(cp_index);
   195   // We might not find the index for jsr292 call.
   196   return (i < 0) ? _no_index_sentinel : i;
   197 }
   199 Klass* ConstantPool::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
   200   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
   201   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
   202   // tag is not updated atomicly.
   204   CPSlot entry = this_oop->slot_at(which);
   205   if (entry.is_resolved()) {
   206     assert(entry.get_klass()->is_klass(), "must be");
   207     // Already resolved - return entry.
   208     return entry.get_klass();
   209   }
   211   // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
   212   // already has updated the object
   213   assert(THREAD->is_Java_thread(), "must be a Java thread");
   214   bool do_resolve = false;
   215   bool in_error = false;
   217   // Create a handle for the mirror. This will preserve the resolved class
   218   // until the loader_data is registered.
   219   Handle mirror_handle;
   221   Symbol* name = NULL;
   222   Handle       loader;
   223   {  MonitorLockerEx ml(this_oop->lock());
   225     if (this_oop->tag_at(which).is_unresolved_klass()) {
   226       if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
   227         in_error = true;
   228       } else {
   229         do_resolve = true;
   230         name   = this_oop->unresolved_klass_at(which);
   231         loader = Handle(THREAD, this_oop->pool_holder()->class_loader());
   232       }
   233     }
   234   } // unlocking constantPool
   237   // The original attempt to resolve this constant pool entry failed so find the
   238   // class of the original error and throw another error of the same class (JVMS 5.4.3).
   239   // If there is a detail message, pass that detail message to the error constructor.
   240   // The JVMS does not strictly require us to duplicate the same detail message,
   241   // or any internal exception fields such as cause or stacktrace.  But since the
   242   // detail message is often a class name or other literal string, we will repeat it if
   243   // we can find it in the symbol table.
   244   if (in_error) {
   245     throw_resolution_error(this_oop, which, CHECK_0);
   246   }
   248   if (do_resolve) {
   249     // this_oop must be unlocked during resolve_or_fail
   250     oop protection_domain = this_oop->pool_holder()->protection_domain();
   251     Handle h_prot (THREAD, protection_domain);
   252     Klass* k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
   253     KlassHandle k;
   254     if (!HAS_PENDING_EXCEPTION) {
   255       k = KlassHandle(THREAD, k_oop);
   256       // preserve the resolved klass.
   257       mirror_handle = Handle(THREAD, k_oop->java_mirror());
   258       // Do access check for klasses
   259       verify_constant_pool_resolve(this_oop, k, THREAD);
   260     }
   262     // Failed to resolve class. We must record the errors so that subsequent attempts
   263     // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
   264     if (HAS_PENDING_EXCEPTION) {
   265         MonitorLockerEx ml(this_oop->lock());
   267         // some other thread has beaten us and has resolved the class.
   268         if (this_oop->tag_at(which).is_klass()) {
   269           CLEAR_PENDING_EXCEPTION;
   270           entry = this_oop->resolved_klass_at(which);
   271           return entry.get_klass();
   272         }
   274         // The tag could have changed to in-error before the lock but we have to
   275         // handle that here for the class case.
   276         save_and_throw_exception(this_oop, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_0);
   277     }
   279     if (TraceClassResolution && !k()->oop_is_array()) {
   280       // skip resolving the constant pool so that this code get's
   281       // called the next time some bytecodes refer to this class.
   282       ResourceMark rm;
   283       int line_number = -1;
   284       const char * source_file = NULL;
   285       if (JavaThread::current()->has_last_Java_frame()) {
   286         // try to identify the method which called this function.
   287         vframeStream vfst(JavaThread::current());
   288         if (!vfst.at_end()) {
   289           line_number = vfst.method()->line_number_from_bci(vfst.bci());
   290           Symbol* s = vfst.method()->method_holder()->source_file_name();
   291           if (s != NULL) {
   292             source_file = s->as_C_string();
   293           }
   294         }
   295       }
   296       if (k() != this_oop->pool_holder()) {
   297         // only print something if the classes are different
   298         if (source_file != NULL) {
   299           tty->print("RESOLVE %s %s %s:%d\n",
   300                      this_oop->pool_holder()->external_name(),
   301                      InstanceKlass::cast(k())->external_name(), source_file, line_number);
   302         } else {
   303           tty->print("RESOLVE %s %s\n",
   304                      this_oop->pool_holder()->external_name(),
   305                      InstanceKlass::cast(k())->external_name());
   306         }
   307       }
   308       return k();
   309     } else {
   310       MonitorLockerEx ml(this_oop->lock());
   311       // Only updated constant pool - if it is resolved.
   312       do_resolve = this_oop->tag_at(which).is_unresolved_klass();
   313       if (do_resolve) {
   314         this_oop->klass_at_put(which, k());
   315       }
   316     }
   317   }
   319   entry = this_oop->resolved_klass_at(which);
   320   assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
   321   return entry.get_klass();
   322 }
   325 // Does not update ConstantPool* - to avoid any exception throwing. Used
   326 // by compiler and exception handling.  Also used to avoid classloads for
   327 // instanceof operations. Returns NULL if the class has not been loaded or
   328 // if the verification of constant pool failed
   329 Klass* ConstantPool::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
   330   CPSlot entry = this_oop->slot_at(which);
   331   if (entry.is_resolved()) {
   332     assert(entry.get_klass()->is_klass(), "must be");
   333     return entry.get_klass();
   334   } else {
   335     assert(entry.is_unresolved(), "must be either symbol or klass");
   336     Thread *thread = Thread::current();
   337     Symbol* name = entry.get_symbol();
   338     oop loader = this_oop->pool_holder()->class_loader();
   339     oop protection_domain = this_oop->pool_holder()->protection_domain();
   340     Handle h_prot (thread, protection_domain);
   341     Handle h_loader (thread, loader);
   342     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
   344     if (k != NULL) {
   345       // Make sure that resolving is legal
   346       EXCEPTION_MARK;
   347       KlassHandle klass(THREAD, k);
   348       // return NULL if verification fails
   349       verify_constant_pool_resolve(this_oop, klass, THREAD);
   350       if (HAS_PENDING_EXCEPTION) {
   351         CLEAR_PENDING_EXCEPTION;
   352         return NULL;
   353       }
   354       return klass();
   355     } else {
   356       return k;
   357     }
   358   }
   359 }
   362 Klass* ConstantPool::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
   363   return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
   364 }
   367 Method* ConstantPool::method_at_if_loaded(constantPoolHandle cpool,
   368                                                    int which) {
   369   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
   370   int cache_index = decode_cpcache_index(which, true);
   371   if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
   372     // FIXME: should be an assert
   373     if (PrintMiscellaneous && (Verbose||WizardMode)) {
   374       tty->print_cr("bad operand %d in:", which); cpool->print();
   375     }
   376     return NULL;
   377   }
   378   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   379   return e->method_if_resolved(cpool);
   380 }
   383 bool ConstantPool::has_appendix_at_if_loaded(constantPoolHandle cpool, int which) {
   384   if (cpool->cache() == NULL)  return false;  // nothing to load yet
   385   int cache_index = decode_cpcache_index(which, true);
   386   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   387   return e->has_appendix();
   388 }
   390 oop ConstantPool::appendix_at_if_loaded(constantPoolHandle cpool, int which) {
   391   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
   392   int cache_index = decode_cpcache_index(which, true);
   393   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   394   return e->appendix_if_resolved(cpool);
   395 }
   398 bool ConstantPool::has_method_type_at_if_loaded(constantPoolHandle cpool, int which) {
   399   if (cpool->cache() == NULL)  return false;  // nothing to load yet
   400   int cache_index = decode_cpcache_index(which, true);
   401   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   402   return e->has_method_type();
   403 }
   405 oop ConstantPool::method_type_at_if_loaded(constantPoolHandle cpool, int which) {
   406   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
   407   int cache_index = decode_cpcache_index(which, true);
   408   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   409   return e->method_type_if_resolved(cpool);
   410 }
   413 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
   414   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   415   return symbol_at(name_index);
   416 }
   419 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
   420   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   421   return symbol_at(signature_index);
   422 }
   425 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
   426   int i = which;
   427   if (!uncached && cache() != NULL) {
   428     if (ConstantPool::is_invokedynamic_index(which)) {
   429       // Invokedynamic index is index into resolved_references
   430       int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
   431       pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
   432       assert(tag_at(pool_index).is_name_and_type(), "");
   433       return pool_index;
   434     }
   435     // change byte-ordering and go via cache
   436     i = remap_instruction_operand_from_cache(which);
   437   } else {
   438     if (tag_at(which).is_invoke_dynamic()) {
   439       int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
   440       assert(tag_at(pool_index).is_name_and_type(), "");
   441       return pool_index;
   442     }
   443   }
   444   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   445   assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
   446   jint ref_index = *int_at_addr(i);
   447   return extract_high_short_from_int(ref_index);
   448 }
   451 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
   452   guarantee(!ConstantPool::is_invokedynamic_index(which),
   453             "an invokedynamic instruction does not have a klass");
   454   int i = which;
   455   if (!uncached && cache() != NULL) {
   456     // change byte-ordering and go via cache
   457     i = remap_instruction_operand_from_cache(which);
   458   }
   459   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   460   jint ref_index = *int_at_addr(i);
   461   return extract_low_short_from_int(ref_index);
   462 }
   466 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
   467   int cpc_index = operand;
   468   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
   469   assert((int)(u2)cpc_index == cpc_index, "clean u2");
   470   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
   471   return member_index;
   472 }
   475 void ConstantPool::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
   476  if (k->oop_is_instance() || k->oop_is_objArray()) {
   477     instanceKlassHandle holder (THREAD, this_oop->pool_holder());
   478     Klass* elem_oop = k->oop_is_instance() ? k() : ObjArrayKlass::cast(k())->bottom_klass();
   479     KlassHandle element (THREAD, elem_oop);
   481     // The element type could be a typeArray - we only need the access check if it is
   482     // an reference to another class
   483     if (element->oop_is_instance()) {
   484       LinkResolver::check_klass_accessability(holder, element, CHECK);
   485     }
   486   }
   487 }
   490 int ConstantPool::name_ref_index_at(int which_nt) {
   491   jint ref_index = name_and_type_at(which_nt);
   492   return extract_low_short_from_int(ref_index);
   493 }
   496 int ConstantPool::signature_ref_index_at(int which_nt) {
   497   jint ref_index = name_and_type_at(which_nt);
   498   return extract_high_short_from_int(ref_index);
   499 }
   502 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
   503   return klass_at(klass_ref_index_at(which), THREAD);
   504 }
   507 Symbol* ConstantPool::klass_name_at(int which) {
   508   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
   509          "Corrupted constant pool");
   510   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
   511   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
   512   // tag is not updated atomicly.
   513   CPSlot entry = slot_at(which);
   514   if (entry.is_resolved()) {
   515     // Already resolved - return entry's name.
   516     assert(entry.get_klass()->is_klass(), "must be");
   517     return entry.get_klass()->name();
   518   } else {
   519     assert(entry.is_unresolved(), "must be either symbol or klass");
   520     return entry.get_symbol();
   521   }
   522 }
   524 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
   525   jint ref_index = klass_ref_index_at(which);
   526   return klass_at_noresolve(ref_index);
   527 }
   529 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
   530   jint ref_index = uncached_klass_ref_index_at(which);
   531   return klass_at_noresolve(ref_index);
   532 }
   534 char* ConstantPool::string_at_noresolve(int which) {
   535   Symbol* s = unresolved_string_at(which);
   536   if (s == NULL) {
   537     return (char*)"<pseudo-string>";
   538   } else {
   539     return unresolved_string_at(which)->as_C_string();
   540   }
   541 }
   543 BasicType ConstantPool::basic_type_for_signature_at(int which) {
   544   return FieldType::basic_type(symbol_at(which));
   545 }
   548 void ConstantPool::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
   549   for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
   550     if (this_oop->tag_at(index).is_string()) {
   551       this_oop->string_at(index, CHECK);
   552     }
   553   }
   554 }
   556 // Resolve all the classes in the constant pool.  If they are all resolved,
   557 // the constant pool is read-only.  Enhancement: allocate cp entries to
   558 // another metaspace, and copy to read-only or read-write space if this
   559 // bit is set.
   560 bool ConstantPool::resolve_class_constants(TRAPS) {
   561   constantPoolHandle cp(THREAD, this);
   562   for (int index = 1; index < length(); index++) { // Index 0 is unused
   563     if (tag_at(index).is_unresolved_klass() &&
   564         klass_at_if_loaded(cp, index) == NULL) {
   565       return false;
   566   }
   567   }
   568   // set_preresolution(); or some bit for future use
   569   return true;
   570 }
   572 Symbol* ConstantPool::exception_message(constantPoolHandle this_oop, int which, constantTag tag, oop pending_exception) {
   573   // Dig out the detailed message to reuse if possible
   574   Symbol* message = NULL;
   575   oop detailed_message = java_lang_Throwable::message(pending_exception);
   576   if (detailed_message != NULL) {
   577      message = java_lang_String::as_symbol_or_null(detailed_message);
   578      if (message != NULL) {
   579        return message;
   580      }
   581   }
   583   // Return specific message for the tag
   584   switch (tag.value()) {
   585   case JVM_CONSTANT_UnresolvedClass:
   586     // return the class name in the error message
   587     message = this_oop->unresolved_klass_at(which);
   588     break;
   589   case JVM_CONSTANT_MethodHandle:
   590     // return the method handle name in the error message
   591     message = this_oop->method_handle_name_ref_at(which);
   592     break;
   593   case JVM_CONSTANT_MethodType:
   594     // return the method type signature in the error message
   595     message = this_oop->method_type_signature_at(which);
   596     break;
   597   default:
   598     ShouldNotReachHere();
   599   }
   601   return message;
   602 }
   604 void ConstantPool::throw_resolution_error(constantPoolHandle this_oop, int which, TRAPS) {
   605   Symbol* message = NULL;
   606   Symbol* error = SystemDictionary::find_resolution_error(this_oop, which, &message);
   607   assert(error != NULL && message != NULL, "checking");
   608   CLEAR_PENDING_EXCEPTION;
   609   ResourceMark rm;
   610   THROW_MSG(error, message->as_C_string());
   611 }
   613 // If resolution for Class, MethodHandle or MethodType fails, save the exception
   614 // in the resolution error table, so that the same exception is thrown again.
   615 void ConstantPool::save_and_throw_exception(constantPoolHandle this_oop, int which,
   616                                             constantTag tag, TRAPS) {
   617   assert(this_oop->lock()->is_locked(), "constant pool lock should be held");
   618   Symbol* error = PENDING_EXCEPTION->klass()->name();
   620   int error_tag = tag.error_value();
   622   if (!PENDING_EXCEPTION->
   623     is_a(SystemDictionary::LinkageError_klass())) {
   624     // Just throw the exception and don't prevent these classes from
   625     // being loaded due to virtual machine errors like StackOverflow
   626     // and OutOfMemoryError, etc, or if the thread was hit by stop()
   627     // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
   628   } else if (this_oop->tag_at(which).value() != error_tag) {
   629     Symbol* message = exception_message(this_oop, which, tag, PENDING_EXCEPTION);
   630     SystemDictionary::add_resolution_error(this_oop, which, error, message);
   631     this_oop->tag_at_put(which, error_tag);
   632   } else {
   633     // some other thread put this in error state
   634     throw_resolution_error(this_oop, which, CHECK);
   635   }
   637   // This exits with some pending exception
   638   assert(HAS_PENDING_EXCEPTION, "should not be cleared");
   639 }
   643 // Called to resolve constants in the constant pool and return an oop.
   644 // Some constant pool entries cache their resolved oop. This is also
   645 // called to create oops from constants to use in arguments for invokedynamic
   646 oop ConstantPool::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
   647   oop result_oop = NULL;
   648   Handle throw_exception;
   650   if (cache_index == _possible_index_sentinel) {
   651     // It is possible that this constant is one which is cached in the objects.
   652     // We'll do a linear search.  This should be OK because this usage is rare.
   653     assert(index > 0, "valid index");
   654     cache_index = this_oop->cp_to_object_index(index);
   655   }
   656   assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
   657   assert(index == _no_index_sentinel || index >= 0, "");
   659   if (cache_index >= 0) {
   660     result_oop = this_oop->resolved_references()->obj_at(cache_index);
   661     if (result_oop != NULL) {
   662       return result_oop;
   663       // That was easy...
   664     }
   665     index = this_oop->object_to_cp_index(cache_index);
   666   }
   668   jvalue prim_value;  // temp used only in a few cases below
   670   constantTag tag = this_oop->tag_at(index);
   672   switch (tag.value()) {
   674   case JVM_CONSTANT_UnresolvedClass:
   675   case JVM_CONSTANT_UnresolvedClassInError:
   676   case JVM_CONSTANT_Class:
   677     {
   678       assert(cache_index == _no_index_sentinel, "should not have been set");
   679       Klass* resolved = klass_at_impl(this_oop, index, CHECK_NULL);
   680       // ldc wants the java mirror.
   681       result_oop = resolved->java_mirror();
   682       break;
   683     }
   685   case JVM_CONSTANT_String:
   686     assert(cache_index != _no_index_sentinel, "should have been set");
   687     if (this_oop->is_pseudo_string_at(index)) {
   688       result_oop = this_oop->pseudo_string_at(index, cache_index);
   689       break;
   690     }
   691     result_oop = string_at_impl(this_oop, index, cache_index, CHECK_NULL);
   692     break;
   694   case JVM_CONSTANT_MethodHandleInError:
   695   case JVM_CONSTANT_MethodTypeInError:
   696     {
   697       throw_resolution_error(this_oop, index, CHECK_NULL);
   698       break;
   699     }
   701   case JVM_CONSTANT_MethodHandle:
   702     {
   703       int ref_kind                 = this_oop->method_handle_ref_kind_at(index);
   704       int callee_index             = this_oop->method_handle_klass_index_at(index);
   705       Symbol*  name =      this_oop->method_handle_name_ref_at(index);
   706       Symbol*  signature = this_oop->method_handle_signature_ref_at(index);
   707       if (PrintMiscellaneous)
   708         tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
   709                       ref_kind, index, this_oop->method_handle_index_at(index),
   710                       callee_index, name->as_C_string(), signature->as_C_string());
   711       KlassHandle callee;
   712       { Klass* k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
   713         callee = KlassHandle(THREAD, k);
   714       }
   715       KlassHandle klass(THREAD, this_oop->pool_holder());
   716       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
   717                                                                    callee, name, signature,
   718                                                                    THREAD);
   719       result_oop = value();
   720       if (HAS_PENDING_EXCEPTION) {
   721         MonitorLockerEx ml(this_oop->lock());  // lock cpool to change tag.
   722         save_and_throw_exception(this_oop, index, tag, CHECK_NULL);
   723       }
   724       break;
   725     }
   727   case JVM_CONSTANT_MethodType:
   728     {
   729       Symbol*  signature = this_oop->method_type_signature_at(index);
   730       if (PrintMiscellaneous)
   731         tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
   732                       index, this_oop->method_type_index_at(index),
   733                       signature->as_C_string());
   734       KlassHandle klass(THREAD, this_oop->pool_holder());
   735       Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
   736       result_oop = value();
   737       if (HAS_PENDING_EXCEPTION) {
   738         MonitorLockerEx ml(this_oop->lock());  // lock cpool to change tag.
   739         save_and_throw_exception(this_oop, index, tag, CHECK_NULL);
   740       }
   741       break;
   742     }
   744   case JVM_CONSTANT_Integer:
   745     assert(cache_index == _no_index_sentinel, "should not have been set");
   746     prim_value.i = this_oop->int_at(index);
   747     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
   748     break;
   750   case JVM_CONSTANT_Float:
   751     assert(cache_index == _no_index_sentinel, "should not have been set");
   752     prim_value.f = this_oop->float_at(index);
   753     result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
   754     break;
   756   case JVM_CONSTANT_Long:
   757     assert(cache_index == _no_index_sentinel, "should not have been set");
   758     prim_value.j = this_oop->long_at(index);
   759     result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
   760     break;
   762   case JVM_CONSTANT_Double:
   763     assert(cache_index == _no_index_sentinel, "should not have been set");
   764     prim_value.d = this_oop->double_at(index);
   765     result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
   766     break;
   768   default:
   769     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
   770                               this_oop(), index, cache_index, tag.value()));
   771     assert(false, "unexpected constant tag");
   772     break;
   773   }
   775   if (cache_index >= 0) {
   776     // Cache the oop here also.
   777     Handle result_handle(THREAD, result_oop);
   778     MonitorLockerEx ml(this_oop->lock());  // don't know if we really need this
   779     oop result = this_oop->resolved_references()->obj_at(cache_index);
   780     // Benign race condition:  resolved_references may already be filled in while we were trying to lock.
   781     // The important thing here is that all threads pick up the same result.
   782     // It doesn't matter which racing thread wins, as long as only one
   783     // result is used by all threads, and all future queries.
   784     // That result may be either a resolved constant or a failure exception.
   785     if (result == NULL) {
   786       this_oop->resolved_references()->obj_at_put(cache_index, result_handle());
   787       return result_handle();
   788     } else {
   789       // Return the winning thread's result.  This can be different than
   790       // result_handle() for MethodHandles.
   791       return result;
   792     }
   793   } else {
   794     return result_oop;
   795   }
   796 }
   798 oop ConstantPool::uncached_string_at(int which, TRAPS) {
   799   Symbol* sym = unresolved_string_at(which);
   800   oop str = StringTable::intern(sym, CHECK_(NULL));
   801   assert(java_lang_String::is_instance(str), "must be string");
   802   return str;
   803 }
   806 oop ConstantPool::resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS) {
   807   assert(this_oop->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
   809   Handle bsm;
   810   int argc;
   811   {
   812     // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
   813     // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
   814     // It is accompanied by the optional arguments.
   815     int bsm_index = this_oop->invoke_dynamic_bootstrap_method_ref_index_at(index);
   816     oop bsm_oop = this_oop->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
   817     if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
   818       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
   819     }
   821     // Extract the optional static arguments.
   822     argc = this_oop->invoke_dynamic_argument_count_at(index);
   823     if (argc == 0)  return bsm_oop;
   825     bsm = Handle(THREAD, bsm_oop);
   826   }
   828   objArrayHandle info;
   829   {
   830     objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
   831     info = objArrayHandle(THREAD, info_oop);
   832   }
   834   info->obj_at_put(0, bsm());
   835   for (int i = 0; i < argc; i++) {
   836     int arg_index = this_oop->invoke_dynamic_argument_index_at(index, i);
   837     oop arg_oop = this_oop->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
   838     info->obj_at_put(1+i, arg_oop);
   839   }
   841   return info();
   842 }
   844 oop ConstantPool::string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS) {
   845   // If the string has already been interned, this entry will be non-null
   846   oop str = this_oop->resolved_references()->obj_at(obj_index);
   847   if (str != NULL) return str;
   848   Symbol* sym = this_oop->unresolved_string_at(which);
   849   str = StringTable::intern(sym, CHECK_(NULL));
   850   this_oop->string_at_put(which, obj_index, str);
   851   assert(java_lang_String::is_instance(str), "must be string");
   852   return str;
   853 }
   856 bool ConstantPool::klass_name_at_matches(instanceKlassHandle k,
   857                                                 int which) {
   858   // Names are interned, so we can compare Symbol*s directly
   859   Symbol* cp_name = klass_name_at(which);
   860   return (cp_name == k->name());
   861 }
   864 // Iterate over symbols and decrement ones which are Symbol*s.
   865 // This is done during GC so do not need to lock constantPool unless we
   866 // have per-thread safepoints.
   867 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
   868 // these symbols but didn't increment the reference count.
   869 void ConstantPool::unreference_symbols() {
   870   for (int index = 1; index < length(); index++) { // Index 0 is unused
   871     constantTag tag = tag_at(index);
   872     if (tag.is_symbol()) {
   873       symbol_at(index)->decrement_refcount();
   874     }
   875   }
   876 }
   879 // Compare this constant pool's entry at index1 to the constant pool
   880 // cp2's entry at index2.
   881 bool ConstantPool::compare_entry_to(int index1, constantPoolHandle cp2,
   882        int index2, TRAPS) {
   884   // The error tags are equivalent to non-error tags when comparing
   885   jbyte t1 = tag_at(index1).non_error_value();
   886   jbyte t2 = cp2->tag_at(index2).non_error_value();
   888   if (t1 != t2) {
   889     // Not the same entry type so there is nothing else to check. Note
   890     // that this style of checking will consider resolved/unresolved
   891     // class pairs as different.
   892     // From the ConstantPool* API point of view, this is correct
   893     // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
   894     // plays out in the context of ConstantPool* merging.
   895     return false;
   896   }
   898   switch (t1) {
   899   case JVM_CONSTANT_Class:
   900   {
   901     Klass* k1 = klass_at(index1, CHECK_false);
   902     Klass* k2 = cp2->klass_at(index2, CHECK_false);
   903     if (k1 == k2) {
   904       return true;
   905     }
   906   } break;
   908   case JVM_CONSTANT_ClassIndex:
   909   {
   910     int recur1 = klass_index_at(index1);
   911     int recur2 = cp2->klass_index_at(index2);
   912     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   913     if (match) {
   914       return true;
   915     }
   916   } break;
   918   case JVM_CONSTANT_Double:
   919   {
   920     jdouble d1 = double_at(index1);
   921     jdouble d2 = cp2->double_at(index2);
   922     if (d1 == d2) {
   923       return true;
   924     }
   925   } break;
   927   case JVM_CONSTANT_Fieldref:
   928   case JVM_CONSTANT_InterfaceMethodref:
   929   case JVM_CONSTANT_Methodref:
   930   {
   931     int recur1 = uncached_klass_ref_index_at(index1);
   932     int recur2 = cp2->uncached_klass_ref_index_at(index2);
   933     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   934     if (match) {
   935       recur1 = uncached_name_and_type_ref_index_at(index1);
   936       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
   937       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   938       if (match) {
   939         return true;
   940       }
   941     }
   942   } break;
   944   case JVM_CONSTANT_Float:
   945   {
   946     jfloat f1 = float_at(index1);
   947     jfloat f2 = cp2->float_at(index2);
   948     if (f1 == f2) {
   949       return true;
   950     }
   951   } break;
   953   case JVM_CONSTANT_Integer:
   954   {
   955     jint i1 = int_at(index1);
   956     jint i2 = cp2->int_at(index2);
   957     if (i1 == i2) {
   958       return true;
   959     }
   960   } break;
   962   case JVM_CONSTANT_Long:
   963   {
   964     jlong l1 = long_at(index1);
   965     jlong l2 = cp2->long_at(index2);
   966     if (l1 == l2) {
   967       return true;
   968     }
   969   } break;
   971   case JVM_CONSTANT_NameAndType:
   972   {
   973     int recur1 = name_ref_index_at(index1);
   974     int recur2 = cp2->name_ref_index_at(index2);
   975     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   976     if (match) {
   977       recur1 = signature_ref_index_at(index1);
   978       recur2 = cp2->signature_ref_index_at(index2);
   979       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   980       if (match) {
   981         return true;
   982       }
   983     }
   984   } break;
   986   case JVM_CONSTANT_StringIndex:
   987   {
   988     int recur1 = string_index_at(index1);
   989     int recur2 = cp2->string_index_at(index2);
   990     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   991     if (match) {
   992       return true;
   993     }
   994   } break;
   996   case JVM_CONSTANT_UnresolvedClass:
   997   {
   998     Symbol* k1 = unresolved_klass_at(index1);
   999     Symbol* k2 = cp2->unresolved_klass_at(index2);
  1000     if (k1 == k2) {
  1001       return true;
  1003   } break;
  1005   case JVM_CONSTANT_MethodType:
  1007     int k1 = method_type_index_at_error_ok(index1);
  1008     int k2 = cp2->method_type_index_at_error_ok(index2);
  1009     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
  1010     if (match) {
  1011       return true;
  1013   } break;
  1015   case JVM_CONSTANT_MethodHandle:
  1017     int k1 = method_handle_ref_kind_at_error_ok(index1);
  1018     int k2 = cp2->method_handle_ref_kind_at_error_ok(index2);
  1019     if (k1 == k2) {
  1020       int i1 = method_handle_index_at_error_ok(index1);
  1021       int i2 = cp2->method_handle_index_at_error_ok(index2);
  1022       bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
  1023       if (match) {
  1024         return true;
  1027   } break;
  1029   case JVM_CONSTANT_InvokeDynamic:
  1031     int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
  1032     int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
  1033     int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
  1034     int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
  1035     // separate statements and variables because CHECK_false is used
  1036     bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
  1037     bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
  1038     return (match_entry && match_operand);
  1039   } break;
  1041   case JVM_CONSTANT_String:
  1043     Symbol* s1 = unresolved_string_at(index1);
  1044     Symbol* s2 = cp2->unresolved_string_at(index2);
  1045     if (s1 == s2) {
  1046       return true;
  1048   } break;
  1050   case JVM_CONSTANT_Utf8:
  1052     Symbol* s1 = symbol_at(index1);
  1053     Symbol* s2 = cp2->symbol_at(index2);
  1054     if (s1 == s2) {
  1055       return true;
  1057   } break;
  1059   // Invalid is used as the tag for the second constant pool entry
  1060   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  1061   // not be seen by itself.
  1062   case JVM_CONSTANT_Invalid: // fall through
  1064   default:
  1065     ShouldNotReachHere();
  1066     break;
  1069   return false;
  1070 } // end compare_entry_to()
  1073 // Resize the operands array with delta_len and delta_size.
  1074 // Used in RedefineClasses for CP merge.
  1075 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
  1076   int old_len  = operand_array_length(operands());
  1077   int new_len  = old_len + delta_len;
  1078   int min_len  = (delta_len > 0) ? old_len : new_len;
  1080   int old_size = operands()->length();
  1081   int new_size = old_size + delta_size;
  1082   int min_size = (delta_size > 0) ? old_size : new_size;
  1084   ClassLoaderData* loader_data = pool_holder()->class_loader_data();
  1085   Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
  1087   // Set index in the resized array for existing elements only
  1088   for (int idx = 0; idx < min_len; idx++) {
  1089     int offset = operand_offset_at(idx);                       // offset in original array
  1090     operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
  1092   // Copy the bootstrap specifiers only
  1093   Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
  1094                                new_ops->adr_at(2*new_len),
  1095                                (min_size - 2*min_len) * sizeof(u2));
  1096   // Explicitly deallocate old operands array.
  1097   // Note, it is not needed for 7u backport.
  1098   if ( operands() != NULL) { // the safety check
  1099     MetadataFactory::free_array<u2>(loader_data, operands());
  1101   set_operands(new_ops);
  1102 } // end resize_operands()
  1105 // Extend the operands array with the length and size of the ext_cp operands.
  1106 // Used in RedefineClasses for CP merge.
  1107 void ConstantPool::extend_operands(constantPoolHandle ext_cp, TRAPS) {
  1108   int delta_len = operand_array_length(ext_cp->operands());
  1109   if (delta_len == 0) {
  1110     return; // nothing to do
  1112   int delta_size = ext_cp->operands()->length();
  1114   assert(delta_len  > 0 && delta_size > 0, "extended operands array must be bigger");
  1116   if (operand_array_length(operands()) == 0) {
  1117     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
  1118     Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
  1119     // The first element index defines the offset of second part
  1120     operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
  1121     set_operands(new_ops);
  1122   } else {
  1123     resize_operands(delta_len, delta_size, CHECK);
  1126 } // end extend_operands()
  1129 // Shrink the operands array to a smaller array with new_len length.
  1130 // Used in RedefineClasses for CP merge.
  1131 void ConstantPool::shrink_operands(int new_len, TRAPS) {
  1132   int old_len = operand_array_length(operands());
  1133   if (new_len == old_len) {
  1134     return; // nothing to do
  1136   assert(new_len < old_len, "shrunken operands array must be smaller");
  1138   int free_base  = operand_next_offset_at(new_len - 1);
  1139   int delta_len  = new_len - old_len;
  1140   int delta_size = 2*delta_len + free_base - operands()->length();
  1142   resize_operands(delta_len, delta_size, CHECK);
  1144 } // end shrink_operands()
  1147 void ConstantPool::copy_operands(constantPoolHandle from_cp,
  1148                                  constantPoolHandle to_cp,
  1149                                  TRAPS) {
  1151   int from_oplen = operand_array_length(from_cp->operands());
  1152   int old_oplen  = operand_array_length(to_cp->operands());
  1153   if (from_oplen != 0) {
  1154     ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
  1155     // append my operands to the target's operands array
  1156     if (old_oplen == 0) {
  1157       // Can't just reuse from_cp's operand list because of deallocation issues
  1158       int len = from_cp->operands()->length();
  1159       Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
  1160       Copy::conjoint_memory_atomic(
  1161           from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
  1162       to_cp->set_operands(new_ops);
  1163     } else {
  1164       int old_len  = to_cp->operands()->length();
  1165       int from_len = from_cp->operands()->length();
  1166       int old_off  = old_oplen * sizeof(u2);
  1167       int from_off = from_oplen * sizeof(u2);
  1168       // Use the metaspace for the destination constant pool
  1169       Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
  1170       int fillp = 0, len = 0;
  1171       // first part of dest
  1172       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
  1173                                    new_operands->adr_at(fillp),
  1174                                    (len = old_off) * sizeof(u2));
  1175       fillp += len;
  1176       // first part of src
  1177       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
  1178                                    new_operands->adr_at(fillp),
  1179                                    (len = from_off) * sizeof(u2));
  1180       fillp += len;
  1181       // second part of dest
  1182       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
  1183                                    new_operands->adr_at(fillp),
  1184                                    (len = old_len - old_off) * sizeof(u2));
  1185       fillp += len;
  1186       // second part of src
  1187       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
  1188                                    new_operands->adr_at(fillp),
  1189                                    (len = from_len - from_off) * sizeof(u2));
  1190       fillp += len;
  1191       assert(fillp == new_operands->length(), "");
  1193       // Adjust indexes in the first part of the copied operands array.
  1194       for (int j = 0; j < from_oplen; j++) {
  1195         int offset = operand_offset_at(new_operands, old_oplen + j);
  1196         assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
  1197         offset += old_len;  // every new tuple is preceded by old_len extra u2's
  1198         operand_offset_at_put(new_operands, old_oplen + j, offset);
  1201       // replace target operands array with combined array
  1202       to_cp->set_operands(new_operands);
  1205 } // end copy_operands()
  1208 // Copy this constant pool's entries at start_i to end_i (inclusive)
  1209 // to the constant pool to_cp's entries starting at to_i. A total of
  1210 // (end_i - start_i) + 1 entries are copied.
  1211 void ConstantPool::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
  1212        constantPoolHandle to_cp, int to_i, TRAPS) {
  1215   int dest_i = to_i;  // leave original alone for debug purposes
  1217   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
  1218     copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
  1220     switch (from_cp->tag_at(src_i).value()) {
  1221     case JVM_CONSTANT_Double:
  1222     case JVM_CONSTANT_Long:
  1223       // double and long take two constant pool entries
  1224       src_i += 2;
  1225       dest_i += 2;
  1226       break;
  1228     default:
  1229       // all others take one constant pool entry
  1230       src_i++;
  1231       dest_i++;
  1232       break;
  1235   copy_operands(from_cp, to_cp, CHECK);
  1237 } // end copy_cp_to_impl()
  1240 // Copy this constant pool's entry at from_i to the constant pool
  1241 // to_cp's entry at to_i.
  1242 void ConstantPool::copy_entry_to(constantPoolHandle from_cp, int from_i,
  1243                                         constantPoolHandle to_cp, int to_i,
  1244                                         TRAPS) {
  1246   int tag = from_cp->tag_at(from_i).value();
  1247   switch (tag) {
  1248   case JVM_CONSTANT_Class:
  1250     Klass* k = from_cp->klass_at(from_i, CHECK);
  1251     to_cp->klass_at_put(to_i, k);
  1252   } break;
  1254   case JVM_CONSTANT_ClassIndex:
  1256     jint ki = from_cp->klass_index_at(from_i);
  1257     to_cp->klass_index_at_put(to_i, ki);
  1258   } break;
  1260   case JVM_CONSTANT_Double:
  1262     jdouble d = from_cp->double_at(from_i);
  1263     to_cp->double_at_put(to_i, d);
  1264     // double takes two constant pool entries so init second entry's tag
  1265     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1266   } break;
  1268   case JVM_CONSTANT_Fieldref:
  1270     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1271     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1272     to_cp->field_at_put(to_i, class_index, name_and_type_index);
  1273   } break;
  1275   case JVM_CONSTANT_Float:
  1277     jfloat f = from_cp->float_at(from_i);
  1278     to_cp->float_at_put(to_i, f);
  1279   } break;
  1281   case JVM_CONSTANT_Integer:
  1283     jint i = from_cp->int_at(from_i);
  1284     to_cp->int_at_put(to_i, i);
  1285   } break;
  1287   case JVM_CONSTANT_InterfaceMethodref:
  1289     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1290     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1291     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
  1292   } break;
  1294   case JVM_CONSTANT_Long:
  1296     jlong l = from_cp->long_at(from_i);
  1297     to_cp->long_at_put(to_i, l);
  1298     // long takes two constant pool entries so init second entry's tag
  1299     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1300   } break;
  1302   case JVM_CONSTANT_Methodref:
  1304     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1305     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1306     to_cp->method_at_put(to_i, class_index, name_and_type_index);
  1307   } break;
  1309   case JVM_CONSTANT_NameAndType:
  1311     int name_ref_index = from_cp->name_ref_index_at(from_i);
  1312     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
  1313     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
  1314   } break;
  1316   case JVM_CONSTANT_StringIndex:
  1318     jint si = from_cp->string_index_at(from_i);
  1319     to_cp->string_index_at_put(to_i, si);
  1320   } break;
  1322   case JVM_CONSTANT_UnresolvedClass:
  1323   case JVM_CONSTANT_UnresolvedClassInError:
  1325     // Can be resolved after checking tag, so check the slot first.
  1326     CPSlot entry = from_cp->slot_at(from_i);
  1327     if (entry.is_resolved()) {
  1328       assert(entry.get_klass()->is_klass(), "must be");
  1329       // Already resolved
  1330       to_cp->klass_at_put(to_i, entry.get_klass());
  1331     } else {
  1332       to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
  1334   } break;
  1336   case JVM_CONSTANT_String:
  1338     Symbol* s = from_cp->unresolved_string_at(from_i);
  1339     to_cp->unresolved_string_at_put(to_i, s);
  1340   } break;
  1342   case JVM_CONSTANT_Utf8:
  1344     Symbol* s = from_cp->symbol_at(from_i);
  1345     // Need to increase refcount, the old one will be thrown away and deferenced
  1346     s->increment_refcount();
  1347     to_cp->symbol_at_put(to_i, s);
  1348   } break;
  1350   case JVM_CONSTANT_MethodType:
  1351   case JVM_CONSTANT_MethodTypeInError:
  1353     jint k = from_cp->method_type_index_at_error_ok(from_i);
  1354     to_cp->method_type_index_at_put(to_i, k);
  1355   } break;
  1357   case JVM_CONSTANT_MethodHandle:
  1358   case JVM_CONSTANT_MethodHandleInError:
  1360     int k1 = from_cp->method_handle_ref_kind_at_error_ok(from_i);
  1361     int k2 = from_cp->method_handle_index_at_error_ok(from_i);
  1362     to_cp->method_handle_index_at_put(to_i, k1, k2);
  1363   } break;
  1365   case JVM_CONSTANT_InvokeDynamic:
  1367     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
  1368     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
  1369     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
  1370     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
  1371   } break;
  1373   // Invalid is used as the tag for the second constant pool entry
  1374   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  1375   // not be seen by itself.
  1376   case JVM_CONSTANT_Invalid: // fall through
  1378   default:
  1380     ShouldNotReachHere();
  1381   } break;
  1383 } // end copy_entry_to()
  1386 // Search constant pool search_cp for an entry that matches this
  1387 // constant pool's entry at pattern_i. Returns the index of a
  1388 // matching entry or zero (0) if there is no matching entry.
  1389 int ConstantPool::find_matching_entry(int pattern_i,
  1390       constantPoolHandle search_cp, TRAPS) {
  1392   // index zero (0) is not used
  1393   for (int i = 1; i < search_cp->length(); i++) {
  1394     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
  1395     if (found) {
  1396       return i;
  1400   return 0;  // entry not found; return unused index zero (0)
  1401 } // end find_matching_entry()
  1404 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
  1405 // cp2's bootstrap specifier at idx2.
  1406 bool ConstantPool::compare_operand_to(int idx1, constantPoolHandle cp2, int idx2, TRAPS) {
  1407   int k1 = operand_bootstrap_method_ref_index_at(idx1);
  1408   int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
  1409   bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
  1411   if (!match) {
  1412     return false;
  1414   int argc = operand_argument_count_at(idx1);
  1415   if (argc == cp2->operand_argument_count_at(idx2)) {
  1416     for (int j = 0; j < argc; j++) {
  1417       k1 = operand_argument_index_at(idx1, j);
  1418       k2 = cp2->operand_argument_index_at(idx2, j);
  1419       match = compare_entry_to(k1, cp2, k2, CHECK_false);
  1420       if (!match) {
  1421         return false;
  1424     return true;           // got through loop; all elements equal
  1426   return false;
  1427 } // end compare_operand_to()
  1429 // Search constant pool search_cp for a bootstrap specifier that matches
  1430 // this constant pool's bootstrap specifier at pattern_i index.
  1431 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
  1432 int ConstantPool::find_matching_operand(int pattern_i,
  1433                     constantPoolHandle search_cp, int search_len, TRAPS) {
  1434   for (int i = 0; i < search_len; i++) {
  1435     bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
  1436     if (found) {
  1437       return i;
  1440   return -1;  // bootstrap specifier not found; return unused index (-1)
  1441 } // end find_matching_operand()
  1444 #ifndef PRODUCT
  1446 const char* ConstantPool::printable_name_at(int which) {
  1448   constantTag tag = tag_at(which);
  1450   if (tag.is_string()) {
  1451     return string_at_noresolve(which);
  1452   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
  1453     return klass_name_at(which)->as_C_string();
  1454   } else if (tag.is_symbol()) {
  1455     return symbol_at(which)->as_C_string();
  1457   return "";
  1460 #endif // PRODUCT
  1463 // JVMTI GetConstantPool support
  1465 // For debugging of constant pool
  1466 const bool debug_cpool = false;
  1468 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
  1470 static void print_cpool_bytes(jint cnt, u1 *bytes) {
  1471   const char* WARN_MSG = "Must not be such entry!";
  1472   jint size = 0;
  1473   u2   idx1, idx2;
  1475   for (jint idx = 1; idx < cnt; idx++) {
  1476     jint ent_size = 0;
  1477     u1   tag  = *bytes++;
  1478     size++;                       // count tag
  1480     printf("const #%03d, tag: %02d ", idx, tag);
  1481     switch(tag) {
  1482       case JVM_CONSTANT_Invalid: {
  1483         printf("Invalid");
  1484         break;
  1486       case JVM_CONSTANT_Unicode: {
  1487         printf("Unicode      %s", WARN_MSG);
  1488         break;
  1490       case JVM_CONSTANT_Utf8: {
  1491         u2 len = Bytes::get_Java_u2(bytes);
  1492         char str[128];
  1493         if (len > 127) {
  1494            len = 127;
  1496         strncpy(str, (char *) (bytes+2), len);
  1497         str[len] = '\0';
  1498         printf("Utf8          \"%s\"", str);
  1499         ent_size = 2 + len;
  1500         break;
  1502       case JVM_CONSTANT_Integer: {
  1503         u4 val = Bytes::get_Java_u4(bytes);
  1504         printf("int          %d", *(int *) &val);
  1505         ent_size = 4;
  1506         break;
  1508       case JVM_CONSTANT_Float: {
  1509         u4 val = Bytes::get_Java_u4(bytes);
  1510         printf("float        %5.3ff", *(float *) &val);
  1511         ent_size = 4;
  1512         break;
  1514       case JVM_CONSTANT_Long: {
  1515         u8 val = Bytes::get_Java_u8(bytes);
  1516         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
  1517         ent_size = 8;
  1518         idx++; // Long takes two cpool slots
  1519         break;
  1521       case JVM_CONSTANT_Double: {
  1522         u8 val = Bytes::get_Java_u8(bytes);
  1523         printf("double       %5.3fd", *(jdouble *)&val);
  1524         ent_size = 8;
  1525         idx++; // Double takes two cpool slots
  1526         break;
  1528       case JVM_CONSTANT_Class: {
  1529         idx1 = Bytes::get_Java_u2(bytes);
  1530         printf("class        #%03d", idx1);
  1531         ent_size = 2;
  1532         break;
  1534       case JVM_CONSTANT_String: {
  1535         idx1 = Bytes::get_Java_u2(bytes);
  1536         printf("String       #%03d", idx1);
  1537         ent_size = 2;
  1538         break;
  1540       case JVM_CONSTANT_Fieldref: {
  1541         idx1 = Bytes::get_Java_u2(bytes);
  1542         idx2 = Bytes::get_Java_u2(bytes+2);
  1543         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
  1544         ent_size = 4;
  1545         break;
  1547       case JVM_CONSTANT_Methodref: {
  1548         idx1 = Bytes::get_Java_u2(bytes);
  1549         idx2 = Bytes::get_Java_u2(bytes+2);
  1550         printf("Method       #%03d, #%03d", idx1, idx2);
  1551         ent_size = 4;
  1552         break;
  1554       case JVM_CONSTANT_InterfaceMethodref: {
  1555         idx1 = Bytes::get_Java_u2(bytes);
  1556         idx2 = Bytes::get_Java_u2(bytes+2);
  1557         printf("InterfMethod #%03d, #%03d", idx1, idx2);
  1558         ent_size = 4;
  1559         break;
  1561       case JVM_CONSTANT_NameAndType: {
  1562         idx1 = Bytes::get_Java_u2(bytes);
  1563         idx2 = Bytes::get_Java_u2(bytes+2);
  1564         printf("NameAndType  #%03d, #%03d", idx1, idx2);
  1565         ent_size = 4;
  1566         break;
  1568       case JVM_CONSTANT_ClassIndex: {
  1569         printf("ClassIndex  %s", WARN_MSG);
  1570         break;
  1572       case JVM_CONSTANT_UnresolvedClass: {
  1573         printf("UnresolvedClass: %s", WARN_MSG);
  1574         break;
  1576       case JVM_CONSTANT_UnresolvedClassInError: {
  1577         printf("UnresolvedClassInErr: %s", WARN_MSG);
  1578         break;
  1580       case JVM_CONSTANT_StringIndex: {
  1581         printf("StringIndex: %s", WARN_MSG);
  1582         break;
  1585     printf(";\n");
  1586     bytes += ent_size;
  1587     size  += ent_size;
  1589   printf("Cpool size: %d\n", size);
  1590   fflush(0);
  1591   return;
  1592 } /* end print_cpool_bytes */
  1595 // Returns size of constant pool entry.
  1596 jint ConstantPool::cpool_entry_size(jint idx) {
  1597   switch(tag_at(idx).value()) {
  1598     case JVM_CONSTANT_Invalid:
  1599     case JVM_CONSTANT_Unicode:
  1600       return 1;
  1602     case JVM_CONSTANT_Utf8:
  1603       return 3 + symbol_at(idx)->utf8_length();
  1605     case JVM_CONSTANT_Class:
  1606     case JVM_CONSTANT_String:
  1607     case JVM_CONSTANT_ClassIndex:
  1608     case JVM_CONSTANT_UnresolvedClass:
  1609     case JVM_CONSTANT_UnresolvedClassInError:
  1610     case JVM_CONSTANT_StringIndex:
  1611     case JVM_CONSTANT_MethodType:
  1612     case JVM_CONSTANT_MethodTypeInError:
  1613       return 3;
  1615     case JVM_CONSTANT_MethodHandle:
  1616     case JVM_CONSTANT_MethodHandleInError:
  1617       return 4; //tag, ref_kind, ref_index
  1619     case JVM_CONSTANT_Integer:
  1620     case JVM_CONSTANT_Float:
  1621     case JVM_CONSTANT_Fieldref:
  1622     case JVM_CONSTANT_Methodref:
  1623     case JVM_CONSTANT_InterfaceMethodref:
  1624     case JVM_CONSTANT_NameAndType:
  1625       return 5;
  1627     case JVM_CONSTANT_InvokeDynamic:
  1628       // u1 tag, u2 bsm, u2 nt
  1629       return 5;
  1631     case JVM_CONSTANT_Long:
  1632     case JVM_CONSTANT_Double:
  1633       return 9;
  1635   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
  1636   return 1;
  1637 } /* end cpool_entry_size */
  1640 // SymbolHashMap is used to find a constant pool index from a string.
  1641 // This function fills in SymbolHashMaps, one for utf8s and one for
  1642 // class names, returns size of the cpool raw bytes.
  1643 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
  1644                                           SymbolHashMap *classmap) {
  1645   jint size = 0;
  1647   for (u2 idx = 1; idx < length(); idx++) {
  1648     u2 tag = tag_at(idx).value();
  1649     size += cpool_entry_size(idx);
  1651     switch(tag) {
  1652       case JVM_CONSTANT_Utf8: {
  1653         Symbol* sym = symbol_at(idx);
  1654         symmap->add_entry(sym, idx);
  1655         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
  1656         break;
  1658       case JVM_CONSTANT_Class:
  1659       case JVM_CONSTANT_UnresolvedClass:
  1660       case JVM_CONSTANT_UnresolvedClassInError: {
  1661         Symbol* sym = klass_name_at(idx);
  1662         classmap->add_entry(sym, idx);
  1663         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
  1664         break;
  1666       case JVM_CONSTANT_Long:
  1667       case JVM_CONSTANT_Double: {
  1668         idx++; // Both Long and Double take two cpool slots
  1669         break;
  1673   return size;
  1674 } /* end hash_utf8_entries_to */
  1677 // Copy cpool bytes.
  1678 // Returns:
  1679 //    0, in case of OutOfMemoryError
  1680 //   -1, in case of internal error
  1681 //  > 0, count of the raw cpool bytes that have been copied
  1682 int ConstantPool::copy_cpool_bytes(int cpool_size,
  1683                                           SymbolHashMap* tbl,
  1684                                           unsigned char *bytes) {
  1685   u2   idx1, idx2;
  1686   jint size  = 0;
  1687   jint cnt   = length();
  1688   unsigned char *start_bytes = bytes;
  1690   for (jint idx = 1; idx < cnt; idx++) {
  1691     u1   tag      = tag_at(idx).value();
  1692     jint ent_size = cpool_entry_size(idx);
  1694     assert(size + ent_size <= cpool_size, "Size mismatch");
  1696     *bytes = tag;
  1697     DBG(printf("#%03hd tag=%03hd, ", idx, tag));
  1698     switch(tag) {
  1699       case JVM_CONSTANT_Invalid: {
  1700         DBG(printf("JVM_CONSTANT_Invalid"));
  1701         break;
  1703       case JVM_CONSTANT_Unicode: {
  1704         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
  1705         DBG(printf("JVM_CONSTANT_Unicode"));
  1706         break;
  1708       case JVM_CONSTANT_Utf8: {
  1709         Symbol* sym = symbol_at(idx);
  1710         char*     str = sym->as_utf8();
  1711         // Warning! It's crashing on x86 with len = sym->utf8_length()
  1712         int       len = (int) strlen(str);
  1713         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
  1714         for (int i = 0; i < len; i++) {
  1715             bytes[3+i] = (u1) str[i];
  1717         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
  1718         break;
  1720       case JVM_CONSTANT_Integer: {
  1721         jint val = int_at(idx);
  1722         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1723         break;
  1725       case JVM_CONSTANT_Float: {
  1726         jfloat val = float_at(idx);
  1727         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1728         break;
  1730       case JVM_CONSTANT_Long: {
  1731         jlong val = long_at(idx);
  1732         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1733         idx++;             // Long takes two cpool slots
  1734         break;
  1736       case JVM_CONSTANT_Double: {
  1737         jdouble val = double_at(idx);
  1738         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1739         idx++;             // Double takes two cpool slots
  1740         break;
  1742       case JVM_CONSTANT_Class:
  1743       case JVM_CONSTANT_UnresolvedClass:
  1744       case JVM_CONSTANT_UnresolvedClassInError: {
  1745         *bytes = JVM_CONSTANT_Class;
  1746         Symbol* sym = klass_name_at(idx);
  1747         idx1 = tbl->symbol_to_value(sym);
  1748         assert(idx1 != 0, "Have not found a hashtable entry");
  1749         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1750         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
  1751         break;
  1753       case JVM_CONSTANT_String: {
  1754         *bytes = JVM_CONSTANT_String;
  1755         Symbol* sym = unresolved_string_at(idx);
  1756         idx1 = tbl->symbol_to_value(sym);
  1757         assert(idx1 != 0, "Have not found a hashtable entry");
  1758         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1759         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
  1760         break;
  1762       case JVM_CONSTANT_Fieldref:
  1763       case JVM_CONSTANT_Methodref:
  1764       case JVM_CONSTANT_InterfaceMethodref: {
  1765         idx1 = uncached_klass_ref_index_at(idx);
  1766         idx2 = uncached_name_and_type_ref_index_at(idx);
  1767         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1768         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1769         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
  1770         break;
  1772       case JVM_CONSTANT_NameAndType: {
  1773         idx1 = name_ref_index_at(idx);
  1774         idx2 = signature_ref_index_at(idx);
  1775         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1776         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1777         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
  1778         break;
  1780       case JVM_CONSTANT_ClassIndex: {
  1781         *bytes = JVM_CONSTANT_Class;
  1782         idx1 = klass_index_at(idx);
  1783         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1784         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
  1785         break;
  1787       case JVM_CONSTANT_StringIndex: {
  1788         *bytes = JVM_CONSTANT_String;
  1789         idx1 = string_index_at(idx);
  1790         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1791         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
  1792         break;
  1794       case JVM_CONSTANT_MethodHandle:
  1795       case JVM_CONSTANT_MethodHandleInError: {
  1796         *bytes = JVM_CONSTANT_MethodHandle;
  1797         int kind = method_handle_ref_kind_at_error_ok(idx);
  1798         idx1 = method_handle_index_at_error_ok(idx);
  1799         *(bytes+1) = (unsigned char) kind;
  1800         Bytes::put_Java_u2((address) (bytes+2), idx1);
  1801         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
  1802         break;
  1804       case JVM_CONSTANT_MethodType:
  1805       case JVM_CONSTANT_MethodTypeInError: {
  1806         *bytes = JVM_CONSTANT_MethodType;
  1807         idx1 = method_type_index_at_error_ok(idx);
  1808         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1809         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
  1810         break;
  1812       case JVM_CONSTANT_InvokeDynamic: {
  1813         *bytes = tag;
  1814         idx1 = extract_low_short_from_int(*int_at_addr(idx));
  1815         idx2 = extract_high_short_from_int(*int_at_addr(idx));
  1816         assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
  1817         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1818         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1819         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
  1820         break;
  1823     DBG(printf("\n"));
  1824     bytes += ent_size;
  1825     size  += ent_size;
  1827   assert(size == cpool_size, "Size mismatch");
  1829   // Keep temorarily for debugging until it's stable.
  1830   DBG(print_cpool_bytes(cnt, start_bytes));
  1831   return (int)(bytes - start_bytes);
  1832 } /* end copy_cpool_bytes */
  1834 #undef DBG
  1837 void ConstantPool::set_on_stack(const bool value) {
  1838   if (value) {
  1839     int old_flags = *const_cast<volatile int *>(&_flags);
  1840     while ((old_flags & _on_stack) == 0) {
  1841       int new_flags = old_flags | _on_stack;
  1842       int result = Atomic::cmpxchg(new_flags, &_flags, old_flags);
  1844       if (result == old_flags) {
  1845         // Succeeded.
  1846         MetadataOnStackMark::record(this, Thread::current());
  1847         return;
  1849       old_flags = result;
  1851   } else {
  1852     // Clearing is done single-threadedly.
  1853     _flags &= ~_on_stack;
  1857 // JSR 292 support for patching constant pool oops after the class is linked and
  1858 // the oop array for resolved references are created.
  1859 // We can't do this during classfile parsing, which is how the other indexes are
  1860 // patched.  The other patches are applied early for some error checking
  1861 // so only defer the pseudo_strings.
  1862 void ConstantPool::patch_resolved_references(
  1863                                             GrowableArray<Handle>* cp_patches) {
  1864   assert(EnableInvokeDynamic, "");
  1865   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
  1866     Handle patch = cp_patches->at(index);
  1867     if (patch.not_null()) {
  1868       assert (tag_at(index).is_string(), "should only be string left");
  1869       // Patching a string means pre-resolving it.
  1870       // The spelling in the constant pool is ignored.
  1871       // The constant reference may be any object whatever.
  1872       // If it is not a real interned string, the constant is referred
  1873       // to as a "pseudo-string", and must be presented to the CP
  1874       // explicitly, because it may require scavenging.
  1875       int obj_index = cp_to_object_index(index);
  1876       pseudo_string_at_put(index, obj_index, patch());
  1877       DEBUG_ONLY(cp_patches->at_put(index, Handle());)
  1880 #ifdef ASSERT
  1881   // Ensure that all the patches have been used.
  1882   for (int index = 0; index < cp_patches->length(); index++) {
  1883     assert(cp_patches->at(index).is_null(),
  1884            err_msg("Unused constant pool patch at %d in class file %s",
  1885                    index,
  1886                    pool_holder()->external_name()));
  1888 #endif // ASSERT
  1891 #ifndef PRODUCT
  1893 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
  1894 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
  1895   guarantee(obj->is_constantPool(), "object must be constant pool");
  1896   constantPoolHandle cp(THREAD, (ConstantPool*)obj);
  1897   guarantee(cp->pool_holder() != NULL, "must be fully loaded");
  1899   for (int i = 0; i< cp->length();  i++) {
  1900     if (cp->tag_at(i).is_unresolved_klass()) {
  1901       // This will force loading of the class
  1902       Klass* klass = cp->klass_at(i, CHECK);
  1903       if (klass->oop_is_instance()) {
  1904         // Force initialization of class
  1905         InstanceKlass::cast(klass)->initialize(CHECK);
  1911 #endif
  1914 // Printing
  1916 void ConstantPool::print_on(outputStream* st) const {
  1917   EXCEPTION_MARK;
  1918   assert(is_constantPool(), "must be constantPool");
  1919   st->print_cr("%s", internal_name());
  1920   if (flags() != 0) {
  1921     st->print(" - flags: 0x%x", flags());
  1922     if (has_preresolution()) st->print(" has_preresolution");
  1923     if (on_stack()) st->print(" on_stack");
  1924     st->cr();
  1926   if (pool_holder() != NULL) {
  1927     st->print_cr(" - holder: " INTPTR_FORMAT, pool_holder());
  1929   st->print_cr(" - cache: " INTPTR_FORMAT, cache());
  1930   st->print_cr(" - resolved_references: " INTPTR_FORMAT, (void *)resolved_references());
  1931   st->print_cr(" - reference_map: " INTPTR_FORMAT, reference_map());
  1933   for (int index = 1; index < length(); index++) {      // Index 0 is unused
  1934     ((ConstantPool*)this)->print_entry_on(index, st);
  1935     switch (tag_at(index).value()) {
  1936       case JVM_CONSTANT_Long :
  1937       case JVM_CONSTANT_Double :
  1938         index++;   // Skip entry following eigth-byte constant
  1942   st->cr();
  1945 // Print one constant pool entry
  1946 void ConstantPool::print_entry_on(const int index, outputStream* st) {
  1947   EXCEPTION_MARK;
  1948   st->print(" - %3d : ", index);
  1949   tag_at(index).print_on(st);
  1950   st->print(" : ");
  1951   switch (tag_at(index).value()) {
  1952     case JVM_CONSTANT_Class :
  1953       { Klass* k = klass_at(index, CATCH);
  1954         guarantee(k != NULL, "need klass");
  1955         k->print_value_on(st);
  1956         st->print(" {0x%lx}", (address)k);
  1958       break;
  1959     case JVM_CONSTANT_Fieldref :
  1960     case JVM_CONSTANT_Methodref :
  1961     case JVM_CONSTANT_InterfaceMethodref :
  1962       st->print("klass_index=%d", uncached_klass_ref_index_at(index));
  1963       st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
  1964       break;
  1965     case JVM_CONSTANT_String :
  1966       if (is_pseudo_string_at(index)) {
  1967         oop anObj = pseudo_string_at(index);
  1968         anObj->print_value_on(st);
  1969         st->print(" {0x%lx}", (address)anObj);
  1970       } else {
  1971         unresolved_string_at(index)->print_value_on(st);
  1973       break;
  1974     case JVM_CONSTANT_Integer :
  1975       st->print("%d", int_at(index));
  1976       break;
  1977     case JVM_CONSTANT_Float :
  1978       st->print("%f", float_at(index));
  1979       break;
  1980     case JVM_CONSTANT_Long :
  1981       st->print_jlong(long_at(index));
  1982       break;
  1983     case JVM_CONSTANT_Double :
  1984       st->print("%lf", double_at(index));
  1985       break;
  1986     case JVM_CONSTANT_NameAndType :
  1987       st->print("name_index=%d", name_ref_index_at(index));
  1988       st->print(" signature_index=%d", signature_ref_index_at(index));
  1989       break;
  1990     case JVM_CONSTANT_Utf8 :
  1991       symbol_at(index)->print_value_on(st);
  1992       break;
  1993     case JVM_CONSTANT_UnresolvedClass :               // fall-through
  1994     case JVM_CONSTANT_UnresolvedClassInError: {
  1995       // unresolved_klass_at requires lock or safe world.
  1996       CPSlot entry = slot_at(index);
  1997       if (entry.is_resolved()) {
  1998         entry.get_klass()->print_value_on(st);
  1999       } else {
  2000         entry.get_symbol()->print_value_on(st);
  2003       break;
  2004     case JVM_CONSTANT_MethodHandle :
  2005     case JVM_CONSTANT_MethodHandleInError :
  2006       st->print("ref_kind=%d", method_handle_ref_kind_at_error_ok(index));
  2007       st->print(" ref_index=%d", method_handle_index_at_error_ok(index));
  2008       break;
  2009     case JVM_CONSTANT_MethodType :
  2010     case JVM_CONSTANT_MethodTypeInError :
  2011       st->print("signature_index=%d", method_type_index_at_error_ok(index));
  2012       break;
  2013     case JVM_CONSTANT_InvokeDynamic :
  2015         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
  2016         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
  2017         int argc = invoke_dynamic_argument_count_at(index);
  2018         if (argc > 0) {
  2019           for (int arg_i = 0; arg_i < argc; arg_i++) {
  2020             int arg = invoke_dynamic_argument_index_at(index, arg_i);
  2021             st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
  2023           st->print("}");
  2026       break;
  2027     default:
  2028       ShouldNotReachHere();
  2029       break;
  2031   st->cr();
  2034 void ConstantPool::print_value_on(outputStream* st) const {
  2035   assert(is_constantPool(), "must be constantPool");
  2036   st->print("constant pool [%d]", length());
  2037   if (has_preresolution()) st->print("/preresolution");
  2038   if (operands() != NULL)  st->print("/operands[%d]", operands()->length());
  2039   print_address_on(st);
  2040   st->print(" for ");
  2041   pool_holder()->print_value_on(st);
  2042   if (pool_holder() != NULL) {
  2043     bool extra = (pool_holder()->constants() != this);
  2044     if (extra)  st->print(" (extra)");
  2046   if (cache() != NULL) {
  2047     st->print(" cache=" PTR_FORMAT, cache());
  2051 #if INCLUDE_SERVICES
  2052 // Size Statistics
  2053 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
  2054   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
  2055   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
  2056   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
  2057   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
  2058   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
  2060   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
  2061                    sz->_cp_refmap_bytes;
  2062   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
  2064 #endif // INCLUDE_SERVICES
  2066 // Verification
  2068 void ConstantPool::verify_on(outputStream* st) {
  2069   guarantee(is_constantPool(), "object must be constant pool");
  2070   for (int i = 0; i< length();  i++) {
  2071     constantTag tag = tag_at(i);
  2072     CPSlot entry = slot_at(i);
  2073     if (tag.is_klass()) {
  2074       if (entry.is_resolved()) {
  2075         guarantee(entry.get_klass()->is_klass(),    "should be klass");
  2077     } else if (tag.is_unresolved_klass()) {
  2078       if (entry.is_resolved()) {
  2079         guarantee(entry.get_klass()->is_klass(),    "should be klass");
  2081     } else if (tag.is_symbol()) {
  2082       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2083     } else if (tag.is_string()) {
  2084       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2087   if (cache() != NULL) {
  2088     // Note: cache() can be NULL before a class is completely setup or
  2089     // in temporary constant pools used during constant pool merging
  2090     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
  2092   if (pool_holder() != NULL) {
  2093     // Note: pool_holder() can be NULL in temporary constant pools
  2094     // used during constant pool merging
  2095     guarantee(pool_holder()->is_klass(),    "should be klass");
  2100 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
  2101   char *str = sym->as_utf8();
  2102   unsigned int hash = compute_hash(str, sym->utf8_length());
  2103   unsigned int index = hash % table_size();
  2105   // check if already in map
  2106   // we prefer the first entry since it is more likely to be what was used in
  2107   // the class file
  2108   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  2109     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  2110     if (en->hash() == hash && en->symbol() == sym) {
  2111         return;  // already there
  2115   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
  2116   entry->set_next(bucket(index));
  2117   _buckets[index].set_entry(entry);
  2118   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  2121 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
  2122   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
  2123   char *str = sym->as_utf8();
  2124   int   len = sym->utf8_length();
  2125   unsigned int hash = SymbolHashMap::compute_hash(str, len);
  2126   unsigned int index = hash % table_size();
  2127   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  2128     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  2129     if (en->hash() == hash && en->symbol() == sym) {
  2130       return en;
  2133   return NULL;

mercurial