633 module_to_name, module_to_name, package_name, module_from_name);
634 } else {
635 oop jlm = module_from->module();
636 assert(jlm != NULL, "Null jlm in module_from ModuleEntry");
637 intptr_t identity_hash = jlm->identity_hash();
638 size_t len = 170 + strlen(current_class_name) + strlen(new_class_name) +
639 2*strlen(module_to_name) + strlen(package_name) + 2*sizeof(uintx);
640 msg = NEW_RESOURCE_ARRAY(char, len);
641 jio_snprintf(msg, len - 1,
642 "class %s (in unnamed module @" SIZE_FORMAT_HEX ") cannot access class %s (in module %s) because module %s does not export %s to unnamed module @" SIZE_FORMAT_HEX,
643 current_class_name, uintx(identity_hash), new_class_name, module_to_name,
644 module_to_name, package_name, uintx(identity_hash));
645 }
646 } else {
647 ShouldNotReachHere();
648 }
649 } // result != OTHER_PROBLEM...
650 return msg;
651 }
652
653 bool Reflection::verify_field_access(const Klass* current_class,
654 const Klass* resolved_class,
655 const Klass* field_class,
656 AccessFlags access,
657 bool classloader_only,
658 bool protected_restriction) {
659 // Verify that current_class can access a field of field_class, where that
660 // field's access bits are "access". We assume that we've already verified
661 // that current_class can access field_class.
662 //
663 // If the classloader_only flag is set, we automatically allow any accesses
664 // in which current_class doesn't have a classloader.
665 //
666 // "resolved_class" is the runtime type of "field_class". Sometimes we don't
667 // need this distinction (e.g. if all we have is the runtime type, or during
668 // class file parsing when we only care about the static type); in that case
669 // callers should ensure that resolved_class == field_class.
670 //
671 if ((current_class == NULL) ||
672 (current_class == field_class) ||
673 access.is_public()) {
674 return true;
675 }
676
677 const Klass* host_class = current_class;
678 if (host_class->is_instance_klass() &&
679 InstanceKlass::cast(host_class)->is_anonymous()) {
680 host_class = InstanceKlass::cast(host_class)->host_klass();
681 assert(host_class != NULL, "Anonymous class has null host class");
682 assert(!(host_class->is_instance_klass() &&
683 InstanceKlass::cast(host_class)->is_anonymous()),
684 "host_class should not be anonymous");
685 }
686 if (host_class == field_class) {
687 return true;
688 }
689
690 if (access.is_protected()) {
691 if (!protected_restriction) {
692 // See if current_class (or outermost host class) is a subclass of field_class
693 // An interface may not access protected members of j.l.Object
694 if (!host_class->is_interface() && host_class->is_subclass_of(field_class)) {
695 if (access.is_static() || // static fields are ok, see 6622385
696 current_class == resolved_class ||
697 field_class == resolved_class ||
698 host_class->is_subclass_of(resolved_class) ||
699 resolved_class->is_subclass_of(host_class)) {
700 return true;
701 }
702 }
703 }
704 }
705
706 if (!access.is_private() && is_same_class_package(current_class, field_class)) {
707 return true;
708 }
709
710 // Allow all accesses from jdk/internal/reflect/MagicAccessorImpl subclasses to
711 // succeed trivially.
712 if (current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
713 return true;
714 }
715
716 return can_relax_access_check_for(
717 current_class, field_class, classloader_only);
718 }
719
720 bool Reflection::is_same_class_package(const Klass* class1, const Klass* class2) {
721 return InstanceKlass::cast(class1)->is_same_class_package(class2);
722 }
723
724 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
725 // throw an incompatible class change exception
726 // If inner_is_member, require the inner to be a member of the outer.
727 // If !inner_is_member, require the inner to be anonymous (a non-member).
728 // Caller is responsible for figuring out in advance which case must be true.
729 void Reflection::check_for_inner_class(const InstanceKlass* outer, const InstanceKlass* inner,
730 bool inner_is_member, TRAPS) {
731 InnerClassesIterator iter(outer);
732 constantPoolHandle cp (THREAD, outer->constants());
733 for (; !iter.done(); iter.next()) {
734 int ioff = iter.inner_class_info_index();
735 int ooff = iter.outer_class_info_index();
736
737 if (inner_is_member && ioff != 0 && ooff != 0) {
|
633 module_to_name, module_to_name, package_name, module_from_name);
634 } else {
635 oop jlm = module_from->module();
636 assert(jlm != NULL, "Null jlm in module_from ModuleEntry");
637 intptr_t identity_hash = jlm->identity_hash();
638 size_t len = 170 + strlen(current_class_name) + strlen(new_class_name) +
639 2*strlen(module_to_name) + strlen(package_name) + 2*sizeof(uintx);
640 msg = NEW_RESOURCE_ARRAY(char, len);
641 jio_snprintf(msg, len - 1,
642 "class %s (in unnamed module @" SIZE_FORMAT_HEX ") cannot access class %s (in module %s) because module %s does not export %s to unnamed module @" SIZE_FORMAT_HEX,
643 current_class_name, uintx(identity_hash), new_class_name, module_to_name,
644 module_to_name, package_name, uintx(identity_hash));
645 }
646 } else {
647 ShouldNotReachHere();
648 }
649 } // result != OTHER_PROBLEM...
650 return msg;
651 }
652
653 bool Reflection::verify_member_access(const Klass* current_class,
654 const Klass* resolved_class,
655 const Klass* member_class,
656 AccessFlags access,
657 bool classloader_only,
658 bool protected_restriction,
659 TRAPS) {
660 // Verify that current_class can access a member of member_class, where that
661 // field's access bits are "access". We assume that we've already verified
662 // that current_class can access member_class.
663 //
664 // If the classloader_only flag is set, we automatically allow any accesses
665 // in which current_class doesn't have a classloader.
666 //
667 // "resolved_class" is the runtime type of "member_class". Sometimes we don't
668 // need this distinction (e.g. if all we have is the runtime type, or during
669 // class file parsing when we only care about the static type); in that case
670 // callers should ensure that resolved_class == member_class.
671 //
672 if ((current_class == NULL) ||
673 (current_class == member_class) ||
674 access.is_public()) {
675 return true;
676 }
677
678 const Klass* host_class = current_class;
679 if (host_class->is_instance_klass() &&
680 InstanceKlass::cast(host_class)->is_anonymous()) {
681 host_class = InstanceKlass::cast(host_class)->host_klass();
682 assert(host_class != NULL, "Anonymous class has null host class");
683 assert(!(host_class->is_instance_klass() &&
684 InstanceKlass::cast(host_class)->is_anonymous()),
685 "host_class should not be anonymous");
686 }
687 if (host_class == member_class) {
688 return true;
689 }
690
691 if (access.is_protected()) {
692 if (!protected_restriction) {
693 // See if current_class (or outermost host class) is a subclass of member_class
694 // An interface may not access protected members of j.l.Object
695 if (!host_class->is_interface() && host_class->is_subclass_of(member_class)) {
696 if (access.is_static() || // static fields are ok, see 6622385
697 current_class == resolved_class ||
698 member_class == resolved_class ||
699 host_class->is_subclass_of(resolved_class) ||
700 resolved_class->is_subclass_of(host_class)) {
701 return true;
702 }
703 }
704 }
705 }
706
707 // package access
708 if (!access.is_private() && is_same_class_package(current_class, member_class)) {
709 return true;
710 }
711
712 // private access between different classes needs a nestmate check, but
713 // not for anonymous classes - so check host_class
714 if (access.is_private() && host_class == current_class) {
715 if (current_class->is_instance_klass() && member_class->is_instance_klass() ) {
716 InstanceKlass* cur_ik = const_cast<InstanceKlass*>(InstanceKlass::cast(current_class));
717 InstanceKlass* field_ik = const_cast<InstanceKlass*>(InstanceKlass::cast(member_class));
718 // Nestmate access checks may require resolution and validation of the nest-host.
719 // It is up to the caller to check for pending exceptions and handle appropriately.
720 bool access = cur_ik->has_nestmate_access_to(field_ik, CHECK_false);
721 if (access) {
722 guarantee(resolved_class->is_subclass_of(member_class), "must be!");
723 return true;
724 }
725 }
726 }
727
728 // Allow all accesses from jdk/internal/reflect/MagicAccessorImpl subclasses to
729 // succeed trivially.
730 if (current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
731 return true;
732 }
733
734 // Check for special relaxations
735 return can_relax_access_check_for(current_class, member_class, classloader_only);
736 }
737
738 bool Reflection::is_same_class_package(const Klass* class1, const Klass* class2) {
739 return InstanceKlass::cast(class1)->is_same_class_package(class2);
740 }
741
742 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
743 // throw an incompatible class change exception
744 // If inner_is_member, require the inner to be a member of the outer.
745 // If !inner_is_member, require the inner to be anonymous (a non-member).
746 // Caller is responsible for figuring out in advance which case must be true.
747 void Reflection::check_for_inner_class(const InstanceKlass* outer, const InstanceKlass* inner,
748 bool inner_is_member, TRAPS) {
749 InnerClassesIterator iter(outer);
750 constantPoolHandle cp (THREAD, outer->constants());
751 for (; !iter.done(); iter.next()) {
752 int ioff = iter.inner_class_info_index();
753 int ooff = iter.outer_class_info_index();
754
755 if (inner_is_member && ioff != 0 && ooff != 0) {
|