Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/classfile/classLoaderExt.cpp
40949 views
1
/*
2
* Copyright (c) 2015, 2021, 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 "cds/filemap.hpp"
27
#include "classfile/classFileParser.hpp"
28
#include "classfile/classFileStream.hpp"
29
#include "classfile/classLoader.inline.hpp"
30
#include "classfile/classLoaderExt.hpp"
31
#include "classfile/classLoaderData.inline.hpp"
32
#include "classfile/classLoadInfo.hpp"
33
#include "classfile/klassFactory.hpp"
34
#include "classfile/modules.hpp"
35
#include "classfile/systemDictionary.hpp"
36
#include "classfile/systemDictionaryShared.hpp"
37
#include "classfile/vmSymbols.hpp"
38
#include "gc/shared/collectedHeap.hpp"
39
#include "logging/log.hpp"
40
#include "memory/allocation.inline.hpp"
41
#include "memory/resourceArea.hpp"
42
#include "oops/instanceKlass.hpp"
43
#include "oops/klass.inline.hpp"
44
#include "oops/oop.inline.hpp"
45
#include "oops/symbol.hpp"
46
#include "runtime/arguments.hpp"
47
#include "runtime/handles.inline.hpp"
48
#include "runtime/java.hpp"
49
#include "runtime/javaCalls.hpp"
50
#include "runtime/os.hpp"
51
#include "services/threadService.hpp"
52
#include "utilities/stringUtils.hpp"
53
54
jshort ClassLoaderExt::_app_class_paths_start_index = ClassLoaderExt::max_classpath_index;
55
jshort ClassLoaderExt::_app_module_paths_start_index = ClassLoaderExt::max_classpath_index;
56
jshort ClassLoaderExt::_max_used_path_index = 0;
57
bool ClassLoaderExt::_has_app_classes = false;
58
bool ClassLoaderExt::_has_platform_classes = false;
59
60
void ClassLoaderExt::append_boot_classpath(ClassPathEntry* new_entry) {
61
if (UseSharedSpaces) {
62
warning("Sharing is only supported for boot loader classes because bootstrap classpath has been appended");
63
FileMapInfo::current_info()->set_has_platform_or_app_classes(false);
64
}
65
ClassLoader::add_to_boot_append_entries(new_entry);
66
}
67
68
void ClassLoaderExt::setup_app_search_path(JavaThread* current) {
69
Arguments::assert_is_dumping_archive();
70
_app_class_paths_start_index = ClassLoader::num_boot_classpath_entries();
71
char* app_class_path = os::strdup(Arguments::get_appclasspath());
72
73
if (strcmp(app_class_path, ".") == 0) {
74
// This doesn't make any sense, even for AppCDS, so let's skip it. We
75
// don't want to throw an error here because -cp "." is usually assigned
76
// by the launcher when classpath is not specified.
77
trace_class_path("app loader class path (skipped)=", app_class_path);
78
} else {
79
trace_class_path("app loader class path=", app_class_path);
80
ClassLoader::setup_app_search_path(current, app_class_path);
81
}
82
}
83
84
void ClassLoaderExt::process_module_table(JavaThread* current, ModuleEntryTable* met) {
85
ResourceMark rm(current);
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(current, path);
92
}
93
m = m->next();
94
}
95
}
96
}
97
void ClassLoaderExt::setup_module_paths(JavaThread* current) {
98
Arguments::assert_is_dumping_archive();
99
_app_module_paths_start_index = ClassLoader::num_boot_classpath_entries() +
100
ClassLoader::num_app_classpath_entries();
101
Handle system_class_loader (current, SystemDictionary::java_system_loader());
102
ModuleEntryTable* met = Modules::get_module_entry_table(system_class_loader);
103
process_module_table(current, met);
104
}
105
106
char* ClassLoaderExt::read_manifest(JavaThread* current, ClassPathEntry* entry,
107
jint *manifest_size, bool clean_text) {
108
const char* name = "META-INF/MANIFEST.MF";
109
char* manifest;
110
jint size;
111
112
assert(entry->is_jar_file(), "must be");
113
manifest = (char*) ((ClassPathZipEntry*)entry )->open_entry(current, name, &size, true);
114
115
if (manifest == NULL) { // No Manifest
116
*manifest_size = 0;
117
return NULL;
118
}
119
120
121
if (clean_text) {
122
// See http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#JAR%20Manifest
123
// (1): replace all CR/LF and CR with LF
124
StringUtils::replace_no_expand(manifest, "\r\n", "\n");
125
126
// (2) remove all new-line continuation (remove all "\n " substrings)
127
StringUtils::replace_no_expand(manifest, "\n ", "");
128
}
129
130
*manifest_size = (jint)strlen(manifest);
131
return manifest;
132
}
133
134
char* ClassLoaderExt::get_class_path_attr(const char* jar_path, char* manifest, jint manifest_size) {
135
const char* tag = "Class-Path: ";
136
const int tag_len = (int)strlen(tag);
137
char* found = NULL;
138
char* line_start = manifest;
139
char* end = manifest + manifest_size;
140
141
assert(*end == 0, "must be nul-terminated");
142
143
while (line_start < end) {
144
char* line_end = strchr(line_start, '\n');
145
if (line_end == NULL) {
146
// JAR spec require the manifest file to be terminated by a new line.
147
break;
148
}
149
if (strncmp(tag, line_start, tag_len) == 0) {
150
if (found != NULL) {
151
// Same behavior as jdk/src/share/classes/java/util/jar/Attributes.java
152
// If duplicated entries are found, the last one is used.
153
log_warning(cds)("Warning: Duplicate name in Manifest: %s.\n"
154
"Ensure that the manifest does not have duplicate entries, and\n"
155
"that blank lines separate individual sections in both your\n"
156
"manifest and in the META-INF/MANIFEST.MF entry in the jar file:\n%s\n", tag, jar_path);
157
}
158
found = line_start + tag_len;
159
assert(found <= line_end, "sanity");
160
*line_end = '\0';
161
}
162
line_start = line_end + 1;
163
}
164
return found;
165
}
166
167
void ClassLoaderExt::process_jar_manifest(JavaThread* current, ClassPathEntry* entry,
168
bool check_for_duplicates) {
169
ResourceMark rm(current);
170
jint manifest_size;
171
char* manifest = read_manifest(current, entry, &manifest_size);
172
173
if (manifest == NULL) {
174
return;
175
}
176
177
if (strstr(manifest, "Extension-List:") != NULL) {
178
vm_exit_during_cds_dumping(err_msg("-Xshare:dump does not support Extension-List in JAR manifest: %s", entry->name()));
179
}
180
181
char* cp_attr = get_class_path_attr(entry->name(), manifest, manifest_size);
182
183
if (cp_attr != NULL && strlen(cp_attr) > 0) {
184
trace_class_path("found Class-Path: ", cp_attr);
185
186
char sep = os::file_separator()[0];
187
const char* dir_name = entry->name();
188
const char* dir_tail = strrchr(dir_name, sep);
189
int dir_len;
190
if (dir_tail == NULL) {
191
dir_len = 0;
192
} else {
193
dir_len = dir_tail - dir_name + 1;
194
}
195
196
// Split the cp_attr by spaces, and add each file
197
char* file_start = cp_attr;
198
char* end = file_start + strlen(file_start);
199
200
while (file_start < end) {
201
char* file_end = strchr(file_start, ' ');
202
if (file_end != NULL) {
203
*file_end = 0;
204
file_end += 1;
205
} else {
206
file_end = end;
207
}
208
209
size_t name_len = strlen(file_start);
210
if (name_len > 0) {
211
ResourceMark rm(current);
212
size_t libname_len = dir_len + name_len;
213
char* libname = NEW_RESOURCE_ARRAY(char, libname_len + 1);
214
int n = os::snprintf(libname, libname_len + 1, "%.*s%s", dir_len, dir_name, file_start);
215
assert((size_t)n == libname_len, "Unexpected number of characters in string");
216
if (ClassLoader::update_class_path_entry_list(current, libname, true, false, true /* from_class_path_attr */)) {
217
trace_class_path("library = ", libname);
218
} else {
219
trace_class_path("library (non-existent) = ", libname);
220
FileMapInfo::record_non_existent_class_path_entry(libname);
221
}
222
}
223
224
file_start = file_end;
225
}
226
}
227
}
228
229
void ClassLoaderExt::setup_search_paths(JavaThread* current) {
230
ClassLoaderExt::setup_app_search_path(current);
231
}
232
233
void ClassLoaderExt::record_result(const s2 classpath_index, InstanceKlass* result) {
234
Arguments::assert_is_dumping_archive();
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_shared_class_loader_type(classloader_type);
251
}
252
253
// Load the class of the given name from the location given by path. The path is specified by
254
// the "source:" in the class list file (see classListParser.cpp), and can be a directory or
255
// a JAR file.
256
InstanceKlass* ClassLoaderExt::load_class(Symbol* name, const char* path, TRAPS) {
257
assert(name != NULL, "invariant");
258
assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
259
ResourceMark rm(THREAD);
260
const char* class_name = name->as_C_string();
261
const char* file_name = file_name_for_class_name(class_name,
262
name->utf8_length());
263
assert(file_name != NULL, "invariant");
264
265
// Lookup stream for parsing .class file
266
ClassFileStream* stream = NULL;
267
ClassPathEntry* e = find_classpath_entry_from_cache(THREAD, path);
268
if (e == NULL) {
269
THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());
270
}
271
272
{
273
PerfClassTraceTime vmtimer(perf_sys_class_lookup_time(),
274
THREAD->get_thread_stat()->perf_timers_addr(),
275
PerfClassTraceTime::CLASS_LOAD);
276
stream = e->open_stream(THREAD, file_name);
277
}
278
279
if (stream == NULL) {
280
// open_stream could return NULL even when no exception has be thrown (JDK-8263632).
281
THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());
282
return NULL;
283
}
284
stream->set_verify(true);
285
286
ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
287
Handle protection_domain;
288
ClassLoadInfo cl_info(protection_domain);
289
InstanceKlass* k = KlassFactory::create_from_stream(stream,
290
name,
291
loader_data,
292
cl_info,
293
CHECK_NULL);
294
return k;
295
}
296
297
struct CachedClassPathEntry {
298
const char* _path;
299
ClassPathEntry* _entry;
300
};
301
302
static GrowableArray<CachedClassPathEntry>* cached_path_entries = NULL;
303
304
ClassPathEntry* ClassLoaderExt::find_classpath_entry_from_cache(JavaThread* current, const char* path) {
305
// This is called from dump time so it's single threaded and there's no need for a lock.
306
assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
307
if (cached_path_entries == NULL) {
308
cached_path_entries = new (ResourceObj::C_HEAP, mtClass) GrowableArray<CachedClassPathEntry>(20, mtClass);
309
}
310
CachedClassPathEntry ccpe;
311
for (int i=0; i<cached_path_entries->length(); i++) {
312
ccpe = cached_path_entries->at(i);
313
if (strcmp(ccpe._path, path) == 0) {
314
if (i != 0) {
315
// Put recent entries at the beginning to speed up searches.
316
cached_path_entries->remove_at(i);
317
cached_path_entries->insert_before(0, ccpe);
318
}
319
return ccpe._entry;
320
}
321
}
322
323
struct stat st;
324
if (os::stat(path, &st) != 0) {
325
// File or directory not found
326
return NULL;
327
}
328
ClassPathEntry* new_entry = NULL;
329
330
new_entry = create_class_path_entry(current, path, &st, false, false);
331
if (new_entry == NULL) {
332
return NULL;
333
}
334
ccpe._path = strdup(path);
335
ccpe._entry = new_entry;
336
cached_path_entries->insert_before(0, ccpe);
337
return new_entry;
338
}
339
340