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