1 /*
2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classFileParser.hpp"
27 #include "classfile/classFileStream.hpp"
28 #include "classfile/classListParser.hpp"
29 #include "classfile/classLoader.inline.hpp"
30 #include "classfile/classLoaderExt.hpp"
31 #include "classfile/classLoaderData.inline.hpp"
32 #include "classfile/klassFactory.hpp"
33 #include "classfile/modules.hpp"
34 #include "classfile/sharedPathsMiscInfo.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #include "classfile/vmSymbols.hpp"
37 #include "memory/allocation.inline.hpp"
38 #include "memory/filemap.hpp"
39 #include "memory/resourceArea.hpp"
40 #include "oops/instanceKlass.hpp"
41 #include "oops/oop.inline.hpp"
42 #include "oops/symbol.hpp"
43 #include "runtime/arguments.hpp"
44 #include "runtime/handles.inline.hpp"
45 #include "runtime/java.hpp"
46 #include "runtime/javaCalls.hpp"
47 #include "runtime/os.hpp"
48 #include "services/threadService.hpp"
49 #include "utilities/stringUtils.hpp"
50
51 jshort ClassLoaderExt::_app_class_paths_start_index = ClassLoaderExt::max_classpath_index;
52 jshort ClassLoaderExt::_app_module_paths_start_index = ClassLoaderExt::max_classpath_index;
53 jshort ClassLoaderExt::_max_used_path_index = 0;
54 bool ClassLoaderExt::_has_app_classes = false;
55 bool ClassLoaderExt::_has_platform_classes = false;
56
append_boot_classpath(ClassPathEntry * new_entry)57 void ClassLoaderExt::append_boot_classpath(ClassPathEntry* new_entry) {
58 #if INCLUDE_CDS
59 if (UseSharedSpaces) {
60 warning("Sharing is only supported for boot loader classes because bootstrap classpath has been appended");
61 FileMapInfo::current_info()->header()->set_has_platform_or_app_classes(false);
62 }
63 #endif
64 ClassLoader::add_to_boot_append_entries(new_entry);
65 }
66
setup_app_search_path()67 void ClassLoaderExt::setup_app_search_path() {
68 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
69 _app_class_paths_start_index = ClassLoader::num_boot_classpath_entries();
70 char* app_class_path = os::strdup(Arguments::get_appclasspath());
71
72 if (strcmp(app_class_path, ".") == 0) {
73 // This doesn't make any sense, even for AppCDS, so let's skip it. We
74 // don't want to throw an error here because -cp "." is usually assigned
75 // by the launcher when classpath is not specified.
76 trace_class_path("app loader class path (skipped)=", app_class_path);
77 } else {
78 trace_class_path("app loader class path=", app_class_path);
79 shared_paths_misc_info()->add_app_classpath(app_class_path);
80 ClassLoader::setup_app_search_path(app_class_path);
81 }
82 }
83
process_module_table(ModuleEntryTable * met,TRAPS)84 void ClassLoaderExt::process_module_table(ModuleEntryTable* met, TRAPS) {
85 ResourceMark rm(THREAD);
86 for (int i = 0; i < met->table_size(); i++) {
87 for (ModuleEntry* m = met->bucket(i); m != NULL;) {
88 char* path = m->location()->as_C_string();
89 if (strncmp(path, "file:", 5) == 0) {
90 path = ClassLoader::skip_uri_protocol(path);
91 ClassLoader::setup_module_search_path(path, THREAD);
92 }
93 m = m->next();
94 }
95 }
96 }
setup_module_paths(TRAPS)97 void ClassLoaderExt::setup_module_paths(TRAPS) {
98 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
99 _app_module_paths_start_index = ClassLoader::num_boot_classpath_entries() +
100 ClassLoader::num_app_classpath_entries();
101 Handle system_class_loader (THREAD, SystemDictionary::java_system_loader());
102 ModuleEntryTable* met = Modules::get_module_entry_table(system_class_loader);
103 process_module_table(met, THREAD);
104 }
105
read_manifest(ClassPathEntry * entry,jint * manifest_size,bool clean_text,TRAPS)106 char* ClassLoaderExt::read_manifest(ClassPathEntry* entry, jint *manifest_size, bool clean_text, TRAPS) {
107 const char* name = "META-INF/MANIFEST.MF";
108 char* manifest;
109 jint size;
110
111 assert(entry->is_jar_file(), "must be");
112 manifest = (char*) ((ClassPathZipEntry*)entry )->open_entry(name, &size, true, CHECK_NULL);
113
114 if (manifest == NULL) { // No Manifest
115 *manifest_size = 0;
116 return NULL;
117 }
118
119
120 if (clean_text) {
121 // See http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#JAR%20Manifest
122 // (1): replace all CR/LF and CR with LF
123 StringUtils::replace_no_expand(manifest, "\r\n", "\n");
124
125 // (2) remove all new-line continuation (remove all "\n " substrings)
126 StringUtils::replace_no_expand(manifest, "\n ", "");
127 }
128
129 *manifest_size = (jint)strlen(manifest);
130 return manifest;
131 }
132
get_class_path_attr(const char * jar_path,char * manifest,jint manifest_size)133 char* ClassLoaderExt::get_class_path_attr(const char* jar_path, char* manifest, jint manifest_size) {
134 const char* tag = "Class-Path: ";
135 const int tag_len = (int)strlen(tag);
136 char* found = NULL;
137 char* line_start = manifest;
138 char* end = manifest + manifest_size;
139
140 assert(*end == 0, "must be nul-terminated");
141
142 while (line_start < end) {
143 char* line_end = strchr(line_start, '\n');
144 if (line_end == NULL) {
145 // JAR spec require the manifest file to be terminated by a new line.
146 break;
147 }
148 if (strncmp(tag, line_start, tag_len) == 0) {
149 if (found != NULL) {
150 // Same behavior as jdk/src/share/classes/java/util/jar/Attributes.java
151 // If duplicated entries are found, the last one is used.
152 tty->print_cr("Warning: Duplicate name in Manifest: %s.\n"
153 "Ensure that the manifest does not have duplicate entries, and\n"
154 "that blank lines separate individual sections in both your\n"
155 "manifest and in the META-INF/MANIFEST.MF entry in the jar file:\n%s\n", tag, jar_path);
156 }
157 found = line_start + tag_len;
158 assert(found <= line_end, "sanity");
159 *line_end = '\0';
160 }
161 line_start = line_end + 1;
162 }
163 return found;
164 }
165
process_jar_manifest(ClassPathEntry * entry,bool check_for_duplicates)166 void ClassLoaderExt::process_jar_manifest(ClassPathEntry* entry,
167 bool check_for_duplicates) {
168 Thread* THREAD = Thread::current();
169 ResourceMark rm(THREAD);
170 jint manifest_size;
171 char* manifest = read_manifest(entry, &manifest_size, CHECK);
172
173 if (manifest == NULL) {
174 return;
175 }
176
177 if (strstr(manifest, "Extension-List:") != NULL) {
178 tty->print_cr("-Xshare:dump does not support Extension-List in JAR manifest: %s", entry->name());
179 vm_exit(1);
180 }
181
182 char* cp_attr = get_class_path_attr(entry->name(), manifest, manifest_size);
183
184 if (cp_attr != NULL && strlen(cp_attr) > 0) {
185 trace_class_path("found Class-Path: ", cp_attr);
186
187 char sep = os::file_separator()[0];
188 const char* dir_name = entry->name();
189 const char* dir_tail = strrchr(dir_name, sep);
190 int dir_len;
191 if (dir_tail == NULL) {
192 dir_len = 0;
193 } else {
194 dir_len = dir_tail - dir_name + 1;
195 }
196
197 // Split the cp_attr by spaces, and add each file
198 char* file_start = cp_attr;
199 char* end = file_start + strlen(file_start);
200
201 while (file_start < end) {
202 char* file_end = strchr(file_start, ' ');
203 if (file_end != NULL) {
204 *file_end = 0;
205 file_end += 1;
206 } else {
207 file_end = end;
208 }
209
210 size_t name_len = strlen(file_start);
211 if (name_len > 0) {
212 ResourceMark rm(THREAD);
213 size_t libname_len = dir_len + name_len;
214 char* libname = NEW_RESOURCE_ARRAY(char, libname_len + 1);
215 int n = os::snprintf(libname, libname_len + 1, "%.*s%s", dir_len, dir_name, file_start);
216 assert((size_t)n == libname_len, "Unexpected number of characters in string");
217 trace_class_path("library = ", libname);
218 ClassLoader::update_class_path_entry_list(libname, true, false);
219 }
220
221 file_start = file_end;
222 }
223 }
224 }
225
setup_search_paths()226 void ClassLoaderExt::setup_search_paths() {
227 shared_paths_misc_info()->record_app_offset();
228 ClassLoaderExt::setup_app_search_path();
229 }
230
record_result(const s2 classpath_index,InstanceKlass * result,TRAPS)231 void ClassLoaderExt::record_result(const s2 classpath_index,
232 InstanceKlass* result,
233 TRAPS) {
234 assert(DumpSharedSpaces, "Sanity");
235
236 // We need to remember where the class comes from during dumping.
237 oop loader = result->class_loader();
238 s2 classloader_type = ClassLoader::BOOT_LOADER;
239 if (SystemDictionary::is_system_class_loader(loader)) {
240 classloader_type = ClassLoader::APP_LOADER;
241 ClassLoaderExt::set_has_app_classes();
242 } else if (SystemDictionary::is_platform_class_loader(loader)) {
243 classloader_type = ClassLoader::PLATFORM_LOADER;
244 ClassLoaderExt::set_has_platform_classes();
245 }
246 if (classpath_index > ClassLoaderExt::max_used_path_index()) {
247 ClassLoaderExt::set_max_used_path_index(classpath_index);
248 }
249 result->set_shared_classpath_index(classpath_index);
250 result->set_class_loader_type(classloader_type);
251 }
252
finalize_shared_paths_misc_info()253 void ClassLoaderExt::finalize_shared_paths_misc_info() {
254 if (!_has_app_classes) {
255 shared_paths_misc_info()->pop_app();
256 }
257 }
258
259 // Load the class of the given name from the location given by path. The path is specified by
260 // the "source:" in the class list file (see classListParser.cpp), and can be a directory or
261 // a JAR file.
load_class(Symbol * name,const char * path,TRAPS)262 InstanceKlass* ClassLoaderExt::load_class(Symbol* name, const char* path, TRAPS) {
263
264 assert(name != NULL, "invariant");
265 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
266 ResourceMark rm(THREAD);
267 const char* class_name = name->as_C_string();
268
269 const char* file_name = file_name_for_class_name(class_name,
270 name->utf8_length());
271 assert(file_name != NULL, "invariant");
272
273 // Lookup stream for parsing .class file
274 ClassFileStream* stream = NULL;
275 ClassPathEntry* e = find_classpath_entry_from_cache(path, CHECK_NULL);
276 if (e == NULL) {
277 return NULL;
278 }
279 {
280 PerfClassTraceTime vmtimer(perf_sys_class_lookup_time(),
281 ((JavaThread*) THREAD)->get_thread_stat()->perf_timers_addr(),
282 PerfClassTraceTime::CLASS_LOAD);
283 stream = e->open_stream(file_name, CHECK_NULL);
284 }
285
286 if (NULL == stream) {
287 tty->print_cr("Preload Warning: Cannot find %s", class_name);
288 return NULL;
289 }
290
291 assert(stream != NULL, "invariant");
292 stream->set_verify(true);
293
294 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
295 Handle protection_domain;
296
297 InstanceKlass* result = KlassFactory::create_from_stream(stream,
298 name,
299 loader_data,
300 protection_domain,
301 NULL, // host_klass
302 NULL, // cp_patches
303 THREAD);
304
305 if (HAS_PENDING_EXCEPTION) {
306 tty->print_cr("Preload Error: Failed to load %s", class_name);
307 return NULL;
308 }
309 result->set_shared_classpath_index(UNREGISTERED_INDEX);
310 SystemDictionaryShared::set_shared_class_misc_info(result, stream);
311 return result;
312 }
313
314 struct CachedClassPathEntry {
315 const char* _path;
316 ClassPathEntry* _entry;
317 };
318
319 static GrowableArray<CachedClassPathEntry>* cached_path_entries = NULL;
320
find_classpath_entry_from_cache(const char * path,TRAPS)321 ClassPathEntry* ClassLoaderExt::find_classpath_entry_from_cache(const char* path, TRAPS) {
322 // This is called from dump time so it's single threaded and there's no need for a lock.
323 assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
324 if (cached_path_entries == NULL) {
325 cached_path_entries = new (ResourceObj::C_HEAP, mtClass) GrowableArray<CachedClassPathEntry>(20, /*c heap*/ true);
326 }
327 CachedClassPathEntry ccpe;
328 for (int i=0; i<cached_path_entries->length(); i++) {
329 ccpe = cached_path_entries->at(i);
330 if (strcmp(ccpe._path, path) == 0) {
331 if (i != 0) {
332 // Put recent entries at the beginning to speed up searches.
333 cached_path_entries->remove_at(i);
334 cached_path_entries->insert_before(0, ccpe);
335 }
336 return ccpe._entry;
337 }
338 }
339
340 struct stat st;
341 if (os::stat(path, &st) != 0) {
342 // File or directory not found
343 return NULL;
344 }
345 ClassPathEntry* new_entry = NULL;
346
347 new_entry = create_class_path_entry(path, &st, false, false, CHECK_NULL);
348 if (new_entry == NULL) {
349 return NULL;
350 }
351 ccpe._path = strdup(path);
352 ccpe._entry = new_entry;
353 cached_path_entries->insert_before(0, ccpe);
354 return new_entry;
355 }
356
load_one_class(ClassListParser * parser,TRAPS)357 Klass* ClassLoaderExt::load_one_class(ClassListParser* parser, TRAPS) {
358 return parser->load_current_class(THREAD);
359 }
360