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