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

1057 void java_lang_Class::allocate_mirror(Klass* k, bool is_scratch, Handle protection_domain, Handle classData,
1058                                       Handle& mirror, Handle& comp_mirror, TRAPS) {
1059   // Allocate mirror (java.lang.Class instance)
1060   oop mirror_oop = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(k, CHECK);
1061   mirror = Handle(THREAD, mirror_oop);
1062 
1063   // Setup indirection from mirror->klass
1064   set_klass(mirror(), k);
1065 
1066   // Set the modifiers flag.
1067   u2 computed_modifiers = k->compute_modifier_flags();
1068   set_modifiers(mirror(), computed_modifiers);
1069 
1070   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
1071   assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
1072 
1073   set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
1074 
1075   // It might also have a component mirror.  This mirror must already exist.
1076   if (k->is_array_klass()) {
1077     if (k->is_typeArray_klass()) {








1078       BasicType type = TypeArrayKlass::cast(k)->element_type();
1079       if (is_scratch) {
1080         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(type));
1081       } else {
1082         comp_mirror = Handle(THREAD, Universe::java_mirror(type));
1083       }
1084     } else {
1085       assert(k->is_objArray_klass(), "Must be");
1086       Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1087       assert(element_klass != nullptr, "Must have an element klass");

1088       if (is_scratch) {
1089         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1090       } else {
1091         comp_mirror = Handle(THREAD, element_klass->java_mirror());
1092       }
1093     }
1094     assert(comp_mirror() != nullptr, "must have a mirror");
1095 
1096     // Two-way link between the array klass and its component mirror:
1097     // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1098     set_component_mirror(mirror(), comp_mirror());
1099     // See below for ordering dependencies between field array_klass in component mirror
1100     // and java_mirror in this klass.
1101   } else {
1102     assert(k->is_instance_klass(), "Must be");
1103 
1104     initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1105     if (HAS_PENDING_EXCEPTION) {
1106       // If any of the fields throws an exception like OOM remove the klass field
1107       // from the mirror so GC doesn't follow it after the klass has been deallocated.
1108       // This mirror looks like a primitive type, which logically it is because it
1109       // it represents no class.
1110       set_klass(mirror(), nullptr);
1111       return;

1127 
1128     allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1129 
1130     // set the classLoader field in the java_lang_Class instance
1131     assert(class_loader() == k->class_loader(), "should be same");
1132     set_class_loader(mirror(), class_loader());
1133 
1134     // Setup indirection from klass->mirror
1135     // after any exceptions can happen during allocations.
1136     k->set_java_mirror(mirror);
1137 
1138     // Set the module field in the java_lang_Class instance.  This must be done
1139     // after the mirror is set.
1140     set_mirror_module_field(THREAD, k, mirror, module);
1141 
1142     if (comp_mirror() != nullptr) {
1143       // Set after k->java_mirror() is published, because compiled code running
1144       // concurrently doesn't expect a k to have a null java_mirror.
1145       release_set_array_klass(comp_mirror(), k);
1146     }

1147     if (CDSConfig::is_dumping_heap()) {
1148       create_scratch_mirror(k, CHECK);
1149     }
1150   } else {
1151     assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1152     fixup_mirror_list()->push(k);
1153   }
1154 }
1155 
1156 #if INCLUDE_CDS_JAVA_HEAP
1157 // The "scratch mirror" stores the states of the mirror object that can be
1158 // decided at dump time (such as the initial values of the static fields, the
1159 // component mirror, etc). At runtime, more information is added to it by
1160 // java_lang_Class::restore_archived_mirror().
1161 //
1162 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1163 // produces the same result as /*runtime*/create_mirror().
1164 //
1165 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1166 // latter may contain dumptime-specific information that cannot be archived
1167 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1168 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) {
1169   if (k->class_loader() != nullptr &&
1170       k->class_loader() != SystemDictionary::java_platform_loader() &&
1171       k->class_loader() != SystemDictionary::java_system_loader()) {
1172     // We only archive the mirrors of classes loaded by the built-in loaders
1173     return;
1174   }
1175 
1176   Handle protection_domain, classData; // set to null. Will be reinitialized at runtime
1177   Handle mirror;
1178   Handle comp_mirror;
1179   allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1180 
1181   if (comp_mirror() != nullptr) {
1182     release_set_array_klass(comp_mirror(), k);
1183   }
1184 
1185   HeapShared::set_scratch_java_mirror(k, mirror());
1186 }
1187 
1188 // Returns true if the mirror is updated, false if no archived mirror
1189 // data is present. After the archived mirror object is restored, the
1190 // shared klass' _has_raw_archived_mirror flag is cleared.
1191 bool java_lang_Class::restore_archived_mirror(Klass *k,

1379   assert(is_instance(java_class), "must be a Class object");
1380   java_class->metadata_field_put(_klass_offset, klass);
1381 }
1382 
1383 
1384 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1385   assert(is_instance(java_class), "must be a Class object");
1386   Symbol* name = nullptr;
1387   bool is_instance = false;
1388   if (is_primitive(java_class)) {
1389     name = vmSymbols::type_signature(primitive_type(java_class));
1390   } else {
1391     Klass* k = as_Klass(java_class);
1392     is_instance = k->is_instance_klass();
1393     name = k->name();
1394   }
1395   if (name == nullptr) {
1396     st->print("<null>");
1397     return;
1398   }
1399   if (is_instance)  st->print("L");


1400   st->write((char*) name->base(), (int) name->utf8_length());
1401   if (is_instance)  st->print(";");
1402 }
1403 
1404 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1405   assert(is_instance(java_class), "must be a Class object");
1406   Symbol* name;
1407   if (is_primitive(java_class)) {
1408     name = vmSymbols::type_signature(primitive_type(java_class));
1409     // Because this can create a new symbol, the caller has to decrement
1410     // the refcount, so make adjustment here and below for symbols returned
1411     // that are not created or incremented due to a successful lookup.
1412     name->increment_refcount();
1413   } else {
1414     Klass* k = as_Klass(java_class);
1415     if (!k->is_instance_klass()) {
1416       name = k->name();
1417       name->increment_refcount();
1418     } else {
1419       ResourceMark rm;

1438   if (is_primitive(java_class)) {
1439     name = type2name(primitive_type(java_class));
1440   } else {
1441     name = as_Klass(java_class)->external_name();
1442   }
1443   if (name == nullptr) {
1444     name = "<null>";
1445   }
1446   return name;
1447 }
1448 
1449 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1450   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1451   assert(k == nullptr || (k->is_klass() && k->is_array_klass()), "should be array klass");
1452   return k;
1453 }
1454 
1455 
1456 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1457   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");




