1  /*
  2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "classfile/classLoaderData.inline.hpp"
 27 #include "classfile/classLoaderDataShared.hpp"
 28 #include "classfile/moduleEntry.hpp"
 29 #include "classfile/packageEntry.hpp"
 30 #include "logging/log.hpp"
 31 #include "memory/metaspaceShared.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 
 34 #if INCLUDE_CDS_JAVA_HEAP
 35 
 36 class ArchivedClassLoaderData {
 37   Array<PackageEntry*>* _packages;
 38   Array<ModuleEntry*>* _modules;
 39 
 40   void assert_valid(ClassLoaderData* loader_data) {
 41     // loader_data may be NULL if the boot layer has loaded no modules for the platform or
 42     // system loaders (e.g., if you create a custom JDK image with only java.base).
 43     if (loader_data != NULL) {
 44       assert(!loader_data->has_class_mirror_holder(),
 45              "loaders for non-strong hidden classes or unsafe anonymous classes not supported");
 46     }
 47   }
 48 public:
 49   ArchivedClassLoaderData() : _packages(NULL), _modules(NULL) {}
 50 
 51   void iterate_symbols(ClassLoaderData* loader_data, MetaspaceClosure* closure);
 52   void allocate(ClassLoaderData* loader_data);
 53   void init_archived_entries(ClassLoaderData* loader_data);
 54   void init_archived_oops(ClassLoaderData* loader_data);
 55 
 56   void serialize(SerializeClosure* f) {
 57     f->do_ptr((void**)&_packages);
 58     f->do_ptr((void**)&_modules);
 59   }
 60 
 61   void restore(ClassLoaderData* loader_data, bool do_entries, bool do_oops);
 62 };
 63 
 64 static ArchivedClassLoaderData _archived_boot_loader_data;
 65 static ArchivedClassLoaderData _archived_platform_loader_data;
 66 static ArchivedClassLoaderData _archived_system_loader_data;
 67 static ModuleEntry* _archived_javabase_moduleEntry = NULL;
 68 
 69 void ArchivedClassLoaderData::iterate_symbols(ClassLoaderData* loader_data, MetaspaceClosure* closure) {
 70   assert(DumpSharedSpaces, "must be");
 71   assert_valid(loader_data);
 72   if (loader_data != NULL) {
 73     loader_data->packages()->iterate_symbols(closure);
 74     loader_data->modules() ->iterate_symbols(closure);
 75   }
 76 }
 77 
 78 void ArchivedClassLoaderData::allocate(ClassLoaderData* loader_data) {
 79   assert(DumpSharedSpaces, "must be");
 80   assert_valid(loader_data);
 81   if (loader_data != NULL) {
 82     // We can't create hashtables at dump time because the hashcode depends on the
 83     // address of the Symbols, which may be relocated at runtime due to ASLR.
 84     // So we store the packages/modules in Arrays. At runtime, we create
 85     // the hashtables using these arrays.
 86     _packages = loader_data->packages()->allocate_archived_entries();
 87     _modules  = loader_data->modules() ->allocate_archived_entries();
 88   }
 89 }
 90 
 91 void ArchivedClassLoaderData::init_archived_entries(ClassLoaderData* loader_data) {
 92   assert(DumpSharedSpaces, "must be");
 93   assert_valid(loader_data);
 94   if (loader_data != NULL) {
 95     loader_data->packages()->init_archived_entries(_packages);
 96     loader_data->modules() ->init_archived_entries(_modules);
 97   }
 98 }
 99 
100 void ArchivedClassLoaderData::init_archived_oops(ClassLoaderData* loader_data) {
101   assert(DumpSharedSpaces, "must be");
102   assert_valid(loader_data);
103   if (loader_data != NULL) {
104     loader_data->modules()->init_archived_oops(_modules);
105   }
106 }
107 
108 void ArchivedClassLoaderData::restore(ClassLoaderData* loader_data, bool do_entries, bool do_oops) {
109   assert(UseSharedSpaces, "must be");
110   assert_valid(loader_data);
111   if (_modules != NULL) { // Could be NULL if we have archived no modules for platform/system loaders
112     ModuleEntryTable* modules = loader_data->modules();
113     PackageEntryTable* packages = loader_data->packages();
114 
115     MutexLocker m1(Module_lock);
116     if (do_entries) {
117       modules->load_archived_entries(loader_data, _modules);
118       packages->load_archived_entries(_packages);
119     }
120     if (do_oops) {
121       modules->restore_archived_oops(loader_data, _modules);
122     }
123   }
124 }
125 
126 // ------------------------------
127 
128 static ClassLoaderData* null_class_loader_data() {
129   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
130   assert(loader_data != NULL, "must be");
131   return loader_data;
132 }
133 
134 static ClassLoaderData* java_platform_loader_data_or_null() {
135   return ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_platform_loader());
136 }
137 
138 static ClassLoaderData* java_system_loader_data_or_null() {
139   return ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_system_loader());
140 }
141 
142 void ClassLoaderDataShared::iterate_symbols(MetaspaceClosure* closure) {
143   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
144   _archived_boot_loader_data.iterate_symbols    (null_class_loader_data(), closure);
145   _archived_platform_loader_data.iterate_symbols(java_platform_loader_data_or_null(), closure);
146   _archived_system_loader_data.iterate_symbols  (java_system_loader_data_or_null(), closure);
147 }
148 
149 void ClassLoaderDataShared::allocate_archived_tables() {
150   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
151   _archived_boot_loader_data.allocate    (null_class_loader_data());
152   _archived_platform_loader_data.allocate(java_platform_loader_data_or_null());
153   _archived_system_loader_data.allocate  (java_system_loader_data_or_null());
154 }
155 
156 void ClassLoaderDataShared::init_archived_tables() {
157   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
158   _archived_boot_loader_data.init_archived_entries    (null_class_loader_data());
159   _archived_platform_loader_data.init_archived_entries(java_platform_loader_data_or_null());
160   _archived_system_loader_data.init_archived_entries  (java_system_loader_data_or_null());
161   _archived_javabase_moduleEntry = ModuleEntry::get_archived_entry(ModuleEntryTable::javabase_moduleEntry());
162 }
163 
164 void ClassLoaderDataShared::init_archived_oops() {
165   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
166   _archived_boot_loader_data.init_archived_oops    (null_class_loader_data());
167   _archived_platform_loader_data.init_archived_oops(java_platform_loader_data_or_null());
168   _archived_system_loader_data.init_archived_oops  (java_system_loader_data_or_null());
169 }
170 
171 void ClassLoaderDataShared::serialize(SerializeClosure* f) {
172   _archived_boot_loader_data.serialize(f);
173   _archived_platform_loader_data.serialize(f);
174   _archived_system_loader_data.serialize(f);
175   f->do_ptr((void**)&_archived_javabase_moduleEntry);
176 
177   if (f->reading() && MetaspaceShared::use_full_module_graph()) {
178     // Must be done before ClassLoader::create_javabase()
179     _archived_boot_loader_data.restore(null_class_loader_data(), true, false);
180     ModuleEntryTable::set_javabase_moduleEntry(_archived_javabase_moduleEntry);
181     log_info(cds)("use_full_module_graph = true; java.base = " INTPTR_FORMAT,
182                   p2i(_archived_javabase_moduleEntry));
183   }
184 }
185 
186 oop ClassLoaderDataShared::restore_archived_oops_for_null_class_loader_data() {
187   assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
188   _archived_boot_loader_data.restore(null_class_loader_data(), false, true);
189   return _archived_javabase_moduleEntry->module();
190 }
191 
192 void ClassLoaderDataShared::restore_java_platform_loader_from_archive(ClassLoaderData* loader_data) {
193   assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
194   _archived_platform_loader_data.restore(loader_data, true, true);
195 }
196 
197 void ClassLoaderDataShared::restore_java_system_loader_from_archive(ClassLoaderData* loader_data) {
198   assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
199   _archived_system_loader_data.restore(loader_data, true, true);
200 }
201 
202 #endif // INCLUDE_CDS_JAVA_HEAP