< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page

 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 = super();
 833   if (s != nullptr) {

 969   }
 970 
 971   // in case the class is linked in the process of linking its superclasses
 972   if (is_linked()) {
 973     return true;
 974   }
 975 
 976   // trace only the link time for this klass that includes
 977   // the verification time
 978   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 979                              ClassLoader::perf_class_link_selftime(),
 980                              ClassLoader::perf_classes_linked(),
 981                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 982                              jt->get_thread_stat()->perf_timers_addr(),
 983                              PerfClassTraceTime::CLASS_LINK);
 984 
 985   // verification & rewriting
 986   {
 987     HandleMark hm(THREAD);
 988     Handle h_init_lock(THREAD, init_lock());
 989     ObjectLocker ol(h_init_lock, jt);




 990     // rewritten will have been set if loader constraint error found
 991     // on an earlier link attempt
 992     // don't verify or rewrite if already rewritten
 993     //
 994 
 995     if (!is_linked()) {
 996       if (!is_rewritten()) {
 997         if (in_aot_cache()) {
 998           assert(!verified_at_dump_time(), "must be");
 999         }
1000         {
1001           bool verify_ok = verify_code(THREAD);
1002           if (!verify_ok) {
1003             return false;
1004           }
1005         }
1006 
1007         // Just in case a side-effect of verify linked this class already
1008         // (which can sometimes happen since the verifier loads classes
1009         // using custom class loaders, which are free to initialize things)

1182 
1183 void InstanceKlass::initialize_impl(TRAPS) {
1184   HandleMark hm(THREAD);
1185 
1186   // Make sure klass is linked (verified) before initialization
1187   // A class could already be verified, since it has been reflected upon.
1188   link_class(CHECK);
1189 
1190   DTRACE_CLASSINIT_PROBE(required, -1);
1191 
1192   bool wait = false;
1193 
1194   JavaThread* jt = THREAD;
1195 
1196   bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1197 
1198   // refer to the JVM book page 47 for description of steps
1199   // Step 1
1200   {
1201     Handle h_init_lock(THREAD, init_lock());
1202     ObjectLocker ol(h_init_lock, jt);
1203 
1204     // Step 2
1205     // If we were to use wait() instead of waitInterruptibly() then
1206     // we might end up throwing IE from link/symbol resolution sites
1207     // that aren't expected to throw.  This would wreak havoc.  See 6320309.
1208     while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1209       if (debug_logging_enabled) {
1210         ResourceMark rm(jt);
1211         log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1212                                jt->name(), external_name(), init_thread_name());
1213       }
1214       wait = true;
1215       jt->set_class_to_be_initialized(this);
1216       ol.wait_uninterruptibly(jt);
1217       jt->set_class_to_be_initialized(nullptr);
1218     }
1219 
1220     // Step 3
1221     if (is_being_initialized() && is_reentrant_initialization(jt)) {
1222       if (debug_logging_enabled) {
1223         ResourceMark rm(jt);
1224         log_debug(class, init)("Thread \"%s\" recursively initializing %s",
1225                                jt->name(), external_name());
1226       }
1227       DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
1228       return;
1229     }
1230 
1231     // Step 4
1232     if (is_initialized()) {
1233       if (debug_logging_enabled) {
1234         ResourceMark rm(jt);
1235         log_debug(class, init)("Thread \"%s\" found %s already initialized",
1236                                jt->name(), external_name());
1237       }

1255       ss.print("Could not initialize class %s", external_name());
1256       if (cause.is_null()) {
1257         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1258       } else {
1259         THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1260                         ss.as_string(), cause);
1261       }
1262     } else {
1263 
1264       // Step 6
1265       set_init_state(being_initialized);
1266       set_init_thread(jt);
1267       if (debug_logging_enabled) {
1268         ResourceMark rm(jt);
1269         log_debug(class, init)("Thread \"%s\" is initializing %s",
1270                                jt->name(), external_name());
1271       }
1272     }
1273   }
1274 




1275   // Step 7
1276   // Next, if C is a class rather than an interface, initialize it's super class and super
1277   // interfaces.
1278   if (!is_interface()) {
1279     Klass* super_klass = super();
1280     if (super_klass != nullptr && super_klass->should_be_initialized()) {
1281       super_klass->initialize(THREAD);
1282     }
1283     // If C implements any interface that declares a non-static, concrete method,
1284     // the initialization of C triggers initialization of its super interfaces.
1285     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1286     // having a superinterface that declares, non-static, concrete methods
1287     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1288       initialize_super_interfaces(THREAD);
1289     }
1290 
1291     // If any exceptions, complete abruptly, throwing the same exception as above.
1292     if (HAS_PENDING_EXCEPTION) {
1293       Handle e(THREAD, PENDING_EXCEPTION);
1294       CLEAR_PENDING_EXCEPTION;

1352     DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1353     if (e->is_a(vmClasses::Error_klass())) {
1354       THROW_OOP(e());
1355     } else {
1356       JavaCallArguments args(e);
1357       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1358                 vmSymbols::throwable_void_signature(),
1359                 &args);
1360     }
1361   }
1362   DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1363 }
1364 
1365 
1366 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1367   Handle h_init_lock(THREAD, init_lock());
1368   if (h_init_lock() != nullptr) {
1369     ObjectLocker ol(h_init_lock, THREAD);
1370     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1371     set_init_state(state);
1372     fence_and_clear_init_lock();
1373     ol.notify_all(CHECK);
1374   } else {
1375     assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1376     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1377     set_init_state(state);
1378   }
1379 }
1380 
1381 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1382 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1383 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1384   assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1385 
1386   DeoptimizationScope deopt_scope;
1387   {
1388     MutexLocker ml(current, Compile_lock);
1389 
1390     set_init_state(InstanceKlass::loaded);
1391     // make sure init_state store is already done.
1392     // 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 = super();
 830   if (s != nullptr) {

 966   }
 967 
 968   // in case the class is linked in the process of linking its superclasses
 969   if (is_linked()) {
 970     return true;
 971   }
 972 
 973   // trace only the link time for this klass that includes
 974   // the verification time
 975   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 976                              ClassLoader::perf_class_link_selftime(),
 977                              ClassLoader::perf_classes_linked(),
 978                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 979                              jt->get_thread_stat()->perf_timers_addr(),
 980                              PerfClassTraceTime::CLASS_LINK);
 981 
 982   // verification & rewriting
 983   {
 984     HandleMark hm(THREAD);
 985     Handle h_init_lock(THREAD, init_lock());
 986     ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE_false);
 987     // Don't preempt once we own the monitor as that would complicate
 988     // redoing the VM call (we don't expect to own this monitor).
 989     NoPreemptMark npm(THREAD);
 990 
 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 (in_aot_cache()) {
 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, CHECK_PREEMPTABLE);
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       ThreadWaitingForClassInit twcl(THREAD, this);
1217       ol.wait_uninterruptibly(CHECK_PREEMPTABLE);