1458   java_class->release_metadata_field_put(_array_klass_offset, klass);
1459 }
1460 
1461 
1462 BasicType java_lang_Class::primitive_type(oop java_class) {
1463   assert(is_primitive(java_class), "just checking");
1464   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1465   BasicType type = T_VOID;
1466   if (ak != nullptr) {
1467     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1468     type = ArrayKlass::cast(ak)->element_type();
1469   } else {
1470     assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1471   }
1472 #ifdef ASSERT
1473   if (CDSConfig::is_dumping_heap()) {
1474     oop mirror = Universe::java_mirror(type);
1475     oop scratch_mirror = HeapShared::scratch_java_mirror(type);
1476     assert(java_class == mirror || java_class == scratch_mirror, "must be consistent");
1477   } else {

2823 
2824     // the format of the stacktrace will be:
2825     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2826     // - 0 or more <init> methods for the exception class (skipped)
2827     // - rest of the stack
2828 
2829     if (!skip_fillInStackTrace_check) {
2830       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2831           throwable->is_a(method->method_holder())) {
2832         continue;
2833       }
2834       else {
2835         skip_fillInStackTrace_check = true; // gone past them all
2836       }
2837     }
2838     if (!skip_throwableInit_check) {
2839       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2840 
2841       // skip <init> methods of the exception class and superclasses
2842       // This is similar to classic VM.
2843       if (method->name() == vmSymbols::object_initializer_name() &&
2844           throwable->is_a(method->method_holder())) {
2845         continue;
2846       } else {
2847         // there are none or we've seen them all - either way stop checking
2848         skip_throwableInit_check = true;
2849       }
2850     }
2851     if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2852       if (skip_hidden) {
2853         if (total_count == 0) {
2854           // The top frame will be hidden from the stack trace.
2855           bt.set_has_hidden_top_frame();
2856         }
2857         continue;
2858       }
2859     }
2860 
2861     bt.push(method, bci, CHECK);
2862     total_count++;
2863   }

3182 int java_lang_ClassFrameInfo::_classOrMemberName_offset;
3183 int java_lang_ClassFrameInfo::_flags_offset;
3184 
3185 #define CLASSFRAMEINFO_FIELDS_DO(macro) \
3186   macro(_classOrMemberName_offset, k, "classOrMemberName", object_signature,  false); \
3187   macro(_flags_offset,             k, vmSymbols::flags_name(), int_signature, false)
3188 
3189 void java_lang_ClassFrameInfo::compute_offsets() {
3190   InstanceKlass* k = vmClasses::ClassFrameInfo_klass();
3191   CLASSFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3192 }
3193 
3194 #if INCLUDE_CDS
3195 void java_lang_ClassFrameInfo::serialize_offsets(SerializeClosure* f) {
3196   CLASSFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3197 }
3198 #endif
3199 
3200 static int get_flags(const methodHandle& m) {
3201   int flags = m->access_flags().as_method_flags();
3202   if (m->is_object_initializer()) {
3203     flags |= java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR;
3204   } else {
3205     // Note: Static initializers can be here. Record them as plain methods.
3206     flags |= java_lang_invoke_MemberName::MN_IS_METHOD;
3207   }
3208   if (m->caller_sensitive()) {
3209     flags |= java_lang_invoke_MemberName::MN_CALLER_SENSITIVE;
3210   }
3211   if (m->is_hidden()) {
3212     flags |= java_lang_invoke_MemberName::MN_HIDDEN_MEMBER;
3213   }
3214   assert((flags & 0xFF000000) == 0, "unexpected flags");
3215   return flags;
3216 }
3217 
3218 oop java_lang_ClassFrameInfo::classOrMemberName(oop obj) {
3219   return obj->obj_field(_classOrMemberName_offset);
3220 }
3221 
3222 int java_lang_ClassFrameInfo::flags(oop obj) {
3223   return obj->int_field(_flags_offset);

3574   constructor->int_field_put(_modifiers_offset, value);
3575 }
3576 
3577 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3578   constructor->obj_field_put(_signature_offset, value);
3579 }
3580 
3581 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3582   constructor->obj_field_put(_annotations_offset, value);
3583 }
3584 
3585 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3586   method->obj_field_put(_parameter_annotations_offset, value);
3587 }
3588 
3589 int java_lang_reflect_Field::_clazz_offset;
3590 int java_lang_reflect_Field::_name_offset;
3591 int java_lang_reflect_Field::_type_offset;
3592 int java_lang_reflect_Field::_slot_offset;
3593 int java_lang_reflect_Field::_modifiers_offset;
3594 int java_lang_reflect_Field::_trusted_final_offset;
3595 int java_lang_reflect_Field::_signature_offset;
3596 int java_lang_reflect_Field::_annotations_offset;
3597 
3598 #define FIELD_FIELDS_DO(macro) \
3599   macro(_clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
3600   macro(_name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
3601   macro(_type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
3602   macro(_slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
3603   macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
3604   macro(_trusted_final_offset,    k, vmSymbols::trusted_final_name(),    bool_signature,       false); \
3605   macro(_signature_offset,        k, vmSymbols::signature_name(),        string_signature,     false); \
3606   macro(_annotations_offset,      k, vmSymbols::annotations_name(),      byte_array_signature, false);
3607 
3608 void java_lang_reflect_Field::compute_offsets() {
3609   InstanceKlass* k = vmClasses::reflect_Field_klass();
3610   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3611 }
3612 
3613 #if INCLUDE_CDS
3614 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3615   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3616 }
3617 #endif
3618 
3619 Handle java_lang_reflect_Field::create(TRAPS) {
3620   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3621   Symbol* name = vmSymbols::java_lang_reflect_Field();
3622   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3623   InstanceKlass* ik = InstanceKlass::cast(k);
3624   // Ensure it is initialized

3649 void java_lang_reflect_Field::set_type(oop field, oop value) {
3650   field->obj_field_put(_type_offset, value);
3651 }
3652 
3653 int java_lang_reflect_Field::slot(oop reflect) {
3654   return reflect->int_field(_slot_offset);
3655 }
3656 
3657 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3658   reflect->int_field_put(_slot_offset, value);
3659 }
3660 
3661 int java_lang_reflect_Field::modifiers(oop field) {
3662   return field->int_field(_modifiers_offset);
3663 }
3664 
3665 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3666   field->int_field_put(_modifiers_offset, value);
3667 }
3668 
3669 void java_lang_reflect_Field::set_trusted_final(oop field) {
3670   field->bool_field_put(_trusted_final_offset, true);
3671 }
3672 
3673 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3674   field->obj_field_put(_signature_offset, value);
3675 }
3676 
3677 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3678   field->obj_field_put(_annotations_offset, value);
3679 }
3680 
3681 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3682   // Allocate java.lang.reflect.RecordComponent instance
3683   HandleMark hm(THREAD);
3684   InstanceKlass* ik = vmClasses::RecordComponent_klass();
3685   assert(ik != nullptr, "must be loaded");
3686   ik->initialize(CHECK_NULL);
3687 
3688   Handle element = ik->allocate_instance_handle(CHECK_NULL);
3689 
3690   Handle decl_class(THREAD, holder->java_mirror());

