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) {
321 if (is_allowed(indy_specs, ik)) {
322 return true;
323 }
324 }
325
326 #ifdef ASSERT
327 if (ik == _aot_init_class) {
328 return true;
329 }
330 #endif
331
332 return false;
333 }
334
335 // TODO: currently we have a hard-coded list. We should turn this into
336 // an annotation: @jdk.internal.vm.annotation.RuntimeSetupRequired
337 // See JDK-8342481.
338 bool AOTClassInitializer::is_runtime_setup_required(InstanceKlass* ik) {
339 return ik == vmClasses::Class_klass() ||
340 ik == vmClasses::internal_Unsafe_klass() ||
341 ik == vmClasses::ConcurrentHashMap_klass();
342 }
343
344 void AOTClassInitializer::call_runtime_setup(JavaThread* current, InstanceKlass* ik) {
345 assert(ik->has_aot_initialized_mirror(), "sanity");
346 if (ik->is_runtime_setup_required()) {
347 if (log_is_enabled(Info, cds, init)) {
348 ResourceMark rm;
349 log_info(cds, init)("Calling %s::runtimeSetup()", ik->external_name());
350 }
351 JavaValue result(T_VOID);
352 JavaCalls::call_static(&result, ik,
353 vmSymbols::runtimeSetup(),
354 vmSymbols::void_method_signature(), current);
355 if (current->has_pending_exception()) {
356 // We cannot continue, as we might have cached instances of ik in the heap, but propagating the
357 // exception would cause ik to be in an error state.
358 AOTLinkedClassBulkLoader::exit_on_exception(current);
359 }
360 }
361 }
362
363 #ifdef ASSERT
364 void AOTClassInitializer::init_test_class(TRAPS) {
365 // -XX:AOTInitTestClass is used in regression tests for adding additional AOT-initialized classes
366 // and heap objects into the AOT cache. The tests must be carefully written to avoid including
367 // any classes that cannot be AOT-initialized.
368 //
369 // -XX:AOTInitTestClass is NOT a general mechanism for including user-defined objects into
370 // the AOT cache. Therefore, this option is NOT available in product JVM.
371 if (AOTInitTestClass != nullptr && CDSConfig::is_initing_classes_at_dump_time()) {
372 log_info(cds)("Debug build only: force initialization of AOTInitTestClass %s", AOTInitTestClass);
373 TempNewSymbol class_name = SymbolTable::new_symbol(AOTInitTestClass);
374 Handle app_loader(THREAD, SystemDictionary::java_system_loader());
375 Klass* k = SystemDictionary::resolve_or_null(class_name, app_loader, CHECK);
376 if (k == nullptr) {
377 vm_exit_during_initialization("AOTInitTestClass not found", AOTInitTestClass);
378 }
379 if (!k->is_instance_klass()) {
380 vm_exit_during_initialization("Invalid name for AOTInitTestClass", AOTInitTestClass);
381 }
382
|
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) {
326 if (is_allowed(indy_specs, ik)) {
327 return true;
328 }
329 }
330
331 #ifdef ASSERT
332 if (ik == _aot_init_class) {
333 return true;
334 }
335 #endif
336
337 return false;
338 }
339
340 // TODO: currently we have a hard-coded list. We should turn this into
341 // an annotation: @jdk.internal.vm.annotation.RuntimeSetupRequired
342 // See JDK-8342481.
343 bool AOTClassInitializer::is_runtime_setup_required(InstanceKlass* ik) {
344 return ik == vmClasses::Class_klass() ||
345 ik == vmClasses::internal_Unsafe_klass() ||
346 ik == vmClasses::ConcurrentHashMap_klass() ||
347 ik == vmClasses::Reference_klass() ||
348 ik->name()->equals("java/net/URI") ||
349 ik->name()->equals("java/lang/module/ModuleDescriptor");
350 }
351
352 void AOTClassInitializer::call_runtime_setup(JavaThread* current, InstanceKlass* ik) {
353 assert(ik->has_aot_initialized_mirror(), "sanity");
354 if (ik->is_runtime_setup_required()) {
355 if (log_is_enabled(Info, cds, init)) {
356 ResourceMark rm;
357 log_info(cds, init)("Calling %s::runtimeSetup()", ik->external_name());
358 }
359 JavaValue result(T_VOID);
360 JavaCalls::call_static(&result, ik,
361 vmSymbols::runtimeSetup(),
362 vmSymbols::void_method_signature(), current);
363 if (current->has_pending_exception()) {
364 // We cannot continue, as we might have cached instances of ik in the heap, but propagating the
365 // exception would cause ik to be in an error state.
366 AOTLinkedClassBulkLoader::exit_on_exception(current);
367 }
368 }
369 }
370
371 // check_can_be_preinited() is quite costly, so we cache the results inside
372 // DumpTimeClassInfo::_can_be_preinited. See also AOTClassInitializer::reset_preinit_check().
373 bool AOTClassInitializer::check_can_be_preinited(InstanceKlass* ik) {
374 ResourceMark rm;
375
376 if (!SystemDictionaryShared::is_builtin(ik)) {
377 log_info(cds, init)("cannot initialize %s (not built-in loader)", ik->external_name());
378 return false;
379 }
380
381 InstanceKlass* super = ik->java_super();
382 if (super != nullptr && !can_be_preinited_locked(super)) {
383 log_info(cds, init)("cannot initialize %s (super %s not initable)", ik->external_name(), super->external_name());
384 return false;
385 }
386
387 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
388 for (int i = 0; i < interfaces->length(); i++) {
389 if (!can_be_preinited_locked(interfaces->at(i))) {
390 log_info(cds, init)("cannot initialize %s (interface %s not initable)",
391 ik->external_name(), interfaces->at(i)->external_name());
392 return false;
393 }
394 }
395
396 if (HeapShared::is_lambda_form_klass(ik)) {
397 // We allow only these to have <clinit> or non-default static fields
398 return true;
399 }
400
401 if (ik->class_initializer() != nullptr) {
402 log_info(cds, init)("cannot initialize %s (has <clinit>)", ik->external_name());
403 return false;
404 }
405 if (ik->is_initialized() && !has_default_static_fields(ik)) {
406 return false;
407 }
408
409 return true;
410 }
411
412 bool AOTClassInitializer::has_default_static_fields(InstanceKlass* ik) {
413 oop mirror = ik->java_mirror();
414
415 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
416 if (fs.access_flags().is_static()) {
417 fieldDescriptor& fd = fs.field_descriptor();
418 int offset = fd.offset();
419 bool is_default = true;
420 bool has_initval = fd.has_initial_value();
421 switch (fd.field_type()) {
422 case T_OBJECT:
423 case T_ARRAY:
424 is_default = mirror->obj_field(offset) == nullptr;
425 break;
426 case T_BOOLEAN:
427 is_default = mirror->bool_field(offset) == (has_initval ? fd.int_initial_value() : 0);
428 break;
429 case T_BYTE:
430 is_default = mirror->byte_field(offset) == (has_initval ? fd.int_initial_value() : 0);
431 break;
432 case T_SHORT:
433 is_default = mirror->short_field(offset) == (has_initval ? fd.int_initial_value() : 0);
434 break;
435 case T_CHAR:
436 is_default = mirror->char_field(offset) == (has_initval ? fd.int_initial_value() : 0);
437 break;
438 case T_INT:
439 is_default = mirror->int_field(offset) == (has_initval ? fd.int_initial_value() : 0);
440 break;
441 case T_LONG:
442 is_default = mirror->long_field(offset) == (has_initval ? fd.long_initial_value() : 0);
443 break;
444 case T_FLOAT:
445 is_default = mirror->float_field(offset) == (has_initval ? fd.float_initial_value() : 0);
446 break;
447 case T_DOUBLE:
448 is_default = mirror->double_field(offset) == (has_initval ? fd.double_initial_value() : 0);
449 break;
450 default:
451 ShouldNotReachHere();
452 }
453
454 if (!is_default) {
455 log_info(cds, init)("cannot initialize %s (static field %s has non-default value)",
456 ik->external_name(), fd.name()->as_C_string());
457 return false;
458 }
459 }
460 }
461
462 return true;
463 }
464
465 bool AOTClassInitializer::can_be_preinited(InstanceKlass* ik) {
466 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
467 return can_be_preinited_locked(ik);
468 }
469
470 bool AOTClassInitializer::can_be_preinited_locked(InstanceKlass* ik) {
471 if (!CDSConfig::is_initing_classes_at_dump_time()) {
472 return false;
473 }
474
475 assert_lock_strong(DumpTimeTable_lock);
476 DumpTimeClassInfo* info = SystemDictionaryShared::get_info_locked(ik);
477 if (!info->has_done_preinit_check()) {
478 info->set_can_be_preinited(AOTClassInitializer::check_can_be_preinited(ik));
479 }
480 return info->can_be_preinited();
481 }
482
483 // Initialize a class at dump time, if possible.
484 void AOTClassInitializer::maybe_preinit_class(InstanceKlass* ik, TRAPS) {
485 #if 0 // FIXME -- leyden+JEP483 merge
486 if (!ik->is_initialized() && AOTClassInitializer::can_be_preinited(ik)) {
487 if (log_is_enabled(Info, cds, init)) {
488 ResourceMark rm;
489 log_info(cds, init)("preinitializing %s", ik->external_name());
490 }
491 ik->initialize(CHECK);
492 }
493 #endif
494 }
495
496 #ifdef ASSERT
497 void AOTClassInitializer::init_test_class(TRAPS) {
498 // -XX:AOTInitTestClass is used in regression tests for adding additional AOT-initialized classes
499 // and heap objects into the AOT cache. The tests must be carefully written to avoid including
500 // any classes that cannot be AOT-initialized.
501 //
502 // -XX:AOTInitTestClass is NOT a general mechanism for including user-defined objects into
503 // the AOT cache. Therefore, this option is NOT available in product JVM.
504 if (AOTInitTestClass != nullptr && CDSConfig::is_initing_classes_at_dump_time()) {
505 log_info(cds)("Debug build only: force initialization of AOTInitTestClass %s", AOTInitTestClass);
506 TempNewSymbol class_name = SymbolTable::new_symbol(AOTInitTestClass);
507 Handle app_loader(THREAD, SystemDictionary::java_system_loader());
508 Klass* k = SystemDictionary::resolve_or_null(class_name, app_loader, CHECK);
509 if (k == nullptr) {
510 vm_exit_during_initialization("AOTInitTestClass not found", AOTInitTestClass);
511 }
512 if (!k->is_instance_klass()) {
513 vm_exit_during_initialization("Invalid name for AOTInitTestClass", AOTInitTestClass);
514 }
515
|