Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/classfile/classLoader.cpp
40949 views
1
/*
2
* Copyright (c) 1997, 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 "jvm.h"
27
#include "jimage.hpp"
28
#include "cds/filemap.hpp"
29
#include "classfile/classFileStream.hpp"
30
#include "classfile/classLoader.inline.hpp"
31
#include "classfile/classLoaderData.inline.hpp"
32
#include "classfile/classLoaderExt.hpp"
33
#include "classfile/classLoadInfo.hpp"
34
#include "classfile/javaClasses.hpp"
35
#include "classfile/moduleEntry.hpp"
36
#include "classfile/modules.hpp"
37
#include "classfile/packageEntry.hpp"
38
#include "classfile/klassFactory.hpp"
39
#include "classfile/symbolTable.hpp"
40
#include "classfile/systemDictionary.hpp"
41
#include "classfile/systemDictionaryShared.hpp"
42
#include "classfile/vmClasses.hpp"
43
#include "classfile/vmSymbols.hpp"
44
#include "compiler/compileBroker.hpp"
45
#include "interpreter/bytecodeStream.hpp"
46
#include "interpreter/oopMapCache.hpp"
47
#include "logging/log.hpp"
48
#include "logging/logStream.hpp"
49
#include "logging/logTag.hpp"
50
#include "memory/allocation.inline.hpp"
51
#include "memory/oopFactory.hpp"
52
#include "memory/resourceArea.hpp"
53
#include "memory/universe.hpp"
54
#include "oops/instanceKlass.hpp"
55
#include "oops/instanceRefKlass.hpp"
56
#include "oops/klass.inline.hpp"
57
#include "oops/method.inline.hpp"
58
#include "oops/objArrayOop.inline.hpp"
59
#include "oops/oop.inline.hpp"
60
#include "oops/symbol.hpp"
61
#include "prims/jvm_misc.hpp"
62
#include "runtime/arguments.hpp"
63
#include "runtime/handles.inline.hpp"
64
#include "runtime/init.hpp"
65
#include "runtime/interfaceSupport.inline.hpp"
66
#include "runtime/java.hpp"
67
#include "runtime/javaCalls.hpp"
68
#include "runtime/os.hpp"
69
#include "runtime/perfData.hpp"
70
#include "runtime/threadCritical.hpp"
71
#include "runtime/timer.hpp"
72
#include "runtime/vm_version.hpp"
73
#include "services/management.hpp"
74
#include "services/threadService.hpp"
75
#include "utilities/classpathStream.hpp"
76
#include "utilities/events.hpp"
77
#include "utilities/hashtable.inline.hpp"
78
#include "utilities/macros.hpp"
79
#include "utilities/utf8.hpp"
80
81
// Entry point in java.dll for path canonicalization
82
83
typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
84
85
static canonicalize_fn_t CanonicalizeEntry = NULL;
86
87
// Entry points in zip.dll for loading zip/jar file entries
88
89
typedef void * * (*ZipOpen_t)(const char *name, char **pmsg);
90
typedef void (*ZipClose_t)(jzfile *zip);
91
typedef jzentry* (*FindEntry_t)(jzfile *zip, const char *name, jint *sizeP, jint *nameLen);
92
typedef jboolean (*ReadEntry_t)(jzfile *zip, jzentry *entry, unsigned char *buf, char *namebuf);
93
typedef jzentry* (*GetNextEntry_t)(jzfile *zip, jint n);
94
typedef jint (*Crc32_t)(jint crc, const jbyte *buf, jint len);
95
96
static ZipOpen_t ZipOpen = NULL;
97
static ZipClose_t ZipClose = NULL;
98
static FindEntry_t FindEntry = NULL;
99
static ReadEntry_t ReadEntry = NULL;
100
static GetNextEntry_t GetNextEntry = NULL;
101
static Crc32_t Crc32 = NULL;
102
int ClassLoader::_libzip_loaded = 0;
103
104
// Entry points for jimage.dll for loading jimage file entries
105
106
static JImageOpen_t JImageOpen = NULL;
107
static JImageClose_t JImageClose = NULL;
108
static JImageFindResource_t JImageFindResource = NULL;
109
static JImageGetResource_t JImageGetResource = NULL;
110
111
// JimageFile pointer, or null if exploded JDK build.
112
static JImageFile* JImage_file = NULL;
113
114
// Globals
115
116
PerfCounter* ClassLoader::_perf_accumulated_time = NULL;
117
PerfCounter* ClassLoader::_perf_classes_inited = NULL;
118
PerfCounter* ClassLoader::_perf_class_init_time = NULL;
119
PerfCounter* ClassLoader::_perf_class_init_selftime = NULL;
120
PerfCounter* ClassLoader::_perf_classes_verified = NULL;
121
PerfCounter* ClassLoader::_perf_class_verify_time = NULL;
122
PerfCounter* ClassLoader::_perf_class_verify_selftime = NULL;
123
PerfCounter* ClassLoader::_perf_classes_linked = NULL;
124
PerfCounter* ClassLoader::_perf_class_link_time = NULL;
125
PerfCounter* ClassLoader::_perf_class_link_selftime = NULL;
126
PerfCounter* ClassLoader::_perf_sys_class_lookup_time = NULL;
127
PerfCounter* ClassLoader::_perf_shared_classload_time = NULL;
128
PerfCounter* ClassLoader::_perf_sys_classload_time = NULL;
129
PerfCounter* ClassLoader::_perf_app_classload_time = NULL;
130
PerfCounter* ClassLoader::_perf_app_classload_selftime = NULL;
131
PerfCounter* ClassLoader::_perf_app_classload_count = NULL;
132
PerfCounter* ClassLoader::_perf_define_appclasses = NULL;
133
PerfCounter* ClassLoader::_perf_define_appclass_time = NULL;
134
PerfCounter* ClassLoader::_perf_define_appclass_selftime = NULL;
135
PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = NULL;
136
PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL;
137
PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL;
138
139
GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = NULL;
140
GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = NULL;
141
ClassPathEntry* ClassLoader::_jrt_entry = NULL;
142
143
ClassPathEntry* volatile ClassLoader::_first_append_entry_list = NULL;
144
ClassPathEntry* volatile ClassLoader::_last_append_entry = NULL;
145
#if INCLUDE_CDS
146
ClassPathEntry* ClassLoader::_app_classpath_entries = NULL;
147
ClassPathEntry* ClassLoader::_last_app_classpath_entry = NULL;
148
ClassPathEntry* ClassLoader::_module_path_entries = NULL;
149
ClassPathEntry* ClassLoader::_last_module_path_entry = NULL;
150
#endif
151
152
// helper routines
153
bool string_starts_with(const char* str, const char* str_to_find) {
154
size_t str_len = strlen(str);
155
size_t str_to_find_len = strlen(str_to_find);
156
if (str_to_find_len > str_len) {
157
return false;
158
}
159
return (strncmp(str, str_to_find, str_to_find_len) == 0);
160
}
161
162
static const char* get_jimage_version_string() {
163
static char version_string[10] = "";
164
if (version_string[0] == '\0') {
165
jio_snprintf(version_string, sizeof(version_string), "%d.%d",
166
VM_Version::vm_major_version(), VM_Version::vm_minor_version());
167
}
168
return (const char*)version_string;
169
}
170
171
bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
172
size_t str_len = strlen(str);
173
size_t str_to_find_len = strlen(str_to_find);
174
if (str_to_find_len > str_len) {
175
return false;
176
}
177
return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
178
}
179
180
// Used to obtain the package name from a fully qualified class name.
181
Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) {
182
if (name == NULL) {
183
if (bad_class_name != NULL) {
184
*bad_class_name = true;
185
}
186
return NULL;
187
}
188
189
int utf_len = name->utf8_length();
190
const jbyte* base = (const jbyte*)name->base();
191
const jbyte* start = base;
192
const jbyte* end = UTF8::strrchr(start, utf_len, JVM_SIGNATURE_SLASH);
193
if (end == NULL) {
194
return NULL;
195
}
196
// Skip over '['s
197
if (*start == JVM_SIGNATURE_ARRAY) {
198
do {
199
start++;
200
} while (start < end && *start == JVM_SIGNATURE_ARRAY);
201
202
// Fully qualified class names should not contain a 'L'.
203
// Set bad_class_name to true to indicate that the package name
204
// could not be obtained due to an error condition.
205
// In this situation, is_same_class_package returns false.
206
if (*start == JVM_SIGNATURE_CLASS) {
207
if (bad_class_name != NULL) {
208
*bad_class_name = true;
209
}
210
return NULL;
211
}
212
}
213
// A class name could have just the slash character in the name,
214
// in which case start > end
215
if (start >= end) {
216
// No package name
217
if (bad_class_name != NULL) {
218
*bad_class_name = true;
219
}
220
return NULL;
221
}
222
return SymbolTable::new_symbol(name, start - base, end - base);
223
}
224
225
// Given a fully qualified package name, find its defining package in the class loader's
226
// package entry table.
227
PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {
228
if (pkg_name == NULL) {
229
return NULL;
230
}
231
PackageEntryTable* pkgEntryTable = loader_data->packages();
232
return pkgEntryTable->lookup_only(pkg_name);
233
}
234
235
const char* ClassPathEntry::copy_path(const char* path) {
236
char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
237
strcpy(copy, path);
238
return copy;
239
}
240
241
ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char* name) {
242
// construct full path name
243
assert((_dir != NULL) && (name != NULL), "sanity");
244
size_t path_len = strlen(_dir) + strlen(name) + strlen(os::file_separator()) + 1;
245
char* path = NEW_RESOURCE_ARRAY_IN_THREAD(current, char, path_len);
246
int len = jio_snprintf(path, path_len, "%s%s%s", _dir, os::file_separator(), name);
247
assert(len == (int)(path_len - 1), "sanity");
248
// check if file exists
249
struct stat st;
250
if (os::stat(path, &st) == 0) {
251
// found file, open it
252
int file_handle = os::open(path, 0, 0);
253
if (file_handle != -1) {
254
// read contents into resource array
255
u1* buffer = NEW_RESOURCE_ARRAY(u1, st.st_size);
256
size_t num_read = os::read(file_handle, (char*) buffer, st.st_size);
257
// close file
258
os::close(file_handle);
259
// construct ClassFileStream
260
if (num_read == (size_t)st.st_size) {
261
if (UsePerfData) {
262
ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read);
263
}
264
FREE_RESOURCE_ARRAY(char, path, path_len);
265
// Resource allocated
266
return new ClassFileStream(buffer,
267
st.st_size,
268
_dir,
269
ClassFileStream::verify);
270
}
271
}
272
}
273
FREE_RESOURCE_ARRAY(char, path, path_len);
274
return NULL;
275
}
276
277
ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name,
278
bool is_boot_append, bool from_class_path_attr) : ClassPathEntry() {
279
_zip = zip;
280
_zip_name = copy_path(zip_name);
281
_from_class_path_attr = from_class_path_attr;
282
}
283
284
ClassPathZipEntry::~ClassPathZipEntry() {
285
(*ZipClose)(_zip);
286
FREE_C_HEAP_ARRAY(char, _zip_name);
287
}
288
289
u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
290
// enable call to C land
291
ThreadToNativeFromVM ttn(current);
292
// check whether zip archive contains name
293
jint name_len;
294
jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len);
295
if (entry == NULL) return NULL;
296
u1* buffer;
297
char name_buf[128];
298
char* filename;
299
if (name_len < 128) {
300
filename = name_buf;
301
} else {
302
filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
303
}
304
305
// read contents into resource array
306
int size = (*filesize) + ((nul_terminate) ? 1 : 0);
307
buffer = NEW_RESOURCE_ARRAY(u1, size);
308
if (!(*ReadEntry)(_zip, entry, buffer, filename)) return NULL;
309
310
// return result
311
if (nul_terminate) {
312
buffer[*filesize] = 0;
313
}
314
return buffer;
315
}
316
317
ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
318
jint filesize;
319
u1* buffer = open_entry(current, name, &filesize, false);
320
if (buffer == NULL) {
321
return NULL;
322
}
323
if (UsePerfData) {
324
ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
325
}
326
// Resource allocated
327
return new ClassFileStream(buffer,
328
filesize,
329
_zip_name,
330
ClassFileStream::verify);
331
}
332
333
// invoke function for each entry in the zip file
334
void ClassPathZipEntry::contents_do(void f(const char* name, void* context), void* context) {
335
JavaThread* thread = JavaThread::current();
336
HandleMark handle_mark(thread);
337
ThreadToNativeFromVM ttn(thread);
338
for (int n = 0; ; n++) {
339
jzentry * ze = ((*GetNextEntry)(_zip, n));
340
if (ze == NULL) break;
341
(*f)(ze->name, context);
342
}
343
}
344
345
DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = NULL;)
346
347
JImageFile* ClassPathImageEntry::jimage() const {
348
return JImage_file;
349
}
350
351
JImageFile* ClassPathImageEntry::jimage_non_null() const {
352
assert(ClassLoader::has_jrt_entry(), "must be");
353
assert(jimage() != NULL, "should have been opened by ClassLoader::lookup_vm_options "
354
"and remained throughout normal JVM lifetime");
355
return jimage();
356
}
357
358
void ClassPathImageEntry::close_jimage() {
359
if (jimage() != NULL) {
360
(*JImageClose)(jimage());
361
JImage_file = NULL;
362
}
363
}
364
365
ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) :
366
ClassPathEntry() {
367
guarantee(jimage != NULL, "jimage file is null");
368
guarantee(name != NULL, "jimage file name is null");
369
assert(_singleton == NULL, "VM supports only one jimage");
370
DEBUG_ONLY(_singleton = this);
371
size_t len = strlen(name) + 1;
372
_name = copy_path(name);
373
}
374
375
ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {
376
return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());
377
}
378
379
// For a class in a named module, look it up in the jimage file using this syntax:
380
// /<module-name>/<package-name>/<base-class>
381
//
382
// Assumptions:
383
// 1. There are no unnamed modules in the jimage file.
384
// 2. A package is in at most one module in the jimage file.
385
//
386
ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
387
jlong size;
388
JImageLocationRef location = (*JImageFindResource)(jimage_non_null(), "", get_jimage_version_string(), name, &size);
389
390
if (location == 0) {
391
TempNewSymbol class_name = SymbolTable::new_symbol(name);
392
TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
393
394
if (pkg_name != NULL) {
395
if (!Universe::is_module_initialized()) {
396
location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size);
397
} else {
398
PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
399
if (package_entry != NULL) {
400
ResourceMark rm(current);
401
// Get the module name
402
ModuleEntry* module = package_entry->module();
403
assert(module != NULL, "Boot classLoader package missing module");
404
assert(module->is_named(), "Boot classLoader package is in unnamed module");
405
const char* module_name = module->name()->as_C_string();
406
if (module_name != NULL) {
407
location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size);
408
}
409
}
410
}
411
}
412
}
413
if (location != 0) {
414
if (UsePerfData) {
415
ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
416
}
417
char* data = NEW_RESOURCE_ARRAY(char, size);
418
(*JImageGetResource)(jimage_non_null(), location, data, size);
419
// Resource allocated
420
assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");
421
return new ClassFileStream((u1*)data,
422
(int)size,
423
_name,
424
ClassFileStream::verify,
425
true); // from_boot_loader_modules_image
426
}
427
428
return NULL;
429
}
430
431
JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf,
432
const char* module_name,
433
const char* file_name,
434
jlong &size) {
435
return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size));
436
}
437
438
bool ClassPathImageEntry::is_modules_image() const {
439
assert(this == _singleton, "VM supports a single jimage");
440
assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry");
441
return true;
442
}
443
444
#if INCLUDE_CDS
445
void ClassLoader::exit_with_path_failure(const char* error, const char* message) {
446
Arguments::assert_is_dumping_archive();
447
tty->print_cr("Hint: enable -Xlog:class+path=info to diagnose the failure");
448
vm_exit_during_initialization(error, message);
449
}
450
#endif
451
452
ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
453
_module_name = module_name;
454
_module_first_entry = NULL;
455
_module_last_entry = NULL;
456
}
457
458
ModuleClassPathList::~ModuleClassPathList() {
459
// Clean out each ClassPathEntry on list
460
ClassPathEntry* e = _module_first_entry;
461
while (e != NULL) {
462
ClassPathEntry* next_entry = e->next();
463
delete e;
464
e = next_entry;
465
}
466
}
467
468
void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {
469
if (new_entry != NULL) {
470
if (_module_last_entry == NULL) {
471
_module_first_entry = _module_last_entry = new_entry;
472
} else {
473
_module_last_entry->set_next(new_entry);
474
_module_last_entry = new_entry;
475
}
476
}
477
}
478
479
void ClassLoader::trace_class_path(const char* msg, const char* name) {
480
LogTarget(Info, class, path) lt;
481
if (lt.is_enabled()) {
482
LogStream ls(lt);
483
if (msg) {
484
ls.print("%s", msg);
485
}
486
if (name) {
487
if (strlen(name) < 256) {
488
ls.print("%s", name);
489
} else {
490
// For very long paths, we need to print each character separately,
491
// as print_cr() has a length limit
492
while (name[0] != '\0') {
493
ls.print("%c", name[0]);
494
name++;
495
}
496
}
497
}
498
ls.cr();
499
}
500
}
501
502
void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {
503
const char* sys_class_path = Arguments::get_sysclasspath();
504
assert(sys_class_path != NULL, "System boot class path must not be NULL");
505
if (PrintSharedArchiveAndExit) {
506
// Don't print sys_class_path - this is the bootcp of this current VM process, not necessarily
507
// the same as the bootcp of the shared archive.
508
} else {
509
trace_class_path("bootstrap loader class path=", sys_class_path);
510
}
511
setup_bootstrap_search_path_impl(current, sys_class_path);
512
}
513
514
#if INCLUDE_CDS
515
void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {
516
Arguments::assert_is_dumping_archive();
517
518
ResourceMark rm(current);
519
ClasspathStream cp_stream(class_path);
520
521
while (cp_stream.has_next()) {
522
const char* path = cp_stream.get_next();
523
update_class_path_entry_list(current, path, false, false, false);
524
}
525
}
526
527
void ClassLoader::add_to_module_path_entries(const char* path,
528
ClassPathEntry* entry) {
529
assert(entry != NULL, "ClassPathEntry should not be NULL");
530
Arguments::assert_is_dumping_archive();
531
532
// The entry does not exist, add to the list
533
if (_module_path_entries == NULL) {
534
assert(_last_module_path_entry == NULL, "Sanity");
535
_module_path_entries = _last_module_path_entry = entry;
536
} else {
537
_last_module_path_entry->set_next(entry);
538
_last_module_path_entry = entry;
539
}
540
}
541
542
// Add a module path to the _module_path_entries list.
543
void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {
544
Arguments::assert_is_dumping_archive();
545
struct stat st;
546
if (os::stat(path, &st) != 0) {
547
tty->print_cr("os::stat error %d (%s). CDS dump aborted (path was \"%s\").",
548
errno, os::errno_name(errno), path);
549
vm_exit_during_initialization();
550
}
551
// File or directory found
552
ClassPathEntry* new_entry = NULL;
553
new_entry = create_class_path_entry(current, path, &st,
554
false /*is_boot_append */, false /* from_class_path_attr */);
555
if (new_entry != NULL) {
556
add_to_module_path_entries(path, new_entry);
557
}
558
}
559
560
#endif // INCLUDE_CDS
561
562
void ClassLoader::close_jrt_image() {
563
// Not applicable for exploded builds
564
if (!ClassLoader::has_jrt_entry()) return;
565
_jrt_entry->close_jimage();
566
}
567
568
// Construct the array of module/path pairs as specified to --patch-module
569
// for the boot loader to search ahead of the jimage, if the class being
570
// loaded is defined to a module that has been specified to --patch-module.
571
void ClassLoader::setup_patch_mod_entries() {
572
JavaThread* current = JavaThread::current();
573
GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
574
int num_of_entries = patch_mod_args->length();
575
576
// Set up the boot loader's _patch_mod_entries list
577
_patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, mtModule);
578
579
for (int i = 0; i < num_of_entries; i++) {
580
const char* module_name = (patch_mod_args->at(i))->module_name();
581
Symbol* const module_sym = SymbolTable::new_symbol(module_name);
582
assert(module_sym != NULL, "Failed to obtain Symbol for module name");
583
ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
584
585
char* class_path = (patch_mod_args->at(i))->path_string();
586
ResourceMark rm(current);
587
ClasspathStream cp_stream(class_path);
588
589
while (cp_stream.has_next()) {
590
const char* path = cp_stream.get_next();
591
struct stat st;
592
if (os::stat(path, &st) == 0) {
593
// File or directory found
594
ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);
595
// If the path specification is valid, enter it into this module's list
596
if (new_entry != NULL) {
597
module_cpl->add_to_list(new_entry);
598
}
599
}
600
}
601
602
// Record the module into the list of --patch-module entries only if
603
// valid ClassPathEntrys have been created
604
if (module_cpl->module_first_entry() != NULL) {
605
_patch_mod_entries->push(module_cpl);
606
}
607
}
608
}
609
610
// Determine whether the module has been patched via the command-line
611
// option --patch-module
612
bool ClassLoader::is_in_patch_mod_entries(Symbol* module_name) {
613
if (_patch_mod_entries != NULL && _patch_mod_entries->is_nonempty()) {
614
int table_len = _patch_mod_entries->length();
615
for (int i = 0; i < table_len; i++) {
616
ModuleClassPathList* patch_mod = _patch_mod_entries->at(i);
617
if (module_name->fast_compare(patch_mod->module_name()) == 0) {
618
return true;
619
}
620
}
621
}
622
return false;
623
}
624
625
// Set up the _jrt_entry if present and boot append path
626
void ClassLoader::setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path) {
627
ResourceMark rm(current);
628
ClasspathStream cp_stream(class_path);
629
bool set_base_piece = true;
630
631
#if INCLUDE_CDS
632
if (Arguments::is_dumping_archive()) {
633
if (!Arguments::has_jimage()) {
634
vm_exit_during_initialization("CDS is not supported in exploded JDK build", NULL);
635
}
636
}
637
#endif
638
639
while (cp_stream.has_next()) {
640
const char* path = cp_stream.get_next();
641
642
if (set_base_piece) {
643
// The first time through the bootstrap_search setup, it must be determined
644
// what the base or core piece of the boot loader search is. Either a java runtime
645
// image is present or this is an exploded module build situation.
646
assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
647
"Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
648
struct stat st;
649
if (os::stat(path, &st) == 0) {
650
// Directory found
651
if (JImage_file != NULL) {
652
assert(Arguments::has_jimage(), "sanity check");
653
const char* canonical_path = get_canonical_path(path, current);
654
assert(canonical_path != NULL, "canonical_path issue");
655
656
_jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path);
657
assert(_jrt_entry != NULL && _jrt_entry->is_modules_image(), "No java runtime image present");
658
assert(_jrt_entry->jimage() != NULL, "No java runtime image");
659
} else {
660
// It's an exploded build.
661
ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);
662
}
663
} else {
664
// If path does not exist, exit
665
vm_exit_during_initialization("Unable to establish the boot loader search path", path);
666
}
667
set_base_piece = false;
668
} else {
669
// Every entry on the system boot class path after the initial base piece,
670
// which is set by os::set_boot_path(), is considered an appended entry.
671
update_class_path_entry_list(current, path, false, true, false);
672
}
673
}
674
}
675
676
// During an exploded modules build, each module defined to the boot loader
677
// will be added to the ClassLoader::_exploded_entries array.
678
void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {
679
assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");
680
assert(_exploded_entries != NULL, "_exploded_entries was not initialized");
681
682
// Find the module's symbol
683
ResourceMark rm(current);
684
const char *module_name = module_sym->as_C_string();
685
const char *home = Arguments::get_java_home();
686
const char file_sep = os::file_separator()[0];
687
// 10 represents the length of "modules" + 2 file separators + \0
688
size_t len = strlen(home) + strlen(module_name) + 10;
689
char *path = NEW_RESOURCE_ARRAY(char, len);
690
jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);
691
692
struct stat st;
693
if (os::stat(path, &st) == 0) {
694
// Directory found
695
ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);
696
697
// If the path specification is valid, enter it into this module's list.
698
// There is no need to check for duplicate modules in the exploded entry list,
699
// since no two modules with the same name can be defined to the boot loader.
700
// This is checked at module definition time in Modules::define_module.
701
if (new_entry != NULL) {
702
ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
703
module_cpl->add_to_list(new_entry);
704
{
705
MutexLocker ml(current, Module_lock);
706
_exploded_entries->push(module_cpl);
707
}
708
log_info(class, load)("path: %s", path);
709
}
710
}
711
}
712
713
jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {
714
// enable call to C land
715
ThreadToNativeFromVM ttn(thread);
716
HandleMark hm(thread);
717
load_zip_library_if_needed();
718
return (*ZipOpen)(canonical_path, error_msg);
719
}
720
721
ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
722
const char *path, const struct stat* st,
723
bool is_boot_append,
724
bool from_class_path_attr) {
725
ClassPathEntry* new_entry = NULL;
726
if ((st->st_mode & S_IFMT) == S_IFREG) {
727
ResourceMark rm(current);
728
// Regular file, should be a zip file
729
// Canonicalized filename
730
const char* canonical_path = get_canonical_path(path, current);
731
if (canonical_path == NULL) {
732
return NULL;
733
}
734
char* error_msg = NULL;
735
jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
736
if (zip != NULL && error_msg == NULL) {
737
new_entry = new ClassPathZipEntry(zip, path, is_boot_append, from_class_path_attr);
738
} else {
739
return NULL;
740
}
741
log_info(class, path)("opened: %s", path);
742
log_info(class, load)("opened: %s", path);
743
} else {
744
// Directory
745
new_entry = new ClassPathDirEntry(path);
746
log_info(class, load)("path: %s", path);
747
}
748
return new_entry;
749
}
750
751
752
// Create a class path zip entry for a given path (return NULL if not found
753
// or zip/JAR file cannot be opened)
754
ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
755
// check for a regular file
756
struct stat st;
757
if (os::stat(path, &st) == 0) {
758
if ((st.st_mode & S_IFMT) == S_IFREG) {
759
JavaThread* thread = JavaThread::current();
760
ResourceMark rm(thread);
761
const char* canonical_path = get_canonical_path(path, thread);
762
if (canonical_path != NULL) {
763
char* error_msg = NULL;
764
jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
765
if (zip != NULL && error_msg == NULL) {
766
// create using canonical path
767
return new ClassPathZipEntry(zip, canonical_path, is_boot_append, false);
768
}
769
}
770
}
771
}
772
return NULL;
773
}
774
775
// The boot append entries are added with a lock, and read lock free.
776
void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
777
if (new_entry != NULL) {
778
MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
779
if (_last_append_entry == NULL) {
780
_last_append_entry = new_entry;
781
assert(first_append_entry() == NULL, "boot loader's append class path entry list not empty");
782
Atomic::release_store(&_first_append_entry_list, new_entry);
783
} else {
784
_last_append_entry->set_next(new_entry);
785
_last_append_entry = new_entry;
786
}
787
}
788
}
789
790
// Record the path entries specified in -cp during dump time. The recorded
791
// information will be used at runtime for loading the archived app classes.
792
//
793
// Note that at dump time, ClassLoader::_app_classpath_entries are NOT used for
794
// loading app classes. Instead, the app class are loaded by the
795
// jdk/internal/loader/ClassLoaders$AppClassLoader instance.
796
void ClassLoader::add_to_app_classpath_entries(JavaThread* current,
797
const char* path,
798
ClassPathEntry* entry,
799
bool check_for_duplicates) {
800
#if INCLUDE_CDS
801
assert(entry != NULL, "ClassPathEntry should not be NULL");
802
ClassPathEntry* e = _app_classpath_entries;
803
if (check_for_duplicates) {
804
while (e != NULL) {
805
if (strcmp(e->name(), entry->name()) == 0) {
806
// entry already exists
807
return;
808
}
809
e = e->next();
810
}
811
}
812
813
// The entry does not exist, add to the list
814
if (_app_classpath_entries == NULL) {
815
assert(_last_app_classpath_entry == NULL, "Sanity");
816
_app_classpath_entries = _last_app_classpath_entry = entry;
817
} else {
818
_last_app_classpath_entry->set_next(entry);
819
_last_app_classpath_entry = entry;
820
}
821
822
if (entry->is_jar_file()) {
823
ClassLoaderExt::process_jar_manifest(current, entry, check_for_duplicates);
824
}
825
#endif
826
}
827
828
// Returns true IFF the file/dir exists and the entry was successfully created.
829
bool ClassLoader::update_class_path_entry_list(JavaThread* current,
830
const char *path,
831
bool check_for_duplicates,
832
bool is_boot_append,
833
bool from_class_path_attr) {
834
struct stat st;
835
if (os::stat(path, &st) == 0) {
836
// File or directory found
837
ClassPathEntry* new_entry = NULL;
838
new_entry = create_class_path_entry(current, path, &st, is_boot_append, from_class_path_attr);
839
if (new_entry == NULL) {
840
return false;
841
}
842
843
// Do not reorder the bootclasspath which would break get_system_package().
844
// Add new entry to linked list
845
if (is_boot_append) {
846
add_to_boot_append_entries(new_entry);
847
} else {
848
add_to_app_classpath_entries(current, path, new_entry, check_for_duplicates);
849
}
850
return true;
851
} else {
852
return false;
853
}
854
}
855
856
static void print_module_entry_table(const GrowableArray<ModuleClassPathList*>* const module_list) {
857
ResourceMark rm;
858
int num_of_entries = module_list->length();
859
for (int i = 0; i < num_of_entries; i++) {
860
ClassPathEntry* e;
861
ModuleClassPathList* mpl = module_list->at(i);
862
tty->print("%s=", mpl->module_name()->as_C_string());
863
e = mpl->module_first_entry();
864
while (e != NULL) {
865
tty->print("%s", e->name());
866
e = e->next();
867
if (e != NULL) {
868
tty->print("%s", os::path_separator());
869
}
870
}
871
tty->print(" ;");
872
}
873
}
874
875
void ClassLoader::print_bootclasspath() {
876
ClassPathEntry* e;
877
tty->print("[bootclasspath= ");
878
879
// Print --patch-module module/path specifications first
880
if (_patch_mod_entries != NULL) {
881
print_module_entry_table(_patch_mod_entries);
882
}
883
884
// [jimage | exploded modules build]
885
if (has_jrt_entry()) {
886
// Print the location of the java runtime image
887
tty->print("%s ;", _jrt_entry->name());
888
} else {
889
// Print exploded module build path specifications
890
if (_exploded_entries != NULL) {
891
print_module_entry_table(_exploded_entries);
892
}
893
}
894
895
// appended entries
896
e = first_append_entry();
897
while (e != NULL) {
898
tty->print("%s ;", e->name());
899
e = e->next();
900
}
901
tty->print_cr("]");
902
}
903
904
void* ClassLoader::dll_lookup(void* lib, const char* name, const char* path) {
905
void* func = os::dll_lookup(lib, name);
906
if (func == NULL) {
907
char msg[256] = "";
908
jio_snprintf(msg, sizeof(msg), "Could not resolve \"%s\"", name);
909
vm_exit_during_initialization(msg, path);
910
}
911
return func;
912
}
913
914
void ClassLoader::load_java_library() {
915
assert(CanonicalizeEntry == NULL, "should not load java library twice");
916
void *javalib_handle = os::native_java_library();
917
if (javalib_handle == NULL) {
918
vm_exit_during_initialization("Unable to load java library", NULL);
919
}
920
921
CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, dll_lookup(javalib_handle, "JDK_Canonicalize", NULL));
922
}
923
924
void ClassLoader::release_load_zip_library() {
925
MutexLocker locker(Zip_lock, Monitor::_no_safepoint_check_flag);
926
if (_libzip_loaded == 0) {
927
load_zip_library();
928
Atomic::release_store(&_libzip_loaded, 1);
929
}
930
}
931
932
void ClassLoader::load_zip_library() {
933
assert(ZipOpen == NULL, "should not load zip library twice");
934
char path[JVM_MAXPATHLEN];
935
char ebuf[1024];
936
void* handle = NULL;
937
if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {
938
handle = os::dll_load(path, ebuf, sizeof ebuf);
939
}
940
if (handle == NULL) {
941
vm_exit_during_initialization("Unable to load zip library", path);
942
}
943
944
ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, dll_lookup(handle, "ZIP_Open", path));
945
ZipClose = CAST_TO_FN_PTR(ZipClose_t, dll_lookup(handle, "ZIP_Close", path));
946
FindEntry = CAST_TO_FN_PTR(FindEntry_t, dll_lookup(handle, "ZIP_FindEntry", path));
947
ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, dll_lookup(handle, "ZIP_ReadEntry", path));
948
GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, dll_lookup(handle, "ZIP_GetNextEntry", path));
949
Crc32 = CAST_TO_FN_PTR(Crc32_t, dll_lookup(handle, "ZIP_CRC32", path));
950
}
951
952
void ClassLoader::load_jimage_library() {
953
assert(JImageOpen == NULL, "should not load jimage library twice");
954
char path[JVM_MAXPATHLEN];
955
char ebuf[1024];
956
void* handle = NULL;
957
if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) {
958
handle = os::dll_load(path, ebuf, sizeof ebuf);
959
}
960
if (handle == NULL) {
961
vm_exit_during_initialization("Unable to load jimage library", path);
962
}
963
964
JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, dll_lookup(handle, "JIMAGE_Open", path));
965
JImageClose = CAST_TO_FN_PTR(JImageClose_t, dll_lookup(handle, "JIMAGE_Close", path));
966
JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, dll_lookup(handle, "JIMAGE_FindResource", path));
967
JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));
968
}
969
970
int ClassLoader::crc32(int crc, const char* buf, int len) {
971
load_zip_library_if_needed();
972
return (*Crc32)(crc, (const jbyte*)buf, len);
973
}
974
975
oop ClassLoader::get_system_package(const char* name, TRAPS) {
976
// Look up the name in the boot loader's package entry table.
977
if (name != NULL) {
978
TempNewSymbol package_sym = SymbolTable::new_symbol(name);
979
// Look for the package entry in the boot loader's package entry table.
980
PackageEntry* package =
981
ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym);
982
983
// Return NULL if package does not exist or if no classes in that package
984
// have been loaded.
985
if (package != NULL && package->has_loaded_class()) {
986
ModuleEntry* module = package->module();
987
if (module->location() != NULL) {
988
ResourceMark rm(THREAD);
989
Handle ml = java_lang_String::create_from_str(
990
module->location()->as_C_string(), THREAD);
991
return ml();
992
}
993
// Return entry on boot loader class path.
994
Handle cph = java_lang_String::create_from_str(
995
ClassLoader::classpath_entry(package->classpath_index())->name(), THREAD);
996
return cph();
997
}
998
}
999
return NULL;
1000
}
1001
1002
objArrayOop ClassLoader::get_system_packages(TRAPS) {
1003
ResourceMark rm(THREAD);
1004
// List of pointers to PackageEntrys that have loaded classes.
1005
GrowableArray<PackageEntry*>* loaded_class_pkgs = new GrowableArray<PackageEntry*>(50);
1006
{
1007
MutexLocker ml(THREAD, Module_lock);
1008
1009
PackageEntryTable* pe_table =
1010
ClassLoaderData::the_null_class_loader_data()->packages();
1011
1012
// Collect the packages that have at least one loaded class.
1013
for (int x = 0; x < pe_table->table_size(); x++) {
1014
for (PackageEntry* package_entry = pe_table->bucket(x);
1015
package_entry != NULL;
1016
package_entry = package_entry->next()) {
1017
if (package_entry->has_loaded_class()) {
1018
loaded_class_pkgs->append(package_entry);
1019
}
1020
}
1021
}
1022
}
1023
1024
1025
// Allocate objArray and fill with java.lang.String
1026
objArrayOop r = oopFactory::new_objArray(vmClasses::String_klass(),
1027
loaded_class_pkgs->length(), CHECK_NULL);
1028
objArrayHandle result(THREAD, r);
1029
for (int x = 0; x < loaded_class_pkgs->length(); x++) {
1030
PackageEntry* package_entry = loaded_class_pkgs->at(x);
1031
Handle str = java_lang_String::create_from_symbol(package_entry->name(), CHECK_NULL);
1032
result->obj_at_put(x, str());
1033
}
1034
return result();
1035
}
1036
1037
// caller needs ResourceMark
1038
const char* ClassLoader::file_name_for_class_name(const char* class_name,
1039
int class_name_len) {
1040
assert(class_name != NULL, "invariant");
1041
assert((int)strlen(class_name) == class_name_len, "invariant");
1042
1043
static const char class_suffix[] = ".class";
1044
size_t class_suffix_len = sizeof(class_suffix);
1045
1046
char* const file_name = NEW_RESOURCE_ARRAY(char,
1047
class_name_len +
1048
class_suffix_len); // includes term NULL
1049
1050
strncpy(file_name, class_name, class_name_len);
1051
strncpy(&file_name[class_name_len], class_suffix, class_suffix_len);
1052
1053
return file_name;
1054
}
1055
1056
ClassPathEntry* find_first_module_cpe(ModuleEntry* mod_entry,
1057
const GrowableArray<ModuleClassPathList*>* const module_list) {
1058
int num_of_entries = module_list->length();
1059
const Symbol* class_module_name = mod_entry->name();
1060
1061
// Loop through all the modules in either the patch-module or exploded entries looking for module
1062
for (int i = 0; i < num_of_entries; i++) {
1063
ModuleClassPathList* module_cpl = module_list->at(i);
1064
Symbol* module_cpl_name = module_cpl->module_name();
1065
1066
if (module_cpl_name->fast_compare(class_module_name) == 0) {
1067
// Class' module has been located.
1068
return module_cpl->module_first_entry();
1069
}
1070
}
1071
return NULL;
1072
}
1073
1074
1075
// Search either the patch-module or exploded build entries for class.
1076
ClassFileStream* ClassLoader::search_module_entries(JavaThread* current,
1077
const GrowableArray<ModuleClassPathList*>* const module_list,
1078
const char* const class_name,
1079
const char* const file_name) {
1080
ClassFileStream* stream = NULL;
1081
1082
// Find the class' defining module in the boot loader's module entry table
1083
TempNewSymbol class_name_symbol = SymbolTable::new_symbol(class_name);
1084
TempNewSymbol pkg_name = package_from_class_name(class_name_symbol);
1085
PackageEntry* pkg_entry = get_package_entry(pkg_name, ClassLoaderData::the_null_class_loader_data());
1086
ModuleEntry* mod_entry = (pkg_entry != NULL) ? pkg_entry->module() : NULL;
1087
1088
// If the module system has not defined java.base yet, then
1089
// classes loaded are assumed to be defined to java.base.
1090
// When java.base is eventually defined by the module system,
1091
// all packages of classes that have been previously loaded
1092
// are verified in ModuleEntryTable::verify_javabase_packages().
1093
if (!Universe::is_module_initialized() &&
1094
!ModuleEntryTable::javabase_defined() &&
1095
mod_entry == NULL) {
1096
mod_entry = ModuleEntryTable::javabase_moduleEntry();
1097
}
1098
1099
// The module must be a named module
1100
ClassPathEntry* e = NULL;
1101
if (mod_entry != NULL && mod_entry->is_named()) {
1102
if (module_list == _exploded_entries) {
1103
// The exploded build entries can be added to at any time so a lock is
1104
// needed when searching them.
1105
assert(!ClassLoader::has_jrt_entry(), "Must be exploded build");
1106
MutexLocker ml(current, Module_lock);
1107
e = find_first_module_cpe(mod_entry, module_list);
1108
} else {
1109
e = find_first_module_cpe(mod_entry, module_list);
1110
}
1111
}
1112
1113
// Try to load the class from the module's ClassPathEntry list.
1114
while (e != NULL) {
1115
stream = e->open_stream(current, file_name);
1116
// No context.check is required since CDS is not supported
1117
// for an exploded modules build or if --patch-module is specified.
1118
if (NULL != stream) {
1119
return stream;
1120
}
1121
e = e->next();
1122
}
1123
// If the module was located, break out even if the class was not
1124
// located successfully from that module's ClassPathEntry list.
1125
// There will not be another valid entry for that module.
1126
return NULL;
1127
}
1128
1129
// Called by the boot classloader to load classes
1130
InstanceKlass* ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {
1131
assert(name != NULL, "invariant");
1132
1133
ResourceMark rm(THREAD);
1134
HandleMark hm(THREAD);
1135
1136
const char* const class_name = name->as_C_string();
1137
1138
EventMark m("loading class %s", class_name);
1139
1140
const char* const file_name = file_name_for_class_name(class_name,
1141
name->utf8_length());
1142
assert(file_name != NULL, "invariant");
1143
1144
// Lookup stream for parsing .class file
1145
ClassFileStream* stream = NULL;
1146
s2 classpath_index = 0;
1147
ClassPathEntry* e = NULL;
1148
1149
// If search_append_only is true, boot loader visibility boundaries are
1150
// set to be _first_append_entry to the end. This includes:
1151
// [-Xbootclasspath/a]; [jvmti appended entries]
1152
//
1153
// If search_append_only is false, boot loader visibility boundaries are
1154
// set to be the --patch-module entries plus the base piece. This includes:
1155
// [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1156
//
1157
1158
// Load Attempt #1: --patch-module
1159
// Determine the class' defining module. If it appears in the _patch_mod_entries,
1160
// attempt to load the class from those locations specific to the module.
1161
// Specifications to --patch-module can contain a partial number of classes
1162
// that are part of the overall module definition. So if a particular class is not
1163
// found within its module specification, the search should continue to Load Attempt #2.
1164
// Note: The --patch-module entries are never searched if the boot loader's
1165
// visibility boundary is limited to only searching the append entries.
1166
if (_patch_mod_entries != NULL && !search_append_only) {
1167
// At CDS dump time, the --patch-module entries are ignored. That means a
1168
// class is still loaded from the runtime image even if it might
1169
// appear in the _patch_mod_entries. The runtime shared class visibility
1170
// check will determine if a shared class is visible based on the runtime
1171
// environemnt, including the runtime --patch-module setting.
1172
//
1173
// DynamicDumpSharedSpaces requires UseSharedSpaces to be enabled. Since --patch-module
1174
// is not supported with UseSharedSpaces, it is not supported with DynamicDumpSharedSpaces.
1175
assert(!DynamicDumpSharedSpaces, "sanity");
1176
if (!DumpSharedSpaces) {
1177
stream = search_module_entries(THREAD, _patch_mod_entries, class_name, file_name);
1178
}
1179
}
1180
1181
// Load Attempt #2: [jimage | exploded build]
1182
if (!search_append_only && (NULL == stream)) {
1183
if (has_jrt_entry()) {
1184
e = _jrt_entry;
1185
stream = _jrt_entry->open_stream(THREAD, file_name);
1186
} else {
1187
// Exploded build - attempt to locate class in its defining module's location.
1188
assert(_exploded_entries != NULL, "No exploded build entries present");
1189
stream = search_module_entries(THREAD, _exploded_entries, class_name, file_name);
1190
}
1191
}
1192
1193
// Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1194
if (search_append_only && (NULL == 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.
1198
assert(classpath_index == 0, "The classpath_index has been incremented incorrectly");
1199
classpath_index = 1;
1200
1201
e = first_append_entry();
1202
while (e != NULL) {
1203
stream = e->open_stream(THREAD, file_name);
1204
if (NULL != stream) {
1205
break;
1206
}
1207
e = e->next();
1208
++classpath_index;
1209
}
1210
}
1211
1212
if (NULL == stream) {
1213
return NULL;
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
return result;
1229
}
1230
1231
#if INCLUDE_CDS
1232
char* ClassLoader::skip_uri_protocol(char* source) {
1233
if (strncmp(source, "file:", 5) == 0) {
1234
// file: protocol path could start with file:/ or file:///
1235
// locate the char after all the forward slashes
1236
int offset = 5;
1237
while (*(source + offset) == '/') {
1238
offset++;
1239
}
1240
source += offset;
1241
// for non-windows platforms, move back one char as the path begins with a '/'
1242
#ifndef _WINDOWS
1243
source -= 1;
1244
#endif
1245
} else if (strncmp(source, "jrt:/", 5) == 0) {
1246
source += 5;
1247
}
1248
return source;
1249
}
1250
1251
// Record the shared classpath index and loader type for classes loaded
1252
// by the builtin loaders at dump time.
1253
void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik, const ClassFileStream* stream) {
1254
Arguments::assert_is_dumping_archive();
1255
assert(stream != NULL, "sanity");
1256
1257
if (ik->is_hidden()) {
1258
// We do not archive hidden classes.
1259
return;
1260
}
1261
1262
oop loader = ik->class_loader();
1263
char* src = (char*)stream->source();
1264
if (src == NULL) {
1265
if (loader == NULL) {
1266
// JFR classes
1267
ik->set_shared_classpath_index(0);
1268
ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);
1269
}
1270
return;
1271
}
1272
1273
assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1274
1275
ResourceMark rm(current);
1276
int classpath_index = -1;
1277
PackageEntry* pkg_entry = ik->package();
1278
1279
if (FileMapInfo::get_number_of_shared_paths() > 0) {
1280
// Save the path from the file: protocol or the module name from the jrt: protocol
1281
// if no protocol prefix is found, path is the same as stream->source(). This path
1282
// must be valid since the class has been successfully parsed.
1283
char* path = skip_uri_protocol(src);
1284
assert(path != NULL, "sanity");
1285
for (int i = 0; i < FileMapInfo::get_number_of_shared_paths(); i++) {
1286
SharedClassPathEntry* ent = FileMapInfo::shared_path(i);
1287
// A shared path has been validated during its creation in ClassLoader::create_class_path_entry(),
1288
// it must be valid here.
1289
assert(ent->name() != NULL, "sanity");
1290
// If the path (from the class stream source) is the same as the shared
1291
// class or module path, then we have a match.
1292
// src may come from the App/Platform class loaders, which would canonicalize
1293
// the file name. We cannot use strcmp to check for equality against ent->name().
1294
// We must use os::same_files (which is faster than canonicalizing ent->name()).
1295
if (os::same_files(ent->name(), path)) {
1296
// NULL pkg_entry and pkg_entry in an unnamed module implies the class
1297
// is from the -cp or boot loader append path which consists of -Xbootclasspath/a
1298
// and jvmti appended entries.
1299
if ((pkg_entry == NULL) || (pkg_entry->in_unnamed_module())) {
1300
// Ensure the index is within the -cp range before assigning
1301
// to the classpath_index.
1302
if (SystemDictionary::is_system_class_loader(loader) &&
1303
(i >= ClassLoaderExt::app_class_paths_start_index()) &&
1304
(i < ClassLoaderExt::app_module_paths_start_index())) {
1305
classpath_index = i;
1306
break;
1307
} else {
1308
if ((i >= 1) &&
1309
(i < ClassLoaderExt::app_class_paths_start_index())) {
1310
// The class must be from boot loader append path which consists of
1311
// -Xbootclasspath/a and jvmti appended entries.
1312
assert(loader == NULL, "sanity");
1313
classpath_index = i;
1314
break;
1315
}
1316
}
1317
} else {
1318
// A class from a named module from the --module-path. Ensure the index is
1319
// within the --module-path range before assigning to the classpath_index.
1320
if ((pkg_entry != NULL) && !(pkg_entry->in_unnamed_module()) && (i > 0)) {
1321
if (i >= ClassLoaderExt::app_module_paths_start_index() &&
1322
i < FileMapInfo::get_number_of_shared_paths()) {
1323
classpath_index = i;
1324
break;
1325
}
1326
}
1327
}
1328
}
1329
// for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1330
// The class must be from the runtime modules image.
1331
if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {
1332
classpath_index = i;
1333
break;
1334
}
1335
}
1336
1337
// No path entry found for this class. Must be a shared class loaded by the
1338
// user defined classloader.
1339
if (classpath_index < 0) {
1340
assert(ik->shared_classpath_index() < 0, "Sanity");
1341
ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1342
SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1343
return;
1344
}
1345
} else {
1346
// The shared path table is set up after module system initialization.
1347
// The path table contains no entry before that. Any classes loaded prior
1348
// to the setup of the shared path table must be from the modules image.
1349
assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");
1350
assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1351
classpath_index = 0;
1352
}
1353
1354
const char* const class_name = ik->name()->as_C_string();
1355
const char* const file_name = file_name_for_class_name(class_name,
1356
ik->name()->utf8_length());
1357
assert(file_name != NULL, "invariant");
1358
1359
ClassLoaderExt::record_result(classpath_index, ik);
1360
}
1361
#endif // INCLUDE_CDS
1362
1363
// Initialize the class loader's access to methods in libzip. Parse and
1364
// process the boot classpath into a list ClassPathEntry objects. Once
1365
// this list has been created, it must not change order (see class PackageInfo)
1366
// it can be appended to and is by jvmti.
1367
1368
void ClassLoader::initialize(TRAPS) {
1369
if (UsePerfData) {
1370
// jvmstat performance counters
1371
NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1372
NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1373
NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1374
NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1375
NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1376
NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
1377
NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
1378
NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
1379
NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
1380
NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
1381
1382
NEWPERFTICKCOUNTER(_perf_sys_class_lookup_time, SUN_CLS, "lookupSysClassTime");
1383
NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
1384
NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
1385
NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
1386
NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
1387
NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
1388
NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
1389
NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
1390
NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
1391
NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
1392
NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
1393
1394
NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");
1395
}
1396
1397
// lookup java library entry points
1398
load_java_library();
1399
// jimage library entry points are loaded below, in lookup_vm_options
1400
setup_bootstrap_search_path(THREAD);
1401
}
1402
1403
char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1404
jlong size;
1405
JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1406
if (location == 0)
1407
return NULL;
1408
char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1409
(*JImageGetResource)(jimage, location, val, size);
1410
val[size] = '\0';
1411
return val;
1412
}
1413
1414
// Lookup VM options embedded in the modules jimage file
1415
char* ClassLoader::lookup_vm_options() {
1416
jint error;
1417
char modules_path[JVM_MAXPATHLEN];
1418
const char* fileSep = os::file_separator();
1419
1420
// Initialize jimage library entry points
1421
load_jimage_library();
1422
1423
jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);
1424
JImage_file =(*JImageOpen)(modules_path, &error);
1425
if (JImage_file == NULL) {
1426
return NULL;
1427
}
1428
1429
const char *jimage_version = get_jimage_version_string();
1430
char *options = lookup_vm_resource(JImage_file, jimage_version, "jdk/internal/vm/options");
1431
return options;
1432
}
1433
1434
#if INCLUDE_CDS
1435
void ClassLoader::initialize_shared_path(JavaThread* current) {
1436
if (Arguments::is_dumping_archive()) {
1437
ClassLoaderExt::setup_search_paths(current);
1438
}
1439
}
1440
1441
void ClassLoader::initialize_module_path(TRAPS) {
1442
if (Arguments::is_dumping_archive()) {
1443
ClassLoaderExt::setup_module_paths(THREAD);
1444
FileMapInfo::allocate_shared_path_table(CHECK);
1445
}
1446
}
1447
1448
// Helper function used by CDS code to get the number of module path
1449
// entries during shared classpath setup time.
1450
int ClassLoader::num_module_path_entries() {
1451
Arguments::assert_is_dumping_archive();
1452
int num_entries = 0;
1453
ClassPathEntry* e= ClassLoader::_module_path_entries;
1454
while (e != NULL) {
1455
num_entries ++;
1456
e = e->next();
1457
}
1458
return num_entries;
1459
}
1460
#endif
1461
1462
jlong ClassLoader::classloader_time_ms() {
1463
return UsePerfData ?
1464
Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1465
}
1466
1467
jlong ClassLoader::class_init_count() {
1468
return UsePerfData ? _perf_classes_inited->get_value() : -1;
1469
}
1470
1471
jlong ClassLoader::class_init_time_ms() {
1472
return UsePerfData ?
1473
Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1474
}
1475
1476
jlong ClassLoader::class_verify_time_ms() {
1477
return UsePerfData ?
1478
Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1479
}
1480
1481
jlong ClassLoader::class_link_count() {
1482
return UsePerfData ? _perf_classes_linked->get_value() : -1;
1483
}
1484
1485
jlong ClassLoader::class_link_time_ms() {
1486
return UsePerfData ?
1487
Management::ticks_to_ms(_perf_class_link_time->get_value()) : -1;
1488
}
1489
1490
int ClassLoader::compute_Object_vtable() {
1491
// hardwired for JDK1.2 -- would need to duplicate class file parsing
1492
// code to determine actual value from file
1493
// Would be value '11' if finals were in vtable
1494
int JDK_1_2_Object_vtable_size = 5;
1495
return JDK_1_2_Object_vtable_size * vtableEntry::size();
1496
}
1497
1498
1499
void classLoader_init1() {
1500
EXCEPTION_MARK;
1501
ClassLoader::initialize(THREAD);
1502
if (HAS_PENDING_EXCEPTION) {
1503
vm_exit_during_initialization("ClassLoader::initialize() failed unexpectedly");
1504
}
1505
}
1506
1507
// Complete the ClassPathEntry setup for the boot loader
1508
void ClassLoader::classLoader_init2(JavaThread* current) {
1509
// Setup the list of module/path pairs for --patch-module processing
1510
// This must be done after the SymbolTable is created in order
1511
// to use fast_compare on module names instead of a string compare.
1512
if (Arguments::get_patch_mod_prefix() != NULL) {
1513
setup_patch_mod_entries();
1514
}
1515
1516
// Create the ModuleEntry for java.base (must occur after setup_patch_mod_entries
1517
// to successfully determine if java.base has been patched)
1518
create_javabase();
1519
1520
// Setup the initial java.base/path pair for the exploded build entries.
1521
// As more modules are defined during module system initialization, more
1522
// entries will be added to the exploded build array.
1523
if (!has_jrt_entry()) {
1524
assert(!DumpSharedSpaces, "DumpSharedSpaces not supported with exploded module builds");
1525
assert(!DynamicDumpSharedSpaces, "DynamicDumpSharedSpaces not supported with exploded module builds");
1526
assert(!UseSharedSpaces, "UsedSharedSpaces not supported with exploded module builds");
1527
// Set up the boot loader's _exploded_entries list. Note that this gets
1528
// done before loading any classes, by the same thread that will
1529
// subsequently do the first class load. So, no lock is needed for this.
1530
assert(_exploded_entries == NULL, "Should only get initialized once");
1531
_exploded_entries = new (ResourceObj::C_HEAP, mtModule)
1532
GrowableArray<ModuleClassPathList*>(EXPLODED_ENTRY_SIZE, mtModule);
1533
add_to_exploded_build_list(current, vmSymbols::java_base());
1534
}
1535
}
1536
1537
char* ClassLoader::get_canonical_path(const char* orig, Thread* thread) {
1538
assert(orig != NULL, "bad arguments");
1539
// caller needs to allocate ResourceMark for the following output buffer
1540
char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);
1541
ResourceMark rm(thread);
1542
// os::native_path writes into orig_copy
1543
char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, strlen(orig)+1);
1544
strcpy(orig_copy, orig);
1545
if ((CanonicalizeEntry)(os::native_path(orig_copy), canonical_path, JVM_MAXPATHLEN) < 0) {
1546
return NULL;
1547
}
1548
return canonical_path;
1549
}
1550
1551
void ClassLoader::create_javabase() {
1552
JavaThread* current = JavaThread::current();
1553
1554
// Create java.base's module entry for the boot
1555
// class loader prior to loading j.l.Ojbect.
1556
ClassLoaderData* null_cld = ClassLoaderData::the_null_class_loader_data();
1557
1558
// Get module entry table
1559
ModuleEntryTable* null_cld_modules = null_cld->modules();
1560
if (null_cld_modules == NULL) {
1561
vm_exit_during_initialization("No ModuleEntryTable for the boot class loader");
1562
}
1563
1564
{
1565
MutexLocker ml(current, Module_lock);
1566
if (ModuleEntryTable::javabase_moduleEntry() == NULL) { // may have been inited by CDS.
1567
ModuleEntry* jb_module = null_cld_modules->locked_create_entry(Handle(),
1568
false, vmSymbols::java_base(), NULL, NULL, null_cld);
1569
if (jb_module == NULL) {
1570
vm_exit_during_initialization("Unable to create ModuleEntry for " JAVA_BASE_NAME);
1571
}
1572
ModuleEntryTable::set_javabase_moduleEntry(jb_module);
1573
}
1574
}
1575
}
1576
1577
// Please keep following two functions at end of this file. With them placed at top or in middle of the file,
1578
// they could get inlined by agressive compiler, an unknown trick, see bug 6966589.
1579
void PerfClassTraceTime::initialize() {
1580
if (!UsePerfData) return;
1581
1582
if (_eventp != NULL) {
1583
// increment the event counter
1584
_eventp->inc();
1585
}
1586
1587
// stop the current active thread-local timer to measure inclusive time
1588
_prev_active_event = -1;
1589
for (int i=0; i < EVENT_TYPE_COUNT; i++) {
1590
if (_timers[i].is_active()) {
1591
assert(_prev_active_event == -1, "should have only one active timer");
1592
_prev_active_event = i;
1593
_timers[i].stop();
1594
}
1595
}
1596
1597
if (_recursion_counters == NULL || (_recursion_counters[_event_type])++ == 0) {
1598
// start the inclusive timer if not recursively called
1599
_t.start();
1600
}
1601
1602
// start thread-local timer of the given event type
1603
if (!_timers[_event_type].is_active()) {
1604
_timers[_event_type].start();
1605
}
1606
}
1607
1608
PerfClassTraceTime::~PerfClassTraceTime() {
1609
if (!UsePerfData) return;
1610
1611
// stop the thread-local timer as the event completes
1612
// and resume the thread-local timer of the event next on the stack
1613
_timers[_event_type].stop();
1614
jlong selftime = _timers[_event_type].ticks();
1615
1616
if (_prev_active_event >= 0) {
1617
_timers[_prev_active_event].start();
1618
}
1619
1620
if (_recursion_counters != NULL && --(_recursion_counters[_event_type]) > 0) return;
1621
1622
// increment the counters only on the leaf call
1623
_t.stop();
1624
_timep->inc(_t.ticks());
1625
if (_selftimep != NULL) {
1626
_selftimep->inc(selftime);
1627
}
1628
// add all class loading related event selftime to the accumulated time counter
1629
ClassLoader::perf_accumulated_time()->inc(selftime);
1630
1631
// reset the timer
1632
_timers[_event_type].reset();
1633
}
1634
1635