3953 }
3954 #endif
3955 
3956 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
3957   assert(obj != nullptr, "sanity");
3958   if (offset != _referent_offset) {
3959     return false;
3960   }
3961 
3962   Klass* k = obj->klass();
3963   if (!k->is_instance_klass()) {
3964     return false;
3965   }
3966 
3967   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
3968   bool is_reference = ik->reference_type() != REF_NONE;
3969   assert(!is_reference || ik->is_subclass_of(vmClasses::Reference_klass()), "sanity");
3970   return is_reference;
3971 }
3972 
3973 int java_lang_boxing_object::_value_offset;
3974 int java_lang_boxing_object::_long_value_offset;
3975 
3976 #define BOXING_FIELDS_DO(macro) \
3977   macro(_value_offset,      integerKlass, "value", int_signature, false); \
3978   macro(_long_value_offset, longKlass, "value", long_signature, false);






3979 
3980 void java_lang_boxing_object::compute_offsets() {
3981   InstanceKlass* integerKlass = vmClasses::Integer_klass();
3982   InstanceKlass* longKlass = vmClasses::Long_klass();
3983   BOXING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3984 }
3985 
3986 #if INCLUDE_CDS
3987 void java_lang_boxing_object::serialize_offsets(SerializeClosure* f) {




3988   BOXING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3989 }
3990 #endif
3991 
3992 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
3993   Klass* k = vmClasses::box_klass(type);
3994   if (k == nullptr)  return nullptr;
3995   InstanceKlass* ik = InstanceKlass::cast(k);
3996   if (!ik->is_initialized()) {
3997     ik->initialize(CHECK_NULL);
3998   }
3999   return ik->allocate_instance(THREAD);
4000 }
4001 
4002 
4003 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
4004   oop box = initialize_and_allocate(type, CHECK_NULL);
4005   if (box == nullptr)  return nullptr;
4006   switch (type) {
4007     case T_BOOLEAN:
4008       box->bool_field_put(_value_offset, value->z);
4009       break;
4010     case T_CHAR:
4011       box->char_field_put(_value_offset, value->c);
4012       break;
4013     case T_FLOAT:
4014       box->float_field_put(_value_offset, value->f);
4015       break;
4016     case T_DOUBLE:
4017       box->double_field_put(_long_value_offset, value->d);
4018       break;
4019     case T_BYTE:
4020       box->byte_field_put(_value_offset, value->b);
4021       break;
4022     case T_SHORT:
4023       box->short_field_put(_value_offset, value->s);
4024       break;
4025     case T_INT:
4026       box->int_field_put(_value_offset, value->i);
4027       break;
4028     case T_LONG:
4029       box->long_field_put(_long_value_offset, value->j);
4030       break;
4031     default:
4032       return nullptr;
4033   }
4034   return box;
4035 }
4036 
4037 
4038 BasicType java_lang_boxing_object::basic_type(oop box) {
4039   if (box == nullptr)  return T_ILLEGAL;
4040   BasicType type = vmClasses::box_klass_type(box->klass());
4041   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
4042     return T_ILLEGAL;
4043   return type;
4044 }
4045 
4046 
4047 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
4048   BasicType type = vmClasses::box_klass_type(box->klass());
4049   switch (type) {
4050   case T_BOOLEAN:
4051     value->z = box->bool_field(_value_offset);
4052     break;
4053   case T_CHAR:
4054     value->c = box->char_field(_value_offset);
4055     break;
4056   case T_FLOAT:
4057     value->f = box->float_field(_value_offset);
4058     break;
4059   case T_DOUBLE:
4060     value->d = box->double_field(_long_value_offset);
4061     break;
4062   case T_BYTE:
4063     value->b = box->byte_field(_value_offset);
4064     break;
4065   case T_SHORT:
4066     value->s = box->short_field(_value_offset);
4067     break;
4068   case T_INT:
4069     value->i = box->int_field(_value_offset);
4070     break;
4071   case T_LONG:
4072     value->j = box->long_field(_long_value_offset);
4073     break;
4074   default:
4075     return T_ILLEGAL;
4076   } // end switch
4077   return type;
4078 }
4079 
4080 
4081 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
4082   BasicType type = vmClasses::box_klass_type(box->klass());
4083   switch (type) {
4084   case T_BOOLEAN:
4085     box->bool_field_put(_value_offset, value->z);
4086     break;
4087   case T_CHAR:
4088     box->char_field_put(_value_offset, value->c);
4089     break;
4090   case T_FLOAT:
4091     box->float_field_put(_value_offset, value->f);
4092     break;
4093   case T_DOUBLE:
4094     box->double_field_put(_long_value_offset, value->d);
4095     break;
4096   case T_BYTE:
4097     box->byte_field_put(_value_offset, value->b);
4098     break;
4099   case T_SHORT:
4100     box->short_field_put(_value_offset, value->s);
4101     break;
4102   case T_INT:
4103     box->int_field_put(_value_offset, value->i);
4104     break;
4105   case T_LONG:
4106     box->long_field_put(_long_value_offset, value->j);
4107     break;
4108   default:
4109     return T_ILLEGAL;
4110   } // end switch
4111   return type;
4112 }
4113 
4114 
4115 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
4116   switch (type) {
4117   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
4118   case T_CHAR:      st->print("%d", value->c);                      break;
4119   case T_BYTE:      st->print("%d", value->b);                      break;
4120   case T_SHORT:     st->print("%d", value->s);                      break;
4121   case T_INT:       st->print("%d", value->i);                      break;
4122   case T_LONG:      st->print(JLONG_FORMAT, value->j);              break;
4123   case T_FLOAT:     st->print("%f", value->f);                      break;
4124   case T_DOUBLE:    st->print("%lf", value->d);                     break;
4125   default:          st->print("type %d?", type);                    break;
4126   }

