776 oop InstanceKlass::protection_domain() const {
777 // return the protection_domain from the mirror
778 return java_lang_Class::protection_domain(java_mirror());
779 }
780
781 objArrayOop InstanceKlass::signers() const {
782 // return the signers from the mirror
783 return java_lang_Class::signers(java_mirror());
784 }
785
786 oop InstanceKlass::init_lock() const {
787 // return the init lock from the mirror
788 oop lock = java_lang_Class::init_lock(java_mirror());
789 // Prevent reordering with any access of initialization state
790 OrderAccess::loadload();
791 assert(lock != nullptr || !is_not_initialized(), // initialized or in_error state
792 "only fully initialized state can have a null lock");
793 return lock;
794 }
795
796 // Set the initialization lock to null so the object can be GC'ed. Any racing
797 // threads to get this lock will see a null lock and will not lock.
798 // That's okay because they all check for initialized state after getting
799 // the lock and return.
800 void InstanceKlass::fence_and_clear_init_lock() {
801 // make sure previous stores are all done, notably the init_state.
802 OrderAccess::storestore();
803 java_lang_Class::clear_init_lock(java_mirror());
804 assert(!is_not_initialized(), "class must be initialized now");
805 }
806
807
808 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
809 // process. The step comments refers to the procedure described in that section.
810 // Note: implementation moved to static method to expose the this pointer.
811 void InstanceKlass::initialize(TRAPS) {
812 if (this->should_be_initialized()) {
813 initialize_impl(CHECK);
814 // Note: at this point the class may be initialized
815 // OR it may be in the state of being initialized
816 // in case of recursive initialization!
817 } else {
818 assert(is_initialized(), "sanity check");
819 }
820 }
821
822 #ifdef ASSERT
823 void InstanceKlass::assert_no_clinit_will_run_for_aot_initialized_class() const {
824 assert(has_aot_initialized_mirror(), "must be");
825
826 InstanceKlass* s = super();
963 }
964
965 // in case the class is linked in the process of linking its superclasses
966 if (is_linked()) {
967 return true;
968 }
969
970 // trace only the link time for this klass that includes
971 // the verification time
972 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
973 ClassLoader::perf_class_link_selftime(),
974 ClassLoader::perf_classes_linked(),
975 jt->get_thread_stat()->perf_recursion_counts_addr(),
976 jt->get_thread_stat()->perf_timers_addr(),
977 PerfClassTraceTime::CLASS_LINK);
978
979 // verification & rewriting
980 {
981 HandleMark hm(THREAD);
982 Handle h_init_lock(THREAD, init_lock());
983 ObjectLocker ol(h_init_lock, jt);
984 // rewritten will have been set if loader constraint error found
985 // on an earlier link attempt
986 // don't verify or rewrite if already rewritten
987 //
988
989 if (!is_linked()) {
990 if (!is_rewritten()) {
991 if (in_aot_cache()) {
992 assert(!verified_at_dump_time(), "must be");
993 }
994 {
995 bool verify_ok = verify_code(THREAD);
996 if (!verify_ok) {
997 return false;
998 }
999 }
1000
1001 // Just in case a side-effect of verify linked this class already
1002 // (which can sometimes happen since the verifier loads classes
1003 // using custom class loaders, which are free to initialize things)
1176
1177 void InstanceKlass::initialize_impl(TRAPS) {
1178 HandleMark hm(THREAD);
1179
1180 // Make sure klass is linked (verified) before initialization
1181 // A class could already be verified, since it has been reflected upon.
1182 link_class(CHECK);
1183
1184 DTRACE_CLASSINIT_PROBE(required, -1);
1185
1186 bool wait = false;
1187
1188 JavaThread* jt = THREAD;
1189
1190 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1191
1192 // refer to the JVM book page 47 for description of steps
1193 // Step 1
1194 {
1195 Handle h_init_lock(THREAD, init_lock());
1196 ObjectLocker ol(h_init_lock, jt);
1197
1198 // Step 2
1199 // If we were to use wait() instead of waitInterruptibly() then
1200 // we might end up throwing IE from link/symbol resolution sites
1201 // that aren't expected to throw. This would wreak havoc. See 6320309.
1202 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1203 if (debug_logging_enabled) {
1204 ResourceMark rm(jt);
1205 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1206 jt->name(), external_name(), init_thread_name());
1207 }
1208 wait = true;
1209 jt->set_class_to_be_initialized(this);
1210 ol.wait_uninterruptibly(jt);
1211 jt->set_class_to_be_initialized(nullptr);
1212 }
1213
1214 // Step 3
1215 if (is_being_initialized() && is_reentrant_initialization(jt)) {
1216 if (debug_logging_enabled) {
1217 ResourceMark rm(jt);
1218 log_debug(class, init)("Thread \"%s\" recursively initializing %s",
1219 jt->name(), external_name());
1220 }
1221 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
1222 return;
1223 }
1224
1225 // Step 4
1226 if (is_initialized()) {
1227 if (debug_logging_enabled) {
1228 ResourceMark rm(jt);
1229 log_debug(class, init)("Thread \"%s\" found %s already initialized",
1230 jt->name(), external_name());
1231 }
1249 ss.print("Could not initialize class %s", external_name());
1250 if (cause.is_null()) {
1251 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1252 } else {
1253 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1254 ss.as_string(), cause);
1255 }
1256 } else {
1257
1258 // Step 6
1259 set_init_state(being_initialized);
1260 set_init_thread(jt);
1261 if (debug_logging_enabled) {
1262 ResourceMark rm(jt);
1263 log_debug(class, init)("Thread \"%s\" is initializing %s",
1264 jt->name(), external_name());
1265 }
1266 }
1267 }
1268
1269 // Step 7
1270 // Next, if C is a class rather than an interface, initialize it's super class and super
1271 // interfaces.
1272 if (!is_interface()) {
1273 Klass* super_klass = super();
1274 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1275 super_klass->initialize(THREAD);
1276 }
1277 // If C implements any interface that declares a non-static, concrete method,
1278 // the initialization of C triggers initialization of its super interfaces.
1279 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1280 // having a superinterface that declares, non-static, concrete methods
1281 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1282 initialize_super_interfaces(THREAD);
1283 }
1284
1285 // If any exceptions, complete abruptly, throwing the same exception as above.
1286 if (HAS_PENDING_EXCEPTION) {
1287 Handle e(THREAD, PENDING_EXCEPTION);
1288 CLEAR_PENDING_EXCEPTION;
|
776 oop InstanceKlass::protection_domain() const {
777 // return the protection_domain from the mirror
778 return java_lang_Class::protection_domain(java_mirror());
779 }
780
781 objArrayOop InstanceKlass::signers() const {
782 // return the signers from the mirror
783 return java_lang_Class::signers(java_mirror());
784 }
785
786 oop InstanceKlass::init_lock() const {
787 // return the init lock from the mirror
788 oop lock = java_lang_Class::init_lock(java_mirror());
789 // Prevent reordering with any access of initialization state
790 OrderAccess::loadload();
791 assert(lock != nullptr || !is_not_initialized(), // initialized or in_error state
792 "only fully initialized state can have a null lock");
793 return lock;
794 }
795
796 // Set the initialization lock to null so the object can be GC'ed. Any racing
797 // threads to get this lock will see a null lock and will not lock.
798 // That's okay because they all check for initialized state after getting
799 // the lock and return. For preempted vthreads we keep the oop protected
800 // in the ObjectMonitor (see ObjectMonitor::set_object_strong()).
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 void InstanceKlass::initialize_preemptable(TRAPS) {
809 if (this->should_be_initialized()) {
810 PREEMPT_ON_INIT_SUPPORTED_ONLY(PreemptableInitCall pic(THREAD, this);)
811 initialize_impl(THREAD);
812 } else {
813 assert(is_initialized(), "sanity check");
814 }
815 }
816
817 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
818 // process. The step comments refers to the procedure described in that section.
819 // Note: implementation moved to static method to expose the this pointer.
820 void InstanceKlass::initialize(TRAPS) {
821 if (this->should_be_initialized()) {
822 initialize_impl(CHECK);
823 // Note: at this point the class may be initialized
824 // OR it may be in the state of being initialized
825 // in case of recursive initialization!
826 } else {
827 assert(is_initialized(), "sanity check");
828 }
829 }
830
831 #ifdef ASSERT
832 void InstanceKlass::assert_no_clinit_will_run_for_aot_initialized_class() const {
833 assert(has_aot_initialized_mirror(), "must be");
834
835 InstanceKlass* s = super();
972 }
973
974 // in case the class is linked in the process of linking its superclasses
975 if (is_linked()) {
976 return true;
977 }
978
979 // trace only the link time for this klass that includes
980 // the verification time
981 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
982 ClassLoader::perf_class_link_selftime(),
983 ClassLoader::perf_classes_linked(),
984 jt->get_thread_stat()->perf_recursion_counts_addr(),
985 jt->get_thread_stat()->perf_timers_addr(),
986 PerfClassTraceTime::CLASS_LINK);
987
988 // verification & rewriting
989 {
990 HandleMark hm(THREAD);
991 Handle h_init_lock(THREAD, init_lock());
992 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE_false);
993 // Don't allow preemption if we link/initialize classes below,
994 // since that would release this monitor while we are in the
995 // middle of linking this class.
996 NoPreemptMark npm(THREAD);
997
998 // rewritten will have been set if loader constraint error found
999 // on an earlier link attempt
1000 // don't verify or rewrite if already rewritten
1001 //
1002
1003 if (!is_linked()) {
1004 if (!is_rewritten()) {
1005 if (in_aot_cache()) {
1006 assert(!verified_at_dump_time(), "must be");
1007 }
1008 {
1009 bool verify_ok = verify_code(THREAD);
1010 if (!verify_ok) {
1011 return false;
1012 }
1013 }
1014
1015 // Just in case a side-effect of verify linked this class already
1016 // (which can sometimes happen since the verifier loads classes
1017 // using custom class loaders, which are free to initialize things)
1190
1191 void InstanceKlass::initialize_impl(TRAPS) {
1192 HandleMark hm(THREAD);
1193
1194 // Make sure klass is linked (verified) before initialization
1195 // A class could already be verified, since it has been reflected upon.
1196 link_class(CHECK);
1197
1198 DTRACE_CLASSINIT_PROBE(required, -1);
1199
1200 bool wait = false;
1201
1202 JavaThread* jt = THREAD;
1203
1204 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1205
1206 // refer to the JVM book page 47 for description of steps
1207 // Step 1
1208 {
1209 Handle h_init_lock(THREAD, init_lock());
1210 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE);
1211
1212 // Step 2
1213 // If we were to use wait() instead of waitInterruptibly() then
1214 // we might end up throwing IE from link/symbol resolution sites
1215 // that aren't expected to throw. This would wreak havoc. See 6320309.
1216 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1217 if (debug_logging_enabled) {
1218 ResourceMark rm(jt);
1219 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1220 jt->name(), external_name(), init_thread_name());
1221 }
1222 wait = true;
1223 ThreadWaitingForClassInit twcl(THREAD, this);
1224 ol.wait_uninterruptibly(CHECK_PREEMPTABLE);
1225 }
1226
1227 // Step 3
1228 if (is_being_initialized() && is_reentrant_initialization(jt)) {
1229 if (debug_logging_enabled) {
1230 ResourceMark rm(jt);
1231 log_debug(class, init)("Thread \"%s\" recursively initializing %s",
1232 jt->name(), external_name());
1233 }
1234 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
1235 return;
1236 }
1237
1238 // Step 4
1239 if (is_initialized()) {
1240 if (debug_logging_enabled) {
1241 ResourceMark rm(jt);
1242 log_debug(class, init)("Thread \"%s\" found %s already initialized",
1243 jt->name(), external_name());
1244 }
1262 ss.print("Could not initialize class %s", external_name());
1263 if (cause.is_null()) {
1264 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1265 } else {
1266 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1267 ss.as_string(), cause);
1268 }
1269 } else {
1270
1271 // Step 6
1272 set_init_state(being_initialized);
1273 set_init_thread(jt);
1274 if (debug_logging_enabled) {
1275 ResourceMark rm(jt);
1276 log_debug(class, init)("Thread \"%s\" is initializing %s",
1277 jt->name(), external_name());
1278 }
1279 }
1280 }
1281
1282 // Block preemption once we are the initializer thread. Unmounting now
1283 // would complicate the reentrant case (identity is platform thread).
1284 NoPreemptMark npm(THREAD);
1285
1286 // Step 7
1287 // Next, if C is a class rather than an interface, initialize it's super class and super
1288 // interfaces.
1289 if (!is_interface()) {
1290 Klass* super_klass = super();
1291 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1292 super_klass->initialize(THREAD);
1293 }
1294 // If C implements any interface that declares a non-static, concrete method,
1295 // the initialization of C triggers initialization of its super interfaces.
1296 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1297 // having a superinterface that declares, non-static, concrete methods
1298 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1299 initialize_super_interfaces(THREAD);
1300 }
1301
1302 // If any exceptions, complete abruptly, throwing the same exception as above.
1303 if (HAS_PENDING_EXCEPTION) {
1304 Handle e(THREAD, PENDING_EXCEPTION);
1305 CLEAR_PENDING_EXCEPTION;
|