src/share/vm/classfile/systemDictionary.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/classfile

src/share/vm/classfile/systemDictionary.cpp

Print this page




 152   if (AlwaysLockClassLoader) return false;
 153   return java_lang_ClassLoader::parallelCapable(class_loader());
 154 }
 155 // ----------------------------------------------------------------------------
 156 // ParallelDefineClass flag does not apply to bootclass loader
 157 bool SystemDictionary::is_parallelDefine(Handle class_loader) {
 158    if (class_loader.is_null()) return false;
 159    if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) {
 160      return true;
 161    }
 162    return false;
 163 }
 164 
 165 /**
 166  * Returns true if the passed class loader is the extension class loader.
 167  */
 168 bool SystemDictionary::is_ext_class_loader(Handle class_loader) {
 169   if (class_loader.is_null()) {
 170     return false;
 171   }
 172   return (class_loader->klass()->name() == vmSymbols::sun_misc_Launcher_ExtClassLoader());
 173 }
 174 
 175 // ----------------------------------------------------------------------------
 176 // Resolving of classes
 177 
 178 // Forwards to resolve_or_null
 179 
 180 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) {
 181   Klass* klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
 182   if (HAS_PENDING_EXCEPTION || klass == NULL) {
 183     KlassHandle k_h(THREAD, klass);
 184     // can return a null klass
 185     klass = handle_resolution_exception(class_name, throw_error, k_h, THREAD);
 186   }
 187   return klass;
 188 }
 189 
 190 Klass* SystemDictionary::handle_resolution_exception(Symbol* class_name,
 191                                                      bool throw_error,
 192                                                      KlassHandle klass_h, TRAPS) {


 339   unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data);
 340   int d_index = dictionary()->hash_to_index(d_hash);
 341   unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data);
 342   int p_index = placeholders()->hash_to_index(p_hash);
 343   // can't throw error holding a lock
 344   bool child_already_loaded = false;
 345   bool throw_circularity_error = false;
 346   {
 347     MutexLocker mu(SystemDictionary_lock, THREAD);
 348     Klass* childk = find_class(d_index, d_hash, child_name, loader_data);
 349     Klass* quicksuperk;
 350     // to support // loading: if child done loading, just return superclass
 351     // if class_name, & class_loader don't match:
 352     // if initial define, SD update will give LinkageError
 353     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
 354     // so we don't throw an exception here.
 355     // see: nsk redefclass014 & java.lang.instrument Instrument032
 356     if ((childk != NULL ) && (is_superclass) &&
 357        ((quicksuperk = InstanceKlass::cast(childk)->super()) != NULL) &&
 358 
 359          ((quicksuperk->name() == class_name) &&
 360             (quicksuperk->class_loader()  == class_loader()))) {
 361            return quicksuperk;
 362     } else {
 363       PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data);
 364       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
 365           throw_circularity_error = true;
 366       }
 367     }
 368     if (!throw_circularity_error) {
 369       PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, loader_data, PlaceholderTable::LOAD_SUPER, class_name, THREAD);
 370     }
 371   }
 372   if (throw_circularity_error) {
 373       ResourceMark rm(THREAD);
 374       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string());
 375   }
 376 
 377 // java.lang.Object should have been found above
 378   assert(class_name != NULL, "null super class for resolving");
 379   // Resolve the super class or interface, check results on return


