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_linked() && SystemDictionaryShared::has_class_failed_verification(ik)) {
145     return false;
146   }
147 
148 /* should we allow old classes to be aot-linked ??? Probably ...
149   if (!k->can_be_verified_at_dumptime()) {
150     // linking old classes 
151     return false;
152   }
153 */
154 
155   if (ik->is_hidden()) {
156     assert(ik->shared_class_loader_type() != ClassLoader::OTHER, "must have been set");
157     if (!CDSConfig::is_dumping_invokedynamic()) {
158       return false;
159     }
160     if (!SystemDictionaryShared::should_hidden_class_be_archived(ik)) {
161       return false;
162     }
163     if (HeapShared::is_lambda_proxy_klass(ik)) {
164       InstanceKlass* nest_host = ik->nest_host_not_null();
165       if (!try_add_candidate(nest_host)) {
166         ResourceMark rm;
167         log_warning(cds, aot, link)("%s cannot be aot-linked because it nest host is not aot-linked", ik->external_name());
168         return false;
169       }
170     }
171   }
172 
173   InstanceKlass* s = ik->java_super();
174   if (s != nullptr && !try_add_candidate(s)) {
175     return false;
176   }
177 
178   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
179   int num_interfaces = interfaces->length();
180   for (int index = 0; index < num_interfaces; index++) {
181     InstanceKlass* intf = interfaces->at(index);
182     if (!try_add_candidate(intf)) {
183       return false;
184     }
185   }
186 
187   // There are no loops in the class hierarchy, and this function is always called single-threaded, so
188   // we know ik has not been added yet.
189   assert(CDSConfig::current_thread_is_vm_or_dumper(), "that's why we don't need locks");
190   add_new_candidate(ik);
191 
192   return true;
193 }
194 
195 void AOTClassLinker::add_candidates() {
196   assert_at_safepoint();
197   if (CDSConfig::is_dumping_aot_linked_classes()) {
198     GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
199     for (GrowableArrayIterator<Klass*> it = klasses->begin(); it != klasses->end(); ++it) {
200       Klass* k = *it;
201       if (k->is_instance_klass()) {
202         try_add_candidate(InstanceKlass::cast(k));
203       }
204     }
205   }
206 }
207 
208 void AOTClassLinker::write_to_archive() {
209   assert(is_initialized(), "sanity");
210   assert_at_safepoint();
211 
212   if (CDSConfig::is_dumping_aot_linked_classes()) {
213     AOTLinkedClassTable* table = AOTLinkedClassTable::get(CDSConfig::is_dumping_static_archive());
214     table->set_boot(write_classes(nullptr, true));
215     table->set_boot2(write_classes(nullptr, false));
216     table->set_platform(write_classes(SystemDictionary::java_platform_loader(), false));
217     table->set_app(write_classes(SystemDictionary::java_system_loader(), false));
218   }
219 }
220 
221 Array<InstanceKlass*>* AOTClassLinker::write_classes(oop class_loader, bool is_javabase) {
222   ResourceMark rm;
223   GrowableArray<InstanceKlass*> list;
224 
225   for (int i = 0; i < _sorted_candidates->length(); i++) {
226     InstanceKlass* ik = _sorted_candidates->at(i);
227     if (ik->class_loader() != class_loader) {
228       continue;
229     }
230     if ((ik->module() == ModuleEntryTable::javabase_moduleEntry()) != is_javabase) {
231       continue;
232     }
233 
234     if (ik->is_shared() && CDSConfig::is_dumping_dynamic_archive()) {
235       if (CDSConfig::is_using_aot_linked_classes()) {
236         // This class was recorded as AOT-linked for the base archive,
237         // so there's no need to do so again for the dynamic archive.
238       } else {
239         list.append(ik);
240       }
241     } else {
242       list.append(ArchiveBuilder::current()->get_buffered_addr(ik));
243     }
244   }
245 
246   if (list.length() == 0) {
247     return nullptr;
248   } else {
249     const char* category = class_category_name(list.at(0));
250     log_info(cds, aot, link)("wrote %d class(es) for category %s", list.length(), category);
251     return ArchiveUtils::archive_array(&list);
252   }
253 }
254 
255 int AOTClassLinker::num_platform_initiated_classes() {
256   if (CDSConfig::is_dumping_aot_linked_classes()) {
257     // AOTLinkedClassBulkLoader will initiate loading of all public boot classes in the platform loader.
258     return count_public_classes(nullptr);
259   } else {
260     return 0;
261   }
262 }
263 
264 int AOTClassLinker::num_app_initiated_classes() {
265   if (CDSConfig::is_dumping_aot_linked_classes()) {
266     // AOTLinkedClassBulkLoader will initiate loading of all public boot/platform classes in the app loader.
267     return count_public_classes(nullptr) + count_public_classes(SystemDictionary::java_platform_loader());
268   } else {
269     return 0;
270   }
271 }
272 
273 int AOTClassLinker::count_public_classes(oop loader) {
274   int n = 0;
275   for (int i = 0; i < _sorted_candidates->length(); i++) {
276     InstanceKlass* ik = _sorted_candidates->at(i);
277     if (ik->is_public() && !ik->is_hidden() && ik->class_loader() == loader) {
278       n++;
279     }
280   }
281 
282   return n;
283 }
284 
285 // Used in logging: "boot1", "boot2", "plat", "app" and "unreg", or "array"
286 const char* AOTClassLinker::class_category_name(Klass* k) {
287   if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
288     k = ArchiveBuilder::current()->get_source_addr(k);
289   }
290 
291   if (k->is_array_klass()) {
292     return "array";
293   } else {
294     oop loader = k->class_loader();
295     if (loader == nullptr) {
296       if (k->module() != nullptr &&
297           k->module()->name() != nullptr &&
298           k->module()->name()->equals("java.base")) {
299         return "boot1"; // boot classes in java.base are loaded in the 1st phase
300       } else {
301         return "boot2"; // boot classes outside of java.base are loaded in the 2nd phase phase
302       }
303     } else {
304       if (loader == SystemDictionary::java_platform_loader()) {
305         return "plat";
306       } else if (loader == SystemDictionary::java_system_loader()) {
307         return "app";
308       } else {
309         return "unreg";
310       }
311     }
312   }
313 }
314 
315 const char* AOTClassLinker::class_category_name(AOTLinkedClassCategory category) {
316   switch (category) {
317   case AOTLinkedClassCategory::BOOT1:
318     return "boot1";
319   case AOTLinkedClassCategory::BOOT2:
320     return "boot2";
321   case AOTLinkedClassCategory::PLATFORM:
322     return "plat";
323   case AOTLinkedClassCategory::APP:
324     return "app";
325   case AOTLinkedClassCategory::UNREGISTERED:
326   default:
327       return "unreg";
328   }
329 }
330