1218     }
1219 
1220     // Step 3
1221     if (is_being_initialized() && is_reentrant_initialization(jt)) {
1222       if (debug_logging_enabled) {
1223         ResourceMark rm(jt);
1224         log_debug(class, init)("Thread \"%s\" recursively initializing %s",
1225                                jt->name(), external_name());
1226       }
1227       DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
1228       return;
1229     }
1230 
1231     // Step 4
1232     if (is_initialized()) {
1233       if (debug_logging_enabled) {
1234         ResourceMark rm(jt);
1235         log_debug(class, init)("Thread \"%s\" found %s already initialized",
1236                                jt->name(), external_name());
1237       }

1255       ss.print("Could not initialize class %s", external_name());
1256       if (cause.is_null()) {
1257         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1258       } else {
1259         THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1260                         ss.as_string(), cause);
1261       }
1262     } else {
1263 
1264       // Step 6
1265       set_init_state(being_initialized);
1266       set_init_thread(jt);
1267       if (debug_logging_enabled) {
1268         ResourceMark rm(jt);
1269         log_debug(class, init)("Thread \"%s\" is initializing %s",
1270                                jt->name(), external_name());
1271       }
1272     }
1273   }
1274 
1275   // Block preemption once we are the initializer thread.
1276   // Unmounting now would complicate the reentrant case.
1277   NoPreemptMark npm(THREAD);
1278 
1279   // Step 7
1280   // Next, if C is a class rather than an interface, initialize it's super class and super
1281   // interfaces.
1282   if (!is_interface()) {
1283     Klass* super_klass = super();
1284     if (super_klass != nullptr && super_klass->should_be_initialized()) {
1285       super_klass->initialize(THREAD);
1286     }
1287     // If C implements any interface that declares a non-static, concrete method,
1288     // the initialization of C triggers initialization of its super interfaces.
1289     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1290     // having a superinterface that declares, non-static, concrete methods
1291     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1292       initialize_super_interfaces(THREAD);
1293     }
1294 
1295     // If any exceptions, complete abruptly, throwing the same exception as above.
1296     if (HAS_PENDING_EXCEPTION) {
1297       Handle e(THREAD, PENDING_EXCEPTION);
1298       CLEAR_PENDING_EXCEPTION;

1356     DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1357     if (e->is_a(vmClasses::Error_klass())) {
1358       THROW_OOP(e());
1359     } else {
1360       JavaCallArguments args(e);
1361       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1362                 vmSymbols::throwable_void_signature(),
1363                 &args);
1364     }
1365   }
1366   DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1367 }
1368 
1369 
1370 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1371   Handle h_init_lock(THREAD, init_lock());
1372   if (h_init_lock() != nullptr) {
1373     ObjectLocker ol(h_init_lock, THREAD);
1374     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1375     set_init_state(state);

1376     ol.notify_all(CHECK);
1377   } else {
1378     assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1379     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1380     set_init_state(state);
1381   }
1382 }
1383 
1384 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1385 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1386 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1387   assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1388 
1389   DeoptimizationScope deopt_scope;
1390   {
1391     MutexLocker ml(current, Compile_lock);
1392 
1393     set_init_state(InstanceKlass::loaded);
1394     // make sure init_state store is already done.
1395     // The compiler reads the hierarchy outside of the Compile_lock.
< prev index next >