1089       parsed_name != NULL &&
1090       !strncmp((const char*)parsed_name->bytes(), pkg, strlen(pkg))) {
1091     // It is illegal to define classes in the "java." package from
1092     // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1093     ResourceMark rm(THREAD);
1094     char* name = parsed_name->as_C_string();
1095     char* index = strrchr(name, '/');
1096     *index = '\0'; // chop to just the package name
1097     while ((index = strchr(name, '/')) != NULL) {
1098       *index = '.'; // replace '/' with '.' in package name
1099     }
1100     const char* fmt = "Prohibited package name: %s";
1101     size_t len = strlen(fmt) + strlen(name);
1102     char* message = NEW_RESOURCE_ARRAY(char, len);
1103     jio_snprintf(message, len, fmt, name);
1104     Exceptions::_throw_msg(THREAD_AND_LOCATION,
1105       vmSymbols::java_lang_SecurityException(), message);
1106   }
1107 
1108   if (!HAS_PENDING_EXCEPTION) {
1109     assert(parsed_name != NULL, "Sanity");
1110     assert(class_name == NULL || class_name == parsed_name, "name mismatch");
1111     // Verification prevents us from creating names with dots in them, this
1112     // asserts that that's the case.
1113     assert(is_internal_format(parsed_name),
1114            "external class name format used internally");
1115 
1116     // Add class just loaded
1117     // If a class loader supports parallel classloading handle parallel define requests
1118     // find_or_define_instance_class may return a different InstanceKlass
1119     if (is_parallelCapable(class_loader)) {
1120       k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
1121     } else {
1122       define_instance_class(k, THREAD);
1123     }
1124   }
1125 
1126   // Make sure we have an entry in the SystemDictionary on success
1127   debug_only( {
1128     if (!HAS_PENDING_EXCEPTION) {
1129       assert(parsed_name != NULL, "parsed_name is still null?");
1130       Symbol*  h_name    = k->name();


1337       JavaCalls::call_virtual(&result,
1338                               class_loader,
1339                               spec_klass,
1340                               vmSymbols::loadClass_name(),
1341                               vmSymbols::string_class_signature(),
1342                               string,
1343                               CHECK_(nh));
1344     }
1345 
1346     assert(result.get_type() == T_OBJECT, "just checking");
1347     oop obj = (oop) result.get_jobject();
1348 
1349     // Primitive classes return null since forName() can not be
1350     // used to obtain any of the Class objects representing primitives or void
1351     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1352       instanceKlassHandle k =
1353                 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1354       // For user defined Java class loaders, check that the name returned is
1355       // the same as that requested.  This check is done for the bootstrap
1356       // loader when parsing the class file.
1357       if (class_name == k->name()) {
1358         return k;
1359       }
1360     }
1361     // Class is not found or has the wrong name, return NULL
1362     return nh;
1363   }
1364 }
1365 
1366 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1367 
1368   ClassLoaderData* loader_data = k->class_loader_data();
1369   Handle class_loader_h(THREAD, loader_data->class_loader());
1370 
1371  // for bootstrap and other parallel classloaders don't acquire lock,
1372  // use placeholder token
1373  // If a parallelCapable class loader calls define_instance_class instead of
1374  // find_or_define_instance_class to get here, we have a timing
1375  // hole with systemDictionary updates and check_constraints
1376  if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1377     assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,