4482 oop java_lang_invoke_MemberName::type(oop mname) {
4483   assert(is_instance(mname), "wrong type");
4484   return mname->obj_field(_type_offset);
4485 }
4486 
4487 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
4488   assert(is_instance(mname), "wrong type");
4489   mname->obj_field_put(_type_offset, type);
4490 }
4491 
4492 int java_lang_invoke_MemberName::flags(oop mname) {
4493   assert(is_instance(mname), "wrong type");
4494   return mname->int_field(_flags_offset);
4495 }
4496 
4497 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4498   assert(is_instance(mname), "wrong type");
4499   mname->int_field_put(_flags_offset, flags);
4500 }
4501 
4502 
4503 // Return vmtarget from ResolvedMethodName method field through indirection
4504 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4505   assert(is_instance(mname), "wrong type");
4506   oop method = mname->obj_field(_method_offset);
4507   return method == nullptr ? nullptr : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4508 }
4509 
4510 bool java_lang_invoke_MemberName::is_method(oop mname) {
4511   assert(is_instance(mname), "must be MemberName");
4512   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
4513 }
4514 
4515 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4516   assert(is_instance(mname), "wrong type");
4517   mname->obj_field_put(_method_offset, resolved_method);
4518 }
4519 
4520 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4521   assert(is_instance(mname), "wrong type");
4522   return (intptr_t) mname->address_field(_vmindex_offset);
4523 }
4524 
4525 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4526   assert(is_instance(mname), "wrong type");
4527   mname->address_field_put(_vmindex_offset, (address) index);
4528 }
4529 
4530 
4531 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4532   assert(is_instance(resolved_method), "wrong type");

