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