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