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 #include "precompiled.hpp" 26 #include "classfile/classLoaderData.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/metadataOnStackMark.hpp" 29 #include "classfile/stringTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/vmSymbols.hpp" 32 #include "interpreter/linkResolver.hpp" 33 #include "memory/heapInspection.hpp" 34 #include "memory/metadataFactory.hpp" 35 #include "memory/metaspaceClosure.hpp" 36 #include "memory/oopFactory.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/constantPool.hpp" 39 #include "oops/instanceKlass.hpp" 40 #include "oops/objArrayKlass.hpp" 41 #include "oops/objArrayOop.inline.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "prims/jvm.h" 44 #include "runtime/fieldType.hpp" 45 #include "runtime/init.hpp" 46 #include "runtime/javaCalls.hpp" 47 #include "runtime/signature.hpp" 48 #include "runtime/vframe.hpp" 49 #include "utilities/copy.hpp" 50 51 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) { 52 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL); 53 int size = ConstantPool::size(length); 54 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags); 55 } 56 57 #ifdef ASSERT 58 59 // MetaspaceObj allocation invariant is calloc equivalent memory 60 // simple verification of this here (JVM_CONSTANT_Invalid == 0 ) 61 static bool tag_array_is_zero_initialized(Array<u1>* tags) { 62 assert(tags != NULL, "invariant"); 63 const int length = tags->length(); 64 for (int index = 0; index < length; ++index) { 65 if (JVM_CONSTANT_Invalid != tags->at(index)) { 66 return false; 67 } 68 } 69 return true; 70 } 71 72 #endif 73 74 ConstantPool::ConstantPool(Array<u1>* tags) : 75 _tags(tags), 76 _length(tags->length()) { 77 78 assert(_tags != NULL, "invariant"); 79 assert(tags->length() == _length, "invariant"); 80 assert(tag_array_is_zero_initialized(tags), "invariant"); 81 assert(0 == flags(), "invariant"); 82 assert(0 == version(), "invariant"); 83 assert(NULL == _pool_holder, "invariant"); 84 } 85 86 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) { 87 if (cache() != NULL) { 88 MetadataFactory::free_array<u2>(loader_data, reference_map()); 89 set_reference_map(NULL); 90 MetadataFactory::free_metadata(loader_data, cache()); 91 set_cache(NULL); 92 } 93 94 MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses()); 95 set_resolved_klasses(NULL); 96 97 MetadataFactory::free_array<jushort>(loader_data, operands()); 98 set_operands(NULL); 99 100 release_C_heap_structures(); 101 102 // free tag array 103 MetadataFactory::free_array<u1>(loader_data, tags()); 104 set_tags(NULL); 105 } 106 107 void ConstantPool::release_C_heap_structures() { 108 // walk constant pool and decrement symbol reference counts 109 unreference_symbols(); 110 } 111 112 void ConstantPool::metaspace_pointers_do(MetaspaceClosure* it) { 113 log_trace(cds)("Iter(ConstantPool): %p", this); 114 115 it->push(&_tags, MetaspaceClosure::_writable); 116 it->push(&_cache); 117 it->push(&_pool_holder); 118 it->push(&_operands); 119 it->push(&_resolved_klasses, MetaspaceClosure::_writable); 120 121 for (int i = 0; i < length(); i++) { 122 // The only MSO's embedded in the CP entries are Symbols: 123 // JVM_CONSTANT_String (normal and pseudo) 124 // JVM_CONSTANT_Utf8 125 constantTag ctag = tag_at(i); 126 if (ctag.is_string() || ctag.is_utf8()) { 127 it->push(symbol_at_addr(i)); 128 } 129 } 130 } 131 132 objArrayOop ConstantPool::resolved_references() const { 133 return (objArrayOop)JNIHandles::resolve(_cache->resolved_references()); 134 } 135 136 // Create resolved_references array and mapping array for original cp indexes 137 // The ldc bytecode was rewritten to have the resolved reference array index so need a way 138 // to map it back for resolving and some unlikely miscellaneous uses. 139 // The objects created by invokedynamic are appended to this list. 140 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data, 141 const intStack& reference_map, 142 int constant_pool_map_length, 143 TRAPS) { 144 // Initialized the resolved object cache. 145 int map_length = reference_map.length(); 146 if (map_length > 0) { 147 // Only need mapping back to constant pool entries. The map isn't used for 148 // invokedynamic resolved_reference entries. For invokedynamic entries, 149 // the constant pool cache index has the mapping back to both the constant 150 // pool and to the resolved reference index. 151 if (constant_pool_map_length > 0) { 152 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK); 153 154 for (int i = 0; i < constant_pool_map_length; i++) { 155 int x = reference_map.at(i); 156 assert(x == (int)(jushort) x, "klass index is too big"); 157 om->at_put(i, (jushort)x); 158 } 159 set_reference_map(om); 160 } 161 162 // Create Java array for holding resolved strings, methodHandles, 163 // methodTypes, invokedynamic and invokehandle appendix objects, etc. 164 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); 165 Handle refs_handle (THREAD, (oop)stom); // must handleize. 166 set_resolved_references(loader_data->add_handle(refs_handle)); 167 } 168 } 169 170 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) { 171 // A ConstantPool can't possibly have 0xffff valid class entries, 172 // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8 173 // entry for the class's name. So at most we will have 0xfffe class entries. 174 // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate 175 // UnresolvedKlass entries that are temporarily created during class redefinition. 176 assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity"); 177 assert(resolved_klasses() == NULL, "sanity"); 178 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK); 179 set_resolved_klasses(rk); 180 } 181 182 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) { 183 int len = length(); 184 int num_klasses = 0; 185 for (int i = 1; i <len; i++) { 186 switch (tag_at(i).value()) { 187 case JVM_CONSTANT_ClassIndex: 188 { 189 const int class_index = klass_index_at(i); 190 unresolved_klass_at_put(i, class_index, num_klasses++); 191 } 192 break; 193 #ifndef PRODUCT 194 case JVM_CONSTANT_Class: 195 case JVM_CONSTANT_UnresolvedClass: 196 case JVM_CONSTANT_UnresolvedClassInError: 197 // All of these should have been reverted back to ClassIndex before calling 198 // this function. 199 ShouldNotReachHere(); 200 #endif 201 } 202 } 203 allocate_resolved_klasses(loader_data, num_klasses, THREAD); 204 } 205 206 // Anonymous class support: 207 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) { 208 assert(is_within_bounds(class_index), "index out of bounds"); 209 assert(is_within_bounds(name_index), "index out of bounds"); 210 assert((resolved_klass_index & 0xffff0000) == 0, "must be"); 211 *int_at_addr(class_index) = 212 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index); 213 214 symbol_at_put(name_index, name); 215 name->increment_refcount(); 216 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index); 217 OrderAccess::release_store_ptr((Klass* volatile *)adr, k); 218 219 // The interpreter assumes when the tag is stored, the klass is resolved 220 // and the Klass* non-NULL, so we need hardware store ordering here. 221 if (k != NULL) { 222 release_tag_at_put(class_index, JVM_CONSTANT_Class); 223 } else { 224 release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass); 225 } 226 } 227 228 // Anonymous class support: 229 void ConstantPool::klass_at_put(int class_index, Klass* k) { 230 assert(k != NULL, "must be valid klass"); 231 CPKlassSlot kslot = klass_slot_at(class_index); 232 int resolved_klass_index = kslot.resolved_klass_index(); 233 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index); 234 OrderAccess::release_store_ptr((Klass* volatile *)adr, k); 235 236 // The interpreter assumes when the tag is stored, the klass is resolved 237 // and the Klass* non-NULL, so we need hardware store ordering here. 238 release_tag_at_put(class_index, JVM_CONSTANT_Class); 239 } 240 241 // CDS support. Create a new resolved_references array. 242 void ConstantPool::restore_unshareable_info(TRAPS) { 243 assert(is_constantPool(), "ensure C++ vtable is restored"); 244 assert(on_stack(), "should always be set for shared constant pools"); 245 assert(is_shared(), "should always be set for shared constant pools"); 246 247 // Only create the new resolved references array if it hasn't been attempted before 248 if (resolved_references() != NULL) return; 249 250 // restore the C++ vtable from the shared archive 251 restore_vtable(); 252 253 if (SystemDictionary::Object_klass_loaded()) { 254 // Recreate the object array and add to ClassLoaderData. 255 int map_length = resolved_reference_length(); 256 if (map_length > 0) { 257 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); 258 Handle refs_handle (THREAD, (oop)stom); // must handleize. 259 260 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 261 set_resolved_references(loader_data->add_handle(refs_handle)); 262 } 263 } 264 } 265 266 void ConstantPool::remove_unshareable_info() { 267 // Resolved references are not in the shared archive. 268 // Save the length for restoration. It is not necessarily the same length 269 // as reference_map.length() if invokedynamic is saved. 270 set_resolved_reference_length( 271 resolved_references() != NULL ? resolved_references()->length() : 0); 272 set_resolved_references(NULL); 273 274 // Shared ConstantPools are in the RO region, so the _flags cannot be modified. 275 // The _on_stack flag is used to prevent ConstantPools from deallocation during 276 // class redefinition. Since shared ConstantPools cannot be deallocated anyway, 277 // we always set _on_stack to true to avoid having to change _flags during runtime. 278 _flags |= (_on_stack | _is_shared); 279 } 280 281 int ConstantPool::cp_to_object_index(int cp_index) { 282 // this is harder don't do this so much. 283 int i = reference_map()->find(cp_index); 284 // We might not find the index for jsr292 call. 285 return (i < 0) ? _no_index_sentinel : i; 286 } 287 288 void ConstantPool::string_at_put(int which, int obj_index, oop str) { 289 resolved_references()->obj_at_put(obj_index, str); 290 } 291 292 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) { 293 ResourceMark rm; 294 int line_number = -1; 295 const char * source_file = NULL; 296 if (JavaThread::current()->has_last_Java_frame()) { 297 // try to identify the method which called this function. 298 vframeStream vfst(JavaThread::current()); 299 if (!vfst.at_end()) { 300 line_number = vfst.method()->line_number_from_bci(vfst.bci()); 301 Symbol* s = vfst.method()->method_holder()->source_file_name(); 302 if (s != NULL) { 303 source_file = s->as_C_string(); 304 } 305 } 306 } 307 if (k != this_cp->pool_holder()) { 308 // only print something if the classes are different 309 if (source_file != NULL) { 310 log_debug(class, resolve)("%s %s %s:%d", 311 this_cp->pool_holder()->external_name(), 312 k->external_name(), source_file, line_number); 313 } else { 314 log_debug(class, resolve)("%s %s", 315 this_cp->pool_holder()->external_name(), 316 k->external_name()); 317 } 318 } 319 } 320 321 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which, 322 bool save_resolution_error, TRAPS) { 323 assert(THREAD->is_Java_thread(), "must be a Java thread"); 324 325 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*. 326 // It is not safe to rely on the tag bit's here, since we don't have a lock, and 327 // the entry and tag is not updated atomicly. 328 CPKlassSlot kslot = this_cp->klass_slot_at(which); 329 int resolved_klass_index = kslot.resolved_klass_index(); 330 int name_index = kslot.name_index(); 331 assert(this_cp->tag_at(name_index).is_symbol(), "sanity"); 332 333 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index); 334 if (klass != NULL) { 335 return klass; 336 } 337 338 // This tag doesn't change back to unresolved class unless at a safepoint. 339 if (this_cp->tag_at(which).is_unresolved_klass_in_error()) { 340 // The original attempt to resolve this constant pool entry failed so find the 341 // class of the original error and throw another error of the same class 342 // (JVMS 5.4.3). 343 // If there is a detail message, pass that detail message to the error. 344 // The JVMS does not strictly require us to duplicate the same detail message, 345 // or any internal exception fields such as cause or stacktrace. But since the 346 // detail message is often a class name or other literal string, we will repeat it 347 // if we can find it in the symbol table. 348 throw_resolution_error(this_cp, which, CHECK_0); 349 ShouldNotReachHere(); 350 } 351 352 Handle mirror_handle; 353 Symbol* name = this_cp->symbol_at(name_index); 354 Handle loader (THREAD, this_cp->pool_holder()->class_loader()); 355 Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain()); 356 Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD); 357 if (!HAS_PENDING_EXCEPTION) { 358 // preserve the resolved klass from unloading 359 mirror_handle = Handle(THREAD, k->java_mirror()); 360 // Do access check for klasses 361 verify_constant_pool_resolve(this_cp, k, THREAD); 362 } 363 364 // Failed to resolve class. We must record the errors so that subsequent attempts 365 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3). 366 if (HAS_PENDING_EXCEPTION) { 367 if (save_resolution_error) { 368 save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL); 369 // If CHECK_NULL above doesn't return the exception, that means that 370 // some other thread has beaten us and has resolved the class. 371 // To preserve old behavior, we return the resolved class. 372 klass = this_cp->resolved_klasses()->at(resolved_klass_index); 373 assert(klass != NULL, "must be resolved if exception was cleared"); 374 return klass; 375 } else { 376 return NULL; // return the pending exception 377 } 378 } 379 380 // Make this class loader depend upon the class loader owning the class reference 381 ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data(); 382 this_key->record_dependency(k, CHECK_NULL); // Can throw OOM 383 384 // logging for class+resolve. 385 if (log_is_enabled(Debug, class, resolve)){ 386 trace_class_resolution(this_cp, k); 387 } 388 Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index); 389 OrderAccess::release_store_ptr((Klass* volatile *)adr, k); 390 // The interpreter assumes when the tag is stored, the klass is resolved 391 // and the Klass* stored in _resolved_klasses is non-NULL, so we need 392 // hardware store ordering here. 393 this_cp->release_tag_at_put(which, JVM_CONSTANT_Class); 394 return k; 395 } 396 397 398 // Does not update ConstantPool* - to avoid any exception throwing. Used 399 // by compiler and exception handling. Also used to avoid classloads for 400 // instanceof operations. Returns NULL if the class has not been loaded or 401 // if the verification of constant pool failed 402 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) { 403 CPKlassSlot kslot = this_cp->klass_slot_at(which); 404 int resolved_klass_index = kslot.resolved_klass_index(); 405 int name_index = kslot.name_index(); 406 assert(this_cp->tag_at(name_index).is_symbol(), "sanity"); 407 408 Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index); 409 if (k != NULL) { 410 return k; 411 } else { 412 Thread *thread = Thread::current(); 413 Symbol* name = this_cp->symbol_at(name_index); 414 oop loader = this_cp->pool_holder()->class_loader(); 415 oop protection_domain = this_cp->pool_holder()->protection_domain(); 416 Handle h_prot (thread, protection_domain); 417 Handle h_loader (thread, loader); 418 Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread); 419 420 if (k != NULL) { 421 // Make sure that resolving is legal 422 EXCEPTION_MARK; 423 // return NULL if verification fails 424 verify_constant_pool_resolve(this_cp, k, THREAD); 425 if (HAS_PENDING_EXCEPTION) { 426 CLEAR_PENDING_EXCEPTION; 427 return NULL; 428 } 429 return k; 430 } else { 431 return k; 432 } 433 } 434 } 435 436 437 Klass* ConstantPool::klass_ref_at_if_loaded(const constantPoolHandle& this_cp, int which) { 438 return klass_at_if_loaded(this_cp, this_cp->klass_ref_index_at(which)); 439 } 440 441 442 Method* ConstantPool::method_at_if_loaded(const constantPoolHandle& cpool, 443 int which) { 444 if (cpool->cache() == NULL) return NULL; // nothing to load yet 445 int cache_index = decode_cpcache_index(which, true); 446 if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) { 447 // FIXME: should be an assert 448 log_debug(class, resolve)("bad operand %d in:", which); cpool->print(); 449 return NULL; 450 } 451 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 452 return e->method_if_resolved(cpool); 453 } 454 455 456 bool ConstantPool::has_appendix_at_if_loaded(const constantPoolHandle& cpool, int which) { 457 if (cpool->cache() == NULL) return false; // nothing to load yet 458 int cache_index = decode_cpcache_index(which, true); 459 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 460 return e->has_appendix(); 461 } 462 463 oop ConstantPool::appendix_at_if_loaded(const constantPoolHandle& cpool, int which) { 464 if (cpool->cache() == NULL) return NULL; // nothing to load yet 465 int cache_index = decode_cpcache_index(which, true); 466 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 467 return e->appendix_if_resolved(cpool); 468 } 469 470 471 bool ConstantPool::has_method_type_at_if_loaded(const constantPoolHandle& cpool, int which) { 472 if (cpool->cache() == NULL) return false; // nothing to load yet 473 int cache_index = decode_cpcache_index(which, true); 474 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 475 return e->has_method_type(); 476 } 477 478 oop ConstantPool::method_type_at_if_loaded(const constantPoolHandle& cpool, int which) { 479 if (cpool->cache() == NULL) return NULL; // nothing to load yet 480 int cache_index = decode_cpcache_index(which, true); 481 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 482 return e->method_type_if_resolved(cpool); 483 } 484 485 486 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) { 487 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); 488 return symbol_at(name_index); 489 } 490 491 492 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) { 493 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); 494 return symbol_at(signature_index); 495 } 496 497 498 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) { 499 int i = which; 500 if (!uncached && cache() != NULL) { 501 if (ConstantPool::is_invokedynamic_index(which)) { 502 // Invokedynamic index is index into the constant pool cache 503 int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index(); 504 pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index); 505 assert(tag_at(pool_index).is_name_and_type(), ""); 506 return pool_index; 507 } 508 // change byte-ordering and go via cache 509 i = remap_instruction_operand_from_cache(which); 510 } else { 511 if (tag_at(which).is_invoke_dynamic()) { 512 int pool_index = invoke_dynamic_name_and_type_ref_index_at(which); 513 assert(tag_at(pool_index).is_name_and_type(), ""); 514 return pool_index; 515 } 516 } 517 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); 518 assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above"); 519 jint ref_index = *int_at_addr(i); 520 return extract_high_short_from_int(ref_index); 521 } 522 523 constantTag ConstantPool::impl_tag_ref_at(int which, bool uncached) { 524 int pool_index = which; 525 if (!uncached && cache() != NULL) { 526 if (ConstantPool::is_invokedynamic_index(which)) { 527 // Invokedynamic index is index into resolved_references 528 pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index(); 529 } else { 530 // change byte-ordering and go via cache 531 pool_index = remap_instruction_operand_from_cache(which); 532 } 533 } 534 return tag_at(pool_index); 535 } 536 537 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) { 538 guarantee(!ConstantPool::is_invokedynamic_index(which), 539 "an invokedynamic instruction does not have a klass"); 540 int i = which; 541 if (!uncached && cache() != NULL) { 542 // change byte-ordering and go via cache 543 i = remap_instruction_operand_from_cache(which); 544 } 545 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); 546 jint ref_index = *int_at_addr(i); 547 return extract_low_short_from_int(ref_index); 548 } 549 550 551 552 int ConstantPool::remap_instruction_operand_from_cache(int operand) { 553 int cpc_index = operand; 554 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG); 555 assert((int)(u2)cpc_index == cpc_index, "clean u2"); 556 int member_index = cache()->entry_at(cpc_index)->constant_pool_index(); 557 return member_index; 558 } 559 560 561 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) { 562 if (k->is_instance_klass() || k->is_objArray_klass()) { 563 InstanceKlass* holder = this_cp->pool_holder(); 564 Klass* elem = k->is_instance_klass() ? k : ObjArrayKlass::cast(k)->bottom_klass(); 565 566 // The element type could be a typeArray - we only need the access check if it is 567 // an reference to another class 568 if (elem->is_instance_klass()) { 569 LinkResolver::check_klass_accessability(holder, elem, CHECK); 570 } 571 } 572 } 573 574 575 int ConstantPool::name_ref_index_at(int which_nt) { 576 jint ref_index = name_and_type_at(which_nt); 577 return extract_low_short_from_int(ref_index); 578 } 579 580 581 int ConstantPool::signature_ref_index_at(int which_nt) { 582 jint ref_index = name_and_type_at(which_nt); 583 return extract_high_short_from_int(ref_index); 584 } 585 586 587 Klass* ConstantPool::klass_ref_at(int which, TRAPS) { 588 return klass_at(klass_ref_index_at(which), THREAD); 589 } 590 591 Symbol* ConstantPool::klass_name_at(int which) const { 592 return symbol_at(klass_slot_at(which).name_index()); 593 } 594 595 Symbol* ConstantPool::klass_ref_at_noresolve(int which) { 596 jint ref_index = klass_ref_index_at(which); 597 return klass_at_noresolve(ref_index); 598 } 599 600 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) { 601 jint ref_index = uncached_klass_ref_index_at(which); 602 return klass_at_noresolve(ref_index); 603 } 604 605 char* ConstantPool::string_at_noresolve(int which) { 606 return unresolved_string_at(which)->as_C_string(); 607 } 608 609 BasicType ConstantPool::basic_type_for_signature_at(int which) const { 610 return FieldType::basic_type(symbol_at(which)); 611 } 612 613 614 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) { 615 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused 616 if (this_cp->tag_at(index).is_string()) { 617 this_cp->string_at(index, CHECK); 618 } 619 } 620 } 621 622 // Resolve all the classes in the constant pool. If they are all resolved, 623 // the constant pool is read-only. Enhancement: allocate cp entries to 624 // another metaspace, and copy to read-only or read-write space if this 625 // bit is set. 626 bool ConstantPool::resolve_class_constants(TRAPS) { 627 constantPoolHandle cp(THREAD, this); 628 for (int index = 1; index < length(); index++) { // Index 0 is unused 629 if (tag_at(index).is_unresolved_klass() && 630 klass_at_if_loaded(cp, index) == NULL) { 631 return false; 632 } 633 } 634 // set_preresolution(); or some bit for future use 635 return true; 636 } 637 638 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) { 639 // Dig out the detailed message to reuse if possible 640 Symbol* message = java_lang_Throwable::detail_message(pending_exception); 641 if (message != NULL) { 642 return message; 643 } 644 645 // Return specific message for the tag 646 switch (tag.value()) { 647 case JVM_CONSTANT_UnresolvedClass: 648 // return the class name in the error message 649 message = this_cp->klass_name_at(which); 650 break; 651 case JVM_CONSTANT_MethodHandle: 652 // return the method handle name in the error message 653 message = this_cp->method_handle_name_ref_at(which); 654 break; 655 case JVM_CONSTANT_MethodType: 656 // return the method type signature in the error message 657 message = this_cp->method_type_signature_at(which); 658 break; 659 default: 660 ShouldNotReachHere(); 661 } 662 663 return message; 664 } 665 666 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) { 667 Symbol* message = NULL; 668 Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message); 669 assert(error != NULL && message != NULL, "checking"); 670 CLEAR_PENDING_EXCEPTION; 671 ResourceMark rm; 672 THROW_MSG(error, message->as_C_string()); 673 } 674 675 // If resolution for Class, MethodHandle or MethodType fails, save the exception 676 // in the resolution error table, so that the same exception is thrown again. 677 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int which, 678 constantTag tag, TRAPS) { 679 Symbol* error = PENDING_EXCEPTION->klass()->name(); 680 681 int error_tag = tag.error_value(); 682 683 if (!PENDING_EXCEPTION-> 684 is_a(SystemDictionary::LinkageError_klass())) { 685 // Just throw the exception and don't prevent these classes from 686 // being loaded due to virtual machine errors like StackOverflow 687 // and OutOfMemoryError, etc, or if the thread was hit by stop() 688 // Needs clarification to section 5.4.3 of the VM spec (see 6308271) 689 } else if (this_cp->tag_at(which).value() != error_tag) { 690 Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION); 691 SystemDictionary::add_resolution_error(this_cp, which, error, message); 692 // CAS in the tag. If a thread beat us to registering this error that's fine. 693 // If another thread resolved the reference, this is a race condition. This 694 // thread may have had a security manager or something temporary. 695 // This doesn't deterministically get an error. So why do we save this? 696 // We save this because jvmti can add classes to the bootclass path after 697 // this error, so it needs to get the same error if the error is first. 698 jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag, 699 (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value()); 700 if (old_tag != error_tag && old_tag != tag.value()) { 701 // MethodHandles and MethodType doesn't change to resolved version. 702 assert(this_cp->tag_at(which).is_klass(), "Wrong tag value"); 703 // Forget the exception and use the resolved class. 704 CLEAR_PENDING_EXCEPTION; 705 } 706 } else { 707 // some other thread put this in error state 708 throw_resolution_error(this_cp, which, CHECK); 709 } 710 } 711 712 // Called to resolve constants in the constant pool and return an oop. 713 // Some constant pool entries cache their resolved oop. This is also 714 // called to create oops from constants to use in arguments for invokedynamic 715 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index, TRAPS) { 716 oop result_oop = NULL; 717 Handle throw_exception; 718 719 if (cache_index == _possible_index_sentinel) { 720 // It is possible that this constant is one which is cached in the objects. 721 // We'll do a linear search. This should be OK because this usage is rare. 722 assert(index > 0, "valid index"); 723 cache_index = this_cp->cp_to_object_index(index); 724 } 725 assert(cache_index == _no_index_sentinel || cache_index >= 0, ""); 726 assert(index == _no_index_sentinel || index >= 0, ""); 727 728 if (cache_index >= 0) { 729 result_oop = this_cp->resolved_references()->obj_at(cache_index); 730 if (result_oop != NULL) { 731 return result_oop; 732 // That was easy... 733 } 734 index = this_cp->object_to_cp_index(cache_index); 735 } 736 737 jvalue prim_value; // temp used only in a few cases below 738 739 constantTag tag = this_cp->tag_at(index); 740 741 switch (tag.value()) { 742 743 case JVM_CONSTANT_UnresolvedClass: 744 case JVM_CONSTANT_UnresolvedClassInError: 745 case JVM_CONSTANT_Class: 746 { 747 assert(cache_index == _no_index_sentinel, "should not have been set"); 748 Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL); 749 // ldc wants the java mirror. 750 result_oop = resolved->java_mirror(); 751 break; 752 } 753 754 case JVM_CONSTANT_String: 755 assert(cache_index != _no_index_sentinel, "should have been set"); 756 if (this_cp->is_pseudo_string_at(index)) { 757 result_oop = this_cp->pseudo_string_at(index, cache_index); 758 break; 759 } 760 result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL); 761 break; 762 763 case JVM_CONSTANT_MethodHandleInError: 764 case JVM_CONSTANT_MethodTypeInError: 765 { 766 throw_resolution_error(this_cp, index, CHECK_NULL); 767 break; 768 } 769 770 case JVM_CONSTANT_MethodHandle: 771 { 772 int ref_kind = this_cp->method_handle_ref_kind_at(index); 773 int callee_index = this_cp->method_handle_klass_index_at(index); 774 Symbol* name = this_cp->method_handle_name_ref_at(index); 775 Symbol* signature = this_cp->method_handle_signature_ref_at(index); 776 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(index)); 777 { ResourceMark rm(THREAD); 778 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s", 779 ref_kind, index, this_cp->method_handle_index_at(index), 780 callee_index, name->as_C_string(), signature->as_C_string()); 781 } 782 783 Klass* callee = klass_at_impl(this_cp, callee_index, true, CHECK_NULL); 784 785 // Check constant pool method consistency 786 if ((callee->is_interface() && m_tag.is_method()) || 787 ((!callee->is_interface() && m_tag.is_interface_method()))) { 788 ResourceMark rm(THREAD); 789 char buf[400]; 790 jio_snprintf(buf, sizeof(buf), 791 "Inconsistent constant pool data in classfile for class %s. " 792 "Method %s%s at index %d is %s and should be %s", 793 callee->name()->as_C_string(), name->as_C_string(), signature->as_C_string(), index, 794 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef", 795 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef"); 796 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 797 } 798 799 Klass* klass = this_cp->pool_holder(); 800 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind, 801 callee, name, signature, 802 THREAD); 803 result_oop = value(); 804 if (HAS_PENDING_EXCEPTION) { 805 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 806 } 807 break; 808 } 809 810 case JVM_CONSTANT_MethodType: 811 { 812 Symbol* signature = this_cp->method_type_signature_at(index); 813 { ResourceMark rm(THREAD); 814 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s", 815 index, this_cp->method_type_index_at(index), 816 signature->as_C_string()); 817 } 818 Klass* klass = this_cp->pool_holder(); 819 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD); 820 result_oop = value(); 821 if (HAS_PENDING_EXCEPTION) { 822 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 823 } 824 break; 825 } 826 827 case JVM_CONSTANT_Integer: 828 assert(cache_index == _no_index_sentinel, "should not have been set"); 829 prim_value.i = this_cp->int_at(index); 830 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL); 831 break; 832 833 case JVM_CONSTANT_Float: 834 assert(cache_index == _no_index_sentinel, "should not have been set"); 835 prim_value.f = this_cp->float_at(index); 836 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL); 837 break; 838 839 case JVM_CONSTANT_Long: 840 assert(cache_index == _no_index_sentinel, "should not have been set"); 841 prim_value.j = this_cp->long_at(index); 842 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL); 843 break; 844 845 case JVM_CONSTANT_Double: 846 assert(cache_index == _no_index_sentinel, "should not have been set"); 847 prim_value.d = this_cp->double_at(index); 848 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL); 849 break; 850 851 default: 852 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d", 853 this_cp(), index, cache_index, tag.value())); 854 assert(false, "unexpected constant tag"); 855 break; 856 } 857 858 if (cache_index >= 0) { 859 // Benign race condition: resolved_references may already be filled in. 860 // The important thing here is that all threads pick up the same result. 861 // It doesn't matter which racing thread wins, as long as only one 862 // result is used by all threads, and all future queries. 863 oop old_result = this_cp->resolved_references()->atomic_compare_exchange_oop(cache_index, result_oop, NULL); 864 if (old_result == NULL) { 865 return result_oop; // was installed 866 } else { 867 // Return the winning thread's result. This can be different than 868 // the result here for MethodHandles. 869 return old_result; 870 } 871 } else { 872 return result_oop; 873 } 874 } 875 876 oop ConstantPool::uncached_string_at(int which, TRAPS) { 877 Symbol* sym = unresolved_string_at(which); 878 oop str = StringTable::intern(sym, CHECK_(NULL)); 879 assert(java_lang_String::is_instance(str), "must be string"); 880 return str; 881 } 882 883 884 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) { 885 assert(this_cp->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool"); 886 887 Handle bsm; 888 int argc; 889 { 890 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments 891 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry. 892 // It is accompanied by the optional arguments. 893 int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index); 894 oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL); 895 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) { 896 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle"); 897 } 898 899 // Extract the optional static arguments. 900 argc = this_cp->invoke_dynamic_argument_count_at(index); 901 if (argc == 0) return bsm_oop; 902 903 bsm = Handle(THREAD, bsm_oop); 904 } 905 906 objArrayHandle info; 907 { 908 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL); 909 info = objArrayHandle(THREAD, info_oop); 910 } 911 912 info->obj_at_put(0, bsm()); 913 for (int i = 0; i < argc; i++) { 914 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); 915 oop arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL); 916 info->obj_at_put(1+i, arg_oop); 917 } 918 919 return info(); 920 } 921 922 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) { 923 // If the string has already been interned, this entry will be non-null 924 oop str = this_cp->resolved_references()->obj_at(obj_index); 925 if (str != NULL) return str; 926 Symbol* sym = this_cp->unresolved_string_at(which); 927 str = StringTable::intern(sym, CHECK_(NULL)); 928 this_cp->string_at_put(which, obj_index, str); 929 assert(java_lang_String::is_instance(str), "must be string"); 930 return str; 931 } 932 933 934 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) { 935 // Names are interned, so we can compare Symbol*s directly 936 Symbol* cp_name = klass_name_at(which); 937 return (cp_name == k->name()); 938 } 939 940 941 // Iterate over symbols and decrement ones which are Symbol*s 942 // This is done during GC. 943 // Only decrement the UTF8 symbols. Strings point to 944 // these symbols but didn't increment the reference count. 945 void ConstantPool::unreference_symbols() { 946 for (int index = 1; index < length(); index++) { // Index 0 is unused 947 constantTag tag = tag_at(index); 948 if (tag.is_symbol()) { 949 symbol_at(index)->decrement_refcount(); 950 } 951 } 952 } 953 954 955 // Compare this constant pool's entry at index1 to the constant pool 956 // cp2's entry at index2. 957 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2, 958 int index2, TRAPS) { 959 960 // The error tags are equivalent to non-error tags when comparing 961 jbyte t1 = tag_at(index1).non_error_value(); 962 jbyte t2 = cp2->tag_at(index2).non_error_value(); 963 964 if (t1 != t2) { 965 // Not the same entry type so there is nothing else to check. Note 966 // that this style of checking will consider resolved/unresolved 967 // class pairs as different. 968 // From the ConstantPool* API point of view, this is correct 969 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this 970 // plays out in the context of ConstantPool* merging. 971 return false; 972 } 973 974 switch (t1) { 975 case JVM_CONSTANT_Class: 976 { 977 Klass* k1 = klass_at(index1, CHECK_false); 978 Klass* k2 = cp2->klass_at(index2, CHECK_false); 979 if (k1 == k2) { 980 return true; 981 } 982 } break; 983 984 case JVM_CONSTANT_ClassIndex: 985 { 986 int recur1 = klass_index_at(index1); 987 int recur2 = cp2->klass_index_at(index2); 988 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 989 if (match) { 990 return true; 991 } 992 } break; 993 994 case JVM_CONSTANT_Double: 995 { 996 jdouble d1 = double_at(index1); 997 jdouble d2 = cp2->double_at(index2); 998 if (d1 == d2) { 999 return true; 1000 } 1001 } break; 1002 1003 case JVM_CONSTANT_Fieldref: 1004 case JVM_CONSTANT_InterfaceMethodref: 1005 case JVM_CONSTANT_Methodref: 1006 { 1007 int recur1 = uncached_klass_ref_index_at(index1); 1008 int recur2 = cp2->uncached_klass_ref_index_at(index2); 1009 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1010 if (match) { 1011 recur1 = uncached_name_and_type_ref_index_at(index1); 1012 recur2 = cp2->uncached_name_and_type_ref_index_at(index2); 1013 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1014 if (match) { 1015 return true; 1016 } 1017 } 1018 } break; 1019 1020 case JVM_CONSTANT_Float: 1021 { 1022 jfloat f1 = float_at(index1); 1023 jfloat f2 = cp2->float_at(index2); 1024 if (f1 == f2) { 1025 return true; 1026 } 1027 } break; 1028 1029 case JVM_CONSTANT_Integer: 1030 { 1031 jint i1 = int_at(index1); 1032 jint i2 = cp2->int_at(index2); 1033 if (i1 == i2) { 1034 return true; 1035 } 1036 } break; 1037 1038 case JVM_CONSTANT_Long: 1039 { 1040 jlong l1 = long_at(index1); 1041 jlong l2 = cp2->long_at(index2); 1042 if (l1 == l2) { 1043 return true; 1044 } 1045 } break; 1046 1047 case JVM_CONSTANT_NameAndType: 1048 { 1049 int recur1 = name_ref_index_at(index1); 1050 int recur2 = cp2->name_ref_index_at(index2); 1051 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1052 if (match) { 1053 recur1 = signature_ref_index_at(index1); 1054 recur2 = cp2->signature_ref_index_at(index2); 1055 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1056 if (match) { 1057 return true; 1058 } 1059 } 1060 } break; 1061 1062 case JVM_CONSTANT_StringIndex: 1063 { 1064 int recur1 = string_index_at(index1); 1065 int recur2 = cp2->string_index_at(index2); 1066 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1067 if (match) { 1068 return true; 1069 } 1070 } break; 1071 1072 case JVM_CONSTANT_UnresolvedClass: 1073 { 1074 Symbol* k1 = klass_name_at(index1); 1075 Symbol* k2 = cp2->klass_name_at(index2); 1076 if (k1 == k2) { 1077 return true; 1078 } 1079 } break; 1080 1081 case JVM_CONSTANT_MethodType: 1082 { 1083 int k1 = method_type_index_at(index1); 1084 int k2 = cp2->method_type_index_at(index2); 1085 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 1086 if (match) { 1087 return true; 1088 } 1089 } break; 1090 1091 case JVM_CONSTANT_MethodHandle: 1092 { 1093 int k1 = method_handle_ref_kind_at(index1); 1094 int k2 = cp2->method_handle_ref_kind_at(index2); 1095 if (k1 == k2) { 1096 int i1 = method_handle_index_at(index1); 1097 int i2 = cp2->method_handle_index_at(index2); 1098 bool match = compare_entry_to(i1, cp2, i2, CHECK_false); 1099 if (match) { 1100 return true; 1101 } 1102 } 1103 } break; 1104 1105 case JVM_CONSTANT_InvokeDynamic: 1106 { 1107 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1); 1108 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2); 1109 int i1 = invoke_dynamic_bootstrap_specifier_index(index1); 1110 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2); 1111 // separate statements and variables because CHECK_false is used 1112 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false); 1113 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false); 1114 return (match_entry && match_operand); 1115 } break; 1116 1117 case JVM_CONSTANT_String: 1118 { 1119 Symbol* s1 = unresolved_string_at(index1); 1120 Symbol* s2 = cp2->unresolved_string_at(index2); 1121 if (s1 == s2) { 1122 return true; 1123 } 1124 } break; 1125 1126 case JVM_CONSTANT_Utf8: 1127 { 1128 Symbol* s1 = symbol_at(index1); 1129 Symbol* s2 = cp2->symbol_at(index2); 1130 if (s1 == s2) { 1131 return true; 1132 } 1133 } break; 1134 1135 // Invalid is used as the tag for the second constant pool entry 1136 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1137 // not be seen by itself. 1138 case JVM_CONSTANT_Invalid: // fall through 1139 1140 default: 1141 ShouldNotReachHere(); 1142 break; 1143 } 1144 1145 return false; 1146 } // end compare_entry_to() 1147 1148 1149 // Resize the operands array with delta_len and delta_size. 1150 // Used in RedefineClasses for CP merge. 1151 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) { 1152 int old_len = operand_array_length(operands()); 1153 int new_len = old_len + delta_len; 1154 int min_len = (delta_len > 0) ? old_len : new_len; 1155 1156 int old_size = operands()->length(); 1157 int new_size = old_size + delta_size; 1158 int min_size = (delta_size > 0) ? old_size : new_size; 1159 1160 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1161 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK); 1162 1163 // Set index in the resized array for existing elements only 1164 for (int idx = 0; idx < min_len; idx++) { 1165 int offset = operand_offset_at(idx); // offset in original array 1166 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array 1167 } 1168 // Copy the bootstrap specifiers only 1169 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len), 1170 new_ops->adr_at(2*new_len), 1171 (min_size - 2*min_len) * sizeof(u2)); 1172 // Explicitly deallocate old operands array. 1173 // Note, it is not needed for 7u backport. 1174 if ( operands() != NULL) { // the safety check 1175 MetadataFactory::free_array<u2>(loader_data, operands()); 1176 } 1177 set_operands(new_ops); 1178 } // end resize_operands() 1179 1180 1181 // Extend the operands array with the length and size of the ext_cp operands. 1182 // Used in RedefineClasses for CP merge. 1183 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) { 1184 int delta_len = operand_array_length(ext_cp->operands()); 1185 if (delta_len == 0) { 1186 return; // nothing to do 1187 } 1188 int delta_size = ext_cp->operands()->length(); 1189 1190 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger"); 1191 1192 if (operand_array_length(operands()) == 0) { 1193 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1194 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK); 1195 // The first element index defines the offset of second part 1196 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array 1197 set_operands(new_ops); 1198 } else { 1199 resize_operands(delta_len, delta_size, CHECK); 1200 } 1201 1202 } // end extend_operands() 1203 1204 1205 // Shrink the operands array to a smaller array with new_len length. 1206 // Used in RedefineClasses for CP merge. 1207 void ConstantPool::shrink_operands(int new_len, TRAPS) { 1208 int old_len = operand_array_length(operands()); 1209 if (new_len == old_len) { 1210 return; // nothing to do 1211 } 1212 assert(new_len < old_len, "shrunken operands array must be smaller"); 1213 1214 int free_base = operand_next_offset_at(new_len - 1); 1215 int delta_len = new_len - old_len; 1216 int delta_size = 2*delta_len + free_base - operands()->length(); 1217 1218 resize_operands(delta_len, delta_size, CHECK); 1219 1220 } // end shrink_operands() 1221 1222 1223 void ConstantPool::copy_operands(const constantPoolHandle& from_cp, 1224 const constantPoolHandle& to_cp, 1225 TRAPS) { 1226 1227 int from_oplen = operand_array_length(from_cp->operands()); 1228 int old_oplen = operand_array_length(to_cp->operands()); 1229 if (from_oplen != 0) { 1230 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data(); 1231 // append my operands to the target's operands array 1232 if (old_oplen == 0) { 1233 // Can't just reuse from_cp's operand list because of deallocation issues 1234 int len = from_cp->operands()->length(); 1235 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK); 1236 Copy::conjoint_memory_atomic( 1237 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2)); 1238 to_cp->set_operands(new_ops); 1239 } else { 1240 int old_len = to_cp->operands()->length(); 1241 int from_len = from_cp->operands()->length(); 1242 int old_off = old_oplen * sizeof(u2); 1243 int from_off = from_oplen * sizeof(u2); 1244 // Use the metaspace for the destination constant pool 1245 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK); 1246 int fillp = 0, len = 0; 1247 // first part of dest 1248 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0), 1249 new_operands->adr_at(fillp), 1250 (len = old_off) * sizeof(u2)); 1251 fillp += len; 1252 // first part of src 1253 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0), 1254 new_operands->adr_at(fillp), 1255 (len = from_off) * sizeof(u2)); 1256 fillp += len; 1257 // second part of dest 1258 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off), 1259 new_operands->adr_at(fillp), 1260 (len = old_len - old_off) * sizeof(u2)); 1261 fillp += len; 1262 // second part of src 1263 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off), 1264 new_operands->adr_at(fillp), 1265 (len = from_len - from_off) * sizeof(u2)); 1266 fillp += len; 1267 assert(fillp == new_operands->length(), ""); 1268 1269 // Adjust indexes in the first part of the copied operands array. 1270 for (int j = 0; j < from_oplen; j++) { 1271 int offset = operand_offset_at(new_operands, old_oplen + j); 1272 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy"); 1273 offset += old_len; // every new tuple is preceded by old_len extra u2's 1274 operand_offset_at_put(new_operands, old_oplen + j, offset); 1275 } 1276 1277 // replace target operands array with combined array 1278 to_cp->set_operands(new_operands); 1279 } 1280 } 1281 } // end copy_operands() 1282 1283 1284 // Copy this constant pool's entries at start_i to end_i (inclusive) 1285 // to the constant pool to_cp's entries starting at to_i. A total of 1286 // (end_i - start_i) + 1 entries are copied. 1287 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, 1288 const constantPoolHandle& to_cp, int to_i, TRAPS) { 1289 1290 1291 int dest_i = to_i; // leave original alone for debug purposes 1292 1293 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) { 1294 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK); 1295 1296 switch (from_cp->tag_at(src_i).value()) { 1297 case JVM_CONSTANT_Double: 1298 case JVM_CONSTANT_Long: 1299 // double and long take two constant pool entries 1300 src_i += 2; 1301 dest_i += 2; 1302 break; 1303 1304 default: 1305 // all others take one constant pool entry 1306 src_i++; 1307 dest_i++; 1308 break; 1309 } 1310 } 1311 copy_operands(from_cp, to_cp, CHECK); 1312 1313 } // end copy_cp_to_impl() 1314 1315 1316 // Copy this constant pool's entry at from_i to the constant pool 1317 // to_cp's entry at to_i. 1318 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i, 1319 const constantPoolHandle& to_cp, int to_i, 1320 TRAPS) { 1321 1322 int tag = from_cp->tag_at(from_i).value(); 1323 switch (tag) { 1324 case JVM_CONSTANT_ClassIndex: 1325 { 1326 jint ki = from_cp->klass_index_at(from_i); 1327 to_cp->klass_index_at_put(to_i, ki); 1328 } break; 1329 1330 case JVM_CONSTANT_Double: 1331 { 1332 jdouble d = from_cp->double_at(from_i); 1333 to_cp->double_at_put(to_i, d); 1334 // double takes two constant pool entries so init second entry's tag 1335 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1336 } break; 1337 1338 case JVM_CONSTANT_Fieldref: 1339 { 1340 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1341 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1342 to_cp->field_at_put(to_i, class_index, name_and_type_index); 1343 } break; 1344 1345 case JVM_CONSTANT_Float: 1346 { 1347 jfloat f = from_cp->float_at(from_i); 1348 to_cp->float_at_put(to_i, f); 1349 } break; 1350 1351 case JVM_CONSTANT_Integer: 1352 { 1353 jint i = from_cp->int_at(from_i); 1354 to_cp->int_at_put(to_i, i); 1355 } break; 1356 1357 case JVM_CONSTANT_InterfaceMethodref: 1358 { 1359 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1360 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1361 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index); 1362 } break; 1363 1364 case JVM_CONSTANT_Long: 1365 { 1366 jlong l = from_cp->long_at(from_i); 1367 to_cp->long_at_put(to_i, l); 1368 // long takes two constant pool entries so init second entry's tag 1369 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1370 } break; 1371 1372 case JVM_CONSTANT_Methodref: 1373 { 1374 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1375 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1376 to_cp->method_at_put(to_i, class_index, name_and_type_index); 1377 } break; 1378 1379 case JVM_CONSTANT_NameAndType: 1380 { 1381 int name_ref_index = from_cp->name_ref_index_at(from_i); 1382 int signature_ref_index = from_cp->signature_ref_index_at(from_i); 1383 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index); 1384 } break; 1385 1386 case JVM_CONSTANT_StringIndex: 1387 { 1388 jint si = from_cp->string_index_at(from_i); 1389 to_cp->string_index_at_put(to_i, si); 1390 } break; 1391 1392 case JVM_CONSTANT_Class: 1393 case JVM_CONSTANT_UnresolvedClass: 1394 case JVM_CONSTANT_UnresolvedClassInError: 1395 { 1396 // Revert to JVM_CONSTANT_ClassIndex 1397 int name_index = from_cp->klass_slot_at(from_i).name_index(); 1398 assert(from_cp->tag_at(name_index).is_symbol(), "sanity"); 1399 to_cp->klass_index_at_put(to_i, name_index); 1400 } break; 1401 1402 case JVM_CONSTANT_String: 1403 { 1404 Symbol* s = from_cp->unresolved_string_at(from_i); 1405 to_cp->unresolved_string_at_put(to_i, s); 1406 } break; 1407 1408 case JVM_CONSTANT_Utf8: 1409 { 1410 Symbol* s = from_cp->symbol_at(from_i); 1411 // Need to increase refcount, the old one will be thrown away and deferenced 1412 s->increment_refcount(); 1413 to_cp->symbol_at_put(to_i, s); 1414 } break; 1415 1416 case JVM_CONSTANT_MethodType: 1417 case JVM_CONSTANT_MethodTypeInError: 1418 { 1419 jint k = from_cp->method_type_index_at(from_i); 1420 to_cp->method_type_index_at_put(to_i, k); 1421 } break; 1422 1423 case JVM_CONSTANT_MethodHandle: 1424 case JVM_CONSTANT_MethodHandleInError: 1425 { 1426 int k1 = from_cp->method_handle_ref_kind_at(from_i); 1427 int k2 = from_cp->method_handle_index_at(from_i); 1428 to_cp->method_handle_index_at_put(to_i, k1, k2); 1429 } break; 1430 1431 case JVM_CONSTANT_InvokeDynamic: 1432 { 1433 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i); 1434 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i); 1435 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands 1436 to_cp->invoke_dynamic_at_put(to_i, k1, k2); 1437 } break; 1438 1439 // Invalid is used as the tag for the second constant pool entry 1440 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1441 // not be seen by itself. 1442 case JVM_CONSTANT_Invalid: // fall through 1443 1444 default: 1445 { 1446 ShouldNotReachHere(); 1447 } break; 1448 } 1449 } // end copy_entry_to() 1450 1451 // Search constant pool search_cp for an entry that matches this 1452 // constant pool's entry at pattern_i. Returns the index of a 1453 // matching entry or zero (0) if there is no matching entry. 1454 int ConstantPool::find_matching_entry(int pattern_i, 1455 const constantPoolHandle& search_cp, TRAPS) { 1456 1457 // index zero (0) is not used 1458 for (int i = 1; i < search_cp->length(); i++) { 1459 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0); 1460 if (found) { 1461 return i; 1462 } 1463 } 1464 1465 return 0; // entry not found; return unused index zero (0) 1466 } // end find_matching_entry() 1467 1468 1469 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool 1470 // cp2's bootstrap specifier at idx2. 1471 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2, TRAPS) { 1472 int k1 = operand_bootstrap_method_ref_index_at(idx1); 1473 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2); 1474 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 1475 1476 if (!match) { 1477 return false; 1478 } 1479 int argc = operand_argument_count_at(idx1); 1480 if (argc == cp2->operand_argument_count_at(idx2)) { 1481 for (int j = 0; j < argc; j++) { 1482 k1 = operand_argument_index_at(idx1, j); 1483 k2 = cp2->operand_argument_index_at(idx2, j); 1484 match = compare_entry_to(k1, cp2, k2, CHECK_false); 1485 if (!match) { 1486 return false; 1487 } 1488 } 1489 return true; // got through loop; all elements equal 1490 } 1491 return false; 1492 } // end compare_operand_to() 1493 1494 // Search constant pool search_cp for a bootstrap specifier that matches 1495 // this constant pool's bootstrap specifier at pattern_i index. 1496 // Return the index of a matching bootstrap specifier or (-1) if there is no match. 1497 int ConstantPool::find_matching_operand(int pattern_i, 1498 const constantPoolHandle& search_cp, int search_len, TRAPS) { 1499 for (int i = 0; i < search_len; i++) { 1500 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1)); 1501 if (found) { 1502 return i; 1503 } 1504 } 1505 return -1; // bootstrap specifier not found; return unused index (-1) 1506 } // end find_matching_operand() 1507 1508 1509 #ifndef PRODUCT 1510 1511 const char* ConstantPool::printable_name_at(int which) { 1512 1513 constantTag tag = tag_at(which); 1514 1515 if (tag.is_string()) { 1516 return string_at_noresolve(which); 1517 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 1518 return klass_name_at(which)->as_C_string(); 1519 } else if (tag.is_symbol()) { 1520 return symbol_at(which)->as_C_string(); 1521 } 1522 return ""; 1523 } 1524 1525 #endif // PRODUCT 1526 1527 1528 // JVMTI GetConstantPool support 1529 1530 // For debugging of constant pool 1531 const bool debug_cpool = false; 1532 1533 #define DBG(code) do { if (debug_cpool) { (code); } } while(0) 1534 1535 static void print_cpool_bytes(jint cnt, u1 *bytes) { 1536 const char* WARN_MSG = "Must not be such entry!"; 1537 jint size = 0; 1538 u2 idx1, idx2; 1539 1540 for (jint idx = 1; idx < cnt; idx++) { 1541 jint ent_size = 0; 1542 u1 tag = *bytes++; 1543 size++; // count tag 1544 1545 printf("const #%03d, tag: %02d ", idx, tag); 1546 switch(tag) { 1547 case JVM_CONSTANT_Invalid: { 1548 printf("Invalid"); 1549 break; 1550 } 1551 case JVM_CONSTANT_Unicode: { 1552 printf("Unicode %s", WARN_MSG); 1553 break; 1554 } 1555 case JVM_CONSTANT_Utf8: { 1556 u2 len = Bytes::get_Java_u2(bytes); 1557 char str[128]; 1558 if (len > 127) { 1559 len = 127; 1560 } 1561 strncpy(str, (char *) (bytes+2), len); 1562 str[len] = '\0'; 1563 printf("Utf8 \"%s\"", str); 1564 ent_size = 2 + len; 1565 break; 1566 } 1567 case JVM_CONSTANT_Integer: { 1568 u4 val = Bytes::get_Java_u4(bytes); 1569 printf("int %d", *(int *) &val); 1570 ent_size = 4; 1571 break; 1572 } 1573 case JVM_CONSTANT_Float: { 1574 u4 val = Bytes::get_Java_u4(bytes); 1575 printf("float %5.3ff", *(float *) &val); 1576 ent_size = 4; 1577 break; 1578 } 1579 case JVM_CONSTANT_Long: { 1580 u8 val = Bytes::get_Java_u8(bytes); 1581 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val); 1582 ent_size = 8; 1583 idx++; // Long takes two cpool slots 1584 break; 1585 } 1586 case JVM_CONSTANT_Double: { 1587 u8 val = Bytes::get_Java_u8(bytes); 1588 printf("double %5.3fd", *(jdouble *)&val); 1589 ent_size = 8; 1590 idx++; // Double takes two cpool slots 1591 break; 1592 } 1593 case JVM_CONSTANT_Class: { 1594 idx1 = Bytes::get_Java_u2(bytes); 1595 printf("class #%03d", idx1); 1596 ent_size = 2; 1597 break; 1598 } 1599 case JVM_CONSTANT_String: { 1600 idx1 = Bytes::get_Java_u2(bytes); 1601 printf("String #%03d", idx1); 1602 ent_size = 2; 1603 break; 1604 } 1605 case JVM_CONSTANT_Fieldref: { 1606 idx1 = Bytes::get_Java_u2(bytes); 1607 idx2 = Bytes::get_Java_u2(bytes+2); 1608 printf("Field #%03d, #%03d", (int) idx1, (int) idx2); 1609 ent_size = 4; 1610 break; 1611 } 1612 case JVM_CONSTANT_Methodref: { 1613 idx1 = Bytes::get_Java_u2(bytes); 1614 idx2 = Bytes::get_Java_u2(bytes+2); 1615 printf("Method #%03d, #%03d", idx1, idx2); 1616 ent_size = 4; 1617 break; 1618 } 1619 case JVM_CONSTANT_InterfaceMethodref: { 1620 idx1 = Bytes::get_Java_u2(bytes); 1621 idx2 = Bytes::get_Java_u2(bytes+2); 1622 printf("InterfMethod #%03d, #%03d", idx1, idx2); 1623 ent_size = 4; 1624 break; 1625 } 1626 case JVM_CONSTANT_NameAndType: { 1627 idx1 = Bytes::get_Java_u2(bytes); 1628 idx2 = Bytes::get_Java_u2(bytes+2); 1629 printf("NameAndType #%03d, #%03d", idx1, idx2); 1630 ent_size = 4; 1631 break; 1632 } 1633 case JVM_CONSTANT_ClassIndex: { 1634 printf("ClassIndex %s", WARN_MSG); 1635 break; 1636 } 1637 case JVM_CONSTANT_UnresolvedClass: { 1638 printf("UnresolvedClass: %s", WARN_MSG); 1639 break; 1640 } 1641 case JVM_CONSTANT_UnresolvedClassInError: { 1642 printf("UnresolvedClassInErr: %s", WARN_MSG); 1643 break; 1644 } 1645 case JVM_CONSTANT_StringIndex: { 1646 printf("StringIndex: %s", WARN_MSG); 1647 break; 1648 } 1649 } 1650 printf(";\n"); 1651 bytes += ent_size; 1652 size += ent_size; 1653 } 1654 printf("Cpool size: %d\n", size); 1655 fflush(0); 1656 return; 1657 } /* end print_cpool_bytes */ 1658 1659 1660 // Returns size of constant pool entry. 1661 jint ConstantPool::cpool_entry_size(jint idx) { 1662 switch(tag_at(idx).value()) { 1663 case JVM_CONSTANT_Invalid: 1664 case JVM_CONSTANT_Unicode: 1665 return 1; 1666 1667 case JVM_CONSTANT_Utf8: 1668 return 3 + symbol_at(idx)->utf8_length(); 1669 1670 case JVM_CONSTANT_Class: 1671 case JVM_CONSTANT_String: 1672 case JVM_CONSTANT_ClassIndex: 1673 case JVM_CONSTANT_UnresolvedClass: 1674 case JVM_CONSTANT_UnresolvedClassInError: 1675 case JVM_CONSTANT_StringIndex: 1676 case JVM_CONSTANT_MethodType: 1677 case JVM_CONSTANT_MethodTypeInError: 1678 return 3; 1679 1680 case JVM_CONSTANT_MethodHandle: 1681 case JVM_CONSTANT_MethodHandleInError: 1682 return 4; //tag, ref_kind, ref_index 1683 1684 case JVM_CONSTANT_Integer: 1685 case JVM_CONSTANT_Float: 1686 case JVM_CONSTANT_Fieldref: 1687 case JVM_CONSTANT_Methodref: 1688 case JVM_CONSTANT_InterfaceMethodref: 1689 case JVM_CONSTANT_NameAndType: 1690 return 5; 1691 1692 case JVM_CONSTANT_InvokeDynamic: 1693 // u1 tag, u2 bsm, u2 nt 1694 return 5; 1695 1696 case JVM_CONSTANT_Long: 1697 case JVM_CONSTANT_Double: 1698 return 9; 1699 } 1700 assert(false, "cpool_entry_size: Invalid constant pool entry tag"); 1701 return 1; 1702 } /* end cpool_entry_size */ 1703 1704 1705 // SymbolHashMap is used to find a constant pool index from a string. 1706 // This function fills in SymbolHashMaps, one for utf8s and one for 1707 // class names, returns size of the cpool raw bytes. 1708 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap, 1709 SymbolHashMap *classmap) { 1710 jint size = 0; 1711 1712 for (u2 idx = 1; idx < length(); idx++) { 1713 u2 tag = tag_at(idx).value(); 1714 size += cpool_entry_size(idx); 1715 1716 switch(tag) { 1717 case JVM_CONSTANT_Utf8: { 1718 Symbol* sym = symbol_at(idx); 1719 symmap->add_entry(sym, idx); 1720 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx)); 1721 break; 1722 } 1723 case JVM_CONSTANT_Class: 1724 case JVM_CONSTANT_UnresolvedClass: 1725 case JVM_CONSTANT_UnresolvedClassInError: { 1726 Symbol* sym = klass_name_at(idx); 1727 classmap->add_entry(sym, idx); 1728 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx)); 1729 break; 1730 } 1731 case JVM_CONSTANT_Long: 1732 case JVM_CONSTANT_Double: { 1733 idx++; // Both Long and Double take two cpool slots 1734 break; 1735 } 1736 } 1737 } 1738 return size; 1739 } /* end hash_utf8_entries_to */ 1740 1741 1742 // Copy cpool bytes. 1743 // Returns: 1744 // 0, in case of OutOfMemoryError 1745 // -1, in case of internal error 1746 // > 0, count of the raw cpool bytes that have been copied 1747 int ConstantPool::copy_cpool_bytes(int cpool_size, 1748 SymbolHashMap* tbl, 1749 unsigned char *bytes) { 1750 u2 idx1, idx2; 1751 jint size = 0; 1752 jint cnt = length(); 1753 unsigned char *start_bytes = bytes; 1754 1755 for (jint idx = 1; idx < cnt; idx++) { 1756 u1 tag = tag_at(idx).value(); 1757 jint ent_size = cpool_entry_size(idx); 1758 1759 assert(size + ent_size <= cpool_size, "Size mismatch"); 1760 1761 *bytes = tag; 1762 DBG(printf("#%03hd tag=%03hd, ", (short)idx, (short)tag)); 1763 switch(tag) { 1764 case JVM_CONSTANT_Invalid: { 1765 DBG(printf("JVM_CONSTANT_Invalid")); 1766 break; 1767 } 1768 case JVM_CONSTANT_Unicode: { 1769 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode"); 1770 DBG(printf("JVM_CONSTANT_Unicode")); 1771 break; 1772 } 1773 case JVM_CONSTANT_Utf8: { 1774 Symbol* sym = symbol_at(idx); 1775 char* str = sym->as_utf8(); 1776 // Warning! It's crashing on x86 with len = sym->utf8_length() 1777 int len = (int) strlen(str); 1778 Bytes::put_Java_u2((address) (bytes+1), (u2) len); 1779 for (int i = 0; i < len; i++) { 1780 bytes[3+i] = (u1) str[i]; 1781 } 1782 DBG(printf("JVM_CONSTANT_Utf8: %s ", str)); 1783 break; 1784 } 1785 case JVM_CONSTANT_Integer: { 1786 jint val = int_at(idx); 1787 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 1788 break; 1789 } 1790 case JVM_CONSTANT_Float: { 1791 jfloat val = float_at(idx); 1792 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 1793 break; 1794 } 1795 case JVM_CONSTANT_Long: { 1796 jlong val = long_at(idx); 1797 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 1798 idx++; // Long takes two cpool slots 1799 break; 1800 } 1801 case JVM_CONSTANT_Double: { 1802 jdouble val = double_at(idx); 1803 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 1804 idx++; // Double takes two cpool slots 1805 break; 1806 } 1807 case JVM_CONSTANT_Class: 1808 case JVM_CONSTANT_UnresolvedClass: 1809 case JVM_CONSTANT_UnresolvedClassInError: { 1810 *bytes = JVM_CONSTANT_Class; 1811 Symbol* sym = klass_name_at(idx); 1812 idx1 = tbl->symbol_to_value(sym); 1813 assert(idx1 != 0, "Have not found a hashtable entry"); 1814 Bytes::put_Java_u2((address) (bytes+1), idx1); 1815 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8())); 1816 break; 1817 } 1818 case JVM_CONSTANT_String: { 1819 *bytes = JVM_CONSTANT_String; 1820 Symbol* sym = unresolved_string_at(idx); 1821 idx1 = tbl->symbol_to_value(sym); 1822 assert(idx1 != 0, "Have not found a hashtable entry"); 1823 Bytes::put_Java_u2((address) (bytes+1), idx1); 1824 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8())); 1825 break; 1826 } 1827 case JVM_CONSTANT_Fieldref: 1828 case JVM_CONSTANT_Methodref: 1829 case JVM_CONSTANT_InterfaceMethodref: { 1830 idx1 = uncached_klass_ref_index_at(idx); 1831 idx2 = uncached_name_and_type_ref_index_at(idx); 1832 Bytes::put_Java_u2((address) (bytes+1), idx1); 1833 Bytes::put_Java_u2((address) (bytes+3), idx2); 1834 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2)); 1835 break; 1836 } 1837 case JVM_CONSTANT_NameAndType: { 1838 idx1 = name_ref_index_at(idx); 1839 idx2 = signature_ref_index_at(idx); 1840 Bytes::put_Java_u2((address) (bytes+1), idx1); 1841 Bytes::put_Java_u2((address) (bytes+3), idx2); 1842 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2)); 1843 break; 1844 } 1845 case JVM_CONSTANT_ClassIndex: { 1846 *bytes = JVM_CONSTANT_Class; 1847 idx1 = klass_index_at(idx); 1848 Bytes::put_Java_u2((address) (bytes+1), idx1); 1849 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1)); 1850 break; 1851 } 1852 case JVM_CONSTANT_StringIndex: { 1853 *bytes = JVM_CONSTANT_String; 1854 idx1 = string_index_at(idx); 1855 Bytes::put_Java_u2((address) (bytes+1), idx1); 1856 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1)); 1857 break; 1858 } 1859 case JVM_CONSTANT_MethodHandle: 1860 case JVM_CONSTANT_MethodHandleInError: { 1861 *bytes = JVM_CONSTANT_MethodHandle; 1862 int kind = method_handle_ref_kind_at(idx); 1863 idx1 = method_handle_index_at(idx); 1864 *(bytes+1) = (unsigned char) kind; 1865 Bytes::put_Java_u2((address) (bytes+2), idx1); 1866 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1)); 1867 break; 1868 } 1869 case JVM_CONSTANT_MethodType: 1870 case JVM_CONSTANT_MethodTypeInError: { 1871 *bytes = JVM_CONSTANT_MethodType; 1872 idx1 = method_type_index_at(idx); 1873 Bytes::put_Java_u2((address) (bytes+1), idx1); 1874 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1)); 1875 break; 1876 } 1877 case JVM_CONSTANT_InvokeDynamic: { 1878 *bytes = tag; 1879 idx1 = extract_low_short_from_int(*int_at_addr(idx)); 1880 idx2 = extract_high_short_from_int(*int_at_addr(idx)); 1881 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4"); 1882 Bytes::put_Java_u2((address) (bytes+1), idx1); 1883 Bytes::put_Java_u2((address) (bytes+3), idx2); 1884 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2)); 1885 break; 1886 } 1887 } 1888 DBG(printf("\n")); 1889 bytes += ent_size; 1890 size += ent_size; 1891 } 1892 assert(size == cpool_size, "Size mismatch"); 1893 1894 // Keep temorarily for debugging until it's stable. 1895 DBG(print_cpool_bytes(cnt, start_bytes)); 1896 return (int)(bytes - start_bytes); 1897 } /* end copy_cpool_bytes */ 1898 1899 #undef DBG 1900 1901 1902 void ConstantPool::set_on_stack(const bool value) { 1903 if (value) { 1904 // Only record if it's not already set. 1905 if (!on_stack()) { 1906 assert(!is_shared(), "should always be set for shared constant pools"); 1907 _flags |= _on_stack; 1908 MetadataOnStackMark::record(this); 1909 } 1910 } else { 1911 // Clearing is done single-threadedly. 1912 if (!is_shared()) { 1913 _flags &= ~_on_stack; 1914 } 1915 } 1916 } 1917 1918 // JSR 292 support for patching constant pool oops after the class is linked and 1919 // the oop array for resolved references are created. 1920 // We can't do this during classfile parsing, which is how the other indexes are 1921 // patched. The other patches are applied early for some error checking 1922 // so only defer the pseudo_strings. 1923 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) { 1924 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused 1925 Handle patch = cp_patches->at(index); 1926 if (patch.not_null()) { 1927 assert (tag_at(index).is_string(), "should only be string left"); 1928 // Patching a string means pre-resolving it. 1929 // The spelling in the constant pool is ignored. 1930 // The constant reference may be any object whatever. 1931 // If it is not a real interned string, the constant is referred 1932 // to as a "pseudo-string", and must be presented to the CP 1933 // explicitly, because it may require scavenging. 1934 int obj_index = cp_to_object_index(index); 1935 pseudo_string_at_put(index, obj_index, patch()); 1936 DEBUG_ONLY(cp_patches->at_put(index, Handle());) 1937 } 1938 } 1939 #ifdef ASSERT 1940 // Ensure that all the patches have been used. 1941 for (int index = 0; index < cp_patches->length(); index++) { 1942 assert(cp_patches->at(index).is_null(), 1943 "Unused constant pool patch at %d in class file %s", 1944 index, 1945 pool_holder()->external_name()); 1946 } 1947 #endif // ASSERT 1948 } 1949 1950 #ifndef PRODUCT 1951 1952 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool 1953 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) { 1954 guarantee(obj->is_constantPool(), "object must be constant pool"); 1955 constantPoolHandle cp(THREAD, (ConstantPool*)obj); 1956 guarantee(cp->pool_holder() != NULL, "must be fully loaded"); 1957 1958 for (int i = 0; i< cp->length(); i++) { 1959 if (cp->tag_at(i).is_unresolved_klass()) { 1960 // This will force loading of the class 1961 Klass* klass = cp->klass_at(i, CHECK); 1962 if (klass->is_instance_klass()) { 1963 // Force initialization of class 1964 InstanceKlass::cast(klass)->initialize(CHECK); 1965 } 1966 } 1967 } 1968 } 1969 1970 #endif 1971 1972 1973 // Printing 1974 1975 void ConstantPool::print_on(outputStream* st) const { 1976 assert(is_constantPool(), "must be constantPool"); 1977 st->print_cr("%s", internal_name()); 1978 if (flags() != 0) { 1979 st->print(" - flags: 0x%x", flags()); 1980 if (has_preresolution()) st->print(" has_preresolution"); 1981 if (on_stack()) st->print(" on_stack"); 1982 st->cr(); 1983 } 1984 if (pool_holder() != NULL) { 1985 st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder())); 1986 } 1987 st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache())); 1988 st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references())); 1989 st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map())); 1990 st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses())); 1991 1992 for (int index = 1; index < length(); index++) { // Index 0 is unused 1993 ((ConstantPool*)this)->print_entry_on(index, st); 1994 switch (tag_at(index).value()) { 1995 case JVM_CONSTANT_Long : 1996 case JVM_CONSTANT_Double : 1997 index++; // Skip entry following eigth-byte constant 1998 } 1999 2000 } 2001 st->cr(); 2002 } 2003 2004 // Print one constant pool entry 2005 void ConstantPool::print_entry_on(const int index, outputStream* st) { 2006 EXCEPTION_MARK; 2007 st->print(" - %3d : ", index); 2008 tag_at(index).print_on(st); 2009 st->print(" : "); 2010 switch (tag_at(index).value()) { 2011 case JVM_CONSTANT_Class : 2012 { Klass* k = klass_at(index, CATCH); 2013 guarantee(k != NULL, "need klass"); 2014 k->print_value_on(st); 2015 st->print(" {" PTR_FORMAT "}", p2i(k)); 2016 } 2017 break; 2018 case JVM_CONSTANT_Fieldref : 2019 case JVM_CONSTANT_Methodref : 2020 case JVM_CONSTANT_InterfaceMethodref : 2021 st->print("klass_index=%d", uncached_klass_ref_index_at(index)); 2022 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index)); 2023 break; 2024 case JVM_CONSTANT_String : 2025 if (is_pseudo_string_at(index)) { 2026 oop anObj = pseudo_string_at(index); 2027 anObj->print_value_on(st); 2028 st->print(" {" PTR_FORMAT "}", p2i(anObj)); 2029 } else { 2030 unresolved_string_at(index)->print_value_on(st); 2031 } 2032 break; 2033 case JVM_CONSTANT_Integer : 2034 st->print("%d", int_at(index)); 2035 break; 2036 case JVM_CONSTANT_Float : 2037 st->print("%f", float_at(index)); 2038 break; 2039 case JVM_CONSTANT_Long : 2040 st->print_jlong(long_at(index)); 2041 break; 2042 case JVM_CONSTANT_Double : 2043 st->print("%lf", double_at(index)); 2044 break; 2045 case JVM_CONSTANT_NameAndType : 2046 st->print("name_index=%d", name_ref_index_at(index)); 2047 st->print(" signature_index=%d", signature_ref_index_at(index)); 2048 break; 2049 case JVM_CONSTANT_Utf8 : 2050 symbol_at(index)->print_value_on(st); 2051 break; 2052 case JVM_CONSTANT_ClassIndex: { 2053 int name_index = *int_at_addr(index); 2054 st->print("klass_index=%d ", name_index); 2055 symbol_at(name_index)->print_value_on(st); 2056 } 2057 break; 2058 case JVM_CONSTANT_UnresolvedClass : // fall-through 2059 case JVM_CONSTANT_UnresolvedClassInError: { 2060 CPKlassSlot kslot = klass_slot_at(index); 2061 int resolved_klass_index = kslot.resolved_klass_index(); 2062 int name_index = kslot.name_index(); 2063 assert(tag_at(name_index).is_symbol(), "sanity"); 2064 2065 Klass* klass = resolved_klasses()->at(resolved_klass_index); 2066 if (klass != NULL) { 2067 klass->print_value_on(st); 2068 } else { 2069 symbol_at(name_index)->print_value_on(st); 2070 } 2071 } 2072 break; 2073 case JVM_CONSTANT_MethodHandle : 2074 case JVM_CONSTANT_MethodHandleInError : 2075 st->print("ref_kind=%d", method_handle_ref_kind_at(index)); 2076 st->print(" ref_index=%d", method_handle_index_at(index)); 2077 break; 2078 case JVM_CONSTANT_MethodType : 2079 case JVM_CONSTANT_MethodTypeInError : 2080 st->print("signature_index=%d", method_type_index_at(index)); 2081 break; 2082 case JVM_CONSTANT_InvokeDynamic : 2083 { 2084 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index)); 2085 st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index)); 2086 int argc = invoke_dynamic_argument_count_at(index); 2087 if (argc > 0) { 2088 for (int arg_i = 0; arg_i < argc; arg_i++) { 2089 int arg = invoke_dynamic_argument_index_at(index, arg_i); 2090 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 2091 } 2092 st->print("}"); 2093 } 2094 } 2095 break; 2096 default: 2097 ShouldNotReachHere(); 2098 break; 2099 } 2100 st->cr(); 2101 } 2102 2103 void ConstantPool::print_value_on(outputStream* st) const { 2104 assert(is_constantPool(), "must be constantPool"); 2105 st->print("constant pool [%d]", length()); 2106 if (has_preresolution()) st->print("/preresolution"); 2107 if (operands() != NULL) st->print("/operands[%d]", operands()->length()); 2108 print_address_on(st); 2109 st->print(" for "); 2110 pool_holder()->print_value_on(st); 2111 if (pool_holder() != NULL) { 2112 bool extra = (pool_holder()->constants() != this); 2113 if (extra) st->print(" (extra)"); 2114 } 2115 if (cache() != NULL) { 2116 st->print(" cache=" PTR_FORMAT, p2i(cache())); 2117 } 2118 } 2119 2120 #if INCLUDE_SERVICES 2121 // Size Statistics 2122 void ConstantPool::collect_statistics(KlassSizeStats *sz) const { 2123 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this)); 2124 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags())); 2125 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache())); 2126 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands())); 2127 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map())); 2128 2129 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes + 2130 sz->_cp_refmap_bytes; 2131 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes; 2132 } 2133 #endif // INCLUDE_SERVICES 2134 2135 // Verification 2136 2137 void ConstantPool::verify_on(outputStream* st) { 2138 guarantee(is_constantPool(), "object must be constant pool"); 2139 for (int i = 0; i< length(); i++) { 2140 constantTag tag = tag_at(i); 2141 if (tag.is_klass() || tag.is_unresolved_klass()) { 2142 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count"); 2143 } else if (tag.is_symbol()) { 2144 CPSlot entry = slot_at(i); 2145 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2146 } else if (tag.is_string()) { 2147 CPSlot entry = slot_at(i); 2148 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2149 } 2150 } 2151 if (cache() != NULL) { 2152 // Note: cache() can be NULL before a class is completely setup or 2153 // in temporary constant pools used during constant pool merging 2154 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache"); 2155 } 2156 if (pool_holder() != NULL) { 2157 // Note: pool_holder() can be NULL in temporary constant pools 2158 // used during constant pool merging 2159 guarantee(pool_holder()->is_klass(), "should be klass"); 2160 } 2161 } 2162 2163 2164 void SymbolHashMap::add_entry(Symbol* sym, u2 value) { 2165 char *str = sym->as_utf8(); 2166 unsigned int hash = compute_hash(str, sym->utf8_length()); 2167 unsigned int index = hash % table_size(); 2168 2169 // check if already in map 2170 // we prefer the first entry since it is more likely to be what was used in 2171 // the class file 2172 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2173 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2174 if (en->hash() == hash && en->symbol() == sym) { 2175 return; // already there 2176 } 2177 } 2178 2179 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value); 2180 entry->set_next(bucket(index)); 2181 _buckets[index].set_entry(entry); 2182 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2183 } 2184 2185 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) { 2186 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL"); 2187 char *str = sym->as_utf8(); 2188 int len = sym->utf8_length(); 2189 unsigned int hash = SymbolHashMap::compute_hash(str, len); 2190 unsigned int index = hash % table_size(); 2191 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2192 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2193 if (en->hash() == hash && en->symbol() == sym) { 2194 return en; 2195 } 2196 } 2197 return NULL; 2198 }