< 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 {

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

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

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

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

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

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

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

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

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

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