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/moduleEntry.hpp"
|
|
27 |
#include "classfile/packageEntry.hpp"
|
|
28 |
#include "memory/resourceArea.hpp"
|
|
29 |
#include "oops/symbol.hpp"
|
|
30 |
#include "runtime/handles.inline.hpp"
|
|
31 |
#include "trace/traceMacros.hpp"
|
|
32 |
#include "utilities/events.hpp"
|
|
33 |
#include "utilities/growableArray.hpp"
|
|
34 |
#include "utilities/hashtable.inline.hpp"
|
|
35 |
|
|
36 |
// Return true if this package is exported to m.
|
|
37 |
bool PackageEntry::is_qexported_to(ModuleEntry* m) const {
|
|
38 |
assert(m != NULL, "No module to lookup in this package's qualified exports list");
|
|
39 |
MutexLocker m1(Module_lock);
|
|
40 |
if (!_is_exported) {
|
|
41 |
return false;
|
|
42 |
} else if (_is_exported_allUnnamed && !m->is_named()) {
|
|
43 |
return true;
|
|
44 |
} else if (_qualified_exports == NULL) {
|
|
45 |
return false;
|
|
46 |
} else {
|
|
47 |
return _qualified_exports->contains(m);
|
|
48 |
}
|
|
49 |
}
|
|
50 |
|
|
51 |
// Add a module to the package's qualified export list.
|
|
52 |
void PackageEntry::add_qexport(ModuleEntry* m) {
|
|
53 |
assert_locked_or_safepoint(Module_lock);
|
|
54 |
assert(_is_exported == true, "Adding a qualified export to a package that is not exported");
|
|
55 |
if (_qualified_exports == NULL) {
|
|
56 |
// Lazily create a package's qualified exports list.
|
|
57 |
// Initial size is small, do not anticipate export lists to be large.
|
|
58 |
_qualified_exports =
|
|
59 |
new (ResourceObj::C_HEAP, mtClass) GrowableArray<ModuleEntry*>(QUAL_EXP_SIZE, true);
|
|
60 |
}
|
|
61 |
_qualified_exports->append_if_missing(m);
|
|
62 |
}
|
|
63 |
|
|
64 |
// Set the package's exported state based on the value of the ModuleEntry.
|
|
65 |
void PackageEntry::set_exported(ModuleEntry* m) {
|
|
66 |
MutexLocker m1(Module_lock);
|
|
67 |
if (is_unqual_exported()) {
|
|
68 |
// An exception could be thrown, but choose to simply ignore.
|
|
69 |
// Illegal to convert an unqualified exported package to be qualifiedly exported
|
|
70 |
return;
|
|
71 |
}
|
|
72 |
|
|
73 |
if (m == NULL) {
|
|
74 |
// NULL indicates the package is being unqualifiedly exported
|
|
75 |
if (_is_exported && _qualified_exports != NULL) {
|
|
76 |
// Legit to transition a package from being qualifiedly exported
|
|
77 |
// to unqualified. Clean up the qualified lists at the next
|
|
78 |
// safepoint.
|
|
79 |
_exported_pending_delete = _qualified_exports;
|
|
80 |
}
|
|
81 |
|
|
82 |
// Mark package as unqualifiedly exported
|
|
83 |
set_unqual_exported();
|
|
84 |
|
|
85 |
} else {
|
|
86 |
// Add the exported module
|
|
87 |
_is_exported = true;
|
|
88 |
add_qexport(m);
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
// Remove dead module entries within the package's exported list.
|
|
93 |
void PackageEntry::purge_qualified_exports() {
|
|
94 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
|
|
95 |
if (_qualified_exports != NULL) {
|
|
96 |
// Go backwards because this removes entries that are dead.
|
|
97 |
int len = _qualified_exports->length();
|
|
98 |
for (int idx = len - 1; idx >= 0; idx--) {
|
|
99 |
ModuleEntry* module_idx = _qualified_exports->at(idx);
|
|
100 |
ClassLoaderData* cld = module_idx->loader();
|
|
101 |
if (cld->is_unloading()) {
|
|
102 |
_qualified_exports->delete_at(idx);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
void PackageEntry::delete_qualified_exports() {
|
|
109 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
|
|
110 |
if (_exported_pending_delete != NULL) {
|
|
111 |
// If a transition occurred from qualified to unqualified, the _qualified_exports
|
|
112 |
// field should have been NULL'ed out.
|
|
113 |
assert(_qualified_exports == NULL, "Package's exported pending delete, exported list should not be active");
|
|
114 |
delete _exported_pending_delete;
|
|
115 |
}
|
|
116 |
|
|
117 |
if (_qualified_exports != NULL) {
|
|
118 |
delete _qualified_exports;
|
|
119 |
}
|
|
120 |
|
|
121 |
_exported_pending_delete = NULL;
|
|
122 |
_qualified_exports = NULL;
|
|
123 |
}
|
|
124 |
|
|
125 |
PackageEntryTable::PackageEntryTable(int table_size)
|
|
126 |
: Hashtable<Symbol*, mtClass>(table_size, sizeof(PackageEntry))
|
|
127 |
{
|
|
128 |
}
|
|
129 |
|
|
130 |
PackageEntryTable::~PackageEntryTable() {
|
|
131 |
assert_locked_or_safepoint(Module_lock);
|
|
132 |
|
|
133 |
// Walk through all buckets and all entries in each bucket,
|
|
134 |
// freeing each entry.
|
|
135 |
for (int i = 0; i < table_size(); ++i) {
|
|
136 |
for (PackageEntry* p = bucket(i); p != NULL;) {
|
|
137 |
PackageEntry* to_remove = p;
|
|
138 |
// read next before freeing.
|
|
139 |
p = p->next();
|
|
140 |
|
|
141 |
// Clean out the C heap allocated qualified exports list first before freeing the entry
|
|
142 |
to_remove->delete_qualified_exports();
|
|
143 |
to_remove->name()->decrement_refcount();
|
|
144 |
|
|
145 |
// Unlink from the Hashtable prior to freeing
|
|
146 |
unlink_entry(to_remove);
|
|
147 |
FREE_C_HEAP_ARRAY(char, to_remove);
|
|
148 |
}
|
|
149 |
}
|
|
150 |
assert(number_of_entries() == 0, "should have removed all entries");
|
|
151 |
assert(new_entry_free_list() == NULL, "entry present on PackageEntryTable's free list");
|
|
152 |
free_buckets();
|
|
153 |
}
|
|
154 |
|
|
155 |
PackageEntry* PackageEntryTable::new_entry(unsigned int hash, Symbol* name, ModuleEntry* module) {
|
|
156 |
assert_locked_or_safepoint(Module_lock);
|
|
157 |
PackageEntry* entry = (PackageEntry*) NEW_C_HEAP_ARRAY(char, entry_size(), mtClass);
|
|
158 |
|
|
159 |
// Initialize everything BasicHashtable would
|
|
160 |
entry->set_next(NULL);
|
|
161 |
entry->set_hash(hash);
|
|
162 |
entry->set_literal(name);
|
|
163 |
|
|
164 |
TRACE_INIT_PACKAGE_ID(entry);
|
|
165 |
|
|
166 |
// Initialize fields specific to a PackageEntry
|
|
167 |
entry->init();
|
|
168 |
entry->name()->increment_refcount();
|
|
169 |
if (!module->is_named()) {
|
|
170 |
// Set the exported state to true because all packages
|
|
171 |
// within the unnamed module are unqualifiedly exported
|
|
172 |
entry->set_exported(true);
|
|
173 |
}
|
|
174 |
entry->set_module(module);
|
|
175 |
return entry;
|
|
176 |
}
|
|
177 |
|
|
178 |
void PackageEntryTable::add_entry(int index, PackageEntry* new_entry) {
|
|
179 |
assert_locked_or_safepoint(Module_lock);
|
|
180 |
Hashtable<Symbol*, mtClass>::add_entry(index, (HashtableEntry<Symbol*, mtClass>*)new_entry);
|
|
181 |
}
|
|
182 |
|
|
183 |
// Create package in loader's package entry table and return the entry.
|
|
184 |
// If entry already exists, return null. Assume Module lock was taken by caller.
|
|
185 |
PackageEntry* PackageEntryTable::locked_create_entry_or_null(Symbol* name, ModuleEntry* module) {
|
|
186 |
assert_locked_or_safepoint(Module_lock);
|
|
187 |
// Check if package already exists. Return NULL if it does.
|
|
188 |
if (lookup_only(name) != NULL) {
|
|
189 |
return NULL;
|
|
190 |
} else {
|
|
191 |
PackageEntry* entry = new_entry(compute_hash(name), name, module);
|
|
192 |
add_entry(index_for(name), entry);
|
|
193 |
return entry;
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
PackageEntry* PackageEntryTable::lookup(Symbol* name, ModuleEntry* module) {
|
|
198 |
PackageEntry* p = lookup_only(name);
|
|
199 |
if (p != NULL) {
|
|
200 |
return p;
|
|
201 |
} else {
|
|
202 |
// If not found, add to table. Grab the PackageEntryTable lock first.
|
|
203 |
MutexLocker ml(Module_lock);
|
|
204 |
|
|
205 |
// Since look-up was done lock-free, we need to check if another thread beat
|
|
206 |
// us in the race to insert the package.
|
|
207 |
PackageEntry* test = lookup_only(name);
|
|
208 |
if (test != NULL) {
|
|
209 |
// A race occurred and another thread introduced the package.
|
|
210 |
return test;
|
|
211 |
} else {
|
|
212 |
assert(module != NULL, "module should never be null");
|
|
213 |
PackageEntry* entry = new_entry(compute_hash(name), name, module);
|
|
214 |
add_entry(index_for(name), entry);
|
|
215 |
return entry;
|
|
216 |
}
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
PackageEntry* PackageEntryTable::lookup_only(Symbol* name) {
|
|
221 |
int index = index_for(name);
|
|
222 |
for (PackageEntry* p = bucket(index); p != NULL; p = p->next()) {
|
|
223 |
if (p->name()->fast_compare(name) == 0) {
|
|
224 |
return p;
|
|
225 |
}
|
|
226 |
}
|
|
227 |
return NULL;
|
|
228 |
}
|
|
229 |
|
|
230 |
// Called when a define module for java.base is being processed.
|
|
231 |
// Verify the packages loaded thus far are in java.base's package list.
|
|
232 |
void PackageEntryTable::verify_javabase_packages(GrowableArray<Symbol*> *pkg_list) {
|
|
233 |
for (int i = 0; i < table_size(); i++) {
|
|
234 |
for (PackageEntry* entry = bucket(i);
|
|
235 |
entry != NULL;
|
|
236 |
entry = entry->next()) {
|
|
237 |
ModuleEntry* m = entry->module();
|
|
238 |
Symbol* module_name = (m == NULL ? NULL : m->name());
|
|
239 |
if (module_name != NULL &&
|
|
240 |
(module_name->fast_compare(vmSymbols::java_base()) == 0) &&
|
|
241 |
!pkg_list->contains(entry->name())) {
|
|
242 |
ResourceMark rm;
|
|
243 |
vm_exit_during_initialization("A non-java.base package was loaded prior to module system initialization", entry->name()->as_C_string());
|
|
244 |
}
|
|
245 |
}
|
|
246 |
}
|
|
247 |
|
|
248 |
}
|
|
249 |
|
|
250 |
// Remove dead entries from all packages' exported list
|
|
251 |
void PackageEntryTable::purge_all_package_exports() {
|
|
252 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
|
|
253 |
for (int i = 0; i < table_size(); i++) {
|
|
254 |
for (PackageEntry* entry = bucket(i);
|
|
255 |
entry != NULL;
|
|
256 |
entry = entry->next()) {
|
|
257 |
if (entry->exported_pending_delete()) {
|
|
258 |
// exported list is pending deletion due to a transition
|
|
259 |
// from qualified to unqualified
|
|
260 |
entry->delete_qualified_exports();
|
|
261 |
} else if (entry->is_qual_exported()) {
|
|
262 |
entry->purge_qualified_exports();
|
|
263 |
}
|
|
264 |
}
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
#ifndef PRODUCT
|
|
269 |
void PackageEntryTable::print() {
|
|
270 |
tty->print_cr("Package Entry Table (table_size=%d, entries=%d)",
|
|
271 |
table_size(), number_of_entries());
|
|
272 |
for (int i = 0; i < table_size(); i++) {
|
|
273 |
for (PackageEntry* probe = bucket(i);
|
|
274 |
probe != NULL;
|
|
275 |
probe = probe->next()) {
|
|
276 |
probe->print();
|
|
277 |
}
|
|
278 |
}
|
|
279 |
}
|
|
280 |
|
|
281 |
void PackageEntry::print() {
|
|
282 |
ResourceMark rm;
|
|
283 |
tty->print_cr("package entry "PTR_FORMAT" name %s module %s classpath_index "
|
|
284 |
INT32_FORMAT " is_exported %d is_exported_allUnnamed %d " "next "PTR_FORMAT,
|
|
285 |
p2i(this), name()->as_C_string(),
|
|
286 |
(module()->is_named() ? module()->name()->as_C_string() : UNNAMED_MODULE),
|
|
287 |
_classpath_index, _is_exported, _is_exported_allUnnamed, p2i(next()));
|
|
288 |
}
|
|
289 |
#endif
|
|
290 |
|
|
291 |
void PackageEntryTable::verify() {
|
|
292 |
int element_count = 0;
|
|
293 |
for (int index = 0; index < table_size(); index++) {
|
|
294 |
for (PackageEntry* probe = bucket(index);
|
|
295 |
probe != NULL;
|
|
296 |
probe = probe->next()) {
|
|
297 |
probe->verify();
|
|
298 |
element_count++;
|
|
299 |
}
|
|
300 |
}
|
|
301 |
guarantee(number_of_entries() == element_count,
|
|
302 |
"Verify of Package Entry Table failed");
|
|
303 |
debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
|
|
304 |
}
|
|
305 |
|
|
306 |
void PackageEntry::verify() {
|
|
307 |
guarantee(name() != NULL, "A package entry must have a corresponding symbol name.");
|
|
308 |
}
|
|
309 |
|
|
310 |
// iteration of qualified exports
|
|
311 |
void PackageEntry::package_exports_do(ModuleClosure* const f) {
|
|
312 |
assert_locked_or_safepoint(Module_lock);
|
|
313 |
assert(f != NULL, "invariant");
|
|
314 |
|
|
315 |
if (is_qual_exported()) {
|
|
316 |
int qe_len = _qualified_exports->length();
|
|
317 |
|
|
318 |
for (int i = 0; i < qe_len; ++i) {
|
|
319 |
f->do_module(_qualified_exports->at(i));
|
|
320 |
}
|
|
321 |
}
|
|
322 |
}
|