21 * questions.
22 *
23 */
24
25 #include "cds/aotConstantPoolResolver.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveUtils.inline.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/finalImageRecipes.hpp"
30 #include "classfile/classLoader.hpp"
31 #include "classfile/javaClasses.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 #include "runtime/mutexLocker.hpp"
40
41 static FinalImageRecipes* _final_image_recipes = nullptr;
42
43 void* FinalImageRecipes::operator new(size_t size) throw() {
44 return ArchiveBuilder::current()->ro_region_alloc(size);
45 }
46
47 void FinalImageRecipes::record_all_classes() {
48 _all_klasses = ArchiveUtils::archive_array(ArchiveBuilder::current()->klasses());
49 ArchivePtrMarker::mark_pointer(&_all_klasses);
50 }
51
52 void FinalImageRecipes::record_recipes_for_constantpool() {
53 ResourceMark rm;
54
55 // The recipes are recorded regardless of CDSConfig::is_dumping_{invokedynamic,dynamic_proxies,reflection_data}().
56 // If some of these options are not enabled, the corresponding recipes will be
57 // ignored during the final image assembly.
58
59 GrowableArray<Array<int>*> tmp_cp_recipes;
60 GrowableArray<int> tmp_flags;
153 ResourceMark rm(current);
154 ConstantPool* cp = ik->constants();
155 GrowableArray<bool> preresolve_list(cp->length(), cp->length(), false);
156 for (int j = 0; j < cp_indices->length(); j++) {
157 preresolve_list.at_put(cp_indices->at(j), true);
158 }
159 if ((flags & CP_RESOLVE_CLASS) != 0) {
160 AOTConstantPoolResolver::preresolve_class_cp_entries(current, ik, &preresolve_list);
161 }
162 if ((flags & CP_RESOLVE_FIELD_AND_METHOD) != 0) {
163 AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(current, ik, &preresolve_list);
164 }
165 if ((flags & CP_RESOLVE_INDY) != 0) {
166 AOTConstantPoolResolver::preresolve_indy_cp_entries(current, ik, &preresolve_list);
167 }
168 }
169 }
170 }
171 }
172
173 void FinalImageRecipes::load_all_classes(TRAPS) {
174 assert(CDSConfig::is_dumping_final_static_archive(), "sanity");
175 Handle class_loader(THREAD, SystemDictionary::java_system_loader());
176 for (int i = 0; i < _all_klasses->length(); i++) {
177 Klass* k = _all_klasses->at(i);
178 int flags = _flags->at(i);
179 if (k->is_instance_klass()) {
180 InstanceKlass* ik = InstanceKlass::cast(k);
181 if (ik->defined_by_other_loaders()) {
182 SystemDictionaryShared::init_dumptime_info(ik);
183 SystemDictionaryShared::add_unregistered_class(THREAD, ik);
184 SystemDictionaryShared::copy_unregistered_class_size_and_crc32(ik);
185 } else if (!ik->is_hidden()) {
186 Klass* actual = SystemDictionary::resolve_or_fail(ik->name(), class_loader, true, CHECK);
187 if (actual != ik) {
188 ResourceMark rm(THREAD);
189 log_error(aot)("Unable to resolve class from CDS archive: %s", ik->external_name());
190 log_error(aot)("Expected: " INTPTR_FORMAT ", actual: " INTPTR_FORMAT, p2i(ik), p2i(actual));
191 log_error(aot)("Please check if your VM command-line is the same as in the training run");
192 AOTMetaspace::unrecoverable_writing_error();
193 }
194 assert(ik->is_loaded(), "must be");
195 ik->link_class(CHECK);
196
197 if (ik->has_aot_safe_initializer() && (flags & WAS_INITED) != 0) {
198 assert(ik->class_loader() == nullptr, "supported only for boot classes for now");
199 ik->initialize(CHECK);
200 }
201 }
202 }
203 }
204 }
205
206 void FinalImageRecipes::record_recipes() {
207 assert(CDSConfig::is_dumping_preimage_static_archive(), "must be");
208 _final_image_recipes = new FinalImageRecipes();
209 _final_image_recipes->record_all_classes();
210 _final_image_recipes->record_recipes_for_constantpool();
211 }
212
213 void FinalImageRecipes::apply_recipes(TRAPS) {
214 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
215 if (_final_image_recipes != nullptr) {
216 _final_image_recipes->apply_recipes_impl(THREAD);
217 if (HAS_PENDING_EXCEPTION) {
218 log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
219 java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
220 log_error(aot)("Please check if your VM command-line is the same as in the training run");
221 AOTMetaspace::unrecoverable_writing_error("Unexpected exception, use -Xlog:aot,exceptions=trace for detail");
222 }
223 }
224
225 // Set it to null as we don't need to write this table into the final image.
226 _final_image_recipes = nullptr;
227 }
228
229 void FinalImageRecipes::apply_recipes_impl(TRAPS) {
230 load_all_classes(CHECK);
231 apply_recipes_for_constantpool(THREAD);
232 }
233
234 void FinalImageRecipes::serialize(SerializeClosure* soc) {
235 soc->do_ptr((void**)&_final_image_recipes);
236 }
|
21 * questions.
22 *
23 */
24
25 #include "cds/aotConstantPoolResolver.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveUtils.inline.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/finalImageRecipes.hpp"
30 #include "classfile/classLoader.hpp"
31 #include "classfile/javaClasses.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 #include "runtime/mutexLocker.hpp"
40
41 GrowableArray<InstanceKlass*>* FinalImageRecipes::_tmp_reflect_klasses = nullptr;
42 GrowableArray<int>* FinalImageRecipes::_tmp_reflect_flags = nullptr;
43 GrowableArray<FinalImageRecipes::TmpDynamicProxyClassInfo>* FinalImageRecipes::_tmp_dynamic_proxy_classes = nullptr;
44 static FinalImageRecipes* _final_image_recipes = nullptr;
45
46 void* FinalImageRecipes::operator new(size_t size) throw() {
47 return ArchiveBuilder::current()->ro_region_alloc(size);
48 }
49
50 void FinalImageRecipes::record_all_classes() {
51 _all_klasses = ArchiveUtils::archive_array(ArchiveBuilder::current()->klasses());
52 ArchivePtrMarker::mark_pointer(&_all_klasses);
53 }
54
55 void FinalImageRecipes::record_recipes_for_constantpool() {
56 ResourceMark rm;
57
58 // The recipes are recorded regardless of CDSConfig::is_dumping_{invokedynamic,dynamic_proxies,reflection_data}().
59 // If some of these options are not enabled, the corresponding recipes will be
60 // ignored during the final image assembly.
61
62 GrowableArray<Array<int>*> tmp_cp_recipes;
63 GrowableArray<int> tmp_flags;
156 ResourceMark rm(current);
157 ConstantPool* cp = ik->constants();
158 GrowableArray<bool> preresolve_list(cp->length(), cp->length(), false);
159 for (int j = 0; j < cp_indices->length(); j++) {
160 preresolve_list.at_put(cp_indices->at(j), true);
161 }
162 if ((flags & CP_RESOLVE_CLASS) != 0) {
163 AOTConstantPoolResolver::preresolve_class_cp_entries(current, ik, &preresolve_list);
164 }
165 if ((flags & CP_RESOLVE_FIELD_AND_METHOD) != 0) {
166 AOTConstantPoolResolver::preresolve_field_and_method_cp_entries(current, ik, &preresolve_list);
167 }
168 if ((flags & CP_RESOLVE_INDY) != 0) {
169 AOTConstantPoolResolver::preresolve_indy_cp_entries(current, ik, &preresolve_list);
170 }
171 }
172 }
173 }
174 }
175
176 void FinalImageRecipes::record_recipes_for_reflection_data() {
177 int reflect_count = 0;
178 if (_tmp_reflect_klasses != nullptr) {
179 for (int i = _tmp_reflect_klasses->length() - 1; i >= 0; i--) {
180 InstanceKlass* ik = _tmp_reflect_klasses->at(i);
181 if (SystemDictionaryShared::is_excluded_class(ik)) {
182 _tmp_reflect_klasses->remove_at(i);
183 _tmp_reflect_flags->remove_at(i);
184 } else {
185 _tmp_reflect_klasses->at_put(i, ArchiveBuilder::current()->get_buffered_addr(ik));
186 }
187 }
188 if (_tmp_reflect_klasses->length() > 0) {
189 _reflect_klasses = ArchiveUtils::archive_array(_tmp_reflect_klasses);
190 _reflect_flags = ArchiveUtils::archive_array(_tmp_reflect_flags);
191
192 ArchivePtrMarker::mark_pointer(&_reflect_klasses);
193 ArchivePtrMarker::mark_pointer(&_reflect_flags);
194 reflect_count = _tmp_reflect_klasses->length();
195 }
196 }
197 log_info(cds)("ReflectionData of %d classes will be archived in final CDS image", reflect_count);
198 }
199
200 void FinalImageRecipes::record_recipes_for_dynamic_proxies() {
201 if (_tmp_dynamic_proxy_classes != nullptr) {
202 // Remove proxies for excluded classes
203 for (int i = _tmp_dynamic_proxy_classes->length() - 1; i >= 0; i--) {
204 TmpDynamicProxyClassInfo* tmp_info = _tmp_dynamic_proxy_classes->adr_at(i);
205 bool exclude = false;
206 for (int j = 0; j < tmp_info->_interfaces->length(); j++) {
207 if (SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(tmp_info->_interfaces->at(j)))) {
208 exclude = true;
209 break;
210 }
211 }
212 if (exclude) {
213 _tmp_dynamic_proxy_classes->remove_at(i);
214 }
215 }
216 int len = _tmp_dynamic_proxy_classes->length();
217 _dynamic_proxy_classes = ArchiveBuilder::new_ro_array<DynamicProxyClassInfo>(len);
218 ArchivePtrMarker::mark_pointer(&_dynamic_proxy_classes);
219 for (int i = 0; i < len; i++) {
220 TmpDynamicProxyClassInfo* tmp_info = _tmp_dynamic_proxy_classes->adr_at(i);
221 DynamicProxyClassInfo* info = _dynamic_proxy_classes->adr_at(i);
222 info->_loader_type = tmp_info->_loader_type;
223 info->_access_flags = tmp_info->_access_flags;
224 info->_proxy_name = ArchiveBuilder::current()->ro_strdup(tmp_info->_proxy_name);
225
226 ResourceMark rm;
227 GrowableArray<Klass*> buffered_interfaces;
228 for (int j = 0; j < tmp_info->_interfaces->length(); j++) {
229 InstanceKlass* intf = InstanceKlass::cast(tmp_info->_interfaces->at(j));
230 assert(!SystemDictionaryShared::is_excluded_class(intf), "sanity");
231 buffered_interfaces.append(ArchiveBuilder::current()->get_buffered_addr(intf));
232 }
233 info->_interfaces = ArchiveUtils::archive_array(&buffered_interfaces);
234
235 ArchivePtrMarker::mark_pointer(&info->_proxy_name);
236 ArchivePtrMarker::mark_pointer(&info->_interfaces);
237 ArchiveBuilder::alloc_stats()->record_dynamic_proxy_class();
238 }
239 }
240 }
241
242 void FinalImageRecipes::load_all_classes(TRAPS) {
243 assert(CDSConfig::is_dumping_final_static_archive(), "sanity");
244 Handle class_loader(THREAD, SystemDictionary::java_system_loader());
245 for (int i = 0; i < _all_klasses->length(); i++) {
246 Klass* k = _all_klasses->at(i);
247 int flags = _flags->at(i);
248 if (k->is_instance_klass()) {
249 InstanceKlass* ik = InstanceKlass::cast(k);
250 if (ik->defined_by_other_loaders()) {
251 SystemDictionaryShared::init_dumptime_info(ik);
252 SystemDictionaryShared::add_unregistered_class(THREAD, ik);
253 SystemDictionaryShared::copy_unregistered_class_size_and_crc32(ik);
254 } else if (!ik->is_hidden()) {
255 Klass* actual = SystemDictionary::resolve_or_fail(ik->name(), class_loader, true, CHECK);
256 if (actual != ik) {
257 ResourceMark rm(THREAD);
258 log_error(aot)("Unable to resolve class from CDS archive: %s", ik->external_name());
259 log_error(aot)("Expected: " INTPTR_FORMAT ", actual: " INTPTR_FORMAT, p2i(ik), p2i(actual));
260 log_error(aot)("Please check if your VM command-line is the same as in the training run");
261 AOTMetaspace::unrecoverable_writing_error();
262 }
263 assert(ik->is_loaded(), "must be");
264 ik->link_class(CHECK);
265
266 if (ik->has_aot_safe_initializer() && (flags & WAS_INITED) != 0) {
267 assert(ik->class_loader() == nullptr, "supported only for boot classes for now");
268 ik->initialize(CHECK);
269 }
270 }
271 }
272 }
273 }
274
275 void FinalImageRecipes::apply_recipes_for_reflection_data(JavaThread* current) {
276 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
277
278 if (CDSConfig::is_dumping_reflection_data() && _reflect_klasses != nullptr) {
279 assert(_reflect_flags != nullptr, "must be");
280 for (int i = 0; i < _reflect_klasses->length(); i++) {
281 InstanceKlass* ik = _reflect_klasses->at(i);
282 int rd_flags = _reflect_flags->at(i);
283 AOTConstantPoolResolver::generate_reflection_data(current, ik, rd_flags);
284 }
285 }
286 }
287
288 void FinalImageRecipes::add_reflection_data_flags(InstanceKlass* ik, TRAPS) {
289 assert(CDSConfig::is_dumping_preimage_static_archive(), "must be");
290 if (ik->is_linked() &&
291 SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) && !ik->is_hidden() &&
292 java_lang_Class::has_reflection_data(ik->java_mirror())) {
293 int rd_flags = AOTConstantPoolResolver::class_reflection_data_flags(ik, CHECK);
294
295 MutexLocker mu(FinalImageRecipes_lock, Mutex::_no_safepoint_check_flag);
296 if (_tmp_reflect_klasses == nullptr) {
297 _tmp_reflect_klasses = new (mtClassShared) GrowableArray<InstanceKlass*>(100, mtClassShared);
298 _tmp_reflect_flags = new (mtClassShared) GrowableArray<int>(100, mtClassShared);
299 }
300 _tmp_reflect_klasses->append(ik);
301 _tmp_reflect_flags->append(rd_flags);
302 }
303 }
304
305 void FinalImageRecipes::add_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags) {
306 int loader_type;
307 if (loader == nullptr) {
308 loader_type = ClassLoader::BOOT_LOADER;
309 } else if (loader == SystemDictionary::java_platform_loader()) {
310 loader_type = ClassLoader::PLATFORM_LOADER;
311 } else if (loader == SystemDictionary::java_system_loader()) {
312 loader_type = ClassLoader::APP_LOADER;
313 } else {
314 return;
315 }
316
317 MutexLocker mu(FinalImageRecipes_lock, Mutex::_no_safepoint_check_flag);
318 if (_tmp_dynamic_proxy_classes == nullptr) {
319 _tmp_dynamic_proxy_classes = new (mtClassShared) GrowableArray<TmpDynamicProxyClassInfo>(32, mtClassShared);
320 }
321
322 log_info(cds, dynamic, proxy)("Adding proxy name %s", proxy_name);
323
324 TmpDynamicProxyClassInfo info;
325 info._loader_type = loader_type;
326 info._access_flags = access_flags;
327 info._proxy_name = os::strdup(proxy_name);
328 info._interfaces = new (mtClassShared) GrowableArray<Klass*>(interfaces->length(), mtClassShared);
329 for (int i = 0; i < interfaces->length(); i++) {
330 Klass* intf = java_lang_Class::as_Klass(interfaces->obj_at(i));
331 info._interfaces->append(InstanceKlass::cast(intf));
332
333 if (log_is_enabled(Info, cds, dynamic, proxy)) {
334 ResourceMark rm;
335 log_info(cds, dynamic, proxy)("interface[%d] = %s", i, intf->external_name());
336 }
337 }
338 _tmp_dynamic_proxy_classes->append(info);
339 }
340
341 void FinalImageRecipes::apply_recipes_for_dynamic_proxies(TRAPS) {
342 if (CDSConfig::is_dumping_dynamic_proxies() && _dynamic_proxy_classes != nullptr) {
343 for (int proxy_index = 0; proxy_index < _dynamic_proxy_classes->length(); proxy_index++) {
344 DynamicProxyClassInfo* info = _dynamic_proxy_classes->adr_at(proxy_index);
345
346 Handle loader(THREAD, ArchiveUtils::builtin_loader_from_type(info->_loader_type));
347 oop proxy_name_oop = java_lang_String::create_oop_from_str(info->_proxy_name, CHECK);
348 Handle proxy_name(THREAD, proxy_name_oop);
349
350 int num_intfs = info->_interfaces->length();
351 objArrayOop interfaces_oop = oopFactory::new_objArray(vmClasses::Class_klass(), num_intfs, CHECK);
352 objArrayHandle interfaces(THREAD, interfaces_oop);
353 for (int intf_index = 0; intf_index < num_intfs; intf_index++) {
354 Klass* k = info->_interfaces->at(intf_index);
355 assert(k->java_mirror() != nullptr, "must be loaded");
356 interfaces()->obj_at_put(intf_index, k->java_mirror());
357 }
358
359 AOTConstantPoolResolver::define_dynamic_proxy_class(loader, proxy_name, interfaces, info->_access_flags, CHECK);
360 }
361 }
362 }
363
364 void FinalImageRecipes::record_recipes() {
365 assert(CDSConfig::is_dumping_preimage_static_archive(), "must be");
366 _final_image_recipes = new FinalImageRecipes();
367 _final_image_recipes->record_all_classes();
368 _final_image_recipes->record_recipes_for_constantpool();
369 _final_image_recipes->record_recipes_for_reflection_data();
370 _final_image_recipes->record_recipes_for_dynamic_proxies();
371 }
372
373 void FinalImageRecipes::apply_recipes(TRAPS) {
374 assert(CDSConfig::is_dumping_final_static_archive(), "must be");
375 if (_final_image_recipes != nullptr) {
376 _final_image_recipes->apply_recipes_impl(THREAD);
377 if (HAS_PENDING_EXCEPTION) {
378 log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
379 java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
380 log_error(aot)("Please check if your VM command-line is the same as in the training run");
381 AOTMetaspace::unrecoverable_writing_error("Unexpected exception, use -Xlog:aot,exceptions=trace for detail");
382 }
383 }
384
385 // Set it to null as we don't need to write this table into the final image.
386 _final_image_recipes = nullptr;
387 }
388
389 void FinalImageRecipes::apply_recipes_impl(TRAPS) {
390 load_all_classes(CHECK);
391 apply_recipes_for_constantpool(THREAD);
392 apply_recipes_for_reflection_data(CHECK);
393 apply_recipes_for_dynamic_proxies(CHECK);
394 }
395
396 void FinalImageRecipes::serialize(SerializeClosure* soc) {
397 soc->do_ptr((void**)&_final_image_recipes);
398 }
|