< prev index next >

src/hotspot/share/classfile/javaClasses.cpp

Print this page

  36 #include "classfile/moduleEntry.hpp"
  37 #include "classfile/stringTable.hpp"
  38 #include "classfile/symbolTable.hpp"
  39 #include "classfile/systemDictionary.hpp"
  40 #include "classfile/vmClasses.hpp"
  41 #include "classfile/vmSymbols.hpp"
  42 #include "code/debugInfo.hpp"
  43 #include "code/dependencyContext.hpp"
  44 #include "code/pcDesc.hpp"
  45 #include "gc/shared/collectedHeap.inline.hpp"
  46 #include "interpreter/interpreter.hpp"
  47 #include "interpreter/linkResolver.hpp"
  48 #include "jvm.h"
  49 #include "logging/log.hpp"
  50 #include "logging/logStream.hpp"
  51 #include "memory/oopFactory.hpp"
  52 #include "memory/resourceArea.hpp"
  53 #include "memory/universe.hpp"
  54 #include "oops/fieldInfo.hpp"
  55 #include "oops/fieldStreams.inline.hpp"


  56 #include "oops/instanceKlass.inline.hpp"
  57 #include "oops/instanceMirrorKlass.hpp"
  58 #include "oops/klass.hpp"
  59 #include "oops/klass.inline.hpp"
  60 #include "oops/method.inline.hpp"
  61 #include "oops/objArrayKlass.hpp"
  62 #include "oops/objArrayOop.inline.hpp"
  63 #include "oops/oopCast.inline.hpp"
  64 #include "oops/oop.inline.hpp"
  65 #include "oops/symbol.hpp"
  66 #include "oops/recordComponent.hpp"
  67 #include "oops/typeArrayOop.inline.hpp"
  68 #include "prims/jvmtiExport.hpp"
  69 #include "prims/methodHandles.hpp"
  70 #include "prims/resolvedMethodTable.hpp"
  71 #include "runtime/continuationEntry.inline.hpp"
  72 #include "runtime/continuationJavaClasses.inline.hpp"
  73 #include "runtime/fieldDescriptor.inline.hpp"
  74 #include "runtime/frame.inline.hpp"
  75 #include "runtime/handles.inline.hpp"
  76 #include "runtime/handshake.hpp"
  77 #include "runtime/init.hpp"

 771                              ((jchar) value->byte_at(index)) & 0xff;
 772     if (c < ' ') {
 773       st->print("\\x%02X", c); // print control characters e.g. \x0A
 774     } else {
 775       st->print("%c", c);
 776     }
 777   }
 778   st->print("\"");
 779 }
 780 
 781 // java_lang_Class
 782 
 783 int java_lang_Class::_klass_offset;
 784 int java_lang_Class::_array_klass_offset;
 785 int java_lang_Class::_oop_size_offset;
 786 int java_lang_Class::_static_oop_field_count_offset;
 787 int java_lang_Class::_class_loader_offset;
 788 int java_lang_Class::_module_offset;
 789 int java_lang_Class::_protection_domain_offset;
 790 int java_lang_Class::_component_mirror_offset;

 791 int java_lang_Class::_signers_offset;
 792 int java_lang_Class::_name_offset;
 793 int java_lang_Class::_source_file_offset;
 794 int java_lang_Class::_classData_offset;
 795 int java_lang_Class::_classRedefinedCount_offset;
 796 
 797 bool java_lang_Class::_offsets_computed = false;
 798 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = nullptr;
 799 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = nullptr;
 800 
 801 #ifdef ASSERT
 802 inline static void assert_valid_static_string_field(fieldDescriptor* fd) {
 803   assert(fd->has_initial_value(), "caller should have checked this");
 804   assert(fd->field_type() == T_OBJECT, "caller should have checked this");
 805   assert(fd->signature() == vmSymbols::string_signature(), "just checking");
 806 }
 807 #endif
 808 
 809 static void initialize_static_string_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
 810   DEBUG_ONLY(assert_valid_static_string_field(fd);)

 970     new (mtModule) GrowableArray<Klass*>(500, mtModule);
 971   set_fixup_module_field_list(module_list);
 972 }
 973 
 974 void java_lang_Class::allocate_mirror(Klass* k, bool is_scratch, Handle protection_domain, Handle classData,
 975                                       Handle& mirror, Handle& comp_mirror, TRAPS) {
 976   // Allocate mirror (java.lang.Class instance)
 977   oop mirror_oop = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(k, CHECK);
 978   mirror = Handle(THREAD, mirror_oop);
 979 
 980   // Setup indirection from mirror->klass
 981   set_klass(mirror(), k);
 982 
 983   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
 984   assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
 985 
 986   set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
 987 
 988   // It might also have a component mirror.  This mirror must already exist.
 989   if (k->is_array_klass()) {
 990     if (k->is_typeArray_klass()) {








 991       BasicType type = TypeArrayKlass::cast(k)->element_type();
 992       if (is_scratch) {
 993         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(type));
 994       } else {
 995         comp_mirror = Handle(THREAD, Universe::java_mirror(type));
 996       }
 997     } else {
 998       assert(k->is_objArray_klass(), "Must be");
 999       Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1000       assert(element_klass != nullptr, "Must have an element klass");





1001       if (is_scratch) {
1002         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1003       } else {
1004         comp_mirror = Handle(THREAD, element_klass->java_mirror());
1005       }
1006     }
1007     assert(comp_mirror() != nullptr, "must have a mirror");
1008 
1009     // Two-way link between the array klass and its component mirror:
1010     // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1011     set_component_mirror(mirror(), comp_mirror());
1012     // See below for ordering dependencies between field array_klass in component mirror
1013     // and java_mirror in this klass.
1014   } else {
1015     assert(k->is_instance_klass(), "Must be");
1016 
1017     initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1018     if (HAS_PENDING_EXCEPTION) {
1019       // If any of the fields throws an exception like OOM remove the klass field
1020       // from the mirror so GC doesn't follow it after the klass has been deallocated.
1021       // This mirror looks like a primitive type, which logically it is because it
1022       // it represents no class.
1023       set_klass(mirror(), nullptr);
1024       return;

1046 
1047     allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1048 
1049     // set the classLoader field in the java_lang_Class instance
1050     assert(class_loader() == k->class_loader(), "should be same");
1051     set_class_loader(mirror(), class_loader());
1052 
1053     // Setup indirection from klass->mirror
1054     // after any exceptions can happen during allocations.
1055     k->set_java_mirror(mirror);
1056 
1057     // Set the module field in the java_lang_Class instance.  This must be done
1058     // after the mirror is set.
1059     set_mirror_module_field(THREAD, k, mirror, module);
1060 
1061     if (comp_mirror() != nullptr) {
1062       // Set after k->java_mirror() is published, because compiled code running
1063       // concurrently doesn't expect a k to have a null java_mirror.
1064       release_set_array_klass(comp_mirror(), k);
1065     }

1066     if (CDSConfig::is_dumping_heap()) {
1067       create_scratch_mirror(k, CHECK);
1068     }
1069   } else {
1070     assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1071     fixup_mirror_list()->push(k);
1072   }
1073 }
1074 
1075 #if INCLUDE_CDS_JAVA_HEAP
1076 // The "scratch mirror" stores the states of the mirror object that can be
1077 // decided at dump time (such as the initial values of the static fields, the
1078 // component mirror, etc). At runtime, more information is added to it by
1079 // java_lang_Class::restore_archived_mirror().
1080 //
1081 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1082 // produces the same result as /*runtime*/create_mirror().
1083 //
1084 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1085 // latter may contain dumptime-specific information that cannot be archived
1086 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1087 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) {
1088   if (k->class_loader() != nullptr &&
1089       k->class_loader() != SystemDictionary::java_platform_loader() &&
1090       k->class_loader() != SystemDictionary::java_system_loader()) {





1091     // We only archive the mirrors of classes loaded by the built-in loaders
1092     return;
1093   }
1094 
1095   Handle protection_domain, classData; // set to null. Will be reinitialized at runtime
1096   Handle mirror;
1097   Handle comp_mirror;
1098   allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1099 
1100   if (comp_mirror() != nullptr) {
1101     release_set_array_klass(comp_mirror(), k);
1102   }
1103 
1104   HeapShared::set_scratch_java_mirror(k, mirror());
1105 }
1106 
1107 // Returns true if the mirror is updated, false if no archived mirror
1108 // data is present. After the archived mirror object is restored, the
1109 // shared klass' _has_raw_archived_mirror flag is cleared.
1110 bool java_lang_Class::restore_archived_mirror(Klass *k,

1274   assert(is_instance(java_class), "must be a Class object");
1275   java_class->metadata_field_put(_klass_offset, klass);
1276 }
1277 
1278 
1279 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1280   assert(is_instance(java_class), "must be a Class object");
1281   Symbol* name = nullptr;
1282   bool is_instance = false;
1283   if (is_primitive(java_class)) {
1284     name = vmSymbols::type_signature(primitive_type(java_class));
1285   } else {
1286     Klass* k = as_Klass(java_class);
1287     is_instance = k->is_instance_klass();
1288     name = k->name();
1289   }
1290   if (name == nullptr) {
1291     st->print("<null>");
1292     return;
1293   }
1294   if (is_instance)  st->print("L");


