Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/classfile/classLoader.hpp
40949 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#ifndef SHARE_CLASSFILE_CLASSLOADER_HPP
26
#define SHARE_CLASSFILE_CLASSLOADER_HPP
27
28
#include "jimage.hpp"
29
#include "runtime/handles.hpp"
30
#include "runtime/perfDataTypes.hpp"
31
#include "utilities/exceptions.hpp"
32
#include "utilities/macros.hpp"
33
34
// The VM class loader.
35
#include <sys/stat.h>
36
37
// Name of boot "modules" image
38
#define MODULES_IMAGE_NAME "modules"
39
40
// Class path entry (directory or zip file)
41
42
class JImageFile;
43
class ClassFileStream;
44
class PackageEntry;
45
template <typename T> class GrowableArray;
46
47
class ClassPathEntry : public CHeapObj<mtClass> {
48
private:
49
ClassPathEntry* volatile _next;
50
protected:
51
const char* copy_path(const char*path);
52
public:
53
ClassPathEntry* next() const;
54
virtual ~ClassPathEntry() {}
55
void set_next(ClassPathEntry* next);
56
57
virtual bool is_modules_image() const { return false; }
58
virtual bool is_jar_file() const { return false; }
59
// Is this entry created from the "Class-path" attribute from a JAR Manifest?
60
virtual bool from_class_path_attr() const { return false; }
61
virtual const char* name() const = 0;
62
virtual JImageFile* jimage() const { return NULL; }
63
virtual void close_jimage() {}
64
// Constructor
65
ClassPathEntry() : _next(NULL) {}
66
// Attempt to locate file_name through this class path entry.
67
// Returns a class file parsing stream if successfull.
68
virtual ClassFileStream* open_stream(JavaThread* current, const char* name) = 0;
69
// Open the stream for a specific class loader
70
virtual ClassFileStream* open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
71
return open_stream(current, name);
72
}
73
};
74
75
class ClassPathDirEntry: public ClassPathEntry {
76
private:
77
const char* _dir; // Name of directory
78
public:
79
const char* name() const { return _dir; }
80
ClassPathDirEntry(const char* dir) {
81
_dir = copy_path(dir);
82
}
83
virtual ~ClassPathDirEntry() {}
84
ClassFileStream* open_stream(JavaThread* current, const char* name);
85
};
86
87
// Type definitions for zip file and zip file entry
88
typedef void* jzfile;
89
typedef struct {
90
char *name; /* entry name */
91
jlong time; /* modification time */
92
jlong size; /* size of uncompressed data */
93
jlong csize; /* size of compressed data (zero if uncompressed) */
94
jint crc; /* crc of uncompressed data */
95
char *comment; /* optional zip file comment */
96
jbyte *extra; /* optional extra data */
97
jlong pos; /* position of LOC header (if negative) or data */
98
} jzentry;
99
100
class ClassPathZipEntry: public ClassPathEntry {
101
private:
102
jzfile* _zip; // The zip archive
103
const char* _zip_name; // Name of zip archive
104
bool _from_class_path_attr; // From the "Class-path" attribute of a jar file
105
public:
106
bool is_jar_file() const { return true; }
107
bool from_class_path_attr() const { return _from_class_path_attr; }
108
const char* name() const { return _zip_name; }
109
ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append, bool from_class_path_attr);
110
virtual ~ClassPathZipEntry();
111
u1* open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate);
112
ClassFileStream* open_stream(JavaThread* current, const char* name);
113
void contents_do(void f(const char* name, void* context), void* context);
114
};
115
116
117
// For java image files
118
class ClassPathImageEntry: public ClassPathEntry {
119
private:
120
const char* _name;
121
DEBUG_ONLY(static ClassPathImageEntry* _singleton;)
122
public:
123
bool is_modules_image() const;
124
const char* name() const { return _name == NULL ? "" : _name; }
125
JImageFile* jimage() const;
126
JImageFile* jimage_non_null() const;
127
void close_jimage();
128
ClassPathImageEntry(JImageFile* jimage, const char* name);
129
virtual ~ClassPathImageEntry() { ShouldNotReachHere(); }
130
ClassFileStream* open_stream(JavaThread* current, const char* name);
131
ClassFileStream* open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data);
132
};
133
134
// ModuleClassPathList contains a linked list of ClassPathEntry's
135
// that have been specified for a specific module. Currently,
136
// the only way to specify a module/path pair is via the --patch-module
137
// command line option.
138
class ModuleClassPathList : public CHeapObj<mtClass> {
139
private:
140
Symbol* _module_name;
141
// First and last entries of class path entries for a specific module
142
ClassPathEntry* _module_first_entry;
143
ClassPathEntry* _module_last_entry;
144
public:
145
Symbol* module_name() const { return _module_name; }
146
ClassPathEntry* module_first_entry() const { return _module_first_entry; }
147
ModuleClassPathList(Symbol* module_name);
148
~ModuleClassPathList();
149
void add_to_list(ClassPathEntry* new_entry);
150
};
151
152
class ClassLoader: AllStatic {
153
public:
154
enum ClassLoaderType {
155
BOOT_LOADER = 1, /* boot loader */
156
PLATFORM_LOADER = 2, /* PlatformClassLoader */
157
APP_LOADER = 3 /* AppClassLoader */
158
};
159
protected:
160
161
// Performance counters
162
static PerfCounter* _perf_accumulated_time;
163
static PerfCounter* _perf_classes_inited;
164
static PerfCounter* _perf_class_init_time;
165
static PerfCounter* _perf_class_init_selftime;
166
static PerfCounter* _perf_classes_verified;
167
static PerfCounter* _perf_class_verify_time;
168
static PerfCounter* _perf_class_verify_selftime;
169
static PerfCounter* _perf_classes_linked;
170
static PerfCounter* _perf_class_link_time;
171
static PerfCounter* _perf_class_link_selftime;
172
static PerfCounter* _perf_sys_class_lookup_time;
173
static PerfCounter* _perf_shared_classload_time;
174
static PerfCounter* _perf_sys_classload_time;
175
static PerfCounter* _perf_app_classload_time;
176
static PerfCounter* _perf_app_classload_selftime;
177
static PerfCounter* _perf_app_classload_count;
178
static PerfCounter* _perf_define_appclasses;
179
static PerfCounter* _perf_define_appclass_time;
180
static PerfCounter* _perf_define_appclass_selftime;
181
static PerfCounter* _perf_app_classfile_bytes_read;
182
static PerfCounter* _perf_sys_classfile_bytes_read;
183
184
static PerfCounter* _unsafe_defineClassCallCounter;
185
186
// The boot class path consists of 3 ordered pieces:
187
// 1. the module/path pairs specified to --patch-module
188
// --patch-module=<module>=<file>(<pathsep><file>)*
189
// 2. the base piece
190
// [jimage | build with exploded modules]
191
// 3. boot loader append path
192
// [-Xbootclasspath/a]; [jvmti appended entries]
193
//
194
// The boot loader must obey this order when attempting
195
// to load a class.
196
197
// 1. Contains the module/path pairs specified to --patch-module
198
static GrowableArray<ModuleClassPathList*>* _patch_mod_entries;
199
200
// 2. the base piece
201
// Contains the ClassPathEntry of the modular java runtime image.
202
// If no java runtime image is present, this indicates a
203
// build with exploded modules is being used instead.
204
static ClassPathEntry* _jrt_entry;
205
static GrowableArray<ModuleClassPathList*>* _exploded_entries;
206
enum { EXPLODED_ENTRY_SIZE = 80 }; // Initial number of exploded modules
207
208
// 3. the boot loader's append path
209
// [-Xbootclasspath/a]; [jvmti appended entries]
210
// Note: boot loader append path does not support named modules.
211
static ClassPathEntry* volatile _first_append_entry_list;
212
static ClassPathEntry* first_append_entry() {
213
return Atomic::load_acquire(&_first_append_entry_list);
214
}
215
216
// Last entry in linked list of appended ClassPathEntry instances
217
static ClassPathEntry* volatile _last_append_entry;
218
219
// Info used by CDS
220
CDS_ONLY(static ClassPathEntry* _app_classpath_entries;)
221
CDS_ONLY(static ClassPathEntry* _last_app_classpath_entry;)
222
CDS_ONLY(static ClassPathEntry* _module_path_entries;)
223
CDS_ONLY(static ClassPathEntry* _last_module_path_entry;)
224
CDS_ONLY(static void setup_app_search_path(JavaThread* current, const char* class_path);)
225
CDS_ONLY(static void setup_module_search_path(JavaThread* current, const char* path);)
226
static void add_to_app_classpath_entries(JavaThread* current,
227
const char* path,
228
ClassPathEntry* entry,
229
bool check_for_duplicates);
230
CDS_ONLY(static void add_to_module_path_entries(const char* path,
231
ClassPathEntry* entry);)
232
public:
233
CDS_ONLY(static ClassPathEntry* app_classpath_entries() {return _app_classpath_entries;})
234
CDS_ONLY(static ClassPathEntry* module_path_entries() {return _module_path_entries;})
235
236
static bool has_bootclasspath_append() { return first_append_entry() != NULL; }
237
238
protected:
239
// Initialization:
240
// - setup the boot loader's system class path
241
// - setup the boot loader's patch mod entries, if present
242
// - create the ModuleEntry for java.base
243
static void setup_bootstrap_search_path(JavaThread* current);
244
static void setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path);
245
static void setup_patch_mod_entries();
246
static void create_javabase();
247
248
static void* dll_lookup(void* lib, const char* name, const char* path);
249
static void load_java_library();
250
static void load_zip_library();
251
static void load_jimage_library();
252
253
private:
254
static int _libzip_loaded; // used to sync loading zip.
255
static void release_load_zip_library();
256
static inline void load_zip_library_if_needed();
257
static jzfile* open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread);
258
259
public:
260
static ClassPathEntry* create_class_path_entry(JavaThread* current,
261
const char *path, const struct stat* st,
262
bool is_boot_append,
263
bool from_class_path_attr);
264
265
// Canonicalizes path names, so strcmp will work properly. This is mainly
266
// to avoid confusing the zip library
267
static char* get_canonical_path(const char* orig, Thread* thread);
268
static const char* file_name_for_class_name(const char* class_name,
269
int class_name_len);
270
static PackageEntry* get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data);
271
static int crc32(int crc, const char* buf, int len);
272
static bool update_class_path_entry_list(JavaThread* current,
273
const char *path,
274
bool check_for_duplicates,
275
bool is_boot_append,
276
bool from_class_path_attr);
277
static void print_bootclasspath();
278
279
// Timing
280
static PerfCounter* perf_accumulated_time() { return _perf_accumulated_time; }
281
static PerfCounter* perf_classes_inited() { return _perf_classes_inited; }
282
static PerfCounter* perf_class_init_time() { return _perf_class_init_time; }
283
static PerfCounter* perf_class_init_selftime() { return _perf_class_init_selftime; }
284
static PerfCounter* perf_classes_verified() { return _perf_classes_verified; }
285
static PerfCounter* perf_class_verify_time() { return _perf_class_verify_time; }
286
static PerfCounter* perf_class_verify_selftime() { return _perf_class_verify_selftime; }
287
static PerfCounter* perf_classes_linked() { return _perf_classes_linked; }
288
static PerfCounter* perf_class_link_time() { return _perf_class_link_time; }
289
static PerfCounter* perf_class_link_selftime() { return _perf_class_link_selftime; }
290
static PerfCounter* perf_sys_class_lookup_time() { return _perf_sys_class_lookup_time; }
291
static PerfCounter* perf_shared_classload_time() { return _perf_shared_classload_time; }
292
static PerfCounter* perf_sys_classload_time() { return _perf_sys_classload_time; }
293
static PerfCounter* perf_app_classload_time() { return _perf_app_classload_time; }
294
static PerfCounter* perf_app_classload_selftime() { return _perf_app_classload_selftime; }
295
static PerfCounter* perf_app_classload_count() { return _perf_app_classload_count; }
296
static PerfCounter* perf_define_appclasses() { return _perf_define_appclasses; }
297
static PerfCounter* perf_define_appclass_time() { return _perf_define_appclass_time; }
298
static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; }
299
static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; }
300
static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; }
301
302
// Record how many calls to Unsafe_DefineClass
303
static PerfCounter* unsafe_defineClassCallCounter() {
304
return _unsafe_defineClassCallCounter;
305
}
306
307
// Modular java runtime image is present vs. a build with exploded modules
308
static bool has_jrt_entry() { return (_jrt_entry != NULL); }
309
static ClassPathEntry* get_jrt_entry() { return _jrt_entry; }
310
static void close_jrt_image();
311
312
// Add a module's exploded directory to the boot loader's exploded module build list
313
static void add_to_exploded_build_list(JavaThread* current, Symbol* module_name);
314
315
// Attempt load of individual class from either the patched or exploded modules build lists
316
static ClassFileStream* search_module_entries(JavaThread* current,
317
const GrowableArray<ModuleClassPathList*>* const module_list,
318
const char* const class_name,
319
const char* const file_name);
320
321
// Load individual .class file
322
static InstanceKlass* load_class(Symbol* class_name, bool search_append_only, TRAPS);
323
324
// If the specified package has been loaded by the system, then returns
325
// the name of the directory or ZIP file that the package was loaded from.
326
// Returns null if the package was not loaded.
327
// Note: The specified name can either be the name of a class or package.
328
// If a package name is specified, then it must be "/"-separator and also
329
// end with a trailing "/".
330
static oop get_system_package(const char* name, TRAPS);
331
332
// Returns an array of Java strings representing all of the currently
333
// loaded system packages.
334
// Note: The package names returned are "/"-separated and end with a
335
// trailing "/".
336
static objArrayOop get_system_packages(TRAPS);
337
338
// Initialization
339
static void initialize(TRAPS);
340
static void classLoader_init2(JavaThread* current);
341
CDS_ONLY(static void initialize_shared_path(JavaThread* current);)
342
CDS_ONLY(static void initialize_module_path(TRAPS);)
343
344
static int compute_Object_vtable();
345
346
static ClassPathEntry* classpath_entry(int n);
347
348
static bool is_in_patch_mod_entries(Symbol* module_name);
349
350
#if INCLUDE_CDS
351
// Sharing dump and restore
352
353
// Helper function used by CDS code to get the number of boot classpath
354
// entries during shared classpath setup time.
355
static int num_boot_classpath_entries();
356
357
static ClassPathEntry* get_next_boot_classpath_entry(ClassPathEntry* e);
358
359
// Helper function used by CDS code to get the number of app classpath
360
// entries during shared classpath setup time.
361
static int num_app_classpath_entries();
362
363
// Helper function used by CDS code to get the number of module path
364
// entries during shared classpath setup time.
365
static int num_module_path_entries();
366
static void exit_with_path_failure(const char* error, const char* message);
367
static char* skip_uri_protocol(char* source);
368
static void record_result(JavaThread* current, InstanceKlass* ik, const ClassFileStream* stream);
369
#endif
370
371
static char* lookup_vm_options();
372
373
static JImageLocationRef jimage_find_resource(JImageFile* jf, const char* module_name,
374
const char* file_name, jlong &size);
375
376
static void trace_class_path(const char* msg, const char* name = NULL);
377
378
// VM monitoring and management support
379
static jlong classloader_time_ms();
380
static jlong class_method_total_size();
381
static jlong class_init_count();
382
static jlong class_init_time_ms();
383
static jlong class_verify_time_ms();
384
static jlong class_link_count();
385
static jlong class_link_time_ms();
386
387
// adds a class path to the boot append entries
388
static void add_to_boot_append_entries(ClassPathEntry* new_entry);
389
390
// creates a class path zip entry (returns NULL if JAR file cannot be opened)
391
static ClassPathZipEntry* create_class_path_zip_entry(const char *apath, bool is_boot_append);
392
393
static bool string_ends_with(const char* str, const char* str_to_find);
394
395
// Extract package name from a fully qualified class name
396
// *bad_class_name is set to true if there's a problem with parsing class_name, to
397
// distinguish from a class_name with no package name, as both cases have a NULL return value
398
static Symbol* package_from_class_name(const Symbol* class_name, bool* bad_class_name = NULL);
399
400
// Debugging
401
static void verify() PRODUCT_RETURN;
402
};
403
404
// PerfClassTraceTime is used to measure time for class loading related events.
405
// This class tracks cumulative time and exclusive time for specific event types.
406
// During the execution of one event, other event types (e.g. class loading and
407
// resolution) as well as recursive calls of the same event type could happen.
408
// Only one elapsed timer (cumulative) and one thread-local self timer (exclusive)
409
// (i.e. only one event type) are active at a time even multiple PerfClassTraceTime
410
// instances have been created as multiple events are happening.
411
class PerfClassTraceTime {
412
public:
413
enum {
414
CLASS_LOAD = 0,
415
CLASS_LINK = 1,
416
CLASS_VERIFY = 2,
417
CLASS_CLINIT = 3,
418
DEFINE_CLASS = 4,
419
EVENT_TYPE_COUNT = 5
420
};
421
protected:
422
// _t tracks time from initialization to destruction of this timer instance
423
// including time for all other event types, and recursive calls of this type.
424
// When a timer is called recursively, the elapsedTimer _t would not be used.
425
elapsedTimer _t;
426
PerfLongCounter* _timep;
427
PerfLongCounter* _selftimep;
428
PerfLongCounter* _eventp;
429
// pointer to thread-local recursion counter and timer array
430
// The thread_local timers track cumulative time for specific event types
431
// exclusive of time for other event types, but including recursive calls
432
// of the same type.
433
int* _recursion_counters;
434
elapsedTimer* _timers;
435
int _event_type;
436
int _prev_active_event;
437
438
public:
439
440
inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */
441
PerfLongCounter* selftimep, /* counter incremented with exclusive time */
442
PerfLongCounter* eventp, /* event counter */
443
int* recursion_counters, /* thread-local recursion counter array */
444
elapsedTimer* timers, /* thread-local timer array */
445
int type /* event type */ ) :
446
_timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) {
447
initialize();
448
}
449
450
inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */
451
elapsedTimer* timers, /* thread-local timer array */
452
int type /* event type */ ) :
453
_timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) {
454
initialize();
455
}
456
457
~PerfClassTraceTime();
458
void initialize();
459
};
460
461
#endif // SHARE_CLASSFILE_CLASSLOADER_HPP
462
463