1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)constantPoolOop.hpp  1.105 07/08/29 13:42:26 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // A constantPool is an array containing class constants as described in the
  29 // class file.
  30 //
  31 // Most of the constant pool entries are written during class parsing, which
  32 // is safe.  For klass and string types, the constant pool entry is
  33 // modified when the entry is resolved.  If a klass or string constant pool
  34 // entry is read without a lock, only the resolved state guarantees that
  35 // the entry in the constant pool is a klass or String object and
  36 // not a symbolOop.
  37 
  38 class SymbolHashMap;
  39 
  40 class constantPoolOopDesc : public arrayOopDesc {
  41   friend class VMStructs;
  42   friend class BytecodeInterpreter;  // Directly extracts an oop in the pool for fast instanceof/checkcast
  43  private:
  44   typeArrayOop         _tags; // the tag array describing the constant pool's contents
  45   constantPoolCacheOop _cache;         // the cache holding interpreter runtime information
  46   klassOop             _pool_holder;   // the corresponding class
  47   // only set to non-zero if constant pool is merged by RedefineClasses
  48   int                  _orig_length;
  49 
  50   void set_tags(typeArrayOop tags)             { oop_store_without_check((oop*)&_tags, tags); }
  51   void tag_at_put(int which, jbyte t)          { tags()->byte_at_put(which, t); }
  52   void release_tag_at_put(int which, jbyte t)  { tags()->release_byte_at_put(which, t); }
  53 
  54  private:
  55   intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(constantPoolOopDesc)); }
  56   oop* tags_addr()       { return (oop*)&_tags; }
  57   oop* cache_addr()      { return (oop*)&_cache; }
  58 
  59   oop* obj_at_addr(int which) const {
  60     assert(is_within_bounds(which), "index out of bounds");
  61     return (oop*) &base()[which];
  62   }
  63 
  64   jint* int_at_addr(int which) const {
  65     assert(is_within_bounds(which), "index out of bounds");
  66     return (jint*) &base()[which];
  67   }
  68 
  69   jlong* long_at_addr(int which) const {
  70     assert(is_within_bounds(which), "index out of bounds");
  71     return (jlong*) &base()[which];
  72   }
  73 
  74   jfloat* float_at_addr(int which) const {
  75     assert(is_within_bounds(which), "index out of bounds");
  76     return (jfloat*) &base()[which];
  77   }
  78 
  79   jdouble* double_at_addr(int which) const {
  80     assert(is_within_bounds(which), "index out of bounds");
  81     return (jdouble*) &base()[which];
  82   }
  83 
  84  public:
  85   typeArrayOop tags() const                 { return _tags; }
  86 
  87   // Klass holding pool
  88   klassOop pool_holder() const              { return _pool_holder; }
  89   void set_pool_holder(klassOop k)          { oop_store_without_check((oop*)&_pool_holder, (oop) k); }
  90   oop* pool_holder_addr()                   { return (oop*)&_pool_holder; }
  91 
  92   // Interpreter runtime support
  93   constantPoolCacheOop cache() const        { return _cache; }
  94   void set_cache(constantPoolCacheOop cache){ oop_store((oop*)&_cache, cache); }
  95 
  96   // Assembly code support
  97   static int tags_offset_in_bytes()         { return offset_of(constantPoolOopDesc, _tags); }
  98   static int cache_offset_in_bytes()        { return offset_of(constantPoolOopDesc, _cache); }
  99   static int pool_holder_offset_in_bytes()  { return offset_of(constantPoolOopDesc, _pool_holder); }
 100 
 101   // Storing constants
 102 
 103   void klass_at_put(int which, klassOop k) { 
 104     oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
 105     // The interpreter assumes when the tag is stored, the klass is resolved
 106     // and the klassOop is a klass rather than a symbolOop, so we need
 107     // hardware store ordering here.
 108     release_tag_at_put(which, JVM_CONSTANT_Class);
 109     if (UseConcMarkSweepGC) {
 110       // In case the earlier card-mark was consumed by a concurrent
 111       // marking thread before the tag was updated, redirty the card.
 112       oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
 113     }
 114   }
 115 
 116   // For temporary use while constructing constant pool
 117   void klass_index_at_put(int which, int name_index) {
 118     tag_at_put(which, JVM_CONSTANT_ClassIndex);
 119     *int_at_addr(which) = name_index; 
 120   }
 121 
 122   // Temporary until actual use
 123   void unresolved_klass_at_put(int which, symbolOop s) {
 124     // Overwrite the old index with a GC friendly value so
 125     // that if GC looks during the transition it won't try
 126     // to treat a small integer as oop.
 127     *obj_at_addr(which) = NULL;
 128     release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);
 129     oop_store_without_check(obj_at_addr(which), oop(s));
 130   }
 131 
 132   // Temporary until actual use
 133   void unresolved_string_at_put(int which, symbolOop s) {
 134     *obj_at_addr(which) = NULL;
 135     release_tag_at_put(which, JVM_CONSTANT_UnresolvedString);
 136     oop_store_without_check(obj_at_addr(which), oop(s));
 137   }
 138 
 139   void int_at_put(int which, jint i) { 
 140     tag_at_put(which, JVM_CONSTANT_Integer);
 141     *int_at_addr(which) = i; 
 142   }
 143   
 144   void long_at_put(int which, jlong l) { 
 145     tag_at_put(which, JVM_CONSTANT_Long);
 146     // *long_at_addr(which) = l; 
 147     Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));
 148   }
 149 
 150   void float_at_put(int which, jfloat f) { 
 151     tag_at_put(which, JVM_CONSTANT_Float);
 152     *float_at_addr(which) = f; 
 153   }
 154 
 155   void double_at_put(int which, jdouble d) { 
 156     tag_at_put(which, JVM_CONSTANT_Double);
 157     // *double_at_addr(which) = d; 
 158     // u8 temp = *(u8*) &d;
 159     Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d));
 160   }
 161 
 162   void symbol_at_put(int which, symbolOop s) {
 163     tag_at_put(which, JVM_CONSTANT_Utf8);
 164     oop_store_without_check(obj_at_addr(which), oop(s));
 165   }
 166 
 167   void string_at_put(int which, oop str) {
 168     oop_store((volatile oop*)obj_at_addr(which), str);
 169     release_tag_at_put(which, JVM_CONSTANT_String);
 170     if (UseConcMarkSweepGC) {
 171       // In case the earlier card-mark was consumed by a concurrent
 172       // marking thread before the tag was updated, redirty the card.
 173       oop_store_without_check((volatile oop *)obj_at_addr(which), str);
 174     }
 175   }
 176 
 177   // For temporary use while constructing constant pool
 178   void string_index_at_put(int which, int string_index) {
 179     tag_at_put(which, JVM_CONSTANT_StringIndex);
 180     *int_at_addr(which) = string_index; 
 181   }
 182 
 183   void field_at_put(int which, int class_index, int name_and_type_index) {
 184     tag_at_put(which, JVM_CONSTANT_Fieldref);
 185     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
 186   }
 187 
 188   void method_at_put(int which, int class_index, int name_and_type_index) {
 189     tag_at_put(which, JVM_CONSTANT_Methodref);
 190     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
 191   }
 192 
 193   void interface_method_at_put(int which, int class_index, int name_and_type_index) {
 194     tag_at_put(which, JVM_CONSTANT_InterfaceMethodref);
 195     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;  // Not so nice
 196   }
 197 
 198   void name_and_type_at_put(int which, int name_index, int signature_index) {
 199     tag_at_put(which, JVM_CONSTANT_NameAndType);
 200     *int_at_addr(which) = ((jint) signature_index<<16) | name_index;  // Not so nice
 201   }
 202   
 203   // Tag query
 204 
 205   constantTag tag_at(int which) const { return (constantTag)tags()->byte_at_acquire(which); }
 206 
 207   // Whether the entry is a pointer that must be GC'd.
 208   bool is_pointer_entry(int which) {
 209     constantTag tag = tag_at(which);
 210     return tag.is_klass() ||
 211       tag.is_unresolved_klass() ||
 212       tag.is_symbol() ||
 213       tag.is_unresolved_string() ||
 214       tag.is_string();
 215   }
 216 
 217   // Fetching constants
 218 
 219   klassOop klass_at(int which, TRAPS) { 
 220     constantPoolHandle h_this(THREAD, this);
 221     return klass_at_impl(h_this, which, CHECK_NULL); 
 222   }  
 223     
 224   symbolOop klass_name_at(int which);  // Returns the name, w/o resolving.
 225 
 226   klassOop resolved_klass_at(int which) {  // Used by Compiler 
 227     guarantee(tag_at(which).is_klass(), "Corrupted constant pool");
 228     // Must do an acquire here in case another thread resolved the klass
 229     // behind our back, lest we later load stale values thru the oop.
 230     return klassOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
 231   }
 232 
 233   // This method should only be used with a cpool lock or during parsing or gc
 234   symbolOop unresolved_klass_at(int which) {     // Temporary until actual use
 235     symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
 236     // check that the klass is still unresolved.
 237     assert(tag_at(which).is_unresolved_klass(), "Corrupted constant pool");
 238     return s;
 239   }
 240 
 241   // RedefineClasses() API support:
 242   symbolOop klass_at_noresolve(int which) { return klass_name_at(which); }
 243 
 244   jint int_at(int which) {
 245     assert(tag_at(which).is_int(), "Corrupted constant pool");
 246     return *int_at_addr(which);
 247   }
 248 
 249   jlong long_at(int which) {
 250     assert(tag_at(which).is_long(), "Corrupted constant pool");
 251     // return *long_at_addr(which);
 252     u8 tmp = Bytes::get_native_u8((address)&base()[which]);
 253     return *((jlong*)&tmp);
 254   }
 255 
 256   jfloat float_at(int which) {
 257     assert(tag_at(which).is_float(), "Corrupted constant pool");
 258     return *float_at_addr(which);
 259   }
 260 
 261   jdouble double_at(int which) {
 262     assert(tag_at(which).is_double(), "Corrupted constant pool");
 263     u8 tmp = Bytes::get_native_u8((address)&base()[which]);
 264     return *((jdouble*)&tmp);
 265   }
 266 
 267   symbolOop symbol_at(int which) {
 268     assert(tag_at(which).is_utf8(), "Corrupted constant pool");
 269     return symbolOop(*obj_at_addr(which));
 270   }
 271 
 272   oop string_at(int which, TRAPS) {
 273     constantPoolHandle h_this(THREAD, this);
 274     return string_at_impl(h_this, which, CHECK_NULL); 
 275   }
 276 
 277   // only called when we are sure a string entry is already resolved (via an
 278   // earlier string_at call.
 279   oop resolved_string_at(int which) {
 280     assert(tag_at(which).is_string(), "Corrupted constant pool");
 281     // Must do an acquire here in case another thread resolved the klass
 282     // behind our back, lest we later load stale values thru the oop.
 283     return (oop)OrderAccess::load_ptr_acquire(obj_at_addr(which));
 284   }
 285 
 286   // This method should only be used with a cpool lock or during parsing or gc
 287   symbolOop unresolved_string_at(int which) {    // Temporary until actual use
 288     symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
 289     // check that the string is still unresolved.
 290     assert(tag_at(which).is_unresolved_string(), "Corrupted constant pool");
 291     return s;
 292   }
 293 
 294   // Returns an UTF8 for a CONSTANT_String entry at a given index.
 295   // UTF8 char* representation was chosen to avoid conversion of
 296   // java_lang_Strings at resolved entries into symbolOops
 297   // or vice versa.
 298   char* string_at_noresolve(int which);
 299 
 300   jint name_and_type_at(int which) {
 301     assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");
 302     return *int_at_addr(which);
 303   }
 304 
 305   // The following methods (klass_ref_at, klass_ref_at_noresolve, name_ref_at,
 306   // signature_ref_at, klass_ref_index_at, name_and_type_ref_index_at,
 307   // name_ref_index_at, signature_ref_index_at) all expect constant pool indices
 308   // from the bytecodes to be passed in, which are actually potentially byte-swapped
 309   // contstant pool cache indices. See field_or_method_at.
 310 
 311   // Lookup for entries consisting of (klass_index, name_and_type index)
 312   klassOop klass_ref_at(int which, TRAPS);
 313   symbolOop klass_ref_at_noresolve(int which);
 314   symbolOop name_ref_at(int which);
 315   symbolOop signature_ref_at(int which);    // the type descriptor
 316   
 317   int klass_ref_index_at(int which);
 318   int name_and_type_ref_index_at(int which);
 319 
 320   // Lookup for entries consisting of (name_index, signature_index)
 321   int name_ref_index_at(int which);
 322   int signature_ref_index_at(int which);
 323 
 324   BasicType basic_type_for_signature_at(int which);
 325 
 326   // Resolve string constants (to prevent allocation during compilation)
 327   void resolve_string_constants(TRAPS) {
 328     constantPoolHandle h_this(THREAD, this);
 329     resolve_string_constants_impl(h_this, CHECK); 
 330   }
 331 
 332   // Klass name matches name at offset
 333   bool klass_name_at_matches(instanceKlassHandle k, int which);
 334 
 335   // Sizing
 336   static int header_size()             { return sizeof(constantPoolOopDesc)/HeapWordSize; }
 337   static int object_size(int length)   { return align_object_size(header_size() + length); }
 338   int object_size()                    { return object_size(length()); }
 339 
 340   friend class constantPoolKlass;
 341   friend class ClassFileParser;
 342   friend class SystemDictionary;
 343 
 344   // Used by compiler to prevent classloading. 
 345   static klassOop klass_at_if_loaded          (constantPoolHandle this_oop, int which);  
 346   static klassOop klass_ref_at_if_loaded      (constantPoolHandle this_oop, int which);
 347   // Same as above - but does LinkResolving.
 348   static klassOop klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int which, TRAPS);  
 349 
 350   // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
 351   // future by other Java code. These take constant pool indices rather than possibly-byte-swapped
 352   // constant pool cache indices as do the peer methods above.
 353   symbolOop uncached_name_ref_at(int which);
 354   symbolOop uncached_signature_ref_at(int which);
 355   int       uncached_klass_ref_index_at(int which);
 356   int       uncached_name_and_type_ref_index_at(int which);
 357 
 358   // Sharing
 359   int pre_resolve_shared_klasses(TRAPS);
 360   void shared_symbols_iterate(OopClosure* closure0);
 361   void shared_tags_iterate(OopClosure* closure0);
 362   void shared_strings_iterate(OopClosure* closure0);
 363 
 364   // Debugging
 365   const char* printable_name_at(int which) PRODUCT_RETURN0;
 366 
 367  private:
 368 
 369   // Takes either a constant pool cache index in possibly byte-swapped
 370   // byte order (which comes from the bytecodes after rewriting) or,
 371   // if "uncached" is true, a vanilla constant pool index
 372   jint field_or_method_at(int which, bool uncached) {
 373     int i = -1;
 374     if (uncached || cache() == NULL) {
 375       i = which;
 376     } else {
 377       // change byte-ordering and go via cache
 378       i = cache()->entry_at(Bytes::swap_u2(which))->constant_pool_index();
 379     }
 380     assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 381     return *int_at_addr(i);
 382   }
 383 
 384   // Used while constructing constant pool (only by ClassFileParser)
 385   jint klass_index_at(int which) {
 386     assert(tag_at(which).is_klass_index(), "Corrupted constant pool");
 387     return *int_at_addr(which);
 388   }
 389 
 390   jint string_index_at(int which) {
 391     assert(tag_at(which).is_string_index(), "Corrupted constant pool");
 392     return *int_at_addr(which);
 393   }
 394 
 395   // Performs the LinkResolver checks
 396   static void verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle klass, TRAPS);
 397 
 398   // Implementation of methods that needs an exposed 'this' pointer, in order to 
 399   // handle GC while executing the method
 400   static klassOop klass_at_impl(constantPoolHandle this_oop, int which, TRAPS);
 401   static oop string_at_impl(constantPoolHandle this_oop, int which, TRAPS);
 402 
 403   // Resolve string constants (to prevent allocation during compilation)
 404   static void resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS);
 405 
 406  public:
 407   // Merging constantPoolOop support:
 408   bool compare_entry_to(int index1, constantPoolHandle cp2, int index2, TRAPS);
 409   void copy_cp_to(int start_i, int end_i, constantPoolHandle to_cp, int to_i,
 410     TRAPS);
 411   void copy_entry_to(int from_i, constantPoolHandle to_cp, int to_i, TRAPS);
 412   int  find_matching_entry(int pattern_i, constantPoolHandle search_cp, TRAPS);
 413   int  orig_length() const                { return _orig_length; }
 414   void set_orig_length(int orig_length)   { _orig_length = orig_length; }
 415 
 416 
 417   // JVMTI accesss - GetConstantPool, RetransformClasses, ...
 418   friend class JvmtiConstantPoolReconstituter;
 419 
 420  private:
 421   jint cpool_entry_size(jint idx);
 422   jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap);
 423 
 424   // Copy cpool bytes into byte array.
 425   // Returns:
 426   //  int > 0, count of the raw cpool bytes that have been copied
 427   //        0, OutOfMemory error
 428   //       -1, Internal error
 429   int  copy_cpool_bytes(int cpool_size,
 430                         SymbolHashMap* tbl,
 431                         unsigned char *bytes);
 432 };
 433 
 434 class SymbolHashMapEntry : public CHeapObj {
 435  private:
 436   unsigned int        _hash;   // 32-bit hash for item
 437   SymbolHashMapEntry* _next;   // Next element in the linked list for this bucket
 438   symbolOop           _symbol; // 1-st part of the mapping: symbol => value
 439   u2                  _value;  // 2-nd part of the mapping: symbol => value
 440 
 441  public:
 442   unsigned   int hash() const             { return _hash;   }
 443   void       set_hash(unsigned int hash)  { _hash = hash;   }
 444 
 445   SymbolHashMapEntry* next() const        { return _next;   }
 446   void set_next(SymbolHashMapEntry* next) { _next = next;   }
 447 
 448   symbolOop  symbol() const               { return _symbol; }
 449   void       set_symbol(symbolOop sym)    { _symbol = sym;  }
 450 
 451   u2         value() const                {  return _value; }
 452   void       set_value(u2 value)          { _value = value; }
 453 
 454   SymbolHashMapEntry(unsigned int hash, symbolOop symbol, u2 value)
 455     : _hash(hash), _symbol(symbol), _value(value), _next(NULL) {}
 456 
 457 }; // End SymbolHashMapEntry class
 458 
 459 
 460 class SymbolHashMapBucket : public CHeapObj {
 461 
 462 private:
 463   SymbolHashMapEntry*    _entry;
 464 
 465 public:
 466   SymbolHashMapEntry* entry() const         {  return _entry; }
 467   void set_entry(SymbolHashMapEntry* entry) { _entry = entry; }
 468   void clear()                              { _entry = NULL;  }
 469 
 470 }; // End SymbolHashMapBucket class
 471 
 472 
 473 class SymbolHashMap: public CHeapObj {
 474 
 475  private:
 476   // Default number of entries in the table
 477   enum SymbolHashMap_Constants {
 478     _Def_HashMap_Size = 256
 479   };
 480 
 481   int                   _table_size;
 482   SymbolHashMapBucket*  _buckets;
 483 
 484   void initialize_table(int table_size) {
 485     _table_size = table_size;
 486     _buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size);
 487     for (int index = 0; index < table_size; index++) {
 488       _buckets[index].clear();
 489     }
 490   }
 491 
 492  public:
 493 
 494   int table_size() const        { return _table_size; }
 495 
 496   SymbolHashMap()               { initialize_table(_Def_HashMap_Size); }
 497   SymbolHashMap(int table_size) { initialize_table(table_size); }
 498 
 499   // hash P(31) from Kernighan & Ritchie
 500   static unsigned int compute_hash(const char* str, int len) {
 501     unsigned int hash = 0;
 502     while (len-- > 0) {
 503       hash = 31*hash + (unsigned) *str;
 504       str++;
 505     }
 506     return hash;
 507   }
 508 
 509   SymbolHashMapEntry* bucket(int i) {
 510     return _buckets[i].entry();
 511   }
 512 
 513   void add_entry(symbolOop sym, u2 value);
 514   SymbolHashMapEntry* find_entry(symbolOop sym);
 515 
 516   u2 symbol_to_value(symbolOop sym) {
 517     SymbolHashMapEntry *entry = find_entry(sym);
 518     return (entry == NULL) ? 0 : entry->value();
 519   }
 520 
 521   ~SymbolHashMap() {
 522     SymbolHashMapEntry* next;
 523     for (int i = 0; i < _table_size; i++) {
 524       for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) {
 525         next = cur->next();
 526         delete(cur); 
 527       }
 528     }
 529     delete _buckets;
 530   }
 531 }; // End SymbolHashMap class