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/archiveBuilder.hpp"
 27 #include "cds/archiveUtils.inline.hpp"
 28 #include "cds/cdsConfig.hpp"
 29 #include "cds/classPrelinker.hpp"
 30 #include "cds/finalImageRecipes.hpp"
 31 #include "classfile/classLoader.hpp"
 32 #include "classfile/systemDictionary.hpp"
 33 #include "classfile/systemDictionaryShared.hpp"
 34 #include "classfile/vmClasses.hpp"
 35 #include "memory/oopFactory.hpp"
 36 #include "memory/resourceArea.hpp"
 37 #include "oops/constantPool.inline.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 
 40 GrowableArray<InstanceKlass*>* FinalImageRecipes::_tmp_reflect_klasses = nullptr;
 41 GrowableArray<int>* FinalImageRecipes::_tmp_reflect_flags = nullptr;
 42 GrowableArray<FinalImageRecipes::TmpDynamicProxyClassInfo>* FinalImageRecipes::_tmp_dynamic_proxy_classes = nullptr;
 43 static FinalImageRecipes* _final_image_recipes = nullptr;
 44 
 45 void* FinalImageRecipes::operator new(size_t size) throw() {
 46   return ArchiveBuilder::current()->ro_region_alloc(size);
 47 }
 48 
 49 void FinalImageRecipes::record_recipes_impl() {
 50   assert(CDSConfig::is_dumping_preimage_static_archive(), "must be");
 51   ResourceMark rm;
 52   GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
 53 
 54   // For CDSConfig::is_dumping_invokedynamic()
 55   GrowableArray<InstanceKlass*> tmp_indy_klasses;
 56   GrowableArray<Array<int>*> tmp_indy_cp_indices;
 57   int total_indys_to_resolve = 0;
 58   for (int i = 0; i < klasses->length(); i++) {
 59     Klass* k = klasses->at(i);
 60     if (k->is_instance_klass()) {
 61       InstanceKlass* ik = InstanceKlass::cast(k);
 62       GrowableArray<int> indices;
 63 
 64       if (ik->constants()->cache() != nullptr) {
 65         Array<ResolvedIndyEntry>* tmp_indy_entries = ik->constants()->cache()->resolved_indy_entries();
 66         if (tmp_indy_entries != nullptr) {
 67           for (int i = 0; i < tmp_indy_entries->length(); i++) {
 68             ResolvedIndyEntry* rie = tmp_indy_entries->adr_at(i);
 69             int cp_index = rie->constant_pool_index();
 70             if (rie->is_resolved()) {
 71               indices.append(cp_index);
 72             }
 73           }
 74         }
 75       }
 76 
 77       if (indices.length() > 0) {
 78         tmp_indy_klasses.append(ArchiveBuilder::current()->get_buffered_addr(ik));
 79         tmp_indy_cp_indices.append(ArchiveUtils::archive_array(&indices));
 80         total_indys_to_resolve += indices.length();
 81       }
 82     }
 83   }
 84 
 85   assert(tmp_indy_klasses.length() == tmp_indy_cp_indices.length(), "must be");
 86   if (tmp_indy_klasses.length() > 0) {
 87     _indy_klasses = ArchiveUtils::archive_array(&tmp_indy_klasses);
 88     _indy_cp_indices = ArchiveUtils::archive_array(&tmp_indy_cp_indices);
 89 
 90     ArchivePtrMarker::mark_pointer(&_indy_klasses);
 91     ArchivePtrMarker::mark_pointer(&_indy_cp_indices);
 92   }
 93   log_info(cds)("%d indies in %d classes will be resolved in final CDS image", total_indys_to_resolve, tmp_indy_klasses.length());
 94 
 95   // For CDSConfig::is_dumping_reflection_data()
 96   int reflect_count = 0;
 97   if (_tmp_reflect_klasses != nullptr) {
 98     for (int i = _tmp_reflect_klasses->length() - 1; i >= 0; i--) {
 99       InstanceKlass* ik = _tmp_reflect_klasses->at(i);
100       if (SystemDictionaryShared::is_excluded_class(ik)) {
101         _tmp_reflect_klasses->remove_at(i);
102         _tmp_reflect_flags->remove_at(i);
103       } else {
104         _tmp_reflect_klasses->at_put(i, ArchiveBuilder::current()->get_buffered_addr(ik));
105       }
106     }
107     if (_tmp_reflect_klasses->length() > 0) {
108       _reflect_klasses = ArchiveUtils::archive_array(_tmp_reflect_klasses);
109       _reflect_flags = ArchiveUtils::archive_array(_tmp_reflect_flags);
110 
111       ArchivePtrMarker::mark_pointer(&_reflect_klasses);
112       ArchivePtrMarker::mark_pointer(&_reflect_flags);
113       reflect_count = _tmp_reflect_klasses->length();
114     }
115   }
116   log_info(cds)("ReflectionData of %d classes will be archived in final CDS image", reflect_count);
117 
118   // Dynamic Proxies
119   if (_tmp_dynamic_proxy_classes != nullptr && ArchiveDynamicProxies) {
120     int len = _tmp_dynamic_proxy_classes->length();
121     _dynamic_proxy_classes = ArchiveBuilder::new_ro_array<DynamicProxyClassInfo>(len);
122     ArchivePtrMarker::mark_pointer(&_dynamic_proxy_classes);
123     for (int i = 0; i < len; i++) {
124       TmpDynamicProxyClassInfo* tmp_info = _tmp_dynamic_proxy_classes->adr_at(i);
125       DynamicProxyClassInfo* info = _dynamic_proxy_classes->adr_at(i);
126       info->_loader_type = tmp_info->_loader_type;
127       info->_access_flags = tmp_info->_access_flags;
128       info->_proxy_name = ArchiveBuilder::current()->ro_strdup(tmp_info->_proxy_name);
129 
130       ResourceMark rm;
131       GrowableArray<Klass*> buffered_interfaces;
132       for (int j = 0; j < tmp_info->_interfaces->length(); j++) {
133         buffered_interfaces.append(ArchiveBuilder::current()->get_buffered_addr(tmp_info->_interfaces->at(j)));
134       }
135       info->_interfaces = ArchiveUtils::archive_array(&buffered_interfaces);
136 
137       ArchivePtrMarker::mark_pointer(&info->_proxy_name);
138       ArchivePtrMarker::mark_pointer(&info->_interfaces);
139       ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
140     }
141   }
142 }
143 
144 void FinalImageRecipes::apply_recipes_for_invokedynamic(TRAPS) {
145   assert(CDSConfig::is_dumping_final_static_archive(), "must be");
146 
147   if (_indy_klasses != nullptr) {
148     assert(_indy_cp_indices != nullptr, "must be");
149     for (int i = 0; i < _indy_klasses->length(); i++) {
150       InstanceKlass* ik = _indy_klasses->at(i);
151       ConstantPool* cp = ik->constants();
152       Array<int>* cp_indices = _indy_cp_indices->at(i);
153       GrowableArray<bool> preresolve_list(cp->length(), cp->length(), false);
154       for (int j = 0; j < cp_indices->length(); j++) {
155         preresolve_list.at_put(cp_indices->at(j), true);
156       }
157       ClassPrelinker::preresolve_indy_cp_entries(THREAD, ik, &preresolve_list);
158     }
159   }
160 }
161 
162 void FinalImageRecipes::apply_recipes_for_reflection_data(JavaThread* current) {
163   assert(CDSConfig::is_dumping_final_static_archive(), "must be");
164 
165   if (_reflect_klasses != nullptr) {
166     assert(_reflect_flags != nullptr, "must be");
167     for (int i = 0; i < _reflect_klasses->length(); i++) {
168       InstanceKlass* ik = _reflect_klasses->at(i);
169       int rd_flags = _reflect_flags->at(i);
170       ClassPrelinker::generate_reflection_data(current, ik, rd_flags);
171     }
172   }
173 }
174 
175 void FinalImageRecipes::add_reflection_data_flags(InstanceKlass* ik, TRAPS) {
176   assert(CDSConfig::is_dumping_preimage_static_archive(), "must be");
177   if (SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) && !ik->is_hidden() &&
178       java_lang_Class::has_reflection_data(ik->java_mirror())) {
179     int rd_flags = ClassPrelinker::class_reflection_data_flags(ik, CHECK);
180     if (_tmp_reflect_klasses == nullptr) {
181       _tmp_reflect_klasses = new (mtClassShared) GrowableArray<InstanceKlass*>(100, mtClassShared);
182       _tmp_reflect_flags = new (mtClassShared) GrowableArray<int>(100, mtClassShared);
183     }
184     _tmp_reflect_klasses->append(ik);
185     _tmp_reflect_flags->append(rd_flags);
186   }
187 }
188 
189 void FinalImageRecipes::add_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
190   int loader_type;
191   if (loader == nullptr) {
192     loader_type = ClassLoader::BOOT_LOADER;
193   } else if (loader == SystemDictionary::java_platform_loader()) {
194     loader_type = ClassLoader::PLATFORM_LOADER;
195   } else if (loader == SystemDictionary::java_system_loader()) {
196     loader_type = ClassLoader::APP_LOADER;
197   } else {
198     return;
199   }
200 
201   if (_tmp_dynamic_proxy_classes == nullptr) {
202     _tmp_dynamic_proxy_classes = new (mtClassShared) GrowableArray<TmpDynamicProxyClassInfo>(32, mtClassShared);
203   }
204 
205   TmpDynamicProxyClassInfo info;
206   info._loader_type = loader_type;
207   info._access_flags = access_flags;
208   info._proxy_name = os::strdup(proxy_name);
209   info._interfaces = new (mtClassShared) GrowableArray<Klass*>(interfaces->length(), mtClassShared);
210   for (int i = 0; i < interfaces->length(); i++) {
211     Klass* intf = java_lang_Class::as_Klass(interfaces->obj_at(i));
212     info._interfaces->append(intf);
213   }
214   _tmp_dynamic_proxy_classes->append(info);
215 }
216 
217 void FinalImageRecipes::apply_recipes_for_dynamic_proxies(TRAPS) {
218   if (ArchiveDynamicProxies && _dynamic_proxy_classes != nullptr) {
219     for (int proxy_index = 0; proxy_index < _dynamic_proxy_classes->length(); proxy_index++) {
220       DynamicProxyClassInfo* info = _dynamic_proxy_classes->adr_at(proxy_index);
221 
222       Handle loader(THREAD, ArchiveUtils::builtin_loader_from_type(info->_loader_type));
223 
224       oop proxy_name_oop = java_lang_String::create_oop_from_str(info->_proxy_name, CHECK);
225       Handle proxy_name(THREAD, proxy_name_oop);
226 
227       int num_intfs = info->_interfaces->length();
228       objArrayOop interfaces_oop = oopFactory::new_objArray(vmClasses::Class_klass(), num_intfs, CHECK);
229       objArrayHandle interfaces(THREAD, interfaces_oop);
230       for (int intf_index = 0; intf_index < num_intfs; intf_index++) {
231         Klass* k = info->_interfaces->at(intf_index);
232         assert(k->java_mirror() != nullptr, "must be loaded");
233         interfaces()->obj_at_put(intf_index, k->java_mirror());
234       }
235 
236       ClassPrelinker::define_dynamic_proxy_class(loader, proxy_name, interfaces, info->_access_flags, CHECK);
237     }
238   }
239 }
240 
241 void FinalImageRecipes::record_recipes() {
242   _final_image_recipes = new FinalImageRecipes();
243   _final_image_recipes->record_recipes_impl();
244 }
245 
246 void FinalImageRecipes::apply_recipes(TRAPS) {
247   assert(CDSConfig::is_dumping_final_static_archive(), "must be");
248 
249   if (_final_image_recipes != nullptr) {
250     _final_image_recipes->apply_recipes_for_invokedynamic(CHECK);
251     _final_image_recipes->apply_recipes_for_reflection_data(THREAD);
252     _final_image_recipes->apply_recipes_for_dynamic_proxies(CHECK);
253   }
254 
255   // Set it to null as we don't need to write this table into the final image.
256   _final_image_recipes = nullptr;
257 }
258 
259 void FinalImageRecipes::serialize(SerializeClosure* soc, bool is_static_archive) {
260   if (is_static_archive) {
261     soc->do_ptr((void**)&_final_image_recipes);
262   }
263 }