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