14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotClassInitializer.hpp"
26 #include "cds/aotLinkedClassBulkLoader.hpp"
27 #include "cds/archiveBuilder.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/heapShared.hpp"
30 #include "cds/regeneratedClasses.hpp"
31 #include "classfile/symbolTable.hpp"
32 #include "classfile/systemDictionaryShared.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "oops/instanceKlass.inline.hpp"
35 #include "oops/symbol.hpp"
36 #include "runtime/java.hpp"
37 #include "runtime/javaCalls.hpp"
38
39 DEBUG_ONLY(InstanceKlass* _aot_init_class = nullptr;)
40
41 bool AOTClassInitializer::can_archive_initialized_mirror(InstanceKlass* ik) {
42 assert(!ArchiveBuilder::is_active() || !ArchiveBuilder::current()->is_in_buffer_space(ik), "must be source klass");
43 if (!CDSConfig::is_initing_classes_at_dump_time()) {
44 return false;
45 }
46
47 if (RegeneratedClasses::is_regenerated_object(ik)) {
48 ik = RegeneratedClasses::get_original_object(ik);
49 }
50
51 check_aot_annotations(ik);
52
53 if (!ik->is_initialized() && !ik->is_being_initialized()) {
54 if (ik->has_aot_safe_initializer()) {
55 ResourceMark rm;
56 log_info(aot, init)("Class %s is annotated with @AOTSafeClassInitializer but has not been initialized",
57 ik->external_name());
235
236 void AOTClassInitializer::call_runtime_setup(JavaThread* current, InstanceKlass* ik) {
237 assert(ik->has_aot_initialized_mirror(), "sanity");
238 if (ik->is_runtime_setup_required()) {
239 if (log_is_enabled(Info, aot, init)) {
240 ResourceMark rm;
241 log_info(aot, init)("Calling %s::runtimeSetup()", ik->external_name());
242 }
243 JavaValue result(T_VOID);
244 JavaCalls::call_static(&result, ik,
245 vmSymbols::runtimeSetup(),
246 vmSymbols::void_method_signature(), current);
247 if (current->has_pending_exception()) {
248 // We cannot continue, as we might have cached instances of ik in the heap, but propagating the
249 // exception would cause ik to be in an error state.
250 AOTLinkedClassBulkLoader::exit_on_exception(current);
251 }
252 }
253 }
254
255 template <typename FUNCTION>
256 void require_annotation_for_super_types(InstanceKlass* ik, const char* annotation, FUNCTION func) {
257 if (log_is_enabled(Info, aot, init)) {
258 ResourceMark rm;
259 log_info(aot, init)("Found %s class %s", annotation, ik->external_name());
260 }
261
262 // Since ik has this annotation, we require that
263 // - all super classes must have this annotation
264 // - all super interfaces that are interface_needs_clinit_execution_as_super()
265 // must have this annotation
266 // This avoid the situation where in the production run, we run the <clinit>
267 // of a supertype but not the <clinit> of ik
268
269 InstanceKlass* super = ik->java_super();
270 if (super != nullptr && !func(super)) {
271 ResourceMark rm;
272 log_error(aot, init)("Missing %s in superclass %s for class %s",
273 annotation, super->external_name(), ik->external_name());
274 AOTMetaspace::unrecoverable_writing_error();
284 AOTMetaspace::unrecoverable_writing_error();
285 }
286 }
287 }
288
289 void AOTClassInitializer::check_aot_annotations(InstanceKlass* ik) {
290 if (ik->has_aot_safe_initializer()) {
291 require_annotation_for_super_types(ik, "@AOTSafeClassInitializer", [&] (const InstanceKlass* supertype) {
292 return supertype->has_aot_safe_initializer();
293 });
294 } else {
295 // @AOTRuntimeSetup only meaningful in @AOTSafeClassInitializer
296 if (ik->is_runtime_setup_required()) {
297 ResourceMark rm;
298 log_error(aot, init)("@AOTRuntimeSetup meaningless in non-@AOTSafeClassInitializer class %s",
299 ik->external_name());
300 }
301 }
302 }
303
304
305 #ifdef ASSERT
306 void AOTClassInitializer::init_test_class(TRAPS) {
307 // -XX:AOTInitTestClass is used in regression tests for adding additional AOT-initialized classes
308 // and heap objects into the AOT cache. The tests must be carefully written to avoid including
309 // any classes that cannot be AOT-initialized.
310 //
311 // -XX:AOTInitTestClass is NOT a general mechanism for including user-defined objects into
312 // the AOT cache. Therefore, this option is NOT available in product JVM.
313 if (AOTInitTestClass != nullptr && CDSConfig::is_initing_classes_at_dump_time()) {
314 log_info(aot)("Debug build only: force initialization of AOTInitTestClass %s", AOTInitTestClass);
315 TempNewSymbol class_name = SymbolTable::new_symbol(AOTInitTestClass);
316 Handle app_loader(THREAD, SystemDictionary::java_system_loader());
317 Klass* k = SystemDictionary::resolve_or_null(class_name, app_loader, CHECK);
318 if (k == nullptr) {
319 vm_exit_during_initialization("AOTInitTestClass not found", AOTInitTestClass);
320 }
321 if (!k->is_instance_klass()) {
322 vm_exit_during_initialization("Invalid name for AOTInitTestClass", AOTInitTestClass);
323 }
324
|
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotClassInitializer.hpp"
26 #include "cds/aotLinkedClassBulkLoader.hpp"
27 #include "cds/archiveBuilder.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/heapShared.hpp"
30 #include "cds/regeneratedClasses.hpp"
31 #include "classfile/symbolTable.hpp"
32 #include "classfile/systemDictionaryShared.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "dumpTimeClassInfo.inline.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "oops/fieldStreams.inline.hpp"
37 #include "oops/instanceKlass.inline.hpp"
38 #include "oops/symbol.hpp"
39 #include "runtime/fieldDescriptor.inline.hpp"
40 #include "runtime/java.hpp"
41 #include "runtime/javaCalls.hpp"
42 #include "runtime/mutexLocker.hpp"
43
44 DEBUG_ONLY(InstanceKlass* _aot_init_class = nullptr;)
45
46 bool AOTClassInitializer::can_archive_initialized_mirror(InstanceKlass* ik) {
47 assert(!ArchiveBuilder::is_active() || !ArchiveBuilder::current()->is_in_buffer_space(ik), "must be source klass");
48 if (!CDSConfig::is_initing_classes_at_dump_time()) {
49 return false;
50 }
51
52 if (RegeneratedClasses::is_regenerated_object(ik)) {
53 ik = RegeneratedClasses::get_original_object(ik);
54 }
55
56 check_aot_annotations(ik);
57
58 if (!ik->is_initialized() && !ik->is_being_initialized()) {
59 if (ik->has_aot_safe_initializer()) {
60 ResourceMark rm;
61 log_info(aot, init)("Class %s is annotated with @AOTSafeClassInitializer but has not been initialized",
62 ik->external_name());
240
241 void AOTClassInitializer::call_runtime_setup(JavaThread* current, InstanceKlass* ik) {
242 assert(ik->has_aot_initialized_mirror(), "sanity");
243 if (ik->is_runtime_setup_required()) {
244 if (log_is_enabled(Info, aot, init)) {
245 ResourceMark rm;
246 log_info(aot, init)("Calling %s::runtimeSetup()", ik->external_name());
247 }
248 JavaValue result(T_VOID);
249 JavaCalls::call_static(&result, ik,
250 vmSymbols::runtimeSetup(),
251 vmSymbols::void_method_signature(), current);
252 if (current->has_pending_exception()) {
253 // We cannot continue, as we might have cached instances of ik in the heap, but propagating the
254 // exception would cause ik to be in an error state.
255 AOTLinkedClassBulkLoader::exit_on_exception(current);
256 }
257 }
258 }
259
260 // check_can_be_preinited() is quite costly, so we cache the results inside
261 // DumpTimeClassInfo::_can_be_preinited. See also AOTClassInitializer::reset_preinit_check().
262 bool AOTClassInitializer::check_can_be_preinited(InstanceKlass* ik) {
263 ResourceMark rm;
264
265 if (!SystemDictionaryShared::is_builtin(ik)) {
266 log_info(cds, init)("cannot initialize %s (not built-in loader)", ik->external_name());
267 return false;
268 }
269
270 InstanceKlass* super = ik->java_super();
271 if (super != nullptr && !can_be_preinited_locked(super)) {
272 log_info(cds, init)("cannot initialize %s (super %s not initable)", ik->external_name(), super->external_name());
273 return false;
274 }
275
276 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
277 for (int i = 0; i < interfaces->length(); i++) {
278 if (!can_be_preinited_locked(interfaces->at(i))) {
279 log_info(cds, init)("cannot initialize %s (interface %s not initable)",
280 ik->external_name(), interfaces->at(i)->external_name());
281 return false;
282 }
283 }
284
285 if (HeapShared::is_lambda_form_klass(ik)) {
286 // We allow only these to have <clinit> or non-default static fields
287 return true;
288 }
289
290 if (ik->class_initializer() != nullptr) {
291 log_info(cds, init)("cannot initialize %s (has <clinit>)", ik->external_name());
292 return false;
293 }
294 if (ik->is_initialized() && !has_default_static_fields(ik)) {
295 return false;
296 }
297
298 return true;
299 }
300
301 bool AOTClassInitializer::has_default_static_fields(InstanceKlass* ik) {
302 oop mirror = ik->java_mirror();
303
304 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
305 if (fs.access_flags().is_static()) {
306 fieldDescriptor& fd = fs.field_descriptor();
307 int offset = fd.offset();
308 bool is_default = true;
309 bool has_initval = fd.has_initial_value();
310 switch (fd.field_type()) {
311 case T_OBJECT:
312 case T_ARRAY:
313 is_default = mirror->obj_field(offset) == nullptr;
314 break;
315 case T_BOOLEAN:
316 is_default = mirror->bool_field(offset) == (has_initval ? fd.int_initial_value() : 0);
317 break;
318 case T_BYTE:
319 is_default = mirror->byte_field(offset) == (has_initval ? fd.int_initial_value() : 0);
320 break;
321 case T_SHORT:
322 is_default = mirror->short_field(offset) == (has_initval ? fd.int_initial_value() : 0);
323 break;
324 case T_CHAR:
325 is_default = mirror->char_field(offset) == (has_initval ? fd.int_initial_value() : 0);
326 break;
327 case T_INT:
328 is_default = mirror->int_field(offset) == (has_initval ? fd.int_initial_value() : 0);
329 break;
330 case T_LONG:
331 is_default = mirror->long_field(offset) == (has_initval ? fd.long_initial_value() : 0);
332 break;
333 case T_FLOAT:
334 is_default = mirror->float_field(offset) == (has_initval ? fd.float_initial_value() : 0);
335 break;
336 case T_DOUBLE:
337 is_default = mirror->double_field(offset) == (has_initval ? fd.double_initial_value() : 0);
338 break;
339 default:
340 ShouldNotReachHere();
341 }
342
343 if (!is_default) {
344 log_info(cds, init)("cannot initialize %s (static field %s has non-default value)",
345 ik->external_name(), fd.name()->as_C_string());
346 return false;
347 }
348 }
349 }
350
351 return true;
352 }
353
354 bool AOTClassInitializer::can_be_preinited(InstanceKlass* ik) {
355 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
356 return can_be_preinited_locked(ik);
357 }
358
359 bool AOTClassInitializer::can_be_preinited_locked(InstanceKlass* ik) {
360 if (!CDSConfig::is_initing_classes_at_dump_time()) {
361 return false;
362 }
363
364 assert_lock_strong(DumpTimeTable_lock);
365 DumpTimeClassInfo* info = SystemDictionaryShared::get_info_locked(ik);
366 if (!info->has_done_preinit_check()) {
367 info->set_can_be_preinited(AOTClassInitializer::check_can_be_preinited(ik));
368 }
369 return info->can_be_preinited();
370 }
371
372 // Initialize a class at dump time, if possible.
373 void AOTClassInitializer::maybe_preinit_class(InstanceKlass* ik, TRAPS) {
374 #if 0 // FIXME -- leyden+JEP483 merge
375 if (!ik->is_initialized() && AOTClassInitializer::can_be_preinited(ik)) {
376 if (log_is_enabled(Info, cds, init)) {
377 ResourceMark rm;
378 log_info(cds, init)("preinitializing %s", ik->external_name());
379 }
380 ik->initialize(CHECK);
381 }
382 #endif
383 }
384
385 template <typename FUNCTION>
386 void require_annotation_for_super_types(InstanceKlass* ik, const char* annotation, FUNCTION func) {
387 if (log_is_enabled(Info, aot, init)) {
388 ResourceMark rm;
389 log_info(aot, init)("Found %s class %s", annotation, ik->external_name());
390 }
391
392 // Since ik has this annotation, we require that
393 // - all super classes must have this annotation
394 // - all super interfaces that are interface_needs_clinit_execution_as_super()
395 // must have this annotation
396 // This avoid the situation where in the production run, we run the <clinit>
397 // of a supertype but not the <clinit> of ik
398
399 InstanceKlass* super = ik->java_super();
400 if (super != nullptr && !func(super)) {
401 ResourceMark rm;
402 log_error(aot, init)("Missing %s in superclass %s for class %s",
403 annotation, super->external_name(), ik->external_name());
404 AOTMetaspace::unrecoverable_writing_error();
414 AOTMetaspace::unrecoverable_writing_error();
415 }
416 }
417 }
418
419 void AOTClassInitializer::check_aot_annotations(InstanceKlass* ik) {
420 if (ik->has_aot_safe_initializer()) {
421 require_annotation_for_super_types(ik, "@AOTSafeClassInitializer", [&] (const InstanceKlass* supertype) {
422 return supertype->has_aot_safe_initializer();
423 });
424 } else {
425 // @AOTRuntimeSetup only meaningful in @AOTSafeClassInitializer
426 if (ik->is_runtime_setup_required()) {
427 ResourceMark rm;
428 log_error(aot, init)("@AOTRuntimeSetup meaningless in non-@AOTSafeClassInitializer class %s",
429 ik->external_name());
430 }
431 }
432 }
433
434 #ifdef ASSERT
435 void AOTClassInitializer::init_test_class(TRAPS) {
436 // -XX:AOTInitTestClass is used in regression tests for adding additional AOT-initialized classes
437 // and heap objects into the AOT cache. The tests must be carefully written to avoid including
438 // any classes that cannot be AOT-initialized.
439 //
440 // -XX:AOTInitTestClass is NOT a general mechanism for including user-defined objects into
441 // the AOT cache. Therefore, this option is NOT available in product JVM.
442 if (AOTInitTestClass != nullptr && CDSConfig::is_initing_classes_at_dump_time()) {
443 log_info(aot)("Debug build only: force initialization of AOTInitTestClass %s", AOTInitTestClass);
444 TempNewSymbol class_name = SymbolTable::new_symbol(AOTInitTestClass);
445 Handle app_loader(THREAD, SystemDictionary::java_system_loader());
446 Klass* k = SystemDictionary::resolve_or_null(class_name, app_loader, CHECK);
447 if (k == nullptr) {
448 vm_exit_during_initialization("AOTInitTestClass not found", AOTInitTestClass);
449 }
450 if (!k->is_instance_klass()) {
451 vm_exit_during_initialization("Invalid name for AOTInitTestClass", AOTInitTestClass);
452 }
453
|