16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/classLoaderData.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/metadataOnStackMark.hpp" 29 #include "classfile/stringTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/vmSymbols.hpp" 32 #include "interpreter/linkResolver.hpp" 33 #include "memory/heapInspection.hpp" 34 #include "memory/metadataFactory.hpp" 35 #include "memory/metaspaceClosure.hpp" 36 #include "memory/oopFactory.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/constantPool.hpp" 39 #include "oops/instanceKlass.hpp" 40 #include "oops/objArrayKlass.hpp" 41 #include "oops/objArrayOop.inline.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "prims/jvm.h" 44 #include "runtime/fieldType.hpp" 45 #include "runtime/init.hpp" 46 #include "runtime/javaCalls.hpp" 47 #include "runtime/signature.hpp" 48 #include "runtime/vframe.hpp" 49 #include "utilities/copy.hpp" 50 51 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) { 52 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL); 53 int size = ConstantPool::size(length); 54 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags); 55 } 56 57 #ifdef ASSERT 58 59 // MetaspaceObj allocation invariant is calloc equivalent memory 60 // simple verification of this here (JVM_CONSTANT_Invalid == 0 ) 61 static bool tag_array_is_zero_initialized(Array<u1>* tags) { 62 assert(tags != NULL, "invariant"); 63 const int length = tags->length(); 64 for (int index = 0; index < length; ++index) { 65 if (JVM_CONSTANT_Invalid != tags->at(index)) { 66 return false; 67 } 68 } 69 return true; 221 if (k != NULL) { 222 release_tag_at_put(class_index, JVM_CONSTANT_Class); 223 } else { 224 release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass); 225 } 226 } 227 228 // Anonymous class support: 229 void ConstantPool::klass_at_put(int class_index, Klass* k) { 230 assert(k != NULL, "must be valid klass"); 231 CPKlassSlot kslot = klass_slot_at(class_index); 232 int resolved_klass_index = kslot.resolved_klass_index(); 233 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index); 234 OrderAccess::release_store_ptr((Klass* volatile *)adr, k); 235 236 // The interpreter assumes when the tag is stored, the klass is resolved 237 // and the Klass* non-NULL, so we need hardware store ordering here. 238 release_tag_at_put(class_index, JVM_CONSTANT_Class); 239 } 240 241 // CDS support. Create a new resolved_references array. 242 void ConstantPool::restore_unshareable_info(TRAPS) { 243 assert(is_constantPool(), "ensure C++ vtable is restored"); 244 assert(on_stack(), "should always be set for shared constant pools"); 245 assert(is_shared(), "should always be set for shared constant pools"); 246 247 // Only create the new resolved references array if it hasn't been attempted before 248 if (resolved_references() != NULL) return; 249 250 // restore the C++ vtable from the shared archive 251 restore_vtable(); 252 253 if (SystemDictionary::Object_klass_loaded()) { 254 // Recreate the object array and add to ClassLoaderData. 255 int map_length = resolved_reference_length(); 256 if (map_length > 0) { 257 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); 258 Handle refs_handle (THREAD, (oop)stom); // must handleize. 259 260 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 261 set_resolved_references(loader_data->add_handle(refs_handle)); 262 } 263 } 264 } 265 266 void ConstantPool::remove_unshareable_info() { 267 // Resolved references are not in the shared archive. 268 // Save the length for restoration. It is not necessarily the same length 269 // as reference_map.length() if invokedynamic is saved. 270 set_resolved_reference_length( 271 resolved_references() != NULL ? resolved_references()->length() : 0); 272 set_resolved_references(NULL); 273 274 // Shared ConstantPools are in the RO region, so the _flags cannot be modified. 275 // The _on_stack flag is used to prevent ConstantPools from deallocation during 276 // class redefinition. Since shared ConstantPools cannot be deallocated anyway, 277 // we always set _on_stack to true to avoid having to change _flags during runtime. 278 _flags |= (_on_stack | _is_shared); 279 } 280 281 int ConstantPool::cp_to_object_index(int cp_index) { 282 // this is harder don't do this so much. 283 int i = reference_map()->find(cp_index); 284 // We might not find the index for jsr292 call. 285 return (i < 0) ? _no_index_sentinel : i; 286 } 287 288 void ConstantPool::string_at_put(int which, int obj_index, oop str) { 289 resolved_references()->obj_at_put(obj_index, str); 290 } 291 292 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) { 602 return klass_at_noresolve(ref_index); 603 } 604 605 char* ConstantPool::string_at_noresolve(int which) { 606 return unresolved_string_at(which)->as_C_string(); 607 } 608 609 BasicType ConstantPool::basic_type_for_signature_at(int which) const { 610 return FieldType::basic_type(symbol_at(which)); 611 } 612 613 614 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) { 615 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused 616 if (this_cp->tag_at(index).is_string()) { 617 this_cp->string_at(index, CHECK); 618 } 619 } 620 } 621 622 // Resolve all the classes in the constant pool. If they are all resolved, 623 // the constant pool is read-only. Enhancement: allocate cp entries to 624 // another metaspace, and copy to read-only or read-write space if this 625 // bit is set. 626 bool ConstantPool::resolve_class_constants(TRAPS) { 627 constantPoolHandle cp(THREAD, this); 628 for (int index = 1; index < length(); index++) { // Index 0 is unused 629 if (tag_at(index).is_unresolved_klass() && 630 klass_at_if_loaded(cp, index) == NULL) { 631 return false; 632 } 633 } 634 // set_preresolution(); or some bit for future use 635 return true; 636 } 637 638 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) { 639 // Dig out the detailed message to reuse if possible 640 Symbol* message = java_lang_Throwable::detail_message(pending_exception); 641 if (message != NULL) { 642 return message; 643 } 644 645 // Return specific message for the tag 646 switch (tag.value()) { 647 case JVM_CONSTANT_UnresolvedClass: 648 // return the class name in the error message 649 message = this_cp->klass_name_at(which); 650 break; 651 case JVM_CONSTANT_MethodHandle: 652 // return the method handle name in the error message 653 message = this_cp->method_handle_name_ref_at(which); 654 break; | 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/classLoaderData.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/metadataOnStackMark.hpp" 29 #include "classfile/stringTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/vmSymbols.hpp" 32 #include "interpreter/linkResolver.hpp" 33 #include "memory/heapInspection.hpp" 34 #include "memory/metadataFactory.hpp" 35 #include "memory/metaspaceClosure.hpp" 36 #include "memory/metaspaceShared.hpp" 37 #include "memory/oopFactory.hpp" 38 #include "memory/resourceArea.hpp" 39 #include "oops/constantPool.hpp" 40 #include "oops/instanceKlass.hpp" 41 #include "oops/objArrayKlass.hpp" 42 #include "oops/objArrayOop.inline.hpp" 43 #include "oops/oop.inline.hpp" 44 #include "prims/jvm.h" 45 #include "runtime/fieldType.hpp" 46 #include "runtime/init.hpp" 47 #include "runtime/javaCalls.hpp" 48 #include "runtime/signature.hpp" 49 #include "runtime/vframe.hpp" 50 #include "utilities/copy.hpp" 51 #if INCLUDE_ALL_GCS 52 #include "gc/g1/g1SATBCardTableModRefBS.hpp" 53 #endif // INCLUDE_ALL_GCS 54 55 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) { 56 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL); 57 int size = ConstantPool::size(length); 58 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags); 59 } 60 61 #ifdef ASSERT 62 63 // MetaspaceObj allocation invariant is calloc equivalent memory 64 // simple verification of this here (JVM_CONSTANT_Invalid == 0 ) 65 static bool tag_array_is_zero_initialized(Array<u1>* tags) { 66 assert(tags != NULL, "invariant"); 67 const int length = tags->length(); 68 for (int index = 0; index < length; ++index) { 69 if (JVM_CONSTANT_Invalid != tags->at(index)) { 70 return false; 71 } 72 } 73 return true; 225 if (k != NULL) { 226 release_tag_at_put(class_index, JVM_CONSTANT_Class); 227 } else { 228 release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass); 229 } 230 } 231 232 // Anonymous class support: 233 void ConstantPool::klass_at_put(int class_index, Klass* k) { 234 assert(k != NULL, "must be valid klass"); 235 CPKlassSlot kslot = klass_slot_at(class_index); 236 int resolved_klass_index = kslot.resolved_klass_index(); 237 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index); 238 OrderAccess::release_store_ptr((Klass* volatile *)adr, k); 239 240 // The interpreter assumes when the tag is stored, the klass is resolved 241 // and the Klass* non-NULL, so we need hardware store ordering here. 242 release_tag_at_put(class_index, JVM_CONSTANT_Class); 243 } 244 245 #if INCLUDE_CDS_JAVA_HEAP 246 // Archive the resolved references 247 void ConstantPool::archive_resolved_references(Thread* THREAD) { 248 if (_cache == NULL) { 249 return; // nothing to do 250 } 251 252 InstanceKlass *ik = pool_holder(); 253 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() || 254 ik->is_shared_app_class())) { 255 // Archiving resolved references for classes from non-builtin loaders 256 // is not yet supported. 257 set_resolved_references(NULL); 258 return; 259 } 260 261 objArrayOop rr = resolved_references(); 262 if (rr != NULL) { 263 for (int i = 0; i < rr->length(); i++) { 264 oop p = rr->obj_at(i); 265 if (p != NULL) { 266 int index = object_to_cp_index(i); 267 if (tag_at(index).is_string()) { 268 oop op = StringTable::create_archived_string(p, THREAD); 269 // If the String object is not archived (possibly too large), 270 // NULL is returned. Also set it in the array, so we won't 271 // have a 'bad' reference in the archived resolved_reference 272 // array. 273 rr->obj_at_put(i, op); 274 } else { 275 rr->obj_at_put(i, NULL); 276 } 277 } 278 } 279 oop archived = MetaspaceShared::archive_heap_object(rr, THREAD); 280 _cache->set_archived_references(archived); 281 set_resolved_references(NULL); 282 } 283 } 284 #endif 285 286 // CDS support. Create a new resolved_references array. 287 void ConstantPool::restore_unshareable_info(TRAPS) { 288 assert(is_constantPool(), "ensure C++ vtable is restored"); 289 assert(on_stack(), "should always be set for shared constant pools"); 290 assert(is_shared(), "should always be set for shared constant pools"); 291 assert(_cache != NULL, "constant pool _cache should not be NULL"); 292 293 // Only create the new resolved references array if it hasn't been attempted before 294 if (resolved_references() != NULL) return; 295 296 // restore the C++ vtable from the shared archive 297 restore_vtable(); 298 299 if (SystemDictionary::Object_klass_loaded()) { 300 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 301 #if INCLUDE_CDS_JAVA_HEAP 302 if (MetaspaceShared::open_archive_heap_region_mapped() && 303 _cache->archived_references() != NULL) { 304 oop archived = _cache->archived_references(); 305 // Make sure GC knows the cached object is now live. This is necessary after 306 // initial GC marking and during concurrent marking as strong roots are only 307 // scanned during initial marking (at the start of the GC marking). 308 assert(UseG1GC, "Requires G1 GC"); 309 G1SATBCardTableModRefBS::enqueue(archived); 310 // Create handle for the archived resolved reference array object 311 Handle refs_handle(THREAD, (oop)archived); 312 set_resolved_references(loader_data->add_handle(refs_handle)); 313 } else 314 #endif 315 { 316 // No mapped archived resolved reference array 317 // Recreate the object array and add to ClassLoaderData. 318 int map_length = resolved_reference_length(); 319 if (map_length > 0) { 320 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); 321 Handle refs_handle(THREAD, (oop)stom); // must handleize. 322 set_resolved_references(loader_data->add_handle(refs_handle)); 323 } 324 } 325 } 326 } 327 328 void ConstantPool::remove_unshareable_info() { 329 // Resolved references are not in the shared archive. 330 // Save the length for restoration. It is not necessarily the same length 331 // as reference_map.length() if invokedynamic is saved. It is needed when 332 // re-creating the resolved reference array if archived heap data cannot be map 333 // at runtime. 334 set_resolved_reference_length( 335 resolved_references() != NULL ? resolved_references()->length() : 0); 336 337 // If archiving heap objects is not allowed, clear the resolved references. 338 // Otherwise, it is cleared after the resolved references array is cached 339 // (see archive_resolved_references()). 340 if (!MetaspaceShared::is_heap_object_archiving_allowed()) { 341 set_resolved_references(NULL); 342 } 343 344 // Shared ConstantPools are in the RO region, so the _flags cannot be modified. 345 // The _on_stack flag is used to prevent ConstantPools from deallocation during 346 // class redefinition. Since shared ConstantPools cannot be deallocated anyway, 347 // we always set _on_stack to true to avoid having to change _flags during runtime. 348 _flags |= (_on_stack | _is_shared); 349 } 350 351 int ConstantPool::cp_to_object_index(int cp_index) { 352 // this is harder don't do this so much. 353 int i = reference_map()->find(cp_index); 354 // We might not find the index for jsr292 call. 355 return (i < 0) ? _no_index_sentinel : i; 356 } 357 358 void ConstantPool::string_at_put(int which, int obj_index, oop str) { 359 resolved_references()->obj_at_put(obj_index, str); 360 } 361 362 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) { 672 return klass_at_noresolve(ref_index); 673 } 674 675 char* ConstantPool::string_at_noresolve(int which) { 676 return unresolved_string_at(which)->as_C_string(); 677 } 678 679 BasicType ConstantPool::basic_type_for_signature_at(int which) const { 680 return FieldType::basic_type(symbol_at(which)); 681 } 682 683 684 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) { 685 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused 686 if (this_cp->tag_at(index).is_string()) { 687 this_cp->string_at(index, CHECK); 688 } 689 } 690 } 691 692 bool ConstantPool::resolve_class_constants(TRAPS) { 693 constantPoolHandle cp(THREAD, this); 694 for (int index = 1; index < length(); index++) { // Index 0 is unused 695 if (tag_at(index).is_string()) { 696 Symbol* sym = cp->unresolved_string_at(index); 697 // Look up only. Only resolve references to already interned strings. 698 oop str = StringTable::lookup(sym); 699 if (str != NULL) { 700 int cache_index = cp->cp_to_object_index(index); 701 cp->string_at_put(index, cache_index, str); 702 } 703 } 704 } 705 return true; 706 } 707 708 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) { 709 // Dig out the detailed message to reuse if possible 710 Symbol* message = java_lang_Throwable::detail_message(pending_exception); 711 if (message != NULL) { 712 return message; 713 } 714 715 // Return specific message for the tag 716 switch (tag.value()) { 717 case JVM_CONSTANT_UnresolvedClass: 718 // return the class name in the error message 719 message = this_cp->klass_name_at(which); 720 break; 721 case JVM_CONSTANT_MethodHandle: 722 // return the method handle name in the error message 723 message = this_cp->method_handle_name_ref_at(which); 724 break; |