< prev index next >

src/hotspot/share/cds/aotConstantPoolResolver.cpp

Print this page

  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/finalImageRecipes.hpp"
 33 #include "cds/heapShared.hpp"
 34 #include "cds/lambdaFormInvokers.inline.hpp"
 35 #include "classfile/classLoader.hpp"
 36 #include "classfile/dictionary.hpp"
 37 #include "classfile/symbolTable.hpp"
 38 #include "classfile/systemDictionary.hpp"
 39 #include "classfile/systemDictionaryShared.hpp"
 40 #include "classfile/vmClasses.hpp"
 41 #include "interpreter/bytecodeStream.hpp"
 42 #include "interpreter/interpreterRuntime.hpp"
 43 #include "memory/resourceArea.hpp"
 44 #include "oops/constantPool.inline.hpp"
 45 #include "oops/fieldStreams.inline.hpp"
 46 #include "oops/instanceKlass.hpp"
 47 #include "oops/klass.inline.hpp"
 48 #include "runtime/handles.inline.hpp"
 49 #include "runtime/javaCalls.hpp"
 50 #include "runtime/signature.hpp"
 51 
 52 // Returns true if we CAN PROVE that cp_index will always resolve to
 53 // the same information at both dump time and run time. This is a
 54 // necessary (but not sufficient) condition for pre-resolving cp_index
 55 // during CDS archive assembly.
 56 bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int cp_index) {
 57   assert(!is_in_archivebuilder_buffer(cp), "sanity");
 58 
 59   if (cp->tag_at(cp_index).is_klass()) {
 60     // We require cp_index to be already resolved. This is fine for now, are we
 61     // currently archive only CP entries that are already resolved.
 62     Klass* resolved_klass = cp->resolved_klass_at(cp_index);
 63     return resolved_klass != nullptr && is_class_resolution_deterministic(cp->pool_holder(), resolved_klass);
 64   } else if (cp->tag_at(cp_index).is_invoke_dynamic()) {
 65     return is_indy_resolution_deterministic(cp, cp_index);
 66   } else if (cp->tag_at(cp_index).is_field() ||
 67              cp->tag_at(cp_index).is_method() ||
 68              cp->tag_at(cp_index).is_interface_method()) {
 69     int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
 70     if (!cp->tag_at(klass_cp_index).is_klass()) {

143   } else if (resolved_class->is_typeArray_klass()) {
144     return true;
145   } else {
146     return false;
147   }
148 }
149 
150 void AOTConstantPoolResolver::preresolve_string_cp_entries(InstanceKlass* ik, TRAPS) {
151   if (!ik->is_linked()) {
152     // The cp->resolved_referenced() array is not ready yet, so we can't call resolve_string().
153     return;
154   }
155   constantPoolHandle cp(THREAD, ik->constants());
156   for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
157     switch (cp->tag_at(cp_index).value()) {
158     case JVM_CONSTANT_String:
159       resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
160       break;
161     }
162   }
163 
164   // Normally, we don't want to archive any CP entries that were not resolved
165   // in the training run. Otherwise the AOT/JIT may inline too much code that has not
166   // been executed.
167   //
168   // However, we want to aggressively resolve all klass/field/method constants for
169   // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes,
170   // so that the compiler can inline through them.
171   if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
172     bool eager_resolve = false;
173 
174     if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
175       eager_resolve = true;
176     }
177     if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) {
178       eager_resolve = true;
179     }
180 
181     if (eager_resolve) {
182       preresolve_class_cp_entries(THREAD, ik, nullptr);
183       preresolve_field_and_method_cp_entries(THREAD, ik, nullptr);
184     }
185   }
186 }
187 
188 // This works only for the boot/platform/app loaders
189 Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
190   HandleMark hm(current);
191   Handle h_loader(current, class_loader);
192   Klass* k = SystemDictionary::find_instance_or_array_klass(current, name, h_loader);
193   if (k != nullptr) {
194     return k;
195   }
196   if (h_loader() == SystemDictionary::java_system_loader()) {
197     return find_loaded_class(current, SystemDictionary::java_platform_loader(), name);
198   } else if (h_loader() == SystemDictionary::java_platform_loader()) {
199     return find_loaded_class(current, nullptr, name);
200   } else {
201     assert(h_loader() == nullptr, "This function only works for boot/platform/app loaders %p %p %p",
202            cast_from_oop<address>(h_loader()),
203            cast_from_oop<address>(SystemDictionary::java_system_loader()),
204            cast_from_oop<address>(SystemDictionary::java_platform_loader()));
205   }