1295   st->write((char*) name->base(), (int) name->utf8_length());
1296   if (is_instance)  st->print(";");
1297 }
1298 
1299 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1300   assert(is_instance(java_class), "must be a Class object");
1301   Symbol* name;
1302   if (is_primitive(java_class)) {
1303     name = vmSymbols::type_signature(primitive_type(java_class));
1304     // Because this can create a new symbol, the caller has to decrement
1305     // the refcount, so make adjustment here and below for symbols returned
1306     // that are not created or incremented due to a successful lookup.
1307     name->increment_refcount();
1308   } else {
1309     Klass* k = as_Klass(java_class);
1310     if (!k->is_instance_klass()) {
1311       name = k->name();
1312       name->increment_refcount();
1313     } else {
1314       ResourceMark rm;

1333   if (is_primitive(java_class)) {
1334     name = type2name(primitive_type(java_class));
1335   } else {
1336     name = as_Klass(java_class)->external_name();
1337   }
1338   if (name == nullptr) {
1339     name = "<null>";
1340   }
1341   return name;
1342 }
1343 
1344 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1345   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1346   assert(k == nullptr || (k->is_klass() && k->is_array_klass()), "should be array klass");
1347   return k;
1348 }
1349 
1350 
1351 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1352   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");




1353   java_class->release_metadata_field_put(_array_klass_offset, klass);
1354 }
1355 
1356 
1357 BasicType java_lang_Class::primitive_type(oop java_class) {
1358   assert(is_primitive(java_class), "just checking");
1359   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1360   BasicType type = T_VOID;
1361   if (ak != nullptr) {
1362     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1363     type = ArrayKlass::cast(ak)->element_type();
1364   } else {
1365     assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1366   }
1367 #ifdef ASSERT
1368   if (CDSConfig::is_dumping_heap()) {
1369     oop mirror = Universe::java_mirror(type);
1370     oop scratch_mirror = HeapShared::scratch_java_mirror(type);
1371     assert(java_class == mirror || java_class == scratch_mirror, "must be consistent");
1372   } else {

2608 
2609     // the format of the stacktrace will be:
2610     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2611     // - 0 or more <init> methods for the exception class (skipped)
2612     // - rest of the stack
2613 
2614     if (!skip_fillInStackTrace_check) {
2615       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2616           throwable->is_a(method->method_holder())) {
2617         continue;
2618       }
2619       else {
2620         skip_fillInStackTrace_check = true; // gone past them all
2621       }
2622     }
2623     if (!skip_throwableInit_check) {
2624       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2625 
2626       // skip <init> methods of the exception class and superclasses
2627       // This is similar to classic VM.
2628       if (method->name() == vmSymbols::object_initializer_name() &&
2629           throwable->is_a(method->method_holder())) {
2630         continue;
2631       } else {
2632         // there are none or we've seen them all - either way stop checking
2633         skip_throwableInit_check = true;
2634       }
2635     }
2636     if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2637       if (skip_hidden) {
2638         if (total_count == 0) {
2639           // The top frame will be hidden from the stack trace.
2640           bt.set_has_hidden_top_frame();
2641         }
2642         continue;
2643       }
2644     }
2645 
2646     bt.push(method, bci, CHECK);
2647     total_count++;
2648   }

