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   void clear_archived_oops();
 63 };
 64 
 65 static ArchivedClassLoaderData _archived_boot_loader_data;
 66 static ArchivedClassLoaderData _archived_platform_loader_data;
 67 static ArchivedClassLoaderData _archived_system_loader_data;
 68 static ModuleEntry* _archived_javabase_moduleEntry = NULL;
 69 
 70 void ArchivedClassLoaderData::iterate_symbols(ClassLoaderData* loader_data, MetaspaceClosure* closure) {
 71   assert(DumpSharedSpaces, "must be");
 72   assert_valid(loader_data);
 73   if (loader_data != NULL) {
 74     loader_data->packages()->iterate_symbols(closure);
 75     loader_data->modules() ->iterate_symbols(closure);
 76   }
 77 }
 78 
 79 void ArchivedClassLoaderData::allocate(ClassLoaderData* loader_data) {
 80   assert(DumpSharedSpaces, "must be");
 81   assert_valid(loader_data);
 82   if (loader_data != NULL) {
 83     // We can't create hashtables at dump time because the hashcode depends on the
 84     // address of the Symbols, which may be relocated at runtime due to ASLR.
 85     // So we store the packages/modules in Arrays. At runtime, we create
 86     // the hashtables using these arrays.
 87     _packages = loader_data->packages()->allocate_archived_entries();
 88     _modules  = loader_data->modules() ->allocate_archived_entries();
 89   }
 90 }
 91 
 92 void ArchivedClassLoaderData::init_archived_entries(ClassLoaderData* loader_data) {
 93   assert(DumpSharedSpaces, "must be");
 94   assert_valid(loader_data);
 95   if (loader_data != NULL) {
 96     loader_data->packages()->init_archived_entries(_packages);
 97     loader_data->modules() ->init_archived_entries(_modules);
 98   }
 99 }
100 
101 void ArchivedClassLoaderData::init_archived_oops(ClassLoaderData* loader_data) {
102   assert(DumpSharedSpaces, "must be");
103   assert_valid(loader_data);
104   if (loader_data != NULL) {
105     loader_data->modules()->init_archived_oops(_modules);
106   }
107 }
108 
109 void ArchivedClassLoaderData::restore(ClassLoaderData* loader_data, bool do_entries, bool do_oops) {
110   assert(UseSharedSpaces, "must be");
111   assert_valid(loader_data);
112   if (_modules != NULL) { // Could be NULL if we have archived no modules for platform/system loaders
113     ModuleEntryTable* modules = loader_data->modules();
114     PackageEntryTable* packages = loader_data->packages();
115 
116     MutexLocker m1(Module_lock);
117     if (do_entries) {
118       modules->load_archived_entries(loader_data, _modules);
119       packages->load_archived_entries(_packages);
120     }
121     if (do_oops) {
122       modules->restore_archived_oops(loader_data, _modules);
123     }
124   }
125 }
126 
127 void ArchivedClassLoaderData::clear_archived_oops() {
128   assert(UseSharedSpaces, "must be");
129   if (_modules != NULL) {
130     for (int i = 0; i < _modules->length(); i++) {
131       _modules->at(i)->clear_archived_oops();
132     }
133   }
134 }
135 
136 // ------------------------------
137 
138 static ClassLoaderData* null_class_loader_data() {
139   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
140   assert(loader_data != NULL, "must be");
141   return loader_data;
142 }
143 
144 static ClassLoaderData* java_platform_loader_data_or_null() {
145   return ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_platform_loader());
146 }
147 
148 static ClassLoaderData* java_system_loader_data_or_null() {
149   return ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_system_loader());
150 }
151 
152 void ClassLoaderDataShared::iterate_symbols(MetaspaceClosure* closure) {
153   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
154   _archived_boot_loader_data.iterate_symbols    (null_class_loader_data(), closure);
155   _archived_platform_loader_data.iterate_symbols(java_platform_loader_data_or_null(), closure);
156   _archived_system_loader_data.iterate_symbols  (java_system_loader_data_or_null(), closure);
157 }
158 
159 void ClassLoaderDataShared::allocate_archived_tables() {
160   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
161   _archived_boot_loader_data.allocate    (null_class_loader_data());
162   _archived_platform_loader_data.allocate(java_platform_loader_data_or_null());
163   _archived_system_loader_data.allocate  (java_system_loader_data_or_null());
164 }
165 
166 void ClassLoaderDataShared::init_archived_tables() {
167   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
168   _archived_boot_loader_data.init_archived_entries    (null_class_loader_data());
169   _archived_platform_loader_data.init_archived_entries(java_platform_loader_data_or_null());
170   _archived_system_loader_data.init_archived_entries  (java_system_loader_data_or_null());
171   _archived_javabase_moduleEntry = ModuleEntry::get_archived_entry(ModuleEntryTable::javabase_moduleEntry());
172 }
173 
174 void ClassLoaderDataShared::init_archived_oops() {
175   assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
176   _archived_boot_loader_data.init_archived_oops    (null_class_loader_data());
177   _archived_platform_loader_data.init_archived_oops(java_platform_loader_data_or_null());
178   _archived_system_loader_data.init_archived_oops  (java_system_loader_data_or_null());
179 }
180 
181 void ClassLoaderDataShared::serialize(SerializeClosure* f) {
182   _archived_boot_loader_data.serialize(f);
183   _archived_platform_loader_data.serialize(f);
184   _archived_system_loader_data.serialize(f);
185   f->do_ptr((void**)&_archived_javabase_moduleEntry);
186 
187   if (f->reading() && MetaspaceShared::use_full_module_graph()) {
188     // Must be done before ClassLoader::create_javabase()
189     _archived_boot_loader_data.restore(null_class_loader_data(), true, false);
190     ModuleEntryTable::set_javabase_moduleEntry(_archived_javabase_moduleEntry);
191     log_info(cds)("use_full_module_graph = true; java.base = " INTPTR_FORMAT,
192                   p2i(_archived_javabase_moduleEntry));
193   }
194 }
195 
196 void ClassLoaderDataShared::clear_archived_oops() {
197   assert(UseSharedSpaces && !MetaspaceShared::use_full_module_graph(), "must be");
198   _archived_boot_loader_data.clear_archived_oops();
199   _archived_platform_loader_data.clear_archived_oops();
200   _archived_system_loader_data.clear_archived_oops();
201 }
202 
203 oop ClassLoaderDataShared::restore_archived_oops_for_null_class_loader_data() {
204   assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
205   _archived_boot_loader_data.restore(null_class_loader_data(), false, true);
206   return _archived_javabase_moduleEntry->module();
207 }
208 
209 void ClassLoaderDataShared::restore_java_platform_loader_from_archive(ClassLoaderData* loader_data) {
210   assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
211   _archived_platform_loader_data.restore(loader_data, true, true);
212 }
213 
214 void ClassLoaderDataShared::restore_java_system_loader_from_archive(ClassLoaderData* loader_data) {
215   assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
216   _archived_system_loader_data.restore(loader_data, true, true);
217 }
218 
219 #endif // INCLUDE_CDS_JAVA_HEAP