1972 
1973     Klass* check = find_class(d_index, d_hash, name, loader_data);
1974     if (check != (Klass*)NULL) {
1975       // if different InstanceKlass - duplicate class definition,
1976       // else - ok, class loaded by a different thread in parallel,
1977       // we should only have found it if it was done loading and ok to use
1978       // system dictionary only holds instance classes, placeholders
1979       // also holds array classes
1980 
1981       assert(check->oop_is_instance(), "noninstance in systemdictionary");
1982       if ((defining == true) || (k() != check)) {
1983         linkage_error = "loader (instance of  %s): attempted  duplicate class "
1984           "definition for name: \"%s\"";
1985       } else {
1986         return;
1987       }
1988     }
1989 
1990 #ifdef ASSERT
1991     Symbol* ph_check = find_placeholder(name, loader_data);
1992     assert(ph_check == NULL || ph_check == name, "invalid symbol");
1993 #endif
1994 
1995     if (linkage_error == NULL) {
1996       if (constraints()->check_or_update(k, class_loader, name) == false) {
1997         linkage_error = "loader constraint violation: loader (instance of %s)"
1998           " previously initiated loading for a different type with name \"%s\"";
1999       }
2000     }
2001   }
2002 
2003   // Throw error now if needed (cannot throw while holding
2004   // SystemDictionary_lock because of rank ordering)
2005 
2006   if (linkage_error) {
2007     ResourceMark rm(THREAD);
2008     const char* class_loader_name = loader_name(class_loader());
2009     char* type_name = k->name()->as_C_string();
2010     size_t buflen = strlen(linkage_error) + strlen(class_loader_name) +
2011       strlen(type_name);
2012     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);




 152   if (AlwaysLockClassLoader) return false;
 153   return java_lang_ClassLoader::parallelCapable(class_loader());
 154 }
 155 // ----------------------------------------------------------------------------
 156 // ParallelDefineClass flag does not apply to bootclass loader
 157 bool SystemDictionary::is_parallelDefine(Handle class_loader) {
 158    if (class_loader.is_null()) return false;
 159    if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) {
 160      return true;
 161    }
 162    return false;
 163 }
 164 
 165 /**
 166  * Returns true if the passed class loader is the extension class loader.
 167  */
 168 bool SystemDictionary::is_ext_class_loader(Handle class_loader) {
 169   if (class_loader.is_null()) {
 170     return false;
 171   }
 172   return (class_loader->klass()->name()->equals(vmSymbols::sun_misc_Launcher_ExtClassLoader()));
 173 }
 174 
 175 // ----------------------------------------------------------------------------
 176 // Resolving of classes
 177 
 178 // Forwards to resolve_or_null
 179 
 180 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) {
 181   Klass* klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
 182   if (HAS_PENDING_EXCEPTION || klass == NULL) {
 183     KlassHandle k_h(THREAD, klass);
 184     // can return a null klass
 185     klass = handle_resolution_exception(class_name, throw_error, k_h, THREAD);
 186   }
 187   return klass;
 188 }
 189 
 190 Klass* SystemDictionary::handle_resolution_exception(Symbol* class_name,
 191                                                      bool throw_error,
 192                                                      KlassHandle klass_h, TRAPS) {


 339   unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data);
 340   int d_index = dictionary()->hash_to_index(d_hash);
 341   unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data);
 342   int p_index = placeholders()->hash_to_index(p_hash);
 343   // can't throw error holding a lock
 344   bool child_already_loaded = false;
 345   bool throw_circularity_error = false;
 346   {
 347     MutexLocker mu(SystemDictionary_lock, THREAD);
 348     Klass* childk = find_class(d_index, d_hash, child_name, loader_data);
 349     Klass* quicksuperk;
 350     // to support // loading: if child done loading, just return superclass
 351     // if class_name, & class_loader don't match:
 352     // if initial define, SD update will give LinkageError
 353     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
 354     // so we don't throw an exception here.
 355     // see: nsk redefclass014 & java.lang.instrument Instrument032
 356     if ((childk != NULL ) && (is_superclass) &&
 357        ((quicksuperk = InstanceKlass::cast(childk)->super()) != NULL) &&
 358 
 359          ((quicksuperk->name()->equals(class_name)) &&
 360             (quicksuperk->class_loader()  == class_loader()))) {
 361            return quicksuperk;
 362     } else {
 363       PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data);
 364       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
 365           throw_circularity_error = true;
 366       }
 367     }
 368     if (!throw_circularity_error) {
 369       PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, loader_data, PlaceholderTable::LOAD_SUPER, class_name, THREAD);
 370     }
 371   }
 372   if (throw_circularity_error) {
 373       ResourceMark rm(THREAD);
 374       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string());
 375   }
 376 
 377 // java.lang.Object should have been found above
 378   assert(class_name != NULL, "null super class for resolving");
 379   // Resolve the super class or interface, check results on return