2967 int java_lang_ClassFrameInfo::_classOrMemberName_offset;
2968 int java_lang_ClassFrameInfo::_flags_offset;
2969 
2970 #define CLASSFRAMEINFO_FIELDS_DO(macro) \
2971   macro(_classOrMemberName_offset, k, "classOrMemberName", object_signature,  false); \
2972   macro(_flags_offset,             k, vmSymbols::flags_name(), int_signature, false)
2973 
2974 void java_lang_ClassFrameInfo::compute_offsets() {
2975   InstanceKlass* k = vmClasses::ClassFrameInfo_klass();
2976   CLASSFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2977 }
2978 
2979 #if INCLUDE_CDS
2980 void java_lang_ClassFrameInfo::serialize_offsets(SerializeClosure* f) {
2981   CLASSFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2982 }
2983 #endif
2984 
2985 static int get_flags(const methodHandle& m) {
2986   int flags = (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS );
2987   if (m->is_initializer()) {
2988     flags |= java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR;
2989   } else {
2990     flags |= java_lang_invoke_MemberName::MN_IS_METHOD;
2991   }
2992   if (m->caller_sensitive()) {
2993     flags |= java_lang_invoke_MemberName::MN_CALLER_SENSITIVE;
2994   }
2995   if (m->is_hidden()) {
2996     flags |= java_lang_invoke_MemberName::MN_HIDDEN_MEMBER;
2997   }
2998   assert((flags & 0xFF000000) == 0, "unexpected flags");
2999   return flags;
3000 }
3001 
3002 oop java_lang_ClassFrameInfo::classOrMemberName(oop obj) {
3003   return obj->obj_field(_classOrMemberName_offset);
3004 }
3005 
3006 int java_lang_ClassFrameInfo::flags(oop obj) {
3007   return obj->int_field(_flags_offset);
3008 }

3358   constructor->int_field_put(_modifiers_offset, value);
3359 }
3360 
3361 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3362   constructor->obj_field_put(_signature_offset, value);
3363 }
3364 
3365 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3366   constructor->obj_field_put(_annotations_offset, value);
3367 }
3368 
3369 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3370   method->obj_field_put(_parameter_annotations_offset, value);
3371 }
3372 
3373 int java_lang_reflect_Field::_clazz_offset;
3374 int java_lang_reflect_Field::_name_offset;
3375 int java_lang_reflect_Field::_type_offset;
3376 int java_lang_reflect_Field::_slot_offset;
3377 int java_lang_reflect_Field::_modifiers_offset;
3378 int java_lang_reflect_Field::_trusted_final_offset;
3379 int java_lang_reflect_Field::_signature_offset;
3380 int java_lang_reflect_Field::_annotations_offset;
3381 
3382 #define FIELD_FIELDS_DO(macro) \
3383   macro(_clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
3384   macro(_name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
3385   macro(_type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
3386   macro(_slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
3387   macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
3388   macro(_trusted_final_offset,    k, vmSymbols::trusted_final_name(),    bool_signature,       false); \
3389   macro(_signature_offset,        k, vmSymbols::signature_name(),        string_signature,     false); \
3390   macro(_annotations_offset,      k, vmSymbols::annotations_name(),      byte_array_signature, false);
3391 
3392 void java_lang_reflect_Field::compute_offsets() {
3393   InstanceKlass* k = vmClasses::reflect_Field_klass();
3394   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3395 }
3396 
3397 #if INCLUDE_CDS
3398 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3399   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3400 }
3401 #endif
3402 
3403 Handle java_lang_reflect_Field::create(TRAPS) {
3404   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3405   Symbol* name = vmSymbols::java_lang_reflect_Field();
3406   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3407   InstanceKlass* ik = InstanceKlass::cast(k);
3408   // Ensure it is initialized

3433 void java_lang_reflect_Field::set_type(oop field, oop value) {
3434   field->obj_field_put(_type_offset, value);
3435 }
3436 
3437 int java_lang_reflect_Field::slot(oop reflect) {
3438   return reflect->int_field(_slot_offset);
3439 }
3440 
3441 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3442   reflect->int_field_put(_slot_offset, value);
3443 }
3444 
3445 int java_lang_reflect_Field::modifiers(oop field) {
3446   return field->int_field(_modifiers_offset);
3447 }
3448 
3449 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3450   field->int_field_put(_modifiers_offset, value);
3451 }
3452 
3453 void java_lang_reflect_Field::set_trusted_final(oop field) {
3454   field->bool_field_put(_trusted_final_offset, true);
3455 }
3456 
3457 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3458   field->obj_field_put(_signature_offset, value);
3459 }
3460 
3461 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3462   field->obj_field_put(_annotations_offset, value);
3463 }
3464 
3465 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3466   // Allocate java.lang.reflect.RecordComponent instance
3467   HandleMark hm(THREAD);
3468   InstanceKlass* ik = vmClasses::RecordComponent_klass();
3469   assert(ik != nullptr, "must be loaded");
3470   ik->initialize(CHECK_NULL);
3471 
3472   Handle element = ik->allocate_instance_handle(CHECK_NULL);
3473 
3474   Handle decl_class(THREAD, holder->java_mirror());

