8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
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/archiveBuilder.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/heapShared.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionaryShared.hpp"
31 #include "classfile/vmSymbols.hpp"
32 #include "oops/instanceKlass.inline.hpp"
33 #include "oops/symbol.hpp"
34 #include "runtime/java.hpp"
35 #include "runtime/javaCalls.hpp"
36
37 DEBUG_ONLY(InstanceKlass* _aot_init_class = nullptr;)
38
39 // Detector for class names we wish to handle specially.
40 // It is either an exact string match or a string prefix match.
41 class AOTClassInitializer::AllowedSpec {
42 const char* _class_name;
43 bool _is_prefix;
44 int _len;
45 public:
46 AllowedSpec(const char* class_name, bool is_prefix = false)
47 : _class_name(class_name), _is_prefix(is_prefix)
48 {
49 _len = (class_name == nullptr) ? 0 : (int)strlen(class_name);
50 }
51 const char* class_name() { return _class_name; }
52
53 bool matches(Symbol* name, int len) {
54 assert(_class_name != nullptr, "caller resp.");
55 if (_is_prefix) {
327 }
328 }
329
330 #ifdef ASSERT
331 if (ik == _aot_init_class) {
332 return true;
333 }
334 #endif
335
336 return false;
337 }
338
339 // TODO: currently we have a hard-coded list. We should turn this into
340 // an annotation: @jdk.internal.vm.annotation.RuntimeSetupRequired
341 // See JDK-8342481.
342 bool AOTClassInitializer::is_runtime_setup_required(InstanceKlass* ik) {
343 return ik == vmClasses::Class_klass() ||
344 ik == vmClasses::internal_Unsafe_klass() ||
345 ik == vmClasses::ConcurrentHashMap_klass() ||
346 ik == vmClasses::MethodHandleImpl_klass() ||
347 ik == vmClasses::Reference_klass();
348 }
349
350 void AOTClassInitializer::call_runtime_setup(JavaThread* current, InstanceKlass* ik) {
351 assert(ik->has_aot_initialized_mirror(), "sanity");
352 if (ik->is_runtime_setup_required()) {
353 if (log_is_enabled(Info, aot, init)) {
354 ResourceMark rm;
355 log_info(aot, init)("Calling %s::runtimeSetup()", ik->external_name());
356 }
357 JavaValue result(T_VOID);
358 JavaCalls::call_static(&result, ik,
359 vmSymbols::runtimeSetup(),
360 vmSymbols::void_method_signature(), current);
361 if (current->has_pending_exception()) {
362 // We cannot continue, as we might have cached instances of ik in the heap, but propagating the
363 // exception would cause ik to be in an error state.
364 AOTLinkedClassBulkLoader::exit_on_exception(current);
365 }
366 }
367 }
368
369 #ifdef ASSERT
370 void AOTClassInitializer::init_test_class(TRAPS) {
371 // -XX:AOTInitTestClass is used in regression tests for adding additional AOT-initialized classes
372 // and heap objects into the AOT cache. The tests must be carefully written to avoid including
373 // any classes that cannot be AOT-initialized.
374 //
375 // -XX:AOTInitTestClass is NOT a general mechanism for including user-defined objects into
376 // the AOT cache. Therefore, this option is NOT available in product JVM.
377 if (AOTInitTestClass != nullptr && CDSConfig::is_initing_classes_at_dump_time()) {
378 log_info(aot)("Debug build only: force initialization of AOTInitTestClass %s", AOTInitTestClass);
379 TempNewSymbol class_name = SymbolTable::new_symbol(AOTInitTestClass);
380 Handle app_loader(THREAD, SystemDictionary::java_system_loader());
381 Klass* k = SystemDictionary::resolve_or_null(class_name, app_loader, CHECK);
382 if (k == nullptr) {
383 vm_exit_during_initialization("AOTInitTestClass not found", AOTInitTestClass);
384 }
385 if (!k->is_instance_klass()) {
386 vm_exit_during_initialization("Invalid name for AOTInitTestClass", AOTInitTestClass);
387 }
388
|
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
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/archiveBuilder.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "dumpTimeClassInfo.inline.hpp"
29 #include "cds/heapShared.hpp"
30 #include "classfile/symbolTable.hpp"
31 #include "classfile/systemDictionaryShared.hpp"
32 #include "classfile/vmSymbols.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "oops/fieldStreams.inline.hpp"
35 #include "oops/instanceKlass.inline.hpp"
36 #include "oops/symbol.hpp"
37 #include "runtime/fieldDescriptor.inline.hpp"
38 #include "runtime/java.hpp"
39 #include "runtime/javaCalls.hpp"
40 #include "runtime/mutexLocker.hpp"
41
42 DEBUG_ONLY(InstanceKlass* _aot_init_class = nullptr;)
43
44 // Detector for class names we wish to handle specially.
45 // It is either an exact string match or a string prefix match.
46 class AOTClassInitializer::AllowedSpec {
47 const char* _class_name;
48 bool _is_prefix;
49 int _len;
50 public:
51 AllowedSpec(const char* class_name, bool is_prefix = false)
52 : _class_name(class_name), _is_prefix(is_prefix)
53 {
54 _len = (class_name == nullptr) ? 0 : (int)strlen(class_name);
55 }
56 const char* class_name() { return _class_name; }
57
58 bool matches(Symbol* name, int len) {
59 assert(_class_name != nullptr, "caller resp.");
60 if (_is_prefix) {
332 }
333 }
334
335 #ifdef ASSERT
336 if (ik == _aot_init_class) {
337 return true;
338 }
339 #endif
340
341 return false;
342 }
343
344 // TODO: currently we have a hard-coded list. We should turn this into
345 // an annotation: @jdk.internal.vm.annotation.RuntimeSetupRequired
346 // See JDK-8342481.
347 bool AOTClassInitializer::is_runtime_setup_required(InstanceKlass* ik) {
348 return ik == vmClasses::Class_klass() ||
349 ik == vmClasses::internal_Unsafe_klass() ||
350 ik == vmClasses::ConcurrentHashMap_klass() ||
351 ik == vmClasses::MethodHandleImpl_klass() ||
352 ik == vmClasses::Reference_klass() ||
353 ik->name()->equals("java/net/URI") ||
354 ik->name()->equals("java/lang/module/ModuleDescriptor");
355 }
356
357 void AOTClassInitializer::call_runtime_setup(JavaThread* current, InstanceKlass* ik) {
358 assert(ik->has_aot_initialized_mirror(), "sanity");
359 if (ik->is_runtime_setup_required()) {
360 if (log_is_enabled(Info, aot, init)) {
361 ResourceMark rm;
362 log_info(aot, init)("Calling %s::runtimeSetup()", ik->external_name());
363 }
364 JavaValue result(T_VOID);
365 JavaCalls::call_static(&result, ik,
366 vmSymbols::runtimeSetup(),
367 vmSymbols::void_method_signature(), current);
368 if (current->has_pending_exception()) {
369 // We cannot continue, as we might have cached instances of ik in the heap, but propagating the
370 // exception would cause ik to be in an error state.
371 AOTLinkedClassBulkLoader::exit_on_exception(current);
372 }
373 }
374 }
375
376 // check_can_be_preinited() is quite costly, so we cache the results inside
377 // DumpTimeClassInfo::_can_be_preinited. See also AOTClassInitializer::reset_preinit_check().
378 bool AOTClassInitializer::check_can_be_preinited(InstanceKlass* ik) {
379 ResourceMark rm;
380
381 if (!SystemDictionaryShared::is_builtin(ik)) {
382 log_info(cds, init)("cannot initialize %s (not built-in loader)", ik->external_name());
383 return false;
384 }
385
386 InstanceKlass* super = ik->java_super();
387 if (super != nullptr && !can_be_preinited_locked(super)) {
388 log_info(cds, init)("cannot initialize %s (super %s not initable)", ik->external_name(), super->external_name());
389 return false;
390 }
391
392 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
393 for (int i = 0; i < interfaces->length(); i++) {
394 if (!can_be_preinited_locked(interfaces->at(i))) {
395 log_info(cds, init)("cannot initialize %s (interface %s not initable)",
396 ik->external_name(), interfaces->at(i)->external_name());
397 return false;
398 }
399 }
400
401 if (HeapShared::is_lambda_form_klass(ik)) {
402 // We allow only these to have <clinit> or non-default static fields
403 return true;
404 }
405
406 if (ik->class_initializer() != nullptr) {
407 log_info(cds, init)("cannot initialize %s (has <clinit>)", ik->external_name());
408 return false;
409 }
410 if (ik->is_initialized() && !has_default_static_fields(ik)) {
411 return false;
412 }
413
414 return true;
415 }
416
417 bool AOTClassInitializer::has_default_static_fields(InstanceKlass* ik) {
418 oop mirror = ik->java_mirror();
419
420 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
421 if (fs.access_flags().is_static()) {
422 fieldDescriptor& fd = fs.field_descriptor();
423 int offset = fd.offset();
424 bool is_default = true;
425 bool has_initval = fd.has_initial_value();
426 switch (fd.field_type()) {
427 case T_OBJECT:
428 case T_ARRAY:
429 is_default = mirror->obj_field(offset) == nullptr;
430 break;
431 case T_BOOLEAN:
432 is_default = mirror->bool_field(offset) == (has_initval ? fd.int_initial_value() : 0);
433 break;
434 case T_BYTE:
435 is_default = mirror->byte_field(offset) == (has_initval ? fd.int_initial_value() : 0);
436 break;
437 case T_SHORT:
438 is_default = mirror->short_field(offset) == (has_initval ? fd.int_initial_value() : 0);
439 break;
440 case T_CHAR:
441 is_default = mirror->char_field(offset) == (has_initval ? fd.int_initial_value() : 0);
442 break;
443 case T_INT:
444 is_default = mirror->int_field(offset) == (has_initval ? fd.int_initial_value() : 0);
445 break;
446 case T_LONG:
447 is_default = mirror->long_field(offset) == (has_initval ? fd.long_initial_value() : 0);
448 break;
449 case T_FLOAT:
450 is_default = mirror->float_field(offset) == (has_initval ? fd.float_initial_value() : 0);
451 break;
452 case T_DOUBLE:
453 is_default = mirror->double_field(offset) == (has_initval ? fd.double_initial_value() : 0);
454 break;
455 default:
456 ShouldNotReachHere();
457 }
458
459 if (!is_default) {
460 log_info(cds, init)("cannot initialize %s (static field %s has non-default value)",
461 ik->external_name(), fd.name()->as_C_string());
462 return false;
463 }
464 }
465 }
466
467 return true;
468 }
469
470 bool AOTClassInitializer::can_be_preinited(InstanceKlass* ik) {
471 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
472 return can_be_preinited_locked(ik);
473 }
474
475 bool AOTClassInitializer::can_be_preinited_locked(InstanceKlass* ik) {
476 if (!CDSConfig::is_initing_classes_at_dump_time()) {
477 return false;
478 }
479
480 assert_lock_strong(DumpTimeTable_lock);
481 DumpTimeClassInfo* info = SystemDictionaryShared::get_info_locked(ik);
482 if (!info->has_done_preinit_check()) {
483 info->set_can_be_preinited(AOTClassInitializer::check_can_be_preinited(ik));
484 }
485 return info->can_be_preinited();
486 }
487
488 // Initialize a class at dump time, if possible.
489 void AOTClassInitializer::maybe_preinit_class(InstanceKlass* ik, TRAPS) {
490 #if 0 // FIXME -- leyden+JEP483 merge
491 if (!ik->is_initialized() && AOTClassInitializer::can_be_preinited(ik)) {
492 if (log_is_enabled(Info, cds, init)) {
493 ResourceMark rm;
494 log_info(cds, init)("preinitializing %s", ik->external_name());
495 }
496 ik->initialize(CHECK);
497 }
498 #endif
499 }
500
501 #ifdef ASSERT
502 void AOTClassInitializer::init_test_class(TRAPS) {
503 // -XX:AOTInitTestClass is used in regression tests for adding additional AOT-initialized classes
504 // and heap objects into the AOT cache. The tests must be carefully written to avoid including
505 // any classes that cannot be AOT-initialized.
506 //
507 // -XX:AOTInitTestClass is NOT a general mechanism for including user-defined objects into
508 // the AOT cache. Therefore, this option is NOT available in product JVM.
509 if (AOTInitTestClass != nullptr && CDSConfig::is_initing_classes_at_dump_time()) {
510 log_info(aot)("Debug build only: force initialization of AOTInitTestClass %s", AOTInitTestClass);
511 TempNewSymbol class_name = SymbolTable::new_symbol(AOTInitTestClass);
512 Handle app_loader(THREAD, SystemDictionary::java_system_loader());
513 Klass* k = SystemDictionary::resolve_or_null(class_name, app_loader, CHECK);
514 if (k == nullptr) {
515 vm_exit_during_initialization("AOTInitTestClass not found", AOTInitTestClass);
516 }
517 if (!k->is_instance_klass()) {
518 vm_exit_during_initialization("Invalid name for AOTInitTestClass", AOTInitTestClass);
519 }
520
|