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 *
23 */
24
25 #include "cds/aotClassLinker.hpp"
26 #include "cds/aotConstantPoolResolver.hpp"
27 #include "cds/archiveBuilder.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "classfile/systemDictionaryShared.hpp"
31 #include "classfile/vmClasses.hpp"
32 #include "interpreter/bytecodeStream.hpp"
33 #include "interpreter/interpreterRuntime.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "oops/constantPool.inline.hpp"
36 #include "oops/instanceKlass.hpp"
37 #include "oops/klass.inline.hpp"
38 #include "runtime/handles.inline.hpp"
39
40 // Returns true if we CAN PROVE that cp_index will always resolve to
41 // the same information at both dump time and run time. This is a
42 // necessary (but not sufficient) condition for pre-resolving cp_index
43 // during CDS archive assembly.
44 bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int cp_index) {
45 assert(!is_in_archivebuilder_buffer(cp), "sanity");
46
47 if (cp->tag_at(cp_index).is_klass()) {
48 // We require cp_index to be already resolved. This is fine for now, are we
49 // currently archive only CP entries that are already resolved.
50 Klass* resolved_klass = cp->resolved_klass_at(cp_index);
51 return resolved_klass != nullptr && is_class_resolution_deterministic(cp->pool_holder(), resolved_klass);
52 } else if (cp->tag_at(cp_index).is_invoke_dynamic()) {
53 return is_indy_resolution_deterministic(cp, cp_index);
54 } else if (cp->tag_at(cp_index).is_field() ||
55 cp->tag_at(cp_index).is_method() ||
56 cp->tag_at(cp_index).is_interface_method()) {
57 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
58 if (!cp->tag_at(klass_cp_index).is_klass()) {
131 } else if (resolved_class->is_typeArray_klass()) {
132 return true;
133 } else {
134 return false;
135 }
136 }
137
138 void AOTConstantPoolResolver::preresolve_string_cp_entries(InstanceKlass* ik, TRAPS) {
139 if (!ik->is_linked()) {
140 // The cp->resolved_referenced() array is not ready yet, so we can't call resolve_string().
141 return;
142 }
143 constantPoolHandle cp(THREAD, ik->constants());
144 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
145 switch (cp->tag_at(cp_index).value()) {
146 case JVM_CONSTANT_String:
147 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
148 break;
149 }
150 }
151 }
152
153 // This works only for the boot/platform/app loaders
154 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
155 HandleMark hm(current);
156 Handle h_loader(current, class_loader);
157 Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader);
158 if (k != nullptr) {
159 return k;
160 }
161 if (h_loader() == SystemDictionary::java_system_loader()) {
162 return find_loaded_class(current, SystemDictionary::java_platform_loader(), name);
163 } else if (h_loader() == SystemDictionary::java_platform_loader()) {
164 return find_loaded_class(current, nullptr, name);
165 } else {
166 assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p",
167 cast_from_oop<address>(h_loader()),
168 cast_from_oop<address>(SystemDictionary::java_system_loader()),
169 cast_from_oop<address>(SystemDictionary::java_platform_loader()));
170 }
593
594 // dynamicMethodType
595 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
596 return false;
597 }
598
599 return true;
600 }
601
602 return false;
603 }
604 #ifdef ASSERT
605 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
606 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
607 return false;
608 } else {
609 return ArchiveBuilder::current()->is_in_buffer_space(p);
610 }
611 }
612 #endif
|
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 *
23 */
24
25 #include "cds/aotClassLinker.hpp"
26 #include "cds/aotClassLocation.hpp"
27 #include "cds/aotConstantPoolResolver.hpp"
28 #include "cds/aotLogging.hpp"
29 #include "cds/archiveBuilder.hpp"
30 #include "cds/archiveUtils.inline.hpp"
31 #include "cds/cdsConfig.hpp"
32 #include "cds/classListWriter.hpp"
33 #include "cds/finalImageRecipes.hpp"
34 #include "cds/heapShared.hpp"
35 #include "cds/lambdaFormInvokers.inline.hpp"
36 #include "classfile/classLoader.hpp"
37 #include "classfile/dictionary.hpp"
38 #include "classfile/symbolTable.hpp"
39 #include "classfile/systemDictionary.hpp"
40 #include "classfile/systemDictionaryShared.hpp"
41 #include "classfile/vmClasses.hpp"
42 #include "interpreter/bytecodeStream.hpp"
43 #include "interpreter/interpreterRuntime.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "oops/constantPool.inline.hpp"
46 #include "oops/fieldStreams.inline.hpp"
47 #include "oops/instanceKlass.hpp"
48 #include "oops/klass.inline.hpp"
49 #include "runtime/handles.inline.hpp"
50 #include "runtime/javaCalls.hpp"
51 #include "runtime/signature.hpp"
52
53 // Returns true if we CAN PROVE that cp_index will always resolve to
54 // the same information at both dump time and run time. This is a
55 // necessary (but not sufficient) condition for pre-resolving cp_index
56 // during CDS archive assembly.
57 bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int cp_index) {
58 assert(!is_in_archivebuilder_buffer(cp), "sanity");
59
60 if (cp->tag_at(cp_index).is_klass()) {
61 // We require cp_index to be already resolved. This is fine for now, are we
62 // currently archive only CP entries that are already resolved.
63 Klass* resolved_klass = cp->resolved_klass_at(cp_index);
64 return resolved_klass != nullptr && is_class_resolution_deterministic(cp->pool_holder(), resolved_klass);
65 } else if (cp->tag_at(cp_index).is_invoke_dynamic()) {
66 return is_indy_resolution_deterministic(cp, cp_index);
67 } else if (cp->tag_at(cp_index).is_field() ||
68 cp->tag_at(cp_index).is_method() ||
69 cp->tag_at(cp_index).is_interface_method()) {
70 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
71 if (!cp->tag_at(klass_cp_index).is_klass()) {
144 } else if (resolved_class->is_typeArray_klass()) {
145 return true;
146 } else {
147 return false;
148 }
149 }
150
151 void AOTConstantPoolResolver::preresolve_string_cp_entries(InstanceKlass* ik, TRAPS) {
152 if (!ik->is_linked()) {
153 // The cp->resolved_referenced() array is not ready yet, so we can't call resolve_string().
154 return;
155 }
156 constantPoolHandle cp(THREAD, ik->constants());
157 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
158 switch (cp->tag_at(cp_index).value()) {
159 case JVM_CONSTANT_String:
160 resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
161 break;
162 }
163 }
164
165 // Normally, we don't want to archive any CP entries that were not resolved
166 // in the training run. Otherwise the AOT/JIT may inline too much code that has not
167 // been executed.
168 //
169 // However, we want to aggressively resolve all klass/field/method constants for
170 // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes,
171 // so that the compiler can inline through them.
172 if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
173 bool eager_resolve = false;
174
175 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
176 eager_resolve = true;
177 }
178 if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) {
179 eager_resolve = true;
180 }
181
182 if (eager_resolve) {
183 preresolve_class_cp_entries(THREAD, ik, nullptr);
184 preresolve_field_and_method_cp_entries(THREAD, ik, nullptr);
185 }
186 }
187 }
188
189 // This works only for the boot/platform/app loaders
190 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
191 HandleMark hm(current);
192 Handle h_loader(current, class_loader);
193 Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader);
194 if (k != nullptr) {
195 return k;
196 }
197 if (h_loader() == SystemDictionary::java_system_loader()) {
198 return find_loaded_class(current, SystemDictionary::java_platform_loader(), name);
199 } else if (h_loader() == SystemDictionary::java_platform_loader()) {
200 return find_loaded_class(current, nullptr, name);
201 } else {
202 assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p",
203 cast_from_oop<address>(h_loader()),
204 cast_from_oop<address>(SystemDictionary::java_system_loader()),
205 cast_from_oop<address>(SystemDictionary::java_platform_loader()));
206 }
629
630 // dynamicMethodType
631 if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
632 return false;
633 }
634
635 return true;
636 }
637
638 return false;
639 }
640 #ifdef ASSERT
641 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
642 if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
643 return false;
644 } else {
645 return ArchiveBuilder::current()->is_in_buffer_space(p);
646 }
647 }
648 #endif
649
650 // Paranoid check -- if any field/method signature in <ik> is referencing a class that is known to be
651 // excluded, do not archive any reflection data in <ik>.
652 bool AOTConstantPoolResolver::check_signature_for_reflection_data(InstanceKlass* ik, Symbol* sig, bool is_method) {
653 precond(SafepointSynchronize::is_at_safepoint());
654 ResourceMark rm;
655 for (SignatureStream ss(sig, is_method); !ss.is_done(); ss.next()) {
656 if (ss.is_reference()) {
657 Symbol* klass_name = ss.as_symbol(/*probe_only=*/true);
658 if (klass_name == nullptr) {
659 // This symbol has never been created during the assembly phase, so it can't
660 // possibly be referencing an excluded class.
661 continue;
662 }
663 if (Signature::is_array(klass_name)) {
664 SignatureStream ss2(klass_name, false);
665 ss2.skip_array_prefix();
666 if (ss2.is_reference() && ss2.as_symbol(/*probe_only=*/true) == nullptr) {
667 // klass_name is an array klass that has not been loaded during the assembly phase, so it can't
668 // possibly be referencing an excluded class.
669 continue;
670 }
671 }
672 Klass* k = find_loaded_class(Thread::current(), ik->class_loader(), klass_name);
673 if (k == nullptr) {
674 // No class of this name has been loaded for this loader, so this name can't
675 // possibly be referencing an excluded class.
676 continue;
677 }
678 if (SystemDictionaryShared::should_be_excluded(k)) {
679 if (log_is_enabled(Warning, aot, resolve)) {
680 ResourceMark rm;
681 log_warning(aot, resolve)("Cannot archive reflection data in %s because it refers to excluded class %s",
682 ik->external_name(), k->external_name());
683 }
684 return false;
685 }
686 }
687 }
688 return true;
689 }
690
691 bool AOTConstantPoolResolver::can_archive_reflection_data_declared_fields(InstanceKlass* ik) {
692 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
693 if (!check_signature_for_reflection_data(ik, fs.signature(), /*is_method=*/false)) {
694 return false;
695 }
696 }
697
698 return true;
699 }
700
701 bool AOTConstantPoolResolver::can_archive_reflection_data_declared_methods(InstanceKlass* ik) {
702 Array<Method*>* methods = ik->methods();
703 for (int i = 0; i < methods->length(); i++) {
704 Method* m = methods->at(i);
705 if (!check_signature_for_reflection_data(ik, m->signature(), /*is_method=*/true)) {
706 return false;
707 }
708 }
709
710 return true;
711 }
712
713 // This is called by AOT assembly phase to see if java_lang_Class::reflection_data(k) can be
714 // archived.
715 bool AOTConstantPoolResolver::can_archive_reflection_data(InstanceKlass* ik) {
716 // At this point, we have resolved the reflection data of *all* classes
717 // recorded by FinalImageRecipes.
718 // When we are in the AOT safepoint, we will archive the reflection
719 // data of <ik> *only if* it doesn't reference any excluded classes.
720 precond(SafepointSynchronize::is_at_safepoint());
721
722 if (!can_archive_reflection_data_declared_fields(ik)) {
723 return false;
724 }
725 if (!can_archive_reflection_data_declared_methods(ik)) {
726 return false;
727 }
728
729 if (log_is_enabled(Info, aot, resolve)) {
730 ResourceMark rm;
731 log_info(aot, resolve)("Archived reflection data in %s", ik->external_name());
732 }
733
734 return true;
735 }
736
737 int AOTConstantPoolResolver::class_reflection_data_flags(InstanceKlass* ik, TRAPS) {
738 assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be");
739
740 HandleMark hm(THREAD);
741 JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
742 JavaValue result(T_INT);
743 JavaCalls::call_special(&result,
744 vmClasses::Class_klass(),
745 vmSymbols::encodeReflectionData_name(),
746 vmSymbols::void_int_signature(),
747 &args, CHECK_0);
748 int flags = result.get_jint();
749 aot_log_info(aot)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags);
750 return flags;
751 }
752
753 void AOTConstantPoolResolver::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) {
754 aot_log_info(aot)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags);
755 JavaThread* THREAD = current; // for exception macros
756 JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
757 args.push_int(rd_flags);
758 JavaValue result(T_OBJECT);
759 JavaCalls::call_special(&result,
760 vmClasses::Class_klass(),
761 vmSymbols::generateReflectionData_name(),
762 vmSymbols::int_void_signature(),
763 &args, THREAD);
764 if (HAS_PENDING_EXCEPTION) {
765 Handle exc_handle(THREAD, PENDING_EXCEPTION);
766 CLEAR_PENDING_EXCEPTION;
767
768 log_warning(aot)("Exception during Class::generateReflectionData() call for %s", ik->external_name());
769 LogStreamHandle(Debug, aot) log;
770 if (log.is_enabled()) {
771 java_lang_Throwable::print_stack_trace(exc_handle, &log);
772 }
773 }
774 }
775
776 Klass* AOTConstantPoolResolver::resolve_boot_class_or_fail(const char* class_name, TRAPS) {
777 Handle class_loader;
778 TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
779 return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, true, THREAD);
780 }
781
782 void AOTConstantPoolResolver::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
783 if (interfaces->length() < 1) {
784 return;
785 }
786 if (ClassListWriter::is_enabled()) {
787 const char* loader_name = ArchiveUtils::builtin_loader_name_or_null(loader);
788 if (loader_name != nullptr) {
789 stringStream ss;
790 ss.print("%s %s %d %d", loader_name, proxy_name, access_flags, interfaces->length());
791 for (int i = 0; i < interfaces->length(); i++) {
792 oop mirror = interfaces->obj_at(i);
793 Klass* k = java_lang_Class::as_Klass(mirror);
794 ss.print(" %s", k->name()->as_C_string());
795 }
796 ClassListWriter w; // This locks ClassListFile_lock
797 w.stream()->print_cr("@dynamic-proxy %s", ss.freeze());
798 }
799 }
800 if (CDSConfig::is_dumping_preimage_static_archive()) {
801 FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags);
802 }
803 }
804
805 void AOTConstantPoolResolver::init_dynamic_proxy_cache(TRAPS) {
806 static bool inited = false;
807 if (inited) {
808 return;
809 }
810 inited = true;
811
812 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK);
813 TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS");
814 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V");
815
816 JavaCallArguments args;
817 args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader()));
818 args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader()));
819 JavaValue result(T_VOID);
820 JavaCalls::call_static(&result,
821 klass,
822 method,
823 signature,
824 &args, CHECK);
825 }
826
827
828 void AOTConstantPoolResolver::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) {
829 if (!CDSConfig::is_dumping_dynamic_proxies()) {
830 return;
831 }
832 init_dynamic_proxy_cache(CHECK);
833
834 Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK);
835 TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS");
836 TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;");
837
838 JavaCallArguments args;
839 args.push_oop(Handle(THREAD, loader()));
840 args.push_oop(Handle(THREAD, proxy_name()));
841 args.push_oop(Handle(THREAD, interfaces()));
842 args.push_int(access_flags);
843 JavaValue result(T_OBJECT);
844 JavaCalls::call_static(&result,
845 klass,
846 method,
847 signature,
848 &args, CHECK);
849
850 // Assumptions:
851 // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified.
852 // All named modules are loaded from the system modules files.
853 // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed.
854 // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived.
855 // The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which
856 // uses a null ProtectionDomain for this class)
857 oop mirror = result.get_oop();
858 assert(mirror != nullptr, "class must have been generated if not OOM");
859 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
860 if (ik->defined_by_boot_loader() || ik->defined_by_platform_loader()) {
861 assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported");
862 ik->set_shared_classpath_index(0);
863 } else {
864 assert(ik->defined_by_app_loader(), "must be");
865 ik->set_shared_classpath_index(AOTClassLocationConfig::dumptime()->app_cp_start_index());
866 }
867
868 ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
869 if (log_is_enabled(Info, cds, dynamic, proxy)) {
870 ResourceMark rm(THREAD);
871 stringStream ss;
872 const char* prefix = "";
873 ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(),
874 ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index());
875 objArrayOop intfs = (objArrayOop)interfaces();
876 for (int i = 0; i < intfs->length(); i++) {
877 oop intf_mirror = intfs->obj_at(i);
878 ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name());
879 prefix = ", ";
880 }
881
882 log_info(cds, dynamic, proxy)("%s", ss.freeze());
883 }
884 }
|