5485   if (!ik->find_local_field(f_name, f_sig, &fd)) {
5486     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
5487     return false;
5488   }
5489   if (fd.is_static()) {
5490     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
5491     return false;
5492   }
5493   if (fd.offset() == deserialized_offset ) {
5494     return true;
5495   } else {
5496     tty->print_cr("Offset of nonstatic field %s.%s is deserialized as %d but should really be %d.",
5497                   klass_name, field_name, deserialized_offset, fd.offset());
5498     return false;
5499   }
5500 }
5501 
5502 void JavaClasses::check_offsets() {
5503   bool valid = true;
5504 
5505 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5506   valid &= check_offset(klass_name, cpp_klass_name :: _##field_name ## _offset, #field_name, field_sig)
5507 
5508 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5509   valid &= check_offset(klass_name, cpp_klass_name :: _##long_ ## field_name ## _offset, #field_name, field_sig)
5510 
5511   // Boxed primitive objects (java_lang_boxing_object)
5512 
5513   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
5514   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
5515   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
5516   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
5517   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
5518   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
5519   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
5520   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
5521 
5522   if (!valid) vm_exit_during_initialization("Field offset verification failed");
5523 }
5524 
5525 #endif // PRODUCT
5526 
5527 int InjectedField::compute_offset() {
5528   InstanceKlass* ik = InstanceKlass::cast(klass());
5529   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5530     if (!may_be_java && !fs.field_flags().is_injected()) {
5531       // Only look at injected fields
5532       continue;
5533     }
5534     if (fs.name() == name() && fs.signature() == signature()) {
5535       return fs.offset();
5536     }
5537   }
5538   ResourceMark rm;
5539   tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
5540 #ifndef PRODUCT

  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"

1059 void java_lang_Class::allocate_mirror(Klass* k, bool is_scratch, Handle protection_domain, Handle classData,
1060                                       Handle& mirror, Handle& comp_mirror, TRAPS) {
1061   // Allocate mirror (java.lang.Class instance)
1062   oop mirror_oop = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(k, CHECK);
1063   mirror = Handle(THREAD, mirror_oop);
1064 
1065   // Setup indirection from mirror->klass
1066   set_klass(mirror(), k);
1067 
1068   // Set the modifiers flag.
1069   u2 computed_modifiers = k->compute_modifier_flags();
1070   set_modifiers(mirror(), computed_modifiers);
1071 
1072   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
1073   assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
1074 
1075   set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
1076 
1077   // It might also have a component mirror.  This mirror must already exist.
1078   if (k->is_array_klass()) {
1079     if (k->is_flatArray_klass()) {
1080       Klass* element_klass = (Klass*) FlatArrayKlass::cast(k)->element_klass();
1081       assert(element_klass->is_inline_klass(), "Must be inline type component");
1082       if (is_scratch) {
1083         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1084       } else {
1085         comp_mirror = Handle(THREAD, element_klass->java_mirror());
1086       }
1087     } else if (k->is_typeArray_klass()) {
1088       BasicType type = TypeArrayKlass::cast(k)->element_type();
1089       if (is_scratch) {
1090         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(type));
1091       } else {
1092         comp_mirror = Handle(THREAD, Universe::java_mirror(type));
1093       }
1094     } else {
1095       assert(k->is_objArray_klass(), "Must be");
1096       Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1097       assert(element_klass != nullptr, "Must have an element klass");
1098       oop comp_oop = element_klass->java_mirror();
1099       if (is_scratch) {
1100         comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1101       } else {
1102         comp_mirror = Handle(THREAD, comp_oop);
1103       }
1104     }
1105     assert(comp_mirror() != nullptr, "must have a mirror");
1106 
1107     // Two-way link between the array klass and its component mirror:
1108     // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1109     set_component_mirror(mirror(), comp_mirror());
1110     // See below for ordering dependencies between field array_klass in component mirror
1111     // and java_mirror in this klass.
1112   } else {
1113     assert(k->is_instance_klass(), "Must be");
1114 
1115     initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1116     if (HAS_PENDING_EXCEPTION) {
1117       // If any of the fields throws an exception like OOM remove the klass field
1118       // from the mirror so GC doesn't follow it after the klass has been deallocated.
1119       // This mirror looks like a primitive type, which logically it is because it
1120       // it represents no class.
1121       set_klass(mirror(), nullptr);
1122       return;

1138 
1139     allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1140 
1141     // set the classLoader field in the java_lang_Class instance
1142     assert(class_loader() == k->class_loader(), "should be same");
1143     set_class_loader(mirror(), class_loader());
1144 
1145     // Setup indirection from klass->mirror
1146     // after any exceptions can happen during allocations.
1147     k->set_java_mirror(mirror);
1148 
1149     // Set the module field in the java_lang_Class instance.  This must be done
1150     // after the mirror is set.
1151     set_mirror_module_field(THREAD, k, mirror, module);
1152 
1153     if (comp_mirror() != nullptr) {
1154       // Set after k->java_mirror() is published, because compiled code running
1155       // concurrently doesn't expect a k to have a null java_mirror.
1156       release_set_array_klass(comp_mirror(), k);
1157     }
1158 
1159     if (CDSConfig::is_dumping_heap()) {
1160       create_scratch_mirror(k, CHECK);
1161     }
1162   } else {
1163     assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1164     fixup_mirror_list()->push(k);
1165   }
1166 }
1167 
1168 #if INCLUDE_CDS_JAVA_HEAP
1169 // The "scratch mirror" stores the states of the mirror object that can be
1170 // decided at dump time (such as the initial values of the static fields, the
1171 // component mirror, etc). At runtime, more information is added to it by
1172 // java_lang_Class::restore_archived_mirror().
1173 //
1174 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1175 // produces the same result as /*runtime*/create_mirror().
1176 //
1177 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1178 // latter may contain dumptime-specific information that cannot be archived
1179 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1180 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) {
1181   if ((k->class_loader() != nullptr &&
1182        k->class_loader() != SystemDictionary::java_platform_loader() &&
1183        k->class_loader() != SystemDictionary::java_system_loader())) {
1184     // We only archive the mirrors of classes loaded by the built-in loaders
1185     return;
1186   }
1187 
1188   Handle protection_domain, classData; // set to null. Will be reinitialized at runtime
1189   Handle mirror;
1190   Handle comp_mirror;
1191   allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1192 
1193   if (comp_mirror() != nullptr) {
1194     release_set_array_klass(comp_mirror(), k);
1195   }
1196 
1197   HeapShared::set_scratch_java_mirror(k, mirror());
1198 }
1199 
1200 // Returns true if the mirror is updated, false if no archived mirror
1201 // data is present. After the archived mirror object is restored, the
1202 // shared klass' _has_raw_archived_mirror flag is cleared.
1203 bool java_lang_Class::restore_archived_mirror(Klass *k,

1391   assert(is_instance(java_class), "must be a Class object");
1392   java_class->metadata_field_put(_klass_offset, klass);
1393 }
1394 
1395 
1396 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1397   assert(is_instance(java_class), "must be a Class object");
1398   Symbol* name = nullptr;
1399   bool is_instance = false;
1400   if (is_primitive(java_class)) {
1401     name = vmSymbols::type_signature(primitive_type(java_class));
1402   } else {
1403     Klass* k = as_Klass(java_class);
1404     is_instance = k->is_instance_klass();
1405     name = k->name();
1406   }
1407   if (name == nullptr) {
1408     st->print("<null>");
1409     return;
1410   }
1411   if (is_instance)  {
1412     st->print("L");
1413   }
1414   st->write((char*) name->base(), (int) name->utf8_length());
1415   if (is_instance)  st->print(";");
1416 }
1417 
1418 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1419   assert(is_instance(java_class), "must be a Class object");
1420   Symbol* name;
1421   if (is_primitive(java_class)) {
1422     name = vmSymbols::type_signature(primitive_type(java_class));
1423     // Because this can create a new symbol, the caller has to decrement
1424     // the refcount, so make adjustment here and below for symbols returned
1425     // that are not created or incremented due to a successful lookup.
1426     name->increment_refcount();
1427   } else {
1428     Klass* k = as_Klass(java_class);
1429     if (!k->is_instance_klass()) {
1430       name = k->name();
1431       name->increment_refcount();
1432     } else {
1433       ResourceMark rm;

1452   if (is_primitive(java_class)) {
1453     name = type2name(primitive_type(java_class));
1454   } else {
1455     name = as_Klass(java_class)->external_name();
1456   }
1457   if (name == nullptr) {
1458     name = "<null>";
1459   }
1460   return name;
1461 }
1462 
1463 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1464   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1465   assert(k == nullptr || (k->is_klass() && k->is_array_klass()), "should be array klass");
1466   return k;
1467 }
1468 
1469 
1470 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1471   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1472   if (klass->is_flatArray_klass() || (ArrayKlass::cast(klass)->is_null_free_array_klass())) {
1473     // TODO 8336006 Ignore flat / null-free arrays
1474     return;
1475   }
1476   java_class->release_metadata_field_put(_array_klass_offset, klass);
1477 }
1478 
1479 
1480 BasicType java_lang_Class::primitive_type(oop java_class) {
1481   assert(is_primitive(java_class), "just checking");
1482   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1483   BasicType type = T_VOID;
1484   if (ak != nullptr) {
1485     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1486     type = ArrayKlass::cast(ak)->element_type();
1487   } else {
1488     assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1489   }
1490 #ifdef ASSERT
1491   if (CDSConfig::is_dumping_heap()) {
1492     oop mirror = Universe::java_mirror(type);
1493     oop scratch_mirror = HeapShared::scratch_java_mirror(type);
1494     assert(java_class == mirror || java_class == scratch_mirror, "must be consistent");
1495   } else {

2841 
2842     // the format of the stacktrace will be:
2843     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2844     // - 0 or more <init> methods for the exception class (skipped)
2845     // - rest of the stack
2846 
2847     if (!skip_fillInStackTrace_check) {
2848       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2849           throwable->is_a(method->method_holder())) {
2850         continue;
2851       }
2852       else {
2853         skip_fillInStackTrace_check = true; // gone past them all
2854       }
2855     }
2856     if (!skip_throwableInit_check) {
2857       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2858 
2859       // skip <init> methods of the exception class and superclasses
2860       // This is similar to classic VM.
2861       if (method->is_object_constructor() &&
2862           throwable->is_a(method->method_holder())) {
2863         continue;
2864       } else {
2865         // there are none or we've seen them all - either way stop checking
2866         skip_throwableInit_check = true;
2867       }
2868     }
2869     if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2870       if (skip_hidden) {
2871         if (total_count == 0) {
2872           // The top frame will be hidden from the stack trace.
2873           bt.set_has_hidden_top_frame();
2874         }
2875         continue;
2876       }
2877     }
2878 
2879     bt.push(method, bci, CHECK);
2880     total_count++;
2881   }

