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 #ifndef SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP 26 #define SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP 27 28 #include "memory/allocation.inline.hpp" 29 #include "oops/arrayOop.hpp" 30 #include "oops/cpCache.hpp" 31 #include "oops/objArrayOop.hpp" 32 #include "oops/oopHandle.hpp" 33 #include "oops/symbol.hpp" 34 #include "oops/typeArrayOop.hpp" 35 #include "runtime/handles.hpp" 36 #include "utilities/align.hpp" 37 #include "utilities/bytes.hpp" 38 #include "utilities/constantTag.hpp" 39 40 // A ConstantPool is an array containing class constants as described in the 41 // class file. 42 // 43 // Most of the constant pool entries are written during class parsing, which 44 // is safe. For klass types, the constant pool entry is 45 // modified when the entry is resolved. If a klass constant pool 46 // entry is read without a lock, only the resolved state guarantees that 47 // the entry in the constant pool is a klass object and not a Symbol*. 48 49 class SymbolHashMap; 50 51 class CPSlot VALUE_OBJ_CLASS_SPEC { 52 friend class ConstantPool; 53 intptr_t _ptr; 54 enum TagBits {_pseudo_bit = 1}; 55 public: 56 57 CPSlot(intptr_t ptr): _ptr(ptr) {} 58 CPSlot(Symbol* ptr, int tag_bits = 0): _ptr((intptr_t)ptr | tag_bits) {} 59 60 intptr_t value() { return _ptr; } 61 bool is_pseudo_string() { return (_ptr & _pseudo_bit) != 0; } 62 63 Symbol* get_symbol() { 64 return (Symbol*)(_ptr & ~_pseudo_bit); 65 } 66 }; 67 68 // This represents a (JVM_CONSTANT_Class, JVM_CONSTANT_UnresolvedClass or JVM_CONSTANT_UnresolvedClassInError) or 69 // (JVM_CONSTANT_Value, JVM_CONSTANT_UnresolvedValue or JVM_CONSTANT_UnresolvedValueInError) 70 // slot in the constant pool. 71 class CPKlassSlot VALUE_OBJ_CLASS_SPEC { 72 // cp->symbol_at(_name_index) gives the name of the class. 73 int _name_index; 74 75 // cp->_resolved_klasses->at(_resolved_klass_index) gives the Klass* for the class. 76 int _resolved_klass_index; 77 public: 78 enum { 79 // This is used during constant pool merging where the resolved klass index is 80 // not yet known, and will be computed at a later stage (during a call to 81 // initialize_unresolved_klasses()). 82 _temp_resolved_klass_index = 0xffff 83 }; 84 CPKlassSlot(int n, int rk) { 85 _name_index = n; 86 _resolved_klass_index = rk; 87 } 88 int name_index() const { 89 return _name_index; 90 } 91 int resolved_klass_index() const { 92 assert(_resolved_klass_index != _temp_resolved_klass_index, "constant pool merging was incomplete"); 93 return _resolved_klass_index; 94 } 95 }; 96 97 class KlassSizeStats; 98 99 class ConstantPool : public Metadata { 100 friend class VMStructs; 101 friend class JVMCIVMStructs; 102 friend class BytecodeInterpreter; // Directly extracts a klass in the pool for fast instanceof/checkcast 103 friend class Universe; // For null constructor 104 private: 105 // If you add a new field that points to any metaspace object, you 106 // must add this field to ConstantPool::metaspace_pointers_do(). 107 Array<u1>* _tags; // the tag array describing the constant pool's contents 108 ConstantPoolCache* _cache; // the cache holding interpreter runtime information 109 InstanceKlass* _pool_holder; // the corresponding class 110 Array<u2>* _operands; // for variable-sized (InvokeDynamic) nodes, usually empty 111 112 // Consider using an array of compressed klass pointers to 113 // save space on 64-bit platforms. 114 Array<Klass*>* _resolved_klasses; 115 116 enum { 117 _has_preresolution = 1, // Flags 118 _on_stack = 2, 119 _is_shared = 4, 120 _has_dynamic_constant = 8 121 }; 122 123 int _flags; // old fashioned bit twiddling 124 int _length; // number of elements in the array 125 126 union { 127 // set for CDS to restore resolved references 128 int _resolved_reference_length; 129 // keeps version number for redefined classes (used in backtrace) 130 int _version; 131 } _saved; 132 133 void set_tags(Array<u1>* tags) { _tags = tags; } 134 void tag_at_put(int which, jbyte t) { tags()->at_put(which, t); } 135 void release_tag_at_put(int which, jbyte t) { tags()->release_at_put(which, t); } 136 137 u1* tag_addr_at(int which) const { return tags()->adr_at(which); } 138 139 void set_operands(Array<u2>* operands) { _operands = operands; } 140 141 int flags() const { return _flags; } 142 void set_flags(int f) { _flags = f; } 143 144 private: 145 intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(ConstantPool)); } 146 147 CPSlot slot_at(int which) const { 148 assert(is_within_bounds(which), "index out of bounds"); 149 assert(!tag_at(which).is_unresolved_klass() && !tag_at(which).is_unresolved_klass_in_error() && 150 !tag_at(which).is_unresolved_value_type() && !tag_at(which).is_unresolved_value_type_in_error(), "Corrupted constant pool"); 151 // Uses volatile because the klass slot changes without a lock. 152 intptr_t adr = OrderAccess::load_acquire(obj_at_addr_raw(which)); 153 assert(adr != 0 || which == 0, "cp entry for klass should not be zero"); 154 return CPSlot(adr); 155 } 156 157 void slot_at_put(int which, CPSlot s) const { 158 assert(is_within_bounds(which), "index out of bounds"); 159 assert(s.value() != 0, "Caught something"); 160 *(intptr_t*)&base()[which] = s.value(); 161 } 162 intptr_t* obj_at_addr_raw(int which) const { 163 assert(is_within_bounds(which), "index out of bounds"); 164 return (intptr_t*) &base()[which]; 165 } 166 167 jint* int_at_addr(int which) const { 168 assert(is_within_bounds(which), "index out of bounds"); 169 return (jint*) &base()[which]; 170 } 171 172 jlong* long_at_addr(int which) const { 173 assert(is_within_bounds(which), "index out of bounds"); 174 return (jlong*) &base()[which]; 175 } 176 177 jfloat* float_at_addr(int which) const { 178 assert(is_within_bounds(which), "index out of bounds"); 179 return (jfloat*) &base()[which]; 180 } 181 182 jdouble* double_at_addr(int which) const { 183 assert(is_within_bounds(which), "index out of bounds"); 184 return (jdouble*) &base()[which]; 185 } 186 187 ConstantPool(Array<u1>* tags); 188 ConstantPool() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); } 189 public: 190 static ConstantPool* allocate(ClassLoaderData* loader_data, int length, TRAPS); 191 192 bool is_constantPool() const volatile { return true; } 193 194 Array<u1>* tags() const { return _tags; } 195 Array<u2>* operands() const { return _operands; } 196 197 bool has_preresolution() const { return (_flags & _has_preresolution) != 0; } 198 void set_has_preresolution() { 199 assert(!is_shared(), "should never be called on shared ConstantPools"); 200 _flags |= _has_preresolution; 201 } 202 203 // Redefine classes support. If a method refering to this constant pool 204 // is on the executing stack, or as a handle in vm code, this constant pool 205 // can't be removed from the set of previous versions saved in the instance 206 // class. 207 bool on_stack() const { return (_flags &_on_stack) != 0; } 208 void set_on_stack(const bool value); 209 210 // Faster than MetaspaceObj::is_shared() - used by set_on_stack() 211 bool is_shared() const { return (_flags & _is_shared) != 0; } 212 213 bool has_dynamic_constant() const { return (_flags & _has_dynamic_constant) != 0; } 214 void set_has_dynamic_constant() { _flags |= _has_dynamic_constant; } 215 216 // Klass holding pool 217 InstanceKlass* pool_holder() const { return _pool_holder; } 218 void set_pool_holder(InstanceKlass* k) { _pool_holder = k; } 219 InstanceKlass** pool_holder_addr() { return &_pool_holder; } 220 221 // Interpreter runtime support 222 ConstantPoolCache* cache() const { return _cache; } 223 void set_cache(ConstantPoolCache* cache){ _cache = cache; } 224 225 virtual void metaspace_pointers_do(MetaspaceClosure* iter); 226 virtual MetaspaceObj::Type type() const { return ConstantPoolType; } 227 228 // Create object cache in the constant pool 229 void initialize_resolved_references(ClassLoaderData* loader_data, 230 const intStack& reference_map, 231 int constant_pool_map_length, 232 TRAPS); 233 234 // resolved strings, methodHandles and callsite objects from the constant pool 235 objArrayOop resolved_references() const; 236 objArrayOop resolved_references_or_null() const; 237 // mapping resolved object array indexes to cp indexes and back. 238 int object_to_cp_index(int index) { return reference_map()->at(index); } 239 int cp_to_object_index(int index); 240 241 void set_resolved_klasses(Array<Klass*>* rk) { _resolved_klasses = rk; } 242 Array<Klass*>* resolved_klasses() const { return _resolved_klasses; } 243 void allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS); 244 void initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS); 245 246 // Invokedynamic indexes. 247 // They must look completely different from normal indexes. 248 // The main reason is that byte swapping is sometimes done on normal indexes. 249 // Finally, it is helpful for debugging to tell the two apart. 250 static bool is_invokedynamic_index(int i) { return (i < 0); } 251 static int decode_invokedynamic_index(int i) { assert(is_invokedynamic_index(i), ""); return ~i; } 252 static int encode_invokedynamic_index(int i) { assert(!is_invokedynamic_index(i), ""); return ~i; } 253 254 255 // The invokedynamic points at a CP cache entry. This entry points back 256 // at the original CP entry (CONSTANT_InvokeDynamic) and also (via f2) at an entry 257 // in the resolved_references array (which provides the appendix argument). 258 int invokedynamic_cp_cache_index(int index) const { 259 assert (is_invokedynamic_index(index), "should be a invokedynamic index"); 260 int cache_index = decode_invokedynamic_index(index); 261 return cache_index; 262 } 263 ConstantPoolCacheEntry* invokedynamic_cp_cache_entry_at(int index) const { 264 // decode index that invokedynamic points to. 265 int cp_cache_index = invokedynamic_cp_cache_index(index); 266 return cache()->entry_at(cp_cache_index); 267 } 268 269 // Assembly code support 270 static int tags_offset_in_bytes() { return offset_of(ConstantPool, _tags); } 271 static int cache_offset_in_bytes() { return offset_of(ConstantPool, _cache); } 272 static int pool_holder_offset_in_bytes() { return offset_of(ConstantPool, _pool_holder); } 273 static int resolved_klasses_offset_in_bytes() { return offset_of(ConstantPool, _resolved_klasses); } 274 275 // Storing constants 276 277 // For temporary use while constructing constant pool. Used during a retransform/class redefinition as well. 278 void klass_index_at_put(int which, int name_index) { 279 tag_at_put(which, JVM_CONSTANT_ClassIndex); 280 *int_at_addr(which) = name_index; 281 } 282 283 // For temporary use while constructing constant pool. Used during a retransform/class redefinition as well. 284 void value_type_index_at_put(int which, int name_index) { 285 tag_at_put(which, JVM_CONSTANT_ValueIndex); 286 *int_at_addr(which) = name_index; 287 } 288 289 // Anonymous class support: 290 void klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name); 291 void klass_at_put(int class_index, Klass* k); 292 293 void unresolved_klass_at_put(int which, int name_index, int resolved_klass_index) { 294 release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass); 295 296 assert((name_index & 0xffff0000) == 0, "must be"); 297 assert((resolved_klass_index & 0xffff0000) == 0, "must be"); 298 *int_at_addr(which) = 299 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index); 300 } 301 302 void unresolved_value_type_at_put(int which, int name_index, int resolved_klass_index) { 303 release_tag_at_put(which, JVM_CONSTANT_UnresolvedValue); 304 305 assert((name_index & 0xffff0000) == 0, "must be"); 306 assert((resolved_klass_index & 0xffff0000) == 0, "must be"); 307 *int_at_addr(which) = 308 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index); 309 } 310 311 void method_handle_index_at_put(int which, int ref_kind, int ref_index) { 312 tag_at_put(which, JVM_CONSTANT_MethodHandle); 313 *int_at_addr(which) = ((jint) ref_index<<16) | ref_kind; 314 } 315 316 void method_type_index_at_put(int which, int ref_index) { 317 tag_at_put(which, JVM_CONSTANT_MethodType); 318 *int_at_addr(which) = ref_index; 319 } 320 321 void dynamic_constant_at_put(int which, int bootstrap_specifier_index, int name_and_type_index) { 322 tag_at_put(which, JVM_CONSTANT_Dynamic); 323 *int_at_addr(which) = ((jint) name_and_type_index<<16) | bootstrap_specifier_index; 324 } 325 326 void invoke_dynamic_at_put(int which, int bootstrap_specifier_index, int name_and_type_index) { 327 tag_at_put(which, JVM_CONSTANT_InvokeDynamic); 328 *int_at_addr(which) = ((jint) name_and_type_index<<16) | bootstrap_specifier_index; 329 } 330 331 void unresolved_string_at_put(int which, Symbol* s) { 332 release_tag_at_put(which, JVM_CONSTANT_String); 333 slot_at_put(which, CPSlot(s)); 334 } 335 336 void int_at_put(int which, jint i) { 337 tag_at_put(which, JVM_CONSTANT_Integer); 338 *int_at_addr(which) = i; 339 } 340 341 void long_at_put(int which, jlong l) { 342 tag_at_put(which, JVM_CONSTANT_Long); 343 // *long_at_addr(which) = l; 344 Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l)); 345 } 346 347 void float_at_put(int which, jfloat f) { 348 tag_at_put(which, JVM_CONSTANT_Float); 349 *float_at_addr(which) = f; 350 } 351 352 void double_at_put(int which, jdouble d) { 353 tag_at_put(which, JVM_CONSTANT_Double); 354 // *double_at_addr(which) = d; 355 // u8 temp = *(u8*) &d; 356 Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d)); 357 } 358 359 Symbol** symbol_at_addr(int which) const { 360 assert(is_within_bounds(which), "index out of bounds"); 361 return (Symbol**) &base()[which]; 362 } 363 364 void symbol_at_put(int which, Symbol* s) { 365 assert(s->refcount() != 0, "should have nonzero refcount"); 366 tag_at_put(which, JVM_CONSTANT_Utf8); 367 *symbol_at_addr(which) = s; 368 } 369 370 void string_at_put(int which, int obj_index, oop str); 371 372 // For temporary use while constructing constant pool 373 void string_index_at_put(int which, int string_index) { 374 tag_at_put(which, JVM_CONSTANT_StringIndex); 375 *int_at_addr(which) = string_index; 376 } 377 378 void field_at_put(int which, int class_index, int name_and_type_index) { 379 tag_at_put(which, JVM_CONSTANT_Fieldref); 380 *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index; 381 } 382 383 void method_at_put(int which, int class_index, int name_and_type_index) { 384 tag_at_put(which, JVM_CONSTANT_Methodref); 385 *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index; 386 } 387 388 void interface_method_at_put(int which, int class_index, int name_and_type_index) { 389 tag_at_put(which, JVM_CONSTANT_InterfaceMethodref); 390 *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index; // Not so nice 391 } 392 393 void name_and_type_at_put(int which, int name_index, int signature_index) { 394 tag_at_put(which, JVM_CONSTANT_NameAndType); 395 *int_at_addr(which) = ((jint) signature_index<<16) | name_index; // Not so nice 396 } 397 398 // Tag query 399 400 constantTag tag_at(int which) const { return (constantTag)tags()->at_acquire(which); } 401 402 // Fetching constants 403 404 Klass* klass_at(int which, TRAPS) { 405 constantPoolHandle h_this(THREAD, this); 406 return klass_at_impl(h_this, which, true, THREAD); 407 } 408 409 // Version of klass_at that doesn't save the resolution error, called during deopt 410 Klass* klass_at_ignore_error(int which, TRAPS) { 411 constantPoolHandle h_this(THREAD, this); 412 return klass_at_impl(h_this, which, false, THREAD); 413 } 414 415 CPKlassSlot klass_slot_at(int which) const { 416 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass() || 417 tag_at(which).is_unresolved_value_type() || tag_at(which).is_value_type(), 418 "Corrupted constant pool"); 419 int value = *int_at_addr(which); 420 int name_index = extract_high_short_from_int(value); 421 int resolved_klass_index = extract_low_short_from_int(value); 422 return CPKlassSlot(name_index, resolved_klass_index); 423 } 424 425 Symbol* klass_name_at(int which) const; // Returns the name, w/o resolving. 426 int klass_name_index_at(int which) const { 427 return klass_slot_at(which).name_index(); 428 } 429 430 Klass* resolved_klass_at(int which) const { // Used by Compiler 431 guarantee(tag_at(which).is_klass() || tag_at(which).is_value_type(), "Corrupted constant pool"); 432 // Must do an acquire here in case another thread resolved the klass 433 // behind our back, lest we later load stale values thru the oop. 434 CPKlassSlot kslot = klass_slot_at(which); 435 assert(tag_at(kslot.name_index()).is_symbol(), "sanity"); 436 437 Klass** adr = resolved_klasses()->adr_at(kslot.resolved_klass_index()); 438 return OrderAccess::load_acquire(adr); 439 } 440 441 // RedefineClasses() API support: 442 Symbol* klass_at_noresolve(int which) { return klass_name_at(which); } 443 void temp_unresolved_klass_at_put(int which, int name_index) { 444 // Used only during constant pool merging for class redefinition. The resolved klass index 445 // will be initialized later by a call to initialize_unresolved_klasses(). 446 unresolved_klass_at_put(which, name_index, CPKlassSlot::_temp_resolved_klass_index); 447 } 448 void temp_unresolved_value_type_at_put(int which, int name_index) { 449 // Used only during constant pool merging for class redefinition. The resolved klass index 450 // will be initialized later by a call to initialize_unresolved_klasses(). 451 unresolved_value_type_at_put(which, name_index, CPKlassSlot::_temp_resolved_klass_index); 452 } 453 454 jint int_at(int which) { 455 assert(tag_at(which).is_int(), "Corrupted constant pool"); 456 return *int_at_addr(which); 457 } 458 459 jlong long_at(int which) { 460 assert(tag_at(which).is_long(), "Corrupted constant pool"); 461 // return *long_at_addr(which); 462 u8 tmp = Bytes::get_native_u8((address)&base()[which]); 463 return *((jlong*)&tmp); 464 } 465 466 jfloat float_at(int which) { 467 assert(tag_at(which).is_float(), "Corrupted constant pool"); 468 return *float_at_addr(which); 469 } 470 471 jdouble double_at(int which) { 472 assert(tag_at(which).is_double(), "Corrupted constant pool"); 473 u8 tmp = Bytes::get_native_u8((address)&base()[which]); 474 return *((jdouble*)&tmp); 475 } 476 477 Symbol* symbol_at(int which) const { 478 assert(tag_at(which).is_utf8(), "Corrupted constant pool"); 479 return *symbol_at_addr(which); 480 } 481 482 oop string_at(int which, int obj_index, TRAPS) { 483 constantPoolHandle h_this(THREAD, this); 484 return string_at_impl(h_this, which, obj_index, THREAD); 485 } 486 oop string_at(int which, TRAPS) { 487 int obj_index = cp_to_object_index(which); 488 return string_at(which, obj_index, THREAD); 489 } 490 491 // Version that can be used before string oop array is created. 492 oop uncached_string_at(int which, TRAPS); 493 494 // A "pseudo-string" is an non-string oop that has found its way into 495 // a String entry. 496 // This can happen if the user patches a live 497 // object into a CONSTANT_String entry of an anonymous class. 498 // Method oops internally created for method handles may also 499 // use pseudo-strings to link themselves to related metaobjects. 500 501 bool is_pseudo_string_at(int which) { 502 assert(tag_at(which).is_string(), "Corrupted constant pool"); 503 return slot_at(which).is_pseudo_string(); 504 } 505 506 oop pseudo_string_at(int which, int obj_index) { 507 assert(is_pseudo_string_at(which), "must be a pseudo-string"); 508 oop s = resolved_references()->obj_at(obj_index); 509 return s; 510 } 511 512 oop pseudo_string_at(int which) { 513 assert(is_pseudo_string_at(which), "must be a pseudo-string"); 514 int obj_index = cp_to_object_index(which); 515 oop s = resolved_references()->obj_at(obj_index); 516 return s; 517 } 518 519 void pseudo_string_at_put(int which, int obj_index, oop x) { 520 assert(tag_at(which).is_string(), "Corrupted constant pool"); 521 Symbol* sym = unresolved_string_at(which); 522 slot_at_put(which, CPSlot(sym, CPSlot::_pseudo_bit)); 523 string_at_put(which, obj_index, x); // this works just fine 524 } 525 526 // only called when we are sure a string entry is already resolved (via an 527 // earlier string_at call. 528 oop resolved_string_at(int which) { 529 assert(tag_at(which).is_string(), "Corrupted constant pool"); 530 // Must do an acquire here in case another thread resolved the klass 531 // behind our back, lest we later load stale values thru the oop. 532 // we might want a volatile_obj_at in ObjArrayKlass. 533 int obj_index = cp_to_object_index(which); 534 return resolved_references()->obj_at(obj_index); 535 } 536 537 Symbol* unresolved_string_at(int which) { 538 assert(tag_at(which).is_string(), "Corrupted constant pool"); 539 Symbol* sym = slot_at(which).get_symbol(); 540 return sym; 541 } 542 543 // Returns an UTF8 for a CONSTANT_String entry at a given index. 544 // UTF8 char* representation was chosen to avoid conversion of 545 // java_lang_Strings at resolved entries into Symbol*s 546 // or vice versa. 547 char* string_at_noresolve(int which); 548 549 jint name_and_type_at(int which) { 550 assert(tag_at(which).is_name_and_type(), "Corrupted constant pool"); 551 return *int_at_addr(which); 552 } 553 554 int method_handle_ref_kind_at(int which) { 555 assert(tag_at(which).is_method_handle() || 556 tag_at(which).is_method_handle_in_error(), "Corrupted constant pool"); 557 return extract_low_short_from_int(*int_at_addr(which)); // mask out unwanted ref_index bits 558 } 559 int method_handle_index_at(int which) { 560 assert(tag_at(which).is_method_handle() || 561 tag_at(which).is_method_handle_in_error(), "Corrupted constant pool"); 562 return extract_high_short_from_int(*int_at_addr(which)); // shift out unwanted ref_kind bits 563 } 564 int method_type_index_at(int which) { 565 assert(tag_at(which).is_method_type() || 566 tag_at(which).is_method_type_in_error(), "Corrupted constant pool"); 567 return *int_at_addr(which); 568 } 569 570 // Derived queries: 571 Symbol* method_handle_name_ref_at(int which) { 572 int member = method_handle_index_at(which); 573 return impl_name_ref_at(member, true); 574 } 575 Symbol* method_handle_signature_ref_at(int which) { 576 int member = method_handle_index_at(which); 577 return impl_signature_ref_at(member, true); 578 } 579 int method_handle_klass_index_at(int which) { 580 int member = method_handle_index_at(which); 581 return impl_klass_ref_index_at(member, true); 582 } 583 Symbol* method_type_signature_at(int which) { 584 int sym = method_type_index_at(which); 585 return symbol_at(sym); 586 } 587 588 int invoke_dynamic_name_and_type_ref_index_at(int which) { 589 assert(tag_at(which).is_invoke_dynamic() || 590 tag_at(which).is_dynamic_constant() || 591 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool"); 592 return extract_high_short_from_int(*int_at_addr(which)); 593 } 594 int invoke_dynamic_bootstrap_specifier_index(int which) { 595 assert(tag_at(which).is_invoke_dynamic() || 596 tag_at(which).is_dynamic_constant() || 597 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool"); 598 return extract_low_short_from_int(*int_at_addr(which)); 599 } 600 int invoke_dynamic_operand_base(int which) { 601 int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which); 602 return operand_offset_at(operands(), bootstrap_specifier_index); 603 } 604 // The first part of the operands array consists of an index into the second part. 605 // Extract a 32-bit index value from the first part. 606 static int operand_offset_at(Array<u2>* operands, int bootstrap_specifier_index) { 607 int n = (bootstrap_specifier_index * 2); 608 assert(n >= 0 && n+2 <= operands->length(), "oob"); 609 // The first 32-bit index points to the beginning of the second part 610 // of the operands array. Make sure this index is in the first part. 611 DEBUG_ONLY(int second_part = build_int_from_shorts(operands->at(0), 612 operands->at(1))); 613 assert(second_part == 0 || n+2 <= second_part, "oob (2)"); 614 int offset = build_int_from_shorts(operands->at(n+0), 615 operands->at(n+1)); 616 // The offset itself must point into the second part of the array. 617 assert(offset == 0 || offset >= second_part && offset <= operands->length(), "oob (3)"); 618 return offset; 619 } 620 static void operand_offset_at_put(Array<u2>* operands, int bootstrap_specifier_index, int offset) { 621 int n = bootstrap_specifier_index * 2; 622 assert(n >= 0 && n+2 <= operands->length(), "oob"); 623 operands->at_put(n+0, extract_low_short_from_int(offset)); 624 operands->at_put(n+1, extract_high_short_from_int(offset)); 625 } 626 static int operand_array_length(Array<u2>* operands) { 627 if (operands == NULL || operands->length() == 0) return 0; 628 int second_part = operand_offset_at(operands, 0); 629 return (second_part / 2); 630 } 631 632 #ifdef ASSERT 633 // operand tuples fit together exactly, end to end 634 static int operand_limit_at(Array<u2>* operands, int bootstrap_specifier_index) { 635 int nextidx = bootstrap_specifier_index + 1; 636 if (nextidx == operand_array_length(operands)) 637 return operands->length(); 638 else 639 return operand_offset_at(operands, nextidx); 640 } 641 int invoke_dynamic_operand_limit(int which) { 642 int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which); 643 return operand_limit_at(operands(), bootstrap_specifier_index); 644 } 645 #endif //ASSERT 646 647 // layout of InvokeDynamic and Dynamic bootstrap method specifier (in second part of operands array): 648 enum { 649 _indy_bsm_offset = 0, // CONSTANT_MethodHandle bsm 650 _indy_argc_offset = 1, // u2 argc 651 _indy_argv_offset = 2 // u2 argv[argc] 652 }; 653 654 // These functions are used in RedefineClasses for CP merge 655 656 int operand_offset_at(int bootstrap_specifier_index) { 657 assert(0 <= bootstrap_specifier_index && 658 bootstrap_specifier_index < operand_array_length(operands()), 659 "Corrupted CP operands"); 660 return operand_offset_at(operands(), bootstrap_specifier_index); 661 } 662 int operand_bootstrap_method_ref_index_at(int bootstrap_specifier_index) { 663 int offset = operand_offset_at(bootstrap_specifier_index); 664 return operands()->at(offset + _indy_bsm_offset); 665 } 666 int operand_argument_count_at(int bootstrap_specifier_index) { 667 int offset = operand_offset_at(bootstrap_specifier_index); 668 int argc = operands()->at(offset + _indy_argc_offset); 669 return argc; 670 } 671 int operand_argument_index_at(int bootstrap_specifier_index, int j) { 672 int offset = operand_offset_at(bootstrap_specifier_index); 673 return operands()->at(offset + _indy_argv_offset + j); 674 } 675 int operand_next_offset_at(int bootstrap_specifier_index) { 676 int offset = operand_offset_at(bootstrap_specifier_index) + _indy_argv_offset 677 + operand_argument_count_at(bootstrap_specifier_index); 678 return offset; 679 } 680 // Compare a bootsrap specifier in the operands arrays 681 bool compare_operand_to(int bootstrap_specifier_index1, const constantPoolHandle& cp2, 682 int bootstrap_specifier_index2, TRAPS); 683 // Find a bootsrap specifier in the operands array 684 int find_matching_operand(int bootstrap_specifier_index, const constantPoolHandle& search_cp, 685 int operands_cur_len, TRAPS); 686 // Resize the operands array with delta_len and delta_size 687 void resize_operands(int delta_len, int delta_size, TRAPS); 688 // Extend the operands array with the length and size of the ext_cp operands 689 void extend_operands(const constantPoolHandle& ext_cp, TRAPS); 690 // Shrink the operands array to a smaller array with new_len length 691 void shrink_operands(int new_len, TRAPS); 692 693 int invoke_dynamic_bootstrap_method_ref_index_at(int which) { 694 assert(tag_at(which).is_invoke_dynamic() || 695 tag_at(which).is_dynamic_constant() || 696 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool"); 697 int op_base = invoke_dynamic_operand_base(which); 698 return operands()->at(op_base + _indy_bsm_offset); 699 } 700 int invoke_dynamic_argument_count_at(int which) { 701 assert(tag_at(which).is_invoke_dynamic() || 702 tag_at(which).is_dynamic_constant() || 703 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool"); 704 int op_base = invoke_dynamic_operand_base(which); 705 int argc = operands()->at(op_base + _indy_argc_offset); 706 DEBUG_ONLY(int end_offset = op_base + _indy_argv_offset + argc; 707 int next_offset = invoke_dynamic_operand_limit(which)); 708 assert(end_offset == next_offset, "matched ending"); 709 return argc; 710 } 711 int invoke_dynamic_argument_index_at(int which, int j) { 712 int op_base = invoke_dynamic_operand_base(which); 713 DEBUG_ONLY(int argc = operands()->at(op_base + _indy_argc_offset)); 714 assert((uint)j < (uint)argc, "oob"); 715 return operands()->at(op_base + _indy_argv_offset + j); 716 } 717 718 // The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve, 719 // name_and_type_ref_index_at) all expect to be passed indices obtained 720 // directly from the bytecode. 721 // If the indices are meant to refer to fields or methods, they are 722 // actually rewritten constant pool cache indices. 723 // The routine remap_instruction_operand_from_cache manages the adjustment 724 // of these values back to constant pool indices. 725 726 // There are also "uncached" versions which do not adjust the operand index; see below. 727 728 // FIXME: Consider renaming these with a prefix "cached_" to make the distinction clear. 729 // In a few cases (the verifier) there are uses before a cpcache has been built, 730 // which are handled by a dynamic check in remap_instruction_operand_from_cache. 731 // FIXME: Remove the dynamic check, and adjust all callers to specify the correct mode. 732 733 // Lookup for entries consisting of (klass_index, name_and_type index) 734 Klass* klass_ref_at(int which, TRAPS); 735 Symbol* klass_ref_at_noresolve(int which); 736 Symbol* name_ref_at(int which) { return impl_name_ref_at(which, false); } 737 Symbol* signature_ref_at(int which) { return impl_signature_ref_at(which, false); } 738 739 int klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, false); } 740 int name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, false); } 741 742 int remap_instruction_operand_from_cache(int operand); // operand must be biased by CPCACHE_INDEX_TAG 743 744 constantTag tag_ref_at(int cp_cache_index) { return impl_tag_ref_at(cp_cache_index, false); } 745 746 // Lookup for entries consisting of (name_index, signature_index) 747 int name_ref_index_at(int which_nt); // == low-order jshort of name_and_type_at(which_nt) 748 int signature_ref_index_at(int which_nt); // == high-order jshort of name_and_type_at(which_nt) 749 750 BasicType basic_type_for_signature_at(int which) const; 751 752 // Resolve string constants (to prevent allocation during compilation) 753 void resolve_string_constants(TRAPS) { 754 constantPoolHandle h_this(THREAD, this); 755 resolve_string_constants_impl(h_this, CHECK); 756 } 757 758 // CDS support 759 void archive_resolved_references(Thread *THREAD) NOT_CDS_JAVA_HEAP_RETURN; 760 void resolve_class_constants(TRAPS) NOT_CDS_JAVA_HEAP_RETURN; 761 void remove_unshareable_info(); 762 void restore_unshareable_info(TRAPS); 763 // The ConstantPool vtable is restored by this call when the ConstantPool is 764 // in the shared archive. See patch_klass_vtables() in metaspaceShared.cpp for 765 // all the gory details. SA, dtrace and pstack helpers distinguish metadata 766 // by their vtable. 767 void restore_vtable() { guarantee(is_constantPool(), "vtable restored by this call"); } 768 769 private: 770 enum { _no_index_sentinel = -1, _possible_index_sentinel = -2 }; 771 public: 772 773 BasicType basic_type_for_constant_at(int which); 774 775 // Resolve late bound constants. 776 oop resolve_constant_at(int index, TRAPS) { 777 constantPoolHandle h_this(THREAD, this); 778 return resolve_constant_at_impl(h_this, index, _no_index_sentinel, NULL, THREAD); 779 } 780 781 oop resolve_cached_constant_at(int cache_index, TRAPS) { 782 constantPoolHandle h_this(THREAD, this); 783 return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, NULL, THREAD); 784 } 785 786 oop resolve_possibly_cached_constant_at(int pool_index, TRAPS) { 787 constantPoolHandle h_this(THREAD, this); 788 return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, NULL, THREAD); 789 } 790 791 oop find_cached_constant_at(int pool_index, bool& found_it, TRAPS) { 792 constantPoolHandle h_this(THREAD, this); 793 return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, &found_it, THREAD); 794 } 795 796 oop resolve_bootstrap_specifier_at(int index, TRAPS) { 797 constantPoolHandle h_this(THREAD, this); 798 return resolve_bootstrap_specifier_at_impl(h_this, index, THREAD); 799 } 800 801 void copy_bootstrap_arguments_at(int index, 802 int start_arg, int end_arg, 803 objArrayHandle info, int pos, 804 bool must_resolve, Handle if_not_available, TRAPS) { 805 constantPoolHandle h_this(THREAD, this); 806 copy_bootstrap_arguments_at_impl(h_this, index, start_arg, end_arg, 807 info, pos, must_resolve, if_not_available, THREAD); 808 } 809 810 // Klass name matches name at offset 811 bool klass_name_at_matches(const InstanceKlass* k, int which); 812 813 // Sizing 814 int length() const { return _length; } 815 void set_length(int length) { _length = length; } 816 817 // Tells whether index is within bounds. 818 bool is_within_bounds(int index) const { 819 return 0 <= index && index < length(); 820 } 821 822 // Sizing (in words) 823 static int header_size() { 824 return align_up((int)sizeof(ConstantPool), wordSize) / wordSize; 825 } 826 static int size(int length) { return align_metadata_size(header_size() + length); } 827 int size() const { return size(length()); } 828 #if INCLUDE_SERVICES 829 void collect_statistics(KlassSizeStats *sz) const; 830 #endif 831 832 // ConstantPools should be stored in the read-only region of CDS archive. 833 static bool is_read_only_by_default() { return true; } 834 835 friend class ClassFileParser; 836 friend class SystemDictionary; 837 838 // Used by CDS. These classes need to access the private ConstantPool() constructor. 839 template <class T> friend class CppVtableTesterA; 840 template <class T> friend class CppVtableTesterB; 841 template <class T> friend class CppVtableCloner; 842 843 // Used by compiler to prevent classloading. 844 static Method* method_at_if_loaded (const constantPoolHandle& this_cp, int which); 845 static bool has_appendix_at_if_loaded (const constantPoolHandle& this_cp, int which); 846 static oop appendix_at_if_loaded (const constantPoolHandle& this_cp, int which); 847 static bool has_method_type_at_if_loaded (const constantPoolHandle& this_cp, int which); 848 static oop method_type_at_if_loaded (const constantPoolHandle& this_cp, int which); 849 static Klass* klass_at_if_loaded (const constantPoolHandle& this_cp, int which); 850 static Klass* klass_ref_at_if_loaded (const constantPoolHandle& this_cp, int which); 851 852 // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the 853 // future by other Java code. These take constant pool indices rather than 854 // constant pool cache indices as do the peer methods above. 855 Symbol* uncached_klass_ref_at_noresolve(int which); 856 Symbol* uncached_name_ref_at(int which) { return impl_name_ref_at(which, true); } 857 Symbol* uncached_signature_ref_at(int which) { return impl_signature_ref_at(which, true); } 858 int uncached_klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, true); } 859 int uncached_name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, true); } 860 861 // Sharing 862 int pre_resolve_shared_klasses(TRAPS); 863 864 // Debugging 865 const char* printable_name_at(int which) PRODUCT_RETURN0; 866 867 #ifdef ASSERT 868 enum { CPCACHE_INDEX_TAG = 0x10000 }; // helps keep CP cache indices distinct from CP indices 869 #else 870 enum { CPCACHE_INDEX_TAG = 0 }; // in product mode, this zero value is a no-op 871 #endif //ASSERT 872 873 static int decode_cpcache_index(int raw_index, bool invokedynamic_ok = false) { 874 if (invokedynamic_ok && is_invokedynamic_index(raw_index)) 875 return decode_invokedynamic_index(raw_index); 876 else 877 return raw_index - CPCACHE_INDEX_TAG; 878 } 879 880 private: 881 882 void set_resolved_references(OopHandle s) { _cache->set_resolved_references(s); } 883 Array<u2>* reference_map() const { return (_cache == NULL) ? NULL : _cache->reference_map(); } 884 void set_reference_map(Array<u2>* o) { _cache->set_reference_map(o); } 885 886 // patch JSR 292 resolved references after the class is linked. 887 void patch_resolved_references(GrowableArray<Handle>* cp_patches); 888 889 Symbol* impl_name_ref_at(int which, bool uncached); 890 Symbol* impl_signature_ref_at(int which, bool uncached); 891 892 int impl_klass_ref_index_at(int which, bool uncached); 893 int impl_name_and_type_ref_index_at(int which, bool uncached); 894 constantTag impl_tag_ref_at(int which, bool uncached); 895 896 // Used while constructing constant pool (only by ClassFileParser) 897 jint klass_index_at(int which) { 898 assert(tag_at(which).is_klass_index(), "Corrupted constant pool"); 899 return *int_at_addr(which); 900 } 901 902 jint value_type_index_at(int which) { 903 assert(tag_at(which).is_value_type_index(), "Corrupted constant pool"); 904 return *int_at_addr(which); 905 } 906 907 jint string_index_at(int which) { 908 assert(tag_at(which).is_string_index(), "Corrupted constant pool"); 909 return *int_at_addr(which); 910 } 911 912 // Performs the LinkResolver checks 913 static void verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* klass, TRAPS); 914 915 // Implementation of methods that needs an exposed 'this' pointer, in order to 916 // handle GC while executing the method 917 static Klass* klass_at_impl(const constantPoolHandle& this_cp, int which, 918 bool save_resolution_error, TRAPS); 919 static oop string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS); 920 921 static void trace_class_resolution(const constantPoolHandle& this_cp, Klass* k); 922 923 // Resolve string constants (to prevent allocation during compilation) 924 static void resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS); 925 926 static oop resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index, 927 bool* status_return, TRAPS); 928 static oop resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS); 929 static void copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int index, 930 int start_arg, int end_arg, 931 objArrayHandle info, int pos, 932 bool must_resolve, Handle if_not_available, TRAPS); 933 934 // Exception handling 935 static Symbol* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception); 936 static void save_and_throw_exception(const constantPoolHandle& this_cp, int which, constantTag tag, TRAPS); 937 938 public: 939 // Exception handling 940 static void throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS); 941 942 // Merging ConstantPool* support: 943 bool compare_entry_to(int index1, const constantPoolHandle& cp2, int index2, TRAPS); 944 void copy_cp_to(int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS) { 945 constantPoolHandle h_this(THREAD, this); 946 copy_cp_to_impl(h_this, start_i, end_i, to_cp, to_i, THREAD); 947 } 948 static void copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS); 949 static void copy_entry_to(const constantPoolHandle& from_cp, int from_i, const constantPoolHandle& to_cp, int to_i, TRAPS); 950 static void copy_operands(const constantPoolHandle& from_cp, const constantPoolHandle& to_cp, TRAPS); 951 int find_matching_entry(int pattern_i, const constantPoolHandle& search_cp, TRAPS); 952 int version() const { return _saved._version; } 953 void set_version(int version) { _saved._version = version; } 954 void increment_and_save_version(int version) { 955 _saved._version = version >= 0 ? (version + 1) : version; // keep overflow 956 } 957 958 void set_resolved_reference_length(int length) { _saved._resolved_reference_length = length; } 959 int resolved_reference_length() const { return _saved._resolved_reference_length; } 960 961 // Decrease ref counts of symbols that are in the constant pool 962 // when the holder class is unloaded 963 void unreference_symbols(); 964 965 // Deallocate constant pool for RedefineClasses 966 void deallocate_contents(ClassLoaderData* loader_data); 967 void release_C_heap_structures(); 968 969 // JVMTI accesss - GetConstantPool, RetransformClasses, ... 970 friend class JvmtiConstantPoolReconstituter; 971 972 private: 973 jint cpool_entry_size(jint idx); 974 jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap); 975 976 // Copy cpool bytes into byte array. 977 // Returns: 978 // int > 0, count of the raw cpool bytes that have been copied 979 // 0, OutOfMemory error 980 // -1, Internal error 981 int copy_cpool_bytes(int cpool_size, 982 SymbolHashMap* tbl, 983 unsigned char *bytes); 984 985 public: 986 // Verify 987 void verify_on(outputStream* st); 988 989 // Printing 990 void print_on(outputStream* st) const; 991 void print_value_on(outputStream* st) const; 992 void print_entry_on(int index, outputStream* st); 993 994 const char* internal_name() const { return "{constant pool}"; } 995 996 #ifndef PRODUCT 997 // Compile the world support 998 static void preload_and_initialize_all_classes(ConstantPool* constant_pool, TRAPS); 999 #endif 1000 }; 1001 1002 class SymbolHashMapEntry : public CHeapObj<mtSymbol> { 1003 private: 1004 unsigned int _hash; // 32-bit hash for item 1005 SymbolHashMapEntry* _next; // Next element in the linked list for this bucket 1006 Symbol* _symbol; // 1-st part of the mapping: symbol => value 1007 u2 _value; // 2-nd part of the mapping: symbol => value 1008 1009 public: 1010 unsigned int hash() const { return _hash; } 1011 void set_hash(unsigned int hash) { _hash = hash; } 1012 1013 SymbolHashMapEntry* next() const { return _next; } 1014 void set_next(SymbolHashMapEntry* next) { _next = next; } 1015 1016 Symbol* symbol() const { return _symbol; } 1017 void set_symbol(Symbol* sym) { _symbol = sym; } 1018 1019 u2 value() const { return _value; } 1020 void set_value(u2 value) { _value = value; } 1021 1022 SymbolHashMapEntry(unsigned int hash, Symbol* symbol, u2 value) 1023 : _hash(hash), _symbol(symbol), _value(value), _next(NULL) {} 1024 1025 }; // End SymbolHashMapEntry class 1026 1027 1028 class SymbolHashMapBucket : public CHeapObj<mtSymbol> { 1029 1030 private: 1031 SymbolHashMapEntry* _entry; 1032 1033 public: 1034 SymbolHashMapEntry* entry() const { return _entry; } 1035 void set_entry(SymbolHashMapEntry* entry) { _entry = entry; } 1036 void clear() { _entry = NULL; } 1037 1038 }; // End SymbolHashMapBucket class 1039 1040 1041 class SymbolHashMap: public CHeapObj<mtSymbol> { 1042 1043 private: 1044 // Default number of entries in the table 1045 enum SymbolHashMap_Constants { 1046 _Def_HashMap_Size = 256 1047 }; 1048 1049 int _table_size; 1050 SymbolHashMapBucket* _buckets; 1051 1052 void initialize_table(int table_size); 1053 1054 public: 1055 1056 int table_size() const { return _table_size; } 1057 1058 SymbolHashMap() { initialize_table(_Def_HashMap_Size); } 1059 SymbolHashMap(int table_size) { initialize_table(table_size); } 1060 1061 // hash P(31) from Kernighan & Ritchie 1062 static unsigned int compute_hash(const char* str, int len) { 1063 unsigned int hash = 0; 1064 while (len-- > 0) { 1065 hash = 31*hash + (unsigned) *str; 1066 str++; 1067 } 1068 return hash; 1069 } 1070 1071 SymbolHashMapEntry* bucket(int i) { 1072 return _buckets[i].entry(); 1073 } 1074 1075 void add_entry(Symbol* sym, u2 value); 1076 SymbolHashMapEntry* find_entry(Symbol* sym); 1077 1078 u2 symbol_to_value(Symbol* sym) { 1079 SymbolHashMapEntry *entry = find_entry(sym); 1080 return (entry == NULL) ? 0 : entry->value(); 1081 } 1082 1083 ~SymbolHashMap() { 1084 SymbolHashMapEntry* next; 1085 for (int i = 0; i < _table_size; i++) { 1086 for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) { 1087 next = cur->next(); 1088 delete(cur); 1089 } 1090 } 1091 FREE_C_HEAP_ARRAY(SymbolHashMapBucket, _buckets); 1092 } 1093 }; // End SymbolHashMap class 1094 1095 #endif // SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP