< 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 (is_scratch) {
1014         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1015       } else {
1016         comp_mirror = Handle(THREAD, comp_oop);
1017       }
1018     }
1019     assert(comp_mirror() != nullptr, "must have a mirror");
1020 
1021     // Two-way link between the array klass and its component mirror:
1022     // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1023     set_component_mirror(mirror(), comp_mirror());
1024     // See below for ordering dependencies between field array_klass in component mirror
1025     // and java_mirror in this klass.
1026   } else {
1027     assert(k->is_instance_klass(), "Must be");
1028 
1029     initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1030     if (HAS_PENDING_EXCEPTION) {
1031       // If any of the fields throws an exception like OOM remove the klass field
1032       // from the mirror so GC doesn't follow it after the klass has been deallocated.
1033       // This mirror looks like a primitive type, which logically it is because it
1034       // it represents no class.
1035       set_klass(mirror(), nullptr);
1036       return;

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

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

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

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

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

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

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

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