1089       parsed_name != NULL &&
1090       !strncmp((const char*)parsed_name->bytes(), pkg, strlen(pkg))) {
1091     // It is illegal to define classes in the "java." package from
1092     // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1093     ResourceMark rm(THREAD);
1094     char* name = parsed_name->as_C_string();
1095     char* index = strrchr(name, '/');
1096     *index = '\0'; // chop to just the package name
1097     while ((index = strchr(name, '/')) != NULL) {
1098       *index = '.'; // replace '/' with '.' in package name
1099     }
1100     const char* fmt = "Prohibited package name: %s";
1101     size_t len = strlen(fmt) + strlen(name);
1102     char* message = NEW_RESOURCE_ARRAY(char, len);
1103     jio_snprintf(message, len, fmt, name);
1104     Exceptions::_throw_msg(THREAD_AND_LOCATION,
1105       vmSymbols::java_lang_SecurityException(), message);
1106   }
1107 
1108   if (!HAS_PENDING_EXCEPTION) {
1109     assert(parsed_name->not_equals(NULL), "Sanity");
1110     assert(class_name == NULL || class_name->equals(parsed_name), "name mismatch");
1111     // Verification prevents us from creating names with dots in them, this
1112     // asserts that that's the case.
1113     assert(is_internal_format(parsed_name),
1114            "external class name format used internally");
1115 
1116     // Add class just loaded
1117     // If a class loader supports parallel classloading handle parallel define requests
1118     // find_or_define_instance_class may return a different InstanceKlass
1119     if (is_parallelCapable(class_loader)) {
1120       k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
1121     } else {
1122       define_instance_class(k, THREAD);
1123     }
1124   }
1125 
1126   // Make sure we have an entry in the SystemDictionary on success
1127   debug_only( {
1128     if (!HAS_PENDING_EXCEPTION) {
1129       assert(parsed_name != NULL, "parsed_name is still null?");
1130       Symbol*  h_name = k->name();


1337       JavaCalls::call_virtual(&result,
1338                               class_loader,
1339                               spec_klass,
1340                               vmSymbols::loadClass_name(),
1341                               vmSymbols::string_class_signature(),
1342                               string,
1343                               CHECK_(nh));
1344     }
1345 
1346     assert(result.get_type() == T_OBJECT, "just checking");
1347     oop obj = (oop) result.get_jobject();
1348 
1349     // Primitive classes return null since forName() can not be
1350     // used to obtain any of the Class objects representing primitives or void
1351     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1352       instanceKlassHandle k =
1353                 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1354       // For user defined Java class loaders, check that the name returned is
1355       // the same as that requested.  This check is done for the bootstrap
1356       // loader when parsing the class file.
1357       if (class_name->equals(k->name())) {
1358         return k;
1359       }
1360     }
1361     // Class is not found or has the wrong name, return NULL
1362     return nh;
1363   }
1364 }
1365 
1366 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1367 
1368   ClassLoaderData* loader_data = k->class_loader_data();
1369   Handle class_loader_h(THREAD, loader_data->class_loader());
1370 
1371  // for bootstrap and other parallel classloaders don't acquire lock,
1372  // use placeholder token
1373  // If a parallelCapable class loader calls define_instance_class instead of
1374  // find_or_define_instance_class to get here, we have a timing
1375  // hole with systemDictionary updates and check_constraints
1376  if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1377     assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,


1972 
1973     Klass* check = find_class(d_index, d_hash, name, loader_data);
1974     if (check != (Klass*)NULL) {
1975       // if different InstanceKlass - duplicate class definition,
1976       // else - ok, class loaded by a different thread in parallel,
1977       // we should only have found it if it was done loading and ok to use
1978       // system dictionary only holds instance classes, placeholders
1979       // also holds array classes
1980 
1981       assert(check->oop_is_instance(), "noninstance in systemdictionary");
1982       if ((defining == true) || (k() != check)) {
1983         linkage_error = "loader (instance of  %s): attempted  duplicate class "
1984           "definition for name: \"%s\"";
1985       } else {
1986         return;
1987       }
1988     }
1989 
1990 #ifdef ASSERT
1991     Symbol* ph_check = find_placeholder(name, loader_data);
1992     assert(ph_check == NULL || ph_check->equals(name), "invalid symbol");
1993 #endif
1994 
1995     if (linkage_error == NULL) {
1996       if (constraints()->check_or_update(k, class_loader, name) == false) {
1997         linkage_error = "loader constraint violation: loader (instance of %s)"
1998           " previously initiated loading for a different type with name \"%s\"";
1999       }
2000     }
2001   }
2002 
2003   // Throw error now if needed (cannot throw while holding
2004   // SystemDictionary_lock because of rank ordering)
2005 
2006   if (linkage_error) {
2007     ResourceMark rm(THREAD);
2008     const char* class_loader_name = loader_name(class_loader());
2009     char* type_name = k->name()->as_C_string();
2010     size_t buflen = strlen(linkage_error) + strlen(class_loader_name) +
2011       strlen(type_name);
2012     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);


src/share/vm/classfile/systemDictionary.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File