1 /*
   2  * Copyright (c) 1997, 2017, 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  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderData.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/metadataOnStackMark.hpp"
  29 #include "classfile/stringTable.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/metaspaceClosure.hpp"
  36 #include "memory/metaspaceShared.hpp"
  37 #include "memory/oopFactory.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/constantPool.hpp"
  40 #include "oops/instanceKlass.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "prims/jvm.h"
  45 #include "runtime/fieldType.hpp"
  46 #include "runtime/init.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/signature.hpp"
  49 #include "runtime/vframe.hpp"
  50 #include "utilities/copy.hpp"
  51 #if INCLUDE_ALL_GCS
  52 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  53 #endif // INCLUDE_ALL_GCS
  54 
  55 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  56   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
  57   int size = ConstantPool::size(length);
  58   return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
  59 }
  60 
  61 #ifdef ASSERT
  62 
  63 // MetaspaceObj allocation invariant is calloc equivalent memory
  64 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
  65 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
  66   assert(tags != NULL, "invariant");
  67   const int length = tags->length();
  68   for (int index = 0; index < length; ++index) {
  69     if (JVM_CONSTANT_Invalid != tags->at(index)) {
  70       return false;
  71     }
  72   }
  73   return true;
  74 }
  75 
  76 #endif
  77 
  78 ConstantPool::ConstantPool(Array<u1>* tags) :
  79   _tags(tags),
  80   _length(tags->length()) {
  81 
  82     assert(_tags != NULL, "invariant");
  83     assert(tags->length() == _length, "invariant");
  84     assert(tag_array_is_zero_initialized(tags), "invariant");
  85     assert(0 == flags(), "invariant");
  86     assert(0 == version(), "invariant");
  87     assert(NULL == _pool_holder, "invariant");
  88 }
  89 
  90 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
  91   if (cache() != NULL) {
  92     MetadataFactory::free_array<u2>(loader_data, reference_map());
  93     set_reference_map(NULL);
  94     MetadataFactory::free_metadata(loader_data, cache());
  95     set_cache(NULL);
  96   }
  97 
  98   MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
  99   set_resolved_klasses(NULL);
 100 
 101   MetadataFactory::free_array<jushort>(loader_data, operands());
 102   set_operands(NULL);
 103 
 104   release_C_heap_structures();
 105 
 106   // free tag array
 107   MetadataFactory::free_array<u1>(loader_data, tags());
 108   set_tags(NULL);
 109 }
 110 
 111 void ConstantPool::release_C_heap_structures() {
 112   // walk constant pool and decrement symbol reference counts
 113   unreference_symbols();
 114 }
 115 
 116 void ConstantPool::metaspace_pointers_do(MetaspaceClosure* it) {
 117   log_trace(cds)("Iter(ConstantPool): %p", this);
 118 
 119   it->push(&_tags, MetaspaceClosure::_writable);
 120   it->push(&_cache);
 121   it->push(&_pool_holder);
 122   it->push(&_operands);
 123   it->push(&_resolved_klasses, MetaspaceClosure::_writable);
 124 
 125   for (int i = 0; i < length(); i++) {
 126     // The only MSO's embedded in the CP entries are Symbols:
 127     //   JVM_CONSTANT_String (normal and pseudo)
 128     //   JVM_CONSTANT_Utf8
 129     constantTag ctag = tag_at(i);
 130     if (ctag.is_string() || ctag.is_utf8()) {
 131       it->push(symbol_at_addr(i));
 132     }
 133   }
 134 }
 135 
 136 objArrayOop ConstantPool::resolved_references() const {
 137   return (objArrayOop)_cache->resolved_references();
 138 }
 139 
 140 // Create resolved_references array and mapping array for original cp indexes
 141 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
 142 // to map it back for resolving and some unlikely miscellaneous uses.
 143 // The objects created by invokedynamic are appended to this list.
 144 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
 145                                                   const intStack& reference_map,
 146                                                   int constant_pool_map_length,
 147                                                   TRAPS) {
 148   // Initialized the resolved object cache.
 149   int map_length = reference_map.length();
 150   if (map_length > 0) {
 151     // Only need mapping back to constant pool entries.  The map isn't used for
 152     // invokedynamic resolved_reference entries.  For invokedynamic entries,
 153     // the constant pool cache index has the mapping back to both the constant
 154     // pool and to the resolved reference index.
 155     if (constant_pool_map_length > 0) {
 156       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
 157 
 158       for (int i = 0; i < constant_pool_map_length; i++) {
 159         int x = reference_map.at(i);
 160         assert(x == (int)(jushort) x, "klass index is too big");
 161         om->at_put(i, (jushort)x);
 162       }
 163       set_reference_map(om);
 164     }
 165 
 166     // Create Java array for holding resolved strings, methodHandles,
 167     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
 168     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 169     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 170     set_resolved_references(loader_data->add_handle(refs_handle));
 171   }
 172 }
 173 
 174 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
 175   // A ConstantPool can't possibly have 0xffff valid class entries,
 176   // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
 177   // entry for the class's name. So at most we will have 0xfffe class entries.
 178   // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
 179   // UnresolvedKlass entries that are temporarily created during class redefinition.
 180   assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
 181   assert(resolved_klasses() == NULL, "sanity");
 182   Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
 183   set_resolved_klasses(rk);
 184 }
 185 
 186 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
 187   int len = length();
 188   int num_klasses = 0;
 189   for (int i = 1; i <len; i++) {
 190     switch (tag_at(i).value()) {
 191     case JVM_CONSTANT_ClassIndex:
 192       {
 193         const int class_index = klass_index_at(i);
 194         unresolved_klass_at_put(i, class_index, num_klasses++);
 195       }
 196       break;
 197 #ifndef PRODUCT
 198     case JVM_CONSTANT_Class:
 199     case JVM_CONSTANT_UnresolvedClass:
 200     case JVM_CONSTANT_UnresolvedClassInError:
 201       // All of these should have been reverted back to ClassIndex before calling
 202       // this function.
 203       ShouldNotReachHere();
 204 #endif
 205     }
 206   }
 207   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
 208 }
 209 
 210 // Anonymous class support:
 211 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
 212   assert(is_within_bounds(class_index), "index out of bounds");
 213   assert(is_within_bounds(name_index), "index out of bounds");
 214   assert((resolved_klass_index & 0xffff0000) == 0, "must be");
 215   *int_at_addr(class_index) =
 216     build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
 217 
 218   symbol_at_put(name_index, name);
 219   name->increment_refcount();
 220   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 221   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 222 
 223   // The interpreter assumes when the tag is stored, the klass is resolved
 224   // and the Klass* non-NULL, so we need hardware store ordering here.
 225   if (k != NULL) {
 226     release_tag_at_put(class_index, JVM_CONSTANT_Class);
 227   } else {
 228     release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
 229   }
 230 }
 231 
 232 // Anonymous class support:
 233 void ConstantPool::klass_at_put(int class_index, Klass* k) {
 234   assert(k != NULL, "must be valid klass");
 235   CPKlassSlot kslot = klass_slot_at(class_index);
 236   int resolved_klass_index = kslot.resolved_klass_index();
 237   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 238   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 239 
 240   // The interpreter assumes when the tag is stored, the klass is resolved
 241   // and the Klass* non-NULL, so we need hardware store ordering here.
 242   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 243 }
 244 
 245 #if INCLUDE_CDS_JAVA_HEAP
 246 // Archive the resolved references
 247 void ConstantPool::archive_resolved_references(Thread* THREAD) {
 248   if (_cache == NULL) {
 249     return; // nothing to do
 250   }
 251 
 252   InstanceKlass *ik = pool_holder();
 253   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 254         ik->is_shared_app_class())) {
 255     // Archiving resolved references for classes from non-builtin loaders
 256     // is not yet supported.
 257     set_resolved_references(NULL);
 258     return;
 259   }
 260 
 261   objArrayOop rr = resolved_references();
 262   if (rr != NULL) {
 263     for (int i = 0; i < rr->length(); i++) {
 264       oop p = rr->obj_at(i);
 265       if (p != NULL) {
 266         int index = object_to_cp_index(i);
 267         // Skip the entry if the string hash code is 0 since the string
 268         // is not included in the shared string_table, see StringTable::copy_shared_string.
 269         if (tag_at(index).is_string() && java_lang_String::hash_code(p) != 0) {
 270           oop op = StringTable::create_archived_string(p, THREAD);
 271           // If the String object is not archived (possibly too large),
 272           // NULL is returned. Also set it in the array, so we won't
 273           // have a 'bad' reference in the archived resolved_reference
 274           // array.
 275           rr->obj_at_put(i, op);
 276         } else {
 277           rr->obj_at_put(i, NULL);
 278         }
 279       }
 280     }
 281     oop archived = MetaspaceShared::archive_heap_object(rr, THREAD);
 282     _cache->set_archived_references(archived);
 283     set_resolved_references(NULL);
 284   }
 285 }
 286 #endif
 287 
 288 // CDS support. Create a new resolved_references array.
 289 void ConstantPool::restore_unshareable_info(TRAPS) {
 290   assert(is_constantPool(), "ensure C++ vtable is restored");
 291   assert(on_stack(), "should always be set for shared constant pools");
 292   assert(is_shared(), "should always be set for shared constant pools");
 293   assert(_cache != NULL, "constant pool _cache should not be NULL");
 294 
 295   // Only create the new resolved references array if it hasn't been attempted before
 296   if (resolved_references() != NULL) return;
 297 
 298   // restore the C++ vtable from the shared archive
 299   restore_vtable();
 300 
 301   if (SystemDictionary::Object_klass_loaded()) {
 302     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
 303 #if INCLUDE_CDS_JAVA_HEAP
 304     if (MetaspaceShared::open_archive_heap_region_mapped() &&
 305         _cache->archived_references() != NULL) {
 306       oop archived = _cache->archived_references();
 307       // Make sure GC knows the cached object is now live. This is necessary after
 308       // initial GC marking and during concurrent marking as strong roots are only
 309       // scanned during initial marking (at the start of the GC marking).
 310       assert(UseG1GC, "Requires G1 GC");
 311       G1SATBCardTableModRefBS::enqueue(archived);
 312       // Create handle for the archived resolved reference array object
 313       Handle refs_handle(THREAD, (oop)archived);
 314       set_resolved_references(loader_data->add_handle(refs_handle));
 315     } else
 316 #endif
 317     {
 318       // No mapped archived resolved reference array
 319       // Recreate the object array and add to ClassLoaderData.
 320       int map_length = resolved_reference_length();
 321       if (map_length > 0) {
 322         objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 323         Handle refs_handle(THREAD, (oop)stom);  // must handleize.
 324         set_resolved_references(loader_data->add_handle(refs_handle));
 325       }
 326     }
 327   }
 328 }
 329 
 330 void ConstantPool::remove_unshareable_info() {
 331   // Resolved references are not in the shared archive.
 332   // Save the length for restoration.  It is not necessarily the same length
 333   // as reference_map.length() if invokedynamic is saved. It is needed when
 334   // re-creating the resolved reference array if archived heap data cannot be map
 335   // at runtime.
 336   set_resolved_reference_length(
 337     resolved_references() != NULL ? resolved_references()->length() : 0);
 338 
 339   // If archiving heap objects is not allowed, clear the resolved references.
 340   // Otherwise, it is cleared after the resolved references array is cached
 341   // (see archive_resolved_references()).
 342   if (!MetaspaceShared::is_heap_object_archiving_allowed()) {
 343     set_resolved_references(NULL);
 344   }
 345 
 346   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
 347   // The _on_stack flag is used to prevent ConstantPools from deallocation during
 348   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 349   // we always set _on_stack to true to avoid having to change _flags during runtime.
 350   _flags |= (_on_stack | _is_shared);
 351 }
 352 
 353 int ConstantPool::cp_to_object_index(int cp_index) {
 354   // this is harder don't do this so much.
 355   int i = reference_map()->find(cp_index);
 356   // We might not find the index for jsr292 call.
 357   return (i < 0) ? _no_index_sentinel : i;
 358 }
 359 
 360 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 361   resolved_references()->obj_at_put(obj_index, str);
 362 }
 363 
 364 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 365   ResourceMark rm;
 366   int line_number = -1;
 367   const char * source_file = NULL;
 368   if (JavaThread::current()->has_last_Java_frame()) {
 369     // try to identify the method which called this function.
 370     vframeStream vfst(JavaThread::current());
 371     if (!vfst.at_end()) {
 372       line_number = vfst.method()->line_number_from_bci(vfst.bci());
 373       Symbol* s = vfst.method()->method_holder()->source_file_name();
 374       if (s != NULL) {
 375         source_file = s->as_C_string();
 376       }
 377     }
 378   }
 379   if (k != this_cp->pool_holder()) {
 380     // only print something if the classes are different
 381     if (source_file != NULL) {
 382       log_debug(class, resolve)("%s %s %s:%d",
 383                  this_cp->pool_holder()->external_name(),
 384                  k->external_name(), source_file, line_number);
 385     } else {
 386       log_debug(class, resolve)("%s %s",
 387                  this_cp->pool_holder()->external_name(),
 388                  k->external_name());
 389     }
 390   }
 391 }
 392 
 393 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 394                                    bool save_resolution_error, TRAPS) {
 395   assert(THREAD->is_Java_thread(), "must be a Java thread");
 396 
 397   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 398   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 399   // the entry and tag is not updated atomicly.
 400   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 401   int resolved_klass_index = kslot.resolved_klass_index();
 402   int name_index = kslot.name_index();
 403   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 404 
 405   Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 406   if (klass != NULL) {
 407     return klass;
 408   }
 409 
 410   // This tag doesn't change back to unresolved class unless at a safepoint.
 411   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 412     // The original attempt to resolve this constant pool entry failed so find the
 413     // class of the original error and throw another error of the same class
 414     // (JVMS 5.4.3).
 415     // If there is a detail message, pass that detail message to the error.
 416     // The JVMS does not strictly require us to duplicate the same detail message,
 417     // or any internal exception fields such as cause or stacktrace.  But since the
 418     // detail message is often a class name or other literal string, we will repeat it
 419     // if we can find it in the symbol table.
 420     throw_resolution_error(this_cp, which, CHECK_0);
 421     ShouldNotReachHere();
 422   }
 423 
 424   Handle mirror_handle;
 425   Symbol* name = this_cp->symbol_at(name_index);
 426   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 427   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 428   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 429   if (!HAS_PENDING_EXCEPTION) {
 430     // preserve the resolved klass from unloading
 431     mirror_handle = Handle(THREAD, k->java_mirror());
 432     // Do access check for klasses
 433     verify_constant_pool_resolve(this_cp, k, THREAD);
 434   }
 435 
 436   // Failed to resolve class. We must record the errors so that subsequent attempts
 437   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 438   if (HAS_PENDING_EXCEPTION) {
 439     if (save_resolution_error) {
 440       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
 441       // If CHECK_NULL above doesn't return the exception, that means that
 442       // some other thread has beaten us and has resolved the class.
 443       // To preserve old behavior, we return the resolved class.
 444       klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 445       assert(klass != NULL, "must be resolved if exception was cleared");
 446       return klass;
 447     } else {
 448       return NULL;  // return the pending exception
 449     }
 450   }
 451 
 452   // Make this class loader depend upon the class loader owning the class reference
 453   ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
 454   this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
 455 
 456   // logging for class+resolve.
 457   if (log_is_enabled(Debug, class, resolve)){
 458     trace_class_resolution(this_cp, k);
 459   }
 460   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 461   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 462   // The interpreter assumes when the tag is stored, the klass is resolved
 463   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
 464   // hardware store ordering here.
 465   this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
 466   return k;
 467 }
 468 
 469 
 470 // Does not update ConstantPool* - to avoid any exception throwing. Used
 471 // by compiler and exception handling.  Also used to avoid classloads for
 472 // instanceof operations. Returns NULL if the class has not been loaded or
 473 // if the verification of constant pool failed
 474 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 475   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 476   int resolved_klass_index = kslot.resolved_klass_index();
 477   int name_index = kslot.name_index();
 478   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 479 
 480   Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
 481   if (k != NULL) {
 482     return k;
 483   } else {
 484     Thread *thread = Thread::current();
 485     Symbol* name = this_cp->symbol_at(name_index);
 486     oop loader = this_cp->pool_holder()->class_loader();
 487     oop protection_domain = this_cp->pool_holder()->protection_domain();
 488     Handle h_prot (thread, protection_domain);
 489     Handle h_loader (thread, loader);
 490     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
 491 
 492     if (k != NULL) {
 493       // Make sure that resolving is legal
 494       EXCEPTION_MARK;
 495       // return NULL if verification fails
 496       verify_constant_pool_resolve(this_cp, k, THREAD);
 497       if (HAS_PENDING_EXCEPTION) {
 498         CLEAR_PENDING_EXCEPTION;
 499         return NULL;
 500       }
 501       return k;
 502     } else {
 503       return k;
 504     }
 505   }
 506 }
 507 
 508 
 509 Klass* ConstantPool::klass_ref_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 510   return klass_at_if_loaded(this_cp, this_cp->klass_ref_index_at(which));
 511 }
 512 
 513 
 514 Method* ConstantPool::method_at_if_loaded(const constantPoolHandle& cpool,
 515                                                    int which) {
 516   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 517   int cache_index = decode_cpcache_index(which, true);
 518   if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
 519     // FIXME: should be an assert
 520     log_debug(class, resolve)("bad operand %d in:", which); cpool->print();
 521     return NULL;
 522   }
 523   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 524   return e->method_if_resolved(cpool);
 525 }
 526 
 527 
 528 bool ConstantPool::has_appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
 529   if (cpool->cache() == NULL)  return false;  // nothing to load yet
 530   int cache_index = decode_cpcache_index(which, true);
 531   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 532   return e->has_appendix();
 533 }
 534 
 535 oop ConstantPool::appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
 536   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 537   int cache_index = decode_cpcache_index(which, true);
 538   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 539   return e->appendix_if_resolved(cpool);
 540 }
 541 
 542 
 543 bool ConstantPool::has_method_type_at_if_loaded(const constantPoolHandle& cpool, int which) {
 544   if (cpool->cache() == NULL)  return false;  // nothing to load yet
 545   int cache_index = decode_cpcache_index(which, true);
 546   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 547   return e->has_method_type();
 548 }
 549 
 550 oop ConstantPool::method_type_at_if_loaded(const constantPoolHandle& cpool, int which) {
 551   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 552   int cache_index = decode_cpcache_index(which, true);
 553   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 554   return e->method_type_if_resolved(cpool);
 555 }
 556 
 557 
 558 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
 559   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 560   return symbol_at(name_index);
 561 }
 562 
 563 
 564 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
 565   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 566   return symbol_at(signature_index);
 567 }
 568 
 569 
 570 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
 571   int i = which;
 572   if (!uncached && cache() != NULL) {
 573     if (ConstantPool::is_invokedynamic_index(which)) {
 574       // Invokedynamic index is index into the constant pool cache
 575       int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
 576       pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
 577       assert(tag_at(pool_index).is_name_and_type(), "");
 578       return pool_index;
 579     }
 580     // change byte-ordering and go via cache
 581     i = remap_instruction_operand_from_cache(which);
 582   } else {
 583     if (tag_at(which).is_invoke_dynamic()) {
 584       int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
 585       assert(tag_at(pool_index).is_name_and_type(), "");
 586       return pool_index;
 587     }
 588   }
 589   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 590   assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
 591   jint ref_index = *int_at_addr(i);
 592   return extract_high_short_from_int(ref_index);
 593 }
 594 
 595 constantTag ConstantPool::impl_tag_ref_at(int which, bool uncached) {
 596   int pool_index = which;
 597   if (!uncached && cache() != NULL) {
 598     if (ConstantPool::is_invokedynamic_index(which)) {
 599       // Invokedynamic index is index into resolved_references
 600       pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
 601     } else {
 602       // change byte-ordering and go via cache
 603       pool_index = remap_instruction_operand_from_cache(which);
 604     }
 605   }
 606   return tag_at(pool_index);
 607 }
 608 
 609 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
 610   guarantee(!ConstantPool::is_invokedynamic_index(which),
 611             "an invokedynamic instruction does not have a klass");
 612   int i = which;
 613   if (!uncached && cache() != NULL) {
 614     // change byte-ordering and go via cache
 615     i = remap_instruction_operand_from_cache(which);
 616   }
 617   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 618   jint ref_index = *int_at_addr(i);
 619   return extract_low_short_from_int(ref_index);
 620 }
 621 
 622 
 623 
 624 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
 625   int cpc_index = operand;
 626   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
 627   assert((int)(u2)cpc_index == cpc_index, "clean u2");
 628   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
 629   return member_index;
 630 }
 631 
 632 
 633 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) {
 634  if (k->is_instance_klass() || k->is_objArray_klass()) {
 635     InstanceKlass* holder = this_cp->pool_holder();
 636     Klass* elem = k->is_instance_klass() ? k : ObjArrayKlass::cast(k)->bottom_klass();
 637 
 638     // The element type could be a typeArray - we only need the access check if it is
 639     // an reference to another class
 640     if (elem->is_instance_klass()) {
 641       LinkResolver::check_klass_accessability(holder, elem, CHECK);
 642     }
 643   }
 644 }
 645 
 646 
 647 int ConstantPool::name_ref_index_at(int which_nt) {
 648   jint ref_index = name_and_type_at(which_nt);
 649   return extract_low_short_from_int(ref_index);
 650 }
 651 
 652 
 653 int ConstantPool::signature_ref_index_at(int which_nt) {
 654   jint ref_index = name_and_type_at(which_nt);
 655   return extract_high_short_from_int(ref_index);
 656 }
 657 
 658 
 659 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
 660   return klass_at(klass_ref_index_at(which), THREAD);
 661 }
 662 
 663 Symbol* ConstantPool::klass_name_at(int which) const {
 664   return symbol_at(klass_slot_at(which).name_index());
 665 }
 666 
 667 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
 668   jint ref_index = klass_ref_index_at(which);
 669   return klass_at_noresolve(ref_index);
 670 }
 671 
 672 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
 673   jint ref_index = uncached_klass_ref_index_at(which);
 674   return klass_at_noresolve(ref_index);
 675 }
 676 
 677 char* ConstantPool::string_at_noresolve(int which) {
 678   return unresolved_string_at(which)->as_C_string();
 679 }
 680 
 681 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
 682   return FieldType::basic_type(symbol_at(which));
 683 }
 684 
 685 
 686 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) {
 687   for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused
 688     if (this_cp->tag_at(index).is_string()) {
 689       this_cp->string_at(index, CHECK);
 690     }
 691   }
 692 }
 693 
 694 bool ConstantPool::resolve_class_constants(TRAPS) {
 695   constantPoolHandle cp(THREAD, this);
 696   for (int index = 1; index < length(); index++) { // Index 0 is unused
 697     if (tag_at(index).is_string()) {
 698       Symbol* sym = cp->unresolved_string_at(index);
 699       // Look up only. Only resolve references to already interned strings.
 700       oop str = StringTable::lookup(sym);
 701       if (str != NULL) {
 702         int cache_index = cp->cp_to_object_index(index);
 703         cp->string_at_put(index, cache_index, str);
 704       }
 705     }
 706   }
 707   return true;
 708 }
 709 
 710 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
 711   // Dig out the detailed message to reuse if possible
 712   Symbol* message = java_lang_Throwable::detail_message(pending_exception);
 713   if (message != NULL) {
 714     return message;
 715   }
 716 
 717   // Return specific message for the tag
 718   switch (tag.value()) {
 719   case JVM_CONSTANT_UnresolvedClass:
 720     // return the class name in the error message
 721     message = this_cp->klass_name_at(which);
 722     break;
 723   case JVM_CONSTANT_MethodHandle:
 724     // return the method handle name in the error message
 725     message = this_cp->method_handle_name_ref_at(which);
 726     break;
 727   case JVM_CONSTANT_MethodType:
 728     // return the method type signature in the error message
 729     message = this_cp->method_type_signature_at(which);
 730     break;
 731   default:
 732     ShouldNotReachHere();
 733   }
 734 
 735   return message;
 736 }
 737 
 738 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
 739   Symbol* message = NULL;
 740   Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message);
 741   assert(error != NULL && message != NULL, "checking");
 742   CLEAR_PENDING_EXCEPTION;
 743   ResourceMark rm;
 744   THROW_MSG(error, message->as_C_string());
 745 }
 746 
 747 // If resolution for Class, MethodHandle or MethodType fails, save the exception
 748 // in the resolution error table, so that the same exception is thrown again.
 749 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int which,
 750                                             constantTag tag, TRAPS) {
 751   Symbol* error = PENDING_EXCEPTION->klass()->name();
 752 
 753   int error_tag = tag.error_value();
 754 
 755   if (!PENDING_EXCEPTION->
 756     is_a(SystemDictionary::LinkageError_klass())) {
 757     // Just throw the exception and don't prevent these classes from
 758     // being loaded due to virtual machine errors like StackOverflow
 759     // and OutOfMemoryError, etc, or if the thread was hit by stop()
 760     // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
 761   } else if (this_cp->tag_at(which).value() != error_tag) {
 762     Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION);
 763     SystemDictionary::add_resolution_error(this_cp, which, error, message);
 764     // CAS in the tag.  If a thread beat us to registering this error that's fine.
 765     // If another thread resolved the reference, this is a race condition. This
 766     // thread may have had a security manager or something temporary.
 767     // This doesn't deterministically get an error.   So why do we save this?
 768     // We save this because jvmti can add classes to the bootclass path after
 769     // this error, so it needs to get the same error if the error is first.
 770     jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag,
 771                             (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value());
 772     if (old_tag != error_tag && old_tag != tag.value()) {
 773       // MethodHandles and MethodType doesn't change to resolved version.
 774       assert(this_cp->tag_at(which).is_klass(), "Wrong tag value");
 775       // Forget the exception and use the resolved class.
 776       CLEAR_PENDING_EXCEPTION;
 777     }
 778   } else {
 779     // some other thread put this in error state
 780     throw_resolution_error(this_cp, which, CHECK);
 781   }
 782 }
 783 
 784 // Called to resolve constants in the constant pool and return an oop.
 785 // Some constant pool entries cache their resolved oop. This is also
 786 // called to create oops from constants to use in arguments for invokedynamic
 787 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index, TRAPS) {
 788   oop result_oop = NULL;
 789   Handle throw_exception;
 790 
 791   if (cache_index == _possible_index_sentinel) {
 792     // It is possible that this constant is one which is cached in the objects.
 793     // We'll do a linear search.  This should be OK because this usage is rare.
 794     assert(index > 0, "valid index");
 795     cache_index = this_cp->cp_to_object_index(index);
 796   }
 797   assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
 798   assert(index == _no_index_sentinel || index >= 0, "");
 799 
 800   if (cache_index >= 0) {
 801     result_oop = this_cp->resolved_references()->obj_at(cache_index);
 802     if (result_oop != NULL) {
 803       return result_oop;
 804       // That was easy...
 805     }
 806     index = this_cp->object_to_cp_index(cache_index);
 807   }
 808 
 809   jvalue prim_value;  // temp used only in a few cases below
 810 
 811   constantTag tag = this_cp->tag_at(index);
 812 
 813   switch (tag.value()) {
 814 
 815   case JVM_CONSTANT_UnresolvedClass:
 816   case JVM_CONSTANT_UnresolvedClassInError:
 817   case JVM_CONSTANT_Class:
 818     {
 819       assert(cache_index == _no_index_sentinel, "should not have been set");
 820       Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL);
 821       // ldc wants the java mirror.
 822       result_oop = resolved->java_mirror();
 823       break;
 824     }
 825 
 826   case JVM_CONSTANT_String:
 827     assert(cache_index != _no_index_sentinel, "should have been set");
 828     if (this_cp->is_pseudo_string_at(index)) {
 829       result_oop = this_cp->pseudo_string_at(index, cache_index);
 830       break;
 831     }
 832     result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL);
 833     break;
 834 
 835   case JVM_CONSTANT_MethodHandleInError:
 836   case JVM_CONSTANT_MethodTypeInError:
 837     {
 838       throw_resolution_error(this_cp, index, CHECK_NULL);
 839       break;
 840     }
 841 
 842   case JVM_CONSTANT_MethodHandle:
 843     {
 844       int ref_kind                 = this_cp->method_handle_ref_kind_at(index);
 845       int callee_index             = this_cp->method_handle_klass_index_at(index);
 846       Symbol*  name =      this_cp->method_handle_name_ref_at(index);
 847       Symbol*  signature = this_cp->method_handle_signature_ref_at(index);
 848       constantTag m_tag  = this_cp->tag_at(this_cp->method_handle_index_at(index));
 849       { ResourceMark rm(THREAD);
 850         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
 851                               ref_kind, index, this_cp->method_handle_index_at(index),
 852                               callee_index, name->as_C_string(), signature->as_C_string());
 853       }
 854 
 855       Klass* callee = klass_at_impl(this_cp, callee_index, true, CHECK_NULL);
 856 
 857       // Check constant pool method consistency
 858       if ((callee->is_interface() && m_tag.is_method()) ||
 859           ((!callee->is_interface() && m_tag.is_interface_method()))) {
 860         ResourceMark rm(THREAD);
 861         char buf[400];
 862         jio_snprintf(buf, sizeof(buf),
 863           "Inconsistent constant pool data in classfile for class %s. "
 864           "Method %s%s at index %d is %s and should be %s",
 865           callee->name()->as_C_string(), name->as_C_string(), signature->as_C_string(), index,
 866           callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
 867           callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
 868         THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 869       }
 870 
 871       Klass* klass = this_cp->pool_holder();
 872       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
 873                                                                    callee, name, signature,
 874                                                                    THREAD);
 875       result_oop = value();
 876       if (HAS_PENDING_EXCEPTION) {
 877         save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
 878       }
 879       break;
 880     }
 881 
 882   case JVM_CONSTANT_MethodType:
 883     {
 884       Symbol*  signature = this_cp->method_type_signature_at(index);
 885       { ResourceMark rm(THREAD);
 886         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
 887                               index, this_cp->method_type_index_at(index),
 888                               signature->as_C_string());
 889       }
 890       Klass* klass = this_cp->pool_holder();
 891       Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
 892       result_oop = value();
 893       if (HAS_PENDING_EXCEPTION) {
 894         save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
 895       }
 896       break;
 897     }
 898 
 899   case JVM_CONSTANT_Integer:
 900     assert(cache_index == _no_index_sentinel, "should not have been set");
 901     prim_value.i = this_cp->int_at(index);
 902     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
 903     break;
 904 
 905   case JVM_CONSTANT_Float:
 906     assert(cache_index == _no_index_sentinel, "should not have been set");
 907     prim_value.f = this_cp->float_at(index);
 908     result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
 909     break;
 910 
 911   case JVM_CONSTANT_Long:
 912     assert(cache_index == _no_index_sentinel, "should not have been set");
 913     prim_value.j = this_cp->long_at(index);
 914     result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
 915     break;
 916 
 917   case JVM_CONSTANT_Double:
 918     assert(cache_index == _no_index_sentinel, "should not have been set");
 919     prim_value.d = this_cp->double_at(index);
 920     result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
 921     break;
 922 
 923   default:
 924     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
 925                               this_cp(), index, cache_index, tag.value()));
 926     assert(false, "unexpected constant tag");
 927     break;
 928   }
 929 
 930   if (cache_index >= 0) {
 931     // Benign race condition:  resolved_references may already be filled in.
 932     // The important thing here is that all threads pick up the same result.
 933     // It doesn't matter which racing thread wins, as long as only one
 934     // result is used by all threads, and all future queries.
 935     oop old_result = this_cp->resolved_references()->atomic_compare_exchange_oop(cache_index, result_oop, NULL);
 936     if (old_result == NULL) {
 937       return result_oop;  // was installed
 938     } else {
 939       // Return the winning thread's result.  This can be different than
 940       // the result here for MethodHandles.
 941       return old_result;
 942     }
 943   } else {
 944     return result_oop;
 945   }
 946 }
 947 
 948 oop ConstantPool::uncached_string_at(int which, TRAPS) {
 949   Symbol* sym = unresolved_string_at(which);
 950   oop str = StringTable::intern(sym, CHECK_(NULL));
 951   assert(java_lang_String::is_instance(str), "must be string");
 952   return str;
 953 }
 954 
 955 
 956 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) {
 957   assert(this_cp->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
 958 
 959   Handle bsm;
 960   int argc;
 961   {
 962     // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
 963     // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
 964     // It is accompanied by the optional arguments.
 965     int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index);
 966     oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
 967     if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
 968       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
 969     }
 970 
 971     // Extract the optional static arguments.
 972     argc = this_cp->invoke_dynamic_argument_count_at(index);
 973     if (argc == 0)  return bsm_oop;
 974 
 975     bsm = Handle(THREAD, bsm_oop);
 976   }
 977 
 978   objArrayHandle info;
 979   {
 980     objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
 981     info = objArrayHandle(THREAD, info_oop);
 982   }
 983 
 984   info->obj_at_put(0, bsm());
 985   for (int i = 0; i < argc; i++) {
 986     int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i);
 987     oop arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
 988     info->obj_at_put(1+i, arg_oop);
 989   }
 990 
 991   return info();
 992 }
 993 
 994 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) {
 995   // If the string has already been interned, this entry will be non-null
 996   oop str = this_cp->resolved_references()->obj_at(obj_index);
 997   if (str != NULL) return str;
 998   Symbol* sym = this_cp->unresolved_string_at(which);
 999   str = StringTable::intern(sym, CHECK_(NULL));
1000   this_cp->string_at_put(which, obj_index, str);
1001   assert(java_lang_String::is_instance(str), "must be string");
1002   return str;
1003 }
1004 
1005 
1006 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
1007   // Names are interned, so we can compare Symbol*s directly
1008   Symbol* cp_name = klass_name_at(which);
1009   return (cp_name == k->name());
1010 }
1011 
1012 
1013 // Iterate over symbols and decrement ones which are Symbol*s
1014 // This is done during GC.
1015 // Only decrement the UTF8 symbols. Strings point to
1016 // these symbols but didn't increment the reference count.
1017 void ConstantPool::unreference_symbols() {
1018   for (int index = 1; index < length(); index++) { // Index 0 is unused
1019     constantTag tag = tag_at(index);
1020     if (tag.is_symbol()) {
1021       symbol_at(index)->decrement_refcount();
1022     }
1023   }
1024 }
1025 
1026 
1027 // Compare this constant pool's entry at index1 to the constant pool
1028 // cp2's entry at index2.
1029 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
1030        int index2, TRAPS) {
1031 
1032   // The error tags are equivalent to non-error tags when comparing
1033   jbyte t1 = tag_at(index1).non_error_value();
1034   jbyte t2 = cp2->tag_at(index2).non_error_value();
1035 
1036   if (t1 != t2) {
1037     // Not the same entry type so there is nothing else to check. Note
1038     // that this style of checking will consider resolved/unresolved
1039     // class pairs as different.
1040     // From the ConstantPool* API point of view, this is correct
1041     // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
1042     // plays out in the context of ConstantPool* merging.
1043     return false;
1044   }
1045 
1046   switch (t1) {
1047   case JVM_CONSTANT_Class:
1048   {
1049     Klass* k1 = klass_at(index1, CHECK_false);
1050     Klass* k2 = cp2->klass_at(index2, CHECK_false);
1051     if (k1 == k2) {
1052       return true;
1053     }
1054   } break;
1055 
1056   case JVM_CONSTANT_ClassIndex:
1057   {
1058     int recur1 = klass_index_at(index1);
1059     int recur2 = cp2->klass_index_at(index2);
1060     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1061     if (match) {
1062       return true;
1063     }
1064   } break;
1065 
1066   case JVM_CONSTANT_Double:
1067   {
1068     jdouble d1 = double_at(index1);
1069     jdouble d2 = cp2->double_at(index2);
1070     if (d1 == d2) {
1071       return true;
1072     }
1073   } break;
1074 
1075   case JVM_CONSTANT_Fieldref:
1076   case JVM_CONSTANT_InterfaceMethodref:
1077   case JVM_CONSTANT_Methodref:
1078   {
1079     int recur1 = uncached_klass_ref_index_at(index1);
1080     int recur2 = cp2->uncached_klass_ref_index_at(index2);
1081     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1082     if (match) {
1083       recur1 = uncached_name_and_type_ref_index_at(index1);
1084       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
1085       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1086       if (match) {
1087         return true;
1088       }
1089     }
1090   } break;
1091 
1092   case JVM_CONSTANT_Float:
1093   {
1094     jfloat f1 = float_at(index1);
1095     jfloat f2 = cp2->float_at(index2);
1096     if (f1 == f2) {
1097       return true;
1098     }
1099   } break;
1100 
1101   case JVM_CONSTANT_Integer:
1102   {
1103     jint i1 = int_at(index1);
1104     jint i2 = cp2->int_at(index2);
1105     if (i1 == i2) {
1106       return true;
1107     }
1108   } break;
1109 
1110   case JVM_CONSTANT_Long:
1111   {
1112     jlong l1 = long_at(index1);
1113     jlong l2 = cp2->long_at(index2);
1114     if (l1 == l2) {
1115       return true;
1116     }
1117   } break;
1118 
1119   case JVM_CONSTANT_NameAndType:
1120   {
1121     int recur1 = name_ref_index_at(index1);
1122     int recur2 = cp2->name_ref_index_at(index2);
1123     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1124     if (match) {
1125       recur1 = signature_ref_index_at(index1);
1126       recur2 = cp2->signature_ref_index_at(index2);
1127       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1128       if (match) {
1129         return true;
1130       }
1131     }
1132   } break;
1133 
1134   case JVM_CONSTANT_StringIndex:
1135   {
1136     int recur1 = string_index_at(index1);
1137     int recur2 = cp2->string_index_at(index2);
1138     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1139     if (match) {
1140       return true;
1141     }
1142   } break;
1143 
1144   case JVM_CONSTANT_UnresolvedClass:
1145   {
1146     Symbol* k1 = klass_name_at(index1);
1147     Symbol* k2 = cp2->klass_name_at(index2);
1148     if (k1 == k2) {
1149       return true;
1150     }
1151   } break;
1152 
1153   case JVM_CONSTANT_MethodType:
1154   {
1155     int k1 = method_type_index_at(index1);
1156     int k2 = cp2->method_type_index_at(index2);
1157     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1158     if (match) {
1159       return true;
1160     }
1161   } break;
1162 
1163   case JVM_CONSTANT_MethodHandle:
1164   {
1165     int k1 = method_handle_ref_kind_at(index1);
1166     int k2 = cp2->method_handle_ref_kind_at(index2);
1167     if (k1 == k2) {
1168       int i1 = method_handle_index_at(index1);
1169       int i2 = cp2->method_handle_index_at(index2);
1170       bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
1171       if (match) {
1172         return true;
1173       }
1174     }
1175   } break;
1176 
1177   case JVM_CONSTANT_InvokeDynamic:
1178   {
1179     int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
1180     int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
1181     int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
1182     int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
1183     // separate statements and variables because CHECK_false is used
1184     bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
1185     bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
1186     return (match_entry && match_operand);
1187   } break;
1188 
1189   case JVM_CONSTANT_String:
1190   {
1191     Symbol* s1 = unresolved_string_at(index1);
1192     Symbol* s2 = cp2->unresolved_string_at(index2);
1193     if (s1 == s2) {
1194       return true;
1195     }
1196   } break;
1197 
1198   case JVM_CONSTANT_Utf8:
1199   {
1200     Symbol* s1 = symbol_at(index1);
1201     Symbol* s2 = cp2->symbol_at(index2);
1202     if (s1 == s2) {
1203       return true;
1204     }
1205   } break;
1206 
1207   // Invalid is used as the tag for the second constant pool entry
1208   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1209   // not be seen by itself.
1210   case JVM_CONSTANT_Invalid: // fall through
1211 
1212   default:
1213     ShouldNotReachHere();
1214     break;
1215   }
1216 
1217   return false;
1218 } // end compare_entry_to()
1219 
1220 
1221 // Resize the operands array with delta_len and delta_size.
1222 // Used in RedefineClasses for CP merge.
1223 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
1224   int old_len  = operand_array_length(operands());
1225   int new_len  = old_len + delta_len;
1226   int min_len  = (delta_len > 0) ? old_len : new_len;
1227 
1228   int old_size = operands()->length();
1229   int new_size = old_size + delta_size;
1230   int min_size = (delta_size > 0) ? old_size : new_size;
1231 
1232   ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1233   Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
1234 
1235   // Set index in the resized array for existing elements only
1236   for (int idx = 0; idx < min_len; idx++) {
1237     int offset = operand_offset_at(idx);                       // offset in original array
1238     operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
1239   }
1240   // Copy the bootstrap specifiers only
1241   Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
1242                                new_ops->adr_at(2*new_len),
1243                                (min_size - 2*min_len) * sizeof(u2));
1244   // Explicitly deallocate old operands array.
1245   // Note, it is not needed for 7u backport.
1246   if ( operands() != NULL) { // the safety check
1247     MetadataFactory::free_array<u2>(loader_data, operands());
1248   }
1249   set_operands(new_ops);
1250 } // end resize_operands()
1251 
1252 
1253 // Extend the operands array with the length and size of the ext_cp operands.
1254 // Used in RedefineClasses for CP merge.
1255 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) {
1256   int delta_len = operand_array_length(ext_cp->operands());
1257   if (delta_len == 0) {
1258     return; // nothing to do
1259   }
1260   int delta_size = ext_cp->operands()->length();
1261 
1262   assert(delta_len  > 0 && delta_size > 0, "extended operands array must be bigger");
1263 
1264   if (operand_array_length(operands()) == 0) {
1265     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1266     Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
1267     // The first element index defines the offset of second part
1268     operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
1269     set_operands(new_ops);
1270   } else {
1271     resize_operands(delta_len, delta_size, CHECK);
1272   }
1273 
1274 } // end extend_operands()
1275 
1276 
1277 // Shrink the operands array to a smaller array with new_len length.
1278 // Used in RedefineClasses for CP merge.
1279 void ConstantPool::shrink_operands(int new_len, TRAPS) {
1280   int old_len = operand_array_length(operands());
1281   if (new_len == old_len) {
1282     return; // nothing to do
1283   }
1284   assert(new_len < old_len, "shrunken operands array must be smaller");
1285 
1286   int free_base  = operand_next_offset_at(new_len - 1);
1287   int delta_len  = new_len - old_len;
1288   int delta_size = 2*delta_len + free_base - operands()->length();
1289 
1290   resize_operands(delta_len, delta_size, CHECK);
1291 
1292 } // end shrink_operands()
1293 
1294 
1295 void ConstantPool::copy_operands(const constantPoolHandle& from_cp,
1296                                  const constantPoolHandle& to_cp,
1297                                  TRAPS) {
1298 
1299   int from_oplen = operand_array_length(from_cp->operands());
1300   int old_oplen  = operand_array_length(to_cp->operands());
1301   if (from_oplen != 0) {
1302     ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
1303     // append my operands to the target's operands array
1304     if (old_oplen == 0) {
1305       // Can't just reuse from_cp's operand list because of deallocation issues
1306       int len = from_cp->operands()->length();
1307       Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
1308       Copy::conjoint_memory_atomic(
1309           from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
1310       to_cp->set_operands(new_ops);
1311     } else {
1312       int old_len  = to_cp->operands()->length();
1313       int from_len = from_cp->operands()->length();
1314       int old_off  = old_oplen * sizeof(u2);
1315       int from_off = from_oplen * sizeof(u2);
1316       // Use the metaspace for the destination constant pool
1317       Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
1318       int fillp = 0, len = 0;
1319       // first part of dest
1320       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
1321                                    new_operands->adr_at(fillp),
1322                                    (len = old_off) * sizeof(u2));
1323       fillp += len;
1324       // first part of src
1325       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
1326                                    new_operands->adr_at(fillp),
1327                                    (len = from_off) * sizeof(u2));
1328       fillp += len;
1329       // second part of dest
1330       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
1331                                    new_operands->adr_at(fillp),
1332                                    (len = old_len - old_off) * sizeof(u2));
1333       fillp += len;
1334       // second part of src
1335       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
1336                                    new_operands->adr_at(fillp),
1337                                    (len = from_len - from_off) * sizeof(u2));
1338       fillp += len;
1339       assert(fillp == new_operands->length(), "");
1340 
1341       // Adjust indexes in the first part of the copied operands array.
1342       for (int j = 0; j < from_oplen; j++) {
1343         int offset = operand_offset_at(new_operands, old_oplen + j);
1344         assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
1345         offset += old_len;  // every new tuple is preceded by old_len extra u2's
1346         operand_offset_at_put(new_operands, old_oplen + j, offset);
1347       }
1348 
1349       // replace target operands array with combined array
1350       to_cp->set_operands(new_operands);
1351     }
1352   }
1353 } // end copy_operands()
1354 
1355 
1356 // Copy this constant pool's entries at start_i to end_i (inclusive)
1357 // to the constant pool to_cp's entries starting at to_i. A total of
1358 // (end_i - start_i) + 1 entries are copied.
1359 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i,
1360        const constantPoolHandle& to_cp, int to_i, TRAPS) {
1361 
1362 
1363   int dest_i = to_i;  // leave original alone for debug purposes
1364 
1365   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1366     copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
1367 
1368     switch (from_cp->tag_at(src_i).value()) {
1369     case JVM_CONSTANT_Double:
1370     case JVM_CONSTANT_Long:
1371       // double and long take two constant pool entries
1372       src_i += 2;
1373       dest_i += 2;
1374       break;
1375 
1376     default:
1377       // all others take one constant pool entry
1378       src_i++;
1379       dest_i++;
1380       break;
1381     }
1382   }
1383   copy_operands(from_cp, to_cp, CHECK);
1384 
1385 } // end copy_cp_to_impl()
1386 
1387 
1388 // Copy this constant pool's entry at from_i to the constant pool
1389 // to_cp's entry at to_i.
1390 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1391                                         const constantPoolHandle& to_cp, int to_i,
1392                                         TRAPS) {
1393 
1394   int tag = from_cp->tag_at(from_i).value();
1395   switch (tag) {
1396   case JVM_CONSTANT_ClassIndex:
1397   {
1398     jint ki = from_cp->klass_index_at(from_i);
1399     to_cp->klass_index_at_put(to_i, ki);
1400   } break;
1401 
1402   case JVM_CONSTANT_Double:
1403   {
1404     jdouble d = from_cp->double_at(from_i);
1405     to_cp->double_at_put(to_i, d);
1406     // double takes two constant pool entries so init second entry's tag
1407     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1408   } break;
1409 
1410   case JVM_CONSTANT_Fieldref:
1411   {
1412     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1413     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1414     to_cp->field_at_put(to_i, class_index, name_and_type_index);
1415   } break;
1416 
1417   case JVM_CONSTANT_Float:
1418   {
1419     jfloat f = from_cp->float_at(from_i);
1420     to_cp->float_at_put(to_i, f);
1421   } break;
1422 
1423   case JVM_CONSTANT_Integer:
1424   {
1425     jint i = from_cp->int_at(from_i);
1426     to_cp->int_at_put(to_i, i);
1427   } break;
1428 
1429   case JVM_CONSTANT_InterfaceMethodref:
1430   {
1431     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1432     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1433     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1434   } break;
1435 
1436   case JVM_CONSTANT_Long:
1437   {
1438     jlong l = from_cp->long_at(from_i);
1439     to_cp->long_at_put(to_i, l);
1440     // long takes two constant pool entries so init second entry's tag
1441     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1442   } break;
1443 
1444   case JVM_CONSTANT_Methodref:
1445   {
1446     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1447     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1448     to_cp->method_at_put(to_i, class_index, name_and_type_index);
1449   } break;
1450 
1451   case JVM_CONSTANT_NameAndType:
1452   {
1453     int name_ref_index = from_cp->name_ref_index_at(from_i);
1454     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1455     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1456   } break;
1457 
1458   case JVM_CONSTANT_StringIndex:
1459   {
1460     jint si = from_cp->string_index_at(from_i);
1461     to_cp->string_index_at_put(to_i, si);
1462   } break;
1463 
1464   case JVM_CONSTANT_Class:
1465   case JVM_CONSTANT_UnresolvedClass:
1466   case JVM_CONSTANT_UnresolvedClassInError:
1467   {
1468     // Revert to JVM_CONSTANT_ClassIndex
1469     int name_index = from_cp->klass_slot_at(from_i).name_index();
1470     assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1471     to_cp->klass_index_at_put(to_i, name_index);
1472   } break;
1473 
1474   case JVM_CONSTANT_String:
1475   {
1476     Symbol* s = from_cp->unresolved_string_at(from_i);
1477     to_cp->unresolved_string_at_put(to_i, s);
1478   } break;
1479 
1480   case JVM_CONSTANT_Utf8:
1481   {
1482     Symbol* s = from_cp->symbol_at(from_i);
1483     // Need to increase refcount, the old one will be thrown away and deferenced
1484     s->increment_refcount();
1485     to_cp->symbol_at_put(to_i, s);
1486   } break;
1487 
1488   case JVM_CONSTANT_MethodType:
1489   case JVM_CONSTANT_MethodTypeInError:
1490   {
1491     jint k = from_cp->method_type_index_at(from_i);
1492     to_cp->method_type_index_at_put(to_i, k);
1493   } break;
1494 
1495   case JVM_CONSTANT_MethodHandle:
1496   case JVM_CONSTANT_MethodHandleInError:
1497   {
1498     int k1 = from_cp->method_handle_ref_kind_at(from_i);
1499     int k2 = from_cp->method_handle_index_at(from_i);
1500     to_cp->method_handle_index_at_put(to_i, k1, k2);
1501   } break;
1502 
1503   case JVM_CONSTANT_InvokeDynamic:
1504   {
1505     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1506     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1507     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
1508     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1509   } break;
1510 
1511   // Invalid is used as the tag for the second constant pool entry
1512   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1513   // not be seen by itself.
1514   case JVM_CONSTANT_Invalid: // fall through
1515 
1516   default:
1517   {
1518     ShouldNotReachHere();
1519   } break;
1520   }
1521 } // end copy_entry_to()
1522 
1523 // Search constant pool search_cp for an entry that matches this
1524 // constant pool's entry at pattern_i. Returns the index of a
1525 // matching entry or zero (0) if there is no matching entry.
1526 int ConstantPool::find_matching_entry(int pattern_i,
1527       const constantPoolHandle& search_cp, TRAPS) {
1528 
1529   // index zero (0) is not used
1530   for (int i = 1; i < search_cp->length(); i++) {
1531     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1532     if (found) {
1533       return i;
1534     }
1535   }
1536 
1537   return 0;  // entry not found; return unused index zero (0)
1538 } // end find_matching_entry()
1539 
1540 
1541 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1542 // cp2's bootstrap specifier at idx2.
1543 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2, TRAPS) {
1544   int k1 = operand_bootstrap_method_ref_index_at(idx1);
1545   int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
1546   bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1547 
1548   if (!match) {
1549     return false;
1550   }
1551   int argc = operand_argument_count_at(idx1);
1552   if (argc == cp2->operand_argument_count_at(idx2)) {
1553     for (int j = 0; j < argc; j++) {
1554       k1 = operand_argument_index_at(idx1, j);
1555       k2 = cp2->operand_argument_index_at(idx2, j);
1556       match = compare_entry_to(k1, cp2, k2, CHECK_false);
1557       if (!match) {
1558         return false;
1559       }
1560     }
1561     return true;           // got through loop; all elements equal
1562   }
1563   return false;
1564 } // end compare_operand_to()
1565 
1566 // Search constant pool search_cp for a bootstrap specifier that matches
1567 // this constant pool's bootstrap specifier at pattern_i index.
1568 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
1569 int ConstantPool::find_matching_operand(int pattern_i,
1570                     const constantPoolHandle& search_cp, int search_len, TRAPS) {
1571   for (int i = 0; i < search_len; i++) {
1572     bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1573     if (found) {
1574       return i;
1575     }
1576   }
1577   return -1;  // bootstrap specifier not found; return unused index (-1)
1578 } // end find_matching_operand()
1579 
1580 
1581 #ifndef PRODUCT
1582 
1583 const char* ConstantPool::printable_name_at(int which) {
1584 
1585   constantTag tag = tag_at(which);
1586 
1587   if (tag.is_string()) {
1588     return string_at_noresolve(which);
1589   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1590     return klass_name_at(which)->as_C_string();
1591   } else if (tag.is_symbol()) {
1592     return symbol_at(which)->as_C_string();
1593   }
1594   return "";
1595 }
1596 
1597 #endif // PRODUCT
1598 
1599 
1600 // JVMTI GetConstantPool support
1601 
1602 // For debugging of constant pool
1603 const bool debug_cpool = false;
1604 
1605 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1606 
1607 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1608   const char* WARN_MSG = "Must not be such entry!";
1609   jint size = 0;
1610   u2   idx1, idx2;
1611 
1612   for (jint idx = 1; idx < cnt; idx++) {
1613     jint ent_size = 0;
1614     u1   tag  = *bytes++;
1615     size++;                       // count tag
1616 
1617     printf("const #%03d, tag: %02d ", idx, tag);
1618     switch(tag) {
1619       case JVM_CONSTANT_Invalid: {
1620         printf("Invalid");
1621         break;
1622       }
1623       case JVM_CONSTANT_Unicode: {
1624         printf("Unicode      %s", WARN_MSG);
1625         break;
1626       }
1627       case JVM_CONSTANT_Utf8: {
1628         u2 len = Bytes::get_Java_u2(bytes);
1629         char str[128];
1630         if (len > 127) {
1631            len = 127;
1632         }
1633         strncpy(str, (char *) (bytes+2), len);
1634         str[len] = '\0';
1635         printf("Utf8          \"%s\"", str);
1636         ent_size = 2 + len;
1637         break;
1638       }
1639       case JVM_CONSTANT_Integer: {
1640         u4 val = Bytes::get_Java_u4(bytes);
1641         printf("int          %d", *(int *) &val);
1642         ent_size = 4;
1643         break;
1644       }
1645       case JVM_CONSTANT_Float: {
1646         u4 val = Bytes::get_Java_u4(bytes);
1647         printf("float        %5.3ff", *(float *) &val);
1648         ent_size = 4;
1649         break;
1650       }
1651       case JVM_CONSTANT_Long: {
1652         u8 val = Bytes::get_Java_u8(bytes);
1653         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1654         ent_size = 8;
1655         idx++; // Long takes two cpool slots
1656         break;
1657       }
1658       case JVM_CONSTANT_Double: {
1659         u8 val = Bytes::get_Java_u8(bytes);
1660         printf("double       %5.3fd", *(jdouble *)&val);
1661         ent_size = 8;
1662         idx++; // Double takes two cpool slots
1663         break;
1664       }
1665       case JVM_CONSTANT_Class: {
1666         idx1 = Bytes::get_Java_u2(bytes);
1667         printf("class        #%03d", idx1);
1668         ent_size = 2;
1669         break;
1670       }
1671       case JVM_CONSTANT_String: {
1672         idx1 = Bytes::get_Java_u2(bytes);
1673         printf("String       #%03d", idx1);
1674         ent_size = 2;
1675         break;
1676       }
1677       case JVM_CONSTANT_Fieldref: {
1678         idx1 = Bytes::get_Java_u2(bytes);
1679         idx2 = Bytes::get_Java_u2(bytes+2);
1680         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1681         ent_size = 4;
1682         break;
1683       }
1684       case JVM_CONSTANT_Methodref: {
1685         idx1 = Bytes::get_Java_u2(bytes);
1686         idx2 = Bytes::get_Java_u2(bytes+2);
1687         printf("Method       #%03d, #%03d", idx1, idx2);
1688         ent_size = 4;
1689         break;
1690       }
1691       case JVM_CONSTANT_InterfaceMethodref: {
1692         idx1 = Bytes::get_Java_u2(bytes);
1693         idx2 = Bytes::get_Java_u2(bytes+2);
1694         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1695         ent_size = 4;
1696         break;
1697       }
1698       case JVM_CONSTANT_NameAndType: {
1699         idx1 = Bytes::get_Java_u2(bytes);
1700         idx2 = Bytes::get_Java_u2(bytes+2);
1701         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1702         ent_size = 4;
1703         break;
1704       }
1705       case JVM_CONSTANT_ClassIndex: {
1706         printf("ClassIndex  %s", WARN_MSG);
1707         break;
1708       }
1709       case JVM_CONSTANT_UnresolvedClass: {
1710         printf("UnresolvedClass: %s", WARN_MSG);
1711         break;
1712       }
1713       case JVM_CONSTANT_UnresolvedClassInError: {
1714         printf("UnresolvedClassInErr: %s", WARN_MSG);
1715         break;
1716       }
1717       case JVM_CONSTANT_StringIndex: {
1718         printf("StringIndex: %s", WARN_MSG);
1719         break;
1720       }
1721     }
1722     printf(";\n");
1723     bytes += ent_size;
1724     size  += ent_size;
1725   }
1726   printf("Cpool size: %d\n", size);
1727   fflush(0);
1728   return;
1729 } /* end print_cpool_bytes */
1730 
1731 
1732 // Returns size of constant pool entry.
1733 jint ConstantPool::cpool_entry_size(jint idx) {
1734   switch(tag_at(idx).value()) {
1735     case JVM_CONSTANT_Invalid:
1736     case JVM_CONSTANT_Unicode:
1737       return 1;
1738 
1739     case JVM_CONSTANT_Utf8:
1740       return 3 + symbol_at(idx)->utf8_length();
1741 
1742     case JVM_CONSTANT_Class:
1743     case JVM_CONSTANT_String:
1744     case JVM_CONSTANT_ClassIndex:
1745     case JVM_CONSTANT_UnresolvedClass:
1746     case JVM_CONSTANT_UnresolvedClassInError:
1747     case JVM_CONSTANT_StringIndex:
1748     case JVM_CONSTANT_MethodType:
1749     case JVM_CONSTANT_MethodTypeInError:
1750       return 3;
1751 
1752     case JVM_CONSTANT_MethodHandle:
1753     case JVM_CONSTANT_MethodHandleInError:
1754       return 4; //tag, ref_kind, ref_index
1755 
1756     case JVM_CONSTANT_Integer:
1757     case JVM_CONSTANT_Float:
1758     case JVM_CONSTANT_Fieldref:
1759     case JVM_CONSTANT_Methodref:
1760     case JVM_CONSTANT_InterfaceMethodref:
1761     case JVM_CONSTANT_NameAndType:
1762       return 5;
1763 
1764     case JVM_CONSTANT_InvokeDynamic:
1765       // u1 tag, u2 bsm, u2 nt
1766       return 5;
1767 
1768     case JVM_CONSTANT_Long:
1769     case JVM_CONSTANT_Double:
1770       return 9;
1771   }
1772   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1773   return 1;
1774 } /* end cpool_entry_size */
1775 
1776 
1777 // SymbolHashMap is used to find a constant pool index from a string.
1778 // This function fills in SymbolHashMaps, one for utf8s and one for
1779 // class names, returns size of the cpool raw bytes.
1780 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
1781                                           SymbolHashMap *classmap) {
1782   jint size = 0;
1783 
1784   for (u2 idx = 1; idx < length(); idx++) {
1785     u2 tag = tag_at(idx).value();
1786     size += cpool_entry_size(idx);
1787 
1788     switch(tag) {
1789       case JVM_CONSTANT_Utf8: {
1790         Symbol* sym = symbol_at(idx);
1791         symmap->add_entry(sym, idx);
1792         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1793         break;
1794       }
1795       case JVM_CONSTANT_Class:
1796       case JVM_CONSTANT_UnresolvedClass:
1797       case JVM_CONSTANT_UnresolvedClassInError: {
1798         Symbol* sym = klass_name_at(idx);
1799         classmap->add_entry(sym, idx);
1800         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1801         break;
1802       }
1803       case JVM_CONSTANT_Long:
1804       case JVM_CONSTANT_Double: {
1805         idx++; // Both Long and Double take two cpool slots
1806         break;
1807       }
1808     }
1809   }
1810   return size;
1811 } /* end hash_utf8_entries_to */
1812 
1813 
1814 // Copy cpool bytes.
1815 // Returns:
1816 //    0, in case of OutOfMemoryError
1817 //   -1, in case of internal error
1818 //  > 0, count of the raw cpool bytes that have been copied
1819 int ConstantPool::copy_cpool_bytes(int cpool_size,
1820                                           SymbolHashMap* tbl,
1821                                           unsigned char *bytes) {
1822   u2   idx1, idx2;
1823   jint size  = 0;
1824   jint cnt   = length();
1825   unsigned char *start_bytes = bytes;
1826 
1827   for (jint idx = 1; idx < cnt; idx++) {
1828     u1   tag      = tag_at(idx).value();
1829     jint ent_size = cpool_entry_size(idx);
1830 
1831     assert(size + ent_size <= cpool_size, "Size mismatch");
1832 
1833     *bytes = tag;
1834     DBG(printf("#%03hd tag=%03hd, ", (short)idx, (short)tag));
1835     switch(tag) {
1836       case JVM_CONSTANT_Invalid: {
1837         DBG(printf("JVM_CONSTANT_Invalid"));
1838         break;
1839       }
1840       case JVM_CONSTANT_Unicode: {
1841         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1842         DBG(printf("JVM_CONSTANT_Unicode"));
1843         break;
1844       }
1845       case JVM_CONSTANT_Utf8: {
1846         Symbol* sym = symbol_at(idx);
1847         char*     str = sym->as_utf8();
1848         // Warning! It's crashing on x86 with len = sym->utf8_length()
1849         int       len = (int) strlen(str);
1850         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1851         for (int i = 0; i < len; i++) {
1852             bytes[3+i] = (u1) str[i];
1853         }
1854         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1855         break;
1856       }
1857       case JVM_CONSTANT_Integer: {
1858         jint val = int_at(idx);
1859         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1860         break;
1861       }
1862       case JVM_CONSTANT_Float: {
1863         jfloat val = float_at(idx);
1864         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1865         break;
1866       }
1867       case JVM_CONSTANT_Long: {
1868         jlong val = long_at(idx);
1869         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1870         idx++;             // Long takes two cpool slots
1871         break;
1872       }
1873       case JVM_CONSTANT_Double: {
1874         jdouble val = double_at(idx);
1875         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1876         idx++;             // Double takes two cpool slots
1877         break;
1878       }
1879       case JVM_CONSTANT_Class:
1880       case JVM_CONSTANT_UnresolvedClass:
1881       case JVM_CONSTANT_UnresolvedClassInError: {
1882         *bytes = JVM_CONSTANT_Class;
1883         Symbol* sym = klass_name_at(idx);
1884         idx1 = tbl->symbol_to_value(sym);
1885         assert(idx1 != 0, "Have not found a hashtable entry");
1886         Bytes::put_Java_u2((address) (bytes+1), idx1);
1887         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1888         break;
1889       }
1890       case JVM_CONSTANT_String: {
1891         *bytes = JVM_CONSTANT_String;
1892         Symbol* sym = unresolved_string_at(idx);
1893         idx1 = tbl->symbol_to_value(sym);
1894         assert(idx1 != 0, "Have not found a hashtable entry");
1895         Bytes::put_Java_u2((address) (bytes+1), idx1);
1896         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
1897         break;
1898       }
1899       case JVM_CONSTANT_Fieldref:
1900       case JVM_CONSTANT_Methodref:
1901       case JVM_CONSTANT_InterfaceMethodref: {
1902         idx1 = uncached_klass_ref_index_at(idx);
1903         idx2 = uncached_name_and_type_ref_index_at(idx);
1904         Bytes::put_Java_u2((address) (bytes+1), idx1);
1905         Bytes::put_Java_u2((address) (bytes+3), idx2);
1906         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1907         break;
1908       }
1909       case JVM_CONSTANT_NameAndType: {
1910         idx1 = name_ref_index_at(idx);
1911         idx2 = signature_ref_index_at(idx);
1912         Bytes::put_Java_u2((address) (bytes+1), idx1);
1913         Bytes::put_Java_u2((address) (bytes+3), idx2);
1914         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1915         break;
1916       }
1917       case JVM_CONSTANT_ClassIndex: {
1918         *bytes = JVM_CONSTANT_Class;
1919         idx1 = klass_index_at(idx);
1920         Bytes::put_Java_u2((address) (bytes+1), idx1);
1921         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1922         break;
1923       }
1924       case JVM_CONSTANT_StringIndex: {
1925         *bytes = JVM_CONSTANT_String;
1926         idx1 = string_index_at(idx);
1927         Bytes::put_Java_u2((address) (bytes+1), idx1);
1928         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1929         break;
1930       }
1931       case JVM_CONSTANT_MethodHandle:
1932       case JVM_CONSTANT_MethodHandleInError: {
1933         *bytes = JVM_CONSTANT_MethodHandle;
1934         int kind = method_handle_ref_kind_at(idx);
1935         idx1 = method_handle_index_at(idx);
1936         *(bytes+1) = (unsigned char) kind;
1937         Bytes::put_Java_u2((address) (bytes+2), idx1);
1938         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
1939         break;
1940       }
1941       case JVM_CONSTANT_MethodType:
1942       case JVM_CONSTANT_MethodTypeInError: {
1943         *bytes = JVM_CONSTANT_MethodType;
1944         idx1 = method_type_index_at(idx);
1945         Bytes::put_Java_u2((address) (bytes+1), idx1);
1946         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
1947         break;
1948       }
1949       case JVM_CONSTANT_InvokeDynamic: {
1950         *bytes = tag;
1951         idx1 = extract_low_short_from_int(*int_at_addr(idx));
1952         idx2 = extract_high_short_from_int(*int_at_addr(idx));
1953         assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
1954         Bytes::put_Java_u2((address) (bytes+1), idx1);
1955         Bytes::put_Java_u2((address) (bytes+3), idx2);
1956         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
1957         break;
1958       }
1959     }
1960     DBG(printf("\n"));
1961     bytes += ent_size;
1962     size  += ent_size;
1963   }
1964   assert(size == cpool_size, "Size mismatch");
1965 
1966   // Keep temorarily for debugging until it's stable.
1967   DBG(print_cpool_bytes(cnt, start_bytes));
1968   return (int)(bytes - start_bytes);
1969 } /* end copy_cpool_bytes */
1970 
1971 #undef DBG
1972 
1973 
1974 void ConstantPool::set_on_stack(const bool value) {
1975   if (value) {
1976     // Only record if it's not already set.
1977     if (!on_stack()) {
1978       assert(!is_shared(), "should always be set for shared constant pools");
1979       _flags |= _on_stack;
1980       MetadataOnStackMark::record(this);
1981     }
1982   } else {
1983     // Clearing is done single-threadedly.
1984     if (!is_shared()) {
1985       _flags &= ~_on_stack;
1986     }
1987   }
1988 }
1989 
1990 // JSR 292 support for patching constant pool oops after the class is linked and
1991 // the oop array for resolved references are created.
1992 // We can't do this during classfile parsing, which is how the other indexes are
1993 // patched.  The other patches are applied early for some error checking
1994 // so only defer the pseudo_strings.
1995 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
1996   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
1997     Handle patch = cp_patches->at(index);
1998     if (patch.not_null()) {
1999       assert (tag_at(index).is_string(), "should only be string left");
2000       // Patching a string means pre-resolving it.
2001       // The spelling in the constant pool is ignored.
2002       // The constant reference may be any object whatever.
2003       // If it is not a real interned string, the constant is referred
2004       // to as a "pseudo-string", and must be presented to the CP
2005       // explicitly, because it may require scavenging.
2006       int obj_index = cp_to_object_index(index);
2007       pseudo_string_at_put(index, obj_index, patch());
2008      DEBUG_ONLY(cp_patches->at_put(index, Handle());)
2009     }
2010   }
2011 #ifdef ASSERT
2012   // Ensure that all the patches have been used.
2013   for (int index = 0; index < cp_patches->length(); index++) {
2014     assert(cp_patches->at(index).is_null(),
2015            "Unused constant pool patch at %d in class file %s",
2016            index,
2017            pool_holder()->external_name());
2018   }
2019 #endif // ASSERT
2020 }
2021 
2022 #ifndef PRODUCT
2023 
2024 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
2025 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
2026   guarantee(obj->is_constantPool(), "object must be constant pool");
2027   constantPoolHandle cp(THREAD, (ConstantPool*)obj);
2028   guarantee(cp->pool_holder() != NULL, "must be fully loaded");
2029 
2030   for (int i = 0; i< cp->length();  i++) {
2031     if (cp->tag_at(i).is_unresolved_klass()) {
2032       // This will force loading of the class
2033       Klass* klass = cp->klass_at(i, CHECK);
2034       if (klass->is_instance_klass()) {
2035         // Force initialization of class
2036         InstanceKlass::cast(klass)->initialize(CHECK);
2037       }
2038     }
2039   }
2040 }
2041 
2042 #endif
2043 
2044 
2045 // Printing
2046 
2047 void ConstantPool::print_on(outputStream* st) const {
2048   assert(is_constantPool(), "must be constantPool");
2049   st->print_cr("%s", internal_name());
2050   if (flags() != 0) {
2051     st->print(" - flags: 0x%x", flags());
2052     if (has_preresolution()) st->print(" has_preresolution");
2053     if (on_stack()) st->print(" on_stack");
2054     st->cr();
2055   }
2056   if (pool_holder() != NULL) {
2057     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
2058   }
2059   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
2060   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
2061   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
2062   st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses()));
2063 
2064   for (int index = 1; index < length(); index++) {      // Index 0 is unused
2065     ((ConstantPool*)this)->print_entry_on(index, st);
2066     switch (tag_at(index).value()) {
2067       case JVM_CONSTANT_Long :
2068       case JVM_CONSTANT_Double :
2069         index++;   // Skip entry following eigth-byte constant
2070     }
2071 
2072   }
2073   st->cr();
2074 }
2075 
2076 // Print one constant pool entry
2077 void ConstantPool::print_entry_on(const int index, outputStream* st) {
2078   EXCEPTION_MARK;
2079   st->print(" - %3d : ", index);
2080   tag_at(index).print_on(st);
2081   st->print(" : ");
2082   switch (tag_at(index).value()) {
2083     case JVM_CONSTANT_Class :
2084       { Klass* k = klass_at(index, CATCH);
2085         guarantee(k != NULL, "need klass");
2086         k->print_value_on(st);
2087         st->print(" {" PTR_FORMAT "}", p2i(k));
2088       }
2089       break;
2090     case JVM_CONSTANT_Fieldref :
2091     case JVM_CONSTANT_Methodref :
2092     case JVM_CONSTANT_InterfaceMethodref :
2093       st->print("klass_index=%d", uncached_klass_ref_index_at(index));
2094       st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
2095       break;
2096     case JVM_CONSTANT_String :
2097       if (is_pseudo_string_at(index)) {
2098         oop anObj = pseudo_string_at(index);
2099         anObj->print_value_on(st);
2100         st->print(" {" PTR_FORMAT "}", p2i(anObj));
2101       } else {
2102         unresolved_string_at(index)->print_value_on(st);
2103       }
2104       break;
2105     case JVM_CONSTANT_Integer :
2106       st->print("%d", int_at(index));
2107       break;
2108     case JVM_CONSTANT_Float :
2109       st->print("%f", float_at(index));
2110       break;
2111     case JVM_CONSTANT_Long :
2112       st->print_jlong(long_at(index));
2113       break;
2114     case JVM_CONSTANT_Double :
2115       st->print("%lf", double_at(index));
2116       break;
2117     case JVM_CONSTANT_NameAndType :
2118       st->print("name_index=%d", name_ref_index_at(index));
2119       st->print(" signature_index=%d", signature_ref_index_at(index));
2120       break;
2121     case JVM_CONSTANT_Utf8 :
2122       symbol_at(index)->print_value_on(st);
2123       break;
2124     case JVM_CONSTANT_ClassIndex: {
2125         int name_index = *int_at_addr(index);
2126         st->print("klass_index=%d ", name_index);
2127         symbol_at(name_index)->print_value_on(st);
2128       }
2129       break;
2130     case JVM_CONSTANT_UnresolvedClass :               // fall-through
2131     case JVM_CONSTANT_UnresolvedClassInError: {
2132         CPKlassSlot kslot = klass_slot_at(index);
2133         int resolved_klass_index = kslot.resolved_klass_index();
2134         int name_index = kslot.name_index();
2135         assert(tag_at(name_index).is_symbol(), "sanity");
2136 
2137         Klass* klass = resolved_klasses()->at(resolved_klass_index);
2138         if (klass != NULL) {
2139           klass->print_value_on(st);
2140         } else {
2141           symbol_at(name_index)->print_value_on(st);
2142         }
2143       }
2144       break;
2145     case JVM_CONSTANT_MethodHandle :
2146     case JVM_CONSTANT_MethodHandleInError :
2147       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2148       st->print(" ref_index=%d", method_handle_index_at(index));
2149       break;
2150     case JVM_CONSTANT_MethodType :
2151     case JVM_CONSTANT_MethodTypeInError :
2152       st->print("signature_index=%d", method_type_index_at(index));
2153       break;
2154     case JVM_CONSTANT_InvokeDynamic :
2155       {
2156         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
2157         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
2158         int argc = invoke_dynamic_argument_count_at(index);
2159         if (argc > 0) {
2160           for (int arg_i = 0; arg_i < argc; arg_i++) {
2161             int arg = invoke_dynamic_argument_index_at(index, arg_i);
2162             st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2163           }
2164           st->print("}");
2165         }
2166       }
2167       break;
2168     default:
2169       ShouldNotReachHere();
2170       break;
2171   }
2172   st->cr();
2173 }
2174 
2175 void ConstantPool::print_value_on(outputStream* st) const {
2176   assert(is_constantPool(), "must be constantPool");
2177   st->print("constant pool [%d]", length());
2178   if (has_preresolution()) st->print("/preresolution");
2179   if (operands() != NULL)  st->print("/operands[%d]", operands()->length());
2180   print_address_on(st);
2181   st->print(" for ");
2182   pool_holder()->print_value_on(st);
2183   if (pool_holder() != NULL) {
2184     bool extra = (pool_holder()->constants() != this);
2185     if (extra)  st->print(" (extra)");
2186   }
2187   if (cache() != NULL) {
2188     st->print(" cache=" PTR_FORMAT, p2i(cache()));
2189   }
2190 }
2191 
2192 #if INCLUDE_SERVICES
2193 // Size Statistics
2194 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2195   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
2196   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
2197   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
2198   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2199   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
2200 
2201   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2202                    sz->_cp_refmap_bytes;
2203   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2204 }
2205 #endif // INCLUDE_SERVICES
2206 
2207 // Verification
2208 
2209 void ConstantPool::verify_on(outputStream* st) {
2210   guarantee(is_constantPool(), "object must be constant pool");
2211   for (int i = 0; i< length();  i++) {
2212     constantTag tag = tag_at(i);
2213     if (tag.is_klass() || tag.is_unresolved_klass()) {
2214       guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
2215     } else if (tag.is_symbol()) {
2216       CPSlot entry = slot_at(i);
2217       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2218     } else if (tag.is_string()) {
2219       CPSlot entry = slot_at(i);
2220       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2221     }
2222   }
2223   if (cache() != NULL) {
2224     // Note: cache() can be NULL before a class is completely setup or
2225     // in temporary constant pools used during constant pool merging
2226     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2227   }
2228   if (pool_holder() != NULL) {
2229     // Note: pool_holder() can be NULL in temporary constant pools
2230     // used during constant pool merging
2231     guarantee(pool_holder()->is_klass(),    "should be klass");
2232   }
2233 }
2234 
2235 
2236 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2237   char *str = sym->as_utf8();
2238   unsigned int hash = compute_hash(str, sym->utf8_length());
2239   unsigned int index = hash % table_size();
2240 
2241   // check if already in map
2242   // we prefer the first entry since it is more likely to be what was used in
2243   // the class file
2244   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2245     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2246     if (en->hash() == hash && en->symbol() == sym) {
2247         return;  // already there
2248     }
2249   }
2250 
2251   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
2252   entry->set_next(bucket(index));
2253   _buckets[index].set_entry(entry);
2254   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2255 }
2256 
2257 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
2258   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
2259   char *str = sym->as_utf8();
2260   int   len = sym->utf8_length();
2261   unsigned int hash = SymbolHashMap::compute_hash(str, len);
2262   unsigned int index = hash % table_size();
2263   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2264     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2265     if (en->hash() == hash && en->symbol() == sym) {
2266       return en;
2267     }
2268   }
2269   return NULL;
2270 }