782 oop InstanceKlass::protection_domain() const {
783 // return the protection_domain from the mirror
784 return java_lang_Class::protection_domain(java_mirror());
785 }
786
787 objArrayOop InstanceKlass::signers() const {
788 // return the signers from the mirror
789 return java_lang_Class::signers(java_mirror());
790 }
791
792 oop InstanceKlass::init_lock() const {
793 // return the init lock from the mirror
794 oop lock = java_lang_Class::init_lock(java_mirror());
795 // Prevent reordering with any access of initialization state
796 OrderAccess::loadload();
797 assert(lock != nullptr || !is_not_initialized(), // initialized or in_error state
798 "only fully initialized state can have a null lock");
799 return lock;
800 }
801
802 // Set the initialization lock to null so the object can be GC'ed. Any racing
803 // threads to get this lock will see a null lock and will not lock.
804 // That's okay because they all check for initialized state after getting
805 // the lock and return.
806 void InstanceKlass::fence_and_clear_init_lock() {
807 // make sure previous stores are all done, notably the init_state.
808 OrderAccess::storestore();
809 java_lang_Class::clear_init_lock(java_mirror());
810 assert(!is_not_initialized(), "class must be initialized now");
811 }
812
813
814 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
815 // process. The step comments refers to the procedure described in that section.
816 // Note: implementation moved to static method to expose the this pointer.
817 void InstanceKlass::initialize(TRAPS) {
818 if (this->should_be_initialized()) {
819 initialize_impl(CHECK);
820 // Note: at this point the class may be initialized
821 // OR it may be in the state of being initialized
822 // in case of recursive initialization!
823 } else {
824 assert(is_initialized(), "sanity check");
825 }
826 }
827
828 #ifdef ASSERT
829 void InstanceKlass::assert_no_clinit_will_run_for_aot_initialized_class() const {
830 assert(has_aot_initialized_mirror(), "must be");
831
832 InstanceKlass* s = java_super();
833 if (s != nullptr) {
970 }
971
972 // in case the class is linked in the process of linking its superclasses
973 if (is_linked()) {
974 return true;
975 }
976
977 // trace only the link time for this klass that includes
978 // the verification time
979 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
980 ClassLoader::perf_class_link_selftime(),
981 ClassLoader::perf_classes_linked(),
982 jt->get_thread_stat()->perf_recursion_counts_addr(),
983 jt->get_thread_stat()->perf_timers_addr(),
984 PerfClassTraceTime::CLASS_LINK);
985
986 // verification & rewriting
987 {
988 HandleMark hm(THREAD);
989 Handle h_init_lock(THREAD, init_lock());
990 ObjectLocker ol(h_init_lock, jt);
991 // rewritten will have been set if loader constraint error found
992 // on an earlier link attempt
993 // don't verify or rewrite if already rewritten
994 //
995
996 if (!is_linked()) {
997 if (!is_rewritten()) {
998 if (is_shared()) {
999 assert(!verified_at_dump_time(), "must be");
1000 }
1001 {
1002 bool verify_ok = verify_code(THREAD);
1003 if (!verify_ok) {
1004 return false;
1005 }
1006 }
1007
1008 // Just in case a side-effect of verify linked this class already
1009 // (which can sometimes happen since the verifier loads classes
1010 // using custom class loaders, which are free to initialize things)
1183
1184 void InstanceKlass::initialize_impl(TRAPS) {
1185 HandleMark hm(THREAD);
1186
1187 // Make sure klass is linked (verified) before initialization
1188 // A class could already be verified, since it has been reflected upon.
1189 link_class(CHECK);
1190
1191 DTRACE_CLASSINIT_PROBE(required, -1);
1192
1193 bool wait = false;
1194
1195 JavaThread* jt = THREAD;
1196
1197 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1198
1199 // refer to the JVM book page 47 for description of steps
1200 // Step 1
1201 {
1202 Handle h_init_lock(THREAD, init_lock());
1203 ObjectLocker ol(h_init_lock, jt);
1204
1205 // Step 2
1206 // If we were to use wait() instead of waitInterruptibly() then
1207 // we might end up throwing IE from link/symbol resolution sites
1208 // that aren't expected to throw. This would wreak havoc. See 6320309.
1209 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1210 if (debug_logging_enabled) {
1211 ResourceMark rm(jt);
1212 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1213 jt->name(), external_name(), init_thread_name());
1214 }
1215 wait = true;
1216 jt->set_class_to_be_initialized(this);
1217 ol.wait_uninterruptibly(jt);
1218 jt->set_class_to_be_initialized(nullptr);
1219 }
1220
1221 // Step 3
1222 if (is_being_initialized() && is_reentrant_initialization(jt)) {
1223 if (debug_logging_enabled) {
1224 ResourceMark rm(jt);
1225 log_debug(class, init)("Thread \"%s\" recursively initializing %s",
1226 jt->name(), external_name());
1227 }
1228 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
1229 return;
1230 }
1231
1232 // Step 4
1233 if (is_initialized()) {
1234 if (debug_logging_enabled) {
1235 ResourceMark rm(jt);
1236 log_debug(class, init)("Thread \"%s\" found %s already initialized",
1237 jt->name(), external_name());
1256 ss.print("Could not initialize class %s", external_name());
1257 if (cause.is_null()) {
1258 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1259 } else {
1260 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1261 ss.as_string(), cause);
1262 }
1263 } else {
1264
1265 // Step 6
1266 set_init_state(being_initialized);
1267 set_init_thread(jt);
1268 if (debug_logging_enabled) {
1269 ResourceMark rm(jt);
1270 log_debug(class, init)("Thread \"%s\" is initializing %s",
1271 jt->name(), external_name());
1272 }
1273 }
1274 }
1275
1276 // Step 7
1277 // Next, if C is a class rather than an interface, initialize it's super class and super
1278 // interfaces.
1279 if (!is_interface()) {
1280 Klass* super_klass = super();
1281 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1282 super_klass->initialize(THREAD);
1283 }
1284 // If C implements any interface that declares a non-static, concrete method,
1285 // the initialization of C triggers initialization of its super interfaces.
1286 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1287 // having a superinterface that declares, non-static, concrete methods
1288 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1289 initialize_super_interfaces(THREAD);
1290 }
1291
1292 // If any exceptions, complete abruptly, throwing the same exception as above.
1293 if (HAS_PENDING_EXCEPTION) {
1294 Handle e(THREAD, PENDING_EXCEPTION);
1295 CLEAR_PENDING_EXCEPTION;
1353 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1354 if (e->is_a(vmClasses::Error_klass())) {
1355 THROW_OOP(e());
1356 } else {
1357 JavaCallArguments args(e);
1358 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1359 vmSymbols::throwable_void_signature(),
1360 &args);
1361 }
1362 }
1363 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1364 }
1365
1366
1367 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1368 Handle h_init_lock(THREAD, init_lock());
1369 if (h_init_lock() != nullptr) {
1370 ObjectLocker ol(h_init_lock, THREAD);
1371 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1372 set_init_state(state);
1373 fence_and_clear_init_lock();
1374 ol.notify_all(CHECK);
1375 } else {
1376 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1377 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1378 set_init_state(state);
1379 }
1380 }
1381
1382 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1383 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1384 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1385 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1386
1387 DeoptimizationScope deopt_scope;
1388 {
1389 MutexLocker ml(current, Compile_lock);
1390
1391 set_init_state(InstanceKlass::loaded);
1392 // make sure init_state store is already done.
1393 // The compiler reads the hierarchy outside of the Compile_lock.
|
782 oop InstanceKlass::protection_domain() const {
783 // return the protection_domain from the mirror
784 return java_lang_Class::protection_domain(java_mirror());
785 }
786
787 objArrayOop InstanceKlass::signers() const {
788 // return the signers from the mirror
789 return java_lang_Class::signers(java_mirror());
790 }
791
792 oop InstanceKlass::init_lock() const {
793 // return the init lock from the mirror
794 oop lock = java_lang_Class::init_lock(java_mirror());
795 // Prevent reordering with any access of initialization state
796 OrderAccess::loadload();
797 assert(lock != nullptr || !is_not_initialized(), // initialized or in_error state
798 "only fully initialized state can have a null lock");
799 return lock;
800 }
801
802 void InstanceKlass::initialize_preemptable(TRAPS) {
803 if (this->should_be_initialized()) {
804 PREEMPT_ON_INIT_SUPPORTED_ONLY(PreemptableInitCall pic(THREAD, this);)
805 initialize_impl(THREAD);
806 } else {
807 assert(is_initialized(), "sanity check");
808 }
809 }
810
811 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
812 // process. The step comments refers to the procedure described in that section.
813 // Note: implementation moved to static method to expose the this pointer.
814 void InstanceKlass::initialize(TRAPS) {
815 if (this->should_be_initialized()) {
816 initialize_impl(CHECK);
817 // Note: at this point the class may be initialized
818 // OR it may be in the state of being initialized
819 // in case of recursive initialization!
820 } else {
821 assert(is_initialized(), "sanity check");
822 }
823 }
824
825 #ifdef ASSERT
826 void InstanceKlass::assert_no_clinit_will_run_for_aot_initialized_class() const {
827 assert(has_aot_initialized_mirror(), "must be");
828
829 InstanceKlass* s = java_super();
830 if (s != nullptr) {
967 }
968
969 // in case the class is linked in the process of linking its superclasses
970 if (is_linked()) {
971 return true;
972 }
973
974 // trace only the link time for this klass that includes
975 // the verification time
976 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
977 ClassLoader::perf_class_link_selftime(),
978 ClassLoader::perf_classes_linked(),
979 jt->get_thread_stat()->perf_recursion_counts_addr(),
980 jt->get_thread_stat()->perf_timers_addr(),
981 PerfClassTraceTime::CLASS_LINK);
982
983 // verification & rewriting
984 {
985 HandleMark hm(THREAD);
986 Handle h_init_lock(THREAD, init_lock());
987 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE_false);
988 // Don't preempt once we own the monitor as that would complicate
989 // redoing the VM call (we don't expect to own this monitor).
990 NoPreemptMark npm(THREAD);
991
992 // rewritten will have been set if loader constraint error found
993 // on an earlier link attempt
994 // don't verify or rewrite if already rewritten
995 //
996
997 if (!is_linked()) {
998 if (!is_rewritten()) {
999 if (is_shared()) {
1000 assert(!verified_at_dump_time(), "must be");
1001 }
1002 {
1003 bool verify_ok = verify_code(THREAD);
1004 if (!verify_ok) {
1005 return false;
1006 }
1007 }
1008
1009 // Just in case a side-effect of verify linked this class already
1010 // (which can sometimes happen since the verifier loads classes
1011 // using custom class loaders, which are free to initialize things)
1184
1185 void InstanceKlass::initialize_impl(TRAPS) {
1186 HandleMark hm(THREAD);
1187
1188 // Make sure klass is linked (verified) before initialization
1189 // A class could already be verified, since it has been reflected upon.
1190 link_class(CHECK);
1191
1192 DTRACE_CLASSINIT_PROBE(required, -1);
1193
1194 bool wait = false;
1195
1196 JavaThread* jt = THREAD;
1197
1198 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1199
1200 // refer to the JVM book page 47 for description of steps
1201 // Step 1
1202 {
1203 Handle h_init_lock(THREAD, init_lock());
1204 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE);
1205
1206 // Step 2
1207 // If we were to use wait() instead of waitInterruptibly() then
1208 // we might end up throwing IE from link/symbol resolution sites
1209 // that aren't expected to throw. This would wreak havoc. See 6320309.
1210 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1211 if (debug_logging_enabled) {
1212 ResourceMark rm(jt);
1213 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1214 jt->name(), external_name(), init_thread_name());
1215 }
1216 wait = true;
1217 jt->set_class_to_be_initialized(this);
1218 ol.wait_uninterruptibly(CHECK_PREEMPTABLE);
1219 jt->set_class_to_be_initialized(nullptr);
1220 }
1221
1222 // Step 3
1223 if (is_being_initialized() && is_reentrant_initialization(jt)) {
1224 if (debug_logging_enabled) {
1225 ResourceMark rm(jt);
1226 log_debug(class, init)("Thread \"%s\" recursively initializing %s",
1227 jt->name(), external_name());
1228 }
1229 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
1230 return;
1231 }
1232
1233 // Step 4
1234 if (is_initialized()) {
1235 if (debug_logging_enabled) {
1236 ResourceMark rm(jt);
1237 log_debug(class, init)("Thread \"%s\" found %s already initialized",
1238 jt->name(), external_name());
1257 ss.print("Could not initialize class %s", external_name());
1258 if (cause.is_null()) {
1259 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1260 } else {
1261 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1262 ss.as_string(), cause);
1263 }
1264 } else {
1265
1266 // Step 6
1267 set_init_state(being_initialized);
1268 set_init_thread(jt);
1269 if (debug_logging_enabled) {
1270 ResourceMark rm(jt);
1271 log_debug(class, init)("Thread \"%s\" is initializing %s",
1272 jt->name(), external_name());
1273 }
1274 }
1275 }
1276
1277 // Block preemption once we are the initializer thread.
1278 // Unmounting now would complicate the reentrant case.
1279 NoPreemptMark npm(THREAD);
1280
1281 // Step 7
1282 // Next, if C is a class rather than an interface, initialize it's super class and super
1283 // interfaces.
1284 if (!is_interface()) {
1285 Klass* super_klass = super();
1286 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1287 super_klass->initialize(THREAD);
1288 }
1289 // If C implements any interface that declares a non-static, concrete method,
1290 // the initialization of C triggers initialization of its super interfaces.
1291 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1292 // having a superinterface that declares, non-static, concrete methods
1293 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1294 initialize_super_interfaces(THREAD);
1295 }
1296
1297 // If any exceptions, complete abruptly, throwing the same exception as above.
1298 if (HAS_PENDING_EXCEPTION) {
1299 Handle e(THREAD, PENDING_EXCEPTION);
1300 CLEAR_PENDING_EXCEPTION;
1358 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1359 if (e->is_a(vmClasses::Error_klass())) {
1360 THROW_OOP(e());
1361 } else {
1362 JavaCallArguments args(e);
1363 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1364 vmSymbols::throwable_void_signature(),
1365 &args);
1366 }
1367 }
1368 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1369 }
1370
1371
1372 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1373 Handle h_init_lock(THREAD, init_lock());
1374 if (h_init_lock() != nullptr) {
1375 ObjectLocker ol(h_init_lock, THREAD);
1376 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1377 set_init_state(state);
1378 ol.notify_all(CHECK);
1379 } else {
1380 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1381 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1382 set_init_state(state);
1383 }
1384 }
1385
1386 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1387 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1388 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1389 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1390
1391 DeoptimizationScope deopt_scope;
1392 {
1393 MutexLocker ml(current, Compile_lock);
1394
1395 set_init_state(InstanceKlass::loaded);
1396 // make sure init_state store is already done.
1397 // The compiler reads the hierarchy outside of the Compile_lock.
|