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/archiveHeapWriter.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveBuilder.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classPrelinker.hpp"
31 #include "cds/heapShared.hpp"
32 #include "classfile/classLoader.hpp"
33 #include "classfile/classLoaderData.hpp"
34 #include "classfile/javaClasses.inline.hpp"
35 #include "classfile/metadataOnStackMark.hpp"
36 #include "classfile/stringTable.hpp"
37 #include "classfile/systemDictionary.hpp"
38 #include "classfile/vmClasses.hpp"
39 #include "classfile/vmSymbols.hpp"
40 #include "code/codeCache.hpp"
41 #include "interpreter/bootstrapInfo.hpp"
42 #include "interpreter/linkResolver.hpp"
43 #include "jvm.h"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "memory/allocation.inline.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/metaspaceClosure.hpp"
49 #include "memory/oopFactory.hpp"
50 #include "memory/resourceArea.hpp"
51 #include "memory/universe.hpp"
52 #include "oops/array.hpp"
53 #include "oops/constantPool.inline.hpp"
54 #include "oops/cpCache.inline.hpp"
55 #include "oops/instanceKlass.hpp"
56 #include "oops/klass.inline.hpp"
57 #include "oops/objArrayKlass.hpp"
266 #endif
267 }
268 }
269 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
270 }
271
272 // Hidden class support:
273 void ConstantPool::klass_at_put(int class_index, Klass* k) {
274 assert(k != nullptr, "must be valid klass");
275 CPKlassSlot kslot = klass_slot_at(class_index);
276 int resolved_klass_index = kslot.resolved_klass_index();
277 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
278 Atomic::release_store(adr, k);
279
280 // The interpreter assumes when the tag is stored, the klass is resolved
281 // and the Klass* non-null, so we need hardware store ordering here.
282 release_tag_at_put(class_index, JVM_CONSTANT_Class);
283 }
284
285 #if INCLUDE_CDS_JAVA_HEAP
286 // Returns the _resolved_reference array after removing unarchivable items from it.
287 // Returns null if this class is not supported, or _resolved_reference doesn't exist.
288 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() {
289 if (_cache == nullptr) {
290 return nullptr; // nothing to do
291 }
292
293 InstanceKlass *ik = pool_holder();
294 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
295 ik->is_shared_app_class())) {
296 // Archiving resolved references for classes from non-builtin loaders
297 // is not yet supported.
298 return nullptr;
299 }
300
301 objArrayOop rr = resolved_references();
302 if (rr != nullptr) {
303 int rr_len = rr->length();
304 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
305 objArrayOop scratch_rr = HeapShared::scratch_resolved_references(src_cp);
306 Array<u2>* ref_map = reference_map();
307 int ref_map_len = ref_map == nullptr ? 0 : ref_map->length();
308 for (int i = 0; i < rr_len; i++) {
309 oop obj = rr->obj_at(i);
310 scratch_rr->obj_at_put(i, nullptr);
311 if (obj != nullptr) {
312 if (i < ref_map_len) {
313 int index = object_to_cp_index(i);
314 if (tag_at(index).is_string()) {
315 assert(java_lang_String::is_instance(obj), "must be");
316 if (!ArchiveHeapWriter::is_string_too_large_to_archive(obj)) {
317 scratch_rr->obj_at_put(i, obj);
318 }
319 }
320 }
321 }
322 }
323 return scratch_rr;
324 }
325 return rr;
326 }
327
328 void ConstantPool::add_dumped_interned_strings() {
329 objArrayOop rr = resolved_references();
330 if (rr != nullptr) {
331 int rr_len = rr->length();
332 for (int i = 0; i < rr_len; i++) {
333 oop p = rr->obj_at(i);
334 if (java_lang_String::is_instance(p) &&
335 !ArchiveHeapWriter::is_string_too_large_to_archive(p)) {
336 HeapShared::add_to_dumped_interned_strings(p);
337 }
338 }
339 }
340 }
341 #endif
342
343 #if INCLUDE_CDS
344 // CDS support. Create a new resolved_references array.
345 void ConstantPool::restore_unshareable_info(TRAPS) {
346 if (!_pool_holder->is_linked() && !_pool_holder->is_rewritten()) {
347 return;
348 }
349 assert(is_constantPool(), "ensure C++ vtable is restored");
350 assert(on_stack(), "should always be set for shared constant pools");
351 assert(is_shared(), "should always be set for shared constant pools");
352 assert(_cache != nullptr, "constant pool _cache should not be null");
353
354 // Only create the new resolved references array if it hasn't been attempted before
355 if (resolved_references() != nullptr) return;
356
357 if (vmClasses::Object_klass_loaded()) {
358 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
359 #if INCLUDE_CDS_JAVA_HEAP
360 if (ArchiveHeapLoader::is_in_use() &&
361 _cache->archived_references() != nullptr) {
362 oop archived = _cache->archived_references();
363 // Create handle for the archived resolved reference array object
364 HandleMark hm(THREAD);
365 Handle refs_handle(THREAD, archived);
366 set_resolved_references(loader_data->add_handle(refs_handle));
367 _cache->clear_archived_references();
368 } else
369 #endif
370 {
371 // No mapped archived resolved reference array
372 // Recreate the object array and add to ClassLoaderData.
373 int map_length = resolved_reference_length();
374 if (map_length > 0) {
375 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
376 HandleMark hm(THREAD);
377 Handle refs_handle(THREAD, stom); // must handleize.
378 set_resolved_references(loader_data->add_handle(refs_handle));
379 }
380 }
381 }
382 }
383
384 void ConstantPool::remove_unshareable_info() {
385 // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
386 // The _on_stack flag is used to prevent ConstantPools from deallocation during
387 // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
388 // we always set _on_stack to true to avoid having to change _flags during runtime.
389 _flags |= (_on_stack | _is_shared);
390
391 // resolved_references(): remember its length. If it cannot be restored
392 // from the archived heap objects at run time, we need to dynamically allocate it.
393 if (cache() != nullptr) {
394 set_resolved_reference_length(
395 resolved_references() != nullptr ? resolved_references()->length() : 0);
396 set_resolved_references(OopHandle());
397 }
398 remove_unshareable_entries();
399 }
400
401 static const char* get_type(Klass* k) {
402 const char* type;
403 Klass* src_k;
404 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
405 src_k = ArchiveBuilder::current()->get_source_addr(k);
406 } else {
407 src_k = k;
408 }
409
410 if (src_k->is_objArray_klass()) {
731
732 // Translate index, which could be CPCache index or Indy index, to a constant pool index
733 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
734 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
735 switch(code) {
736 case Bytecodes::_invokedynamic:
737 return invokedynamic_bootstrap_ref_index_at(index);
738 case Bytecodes::_getfield:
739 case Bytecodes::_getstatic:
740 case Bytecodes::_putfield:
741 case Bytecodes::_putstatic:
742 return resolved_field_entry_at(index)->constant_pool_index();
743 case Bytecodes::_invokeinterface:
744 case Bytecodes::_invokehandle:
745 case Bytecodes::_invokespecial:
746 case Bytecodes::_invokestatic:
747 case Bytecodes::_invokevirtual:
748 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
749 return resolved_method_entry_at(index)->constant_pool_index();
750 default:
751 tty->print_cr("Unexpected bytecode: %d", code);
752 ShouldNotReachHere(); // All cases should have been handled
753 return -1;
754 }
755 }
756
757 bool ConstantPool::is_resolved(int index, Bytecodes::Code code) {
758 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
759 switch(code) {
760 case Bytecodes::_invokedynamic:
761 return resolved_indy_entry_at(index)->is_resolved();
762
763 case Bytecodes::_getfield:
764 case Bytecodes::_getstatic:
765 case Bytecodes::_putfield:
766 case Bytecodes::_putstatic:
767 return resolved_field_entry_at(index)->is_resolved(code);
768
769 case Bytecodes::_invokeinterface:
770 case Bytecodes::_invokehandle:
771 case Bytecodes::_invokespecial:
772 case Bytecodes::_invokestatic:
773 case Bytecodes::_invokevirtual:
|
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/archiveHeapWriter.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveBuilder.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classPrelinker.hpp"
31 #include "cds/lambdaFormInvokers.inline.hpp"
32 #include "cds/heapShared.hpp"
33 #include "classfile/classLoader.hpp"
34 #include "classfile/classLoaderData.hpp"
35 #include "classfile/javaClasses.inline.hpp"
36 #include "classfile/metadataOnStackMark.hpp"
37 #include "classfile/stringTable.hpp"
38 #include "classfile/systemDictionary.hpp"
39 #include "classfile/systemDictionaryShared.hpp"
40 #include "classfile/vmClasses.hpp"
41 #include "classfile/vmSymbols.hpp"
42 #include "code/codeCache.hpp"
43 #include "interpreter/bootstrapInfo.hpp"
44 #include "interpreter/linkResolver.hpp"
45 #include "jvm.h"
46 #include "logging/log.hpp"
47 #include "logging/logStream.hpp"
48 #include "memory/allocation.inline.hpp"
49 #include "memory/metadataFactory.hpp"
50 #include "memory/metaspaceClosure.hpp"
51 #include "memory/oopFactory.hpp"
52 #include "memory/resourceArea.hpp"
53 #include "memory/universe.hpp"
54 #include "oops/array.hpp"
55 #include "oops/constantPool.inline.hpp"
56 #include "oops/cpCache.inline.hpp"
57 #include "oops/instanceKlass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/objArrayKlass.hpp"
268 #endif
269 }
270 }
271 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
272 }
273
274 // Hidden class support:
275 void ConstantPool::klass_at_put(int class_index, Klass* k) {
276 assert(k != nullptr, "must be valid klass");
277 CPKlassSlot kslot = klass_slot_at(class_index);
278 int resolved_klass_index = kslot.resolved_klass_index();
279 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
280 Atomic::release_store(adr, k);
281
282 // The interpreter assumes when the tag is stored, the klass is resolved
283 // and the Klass* non-null, so we need hardware store ordering here.
284 release_tag_at_put(class_index, JVM_CONSTANT_Class);
285 }
286
287 #if INCLUDE_CDS_JAVA_HEAP
288 template <typename Function>
289 void ConstantPool::iterate_archivable_resolved_references(Function function) {
290 objArrayOop rr = resolved_references();
291 if (rr != nullptr && cache() != nullptr && CDSConfig::is_dumping_invokedynamic()) {
292 Array<ResolvedIndyEntry>* indy_entries = cache()->resolved_indy_entries();
293 if (indy_entries != nullptr) {
294 for (int i = 0; i < indy_entries->length(); i++) {
295 ResolvedIndyEntry *rie = indy_entries->adr_at(i);
296 if (rie->is_resolved() && ClassPrelinker::is_resolution_deterministic(this, rie->constant_pool_index())) {
297 int rr_index = rie->resolved_references_index();
298 function(rr_index);
299 }
300 }
301 }
302
303 Array<ResolvedMethodEntry>* method_entries = cache()->resolved_method_entries();
304 if (method_entries != nullptr) {
305 for (int i = 0; i < method_entries->length(); i++) {
306 ResolvedMethodEntry* rme = method_entries->adr_at(i);
307 if (rme->is_resolved(Bytecodes::_invokehandle) && rme->has_appendix() &&
308 cache()->can_archive_resolved_method(this, rme)) {
309 int rr_index = rme->resolved_references_index();
310 function(rr_index);
311 }
312 }
313 }
314 }
315 }
316
317 // Returns the _resolved_reference array after removing unarchivable items from it.
318 // Returns null if this class is not supported, or _resolved_reference doesn't exist.
319 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() {
320 if (_cache == nullptr) {
321 return nullptr; // nothing to do
322 }
323
324 InstanceKlass *ik = pool_holder();
325 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
326 ik->is_shared_app_class())) {
327 // Archiving resolved references for classes from non-builtin loaders
328 // is not yet supported.
329 return nullptr;
330 }
331
332 objArrayOop rr = resolved_references();
333 if (rr != nullptr) {
334 ResourceMark rm;
335 int rr_len = rr->length();
336 GrowableArray<bool> keep_resolved_refs(rr_len, rr_len, false);
337
338 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
339 src_cp->iterate_archivable_resolved_references([&](int rr_index) {
340 keep_resolved_refs.at_put(rr_index, true);
341 });
342
343 objArrayOop scratch_rr = HeapShared::scratch_resolved_references(src_cp);
344 Array<u2>* ref_map = reference_map();
345 int ref_map_len = ref_map == nullptr ? 0 : ref_map->length();
346 for (int i = 0; i < rr_len; i++) {
347 oop obj = rr->obj_at(i);
348 scratch_rr->obj_at_put(i, nullptr);
349 if (obj != nullptr) {
350 if (i < ref_map_len) {
351 int index = object_to_cp_index(i);
352 if (tag_at(index).is_string()) {
353 assert(java_lang_String::is_instance(obj), "must be");
354 if (!ArchiveHeapWriter::is_string_too_large_to_archive(obj)) {
355 scratch_rr->obj_at_put(i, obj);
356 }
357 }
358 } else if (keep_resolved_refs.at(i)) {
359 scratch_rr->obj_at_put(i, obj);
360 }
361 }
362 }
363 return scratch_rr;
364 }
365 return rr;
366 }
367
368 void ConstantPool::find_archivable_hidden_classes() {
369 if (_cache == nullptr) {
370 return;
371 }
372
373 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
374 if (loader_data == nullptr) {
375 // These are custom loader classes from the preimage
376 return;
377 }
378
379 if (!SystemDictionaryShared::is_builtin_loader(loader_data)) {
380 // Archiving resolved references for classes from non-builtin loaders
381 // is not yet supported.
382 return;
383 }
384
385 objArrayOop rr = resolved_references();
386 if (rr != nullptr) {
387 iterate_archivable_resolved_references([&](int rr_index) {
388 oop obj = rr->obj_at(rr_index);
389 HeapShared::find_archivable_hidden_classes_in_object(obj);
390 });
391 }
392 }
393
394 void ConstantPool::add_dumped_interned_strings() {
395 objArrayOop rr = resolved_references();
396 if (rr != nullptr) {
397 int rr_len = rr->length();
398 for (int i = 0; i < rr_len; i++) {
399 oop p = rr->obj_at(i);
400 if (java_lang_String::is_instance(p) &&
401 !ArchiveHeapWriter::is_string_too_large_to_archive(p)) {
402 HeapShared::add_to_dumped_interned_strings(p);
403 }
404 }
405 }
406 }
407 #endif
408
409 #if INCLUDE_CDS
410 // CDS support. Create a new resolved_references array.
411 void ConstantPool::restore_unshareable_info(TRAPS) {
412 if (!_pool_holder->is_linked() && !_pool_holder->is_rewritten()) {
413 return;
414 }
415 assert(is_constantPool(), "ensure C++ vtable is restored");
416 assert(on_stack(), "should always be set for shared constant pools");
417 assert(is_shared(), "should always be set for shared constant pools");
418 if (is_for_method_handle_intrinsic()) {
419 return;
420 }
421 assert(_cache != nullptr, "constant pool _cache should not be null");
422
423 // Only create the new resolved references array if it hasn't been attempted before
424 if (resolved_references() != nullptr) return;
425
426 if (vmClasses::Object_klass_loaded()) {
427 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
428 #if INCLUDE_CDS_JAVA_HEAP
429 if (ArchiveHeapLoader::is_in_use() &&
430 _cache->archived_references() != nullptr) {
431 oop archived = _cache->archived_references();
432 // Create handle for the archived resolved reference array object
433 HandleMark hm(THREAD);
434 Handle refs_handle(THREAD, archived);
435 set_resolved_references(loader_data->add_handle(refs_handle));
436 _cache->clear_archived_references();
437 } else
438 #endif
439 {
440 // No mapped archived resolved reference array
441 // Recreate the object array and add to ClassLoaderData.
442 int map_length = resolved_reference_length();
443 if (map_length > 0) {
444 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
445 HandleMark hm(THREAD);
446 Handle refs_handle(THREAD, stom); // must handleize.
447 set_resolved_references(loader_data->add_handle(refs_handle));
448 }
449 }
450 }
451
452 if (CDSConfig::is_dumping_final_static_archive() && resolved_references() != nullptr) {
453 objArrayOop scratch_references = oopFactory::new_objArray(vmClasses::Object_klass(), resolved_references()->length(), CHECK);
454 HeapShared::add_scratch_resolved_references(this, scratch_references);
455 }
456 }
457
458 void ConstantPool::remove_unshareable_info() {
459 // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
460 // The _on_stack flag is used to prevent ConstantPools from deallocation during
461 // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
462 // we always set _on_stack to true to avoid having to change _flags during runtime.
463 _flags |= (_on_stack | _is_shared);
464
465 if (!ArchiveBuilder::current()->get_source_addr(_pool_holder)->is_linked()) {
466 return;
467 }
468
469 if (is_for_method_handle_intrinsic()) {
470 // This CP was created by Method::make_method_handle_intrinsic() and has nothing
471 // that need to be removed/restored. It has no cpCache since the intrinsic methods
472 // don't have any bytecodes.
473 assert(cache() == NULL, "must not have cpCache");
474 return;
475 }
476
477 // resolved_references(): remember its length. If it cannot be restored
478 // from the archived heap objects at run time, we need to dynamically allocate it.
479 if (cache() != nullptr) {
480 set_resolved_reference_length(
481 resolved_references() != nullptr ? resolved_references()->length() : 0);
482 set_resolved_references(OopHandle());
483 }
484 remove_unshareable_entries();
485 }
486
487 static const char* get_type(Klass* k) {
488 const char* type;
489 Klass* src_k;
490 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
491 src_k = ArchiveBuilder::current()->get_source_addr(k);
492 } else {
493 src_k = k;
494 }
495
496 if (src_k->is_objArray_klass()) {
817
818 // Translate index, which could be CPCache index or Indy index, to a constant pool index
819 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
820 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
821 switch(code) {
822 case Bytecodes::_invokedynamic:
823 return invokedynamic_bootstrap_ref_index_at(index);
824 case Bytecodes::_getfield:
825 case Bytecodes::_getstatic:
826 case Bytecodes::_putfield:
827 case Bytecodes::_putstatic:
828 return resolved_field_entry_at(index)->constant_pool_index();
829 case Bytecodes::_invokeinterface:
830 case Bytecodes::_invokehandle:
831 case Bytecodes::_invokespecial:
832 case Bytecodes::_invokestatic:
833 case Bytecodes::_invokevirtual:
834 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
835 return resolved_method_entry_at(index)->constant_pool_index();
836 default:
837 fatal("Unexpected bytecode: %s", Bytecodes::name(code));
838 }
839 }
840
841 bool ConstantPool::is_resolved(int index, Bytecodes::Code code) {
842 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
843 switch(code) {
844 case Bytecodes::_invokedynamic:
845 return resolved_indy_entry_at(index)->is_resolved();
846
847 case Bytecodes::_getfield:
848 case Bytecodes::_getstatic:
849 case Bytecodes::_putfield:
850 case Bytecodes::_putstatic:
851 return resolved_field_entry_at(index)->is_resolved(code);
852
853 case Bytecodes::_invokeinterface:
854 case Bytecodes::_invokehandle:
855 case Bytecodes::_invokespecial:
856 case Bytecodes::_invokestatic:
857 case Bytecodes::_invokevirtual:
|