871
872
873 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) {
874 assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
875
876 // If the offset was read from the shared archive, it was fixed up already
877 if (!k->is_shared()) {
878 if (k->is_instance_klass()) {
879 // During bootstrap, java.lang.Class wasn't loaded so static field
880 // offsets were computed without the size added it. Go back and
881 // update all the static field offsets to included the size.
882 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
883 if (fs.access_flags().is_static()) {
884 int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
885 fs.set_offset(real_offset);
886 }
887 }
888 }
889 }
890
891 if (k->is_shared() && k->has_raw_archived_mirror()) {
892 if (HeapShared::open_archive_heap_region_mapped()) {
893 bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK);
894 assert(present, "Missing archived mirror for %s", k->external_name());
895 return;
896 } else {
897 k->clear_java_mirror_handle();
898 k->clear_has_raw_archived_mirror();
899 }
900 }
901 create_mirror(k, Handle(), Handle(), Handle(), Handle(), CHECK);
902 }
903
904 void java_lang_Class::initialize_mirror_fields(Klass* k,
905 Handle mirror,
906 Handle protection_domain,
907 Handle classData,
908 TRAPS) {
909 // Allocate a simple java object for a lock.
910 // This needs to be a java object because during class initialization
911 // it can be held across a java call.
912 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
913 set_init_lock(mirror(), r);
914
915 // Set protection domain also
916 set_protection_domain(mirror(), protection_domain());
917
918 // Initialize static fields
1144 "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1145 type2name(bt), p2i(m), p2i(archived_m));
1146
1147 Universe::replace_mirror(bt, archived_m);
1148 }
1149 }
1150 }
1151 //
1152 // After the mirror object is successfully archived, the archived
1153 // klass is set with _has_archived_raw_mirror flag.
1154 //
1155 // The _has_archived_raw_mirror flag is cleared at runtime when the
1156 // archived mirror is restored. If archived java heap data cannot
1157 // be used at runtime, new mirror object is created for the shared
1158 // class. The _has_archived_raw_mirror is cleared also during the process.
1159 oop java_lang_Class::archive_mirror(Klass* k, TRAPS) {
1160 assert(HeapShared::is_heap_object_archiving_allowed(),
1161 "HeapShared::is_heap_object_archiving_allowed() must be true");
1162
1163 // Mirror is already archived
1164 if (k->has_raw_archived_mirror()) {
1165 assert(k->archived_java_mirror_raw() != NULL, "no archived mirror");
1166 return k->archived_java_mirror_raw();
1167 }
1168
1169 // No mirror
1170 oop mirror = k->java_mirror();
1171 if (mirror == NULL) {
1172 return NULL;
1173 }
1174
1175 if (k->is_instance_klass()) {
1176 InstanceKlass *ik = InstanceKlass::cast(k);
1177 assert(ik->signers() == NULL, "class with signer should have been excluded");
1178
1179 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
1180 ik->is_shared_app_class())) {
1181 // Archiving mirror for classes from non-builtin loaders is not
1182 // supported.
1183 return NULL;
1184 }
1185 }
1186
1187 // Now start archiving the mirror object
1188 oop archived_mirror = HeapShared::archive_heap_object(mirror, THREAD);
1189 if (archived_mirror == NULL) {
1190 return NULL;
1191 }
1192
1193 archived_mirror = process_archived_mirror(k, mirror, archived_mirror, THREAD);
1194 if (archived_mirror == NULL) {
1195 return NULL;
1196 }
1197
1198 k->set_archived_java_mirror_raw(archived_mirror);
1199
1200 k->set_has_raw_archived_mirror();
1201
1202 ResourceMark rm;
1203 log_trace(cds, heap, mirror)(
1204 "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1205 k->external_name(), p2i(mirror), p2i(archived_mirror));
1206
1207 return archived_mirror;
1208 }
1209
1210 // The process is based on create_mirror().
1211 oop java_lang_Class::process_archived_mirror(Klass* k, oop mirror,
1212 oop archived_mirror,
1213 Thread *THREAD) {
1214 // Clear nonstatic fields in archived mirror. Some of the fields will be set
1215 // to archived metadata and objects below.
1216 Klass *c = archived_mirror->klass();
1217 Handle archived_mirror_h(THREAD, archived_mirror);
1218 ResetMirrorField reset(archived_mirror_h);
1219 InstanceKlass::cast(c)->do_nonstatic_fields(&reset);
1220
1296 archived_mirror->metadata_field_put(_array_klass_offset,
1297 (Klass*)(address(ak) + MetaspaceShared::relocation_delta()));
1298 }
1299 }
1300
1301
1302 // Returns true if the mirror is updated, false if no archived mirror
1303 // data is present. After the archived mirror object is restored, the
1304 // shared klass' _has_raw_archived_mirror flag is cleared.
1305 bool java_lang_Class::restore_archived_mirror(Klass *k,
1306 Handle class_loader, Handle module,
1307 Handle protection_domain, TRAPS) {
1308 // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1309 // see more details in SystemDictionary::resolve_well_known_classes().
1310 if (!SystemDictionary::Class_klass_loaded()) {
1311 assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
1312 fixup_mirror_list()->push(k);
1313 return true;
1314 }
1315
1316 oop m = HeapShared::materialize_archived_object(k->archived_java_mirror_raw_narrow());
1317 if (m == NULL) {
1318 return false;
1319 }
1320
1321 // mirror is archived, restore
1322 log_debug(cds, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m));
1323 assert(HeapShared::is_archived_object(m), "must be archived mirror object");
1324 assert(as_Klass(m) == k, "must be");
1325 Handle mirror(THREAD, m);
1326
1327 if (!k->is_array_klass()) {
1328 // - local static final fields with initial values were initialized at dump time
1329
1330 // create the init_lock
1331 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false));
1332 set_init_lock(mirror(), r);
1333
1334 if (protection_domain.not_null()) {
1335 set_protection_domain(mirror(), protection_domain());
1336 }
1337 }
1338
1339 assert(class_loader() == k->class_loader(), "should be same");
1340 if (class_loader.not_null()) {
1341 set_class_loader(mirror(), class_loader());
1342 }
1343
1344 k->set_java_mirror(mirror);
1345 k->clear_has_raw_archived_mirror();
1346
1347 set_mirror_module_field(k, mirror, module, THREAD);
1348
1349 if (log_is_enabled(Trace, cds, heap, mirror)) {
1350 ResourceMark rm(THREAD);
1351 log_trace(cds, heap, mirror)(
1352 "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1353 }
1354
1355 return true;
1356 }
1357 #endif // INCLUDE_CDS_JAVA_HEAP
1358
1359 void java_lang_Class::fixup_module_field(Klass* k, Handle module) {
1360 assert(_module_offset != 0, "must have been computed already");
1361 java_lang_Class::set_module(k->java_mirror(), module());
1362 }
1363
1364 int java_lang_Class::oop_size(oop java_class) {
1365 assert(_oop_size_offset != 0, "must be set");
|
871
872
873 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) {
874 assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
875
876 // If the offset was read from the shared archive, it was fixed up already
877 if (!k->is_shared()) {
878 if (k->is_instance_klass()) {
879 // During bootstrap, java.lang.Class wasn't loaded so static field
880 // offsets were computed without the size added it. Go back and
881 // update all the static field offsets to included the size.
882 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
883 if (fs.access_flags().is_static()) {
884 int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
885 fs.set_offset(real_offset);
886 }
887 }
888 }
889 }
890
891 if (k->is_shared() && k->has_archived_mirror_index()) {
892 if (HeapShared::open_archive_heap_region_mapped()) {
893 bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK);
894 assert(present, "Missing archived mirror for %s", k->external_name());
895 return;
896 } else {
897 k->clear_java_mirror_handle();
898 k->clear_archived_mirror_index();
899 }
900 }
901 create_mirror(k, Handle(), Handle(), Handle(), Handle(), CHECK);
902 }
903
904 void java_lang_Class::initialize_mirror_fields(Klass* k,
905 Handle mirror,
906 Handle protection_domain,
907 Handle classData,
908 TRAPS) {
909 // Allocate a simple java object for a lock.
910 // This needs to be a java object because during class initialization
911 // it can be held across a java call.
912 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
913 set_init_lock(mirror(), r);
914
915 // Set protection domain also
916 set_protection_domain(mirror(), protection_domain());
917
918 // Initialize static fields
1144 "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1145 type2name(bt), p2i(m), p2i(archived_m));
1146
1147 Universe::replace_mirror(bt, archived_m);
1148 }
1149 }
1150 }
1151 //
1152 // After the mirror object is successfully archived, the archived
1153 // klass is set with _has_archived_raw_mirror flag.
1154 //
1155 // The _has_archived_raw_mirror flag is cleared at runtime when the
1156 // archived mirror is restored. If archived java heap data cannot
1157 // be used at runtime, new mirror object is created for the shared
1158 // class. The _has_archived_raw_mirror is cleared also during the process.
1159 oop java_lang_Class::archive_mirror(Klass* k, TRAPS) {
1160 assert(HeapShared::is_heap_object_archiving_allowed(),
1161 "HeapShared::is_heap_object_archiving_allowed() must be true");
1162
1163 // Mirror is already archived
1164 if (k->has_archived_mirror_index()) {
1165 assert(k->archived_java_mirror() != NULL, "no archived mirror");
1166 return k->archived_java_mirror();
1167 }
1168
1169 // No mirror
1170 oop mirror = k->java_mirror();
1171 if (mirror == NULL) {
1172 return NULL;
1173 }
1174
1175 if (k->is_instance_klass()) {
1176 InstanceKlass *ik = InstanceKlass::cast(k);
1177 assert(ik->signers() == NULL, "class with signer should have been excluded");
1178
1179 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
1180 ik->is_shared_app_class())) {
1181 // Archiving mirror for classes from non-builtin loaders is not
1182 // supported.
1183 return NULL;
1184 }
1185 }
1186
1187 // Now start archiving the mirror object
1188 oop archived_mirror = HeapShared::archive_heap_object(mirror, THREAD);
1189 if (archived_mirror == NULL) {
1190 return NULL;
1191 }
1192
1193 archived_mirror = process_archived_mirror(k, mirror, archived_mirror, THREAD);
1194 if (archived_mirror == NULL) {
1195 return NULL;
1196 }
1197
1198 k->set_archived_java_mirror(archived_mirror);
1199
1200 ResourceMark rm;
1201 log_trace(cds, heap, mirror)(
1202 "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1203 k->external_name(), p2i(mirror), p2i(archived_mirror));
1204
1205 return archived_mirror;
1206 }
1207
1208 // The process is based on create_mirror().
1209 oop java_lang_Class::process_archived_mirror(Klass* k, oop mirror,
1210 oop archived_mirror,
1211 Thread *THREAD) {
1212 // Clear nonstatic fields in archived mirror. Some of the fields will be set
1213 // to archived metadata and objects below.
1214 Klass *c = archived_mirror->klass();
1215 Handle archived_mirror_h(THREAD, archived_mirror);
1216 ResetMirrorField reset(archived_mirror_h);
1217 InstanceKlass::cast(c)->do_nonstatic_fields(&reset);
1218
1294 archived_mirror->metadata_field_put(_array_klass_offset,
1295 (Klass*)(address(ak) + MetaspaceShared::relocation_delta()));
1296 }
1297 }
1298
1299
1300 // Returns true if the mirror is updated, false if no archived mirror
1301 // data is present. After the archived mirror object is restored, the
1302 // shared klass' _has_raw_archived_mirror flag is cleared.
1303 bool java_lang_Class::restore_archived_mirror(Klass *k,
1304 Handle class_loader, Handle module,
1305 Handle protection_domain, TRAPS) {
1306 // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1307 // see more details in SystemDictionary::resolve_well_known_classes().
1308 if (!SystemDictionary::Class_klass_loaded()) {
1309 assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
1310 fixup_mirror_list()->push(k);
1311 return true;
1312 }
1313
1314 oop m = k->archived_java_mirror();
1315 assert(m != NULL, "must have stored non-null archived mirror");
1316
1317 // Sanity: clear it now to prevent re-initialization if any of the following fails
1318 k->clear_archived_mirror_index();
1319
1320 // mirror is archived, restore
1321 log_debug(cds, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m));
1322 assert(HeapShared::is_archived_object(m), "must be archived mirror object");
1323 assert(as_Klass(m) == k, "must be");
1324 Handle mirror(THREAD, m);
1325
1326 if (!k->is_array_klass()) {
1327 // - local static final fields with initial values were initialized at dump time
1328
1329 // create the init_lock
1330 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false));
1331 set_init_lock(mirror(), r);
1332
1333 if (protection_domain.not_null()) {
1334 set_protection_domain(mirror(), protection_domain());
1335 }
1336 }
1337
1338 assert(class_loader() == k->class_loader(), "should be same");
1339 if (class_loader.not_null()) {
1340 set_class_loader(mirror(), class_loader());
1341 }
1342
1343 k->set_java_mirror(mirror);
1344
1345 set_mirror_module_field(k, mirror, module, THREAD);
1346
1347 if (log_is_enabled(Trace, cds, heap, mirror)) {
1348 ResourceMark rm(THREAD);
1349 log_trace(cds, heap, mirror)(
1350 "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1351 }
1352
1353 return true;
1354 }
1355 #endif // INCLUDE_CDS_JAVA_HEAP
1356
1357 void java_lang_Class::fixup_module_field(Klass* k, Handle module) {
1358 assert(_module_offset != 0, "must have been computed already");
1359 java_lang_Class::set_module(k->java_mirror(), module());
1360 }
1361
1362 int java_lang_Class::oop_size(oop java_class) {
1363 assert(_oop_size_offset != 0, "must be set");
|