-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathclassLoader.cpp
More file actions
1690 lines (1502 loc) · 63.5 KB
/
classLoader.cpp
File metadata and controls
1690 lines (1502 loc) · 63.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "cds/cds_globals.hpp"
#include "cds/filemap.hpp"
#include "classfile/classFileStream.hpp"
#include "classfile/classLoader.inline.hpp"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/classLoaderExt.hpp"
#include "classfile/classLoadInfo.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/modules.hpp"
#include "classfile/packageEntry.hpp"
#include "classfile/klassFactory.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/systemDictionaryShared.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "compiler/compileBroker.hpp"
#include "interpreter/bytecodeStream.hpp"
#include "interpreter/oopMapCache.hpp"
#include "jimage.hpp"
#include "jvm.h"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "logging/logTag.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/oopFactory.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/instanceRefKlass.hpp"
#include "oops/klass.inline.hpp"
#include "oops/method.inline.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/symbol.hpp"
#include "prims/jvm_misc.hpp"
#include "runtime/arguments.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/init.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/os.hpp"
#include "runtime/perfData.hpp"
#include "runtime/threadCritical.hpp"
#include "runtime/timer.hpp"
#include "runtime/vm_version.hpp"
#include "services/management.hpp"
#include "services/threadService.hpp"
#include "utilities/classpathStream.hpp"
#include "utilities/events.hpp"
#include "utilities/macros.hpp"
#include "utilities/utf8.hpp"
#include <stdlib.h>
#include <ctype.h>
// Entry point in java.dll for path canonicalization
typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
static canonicalize_fn_t CanonicalizeEntry = nullptr;
// Entry points in zip.dll for loading zip/jar file entries
typedef void * * (*ZipOpen_t)(const char *name, char **pmsg);
typedef void (*ZipClose_t)(jzfile *zip);
typedef jzentry* (*FindEntry_t)(jzfile *zip, const char *name, jint *sizeP, jint *nameLen);
typedef jboolean (*ReadEntry_t)(jzfile *zip, jzentry *entry, unsigned char *buf, char *namebuf);
typedef jint (*Crc32_t)(jint crc, const jbyte *buf, jint len);
static ZipOpen_t ZipOpen = nullptr;
static ZipClose_t ZipClose = nullptr;
static FindEntry_t FindEntry = nullptr;
static ReadEntry_t ReadEntry = nullptr;
static Crc32_t Crc32 = nullptr;
int ClassLoader::_libzip_loaded = 0;
void* ClassLoader::_zip_handle = nullptr;
// Entry points for jimage.dll for loading jimage file entries
static JImageOpen_t JImageOpen = nullptr;
static JImageClose_t JImageClose = nullptr;
static JImageFindResource_t JImageFindResource = nullptr;
static JImageGetResource_t JImageGetResource = nullptr;
// JimageFile pointer, or null if exploded JDK build.
static JImageFile* JImage_file = nullptr;
// Globals
PerfCounter* ClassLoader::_perf_accumulated_time = nullptr;
PerfCounter* ClassLoader::_perf_classes_inited = nullptr;
PerfCounter* ClassLoader::_perf_class_init_time = nullptr;
PerfCounter* ClassLoader::_perf_class_init_selftime = nullptr;
PerfCounter* ClassLoader::_perf_classes_verified = nullptr;
PerfCounter* ClassLoader::_perf_class_verify_time = nullptr;
PerfCounter* ClassLoader::_perf_class_verify_selftime = nullptr;
PerfCounter* ClassLoader::_perf_classes_linked = nullptr;
PerfCounter* ClassLoader::_perf_class_link_time = nullptr;
PerfCounter* ClassLoader::_perf_class_link_selftime = nullptr;
PerfCounter* ClassLoader::_perf_shared_classload_time = nullptr;
PerfCounter* ClassLoader::_perf_sys_classload_time = nullptr;
PerfCounter* ClassLoader::_perf_app_classload_time = nullptr;
PerfCounter* ClassLoader::_perf_app_classload_selftime = nullptr;
PerfCounter* ClassLoader::_perf_app_classload_count = nullptr;
PerfCounter* ClassLoader::_perf_define_appclasses = nullptr;
PerfCounter* ClassLoader::_perf_define_appclass_time = nullptr;
PerfCounter* ClassLoader::_perf_define_appclass_selftime = nullptr;
PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = nullptr;
PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = nullptr;
PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = nullptr;
PerfCounter* ClassLoader::_perf_secondary_hash_time = nullptr;
GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
ClassPathEntry* ClassLoader::_jrt_entry = nullptr;
ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
ClassPathEntry* volatile ClassLoader::_last_append_entry = nullptr;
#if INCLUDE_CDS
ClassPathEntry* ClassLoader::_app_classpath_entries = nullptr;
ClassPathEntry* ClassLoader::_last_app_classpath_entry = nullptr;
ClassPathEntry* ClassLoader::_module_path_entries = nullptr;
ClassPathEntry* ClassLoader::_last_module_path_entry = nullptr;
#endif
// helper routines
bool string_starts_with(const char* str, const char* str_to_find) {
size_t str_len = strlen(str);
size_t str_to_find_len = strlen(str_to_find);
if (str_to_find_len > str_len) {
return false;
}
return (strncmp(str, str_to_find, str_to_find_len) == 0);
}
static const char* get_jimage_version_string() {
static char version_string[10] = "";
if (version_string[0] == '\0') {
jio_snprintf(version_string, sizeof(version_string), "%d.%d",
VM_Version::vm_major_version(), VM_Version::vm_minor_version());
}
return (const char*)version_string;
}
bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
size_t str_len = strlen(str);
size_t str_to_find_len = strlen(str_to_find);
if (str_to_find_len > str_len) {
return false;
}
return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
}
// Used to obtain the package name from a fully qualified class name.
Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) {
if (name == nullptr) {
if (bad_class_name != nullptr) {
*bad_class_name = true;
}
return nullptr;
}
int utf_len = name->utf8_length();
const jbyte* base = (const jbyte*)name->base();
const jbyte* start = base;
const jbyte* end = UTF8::strrchr(start, utf_len, JVM_SIGNATURE_SLASH);
if (end == nullptr) {
return nullptr;
}
// Skip over '['s
if (*start == JVM_SIGNATURE_ARRAY) {
do {
start++;
} while (start < end && *start == JVM_SIGNATURE_ARRAY);
// Fully qualified class names should not contain a 'L'.
// Set bad_class_name to true to indicate that the package name
// could not be obtained due to an error condition.
// In this situation, is_same_class_package returns false.
if (*start == JVM_SIGNATURE_CLASS) {
if (bad_class_name != nullptr) {
*bad_class_name = true;
}
return nullptr;
}
}
// A class name could have just the slash character in the name,
// in which case start > end
if (start >= end) {
// No package name
if (bad_class_name != nullptr) {
*bad_class_name = true;
}
return nullptr;
}
return SymbolTable::new_symbol(name, start - base, end - base);
}
// Given a fully qualified package name, find its defining package in the class loader's
// package entry table.
PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {
if (pkg_name == nullptr) {
return nullptr;
}
PackageEntryTable* pkgEntryTable = loader_data->packages();
return pkgEntryTable->lookup_only(pkg_name);
}
const char* ClassPathEntry::copy_path(const char* path) {
char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
strcpy(copy, path);
return copy;
}
ClassPathDirEntry::~ClassPathDirEntry() {
FREE_C_HEAP_ARRAY(char, _dir);
}
ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char* name) {
// construct full path name
assert((_dir != nullptr) && (name != nullptr), "sanity");
size_t path_len = strlen(_dir) + strlen(name) + strlen(os::file_separator()) + 1;
char* path = NEW_RESOURCE_ARRAY_IN_THREAD(current, char, path_len);
int len = jio_snprintf(path, path_len, "%s%s%s", _dir, os::file_separator(), name);
assert(len == (int)(path_len - 1), "sanity");
// check if file exists
struct stat st;
if (os::stat(path, &st) == 0) {
// found file, open it
int file_handle = os::open(path, 0, 0);
if (file_handle != -1) {
// read contents into resource array
u1* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(current, u1, st.st_size);
size_t num_read = ::read(file_handle, (char*) buffer, st.st_size);
// close file
::close(file_handle);
// construct ClassFileStream
if (num_read == (size_t)st.st_size) {
if (UsePerfData) {
ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read);
}
#ifdef ASSERT
// Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for
// debug builds so that we guard against use-after-free bugs.
FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
#endif
// Resource allocated
return new ClassFileStream(buffer,
st.st_size,
_dir,
ClassFileStream::verify);
}
}
}
FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
return nullptr;
}
ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name,
bool is_boot_append, bool from_class_path_attr) : ClassPathEntry() {
_zip = zip;
_zip_name = copy_path(zip_name);
_from_class_path_attr = from_class_path_attr;
}
ClassPathZipEntry::~ClassPathZipEntry() {
(*ZipClose)(_zip);
FREE_C_HEAP_ARRAY(char, _zip_name);
}
u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
// enable call to C land
ThreadToNativeFromVM ttn(current);
// check whether zip archive contains name
jint name_len;
jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len);
if (entry == nullptr) return nullptr;
u1* buffer;
char name_buf[128];
char* filename;
if (name_len < 128) {
filename = name_buf;
} else {
filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
}
// read contents into resource array
size_t size = (uint32_t)(*filesize);
if (nul_terminate) {
if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {
return nullptr; // 32-bit integer overflow will occur.
}
size++;
}
buffer = NEW_RESOURCE_ARRAY(u1, size);
if (!(*ReadEntry)(_zip, entry, buffer, filename)) return nullptr;
// return result
if (nul_terminate) {
buffer[size - 1] = 0;
}
return buffer;
}
ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
jint filesize;
u1* buffer = open_entry(current, name, &filesize, false);
if (buffer == nullptr) {
return nullptr;
}
if (UsePerfData) {
ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
}
// Resource allocated
return new ClassFileStream(buffer,
filesize,
_zip_name,
ClassFileStream::verify);
}
DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = nullptr;)
JImageFile* ClassPathImageEntry::jimage() const {
return JImage_file;
}
JImageFile* ClassPathImageEntry::jimage_non_null() const {
assert(ClassLoader::has_jrt_entry(), "must be");
assert(jimage() != nullptr, "should have been opened by ClassLoader::lookup_vm_options "
"and remained throughout normal JVM lifetime");
return jimage();
}
void ClassPathImageEntry::close_jimage() {
if (jimage() != nullptr) {
(*JImageClose)(jimage());
JImage_file = nullptr;
}
}
ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) :
ClassPathEntry() {
guarantee(jimage != nullptr, "jimage file is null");
guarantee(name != nullptr, "jimage file name is null");
assert(_singleton == nullptr, "VM supports only one jimage");
DEBUG_ONLY(_singleton = this);
size_t len = strlen(name) + 1;
_name = copy_path(name);
}
ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {
return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());
}
// For a class in a named module, look it up in the jimage file using this syntax:
// /<module-name>/<package-name>/<base-class>
//
// Assumptions:
// 1. There are no unnamed modules in the jimage file.
// 2. A package is in at most one module in the jimage file.
//
ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
jlong size;
JImageLocationRef location = (*JImageFindResource)(jimage_non_null(), "", get_jimage_version_string(), name, &size);
if (location == 0) {
TempNewSymbol class_name = SymbolTable::new_symbol(name);
TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
if (pkg_name != nullptr) {
if (!Universe::is_module_initialized()) {
location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size);
} else {
PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
if (package_entry != nullptr) {
ResourceMark rm(current);
// Get the module name
ModuleEntry* module = package_entry->module();
assert(module != nullptr, "Boot classLoader package missing module");
assert(module->is_named(), "Boot classLoader package is in unnamed module");
const char* module_name = module->name()->as_C_string();
if (module_name != nullptr) {
location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size);
}
}
}
}
}
if (location != 0) {
if (UsePerfData) {
ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
}
char* data = NEW_RESOURCE_ARRAY(char, size);
(*JImageGetResource)(jimage_non_null(), location, data, size);
// Resource allocated
assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");
return new ClassFileStream((u1*)data,
(int)size,
_name,
ClassFileStream::verify,
true); // from_boot_loader_modules_image
}
return nullptr;
}
JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf,
const char* module_name,
const char* file_name,
jlong &size) {
return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size));
}
bool ClassPathImageEntry::is_modules_image() const {
assert(this == _singleton, "VM supports a single jimage");
assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry");
return true;
}
#if INCLUDE_CDS
void ClassLoader::exit_with_path_failure(const char* error, const char* message) {
Arguments::assert_is_dumping_archive();
tty->print_cr("Hint: enable -Xlog:class+path=info to diagnose the failure");
vm_exit_during_initialization(error, message);
}
#endif
ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
_module_name = module_name;
_module_first_entry = nullptr;
_module_last_entry = nullptr;
}
ModuleClassPathList::~ModuleClassPathList() {
// Clean out each ClassPathEntry on list
ClassPathEntry* e = _module_first_entry;
while (e != nullptr) {
ClassPathEntry* next_entry = e->next();
delete e;
e = next_entry;
}
}
void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {
if (new_entry != nullptr) {
if (_module_last_entry == nullptr) {
_module_first_entry = _module_last_entry = new_entry;
} else {
_module_last_entry->set_next(new_entry);
_module_last_entry = new_entry;
}
}
}
void ClassLoader::trace_class_path(const char* msg, const char* name) {
LogTarget(Info, class, path) lt;
if (lt.is_enabled()) {
LogStream ls(lt);
if (msg) {
ls.print("%s", msg);
}
if (name) {
if (strlen(name) < 256) {
ls.print("%s", name);
} else {
// For very long paths, we need to print each character separately,
// as print_cr() has a length limit
while (name[0] != '\0') {
ls.print("%c", name[0]);
name++;
}
}
}
ls.cr();
}
}
void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {
const char* bootcp = Arguments::get_boot_class_path();
assert(bootcp != nullptr, "Boot class path must not be nullptr");
if (PrintSharedArchiveAndExit) {
// Don't print bootcp - this is the bootcp of this current VM process, not necessarily
// the same as the boot classpath of the shared archive.
} else {
trace_class_path("bootstrap loader class path=", bootcp);
}
setup_bootstrap_search_path_impl(current, bootcp);
}
#if INCLUDE_CDS
void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {
Arguments::assert_is_dumping_archive();
ResourceMark rm(current);
ClasspathStream cp_stream(class_path);
while (cp_stream.has_next()) {
const char* path = cp_stream.get_next();
update_class_path_entry_list(current, path, /* check_for_duplicates */ true,
/* is_boot_append */ false, /* from_class_path_attr */ false);
}
}
void ClassLoader::add_to_module_path_entries(const char* path,
ClassPathEntry* entry) {
assert(entry != nullptr, "ClassPathEntry should not be nullptr");
Arguments::assert_is_dumping_archive();
// The entry does not exist, add to the list
if (_module_path_entries == nullptr) {
assert(_last_module_path_entry == nullptr, "Sanity");
_module_path_entries = _last_module_path_entry = entry;
} else {
_last_module_path_entry->set_next(entry);
_last_module_path_entry = entry;
}
}
// Add a module path to the _module_path_entries list.
void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {
Arguments::assert_is_dumping_archive();
struct stat st;
if (os::stat(path, &st) != 0) {
tty->print_cr("os::stat error %d (%s). CDS dump aborted (path was \"%s\").",
errno, os::errno_name(errno), path);
vm_exit_during_initialization();
}
// File or directory found
ClassPathEntry* new_entry = nullptr;
new_entry = create_class_path_entry(current, path, &st,
false /*is_boot_append */, false /* from_class_path_attr */);
if (new_entry != nullptr) {
add_to_module_path_entries(path, new_entry);
}
}
#endif // INCLUDE_CDS
void ClassLoader::close_jrt_image() {
// Not applicable for exploded builds
if (!ClassLoader::has_jrt_entry()) return;
_jrt_entry->close_jimage();
}
// Construct the array of module/path pairs as specified to --patch-module
// for the boot loader to search ahead of the jimage, if the class being
// loaded is defined to a module that has been specified to --patch-module.
void ClassLoader::setup_patch_mod_entries() {
JavaThread* current = JavaThread::current();
GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
int num_of_entries = patch_mod_args->length();
// Set up the boot loader's _patch_mod_entries list
_patch_mod_entries = new (mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, mtModule);
for (int i = 0; i < num_of_entries; i++) {
const char* module_name = (patch_mod_args->at(i))->module_name();
Symbol* const module_sym = SymbolTable::new_symbol(module_name);
assert(module_sym != nullptr, "Failed to obtain Symbol for module name");
ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
char* class_path = (patch_mod_args->at(i))->path_string();
ResourceMark rm(current);
ClasspathStream cp_stream(class_path);
while (cp_stream.has_next()) {
const char* path = cp_stream.get_next();
struct stat st;
if (os::stat(path, &st) == 0) {
// File or directory found
ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);
// If the path specification is valid, enter it into this module's list
if (new_entry != nullptr) {
module_cpl->add_to_list(new_entry);
}
}
}
// Record the module into the list of --patch-module entries only if
// valid ClassPathEntrys have been created
if (module_cpl->module_first_entry() != nullptr) {
_patch_mod_entries->push(module_cpl);
}
}
}
// Determine whether the module has been patched via the command-line
// option --patch-module
bool ClassLoader::is_in_patch_mod_entries(Symbol* module_name) {
if (_patch_mod_entries != nullptr && _patch_mod_entries->is_nonempty()) {
int table_len = _patch_mod_entries->length();
for (int i = 0; i < table_len; i++) {
ModuleClassPathList* patch_mod = _patch_mod_entries->at(i);
if (module_name->fast_compare(patch_mod->module_name()) == 0) {
return true;
}
}
}
return false;
}
// Set up the _jrt_entry if present and boot append path
void ClassLoader::setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path) {
ResourceMark rm(current);
ClasspathStream cp_stream(class_path);
bool set_base_piece = true;
#if INCLUDE_CDS
if (Arguments::is_dumping_archive()) {
if (!Arguments::has_jimage()) {
vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr);
}
}
#endif
while (cp_stream.has_next()) {
const char* path = cp_stream.get_next();
if (set_base_piece) {
// The first time through the bootstrap_search setup, it must be determined
// what the base or core piece of the boot loader search is. Either a java runtime
// image is present or this is an exploded module build situation.
assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
"Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
struct stat st;
if (os::stat(path, &st) == 0) {
// Directory found
if (JImage_file != nullptr) {
assert(Arguments::has_jimage(), "sanity check");
const char* canonical_path = get_canonical_path(path, current);
assert(canonical_path != nullptr, "canonical_path issue");
_jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path);
assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present");
assert(_jrt_entry->jimage() != nullptr, "No java runtime image");
} // else it's an exploded build.
} else {
// If path does not exist, exit
vm_exit_during_initialization("Unable to establish the boot loader search path", path);
}
set_base_piece = false;
} else {
// Every entry on the boot class path after the initial base piece,
// which is set by os::set_boot_path(), is considered an appended entry.
update_class_path_entry_list(current, path, /* check_for_duplicates */ false,
/* is_boot_append */ true, /* from_class_path_attr */ false);
}
}
}
// Gets the exploded path for the named module. The memory for the path
// is allocated on the C heap if `c_heap` is true otherwise in the resource area.
static const char* get_exploded_module_path(const char* module_name, bool c_heap) {
const char *home = Arguments::get_java_home();
const char file_sep = os::file_separator()[0];
// 10 represents the length of "modules" + 2 file separators + \0
size_t len = strlen(home) + strlen(module_name) + 10;
char *path = c_heap ? NEW_C_HEAP_ARRAY(char, len, mtModule) : NEW_RESOURCE_ARRAY(char, len);
jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);
return path;
}
// During an exploded modules build, each module defined to the boot loader
// will be added to the ClassLoader::_exploded_entries array.
void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {
assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");
assert(_exploded_entries != nullptr, "_exploded_entries was not initialized");
// Find the module's symbol
ResourceMark rm(current);
const char *module_name = module_sym->as_C_string();
const char *path = get_exploded_module_path(module_name, false);
struct stat st;
if (os::stat(path, &st) == 0) {
// Directory found
ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);
// If the path specification is valid, enter it into this module's list.
// There is no need to check for duplicate modules in the exploded entry list,
// since no two modules with the same name can be defined to the boot loader.
// This is checked at module definition time in Modules::define_module.
if (new_entry != nullptr) {
ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
module_cpl->add_to_list(new_entry);
{
MutexLocker ml(current, Module_lock);
_exploded_entries->push(module_cpl);
}
log_info(class, load)("path: %s", path);
}
}
}
jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {
// enable call to C land
ThreadToNativeFromVM ttn(thread);
HandleMark hm(thread);
load_zip_library_if_needed();
return (*ZipOpen)(canonical_path, error_msg);
}
ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
const char *path, const struct stat* st,
bool is_boot_append,
bool from_class_path_attr) {
ClassPathEntry* new_entry = nullptr;
if ((st->st_mode & S_IFMT) == S_IFREG) {
ResourceMark rm(current);
// Regular file, should be a zip file
// Canonicalized filename
const char* canonical_path = get_canonical_path(path, current);
if (canonical_path == nullptr) {
return nullptr;
}
char* error_msg = nullptr;
jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
if (zip != nullptr && error_msg == nullptr) {
new_entry = new ClassPathZipEntry(zip, path, is_boot_append, from_class_path_attr);
} else {
#if INCLUDE_CDS
ClassLoaderExt::set_has_non_jar_in_classpath();
#endif
return nullptr;
}
log_info(class, path)("opened: %s", path);
log_info(class, load)("opened: %s", path);
} else {
// Directory
new_entry = new ClassPathDirEntry(path);
log_info(class, load)("path: %s", path);
}
return new_entry;
}
// Create a class path zip entry for a given path (return null if not found
// or zip/JAR file cannot be opened)
ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
// check for a regular file
struct stat st;
if (os::stat(path, &st) == 0) {
if ((st.st_mode & S_IFMT) == S_IFREG) {
JavaThread* thread = JavaThread::current();
ResourceMark rm(thread);
const char* canonical_path = get_canonical_path(path, thread);
if (canonical_path != nullptr) {
char* error_msg = nullptr;
jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
if (zip != nullptr && error_msg == nullptr) {
// create using canonical path
return new ClassPathZipEntry(zip, canonical_path, is_boot_append, false);
}
}
}
}
return nullptr;
}
// The boot append entries are added with a lock, and read lock free.
void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
if (new_entry != nullptr) {
MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
if (_last_append_entry == nullptr) {
_last_append_entry = new_entry;
assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty");
Atomic::release_store(&_first_append_entry_list, new_entry);
} else {
_last_append_entry->set_next(new_entry);
_last_append_entry = new_entry;
}
}
}
// Record the path entries specified in -cp during dump time. The recorded
// information will be used at runtime for loading the archived app classes.
//
// Note that at dump time, ClassLoader::_app_classpath_entries are NOT used for
// loading app classes. Instead, the app class are loaded by the
// jdk/internal/loader/ClassLoaders$AppClassLoader instance.
bool ClassLoader::add_to_app_classpath_entries(JavaThread* current,
ClassPathEntry* entry,
bool check_for_duplicates) {
#if INCLUDE_CDS
assert(entry != nullptr, "ClassPathEntry should not be nullptr");
ClassPathEntry* e = _app_classpath_entries;
if (check_for_duplicates) {
while (e != nullptr) {
if (strcmp(e->name(), entry->name()) == 0) {
// entry already exists
return false;
}
e = e->next();
}
}
// The entry does not exist, add to the list
if (_app_classpath_entries == nullptr) {
assert(_last_app_classpath_entry == nullptr, "Sanity");
_app_classpath_entries = _last_app_classpath_entry = entry;
} else {
_last_app_classpath_entry->set_next(entry);
_last_app_classpath_entry = entry;
}
if (entry->is_jar_file()) {
ClassLoaderExt::process_jar_manifest(current, entry);
}
#endif
return true;
}
// Returns true IFF the file/dir exists and the entry was successfully created.
bool ClassLoader::update_class_path_entry_list(JavaThread* current,
const char *path,
bool check_for_duplicates,
bool is_boot_append,
bool from_class_path_attr) {
struct stat st;
if (os::stat(path, &st) == 0) {
// File or directory found
ClassPathEntry* new_entry = nullptr;
new_entry = create_class_path_entry(current, path, &st, is_boot_append, from_class_path_attr);
if (new_entry == nullptr) {
return false;
}
// Do not reorder the bootclasspath which would break get_system_package().
// Add new entry to linked list
if (is_boot_append) {
add_to_boot_append_entries(new_entry);
} else {
if (!add_to_app_classpath_entries(current, new_entry, check_for_duplicates)) {
// new_entry is not saved, free it now
delete new_entry;
}
}
return true;
} else {
return false;
}
}
static void print_module_entry_table(const GrowableArray<ModuleClassPathList*>* const module_list) {
ResourceMark rm;
int num_of_entries = module_list->length();
for (int i = 0; i < num_of_entries; i++) {
ClassPathEntry* e;
ModuleClassPathList* mpl = module_list->at(i);
tty->print("%s=", mpl->module_name()->as_C_string());
e = mpl->module_first_entry();
while (e != nullptr) {
tty->print("%s", e->name());
e = e->next();
if (e != nullptr) {
tty->print("%s", os::path_separator());
}
}
tty->print(" ;");
}
}
void ClassLoader::print_bootclasspath() {
ClassPathEntry* e;
tty->print("[bootclasspath= ");
// Print --patch-module module/path specifications first
if (_patch_mod_entries != nullptr) {
print_module_entry_table(_patch_mod_entries);
}
// [jimage | exploded modules build]
if (has_jrt_entry()) {
// Print the location of the java runtime image
tty->print("%s ;", _jrt_entry->name());
} else {
// Print exploded module build path specifications
if (_exploded_entries != nullptr) {
print_module_entry_table(_exploded_entries);
}
}
// appended entries
e = first_append_entry();
while (e != nullptr) {
tty->print("%s ;", e->name());
e = e->next();
}
tty->print_cr("]");
}
void* ClassLoader::dll_lookup(void* lib, const char* name, const char* path) {
void* func = os::dll_lookup(lib, name);
if (func == nullptr) {
char msg[256] = "";
jio_snprintf(msg, sizeof(msg), "Could not resolve \"%s\"", name);
vm_exit_during_initialization(msg, path);
}
return func;
}
void ClassLoader::load_java_library() {
assert(CanonicalizeEntry == nullptr, "should not load java library twice");
void *javalib_handle = os::native_java_library();
if (javalib_handle == nullptr) {
vm_exit_during_initialization("Unable to load java library", nullptr);
}
CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, dll_lookup(javalib_handle, "JDK_Canonicalize", nullptr));
}
void ClassLoader::release_load_zip_library() {
MutexLocker locker(Zip_lock, Monitor::_no_safepoint_check_flag);
if (_libzip_loaded == 0) {
load_zip_library();
Atomic::release_store(&_libzip_loaded, 1);
}
}
void ClassLoader::load_zip_library() {
assert(ZipOpen == nullptr, "should not load zip library twice");
char path[JVM_MAXPATHLEN];
char ebuf[1024];
if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {
_zip_handle = os::dll_load(path, ebuf, sizeof ebuf);
}
if (_zip_handle == nullptr) {
vm_exit_during_initialization("Unable to load zip library", path);
}
ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, dll_lookup(_zip_handle, "ZIP_Open", path));
ZipClose = CAST_TO_FN_PTR(ZipClose_t, dll_lookup(_zip_handle, "ZIP_Close", path));
FindEntry = CAST_TO_FN_PTR(FindEntry_t, dll_lookup(_zip_handle, "ZIP_FindEntry", path));
ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, dll_lookup(_zip_handle, "ZIP_ReadEntry", path));
Crc32 = CAST_TO_FN_PTR(Crc32_t, dll_lookup(_zip_handle, "ZIP_CRC32", path));
}
void ClassLoader::load_jimage_library() {
assert(JImageOpen == nullptr, "should not load jimage library twice");
char path[JVM_MAXPATHLEN];
char ebuf[1024];
void* handle = nullptr;
if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) {
handle = os::dll_load(path, ebuf, sizeof ebuf);
}
if (handle == nullptr) {
vm_exit_during_initialization("Unable to load jimage library", path);
}
JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, dll_lookup(handle, "JIMAGE_Open", path));
JImageClose = CAST_TO_FN_PTR(JImageClose_t, dll_lookup(handle, "JIMAGE_Close", path));
JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, dll_lookup(handle, "JIMAGE_FindResource", path));
JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));
}
int ClassLoader::crc32(int crc, const char* buf, int len) {
load_zip_library_if_needed();
return (*Crc32)(crc, (const jbyte*)buf, len);
}
oop ClassLoader::get_system_package(const char* name, TRAPS) {
// Look up the name in the boot loader's package entry table.
if (name != nullptr) {
TempNewSymbol package_sym = SymbolTable::new_symbol(name);
// Look for the package entry in the boot loader's package entry table.
PackageEntry* package =
ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym);
// Return null if package does not exist or if no classes in that package
// have been loaded.
if (package != nullptr && package->has_loaded_class()) {
ModuleEntry* module = package->module();