48138
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2017, 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/classFileStream.hpp"
|
|
27 |
#include "classfile/classListParser.hpp"
|
|
28 |
#include "classfile/classLoader.hpp"
|
|
29 |
#include "classfile/classLoaderData.inline.hpp"
|
|
30 |
#include "classfile/classLoaderExt.hpp"
|
|
31 |
#include "classfile/compactHashtable.inline.hpp"
|
|
32 |
#include "classfile/dictionary.hpp"
|
|
33 |
#include "classfile/javaClasses.hpp"
|
|
34 |
#include "classfile/sharedClassUtil.hpp"
|
|
35 |
#include "classfile/symbolTable.hpp"
|
|
36 |
#include "classfile/systemDictionary.hpp"
|
|
37 |
#include "classfile/systemDictionaryShared.hpp"
|
|
38 |
#include "classfile/verificationType.hpp"
|
|
39 |
#include "classfile/vmSymbols.hpp"
|
|
40 |
#include "logging/log.hpp"
|
|
41 |
#include "memory/allocation.hpp"
|
|
42 |
#include "memory/filemap.hpp"
|
|
43 |
#include "memory/metadataFactory.hpp"
|
|
44 |
#include "memory/metaspaceClosure.hpp"
|
|
45 |
#include "memory/oopFactory.hpp"
|
|
46 |
#include "memory/resourceArea.hpp"
|
|
47 |
#include "oops/instanceKlass.hpp"
|
|
48 |
#include "oops/klass.inline.hpp"
|
|
49 |
#include "oops/objArrayOop.inline.hpp"
|
|
50 |
#include "oops/oop.inline.hpp"
|
|
51 |
#include "runtime/java.hpp"
|
|
52 |
#include "runtime/javaCalls.hpp"
|
|
53 |
#include "runtime/mutexLocker.hpp"
|
|
54 |
#include "utilities/hashtable.inline.hpp"
|
|
55 |
#include "utilities/stringUtils.hpp"
|
|
56 |
|
|
57 |
|
|
58 |
objArrayOop SystemDictionaryShared::_shared_protection_domains = NULL;
|
|
59 |
objArrayOop SystemDictionaryShared::_shared_jar_urls = NULL;
|
|
60 |
objArrayOop SystemDictionaryShared::_shared_jar_manifests = NULL;
|
|
61 |
|
|
62 |
static Mutex* SharedDictionary_lock = NULL;
|
|
63 |
|
|
64 |
void SystemDictionaryShared::initialize(TRAPS) {
|
|
65 |
if (_java_system_loader != NULL) {
|
|
66 |
SharedDictionary_lock = new Mutex(Mutex::leaf, "SharedDictionary_lock", true);
|
|
67 |
|
|
68 |
// These classes need to be initialized before calling get_shared_jar_manifest(), etc.
|
|
69 |
SystemDictionary::ByteArrayInputStream_klass()->initialize(CHECK);
|
|
70 |
SystemDictionary::File_klass()->initialize(CHECK);
|
|
71 |
SystemDictionary::Jar_Manifest_klass()->initialize(CHECK);
|
|
72 |
SystemDictionary::CodeSource_klass()->initialize(CHECK);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
oop SystemDictionaryShared::shared_protection_domain(int index) {
|
|
77 |
return _shared_protection_domains->obj_at(index);
|
|
78 |
}
|
|
79 |
|
|
80 |
oop SystemDictionaryShared::shared_jar_url(int index) {
|
|
81 |
return _shared_jar_urls->obj_at(index);
|
|
82 |
}
|
|
83 |
|
|
84 |
oop SystemDictionaryShared::shared_jar_manifest(int index) {
|
|
85 |
return _shared_jar_manifests->obj_at(index);
|
|
86 |
}
|
|
87 |
|
|
88 |
|
|
89 |
Handle SystemDictionaryShared::get_shared_jar_manifest(int shared_path_index, TRAPS) {
|
|
90 |
Handle empty;
|
|
91 |
Handle manifest ;
|
|
92 |
if (shared_jar_manifest(shared_path_index) == NULL) {
|
|
93 |
SharedClassPathEntryExt* ent = (SharedClassPathEntryExt*)FileMapInfo::shared_classpath(shared_path_index);
|
|
94 |
long size = ent->manifest_size();
|
|
95 |
if (size <= 0) {
|
|
96 |
return empty; // No manifest - return NULL handle
|
|
97 |
}
|
|
98 |
|
|
99 |
// ByteArrayInputStream bais = new ByteArrayInputStream(buf);
|
|
100 |
InstanceKlass* bais_klass = SystemDictionary::ByteArrayInputStream_klass();
|
|
101 |
Handle bais = bais_klass->allocate_instance_handle(CHECK_(empty));
|
|
102 |
{
|
|
103 |
const char* src = ent->manifest();
|
|
104 |
assert(src != NULL, "No Manifest data");
|
|
105 |
typeArrayOop buf = oopFactory::new_byteArray(size, CHECK_(empty));
|
|
106 |
typeArrayHandle bufhandle(THREAD, buf);
|
|
107 |
char* dst = (char*)(buf->byte_at_addr(0));
|
|
108 |
memcpy(dst, src, (size_t)size);
|
|
109 |
|
|
110 |
JavaValue result(T_VOID);
|
|
111 |
JavaCalls::call_special(&result, bais, bais_klass,
|
|
112 |
vmSymbols::object_initializer_name(),
|
|
113 |
vmSymbols::byte_array_void_signature(),
|
|
114 |
bufhandle, CHECK_(empty));
|
|
115 |
}
|
|
116 |
|
|
117 |
// manifest = new Manifest(bais)
|
|
118 |
InstanceKlass* manifest_klass = SystemDictionary::Jar_Manifest_klass();
|
|
119 |
manifest = manifest_klass->allocate_instance_handle(CHECK_(empty));
|
|
120 |
{
|
|
121 |
JavaValue result(T_VOID);
|
|
122 |
JavaCalls::call_special(&result, manifest, manifest_klass,
|
|
123 |
vmSymbols::object_initializer_name(),
|
|
124 |
vmSymbols::input_stream_void_signature(),
|
|
125 |
bais, CHECK_(empty));
|
|
126 |
}
|
|
127 |
atomic_set_shared_jar_manifest(shared_path_index, manifest());
|
|
128 |
}
|
|
129 |
|
|
130 |
manifest = Handle(THREAD, shared_jar_manifest(shared_path_index));
|
|
131 |
assert(manifest.not_null(), "sanity");
|
|
132 |
return manifest;
|
|
133 |
}
|
|
134 |
|
|
135 |
Handle SystemDictionaryShared::get_shared_jar_url(int shared_path_index, TRAPS) {
|
|
136 |
Handle url_h;
|
|
137 |
if (shared_jar_url(shared_path_index) == NULL) {
|
|
138 |
JavaValue result(T_OBJECT);
|
|
139 |
const char* path = FileMapInfo::shared_classpath_name(shared_path_index);
|
|
140 |
Handle path_string = java_lang_String::create_from_str(path, CHECK_(url_h));
|
|
141 |
Klass* classLoaders_klass =
|
|
142 |
SystemDictionary::jdk_internal_loader_ClassLoaders_klass();
|
|
143 |
JavaCalls::call_static(&result, classLoaders_klass,
|
|
144 |
vmSymbols::toFileURL_name(),
|
|
145 |
vmSymbols::toFileURL_signature(),
|
|
146 |
path_string, CHECK_(url_h));
|
|
147 |
|
|
148 |
atomic_set_shared_jar_url(shared_path_index, (oop)result.get_jobject());
|
|
149 |
}
|
|
150 |
|
|
151 |
url_h = Handle(THREAD, shared_jar_url(shared_path_index));
|
|
152 |
assert(url_h.not_null(), "sanity");
|
|
153 |
return url_h;
|
|
154 |
}
|
|
155 |
|
|
156 |
Handle SystemDictionaryShared::get_package_name(Symbol* class_name, TRAPS) {
|
|
157 |
ResourceMark rm(THREAD);
|
|
158 |
Handle pkgname_string;
|
|
159 |
char* pkgname = (char*) ClassLoader::package_from_name((const char*) class_name->as_C_string());
|
|
160 |
if (pkgname != NULL) { // Package prefix found
|
|
161 |
StringUtils::replace_no_expand(pkgname, "/", ".");
|
|
162 |
pkgname_string = java_lang_String::create_from_str(pkgname,
|
|
163 |
CHECK_(pkgname_string));
|
|
164 |
}
|
|
165 |
return pkgname_string;
|
|
166 |
}
|
|
167 |
|
|
168 |
// Define Package for shared app classes from JAR file and also checks for
|
|
169 |
// package sealing (all done in Java code)
|
|
170 |
// See http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html
|
|
171 |
void SystemDictionaryShared::define_shared_package(Symbol* class_name,
|
|
172 |
Handle class_loader,
|
|
173 |
Handle manifest,
|
|
174 |
Handle url,
|
|
175 |
TRAPS) {
|
|
176 |
assert(class_loader == _java_system_loader, "unexpected class loader");
|
|
177 |
// get_package_name() returns a NULL handle if the class is in unnamed package
|
|
178 |
Handle pkgname_string = get_package_name(class_name, CHECK);
|
|
179 |
if (pkgname_string.not_null()) {
|
|
180 |
Klass* app_classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_AppClassLoader_klass();
|
|
181 |
JavaValue result(T_OBJECT);
|
|
182 |
JavaCallArguments args(3);
|
|
183 |
args.set_receiver(class_loader);
|
|
184 |
args.push_oop(pkgname_string);
|
|
185 |
args.push_oop(manifest);
|
|
186 |
args.push_oop(url);
|
|
187 |
JavaCalls::call_virtual(&result, app_classLoader_klass,
|
|
188 |
vmSymbols::defineOrCheckPackage_name(),
|
|
189 |
vmSymbols::defineOrCheckPackage_signature(),
|
|
190 |
&args,
|
|
191 |
CHECK);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
// Define Package for shared app/platform classes from named module
|
|
196 |
void SystemDictionaryShared::define_shared_package(Symbol* class_name,
|
|
197 |
Handle class_loader,
|
|
198 |
ModuleEntry* mod_entry,
|
|
199 |
TRAPS) {
|
|
200 |
assert(mod_entry != NULL, "module_entry should not be NULL");
|
|
201 |
Handle module_handle(THREAD, mod_entry->module());
|
|
202 |
|
|
203 |
Handle pkg_name = get_package_name(class_name, CHECK);
|
|
204 |
assert(pkg_name.not_null(), "Package should not be null for class in named module");
|
|
205 |
|
|
206 |
Klass* classLoader_klass;
|
|
207 |
if (SystemDictionary::is_system_class_loader(class_loader())) {
|
|
208 |
classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_AppClassLoader_klass();
|
|
209 |
} else {
|
|
210 |
assert(SystemDictionary::is_platform_class_loader(class_loader()), "unexpected classloader");
|
|
211 |
classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_PlatformClassLoader_klass();
|
|
212 |
}
|
|
213 |
|
|
214 |
JavaValue result(T_OBJECT);
|
|
215 |
JavaCallArguments args(2);
|
|
216 |
args.set_receiver(class_loader);
|
|
217 |
args.push_oop(pkg_name);
|
|
218 |
args.push_oop(module_handle);
|
|
219 |
JavaCalls::call_virtual(&result, classLoader_klass,
|
|
220 |
vmSymbols::definePackage_name(),
|
|
221 |
vmSymbols::definePackage_signature(),
|
|
222 |
&args,
|
|
223 |
CHECK);
|
|
224 |
}
|
|
225 |
|
|
226 |
// Get the ProtectionDomain associated with the CodeSource from the classloader.
|
|
227 |
Handle SystemDictionaryShared::get_protection_domain_from_classloader(Handle class_loader,
|
|
228 |
Handle url, TRAPS) {
|
|
229 |
// CodeSource cs = new CodeSource(url, null);
|
|
230 |
InstanceKlass* cs_klass = SystemDictionary::CodeSource_klass();
|
|
231 |
Handle cs = cs_klass->allocate_instance_handle(CHECK_NH);
|
|
232 |
JavaValue void_result(T_VOID);
|
|
233 |
JavaCalls::call_special(&void_result, cs, cs_klass,
|
|
234 |
vmSymbols::object_initializer_name(),
|
|
235 |
vmSymbols::url_code_signer_array_void_signature(),
|
|
236 |
url, Handle(), CHECK_NH);
|
|
237 |
|
|
238 |
// protection_domain = SecureClassLoader.getProtectionDomain(cs);
|
|
239 |
Klass* secureClassLoader_klass = SystemDictionary::SecureClassLoader_klass();
|
|
240 |
JavaValue obj_result(T_OBJECT);
|
|
241 |
JavaCalls::call_virtual(&obj_result, class_loader, secureClassLoader_klass,
|
|
242 |
vmSymbols::getProtectionDomain_name(),
|
|
243 |
vmSymbols::getProtectionDomain_signature(),
|
|
244 |
cs, CHECK_NH);
|
|
245 |
return Handle(THREAD, (oop)obj_result.get_jobject());
|
|
246 |
}
|
|
247 |
|
|
248 |
// Returns the ProtectionDomain associated with the JAR file identified by the url.
|
|
249 |
Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader,
|
|
250 |
int shared_path_index,
|
|
251 |
Handle url,
|
|
252 |
TRAPS) {
|
|
253 |
Handle protection_domain;
|
|
254 |
if (shared_protection_domain(shared_path_index) == NULL) {
|
|
255 |
Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD);
|
|
256 |
atomic_set_shared_protection_domain(shared_path_index, pd());
|
|
257 |
}
|
|
258 |
|
|
259 |
// Acquire from the cache because if another thread beats the current one to
|
|
260 |
// set the shared protection_domain and the atomic_set fails, the current thread
|
|
261 |
// needs to get the updated protection_domain from the cache.
|
|
262 |
protection_domain = Handle(THREAD, shared_protection_domain(shared_path_index));
|
|
263 |
assert(protection_domain.not_null(), "sanity");
|
|
264 |
return protection_domain;
|
|
265 |
}
|
|
266 |
|
|
267 |
// Returns the ProtectionDomain associated with the moduleEntry.
|
|
268 |
Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader,
|
|
269 |
ModuleEntry* mod, TRAPS) {
|
|
270 |
ClassLoaderData *loader_data = mod->loader_data();
|
|
271 |
Handle protection_domain;
|
|
272 |
if (mod->shared_protection_domain() == NULL) {
|
|
273 |
Symbol* location = mod->location();
|
|
274 |
if (location != NULL) {
|
|
275 |
Handle url_string = java_lang_String::create_from_symbol(
|
|
276 |
location, CHECK_(protection_domain));
|
|
277 |
JavaValue result(T_OBJECT);
|
|
278 |
Klass* classLoaders_klass =
|
|
279 |
SystemDictionary::jdk_internal_loader_ClassLoaders_klass();
|
|
280 |
JavaCalls::call_static(&result, classLoaders_klass, vmSymbols::toFileURL_name(),
|
|
281 |
vmSymbols::toFileURL_signature(),
|
|
282 |
url_string, CHECK_(protection_domain));
|
|
283 |
Handle url = Handle(THREAD, (oop)result.get_jobject());
|
|
284 |
|
|
285 |
Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD);
|
|
286 |
mod->set_shared_protection_domain(loader_data, pd);
|
|
287 |
}
|
|
288 |
}
|
|
289 |
|
|
290 |
protection_domain = Handle(THREAD, mod->shared_protection_domain());
|
|
291 |
assert(protection_domain.not_null(), "sanity");
|
|
292 |
return protection_domain;
|
|
293 |
}
|
|
294 |
|
|
295 |
// Initializes the java.lang.Package and java.security.ProtectionDomain objects associated with
|
|
296 |
// the given InstanceKlass.
|
|
297 |
// Returns the ProtectionDomain for the InstanceKlass.
|
|
298 |
Handle SystemDictionaryShared::init_security_info(Handle class_loader, InstanceKlass* ik, TRAPS) {
|
|
299 |
Handle pd;
|
|
300 |
|
|
301 |
if (ik != NULL) {
|
|
302 |
int index = ik->shared_classpath_index();
|
|
303 |
assert(index >= 0, "Sanity");
|
|
304 |
SharedClassPathEntryExt* ent =
|
|
305 |
(SharedClassPathEntryExt*)FileMapInfo::shared_classpath(index);
|
|
306 |
Symbol* class_name = ik->name();
|
|
307 |
|
|
308 |
if (ent->is_modules_image()) {
|
|
309 |
// For shared app/platform classes originated from the run-time image:
|
|
310 |
// The ProtectionDomains are cached in the corresponding ModuleEntries
|
|
311 |
// for fast access by the VM.
|
|
312 |
ResourceMark rm;
|
|
313 |
ClassLoaderData *loader_data =
|
|
314 |
ClassLoaderData::class_loader_data(class_loader());
|
|
315 |
PackageEntryTable* pkgEntryTable = loader_data->packages();
|
|
316 |
TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK_(pd));
|
|
317 |
if (pkg_name != NULL) {
|
|
318 |
PackageEntry* pkg_entry = pkgEntryTable->lookup_only(pkg_name);
|
|
319 |
if (pkg_entry != NULL) {
|
|
320 |
ModuleEntry* mod_entry = pkg_entry->module();
|
|
321 |
pd = get_shared_protection_domain(class_loader, mod_entry, THREAD);
|
|
322 |
define_shared_package(class_name, class_loader, mod_entry, CHECK_(pd));
|
|
323 |
}
|
|
324 |
}
|
|
325 |
} else {
|
|
326 |
// For shared app/platform classes originated from JAR files on the class path:
|
|
327 |
// Each of the 3 SystemDictionaryShared::_shared_xxx arrays has the same length
|
|
328 |
// as the shared classpath table in the shared archive (see
|
|
329 |
// FileMap::_classpath_entry_table in filemap.hpp for details).
|
|
330 |
//
|
|
331 |
// If a shared InstanceKlass k is loaded from the class path, let
|
|
332 |
//
|
|
333 |
// index = k->shared_classpath_index():
|
|
334 |
//
|
|
335 |
// FileMap::_classpath_entry_table[index] identifies the JAR file that contains k.
|
|
336 |
//
|
|
337 |
// k's protection domain is:
|
|
338 |
//
|
|
339 |
// ProtectionDomain pd = _shared_protection_domains[index];
|
|
340 |
//
|
|
341 |
// and k's Package is initialized using
|
|
342 |
//
|
|
343 |
// manifest = _shared_jar_manifests[index];
|
|
344 |
// url = _shared_jar_urls[index];
|
|
345 |
// define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
|
|
346 |
//
|
|
347 |
// Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by
|
|
348 |
// the corresponding SystemDictionaryShared::get_shared_xxx() function.
|
|
349 |
Handle manifest = get_shared_jar_manifest(index, CHECK_(pd));
|
|
350 |
Handle url = get_shared_jar_url(index, CHECK_(pd));
|
|
351 |
define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
|
|
352 |
pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd));
|
|
353 |
}
|
|
354 |
}
|
|
355 |
return pd;
|
|
356 |
}
|
|
357 |
|
|
358 |
// Currently AppCDS only archives classes from the run-time image, the
|
|
359 |
// -Xbootclasspath/a path, and the class path. The following rules need to be
|
|
360 |
// revised when AppCDS is changed to archive classes from other code sources
|
|
361 |
// in the future, for example the module path (specified by -p).
|
|
362 |
//
|
|
363 |
// Check if a shared class can be loaded by the specific classloader. Following
|
|
364 |
// are the "visible" archived classes for different classloaders.
|
|
365 |
//
|
|
366 |
// NULL classloader:
|
|
367 |
// - see SystemDictionary::is_shared_class_visible()
|
|
368 |
// Platform classloader:
|
|
369 |
// - Module class from "modules" jimage. ModuleEntry must be defined in the
|
|
370 |
// classloader.
|
|
371 |
// App Classloader:
|
|
372 |
// - Module class from "modules" jimage. ModuleEntry must be defined in the
|
|
373 |
// classloader.
|
|
374 |
// - Class from -cp. The class must have no PackageEntry defined in any of the
|
|
375 |
// boot/platform/app classloader, or must be in the unnamed module defined in the
|
|
376 |
// AppClassLoader.
|
|
377 |
bool SystemDictionaryShared::is_shared_class_visible_for_classloader(
|
|
378 |
InstanceKlass* ik,
|
|
379 |
Handle class_loader,
|
|
380 |
const char* pkg_string,
|
|
381 |
Symbol* pkg_name,
|
|
382 |
PackageEntry* pkg_entry,
|
|
383 |
ModuleEntry* mod_entry,
|
|
384 |
TRAPS) {
|
|
385 |
assert(class_loader.not_null(), "Class loader should not be NULL");
|
|
386 |
assert(Universe::is_module_initialized(), "Module system is not initialized");
|
|
387 |
|
|
388 |
int path_index = ik->shared_classpath_index();
|
|
389 |
SharedClassPathEntry* ent =
|
|
390 |
(SharedClassPathEntry*)FileMapInfo::shared_classpath(path_index);
|
|
391 |
|
|
392 |
if (SystemDictionary::is_platform_class_loader(class_loader())) {
|
|
393 |
assert(ent != NULL, "shared class for PlatformClassLoader should have valid SharedClassPathEntry");
|
|
394 |
// The PlatformClassLoader can only load archived class originated from the
|
|
395 |
// run-time image. The class' PackageEntry/ModuleEntry must be
|
|
396 |
// defined by the PlatformClassLoader.
|
|
397 |
if (mod_entry != NULL) {
|
|
398 |
// PackageEntry/ModuleEntry is found in the classloader. Check if the
|
|
399 |
// ModuleEntry's location agrees with the archived class' origination.
|
|
400 |
if (ent->is_modules_image() && mod_entry->location()->starts_with("jrt:")) {
|
|
401 |
return true; // Module class from the "modules" jimage
|
|
402 |
}
|
|
403 |
}
|
|
404 |
} else if (SystemDictionary::is_system_class_loader(class_loader())) {
|
|
405 |
assert(ent != NULL, "shared class for system loader should have valid SharedClassPathEntry");
|
|
406 |
if (pkg_string == NULL) {
|
|
407 |
// The archived class is in the unnamed package. Currently, the boot image
|
|
408 |
// does not contain any class in the unnamed package.
|
|
409 |
assert(!ent->is_modules_image(), "Class in the unnamed package must be from the classpath");
|
|
410 |
if (path_index >= ClassLoaderExt::app_paths_start_index()) {
|
|
411 |
return true;
|
|
412 |
}
|
|
413 |
} else {
|
|
414 |
// Check if this is from a PackageEntry/ModuleEntry defined in the AppClassloader.
|
|
415 |
if (pkg_entry == NULL) {
|
|
416 |
// It's not guaranteed that the class is from the classpath if the
|
|
417 |
// PackageEntry cannot be found from the AppClassloader. Need to check
|
|
418 |
// the boot and platform classloader as well.
|
|
419 |
if (get_package_entry(pkg_name, ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_platform_loader())) == NULL &&
|
|
420 |
get_package_entry(pkg_name, ClassLoaderData::the_null_class_loader_data()) == NULL) {
|
|
421 |
// The PackageEntry is not defined in any of the boot/platform/app classloaders.
|
|
422 |
// The archived class must from -cp path and not from the run-time image.
|
|
423 |
if (!ent->is_modules_image() && path_index >= ClassLoaderExt::app_paths_start_index()) {
|
|
424 |
return true;
|
|
425 |
}
|
|
426 |
}
|
|
427 |
} else if (mod_entry != NULL) {
|
|
428 |
// The package/module is defined in the AppClassLoader. Currently we only
|
|
429 |
// support archiving application module class from the run-time image.
|
|
430 |
// Packages from the -cp path are in the unnamed_module.
|
|
431 |
if ((ent->is_modules_image() && mod_entry->location()->starts_with("jrt:")) ||
|
|
432 |
(pkg_entry->in_unnamed_module() && path_index >= ClassLoaderExt::app_paths_start_index())) {
|
|
433 |
DEBUG_ONLY( \
|
|
434 |
ClassLoaderData* loader_data = class_loader_data(class_loader); \
|
|
435 |
if (pkg_entry->in_unnamed_module()) \
|
|
436 |
assert(mod_entry == loader_data->unnamed_module(), "the unnamed module is not defined in the classloader");)
|
|
437 |
|
|
438 |
return true;
|
|
439 |
}
|
|
440 |
}
|
|
441 |
}
|
|
442 |
} else {
|
|
443 |
// TEMP: if a shared class can be found by a custom loader, consider it visible now.
|
|
444 |
// FIXME: is this actually correct?
|
|
445 |
return true;
|
|
446 |
}
|
|
447 |
return false;
|
|
448 |
}
|
|
449 |
|
|
450 |
// The following stack shows how this code is reached:
|
|
451 |
//
|
|
452 |
// [0] SystemDictionaryShared::find_or_load_shared_class()
|
|
453 |
// [1] JVM_FindLoadedClass
|
|
454 |
// [2] java.lang.ClassLoader.findLoadedClass0()
|
|
455 |
// [3] java.lang.ClassLoader.findLoadedClass()
|
|
456 |
// [4] java.lang.ClassLoader.loadClass()
|
|
457 |
// [5] jdk.internal.loader.ClassLoaders$AppClassLoader_klass.loadClass()
|
|
458 |
//
|
|
459 |
// Because AppCDS supports only the PlatformClassLoader and AppClassLoader, we make the following
|
|
460 |
// assumptions (based on the JDK 8.0 source code):
|
|
461 |
//
|
|
462 |
// [a] these two loaders use the default implementation of
|
|
463 |
// ClassLoader.loadClass(String name, boolean resolve), which
|
|
464 |
// [b] calls findLoadedClass(name), immediately followed by parent.loadClass(),
|
|
465 |
// immediately followed by findClass(name).
|
|
466 |
// [c] If the requested class is a shared class of the current class loader, parent.loadClass()
|
|
467 |
// always returns null, and
|
|
468 |
// [d] if AppCDS is not enabled, the class would be loaded by findClass() by decoding it from a
|
|
469 |
// JAR file and then parsed.
|
|
470 |
//
|
|
471 |
// Given these assumptions, we intercept the findLoadedClass() call to invoke
|
|
472 |
// SystemDictionaryShared::find_or_load_shared_class() to load the shared class from
|
|
473 |
// the archive. The reasons are:
|
|
474 |
//
|
|
475 |
// + Because AppCDS is a commercial feature, we want to hide the implementation. There
|
|
476 |
// is currently no easy way to hide Java code, so we did it with native code.
|
|
477 |
// + Start-up is improved because we avoid decoding the JAR file, and avoid delegating
|
|
478 |
// to the parent (since we know the parent will not find this class).
|
|
479 |
//
|
|
480 |
// NOTE: there's a lot of assumption about the Java code. If any of that change, this
|
|
481 |
// needs to be redesigned.
|
|
482 |
//
|
|
483 |
// An alternative is to modify the Java code of AppClassLoader.loadClass().
|
|
484 |
//
|
|
485 |
InstanceKlass* SystemDictionaryShared::find_or_load_shared_class(
|
|
486 |
Symbol* name, Handle class_loader, TRAPS) {
|
|
487 |
if (DumpSharedSpaces) {
|
|
488 |
return NULL;
|
|
489 |
}
|
|
490 |
|
|
491 |
InstanceKlass* k = NULL;
|
|
492 |
if (shared_dictionary() != NULL &&
|
|
493 |
UseAppCDS && (SystemDictionary::is_system_class_loader(class_loader()) ||
|
|
494 |
SystemDictionary::is_platform_class_loader(class_loader()))) {
|
|
495 |
|
|
496 |
// Fix for 4474172; see evaluation for more details
|
|
497 |
class_loader = Handle(
|
|
498 |
THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
|
|
499 |
ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
|
|
500 |
Dictionary* dictionary = loader_data->dictionary();
|
|
501 |
|
|
502 |
unsigned int d_hash = dictionary->compute_hash(name);
|
|
503 |
|
|
504 |
bool DoObjectLock = true;
|
|
505 |
if (is_parallelCapable(class_loader)) {
|
|
506 |
DoObjectLock = false;
|
|
507 |
}
|
|
508 |
|
|
509 |
// Make sure we are synchronized on the class loader before we proceed
|
|
510 |
//
|
|
511 |
// Note: currently, find_or_load_shared_class is called only from
|
|
512 |
// JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader,
|
|
513 |
// which are parallel-capable loaders, so this lock is NOT taken.
|
|
514 |
Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
|
|
515 |
check_loader_lock_contention(lockObject, THREAD);
|
|
516 |
ObjectLocker ol(lockObject, THREAD, DoObjectLock);
|
|
517 |
|
|
518 |
{
|
|
519 |
MutexLocker mu(SystemDictionary_lock, THREAD);
|
|
520 |
Klass* check = find_class(d_hash, name, dictionary);
|
|
521 |
if (check != NULL) {
|
|
522 |
return InstanceKlass::cast(check);
|
|
523 |
}
|
|
524 |
}
|
|
525 |
|
|
526 |
k = load_shared_class_for_builtin_loader(name, class_loader, THREAD);
|
|
527 |
if (k != NULL) {
|
|
528 |
define_instance_class(k, CHECK_NULL);
|
|
529 |
}
|
|
530 |
}
|
|
531 |
|
|
532 |
return k;
|
|
533 |
}
|
|
534 |
|
|
535 |
InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
|
|
536 |
Symbol* class_name, Handle class_loader, TRAPS) {
|
|
537 |
assert(UseAppCDS && shared_dictionary() != NULL, "already checked");
|
|
538 |
Klass* k = shared_dictionary()->find_class_for_builtin_loader(class_name);
|
|
539 |
|
|
540 |
if (k != NULL) {
|
|
541 |
InstanceKlass* ik = InstanceKlass::cast(k);
|
|
542 |
if ((ik->is_shared_app_class() &&
|
|
543 |
SystemDictionary::is_system_class_loader(class_loader())) ||
|
|
544 |
(ik->is_shared_platform_class() &&
|
|
545 |
SystemDictionary::is_platform_class_loader(class_loader()))) {
|
|
546 |
Handle protection_domain =
|
|
547 |
SystemDictionaryShared::init_security_info(class_loader, ik, CHECK_NULL);
|
|
548 |
return load_shared_class(ik, class_loader, protection_domain, THREAD);
|
|
549 |
}
|
|
550 |
}
|
|
551 |
|
|
552 |
return NULL;
|
|
553 |
}
|
|
554 |
|
|
555 |
void SystemDictionaryShared::oops_do(OopClosure* f) {
|
|
556 |
f->do_oop((oop*)&_shared_protection_domains);
|
|
557 |
f->do_oop((oop*)&_shared_jar_urls);
|
|
558 |
f->do_oop((oop*)&_shared_jar_manifests);
|
|
559 |
}
|
|
560 |
|
|
561 |
void SystemDictionaryShared::allocate_shared_protection_domain_array(int size, TRAPS) {
|
|
562 |
if (_shared_protection_domains == NULL) {
|
|
563 |
_shared_protection_domains = oopFactory::new_objArray(
|
|
564 |
SystemDictionary::ProtectionDomain_klass(), size, CHECK);
|
|
565 |
}
|
|
566 |
}
|
|
567 |
|
|
568 |
void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) {
|
|
569 |
if (_shared_jar_urls == NULL) {
|
|
570 |
_shared_jar_urls = oopFactory::new_objArray(
|
|
571 |
SystemDictionary::URL_klass(), size, CHECK);
|
|
572 |
}
|
|
573 |
}
|
|
574 |
|
|
575 |
void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) {
|
|
576 |
if (_shared_jar_manifests == NULL) {
|
|
577 |
_shared_jar_manifests = oopFactory::new_objArray(
|
|
578 |
SystemDictionary::Jar_Manifest_klass(), size, CHECK);
|
|
579 |
}
|
|
580 |
}
|
|
581 |
|
|
582 |
void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) {
|
|
583 |
allocate_shared_protection_domain_array(size, CHECK);
|
|
584 |
allocate_shared_jar_url_array(size, CHECK);
|
|
585 |
allocate_shared_jar_manifest_array(size, CHECK);
|
|
586 |
}
|
|
587 |
|
|
588 |
|
|
589 |
InstanceKlass* SystemDictionaryShared::lookup_from_stream(const Symbol* class_name,
|
|
590 |
Handle class_loader,
|
|
591 |
Handle protection_domain,
|
|
592 |
const ClassFileStream* cfs,
|
|
593 |
TRAPS) {
|
|
594 |
if (!UseAppCDS || shared_dictionary() == NULL) {
|
|
595 |
return NULL;
|
|
596 |
}
|
|
597 |
if (class_name == NULL) { // don't do this for anonymous classes
|
|
598 |
return NULL;
|
|
599 |
}
|
|
600 |
if (class_loader.is_null() ||
|
|
601 |
SystemDictionary::is_system_class_loader(class_loader()) ||
|
|
602 |
SystemDictionary::is_platform_class_loader(class_loader())) {
|
|
603 |
// This function is called for loading only UNREGISTERED classes.
|
|
604 |
// Do nothing for the BUILTIN loaders.
|
|
605 |
return NULL;
|
|
606 |
}
|
|
607 |
|
|
608 |
ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
|
|
609 |
Klass* k;
|
|
610 |
|
|
611 |
{ // UNREGISTERED loader
|
|
612 |
if (!shared_dictionary()->class_exists_for_unregistered_loader(class_name)) {
|
|
613 |
// No classes of this name for unregistered loaders.
|
|
614 |
return NULL;
|
|
615 |
}
|
|
616 |
|
|
617 |
int clsfile_size = cfs->length();
|
|
618 |
int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
|
|
619 |
|
|
620 |
k = shared_dictionary()->find_class_for_unregistered_loader(class_name,
|
|
621 |
clsfile_size, clsfile_crc32);
|
|
622 |
}
|
|
623 |
|
|
624 |
if (k == NULL) { // not archived
|
|
625 |
return NULL;
|
|
626 |
}
|
|
627 |
|
|
628 |
return acquire_class_for_current_thread(InstanceKlass::cast(k), class_loader,
|
|
629 |
protection_domain, THREAD);
|
|
630 |
}
|
|
631 |
|
|
632 |
InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread(
|
|
633 |
InstanceKlass *ik,
|
|
634 |
Handle class_loader,
|
|
635 |
Handle protection_domain,
|
|
636 |
TRAPS) {
|
|
637 |
ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
|
|
638 |
|
|
639 |
{
|
|
640 |
MutexLocker mu(SharedDictionary_lock, THREAD);
|
|
641 |
if (ik->class_loader_data() != NULL) {
|
|
642 |
// ik is already loaded (by this loader or by a different loader)
|
|
643 |
// or ik is being loaded by a different thread (by this loader or by a different loader)
|
|
644 |
return NULL;
|
|
645 |
}
|
|
646 |
|
|
647 |
// No other thread has acquired this yet, so give it to *this thread*
|
|
648 |
ik->set_class_loader_data(loader_data);
|
|
649 |
}
|
|
650 |
|
|
651 |
// No longer holding SharedDictionary_lock
|
|
652 |
// No need to lock, as <ik> can be held only by a single thread.
|
|
653 |
loader_data->add_class(ik);
|
|
654 |
|
|
655 |
// Load and check super/interfaces, restore unsharable info
|
|
656 |
InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, THREAD);
|
|
657 |
if (shared_klass == NULL || HAS_PENDING_EXCEPTION) {
|
|
658 |
// TODO: clean up <ik> so it can be used again
|
|
659 |
return NULL;
|
|
660 |
}
|
|
661 |
|
|
662 |
return shared_klass;
|
|
663 |
}
|
|
664 |
|
|
665 |
bool SystemDictionaryShared::add_non_builtin_klass(Symbol* name, ClassLoaderData* loader_data,
|
|
666 |
InstanceKlass* k,
|
|
667 |
TRAPS) {
|
|
668 |
assert(DumpSharedSpaces, "only when dumping");
|
|
669 |
assert(UseAppCDS && boot_loader_dictionary() != NULL, "must be");
|
|
670 |
|
|
671 |
if (boot_loader_dictionary()->add_non_builtin_klass(name, loader_data, k)) {
|
|
672 |
MutexLocker mu_r(Compile_lock, THREAD); // not really necessary, but add_to_hierarchy asserts this.
|
|
673 |
add_to_hierarchy(k, CHECK_0);
|
|
674 |
return true;
|
|
675 |
}
|
|
676 |
return false;
|
|
677 |
}
|
|
678 |
|
|
679 |
// This function is called to resolve the super/interfaces of shared classes for
|
|
680 |
// non-built-in loaders. E.g., ChildClass in the below example
|
|
681 |
// where "super:" (and optionally "interface:") have been specified.
|
|
682 |
//
|
|
683 |
// java/lang/Object id: 0
|
|
684 |
// Interface id: 2 super: 0 source: cust.jar
|
|
685 |
// ChildClass id: 4 super: 0 interfaces: 2 source: cust.jar
|
|
686 |
Klass* SystemDictionaryShared::dump_time_resolve_super_or_fail(
|
|
687 |
Symbol* child_name, Symbol* class_name, Handle class_loader,
|
|
688 |
Handle protection_domain, bool is_superclass, TRAPS) {
|
|
689 |
|
|
690 |
assert(DumpSharedSpaces, "only when dumping");
|
|
691 |
|
|
692 |
ClassListParser* parser = ClassListParser::instance();
|
|
693 |
if (parser == NULL) {
|
|
694 |
// We're still loading the well-known classes, before the ClassListParser is created.
|
|
695 |
return NULL;
|
|
696 |
}
|
|
697 |
if (child_name->equals(parser->current_class_name())) {
|
|
698 |
// When this function is called, all the numbered super and interface types
|
|
699 |
// must have already been loaded. Hence this function is never recursively called.
|
|
700 |
if (is_superclass) {
|
|
701 |
return parser->lookup_super_for_current_class(class_name);
|
|
702 |
} else {
|
|
703 |
return parser->lookup_interface_for_current_class(class_name);
|
|
704 |
}
|
|
705 |
} else {
|
|
706 |
// The VM is not trying to resolve a super type of parser->current_class_name().
|
|
707 |
// Instead, it's resolving an error class (because parser->current_class_name() has
|
|
708 |
// failed parsing or verification). Don't do anything here.
|
|
709 |
return NULL;
|
|
710 |
}
|
|
711 |
}
|
|
712 |
|
|
713 |
struct SharedMiscInfo {
|
|
714 |
Klass* _klass;
|
|
715 |
int _clsfile_size;
|
|
716 |
int _clsfile_crc32;
|
|
717 |
};
|
|
718 |
|
|
719 |
static GrowableArray<SharedMiscInfo>* misc_info_array = NULL;
|
|
720 |
|
|
721 |
void SystemDictionaryShared::set_shared_class_misc_info(Klass* k, ClassFileStream* cfs) {
|
|
722 |
assert(DumpSharedSpaces, "only when dumping");
|
|
723 |
int clsfile_size = cfs->length();
|
|
724 |
int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
|
|
725 |
|
|
726 |
if (misc_info_array == NULL) {
|
|
727 |
misc_info_array = new (ResourceObj::C_HEAP, mtClass) GrowableArray<SharedMiscInfo>(20, /*c heap*/ true);
|
|
728 |
}
|
|
729 |
|
|
730 |
SharedMiscInfo misc_info;
|
|
731 |
DEBUG_ONLY({
|
|
732 |
for (int i=0; i<misc_info_array->length(); i++) {
|
|
733 |
misc_info = misc_info_array->at(i);
|
|
734 |
assert(misc_info._klass != k, "cannot call set_shared_class_misc_info twice for the same class");
|
|
735 |
}
|
|
736 |
});
|
|
737 |
|
|
738 |
misc_info._klass = k;
|
|
739 |
misc_info._clsfile_size = clsfile_size;
|
|
740 |
misc_info._clsfile_crc32 = clsfile_crc32;
|
|
741 |
|
|
742 |
misc_info_array->append(misc_info);
|
|
743 |
}
|
|
744 |
|
|
745 |
void SystemDictionaryShared::init_shared_dictionary_entry(Klass* k, DictionaryEntry* ent) {
|
|
746 |
SharedDictionaryEntry* entry = (SharedDictionaryEntry*)ent;
|
|
747 |
entry->_id = -1;
|
|
748 |
entry->_clsfile_size = -1;
|
|
749 |
entry->_clsfile_crc32 = -1;
|
|
750 |
entry->_verifier_constraints = NULL;
|
|
751 |
entry->_verifier_constraint_flags = NULL;
|
|
752 |
|
|
753 |
if (misc_info_array != NULL) {
|
|
754 |
for (int i=0; i<misc_info_array->length(); i++) {
|
|
755 |
SharedMiscInfo misc_info = misc_info_array->at(i);
|
|
756 |
if (misc_info._klass == k) {
|
|
757 |
entry->_clsfile_size = misc_info._clsfile_size;
|
|
758 |
entry->_clsfile_crc32 = misc_info._clsfile_crc32;
|
|
759 |
misc_info_array->remove_at(i);
|
|
760 |
return;
|
|
761 |
}
|
|
762 |
}
|
|
763 |
}
|
|
764 |
}
|
|
765 |
|
|
766 |
bool SystemDictionaryShared::add_verification_constraint(Klass* k, Symbol* name,
|
|
767 |
Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
|
|
768 |
assert(DumpSharedSpaces, "called at dump time only");
|
|
769 |
|
|
770 |
// Skip anonymous classes, which are not archived as they are not in
|
|
771 |
// dictionary (see assert_no_anonymoys_classes_in_dictionaries() in
|
|
772 |
// VM_PopulateDumpSharedSpace::doit()).
|
|
773 |
if (k->class_loader_data()->is_anonymous()) {
|
|
774 |
return true; // anonymous classes are not archived, skip
|
|
775 |
}
|
|
776 |
|
|
777 |
SharedDictionaryEntry* entry = ((SharedDictionary*)(k->class_loader_data()->dictionary()))->find_entry_for(k);
|
|
778 |
ResourceMark rm;
|
|
779 |
// Lambda classes are not archived and will be regenerated at runtime.
|
|
780 |
if (entry == NULL && strstr(k->name()->as_C_string(), "Lambda$") != NULL) {
|
|
781 |
return true;
|
|
782 |
}
|
|
783 |
assert(entry != NULL, "class should be in dictionary before being verified");
|
|
784 |
entry->add_verification_constraint(name, from_name, from_field_is_protected,
|
|
785 |
from_is_array, from_is_object);
|
|
786 |
if (entry->is_builtin()) {
|
|
787 |
// For builtin class loaders, we can try to complete the verification check at dump time,
|
|
788 |
// because we can resolve all the constraint classes.
|
|
789 |
return false;
|
|
790 |
} else {
|
|
791 |
// For non-builtin class loaders, we cannot complete the verification check at dump time,
|
|
792 |
// because at dump time we don't know how to resolve classes for such loaders.
|
|
793 |
return true;
|
|
794 |
}
|
|
795 |
}
|
|
796 |
|
|
797 |
void SystemDictionaryShared::finalize_verification_constraints() {
|
|
798 |
boot_loader_dictionary()->finalize_verification_constraints();
|
|
799 |
}
|
|
800 |
|
|
801 |
void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
|
|
802 |
TRAPS) {
|
|
803 |
assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only");
|
|
804 |
SharedDictionaryEntry* entry = shared_dictionary()->find_entry_for(klass);
|
|
805 |
assert(entry != NULL, "call this only for shared classes");
|
|
806 |
entry->check_verification_constraints(klass, THREAD);
|
|
807 |
}
|
|
808 |
|
|
809 |
SharedDictionaryEntry* SharedDictionary::find_entry_for(Klass* klass) {
|
|
810 |
Symbol* class_name = klass->name();
|
|
811 |
unsigned int hash = compute_hash(class_name);
|
|
812 |
int index = hash_to_index(hash);
|
|
813 |
|
|
814 |
for (SharedDictionaryEntry* entry = bucket(index);
|
|
815 |
entry != NULL;
|
|
816 |
entry = entry->next()) {
|
|
817 |
if (entry->hash() == hash && entry->literal() == klass) {
|
|
818 |
return entry;
|
|
819 |
}
|
|
820 |
}
|
|
821 |
|
|
822 |
return NULL;
|
|
823 |
}
|
|
824 |
|
|
825 |
void SharedDictionary::finalize_verification_constraints() {
|
|
826 |
int bytes = 0, count = 0;
|
|
827 |
for (int index = 0; index < table_size(); index++) {
|
|
828 |
for (SharedDictionaryEntry *probe = bucket(index);
|
|
829 |
probe != NULL;
|
|
830 |
probe = probe->next()) {
|
|
831 |
int n = probe->finalize_verification_constraints();
|
|
832 |
if (n > 0) {
|
|
833 |
bytes += n;
|
|
834 |
count ++;
|
|
835 |
}
|
|
836 |
}
|
|
837 |
}
|
|
838 |
if (log_is_enabled(Info, cds, verification)) {
|
|
839 |
double avg = 0;
|
|
840 |
if (count > 0) {
|
|
841 |
avg = double(bytes) / double(count);
|
|
842 |
}
|
|
843 |
log_info(cds, verification)("Recorded verification constraints for %d classes = %d bytes (avg = %.2f bytes) ", count, bytes, avg);
|
|
844 |
}
|
|
845 |
}
|
|
846 |
|
|
847 |
void SharedDictionaryEntry::add_verification_constraint(Symbol* name,
|
|
848 |
Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
|
|
849 |
if (_verifier_constraints == NULL) {
|
|
850 |
_verifier_constraints = new(ResourceObj::C_HEAP, mtClass) GrowableArray<Symbol*>(8, true, mtClass);
|
|
851 |
}
|
|
852 |
if (_verifier_constraint_flags == NULL) {
|
|
853 |
_verifier_constraint_flags = new(ResourceObj::C_HEAP, mtClass) GrowableArray<char>(4, true, mtClass);
|
|
854 |
}
|
|
855 |
GrowableArray<Symbol*>* vc_array = (GrowableArray<Symbol*>*)_verifier_constraints;
|
|
856 |
for (int i=0; i<vc_array->length(); i+= 2) {
|
|
857 |
if (name == vc_array->at(i) &&
|
|
858 |
from_name == vc_array->at(i+1)) {
|
|
859 |
return;
|
|
860 |
}
|
|
861 |
}
|
|
862 |
vc_array->append(name);
|
|
863 |
vc_array->append(from_name);
|
|
864 |
|
|
865 |
GrowableArray<char>* vcflags_array = (GrowableArray<char>*)_verifier_constraint_flags;
|
|
866 |
char c = 0;
|
|
867 |
c |= from_field_is_protected ? FROM_FIELD_IS_PROTECTED : 0;
|
|
868 |
c |= from_is_array ? FROM_IS_ARRAY : 0;
|
|
869 |
c |= from_is_object ? FROM_IS_OBJECT : 0;
|
|
870 |
vcflags_array->append(c);
|
|
871 |
|
|
872 |
if (log_is_enabled(Trace, cds, verification)) {
|
|
873 |
ResourceMark rm;
|
|
874 |
log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s",
|
|
875 |
instance_klass()->external_name(), from_name->as_klass_external_name(),
|
|
876 |
name->as_klass_external_name());
|
|
877 |
}
|
|
878 |
}
|
|
879 |
|
|
880 |
int SharedDictionaryEntry::finalize_verification_constraints() {
|
|
881 |
assert(DumpSharedSpaces, "called at dump time only");
|
|
882 |
Thread* THREAD = Thread::current();
|
|
883 |
ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
|
|
884 |
GrowableArray<Symbol*>* vc_array = (GrowableArray<Symbol*>*)_verifier_constraints;
|
|
885 |
GrowableArray<char>* vcflags_array = (GrowableArray<char>*)_verifier_constraint_flags;
|
|
886 |
|
|
887 |
if (vc_array != NULL) {
|
|
888 |
if (log_is_enabled(Trace, cds, verification)) {
|
|
889 |
ResourceMark rm;
|
|
890 |
log_trace(cds, verification)("finalize_verification_constraint: %s",
|
|
891 |
literal()->external_name());
|
|
892 |
}
|
|
893 |
|
|
894 |
// Copy the constraints from C_HEAP-alloced GrowableArrays to Metaspace-alloced
|
|
895 |
// Arrays
|
|
896 |
int size = 0;
|
|
897 |
{
|
|
898 |
// FIXME: change this to be done after relocation, so we can use symbol offset??
|
|
899 |
int length = vc_array->length();
|
|
900 |
Array<Symbol*>* out = MetadataFactory::new_array<Symbol*>(loader_data, length, 0, THREAD);
|
|
901 |
assert(out != NULL, "Dump time allocation failure would have aborted VM");
|
|
902 |
for (int i=0; i<length; i++) {
|
|
903 |
out->at_put(i, vc_array->at(i));
|
|
904 |
}
|
|
905 |
_verifier_constraints = out;
|
|
906 |
size += out->size() * BytesPerWord;
|
|
907 |
delete vc_array;
|
|
908 |
}
|
|
909 |
{
|
|
910 |
int length = vcflags_array->length();
|
|
911 |
Array<char>* out = MetadataFactory::new_array<char>(loader_data, length, 0, THREAD);
|
|
912 |
assert(out != NULL, "Dump time allocation failure would have aborted VM");
|
|
913 |
for (int i=0; i<length; i++) {
|
|
914 |
out->at_put(i, vcflags_array->at(i));
|
|
915 |
}
|
|
916 |
_verifier_constraint_flags = out;
|
|
917 |
size += out->size() * BytesPerWord;
|
|
918 |
delete vcflags_array;
|
|
919 |
}
|
|
920 |
|
|
921 |
return size;
|
|
922 |
}
|
|
923 |
return 0;
|
|
924 |
}
|
|
925 |
|
|
926 |
void SharedDictionaryEntry::check_verification_constraints(InstanceKlass* klass, TRAPS) {
|
|
927 |
Array<Symbol*>* vc_array = (Array<Symbol*>*)_verifier_constraints;
|
|
928 |
Array<char>* vcflags_array = (Array<char>*)_verifier_constraint_flags;
|
|
929 |
|
|
930 |
if (vc_array != NULL) {
|
|
931 |
int length = vc_array->length();
|
|
932 |
for (int i=0; i<length; i+=2) {
|
|
933 |
Symbol* name = vc_array->at(i);
|
|
934 |
Symbol* from_name = vc_array->at(i+1);
|
|
935 |
char c = vcflags_array->at(i/2);
|
|
936 |
|
|
937 |
bool from_field_is_protected = (c & FROM_FIELD_IS_PROTECTED) ? true : false;
|
|
938 |
bool from_is_array = (c & FROM_IS_ARRAY) ? true : false;
|
|
939 |
bool from_is_object = (c & FROM_IS_OBJECT) ? true : false;
|
|
940 |
|
|
941 |
bool ok = VerificationType::resolve_and_check_assignability(klass, name,
|
|
942 |
from_name, from_field_is_protected, from_is_array, from_is_object, CHECK);
|
|
943 |
if (!ok) {
|
|
944 |
ResourceMark rm(THREAD);
|
|
945 |
stringStream ss;
|
|
946 |
|
|
947 |
ss.print_cr("Bad type on operand stack");
|
|
948 |
ss.print_cr("Exception Details:");
|
|
949 |
ss.print_cr(" Location:\n %s", klass->name()->as_C_string());
|
|
950 |
ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'",
|
|
951 |
from_name->as_quoted_ascii(), name->as_quoted_ascii());
|
|
952 |
THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string());
|
|
953 |
}
|
|
954 |
}
|
|
955 |
}
|
|
956 |
}
|
|
957 |
|
|
958 |
void SharedDictionaryEntry::metaspace_pointers_do(MetaspaceClosure* it) {
|
|
959 |
it->push((Array<Symbol*>**)&_verifier_constraints);
|
|
960 |
it->push((Array<char>**)&_verifier_constraint_flags);
|
|
961 |
}
|
|
962 |
|
|
963 |
bool SharedDictionary::add_non_builtin_klass(const Symbol* class_name,
|
|
964 |
ClassLoaderData* loader_data,
|
|
965 |
InstanceKlass* klass) {
|
|
966 |
|
|
967 |
assert(DumpSharedSpaces, "supported only when dumping");
|
|
968 |
assert(klass != NULL, "adding NULL klass");
|
|
969 |
assert(klass->name() == class_name, "sanity check on name");
|
|
970 |
assert(klass->shared_classpath_index() < 0,
|
|
971 |
"the shared classpath index should not be set for shared class loaded by the custom loaders");
|
|
972 |
|
|
973 |
// Add an entry for a non-builtin class.
|
|
974 |
// For a shared class for custom class loaders, SystemDictionary::resolve_or_null will
|
|
975 |
// not find this class, because is_builtin() is false.
|
|
976 |
unsigned int hash = compute_hash(class_name);
|
|
977 |
int index = hash_to_index(hash);
|
|
978 |
|
|
979 |
for (SharedDictionaryEntry* entry = bucket(index);
|
|
980 |
entry != NULL;
|
|
981 |
entry = entry->next()) {
|
|
982 |
if (entry->hash() == hash) {
|
|
983 |
Klass* klass = (Klass*)entry->literal();
|
|
984 |
if (klass->name() == class_name && klass->class_loader_data() == loader_data) {
|
|
985 |
// There is already a class defined with the same name
|
|
986 |
return false;
|
|
987 |
}
|
|
988 |
}
|
|
989 |
}
|
|
990 |
|
|
991 |
assert(Dictionary::entry_size() >= sizeof(SharedDictionaryEntry), "must be big enough");
|
|
992 |
SharedDictionaryEntry* entry = (SharedDictionaryEntry*)new_entry(hash, klass);
|
|
993 |
add_entry(index, entry);
|
|
994 |
|
|
995 |
assert(entry->is_unregistered(), "sanity");
|
|
996 |
assert(!entry->is_builtin(), "sanity");
|
|
997 |
return true;
|
|
998 |
}
|
|
999 |
|
|
1000 |
|
|
1001 |
//-----------------
|
|
1002 |
// SharedDictionary
|
|
1003 |
//-----------------
|
|
1004 |
|
|
1005 |
|
|
1006 |
Klass* SharedDictionary::find_class_for_builtin_loader(const Symbol* name) const {
|
|
1007 |
SharedDictionaryEntry* entry = get_entry_for_builtin_loader(name);
|
|
1008 |
return entry != NULL ? entry->instance_klass() : (Klass*)NULL;
|
|
1009 |
}
|
|
1010 |
|
|
1011 |
Klass* SharedDictionary::find_class_for_unregistered_loader(const Symbol* name,
|
|
1012 |
int clsfile_size,
|
|
1013 |
int clsfile_crc32) const {
|
|
1014 |
|
|
1015 |
const SharedDictionaryEntry* entry = get_entry_for_unregistered_loader(name,
|
|
1016 |
clsfile_size,
|
|
1017 |
clsfile_crc32);
|
|
1018 |
return entry != NULL ? entry->instance_klass() : (Klass*)NULL;
|
|
1019 |
}
|
|
1020 |
|
|
1021 |
void SharedDictionary::update_entry(Klass* klass, int id) {
|
|
1022 |
assert(DumpSharedSpaces, "supported only when dumping");
|
|
1023 |
Symbol* class_name = klass->name();
|
|
1024 |
unsigned int hash = compute_hash(class_name);
|
|
1025 |
int index = hash_to_index(hash);
|
|
1026 |
|
|
1027 |
for (SharedDictionaryEntry* entry = bucket(index);
|
|
1028 |
entry != NULL;
|
|
1029 |
entry = entry->next()) {
|
|
1030 |
if (entry->hash() == hash && entry->literal() == klass) {
|
|
1031 |
entry->_id = id;
|
|
1032 |
return;
|
|
1033 |
}
|
|
1034 |
}
|
|
1035 |
|
|
1036 |
ShouldNotReachHere();
|
|
1037 |
}
|
|
1038 |
|
|
1039 |
SharedDictionaryEntry* SharedDictionary::get_entry_for_builtin_loader(const Symbol* class_name) const {
|
|
1040 |
assert(!DumpSharedSpaces, "supported only when at runtime");
|
|
1041 |
unsigned int hash = compute_hash(class_name);
|
|
1042 |
const int index = hash_to_index(hash);
|
|
1043 |
|
|
1044 |
for (SharedDictionaryEntry* entry = bucket(index);
|
|
1045 |
entry != NULL;
|
|
1046 |
entry = entry->next()) {
|
|
1047 |
if (entry->hash() == hash && entry->equals(class_name)) {
|
|
1048 |
if (entry->is_builtin()) {
|
|
1049 |
return entry;
|
|
1050 |
}
|
|
1051 |
}
|
|
1052 |
}
|
|
1053 |
return NULL;
|
|
1054 |
}
|
|
1055 |
|
|
1056 |
SharedDictionaryEntry* SharedDictionary::get_entry_for_unregistered_loader(const Symbol* class_name,
|
|
1057 |
int clsfile_size,
|
|
1058 |
int clsfile_crc32) const {
|
|
1059 |
assert(!DumpSharedSpaces, "supported only when at runtime");
|
|
1060 |
unsigned int hash = compute_hash(class_name);
|
|
1061 |
int index = hash_to_index(hash);
|
|
1062 |
|
|
1063 |
for (SharedDictionaryEntry* entry = bucket(index);
|
|
1064 |
entry != NULL;
|
|
1065 |
entry = entry->next()) {
|
|
1066 |
if (entry->hash() == hash && entry->equals(class_name)) {
|
|
1067 |
if (entry->is_unregistered()) {
|
|
1068 |
if (clsfile_size == -1) {
|
|
1069 |
// We're called from class_exists_for_unregistered_loader. At run time, we want to
|
|
1070 |
// compute the CRC of a ClassFileStream only if there is an UNREGISTERED class
|
|
1071 |
// with the matching name.
|
|
1072 |
return entry;
|
|
1073 |
} else {
|
|
1074 |
// We're called from find_class_for_unregistered_loader
|
|
1075 |
if (entry->_clsfile_size && clsfile_crc32 == entry->_clsfile_crc32) {
|
|
1076 |
return entry;
|
|
1077 |
}
|
|
1078 |
}
|
|
1079 |
|
|
1080 |
// There can be only 1 class with this name for unregistered loaders.
|
|
1081 |
return NULL;
|
|
1082 |
}
|
|
1083 |
}
|
|
1084 |
}
|
|
1085 |
return NULL;
|
|
1086 |
}
|