4276 int java_lang_invoke_MemberName::flags(oop mname) {
4277   assert(is_instance(mname), "wrong type");
4278   return mname->int_field(_flags_offset);
4279 }
4280 
4281 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4282   assert(is_instance(mname), "wrong type");
4283   mname->int_field_put(_flags_offset, flags);
4284 }
4285 
4286 
4287 // Return vmtarget from ResolvedMethodName method field through indirection
4288 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4289   assert(is_instance(mname), "wrong type");
4290   oop method = mname->obj_field(_method_offset);
4291   return method == nullptr ? nullptr : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4292 }
4293 
4294 bool java_lang_invoke_MemberName::is_method(oop mname) {
4295   assert(is_instance(mname), "must be MemberName");
4296   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
4297 }
4298 
4299 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4300   assert(is_instance(mname), "wrong type");
4301   mname->obj_field_put(_method_offset, resolved_method);
4302 }
4303 
4304 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4305   assert(is_instance(mname), "wrong type");
4306   return (intptr_t) mname->address_field(_vmindex_offset);
4307 }
4308 
4309 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4310   assert(is_instance(mname), "wrong type");
4311   mname->address_field_put(_vmindex_offset, (address) index);
4312 }
4313 
4314 
4315 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4316   assert(is_instance(resolved_method), "wrong type");

  36 #include "classfile/moduleEntry.hpp"
  37 #include "classfile/stringTable.hpp"
  38 #include "classfile/symbolTable.hpp"
  39 #include "classfile/systemDictionary.hpp"
  40 #include "classfile/vmClasses.hpp"
  41 #include "classfile/vmSymbols.hpp"
  42 #include "code/debugInfo.hpp"
  43 #include "code/dependencyContext.hpp"
  44 #include "code/pcDesc.hpp"
  45 #include "gc/shared/collectedHeap.inline.hpp"
  46 #include "interpreter/interpreter.hpp"
  47 #include "interpreter/linkResolver.hpp"
  48 #include "jvm.h"
  49 #include "logging/log.hpp"
  50 #include "logging/logStream.hpp"
  51 #include "memory/oopFactory.hpp"
  52 #include "memory/resourceArea.hpp"
  53 #include "memory/universe.hpp"
  54 #include "oops/fieldInfo.hpp"
  55 #include "oops/fieldStreams.inline.hpp"
  56 #include "oops/flatArrayKlass.hpp"
  57 #include "oops/inlineKlass.inline.hpp"
  58 #include "oops/instanceKlass.inline.hpp"
  59 #include "oops/instanceMirrorKlass.inline.hpp"
  60 #include "oops/klass.hpp"
  61 #include "oops/klass.inline.hpp"
  62 #include "oops/method.inline.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oopCast.inline.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "oops/symbol.hpp"
  68 #include "oops/recordComponent.hpp"
  69 #include "oops/typeArrayOop.inline.hpp"
  70 #include "prims/jvmtiExport.hpp"
  71 #include "prims/methodHandles.hpp"
  72 #include "prims/resolvedMethodTable.hpp"
  73 #include "runtime/continuationEntry.inline.hpp"
  74 #include "runtime/continuationJavaClasses.inline.hpp"
  75 #include "runtime/fieldDescriptor.inline.hpp"
  76 #include "runtime/frame.inline.hpp"
  77 #include "runtime/handles.inline.hpp"
  78 #include "runtime/handshake.hpp"
  79 #include "runtime/init.hpp"

 773                              ((jchar) value->byte_at(index)) & 0xff;
 774     if (c < ' ') {
 775       st->print("\\x%02X", c); // print control characters e.g. \x0A
 776     } else {
 777       st->print("%c", c);
 778     }
 779   }
 780   st->print("\"");
 781 }
 782 
 783 // java_lang_Class
 784 
 785 int java_lang_Class::_klass_offset;
 786 int java_lang_Class::_array_klass_offset;
 787 int java_lang_Class::_oop_size_offset;
 788 int java_lang_Class::_static_oop_field_count_offset;
 789 int java_lang_Class::_class_loader_offset;
 790 int java_lang_Class::_module_offset;
 791 int java_lang_Class::_protection_domain_offset;
 792 int java_lang_Class::_component_mirror_offset;
 793 
 794 int java_lang_Class::_signers_offset;
 795 int java_lang_Class::_name_offset;
 796 int java_lang_Class::_source_file_offset;
 797 int java_lang_Class::_classData_offset;
 798 int java_lang_Class::_classRedefinedCount_offset;
 799 
 800 bool java_lang_Class::_offsets_computed = false;
 801 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = nullptr;
 802 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = nullptr;
 803 
 804 #ifdef ASSERT
 805 inline static void assert_valid_static_string_field(fieldDescriptor* fd) {
 806   assert(fd->has_initial_value(), "caller should have checked this");
 807   assert(fd->field_type() == T_OBJECT, "caller should have checked this");
 808   assert(fd->signature() == vmSymbols::string_signature(), "just checking");
 809 }
 810 #endif
 811 
 812 static void initialize_static_string_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
 813   DEBUG_ONLY(assert_valid_static_string_field(fd);)

 973     new (mtModule) GrowableArray<Klass*>(500, mtModule);
 974   set_fixup_module_field_list(module_list);
 975 }
 976 
 977 void java_lang_Class::allocate_mirror(Klass* k, bool is_scratch, Handle protection_domain, Handle classData,
 978                                       Handle& mirror, Handle& comp_mirror, TRAPS) {
 979   // Allocate mirror (java.lang.Class instance)
 980   oop mirror_oop = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(k, CHECK);
 981   mirror = Handle(THREAD, mirror_oop);
 982 
 983   // Setup indirection from mirror->klass
 984   set_klass(mirror(), k);
 985 
 986   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
 987   assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
 988 
 989   set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
 990 
 991   // It might also have a component mirror.  This mirror must already exist.
 992   if (k->is_array_klass()) {
 993     if (k->is_flatArray_klass()) {
 994       Klass* element_klass = (Klass*) FlatArrayKlass::cast(k)->element_klass();
 995       assert(element_klass->is_inline_klass(), "Must be inline type component");
 996       if (is_scratch) {
 997         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
 998       } else {
 999         comp_mirror = Handle(THREAD, element_klass->java_mirror());
1000       }
1001     } else if (k->is_typeArray_klass()) {
1002       BasicType type = TypeArrayKlass::cast(k)->element_type();
1003       if (is_scratch) {
1004         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(type));
1005       } else {
1006         comp_mirror = Handle(THREAD, Universe::java_mirror(type));
1007       }
1008     } else {
1009       assert(k->is_objArray_klass(), "Must be");
1010       Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1011       assert(element_klass != nullptr, "Must have an element klass");
1012       oop comp_oop = element_klass->java_mirror();
1013       if (element_klass->is_inline_klass()) {
1014         InlineKlass* ik = InlineKlass::cast(element_klass);
1015         comp_oop = ik->java_mirror();
1016       }
1017       if (is_scratch) {
1018         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1019       } else {
1020         comp_mirror = Handle(THREAD, comp_oop);
1021       }
1022     }
1023     assert(comp_mirror() != nullptr, "must have a mirror");
1024 
1025     // Two-way link between the array klass and its component mirror:
1026     // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1027     set_component_mirror(mirror(), comp_mirror());
1028     // See below for ordering dependencies between field array_klass in component mirror
1029     // and java_mirror in this klass.
1030   } else {
1031     assert(k->is_instance_klass(), "Must be");
1032 
1033     initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1034     if (HAS_PENDING_EXCEPTION) {
1035       // If any of the fields throws an exception like OOM remove the klass field
1036       // from the mirror so GC doesn't follow it after the klass has been deallocated.
1037       // This mirror looks like a primitive type, which logically it is because it
1038       // it represents no class.
1039       set_klass(mirror(), nullptr);
1040       return;

1062 
1063     allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1064 
1065     // set the classLoader field in the java_lang_Class instance
1066     assert(class_loader() == k->class_loader(), "should be same");
1067     set_class_loader(mirror(), class_loader());
1068 
1069     // Setup indirection from klass->mirror
1070     // after any exceptions can happen during allocations.
1071     k->set_java_mirror(mirror);
1072 
1073     // Set the module field in the java_lang_Class instance.  This must be done
1074     // after the mirror is set.
1075     set_mirror_module_field(THREAD, k, mirror, module);
1076 
1077     if (comp_mirror() != nullptr) {
1078       // Set after k->java_mirror() is published, because compiled code running
1079       // concurrently doesn't expect a k to have a null java_mirror.
1080       release_set_array_klass(comp_mirror(), k);
1081     }
1082 
1083     if (CDSConfig::is_dumping_heap()) {
1084       create_scratch_mirror(k, CHECK);
1085     }
1086   } else {
1087     assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1088     fixup_mirror_list()->push(k);
1089   }
1090 }
1091 
1092 #if INCLUDE_CDS_JAVA_HEAP
1093 // The "scratch mirror" stores the states of the mirror object that can be
1094 // decided at dump time (such as the initial values of the static fields, the
1095 // component mirror, etc). At runtime, more information is added to it by
1096 // java_lang_Class::restore_archived_mirror().
1097 //
1098 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1099 // produces the same result as /*runtime*/create_mirror().
1100 //
1101 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1102 // latter may contain dumptime-specific information that cannot be archived
1103 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1104 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) {
1105   // Inline classes encapsulate two mirror objects, a value mirror (primitive value mirror)
1106   // and a reference mirror (primitive class mirror), skip over scratch mirror allocation
1107   // for inline classes, they will not be part of shared archive and will be created while
1108   // restoring unshared fileds. Refer Klass::restore_unshareable_info() for more details.
1109   if (k->is_inline_klass() ||
1110       (k->class_loader() != nullptr &&
1111        k->class_loader() != SystemDictionary::java_platform_loader() &&
1112        k->class_loader() != SystemDictionary::java_system_loader())) {
1113     // We only archive the mirrors of classes loaded by the built-in loaders
1114     return;
1115   }
1116 
1117   Handle protection_domain, classData; // set to null. Will be reinitialized at runtime
1118   Handle mirror;
1119   Handle comp_mirror;
1120   allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1121 
1122   if (comp_mirror() != nullptr) {
1123     release_set_array_klass(comp_mirror(), k);
1124   }
1125 
1126   HeapShared::set_scratch_java_mirror(k, mirror());
1127 }
1128 
1129 // Returns true if the mirror is updated, false if no archived mirror
1130 // data is present. After the archived mirror object is restored, the
1131 // shared klass' _has_raw_archived_mirror flag is cleared.
1132 bool java_lang_Class::restore_archived_mirror(Klass *k,

1296   assert(is_instance(java_class), "must be a Class object");
1297   java_class->metadata_field_put(_klass_offset, klass);
1298 }
1299 
1300 
1301 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1302   assert(is_instance(java_class), "must be a Class object");
1303   Symbol* name = nullptr;
1304   bool is_instance = false;
1305   if (is_primitive(java_class)) {
1306     name = vmSymbols::type_signature(primitive_type(java_class));
1307   } else {
1308     Klass* k = as_Klass(java_class);
1309     is_instance = k->is_instance_klass();
1310     name = k->name();
1311   }
1312   if (name == nullptr) {
1313     st->print("<null>");
1314     return;
1315   }
1316   if (is_instance)  {
1317     st->print("L");
1318   }
1319   st->write((char*) name->base(), (int) name->utf8_length());
1320   if (is_instance)  st->print(";");
1321 }
1322 
1323 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1324   assert(is_instance(java_class), "must be a Class object");
1325   Symbol* name;
1326   if (is_primitive(java_class)) {
1327     name = vmSymbols::type_signature(primitive_type(java_class));
1328     // Because this can create a new symbol, the caller has to decrement
1329     // the refcount, so make adjustment here and below for symbols returned
1330     // that are not created or incremented due to a successful lookup.
1331     name->increment_refcount();
1332   } else {
1333     Klass* k = as_Klass(java_class);
1334     if (!k->is_instance_klass()) {
1335       name = k->name();
1336       name->increment_refcount();
1337     } else {
1338       ResourceMark rm;

1357   if (is_primitive(java_class)) {
1358     name = type2name(primitive_type(java_class));
1359   } else {
1360     name = as_Klass(java_class)->external_name();
1361   }
1362   if (name == nullptr) {
1363     name = "<null>";
1364   }
1365   return name;
1366 }
1367 
1368 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1369   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1370   assert(k == nullptr || (k->is_klass() && k->is_array_klass()), "should be array klass");
1371   return k;
1372 }
1373 
1374 
1375 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1376   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1377   if (klass->is_flatArray_klass() || (klass->is_objArray_klass() && ObjArrayKlass::cast(klass)->is_null_free_array_klass())) {
1378     // TODO 8325106 Ignore flat / null-free arrays
1379     return;
1380   }
1381   java_class->release_metadata_field_put(_array_klass_offset, klass);
1382 }
1383 
1384 
1385 BasicType java_lang_Class::primitive_type(oop java_class) {
1386   assert(is_primitive(java_class), "just checking");
1387   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1388   BasicType type = T_VOID;
1389   if (ak != nullptr) {
1390     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1391     type = ArrayKlass::cast(ak)->element_type();
1392   } else {
1393     assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1394   }
1395 #ifdef ASSERT
1396   if (CDSConfig::is_dumping_heap()) {
1397     oop mirror = Universe::java_mirror(type);
1398     oop scratch_mirror = HeapShared::scratch_java_mirror(type);
1399     assert(java_class == mirror || java_class == scratch_mirror, "must be consistent");
1400   } else {

2636 
2637     // the format of the stacktrace will be:
2638     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2639     // - 0 or more <init> methods for the exception class (skipped)
2640     // - rest of the stack
2641 
2642     if (!skip_fillInStackTrace_check) {
2643       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2644           throwable->is_a(method->method_holder())) {
2645         continue;
2646       }
2647       else {
2648         skip_fillInStackTrace_check = true; // gone past them all
2649       }
2650     }
2651     if (!skip_throwableInit_check) {
2652       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2653 
2654       // skip <init> methods of the exception class and superclasses
2655       // This is similar to classic VM.
2656       if (method->is_object_constructor() &&
2657           throwable->is_a(method->method_holder())) {
2658         continue;
2659       } else {
2660         // there are none or we've seen them all - either way stop checking
2661         skip_throwableInit_check = true;
2662       }
2663     }
2664     if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2665       if (skip_hidden) {
2666         if (total_count == 0) {
2667           // The top frame will be hidden from the stack trace.
2668           bt.set_has_hidden_top_frame();
2669         }
2670         continue;
2671       }
2672     }
2673 
2674     bt.push(method, bci, CHECK);
2675     total_count++;
2676   }

