author | hseigel |
Fri, 13 Jan 2017 07:19:03 -0500 | |
changeset 43446 | 4f9ac7ab99d9 |
parent 42656 | 5360d39e2aba |
child 43466 | add500644443 |
permissions | -rw-r--r-- |
36508 | 1 |
/* |
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
36508 | 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 "classfile/classFileParser.hpp" |
|
27 |
#include "classfile/classLoader.hpp" |
|
28 |
#include "classfile/classLoaderData.inline.hpp" |
|
29 |
#include "classfile/javaAssertions.hpp" |
|
30 |
#include "classfile/javaClasses.hpp" |
|
31 |
#include "classfile/javaClasses.inline.hpp" |
|
32 |
#include "classfile/moduleEntry.hpp" |
|
33 |
#include "classfile/modules.hpp" |
|
34 |
#include "classfile/packageEntry.hpp" |
|
35 |
#include "classfile/stringTable.hpp" |
|
36 |
#include "classfile/symbolTable.hpp" |
|
42307
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
37 |
#include "classfile/systemDictionary.hpp" |
36508 | 38 |
#include "classfile/vmSymbols.hpp" |
39 |
#include "logging/log.hpp" |
|
37248 | 40 |
#include "memory/resourceArea.hpp" |
36508 | 41 |
#include "oops/instanceKlass.hpp" |
42 |
#include "oops/objArrayKlass.hpp" |
|
43 |
#include "oops/objArrayOop.inline.hpp" |
|
44 |
#include "runtime/arguments.hpp" |
|
45 |
#include "runtime/handles.inline.hpp" |
|
46 |
#include "runtime/javaCalls.hpp" |
|
47 |
#include "runtime/reflection.hpp" |
|
42307
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
48 |
#include "utilities/stringUtils.hpp" |
36508 | 49 |
#include "utilities/utf8.hpp" |
50 |
||
51 |
static bool verify_module_name(char *module_name) { |
|
52 |
if (module_name == NULL) return false; |
|
53 |
int len = (int)strlen(module_name); |
|
42630
ae91fbc4b59f
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42307
diff
changeset
|
54 |
return (len > 0 && len <= Symbol::max_length()); |
36508 | 55 |
} |
56 |
||
57 |
bool Modules::verify_package_name(char *package_name) { |
|
58 |
if (package_name == NULL) return false; |
|
59 |
int len = (int)strlen(package_name); |
|
60 |
return (len > 0 && len <= Symbol::max_length() && |
|
61 |
UTF8::is_legal_utf8((unsigned char *)package_name, len, false) && |
|
62 |
ClassFileParser::verify_unqualified_name(package_name, len, |
|
63 |
ClassFileParser::LegalClass)); |
|
64 |
} |
|
65 |
||
66 |
static char* get_module_name(oop module, TRAPS) { |
|
67 |
oop name_oop = java_lang_reflect_Module::name(module); |
|
68 |
if (name_oop == NULL) { |
|
69 |
THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "Null module name"); |
|
70 |
} |
|
71 |
char* module_name = java_lang_String::as_utf8_string(name_oop); |
|
72 |
if (!verify_module_name(module_name)) { |
|
73 |
THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), |
|
74 |
err_msg("Invalid module name: %s", |
|
75 |
module_name != NULL ? module_name : "NULL")); |
|
76 |
} |
|
77 |
return module_name; |
|
78 |
} |
|
79 |
||
80 |
static const char* get_module_version(jstring version) { |
|
81 |
if (version == NULL) { |
|
82 |
return NULL; |
|
83 |
} |
|
84 |
return java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(version)); |
|
85 |
} |
|
86 |
||
87 |
static ModuleEntryTable* get_module_entry_table(Handle h_loader, TRAPS) { |
|
88 |
// This code can be called during start-up, before the classLoader's classLoader data got |
|
89 |
// created. So, call register_loader() to make sure the classLoader data gets created. |
|
90 |
ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL); |
|
91 |
return loader_cld->modules(); |
|
92 |
} |
|
93 |
||
94 |
static PackageEntryTable* get_package_entry_table(Handle h_loader, TRAPS) { |
|
95 |
// This code can be called during start-up, before the classLoader's classLoader data got |
|
96 |
// created. So, call register_loader() to make sure the classLoader data gets created. |
|
97 |
ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL); |
|
98 |
return loader_cld->packages(); |
|
99 |
} |
|
100 |
||
101 |
static ModuleEntry* get_module_entry(jobject module, TRAPS) { |
|
102 |
Handle module_h(THREAD, JNIHandles::resolve(module)); |
|
103 |
if (!java_lang_reflect_Module::is_instance(module_h())) { |
|
40020
20738e6bef83
8160487: JVM should validate a module by checking for an instance of java.lang.reflect.Module
lfoltan
parents:
40013
diff
changeset
|
104 |
THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), |
20738e6bef83
8160487: JVM should validate a module by checking for an instance of java.lang.reflect.Module
lfoltan
parents:
40013
diff
changeset
|
105 |
"module is not an instance of type java.lang.reflect.Module"); |
36508 | 106 |
} |
107 |
return java_lang_reflect_Module::module_entry(module_h(), CHECK_NULL); |
|
108 |
} |
|
109 |
||
110 |
static PackageEntry* get_package_entry(ModuleEntry* module_entry, jstring package, TRAPS) { |
|
111 |
ResourceMark rm(THREAD); |
|
112 |
if (package == NULL) return NULL; |
|
113 |
const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
114 |
if (package_name == NULL) return NULL; |
|
115 |
TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK_NULL); |
|
39616
f82b1f888578
8159262: Walking PackageEntry Export and ModuleEntry Reads Must Occur Only When Neccessary And Wait Until ClassLoader's Aliveness Determined
lfoltan
parents:
39290
diff
changeset
|
116 |
PackageEntryTable* package_entry_table = module_entry->loader_data()->packages(); |
36508 | 117 |
assert(package_entry_table != NULL, "Unexpected null package entry table"); |
118 |
return package_entry_table->lookup_only(pkg_symbol); |
|
119 |
} |
|
120 |
||
121 |
static PackageEntry* get_package_entry_by_name(Symbol* package, |
|
122 |
Handle h_loader, |
|
123 |
TRAPS) { |
|
124 |
if (package != NULL) { |
|
125 |
ResourceMark rm(THREAD); |
|
126 |
if (Modules::verify_package_name(package->as_C_string())) { |
|
127 |
PackageEntryTable* const package_entry_table = |
|
128 |
get_package_entry_table(h_loader, CHECK_NULL); |
|
129 |
assert(package_entry_table != NULL, "Unexpected null package entry table"); |
|
130 |
return package_entry_table->lookup_only(package); |
|
131 |
} |
|
132 |
} |
|
133 |
return NULL; |
|
134 |
} |
|
135 |
||
136 |
bool Modules::is_package_defined(Symbol* package, Handle h_loader, TRAPS) { |
|
137 |
PackageEntry* res = get_package_entry_by_name(package, h_loader, CHECK_false); |
|
138 |
return res != NULL; |
|
139 |
} |
|
140 |
||
141 |
static void define_javabase_module(jobject module, jstring version, |
|
142 |
jstring location, jobjectArray packages, TRAPS) { |
|
143 |
ResourceMark rm(THREAD); |
|
144 |
||
145 |
Handle module_handle(THREAD, JNIHandles::resolve(module)); |
|
146 |
||
147 |
// Obtain java.base's module version |
|
148 |
const char* module_version = get_module_version(version); |
|
149 |
TempNewSymbol version_symbol; |
|
150 |
if (module_version != NULL) { |
|
151 |
version_symbol = SymbolTable::new_symbol(module_version, CHECK); |
|
152 |
} else { |
|
153 |
version_symbol = NULL; |
|
154 |
} |
|
155 |
||
156 |
// Obtain java.base's location |
|
157 |
const char* module_location = NULL; |
|
158 |
TempNewSymbol location_symbol = NULL; |
|
159 |
if (location != NULL) { |
|
160 |
module_location = |
|
161 |
java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location)); |
|
162 |
if (module_location != NULL) { |
|
163 |
location_symbol = SymbolTable::new_symbol(module_location, CHECK); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages)); |
|
168 |
objArrayHandle packages_h(THREAD, packages_oop); |
|
169 |
int num_packages = (packages_h == NULL ? 0 : packages_h->length()); |
|
170 |
||
171 |
// Check that the list of packages has no duplicates and that the |
|
172 |
// packages are syntactically ok. |
|
173 |
GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); |
|
174 |
for (int x = 0; x < num_packages; x++) { |
|
175 |
oop string_obj = packages_h->obj_at(x); |
|
176 |
||
177 |
if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) { |
|
178 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
179 |
"Bad package name for module: " JAVA_BASE_NAME); |
36508 | 180 |
} |
181 |
char *package_name = java_lang_String::as_utf8_string(string_obj); |
|
182 |
if (!Modules::verify_package_name(package_name)) { |
|
183 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
184 |
err_msg("Invalid package name: %s for module: " JAVA_BASE_NAME, package_name)); |
36508 | 185 |
} |
186 |
Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK); |
|
187 |
// append_if_missing() returns FALSE if entry already exists. |
|
188 |
if (!pkg_list->append_if_missing(pkg_symbol)) { |
|
189 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
190 |
err_msg("Duplicate package name: %s for module " JAVA_BASE_NAME, |
36508 | 191 |
package_name)); |
192 |
} |
|
193 |
} |
|
194 |
||
195 |
// Validate java_base's loader is the boot loader. |
|
196 |
oop loader = java_lang_reflect_Module::loader(module_handle()); |
|
197 |
if (loader != NULL) { |
|
198 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
199 |
"Class loader must be the boot class loader"); |
|
200 |
} |
|
201 |
Handle h_loader = Handle(THREAD, loader); |
|
202 |
||
203 |
// Ensure the boot loader's PackageEntryTable has been created |
|
204 |
PackageEntryTable* package_table = get_package_entry_table(h_loader, CHECK); |
|
205 |
assert(pkg_list->length() == 0 || package_table != NULL, "Bad package_table"); |
|
206 |
||
207 |
// Ensure java.base's ModuleEntry has been created |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
208 |
assert(ModuleEntryTable::javabase_moduleEntry() != NULL, "No ModuleEntry for " JAVA_BASE_NAME); |
36508 | 209 |
|
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
210 |
bool duplicate_javabase = false; |
36508 | 211 |
{ |
212 |
MutexLocker m1(Module_lock, THREAD); |
|
213 |
||
38014
8731fa11f766
8152949: Jigsaw crash when Klass in _fixup_module_field_list is unloaded
lfoltan
parents:
37248
diff
changeset
|
214 |
if (ModuleEntryTable::javabase_defined()) { |
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
215 |
duplicate_javabase = true; |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
216 |
} else { |
38014
8731fa11f766
8152949: Jigsaw crash when Klass in _fixup_module_field_list is unloaded
lfoltan
parents:
37248
diff
changeset
|
217 |
|
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
218 |
// Verify that all java.base packages created during bootstrapping are in |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
219 |
// pkg_list. If any are not in pkg_list, than a non-java.base class was |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
220 |
// loaded erroneously pre java.base module definition. |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
221 |
package_table->verify_javabase_packages(pkg_list); |
36508 | 222 |
|
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
223 |
// loop through and add any new packages for java.base |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
224 |
PackageEntry* pkg; |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
225 |
for (int x = 0; x < pkg_list->length(); x++) { |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
226 |
// Some of java.base's packages were added early in bootstrapping, ignore duplicates. |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
227 |
if (package_table->lookup_only(pkg_list->at(x)) == NULL) { |
41183
207b92e69457
8163406: The fixup_module_list must be protected by Module_lock when inserting new entries
lfoltan
parents:
40020
diff
changeset
|
228 |
pkg = package_table->locked_create_entry_or_null(pkg_list->at(x), ModuleEntryTable::javabase_moduleEntry()); |
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
229 |
assert(pkg != NULL, "Unable to create a " JAVA_BASE_NAME " package entry"); |
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
230 |
} |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
231 |
// Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
232 |
// the Symbol* that was created above for each package. The refcount was incremented |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
233 |
// by SymbolTable::new_symbol and as well by the PackageEntry creation. |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
234 |
pkg_list->at(x)->decrement_refcount(); |
36508 | 235 |
} |
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
236 |
|
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
237 |
// Finish defining java.base's ModuleEntry |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
238 |
ModuleEntryTable::finalize_javabase(module_handle, version_symbol, location_symbol); |
36508 | 239 |
} |
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
240 |
} |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
241 |
if (duplicate_javabase) { |
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
242 |
THROW_MSG(vmSymbols::java_lang_InternalError(), |
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
243 |
"Module " JAVA_BASE_NAME " is already defined"); |
36508 | 244 |
} |
245 |
||
41691
1be281ee5de4
8166364: fatal error: acquiring lock DirtyCardQ_CBL_mon/16 out of order with lock Module_lock/6 -- possible deadlock
hseigel
parents:
41183
diff
changeset
|
246 |
// Only the thread that actually defined the base module will get here, |
1be281ee5de4
8166364: fatal error: acquiring lock DirtyCardQ_CBL_mon/16 out of order with lock Module_lock/6 -- possible deadlock
hseigel
parents:
41183
diff
changeset
|
247 |
// so no locking is needed. |
1be281ee5de4
8166364: fatal error: acquiring lock DirtyCardQ_CBL_mon/16 out of order with lock Module_lock/6 -- possible deadlock
hseigel
parents:
41183
diff
changeset
|
248 |
|
1be281ee5de4
8166364: fatal error: acquiring lock DirtyCardQ_CBL_mon/16 out of order with lock Module_lock/6 -- possible deadlock
hseigel
parents:
41183
diff
changeset
|
249 |
// Patch any previously loaded class's module field with java.base's java.lang.reflect.Module. |
1be281ee5de4
8166364: fatal error: acquiring lock DirtyCardQ_CBL_mon/16 out of order with lock Module_lock/6 -- possible deadlock
hseigel
parents:
41183
diff
changeset
|
250 |
ModuleEntryTable::patch_javabase_entries(module_handle); |
1be281ee5de4
8166364: fatal error: acquiring lock DirtyCardQ_CBL_mon/16 out of order with lock Module_lock/6 -- possible deadlock
hseigel
parents:
41183
diff
changeset
|
251 |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
252 |
log_debug(modules)("define_javabase_module(): Definition of module: " |
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
253 |
JAVA_BASE_NAME ", version: %s, location: %s, package #: %d", |
36508 | 254 |
module_version != NULL ? module_version : "NULL", |
255 |
module_location != NULL ? module_location : "NULL", |
|
256 |
pkg_list->length()); |
|
257 |
||
258 |
// packages defined to java.base |
|
259 |
for (int x = 0; x < pkg_list->length(); x++) { |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
260 |
log_trace(modules)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME, |
36508 | 261 |
(pkg_list->at(x))->as_C_string()); |
262 |
} |
|
263 |
} |
|
264 |
||
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
265 |
// Caller needs ResourceMark. |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
266 |
void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) { |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
267 |
const char* package_name = package->name()->as_C_string(); |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
268 |
if (package->module()->is_named()) { |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
269 |
THROW_MSG(vmSymbols::java_lang_IllegalStateException(), |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
270 |
err_msg("Package %s for module %s is already in another module, %s, defined to the class loader", |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
271 |
package_name, module_name, package->module()->name()->as_C_string())); |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
272 |
} else { |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
273 |
THROW_MSG(vmSymbols::java_lang_IllegalStateException(), |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
274 |
err_msg("Package %s for module %s is already in the unnamed module defined to the class loader", |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
275 |
package_name, module_name)); |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
276 |
} |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
277 |
} |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
278 |
|
36508 | 279 |
void Modules::define_module(jobject module, jstring version, |
280 |
jstring location, jobjectArray packages, TRAPS) { |
|
281 |
ResourceMark rm(THREAD); |
|
282 |
||
283 |
if (module == NULL) { |
|
284 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); |
|
285 |
} |
|
286 |
Handle module_handle(THREAD, JNIHandles::resolve(module)); |
|
40020
20738e6bef83
8160487: JVM should validate a module by checking for an instance of java.lang.reflect.Module
lfoltan
parents:
40013
diff
changeset
|
287 |
if (!java_lang_reflect_Module::is_instance(module_handle())) { |
36508 | 288 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
40020
20738e6bef83
8160487: JVM should validate a module by checking for an instance of java.lang.reflect.Module
lfoltan
parents:
40013
diff
changeset
|
289 |
"module is not an instance of type java.lang.reflect.Module"); |
36508 | 290 |
} |
291 |
||
292 |
char* module_name = get_module_name(module_handle(), CHECK); |
|
293 |
if (module_name == NULL) { |
|
294 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
295 |
"Module name cannot be null"); |
|
296 |
} |
|
297 |
||
298 |
// Special handling of java.base definition |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
299 |
if (strcmp(module_name, JAVA_BASE_NAME) == 0) { |
36508 | 300 |
define_javabase_module(module, version, location, packages, CHECK); |
301 |
return; |
|
302 |
} |
|
303 |
||
304 |
const char* module_version = get_module_version(version); |
|
305 |
||
42307
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
306 |
oop loader = java_lang_reflect_Module::loader(module_handle()); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
307 |
// Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader. |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
308 |
if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) { |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
309 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
310 |
"Class loader is an invalid delegating class loader"); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
311 |
} |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
312 |
Handle h_loader = Handle(THREAD, loader); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
313 |
|
36508 | 314 |
objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages)); |
315 |
objArrayHandle packages_h(THREAD, packages_oop); |
|
316 |
int num_packages = (packages_h == NULL ? 0 : packages_h->length()); |
|
317 |
||
318 |
// Check that the list of packages has no duplicates and that the |
|
319 |
// packages are syntactically ok. |
|
320 |
GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); |
|
321 |
for (int x = 0; x < num_packages; x++) { |
|
322 |
oop string_obj = packages_h->obj_at(x); |
|
323 |
||
324 |
if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) { |
|
325 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
326 |
err_msg("Bad package name for module: %s", module_name)); |
|
327 |
} |
|
328 |
char *package_name = java_lang_String::as_utf8_string(string_obj); |
|
329 |
if (!verify_package_name(package_name)) { |
|
330 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
331 |
err_msg("Invalid package name: %s for module: %s", |
|
332 |
package_name, module_name)); |
|
333 |
} |
|
42307
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
334 |
|
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
335 |
// Only modules defined to either the boot or platform class loader, can define a "java/" package. |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
336 |
if (!h_loader.is_null() && |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
337 |
!SystemDictionary::is_platform_class_loader(h_loader) && |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
338 |
strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0) { |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
339 |
const char* class_loader_name = SystemDictionary::loader_name(h_loader()); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
340 |
StringUtils::replace_no_expand(package_name, "/", "."); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
341 |
const char* msg_text1 = "Class loader (instance of): "; |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
342 |
const char* msg_text2 = " tried to define prohibited package name: "; |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
343 |
size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(package_name) + 1; |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
344 |
char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
345 |
jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, package_name); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
346 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
347 |
} |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
348 |
|
36508 | 349 |
Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK); |
350 |
// append_if_missing() returns FALSE if entry already exists. |
|
351 |
if (!pkg_list->append_if_missing(pkg_symbol)) { |
|
352 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
353 |
err_msg("Duplicate package name: %s for module %s", |
|
354 |
package_name, module_name)); |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK); |
|
359 |
assert(module_table != NULL, "module entry table shouldn't be null"); |
|
360 |
||
361 |
// Create symbol* entry for module name. |
|
362 |
TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, CHECK); |
|
363 |
||
364 |
bool dupl_modules = false; |
|
365 |
||
366 |
// Create symbol* entry for module version. |
|
367 |
TempNewSymbol version_symbol; |
|
368 |
if (module_version != NULL) { |
|
369 |
version_symbol = SymbolTable::new_symbol(module_version, CHECK); |
|
370 |
} else { |
|
371 |
version_symbol = NULL; |
|
372 |
} |
|
373 |
||
374 |
// Create symbol* entry for module location. |
|
375 |
const char* module_location = NULL; |
|
376 |
TempNewSymbol location_symbol = NULL; |
|
377 |
if (location != NULL) { |
|
378 |
module_location = |
|
379 |
java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location)); |
|
380 |
if (module_location != NULL) { |
|
381 |
location_symbol = SymbolTable::new_symbol(module_location, CHECK); |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
ClassLoaderData* loader_data = ClassLoaderData::class_loader_data_or_null(h_loader()); |
|
386 |
assert(loader_data != NULL, "class loader data shouldn't be null"); |
|
387 |
||
388 |
PackageEntryTable* package_table = NULL; |
|
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
389 |
PackageEntry* existing_pkg = NULL; |
36508 | 390 |
{ |
391 |
MutexLocker ml(Module_lock, THREAD); |
|
392 |
||
393 |
if (num_packages > 0) { |
|
394 |
package_table = get_package_entry_table(h_loader, CHECK); |
|
395 |
assert(package_table != NULL, "Missing package_table"); |
|
396 |
||
397 |
// Check that none of the packages exist in the class loader's package table. |
|
398 |
for (int x = 0; x < pkg_list->length(); x++) { |
|
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
399 |
existing_pkg = package_table->lookup_only(pkg_list->at(x)); |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
400 |
if (existing_pkg != NULL) { |
36508 | 401 |
// This could be because the module was already defined. If so, |
402 |
// report that error instead of the package error. |
|
403 |
if (module_table->lookup_only(module_symbol) != NULL) { |
|
404 |
dupl_modules = true; |
|
405 |
} |
|
406 |
break; |
|
407 |
} |
|
408 |
} |
|
409 |
} // if (num_packages > 0)... |
|
410 |
||
411 |
// Add the module and its packages. |
|
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
412 |
if (!dupl_modules && existing_pkg == NULL) { |
36508 | 413 |
// Create the entry for this module in the class loader's module entry table. |
414 |
ModuleEntry* module_entry = module_table->locked_create_entry_or_null(module_handle, module_symbol, |
|
415 |
version_symbol, location_symbol, loader_data); |
|
416 |
||
417 |
if (module_entry == NULL) { |
|
418 |
dupl_modules = true; |
|
419 |
} else { |
|
420 |
// Add the packages. |
|
421 |
assert(pkg_list->length() == 0 || package_table != NULL, "Bad package table"); |
|
422 |
PackageEntry* pkg; |
|
423 |
for (int y = 0; y < pkg_list->length(); y++) { |
|
424 |
pkg = package_table->locked_create_entry_or_null(pkg_list->at(y), module_entry); |
|
425 |
assert(pkg != NULL, "Unable to create a module's package entry"); |
|
426 |
||
427 |
// Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of |
|
428 |
// the Symbol* that was created above for each package. The refcount was incremented |
|
429 |
// by SymbolTable::new_symbol and as well by the PackageEntry creation. |
|
430 |
pkg_list->at(y)->decrement_refcount(); |
|
431 |
} |
|
432 |
||
433 |
// Store pointer to ModuleEntry record in java.lang.reflect.Module object. |
|
434 |
java_lang_reflect_Module::set_module_entry(module_handle(), module_entry); |
|
435 |
} |
|
436 |
} |
|
437 |
} // Release the lock |
|
438 |
||
439 |
// any errors ? |
|
440 |
if (dupl_modules) { |
|
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
441 |
THROW_MSG(vmSymbols::java_lang_IllegalStateException(), |
36508 | 442 |
err_msg("Module %s is already defined", module_name)); |
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
443 |
} else if (existing_pkg != NULL) { |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
444 |
throw_dup_pkg_exception(module_name, existing_pkg, CHECK); |
36508 | 445 |
} |
446 |
||
447 |
if (log_is_enabled(Debug, modules)) { |
|
37242 | 448 |
outputStream* logst = Log(modules)::debug_stream(); |
36508 | 449 |
logst->print("define_module(): creation of module: %s, version: %s, location: %s, ", |
450 |
module_name, module_version != NULL ? module_version : "NULL", |
|
451 |
module_location != NULL ? module_location : "NULL"); |
|
452 |
loader_data->print_value_on(logst); |
|
453 |
logst->print_cr(", package #: %d", pkg_list->length()); |
|
454 |
for (int y = 0; y < pkg_list->length(); y++) { |
|
455 |
log_trace(modules)("define_module(): creation of package %s for module %s", |
|
456 |
(pkg_list->at(y))->as_C_string(), module_name); |
|
457 |
} |
|
458 |
} |
|
459 |
||
37773
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
460 |
// If the module is defined to the boot loader and an exploded build is being |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
461 |
// used, prepend <java.home>/modules/modules_name, if it exists, to the system boot class path. |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
462 |
if (loader == NULL && |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
463 |
!Universe::is_module_initialized() && |
40013 | 464 |
!ClassLoader::has_jrt_entry()) { |
465 |
ClassLoader::add_to_exploded_build_list(module_symbol, CHECK); |
|
36508 | 466 |
} |
467 |
} |
|
468 |
||
469 |
void Modules::set_bootloader_unnamed_module(jobject module, TRAPS) { |
|
470 |
ResourceMark rm(THREAD); |
|
471 |
||
472 |
if (module == NULL) { |
|
473 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); |
|
474 |
} |
|
475 |
Handle module_handle(THREAD, JNIHandles::resolve(module)); |
|
40020
20738e6bef83
8160487: JVM should validate a module by checking for an instance of java.lang.reflect.Module
lfoltan
parents:
40013
diff
changeset
|
476 |
if (!java_lang_reflect_Module::is_instance(module_handle())) { |
36508 | 477 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
40020
20738e6bef83
8160487: JVM should validate a module by checking for an instance of java.lang.reflect.Module
lfoltan
parents:
40013
diff
changeset
|
478 |
"module is not an instance of type java.lang.reflect.Module"); |
36508 | 479 |
} |
480 |
||
481 |
// Ensure that this is an unnamed module |
|
482 |
oop name = java_lang_reflect_Module::name(module_handle()); |
|
483 |
if (name != NULL) { |
|
484 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
485 |
"boot loader's unnamed module's java.lang.reflect.Module has a name"); |
|
486 |
} |
|
487 |
||
488 |
// Validate java_base's loader is the boot loader. |
|
489 |
oop loader = java_lang_reflect_Module::loader(module_handle()); |
|
490 |
if (loader != NULL) { |
|
491 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
492 |
"Class loader must be the boot class loader"); |
|
493 |
} |
|
494 |
Handle h_loader = Handle(THREAD, loader); |
|
495 |
||
496 |
log_debug(modules)("set_bootloader_unnamed_module(): recording unnamed module for boot loader"); |
|
497 |
||
498 |
// Ensure the boot loader's PackageEntryTable has been created |
|
499 |
ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK); |
|
500 |
||
501 |
// Set java.lang.reflect.Module for the boot loader's unnamed module |
|
502 |
ModuleEntry* unnamed_module = module_table->unnamed_module(); |
|
503 |
assert(unnamed_module != NULL, "boot loader's unnamed ModuleEntry not defined"); |
|
504 |
unnamed_module->set_module(ClassLoaderData::the_null_class_loader_data()->add_handle(module_handle)); |
|
505 |
// Store pointer to the ModuleEntry in the unnamed module's java.lang.reflect.Module object. |
|
506 |
java_lang_reflect_Module::set_module_entry(module_handle(), unnamed_module); |
|
507 |
} |
|
508 |
||
509 |
void Modules::add_module_exports(jobject from_module, jstring package, jobject to_module, TRAPS) { |
|
510 |
if (package == NULL) { |
|
511 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
512 |
"package is null"); |
|
513 |
} |
|
514 |
if (from_module == NULL) { |
|
515 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
516 |
"from_module is null"); |
|
517 |
} |
|
518 |
ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); |
|
519 |
if (from_module_entry == NULL) { |
|
520 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
521 |
"from_module cannot be found"); |
|
522 |
} |
|
523 |
||
524 |
// All packages in unnamed are exported by default. |
|
525 |
if (!from_module_entry->is_named()) return; |
|
526 |
||
527 |
ModuleEntry* to_module_entry; |
|
528 |
if (to_module == NULL) { |
|
529 |
to_module_entry = NULL; // It's an unqualified export. |
|
530 |
} else { |
|
531 |
to_module_entry = get_module_entry(to_module, CHECK); |
|
532 |
if (to_module_entry == NULL) { |
|
533 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
534 |
"to_module is invalid"); |
|
535 |
} |
|
536 |
} |
|
537 |
||
538 |
PackageEntry *package_entry = get_package_entry(from_module_entry, package, CHECK); |
|
539 |
ResourceMark rm(THREAD); |
|
540 |
if (package_entry == NULL) { |
|
541 |
const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
542 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
543 |
err_msg("Package %s not found in from_module %s", |
|
544 |
package_name != NULL ? package_name : "", |
|
545 |
from_module_entry->name()->as_C_string())); |
|
546 |
} |
|
547 |
if (package_entry->module() != from_module_entry) { |
|
548 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
549 |
err_msg("Package: %s found in module %s, not in from_module: %s", |
|
550 |
package_entry->name()->as_C_string(), |
|
551 |
package_entry->module()->name()->as_C_string(), |
|
552 |
from_module_entry->name()->as_C_string())); |
|
553 |
} |
|
554 |
||
555 |
log_debug(modules)("add_module_exports(): package %s in module %s is exported to module %s", |
|
556 |
package_entry->name()->as_C_string(), |
|
557 |
from_module_entry->name()->as_C_string(), |
|
558 |
to_module_entry == NULL ? "NULL" : |
|
559 |
to_module_entry->is_named() ? |
|
560 |
to_module_entry->name()->as_C_string() : UNNAMED_MODULE); |
|
561 |
||
39290
0cc9f5028562
8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
38733
diff
changeset
|
562 |
// Do nothing if modules are the same. |
0cc9f5028562
8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
38733
diff
changeset
|
563 |
if (from_module_entry != to_module_entry) { |
36508 | 564 |
package_entry->set_exported(to_module_entry); |
565 |
} |
|
566 |
} |
|
567 |
||
568 |
||
569 |
void Modules::add_module_exports_qualified(jobject from_module, jstring package, |
|
570 |
jobject to_module, TRAPS) { |
|
571 |
if (to_module == NULL) { |
|
572 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
573 |
"to_module is null"); |
|
574 |
} |
|
575 |
add_module_exports(from_module, package, to_module, CHECK); |
|
576 |
} |
|
577 |
||
578 |
void Modules::add_reads_module(jobject from_module, jobject to_module, TRAPS) { |
|
579 |
if (from_module == NULL) { |
|
580 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
581 |
"from_module is null"); |
|
582 |
} |
|
583 |
||
584 |
ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); |
|
585 |
if (from_module_entry == NULL) { |
|
586 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
587 |
"from_module is not valid"); |
|
588 |
} |
|
589 |
||
590 |
ModuleEntry* to_module_entry; |
|
591 |
if (to_module != NULL) { |
|
592 |
to_module_entry = get_module_entry(to_module, CHECK); |
|
593 |
if (to_module_entry == NULL) { |
|
594 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
595 |
"to_module is invalid"); |
|
596 |
} |
|
597 |
} else { |
|
598 |
to_module_entry = NULL; |
|
599 |
} |
|
600 |
||
601 |
ResourceMark rm(THREAD); |
|
602 |
log_debug(modules)("add_reads_module(): Adding read from module %s to module %s", |
|
603 |
from_module_entry->is_named() ? |
|
604 |
from_module_entry->name()->as_C_string() : UNNAMED_MODULE, |
|
605 |
to_module_entry == NULL ? "all unnamed" : |
|
606 |
(to_module_entry->is_named() ? |
|
607 |
to_module_entry->name()->as_C_string() : UNNAMED_MODULE)); |
|
608 |
||
609 |
// if modules are the same or if from_module is unnamed then no need to add the read. |
|
610 |
if (from_module_entry != to_module_entry && from_module_entry->is_named()) { |
|
611 |
from_module_entry->add_read(to_module_entry); |
|
612 |
} |
|
613 |
} |
|
614 |
||
615 |
// This method is called by JFR and JNI. |
|
616 |
jobject Modules::get_module(jclass clazz, TRAPS) { |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
617 |
assert(ModuleEntryTable::javabase_defined(), |
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
618 |
"Attempt to call get_module before " JAVA_BASE_NAME " is defined"); |
36508 | 619 |
|
620 |
if (clazz == NULL) { |
|
621 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
622 |
"class is null", JNI_FALSE); |
|
623 |
} |
|
624 |
oop mirror = JNIHandles::resolve_non_null(clazz); |
|
625 |
if (mirror == NULL) { |
|
626 |
log_debug(modules)("get_module(): no mirror, returning NULL"); |
|
627 |
return NULL; |
|
628 |
} |
|
629 |
if (!java_lang_Class::is_instance(mirror)) { |
|
630 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
631 |
"Invalid class", JNI_FALSE); |
|
632 |
} |
|
633 |
||
634 |
oop module = java_lang_Class::module(mirror); |
|
635 |
||
636 |
assert(module != NULL, "java.lang.Class module field not set"); |
|
40020
20738e6bef83
8160487: JVM should validate a module by checking for an instance of java.lang.reflect.Module
lfoltan
parents:
40013
diff
changeset
|
637 |
assert(java_lang_reflect_Module::is_instance(module), "module is not an instance of type java.lang.reflect.Module"); |
36508 | 638 |
|
639 |
if (log_is_enabled(Debug, modules)) { |
|
640 |
ResourceMark rm(THREAD); |
|
37242 | 641 |
outputStream* logst = Log(modules)::debug_stream(); |
36508 | 642 |
Klass* klass = java_lang_Class::as_Klass(mirror); |
643 |
oop module_name = java_lang_reflect_Module::name(module); |
|
644 |
if (module_name != NULL) { |
|
645 |
logst->print("get_module(): module "); |
|
646 |
java_lang_String::print(module_name, tty); |
|
647 |
} else { |
|
648 |
logst->print("get_module(): Unamed Module"); |
|
649 |
} |
|
650 |
if (klass != NULL) { |
|
651 |
logst->print_cr(" for class %s", klass->external_name()); |
|
652 |
} else { |
|
653 |
logst->print_cr(" for primitive class"); |
|
654 |
} |
|
655 |
} |
|
656 |
||
657 |
return JNIHandles::make_local(THREAD, module); |
|
658 |
} |
|
659 |
||
660 |
||
661 |
jobject Modules::get_module_by_package_name(jobject loader, jstring package, TRAPS) { |
|
662 |
ResourceMark rm(THREAD); |
|
663 |
assert(ModuleEntryTable::javabase_defined(), |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
664 |
"Attempt to call get_module_from_pkg before " JAVA_BASE_NAME " is defined"); |
36508 | 665 |
|
666 |
if (NULL == package) { |
|
667 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
668 |
"package is null", JNI_FALSE); |
|
669 |
} |
|
670 |
const char* package_str = |
|
671 |
java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
672 |
if (NULL == package_str) { |
|
673 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
674 |
"Invalid package", JNI_FALSE); |
|
675 |
} |
|
676 |
||
677 |
Handle h_loader (THREAD, JNIHandles::resolve(loader)); |
|
678 |
// Check that loader is a subclass of java.lang.ClassLoader. |
|
679 |
if (loader != NULL && !java_lang_ClassLoader::is_subclass(h_loader->klass())) { |
|
680 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
681 |
"Class loader is not a subclass of java.lang.ClassLoader", JNI_FALSE); |
|
682 |
} |
|
683 |
||
684 |
if (strlen(package_str) == 0) { |
|
685 |
// Return the unnamed module |
|
686 |
ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK_NULL); |
|
687 |
if (NULL == module_table) return NULL; |
|
688 |
const ModuleEntry* const unnamed_module = module_table->unnamed_module(); |
|
689 |
return JNIHandles::make_local(THREAD, JNIHandles::resolve(unnamed_module->module())); |
|
690 |
||
691 |
} else { |
|
692 |
TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL); |
|
693 |
return get_module(package_sym, h_loader, CHECK_NULL); |
|
694 |
} |
|
695 |
return NULL; |
|
696 |
} |
|
697 |
||
698 |
||
39706 | 699 |
jobject Modules::get_named_module(Handle h_loader, const char* package_str, TRAPS) { |
700 |
assert(ModuleEntryTable::javabase_defined(), |
|
42636
aafc434ba580
8169734: Update uses of string "java.base" to macro
rprotacio
parents:
41691
diff
changeset
|
701 |
"Attempt to call get_named_module before " JAVA_BASE_NAME " is defined"); |
39706 | 702 |
assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()), |
703 |
"Class loader is not a subclass of java.lang.ClassLoader"); |
|
704 |
assert(package_str != NULL, "the package_str should not be NULL"); |
|
705 |
||
706 |
if (strlen(package_str) == 0) { |
|
707 |
return NULL; |
|
708 |
} |
|
709 |
TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL); |
|
710 |
const PackageEntry* const pkg_entry = |
|
711 |
get_package_entry_by_name(package_sym, h_loader, THREAD); |
|
712 |
const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL); |
|
713 |
||
714 |
if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) { |
|
715 |
return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module())); |
|
716 |
} |
|
717 |
return NULL; |
|
718 |
} |
|
719 |
||
720 |
||
36508 | 721 |
// This method is called by JFR and by the above method. |
722 |
jobject Modules::get_module(Symbol* package_name, Handle h_loader, TRAPS) { |
|
723 |
const PackageEntry* const pkg_entry = |
|
724 |
get_package_entry_by_name(package_name, h_loader, THREAD); |
|
725 |
const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL); |
|
726 |
||
727 |
if (module_entry != NULL && |
|
728 |
module_entry->module() != NULL) { |
|
729 |
return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module())); |
|
730 |
} |
|
731 |
||
732 |
return NULL; |
|
733 |
} |
|
734 |
||
735 |
void Modules::add_module_package(jobject module, jstring package, TRAPS) { |
|
736 |
ResourceMark rm(THREAD); |
|
737 |
||
738 |
if (module == NULL) { |
|
739 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
740 |
"module is null"); |
|
741 |
} |
|
742 |
if (package == NULL) { |
|
743 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
744 |
"package is null"); |
|
745 |
} |
|
746 |
ModuleEntry* module_entry = get_module_entry(module, CHECK); |
|
747 |
if (module_entry == NULL) { |
|
748 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
749 |
"module is invalid"); |
|
750 |
} |
|
751 |
if (!module_entry->is_named()) { |
|
752 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
753 |
"module cannot be an unnamed module"); |
|
754 |
} |
|
755 |
char *package_name = java_lang_String::as_utf8_string( |
|
756 |
JNIHandles::resolve_non_null(package)); |
|
757 |
if (package_name == NULL) { |
|
758 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Bad package"); |
|
759 |
} |
|
760 |
if (!verify_package_name(package_name)) { |
|
761 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
762 |
err_msg("Invalid package name: %s", package_name)); |
|
763 |
} |
|
764 |
||
42307
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
765 |
ClassLoaderData *loader_data = module_entry->loader_data(); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
766 |
|
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
767 |
// Only modules defined to either the boot or platform class loader, can define a "java/" package. |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
768 |
if (!loader_data->is_the_null_class_loader_data() && |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
769 |
!loader_data->is_platform_class_loader_data() && |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
770 |
strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0) { |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
771 |
const char* class_loader_name = SystemDictionary::loader_name(loader_data); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
772 |
StringUtils::replace_no_expand(package_name, "/", "."); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
773 |
const char* msg_text1 = "Class loader (instance of): "; |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
774 |
const char* msg_text2 = " tried to define prohibited package name: "; |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
775 |
size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(package_name) + 1; |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
776 |
char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
777 |
jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, package_name); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
778 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message); |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
779 |
} |
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
780 |
|
36508 | 781 |
log_debug(modules)("add_module_package(): Adding package %s to module %s", |
782 |
package_name, module_entry->name()->as_C_string()); |
|
783 |
||
784 |
TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK); |
|
42307
cefc81dc1d52
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41691
diff
changeset
|
785 |
PackageEntryTable* package_table = loader_data->packages(); |
36508 | 786 |
assert(package_table != NULL, "Missing package_table"); |
787 |
||
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
788 |
PackageEntry* existing_pkg = NULL; |
36508 | 789 |
{ |
790 |
MutexLocker ml(Module_lock, THREAD); |
|
791 |
||
792 |
// Check that the package does not exist in the class loader's package table. |
|
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
793 |
existing_pkg = package_table->lookup_only(pkg_symbol); |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
794 |
if (existing_pkg == NULL) { |
36508 | 795 |
PackageEntry* pkg = package_table->locked_create_entry_or_null(pkg_symbol, module_entry); |
796 |
assert(pkg != NULL, "Unable to create a module's package entry"); |
|
797 |
} |
|
798 |
} |
|
43446
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
799 |
if (existing_pkg != NULL) { |
4f9ac7ab99d9
8172288: Fix Jigsaw related module/package error messages and throw correct exceptions
hseigel
parents:
42656
diff
changeset
|
800 |
throw_dup_pkg_exception(module_entry->name()->as_C_string(), existing_pkg, CHECK); |
36508 | 801 |
} |
802 |
} |
|
803 |
||
804 |
// Export package in module to all unnamed modules. |
|
805 |
void Modules::add_module_exports_to_all_unnamed(jobject module, jstring package, TRAPS) { |
|
806 |
if (module == NULL) { |
|
807 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
808 |
"module is null"); |
|
809 |
} |
|
810 |
if (package == NULL) { |
|
811 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
812 |
"package is null"); |
|
813 |
} |
|
814 |
ModuleEntry* module_entry = get_module_entry(module, CHECK); |
|
815 |
if (module_entry == NULL) { |
|
816 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
817 |
"module is invalid"); |
|
818 |
} |
|
819 |
||
820 |
if (module_entry->is_named()) { // No-op for unnamed module. |
|
821 |
PackageEntry *package_entry = get_package_entry(module_entry, package, CHECK); |
|
822 |
ResourceMark rm(THREAD); |
|
823 |
if (package_entry == NULL) { |
|
824 |
const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
825 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
826 |
err_msg("Package %s not found in module %s", |
|
827 |
package_name != NULL ? package_name : "", |
|
828 |
module_entry->name()->as_C_string())); |
|
829 |
} |
|
830 |
if (package_entry->module() != module_entry) { |
|
831 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
832 |
err_msg("Package: %s found in module %s, not in module: %s", |
|
833 |
package_entry->name()->as_C_string(), |
|
834 |
package_entry->module()->name()->as_C_string(), |
|
835 |
module_entry->name()->as_C_string())); |
|
836 |
} |
|
837 |
||
838 |
log_debug(modules)("add_module_exports_to_all_unnamed(): package %s in module" |
|
839 |
" %s is exported to all unnamed modules", |
|
840 |
package_entry->name()->as_C_string(), |
|
841 |
module_entry->name()->as_C_string()); |
|
842 |
||
843 |
// Mark package as exported to all unnamed modules, unless already |
|
844 |
// unqualifiedly exported. |
|
845 |
if (!package_entry->is_unqual_exported()) { |
|
846 |
package_entry->set_is_exported_allUnnamed(); |
|
847 |
} |
|
848 |
} |
|
849 |
} |