1 /*
  2  * Copyright (c) 2024, 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 "cds/aotClassLinker.hpp"
 27 #include "cds/aotConstantPoolResolver.hpp"
 28 #include "cds/aotLinkedClassTable.hpp"
 29 #include "cds/archiveBuilder.hpp"
 30 #include "cds/archiveUtils.inline.hpp"
 31 #include "cds/cdsConfig.hpp"
 32 #include "cds/heapShared.hpp"
 33 #include "cds/lambdaFormInvokers.inline.hpp"
 34 #include "classfile/classLoader.hpp"
 35 #include "classfile/dictionary.hpp"
 36 #include "classfile/systemDictionary.hpp"
 37 #include "classfile/systemDictionaryShared.hpp"
 38 #include "classfile/vmClasses.hpp"
 39 #include "memory/resourceArea.hpp"
 40 #include "oops/constantPool.inline.hpp"
 41 #include "oops/instanceKlass.hpp"
 42 #include "oops/klass.inline.hpp"
 43 #include "runtime/handles.inline.hpp"
 44 
 45 AOTClassLinker::ClassesTable* AOTClassLinker::_vm_classes = nullptr;
 46 AOTClassLinker::ClassesTable* AOTClassLinker::_candidates = nullptr;
 47 GrowableArrayCHeap<InstanceKlass*, mtClassShared>* AOTClassLinker::_sorted_candidates = nullptr;
 48 
 49 #ifdef ASSERT
 50 bool AOTClassLinker::is_initialized() {
 51   assert(CDSConfig::is_dumping_archive(), "AOTClassLinker is for CDS dumping only");
 52   return _vm_classes != nullptr;
 53 }
 54 #endif
 55 
 56 void AOTClassLinker::initialize() {
 57   assert(!is_initialized(), "sanity");
 58 
 59   _vm_classes = new (mtClass)ClassesTable();
 60   _candidates = new (mtClass)ClassesTable();
 61   _sorted_candidates = new GrowableArrayCHeap<InstanceKlass*, mtClassShared>(1000);
 62 
 63   for (auto id : EnumRange<vmClassID>{}) {
 64     add_vm_class(vmClasses::klass_at(id));
 65   }
 66 
 67   assert(is_initialized(), "sanity");
 68 
 69   AOTConstantPoolResolver::initialize();
 70 }
 71 
 72 void AOTClassLinker::dispose() {
 73   assert(is_initialized(), "sanity");
 74 
 75   delete _vm_classes;
 76   delete _candidates;
 77   delete _sorted_candidates;
 78   _vm_classes = nullptr;
 79   _candidates = nullptr;
 80   _sorted_candidates = nullptr;
 81 
 82   assert(!is_initialized(), "sanity");
 83 
 84   AOTConstantPoolResolver::dispose();
 85 }
 86 
 87 bool AOTClassLinker::is_vm_class(InstanceKlass* ik) {
 88   assert(is_initialized(), "sanity");
 89   return (_vm_classes->get(ik) != nullptr);
 90 }
 91 
 92 void AOTClassLinker::add_vm_class(InstanceKlass* ik) {
 93   assert(is_initialized(), "sanity");
 94   bool created;
 95   _vm_classes->put_if_absent(ik, &created);
 96   if (created) {
 97     if (CDSConfig::is_dumping_aot_linked_classes()) {
 98       bool v = try_add_candidate(ik);
 99       assert(v, "must succeed for VM class");
100     }
101     InstanceKlass* super = ik->java_super();
102     if (super != nullptr) {
103       add_vm_class(super);
104     }
105     Array<InstanceKlass*>* ifs = ik->local_interfaces();
106     for (int i = 0; i < ifs->length(); i++) {
107       add_vm_class(ifs->at(i));
108     }
109   }
110 }
111 
112 bool AOTClassLinker::is_candidate(InstanceKlass* ik) {
113   return (_candidates->get(ik) != nullptr);
114 }
115 
116 void AOTClassLinker::add_new_candidate(InstanceKlass* ik) {
117   assert(!is_candidate(ik), "caller need to check");
118   _candidates->put_when_absent(ik, true);
119   _sorted_candidates->append(ik);
120 
121   if (log_is_enabled(Info, cds, aot, link)) {
122     ResourceMark rm;
123     log_info(cds, aot, link)("%s %s %p", class_category_name(ik), ik->external_name(), ik);
124   }
125 }
126 
127 // ik is a candidate for aot-linking; see if it can really work
128 // that way, and return success or failure. Not only must ik itself
129 // look like a class that can be aot-linked but its supers must also be
130 // aot-linkable.
131 bool AOTClassLinker::try_add_candidate(InstanceKlass* ik) {
132   assert(is_initialized(), "sanity");
133   assert(CDSConfig::is_dumping_aot_linked_classes(), "sanity");
134 
135   if (!SystemDictionaryShared::is_builtin(ik)) {
136     // not loaded by a class loader which we know about
137     return false;
138   }
139 
140   if (is_candidate(ik)) { // already checked.
141     return true;
142   }
143 
144   if (ik->is_hidden()) {
145     assert(ik->shared_class_loader_type() != ClassLoader::OTHER, "must have been set");
146     if (!CDSConfig::is_dumping_invokedynamic()) {
147       return false;
148     }
149     if (!SystemDictionaryShared::should_hidden_class_be_archived(ik)) {
150       return false;
151     }
152     if (HeapShared::is_lambda_proxy_klass(ik)) {
153       InstanceKlass* nest_host = ik->nest_host_not_null();
154       if (!try_add_candidate(nest_host)) {
155         ResourceMark rm;
156         log_warning(cds, aot, link)("%s cannot be aot-linked because it nest host is not aot-linked", ik->external_name());
157         return false;
158       }
159     }
160   }
161 
162   InstanceKlass* s = ik->java_super();
163   if (s != nullptr && !try_add_candidate(s)) {
164     return false;
165   }
166 
167   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
168   int num_interfaces = interfaces->length();
169   for (int index = 0; index < num_interfaces; index++) {
170     InstanceKlass* intf = interfaces->at(index);
171     if (!try_add_candidate(intf)) {
172       return false;
173     }
174   }
175 
176   // There are no loops in the class hierarchy, and this function is always called single-threaded, so
177   // we know ik has not been added yet.
178   assert(CDSConfig::current_thread_is_vm_or_dumper(), "that's why we don't need locks");
179   add_new_candidate(ik);
180 
181   return true;
182 }
183 
184 void AOTClassLinker::add_candidates() {
185   assert_at_safepoint();
186   if (CDSConfig::is_dumping_aot_linked_classes()) {
187     GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
188     for (GrowableArrayIterator<Klass*> it = klasses->begin(); it != klasses->end(); ++it) {
189       Klass* k = *it;
190       if (k->is_instance_klass()) {
191         try_add_candidate(InstanceKlass::cast(k));
192       }
193     }
194   }
195 }
196 
197 void AOTClassLinker::write_to_archive() {
198   assert(is_initialized(), "sanity");
199   assert_at_safepoint();
200 
201   if (CDSConfig::is_dumping_aot_linked_classes()) {
202     AOTLinkedClassTable* table = AOTLinkedClassTable::get(CDSConfig::is_dumping_static_archive());
203     table->set_boot(write_classes(nullptr, true));
204     table->set_boot2(write_classes(nullptr, false));
205     table->set_platform(write_classes(SystemDictionary::java_platform_loader(), false));
206     table->set_app(write_classes(SystemDictionary::java_system_loader(), false));
207   }
208 }
209 
210 Array<InstanceKlass*>* AOTClassLinker::write_classes(oop class_loader, bool is_javabase) {
211   ResourceMark rm;
212   GrowableArray<InstanceKlass*> list;
213 
214   for (int i = 0; i < _sorted_candidates->length(); i++) {
215     InstanceKlass* ik = _sorted_candidates->at(i);
216     if (ik->class_loader() != class_loader) {
217       continue;
218     }
219     if ((ik->module() == ModuleEntryTable::javabase_moduleEntry()) != is_javabase) {
220       continue;
221     }
222 
223     if (ik->is_shared() && CDSConfig::is_dumping_dynamic_archive()) {
224       if (CDSConfig::is_using_aot_linked_classes()) {
225         // This class was recorded as AOT-linked for the base archive,
226         // so there's no need to do so again for the dynamic archive.
227       } else {
228         list.append(ik);
229       }
230     } else {
231       list.append(ArchiveBuilder::current()->get_buffered_addr(ik));
232     }
233   }
234 
235   if (list.length() == 0) {
236     return nullptr;
237   } else {
238     const char* category = class_category_name(list.at(0));
239     log_info(cds, aot, link)("wrote %d class(es) for category %s", list.length(), category);
240     return ArchiveUtils::archive_array(&list);
241   }
242 }
243 
244 int AOTClassLinker::num_platform_initiated_classes() {
245   if (CDSConfig::is_dumping_aot_linked_classes()) {
246     // AOTLinkedClassBulkLoader will initiate loading of all public boot classes in the platform loader.
247     return count_public_classes(nullptr);
248   } else {
249     return 0;
250   }
251 }
252 
253 int AOTClassLinker::num_app_initiated_classes() {
254   if (CDSConfig::is_dumping_aot_linked_classes()) {
255     // AOTLinkedClassBulkLoader will initiate loading of all public boot/platform classes in the app loader.
256     return count_public_classes(nullptr) + count_public_classes(SystemDictionary::java_platform_loader());
257   } else {
258     return 0;
259   }
260 }
261 
262 int AOTClassLinker::count_public_classes(oop loader) {
263   int n = 0;
264   for (int i = 0; i < _sorted_candidates->length(); i++) {
265     InstanceKlass* ik = _sorted_candidates->at(i);
266     if (ik->is_public() && !ik->is_hidden() && ik->class_loader() == loader) {
267       n++;
268     }
269   }
270 
271   return n;
272 }
273 
274 // Used in logging: "boot1", "boot2", "plat", "app" and "unreg", or "array"
275 const char* AOTClassLinker::class_category_name(Klass* k) {
276   if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
277     k = ArchiveBuilder::current()->get_source_addr(k);
278   }
279 
280   if (k->is_array_klass()) {
281     return "array";
282   } else {
283     oop loader = k->class_loader();
284     if (loader == nullptr) {
285       if (k->module() != nullptr &&
286           k->module()->name() != nullptr &&
287           k->module()->name()->equals("java.base")) {
288         return "boot1"; // boot classes in java.base are loaded in the 1st phase
289       } else {
290         return "boot2"; // boot classes outside of java.base are loaded in the 2nd phase phase
291       }
292     } else {
293       if (loader == SystemDictionary::java_platform_loader()) {
294         return "plat";
295       } else if (loader == SystemDictionary::java_system_loader()) {
296         return "app";
297       } else {
298         return "unreg";
299       }
300     }
301   }
302 }
303 
304 const char* AOTClassLinker::class_category_name(AOTLinkedClassCategory category) {
305   switch (category) {
306   case AOTLinkedClassCategory::BOOT1:
307     return "boot1";
308   case AOTLinkedClassCategory::BOOT2:
309     return "boot2";
310   case AOTLinkedClassCategory::PLATFORM:
311     return "plat";
312   case AOTLinkedClassCategory::APP:
313     return "app";
314   case AOTLinkedClassCategory::UNREGISTERED:
315   default:
316       return "unreg";
317   }
318 }
319