2995 int java_lang_ClassFrameInfo::_classOrMemberName_offset;
2996 int java_lang_ClassFrameInfo::_flags_offset;
2997 
2998 #define CLASSFRAMEINFO_FIELDS_DO(macro) \
2999   macro(_classOrMemberName_offset, k, "classOrMemberName", object_signature,  false); \
3000   macro(_flags_offset,             k, vmSymbols::flags_name(), int_signature, false)
3001 
3002 void java_lang_ClassFrameInfo::compute_offsets() {
3003   InstanceKlass* k = vmClasses::ClassFrameInfo_klass();
3004   CLASSFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3005 }
3006 
3007 #if INCLUDE_CDS
3008 void java_lang_ClassFrameInfo::serialize_offsets(SerializeClosure* f) {
3009   CLASSFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3010 }
3011 #endif
3012 
3013 static int get_flags(const methodHandle& m) {
3014   int flags = (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS );
3015   if (m->is_object_constructor()) {
3016     flags |= java_lang_invoke_MemberName::MN_IS_OBJECT_CONSTRUCTOR;
3017   } else {
3018     flags |= java_lang_invoke_MemberName::MN_IS_METHOD;
3019   }
3020   if (m->caller_sensitive()) {
3021     flags |= java_lang_invoke_MemberName::MN_CALLER_SENSITIVE;
3022   }
3023   if (m->is_hidden()) {
3024     flags |= java_lang_invoke_MemberName::MN_HIDDEN_MEMBER;
3025   }
3026   assert((flags & 0xFF000000) == 0, "unexpected flags");
3027   return flags;
3028 }
3029 
3030 oop java_lang_ClassFrameInfo::classOrMemberName(oop obj) {
3031   return obj->obj_field(_classOrMemberName_offset);
3032 }
3033 
3034 int java_lang_ClassFrameInfo::flags(oop obj) {
3035   return obj->int_field(_flags_offset);
3036 }

