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