Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/classfile/modules.cpp
40949 views
1
/*
2
* Copyright (c) 2016, 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 "cds/metaspaceShared.hpp"
28
#include "classfile/classFileParser.hpp"
29
#include "classfile/classLoader.hpp"
30
#include "classfile/classLoaderData.inline.hpp"
31
#include "classfile/classLoaderDataShared.hpp"
32
#include "classfile/javaAssertions.hpp"
33
#include "classfile/javaClasses.hpp"
34
#include "classfile/javaClasses.inline.hpp"
35
#include "classfile/moduleEntry.hpp"
36
#include "classfile/modules.hpp"
37
#include "classfile/packageEntry.hpp"
38
#include "classfile/stringTable.hpp"
39
#include "classfile/symbolTable.hpp"
40
#include "classfile/systemDictionary.hpp"
41
#include "classfile/vmClasses.hpp"
42
#include "classfile/vmSymbols.hpp"
43
#include "logging/log.hpp"
44
#include "logging/logStream.hpp"
45
#include "memory/resourceArea.hpp"
46
#include "prims/jvmtiExport.hpp"
47
#include "runtime/globals_extension.hpp"
48
#include "runtime/handles.inline.hpp"
49
#include "runtime/javaCalls.hpp"
50
#include "runtime/jniHandles.inline.hpp"
51
#include "utilities/formatBuffer.hpp"
52
#include "utilities/stringUtils.hpp"
53
#include "utilities/utf8.hpp"
54
55
static bool verify_module_name(const char *module_name, int len) {
56
assert(module_name != NULL, "invariant");
57
return (len > 0 && len <= Symbol::max_length());
58
}
59
60
static bool verify_package_name(const char* package_name, int len) {
61
assert(package_name != NULL, "Package name derived from non-null jstring can't be NULL");
62
return (len > 0 && len <= Symbol::max_length() &&
63
ClassFileParser::verify_unqualified_name(package_name, len,
64
ClassFileParser::LegalClass));
65
}
66
67
static char* get_module_name(oop module, int& len, TRAPS) {
68
oop name_oop = java_lang_Module::name(module);
69
if (name_oop == NULL) {
70
THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "Null module name");
71
}
72
char* module_name = java_lang_String::as_utf8_string(name_oop, len);
73
if (!verify_module_name(module_name, len)) {
74
THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
75
err_msg("Invalid module name: %s", module_name));
76
}
77
return module_name;
78
}
79
80
static Symbol* as_symbol(jstring str_object) {
81
if (str_object == NULL) {
82
return NULL;
83
}
84
int len;
85
char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(str_object), len);
86
return SymbolTable::new_symbol(str, len);
87
}
88
89
ModuleEntryTable* Modules::get_module_entry_table(Handle h_loader) {
90
// This code can be called during start-up, before the classLoader's classLoader data got
91
// created. So, call register_loader() to make sure the classLoader data gets created.
92
ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader);
93
return loader_cld->modules();
94
}
95
96
static PackageEntryTable* get_package_entry_table(Handle h_loader) {
97
// This code can be called during start-up, before the classLoader's classLoader data got
98
// created. So, call register_loader() to make sure the classLoader data gets created.
99
ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader);
100
return loader_cld->packages();
101
}
102
103
static ModuleEntry* get_module_entry(Handle module, TRAPS) {
104
if (!java_lang_Module::is_instance(module())) {
105
THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
106
"module is not an instance of type java.lang.Module");
107
}
108
return java_lang_Module::module_entry(module());
109
}
110
111
112
static PackageEntry* get_locked_package_entry(ModuleEntry* module_entry, const char* package_name, int len) {
113
assert(Module_lock->owned_by_self(), "should have the Module_lock");
114
assert(package_name != NULL, "Precondition");
115
TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, len);
116
PackageEntryTable* package_entry_table = module_entry->loader_data()->packages();
117
assert(package_entry_table != NULL, "Unexpected null package entry table");
118
PackageEntry* package_entry = package_entry_table->locked_lookup_only(pkg_symbol);
119
assert(package_entry == NULL || package_entry->module() == module_entry, "Unexpectedly found a package linked to another module");
120
return package_entry;
121
}
122
123
static PackageEntry* get_package_entry_by_name(Symbol* package, Handle h_loader) {
124
if (package != NULL) {
125
PackageEntryTable* const package_entry_table =
126
get_package_entry_table(h_loader);
127
assert(package_entry_table != NULL, "Unexpected null package entry table");
128
return package_entry_table->lookup_only(package);
129
}
130
return NULL;
131
}
132
133
bool Modules::is_package_defined(Symbol* package, Handle h_loader) {
134
PackageEntry* res = get_package_entry_by_name(package, h_loader);
135
return res != NULL;
136
}
137
138
// Converts the String oop to an internal package
139
// Will use the provided buffer if it's sufficiently large, otherwise allocates
140
// a resource array
141
// The length of the resulting string will be assigned to utf8_len
142
static const char* as_internal_package(oop package_string, char* buf, int buflen, int& utf8_len) {
143
char* package_name = java_lang_String::as_utf8_string_full(package_string, buf, buflen, utf8_len);
144
145
// Turn all '/'s into '.'s
146
for (int index = 0; index < utf8_len; index++) {
147
if (package_name[index] == JVM_SIGNATURE_DOT) {
148
package_name[index] = JVM_SIGNATURE_SLASH;
149
}
150
}
151
return package_name;
152
}
153
154
static void define_javabase_module(Handle module_handle, jstring version, jstring location,
155
objArrayHandle pkgs, int num_packages, TRAPS) {
156
ResourceMark rm(THREAD);
157
158
// Obtain java.base's module version
159
TempNewSymbol version_symbol = as_symbol(version);
160
161
// Obtain java.base's location
162
TempNewSymbol location_symbol = as_symbol(location);
163
164
// Check that the packages are syntactically ok.
165
char buf[128];
166
GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
167
for (int x = 0; x < num_packages; x++) {
168
oop pkg_str = pkgs->obj_at(x);
169
170
if (pkg_str == NULL || pkg_str->klass() != vmClasses::String_klass()) {
171
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
172
err_msg("Bad package name"));
173
}
174
175
int package_len;
176
const char* package_name = as_internal_package(pkg_str, buf, sizeof(buf), package_len);
177
if (!verify_package_name(package_name, package_len)) {
178
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
179
err_msg("Invalid package name: %s for module: " JAVA_BASE_NAME, package_name));
180
}
181
Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, package_len);
182
pkg_list->append(pkg_symbol);
183
}
184
185
// Validate java_base's loader is the boot loader.
186
oop loader = java_lang_Module::loader(module_handle());
187
if (loader != NULL) {
188
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
189
"Class loader must be the boot class loader");
190
}
191
Handle h_loader(THREAD, loader);
192
193
// Ensure the boot loader's PackageEntryTable has been created
194
PackageEntryTable* package_table = get_package_entry_table(h_loader);
195
assert(pkg_list->length() == 0 || package_table != NULL, "Bad package_table");
196
197
// Ensure java.base's ModuleEntry has been created
198
assert(ModuleEntryTable::javabase_moduleEntry() != NULL, "No ModuleEntry for " JAVA_BASE_NAME);
199
200
bool duplicate_javabase = false;
201
{
202
MutexLocker m1(THREAD, Module_lock);
203
204
if (ModuleEntryTable::javabase_defined()) {
205
duplicate_javabase = true;
206
} else {
207
208
// Verify that all java.base packages created during bootstrapping are in
209
// pkg_list. If any are not in pkg_list, than a non-java.base class was
210
// loaded erroneously pre java.base module definition.
211
package_table->verify_javabase_packages(pkg_list);
212
213
// loop through and add any new packages for java.base
214
for (int x = 0; x < pkg_list->length(); x++) {
215
// Some of java.base's packages were added early in bootstrapping, ignore duplicates.
216
package_table->locked_create_entry_if_not_exist(pkg_list->at(x),
217
ModuleEntryTable::javabase_moduleEntry());
218
assert(package_table->locked_lookup_only(pkg_list->at(x)) != NULL,
219
"Unable to create a " JAVA_BASE_NAME " package entry");
220
// Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of
221
// the Symbol* that was created above for each package. The refcount was incremented
222
// by SymbolTable::new_symbol and as well by the PackageEntry creation.
223
pkg_list->at(x)->decrement_refcount();
224
}
225
226
// Finish defining java.base's ModuleEntry
227
ModuleEntryTable::finalize_javabase(module_handle, version_symbol, location_symbol);
228
}
229
}
230
if (duplicate_javabase) {
231
THROW_MSG(vmSymbols::java_lang_InternalError(),
232
"Module " JAVA_BASE_NAME " is already defined");
233
}
234
235
// Only the thread that actually defined the base module will get here,
236
// so no locking is needed.
237
238
// Patch any previously loaded class's module field with java.base's java.lang.Module.
239
ModuleEntryTable::patch_javabase_entries(module_handle);
240
241
log_info(module, load)(JAVA_BASE_NAME " location: %s",
242
location_symbol != NULL ? location_symbol->as_C_string() : "NULL");
243
log_debug(module)("define_javabase_module(): Definition of module: "
244
JAVA_BASE_NAME ", version: %s, location: %s, package #: %d",
245
version_symbol != NULL ? version_symbol->as_C_string() : "NULL",
246
location_symbol != NULL ? location_symbol->as_C_string() : "NULL",
247
pkg_list->length());
248
249
// packages defined to java.base
250
if (log_is_enabled(Trace, module)) {
251
for (int x = 0; x < pkg_list->length(); x++) {
252
log_trace(module)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME,
253
(pkg_list->at(x))->as_C_string());
254
}
255
}
256
}
257
258
// Caller needs ResourceMark.
259
void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) {
260
const char* package_name = package->name()->as_C_string();
261
if (package->module()->is_named()) {
262
THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
263
err_msg("Package %s for module %s is already in another module, %s, defined to the class loader",
264
package_name, module_name, package->module()->name()->as_C_string()));
265
} else {
266
THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
267
err_msg("Package %s for module %s is already in the unnamed module defined to the class loader",
268
package_name, module_name));
269
}
270
}
271
272
void Modules::define_module(Handle module, jboolean is_open, jstring version,
273
jstring location, jobjectArray packages, TRAPS) {
274
check_cds_restrictions(CHECK);
275
ResourceMark rm(THREAD);
276
277
if (module.is_null()) {
278
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
279
}
280
281
if (!java_lang_Module::is_instance(module())) {
282
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
283
"module is not an instance of type java.lang.Module");
284
}
285
286
int module_name_len;
287
char* module_name = get_module_name(module(), module_name_len, CHECK);
288
if (module_name == NULL) {
289
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
290
"Module name cannot be null");
291
}
292
293
// Resolve packages
294
objArrayHandle packages_h(THREAD, objArrayOop(JNIHandles::resolve(packages)));
295
int num_packages = (packages_h.is_null() ? 0 : packages_h->length());
296
297
// Special handling of java.base definition
298
if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
299
assert(is_open == JNI_FALSE, "java.base module cannot be open");
300
define_javabase_module(module, version, location, packages_h, num_packages, CHECK);
301
return;
302
}
303
304
oop loader = java_lang_Module::loader(module());
305
// Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader.
306
if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) {
307
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
308
"Class loader is an invalid delegating class loader");
309
}
310
Handle h_loader = Handle(THREAD, loader);
311
// define_module can be called during start-up, before the class loader's ClassLoaderData
312
// has been created. SystemDictionary::register_loader ensures creation, if needed.
313
ClassLoaderData* loader_data = SystemDictionary::register_loader(h_loader);
314
assert(loader_data != NULL, "class loader data shouldn't be null");
315
316
// Only modules defined to either the boot or platform class loader, can define a "java/" package.
317
bool java_pkg_disallowed = !h_loader.is_null() &&
318
!SystemDictionary::is_platform_class_loader(h_loader());
319
320
// Check that the list of packages has no duplicates and that the
321
// packages are syntactically ok.
322
char buf[128];
323
GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
324
for (int x = 0; x < num_packages; x++) {
325
oop pkg_str = packages_h->obj_at(x);
326
if (pkg_str == NULL || pkg_str->klass() != vmClasses::String_klass()) {
327
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
328
err_msg("Bad package name"));
329
}
330
331
int package_len;
332
const char* package_name = as_internal_package(pkg_str, buf, sizeof(buf), package_len);
333
if (!verify_package_name(package_name, package_len)) {
334
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
335
err_msg("Invalid package name: %s for module: %s",
336
package_name, module_name));
337
}
338
339
// Only modules defined to either the boot or platform class loader, can define a "java/" package.
340
if (java_pkg_disallowed &&
341
(strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0 &&
342
(package_name[JAVAPKG_LEN] == JVM_SIGNATURE_SLASH || package_name[JAVAPKG_LEN] == '\0'))) {
343
const char* class_loader_name = loader_data->loader_name_and_id();
344
size_t pkg_len = strlen(package_name);
345
char* pkg_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, pkg_len + 1);
346
strncpy(pkg_name, package_name, pkg_len + 1);
347
StringUtils::replace_no_expand(pkg_name, "/", ".");
348
const char* msg_text1 = "Class loader (instance of): ";
349
const char* msg_text2 = " tried to define prohibited package name: ";
350
size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + pkg_len + 1;
351
char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
352
jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, pkg_name);
353
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message);
354
}
355
356
Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, package_len);
357
pkg_list->append(pkg_symbol);
358
}
359
360
ModuleEntryTable* module_table = get_module_entry_table(h_loader);
361
assert(module_table != NULL, "module entry table shouldn't be null");
362
363
// Create symbol* entry for module name.
364
TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, module_name_len);
365
366
bool dupl_modules = false;
367
368
// Create symbol for module version.
369
TempNewSymbol version_symbol = as_symbol(version);
370
371
// Create symbol* entry for module location.
372
TempNewSymbol location_symbol = as_symbol(location);
373
374
PackageEntryTable* package_table = NULL;
375
PackageEntry* existing_pkg = NULL;
376
{
377
MutexLocker ml(THREAD, Module_lock);
378
379
if (num_packages > 0) {
380
package_table = get_package_entry_table(h_loader);
381
assert(package_table != NULL, "Missing package_table");
382
383
// Check that none of the packages exist in the class loader's package table.
384
for (int x = 0; x < pkg_list->length(); x++) {
385
existing_pkg = package_table->locked_lookup_only(pkg_list->at(x));
386
if (existing_pkg != NULL) {
387
// This could be because the module was already defined. If so,
388
// report that error instead of the package error.
389
if (module_table->lookup_only(module_symbol) != NULL) {
390
dupl_modules = true;
391
}
392
break;
393
}
394
}
395
} // if (num_packages > 0)...
396
397
// Add the module and its packages.
398
if (!dupl_modules && existing_pkg == NULL) {
399
if (module_table->lookup_only(module_symbol) == NULL) {
400
// Create the entry for this module in the class loader's module entry table.
401
ModuleEntry* module_entry = module_table->locked_create_entry(module,
402
(is_open == JNI_TRUE), module_symbol,
403
version_symbol, location_symbol, loader_data);
404
assert(module_entry != NULL, "module_entry creation failed");
405
406
// Add the packages.
407
assert(pkg_list->length() == 0 || package_table != NULL, "Bad package table");
408
for (int y = 0; y < pkg_list->length(); y++) {
409
package_table->locked_create_entry(pkg_list->at(y), module_entry);
410
411
// Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of
412
// the Symbol* that was created above for each package. The refcount was incremented
413
// by SymbolTable::new_symbol and as well by the PackageEntry creation.
414
pkg_list->at(y)->decrement_refcount();
415
}
416
417
// Store pointer to ModuleEntry record in java.lang.Module object.
418
java_lang_Module::set_module_entry(module(), module_entry);
419
} else {
420
dupl_modules = true;
421
}
422
}
423
} // Release the lock
424
425
// any errors ?
426
if (dupl_modules) {
427
THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
428
err_msg("Module %s is already defined", module_name));
429
} else if (existing_pkg != NULL) {
430
throw_dup_pkg_exception(module_name, existing_pkg, CHECK);
431
}
432
433
log_info(module, load)("%s location: %s", module_name,
434
location_symbol != NULL ? location_symbol->as_C_string() : "NULL");
435
LogTarget(Debug, module) lt;
436
if (lt.is_enabled()) {
437
LogStream ls(lt);
438
ls.print("define_module(): creation of module: %s, version: %s, location: %s, ",
439
module_name, version_symbol != NULL ? version_symbol->as_C_string() : "NULL",
440
location_symbol != NULL ? location_symbol->as_C_string() : "NULL");
441
loader_data->print_value_on(&ls);
442
ls.print_cr(", package #: %d", pkg_list->length());
443
for (int y = 0; y < pkg_list->length(); y++) {
444
log_trace(module)("define_module(): creation of package %s for module %s",
445
(pkg_list->at(y))->as_C_string(), module_name);
446
}
447
}
448
449
// If the module is defined to the boot loader and an exploded build is being
450
// used, prepend <java.home>/modules/modules_name to the system boot class path.
451
if (h_loader.is_null() && !ClassLoader::has_jrt_entry()) {
452
ClassLoader::add_to_exploded_build_list(THREAD, module_symbol);
453
}
454
455
#ifdef COMPILER2
456
// Special handling of jdk.incubator.vector
457
if (strcmp(module_name, "jdk.incubator.vector") == 0) {
458
if (FLAG_IS_DEFAULT(EnableVectorSupport)) {
459
FLAG_SET_DEFAULT(EnableVectorSupport, true);
460
}
461
if (EnableVectorSupport && FLAG_IS_DEFAULT(EnableVectorReboxing)) {
462
FLAG_SET_DEFAULT(EnableVectorReboxing, true);
463
}
464
if (EnableVectorSupport && EnableVectorReboxing && FLAG_IS_DEFAULT(EnableVectorAggressiveReboxing)) {
465
FLAG_SET_DEFAULT(EnableVectorAggressiveReboxing, true);
466
}
467
if (EnableVectorSupport && FLAG_IS_DEFAULT(UseVectorStubs)) {
468
FLAG_SET_DEFAULT(UseVectorStubs, true);
469
}
470
log_info(compilation)("EnableVectorSupport=%s", (EnableVectorSupport ? "true" : "false"));
471
log_info(compilation)("EnableVectorReboxing=%s", (EnableVectorReboxing ? "true" : "false"));
472
log_info(compilation)("EnableVectorAggressiveReboxing=%s", (EnableVectorAggressiveReboxing ? "true" : "false"));
473
log_info(compilation)("UseVectorStubs=%s", (UseVectorStubs ? "true" : "false"));
474
}
475
#endif // COMPILER2
476
}
477
478
#if INCLUDE_CDS_JAVA_HEAP
479
void Modules::define_archived_modules(Handle h_platform_loader, Handle h_system_loader, TRAPS) {
480
assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
481
482
// We don't want the classes used by the archived full module graph to be redefined by JVMTI.
483
// Luckily, such classes are loaded in the JVMTI "early" phase, and CDS is disabled if a JVMTI
484
// agent wants to redefine classes in this phase.
485
JVMTI_ONLY(assert(JvmtiExport::is_early_phase(), "must be"));
486
assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
487
"CDS should be disabled if early class hooks are enabled");
488
489
Handle java_base_module(THREAD, ClassLoaderDataShared::restore_archived_oops_for_null_class_loader_data());
490
// Patch any previously loaded class's module field with java.base's java.lang.Module.
491
ModuleEntryTable::patch_javabase_entries(java_base_module);
492
493
if (h_platform_loader.is_null()) {
494
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null platform loader object");
495
}
496
497
if (h_system_loader.is_null()) {
498
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
499
}
500
501
ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
502
ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
503
504
ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
505
ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
506
}
507
508
void Modules::check_cds_restrictions(TRAPS) {
509
if (DumpSharedSpaces && Universe::is_module_initialized() && MetaspaceShared::use_full_module_graph()) {
510
THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
511
"During -Xshare:dump, module system cannot be modified after it's initialized");
512
}
513
}
514
#endif // INCLUDE_CDS_JAVA_HEAP
515
516
void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
517
ResourceMark rm(THREAD);
518
519
if (module.is_null()) {
520
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
521
}
522
if (!java_lang_Module::is_instance(module())) {
523
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
524
"module is not an instance of type java.lang.Module");
525
}
526
527
// Ensure that this is an unnamed module
528
oop name = java_lang_Module::name(module());
529
if (name != NULL) {
530
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
531
"boot loader's unnamed module's java.lang.Module has a name");
532
}
533
534
// Validate java_base's loader is the boot loader.
535
oop loader = java_lang_Module::loader(module());
536
if (loader != NULL) {
537
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
538
"Class loader must be the boot class loader");
539
}
540
541
log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
542
543
// Set java.lang.Module for the boot loader's unnamed module
544
ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
545
ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
546
assert(unnamed_module != NULL, "boot loader's unnamed ModuleEntry not defined");
547
unnamed_module->set_module(boot_loader_data->add_handle(module));
548
// Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
549
java_lang_Module::set_module_entry(module(), unnamed_module);
550
}
551
552
void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
553
check_cds_restrictions(CHECK);
554
555
if (package_name == NULL) {
556
THROW_MSG(vmSymbols::java_lang_NullPointerException(),
557
"package is null");
558
}
559
if (from_module.is_null()) {
560
THROW_MSG(vmSymbols::java_lang_NullPointerException(),
561
"from_module is null");
562
}
563
ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
564
if (from_module_entry == NULL) {
565
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
566
"from_module cannot be found");
567
}
568
569
// All packages in unnamed and open modules are exported by default.
570
if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
571
572
ModuleEntry* to_module_entry;
573
if (to_module.is_null()) {
574
to_module_entry = NULL; // It's an unqualified export.
575
} else {
576
to_module_entry = get_module_entry(to_module, CHECK);
577
if (to_module_entry == NULL) {
578
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
579
"to_module is invalid");
580
}
581
}
582
583
PackageEntry* package_entry = NULL;
584
char buf[128];
585
int package_len;
586
587
ResourceMark rm(THREAD);
588
const char* pkg = as_internal_package(JNIHandles::resolve_non_null(package_name), buf, sizeof(buf), package_len);
589
{
590
MutexLocker ml(THREAD, Module_lock);
591
package_entry = get_locked_package_entry(from_module_entry, pkg, package_len);
592
// Do nothing if modules are the same
593
// If the package is not found we'll throw an exception later
594
if (from_module_entry != to_module_entry &&
595
package_entry != NULL) {
596
package_entry->set_exported(to_module_entry);
597
}
598
}
599
600
// Handle errors and logging outside locked section
601
if (package_entry == NULL) {
602
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
603
err_msg("Package %s not found in from_module %s",
604
pkg != NULL ? pkg : "",
605
from_module_entry->name()->as_C_string()));
606
}
607
608
if (log_is_enabled(Debug, module)) {
609
log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
610
package_entry->name()->as_C_string(),
611
from_module_entry->name()->as_C_string(),
612
to_module_entry == NULL ? "NULL" :
613
to_module_entry->is_named() ?
614
to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
615
}
616
}
617
618
619
void Modules::add_module_exports_qualified(Handle from_module, jstring package,
620
Handle to_module, TRAPS) {
621
check_cds_restrictions(CHECK);
622
if (to_module.is_null()) {
623
THROW_MSG(vmSymbols::java_lang_NullPointerException(),
624
"to_module is null");
625
}
626
add_module_exports(from_module, package, to_module, CHECK);
627
}
628
629
void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
630
check_cds_restrictions(CHECK);
631
if (from_module.is_null()) {
632
THROW_MSG(vmSymbols::java_lang_NullPointerException(),
633
"from_module is null");
634
}
635
636
ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
637
if (from_module_entry == NULL) {
638
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
639
"from_module is not valid");
640
}
641
642
ModuleEntry* to_module_entry;
643
if (!to_module.is_null()) {
644
to_module_entry = get_module_entry(to_module, CHECK);
645
if (to_module_entry == NULL) {
646
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
647
"to_module is invalid");
648
}
649
} else {
650
to_module_entry = NULL;
651
}
652
653
ResourceMark rm(THREAD);
654
log_debug(module)("add_reads_module(): Adding read from module %s to module %s",
655
from_module_entry->is_named() ?
656
from_module_entry->name()->as_C_string() : UNNAMED_MODULE,
657
to_module_entry == NULL ? "all unnamed" :
658
(to_module_entry->is_named() ?
659
to_module_entry->name()->as_C_string() : UNNAMED_MODULE));
660
661
// if modules are the same or if from_module is unnamed then no need to add the read.
662
if (from_module_entry != to_module_entry && from_module_entry->is_named()) {
663
from_module_entry->add_read(to_module_entry);
664
}
665
}
666
667
// This method is called by JFR and JNI.
668
jobject Modules::get_module(jclass clazz, TRAPS) {
669
assert(ModuleEntryTable::javabase_defined(),
670
"Attempt to call get_module before " JAVA_BASE_NAME " is defined");
671
672
if (clazz == NULL) {
673
THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
674
"class is null", JNI_FALSE);
675
}
676
oop mirror = JNIHandles::resolve_non_null(clazz);
677
if (mirror == NULL) {
678
log_debug(module)("get_module(): no mirror, returning NULL");
679
return NULL;
680
}
681
if (!java_lang_Class::is_instance(mirror)) {
682
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
683
"Invalid class", JNI_FALSE);
684
}
685
686
oop module = java_lang_Class::module(mirror);
687
688
assert(module != NULL, "java.lang.Class module field not set");
689
assert(java_lang_Module::is_instance(module), "module is not an instance of type java.lang.Module");
690
691
LogTarget(Debug,module) lt;
692
if (lt.is_enabled()) {
693
ResourceMark rm(THREAD);
694
LogStream ls(lt);
695
Klass* klass = java_lang_Class::as_Klass(mirror);
696
oop module_name = java_lang_Module::name(module);
697
if (module_name != NULL) {
698
ls.print("get_module(): module ");
699
java_lang_String::print(module_name, tty);
700
} else {
701
ls.print("get_module(): Unamed Module");
702
}
703
if (klass != NULL) {
704
ls.print_cr(" for class %s", klass->external_name());
705
} else {
706
ls.print_cr(" for primitive class");
707
}
708
}
709
710
return JNIHandles::make_local(THREAD, module);
711
}
712
713
oop Modules::get_named_module(Handle h_loader, const char* package_name) {
714
assert(ModuleEntryTable::javabase_defined(),
715
"Attempt to call get_named_module before " JAVA_BASE_NAME " is defined");
716
assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
717
"Class loader is not a subclass of java.lang.ClassLoader");
718
assert(package_name != NULL, "the package_name should not be NULL");
719
720
if (strlen(package_name) == 0) {
721
return NULL;
722
}
723
TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
724
const PackageEntry* const pkg_entry =
725
get_package_entry_by_name(package_sym, h_loader);
726
const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
727
728
if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) {
729
return module_entry->module();
730
}
731
return NULL;
732
}
733
734
// Export package in module to all unnamed modules.
735
void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
736
check_cds_restrictions(CHECK);
737
if (module.is_null()) {
738
THROW_MSG(vmSymbols::java_lang_NullPointerException(),
739
"module is null");
740
}
741
if (package_name == NULL) {
742
THROW_MSG(vmSymbols::java_lang_NullPointerException(),
743
"package is null");
744
}
745
ModuleEntry* module_entry = get_module_entry(module, CHECK);
746
if (module_entry == NULL) {
747
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
748
"module is invalid");
749
}
750
751
// No-op for unnamed module and open modules
752
if (!module_entry->is_named() || module_entry->is_open())
753
return;
754
755
ResourceMark rm(THREAD);
756
char buf[128];
757
int pkg_len;
758
const char* pkg = as_internal_package(JNIHandles::resolve_non_null(package_name), buf, sizeof(buf), pkg_len);
759
PackageEntry* package_entry = NULL;
760
{
761
MutexLocker m1(THREAD, Module_lock);
762
package_entry = get_locked_package_entry(module_entry, pkg, pkg_len);
763
764
// Mark package as exported to all unnamed modules.
765
if (package_entry != NULL) {
766
package_entry->set_is_exported_allUnnamed();
767
}
768
}
769
770
// Handle errors and logging outside locked section
771
if (package_entry == NULL) {
772
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
773
err_msg("Package %s not found in module %s",
774
pkg != NULL ? pkg : "",
775
module_entry->name()->as_C_string()));
776
}
777
778
if (log_is_enabled(Debug, module)) {
779
log_debug(module)("add_module_exports_to_all_unnamed(): package %s in module"
780
" %s is exported to all unnamed modules",
781
package_entry->name()->as_C_string(),
782
module_entry->name()->as_C_string());
783
}
784
}
785
786