3386   constructor->int_field_put(_modifiers_offset, value);
3387 }
3388 
3389 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3390   constructor->obj_field_put(_signature_offset, value);
3391 }
3392 
3393 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3394   constructor->obj_field_put(_annotations_offset, value);
3395 }
3396 
3397 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3398   method->obj_field_put(_parameter_annotations_offset, value);
3399 }
3400 
3401 int java_lang_reflect_Field::_clazz_offset;
3402 int java_lang_reflect_Field::_name_offset;
3403 int java_lang_reflect_Field::_type_offset;
3404 int java_lang_reflect_Field::_slot_offset;
3405 int java_lang_reflect_Field::_modifiers_offset;
3406 int java_lang_reflect_Field::_flags_offset;
3407 int java_lang_reflect_Field::_signature_offset;
3408 int java_lang_reflect_Field::_annotations_offset;
3409 
3410 #define FIELD_FIELDS_DO(macro) \
3411   macro(_clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
3412   macro(_name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
3413   macro(_type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
3414   macro(_slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
3415   macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
3416   macro(_flags_offset,     k, vmSymbols::flags_name(),     int_signature,    false); \
3417   macro(_signature_offset,        k, vmSymbols::signature_name(),        string_signature,     false); \
3418   macro(_annotations_offset,      k, vmSymbols::annotations_name(),      byte_array_signature, false);
3419 
3420 void java_lang_reflect_Field::compute_offsets() {
3421   InstanceKlass* k = vmClasses::reflect_Field_klass();
3422   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3423 }
3424 
3425 #if INCLUDE_CDS
3426 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3427   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3428 }
3429 #endif
3430 
3431 Handle java_lang_reflect_Field::create(TRAPS) {
3432   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3433   Symbol* name = vmSymbols::java_lang_reflect_Field();
3434   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3435   InstanceKlass* ik = InstanceKlass::cast(k);
3436   // Ensure it is initialized

3461 void java_lang_reflect_Field::set_type(oop field, oop value) {
3462   field->obj_field_put(_type_offset, value);
3463 }
3464 
3465 int java_lang_reflect_Field::slot(oop reflect) {
3466   return reflect->int_field(_slot_offset);
3467 }
3468 
3469 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3470   reflect->int_field_put(_slot_offset, value);
3471 }
3472 
3473 int java_lang_reflect_Field::modifiers(oop field) {
3474   return field->int_field(_modifiers_offset);
3475 }
3476 
3477 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3478   field->int_field_put(_modifiers_offset, value);
3479 }
3480 
3481 void java_lang_reflect_Field::set_flags(oop field, int value) {
3482   field->int_field_put(_flags_offset, value);
3483 }
3484 
3485 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3486   field->obj_field_put(_signature_offset, value);
3487 }
3488 
3489 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3490   field->obj_field_put(_annotations_offset, value);
3491 }
3492 
3493 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3494   // Allocate java.lang.reflect.RecordComponent instance
3495   HandleMark hm(THREAD);
3496   InstanceKlass* ik = vmClasses::RecordComponent_klass();
3497   assert(ik != nullptr, "must be loaded");
3498   ik->initialize(CHECK_NULL);
3499 
3500   Handle element = ik->allocate_instance_handle(CHECK_NULL);
3501 
3502   Handle decl_class(THREAD, holder->java_mirror());

