< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page
rev 49800 : 8199852: Print more information about class loaders in LinkageErrors.
Reviewed-by: dholmes


2143 // If so, returns the basic type it holds.  If not, returns T_OBJECT.
2144 BasicType SystemDictionary::box_klass_type(Klass* k) {
2145   assert(k != NULL, "");
2146   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
2147     if (_box_klasses[i] == k)
2148       return (BasicType)i;
2149   }
2150   return T_OBJECT;
2151 }
2152 
2153 // Constraints on class loaders. The details of the algorithm can be
2154 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
2155 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
2156 // that the dictionary needs to maintain a set of contraints that
2157 // must be satisfied by all classes in the dictionary.
2158 // if defining is true, then LinkageError if already in dictionary
2159 // if initiating loader, then ok if InstanceKlass matches existing entry
2160 
2161 void SystemDictionary::check_constraints(unsigned int d_hash,
2162                                          InstanceKlass* k,
2163                                          Handle class_loader, bool defining,

2164                                          TRAPS) {




2165   const char *linkage_error1 = NULL;
2166   const char *linkage_error2 = NULL;




2167   {
2168     Symbol*  name  = k->name();
2169     ClassLoaderData *loader_data = class_loader_data(class_loader);
2170 
2171     MutexLocker mu(SystemDictionary_lock, THREAD);
2172 
2173     InstanceKlass* check = find_class(d_hash, name, loader_data->dictionary());
2174     if (check != NULL) {
2175       // if different InstanceKlass - duplicate class definition,
2176       // else - ok, class loaded by a different thread in parallel,
2177       // we should only have found it if it was done loading and ok to use
2178       // dictionary only holds instance classes, placeholders
2179       // also holds array classes
2180 
2181       assert(check->is_instance_klass(), "noninstance in systemdictionary");
2182       if ((defining == true) || (k != check)) {
2183         linkage_error1 = "loader (instance of ";
2184         linkage_error2 = "): attempted duplicate class definition for name: \"";


2185       } else {
2186         return;
2187       }
2188     }
2189 
2190 #ifdef ASSERT
2191     Symbol* ph_check = find_placeholder(name, loader_data);
2192     assert(ph_check == NULL || ph_check == name, "invalid symbol");
2193 #endif
2194 
2195     if (linkage_error1 == NULL) {
2196       if (constraints()->check_or_update(k, class_loader, name) == false) {
2197         linkage_error1 = "loader constraint violation: loader (instance of ";
2198         linkage_error2 = ") previously initiated loading for a different type with name \"";









2199       }
2200     }
2201   }
2202 
2203   // Throw error now if needed (cannot throw while holding
2204   // SystemDictionary_lock because of rank ordering)
2205 
2206   if (linkage_error1) {
2207     ResourceMark rm(THREAD);
2208     const char* class_loader_name = loader_name(class_loader());
2209     char* type_name = k->name()->as_C_string();
2210     size_t buflen = strlen(linkage_error1) + strlen(class_loader_name) +
2211       strlen(linkage_error2) + strlen(type_name) + 2; // +2 for '"' and null byte.
2212     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
2213     jio_snprintf(buf, buflen, "%s%s%s%s\"", linkage_error1, class_loader_name, linkage_error2, type_name);
2214     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
2215   }
2216 }
2217 
2218 
2219 // Update class loader data dictionary - done after check_constraint and add_to_hierachy
2220 // have been called.
2221 void SystemDictionary::update_dictionary(unsigned int d_hash,
2222                                          int p_index, unsigned int p_hash,
2223                                          InstanceKlass* k,
2224                                          Handle class_loader,
2225                                          TRAPS) {
2226   // Compile_lock prevents systemDictionary updates during compilations
2227   assert_locked_or_safepoint(Compile_lock);
2228   Symbol*  name  = k->name();
2229   ClassLoaderData *loader_data = class_loader_data(class_loader);
2230 
2231   {
2232     MutexLocker mu1(SystemDictionary_lock, THREAD);
2233 
2234     // See whether biased locking is enabled and if so set it for this
2235     // klass.
2236     // Note that this must be done past the last potential blocking
2237     // point / safepoint. We enable biased locking lazily using a
2238     // VM_Operation to iterate the SystemDictionary and installing the


3094 // Combining platform and system loader dictionaries into boot loader dictionary.
3095 // During run time, we only have one shared dictionary.
3096 void SystemDictionary::combine_shared_dictionaries() {
3097   assert(DumpSharedSpaces, "dump time only");
3098   // If AppCDS isn't enabled, we only dump the classes in the boot loader dictionary
3099   // into the shared archive.
3100   if (UseAppCDS) {
3101     Dictionary* master_dictionary = ClassLoaderData::the_null_class_loader_data()->dictionary();
3102     CombineDictionariesClosure cdc(master_dictionary);
3103     ClassLoaderDataGraph::cld_do(&cdc);
3104   }
3105 
3106   // These tables are no longer valid or necessary. Keeping them around will
3107   // cause SystemDictionary::verify() to fail. Let's empty them.
3108   _placeholders        = new PlaceholderTable(_placeholder_table_size);
3109   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
3110 
3111   NOT_PRODUCT(SystemDictionary::verify());
3112 }
3113 
3114 // caller needs ResourceMark
3115 const char* SystemDictionary::loader_name(const oop loader) {
3116   return ((loader) == NULL ? "<bootloader>" :
3117           InstanceKlass::cast((loader)->klass())->name()->as_C_string());
3118 }
3119 
3120 // caller needs ResourceMark
3121 const char* SystemDictionary::loader_name(const ClassLoaderData* loader_data) {
3122   return (loader_data->class_loader() == NULL ? "<bootloader>" :
3123           SystemDictionary::loader_name(loader_data->class_loader()));
3124 }
3125 
3126 void SystemDictionary::initialize_oop_storage() {
3127   _vm_weak_oop_storage =
3128     new OopStorage("VM Weak Oop Handles",
3129                    VMWeakAlloc_lock,
3130                    VMWeakActive_lock);
3131 }
3132 
3133 OopStorage* SystemDictionary::vm_weak_oop_storage() {
3134   assert(_vm_weak_oop_storage != NULL, "Uninitialized");


2143 // If so, returns the basic type it holds.  If not, returns T_OBJECT.
2144 BasicType SystemDictionary::box_klass_type(Klass* k) {
2145   assert(k != NULL, "");
2146   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
2147     if (_box_klasses[i] == k)
2148       return (BasicType)i;
2149   }
2150   return T_OBJECT;
2151 }
2152 
2153 // Constraints on class loaders. The details of the algorithm can be
2154 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
2155 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
2156 // that the dictionary needs to maintain a set of contraints that
2157 // must be satisfied by all classes in the dictionary.
2158 // if defining is true, then LinkageError if already in dictionary
2159 // if initiating loader, then ok if InstanceKlass matches existing entry
2160 
2161 void SystemDictionary::check_constraints(unsigned int d_hash,
2162                                          InstanceKlass* k,
2163                                          Handle class_loader,
2164                                          bool defining,
2165                                          TRAPS) {
2166   ResourceMark rm(THREAD);
2167   stringStream ss;
2168   bool throwException = false;
2169 
2170   const char *linkage_error1 = NULL;
2171   const char *linkage_error2 = NULL;
2172   const char *linkage_error3 = "";
2173   // Remember the loader of the similar class that is already loaded.
2174   const char *existing_klass_loader_name = "";
2175 
2176   {
2177     Symbol*  name  = k->name();
2178     ClassLoaderData *loader_data = class_loader_data(class_loader);
2179 
2180     MutexLocker mu(SystemDictionary_lock, THREAD);
2181 
2182     InstanceKlass* check = find_class(d_hash, name, loader_data->dictionary());
2183     if (check != NULL) {
2184       // If different InstanceKlass - duplicate class definition,
2185       // else - ok, class loaded by a different thread in parallel.
2186       // We should only have found it if it was done loading and ok to use.
2187       // The dictionary only holds instance classes, placeholders
2188       // also hold array classes.
2189 
2190       assert(check->is_instance_klass(), "noninstance in systemdictionary");
2191       if ((defining == true) || (k != check)) {
2192         throwException = true;
2193         ss.print("loader %s", java_lang_ClassLoader::describe_external(class_loader()));
2194         ss.print(" attempted duplicate %s definition for %s.",
2195                  k->external_kind(), k->external_name());
2196       } else {
2197         return;
2198       }
2199     }
2200 
2201 #ifdef ASSERT
2202     Symbol* ph_check = find_placeholder(name, loader_data);
2203     assert(ph_check == NULL || ph_check == name, "invalid symbol");
2204 #endif
2205 
2206     if (throwException == false) {
2207       if (constraints()->check_or_update(k, class_loader, name) == false) {
2208         throwException = true;
2209         ss.print("loader constraint violation: loader %s",
2210                  java_lang_ClassLoader::describe_external(class_loader()));
2211         ss.print(" wants to load %s %s.",
2212                  k->external_kind(), k->external_name());
2213         Klass *existing_klass = constraints()->find_constrained_klass(name, class_loader);
2214         if (existing_klass->class_loader() != class_loader()) {
2215           ss.print(" A different %s with the same name was previously loaded by %s.",
2216                    existing_klass->external_kind(),
2217                    java_lang_ClassLoader::describe_external(existing_klass->class_loader()));
2218         }
2219       }
2220     }
2221   }
2222 
2223   // Throw error now if needed (cannot throw while holding
2224   // SystemDictionary_lock because of rank ordering)
2225   if (throwException == true) {
2226     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());








2227   }
2228 }
2229 

2230 // Update class loader data dictionary - done after check_constraint and add_to_hierachy
2231 // have been called.
2232 void SystemDictionary::update_dictionary(unsigned int d_hash,
2233                                          int p_index, unsigned int p_hash,
2234                                          InstanceKlass* k,
2235                                          Handle class_loader,
2236                                          TRAPS) {
2237   // Compile_lock prevents systemDictionary updates during compilations
2238   assert_locked_or_safepoint(Compile_lock);
2239   Symbol*  name  = k->name();
2240   ClassLoaderData *loader_data = class_loader_data(class_loader);
2241 
2242   {
2243     MutexLocker mu1(SystemDictionary_lock, THREAD);
2244 
2245     // See whether biased locking is enabled and if so set it for this
2246     // klass.
2247     // Note that this must be done past the last potential blocking
2248     // point / safepoint. We enable biased locking lazily using a
2249     // VM_Operation to iterate the SystemDictionary and installing the


3105 // Combining platform and system loader dictionaries into boot loader dictionary.
3106 // During run time, we only have one shared dictionary.
3107 void SystemDictionary::combine_shared_dictionaries() {
3108   assert(DumpSharedSpaces, "dump time only");
3109   // If AppCDS isn't enabled, we only dump the classes in the boot loader dictionary
3110   // into the shared archive.
3111   if (UseAppCDS) {
3112     Dictionary* master_dictionary = ClassLoaderData::the_null_class_loader_data()->dictionary();
3113     CombineDictionariesClosure cdc(master_dictionary);
3114     ClassLoaderDataGraph::cld_do(&cdc);
3115   }
3116 
3117   // These tables are no longer valid or necessary. Keeping them around will
3118   // cause SystemDictionary::verify() to fail. Let's empty them.
3119   _placeholders        = new PlaceholderTable(_placeholder_table_size);
3120   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
3121 
3122   NOT_PRODUCT(SystemDictionary::verify());
3123 }
3124 
3125 // Caller needs ResourceMark.
3126 const char* SystemDictionary::loader_name(const oop loader) {
3127   return ((loader) == NULL ? "<bootloader>" :
3128           InstanceKlass::cast((loader)->klass())->name()->as_C_string());
3129 }
3130 
3131 // caller needs ResourceMark
3132 const char* SystemDictionary::loader_name(const ClassLoaderData* loader_data) {
3133   return (loader_data->class_loader() == NULL ? "<bootloader>" :
3134           SystemDictionary::loader_name(loader_data->class_loader()));
3135 }
3136 
3137 void SystemDictionary::initialize_oop_storage() {
3138   _vm_weak_oop_storage =
3139     new OopStorage("VM Weak Oop Handles",
3140                    VMWeakAlloc_lock,
3141                    VMWeakActive_lock);
3142 }
3143 
3144 OopStorage* SystemDictionary::vm_weak_oop_storage() {
3145   assert(_vm_weak_oop_storage != NULL, "Uninitialized");
< prev index next >