46 #include "oops/klass.hpp"
47 #include "oops/method.inline.hpp"
48 #include "oops/objArrayOop.inline.hpp"
49 #include "oops/oop.inline.hpp"
50 #include "oops/symbol.hpp"
51 #include "oops/typeArrayOop.inline.hpp"
52 #include "prims/resolvedMethodTable.hpp"
53 #include "runtime/fieldDescriptor.hpp"
54 #include "runtime/frame.inline.hpp"
55 #include "runtime/handles.inline.hpp"
56 #include "runtime/interfaceSupport.inline.hpp"
57 #include "runtime/java.hpp"
58 #include "runtime/javaCalls.hpp"
59 #include "runtime/jniHandles.inline.hpp"
60 #include "runtime/safepoint.hpp"
61 #include "runtime/safepointVerifiers.hpp"
62 #include "runtime/thread.inline.hpp"
63 #include "runtime/vframe.inline.hpp"
64 #include "utilities/align.hpp"
65 #include "utilities/preserveException.hpp"
66
67 #if INCLUDE_JVMCI
68 #include "jvmci/jvmciJavaClasses.hpp"
69 #endif
70
71 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java) \
72 klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum);
73
74 #if INCLUDE_CDS
75 #define INJECTED_FIELD_SERIALIZE_OFFSET(klass, name, signature, may_be_java) \
76 f->do_u4((u4*)&_##name##_offset);
77 #endif
78
79 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java) \
80 { SystemDictionary::WK_KLASS_ENUM_NAME(klass), vmSymbols::VM_SYMBOL_ENUM_NAME(name##_name), vmSymbols::VM_SYMBOL_ENUM_NAME(signature), may_be_java },
81
82 InjectedField JavaClasses::_injected_fields[] = {
83 ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
84 };
85
86 int JavaClasses::compute_injected_offset(InjectedFieldID id) {
792 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) {
793 assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
794
795 // If the offset was read from the shared archive, it was fixed up already
796 if (!k->is_shared()) {
797 if (k->is_instance_klass()) {
798 // During bootstrap, java.lang.Class wasn't loaded so static field
799 // offsets were computed without the size added it. Go back and
800 // update all the static field offsets to included the size.
801 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
802 if (fs.access_flags().is_static()) {
803 int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
804 fs.set_offset(real_offset);
805 }
806 }
807 }
808 }
809
810 if (k->is_shared() && k->has_raw_archived_mirror()) {
811 if (MetaspaceShared::open_archive_heap_region_mapped()) {
812 oop m = k->archived_java_mirror();
813 assert(m != NULL, "archived mirror is NULL");
814 assert(MetaspaceShared::is_archive_object(m), "must be archived mirror object");
815 Handle m_h(THREAD, m);
816 // restore_archived_mirror() clears the klass' _has_raw_archived_mirror flag
817 restore_archived_mirror(k, m_h, Handle(), Handle(), Handle(), CHECK);
818 return;
819 } else {
820 k->set_java_mirror_handle(NULL);
821 k->clear_has_raw_archived_mirror();
822 }
823 }
824 create_mirror(k, Handle(), Handle(), Handle(), CHECK);
825 }
826
827 void java_lang_Class::initialize_mirror_fields(Klass* k,
828 Handle mirror,
829 Handle protection_domain,
830 TRAPS) {
831 // Allocate a simple java object for a lock.
832 // This needs to be a java object because during class initialization
833 // it can be held across a java call.
834 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
835 set_init_lock(mirror(), r);
836
837 // Set protection domain also
1190 // The archived mirror's field at _klass_offset is still pointing to the original
1191 // klass. Updated the field in the archived mirror to point to the relocated
1192 // klass in the archive.
1193 Klass *reloc_k = MetaspaceShared::get_relocated_klass(as_Klass(mirror));
1194 log_debug(cds, mirror)("Relocate mirror metadata field at _klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1195 p2i(as_Klass(mirror)), p2i(reloc_k));
1196 archived_mirror->metadata_field_put(_klass_offset, reloc_k);
1197
1198 // The field at _array_klass_offset is pointing to the original one dimension
1199 // higher array klass if exists. Relocate the pointer.
1200 Klass *arr = array_klass_acquire(mirror);
1201 if (arr != NULL) {
1202 Klass *reloc_arr = MetaspaceShared::get_relocated_klass(arr);
1203 log_debug(cds, mirror)("Relocate mirror metadata field at _array_klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1204 p2i(arr), p2i(reloc_arr));
1205 archived_mirror->metadata_field_put(_array_klass_offset, reloc_arr);
1206 }
1207 return archived_mirror;
1208 }
1209
1210 // After the archived mirror object is restored, the shared klass'
1211 // _has_raw_archived_mirror flag is cleared
1212 void java_lang_Class::restore_archived_mirror(Klass *k, Handle mirror,
1213 Handle class_loader, Handle module,
1214 Handle protection_domain, TRAPS) {
1215
1216 // The java.lang.Class field offsets were archived and reloaded from archive.
1217 // No need to put classes on the fixup_mirror_list before java.lang.Class
1218 // is loaded.
1219
1220 if (!k->is_array_klass()) {
1221 // - local static final fields with initial values were initialized at dump time
1222
1223 // create the init_lock
1224 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
1225 set_init_lock(mirror(), r);
1226
1227 if (protection_domain.not_null()) {
1228 set_protection_domain(mirror(), protection_domain());
1229 }
1230 }
1231
1232 assert(class_loader() == k->class_loader(), "should be same");
1233 if (class_loader.not_null()) {
1234 set_class_loader(mirror(), class_loader());
1235 }
1236
1237 k->set_java_mirror(mirror);
1238 k->clear_has_raw_archived_mirror();
1239
1240 set_mirror_module_field(k, mirror, module, THREAD);
1241
1242 ResourceMark rm;
1243 log_trace(cds, mirror)("Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1244 }
1245 #endif // INCLUDE_CDS_JAVA_HEAP
1246
1247 void java_lang_Class::fixup_module_field(Klass* k, Handle module) {
1248 assert(_module_offset != 0, "must have been computed already");
1249 java_lang_Class::set_module(k->java_mirror(), module());
1250 }
1251
1252 int java_lang_Class::oop_size(oop java_class) {
1253 assert(_oop_size_offset != 0, "must be set");
1254 int size = java_class->int_field(_oop_size_offset);
1255 assert(size > 0, "Oop size must be greater than zero, not %d", size);
1256 return size;
1257 }
1258
1259 void java_lang_Class::set_oop_size(oop java_class, int size) {
1260 assert(_oop_size_offset != 0, "must be set");
1261 assert(size > 0, "Oop size must be greater than zero, not %d", size);
1262 java_class->int_field_put(_oop_size_offset, size);
1263 }
|
46 #include "oops/klass.hpp"
47 #include "oops/method.inline.hpp"
48 #include "oops/objArrayOop.inline.hpp"
49 #include "oops/oop.inline.hpp"
50 #include "oops/symbol.hpp"
51 #include "oops/typeArrayOop.inline.hpp"
52 #include "prims/resolvedMethodTable.hpp"
53 #include "runtime/fieldDescriptor.hpp"
54 #include "runtime/frame.inline.hpp"
55 #include "runtime/handles.inline.hpp"
56 #include "runtime/interfaceSupport.inline.hpp"
57 #include "runtime/java.hpp"
58 #include "runtime/javaCalls.hpp"
59 #include "runtime/jniHandles.inline.hpp"
60 #include "runtime/safepoint.hpp"
61 #include "runtime/safepointVerifiers.hpp"
62 #include "runtime/thread.inline.hpp"
63 #include "runtime/vframe.inline.hpp"
64 #include "utilities/align.hpp"
65 #include "utilities/preserveException.hpp"
66 #if INCLUDE_JVMCI
67 #include "jvmci/jvmciJavaClasses.hpp"
68 #endif
69
70 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java) \
71 klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum);
72
73 #if INCLUDE_CDS
74 #define INJECTED_FIELD_SERIALIZE_OFFSET(klass, name, signature, may_be_java) \
75 f->do_u4((u4*)&_##name##_offset);
76 #endif
77
78 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java) \
79 { SystemDictionary::WK_KLASS_ENUM_NAME(klass), vmSymbols::VM_SYMBOL_ENUM_NAME(name##_name), vmSymbols::VM_SYMBOL_ENUM_NAME(signature), may_be_java },
80
81 InjectedField JavaClasses::_injected_fields[] = {
82 ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
83 };
84
85 int JavaClasses::compute_injected_offset(InjectedFieldID id) {
791 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) {
792 assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
793
794 // If the offset was read from the shared archive, it was fixed up already
795 if (!k->is_shared()) {
796 if (k->is_instance_klass()) {
797 // During bootstrap, java.lang.Class wasn't loaded so static field
798 // offsets were computed without the size added it. Go back and
799 // update all the static field offsets to included the size.
800 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
801 if (fs.access_flags().is_static()) {
802 int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
803 fs.set_offset(real_offset);
804 }
805 }
806 }
807 }
808
809 if (k->is_shared() && k->has_raw_archived_mirror()) {
810 if (MetaspaceShared::open_archive_heap_region_mapped()) {
811 bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK);
812 assert(present, "Missing archived mirror for %s", k->external_name());
813 return;
814 } else {
815 k->set_java_mirror_handle(NULL);
816 k->clear_has_raw_archived_mirror();
817 }
818 }
819 create_mirror(k, Handle(), Handle(), Handle(), CHECK);
820 }
821
822 void java_lang_Class::initialize_mirror_fields(Klass* k,
823 Handle mirror,
824 Handle protection_domain,
825 TRAPS) {
826 // Allocate a simple java object for a lock.
827 // This needs to be a java object because during class initialization
828 // it can be held across a java call.
829 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
830 set_init_lock(mirror(), r);
831
832 // Set protection domain also
1185 // The archived mirror's field at _klass_offset is still pointing to the original
1186 // klass. Updated the field in the archived mirror to point to the relocated
1187 // klass in the archive.
1188 Klass *reloc_k = MetaspaceShared::get_relocated_klass(as_Klass(mirror));
1189 log_debug(cds, mirror)("Relocate mirror metadata field at _klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1190 p2i(as_Klass(mirror)), p2i(reloc_k));
1191 archived_mirror->metadata_field_put(_klass_offset, reloc_k);
1192
1193 // The field at _array_klass_offset is pointing to the original one dimension
1194 // higher array klass if exists. Relocate the pointer.
1195 Klass *arr = array_klass_acquire(mirror);
1196 if (arr != NULL) {
1197 Klass *reloc_arr = MetaspaceShared::get_relocated_klass(arr);
1198 log_debug(cds, mirror)("Relocate mirror metadata field at _array_klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1199 p2i(arr), p2i(reloc_arr));
1200 archived_mirror->metadata_field_put(_array_klass_offset, reloc_arr);
1201 }
1202 return archived_mirror;
1203 }
1204
1205 // Returns true if the mirror is updated, false if no archived mirror
1206 // data is present. After the archived mirror object is restored, the
1207 // shared klass' _has_raw_archived_mirror flag is cleared.
1208 bool java_lang_Class::restore_archived_mirror(Klass *k,
1209 Handle class_loader, Handle module,
1210 Handle protection_domain, TRAPS) {
1211 oop m = MetaspaceShared::unarchive_heap_object(k->archived_java_mirror_raw());
1212
1213 if (m == NULL) {
1214 return false;
1215 }
1216
1217 log_debug(cds, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m));
1218
1219 // mirror is archived, restore
1220 assert(MetaspaceShared::is_archive_object(m), "must be archived mirror object");
1221 Handle mirror(THREAD, m);
1222
1223 // The java.lang.Class field offsets were archived and reloaded from archive.
1224 // No need to put classes on the fixup_mirror_list before java.lang.Class
1225 // is loaded.
1226
1227 if (!k->is_array_klass()) {
1228 // - local static final fields with initial values were initialized at dump time
1229
1230 // create the init_lock
1231 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false));
1232 set_init_lock(mirror(), r);
1233
1234 if (protection_domain.not_null()) {
1235 set_protection_domain(mirror(), protection_domain());
1236 }
1237 }
1238
1239 assert(class_loader() == k->class_loader(), "should be same");
1240 if (class_loader.not_null()) {
1241 set_class_loader(mirror(), class_loader());
1242 }
1243
1244 k->set_java_mirror(mirror);
1245 k->clear_has_raw_archived_mirror();
1246
1247 set_mirror_module_field(k, mirror, module, THREAD);
1248
1249 ResourceMark rm;
1250 log_trace(cds, mirror)("Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1251
1252 return true;
1253 }
1254 #endif // INCLUDE_CDS_JAVA_HEAP
1255
1256 void java_lang_Class::fixup_module_field(Klass* k, Handle module) {
1257 assert(_module_offset != 0, "must have been computed already");
1258 java_lang_Class::set_module(k->java_mirror(), module());
1259 }
1260
1261 int java_lang_Class::oop_size(oop java_class) {
1262 assert(_oop_size_offset != 0, "must be set");
1263 int size = java_class->int_field(_oop_size_offset);
1264 assert(size > 0, "Oop size must be greater than zero, not %d", size);
1265 return size;
1266 }
1267
1268 void java_lang_Class::set_oop_size(oop java_class, int size) {
1269 assert(_oop_size_offset != 0, "must be set");
1270 assert(size > 0, "Oop size must be greater than zero, not %d", size);
1271 java_class->int_field_put(_oop_size_offset, size);
1272 }
|