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/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()) {
465 // cache() is null if this class is not yet linked.
466 cache()->remove_unshareable_info();
467 }
468 }
469
470 void ConstantPool::remove_resolved_klass_if_non_deterministic(int cp_index) {
471 assert(ArchiveBuilder::current()->is_in_buffer_space(this), "must be");
472 assert(tag_at(cp_index).is_klass(), "must be resolved");
473
474 Klass* k = resolved_klass_at(cp_index);
475 bool can_archive;
476
477 if (k == nullptr) {
478 // We'd come here if the referenced class has been excluded via
479 // SystemDictionaryShared::is_excluded_class(). As a result, ArchiveBuilder
480 // has cleared the resolved_klasses()->at(...) pointer to NULL. Thus, we
481 // need to revert the tag to JVM_CONSTANT_UnresolvedClass.
482 can_archive = false;
483 } else {
484 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
485 can_archive = ClassPrelinker::is_resolution_deterministic(src_cp, cp_index);
486 }
487
488 if (!can_archive) {
489 int resolved_klass_index = klass_slot_at(cp_index).resolved_klass_index();
490 resolved_klasses()->at_put(resolved_klass_index, nullptr);
491 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
492 }
493
494 LogStreamHandle(Trace, cds, resolve) log;
495 if (log.is_enabled()) {
496 ResourceMark rm;
497 log.print("%s klass CP entry [%3d]: %s %s",
498 (can_archive ? "archived" : "reverted"),
499 cp_index, pool_holder()->name()->as_C_string(), get_type(pool_holder()));
500 if (can_archive) {
501 log.print(" => %s %s%s", k->name()->as_C_string(), get_type(k),
502 (!k->is_instance_klass() || pool_holder()->is_subtype_of(k)) ? "" : " (not supertype)");
503 } else {
504 Symbol* name = klass_name_at(cp_index);
505 log.print(" %s", name->as_C_string());
506 }
507 }
508
509 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(can_archive, /*reverted=*/!can_archive);
510 }
511 #endif // INCLUDE_CDS
512
513 int ConstantPool::cp_to_object_index(int cp_index) {
514 // this is harder don't do this so much.
515 int i = reference_map()->find(checked_cast<u2>(cp_index));
516 // We might not find the index for jsr292 call.
517 return (i < 0) ? _no_index_sentinel : i;
518 }
519
520 void ConstantPool::string_at_put(int obj_index, oop str) {
521 oop result = set_resolved_reference_at(obj_index, str);
522 assert(result == nullptr || result == str, "Only set once or to the same string.");
523 }
524
525 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
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:
|
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/aotConstantPoolResolver.hpp"
27 #include "cds/archiveHeapWriter.hpp"
28 #include "cds/archiveHeapLoader.hpp"
29 #include "cds/archiveBuilder.hpp"
30 #include "cds/cdsConfig.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/systemDictionaryShared.hpp"
39 #include "classfile/vmClasses.hpp"
40 #include "classfile/vmSymbols.hpp"
41 #include "code/codeCache.hpp"
42 #include "interpreter/bootstrapInfo.hpp"
43 #include "interpreter/linkResolver.hpp"
44 #include "jvm.h"
45 #include "logging/log.hpp"
46 #include "logging/logStream.hpp"
47 #include "memory/allocation.inline.hpp"
48 #include "memory/metadataFactory.hpp"
49 #include "memory/metaspaceClosure.hpp"
50 #include "memory/oopFactory.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "oops/array.hpp"
54 #include "oops/constantPool.inline.hpp"
55 #include "oops/cpCache.inline.hpp"
56 #include "oops/instanceKlass.hpp"
57 #include "oops/klass.inline.hpp"
58 #include "oops/objArrayKlass.hpp"
267 #endif
268 }
269 }
270 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
271 }
272
273 // Hidden class support:
274 void ConstantPool::klass_at_put(int class_index, Klass* k) {
275 assert(k != nullptr, "must be valid klass");
276 CPKlassSlot kslot = klass_slot_at(class_index);
277 int resolved_klass_index = kslot.resolved_klass_index();
278 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
279 Atomic::release_store(adr, k);
280
281 // The interpreter assumes when the tag is stored, the klass is resolved
282 // and the Klass* non-null, so we need hardware store ordering here.
283 release_tag_at_put(class_index, JVM_CONSTANT_Class);
284 }
285
286 #if INCLUDE_CDS_JAVA_HEAP
287 template <typename Function>
288 void ConstantPool::iterate_archivable_resolved_references(Function function) {
289 objArrayOop rr = resolved_references();
290 if (rr != nullptr && cache() != nullptr && CDSConfig::is_dumping_invokedynamic()) {
291 Array<ResolvedIndyEntry>* indy_entries = cache()->resolved_indy_entries();
292 if (indy_entries != nullptr) {
293 for (int i = 0; i < indy_entries->length(); i++) {
294 ResolvedIndyEntry *rie = indy_entries->adr_at(i);
295 if (rie->is_resolved() && AOTConstantPoolResolver::is_resolution_deterministic(this, rie->constant_pool_index())) {
296 int rr_index = rie->resolved_references_index();
297 function(rr_index);
298 }
299 }
300 }
301
302 Array<ResolvedMethodEntry>* method_entries = cache()->resolved_method_entries();
303 if (method_entries != nullptr) {
304 for (int i = 0; i < method_entries->length(); i++) {
305 ResolvedMethodEntry* rme = method_entries->adr_at(i);
306 if (rme->is_resolved(Bytecodes::_invokehandle) && rme->has_appendix() &&
307 cache()->can_archive_resolved_method(this, rme)) {
308 int rr_index = rme->resolved_references_index();
309 function(rr_index);
310 }
311 }
312 }
313 }
314 }
315
316 // Returns the _resolved_reference array after removing unarchivable items from it.
317 // Returns null if this class is not supported, or _resolved_reference doesn't exist.
318 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() {
319 if (_cache == nullptr) {
320 return nullptr; // nothing to do
321 }
322
323 InstanceKlass *ik = pool_holder();
324 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
325 ik->is_shared_app_class())) {
326 // Archiving resolved references for classes from non-builtin loaders
327 // is not yet supported.
328 return nullptr;
329 }
330
331 objArrayOop rr = resolved_references();
332 if (rr != nullptr) {
333 ResourceMark rm;
334 int rr_len = rr->length();
335 GrowableArray<bool> keep_resolved_refs(rr_len, rr_len, false);
336
337 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
338 src_cp->iterate_archivable_resolved_references([&](int rr_index) {
339 keep_resolved_refs.at_put(rr_index, true);
340 });
341
342 objArrayOop scratch_rr = HeapShared::scratch_resolved_references(src_cp);
343 Array<u2>* ref_map = reference_map();
344 int ref_map_len = ref_map == nullptr ? 0 : ref_map->length();
345 for (int i = 0; i < rr_len; i++) {
346 oop obj = rr->obj_at(i);
347 scratch_rr->obj_at_put(i, nullptr);
348 if (obj != nullptr) {
349 if (i < ref_map_len) {
350 int index = object_to_cp_index(i);
351 if (tag_at(index).is_string()) {
352 assert(java_lang_String::is_instance(obj), "must be");
353 if (!ArchiveHeapWriter::is_string_too_large_to_archive(obj)) {
354 scratch_rr->obj_at_put(i, obj);
355 }
356 }
357 } else if (keep_resolved_refs.at(i)) {
358 scratch_rr->obj_at_put(i, obj);
359 }
360 }
361 }
362 return scratch_rr;
363 }
364 return rr;
365 }
366
367 void ConstantPool::find_archivable_hidden_classes() {
368 if (_cache == nullptr) {
369 return;
370 }
371
372 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
373 if (loader_data == nullptr) {
374 // These are custom loader classes from the preimage
375 return;
376 }
377
378 if (!SystemDictionaryShared::is_builtin_loader(loader_data)) {
379 // Archiving resolved references for classes from non-builtin loaders
380 // is not yet supported.
381 return;
382 }
383
384 objArrayOop rr = resolved_references();
385 if (rr != nullptr) {
386 iterate_archivable_resolved_references([&](int rr_index) {
387 oop obj = rr->obj_at(rr_index);
388 HeapShared::find_archivable_hidden_classes_in_object(obj);
389 });
390 }
391 }
392
393 void ConstantPool::add_dumped_interned_strings() {
394 objArrayOop rr = resolved_references();
395 if (rr != nullptr) {
396 int rr_len = rr->length();
397 for (int i = 0; i < rr_len; i++) {
398 oop p = rr->obj_at(i);
399 if (java_lang_String::is_instance(p) &&
400 !ArchiveHeapWriter::is_string_too_large_to_archive(p)) {
401 HeapShared::add_to_dumped_interned_strings(p);
402 }
403 }
404 }
405 }
406 #endif
407
408 #if INCLUDE_CDS
409 // CDS support. Create a new resolved_references array.
410 void ConstantPool::restore_unshareable_info(TRAPS) {
411 if (!_pool_holder->is_linked() && !_pool_holder->is_rewritten()) {
412 return;
413 }
414 assert(is_constantPool(), "ensure C++ vtable is restored");
415 assert(on_stack(), "should always be set for shared constant pools");
416 assert(is_shared(), "should always be set for shared constant pools");
417 if (is_for_method_handle_intrinsic()) {
418 return;
419 }
420 assert(_cache != nullptr, "constant pool _cache should not be null");
421
422 // Only create the new resolved references array if it hasn't been attempted before
423 if (resolved_references() != nullptr) return;
424
425 if (vmClasses::Object_klass_loaded()) {
426 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
427 #if INCLUDE_CDS_JAVA_HEAP
428 if (ArchiveHeapLoader::is_in_use() &&
429 _cache->archived_references() != nullptr) {
430 oop archived = _cache->archived_references();
431 // Create handle for the archived resolved reference array object
432 HandleMark hm(THREAD);
433 Handle refs_handle(THREAD, archived);
434 set_resolved_references(loader_data->add_handle(refs_handle));
435 _cache->clear_archived_references();
436 } else
437 #endif
438 {
439 // No mapped archived resolved reference array
440 // Recreate the object array and add to ClassLoaderData.
441 int map_length = resolved_reference_length();
442 if (map_length > 0) {
443 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
444 HandleMark hm(THREAD);
445 Handle refs_handle(THREAD, stom); // must handleize.
446 set_resolved_references(loader_data->add_handle(refs_handle));
447 }
448 }
449 }
450
451 if (CDSConfig::is_dumping_final_static_archive() && resolved_references() != nullptr) {
452 objArrayOop scratch_references = oopFactory::new_objArray(vmClasses::Object_klass(), resolved_references()->length(), CHECK);
453 HeapShared::add_scratch_resolved_references(this, scratch_references);
454 }
455 }
456
457 void ConstantPool::remove_unshareable_info() {
458 // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
459 // The _on_stack flag is used to prevent ConstantPools from deallocation during
460 // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
461 // we always set _on_stack to true to avoid having to change _flags during runtime.
462 _flags |= (_on_stack | _is_shared);
463
464 if (!ArchiveBuilder::current()->get_source_addr(_pool_holder)->is_linked()) {
465 return;
466 }
467
468 if (is_for_method_handle_intrinsic()) {
469 // This CP was created by Method::make_method_handle_intrinsic() and has nothing
470 // that need to be removed/restored. It has no cpCache since the intrinsic methods
471 // don't have any bytecodes.
472 assert(cache() == NULL, "must not have cpCache");
473 return;
474 }
475
476 // resolved_references(): remember its length. If it cannot be restored
477 // from the archived heap objects at run time, we need to dynamically allocate it.
478 if (cache() != nullptr) {
479 set_resolved_reference_length(
480 resolved_references() != nullptr ? resolved_references()->length() : 0);
481 set_resolved_references(OopHandle());
482 }
483 remove_unshareable_entries();
484 }
485
486 static const char* get_type(Klass* k) {
487 const char* type;
488 Klass* src_k;
489 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
490 src_k = ArchiveBuilder::current()->get_source_addr(k);
491 } else {
492 src_k = k;
493 }
494
495 if (src_k->is_objArray_klass()) {
550 // cache() is null if this class is not yet linked.
551 cache()->remove_unshareable_info();
552 }
553 }
554
555 void ConstantPool::remove_resolved_klass_if_non_deterministic(int cp_index) {
556 assert(ArchiveBuilder::current()->is_in_buffer_space(this), "must be");
557 assert(tag_at(cp_index).is_klass(), "must be resolved");
558
559 Klass* k = resolved_klass_at(cp_index);
560 bool can_archive;
561
562 if (k == nullptr) {
563 // We'd come here if the referenced class has been excluded via
564 // SystemDictionaryShared::is_excluded_class(). As a result, ArchiveBuilder
565 // has cleared the resolved_klasses()->at(...) pointer to NULL. Thus, we
566 // need to revert the tag to JVM_CONSTANT_UnresolvedClass.
567 can_archive = false;
568 } else {
569 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
570 can_archive = AOTConstantPoolResolver::is_resolution_deterministic(src_cp, cp_index);
571 }
572
573 if (!can_archive) {
574 int resolved_klass_index = klass_slot_at(cp_index).resolved_klass_index();
575 resolved_klasses()->at_put(resolved_klass_index, nullptr);
576 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
577 }
578
579 LogStreamHandle(Trace, cds, resolve) log;
580 if (log.is_enabled()) {
581 ResourceMark rm;
582 log.print("%s klass CP entry [%3d]: %s %s",
583 (can_archive ? "archived" : "reverted"),
584 cp_index, pool_holder()->name()->as_C_string(), get_type(pool_holder()));
585 if (can_archive) {
586 log.print(" => %s %s%s", k->name()->as_C_string(), get_type(k),
587 (!k->is_instance_klass() || pool_holder()->is_subtype_of(k)) ? "" : " (not supertype)");
588 } else {
589 Symbol* name = klass_name_at(cp_index);
590 log.print(" => %s", name->as_C_string());
591 }
592 }
593
594 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(can_archive, /*reverted=*/!can_archive);
595 }
596 #endif // INCLUDE_CDS
597
598 int ConstantPool::cp_to_object_index(int cp_index) {
599 // this is harder don't do this so much.
600 int i = reference_map()->find(checked_cast<u2>(cp_index));
601 // We might not find the index for jsr292 call.
602 return (i < 0) ? _no_index_sentinel : i;
603 }
604
605 void ConstantPool::string_at_put(int obj_index, oop str) {
606 oop result = set_resolved_reference_at(obj_index, str);
607 assert(result == nullptr || result == str, "Only set once or to the same string.");
608 }
609
610 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
816
817 // Translate index, which could be CPCache index or Indy index, to a constant pool index
818 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
819 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
820 switch(code) {
821 case Bytecodes::_invokedynamic:
822 return invokedynamic_bootstrap_ref_index_at(index);
823 case Bytecodes::_getfield:
824 case Bytecodes::_getstatic:
825 case Bytecodes::_putfield:
826 case Bytecodes::_putstatic:
827 return resolved_field_entry_at(index)->constant_pool_index();
828 case Bytecodes::_invokeinterface:
829 case Bytecodes::_invokehandle:
830 case Bytecodes::_invokespecial:
831 case Bytecodes::_invokestatic:
832 case Bytecodes::_invokevirtual:
833 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
834 return resolved_method_entry_at(index)->constant_pool_index();
835 default:
836 fatal("Unexpected bytecode: %s", Bytecodes::name(code));
837 }
838 }
839
840 bool ConstantPool::is_resolved(int index, Bytecodes::Code code) {
841 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
842 switch(code) {
843 case Bytecodes::_invokedynamic:
844 return resolved_indy_entry_at(index)->is_resolved();
845
846 case Bytecodes::_getfield:
847 case Bytecodes::_getstatic:
848 case Bytecodes::_putfield:
849 case Bytecodes::_putstatic:
850 return resolved_field_entry_at(index)->is_resolved(code);
851
852 case Bytecodes::_invokeinterface:
853 case Bytecodes::_invokehandle:
854 case Bytecodes::_invokespecial:
855 case Bytecodes::_invokestatic:
856 case Bytecodes::_invokevirtual:
|