3200 int java_lang_ClassFrameInfo::_classOrMemberName_offset;
3201 int java_lang_ClassFrameInfo::_flags_offset;
3202 
3203 #define CLASSFRAMEINFO_FIELDS_DO(macro) \
3204   macro(_classOrMemberName_offset, k, "classOrMemberName", object_signature,  false); \
3205   macro(_flags_offset,             k, vmSymbols::flags_name(), int_signature, false)
3206 
3207 void java_lang_ClassFrameInfo::compute_offsets() {
3208   InstanceKlass* k = vmClasses::ClassFrameInfo_klass();
3209   CLASSFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3210 }
3211 
3212 #if INCLUDE_CDS
3213 void java_lang_ClassFrameInfo::serialize_offsets(SerializeClosure* f) {
3214   CLASSFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3215 }
3216 #endif
3217 
3218 static int get_flags(const methodHandle& m) {
3219   int flags = m->access_flags().as_method_flags();
3220   if (m->is_object_constructor()) {
3221     flags |= java_lang_invoke_MemberName::MN_IS_OBJECT_CONSTRUCTOR;
3222   } else {
3223     // Note: Static initializers can be here. Record them as plain methods.
3224     flags |= java_lang_invoke_MemberName::MN_IS_METHOD;
3225   }
3226   if (m->caller_sensitive()) {
3227     flags |= java_lang_invoke_MemberName::MN_CALLER_SENSITIVE;
3228   }
3229   if (m->is_hidden()) {
3230     flags |= java_lang_invoke_MemberName::MN_HIDDEN_MEMBER;
3231   }
3232   assert((flags & 0xFF000000) == 0, "unexpected flags");
3233   return flags;
3234 }
3235 
3236 oop java_lang_ClassFrameInfo::classOrMemberName(oop obj) {
3237   return obj->obj_field(_classOrMemberName_offset);
3238 }
3239 
3240 int java_lang_ClassFrameInfo::flags(oop obj) {
3241   return obj->int_field(_flags_offset);

3592   constructor->int_field_put(_modifiers_offset, value);
3593 }
3594 
3595 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3596   constructor->obj_field_put(_signature_offset, value);
3597 }
3598 
3599 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3600   constructor->obj_field_put(_annotations_offset, value);
3601 }
3602 
3603 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3604   method->obj_field_put(_parameter_annotations_offset, value);
3605 }
3606 
3607 int java_lang_reflect_Field::_clazz_offset;
3608 int java_lang_reflect_Field::_name_offset;
3609 int java_lang_reflect_Field::_type_offset;
3610 int java_lang_reflect_Field::_slot_offset;
3611 int java_lang_reflect_Field::_modifiers_offset;
3612 int java_lang_reflect_Field::_flags_offset;
3613 int java_lang_reflect_Field::_signature_offset;
3614 int java_lang_reflect_Field::_annotations_offset;
3615 
3616 #define FIELD_FIELDS_DO(macro) \
3617   macro(_clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
3618   macro(_name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
3619   macro(_type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
3620   macro(_slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
3621   macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
3622   macro(_flags_offset,     k, vmSymbols::flags_name(),     int_signature,    false); \
3623   macro(_signature_offset,        k, vmSymbols::signature_name(),        string_signature,     false); \
3624   macro(_annotations_offset,      k, vmSymbols::annotations_name(),      byte_array_signature, false);
3625 
3626 void java_lang_reflect_Field::compute_offsets() {
3627   InstanceKlass* k = vmClasses::reflect_Field_klass();
3628   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3629 }
3630 
3631 #if INCLUDE_CDS
3632 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3633   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3634 }
3635 #endif
3636 
3637 Handle java_lang_reflect_Field::create(TRAPS) {
3638   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3639   Symbol* name = vmSymbols::java_lang_reflect_Field();
3640   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3641   InstanceKlass* ik = InstanceKlass::cast(k);
3642   // Ensure it is initialized

3667 void java_lang_reflect_Field::set_type(oop field, oop value) {
3668   field->obj_field_put(_type_offset, value);
3669 }
3670 
3671 int java_lang_reflect_Field::slot(oop reflect) {
3672   return reflect->int_field(_slot_offset);
3673 }
3674 
3675 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3676   reflect->int_field_put(_slot_offset, value);
3677 }
3678 
3679 int java_lang_reflect_Field::modifiers(oop field) {
3680   return field->int_field(_modifiers_offset);
3681 }
3682 
3683 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3684   field->int_field_put(_modifiers_offset, value);
3685 }
3686 
3687 void java_lang_reflect_Field::set_flags(oop field, int value) {
3688   field->int_field_put(_flags_offset, value);
3689 }
3690 
3691 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3692   field->obj_field_put(_signature_offset, value);
3693 }
3694 
3695 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3696   field->obj_field_put(_annotations_offset, value);
3697 }
3698 
3699 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3700   // Allocate java.lang.reflect.RecordComponent instance
3701   HandleMark hm(THREAD);
3702   InstanceKlass* ik = vmClasses::RecordComponent_klass();
3703   assert(ik != nullptr, "must be loaded");
3704   ik->initialize(CHECK_NULL);
3705 
3706   Handle element = ik->allocate_instance_handle(CHECK_NULL);
3707 
3708   Handle decl_class(THREAD, holder->java_mirror());

