343 // and k's Package is initialized using
344 //
345 // manifest = _shared_jar_manifests[index];
346 // url = _shared_jar_urls[index];
347 // define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
348 //
349 // Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by
350 // the corresponding SystemDictionaryShared::get_shared_xxx() function.
351 Handle manifest = get_shared_jar_manifest(index, CHECK_(pd));
352 Handle url = get_shared_jar_url(index, CHECK_(pd));
353 define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
354 pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd));
355 }
356 }
357 return pd;
358 }
359
360 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) {
361 oop class_loader = loader_data->class_loader();
362 return (class_loader == NULL ||
363 (UseAppCDS && (SystemDictionary::is_system_class_loader(class_loader) ||
364 SystemDictionary::is_platform_class_loader(class_loader)))
365 );
366 }
367
368 // Currently AppCDS only archives classes from the run-time image, the
369 // -Xbootclasspath/a path, the class path, and the module path.
370 //
371 // Check if a shared class can be loaded by the specific classloader. Following
372 // are the "visible" archived classes for different classloaders.
373 //
374 // NULL classloader:
375 // - see SystemDictionary::is_shared_class_visible()
376 // Platform classloader:
377 // - Module class from runtime image. ModuleEntry must be defined in the
378 // classloader.
379 // App classloader:
380 // - Module Class from runtime image and module path. ModuleEntry must be defined in the
381 // classloader.
382 // - Class from -cp. The class must have no PackageEntry defined in any of the
383 // boot/platform/app classloader, or must be in the unnamed module defined in the
384 // AppClassLoader.
385 bool SystemDictionaryShared::is_shared_class_visible_for_classloader(
491 // always returns null, and
492 // [d] if AppCDS is not enabled, the class would be loaded by findClass() by decoding it from a
493 // JAR file and then parsed.
494 //
495 // Given these assumptions, we intercept the findLoadedClass() call to invoke
496 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from
497 // the archive. The reasons are:
498 //
499 // + Because AppCDS is a commercial feature, we want to hide the implementation. There
500 // is currently no easy way to hide Java code, so we did it with native code.
501 // + Start-up is improved because we avoid decoding the JAR file, and avoid delegating
502 // to the parent (since we know the parent will not find this class).
503 //
504 // NOTE: there's a lot of assumption about the Java code. If any of that change, this
505 // needs to be redesigned.
506 //
507 // An alternative is to modify the Java code of AppClassLoader.loadClass().
508 //
509 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class(
510 Symbol* name, Handle class_loader, TRAPS) {
511 if (DumpSharedSpaces) {
512 return NULL;
513 }
514
515 InstanceKlass* k = NULL;
516 if (shared_dictionary() != NULL &&
517 UseAppCDS && (SystemDictionary::is_system_class_loader(class_loader()) ||
518 SystemDictionary::is_platform_class_loader(class_loader()))) {
519
520 // Fix for 4474172; see evaluation for more details
521 class_loader = Handle(
522 THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
523 ClassLoaderData *loader_data = register_loader(class_loader);
524 Dictionary* dictionary = loader_data->dictionary();
525
526 unsigned int d_hash = dictionary->compute_hash(name);
527
528 bool DoObjectLock = true;
529 if (is_parallelCapable(class_loader)) {
530 DoObjectLock = false;
531 }
532
533 // Make sure we are synchronized on the class loader before we proceed
534 //
535 // Note: currently, find_or_load_shared_class is called only from
536 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader,
537 // which are parallel-capable loaders, so this lock is NOT taken.
538 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
539 check_loader_lock_contention(lockObject, THREAD);
540 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
541
542 {
543 MutexLocker mu(SystemDictionary_lock, THREAD);
544 Klass* check = find_class(d_hash, name, dictionary);
545 if (check != NULL) {
546 return InstanceKlass::cast(check);
547 }
548 }
549
550 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD);
551 if (k != NULL) {
552 define_instance_class(k, CHECK_NULL);
553 }
554 }
555
556 return k;
557 }
558
559 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
560 Symbol* class_name, Handle class_loader, TRAPS) {
561 assert(UseAppCDS && shared_dictionary() != NULL, "already checked");
562 Klass* k = shared_dictionary()->find_class_for_builtin_loader(class_name);
563
564 if (k != NULL) {
565 InstanceKlass* ik = InstanceKlass::cast(k);
566 if ((ik->is_shared_app_class() &&
567 SystemDictionary::is_system_class_loader(class_loader())) ||
568 (ik->is_shared_platform_class() &&
569 SystemDictionary::is_platform_class_loader(class_loader()))) {
570 Handle protection_domain =
571 SystemDictionaryShared::init_security_info(class_loader, ik, CHECK_NULL);
572 return load_shared_class(ik, class_loader, protection_domain, THREAD);
573 }
574 }
575
576 return NULL;
577 }
578
579 void SystemDictionaryShared::oops_do(OopClosure* f) {
580 f->do_oop((oop*)&_shared_protection_domains);
581 f->do_oop((oop*)&_shared_jar_urls);
592 void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) {
593 if (_shared_jar_urls == NULL) {
594 _shared_jar_urls = oopFactory::new_objArray(
595 SystemDictionary::URL_klass(), size, CHECK);
596 }
597 }
598
599 void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) {
600 if (_shared_jar_manifests == NULL) {
601 _shared_jar_manifests = oopFactory::new_objArray(
602 SystemDictionary::Jar_Manifest_klass(), size, CHECK);
603 }
604 }
605
606 void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) {
607 allocate_shared_protection_domain_array(size, CHECK);
608 allocate_shared_jar_url_array(size, CHECK);
609 allocate_shared_jar_manifest_array(size, CHECK);
610 }
611
612
613 InstanceKlass* SystemDictionaryShared::lookup_from_stream(const Symbol* class_name,
614 Handle class_loader,
615 Handle protection_domain,
616 const ClassFileStream* cfs,
617 TRAPS) {
618 if (!UseAppCDS || shared_dictionary() == NULL) {
619 return NULL;
620 }
621 if (class_name == NULL) { // don't do this for anonymous classes
622 return NULL;
623 }
624 if (class_loader.is_null() ||
625 SystemDictionary::is_system_class_loader(class_loader()) ||
626 SystemDictionary::is_platform_class_loader(class_loader())) {
627 // This function is called for loading only UNREGISTERED classes.
628 // Do nothing for the BUILTIN loaders.
629 return NULL;
630 }
631
632 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
633 Klass* k;
634
635 { // UNREGISTERED loader
636 if (!shared_dictionary()->class_exists_for_unregistered_loader(class_name)) {
637 // No classes of this name for unregistered loaders.
638 return NULL;
639 }
640
641 int clsfile_size = cfs->length();
642 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
643
644 k = shared_dictionary()->find_class_for_unregistered_loader(class_name,
645 clsfile_size, clsfile_crc32);
646 }
647
669 }
670
671 // No other thread has acquired this yet, so give it to *this thread*
672 ik->set_class_loader_data(loader_data);
673 }
674
675 // No longer holding SharedDictionary_lock
676 // No need to lock, as <ik> can be held only by a single thread.
677 loader_data->add_class(ik);
678
679 // Load and check super/interfaces, restore unsharable info
680 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, THREAD);
681 if (shared_klass == NULL || HAS_PENDING_EXCEPTION) {
682 // TODO: clean up <ik> so it can be used again
683 return NULL;
684 }
685
686 return shared_klass;
687 }
688
689 bool SystemDictionaryShared::add_non_builtin_klass(Symbol* name, ClassLoaderData* loader_data,
690 InstanceKlass* k,
691 TRAPS) {
692 assert(DumpSharedSpaces, "only when dumping");
693 assert(UseAppCDS && boot_loader_dictionary() != NULL, "must be");
694
695 if (boot_loader_dictionary()->add_non_builtin_klass(name, loader_data, k)) {
696 MutexLocker mu_r(Compile_lock, THREAD); // not really necessary, but add_to_hierarchy asserts this.
697 add_to_hierarchy(k, CHECK_0);
698 return true;
699 }
700 return false;
701 }
702
703 // This function is called to resolve the super/interfaces of shared classes for
704 // non-built-in loaders. E.g., ChildClass in the below example
705 // where "super:" (and optionally "interface:") have been specified.
706 //
707 // java/lang/Object id: 0
708 // Interface id: 2 super: 0 source: cust.jar
709 // ChildClass id: 4 super: 0 interfaces: 2 source: cust.jar
710 Klass* SystemDictionaryShared::dump_time_resolve_super_or_fail(
711 Symbol* child_name, Symbol* class_name, Handle class_loader,
712 Handle protection_domain, bool is_superclass, TRAPS) {
713
|
343 // and k's Package is initialized using
344 //
345 // manifest = _shared_jar_manifests[index];
346 // url = _shared_jar_urls[index];
347 // define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
348 //
349 // Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by
350 // the corresponding SystemDictionaryShared::get_shared_xxx() function.
351 Handle manifest = get_shared_jar_manifest(index, CHECK_(pd));
352 Handle url = get_shared_jar_url(index, CHECK_(pd));
353 define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
354 pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd));
355 }
356 }
357 return pd;
358 }
359
360 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) {
361 oop class_loader = loader_data->class_loader();
362 return (class_loader == NULL ||
363 SystemDictionary::is_system_class_loader(class_loader) ||
364 SystemDictionary::is_platform_class_loader(class_loader));
365 }
366
367 // Currently AppCDS only archives classes from the run-time image, the
368 // -Xbootclasspath/a path, the class path, and the module path.
369 //
370 // Check if a shared class can be loaded by the specific classloader. Following
371 // are the "visible" archived classes for different classloaders.
372 //
373 // NULL classloader:
374 // - see SystemDictionary::is_shared_class_visible()
375 // Platform classloader:
376 // - Module class from runtime image. ModuleEntry must be defined in the
377 // classloader.
378 // App classloader:
379 // - Module Class from runtime image and module path. ModuleEntry must be defined in the
380 // classloader.
381 // - Class from -cp. The class must have no PackageEntry defined in any of the
382 // boot/platform/app classloader, or must be in the unnamed module defined in the
383 // AppClassLoader.
384 bool SystemDictionaryShared::is_shared_class_visible_for_classloader(
490 // always returns null, and
491 // [d] if AppCDS is not enabled, the class would be loaded by findClass() by decoding it from a
492 // JAR file and then parsed.
493 //
494 // Given these assumptions, we intercept the findLoadedClass() call to invoke
495 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from
496 // the archive. The reasons are:
497 //
498 // + Because AppCDS is a commercial feature, we want to hide the implementation. There
499 // is currently no easy way to hide Java code, so we did it with native code.
500 // + Start-up is improved because we avoid decoding the JAR file, and avoid delegating
501 // to the parent (since we know the parent will not find this class).
502 //
503 // NOTE: there's a lot of assumption about the Java code. If any of that change, this
504 // needs to be redesigned.
505 //
506 // An alternative is to modify the Java code of AppClassLoader.loadClass().
507 //
508 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class(
509 Symbol* name, Handle class_loader, TRAPS) {
510 InstanceKlass* k = NULL;
511 if (UseSharedSpaces) {
512 FileMapHeaderExt* header = (FileMapHeaderExt*)FileMapInfo::current_info()->header();
513 if (!header->has_platform_or_app_classes()) {
514 return NULL;
515 }
516
517 if (shared_dictionary() != NULL &&
518 (SystemDictionary::is_system_class_loader(class_loader()) ||
519 SystemDictionary::is_platform_class_loader(class_loader()))) {
520 // Fix for 4474172; see evaluation for more details
521 class_loader = Handle(
522 THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
523 ClassLoaderData *loader_data = register_loader(class_loader);
524 Dictionary* dictionary = loader_data->dictionary();
525
526 unsigned int d_hash = dictionary->compute_hash(name);
527
528 bool DoObjectLock = true;
529 if (is_parallelCapable(class_loader)) {
530 DoObjectLock = false;
531 }
532
533 // Make sure we are synchronized on the class loader before we proceed
534 //
535 // Note: currently, find_or_load_shared_class is called only from
536 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader,
537 // which are parallel-capable loaders, so this lock is NOT taken.
538 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
539 check_loader_lock_contention(lockObject, THREAD);
540 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
541
542 {
543 MutexLocker mu(SystemDictionary_lock, THREAD);
544 Klass* check = find_class(d_hash, name, dictionary);
545 if (check != NULL) {
546 return InstanceKlass::cast(check);
547 }
548 }
549
550 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD);
551 if (k != NULL) {
552 define_instance_class(k, CHECK_NULL);
553 }
554 }
555 }
556 return k;
557 }
558
559 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
560 Symbol* class_name, Handle class_loader, TRAPS) {
561 assert(UseSharedSpaces, "must be");
562 assert(shared_dictionary() != NULL, "already checked");
563 Klass* k = shared_dictionary()->find_class_for_builtin_loader(class_name);
564
565 if (k != NULL) {
566 InstanceKlass* ik = InstanceKlass::cast(k);
567 if ((ik->is_shared_app_class() &&
568 SystemDictionary::is_system_class_loader(class_loader())) ||
569 (ik->is_shared_platform_class() &&
570 SystemDictionary::is_platform_class_loader(class_loader()))) {
571 Handle protection_domain =
572 SystemDictionaryShared::init_security_info(class_loader, ik, CHECK_NULL);
573 return load_shared_class(ik, class_loader, protection_domain, THREAD);
574 }
575 }
576
577 return NULL;
578 }
579
580 void SystemDictionaryShared::oops_do(OopClosure* f) {
581 f->do_oop((oop*)&_shared_protection_domains);
582 f->do_oop((oop*)&_shared_jar_urls);
593 void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) {
594 if (_shared_jar_urls == NULL) {
595 _shared_jar_urls = oopFactory::new_objArray(
596 SystemDictionary::URL_klass(), size, CHECK);
597 }
598 }
599
600 void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) {
601 if (_shared_jar_manifests == NULL) {
602 _shared_jar_manifests = oopFactory::new_objArray(
603 SystemDictionary::Jar_Manifest_klass(), size, CHECK);
604 }
605 }
606
607 void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) {
608 allocate_shared_protection_domain_array(size, CHECK);
609 allocate_shared_jar_url_array(size, CHECK);
610 allocate_shared_jar_manifest_array(size, CHECK);
611 }
612
613 // This function is called for loading only UNREGISTERED classes
614 InstanceKlass* SystemDictionaryShared::lookup_from_stream(const Symbol* class_name,
615 Handle class_loader,
616 Handle protection_domain,
617 const ClassFileStream* cfs,
618 TRAPS) {
619 if (shared_dictionary() == NULL) {
620 return NULL;
621 }
622 if (class_name == NULL) { // don't do this for anonymous classes
623 return NULL;
624 }
625 if (class_loader.is_null() ||
626 SystemDictionary::is_system_class_loader(class_loader()) ||
627 SystemDictionary::is_platform_class_loader(class_loader())) {
628 // Do nothing for the BUILTIN loaders.
629 return NULL;
630 }
631
632 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
633 Klass* k;
634
635 { // UNREGISTERED loader
636 if (!shared_dictionary()->class_exists_for_unregistered_loader(class_name)) {
637 // No classes of this name for unregistered loaders.
638 return NULL;
639 }
640
641 int clsfile_size = cfs->length();
642 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
643
644 k = shared_dictionary()->find_class_for_unregistered_loader(class_name,
645 clsfile_size, clsfile_crc32);
646 }
647
669 }
670
671 // No other thread has acquired this yet, so give it to *this thread*
672 ik->set_class_loader_data(loader_data);
673 }
674
675 // No longer holding SharedDictionary_lock
676 // No need to lock, as <ik> can be held only by a single thread.
677 loader_data->add_class(ik);
678
679 // Load and check super/interfaces, restore unsharable info
680 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, THREAD);
681 if (shared_klass == NULL || HAS_PENDING_EXCEPTION) {
682 // TODO: clean up <ik> so it can be used again
683 return NULL;
684 }
685
686 return shared_klass;
687 }
688
689 bool SystemDictionaryShared::add_non_builtin_klass(Symbol* name,
690 ClassLoaderData* loader_data,
691 InstanceKlass* k,
692 TRAPS) {
693 assert(DumpSharedSpaces, "only when dumping");
694 assert(boot_loader_dictionary() != NULL, "must be");
695
696 if (boot_loader_dictionary()->add_non_builtin_klass(name, loader_data, k)) {
697 MutexLocker mu_r(Compile_lock, THREAD); // not really necessary, but add_to_hierarchy asserts this.
698 add_to_hierarchy(k, CHECK_0);
699 return true;
700 }
701 return false;
702 }
703
704 // This function is called to resolve the super/interfaces of shared classes for
705 // non-built-in loaders. E.g., ChildClass in the below example
706 // where "super:" (and optionally "interface:") have been specified.
707 //
708 // java/lang/Object id: 0
709 // Interface id: 2 super: 0 source: cust.jar
710 // ChildClass id: 4 super: 0 interfaces: 2 source: cust.jar
711 Klass* SystemDictionaryShared::dump_time_resolve_super_or_fail(
712 Symbol* child_name, Symbol* class_name, Handle class_loader,
713 Handle protection_domain, bool is_superclass, TRAPS) {
714
|