< prev index next >

src/hotspot/share/classfile/classLoader.cpp

Print this page
rev 58452 : imported patch pkg_name_from_class


 159 }
 160 
 161 static const char* get_jimage_version_string() {
 162   static char version_string[10] = "";
 163   if (version_string[0] == '\0') {
 164     jio_snprintf(version_string, sizeof(version_string), "%d.%d",
 165                  VM_Version::vm_major_version(), VM_Version::vm_minor_version());
 166   }
 167   return (const char*)version_string;
 168 }
 169 
 170 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
 171   size_t str_len = strlen(str);
 172   size_t str_to_find_len = strlen(str_to_find);
 173   if (str_to_find_len > str_len) {
 174     return false;
 175   }
 176   return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
 177 }
 178 
 179 // Used to obtain the package name from a fully qualified class name.
 180 // It is the responsibility of the caller to establish a ResourceMark.
 181 const char* ClassLoader::package_from_name(const char* const class_name, bool* bad_class_name) {
 182   if (class_name == NULL) {
 183     if (bad_class_name != NULL) {
 184       *bad_class_name = true;
 185     }
 186     return NULL;
 187   }
 188 
 189   if (bad_class_name != NULL) {
 190     *bad_class_name = false;
 191   }
 192 
 193   const char* const last_slash = strrchr(class_name, JVM_SIGNATURE_SLASH);
 194   if (last_slash == NULL) {
 195     // No package name
 196     return NULL;
 197   }
 198 
 199   char* class_name_ptr = (char*) class_name;
 200   // Skip over '['s
 201   if (*class_name_ptr == JVM_SIGNATURE_ARRAY) {
 202     do {
 203       class_name_ptr++;
 204     } while (*class_name_ptr == JVM_SIGNATURE_ARRAY);
 205 
 206     // Fully qualified class names should not contain a 'L'.
 207     // Set bad_class_name to true to indicate that the package name
 208     // could not be obtained due to an error condition.
 209     // In this situation, is_same_class_package returns false.
 210     if (*class_name_ptr == JVM_SIGNATURE_CLASS) {
 211       if (bad_class_name != NULL) {
 212         *bad_class_name = true;
 213       }
 214       return NULL;
 215     }
 216   }
 217 
 218   int length = last_slash - class_name_ptr;
 219 
 220   // A class name could have just the slash character in the name.
 221   if (length <= 0) {
 222     // No package name
 223     if (bad_class_name != NULL) {
 224       *bad_class_name = true;
 225     }
 226     return NULL;
 227   }
 228 
 229   // drop name after last slash (including slash)
 230   // Ex., "java/lang/String.class" => "java/lang"
 231   char* pkg_name = NEW_RESOURCE_ARRAY(char, length + 1);
 232   strncpy(pkg_name, class_name_ptr, length);
 233   *(pkg_name+length) = '\0';
 234 
 235   return (const char *)pkg_name;
 236 }
 237 
 238 // Given a fully qualified class name, find its defining package in the class loader's
 239 // package entry table.
 240 PackageEntry* ClassLoader::get_package_entry(const char* class_name, ClassLoaderData* loader_data, TRAPS) {
 241   ResourceMark rm(THREAD);
 242   const char *pkg_name = ClassLoader::package_from_name(class_name);
 243   if (pkg_name == NULL) {
 244     return NULL;
 245   }
 246   PackageEntryTable* pkgEntryTable = loader_data->packages();
 247   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(pkg_name);
 248   return pkgEntryTable->lookup_only(pkg_symbol);
 249 }
 250 
 251 const char* ClassPathEntry::copy_path(const char* path) {
 252   char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
 253   strcpy(copy, path);
 254   return copy;
 255 }
 256 
 257 ClassFileStream* ClassPathDirEntry::open_stream(const char* name, TRAPS) {
 258   // construct full path name
 259   assert((_dir != NULL) && (name != NULL), "sanity");
 260   size_t path_len = strlen(_dir) + strlen(name) + strlen(os::file_separator()) + 1;
 261   char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, path_len);
 262   int len = jio_snprintf(path, path_len, "%s%s%s", _dir, os::file_separator(), name);
 263   assert(len == (int)(path_len - 1), "sanity");
 264   // check if file exists
 265   struct stat st;
 266   if (os::stat(path, &st) == 0) {
 267     // found file, open it
 268     int file_handle = os::open(path, 0, 0);


 390     _jimage = NULL;
 391   }
 392 }
 393 
 394 ClassFileStream* ClassPathImageEntry::open_stream(const char* name, TRAPS) {
 395   return open_stream_for_loader(name, ClassLoaderData::the_null_class_loader_data(), THREAD);
 396 }
 397 
 398 // For a class in a named module, look it up in the jimage file using this syntax:
 399 //    /<module-name>/<package-name>/<base-class>
 400 //
 401 // Assumptions:
 402 //     1. There are no unnamed modules in the jimage file.
 403 //     2. A package is in at most one module in the jimage file.
 404 //
 405 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(const char* name, ClassLoaderData* loader_data, TRAPS) {
 406   jlong size;
 407   JImageLocationRef location = (*JImageFindResource)(_jimage, "", get_jimage_version_string(), name, &size);
 408 
 409   if (location == 0) {
 410     ResourceMark rm;
 411     const char* pkg_name = ClassLoader::package_from_name(name);
 412 
 413     if (pkg_name != NULL) {
 414       if (!Universe::is_module_initialized()) {
 415         location = (*JImageFindResource)(_jimage, JAVA_BASE_NAME, get_jimage_version_string(), name, &size);
 416       } else {
 417         PackageEntry* package_entry = ClassLoader::get_package_entry(name, loader_data, CHECK_NULL);
 418         if (package_entry != NULL) {
 419           ResourceMark rm;
 420           // Get the module name
 421           ModuleEntry* module = package_entry->module();
 422           assert(module != NULL, "Boot classLoader package missing module");
 423           assert(module->is_named(), "Boot classLoader package is in unnamed module");
 424           const char* module_name = module->name()->as_C_string();
 425           if (module_name != NULL) {
 426             location = (*JImageFindResource)(_jimage, module_name, get_jimage_version_string(), name, &size);
 427           }
 428         }
 429       }
 430     }
 431   }
 432   if (location != 0) {
 433     if (UsePerfData) {
 434       ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
 435     }
 436     char* data = NEW_RESOURCE_ARRAY(char, size);
 437     (*JImageGetResource)(_jimage, location, data, size);


1012   void* handle = NULL;
1013   if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) {
1014     handle = os::dll_load(path, ebuf, sizeof ebuf);
1015   }
1016   if (handle == NULL) {
1017     vm_exit_during_initialization("Unable to load jimage library", path);
1018   }
1019 
1020   JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, dll_lookup(handle, "JIMAGE_Open", path));
1021   JImageClose = CAST_TO_FN_PTR(JImageClose_t, dll_lookup(handle, "JIMAGE_Close", path));
1022   JImagePackageToModule = CAST_TO_FN_PTR(JImagePackageToModule_t, dll_lookup(handle, "JIMAGE_PackageToModule", path));
1023   JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, dll_lookup(handle, "JIMAGE_FindResource", path));
1024   JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));
1025   JImageResourceIterator = CAST_TO_FN_PTR(JImageResourceIterator_t, dll_lookup(handle, "JIMAGE_ResourceIterator", path));
1026 }
1027 
1028 int ClassLoader::crc32(int crc, const char* buf, int len) {
1029   return (*Crc32)(crc, (const jbyte*)buf, len);
1030 }
1031 
1032 // Function add_package extracts the package from the fully qualified class name
1033 // and checks if the package is in the boot loader's package entry table.  If so,
1034 // then it sets the classpath_index in the package entry record.
1035 //
1036 // The classpath_index field is used to find the entry on the boot loader class
1037 // path for packages with classes loaded by the boot loader from -Xbootclasspath/a
1038 // in an unnamed module.  It is also used to indicate (for all packages whose
1039 // classes are loaded by the boot loader) that at least one of the package's
1040 // classes has been loaded.
1041 bool ClassLoader::add_package(const char *fullq_class_name, s2 classpath_index, TRAPS) {
1042   assert(fullq_class_name != NULL, "just checking");
1043 
1044   // Get package name from fully qualified class name.
1045   ResourceMark rm(THREAD);
1046   const char *cp = package_from_name(fullq_class_name);
1047   if (cp != NULL) {
1048     PackageEntryTable* pkg_entry_tbl = ClassLoaderData::the_null_class_loader_data()->packages();
1049     TempNewSymbol pkg_symbol = SymbolTable::new_symbol(cp);
1050     PackageEntry* pkg_entry = pkg_entry_tbl->lookup_only(pkg_symbol);
1051     if (pkg_entry != NULL) {
1052       assert(classpath_index != -1, "Unexpected classpath_index");
1053       pkg_entry->set_classpath_index(classpath_index);
1054     } else {
1055       return false;
1056     }
1057   }
1058   return true;
1059 }
1060 
1061 oop ClassLoader::get_system_package(const char* name, TRAPS) {
1062   // Look up the name in the boot loader's package entry table.
1063   if (name != NULL) {
1064     TempNewSymbol package_sym = SymbolTable::new_symbol(name);
1065     // Look for the package entry in the boot loader's package entry table.
1066     PackageEntry* package =
1067       ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym);
1068 
1069     // Return NULL if package does not exist or if no classes in that package
1070     // have been loaded.


1149     ModuleClassPathList* module_cpl = module_list->at(i);
1150     Symbol* module_cpl_name = module_cpl->module_name();
1151 
1152     if (module_cpl_name->fast_compare(class_module_name) == 0) {
1153       // Class' module has been located.
1154       return module_cpl->module_first_entry();
1155     }
1156   }
1157   return NULL;
1158 }
1159 
1160 
1161 // Search either the patch-module or exploded build entries for class.
1162 ClassFileStream* ClassLoader::search_module_entries(const GrowableArray<ModuleClassPathList*>* const module_list,
1163                                                     const char* const class_name,
1164                                                     const char* const file_name,
1165                                                     TRAPS) {
1166   ClassFileStream* stream = NULL;
1167 
1168   // Find the class' defining module in the boot loader's module entry table
1169   PackageEntry* pkg_entry = get_package_entry(class_name, ClassLoaderData::the_null_class_loader_data(), CHECK_NULL);


1170   ModuleEntry* mod_entry = (pkg_entry != NULL) ? pkg_entry->module() : NULL;
1171 
1172   // If the module system has not defined java.base yet, then
1173   // classes loaded are assumed to be defined to java.base.
1174   // When java.base is eventually defined by the module system,
1175   // all packages of classes that have been previously loaded
1176   // are verified in ModuleEntryTable::verify_javabase_packages().
1177   if (!Universe::is_module_initialized() &&
1178       !ModuleEntryTable::javabase_defined() &&
1179       mod_entry == NULL) {
1180     mod_entry = ModuleEntryTable::javabase_moduleEntry();
1181   }
1182 
1183   // The module must be a named module
1184   ClassPathEntry* e = NULL;
1185   if (mod_entry != NULL && mod_entry->is_named()) {
1186     if (module_list == _exploded_entries) {
1187       // The exploded build entries can be added to at any time so a lock is
1188       // needed when searching them.
1189       assert(!ClassLoader::has_jrt_entry(), "Must be exploded build");


1300 
1301   stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1302 
1303   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1304   Handle protection_domain;
1305 
1306   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1307                                                            name,
1308                                                            loader_data,
1309                                                            protection_domain,
1310                                                            NULL, // unsafe_anonymous_host
1311                                                            NULL, // cp_patches
1312                                                            THREAD);
1313   if (HAS_PENDING_EXCEPTION) {
1314     if (DumpSharedSpaces) {
1315       log_error(cds)("Preload Error: Failed to load %s", class_name);
1316     }
1317     return NULL;
1318   }
1319 
1320   if (!add_package(file_name, classpath_index, THREAD)) {
1321     return NULL;
1322   }
1323 
1324   return result;
1325 }
1326 
1327 #if INCLUDE_CDS
1328 char* ClassLoader::skip_uri_protocol(char* source) {
1329   if (strncmp(source, "file:", 5) == 0) {
1330     // file: protocol path could start with file:/ or file:///
1331     // locate the char after all the forward slashes
1332     int offset = 5;
1333     while (*(source + offset) == '/') {
1334         offset++;
1335     }
1336     source += offset;
1337   // for non-windows platforms, move back one char as the path begins with a '/'
1338 #ifndef _WINDOWS
1339     source -= 1;
1340 #endif




 159 }
 160 
 161 static const char* get_jimage_version_string() {
 162   static char version_string[10] = "";
 163   if (version_string[0] == '\0') {
 164     jio_snprintf(version_string, sizeof(version_string), "%d.%d",
 165                  VM_Version::vm_major_version(), VM_Version::vm_minor_version());
 166   }
 167   return (const char*)version_string;
 168 }
 169 
 170 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
 171   size_t str_len = strlen(str);
 172   size_t str_to_find_len = strlen(str_to_find);
 173   if (str_to_find_len > str_len) {
 174     return false;
 175   }
 176   return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
 177 }
 178 



























































 179 // Given a fully qualified class name, find its defining package in the class loader's
 180 // package entry table.
 181 PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data, TRAPS) {


 182   if (pkg_name == NULL) {
 183     return NULL;
 184   }
 185   PackageEntryTable* pkgEntryTable = loader_data->packages();
 186   return pkgEntryTable->lookup_only(pkg_name);

 187 }
 188 
 189 const char* ClassPathEntry::copy_path(const char* path) {
 190   char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
 191   strcpy(copy, path);
 192   return copy;
 193 }
 194 
 195 ClassFileStream* ClassPathDirEntry::open_stream(const char* name, TRAPS) {
 196   // construct full path name
 197   assert((_dir != NULL) && (name != NULL), "sanity");
 198   size_t path_len = strlen(_dir) + strlen(name) + strlen(os::file_separator()) + 1;
 199   char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, path_len);
 200   int len = jio_snprintf(path, path_len, "%s%s%s", _dir, os::file_separator(), name);
 201   assert(len == (int)(path_len - 1), "sanity");
 202   // check if file exists
 203   struct stat st;
 204   if (os::stat(path, &st) == 0) {
 205     // found file, open it
 206     int file_handle = os::open(path, 0, 0);


 328     _jimage = NULL;
 329   }
 330 }
 331 
 332 ClassFileStream* ClassPathImageEntry::open_stream(const char* name, TRAPS) {
 333   return open_stream_for_loader(name, ClassLoaderData::the_null_class_loader_data(), THREAD);
 334 }
 335 
 336 // For a class in a named module, look it up in the jimage file using this syntax:
 337 //    /<module-name>/<package-name>/<base-class>
 338 //
 339 // Assumptions:
 340 //     1. There are no unnamed modules in the jimage file.
 341 //     2. A package is in at most one module in the jimage file.
 342 //
 343 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(const char* name, ClassLoaderData* loader_data, TRAPS) {
 344   jlong size;
 345   JImageLocationRef location = (*JImageFindResource)(_jimage, "", get_jimage_version_string(), name, &size);
 346 
 347   if (location == 0) {
 348     TempNewSymbol class_name = SymbolTable::new_symbol(name);
 349     TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name);
 350 
 351     if (pkg_name != NULL) {
 352       if (!Universe::is_module_initialized()) {
 353         location = (*JImageFindResource)(_jimage, JAVA_BASE_NAME, get_jimage_version_string(), name, &size);
 354       } else {
 355         PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data, CHECK_NULL);
 356         if (package_entry != NULL) {
 357           ResourceMark rm;
 358           // Get the module name
 359           ModuleEntry* module = package_entry->module();
 360           assert(module != NULL, "Boot classLoader package missing module");
 361           assert(module->is_named(), "Boot classLoader package is in unnamed module");
 362           const char* module_name = module->name()->as_C_string();
 363           if (module_name != NULL) {
 364             location = (*JImageFindResource)(_jimage, module_name, get_jimage_version_string(), name, &size);
 365           }
 366         }
 367       }
 368     }
 369   }
 370   if (location != 0) {
 371     if (UsePerfData) {
 372       ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
 373     }
 374     char* data = NEW_RESOURCE_ARRAY(char, size);
 375     (*JImageGetResource)(_jimage, location, data, size);


 950   void* handle = NULL;
 951   if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) {
 952     handle = os::dll_load(path, ebuf, sizeof ebuf);
 953   }
 954   if (handle == NULL) {
 955     vm_exit_during_initialization("Unable to load jimage library", path);
 956   }
 957 
 958   JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, dll_lookup(handle, "JIMAGE_Open", path));
 959   JImageClose = CAST_TO_FN_PTR(JImageClose_t, dll_lookup(handle, "JIMAGE_Close", path));
 960   JImagePackageToModule = CAST_TO_FN_PTR(JImagePackageToModule_t, dll_lookup(handle, "JIMAGE_PackageToModule", path));
 961   JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, dll_lookup(handle, "JIMAGE_FindResource", path));
 962   JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));
 963   JImageResourceIterator = CAST_TO_FN_PTR(JImageResourceIterator_t, dll_lookup(handle, "JIMAGE_ResourceIterator", path));
 964 }
 965 
 966 int ClassLoader::crc32(int crc, const char* buf, int len) {
 967   return (*Crc32)(crc, (const jbyte*)buf, len);
 968 }
 969 
 970 // Function add_package checks if the package of the InstanceKlass is in the
 971 // boot loader's package entry table.  If so, then it sets the classpath_index
 972 // in the package entry record.
 973 //
 974 // The classpath_index field is used to find the entry on the boot loader class
 975 // path for packages with classes loaded by the boot loader from -Xbootclasspath/a
 976 // in an unnamed module.  It is also used to indicate (for all packages whose
 977 // classes are loaded by the boot loader) that at least one of the package's
 978 // classes has been loaded.
 979 bool ClassLoader::add_package(const InstanceKlass* ik, s2 classpath_index, TRAPS) {
 980   assert(ik != NULL, "just checking");
 981 
 982   // Get package name from fully qualified class name.
 983   PackageEntry* ik_pkg = ik->package();
 984   if (ik_pkg != NULL) {

 985     PackageEntryTable* pkg_entry_tbl = ClassLoaderData::the_null_class_loader_data()->packages();
 986     PackageEntry* pkg_entry = pkg_entry_tbl->lookup_only(ik_pkg->name());

 987     if (pkg_entry != NULL) {
 988       assert(classpath_index != -1, "Unexpected classpath_index");
 989       pkg_entry->set_classpath_index(classpath_index);
 990     } else {
 991       return false;
 992     }
 993   }
 994   return true;
 995 }
 996 
 997 oop ClassLoader::get_system_package(const char* name, TRAPS) {
 998   // Look up the name in the boot loader's package entry table.
 999   if (name != NULL) {
1000     TempNewSymbol package_sym = SymbolTable::new_symbol(name);
1001     // Look for the package entry in the boot loader's package entry table.
1002     PackageEntry* package =
1003       ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym);
1004 
1005     // Return NULL if package does not exist or if no classes in that package
1006     // have been loaded.


1085     ModuleClassPathList* module_cpl = module_list->at(i);
1086     Symbol* module_cpl_name = module_cpl->module_name();
1087 
1088     if (module_cpl_name->fast_compare(class_module_name) == 0) {
1089       // Class' module has been located.
1090       return module_cpl->module_first_entry();
1091     }
1092   }
1093   return NULL;
1094 }
1095 
1096 
1097 // Search either the patch-module or exploded build entries for class.
1098 ClassFileStream* ClassLoader::search_module_entries(const GrowableArray<ModuleClassPathList*>* const module_list,
1099                                                     const char* const class_name,
1100                                                     const char* const file_name,
1101                                                     TRAPS) {
1102   ClassFileStream* stream = NULL;
1103 
1104   // Find the class' defining module in the boot loader's module entry table
1105   TempNewSymbol class_name_symbol = SymbolTable::new_symbol(class_name);
1106   TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name_symbol);
1107   PackageEntry* pkg_entry = get_package_entry(pkg_name, ClassLoaderData::the_null_class_loader_data(), CHECK_NULL);
1108   ModuleEntry* mod_entry = (pkg_entry != NULL) ? pkg_entry->module() : NULL;
1109 
1110   // If the module system has not defined java.base yet, then
1111   // classes loaded are assumed to be defined to java.base.
1112   // When java.base is eventually defined by the module system,
1113   // all packages of classes that have been previously loaded
1114   // are verified in ModuleEntryTable::verify_javabase_packages().
1115   if (!Universe::is_module_initialized() &&
1116       !ModuleEntryTable::javabase_defined() &&
1117       mod_entry == NULL) {
1118     mod_entry = ModuleEntryTable::javabase_moduleEntry();
1119   }
1120 
1121   // The module must be a named module
1122   ClassPathEntry* e = NULL;
1123   if (mod_entry != NULL && mod_entry->is_named()) {
1124     if (module_list == _exploded_entries) {
1125       // The exploded build entries can be added to at any time so a lock is
1126       // needed when searching them.
1127       assert(!ClassLoader::has_jrt_entry(), "Must be exploded build");


1238 
1239   stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1240 
1241   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1242   Handle protection_domain;
1243 
1244   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1245                                                            name,
1246                                                            loader_data,
1247                                                            protection_domain,
1248                                                            NULL, // unsafe_anonymous_host
1249                                                            NULL, // cp_patches
1250                                                            THREAD);
1251   if (HAS_PENDING_EXCEPTION) {
1252     if (DumpSharedSpaces) {
1253       log_error(cds)("Preload Error: Failed to load %s", class_name);
1254     }
1255     return NULL;
1256   }
1257 
1258   if (!add_package(result, classpath_index, THREAD)) {
1259     return NULL;
1260   }
1261 
1262   return result;
1263 }
1264 
1265 #if INCLUDE_CDS
1266 char* ClassLoader::skip_uri_protocol(char* source) {
1267   if (strncmp(source, "file:", 5) == 0) {
1268     // file: protocol path could start with file:/ or file:///
1269     // locate the char after all the forward slashes
1270     int offset = 5;
1271     while (*(source + offset) == '/') {
1272         offset++;
1273     }
1274     source += offset;
1275   // for non-windows platforms, move back one char as the path begins with a '/'
1276 #ifndef _WINDOWS
1277     source -= 1;
1278 #endif


< prev index next >