author | lfoltan |
Thu, 21 Apr 2016 09:23:04 -0400 | |
changeset 38014 | 8731fa11f766 |
parent 37503 | 77531df4dad3 |
child 38733 | 2b65f4db449e |
permissions | -rw-r--r-- |
36508 | 1 |
/* |
2 |
* Copyright (c) 2016, 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 "classfile/classLoaderData.hpp" |
|
27 |
#include "classfile/javaClasses.hpp" |
|
28 |
#include "classfile/moduleEntry.hpp" |
|
29 |
#include "logging/log.hpp" |
|
30 |
#include "memory/resourceArea.hpp" |
|
31 |
#include "oops/symbol.hpp" |
|
32 |
#include "prims/jni.h" |
|
33 |
#include "runtime/handles.inline.hpp" |
|
34 |
#include "runtime/safepoint.hpp" |
|
35 |
#include "trace/traceMacros.hpp" |
|
36 |
#include "utilities/events.hpp" |
|
37 |
#include "utilities/growableArray.hpp" |
|
38 |
#include "utilities/hashtable.inline.hpp" |
|
37503
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
39 |
#include "utilities/ostream.hpp" |
36508 | 40 |
|
41 |
ModuleEntry* ModuleEntryTable::_javabase_module = NULL; |
|
42 |
||
43 |
||
44 |
void ModuleEntry::set_location(Symbol* location) { |
|
45 |
if (_location != NULL) { |
|
46 |
// _location symbol's refcounts are managed by ModuleEntry, |
|
47 |
// must decrement the old one before updating. |
|
48 |
_location->decrement_refcount(); |
|
49 |
} |
|
50 |
||
51 |
_location = location; |
|
52 |
||
53 |
if (location != NULL) { |
|
54 |
location->increment_refcount(); |
|
55 |
} |
|
56 |
} |
|
57 |
||
58 |
void ModuleEntry::set_version(Symbol* version) { |
|
59 |
if (_version != NULL) { |
|
60 |
// _version symbol's refcounts are managed by ModuleEntry, |
|
61 |
// must decrement the old one before updating. |
|
62 |
_version->decrement_refcount(); |
|
63 |
} |
|
64 |
||
65 |
_version = version; |
|
66 |
||
67 |
if (version != NULL) { |
|
68 |
version->increment_refcount(); |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
// Returns the shared ProtectionDomain |
|
73 |
Handle ModuleEntry::shared_protection_domain() { |
|
74 |
return Handle(JNIHandles::resolve(_pd)); |
|
75 |
} |
|
76 |
||
77 |
// Set the shared ProtectionDomain atomically |
|
78 |
void ModuleEntry::set_shared_protection_domain(ClassLoaderData *loader_data, |
|
79 |
Handle pd_h) { |
|
80 |
// Create a JNI handle for the shared ProtectionDomain and save it atomically. |
|
81 |
// If someone beats us setting the _pd cache, the created JNI handle is destroyed. |
|
82 |
jobject obj = loader_data->add_handle(pd_h); |
|
83 |
if (Atomic::cmpxchg_ptr(obj, &_pd, NULL) != NULL) { |
|
84 |
loader_data->remove_handle(obj); |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
// Returns true if this module can read module m |
|
89 |
bool ModuleEntry::can_read(ModuleEntry* m) const { |
|
90 |
assert(m != NULL, "No module to lookup in this module's reads list"); |
|
91 |
||
92 |
// Unnamed modules read everyone and all modules |
|
93 |
// read java.base. If either of these conditions |
|
94 |
// hold, readability has been established. |
|
95 |
if (!this->is_named() || |
|
96 |
(m == ModuleEntryTable::javabase_module())) { |
|
97 |
return true; |
|
98 |
} |
|
99 |
||
100 |
MutexLocker m1(Module_lock); |
|
101 |
if (!has_reads()) { |
|
102 |
return false; |
|
103 |
} else { |
|
104 |
return _reads->contains(m); |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
// Add a new module to this module's reads list |
|
109 |
void ModuleEntry::add_read(ModuleEntry* m) { |
|
110 |
MutexLocker m1(Module_lock); |
|
111 |
if (m == NULL) { |
|
112 |
set_can_read_all_unnamed(); |
|
113 |
} else { |
|
114 |
if (_reads == NULL) { |
|
115 |
// Lazily create a module's reads list |
|
116 |
_reads = new (ResourceObj::C_HEAP, mtClass)GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, true); |
|
117 |
} |
|
118 |
_reads->append_if_missing(m); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
bool ModuleEntry::has_reads() const { |
|
123 |
assert_locked_or_safepoint(Module_lock); |
|
124 |
return ((_reads != NULL) && !_reads->is_empty()); |
|
125 |
} |
|
126 |
||
127 |
// Purge dead module entries out of reads list. |
|
128 |
void ModuleEntry::purge_reads() { |
|
129 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
|
130 |
if (has_reads()) { |
|
131 |
// Go backwards because this removes entries that are dead. |
|
132 |
int len = _reads->length(); |
|
133 |
for (int idx = len - 1; idx >= 0; idx--) { |
|
134 |
ModuleEntry* module_idx = _reads->at(idx); |
|
135 |
ClassLoaderData* cld = module_idx->loader(); |
|
136 |
if (cld->is_unloading()) { |
|
137 |
_reads->delete_at(idx); |
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
void ModuleEntry::module_reads_do(ModuleClosure* const f) { |
|
144 |
assert_locked_or_safepoint(Module_lock); |
|
145 |
assert(f != NULL, "invariant"); |
|
146 |
||
147 |
if (has_reads()) { |
|
148 |
int reads_len = _reads->length(); |
|
149 |
for (int i = 0; i < reads_len; ++i) { |
|
150 |
f->do_module(_reads->at(i)); |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
void ModuleEntry::delete_reads() { |
|
156 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
|
157 |
delete _reads; |
|
158 |
_reads = NULL; |
|
159 |
} |
|
160 |
||
161 |
ModuleEntryTable::ModuleEntryTable(int table_size) |
|
162 |
: Hashtable<Symbol*, mtClass>(table_size, sizeof(ModuleEntry)), _unnamed_module(NULL) |
|
163 |
{ |
|
164 |
} |
|
165 |
||
166 |
ModuleEntryTable::~ModuleEntryTable() { |
|
167 |
assert_locked_or_safepoint(Module_lock); |
|
168 |
||
169 |
// Walk through all buckets and all entries in each bucket, |
|
170 |
// freeing each entry. |
|
171 |
for (int i = 0; i < table_size(); ++i) { |
|
172 |
for (ModuleEntry* m = bucket(i); m != NULL;) { |
|
173 |
ModuleEntry* to_remove = m; |
|
174 |
// read next before freeing. |
|
175 |
m = m->next(); |
|
176 |
||
177 |
ResourceMark rm; |
|
178 |
log_debug(modules)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ? |
|
179 |
to_remove->name()->as_C_string() : UNNAMED_MODULE); |
|
180 |
||
181 |
// Clean out the C heap allocated reads list first before freeing the entry |
|
182 |
to_remove->delete_reads(); |
|
183 |
if (to_remove->name() != NULL) { |
|
184 |
to_remove->name()->decrement_refcount(); |
|
185 |
} |
|
186 |
if (to_remove->version() != NULL) { |
|
187 |
to_remove->version()->decrement_refcount(); |
|
188 |
} |
|
189 |
if (to_remove->location() != NULL) { |
|
190 |
to_remove->location()->decrement_refcount(); |
|
191 |
} |
|
192 |
||
193 |
// Unlink from the Hashtable prior to freeing |
|
194 |
unlink_entry(to_remove); |
|
195 |
FREE_C_HEAP_ARRAY(char, to_remove); |
|
196 |
} |
|
197 |
} |
|
198 |
assert(number_of_entries() == 0, "should have removed all entries"); |
|
199 |
assert(new_entry_free_list() == NULL, "entry present on ModuleEntryTable's free list"); |
|
200 |
free_buckets(); |
|
201 |
} |
|
202 |
||
203 |
void ModuleEntryTable::create_unnamed_module(ClassLoaderData* loader_data) { |
|
204 |
assert_locked_or_safepoint(Module_lock); |
|
205 |
||
206 |
// Each ModuleEntryTable has exactly one unnamed module |
|
207 |
if (loader_data->is_the_null_class_loader_data()) { |
|
208 |
// For the boot loader, the java.lang.reflect.Module for the unnamed module |
|
209 |
// is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At |
|
210 |
// this point initially create the ModuleEntry for the unnamed module. |
|
211 |
_unnamed_module = new_entry(0, Handle(NULL), NULL, NULL, NULL, loader_data); |
|
212 |
} else { |
|
213 |
// For all other class loaders the java.lang.reflect.Module for their |
|
214 |
// corresponding unnamed module can be found in the java.lang.ClassLoader object. |
|
215 |
oop module = java_lang_ClassLoader::unnamedModule(loader_data->class_loader()); |
|
216 |
_unnamed_module = new_entry(0, Handle(module), NULL, NULL, NULL, loader_data); |
|
217 |
||
218 |
// Store pointer to the ModuleEntry in the unnamed module's java.lang.reflect.Module |
|
219 |
// object. |
|
220 |
java_lang_reflect_Module::set_module_entry(module, _unnamed_module); |
|
221 |
} |
|
222 |
||
223 |
// Add to bucket 0, no name to hash on |
|
224 |
add_entry(0, _unnamed_module); |
|
225 |
} |
|
226 |
||
227 |
ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle, Symbol* name, |
|
228 |
Symbol* version, Symbol* location, |
|
229 |
ClassLoaderData* loader_data) { |
|
230 |
assert_locked_or_safepoint(Module_lock); |
|
231 |
ModuleEntry* entry = (ModuleEntry*) NEW_C_HEAP_ARRAY(char, entry_size(), mtClass); |
|
232 |
||
233 |
// Initialize everything BasicHashtable would |
|
234 |
entry->set_next(NULL); |
|
235 |
entry->set_hash(hash); |
|
236 |
entry->set_literal(name); |
|
237 |
||
238 |
// Initialize fields specific to a ModuleEntry |
|
239 |
entry->init(); |
|
240 |
if (name != NULL) { |
|
241 |
name->increment_refcount(); |
|
242 |
} else { |
|
243 |
// Unnamed modules can read all other unnamed modules. |
|
244 |
entry->set_can_read_all_unnamed(); |
|
245 |
} |
|
246 |
||
247 |
if (!module_handle.is_null()) { |
|
248 |
entry->set_module(loader_data->add_handle(module_handle)); |
|
249 |
} |
|
250 |
||
251 |
entry->set_loader(loader_data); |
|
252 |
entry->set_version(version); |
|
253 |
entry->set_location(location); |
|
254 |
||
255 |
TRACE_INIT_MODULE_ID(entry); |
|
256 |
||
257 |
return entry; |
|
258 |
} |
|
259 |
||
260 |
void ModuleEntryTable::add_entry(int index, ModuleEntry* new_entry) { |
|
261 |
assert_locked_or_safepoint(Module_lock); |
|
262 |
Hashtable<Symbol*, mtClass>::add_entry(index, (HashtableEntry<Symbol*, mtClass>*)new_entry); |
|
263 |
} |
|
264 |
||
265 |
ModuleEntry* ModuleEntryTable::locked_create_entry_or_null(Handle module_handle, |
|
266 |
Symbol* module_name, |
|
267 |
Symbol* module_version, |
|
268 |
Symbol* module_location, |
|
269 |
ClassLoaderData* loader_data) { |
|
270 |
assert(module_name != NULL, "ModuleEntryTable locked_create_entry_or_null should never be called for unnamed module."); |
|
271 |
assert_locked_or_safepoint(Module_lock); |
|
272 |
// Check if module already exists. |
|
273 |
if (lookup_only(module_name) != NULL) { |
|
274 |
return NULL; |
|
275 |
} else { |
|
276 |
ModuleEntry* entry = new_entry(compute_hash(module_name), module_handle, module_name, |
|
277 |
module_version, module_location, loader_data); |
|
278 |
add_entry(index_for(module_name), entry); |
|
279 |
return entry; |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
// lookup_only by Symbol* to find a ModuleEntry. |
|
284 |
ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) { |
|
285 |
if (name == NULL) { |
|
286 |
// Return this table's unnamed module |
|
287 |
return unnamed_module(); |
|
288 |
} |
|
289 |
int index = index_for(name); |
|
290 |
for (ModuleEntry* m = bucket(index); m != NULL; m = m->next()) { |
|
291 |
if (m->name()->fast_compare(name) == 0) { |
|
292 |
return m; |
|
293 |
} |
|
294 |
} |
|
295 |
return NULL; |
|
296 |
} |
|
297 |
||
298 |
// Remove dead modules from all other alive modules' reads list. |
|
299 |
// This should only occur at class unloading. |
|
300 |
void ModuleEntryTable::purge_all_module_reads() { |
|
301 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
|
302 |
for (int i = 0; i < table_size(); i++) { |
|
303 |
for (ModuleEntry* entry = bucket(i); |
|
304 |
entry != NULL; |
|
305 |
entry = entry->next()) { |
|
306 |
entry->purge_reads(); |
|
307 |
} |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
void ModuleEntryTable::finalize_javabase(Handle module_handle, Symbol* version, Symbol* location) { |
|
312 |
assert_locked_or_safepoint(Module_lock); |
|
313 |
ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data(); |
|
314 |
ModuleEntryTable* module_table = boot_loader_data->modules(); |
|
315 |
||
316 |
assert(module_table != NULL, "boot loader's ModuleEntryTable not defined"); |
|
317 |
||
318 |
if (module_handle.is_null()) { |
|
319 |
fatal("Unable to finalize module definition for java.base"); |
|
320 |
} |
|
321 |
||
322 |
// Set java.lang.reflect.Module, version and location for java.base |
|
323 |
ModuleEntry* jb_module = javabase_module(); |
|
324 |
assert(jb_module != NULL, "java.base ModuleEntry not defined"); |
|
325 |
jb_module->set_module(boot_loader_data->add_handle(module_handle)); |
|
326 |
jb_module->set_version(version); |
|
327 |
jb_module->set_location(location); |
|
328 |
// Store pointer to the ModuleEntry for java.base in the java.lang.reflect.Module object. |
|
329 |
java_lang_reflect_Module::set_module_entry(module_handle(), jb_module); |
|
330 |
} |
|
331 |
||
332 |
void ModuleEntryTable::patch_javabase_entries(Handle module_handle) { |
|
333 |
if (module_handle.is_null()) { |
|
334 |
fatal("Unable to patch the module field of classes loaded prior to java.base's definition, invalid java.lang.reflect.Module"); |
|
335 |
} |
|
336 |
||
337 |
// Do the fixups for the basic primitive types |
|
338 |
java_lang_Class::set_module(Universe::int_mirror(), module_handle()); |
|
339 |
java_lang_Class::set_module(Universe::float_mirror(), module_handle()); |
|
340 |
java_lang_Class::set_module(Universe::double_mirror(), module_handle()); |
|
341 |
java_lang_Class::set_module(Universe::byte_mirror(), module_handle()); |
|
342 |
java_lang_Class::set_module(Universe::bool_mirror(), module_handle()); |
|
343 |
java_lang_Class::set_module(Universe::char_mirror(), module_handle()); |
|
344 |
java_lang_Class::set_module(Universe::long_mirror(), module_handle()); |
|
345 |
java_lang_Class::set_module(Universe::short_mirror(), module_handle()); |
|
346 |
java_lang_Class::set_module(Universe::void_mirror(), module_handle()); |
|
347 |
||
348 |
// Do the fixups for classes that have already been created. |
|
349 |
GrowableArray <Klass*>* list = java_lang_Class::fixup_module_field_list(); |
|
350 |
int list_length = list->length(); |
|
351 |
for (int i = 0; i < list_length; i++) { |
|
352 |
Klass* k = list->at(i); |
|
353 |
assert(k->is_klass(), "List should only hold classes"); |
|
354 |
Thread* THREAD = Thread::current(); |
|
355 |
KlassHandle kh(THREAD, k); |
|
356 |
java_lang_Class::fixup_module_field(kh, module_handle); |
|
38014
8731fa11f766
8152949: Jigsaw crash when Klass in _fixup_module_field_list is unloaded
lfoltan
parents:
37503
diff
changeset
|
357 |
k->class_loader_data()->dec_keep_alive(); |
36508 | 358 |
} |
359 |
||
360 |
delete java_lang_Class::fixup_module_field_list(); |
|
361 |
java_lang_Class::set_fixup_module_field_list(NULL); |
|
362 |
} |
|
363 |
||
37503
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
364 |
void ModuleEntryTable::print(outputStream* st) { |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
365 |
st->print_cr("Module Entry Table (table_size=%d, entries=%d)", |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
366 |
table_size(), number_of_entries()); |
36508 | 367 |
for (int i = 0; i < table_size(); i++) { |
368 |
for (ModuleEntry* probe = bucket(i); |
|
369 |
probe != NULL; |
|
370 |
probe = probe->next()) { |
|
37503
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
371 |
probe->print(st); |
36508 | 372 |
} |
373 |
} |
|
374 |
} |
|
375 |
||
37503
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
376 |
void ModuleEntry::print(outputStream* st) { |
36508 | 377 |
ResourceMark rm; |
37503
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
378 |
st->print_cr("entry "PTR_FORMAT" name %s module "PTR_FORMAT" loader %s version %s location %s strict %s next "PTR_FORMAT, |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
379 |
p2i(this), |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
380 |
name() == NULL ? UNNAMED_MODULE : name()->as_C_string(), |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
381 |
p2i(module()), |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
382 |
loader()->loader_name(), |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
383 |
version() != NULL ? version()->as_C_string() : "NULL", |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
384 |
location() != NULL ? location()->as_C_string() : "NULL", |
77531df4dad3
8152845: Improve PackageEntry and ModuleEntry print methods for future logging
rprotacio
parents:
36508
diff
changeset
|
385 |
BOOL_TO_STR(!can_read_all_unnamed()), p2i(next())); |
36508 | 386 |
} |
387 |
||
388 |
void ModuleEntryTable::verify() { |
|
389 |
int element_count = 0; |
|
390 |
for (int i = 0; i < table_size(); i++) { |
|
391 |
for (ModuleEntry* probe = bucket(i); |
|
392 |
probe != NULL; |
|
393 |
probe = probe->next()) { |
|
394 |
probe->verify(); |
|
395 |
element_count++; |
|
396 |
} |
|
397 |
} |
|
398 |
guarantee(number_of_entries() == element_count, |
|
399 |
"Verify of Module Entry Table failed"); |
|
400 |
debug_only(verify_lookup_length((double)number_of_entries() / table_size())); |
|
401 |
} |
|
402 |
||
403 |
void ModuleEntry::verify() { |
|
404 |
guarantee(loader() != NULL, "A module entry must be associated with a loader."); |
|
405 |
} |