3971 }
3972 #endif
3973 
3974 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
3975   assert(obj != nullptr, "sanity");
3976   if (offset != _referent_offset) {
3977     return false;
3978   }
3979 
3980   Klass* k = obj->klass();
3981   if (!k->is_instance_klass()) {
3982     return false;
3983   }
3984 
3985   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
3986   bool is_reference = ik->reference_type() != REF_NONE;
3987   assert(!is_reference || ik->is_subclass_of(vmClasses::Reference_klass()), "sanity");
3988   return is_reference;
3989 }
3990 
3991 int* java_lang_boxing_object::_offsets;

3992 
3993 #define BOXING_FIELDS_DO(macro)                                                                                                    \
3994   macro(java_lang_boxing_object::_offsets[T_BOOLEAN - T_BOOLEAN], vmClasses::Boolean_klass(),   "value", bool_signature,   false); \
3995   macro(java_lang_boxing_object::_offsets[T_CHAR - T_BOOLEAN],    vmClasses::Character_klass(), "value", char_signature,   false); \
3996   macro(java_lang_boxing_object::_offsets[T_FLOAT - T_BOOLEAN],   vmClasses::Float_klass(),     "value", float_signature,  false); \
3997   macro(java_lang_boxing_object::_offsets[T_DOUBLE - T_BOOLEAN],  vmClasses::Double_klass(),    "value", double_signature, false); \
3998   macro(java_lang_boxing_object::_offsets[T_BYTE - T_BOOLEAN],    vmClasses::Byte_klass(),      "value", byte_signature,   false); \
3999   macro(java_lang_boxing_object::_offsets[T_SHORT - T_BOOLEAN],   vmClasses::Short_klass(),     "value", short_signature,  false); \
4000   macro(java_lang_boxing_object::_offsets[T_INT - T_BOOLEAN],     vmClasses::Integer_klass(),   "value", int_signature,    false); \
4001   macro(java_lang_boxing_object::_offsets[T_LONG - T_BOOLEAN],    vmClasses::Long_klass(),      "value", long_signature,   false);
4002 
4003 void java_lang_boxing_object::compute_offsets() {
4004   assert(T_LONG - T_BOOLEAN == 7, "Sanity check");
4005   java_lang_boxing_object::_offsets = NEW_C_HEAP_ARRAY(int, 8, mtInternal);
4006   BOXING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4007 }
4008 
4009 #if INCLUDE_CDS
4010 void java_lang_boxing_object::serialize_offsets(SerializeClosure* f) {
4011   if (f->reading()) {
4012     assert(T_LONG - T_BOOLEAN == 7, "Sanity check");
4013     java_lang_boxing_object::_offsets = NEW_C_HEAP_ARRAY(int, 8, mtInternal);
4014   }
4015   BOXING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4016 }
4017 #endif
4018 
4019 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
4020   Klass* k = vmClasses::box_klass(type);
4021   if (k == nullptr)  return nullptr;
4022   InstanceKlass* ik = InstanceKlass::cast(k);
4023   if (!ik->is_initialized()) {
4024     ik->initialize(CHECK_NULL);
4025   }
4026   return ik->allocate_instance(THREAD);
4027 }
4028 
4029 
4030 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
4031   oop box = initialize_and_allocate(type, CHECK_NULL);
4032   if (box == nullptr)  return nullptr;
4033   switch (type) {
4034     case T_BOOLEAN:
4035       box->bool_field_put(value_offset(type), value->z);
4036       break;
4037     case T_CHAR:
4038       box->char_field_put(value_offset(type), value->c);
4039       break;
4040     case T_FLOAT:
4041       box->float_field_put(value_offset(type), value->f);
4042       break;
4043     case T_DOUBLE:
4044       box->double_field_put(value_offset(type), value->d);
4045       break;
4046     case T_BYTE:
4047       box->byte_field_put(value_offset(type), value->b);
4048       break;
4049     case T_SHORT:
4050       box->short_field_put(value_offset(type), value->s);
4051       break;
4052     case T_INT:
4053       box->int_field_put(value_offset(type), value->i);
4054       break;
4055     case T_LONG:
4056       box->long_field_put(value_offset(type), value->j);
4057       break;
4058     default:
4059       return nullptr;
4060   }
4061   return box;
4062 }
4063 
4064 
4065 BasicType java_lang_boxing_object::basic_type(oop box) {
4066   if (box == nullptr)  return T_ILLEGAL;
4067   BasicType type = vmClasses::box_klass_type(box->klass());
4068   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
4069     return T_ILLEGAL;
4070   return type;
4071 }
4072 
4073 
4074 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
4075   BasicType type = vmClasses::box_klass_type(box->klass());
4076   switch (type) {
4077   case T_BOOLEAN:
4078     value->z = box->bool_field(value_offset(type));
4079     break;
4080   case T_CHAR:
4081     value->c = box->char_field(value_offset(type));
4082     break;
4083   case T_FLOAT:
4084     value->f = box->float_field(value_offset(type));
4085     break;
4086   case T_DOUBLE:
4087     value->d = box->double_field(value_offset(type));
4088     break;
4089   case T_BYTE:
4090     value->b = box->byte_field(value_offset(type));
4091     break;
4092   case T_SHORT:
4093     value->s = box->short_field(value_offset(type));
4094     break;
4095   case T_INT:
4096     value->i = box->int_field(value_offset(type));
4097     break;
4098   case T_LONG:
4099     value->j = box->long_field(value_offset(type));
4100     break;
4101   default:
4102     return T_ILLEGAL;
4103   } // end switch
4104   return type;
4105 }
4106 
4107 
4108 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
4109   BasicType type = vmClasses::box_klass_type(box->klass());
4110   switch (type) {
4111   case T_BOOLEAN:
4112     box->bool_field_put(value_offset(type), value->z);
4113     break;
4114   case T_CHAR:
4115     box->char_field_put(value_offset(type), value->c);
4116     break;
4117   case T_FLOAT:
4118     box->float_field_put(value_offset(type), value->f);
4119     break;
4120   case T_DOUBLE:
4121     box->double_field_put(value_offset(type), value->d);
4122     break;
4123   case T_BYTE:
4124     box->byte_field_put(value_offset(type), value->b);
4125     break;
4126   case T_SHORT:
4127     box->short_field_put(value_offset(type), value->s);
4128     break;
4129   case T_INT:
4130     box->int_field_put(value_offset(type), value->i);
4131     break;
4132   case T_LONG:
4133     box->long_field_put(value_offset(type), value->j);
4134     break;
4135   default:
4136     return T_ILLEGAL;
4137   } // end switch
4138   return type;
4139 }
4140 
4141 
4142 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
4143   switch (type) {
4144   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
4145   case T_CHAR:      st->print("%d", value->c);                      break;
4146   case T_BYTE:      st->print("%d", value->b);                      break;
4147   case T_SHORT:     st->print("%d", value->s);                      break;
4148   case T_INT:       st->print("%d", value->i);                      break;
4149   case T_LONG:      st->print(JLONG_FORMAT, value->j);              break;
4150   case T_FLOAT:     st->print("%f", value->f);                      break;
4151   case T_DOUBLE:    st->print("%lf", value->d);                     break;
4152   default:          st->print("type %d?", type);                    break;
4153   }

