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