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