1122
1123 // Called by the boot classloader to load classes
1124 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1125 assert(name != nullptr, "invariant");
1126
1127 ResourceMark rm(THREAD);
1128 HandleMark hm(THREAD);
1129
1130 const char* const class_name = name->as_C_string();
1131
1132 EventMarkClassLoading m("Loading class %s", class_name);
1133
1134 const char* const file_name = file_name_for_class_name(class_name,
1135 name->utf8_length());
1136 assert(file_name != nullptr, "invariant");
1137
1138 // Lookup stream for parsing .class file
1139 ClassFileStream* stream = nullptr;
1140 s2 classpath_index = 0;
1141 ClassPathEntry* e = nullptr;
1142
1143 // If search_append_only is true, boot loader visibility boundaries are
1144 // set to be _first_append_entry to the end. This includes:
1145 // [-Xbootclasspath/a]; [jvmti appended entries]
1146 //
1147 // If search_append_only is false, boot loader visibility boundaries are
1148 // set to be the --patch-module entries plus the base piece. This includes:
1149 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1150 //
1151
1152 // Load Attempt #1: --patch-module
1153 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1154 // attempt to load the class from those locations specific to the module.
1155 // Specifications to --patch-module can contain a partial number of classes
1156 // that are part of the overall module definition. So if a particular class is not
1157 // found within its module specification, the search should continue to Load Attempt #2.
1158 // Note: The --patch-module entries are never searched if the boot loader's
1159 // visibility boundary is limited to only searching the append entries.
1160 if (_patch_mod_entries != nullptr && !search_append_only) {
1161 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1162 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1163 }
1164
1165 // Load Attempt #2: [jimage | exploded build]
1166 if (!search_append_only && (nullptr == stream)) {
1167 if (has_jrt_entry()) {
1168 e = _jrt_entry;
1169 stream = _jrt_entry->open_stream(THREAD, file_name);
1170 } else {
1171 // Exploded build - attempt to locate class in its defining module's location.
1172 assert(_exploded_entries != nullptr, "No exploded build entries present");
1173 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1174 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1175 }
1176 }
1177
1178 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1179 if (search_append_only && (nullptr == stream)) {
1180 // For the boot loader append path search, the starting classpath_index
1181 // for the appended piece is always 1 to account for either the
1182 // _jrt_entry or the _exploded_entries.
1193 ++classpath_index;
1194 }
1195 }
1196
1197 if (nullptr == stream) {
1198 return nullptr;
1199 }
1200
1201 stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1202
1203 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1204 Handle protection_domain;
1205 ClassLoadInfo cl_info(protection_domain);
1206
1207 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1208 name,
1209 loader_data,
1210 cl_info,
1211 CHECK_NULL);
1212 result->set_classpath_index(classpath_index);
1213 return result;
1214 }
1215
1216 #if INCLUDE_CDS
1217 static const char* skip_uri_protocol(const char* source) {
1218 if (strncmp(source, "file:", 5) == 0) {
1219 // file: protocol path could start with file:/ or file:///
1220 // locate the char after all the forward slashes
1221 int offset = 5;
1222 while (*(source + offset) == '/') {
1223 offset++;
1224 }
1225 source += offset;
1226 // for non-windows platforms, move back one char as the path begins with a '/'
1227 #ifndef _WINDOWS
1228 source -= 1;
1229 #endif
1230 } else if (strncmp(source, "jrt:/", 5) == 0) {
1231 source += 5;
1232 }
1269 for (size_t i = 0; i < strlen(uri); ++i) {
1270 char decoded = decode_percent_encoded(uri, i);
1271 path[path_index++] = decoded;
1272 }
1273 path[path_index] = '\0';
1274 return path;
1275 }
1276
1277 // Record the shared classpath index and loader type for classes loaded
1278 // by the builtin loaders at dump time.
1279 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1280 const ClassFileStream* stream, bool redefined) {
1281 assert(CDSConfig::is_dumping_archive(), "sanity");
1282 assert(stream != nullptr, "sanity");
1283
1284 if (ik->is_hidden()) {
1285 // We do not archive hidden classes.
1286 return;
1287 }
1288
1289 oop loader = ik->class_loader();
1290 char* src = (char*)stream->source();
1291 if (src == nullptr) {
1292 if (loader == nullptr) {
1293 // JFR classes
1294 ik->set_shared_classpath_index(0);
1295 ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1296 }
1297 return;
1298 }
1299
1300 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1301
1302 ResourceMark rm(current);
1303 int classpath_index = -1;
1304 PackageEntry* pkg_entry = ik->package();
1305
1306 if (FileMapInfo::get_number_of_shared_paths() > 0) {
1307 // Save the path from the file: protocol or the module name from the jrt: protocol
1308 // if no protocol prefix is found, path is the same as stream->source(). This path
1356 // for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1357 // The class must be from the runtime modules image.
1358 if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {
1359 classpath_index = i;
1360 break;
1361 }
1362 }
1363
1364 // No path entry found for this class: most likely a shared class loaded by the
1365 // user defined classloader.
1366 if (classpath_index < 0 && !SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1367 assert(ik->shared_classpath_index() < 0, "not assigned yet");
1368 ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1369 SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1370 return;
1371 }
1372 } else {
1373 // The shared path table is set up after module system initialization.
1374 // The path table contains no entry before that. Any classes loaded prior
1375 // to the setup of the shared path table must be from the modules image.
1376 assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");
1377 assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1378 classpath_index = 0;
1379 }
1380
1381 const char* const class_name = ik->name()->as_C_string();
1382 const char* const file_name = file_name_for_class_name(class_name,
1383 ik->name()->utf8_length());
1384 assert(file_name != nullptr, "invariant");
1385 ClassLoaderExt::record_result(checked_cast<s2>(classpath_index), ik, redefined);
1386 }
1387 #endif // INCLUDE_CDS
1388
1389 // Initialize the class loader's access to methods in libzip. Parse and
1390 // process the boot classpath into a list ClassPathEntry objects. Once
1391 // this list has been created, it must not change order (see class PackageInfo)
1392 // it can be appended to and is by jvmti.
1393
1394 void ClassLoader::initialize(TRAPS) {
1395 if (UsePerfData) {
1396 // jvmstat performance counters
|
1122
1123 // Called by the boot classloader to load classes
1124 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1125 assert(name != nullptr, "invariant");
1126
1127 ResourceMark rm(THREAD);
1128 HandleMark hm(THREAD);
1129
1130 const char* const class_name = name->as_C_string();
1131
1132 EventMarkClassLoading m("Loading class %s", class_name);
1133
1134 const char* const file_name = file_name_for_class_name(class_name,
1135 name->utf8_length());
1136 assert(file_name != nullptr, "invariant");
1137
1138 // Lookup stream for parsing .class file
1139 ClassFileStream* stream = nullptr;
1140 s2 classpath_index = 0;
1141 ClassPathEntry* e = nullptr;
1142 bool is_patched = false;
1143
1144 // If search_append_only is true, boot loader visibility boundaries are
1145 // set to be _first_append_entry to the end. This includes:
1146 // [-Xbootclasspath/a]; [jvmti appended entries]
1147 //
1148 // If search_append_only is false, boot loader visibility boundaries are
1149 // set to be the --patch-module entries plus the base piece. This includes:
1150 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1151 //
1152
1153 // Load Attempt #1: --patch-module
1154 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1155 // attempt to load the class from those locations specific to the module.
1156 // Specifications to --patch-module can contain a partial number of classes
1157 // that are part of the overall module definition. So if a particular class is not
1158 // found within its module specification, the search should continue to Load Attempt #2.
1159 // Note: The --patch-module entries are never searched if the boot loader's
1160 // visibility boundary is limited to only searching the append entries.
1161 if (_patch_mod_entries != nullptr && !search_append_only) {
1162 // At CDS dump time, the --patch-module entries are ignored. That means a
1163 // class is still loaded from the runtime image even if it might
1164 // appear in the _patch_mod_entries. The runtime shared class visibility
1165 // check will determine if a shared class is visible based on the runtime
1166 // environment, including the runtime --patch-module setting.
1167 if (!CDSConfig::is_valhalla_preview()) {
1168 // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1169 // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1170 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1171 }
1172 if (CDSConfig::is_valhalla_preview() || !CDSConfig::is_dumping_static_archive()) {
1173 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1174 if (stream != nullptr) {
1175 is_patched = true;
1176 }
1177 }
1178 }
1179
1180 // Load Attempt #2: [jimage | exploded build]
1181 if (!search_append_only && (nullptr == stream)) {
1182 if (has_jrt_entry()) {
1183 e = _jrt_entry;
1184 stream = _jrt_entry->open_stream(THREAD, file_name);
1185 } else {
1186 // Exploded build - attempt to locate class in its defining module's location.
1187 assert(_exploded_entries != nullptr, "No exploded build entries present");
1188 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1189 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1190 }
1191 }
1192
1193 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1194 if (search_append_only && (nullptr == stream)) {
1195 // For the boot loader append path search, the starting classpath_index
1196 // for the appended piece is always 1 to account for either the
1197 // _jrt_entry or the _exploded_entries.
1208 ++classpath_index;
1209 }
1210 }
1211
1212 if (nullptr == stream) {
1213 return nullptr;
1214 }
1215
1216 stream->set_verify(ClassLoaderExt::should_verify(classpath_index));
1217
1218 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1219 Handle protection_domain;
1220 ClassLoadInfo cl_info(protection_domain);
1221
1222 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1223 name,
1224 loader_data,
1225 cl_info,
1226 CHECK_NULL);
1227 result->set_classpath_index(classpath_index);
1228 if (is_patched) {
1229 result->set_shared_classpath_index(0);
1230 result->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1231 }
1232 return result;
1233 }
1234
1235 #if INCLUDE_CDS
1236 static const char* skip_uri_protocol(const char* source) {
1237 if (strncmp(source, "file:", 5) == 0) {
1238 // file: protocol path could start with file:/ or file:///
1239 // locate the char after all the forward slashes
1240 int offset = 5;
1241 while (*(source + offset) == '/') {
1242 offset++;
1243 }
1244 source += offset;
1245 // for non-windows platforms, move back one char as the path begins with a '/'
1246 #ifndef _WINDOWS
1247 source -= 1;
1248 #endif
1249 } else if (strncmp(source, "jrt:/", 5) == 0) {
1250 source += 5;
1251 }
1288 for (size_t i = 0; i < strlen(uri); ++i) {
1289 char decoded = decode_percent_encoded(uri, i);
1290 path[path_index++] = decoded;
1291 }
1292 path[path_index] = '\0';
1293 return path;
1294 }
1295
1296 // Record the shared classpath index and loader type for classes loaded
1297 // by the builtin loaders at dump time.
1298 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1299 const ClassFileStream* stream, bool redefined) {
1300 assert(CDSConfig::is_dumping_archive(), "sanity");
1301 assert(stream != nullptr, "sanity");
1302
1303 if (ik->is_hidden()) {
1304 // We do not archive hidden classes.
1305 return;
1306 }
1307
1308 if (ik->shared_classpath_index() == 0 && ik->is_shared_boot_class()) {
1309 return;
1310 }
1311
1312 oop loader = ik->class_loader();
1313 char* src = (char*)stream->source();
1314 if (src == nullptr) {
1315 if (loader == nullptr) {
1316 // JFR classes
1317 ik->set_shared_classpath_index(0);
1318 ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1319 }
1320 return;
1321 }
1322
1323 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1324
1325 ResourceMark rm(current);
1326 int classpath_index = -1;
1327 PackageEntry* pkg_entry = ik->package();
1328
1329 if (FileMapInfo::get_number_of_shared_paths() > 0) {
1330 // Save the path from the file: protocol or the module name from the jrt: protocol
1331 // if no protocol prefix is found, path is the same as stream->source(). This path
1379 // for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1380 // The class must be from the runtime modules image.
1381 if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {
1382 classpath_index = i;
1383 break;
1384 }
1385 }
1386
1387 // No path entry found for this class: most likely a shared class loaded by the
1388 // user defined classloader.
1389 if (classpath_index < 0 && !SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1390 assert(ik->shared_classpath_index() < 0, "not assigned yet");
1391 ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1392 SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1393 return;
1394 }
1395 } else {
1396 // The shared path table is set up after module system initialization.
1397 // The path table contains no entry before that. Any classes loaded prior
1398 // to the setup of the shared path table must be from the modules image.
1399 if (!CDSConfig::is_valhalla_preview()) {
1400 assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");
1401 }
1402 assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1403 classpath_index = 0;
1404 }
1405
1406 const char* const class_name = ik->name()->as_C_string();
1407 const char* const file_name = file_name_for_class_name(class_name,
1408 ik->name()->utf8_length());
1409 assert(file_name != nullptr, "invariant");
1410 ClassLoaderExt::record_result(checked_cast<s2>(classpath_index), ik, redefined);
1411 }
1412 #endif // INCLUDE_CDS
1413
1414 // Initialize the class loader's access to methods in libzip. Parse and
1415 // process the boot classpath into a list ClassPathEntry objects. Once
1416 // this list has been created, it must not change order (see class PackageInfo)
1417 // it can be appended to and is by jvmti.
1418
1419 void ClassLoader::initialize(TRAPS) {
1420 if (UsePerfData) {
1421 // jvmstat performance counters
|