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