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