4304 int java_lang_invoke_MemberName::flags(oop mname) {
4305   assert(is_instance(mname), "wrong type");
4306   return mname->int_field(_flags_offset);
4307 }
4308 
4309 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4310   assert(is_instance(mname), "wrong type");
4311   mname->int_field_put(_flags_offset, flags);
4312 }
4313 
4314 
4315 // Return vmtarget from ResolvedMethodName method field through indirection
4316 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4317   assert(is_instance(mname), "wrong type");
4318   oop method = mname->obj_field(_method_offset);
4319   return method == nullptr ? nullptr : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4320 }
4321 
4322 bool java_lang_invoke_MemberName::is_method(oop mname) {
4323   assert(is_instance(mname), "must be MemberName");
4324   return (flags(mname) & (MN_IS_METHOD | MN_IS_OBJECT_CONSTRUCTOR)) > 0;
4325 }
4326 
4327 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4328   assert(is_instance(mname), "wrong type");
4329   mname->obj_field_put(_method_offset, resolved_method);
4330 }
4331 
4332 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4333   assert(is_instance(mname), "wrong type");
4334   return (intptr_t) mname->address_field(_vmindex_offset);
4335 }
4336 
4337 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4338   assert(is_instance(mname), "wrong type");
4339   mname->address_field_put(_vmindex_offset, (address) index);
4340 }
4341 
4342 
4343 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4344   assert(is_instance(resolved_method), "wrong type");
< prev index next >