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 case JVM_CONSTANT_InvokeDynamicInError: 744 { 745 throw_resolution_error(this_cp, index, CHECK_NULL); 746 break; 747 } 748 749 case JVM_CONSTANT_MethodHandle: 750 { 751 int ref_kind = this_cp->method_handle_ref_kind_at(index); 752 int callee_index = this_cp->method_handle_klass_index_at(index); 753 Symbol* name = this_cp->method_handle_name_ref_at(index); 754 Symbol* signature = this_cp->method_handle_signature_ref_at(index); 755 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(index)); 756 { ResourceMark rm(THREAD); 757 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s", 758 ref_kind, index, this_cp->method_handle_index_at(index), 759 callee_index, name->as_C_string(), signature->as_C_string()); 760 } 761 762 Klass* callee = klass_at_impl(this_cp, callee_index, true, CHECK_NULL); 763 764 // Check constant pool method consistency 765 if ((callee->is_interface() && m_tag.is_method()) || 766 ((!callee->is_interface() && m_tag.is_interface_method()))) { 767 ResourceMark rm(THREAD); 768 char buf[400]; 769 jio_snprintf(buf, sizeof(buf), 770 "Inconsistent constant pool data in classfile for class %s. " 771 "Method %s%s at index %d is %s and should be %s", 772 callee->name()->as_C_string(), name->as_C_string(), signature->as_C_string(), index, 773 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef", 774 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef"); 775 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 776 } 777 778 Klass* klass = this_cp->pool_holder(); 779 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind, 780 callee, name, signature, 781 THREAD); 782 result_oop = value(); 783 if (HAS_PENDING_EXCEPTION) { 784 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 785 } 786 break; 787 } 788 789 case JVM_CONSTANT_MethodType: 790 { 791 Symbol* signature = this_cp->method_type_signature_at(index); 792 { ResourceMark rm(THREAD); 793 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s", 794 index, this_cp->method_type_index_at(index), 795 signature->as_C_string()); 796 } 797 Klass* klass = this_cp->pool_holder(); 798 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD); 799 result_oop = value(); 800 if (HAS_PENDING_EXCEPTION) { 801 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 802 } 803 break; 804 } 805 806 case JVM_CONSTANT_Integer: 807 assert(cache_index == _no_index_sentinel, "should not have been set"); 808 prim_value.i = this_cp->int_at(index); 809 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL); 810 break; 811 812 case JVM_CONSTANT_Float: 813 assert(cache_index == _no_index_sentinel, "should not have been set"); 814 prim_value.f = this_cp->float_at(index); 815 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL); 816 break; 817 818 case JVM_CONSTANT_Long: 819 assert(cache_index == _no_index_sentinel, "should not have been set"); 820 prim_value.j = this_cp->long_at(index); 821 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL); 822 break; 823 824 case JVM_CONSTANT_Double: 825 assert(cache_index == _no_index_sentinel, "should not have been set"); 826 prim_value.d = this_cp->double_at(index); 827 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL); 828 break; 829 830 default: 831 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d", 832 this_cp(), index, cache_index, tag.value())); 833 assert(false, "unexpected constant tag"); 834 break; 835 } 836 837 if (cache_index >= 0) { 838 // Benign race condition: resolved_references may already be filled in. 839 // The important thing here is that all threads pick up the same result. 840 // It doesn't matter which racing thread wins, as long as only one 841 // result is used by all threads, and all future queries. 842 oop old_result = this_cp->resolved_references()->atomic_compare_exchange_oop(cache_index, result_oop, NULL); 843 if (old_result == NULL) { 844 return result_oop; // was installed 845 } else { 846 // Return the winning thread's result. This can be different than 847 // the result here for MethodHandles. 848 return old_result; 849 } 850 } else { 851 return result_oop; 852 } 853 } 854 855 oop ConstantPool::uncached_string_at(int which, TRAPS) { 856 Symbol* sym = unresolved_string_at(which); 857 oop str = StringTable::intern(sym, CHECK_(NULL)); 858 assert(java_lang_String::is_instance(str), "must be string"); 859 return str; 860 } 861 862 863 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) { 864 assert(this_cp->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool"); 865 866 Handle bsm; 867 int argc; 868 { 869 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments 870 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry. 871 // It is accompanied by the optional arguments. 872 int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index); 873 oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL); 874 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) { 875 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle"); 876 } 877 878 // Extract the optional static arguments. 879 argc = this_cp->invoke_dynamic_argument_count_at(index); 880 if (argc == 0) return bsm_oop; 881 882 bsm = Handle(THREAD, bsm_oop); 883 } 884 885 objArrayHandle info; 886 { 887 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL); 888 info = objArrayHandle(THREAD, info_oop); 889 } 890 891 info->obj_at_put(0, bsm()); 892 for (int i = 0; i < argc; i++) { 893 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); 894 oop arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL); 895 info->obj_at_put(1+i, arg_oop); 896 } 897 898 return info(); 899 } 900 901 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) { 902 // If the string has already been interned, this entry will be non-null 903 oop str = this_cp->resolved_references()->obj_at(obj_index); 904 if (str != NULL) return str; 905 Symbol* sym = this_cp->unresolved_string_at(which); 906 str = StringTable::intern(sym, CHECK_(NULL)); 907 this_cp->string_at_put(which, obj_index, str); 908 assert(java_lang_String::is_instance(str), "must be string"); 909 return str; 910 } 911 912 913 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) { 914 // Names are interned, so we can compare Symbol*s directly 915 Symbol* cp_name = klass_name_at(which); 916 return (cp_name == k->name()); 917 } 918 919 920 // Iterate over symbols and decrement ones which are Symbol*s 921 // This is done during GC. 922 // Only decrement the UTF8 symbols. Strings point to 923 // these symbols but didn't increment the reference count. 924 void ConstantPool::unreference_symbols() { 925 for (int index = 1; index < length(); index++) { // Index 0 is unused 926 constantTag tag = tag_at(index); 927 if (tag.is_symbol()) { 928 symbol_at(index)->decrement_refcount(); 929 } 930 } 931 } 932 933 934 // Compare this constant pool's entry at index1 to the constant pool 935 // cp2's entry at index2. 936 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2, 937 int index2, TRAPS) { 938 939 // The error tags are equivalent to non-error tags when comparing 940 jbyte t1 = tag_at(index1).non_error_value(); 941 jbyte t2 = cp2->tag_at(index2).non_error_value(); 942 943 if (t1 != t2) { 944 // Not the same entry type so there is nothing else to check. Note 945 // that this style of checking will consider resolved/unresolved 946 // class pairs as different. 947 // From the ConstantPool* API point of view, this is correct 948 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this 949 // plays out in the context of ConstantPool* merging. 950 return false; 951 } 952 953 switch (t1) { 954 case JVM_CONSTANT_Class: 955 { 956 Klass* k1 = klass_at(index1, CHECK_false); 957 Klass* k2 = cp2->klass_at(index2, CHECK_false); 958 if (k1 == k2) { 959 return true; 960 } 961 } break; 962 963 case JVM_CONSTANT_ClassIndex: 964 { 965 int recur1 = klass_index_at(index1); 966 int recur2 = cp2->klass_index_at(index2); 967 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 968 if (match) { 969 return true; 970 } 971 } break; 972 973 case JVM_CONSTANT_Double: 974 { 975 jdouble d1 = double_at(index1); 976 jdouble d2 = cp2->double_at(index2); 977 if (d1 == d2) { 978 return true; 979 } 980 } break; 981 982 case JVM_CONSTANT_Fieldref: 983 case JVM_CONSTANT_InterfaceMethodref: 984 case JVM_CONSTANT_Methodref: 985 { 986 int recur1 = uncached_klass_ref_index_at(index1); 987 int recur2 = cp2->uncached_klass_ref_index_at(index2); 988 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 989 if (match) { 990 recur1 = uncached_name_and_type_ref_index_at(index1); 991 recur2 = cp2->uncached_name_and_type_ref_index_at(index2); 992 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 993 if (match) { 994 return true; 995 } 996 } 997 } break; 998 999 case JVM_CONSTANT_Float: 1000 { 1001 jfloat f1 = float_at(index1); 1002 jfloat f2 = cp2->float_at(index2); 1003 if (f1 == f2) { 1004 return true; 1005 } 1006 } break; 1007 1008 case JVM_CONSTANT_Integer: 1009 { 1010 jint i1 = int_at(index1); 1011 jint i2 = cp2->int_at(index2); 1012 if (i1 == i2) { 1013 return true; 1014 } 1015 } break; 1016 1017 case JVM_CONSTANT_Long: 1018 { 1019 jlong l1 = long_at(index1); 1020 jlong l2 = cp2->long_at(index2); 1021 if (l1 == l2) { 1022 return true; 1023 } 1024 } break; 1025 1026 case JVM_CONSTANT_NameAndType: 1027 { 1028 int recur1 = name_ref_index_at(index1); 1029 int recur2 = cp2->name_ref_index_at(index2); 1030 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1031 if (match) { 1032 recur1 = signature_ref_index_at(index1); 1033 recur2 = cp2->signature_ref_index_at(index2); 1034 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1035 if (match) { 1036 return true; 1037 } 1038 } 1039 } break; 1040 1041 case JVM_CONSTANT_StringIndex: 1042 { 1043 int recur1 = string_index_at(index1); 1044 int recur2 = cp2->string_index_at(index2); 1045 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1046 if (match) { 1047 return true; 1048 } 1049 } break; 1050 1051 case JVM_CONSTANT_UnresolvedClass: 1052 { 1053 Symbol* k1 = klass_name_at(index1); 1054 Symbol* k2 = cp2->klass_name_at(index2); 1055 if (k1 == k2) { 1056 return true; 1057 } 1058 } break; 1059 1060 case JVM_CONSTANT_MethodType: 1061 { 1062 int k1 = method_type_index_at(index1); 1063 int k2 = cp2->method_type_index_at(index2); 1064 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 1065 if (match) { 1066 return true; 1067 } 1068 } break; 1069 1070 case JVM_CONSTANT_MethodHandle: 1071 { 1072 int k1 = method_handle_ref_kind_at(index1); 1073 int k2 = cp2->method_handle_ref_kind_at(index2); 1074 if (k1 == k2) { 1075 int i1 = method_handle_index_at(index1); 1076 int i2 = cp2->method_handle_index_at(index2); 1077 bool match = compare_entry_to(i1, cp2, i2, CHECK_false); 1078 if (match) { 1079 return true; 1080 } 1081 } 1082 } break; 1083 1084 case JVM_CONSTANT_InvokeDynamic: 1085 { 1086 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1); 1087 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2); 1088 int i1 = invoke_dynamic_bootstrap_specifier_index(index1); 1089 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2); 1090 // separate statements and variables because CHECK_false is used 1091 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false); 1092 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false); 1093 return (match_entry && match_operand); 1094 } break; 1095 1096 case JVM_CONSTANT_String: 1097 { 1098 Symbol* s1 = unresolved_string_at(index1); 1099 Symbol* s2 = cp2->unresolved_string_at(index2); 1100 if (s1 == s2) { 1101 return true; 1102 } 1103 } break; 1104 1105 case JVM_CONSTANT_Utf8: 1106 { 1107 Symbol* s1 = symbol_at(index1); 1108 Symbol* s2 = cp2->symbol_at(index2); 1109 if (s1 == s2) { 1110 return true; 1111 } 1112 } break; 1113 1114 // Invalid is used as the tag for the second constant pool entry 1115 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1116 // not be seen by itself. 1117 case JVM_CONSTANT_Invalid: // fall through 1118 1119 default: 1120 ShouldNotReachHere(); 1121 break; 1122 } 1123 1124 return false; 1125 } // end compare_entry_to() 1126 1127 1128 // Resize the operands array with delta_len and delta_size. 1129 // Used in RedefineClasses for CP merge. 1130 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) { 1131 int old_len = operand_array_length(operands()); 1132 int new_len = old_len + delta_len; 1133 int min_len = (delta_len > 0) ? old_len : new_len; 1134 1135 int old_size = operands()->length(); 1136 int new_size = old_size + delta_size; 1137 int min_size = (delta_size > 0) ? old_size : new_size; 1138 1139 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1140 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK); 1141 1142 // Set index in the resized array for existing elements only 1143 for (int idx = 0; idx < min_len; idx++) { 1144 int offset = operand_offset_at(idx); // offset in original array 1145 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array 1146 } 1147 // Copy the bootstrap specifiers only 1148 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len), 1149 new_ops->adr_at(2*new_len), 1150 (min_size - 2*min_len) * sizeof(u2)); 1151 // Explicitly deallocate old operands array. 1152 // Note, it is not needed for 7u backport. 1153 if ( operands() != NULL) { // the safety check 1154 MetadataFactory::free_array<u2>(loader_data, operands()); 1155 } 1156 set_operands(new_ops); 1157 } // end resize_operands() 1158 1159 1160 // Extend the operands array with the length and size of the ext_cp operands. 1161 // Used in RedefineClasses for CP merge. 1162 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) { 1163 int delta_len = operand_array_length(ext_cp->operands()); 1164 if (delta_len == 0) { 1165 return; // nothing to do 1166 } 1167 int delta_size = ext_cp->operands()->length(); 1168 1169 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger"); 1170 1171 if (operand_array_length(operands()) == 0) { 1172 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1173 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK); 1174 // The first element index defines the offset of second part 1175 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array 1176 set_operands(new_ops); 1177 } else { 1178 resize_operands(delta_len, delta_size, CHECK); 1179 } 1180 1181 } // end extend_operands() 1182 1183 1184 // Shrink the operands array to a smaller array with new_len length. 1185 // Used in RedefineClasses for CP merge. 1186 void ConstantPool::shrink_operands(int new_len, TRAPS) { 1187 int old_len = operand_array_length(operands()); 1188 if (new_len == old_len) { 1189 return; // nothing to do 1190 } 1191 assert(new_len < old_len, "shrunken operands array must be smaller"); 1192 1193 int free_base = operand_next_offset_at(new_len - 1); 1194 int delta_len = new_len - old_len; 1195 int delta_size = 2*delta_len + free_base - operands()->length(); 1196 1197 resize_operands(delta_len, delta_size, CHECK); 1198 1199 } // end shrink_operands() 1200 1201 1202 void ConstantPool::copy_operands(const constantPoolHandle& from_cp, 1203 const constantPoolHandle& to_cp, 1204 TRAPS) { 1205 1206 int from_oplen = operand_array_length(from_cp->operands()); 1207 int old_oplen = operand_array_length(to_cp->operands()); 1208 if (from_oplen != 0) { 1209 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data(); 1210 // append my operands to the target's operands array 1211 if (old_oplen == 0) { 1212 // Can't just reuse from_cp's operand list because of deallocation issues 1213 int len = from_cp->operands()->length(); 1214 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK); 1215 Copy::conjoint_memory_atomic( 1216 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2)); 1217 to_cp->set_operands(new_ops); 1218 } else { 1219 int old_len = to_cp->operands()->length(); 1220 int from_len = from_cp->operands()->length(); 1221 int old_off = old_oplen * sizeof(u2); 1222 int from_off = from_oplen * sizeof(u2); 1223 // Use the metaspace for the destination constant pool 1224 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK); 1225 int fillp = 0, len = 0; 1226 // first part of dest 1227 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0), 1228 new_operands->adr_at(fillp), 1229 (len = old_off) * sizeof(u2)); 1230 fillp += len; 1231 // first part of src 1232 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0), 1233 new_operands->adr_at(fillp), 1234 (len = from_off) * sizeof(u2)); 1235 fillp += len; 1236 // second part of dest 1237 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off), 1238 new_operands->adr_at(fillp), 1239 (len = old_len - old_off) * sizeof(u2)); 1240 fillp += len; 1241 // second part of src 1242 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off), 1243 new_operands->adr_at(fillp), 1244 (len = from_len - from_off) * sizeof(u2)); 1245 fillp += len; 1246 assert(fillp == new_operands->length(), ""); 1247 1248 // Adjust indexes in the first part of the copied operands array. 1249 for (int j = 0; j < from_oplen; j++) { 1250 int offset = operand_offset_at(new_operands, old_oplen + j); 1251 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy"); 1252 offset += old_len; // every new tuple is preceded by old_len extra u2's 1253 operand_offset_at_put(new_operands, old_oplen + j, offset); 1254 } 1255 1256 // replace target operands array with combined array 1257 to_cp->set_operands(new_operands); 1258 } 1259 } 1260 } // end copy_operands() 1261 1262 1263 // Copy this constant pool's entries at start_i to end_i (inclusive) 1264 // to the constant pool to_cp's entries starting at to_i. A total of 1265 // (end_i - start_i) + 1 entries are copied. 1266 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, 1267 const constantPoolHandle& to_cp, int to_i, TRAPS) { 1268 1269 1270 int dest_i = to_i; // leave original alone for debug purposes 1271 1272 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) { 1273 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK); 1274 1275 switch (from_cp->tag_at(src_i).value()) { 1276 case JVM_CONSTANT_Double: 1277 case JVM_CONSTANT_Long: 1278 // double and long take two constant pool entries 1279 src_i += 2; 1280 dest_i += 2; 1281 break; 1282 1283 default: 1284 // all others take one constant pool entry 1285 src_i++; 1286 dest_i++; 1287 break; 1288 } 1289 } 1290 copy_operands(from_cp, to_cp, CHECK); 1291 1292 } // end copy_cp_to_impl() 1293 1294 1295 // Copy this constant pool's entry at from_i to the constant pool 1296 // to_cp's entry at to_i. 1297 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i, 1298 const constantPoolHandle& to_cp, int to_i, 1299 TRAPS) { 1300 1301 int tag = from_cp->tag_at(from_i).value(); 1302 switch (tag) { 1303 case JVM_CONSTANT_ClassIndex: 1304 { 1305 jint ki = from_cp->klass_index_at(from_i); 1306 to_cp->klass_index_at_put(to_i, ki); 1307 } break; 1308 1309 case JVM_CONSTANT_Double: 1310 { 1311 jdouble d = from_cp->double_at(from_i); 1312 to_cp->double_at_put(to_i, d); 1313 // double takes two constant pool entries so init second entry's tag 1314 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1315 } break; 1316 1317 case JVM_CONSTANT_Fieldref: 1318 { 1319 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1320 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1321 to_cp->field_at_put(to_i, class_index, name_and_type_index); 1322 } break; 1323 1324 case JVM_CONSTANT_Float: 1325 { 1326 jfloat f = from_cp->float_at(from_i); 1327 to_cp->float_at_put(to_i, f); 1328 } break; 1329 1330 case JVM_CONSTANT_Integer: 1331 { 1332 jint i = from_cp->int_at(from_i); 1333 to_cp->int_at_put(to_i, i); 1334 } break; 1335 1336 case JVM_CONSTANT_InterfaceMethodref: 1337 { 1338 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1339 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1340 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index); 1341 } break; 1342 1343 case JVM_CONSTANT_Long: 1344 { 1345 jlong l = from_cp->long_at(from_i); 1346 to_cp->long_at_put(to_i, l); 1347 // long takes two constant pool entries so init second entry's tag 1348 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1349 } break; 1350 1351 case JVM_CONSTANT_Methodref: 1352 { 1353 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1354 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1355 to_cp->method_at_put(to_i, class_index, name_and_type_index); 1356 } break; 1357 1358 case JVM_CONSTANT_NameAndType: 1359 { 1360 int name_ref_index = from_cp->name_ref_index_at(from_i); 1361 int signature_ref_index = from_cp->signature_ref_index_at(from_i); 1362 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index); 1363 } break; 1364 1365 case JVM_CONSTANT_StringIndex: 1366 { 1367 jint si = from_cp->string_index_at(from_i); 1368 to_cp->string_index_at_put(to_i, si); 1369 } break; 1370 1371 case JVM_CONSTANT_Class: 1372 case JVM_CONSTANT_UnresolvedClass: 1373 case JVM_CONSTANT_UnresolvedClassInError: 1374 { 1375 // Revert to JVM_CONSTANT_ClassIndex 1376 int name_index = from_cp->klass_slot_at(from_i).name_index(); 1377 assert(from_cp->tag_at(name_index).is_symbol(), "sanity"); 1378 to_cp->klass_index_at_put(to_i, name_index); 1379 } break; 1380 1381 case JVM_CONSTANT_String: 1382 { 1383 Symbol* s = from_cp->unresolved_string_at(from_i); 1384 to_cp->unresolved_string_at_put(to_i, s); 1385 } break; 1386 1387 case JVM_CONSTANT_Utf8: 1388 { 1389 Symbol* s = from_cp->symbol_at(from_i); 1390 // Need to increase refcount, the old one will be thrown away and deferenced 1391 s->increment_refcount(); 1392 to_cp->symbol_at_put(to_i, s); 1393 } break; 1394 1395 case JVM_CONSTANT_MethodType: 1396 case JVM_CONSTANT_MethodTypeInError: 1397 { 1398 jint k = from_cp->method_type_index_at(from_i); 1399 to_cp->method_type_index_at_put(to_i, k); 1400 } break; 1401 1402 case JVM_CONSTANT_MethodHandle: 1403 case JVM_CONSTANT_MethodHandleInError: 1404 { 1405 int k1 = from_cp->method_handle_ref_kind_at(from_i); 1406 int k2 = from_cp->method_handle_index_at(from_i); 1407 to_cp->method_handle_index_at_put(to_i, k1, k2); 1408 } break; 1409 1410 case JVM_CONSTANT_InvokeDynamic: 1411 case JVM_CONSTANT_InvokeDynamicInError: 1412 { 1413 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i); 1414 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i); 1415 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands 1416 to_cp->invoke_dynamic_at_put(to_i, k1, k2); 1417 } break; 1418 1419 // Invalid is used as the tag for the second constant pool entry 1420 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1421 // not be seen by itself. 1422 case JVM_CONSTANT_Invalid: // fall through 1423 1424 default: 1425 { 1426 ShouldNotReachHere(); 1427 } break; 1428 } 1429 } // end copy_entry_to() 1430 1431 // Search constant pool search_cp for an entry that matches this 1432 // constant pool's entry at pattern_i. Returns the index of a 1433 // matching entry or zero (0) if there is no matching entry. 1434 int ConstantPool::find_matching_entry(int pattern_i, 1435 const constantPoolHandle& search_cp, TRAPS) { 1436 1437 // index zero (0) is not used 1438 for (int i = 1; i < search_cp->length(); i++) { 1439 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0); 1440 if (found) { 1441 return i; 1442 } 1443 } 1444 1445 return 0; // entry not found; return unused index zero (0) 1446 } // end find_matching_entry() 1447 1448 1449 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool 1450 // cp2's bootstrap specifier at idx2. 1451 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2, TRAPS) { 1452 int k1 = operand_bootstrap_method_ref_index_at(idx1); 1453 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2); 1454 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 1455 1456 if (!match) { 1457 return false; 1458 } 1459 int argc = operand_argument_count_at(idx1); 1460 if (argc == cp2->operand_argument_count_at(idx2)) { 1461 for (int j = 0; j < argc; j++) { 1462 k1 = operand_argument_index_at(idx1, j); 1463 k2 = cp2->operand_argument_index_at(idx2, j); 1464 match = compare_entry_to(k1, cp2, k2, CHECK_false); 1465 if (!match) { 1466 return false; 1467 } 1468 } 1469 return true; // got through loop; all elements equal 1470 } 1471 return false; 1472 } // end compare_operand_to() 1473 1474 // Search constant pool search_cp for a bootstrap specifier that matches 1475 // this constant pool's bootstrap specifier at pattern_i index. 1476 // Return the index of a matching bootstrap specifier or (-1) if there is no match. 1477 int ConstantPool::find_matching_operand(int pattern_i, 1478 const constantPoolHandle& search_cp, int search_len, TRAPS) { 1479 for (int i = 0; i < search_len; i++) { 1480 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1)); 1481 if (found) { 1482 return i; 1483 } 1484 } 1485 return -1; // bootstrap specifier not found; return unused index (-1) 1486 } // end find_matching_operand() 1487 1488 1489 #ifndef PRODUCT 1490 1491 const char* ConstantPool::printable_name_at(int which) { 1492 1493 constantTag tag = tag_at(which); 1494 1495 if (tag.is_string()) { 1496 return string_at_noresolve(which); 1497 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 1498 return klass_name_at(which)->as_C_string(); 1499 } else if (tag.is_symbol()) { 1500 return symbol_at(which)->as_C_string(); 1501 } 1502 return ""; 1503 } 1504 1505 #endif // PRODUCT 1506 1507 1508 // JVMTI GetConstantPool support 1509 1510 // For debugging of constant pool 1511 const bool debug_cpool = false; 1512 1513 #define DBG(code) do { if (debug_cpool) { (code); } } while(0) 1514 1515 static void print_cpool_bytes(jint cnt, u1 *bytes) { 1516 const char* WARN_MSG = "Must not be such entry!"; 1517 jint size = 0; 1518 u2 idx1, idx2; 1519 1520 for (jint idx = 1; idx < cnt; idx++) { 1521 jint ent_size = 0; 1522 u1 tag = *bytes++; 1523 size++; // count tag 1524 1525 printf("const #%03d, tag: %02d ", idx, tag); 1526 switch(tag) { 1527 case JVM_CONSTANT_Invalid: { 1528 printf("Invalid"); 1529 break; 1530 } 1531 case JVM_CONSTANT_Unicode: { 1532 printf("Unicode %s", WARN_MSG); 1533 break; 1534 } 1535 case JVM_CONSTANT_Utf8: { 1536 u2 len = Bytes::get_Java_u2(bytes); 1537 char str[128]; 1538 if (len > 127) { 1539 len = 127; 1540 } 1541 strncpy(str, (char *) (bytes+2), len); 1542 str[len] = '\0'; 1543 printf("Utf8 \"%s\"", str); 1544 ent_size = 2 + len; 1545 break; 1546 } 1547 case JVM_CONSTANT_Integer: { 1548 u4 val = Bytes::get_Java_u4(bytes); 1549 printf("int %d", *(int *) &val); 1550 ent_size = 4; 1551 break; 1552 } 1553 case JVM_CONSTANT_Float: { 1554 u4 val = Bytes::get_Java_u4(bytes); 1555 printf("float %5.3ff", *(float *) &val); 1556 ent_size = 4; 1557 break; 1558 } 1559 case JVM_CONSTANT_Long: { 1560 u8 val = Bytes::get_Java_u8(bytes); 1561 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val); 1562 ent_size = 8; 1563 idx++; // Long takes two cpool slots 1564 break; 1565 } 1566 case JVM_CONSTANT_Double: { 1567 u8 val = Bytes::get_Java_u8(bytes); 1568 printf("double %5.3fd", *(jdouble *)&val); 1569 ent_size = 8; 1570 idx++; // Double takes two cpool slots 1571 break; 1572 } 1573 case JVM_CONSTANT_Class: { 1574 idx1 = Bytes::get_Java_u2(bytes); 1575 printf("class #%03d", idx1); 1576 ent_size = 2; 1577 break; 1578 } 1579 case JVM_CONSTANT_String: { 1580 idx1 = Bytes::get_Java_u2(bytes); 1581 printf("String #%03d", idx1); 1582 ent_size = 2; 1583 break; 1584 } 1585 case JVM_CONSTANT_Fieldref: { 1586 idx1 = Bytes::get_Java_u2(bytes); 1587 idx2 = Bytes::get_Java_u2(bytes+2); 1588 printf("Field #%03d, #%03d", (int) idx1, (int) idx2); 1589 ent_size = 4; 1590 break; 1591 } 1592 case JVM_CONSTANT_Methodref: { 1593 idx1 = Bytes::get_Java_u2(bytes); 1594 idx2 = Bytes::get_Java_u2(bytes+2); 1595 printf("Method #%03d, #%03d", idx1, idx2); 1596 ent_size = 4; 1597 break; 1598 } 1599 case JVM_CONSTANT_InterfaceMethodref: { 1600 idx1 = Bytes::get_Java_u2(bytes); 1601 idx2 = Bytes::get_Java_u2(bytes+2); 1602 printf("InterfMethod #%03d, #%03d", idx1, idx2); 1603 ent_size = 4; 1604 break; 1605 } 1606 case JVM_CONSTANT_NameAndType: { 1607 idx1 = Bytes::get_Java_u2(bytes); 1608 idx2 = Bytes::get_Java_u2(bytes+2); 1609 printf("NameAndType #%03d, #%03d", idx1, idx2); 1610 ent_size = 4; 1611 break; 1612 } 1613 case JVM_CONSTANT_ClassIndex: { 1614 printf("ClassIndex %s", WARN_MSG); 1615 break; 1616 } 1617 case JVM_CONSTANT_UnresolvedClass: { 1618 printf("UnresolvedClass: %s", WARN_MSG); 1619 break; 1620 } 1621 case JVM_CONSTANT_UnresolvedClassInError: { 1622 printf("UnresolvedClassInErr: %s", WARN_MSG); 1623 break; 1624 } 1625 case JVM_CONSTANT_StringIndex: { 1626 printf("StringIndex: %s", WARN_MSG); 1627 break; 1628 } 1629 } 1630 printf(";\n"); 1631 bytes += ent_size; 1632 size += ent_size; 1633 } 1634 printf("Cpool size: %d\n", size); 1635 fflush(0); 1636 return; 1637 } /* end print_cpool_bytes */ 1638 1639 1640 // Returns size of constant pool entry. 1641 jint ConstantPool::cpool_entry_size(jint idx) { 1642 switch(tag_at(idx).value()) { 1643 case JVM_CONSTANT_Invalid: 1644 case JVM_CONSTANT_Unicode: 1645 return 1; 1646 1647 case JVM_CONSTANT_Utf8: 1648 return 3 + symbol_at(idx)->utf8_length(); 1649 1650 case JVM_CONSTANT_Class: 1651 case JVM_CONSTANT_String: 1652 case JVM_CONSTANT_ClassIndex: 1653 case JVM_CONSTANT_UnresolvedClass: 1654 case JVM_CONSTANT_UnresolvedClassInError: 1655 case JVM_CONSTANT_StringIndex: 1656 case JVM_CONSTANT_MethodType: 1657 case JVM_CONSTANT_MethodTypeInError: 1658 return 3; 1659 1660 case JVM_CONSTANT_MethodHandle: 1661 case JVM_CONSTANT_MethodHandleInError: 1662 return 4; //tag, ref_kind, ref_index 1663 1664 case JVM_CONSTANT_Integer: 1665 case JVM_CONSTANT_Float: 1666 case JVM_CONSTANT_Fieldref: 1667 case JVM_CONSTANT_Methodref: 1668 case JVM_CONSTANT_InterfaceMethodref: 1669 case JVM_CONSTANT_NameAndType: 1670 return 5; 1671 1672 case JVM_CONSTANT_InvokeDynamic: 1673 case JVM_CONSTANT_InvokeDynamicInError: 1674 // u1 tag, u2 bsm, u2 nt 1675 return 5; 1676 1677 case JVM_CONSTANT_Long: 1678 case JVM_CONSTANT_Double: 1679 return 9; 1680 } 1681 assert(false, "cpool_entry_size: Invalid constant pool entry tag"); 1682 return 1; 1683 } /* end cpool_entry_size */ 1684 1685 1686 // SymbolHashMap is used to find a constant pool index from a string. 1687 // This function fills in SymbolHashMaps, one for utf8s and one for 1688 // class names, returns size of the cpool raw bytes. 1689 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap, 1690 SymbolHashMap *classmap) { 1691 jint size = 0; 1692 1693 for (u2 idx = 1; idx < length(); idx++) { 1694 u2 tag = tag_at(idx).value(); 1695 size += cpool_entry_size(idx); 1696 1697 switch(tag) { 1698 case JVM_CONSTANT_Utf8: { 1699 Symbol* sym = symbol_at(idx); 1700 symmap->add_entry(sym, idx); 1701 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx)); 1702 break; 1703 } 1704 case JVM_CONSTANT_Class: 1705 case JVM_CONSTANT_UnresolvedClass: 1706 case JVM_CONSTANT_UnresolvedClassInError: { 1707 Symbol* sym = klass_name_at(idx); 1708 classmap->add_entry(sym, idx); 1709 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx)); 1710 break; 1711 } 1712 case JVM_CONSTANT_Long: 1713 case JVM_CONSTANT_Double: { 1714 idx++; // Both Long and Double take two cpool slots 1715 break; 1716 } 1717 } 1718 } 1719 return size; 1720 } /* end hash_utf8_entries_to */ 1721 1722 1723 // Copy cpool bytes. 1724 // Returns: 1725 // 0, in case of OutOfMemoryError 1726 // -1, in case of internal error 1727 // > 0, count of the raw cpool bytes that have been copied 1728 int ConstantPool::copy_cpool_bytes(int cpool_size, 1729 SymbolHashMap* tbl, 1730 unsigned char *bytes) { 1731 u2 idx1, idx2; 1732 jint size = 0; 1733 jint cnt = length(); 1734 unsigned char *start_bytes = bytes; 1735 1736 for (jint idx = 1; idx < cnt; idx++) { 1737 u1 tag = tag_at(idx).value(); 1738 jint ent_size = cpool_entry_size(idx); 1739 1740 assert(size + ent_size <= cpool_size, "Size mismatch"); 1741 1742 *bytes = tag; 1743 DBG(printf("#%03hd tag=%03hd, ", idx, tag)); 1744 switch(tag) { 1745 case JVM_CONSTANT_Invalid: { 1746 DBG(printf("JVM_CONSTANT_Invalid")); 1747 break; 1748 } 1749 case JVM_CONSTANT_Unicode: { 1750 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode"); 1751 DBG(printf("JVM_CONSTANT_Unicode")); 1752 break; 1753 } 1754 case JVM_CONSTANT_Utf8: { 1755 Symbol* sym = symbol_at(idx); 1756 char* str = sym->as_utf8(); 1757 // Warning! It's crashing on x86 with len = sym->utf8_length() 1758 int len = (int) strlen(str); 1759 Bytes::put_Java_u2((address) (bytes+1), (u2) len); 1760 for (int i = 0; i < len; i++) { 1761 bytes[3+i] = (u1) str[i]; 1762 } 1763 DBG(printf("JVM_CONSTANT_Utf8: %s ", str)); 1764 break; 1765 } 1766 case JVM_CONSTANT_Integer: { 1767 jint val = int_at(idx); 1768 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 1769 break; 1770 } 1771 case JVM_CONSTANT_Float: { 1772 jfloat val = float_at(idx); 1773 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 1774 break; 1775 } 1776 case JVM_CONSTANT_Long: { 1777 jlong val = long_at(idx); 1778 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 1779 idx++; // Long takes two cpool slots 1780 break; 1781 } 1782 case JVM_CONSTANT_Double: { 1783 jdouble val = double_at(idx); 1784 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 1785 idx++; // Double takes two cpool slots 1786 break; 1787 } 1788 case JVM_CONSTANT_Class: 1789 case JVM_CONSTANT_UnresolvedClass: 1790 case JVM_CONSTANT_UnresolvedClassInError: { 1791 *bytes = JVM_CONSTANT_Class; 1792 Symbol* sym = klass_name_at(idx); 1793 idx1 = tbl->symbol_to_value(sym); 1794 assert(idx1 != 0, "Have not found a hashtable entry"); 1795 Bytes::put_Java_u2((address) (bytes+1), idx1); 1796 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8())); 1797 break; 1798 } 1799 case JVM_CONSTANT_String: { 1800 *bytes = JVM_CONSTANT_String; 1801 Symbol* sym = unresolved_string_at(idx); 1802 idx1 = tbl->symbol_to_value(sym); 1803 assert(idx1 != 0, "Have not found a hashtable entry"); 1804 Bytes::put_Java_u2((address) (bytes+1), idx1); 1805 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8())); 1806 break; 1807 } 1808 case JVM_CONSTANT_Fieldref: 1809 case JVM_CONSTANT_Methodref: 1810 case JVM_CONSTANT_InterfaceMethodref: { 1811 idx1 = uncached_klass_ref_index_at(idx); 1812 idx2 = uncached_name_and_type_ref_index_at(idx); 1813 Bytes::put_Java_u2((address) (bytes+1), idx1); 1814 Bytes::put_Java_u2((address) (bytes+3), idx2); 1815 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2)); 1816 break; 1817 } 1818 case JVM_CONSTANT_NameAndType: { 1819 idx1 = name_ref_index_at(idx); 1820 idx2 = signature_ref_index_at(idx); 1821 Bytes::put_Java_u2((address) (bytes+1), idx1); 1822 Bytes::put_Java_u2((address) (bytes+3), idx2); 1823 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2)); 1824 break; 1825 } 1826 case JVM_CONSTANT_ClassIndex: { 1827 *bytes = JVM_CONSTANT_Class; 1828 idx1 = klass_index_at(idx); 1829 Bytes::put_Java_u2((address) (bytes+1), idx1); 1830 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1)); 1831 break; 1832 } 1833 case JVM_CONSTANT_StringIndex: { 1834 *bytes = JVM_CONSTANT_String; 1835 idx1 = string_index_at(idx); 1836 Bytes::put_Java_u2((address) (bytes+1), idx1); 1837 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1)); 1838 break; 1839 } 1840 case JVM_CONSTANT_MethodHandle: 1841 case JVM_CONSTANT_MethodHandleInError: { 1842 *bytes = JVM_CONSTANT_MethodHandle; 1843 int kind = method_handle_ref_kind_at(idx); 1844 idx1 = method_handle_index_at(idx); 1845 *(bytes+1) = (unsigned char) kind; 1846 Bytes::put_Java_u2((address) (bytes+2), idx1); 1847 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1)); 1848 break; 1849 } 1850 case JVM_CONSTANT_MethodType: 1851 case JVM_CONSTANT_MethodTypeInError: { 1852 *bytes = JVM_CONSTANT_MethodType; 1853 idx1 = method_type_index_at(idx); 1854 Bytes::put_Java_u2((address) (bytes+1), idx1); 1855 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1)); 1856 break; 1857 } 1858 case JVM_CONSTANT_InvokeDynamic: 1859 case JVM_CONSTANT_InvokeDynamicInError: { 1860 *bytes = tag; 1861 idx1 = extract_low_short_from_int(*int_at_addr(idx)); 1862 idx2 = extract_high_short_from_int(*int_at_addr(idx)); 1863 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4"); 1864 Bytes::put_Java_u2((address) (bytes+1), idx1); 1865 Bytes::put_Java_u2((address) (bytes+3), idx2); 1866 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2)); 1867 break; 1868 } 1869 } 1870 DBG(printf("\n")); 1871 bytes += ent_size; 1872 size += ent_size; 1873 } 1874 assert(size == cpool_size, "Size mismatch"); 1875 1876 // Keep temorarily for debugging until it's stable. 1877 DBG(print_cpool_bytes(cnt, start_bytes)); 1878 return (int)(bytes - start_bytes); 1879 } /* end copy_cpool_bytes */ 1880 1881 #undef DBG 1882 1883 1884 void ConstantPool::set_on_stack(const bool value) { 1885 if (value) { 1886 // Only record if it's not already set. 1887 if (!on_stack()) { 1888 assert(!is_shared(), "should always be set for shared constant pools"); 1889 _flags |= _on_stack; 1890 MetadataOnStackMark::record(this); 1891 } 1892 } else { 1893 // Clearing is done single-threadedly. 1894 if (!is_shared()) { 1895 _flags &= ~_on_stack; 1896 } 1897 } 1898 } 1899 1900 // JSR 292 support for patching constant pool oops after the class is linked and 1901 // the oop array for resolved references are created. 1902 // We can't do this during classfile parsing, which is how the other indexes are 1903 // patched. The other patches are applied early for some error checking 1904 // so only defer the pseudo_strings. 1905 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) { 1906 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused 1907 Handle patch = cp_patches->at(index); 1908 if (patch.not_null()) { 1909 assert (tag_at(index).is_string(), "should only be string left"); 1910 // Patching a string means pre-resolving it. 1911 // The spelling in the constant pool is ignored. 1912 // The constant reference may be any object whatever. 1913 // If it is not a real interned string, the constant is referred 1914 // to as a "pseudo-string", and must be presented to the CP 1915 // explicitly, because it may require scavenging. 1916 int obj_index = cp_to_object_index(index); 1917 pseudo_string_at_put(index, obj_index, patch()); 1918 DEBUG_ONLY(cp_patches->at_put(index, Handle());) 1919 } 1920 } 1921 #ifdef ASSERT 1922 // Ensure that all the patches have been used. 1923 for (int index = 0; index < cp_patches->length(); index++) { 1924 assert(cp_patches->at(index).is_null(), 1925 "Unused constant pool patch at %d in class file %s", 1926 index, 1927 pool_holder()->external_name()); 1928 } 1929 #endif // ASSERT 1930 } 1931 1932 #ifndef PRODUCT 1933 1934 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool 1935 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) { 1936 guarantee(obj->is_constantPool(), "object must be constant pool"); 1937 constantPoolHandle cp(THREAD, (ConstantPool*)obj); 1938 guarantee(cp->pool_holder() != NULL, "must be fully loaded"); 1939 1940 for (int i = 0; i< cp->length(); i++) { 1941 if (cp->tag_at(i).is_unresolved_klass()) { 1942 // This will force loading of the class 1943 Klass* klass = cp->klass_at(i, CHECK); 1944 if (klass->is_instance_klass()) { 1945 // Force initialization of class 1946 InstanceKlass::cast(klass)->initialize(CHECK); 1947 } 1948 } 1949 } 1950 } 1951 1952 #endif 1953 1954 1955 // Printing 1956 1957 void ConstantPool::print_on(outputStream* st) const { 1958 assert(is_constantPool(), "must be constantPool"); 1959 st->print_cr("%s", internal_name()); 1960 if (flags() != 0) { 1961 st->print(" - flags: 0x%x", flags()); 1962 if (has_preresolution()) st->print(" has_preresolution"); 1963 if (on_stack()) st->print(" on_stack"); 1964 st->cr(); 1965 } 1966 if (pool_holder() != NULL) { 1967 st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder())); 1968 } 1969 st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache())); 1970 st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references())); 1971 st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map())); 1972 st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses())); 1973 1974 for (int index = 1; index < length(); index++) { // Index 0 is unused 1975 ((ConstantPool*)this)->print_entry_on(index, st); 1976 switch (tag_at(index).value()) { 1977 case JVM_CONSTANT_Long : 1978 case JVM_CONSTANT_Double : 1979 index++; // Skip entry following eigth-byte constant 1980 } 1981 1982 } 1983 st->cr(); 1984 } 1985 1986 // Print one constant pool entry 1987 void ConstantPool::print_entry_on(const int index, outputStream* st) { 1988 EXCEPTION_MARK; 1989 st->print(" - %3d : ", index); 1990 tag_at(index).print_on(st); 1991 st->print(" : "); 1992 switch (tag_at(index).value()) { 1993 case JVM_CONSTANT_Class : 1994 { Klass* k = klass_at(index, CATCH); 1995 guarantee(k != NULL, "need klass"); 1996 k->print_value_on(st); 1997 st->print(" {" PTR_FORMAT "}", p2i(k)); 1998 } 1999 break; 2000 case JVM_CONSTANT_Fieldref : 2001 case JVM_CONSTANT_Methodref : 2002 case JVM_CONSTANT_InterfaceMethodref : 2003 st->print("klass_index=%d", uncached_klass_ref_index_at(index)); 2004 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index)); 2005 break; 2006 case JVM_CONSTANT_String : 2007 if (is_pseudo_string_at(index)) { 2008 oop anObj = pseudo_string_at(index); 2009 anObj->print_value_on(st); 2010 st->print(" {" PTR_FORMAT "}", p2i(anObj)); 2011 } else { 2012 unresolved_string_at(index)->print_value_on(st); 2013 } 2014 break; 2015 case JVM_CONSTANT_Integer : 2016 st->print("%d", int_at(index)); 2017 break; 2018 case JVM_CONSTANT_Float : 2019 st->print("%f", float_at(index)); 2020 break; 2021 case JVM_CONSTANT_Long : 2022 st->print_jlong(long_at(index)); 2023 break; 2024 case JVM_CONSTANT_Double : 2025 st->print("%lf", double_at(index)); 2026 break; 2027 case JVM_CONSTANT_NameAndType : 2028 st->print("name_index=%d", name_ref_index_at(index)); 2029 st->print(" signature_index=%d", signature_ref_index_at(index)); 2030 break; 2031 case JVM_CONSTANT_Utf8 : 2032 symbol_at(index)->print_value_on(st); 2033 break; 2034 case JVM_CONSTANT_ClassIndex: { 2035 int name_index = *int_at_addr(index); 2036 st->print("klass_index=%d ", name_index); 2037 symbol_at(name_index)->print_value_on(st); 2038 } 2039 break; 2040 case JVM_CONSTANT_UnresolvedClass : // fall-through 2041 case JVM_CONSTANT_UnresolvedClassInError: { 2042 CPKlassSlot kslot = klass_slot_at(index); 2043 int resolved_klass_index = kslot.resolved_klass_index(); 2044 int name_index = kslot.name_index(); 2045 assert(tag_at(name_index).is_symbol(), "sanity"); 2046 2047 Klass* klass = resolved_klasses()->at(resolved_klass_index); 2048 if (klass != NULL) { 2049 klass->print_value_on(st); 2050 } else { 2051 symbol_at(name_index)->print_value_on(st); 2052 } 2053 } 2054 break; 2055 case JVM_CONSTANT_MethodHandle : 2056 case JVM_CONSTANT_MethodHandleInError : 2057 st->print("ref_kind=%d", method_handle_ref_kind_at(index)); 2058 st->print(" ref_index=%d", method_handle_index_at(index)); 2059 break; 2060 case JVM_CONSTANT_MethodType : 2061 case JVM_CONSTANT_MethodTypeInError : 2062 st->print("signature_index=%d", method_type_index_at(index)); 2063 break; 2064 case JVM_CONSTANT_InvokeDynamic : 2065 case JVM_CONSTANT_InvokeDynamicInError : 2066 { 2067 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index)); 2068 st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index)); 2069 int argc = invoke_dynamic_argument_count_at(index); 2070 if (argc > 0) { 2071 for (int arg_i = 0; arg_i < argc; arg_i++) { 2072 int arg = invoke_dynamic_argument_index_at(index, arg_i); 2073 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 2074 } 2075 st->print("}"); 2076 } 2077 } 2078 break; 2079 default: 2080 ShouldNotReachHere(); 2081 break; 2082 } 2083 st->cr(); 2084 } 2085 2086 void ConstantPool::print_value_on(outputStream* st) const { 2087 assert(is_constantPool(), "must be constantPool"); 2088 st->print("constant pool [%d]", length()); 2089 if (has_preresolution()) st->print("/preresolution"); 2090 if (operands() != NULL) st->print("/operands[%d]", operands()->length()); 2091 print_address_on(st); 2092 st->print(" for "); 2093 pool_holder()->print_value_on(st); 2094 if (pool_holder() != NULL) { 2095 bool extra = (pool_holder()->constants() != this); 2096 if (extra) st->print(" (extra)"); 2097 } 2098 if (cache() != NULL) { 2099 st->print(" cache=" PTR_FORMAT, p2i(cache())); 2100 } 2101 } 2102 2103 #if INCLUDE_SERVICES 2104 // Size Statistics 2105 void ConstantPool::collect_statistics(KlassSizeStats *sz) const { 2106 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this)); 2107 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags())); 2108 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache())); 2109 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands())); 2110 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map())); 2111 2112 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes + 2113 sz->_cp_refmap_bytes; 2114 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes; 2115 } 2116 #endif // INCLUDE_SERVICES 2117 2118 // Verification 2119 2120 void ConstantPool::verify_on(outputStream* st) { 2121 guarantee(is_constantPool(), "object must be constant pool"); 2122 for (int i = 0; i< length(); i++) { 2123 constantTag tag = tag_at(i); 2124 if (tag.is_klass() || tag.is_unresolved_klass()) { 2125 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count"); 2126 } else if (tag.is_symbol()) { 2127 CPSlot entry = slot_at(i); 2128 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2129 } else if (tag.is_string()) { 2130 CPSlot entry = slot_at(i); 2131 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2132 } 2133 } 2134 if (cache() != NULL) { 2135 // Note: cache() can be NULL before a class is completely setup or 2136 // in temporary constant pools used during constant pool merging 2137 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache"); 2138 } 2139 if (pool_holder() != NULL) { 2140 // Note: pool_holder() can be NULL in temporary constant pools 2141 // used during constant pool merging 2142 guarantee(pool_holder()->is_klass(), "should be klass"); 2143 } 2144 } 2145 2146 2147 void SymbolHashMap::add_entry(Symbol* sym, u2 value) { 2148 char *str = sym->as_utf8(); 2149 unsigned int hash = compute_hash(str, sym->utf8_length()); 2150 unsigned int index = hash % table_size(); 2151 2152 // check if already in map 2153 // we prefer the first entry since it is more likely to be what was used in 2154 // the class file 2155 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2156 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2157 if (en->hash() == hash && en->symbol() == sym) { 2158 return; // already there 2159 } 2160 } 2161 2162 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value); 2163 entry->set_next(bucket(index)); 2164 _buckets[index].set_entry(entry); 2165 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2166 } 2167 2168 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) { 2169 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL"); 2170 char *str = sym->as_utf8(); 2171 int len = sym->utf8_length(); 2172 unsigned int hash = SymbolHashMap::compute_hash(str, len); 2173 unsigned int index = hash % table_size(); 2174 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2175 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2176 if (en->hash() == hash && en->symbol() == sym) { 2177 return en; 2178 } 2179 } 2180 return NULL; 2181 }