628 
629     // dynamicMethodType
630     if (!check_lambda_metafactory_methodtype_arg(cp, bsms_attribute_index, 2)) {
631       return false;
632     }
633 
634     return true;
635   }
636 
637   return false;
638 }
639 #ifdef ASSERT
640 bool AOTConstantPoolResolver::is_in_archivebuilder_buffer(address p) {
641   if (!Thread::current()->is_VM_thread() || ArchiveBuilder::current() == nullptr) {
642     return false;
643   } else {
644     return ArchiveBuilder::current()->is_in_buffer_space(p);
645   }
646 }
647 #endif
648 
649 // Paranoid check -- if any field/method signature in <ik> is referencing a class that is known to be
650 // excluded, do not archive any reflection data in <ik>.
651 bool AOTConstantPoolResolver::check_signature_for_reflection_data(InstanceKlass* ik, Symbol* sig, bool is_method) {
652   precond(SafepointSynchronize::is_at_safepoint());
653   ResourceMark rm;
654   for (SignatureStream ss(sig, is_method); !ss.is_done(); ss.next()) {
655     if (ss.is_reference()) {
656       Symbol* klass_name = ss.as_symbol(/*probe_only=*/true);
657       if (klass_name == nullptr) {
658         // This symbol has never been created during the assembly phase, so it can't
659         // possibly be referencing an excluded class.
660         continue;
661       }
662       if (Signature::is_array(klass_name)) {
663         SignatureStream ss2(klass_name, false);
664         ss2.skip_array_prefix();
665         if (ss2.is_reference() && ss2.as_symbol(/*probe_only=*/true) == nullptr) {
666           // klass_name is an array klass that has not been loaded during the assembly phase, so it can't
667           // possibly be referencing an excluded class.
668           continue;
669         }
670       }
671       Klass* k = find_loaded_class(Thread::current(), ik->class_loader(), klass_name);
672       if (k == nullptr) {
673         // No class of this name has been loaded for this loader, so this name can't
674         // possibly be referencing an excluded class.
675         continue;
676       }
677       if (SystemDictionaryShared::should_be_excluded(k)) {
678         if (log_is_enabled(Warning, aot, resolve)) {
679           ResourceMark rm;
680           log_warning(aot, resolve)("Cannot archive reflection data in %s because it refers to excluded class %s",
681                                     ik->external_name(), k->external_name());
682         }
683         return false;
684       }
685     }
686   }
687   return true;
688 }
689 
690 bool AOTConstantPoolResolver::can_archive_reflection_data_declared_fields(InstanceKlass* ik) {
691   for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
692     if (!check_signature_for_reflection_data(ik, fs.signature(), /*is_method=*/false)) {
693       return false;
694     }
695   }
696 
697   return true;
698 }
699 
700 bool AOTConstantPoolResolver::can_archive_reflection_data_declared_methods(InstanceKlass* ik) {
701   Array<Method*>* methods = ik->methods();
702   for (int i = 0; i < methods->length(); i++) {
703     Method* m = methods->at(i);
704     if (!check_signature_for_reflection_data(ik, m->signature(), /*is_method=*/true)) {
705       return false;
706     }
707   }
708 
709   return true;
710 }
711 
712 // This is called by AOT assembly phase to see if java_lang_Class::reflection_data(k) can be
713 // archived.
714 bool AOTConstantPoolResolver::can_archive_reflection_data(InstanceKlass* ik) {
715   // At this point, we have resolved the reflection data of *all* classes
716   // recorded by FinalImageRecipes.
717   // When we are in the AOT safepoint, we will archive the reflection
718   // data of <ik> *only if* it doesn't reference any excluded classes.
719   precond(SafepointSynchronize::is_at_safepoint());
720 
721   if (!can_archive_reflection_data_declared_fields(ik)) {
722     return false;
723   }
724   if (!can_archive_reflection_data_declared_methods(ik)) {
725     return false;
726   }
727 
728   if (log_is_enabled(Info, aot, resolve)) {
729     ResourceMark rm;
730     log_info(aot, resolve)("Archived reflection data in %s", ik->external_name());
731   }
732 
733   return true;
734 }
735 
736 int AOTConstantPoolResolver::class_reflection_data_flags(InstanceKlass* ik, TRAPS) {
737   assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be");
738 
739   HandleMark hm(THREAD);
740   JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
741   JavaValue result(T_INT);
742   JavaCalls::call_special(&result,
743                           vmClasses::Class_klass(),
744                           vmSymbols::encodeReflectionData_name(),
745                           vmSymbols::void_int_signature(),
746                           &args, CHECK_0);
747   int flags = result.get_jint();
748   aot_log_info(aot)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags);
749   return flags;
750 }
751 
752 void AOTConstantPoolResolver::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) {
753   aot_log_info(aot)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags);
754   JavaThread* THREAD = current; // for exception macros
755   JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
756   args.push_int(rd_flags);
757   JavaValue result(T_OBJECT);
758   JavaCalls::call_special(&result,
759                           vmClasses::Class_klass(),
760                           vmSymbols::generateReflectionData_name(),
761                           vmSymbols::int_void_signature(),
762                           &args, THREAD);
763   if (HAS_PENDING_EXCEPTION) {
764     Handle exc_handle(THREAD, PENDING_EXCEPTION);
765     CLEAR_PENDING_EXCEPTION;
766 
767     log_warning(aot)("Exception during Class::generateReflectionData() call for %s", ik->external_name());
768     LogStreamHandle(Debug, aot) log;
769     if (log.is_enabled()) {
770       java_lang_Throwable::print_stack_trace(exc_handle, &log);
771     }
772   }
773 }
774 
775 Klass* AOTConstantPoolResolver::resolve_boot_class_or_fail(const char* class_name, TRAPS) {
776   Handle class_loader;
777   TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
778   return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, true, THREAD);
779 }
780 
781 void AOTConstantPoolResolver::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
782   if (interfaces->length() < 1) {
783     return;
784   }
785   if (CDSConfig::is_dumping_preimage_static_archive()) {
786     FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags);
787   }
788 }
789 
790 void AOTConstantPoolResolver::init_dynamic_proxy_cache(TRAPS) {
791   static bool inited = false;
792   if (inited) {
793     return;
794   }
795   inited = true;
796 
797   Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK);
798   TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS");
799   TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V");
800 
801   JavaCallArguments args;
802   args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader()));
803   args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader()));
804   JavaValue result(T_VOID);
805   JavaCalls::call_static(&result,
806                          klass,
807                          method,
808                          signature,
809                          &args, CHECK);
810 }
811 
812 
813 void AOTConstantPoolResolver::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) {
814   if (!CDSConfig::is_dumping_dynamic_proxies()) {
815     return;
816   }
817   init_dynamic_proxy_cache(CHECK);
818 
819   Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK);
820   TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS");
821   TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;");
822 
823   JavaCallArguments args;
824   args.push_oop(Handle(THREAD, loader()));
825   args.push_oop(Handle(THREAD, proxy_name()));
826   args.push_oop(Handle(THREAD, interfaces()));
827   args.push_int(access_flags);
828   JavaValue result(T_OBJECT);
829   JavaCalls::call_static(&result,
830                          klass,
831                          method,
832                          signature,
833                          &args, CHECK);
834 
835   // Assumptions:
836   // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified.
837   // All named modules are loaded from the system modules files.
838   // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed.
839   // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived.
840   //       The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which
841   //       uses a null ProtectionDomain for this class)
842   oop mirror = result.get_oop();
843   assert(mirror != nullptr, "class must have been generated if not OOM");
844   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
845   if (ik->defined_by_boot_loader() || ik->defined_by_platform_loader()) {
846     assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported");
847     ik->set_shared_classpath_index(0);
848   } else {
849     assert(ik->defined_by_app_loader(), "must be");
850     ik->set_shared_classpath_index(AOTClassLocationConfig::dumptime()->app_cp_start_index());
851   }
852 
853   ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
854   if (log_is_enabled(Info, cds, dynamic, proxy)) {
855     ResourceMark rm(THREAD);
856     stringStream ss;
857     const char* prefix = "";
858     ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(),
859              ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index());
860     objArrayOop intfs = (objArrayOop)interfaces();
861     for (int i = 0; i < intfs->length(); i++) {
862       oop intf_mirror = intfs->obj_at(i);
863       ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name());
864       prefix = ", ";
865     }
866 
867     log_info(cds, dynamic, proxy)("%s", ss.freeze());
868   }
869 }
< prev index next >