1014
1015 // Called by the boot classloader to load classes
1016 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1017 assert(name != nullptr, "invariant");
1018
1019 ResourceMark rm(THREAD);
1020 HandleMark hm(THREAD);
1021
1022 const char* const class_name = name->as_C_string();
1023
1024 EventMarkClassLoading m("Loading class %s", class_name);
1025
1026 const char* const file_name = file_name_for_class_name(class_name,
1027 name->utf8_length());
1028 assert(file_name != nullptr, "invariant");
1029
1030 // Lookup stream for parsing .class file
1031 ClassFileStream* stream = nullptr;
1032 s2 classpath_index = 0;
1033 ClassPathEntry* e = nullptr;
1034
1035 // If search_append_only is true, boot loader visibility boundaries are
1036 // set to be _first_append_entry to the end. This includes:
1037 // [-Xbootclasspath/a]; [jvmti appended entries]
1038 //
1039 // If search_append_only is false, boot loader visibility boundaries are
1040 // set to be the --patch-module entries plus the base piece. This includes:
1041 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1042 //
1043
1044 // Load Attempt #1: --patch-module
1045 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1046 // attempt to load the class from those locations specific to the module.
1047 // Specifications to --patch-module can contain a partial number of classes
1048 // that are part of the overall module definition. So if a particular class is not
1049 // found within its module specification, the search should continue to Load Attempt #2.
1050 // Note: The --patch-module entries are never searched if the boot loader's
1051 // visibility boundary is limited to only searching the append entries.
1052 if (_patch_mod_entries != nullptr && !search_append_only) {
1053 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1054 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1055 }
1056
1057 // Load Attempt #2: [jimage | exploded build]
1058 if (!search_append_only && (nullptr == stream)) {
1059 if (has_jrt_entry()) {
1060 e = _jrt_entry;
1061 stream = _jrt_entry->open_stream(THREAD, file_name);
1062 } else {
1063 // Exploded build - attempt to locate class in its defining module's location.
1064 assert(_exploded_entries != nullptr, "No exploded build entries present");
1065 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1066 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1067 }
1068 }
1069
1070 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1071 if (search_append_only && (nullptr == stream)) {
1072 // For the boot loader append path search, the starting classpath_index
1073 // for the appended piece is always 1 to account for either the
1074 // _jrt_entry or the _exploded_entries.
1083 }
1084 e = e->next();
1085 ++classpath_index;
1086 }
1087 }
1088
1089 if (nullptr == stream) {
1090 return nullptr;
1091 }
1092
1093 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1094 Handle protection_domain;
1095 ClassLoadInfo cl_info(protection_domain);
1096
1097 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1098 name,
1099 loader_data,
1100 cl_info,
1101 CHECK_NULL);
1102 result->set_classpath_index(classpath_index);
1103 return result;
1104 }
1105
1106 #if INCLUDE_CDS
1107 static const char* skip_uri_protocol(const char* source) {
1108 if (strncmp(source, "file:", 5) == 0) {
1109 // file: protocol path could start with file:/ or file:///
1110 // locate the char after all the forward slashes
1111 int offset = 5;
1112 while (*(source + offset) == '/') {
1113 offset++;
1114 }
1115 source += offset;
1116 // for non-windows platforms, move back one char as the path begins with a '/'
1117 #ifndef _WINDOWS
1118 source -= 1;
1119 #endif
1120 } else if (strncmp(source, "jrt:/", 5) == 0) {
1121 source += 5;
1122 }
1159 for (size_t i = 0; i < strlen(uri); ++i) {
1160 char decoded = decode_percent_encoded(uri, i);
1161 path[path_index++] = decoded;
1162 }
1163 path[path_index] = '\0';
1164 return path;
1165 }
1166
1167 // Record the shared classpath index and loader type for classes loaded
1168 // by the builtin loaders at dump time.
1169 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1170 const ClassFileStream* stream, bool redefined) {
1171 assert(CDSConfig::is_dumping_archive(), "sanity");
1172 assert(stream != nullptr, "sanity");
1173
1174 if (ik->is_hidden()) {
1175 record_hidden_class(ik);
1176 return;
1177 }
1178
1179 oop loader = ik->class_loader();
1180 char* src = (char*)stream->source();
1181 if (src == nullptr) {
1182 if (loader == nullptr) {
1183 // JFR classes
1184 ik->set_shared_classpath_index(0);
1185 ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1186 }
1187 return;
1188 }
1189
1190 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1191
1192 ResourceMark rm(current);
1193 int classpath_index = -1;
1194 PackageEntry* pkg_entry = ik->package();
1195
1196 if (!AOTClassLocationConfig::dumptime_is_ready()) {
1197 // The shared path table is set up after module system initialization.
1198 // The path table contains no entry before that. Any classes loaded prior
|
1014
1015 // Called by the boot classloader to load classes
1016 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1017 assert(name != nullptr, "invariant");
1018
1019 ResourceMark rm(THREAD);
1020 HandleMark hm(THREAD);
1021
1022 const char* const class_name = name->as_C_string();
1023
1024 EventMarkClassLoading m("Loading class %s", class_name);
1025
1026 const char* const file_name = file_name_for_class_name(class_name,
1027 name->utf8_length());
1028 assert(file_name != nullptr, "invariant");
1029
1030 // Lookup stream for parsing .class file
1031 ClassFileStream* stream = nullptr;
1032 s2 classpath_index = 0;
1033 ClassPathEntry* e = nullptr;
1034 bool is_patched = false;
1035
1036 // If search_append_only is true, boot loader visibility boundaries are
1037 // set to be _first_append_entry to the end. This includes:
1038 // [-Xbootclasspath/a]; [jvmti appended entries]
1039 //
1040 // If search_append_only is false, boot loader visibility boundaries are
1041 // set to be the --patch-module entries plus the base piece. This includes:
1042 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1043 //
1044
1045 // Load Attempt #1: --patch-module
1046 // Determine the class' defining module. If it appears in the _patch_mod_entries,
1047 // attempt to load the class from those locations specific to the module.
1048 // Specifications to --patch-module can contain a partial number of classes
1049 // that are part of the overall module definition. So if a particular class is not
1050 // found within its module specification, the search should continue to Load Attempt #2.
1051 // Note: The --patch-module entries are never searched if the boot loader's
1052 // visibility boundary is limited to only searching the append entries.
1053 if (_patch_mod_entries != nullptr && !search_append_only) {
1054 // At CDS dump time, the --patch-module entries are ignored. That means a
1055 // class is still loaded from the runtime image even if it might
1056 // appear in the _patch_mod_entries. The runtime shared class visibility
1057 // check will determine if a shared class is visible based on the runtime
1058 // environment, including the runtime --patch-module setting.
1059 if (!CDSConfig::is_valhalla_preview()) {
1060 // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1061 // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1062 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1063 }
1064 if (CDSConfig::is_valhalla_preview() || !CDSConfig::is_dumping_static_archive()) {
1065 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1066 if (stream != nullptr) {
1067 is_patched = true;
1068 }
1069 }
1070 }
1071
1072 // Load Attempt #2: [jimage | exploded build]
1073 if (!search_append_only && (nullptr == stream)) {
1074 if (has_jrt_entry()) {
1075 e = _jrt_entry;
1076 stream = _jrt_entry->open_stream(THREAD, file_name);
1077 } else {
1078 // Exploded build - attempt to locate class in its defining module's location.
1079 assert(_exploded_entries != nullptr, "No exploded build entries present");
1080 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1081 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1082 }
1083 }
1084
1085 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1086 if (search_append_only && (nullptr == stream)) {
1087 // For the boot loader append path search, the starting classpath_index
1088 // for the appended piece is always 1 to account for either the
1089 // _jrt_entry or the _exploded_entries.
1098 }
1099 e = e->next();
1100 ++classpath_index;
1101 }
1102 }
1103
1104 if (nullptr == stream) {
1105 return nullptr;
1106 }
1107
1108 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1109 Handle protection_domain;
1110 ClassLoadInfo cl_info(protection_domain);
1111
1112 InstanceKlass* result = KlassFactory::create_from_stream(stream,
1113 name,
1114 loader_data,
1115 cl_info,
1116 CHECK_NULL);
1117 result->set_classpath_index(classpath_index);
1118 if (is_patched) {
1119 result->set_shared_classpath_index(0);
1120 result->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1121 }
1122 return result;
1123 }
1124
1125 #if INCLUDE_CDS
1126 static const char* skip_uri_protocol(const char* source) {
1127 if (strncmp(source, "file:", 5) == 0) {
1128 // file: protocol path could start with file:/ or file:///
1129 // locate the char after all the forward slashes
1130 int offset = 5;
1131 while (*(source + offset) == '/') {
1132 offset++;
1133 }
1134 source += offset;
1135 // for non-windows platforms, move back one char as the path begins with a '/'
1136 #ifndef _WINDOWS
1137 source -= 1;
1138 #endif
1139 } else if (strncmp(source, "jrt:/", 5) == 0) {
1140 source += 5;
1141 }
1178 for (size_t i = 0; i < strlen(uri); ++i) {
1179 char decoded = decode_percent_encoded(uri, i);
1180 path[path_index++] = decoded;
1181 }
1182 path[path_index] = '\0';
1183 return path;
1184 }
1185
1186 // Record the shared classpath index and loader type for classes loaded
1187 // by the builtin loaders at dump time.
1188 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1189 const ClassFileStream* stream, bool redefined) {
1190 assert(CDSConfig::is_dumping_archive(), "sanity");
1191 assert(stream != nullptr, "sanity");
1192
1193 if (ik->is_hidden()) {
1194 record_hidden_class(ik);
1195 return;
1196 }
1197
1198 if (ik->shared_classpath_index() == 0 && ik->is_shared_boot_class()) {
1199 return;
1200 }
1201
1202 oop loader = ik->class_loader();
1203 char* src = (char*)stream->source();
1204 if (src == nullptr) {
1205 if (loader == nullptr) {
1206 // JFR classes
1207 ik->set_shared_classpath_index(0);
1208 ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1209 }
1210 return;
1211 }
1212
1213 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1214
1215 ResourceMark rm(current);
1216 int classpath_index = -1;
1217 PackageEntry* pkg_entry = ik->package();
1218
1219 if (!AOTClassLocationConfig::dumptime_is_ready()) {
1220 // The shared path table is set up after module system initialization.
1221 // The path table contains no entry before that. Any classes loaded prior
|