1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
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 *
530 if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
531 if (appendix.not_null()) expected_size_of_params += 1;
532 if (actual_size_of_params != expected_size_of_params) {
533 tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
534 tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
535 result->print();
536 }
537 assert(actual_size_of_params == expected_size_of_params,
538 "%d != %d", actual_size_of_params, expected_size_of_params);
539 #endif //ASSERT
540
541 assert(appendix_result_or_null != NULL, "");
542 (*appendix_result_or_null) = appendix;
543 }
544 return result;
545 }
546 }
547 return NULL;
548 }
549
550 void LinkResolver::check_method_accessability(Klass* ref_klass,
551 Klass* resolved_klass,
552 Klass* sel_klass,
553 const methodHandle& sel_method,
554 TRAPS) {
555
556 AccessFlags flags = sel_method->access_flags();
557
558 // Special case: arrays always override "clone". JVMS 2.15.
559 // If the resolved klass is an array class, and the declaring class
560 // is java.lang.Object and the method is "clone", set the flags
561 // to public.
562 //
563 // We'll check for the method name first, as that's most likely
564 // to be false (so we'll short-circuit out of these tests).
565 if (sel_method->name() == vmSymbols::clone_name() &&
566 sel_klass == SystemDictionary::Object_klass() &&
567 resolved_klass->is_array_klass()) {
568 // We need to change "protected" to "public".
569 assert(flags.is_protected(), "clone not protected?");
570 jint new_flags = flags.as_int();
571 new_flags = new_flags & (~JVM_ACC_PROTECTED);
572 new_flags = new_flags | JVM_ACC_PUBLIC;
573 flags.set_flags(new_flags);
574 }
575 // assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
576
577 bool can_access = Reflection::verify_member_access(ref_klass,
578 resolved_klass,
579 sel_klass,
580 flags,
581 true, false, CHECK);
582 // Any existing exceptions that may have been thrown, for example LinkageErrors
583 // from nest-host resolution, have been allowed to propagate.
584 if (!can_access) {
585 ResourceMark rm(THREAD);
586 bool same_module = (sel_klass->module() == ref_klass->module());
587 Exceptions::fthrow(
588 THREAD_AND_LOCATION,
589 vmSymbols::java_lang_IllegalAccessError(),
590 "class %s tried to access %s%s%smethod '%s' (%s%s%s)",
591 ref_klass->external_name(),
592 sel_method->is_abstract() ? "abstract " : "",
593 sel_method->is_protected() ? "protected " : "",
594 sel_method->is_private() ? "private " : "",
595 sel_method->external_name(),
596 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
597 (same_module) ? "" : "; ",
598 (same_module) ? "" : sel_klass->class_in_module_of_loader()
599 );
600 return;
601 }
602 }
603
604 Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
605 const constantPoolHandle& pool, int index, TRAPS) {
606 // This method is used only
607 // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
608 // and
609 // (2) in Bytecode_invoke::static_target
610 // It appears to fail when applied to an invokeinterface call site.
611 // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
612 // resolve klass
613 if (code == Bytecodes::_invokedynamic) {
614 Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
615 Symbol* method_name = vmSymbols::invoke_name();
616 Symbol* method_signature = pool->signature_ref_at(index);
617 Klass* current_klass = pool->pool_holder();
618 LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
619 return resolve_method(link_info, code, THREAD);
898 }
899
900 //------------------------------------------------------------------------------------------------------------------------
901 // Field resolution
902
903 void LinkResolver::check_field_accessability(Klass* ref_klass,
904 Klass* resolved_klass,
905 Klass* sel_klass,
906 const fieldDescriptor& fd,
907 TRAPS) {
908 bool can_access = Reflection::verify_member_access(ref_klass,
909 resolved_klass,
910 sel_klass,
911 fd.access_flags(),
912 true, false, CHECK);
913 // Any existing exceptions that may have been thrown, for example LinkageErrors
914 // from nest-host resolution, have been allowed to propagate.
915 if (!can_access) {
916 bool same_module = (sel_klass->module() == ref_klass->module());
917 ResourceMark rm(THREAD);
918 Exceptions::fthrow(
919 THREAD_AND_LOCATION,
920 vmSymbols::java_lang_IllegalAccessError(),
921 "class %s tried to access %s%sfield %s.%s (%s%s%s)",
922 ref_klass->external_name(),
923 fd.is_protected() ? "protected " : "",
924 fd.is_private() ? "private " : "",
925 sel_klass->external_name(),
926 fd.name()->as_C_string(),
927 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
928 (same_module) ? "" : "; ",
929 (same_module) ? "" : sel_klass->class_in_module_of_loader()
930 );
931 return;
932 }
933 }
934
935 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
936 LinkInfo link_info(pool, index, method, CHECK);
937 resolve_field(fd, link_info, byte, true, CHECK);
938 }
939
940 void LinkResolver::resolve_field(fieldDescriptor& fd,
941 const LinkInfo& link_info,
942 Bytecodes::Code byte, bool initialize_class,
943 TRAPS) {
944 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
945 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||
946 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield ||
947 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
948
949 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
950 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
|
1 /*
2 * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
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 *
530 if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
531 if (appendix.not_null()) expected_size_of_params += 1;
532 if (actual_size_of_params != expected_size_of_params) {
533 tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
534 tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
535 result->print();
536 }
537 assert(actual_size_of_params == expected_size_of_params,
538 "%d != %d", actual_size_of_params, expected_size_of_params);
539 #endif //ASSERT
540
541 assert(appendix_result_or_null != NULL, "");
542 (*appendix_result_or_null) = appendix;
543 }
544 return result;
545 }
546 }
547 return NULL;
548 }
549
550 static void print_nest_host_error_on(stringStream* ss, Klass* ref_klass, Klass* sel_klass, TRAPS) {
551 assert(ref_klass->is_instance_klass(), "must be");
552 assert(sel_klass->is_instance_klass(), "must be");
553 InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
554 InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
555 const char* nest_host_error_1 = ref_ik->nest_host_error(THREAD);
556 const char* nest_host_error_2 = sel_ik->nest_host_error(THREAD);
557 if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) {
558 ss->print(", (%s%s%s)",
559 (nest_host_error_1 != NULL) ? nest_host_error_1 : "",
560 (nest_host_error_1 != NULL && nest_host_error_2 != NULL) ? ", " : "",
561 (nest_host_error_2 != NULL) ? nest_host_error_2 : "");
562 }
563 }
564
565 void LinkResolver::check_method_accessability(Klass* ref_klass,
566 Klass* resolved_klass,
567 Klass* sel_klass,
568 const methodHandle& sel_method,
569 TRAPS) {
570
571 AccessFlags flags = sel_method->access_flags();
572
573 // Special case: arrays always override "clone". JVMS 2.15.
574 // If the resolved klass is an array class, and the declaring class
575 // is java.lang.Object and the method is "clone", set the flags
576 // to public.
577 //
578 // We'll check for the method name first, as that's most likely
579 // to be false (so we'll short-circuit out of these tests).
580 if (sel_method->name() == vmSymbols::clone_name() &&
581 sel_klass == SystemDictionary::Object_klass() &&
582 resolved_klass->is_array_klass()) {
583 // We need to change "protected" to "public".
584 assert(flags.is_protected(), "clone not protected?");
585 jint new_flags = flags.as_int();
586 new_flags = new_flags & (~JVM_ACC_PROTECTED);
587 new_flags = new_flags | JVM_ACC_PUBLIC;
588 flags.set_flags(new_flags);
589 }
590 // assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
591
592 bool can_access = Reflection::verify_member_access(ref_klass,
593 resolved_klass,
594 sel_klass,
595 flags,
596 true, false, CHECK);
597 // Any existing exceptions that may have been thrown
598 // have been allowed to propagate.
599 if (!can_access) {
600 ResourceMark rm(THREAD);
601 stringStream ss;
602 bool same_module = (sel_klass->module() == ref_klass->module());
603 ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",
604 ref_klass->external_name(),
605 sel_method->is_abstract() ? "abstract " : "",
606 sel_method->is_protected() ? "protected " : "",
607 sel_method->is_private() ? "private " : "",
608 sel_method->external_name(),
609 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
610 (same_module) ? "" : "; ",
611 (same_module) ? "" : sel_klass->class_in_module_of_loader()
612 );
613
614 // For private access check if there was a problem with nest host
615 // resolution, and if so report that as part of the message.
616 if (sel_method->is_private()) {
617 print_nest_host_error_on(&ss, ref_klass, sel_klass, THREAD);
618 }
619
620 Exceptions::fthrow(THREAD_AND_LOCATION,
621 vmSymbols::java_lang_IllegalAccessError(),
622 "%s",
623 ss.as_string()
624 );
625 return;
626 }
627 }
628
629 Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
630 const constantPoolHandle& pool, int index, TRAPS) {
631 // This method is used only
632 // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
633 // and
634 // (2) in Bytecode_invoke::static_target
635 // It appears to fail when applied to an invokeinterface call site.
636 // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
637 // resolve klass
638 if (code == Bytecodes::_invokedynamic) {
639 Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
640 Symbol* method_name = vmSymbols::invoke_name();
641 Symbol* method_signature = pool->signature_ref_at(index);
642 Klass* current_klass = pool->pool_holder();
643 LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
644 return resolve_method(link_info, code, THREAD);
923 }
924
925 //------------------------------------------------------------------------------------------------------------------------
926 // Field resolution
927
928 void LinkResolver::check_field_accessability(Klass* ref_klass,
929 Klass* resolved_klass,
930 Klass* sel_klass,
931 const fieldDescriptor& fd,
932 TRAPS) {
933 bool can_access = Reflection::verify_member_access(ref_klass,
934 resolved_klass,
935 sel_klass,
936 fd.access_flags(),
937 true, false, CHECK);
938 // Any existing exceptions that may have been thrown, for example LinkageErrors
939 // from nest-host resolution, have been allowed to propagate.
940 if (!can_access) {
941 bool same_module = (sel_klass->module() == ref_klass->module());
942 ResourceMark rm(THREAD);
943 stringStream ss;
944 ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",
945 ref_klass->external_name(),
946 fd.is_protected() ? "protected " : "",
947 fd.is_private() ? "private " : "",
948 sel_klass->external_name(),
949 fd.name()->as_C_string(),
950 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
951 (same_module) ? "" : "; ",
952 (same_module) ? "" : sel_klass->class_in_module_of_loader()
953 );
954 // For private access check if there was a problem with nest host
955 // resolution, and if so report that as part of the message.
956 if (fd.is_private()) {
957 print_nest_host_error_on(&ss, ref_klass, sel_klass, THREAD);
958 }
959 Exceptions::fthrow(THREAD_AND_LOCATION,
960 vmSymbols::java_lang_IllegalAccessError(),
961 "%s",
962 ss.as_string()
963 );
964 return;
965 }
966 }
967
968 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
969 LinkInfo link_info(pool, index, method, CHECK);
970 resolve_field(fd, link_info, byte, true, CHECK);
971 }
972
973 void LinkResolver::resolve_field(fieldDescriptor& fd,
974 const LinkInfo& link_info,
975 Bytecodes::Code byte, bool initialize_class,
976 TRAPS) {
977 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
978 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||
979 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield ||
980 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
981
982 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
983 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
|