1127 allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1128
1129 // set the classLoader field in the java_lang_Class instance
1130 assert(class_loader() == k->class_loader(), "should be same");
1131 set_class_loader(mirror(), class_loader());
1132
1133 // Setup indirection from klass->mirror
1134 // after any exceptions can happen during allocations.
1135 k->set_java_mirror(mirror);
1136
1137 // Set the module field in the java_lang_Class instance. This must be done
1138 // after the mirror is set.
1139 set_mirror_module_field(THREAD, k, mirror, module);
1140
1141 if (comp_mirror() != nullptr) {
1142 // Set after k->java_mirror() is published, because compiled code running
1143 // concurrently doesn't expect a k to have a null java_mirror.
1144 release_set_array_klass(comp_mirror(), k);
1145 }
1146 if (CDSConfig::is_dumping_heap()) {
1147 create_scratch_mirror(k, CHECK);
1148 }
1149 } else {
1150 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1151 fixup_mirror_list()->push(k);
1152 }
1153 }
1154
1155 #if INCLUDE_CDS_JAVA_HEAP
1156 // The "scratch mirror" stores the states of the mirror object that can be
1157 // decided at dump time (such as the initial values of the static fields, the
1158 // component mirror, etc). At runtime, more information is added to it by
1159 // java_lang_Class::restore_archived_mirror().
1160 //
1161 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1162 // produces the same result as /*runtime*/create_mirror().
1163 //
1164 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1165 // latter may contain dumptime-specific information that cannot be archived
1166 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1167 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) {
1168 if (k->class_loader() != nullptr &&
1169 k->class_loader() != SystemDictionary::java_platform_loader() &&
1170 k->class_loader() != SystemDictionary::java_system_loader()) {
1171 // We only archive the mirrors of classes loaded by the built-in loaders
1172 return;
1173 }
1174
1175 Handle protection_domain, classData; // set to null. Will be reinitialized at runtime
1176 Handle mirror;
1177 Handle comp_mirror;
1178 allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1179
1180 if (comp_mirror() != nullptr) {
1181 release_set_array_klass(comp_mirror(), k);
1182 }
1183
1184 HeapShared::set_scratch_java_mirror(k, mirror());
1185 }
1186
1187 // Returns true if the mirror is updated, false if no archived mirror
1188 // data is present. After the archived mirror object is restored, the
1189 // shared klass' _has_raw_archived_mirror flag is cleared.
1190 bool java_lang_Class::restore_archived_mirror(Klass *k,
1191 Handle class_loader, Handle module,
1192 Handle protection_domain, TRAPS) {
1193 // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1194 // see more details in vmClasses::resolve_all().
1195 if (!vmClasses::Class_klass_loaded()) {
1289 }
1290 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1291 assert(_init_lock_offset != 0, "must be set");
1292 java_class->obj_field_put(_init_lock_offset, init_lock);
1293 }
1294
1295 objArrayOop java_lang_Class::signers(oop java_class) {
1296 assert(_signers_offset != 0, "must be set");
1297 return (objArrayOop)java_class->obj_field(_signers_offset);
1298 }
1299
1300 oop java_lang_Class::class_data(oop java_class) {
1301 assert(_classData_offset != 0, "must be set");
1302 return java_class->obj_field(_classData_offset);
1303 }
1304 void java_lang_Class::set_class_data(oop java_class, oop class_data) {
1305 assert(_classData_offset != 0, "must be set");
1306 java_class->obj_field_put(_classData_offset, class_data);
1307 }
1308
1309 void java_lang_Class::set_reflection_data(oop java_class, oop reflection_data) {
1310 assert(_reflectionData_offset != 0, "must be set");
1311 java_class->obj_field_put(_reflectionData_offset, reflection_data);
1312 }
1313
1314 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1315 assert(_class_loader_offset != 0, "offsets should have been initialized");
1316 java_class->obj_field_put(_class_loader_offset, loader);
1317 }
1318
1319 oop java_lang_Class::class_loader(oop java_class) {
1320 assert(_class_loader_offset != 0, "must be set");
1321 return java_class->obj_field(_class_loader_offset);
1322 }
1323
1324 oop java_lang_Class::module(oop java_class) {
1325 assert(_module_offset != 0, "must be set");
1326 return java_class->obj_field(_module_offset);
1327 }
1328
1491 (*reference_klass) = as_Klass(java_class);
1492 return T_OBJECT;
1493 }
1494 }
1495
1496
1497 oop java_lang_Class::primitive_mirror(BasicType t) {
1498 oop mirror = Universe::java_mirror(t);
1499 assert(mirror != nullptr && mirror->is_a(vmClasses::Class_klass()), "must be a Class");
1500 assert(is_primitive(mirror), "must be primitive");
1501 return mirror;
1502 }
1503
1504 #define CLASS_FIELDS_DO(macro) \
1505 macro(_classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false); \
1506 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \
1507 macro(_component_mirror_offset, k, "componentType", class_signature, false); \
1508 macro(_module_offset, k, "module", module_signature, false); \
1509 macro(_name_offset, k, "name", string_signature, false); \
1510 macro(_classData_offset, k, "classData", object_signature, false); \
1511 macro(_reflectionData_offset, k, "reflectionData", java_lang_ref_SoftReference_signature, false); \
1512 macro(_signers_offset, k, "signers", object_array_signature, false); \
1513 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), char_signature, false); \
1514 macro(_protection_domain_offset, k, "protectionDomain", java_security_ProtectionDomain_signature, false); \
1515 macro(_is_primitive_offset, k, "primitive", bool_signature, false);
1516
1517 void java_lang_Class::compute_offsets() {
1518 if (_offsets_computed) {
1519 return;
1520 }
1521
1522 _offsets_computed = true;
1523
1524 InstanceKlass* k = vmClasses::Class_klass();
1525 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1526 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1527 }
1528
1529 #if INCLUDE_CDS
1530 void java_lang_Class::serialize_offsets(SerializeClosure* f) {
1531 f->do_bool(&_offsets_computed);
2300 return msg_utf8;
2301 }
2302
2303 oop java_lang_Throwable::cause(oop throwable) {
2304 return throwable->obj_field(_cause_offset);
2305 }
2306
2307 void java_lang_Throwable::set_message(oop throwable, oop value) {
2308 throwable->obj_field_put(_detailMessage_offset, value);
2309 }
2310
2311
2312 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
2313 throwable->obj_field_put(_stackTrace_offset, st_element_array);
2314 }
2315
2316 void java_lang_Throwable::clear_stacktrace(oop throwable) {
2317 set_stacktrace(throwable, nullptr);
2318 }
2319
2320
2321 void java_lang_Throwable::print(oop throwable, outputStream* st) {
2322 ResourceMark rm;
2323 Klass* k = throwable->klass();
2324 assert(k != nullptr, "just checking");
2325 st->print("%s", k->external_name());
2326 oop msg = message(throwable);
2327 if (msg != nullptr) {
2328 st->print(": %s", java_lang_String::as_utf8_string(msg));
2329 }
2330 }
2331
2332 // After this many redefines, the stack trace is unreliable.
2333 const int MAX_VERSION = USHRT_MAX;
2334
2335 static inline bool version_matches(Method* method, int version) {
2336 assert(version < MAX_VERSION, "version is too big");
2337 return method != nullptr && (method->constants()->version() == version);
2338 }
2339
5443 #if INCLUDE_CDS
5444 #define DO_SERIALIZE_OFFSETS(k) k::serialize_offsets(soc);
5445
5446 void JavaClasses::serialize_offsets(SerializeClosure* soc) {
5447 BASIC_JAVA_CLASSES_DO(DO_SERIALIZE_OFFSETS);
5448 }
5449 #endif
5450
5451 #if INCLUDE_CDS_JAVA_HEAP
5452 bool JavaClasses::is_supported_for_archiving(oop obj) {
5453 Klass* klass = obj->klass();
5454
5455 if (!CDSConfig::is_dumping_invokedynamic()) {
5456 // These are supported by CDS only when CDSConfig::is_dumping_invokedynamic() is enabled.
5457 if (klass == vmClasses::ResolvedMethodName_klass() ||
5458 klass == vmClasses::MemberName_klass()) {
5459 return false;
5460 }
5461 }
5462
5463 if (klass->is_subclass_of(vmClasses::Reference_klass())) {
5464 // It's problematic to archive Reference objects. One of the reasons is that
5465 // Reference::discovered may pull in unwanted objects (see JDK-8284336)
5466 return false;
5467 }
5468
5469 return true;
5470 }
5471 #endif
5472
5473 #ifndef PRODUCT
5474
5475 // These functions exist to assert the validity of de-serialized offsets in boxing object as a sanity check.
5476
5477 bool JavaClasses::check_offset(const char *klass_name, int deserialized_offset, const char *field_name,
5478 const char* field_sig) {
5479 EXCEPTION_MARK;
5480 fieldDescriptor fd;
5481 TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name);
5482 Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
5483 InstanceKlass* ik = InstanceKlass::cast(k);
5484 TempNewSymbol f_name = SymbolTable::new_symbol(field_name);
5485 TempNewSymbol f_sig = SymbolTable::new_symbol(field_sig);
5486 if (!ik->find_local_field(f_name, f_sig, &fd)) {
5487 tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
5488 return false;
|
1127 allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1128
1129 // set the classLoader field in the java_lang_Class instance
1130 assert(class_loader() == k->class_loader(), "should be same");
1131 set_class_loader(mirror(), class_loader());
1132
1133 // Setup indirection from klass->mirror
1134 // after any exceptions can happen during allocations.
1135 k->set_java_mirror(mirror);
1136
1137 // Set the module field in the java_lang_Class instance. This must be done
1138 // after the mirror is set.
1139 set_mirror_module_field(THREAD, k, mirror, module);
1140
1141 if (comp_mirror() != nullptr) {
1142 // Set after k->java_mirror() is published, because compiled code running
1143 // concurrently doesn't expect a k to have a null java_mirror.
1144 release_set_array_klass(comp_mirror(), k);
1145 }
1146 if (CDSConfig::is_dumping_heap()) {
1147 if (CDSConfig::is_dumping_protection_domains()) {
1148 create_scratch_mirror(k, protection_domain, CHECK);
1149 } else {
1150 create_scratch_mirror(k, Handle() /* null protection_domain*/, CHECK);
1151 }
1152 }
1153 } else {
1154 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1155 fixup_mirror_list()->push(k);
1156 }
1157 }
1158
1159 #if INCLUDE_CDS_JAVA_HEAP
1160 // The "scratch mirror" stores the states of the mirror object that can be
1161 // decided at dump time (such as the initial values of the static fields, the
1162 // component mirror, etc). At runtime, more information is added to it by
1163 // java_lang_Class::restore_archived_mirror().
1164 //
1165 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1166 // produces the same result as /*runtime*/create_mirror().
1167 //
1168 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1169 // latter may contain dumptime-specific information that cannot be archived
1170 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1171 void java_lang_Class::create_scratch_mirror(Klass* k, Handle protection_domain, TRAPS) {
1172 if (k->class_loader() != nullptr &&
1173 k->class_loader() != SystemDictionary::java_platform_loader() &&
1174 k->class_loader() != SystemDictionary::java_system_loader()) {
1175 // We only archive the mirrors of classes loaded by the built-in loaders
1176 return;
1177 }
1178
1179 Handle classData; // set to null. Will be reinitialized at runtime
1180 Handle mirror;
1181 Handle comp_mirror;
1182 allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1183
1184 if (comp_mirror() != nullptr) {
1185 release_set_array_klass(comp_mirror(), k);
1186 }
1187
1188 HeapShared::set_scratch_java_mirror(k, mirror());
1189 }
1190
1191 // Returns true if the mirror is updated, false if no archived mirror
1192 // data is present. After the archived mirror object is restored, the
1193 // shared klass' _has_raw_archived_mirror flag is cleared.
1194 bool java_lang_Class::restore_archived_mirror(Klass *k,
1195 Handle class_loader, Handle module,
1196 Handle protection_domain, TRAPS) {
1197 // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1198 // see more details in vmClasses::resolve_all().
1199 if (!vmClasses::Class_klass_loaded()) {
1293 }
1294 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1295 assert(_init_lock_offset != 0, "must be set");
1296 java_class->obj_field_put(_init_lock_offset, init_lock);
1297 }
1298
1299 objArrayOop java_lang_Class::signers(oop java_class) {
1300 assert(_signers_offset != 0, "must be set");
1301 return (objArrayOop)java_class->obj_field(_signers_offset);
1302 }
1303
1304 oop java_lang_Class::class_data(oop java_class) {
1305 assert(_classData_offset != 0, "must be set");
1306 return java_class->obj_field(_classData_offset);
1307 }
1308 void java_lang_Class::set_class_data(oop java_class, oop class_data) {
1309 assert(_classData_offset != 0, "must be set");
1310 java_class->obj_field_put(_classData_offset, class_data);
1311 }
1312
1313 oop java_lang_Class::reflection_data(oop java_class) {
1314 assert(_reflectionData_offset != 0, "must be set");
1315 return java_class->obj_field(_reflectionData_offset);
1316 }
1317
1318 bool java_lang_Class::has_reflection_data(oop java_class) {
1319 return (java_lang_Class::reflection_data(java_class) != nullptr);
1320 }
1321
1322 void java_lang_Class::set_reflection_data(oop java_class, oop reflection_data) {
1323 assert(_reflectionData_offset != 0, "must be set");
1324 java_class->obj_field_put(_reflectionData_offset, reflection_data);
1325 }
1326
1327 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1328 assert(_class_loader_offset != 0, "offsets should have been initialized");
1329 java_class->obj_field_put(_class_loader_offset, loader);
1330 }
1331
1332 oop java_lang_Class::class_loader(oop java_class) {
1333 assert(_class_loader_offset != 0, "must be set");
1334 return java_class->obj_field(_class_loader_offset);
1335 }
1336
1337 oop java_lang_Class::module(oop java_class) {
1338 assert(_module_offset != 0, "must be set");
1339 return java_class->obj_field(_module_offset);
1340 }
1341
1504 (*reference_klass) = as_Klass(java_class);
1505 return T_OBJECT;
1506 }
1507 }
1508
1509
1510 oop java_lang_Class::primitive_mirror(BasicType t) {
1511 oop mirror = Universe::java_mirror(t);
1512 assert(mirror != nullptr && mirror->is_a(vmClasses::Class_klass()), "must be a Class");
1513 assert(is_primitive(mirror), "must be primitive");
1514 return mirror;
1515 }
1516
1517 #define CLASS_FIELDS_DO(macro) \
1518 macro(_classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false); \
1519 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \
1520 macro(_component_mirror_offset, k, "componentType", class_signature, false); \
1521 macro(_module_offset, k, "module", module_signature, false); \
1522 macro(_name_offset, k, "name", string_signature, false); \
1523 macro(_classData_offset, k, "classData", object_signature, false); \
1524 macro(_reflectionData_offset, k, "reflectionData", class_ReflectionData_signature, false); \
1525 macro(_signers_offset, k, "signers", object_array_signature, false); \
1526 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), char_signature, false); \
1527 macro(_protection_domain_offset, k, "protectionDomain", java_security_ProtectionDomain_signature, false); \
1528 macro(_is_primitive_offset, k, "primitive", bool_signature, false);
1529
1530 void java_lang_Class::compute_offsets() {
1531 if (_offsets_computed) {
1532 return;
1533 }
1534
1535 _offsets_computed = true;
1536
1537 InstanceKlass* k = vmClasses::Class_klass();
1538 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1539 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1540 }
1541
1542 #if INCLUDE_CDS
1543 void java_lang_Class::serialize_offsets(SerializeClosure* f) {
1544 f->do_bool(&_offsets_computed);
2313 return msg_utf8;
2314 }
2315
2316 oop java_lang_Throwable::cause(oop throwable) {
2317 return throwable->obj_field(_cause_offset);
2318 }
2319
2320 void java_lang_Throwable::set_message(oop throwable, oop value) {
2321 throwable->obj_field_put(_detailMessage_offset, value);
2322 }
2323
2324
2325 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
2326 throwable->obj_field_put(_stackTrace_offset, st_element_array);
2327 }
2328
2329 void java_lang_Throwable::clear_stacktrace(oop throwable) {
2330 set_stacktrace(throwable, nullptr);
2331 }
2332
2333 oop java_lang_Throwable::create_exception_instance(Symbol* class_name, TRAPS) {
2334 Klass* k = SystemDictionary::resolve_or_fail(class_name, true, CHECK_NULL);
2335 return InstanceKlass::cast(k)->allocate_instance(CHECK_NULL);
2336 }
2337
2338 void java_lang_Throwable::print(oop throwable, outputStream* st) {
2339 ResourceMark rm;
2340 Klass* k = throwable->klass();
2341 assert(k != nullptr, "just checking");
2342 st->print("%s", k->external_name());
2343 oop msg = message(throwable);
2344 if (msg != nullptr) {
2345 st->print(": %s", java_lang_String::as_utf8_string(msg));
2346 }
2347 }
2348
2349 // After this many redefines, the stack trace is unreliable.
2350 const int MAX_VERSION = USHRT_MAX;
2351
2352 static inline bool version_matches(Method* method, int version) {
2353 assert(version < MAX_VERSION, "version is too big");
2354 return method != nullptr && (method->constants()->version() == version);
2355 }
2356
5460 #if INCLUDE_CDS
5461 #define DO_SERIALIZE_OFFSETS(k) k::serialize_offsets(soc);
5462
5463 void JavaClasses::serialize_offsets(SerializeClosure* soc) {
5464 BASIC_JAVA_CLASSES_DO(DO_SERIALIZE_OFFSETS);
5465 }
5466 #endif
5467
5468 #if INCLUDE_CDS_JAVA_HEAP
5469 bool JavaClasses::is_supported_for_archiving(oop obj) {
5470 Klass* klass = obj->klass();
5471
5472 if (!CDSConfig::is_dumping_invokedynamic()) {
5473 // These are supported by CDS only when CDSConfig::is_dumping_invokedynamic() is enabled.
5474 if (klass == vmClasses::ResolvedMethodName_klass() ||
5475 klass == vmClasses::MemberName_klass()) {
5476 return false;
5477 }
5478 }
5479
5480 return true;
5481 }
5482 #endif
5483
5484 #ifndef PRODUCT
5485
5486 // These functions exist to assert the validity of de-serialized offsets in boxing object as a sanity check.
5487
5488 bool JavaClasses::check_offset(const char *klass_name, int deserialized_offset, const char *field_name,
5489 const char* field_sig) {
5490 EXCEPTION_MARK;
5491 fieldDescriptor fd;
5492 TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name);
5493 Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
5494 InstanceKlass* ik = InstanceKlass::cast(k);
5495 TempNewSymbol f_name = SymbolTable::new_symbol(field_name);
5496 TempNewSymbol f_sig = SymbolTable::new_symbol(field_sig);
5497 if (!ik->find_local_field(f_name, f_sig, &fd)) {
5498 tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
5499 return false;
|