4509 oop java_lang_invoke_MemberName::type(oop mname) {
4510   assert(is_instance(mname), "wrong type");
4511   return mname->obj_field(_type_offset);
4512 }
4513 
4514 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
4515   assert(is_instance(mname), "wrong type");
4516   mname->obj_field_put(_type_offset, type);
4517 }
4518 
4519 int java_lang_invoke_MemberName::flags(oop mname) {
4520   assert(is_instance(mname), "wrong type");
4521   return mname->int_field(_flags_offset);
4522 }
4523 
4524 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4525   assert(is_instance(mname), "wrong type");
4526   mname->int_field_put(_flags_offset, flags);
4527 }
4528 

4529 // Return vmtarget from ResolvedMethodName method field through indirection
4530 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4531   assert(is_instance(mname), "wrong type");
4532   oop method = mname->obj_field(_method_offset);
4533   return method == nullptr ? nullptr : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4534 }
4535 
4536 bool java_lang_invoke_MemberName::is_method(oop mname) {
4537   assert(is_instance(mname), "must be MemberName");
4538   return (flags(mname) & (MN_IS_METHOD | MN_IS_OBJECT_CONSTRUCTOR)) > 0;
4539 }
4540 
4541 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4542   assert(is_instance(mname), "wrong type");
4543   mname->obj_field_put(_method_offset, resolved_method);
4544 }
4545 
4546 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4547   assert(is_instance(mname), "wrong type");
4548   return (intptr_t) mname->address_field(_vmindex_offset);
4549 }
4550 
4551 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4552   assert(is_instance(mname), "wrong type");
4553   mname->address_field_put(_vmindex_offset, (address) index);
4554 }
4555 
4556 
4557 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4558   assert(is_instance(resolved_method), "wrong type");

5511   if (!ik->find_local_field(f_name, f_sig, &fd)) {
5512     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
5513     return false;
5514   }
5515   if (fd.is_static()) {
5516     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
5517     return false;
5518   }
5519   if (fd.offset() == deserialized_offset ) {
5520     return true;
5521   } else {
5522     tty->print_cr("Offset of nonstatic field %s.%s is deserialized as %d but should really be %d.",
5523                   klass_name, field_name, deserialized_offset, fd.offset());
5524     return false;
5525   }
5526 }
5527 
5528 void JavaClasses::check_offsets() {
5529   bool valid = true;
5530 
5531 #define CHECK_OFFSET(klass_name, type, field_sig) \
5532   valid &= check_offset(klass_name, java_lang_boxing_object::value_offset(type), "value", field_sig)
5533 
5534   CHECK_OFFSET("java/lang/Boolean",   T_BOOLEAN, "Z");
5535   CHECK_OFFSET("java/lang/Character", T_CHAR,    "C");
5536   CHECK_OFFSET("java/lang/Float",     T_FLOAT,   "F");
5537   CHECK_OFFSET("java/lang/Double",    T_DOUBLE,  "D");
5538   CHECK_OFFSET("java/lang/Byte",      T_BYTE,    "B");
5539   CHECK_OFFSET("java/lang/Short",     T_SHORT,   "S");
5540   CHECK_OFFSET("java/lang/Integer",   T_INT,     "I");
5541   CHECK_OFFSET("java/lang/Long",      T_LONG,    "J");





5542 
5543   if (!valid) vm_exit_during_initialization("Field offset verification failed");
5544 }
5545 
5546 #endif // PRODUCT
5547 
5548 int InjectedField::compute_offset() {
5549   InstanceKlass* ik = InstanceKlass::cast(klass());
5550   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5551     if (!may_be_java && !fs.field_flags().is_injected()) {
5552       // Only look at injected fields
5553       continue;
5554     }
5555     if (fs.name() == name() && fs.signature() == signature()) {
5556       return fs.offset();
5557     }
5558   }
5559   ResourceMark rm;
5560   tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
5561 #ifndef PRODUCT
< prev index next >