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