< prev index next >

src/hotspot/share/cds/aotConstantPoolResolver.cpp

Print this page
@@ -21,23 +21,36 @@
   * questions.
   *
   */
  
  #include "cds/aotClassLinker.hpp"
+ #include "cds/aotClassLocation.hpp"
  #include "cds/aotConstantPoolResolver.hpp"
+ #include "cds/aotLogging.hpp"
  #include "cds/archiveBuilder.hpp"
+ #include "cds/archiveUtils.inline.hpp"
  #include "cds/cdsConfig.hpp"
+ #include "cds/classListWriter.hpp"
+ #include "cds/finalImageRecipes.hpp"
+ #include "cds/heapShared.hpp"
+ #include "cds/lambdaFormInvokers.inline.hpp"
+ #include "classfile/classLoader.hpp"
+ #include "classfile/dictionary.hpp"
+ #include "classfile/symbolTable.hpp"
  #include "classfile/systemDictionary.hpp"
  #include "classfile/systemDictionaryShared.hpp"
  #include "classfile/vmClasses.hpp"
  #include "interpreter/bytecodeStream.hpp"
  #include "interpreter/interpreterRuntime.hpp"
  #include "memory/resourceArea.hpp"
  #include "oops/constantPool.inline.hpp"
+ #include "oops/fieldStreams.inline.hpp"
  #include "oops/instanceKlass.hpp"
  #include "oops/klass.inline.hpp"
  #include "runtime/handles.inline.hpp"
+ #include "runtime/javaCalls.hpp"
+ #include "runtime/signature.hpp"
  
  // Returns true if we CAN PROVE that cp_index will always resolve to
  // the same information at both dump time and run time. This is a
  // necessary (but not sufficient) condition for pre-resolving cp_index
  // during CDS archive assembly.

@@ -142,10 +155,33 @@
      case JVM_CONSTANT_String:
        resolve_string(cp, cp_index, CHECK); // may throw OOM when interning strings.
        break;
      }
    }
