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