+ 
+   // Normally, we don't want to archive any CP entries that were not resolved
+   // in the training run. Otherwise the AOT/JIT may inline too much code that has not
+   // been executed.
+   //
+   // However, we want to aggressively resolve all klass/field/method constants for
+   // LambdaForm Invoker Holder classes, Lambda Proxy classes, and LambdaForm classes,
+   // so that the compiler can inline through them.
+   if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
+     bool eager_resolve = false;
+ 
+     if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
+       eager_resolve = true;
+     }
+     if (ik->is_hidden() && HeapShared::is_archivable_hidden_klass(ik)) {
+       eager_resolve = true;
+     }
+ 
+     if (eager_resolve) {
+       preresolve_class_cp_entries(THREAD, ik, nullptr);
+       preresolve_field_and_method_cp_entries(THREAD, ik, nullptr);
+     }
+   }
  }
  
  // This works only for the boot/platform/app loaders
  Klass* AOTConstantPoolResolver::find_loaded_class(Thread* current, oop class_loader, Symbol* name) {
    HandleMark hm(current);

@@ -222,10 +258,12 @@
      BytecodeStream bcs(methodHandle(THREAD, m));
      while (!bcs.is_last_bytecode()) {
        bcs.next();
        Bytecodes::Code raw_bc = bcs.raw_code();
        switch (raw_bc) {
+       case Bytecodes::_getstatic:
+       case Bytecodes::_putstatic:
        case Bytecodes::_getfield:
        case Bytecodes::_putfield:
          maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
          if (HAS_PENDING_EXCEPTION) {
            CLEAR_PENDING_EXCEPTION; // just ignore

@@ -233,10 +271,11 @@
          break;
        case Bytecodes::_invokehandle:
        case Bytecodes::_invokespecial:
        case Bytecodes::_invokevirtual:
        case Bytecodes::_invokeinterface:
+       case Bytecodes::_invokestatic:
          maybe_resolve_fmi_ref(ik, m, raw_bc, bcs.get_index_u2(), preresolve_list, THREAD);
          if (HAS_PENDING_EXCEPTION) {
            CLEAR_PENDING_EXCEPTION; // just ignore
          }
          break;

@@ -269,17 +308,34 @@
      // Do not resolve any field/methods from a class that has not been loaded yet.
      return;
    }
  
    Klass* resolved_klass = cp->klass_ref_at(raw_index, bc, CHECK);
+   const char* is_static = "";
  
    switch (bc) {
+   case Bytecodes::_getstatic:
+   case Bytecodes::_putstatic:
+     if (!VM_Version::supports_fast_class_init_checks()) {
+       return; // Do not resolve since interpreter lacks fast clinit barriers support
+     }
+     InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
+     is_static = " *** static";
+     break;
    case Bytecodes::_getfield:
    case Bytecodes::_putfield:
      InterpreterRuntime::resolve_get_put(bc, raw_index, mh, cp, false /*initialize_holder*/, CHECK);
      break;
  
+   case Bytecodes::_invokestatic:
+     if (!VM_Version::supports_fast_class_init_checks()) {
+       return; // Do not resolve since interpreter lacks fast clinit barriers support
+     }
+     InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
+     is_static = " *** static";
+     break;
+ 
    case Bytecodes::_invokevirtual:
    case Bytecodes::_invokespecial:
    case Bytecodes::_invokeinterface:
      InterpreterRuntime::cds_resolve_invoke(bc, raw_index, cp, CHECK);
      break;

@@ -295,15 +351,15 @@
    if (log_is_enabled(Trace, aot, resolve)) {
      ResourceMark rm(THREAD);
      bool resolved = cp->is_resolved(raw_index, bc);
      Symbol* name = cp->name_ref_at(raw_index, bc);
      Symbol* signature = cp->signature_ref_at(raw_index, bc);
-     log_trace(aot, resolve)("%s %s [%3d] %s -> %s.%s:%s",
+     log_trace(aot, resolve)("%s %s [%3d] %s -> %s.%s:%s%s",
                              (resolved ? "Resolved" : "Failed to resolve"),
                              Bytecodes::name(bc), cp_index, ik->external_name(),
                              resolved_klass->external_name(),
-                             name->as_C_string(), signature->as_C_string());
+                             name->as_C_string(), signature->as_C_string(), is_static);
    }
  }
  
  void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list) {
    JavaThread* THREAD = current;

@@ -547,5 +603,241 @@
    } else {
      return ArchiveBuilder::current()->is_in_buffer_space(p);
    }
  }
  #endif
+ 
+ // Paranoid check -- if any field/method signature in <ik> is referencing a class that is known to be
+ // excluded, do not archive any reflection data in <ik>.
+ bool AOTConstantPoolResolver::check_signature_for_reflection_data(InstanceKlass* ik, Symbol* sig, bool is_method) {
+   precond(SafepointSynchronize::is_at_safepoint());
+   ResourceMark rm;
+   for (SignatureStream ss(sig, is_method); !ss.is_done(); ss.next()) {
+     if (ss.is_reference()) {
+       Symbol* klass_name = ss.as_symbol(/*probe_only=*/true);
+       if (klass_name == nullptr) {
+         // This symbol has never been created during the assembly phase, so it can't
+         // possibly be referencing an excluded class.
+         continue;
+       }
+       if (Signature::is_array(klass_name)) {
+         SignatureStream ss2(klass_name, false);
+         ss2.skip_array_prefix();
+         if (ss2.is_reference() && ss2.as_symbol(/*probe_only=*/true) == nullptr) {
+           // klass_name is an array klass that has not been loaded during the assembly phase, so it can't
+           // possibly be referencing an excluded class.
+           continue;
+         }
+       }
+       Klass* k = find_loaded_class(Thread::current(), ik->class_loader(), klass_name);
+       if (k == nullptr) {
+         // No class of this name has been loaded for this loader, so this name can't
+         // possibly be referencing an excluded class.
+         continue;
+       }
+       if (SystemDictionaryShared::should_be_excluded(k)) {
+         if (log_is_enabled(Warning, aot, resolve)) {
+           ResourceMark rm;
+           log_warning(aot, resolve)("Cannot archive reflection data in %s because it refers to excluded class %s",
+                                     ik->external_name(), k->external_name());
+         }
+         return false;
+       }
+     }
+   }
+   return true;
+ }
+ 
+ bool AOTConstantPoolResolver::can_archive_reflection_data_declared_fields(InstanceKlass* ik) {
+   for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
+     if (!check_signature_for_reflection_data(ik, fs.signature(), /*is_method=*/false)) {
+       return false;
+     }
+   }
+ 
+   return true;
+ }
+ 
+ bool AOTConstantPoolResolver::can_archive_reflection_data_declared_methods(InstanceKlass* ik) {
+   Array<Method*>* methods = ik->methods();
+   for (int i = 0; i < methods->length(); i++) {
+     Method* m = methods->at(i);
+     if (!check_signature_for_reflection_data(ik, m->signature(), /*is_method=*/true)) {
+       return false;
+     }
+   }
+ 
+   return true;
+ }
+ 
+ // This is called by AOT assembly phase to see if java_lang_Class::reflection_data(k) can be
+ // archived.
+ bool AOTConstantPoolResolver::can_archive_reflection_data(InstanceKlass* ik) {
+   // At this point, we have resolved the reflection data of *all* classes
+   // recorded by FinalImageRecipes.
+   // When we are in the AOT safepoint, we will archive the reflection
+   // data of <ik> *only if* it doesn't reference any excluded classes.
+   precond(SafepointSynchronize::is_at_safepoint());
+ 
+   if (!can_archive_reflection_data_declared_fields(ik)) {
+     return false;
+   }
+   if (!can_archive_reflection_data_declared_methods(ik)) {
+     return false;
+   }
+ 
+   if (log_is_enabled(Info, aot, resolve)) {
+     ResourceMark rm;
+     log_info(aot, resolve)("Archived reflection data in %s", ik->external_name());
+   }
+ 
+   return true;
+ }
+ 
+ int AOTConstantPoolResolver::class_reflection_data_flags(InstanceKlass* ik, TRAPS) {
+   assert(java_lang_Class::has_reflection_data(ik->java_mirror()), "must be");
+ 
+   HandleMark hm(THREAD);
+   JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
+   JavaValue result(T_INT);
+   JavaCalls::call_special(&result,
+                           vmClasses::Class_klass(),
+                           vmSymbols::encodeReflectionData_name(),
+                           vmSymbols::void_int_signature(),
+                           &args, CHECK_0);
+   int flags = result.get_jint();
+   aot_log_info(aot)("Encode ReflectionData: %s (flags=0x%x)", ik->external_name(), flags);
+   return flags;
+ }
+ 
+ void AOTConstantPoolResolver::generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags) {
+   aot_log_info(aot)("Generate ReflectionData: %s (flags=" INT32_FORMAT_X ")", ik->external_name(), rd_flags);
+   JavaThread* THREAD = current; // for exception macros
+   JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
+   args.push_int(rd_flags);
+   JavaValue result(T_OBJECT);
+   JavaCalls::call_special(&result,
+                           vmClasses::Class_klass(),
+                           vmSymbols::generateReflectionData_name(),
+                           vmSymbols::int_void_signature(),
+                           &args, THREAD);
+   if (HAS_PENDING_EXCEPTION) {
+     Handle exc_handle(THREAD, PENDING_EXCEPTION);
+     CLEAR_PENDING_EXCEPTION;
+ 
+     log_warning(aot)("Exception during Class::generateReflectionData() call for %s", ik->external_name());
+     LogStreamHandle(Debug, aot) log;
+     if (log.is_enabled()) {
+       java_lang_Throwable::print_stack_trace(exc_handle, &log);
+     }
+   }
+ }
+ 
+ Klass* AOTConstantPoolResolver::resolve_boot_class_or_fail(const char* class_name, TRAPS) {
+   Handle class_loader;
+   TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
+   return SystemDictionary::resolve_or_fail(class_name_sym, class_loader, true, THREAD);
+ }
+ 
+ void AOTConstantPoolResolver::trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
+   if (interfaces->length() < 1) {
+     return;
+   }
+   if (ClassListWriter::is_enabled()) {
+     const char* loader_name = ArchiveUtils::builtin_loader_name_or_null(loader);
+     if (loader_name != nullptr) {
+       stringStream ss;
+       ss.print("%s %s %d %d", loader_name, proxy_name, access_flags, interfaces->length());
+       for (int i = 0; i < interfaces->length(); i++) {
+         oop mirror = interfaces->obj_at(i);
+         Klass* k = java_lang_Class::as_Klass(mirror);
+         ss.print(" %s", k->name()->as_C_string());
+       }
+       ClassListWriter w; // This locks ClassListFile_lock
+       w.stream()->print_cr("@dynamic-proxy %s", ss.freeze());
+     }
+   }
+   if (CDSConfig::is_dumping_preimage_static_archive()) {
+     FinalImageRecipes::add_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags);
+   }
+ }
+ 
+ void AOTConstantPoolResolver::init_dynamic_proxy_cache(TRAPS) {
+   static bool inited = false;
+   if (inited) {
+     return;
+   }
+   inited = true;
+ 
+   Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy", CHECK);
+   TempNewSymbol method = SymbolTable::new_symbol("initCacheForCDS");
+   TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)V");
+ 
+   JavaCallArguments args;
+   args.push_oop(Handle(THREAD, SystemDictionary::java_platform_loader()));
+   args.push_oop(Handle(THREAD, SystemDictionary::java_system_loader()));
+   JavaValue result(T_VOID);
+   JavaCalls::call_static(&result,
+                          klass,
+                          method,
+                          signature,
+                          &args, CHECK);
+ }
+ 
+ 
+ void AOTConstantPoolResolver::define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS) {
+   if (!CDSConfig::is_dumping_dynamic_proxies()) {
+     return;
+   }
+   init_dynamic_proxy_cache(CHECK);
+ 
+   Klass* klass = resolve_boot_class_or_fail("java/lang/reflect/Proxy$ProxyBuilder", CHECK);
+   TempNewSymbol method = SymbolTable::new_symbol("defineProxyClassForCDS");
+   TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/Class;I)Ljava/lang/Class;");
+ 
+   JavaCallArguments args;
+   args.push_oop(Handle(THREAD, loader()));
+   args.push_oop(Handle(THREAD, proxy_name()));
+   args.push_oop(Handle(THREAD, interfaces()));
+   args.push_int(access_flags);
+   JavaValue result(T_OBJECT);
+   JavaCalls::call_static(&result,
+                          klass,
+                          method,
+                          signature,
+                          &args, CHECK);
+ 
+   // Assumptions:
+   // FMG is archived, which means -modulepath and -Xbootclasspath are both not specified.
+   // All named modules are loaded from the system modules files.
+   // TODO: test support for -Xbootclasspath after JDK-8322322. Some of the code below need to be changed.
+   // TODO: we just give dummy shared_classpath_index for the generated class so that it will be archived.
+   //       The index is not used at runtime (see SystemDictionaryShared::load_shared_class_for_builtin_loader, which
+   //       uses a null ProtectionDomain for this class)
+   oop mirror = result.get_oop();
+   assert(mirror != nullptr, "class must have been generated if not OOM");
+   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
+   if (ik->defined_by_boot_loader() || ik->defined_by_platform_loader()) {
+     assert(ik->module()->is_named(), "dynamic proxies defined in unnamed modules for boot/platform loaders not supported");
+     ik->set_shared_classpath_index(0);
+   } else {
+     assert(ik->defined_by_app_loader(), "must be");
+     ik->set_shared_classpath_index(AOTClassLocationConfig::dumptime()->app_cp_start_index());
+   }
+ 
+   ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
+   if (log_is_enabled(Info, cds, dynamic, proxy)) {
+     ResourceMark rm(THREAD);
+     stringStream ss;
+     const char* prefix = "";
+     ss.print("%s (%-7s, cp index = %d) implements ", ik->external_name(),
+              ArchiveUtils::builtin_loader_name(loader()), ik->shared_classpath_index());
+     objArrayOop intfs = (objArrayOop)interfaces();
+     for (int i = 0; i < intfs->length(); i++) {
+       oop intf_mirror = intfs->obj_at(i);
+       ss.print("%s%s", prefix, java_lang_Class::as_Klass(intf_mirror)->external_name());
+       prefix = ", ";
+     }
+ 
+     log_info(cds, dynamic, proxy)("%s", ss.freeze());
+   }
+ }
< prev index next >