1 /*
2 * Copyright (c) 1997, 2025, 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 "cds/aotConstantPoolResolver.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/heapShared.inline.hpp"
29 #include "classfile/classLoader.hpp"
30 #include "classfile/classLoaderData.hpp"
31 #include "classfile/javaClasses.inline.hpp"
32 #include "classfile/metadataOnStackMark.hpp"
33 #include "classfile/stringTable.hpp"
34 #include "classfile/systemDictionary.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #include "classfile/vmClasses.hpp"
37 #include "classfile/vmSymbols.hpp"
38 #include "code/codeCache.hpp"
39 #include "interpreter/bootstrapInfo.hpp"
40 #include "interpreter/linkResolver.hpp"
41 #include "jvm.h"
42 #include "logging/log.hpp"
43 #include "logging/logStream.hpp"
44 #include "memory/allocation.inline.hpp"
45 #include "memory/metadataFactory.hpp"
46 #include "memory/metaspaceClosure.hpp"
47 #include "memory/oopFactory.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "memory/universe.hpp"
50 #include "oops/array.hpp"
51 #include "oops/constantPool.inline.hpp"
52 #include "oops/cpCache.inline.hpp"
53 #include "oops/fieldStreams.inline.hpp"
54 #include "oops/instanceKlass.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/objArrayKlass.hpp"
57 #include "oops/objArrayOop.inline.hpp"
58 #include "oops/oop.inline.hpp"
59 #include "oops/typeArrayOop.inline.hpp"
60 #include "prims/jvmtiExport.hpp"
61 #include "runtime/atomicAccess.hpp"
62 #include "runtime/fieldDescriptor.inline.hpp"
63 #include "runtime/handles.inline.hpp"
64 #include "runtime/init.hpp"
65 #include "runtime/javaCalls.hpp"
66 #include "runtime/javaThread.inline.hpp"
67 #include "runtime/perfData.hpp"
68 #include "runtime/signature.hpp"
69 #include "runtime/vframe.inline.hpp"
70 #include "utilities/checkedCast.hpp"
71 #include "utilities/copy.hpp"
72
73 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
74 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
75 int size = ConstantPool::size(length);
76 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
77 }
78
79 void ConstantPool::copy_fields(const ConstantPool* orig) {
80 // Preserve dynamic constant information from the original pool
81 if (orig->has_dynamic_constant()) {
82 set_has_dynamic_constant();
83 }
84
85 set_major_version(orig->major_version());
86 set_minor_version(orig->minor_version());
87
88 set_source_file_name_index(orig->source_file_name_index());
89 set_generic_signature_index(orig->generic_signature_index());
90 }
91
92 #ifdef ASSERT
93
94 // MetaspaceObj allocation invariant is calloc equivalent memory
95 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
96 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
97 assert(tags != nullptr, "invariant");
98 const int length = tags->length();
99 for (int index = 0; index < length; ++index) {
100 if (JVM_CONSTANT_Invalid != tags->at(index)) {
101 return false;
102 }
103 }
104 return true;
105 }
106
107 #endif
108
109 ConstantPool::ConstantPool() {
110 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
111 }
112
113 ConstantPool::ConstantPool(Array<u1>* tags) :
114 _tags(tags),
115 _length(tags->length()) {
116
117 assert(_tags != nullptr, "invariant");
118 assert(tags->length() == _length, "invariant");
119 assert(tag_array_is_zero_initialized(tags), "invariant");
120 assert(0 == flags(), "invariant");
121 assert(0 == version(), "invariant");
122 assert(nullptr == _pool_holder, "invariant");
123 }
124
125 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
126 if (cache() != nullptr) {
127 MetadataFactory::free_metadata(loader_data, cache());
128 set_cache(nullptr);
129 }
130
131 MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
132 set_resolved_klasses(nullptr);
133
134 MetadataFactory::free_array<jushort>(loader_data, operands());
135 set_operands(nullptr);
136
137 release_C_heap_structures();
138
139 // free tag array
140 MetadataFactory::free_array<u1>(loader_data, tags());
141 set_tags(nullptr);
142 }
143
144 void ConstantPool::release_C_heap_structures() {
145 // walk constant pool and decrement symbol reference counts
146 unreference_symbols();
147 }
148
149 void ConstantPool::metaspace_pointers_do(MetaspaceClosure* it) {
150 log_trace(aot)("Iter(ConstantPool): %p", this);
151
152 it->push(&_tags, MetaspaceClosure::_writable);
153 it->push(&_cache);
154 it->push(&_pool_holder);
155 it->push(&_operands);
156 it->push(&_resolved_klasses, MetaspaceClosure::_writable);
157
158 for (int i = 0; i < length(); i++) {
159 // The only MSO's embedded in the CP entries are Symbols:
160 // JVM_CONSTANT_String
161 // JVM_CONSTANT_Utf8
162 constantTag ctag = tag_at(i);
163 if (ctag.is_string() || ctag.is_utf8()) {
164 it->push(symbol_at_addr(i));
165 }
166 }
167 }
168
169 objArrayOop ConstantPool::resolved_references() const {
170 return _cache->resolved_references();
171 }
172
173 // Called from outside constant pool resolution where a resolved_reference array
174 // may not be present.
175 objArrayOop ConstantPool::resolved_references_or_null() const {
176 if (_cache == nullptr) {
177 return nullptr;
178 } else {
179 return _cache->resolved_references();
180 }
181 }
182
183 oop ConstantPool::resolved_reference_at(int index) const {
184 oop result = resolved_references()->obj_at(index);
185 assert(oopDesc::is_oop_or_null(result), "Must be oop");
186 return result;
187 }
188
189 // Use a CAS for multithreaded access
190 oop ConstantPool::set_resolved_reference_at(int index, oop new_result) {
191 assert(oopDesc::is_oop_or_null(new_result), "Must be oop");
192 return resolved_references()->replace_if_null(index, new_result);
193 }
194
195 // Create resolved_references array and mapping array for original cp indexes
196 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
197 // to map it back for resolving and some unlikely miscellaneous uses.
198 // The objects created by invokedynamic are appended to this list.
199 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
200 const intStack& reference_map,
201 int constant_pool_map_length,
202 TRAPS) {
203 // Initialized the resolved object cache.
204 int map_length = reference_map.length();
205 if (map_length > 0) {
206 // Only need mapping back to constant pool entries. The map isn't used for
207 // invokedynamic resolved_reference entries. For invokedynamic entries,
208 // the constant pool cache index has the mapping back to both the constant
209 // pool and to the resolved reference index.
210 if (constant_pool_map_length > 0) {
211 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
212
213 for (int i = 0; i < constant_pool_map_length; i++) {
214 int x = reference_map.at(i);
215 assert(x == (int)(jushort) x, "klass index is too big");
216 om->at_put(i, (jushort)x);
217 }
218 set_reference_map(om);
219 }
220
221 // Create Java array for holding resolved strings, methodHandles,
222 // methodTypes, invokedynamic and invokehandle appendix objects, etc.
223 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
224 HandleMark hm(THREAD);
225 Handle refs_handle (THREAD, stom); // must handleize.
226 set_resolved_references(loader_data->add_handle(refs_handle));
227
228 // Create a "scratch" copy of the resolved references array to archive
229 if (CDSConfig::is_dumping_heap()) {
230 objArrayOop scratch_references = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
231 HeapShared::add_scratch_resolved_references(this, scratch_references);
232 }
233 }
234 }
235
236 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
237 // A ConstantPool can't possibly have 0xffff valid class entries,
238 // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
239 // entry for the class's name. So at most we will have 0xfffe class entries.
240 // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
241 // UnresolvedKlass entries that are temporarily created during class redefinition.
242 assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
243 assert(resolved_klasses() == nullptr, "sanity");
244 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
245 set_resolved_klasses(rk);
246 }
247
248 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
249 int len = length();
250 int num_klasses = 0;
251 for (int i = 1; i <len; i++) {
252 switch (tag_at(i).value()) {
253 case JVM_CONSTANT_ClassIndex:
254 {
255 const int class_index = klass_index_at(i);
256 unresolved_klass_at_put(i, class_index, num_klasses++);
257 }
258 break;
259 #ifndef PRODUCT
260 case JVM_CONSTANT_Class:
261 case JVM_CONSTANT_UnresolvedClass:
262 case JVM_CONSTANT_UnresolvedClassInError:
263 // All of these should have been reverted back to ClassIndex before calling
264 // this function.
265 ShouldNotReachHere();
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 AtomicAccess::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 template <typename Function>
287 void ConstantPool::iterate_archivable_resolved_references(Function function) {
288 objArrayOop rr = resolved_references();
289 if (rr != nullptr && cache() != nullptr && CDSConfig::is_dumping_method_handles()) {
290 Array<ResolvedIndyEntry>* indy_entries = cache()->resolved_indy_entries();
291 if (indy_entries != nullptr) {
292 for (int i = 0; i < indy_entries->length(); i++) {
293 ResolvedIndyEntry *rie = indy_entries->adr_at(i);
294 if (rie->is_resolved() && AOTConstantPoolResolver::is_resolution_deterministic(this, rie->constant_pool_index())) {
295 int rr_index = rie->resolved_references_index();
296 assert(resolved_reference_at(rr_index) != nullptr, "must exist");
297 function(rr_index);
298
299 // Save the BSM as well (sometimes the JIT looks up the BSM it for replay)
300 int indy_cp_index = rie->constant_pool_index();
301 int bsm_mh_cp_index = bootstrap_method_ref_index_at(indy_cp_index);
302 int bsm_rr_index = cp_to_object_index(bsm_mh_cp_index);
303 assert(resolved_reference_at(bsm_rr_index) != nullptr, "must exist");
304 function(bsm_rr_index);
305 }
306 }
307 }
308
309 Array<ResolvedMethodEntry>* method_entries = cache()->resolved_method_entries();
310 if (method_entries != nullptr) {
311 for (int i = 0; i < method_entries->length(); i++) {
312 ResolvedMethodEntry* rme = method_entries->adr_at(i);
313 if (rme->is_resolved(Bytecodes::_invokehandle) && rme->has_appendix() &&
314 cache()->can_archive_resolved_method(this, rme)) {
315 int rr_index = rme->resolved_references_index();
316 assert(resolved_reference_at(rr_index) != nullptr, "must exist");
317 function(rr_index);
318 }
319 }
320 }
321 }
322 }
323
324 // Returns the _resolved_reference array after removing unarchivable items from it.
325 // Returns null if this class is not supported, or _resolved_reference doesn't exist.
326 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() {
327 if (_cache == nullptr) {
328 return nullptr; // nothing to do
329 }
330
331 InstanceKlass *ik = pool_holder();
332 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
333 // Archiving resolved references for classes from non-builtin loaders
334 // is not yet supported.
335 return nullptr;
336 }
337
338 objArrayOop rr = resolved_references();
339 if (rr != nullptr) {
340 ResourceMark rm;
341 int rr_len = rr->length();
342 GrowableArray<bool> keep_resolved_refs(rr_len, rr_len, false);
343
344 iterate_archivable_resolved_references([&](int rr_index) {
345 keep_resolved_refs.at_put(rr_index, true);
346 });
347
348 objArrayOop scratch_rr = HeapShared::scratch_resolved_references(this);
349 Array<u2>* ref_map = reference_map();
350 int ref_map_len = ref_map == nullptr ? 0 : ref_map->length();
351 for (int i = 0; i < rr_len; i++) {
352 oop obj = rr->obj_at(i);
353 scratch_rr->obj_at_put(i, nullptr);
354 if (obj != nullptr) {
355 if (i < ref_map_len) {
356 int index = object_to_cp_index(i);
357 if (tag_at(index).is_string()) {
358 assert(java_lang_String::is_instance(obj), "must be");
359 if (!HeapShared::is_string_too_large_to_archive(obj)) {
360 scratch_rr->obj_at_put(i, obj);
361 }
362 continue;
363 }
364 }
365
366 if (keep_resolved_refs.at(i)) {
367 scratch_rr->obj_at_put(i, obj);
368 }
369 }
370 }
371 return scratch_rr;
372 }
373 return rr;
374 }
375 #endif
376
377 #if INCLUDE_CDS
378 // CDS support. Create a new resolved_references array.
379 void ConstantPool::restore_unshareable_info(TRAPS) {
380 if (!_pool_holder->is_linked() && !_pool_holder->is_rewritten()) {
381 return;
382 }
383 assert(is_constantPool(), "ensure C++ vtable is restored");
384 assert(on_stack(), "should always be set for constant pools in AOT cache");
385 assert(in_aot_cache(), "should always be set for constant pools in AOT cache");
386 if (is_for_method_handle_intrinsic()) {
387 // See the same check in remove_unshareable_info() below.
388 assert(cache() == nullptr, "must not have cpCache");
389 return;
390 }
391 assert(_cache != nullptr, "constant pool _cache should not be null");
392
393 // Only create the new resolved references array if it hasn't been attempted before
394 if (resolved_references() != nullptr) return;
395
396 if (vmClasses::Object_klass_is_loaded()) {
397 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
398 #if INCLUDE_CDS_JAVA_HEAP
399 if (HeapShared::is_archived_heap_in_use() &&
400 _cache->archived_references() != nullptr) {
401 oop archived = _cache->archived_references();
402 // Create handle for the archived resolved reference array object
403 HandleMark hm(THREAD);
404 Handle refs_handle(THREAD, archived);
405 set_resolved_references(loader_data->add_handle(refs_handle));
406 _cache->clear_archived_references();
407 } else
408 #endif
409 {
410 // No mapped archived resolved reference array
411 // Recreate the object array and add to ClassLoaderData.
412 int map_length = resolved_reference_length();
413 if (map_length > 0) {
414 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK);
415 HandleMark hm(THREAD);
416 Handle refs_handle(THREAD, stom); // must handleize.
417 set_resolved_references(loader_data->add_handle(refs_handle));
418 }
419 }
420 }
421
422 if (CDSConfig::is_dumping_final_static_archive() && CDSConfig::is_dumping_heap() && resolved_references() != nullptr) {
423 objArrayOop scratch_references = oopFactory::new_objArray(vmClasses::Object_klass(), resolved_references()->length(), CHECK);
424 HeapShared::add_scratch_resolved_references(this, scratch_references);
425 }
426 }
427
428 void ConstantPool::remove_unshareable_info() {
429 // ConstantPools in AOT cache are in the RO region, so the _flags cannot be modified.
430 // The _on_stack flag is used to prevent ConstantPools from deallocation during
431 // class redefinition. Since such ConstantPools cannot be deallocated anyway,
432 // we always set _on_stack to true to avoid having to change _flags during runtime.
433 _flags |= (_on_stack | _in_aot_cache);
434
435 if (is_for_method_handle_intrinsic()) {
436 // This CP was created by Method::make_method_handle_intrinsic() and has nothing
437 // that need to be removed/restored. It has no cpCache since the intrinsic methods
438 // don't have any bytecodes.
439 assert(cache() == nullptr, "must not have cpCache");
440 return;
441 }
442
443 bool update_resolved_reference = true;
444 if (CDSConfig::is_dumping_final_static_archive()) {
445 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
446 InstanceKlass* src_holder = src_cp->pool_holder();
447 if (src_holder->defined_by_other_loaders()) {
448 // Unregistered classes are not loaded in the AOT assembly phase. The resolved reference length
449 // is already saved during the training run.
450 precond(!src_holder->is_loaded());
451 precond(resolved_reference_length() >= 0);
452 precond(resolved_references() == nullptr);
453 update_resolved_reference = false;
454 }
455 }
456
457 // resolved_references(): remember its length. If it cannot be restored
458 // from the archived heap objects at run time, we need to dynamically allocate it.
459 if (update_resolved_reference && cache() != nullptr) {
460 set_resolved_reference_length(
461 resolved_references() != nullptr ? resolved_references()->length() : 0);
462 set_resolved_references(OopHandle());
463 }
464 remove_unshareable_entries();
465 }
466
467 static const char* get_type(Klass* k) {
468 const char* type;
469 Klass* src_k;
470 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
471 src_k = ArchiveBuilder::current()->get_source_addr(k);
472 } else {
473 src_k = k;
474 }
475
476 if (src_k->is_objArray_klass()) {
477 src_k = ObjArrayKlass::cast(src_k)->bottom_klass();
478 assert(!src_k->is_objArray_klass(), "sanity");
479 }
480
481 if (src_k->is_typeArray_klass()) {
482 type = "prim";
483 } else {
484 InstanceKlass* src_ik = InstanceKlass::cast(src_k);
485 if (CDSConfig::is_dumping_final_static_archive() && src_ik->class_loader_data() == nullptr) {
486 return "unreg";
487 }
488 if (src_ik->defined_by_boot_loader()) {
489 return "boot";
490 } else if (src_ik->defined_by_platform_loader()) {
491 return "plat";
492 } else if (src_ik->defined_by_app_loader()) {
493 return "app";
494 } else {
495 return "unreg";
496 }
497 }
498
499 return type;
500 }
501
502 void ConstantPool::remove_unshareable_entries() {
503 ResourceMark rm;
504 log_info(aot, resolve)("Archiving CP entries for %s", pool_holder()->name()->as_C_string());
505 for (int cp_index = 1; cp_index < length(); cp_index++) { // cp_index 0 is unused
506 int cp_tag = tag_at(cp_index).value();
507 switch (cp_tag) {
508 case JVM_CONSTANT_UnresolvedClass:
509 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false, false);
510 break;
511 case JVM_CONSTANT_UnresolvedClassInError:
512 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
513 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false, true);
514 break;
515 case JVM_CONSTANT_MethodHandleInError:
516 tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
517 break;
518 case JVM_CONSTANT_MethodTypeInError:
519 tag_at_put(cp_index, JVM_CONSTANT_MethodType);
520 break;
521 case JVM_CONSTANT_DynamicInError:
522 tag_at_put(cp_index, JVM_CONSTANT_Dynamic);
523 break;
524 case JVM_CONSTANT_Class:
525 remove_resolved_klass_if_non_deterministic(cp_index);
526 break;
527 default:
528 break;
529 }
530 }
531
532 if (cache() != nullptr) {
533 // cache() is null if this class is not yet linked.
534 cache()->remove_unshareable_info();
535 }
536 }
537
538 void ConstantPool::remove_resolved_klass_if_non_deterministic(int cp_index) {
539 assert(ArchiveBuilder::current()->is_in_buffer_space(this), "must be");
540 assert(tag_at(cp_index).is_klass(), "must be resolved");
541
542 bool can_archive;
543 Klass* k = nullptr;
544
545 if (CDSConfig::is_dumping_preimage_static_archive()) {
546 can_archive = false;
547 } else {
548 k = resolved_klass_at(cp_index);
549 if (k == nullptr) {
550 // We'd come here if the referenced class has been excluded via
551 // SystemDictionaryShared::is_excluded_class(). As a result, ArchiveBuilder
552 // has cleared the resolved_klasses()->at(...) pointer to null. Thus, we
553 // need to revert the tag to JVM_CONSTANT_UnresolvedClass.
554 can_archive = false;
555 } else {
556 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
557 can_archive = AOTConstantPoolResolver::is_resolution_deterministic(src_cp, cp_index);
558 }
559 }
560
561 if (!can_archive) {
562 int resolved_klass_index = klass_slot_at(cp_index).resolved_klass_index();
563 // This might be at a safepoint but do this in the right order.
564 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
565 resolved_klasses()->at_put(resolved_klass_index, nullptr);
566 }
567
568 LogStreamHandle(Trace, aot, resolve) log;
569 if (log.is_enabled()) {
570 ResourceMark rm;
571 log.print("%s klass CP entry [%3d]: %s %s",
572 (can_archive ? "archived" : "reverted"),
573 cp_index, pool_holder()->name()->as_C_string(), get_type(pool_holder()));
574 if (can_archive) {
575 log.print(" => %s %s%s", k->name()->as_C_string(), get_type(k),
576 (!k->is_instance_klass() || pool_holder()->is_subtype_of(k)) ? "" : " (not supertype)");
577 } else {
578 Symbol* name = klass_name_at(cp_index);
579 log.print(" => %s", name->as_C_string());
580 }
581 }
582
583 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(can_archive, /*reverted=*/!can_archive);
584 }
585 #endif // INCLUDE_CDS
586
587 int ConstantPool::cp_to_object_index(int cp_index) {
588 // this is harder don't do this so much.
589 int i = reference_map()->find(checked_cast<u2>(cp_index));
590 // We might not find the index for jsr292 call.
591 return (i < 0) ? _no_index_sentinel : i;
592 }
593
594 void ConstantPool::string_at_put(int obj_index, oop str) {
595 oop result = set_resolved_reference_at(obj_index, str);
596 assert(result == nullptr || result == str, "Only set once or to the same string.");
597 }
598
599 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
600 ResourceMark rm;
601 int line_number = -1;
602 const char * source_file = nullptr;
603 if (JavaThread::current()->has_last_Java_frame()) {
604 // try to identify the method which called this function.
605 vframeStream vfst(JavaThread::current());
606 if (!vfst.at_end()) {
607 line_number = vfst.method()->line_number_from_bci(vfst.bci());
608 Symbol* s = vfst.method()->method_holder()->source_file_name();
609 if (s != nullptr) {
610 source_file = s->as_C_string();
611 }
612 }
613 }
614 if (k != this_cp->pool_holder()) {
615 // only print something if the classes are different
616 if (source_file != nullptr) {
617 log_debug(class, resolve)("%s %s %s:%d",
618 this_cp->pool_holder()->external_name(),
619 k->external_name(), source_file, line_number);
620 } else {
621 log_debug(class, resolve)("%s %s",
622 this_cp->pool_holder()->external_name(),
623 k->external_name());
624 }
625 }
626 }
627
628 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int cp_index,
629 TRAPS) {
630 JavaThread* javaThread = THREAD;
631
632 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
633 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
634 // the entry and tag is not updated atomically.
635 CPKlassSlot kslot = this_cp->klass_slot_at(cp_index);
636 int resolved_klass_index = kslot.resolved_klass_index();
637 int name_index = kslot.name_index();
638 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
639
640 // The tag must be JVM_CONSTANT_Class in order to read the correct value from
641 // the unresolved_klasses() array.
642 if (this_cp->tag_at(cp_index).is_klass()) {
643 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
644 assert(klass != nullptr, "must be resolved");
645 return klass;
646 }
647
648 // This tag doesn't change back to unresolved class unless at a safepoint.
649 if (this_cp->tag_at(cp_index).is_unresolved_klass_in_error()) {
650 // The original attempt to resolve this constant pool entry failed so find the
651 // class of the original error and throw another error of the same class
652 // (JVMS 5.4.3).
653 // If there is a detail message, pass that detail message to the error.
654 // The JVMS does not strictly require us to duplicate the same detail message,
655 // or any internal exception fields such as cause or stacktrace. But since the
656 // detail message is often a class name or other literal string, we will repeat it
657 // if we can find it in the symbol table.
658 throw_resolution_error(this_cp, cp_index, CHECK_NULL);
659 ShouldNotReachHere();
660 }
661
662 HandleMark hm(THREAD);
663 Handle mirror_handle;
664 Symbol* name = this_cp->symbol_at(name_index);
665 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
666
667 Klass* k;
668 {
669 // Turn off the single stepping while doing class resolution
670 JvmtiHideSingleStepping jhss(javaThread);
671 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD);
672 } // JvmtiHideSingleStepping jhss(javaThread);
673
674 if (!HAS_PENDING_EXCEPTION) {
675 // preserve the resolved klass from unloading
676 mirror_handle = Handle(THREAD, k->java_mirror());
677 // Do access check for klasses
678 verify_constant_pool_resolve(this_cp, k, THREAD);
679 }
680
681 // Failed to resolve class. We must record the errors so that subsequent attempts
682 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
683 if (HAS_PENDING_EXCEPTION) {
684 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
685 // If CHECK_NULL above doesn't return the exception, that means that
686 // some other thread has beaten us and has resolved the class.
687 // To preserve old behavior, we return the resolved class.
688 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
689 assert(klass != nullptr, "must be resolved if exception was cleared");
690 return klass;
691 }
692
693 // logging for class+resolve.
694 if (log_is_enabled(Debug, class, resolve)){
695 trace_class_resolution(this_cp, k);
696 }
697
698 // The interpreter assumes when the tag is stored, the klass is resolved
699 // and the Klass* stored in _resolved_klasses is non-null, so we need
700 // hardware store ordering here.
701 // We also need to CAS to not overwrite an error from a racing thread.
702 Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
703 AtomicAccess::release_store(adr, k);
704
705 jbyte old_tag = AtomicAccess::cmpxchg((jbyte*)this_cp->tag_addr_at(cp_index),
706 (jbyte)JVM_CONSTANT_UnresolvedClass,
707 (jbyte)JVM_CONSTANT_Class);
708
709 // We need to recheck exceptions from racing thread and return the same.
710 if (old_tag == JVM_CONSTANT_UnresolvedClassInError) {
711 // Remove klass.
712 AtomicAccess::store(adr, (Klass*)nullptr);
713 throw_resolution_error(this_cp, cp_index, CHECK_NULL);
714 }
715
716 return k;
717 }
718
719
720 // Does not update ConstantPool* - to avoid any exception throwing. Used
721 // by compiler and exception handling. Also used to avoid classloads for
722 // instanceof operations. Returns null if the class has not been loaded or
723 // if the verification of constant pool failed
724 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
725 CPKlassSlot kslot = this_cp->klass_slot_at(which);
726 int resolved_klass_index = kslot.resolved_klass_index();
727 int name_index = kslot.name_index();
728 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
729
730 if (this_cp->tag_at(which).is_klass()) {
731 Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
732 assert(k != nullptr, "must be resolved");
733 return k;
734 } else if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
735 return nullptr;
736 } else {
737 Thread* current = Thread::current();
738 HandleMark hm(current);
739 Symbol* name = this_cp->symbol_at(name_index);
740 oop loader = this_cp->pool_holder()->class_loader();
741 Handle h_loader (current, loader);
742 Klass* k = SystemDictionary::find_instance_klass(current, name, h_loader);
743
744 // Avoid constant pool verification at a safepoint, as it takes the Module_lock.
745 if (k != nullptr && current->is_Java_thread()) {
746 // Make sure that resolving is legal
747 JavaThread* THREAD = JavaThread::cast(current); // For exception macros.
748 ExceptionMark em(THREAD);
749 // return null if verification fails
750 verify_constant_pool_resolve(this_cp, k, THREAD);
751 if (HAS_PENDING_EXCEPTION) {
752 CLEAR_PENDING_EXCEPTION;
753 return nullptr;
754 }
755 return k;
756 } else {
757 return k;
758 }
759 }
760 }
761
762 Method* ConstantPool::method_at_if_loaded(const constantPoolHandle& cpool,
763 int which) {
764 if (cpool->cache() == nullptr) return nullptr; // nothing to load yet
765 if (!(which >= 0 && which < cpool->resolved_method_entries_length())) {
766 // FIXME: should be an assert
767 log_debug(class, resolve)("bad operand %d in:", which); cpool->print();
768 return nullptr;
769 }
770 return cpool->cache()->method_if_resolved(which);
771 }
772
773
774 bool ConstantPool::has_appendix_at_if_loaded(const constantPoolHandle& cpool, int which, Bytecodes::Code code) {
775 if (cpool->cache() == nullptr) return false; // nothing to load yet
776 if (code == Bytecodes::_invokedynamic) {
777 return cpool->resolved_indy_entry_at(which)->has_appendix();
778 } else {
779 return cpool->resolved_method_entry_at(which)->has_appendix();
780 }
781 }
782
783 oop ConstantPool::appendix_at_if_loaded(const constantPoolHandle& cpool, int which, Bytecodes::Code code) {
784 if (cpool->cache() == nullptr) return nullptr; // nothing to load yet
785 if (code == Bytecodes::_invokedynamic) {
786 return cpool->resolved_reference_from_indy(which);
787 } else {
788 return cpool->cache()->appendix_if_resolved(which);
789 }
790 }
791
792
793 bool ConstantPool::has_local_signature_at_if_loaded(const constantPoolHandle& cpool, int which, Bytecodes::Code code) {
794 if (cpool->cache() == nullptr) return false; // nothing to load yet
795 if (code == Bytecodes::_invokedynamic) {
796 return cpool->resolved_indy_entry_at(which)->has_local_signature();
797 } else {
798 return cpool->resolved_method_entry_at(which)->has_local_signature();
799 }
800 }
801
802 // Translate index, which could be CPCache index or Indy index, to a constant pool index
803 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
804 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
805 switch(code) {
806 case Bytecodes::_invokedynamic:
807 return invokedynamic_bootstrap_ref_index_at(index);
808 case Bytecodes::_getfield:
809 case Bytecodes::_getstatic:
810 case Bytecodes::_putfield:
811 case Bytecodes::_putstatic:
812 return resolved_field_entry_at(index)->constant_pool_index();
813 case Bytecodes::_invokeinterface:
814 case Bytecodes::_invokehandle:
815 case Bytecodes::_invokespecial:
816 case Bytecodes::_invokestatic:
817 case Bytecodes::_invokevirtual:
818 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
819 return resolved_method_entry_at(index)->constant_pool_index();
820 default:
821 fatal("Unexpected bytecode: %s", Bytecodes::name(code));
822 }
823 }
824
825 bool ConstantPool::is_resolved(int index, Bytecodes::Code code) {
826 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
827 switch(code) {
828 case Bytecodes::_invokedynamic:
829 return resolved_indy_entry_at(index)->is_resolved();
830
831 case Bytecodes::_getfield:
832 case Bytecodes::_getstatic:
833 case Bytecodes::_putfield:
834 case Bytecodes::_putstatic:
835 return resolved_field_entry_at(index)->is_resolved(code);
836
837 case Bytecodes::_invokeinterface:
838 case Bytecodes::_invokehandle:
839 case Bytecodes::_invokespecial:
840 case Bytecodes::_invokestatic:
841 case Bytecodes::_invokevirtual:
842 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this
843 return resolved_method_entry_at(index)->is_resolved(code);
844
845 default:
846 fatal("Unexpected bytecode: %s", Bytecodes::name(code));
847 }
848 }
849
850 u2 ConstantPool::uncached_name_and_type_ref_index_at(int cp_index) {
851 if (tag_at(cp_index).has_bootstrap()) {
852 u2 pool_index = bootstrap_name_and_type_ref_index_at(cp_index);
853 assert(tag_at(pool_index).is_name_and_type(), "");
854 return pool_index;
855 }
856 assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool");
857 assert(!tag_at(cp_index).has_bootstrap(), "Must be handled above");
858 jint ref_index = *int_at_addr(cp_index);
859 return extract_high_short_from_int(ref_index);
860 }
861
862 u2 ConstantPool::name_and_type_ref_index_at(int index, Bytecodes::Code code) {
863 return uncached_name_and_type_ref_index_at(to_cp_index(index, code));
864 }
865
866 constantTag ConstantPool::tag_ref_at(int which, Bytecodes::Code code) {
867 // which may be either a Constant Pool index or a rewritten index
868 int pool_index = which;
869 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
870 pool_index = to_cp_index(which, code);
871 return tag_at(pool_index);
872 }
873
874 u2 ConstantPool::uncached_klass_ref_index_at(int cp_index) {
875 assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool");
876 jint ref_index = *int_at_addr(cp_index);
877 return extract_low_short_from_int(ref_index);
878 }
879
880 u2 ConstantPool::klass_ref_index_at(int index, Bytecodes::Code code) {
881 assert(code != Bytecodes::_invokedynamic,
882 "an invokedynamic instruction does not have a klass");
883 return uncached_klass_ref_index_at(to_cp_index(index, code));
884 }
885
886 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) {
887 if (!(k->is_instance_klass() || k->is_objArray_klass())) {
888 return; // short cut, typeArray klass is always accessible
889 }
890 Klass* holder = this_cp->pool_holder();
891 LinkResolver::check_klass_accessibility(holder, k, CHECK);
892 }
893
894
895 u2 ConstantPool::name_ref_index_at(int cp_index) {
896 jint ref_index = name_and_type_at(cp_index);
897 return extract_low_short_from_int(ref_index);
898 }
899
900
901 u2 ConstantPool::signature_ref_index_at(int cp_index) {
902 jint ref_index = name_and_type_at(cp_index);
903 return extract_high_short_from_int(ref_index);
904 }
905
906
907 Klass* ConstantPool::klass_ref_at(int which, Bytecodes::Code code, TRAPS) {
908 return klass_at(klass_ref_index_at(which, code), THREAD);
909 }
910
911 Symbol* ConstantPool::klass_name_at(int cp_index) const {
912 return symbol_at(klass_slot_at(cp_index).name_index());
913 }
914
915 Symbol* ConstantPool::klass_ref_at_noresolve(int which, Bytecodes::Code code) {
916 jint ref_index = klass_ref_index_at(which, code);
917 return klass_at_noresolve(ref_index);
918 }
919
920 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int cp_index) {
921 jint ref_index = uncached_klass_ref_index_at(cp_index);
922 return klass_at_noresolve(ref_index);
923 }
924
925 char* ConstantPool::string_at_noresolve(int cp_index) {
926 return unresolved_string_at(cp_index)->as_C_string();
927 }
928
929 BasicType ConstantPool::basic_type_for_signature_at(int cp_index) const {
930 return Signature::basic_type(symbol_at(cp_index));
931 }
932
933
934 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) {
935 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused
936 if (this_cp->tag_at(index).is_string()) {
937 this_cp->string_at(index, CHECK);
938 }
939 }
940 }
941
942 static const char* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
943 // Note: caller needs ResourceMark
944
945 // Dig out the detailed message to reuse if possible
946 const char* msg = java_lang_Throwable::message_as_utf8(pending_exception);
947 if (msg != nullptr) {
948 return msg;
949 }
950
951 Symbol* message = nullptr;
952 // Return specific message for the tag
953 switch (tag.value()) {
954 case JVM_CONSTANT_UnresolvedClass:
955 // return the class name in the error message
956 message = this_cp->klass_name_at(which);
957 break;
958 case JVM_CONSTANT_MethodHandle:
959 // return the method handle name in the error message
960 message = this_cp->method_handle_name_ref_at(which);
961 break;
962 case JVM_CONSTANT_MethodType:
963 // return the method type signature in the error message
964 message = this_cp->method_type_signature_at(which);
965 break;
966 case JVM_CONSTANT_Dynamic:
967 // return the name of the condy in the error message
968 message = this_cp->uncached_name_ref_at(which);
969 break;
970 default:
971 ShouldNotReachHere();
972 }
973
974 return message != nullptr ? message->as_C_string() : nullptr;
975 }
976
977 static void add_resolution_error(JavaThread* current, const constantPoolHandle& this_cp, int which,
978 constantTag tag, oop pending_exception) {
979
980 ResourceMark rm(current);
981 Symbol* error = pending_exception->klass()->name();
982 oop cause = java_lang_Throwable::cause(pending_exception);
983
984 // Also dig out the exception cause, if present.
985 Symbol* cause_sym = nullptr;
986 const char* cause_msg = nullptr;
987 if (cause != nullptr && cause != pending_exception) {
988 cause_sym = cause->klass()->name();
989 cause_msg = java_lang_Throwable::message_as_utf8(cause);
990 }
991
992 const char* message = exception_message(this_cp, which, tag, pending_exception);
993 SystemDictionary::add_resolution_error(this_cp, which, error, message, cause_sym, cause_msg);
994 }
995
996
997 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
998 ResourceMark rm(THREAD);
999 const char* message = nullptr;
1000 Symbol* cause = nullptr;
1001 const char* cause_msg = nullptr;
1002 Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message, &cause, &cause_msg);
1003 assert(error != nullptr, "checking");
1004
1005 CLEAR_PENDING_EXCEPTION;
1006 if (message != nullptr) {
1007 if (cause != nullptr) {
1008 Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg);
1009 THROW_MSG_CAUSE(error, message, h_cause);
1010 } else {
1011 THROW_MSG(error, message);
1012 }
1013 } else {
1014 if (cause != nullptr) {
1015 Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg);
1016 THROW_CAUSE(error, h_cause);
1017 } else {
1018 THROW(error);
1019 }
1020 }
1021 }
1022
1023 // If resolution for Class, Dynamic constant, MethodHandle or MethodType fails, save the
1024 // exception in the resolution error table, so that the same exception is thrown again.
1025 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int cp_index,
1026 constantTag tag, TRAPS) {
1027
1028 int error_tag = tag.error_value();
1029
1030 if (!PENDING_EXCEPTION->
1031 is_a(vmClasses::LinkageError_klass())) {
1032 // Just throw the exception and don't prevent these classes from
1033 // being loaded due to virtual machine errors like StackOverflow
1034 // and OutOfMemoryError, etc, or if the thread was hit by stop()
1035 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
1036 } else if (this_cp->tag_at(cp_index).value() != error_tag) {
1037 add_resolution_error(THREAD, this_cp, cp_index, tag, PENDING_EXCEPTION);
1038 // CAS in the tag. If a thread beat us to registering this error that's fine.
1039 // If another thread resolved the reference, this is a race condition. This
1040 // thread may have had a security manager or something temporary.
1041 // This doesn't deterministically get an error. So why do we save this?
1042 // We save this because jvmti can add classes to the bootclass path after
1043 // this error, so it needs to get the same error if the error is first.
1044 jbyte old_tag = AtomicAccess::cmpxchg((jbyte*)this_cp->tag_addr_at(cp_index),
1045 (jbyte)tag.value(),
1046 (jbyte)error_tag);
1047 if (old_tag != error_tag && old_tag != tag.value()) {
1048 // MethodHandles and MethodType doesn't change to resolved version.
1049 assert(this_cp->tag_at(cp_index).is_klass(), "Wrong tag value");
1050 // Forget the exception and use the resolved class.
1051 CLEAR_PENDING_EXCEPTION;
1052 }
1053 } else {
1054 // some other thread put this in error state
1055 throw_resolution_error(this_cp, cp_index, CHECK);
1056 }
1057 }
1058
1059 constantTag ConstantPool::constant_tag_at(int cp_index) {
1060 constantTag tag = tag_at(cp_index);
1061 if (tag.is_dynamic_constant()) {
1062 BasicType bt = basic_type_for_constant_at(cp_index);
1063 return constantTag(constantTag::type2tag(bt));
1064 }
1065 return tag;
1066 }
1067
1068 BasicType ConstantPool::basic_type_for_constant_at(int cp_index) {
1069 constantTag tag = tag_at(cp_index);
1070 if (tag.is_dynamic_constant() ||
1071 tag.is_dynamic_constant_in_error()) {
1072 // have to look at the signature for this one
1073 Symbol* constant_type = uncached_signature_ref_at(cp_index);
1074 return Signature::basic_type(constant_type);
1075 }
1076 return tag.basic_type();
1077 }
1078
1079 // Called to resolve constants in the constant pool and return an oop.
1080 // Some constant pool entries cache their resolved oop. This is also
1081 // called to create oops from constants to use in arguments for invokedynamic
1082 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp,
1083 int cp_index, int cache_index,
1084 bool* status_return, TRAPS) {
1085 oop result_oop = nullptr;
1086
1087 if (cache_index == _possible_index_sentinel) {
1088 // It is possible that this constant is one which is cached in the objects.
1089 // We'll do a linear search. This should be OK because this usage is rare.
1090 // FIXME: If bootstrap specifiers stress this code, consider putting in
1091 // a reverse index. Binary search over a short array should do it.
1092 assert(cp_index > 0, "valid constant pool index");
1093 cache_index = this_cp->cp_to_object_index(cp_index);
1094 }
1095 assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
1096 assert(cp_index == _no_index_sentinel || cp_index >= 0, "");
1097
1098 if (cache_index >= 0) {
1099 result_oop = this_cp->resolved_reference_at(cache_index);
1100 if (result_oop != nullptr) {
1101 if (result_oop == Universe::the_null_sentinel()) {
1102 DEBUG_ONLY(int temp_index = (cp_index >= 0 ? cp_index : this_cp->object_to_cp_index(cache_index)));
1103 assert(this_cp->tag_at(temp_index).is_dynamic_constant(), "only condy uses the null sentinel");
1104 result_oop = nullptr;
1105 }
1106 if (status_return != nullptr) (*status_return) = true;
1107 return result_oop;
1108 // That was easy...
1109 }
1110 cp_index = this_cp->object_to_cp_index(cache_index);
1111 }
1112
1113 jvalue prim_value; // temp used only in a few cases below
1114
1115 constantTag tag = this_cp->tag_at(cp_index);
1116
1117 if (status_return != nullptr) {
1118 // don't trigger resolution if the constant might need it
1119 switch (tag.value()) {
1120 case JVM_CONSTANT_Class:
1121 assert(this_cp->resolved_klass_at(cp_index) != nullptr, "must be resolved");
1122 break;
1123 case JVM_CONSTANT_String:
1124 case JVM_CONSTANT_Integer:
1125 case JVM_CONSTANT_Float:
1126 case JVM_CONSTANT_Long:
1127 case JVM_CONSTANT_Double:
1128 // these guys trigger OOM at worst
1129 break;
1130 default:
1131 (*status_return) = false;
1132 return nullptr;
1133 }
1134 // from now on there is either success or an OOME
1135 (*status_return) = true;
1136 }
1137
1138 switch (tag.value()) {
1139
1140 case JVM_CONSTANT_UnresolvedClass:
1141 case JVM_CONSTANT_Class:
1142 {
1143 assert(cache_index == _no_index_sentinel, "should not have been set");
1144 Klass* resolved = klass_at_impl(this_cp, cp_index, CHECK_NULL);
1145 // ldc wants the java mirror.
1146 result_oop = resolved->java_mirror();
1147 break;
1148 }
1149
1150 case JVM_CONSTANT_Dynamic:
1151 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokedynamic_time(),
1152 ClassLoader::perf_resolve_invokedynamic_count());
1153
1154 // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1155 BootstrapInfo bootstrap_specifier(this_cp, cp_index);
1156
1157 // The initial step in resolving an unresolved symbolic reference to a
1158 // dynamically-computed constant is to resolve the symbolic reference to a
1159 // method handle which will be the bootstrap method for the dynamically-computed
1160 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1161 // method fails, then a MethodHandleInError is stored at the corresponding
1162 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1163 // set a DynamicConstantInError here since any subsequent use of this
1164 // bootstrap method will encounter the resolution of MethodHandleInError.
1165 // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1166 // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1167 // for the bootstrap_specifier created above.
1168 SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1169 Exceptions::wrap_dynamic_exception(/* is_indy */ false, THREAD);
1170 if (HAS_PENDING_EXCEPTION) {
1171 // Resolution failure of the dynamically-computed constant, save_and_throw_exception
1172 // will check for a LinkageError and store a DynamicConstantInError.
1173 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1174 }
1175 result_oop = bootstrap_specifier.resolved_value()();
1176 BasicType type = Signature::basic_type(bootstrap_specifier.signature());
1177 if (!is_reference_type(type)) {
1178 // Make sure the primitive value is properly boxed.
1179 // This is a JDK responsibility.
1180 const char* fail = nullptr;
1181 if (result_oop == nullptr) {
1182 fail = "null result instead of box";
1183 } else if (!is_java_primitive(type)) {
1184 // FIXME: support value types via unboxing
1185 fail = "can only handle references and primitives";
1186 } else if (!java_lang_boxing_object::is_instance(result_oop, type)) {
1187 fail = "primitive is not properly boxed";
1188 }
1189 if (fail != nullptr) {
1190 // Since this exception is not a LinkageError, throw exception
1191 // but do not save a DynamicInError resolution result.
1192 // See section 5.4.3 of the VM spec.
1193 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail);
1194 }
1195 }
1196
1197 LogTarget(Debug, methodhandles, condy) lt_condy;
1198 if (lt_condy.is_enabled()) {
1199 LogStream ls(lt_condy);
1200 bootstrap_specifier.print_msg_on(&ls, "resolve_constant_at_impl");
1201 }
1202 break;
1203 }
1204
1205 case JVM_CONSTANT_String:
1206 assert(cache_index != _no_index_sentinel, "should have been set");
1207 result_oop = string_at_impl(this_cp, cp_index, cache_index, CHECK_NULL);
1208 break;
1209
1210 case JVM_CONSTANT_MethodHandle:
1211 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_handle_time(),
1212 ClassLoader::perf_resolve_method_handle_count());
1213
1214 int ref_kind = this_cp->method_handle_ref_kind_at(cp_index);
1215 int callee_index = this_cp->method_handle_klass_index_at(cp_index);
1216 Symbol* name = this_cp->method_handle_name_ref_at(cp_index);
1217 Symbol* signature = this_cp->method_handle_signature_ref_at(cp_index);
1218 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(cp_index));
1219 { ResourceMark rm(THREAD);
1220 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
1221 ref_kind, cp_index, this_cp->method_handle_index_at(cp_index),
1222 callee_index, name->as_C_string(), signature->as_C_string());
1223 }
1224
1225 Klass* callee = klass_at_impl(this_cp, callee_index, THREAD);
1226 if (HAS_PENDING_EXCEPTION) {
1227 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1228 }
1229
1230 // Check constant pool method consistency
1231 if ((callee->is_interface() && m_tag.is_method()) ||
1232 (!callee->is_interface() && m_tag.is_interface_method())) {
1233 ResourceMark rm(THREAD);
1234 stringStream ss;
1235 ss.print("Inconsistent constant pool data in classfile for class %s. "
1236 "Method '", callee->name()->as_C_string());
1237 signature->print_as_signature_external_return_type(&ss);
1238 ss.print(" %s(", name->as_C_string());
1239 signature->print_as_signature_external_parameters(&ss);
1240 ss.print(")' at index %d is %s and should be %s",
1241 cp_index,
1242 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
1243 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
1244 // Names are all known to be < 64k so we know this formatted message is not excessively large.
1245 Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IncompatibleClassChangeError(), "%s", ss.as_string());
1246 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1247 }
1248
1249 Klass* klass = this_cp->pool_holder();
1250 HandleMark hm(THREAD);
1251 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
1252 callee, name, signature,
1253 THREAD);
1254 if (HAS_PENDING_EXCEPTION) {
1255 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1256 }
1257 result_oop = value();
1258 break;
1259 }
1260
1261 case JVM_CONSTANT_MethodType:
1262 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_type_time(),
1263 ClassLoader::perf_resolve_method_type_count());
1264
1265 Symbol* signature = this_cp->method_type_signature_at(cp_index);
1266 { ResourceMark rm(THREAD);
1267 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
1268 cp_index, this_cp->method_type_index_at(cp_index),
1269 signature->as_C_string());
1270 }
1271 Klass* klass = this_cp->pool_holder();
1272 HandleMark hm(THREAD);
1273 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
1274 result_oop = value();
1275 if (HAS_PENDING_EXCEPTION) {
1276 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL);
1277 }
1278 break;
1279 }
1280
1281 case JVM_CONSTANT_Integer:
1282 assert(cache_index == _no_index_sentinel, "should not have been set");
1283 prim_value.i = this_cp->int_at(cp_index);
1284 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
1285 break;
1286
1287 case JVM_CONSTANT_Float:
1288 assert(cache_index == _no_index_sentinel, "should not have been set");
1289 prim_value.f = this_cp->float_at(cp_index);
1290 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
1291 break;
1292
1293 case JVM_CONSTANT_Long:
1294 assert(cache_index == _no_index_sentinel, "should not have been set");
1295 prim_value.j = this_cp->long_at(cp_index);
1296 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
1297 break;
1298
1299 case JVM_CONSTANT_Double:
1300 assert(cache_index == _no_index_sentinel, "should not have been set");
1301 prim_value.d = this_cp->double_at(cp_index);
1302 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
1303 break;
1304
1305 case JVM_CONSTANT_UnresolvedClassInError:
1306 case JVM_CONSTANT_DynamicInError:
1307 case JVM_CONSTANT_MethodHandleInError:
1308 case JVM_CONSTANT_MethodTypeInError:
1309 throw_resolution_error(this_cp, cp_index, CHECK_NULL);
1310 break;
1311
1312 default:
1313 fatal("unexpected constant tag at CP %p[%d/%d] = %d", this_cp(), cp_index, cache_index, tag.value());
1314 break;
1315 }
1316
1317 if (cache_index >= 0) {
1318 // Benign race condition: resolved_references may already be filled in.
1319 // The important thing here is that all threads pick up the same result.
1320 // It doesn't matter which racing thread wins, as long as only one
1321 // result is used by all threads, and all future queries.
1322 oop new_result = (result_oop == nullptr ? Universe::the_null_sentinel() : result_oop);
1323 oop old_result = this_cp->set_resolved_reference_at(cache_index, new_result);
1324 if (old_result == nullptr) {
1325 return result_oop; // was installed
1326 } else {
1327 // Return the winning thread's result. This can be different than
1328 // the result here for MethodHandles.
1329 if (old_result == Universe::the_null_sentinel())
1330 old_result = nullptr;
1331 return old_result;
1332 }
1333 } else {
1334 assert(result_oop != Universe::the_null_sentinel(), "");
1335 return result_oop;
1336 }
1337 }
1338
1339 oop ConstantPool::uncached_string_at(int cp_index, TRAPS) {
1340 Symbol* sym = unresolved_string_at(cp_index);
1341 oop str = StringTable::intern(sym, CHECK_(nullptr));
1342 assert(java_lang_String::is_instance(str), "must be string");
1343 return str;
1344 }
1345
1346 void ConstantPool::copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int cp_index,
1347 int start_arg, int end_arg,
1348 objArrayHandle info, int pos,
1349 bool must_resolve, Handle if_not_available,
1350 TRAPS) {
1351 int limit = pos + end_arg - start_arg;
1352 // checks: cp_index in range [0..this_cp->length),
1353 // tag at cp_index, start..end in range [0..this_cp->bootstrap_argument_count],
1354 // info array non-null, pos..limit in [0..info.length]
1355 if ((0 >= cp_index || cp_index >= this_cp->length()) ||
1356 !(this_cp->tag_at(cp_index).is_invoke_dynamic() ||
1357 this_cp->tag_at(cp_index).is_dynamic_constant()) ||
1358 (0 > start_arg || start_arg > end_arg) ||
1359 (end_arg > this_cp->bootstrap_argument_count_at(cp_index)) ||
1360 (0 > pos || pos > limit) ||
1361 (info.is_null() || limit > info->length())) {
1362 // An index or something else went wrong; throw an error.
1363 // Since this is an internal API, we don't expect this,
1364 // so we don't bother to craft a nice message.
1365 THROW_MSG(vmSymbols::java_lang_LinkageError(), "bad BSM argument access");
1366 }
1367 // now we can loop safely
1368 int info_i = pos;
1369 for (int i = start_arg; i < end_arg; i++) {
1370 int arg_index = this_cp->bootstrap_argument_index_at(cp_index, i);
1371 oop arg_oop;
1372 if (must_resolve) {
1373 arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK);
1374 } else {
1375 bool found_it = false;
1376 arg_oop = this_cp->find_cached_constant_at(arg_index, found_it, CHECK);
1377 if (!found_it) arg_oop = if_not_available();
1378 }
1379 info->obj_at_put(info_i++, arg_oop);
1380 }
1381 }
1382
1383 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int cp_index, int obj_index, TRAPS) {
1384 // If the string has already been interned, this entry will be non-null
1385 oop str = this_cp->resolved_reference_at(obj_index);
1386 assert(str != Universe::the_null_sentinel(), "");
1387 if (str != nullptr) return str;
1388 Symbol* sym = this_cp->unresolved_string_at(cp_index);
1389 str = StringTable::intern(sym, CHECK_(nullptr));
1390 this_cp->string_at_put(obj_index, str);
1391 assert(java_lang_String::is_instance(str), "must be string");
1392 return str;
1393 }
1394
1395
1396 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int cp_index) {
1397 // Names are interned, so we can compare Symbol*s directly
1398 Symbol* cp_name = klass_name_at(cp_index);
1399 return (cp_name == k->name());
1400 }
1401
1402
1403 // Iterate over symbols and decrement ones which are Symbol*s
1404 // This is done during GC.
1405 // Only decrement the UTF8 symbols. Strings point to
1406 // these symbols but didn't increment the reference count.
1407 void ConstantPool::unreference_symbols() {
1408 for (int index = 1; index < length(); index++) { // Index 0 is unused
1409 constantTag tag = tag_at(index);
1410 if (tag.is_symbol()) {
1411 symbol_at(index)->decrement_refcount();
1412 }
1413 }
1414 }
1415
1416
1417 // Compare this constant pool's entry at index1 to the constant pool
1418 // cp2's entry at index2.
1419 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
1420 int index2) {
1421
1422 // The error tags are equivalent to non-error tags when comparing
1423 jbyte t1 = tag_at(index1).non_error_value();
1424 jbyte t2 = cp2->tag_at(index2).non_error_value();
1425
1426 // Some classes are pre-resolved (like Throwable) which may lead to
1427 // consider it as a different entry. We then revert them back temporarily
1428 // to ensure proper comparison.
1429 if (t1 == JVM_CONSTANT_Class) {
1430 t1 = JVM_CONSTANT_UnresolvedClass;
1431 }
1432 if (t2 == JVM_CONSTANT_Class) {
1433 t2 = JVM_CONSTANT_UnresolvedClass;
1434 }
1435
1436 if (t1 != t2) {
1437 // Not the same entry type so there is nothing else to check. Note
1438 // that this style of checking will consider resolved/unresolved
1439 // class pairs as different.
1440 // From the ConstantPool* API point of view, this is correct
1441 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
1442 // plays out in the context of ConstantPool* merging.
1443 return false;
1444 }
1445
1446 switch (t1) {
1447 case JVM_CONSTANT_ClassIndex:
1448 {
1449 int recur1 = klass_index_at(index1);
1450 int recur2 = cp2->klass_index_at(index2);
1451 if (compare_entry_to(recur1, cp2, recur2)) {
1452 return true;
1453 }
1454 } break;
1455
1456 case JVM_CONSTANT_Double:
1457 {
1458 jdouble d1 = double_at(index1);
1459 jdouble d2 = cp2->double_at(index2);
1460 if (d1 == d2) {
1461 return true;
1462 }
1463 } break;
1464
1465 case JVM_CONSTANT_Fieldref:
1466 case JVM_CONSTANT_InterfaceMethodref:
1467 case JVM_CONSTANT_Methodref:
1468 {
1469 int recur1 = uncached_klass_ref_index_at(index1);
1470 int recur2 = cp2->uncached_klass_ref_index_at(index2);
1471 bool match = compare_entry_to(recur1, cp2, recur2);
1472 if (match) {
1473 recur1 = uncached_name_and_type_ref_index_at(index1);
1474 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
1475 if (compare_entry_to(recur1, cp2, recur2)) {
1476 return true;
1477 }
1478 }
1479 } break;
1480
1481 case JVM_CONSTANT_Float:
1482 {
1483 jfloat f1 = float_at(index1);
1484 jfloat f2 = cp2->float_at(index2);
1485 if (f1 == f2) {
1486 return true;
1487 }
1488 } break;
1489
1490 case JVM_CONSTANT_Integer:
1491 {
1492 jint i1 = int_at(index1);
1493 jint i2 = cp2->int_at(index2);
1494 if (i1 == i2) {
1495 return true;
1496 }
1497 } break;
1498
1499 case JVM_CONSTANT_Long:
1500 {
1501 jlong l1 = long_at(index1);
1502 jlong l2 = cp2->long_at(index2);
1503 if (l1 == l2) {
1504 return true;
1505 }
1506 } break;
1507
1508 case JVM_CONSTANT_NameAndType:
1509 {
1510 int recur1 = name_ref_index_at(index1);
1511 int recur2 = cp2->name_ref_index_at(index2);
1512 if (compare_entry_to(recur1, cp2, recur2)) {
1513 recur1 = signature_ref_index_at(index1);
1514 recur2 = cp2->signature_ref_index_at(index2);
1515 if (compare_entry_to(recur1, cp2, recur2)) {
1516 return true;
1517 }
1518 }
1519 } break;
1520
1521 case JVM_CONSTANT_StringIndex:
1522 {
1523 int recur1 = string_index_at(index1);
1524 int recur2 = cp2->string_index_at(index2);
1525 if (compare_entry_to(recur1, cp2, recur2)) {
1526 return true;
1527 }
1528 } break;
1529
1530 case JVM_CONSTANT_UnresolvedClass:
1531 {
1532 Symbol* k1 = klass_name_at(index1);
1533 Symbol* k2 = cp2->klass_name_at(index2);
1534 if (k1 == k2) {
1535 return true;
1536 }
1537 } break;
1538
1539 case JVM_CONSTANT_MethodType:
1540 {
1541 int k1 = method_type_index_at(index1);
1542 int k2 = cp2->method_type_index_at(index2);
1543 if (compare_entry_to(k1, cp2, k2)) {
1544 return true;
1545 }
1546 } break;
1547
1548 case JVM_CONSTANT_MethodHandle:
1549 {
1550 int k1 = method_handle_ref_kind_at(index1);
1551 int k2 = cp2->method_handle_ref_kind_at(index2);
1552 if (k1 == k2) {
1553 int i1 = method_handle_index_at(index1);
1554 int i2 = cp2->method_handle_index_at(index2);
1555 if (compare_entry_to(i1, cp2, i2)) {
1556 return true;
1557 }
1558 }
1559 } break;
1560
1561 case JVM_CONSTANT_Dynamic:
1562 {
1563 int k1 = bootstrap_name_and_type_ref_index_at(index1);
1564 int k2 = cp2->bootstrap_name_and_type_ref_index_at(index2);
1565 int i1 = bootstrap_methods_attribute_index(index1);
1566 int i2 = cp2->bootstrap_methods_attribute_index(index2);
1567 bool match_entry = compare_entry_to(k1, cp2, k2);
1568 bool match_operand = compare_operand_to(i1, cp2, i2);
1569 return (match_entry && match_operand);
1570 } break;
1571
1572 case JVM_CONSTANT_InvokeDynamic:
1573 {
1574 int k1 = bootstrap_name_and_type_ref_index_at(index1);
1575 int k2 = cp2->bootstrap_name_and_type_ref_index_at(index2);
1576 int i1 = bootstrap_methods_attribute_index(index1);
1577 int i2 = cp2->bootstrap_methods_attribute_index(index2);
1578 bool match_entry = compare_entry_to(k1, cp2, k2);
1579 bool match_operand = compare_operand_to(i1, cp2, i2);
1580 return (match_entry && match_operand);
1581 } break;
1582
1583 case JVM_CONSTANT_String:
1584 {
1585 Symbol* s1 = unresolved_string_at(index1);
1586 Symbol* s2 = cp2->unresolved_string_at(index2);
1587 if (s1 == s2) {
1588 return true;
1589 }
1590 } break;
1591
1592 case JVM_CONSTANT_Utf8:
1593 {
1594 Symbol* s1 = symbol_at(index1);
1595 Symbol* s2 = cp2->symbol_at(index2);
1596 if (s1 == s2) {
1597 return true;
1598 }
1599 } break;
1600
1601 // Invalid is used as the tag for the second constant pool entry
1602 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1603 // not be seen by itself.
1604 case JVM_CONSTANT_Invalid: // fall through
1605
1606 default:
1607 ShouldNotReachHere();
1608 break;
1609 }
1610
1611 return false;
1612 } // end compare_entry_to()
1613
1614
1615 // Resize the operands array with delta_len and delta_size.
1616 // Used in RedefineClasses for CP merge.
1617 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
1618 int old_len = operand_array_length(operands());
1619 int new_len = old_len + delta_len;
1620 int min_len = (delta_len > 0) ? old_len : new_len;
1621
1622 int old_size = operands()->length();
1623 int new_size = old_size + delta_size;
1624 int min_size = (delta_size > 0) ? old_size : new_size;
1625
1626 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1627 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
1628
1629 // Set index in the resized array for existing elements only
1630 for (int idx = 0; idx < min_len; idx++) {
1631 int offset = operand_offset_at(idx); // offset in original array
1632 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
1633 }
1634 // Copy the bootstrap specifiers only
1635 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
1636 new_ops->adr_at(2*new_len),
1637 (min_size - 2*min_len) * sizeof(u2));
1638 // Explicitly deallocate old operands array.
1639 // Note, it is not needed for 7u backport.
1640 if ( operands() != nullptr) { // the safety check
1641 MetadataFactory::free_array<u2>(loader_data, operands());
1642 }
1643 set_operands(new_ops);
1644 } // end resize_operands()
1645
1646
1647 // Extend the operands array with the length and size of the ext_cp operands.
1648 // Used in RedefineClasses for CP merge.
1649 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) {
1650 int delta_len = operand_array_length(ext_cp->operands());
1651 if (delta_len == 0) {
1652 return; // nothing to do
1653 }
1654 int delta_size = ext_cp->operands()->length();
1655
1656 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger");
1657
1658 if (operand_array_length(operands()) == 0) {
1659 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1660 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
1661 // The first element index defines the offset of second part
1662 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
1663 set_operands(new_ops);
1664 } else {
1665 resize_operands(delta_len, delta_size, CHECK);
1666 }
1667
1668 } // end extend_operands()
1669
1670
1671 // Shrink the operands array to a smaller array with new_len length.
1672 // Used in RedefineClasses for CP merge.
1673 void ConstantPool::shrink_operands(int new_len, TRAPS) {
1674 int old_len = operand_array_length(operands());
1675 if (new_len == old_len) {
1676 return; // nothing to do
1677 }
1678 assert(new_len < old_len, "shrunken operands array must be smaller");
1679
1680 int free_base = operand_next_offset_at(new_len - 1);
1681 int delta_len = new_len - old_len;
1682 int delta_size = 2*delta_len + free_base - operands()->length();
1683
1684 resize_operands(delta_len, delta_size, CHECK);
1685
1686 } // end shrink_operands()
1687
1688
1689 void ConstantPool::copy_operands(const constantPoolHandle& from_cp,
1690 const constantPoolHandle& to_cp,
1691 TRAPS) {
1692
1693 int from_oplen = operand_array_length(from_cp->operands());
1694 int old_oplen = operand_array_length(to_cp->operands());
1695 if (from_oplen != 0) {
1696 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
1697 // append my operands to the target's operands array
1698 if (old_oplen == 0) {
1699 // Can't just reuse from_cp's operand list because of deallocation issues
1700 int len = from_cp->operands()->length();
1701 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
1702 Copy::conjoint_memory_atomic(
1703 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
1704 to_cp->set_operands(new_ops);
1705 } else {
1706 int old_len = to_cp->operands()->length();
1707 int from_len = from_cp->operands()->length();
1708 int old_off = old_oplen * sizeof(u2);
1709 int from_off = from_oplen * sizeof(u2);
1710 // Use the metaspace for the destination constant pool
1711 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
1712 int fillp = 0, len = 0;
1713 // first part of dest
1714 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
1715 new_operands->adr_at(fillp),
1716 (len = old_off) * sizeof(u2));
1717 fillp += len;
1718 // first part of src
1719 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
1720 new_operands->adr_at(fillp),
1721 (len = from_off) * sizeof(u2));
1722 fillp += len;
1723 // second part of dest
1724 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
1725 new_operands->adr_at(fillp),
1726 (len = old_len - old_off) * sizeof(u2));
1727 fillp += len;
1728 // second part of src
1729 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
1730 new_operands->adr_at(fillp),
1731 (len = from_len - from_off) * sizeof(u2));
1732 fillp += len;
1733 assert(fillp == new_operands->length(), "");
1734
1735 // Adjust indexes in the first part of the copied operands array.
1736 for (int j = 0; j < from_oplen; j++) {
1737 int offset = operand_offset_at(new_operands, old_oplen + j);
1738 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
1739 offset += old_len; // every new tuple is preceded by old_len extra u2's
1740 operand_offset_at_put(new_operands, old_oplen + j, offset);
1741 }
1742
1743 // replace target operands array with combined array
1744 to_cp->set_operands(new_operands);
1745 }
1746 }
1747 } // end copy_operands()
1748
1749
1750 // Copy this constant pool's entries at start_i to end_i (inclusive)
1751 // to the constant pool to_cp's entries starting at to_i. A total of
1752 // (end_i - start_i) + 1 entries are copied.
1753 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i,
1754 const constantPoolHandle& to_cp, int to_i, TRAPS) {
1755
1756
1757 int dest_cpi = to_i; // leave original alone for debug purposes
1758
1759 for (int src_cpi = start_i; src_cpi <= end_i; /* see loop bottom */ ) {
1760 copy_entry_to(from_cp, src_cpi, to_cp, dest_cpi);
1761
1762 switch (from_cp->tag_at(src_cpi).value()) {
1763 case JVM_CONSTANT_Double:
1764 case JVM_CONSTANT_Long:
1765 // double and long take two constant pool entries
1766 src_cpi += 2;
1767 dest_cpi += 2;
1768 break;
1769
1770 default:
1771 // all others take one constant pool entry
1772 src_cpi++;
1773 dest_cpi++;
1774 break;
1775 }
1776 }
1777 copy_operands(from_cp, to_cp, CHECK);
1778
1779 } // end copy_cp_to_impl()
1780
1781
1782 // Copy this constant pool's entry at from_i to the constant pool
1783 // to_cp's entry at to_i.
1784 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1785 const constantPoolHandle& to_cp, int to_i) {
1786
1787 int tag = from_cp->tag_at(from_i).value();
1788 switch (tag) {
1789 case JVM_CONSTANT_ClassIndex:
1790 {
1791 jint ki = from_cp->klass_index_at(from_i);
1792 to_cp->klass_index_at_put(to_i, ki);
1793 } break;
1794
1795 case JVM_CONSTANT_Double:
1796 {
1797 jdouble d = from_cp->double_at(from_i);
1798 to_cp->double_at_put(to_i, d);
1799 // double takes two constant pool entries so init second entry's tag
1800 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1801 } break;
1802
1803 case JVM_CONSTANT_Fieldref:
1804 {
1805 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1806 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1807 to_cp->field_at_put(to_i, class_index, name_and_type_index);
1808 } break;
1809
1810 case JVM_CONSTANT_Float:
1811 {
1812 jfloat f = from_cp->float_at(from_i);
1813 to_cp->float_at_put(to_i, f);
1814 } break;
1815
1816 case JVM_CONSTANT_Integer:
1817 {
1818 jint i = from_cp->int_at(from_i);
1819 to_cp->int_at_put(to_i, i);
1820 } break;
1821
1822 case JVM_CONSTANT_InterfaceMethodref:
1823 {
1824 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1825 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1826 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1827 } break;
1828
1829 case JVM_CONSTANT_Long:
1830 {
1831 jlong l = from_cp->long_at(from_i);
1832 to_cp->long_at_put(to_i, l);
1833 // long takes two constant pool entries so init second entry's tag
1834 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1835 } break;
1836
1837 case JVM_CONSTANT_Methodref:
1838 {
1839 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1840 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1841 to_cp->method_at_put(to_i, class_index, name_and_type_index);
1842 } break;
1843
1844 case JVM_CONSTANT_NameAndType:
1845 {
1846 int name_ref_index = from_cp->name_ref_index_at(from_i);
1847 int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1848 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1849 } break;
1850
1851 case JVM_CONSTANT_StringIndex:
1852 {
1853 jint si = from_cp->string_index_at(from_i);
1854 to_cp->string_index_at_put(to_i, si);
1855 } break;
1856
1857 case JVM_CONSTANT_Class:
1858 case JVM_CONSTANT_UnresolvedClass:
1859 case JVM_CONSTANT_UnresolvedClassInError:
1860 {
1861 // Revert to JVM_CONSTANT_ClassIndex
1862 int name_index = from_cp->klass_slot_at(from_i).name_index();
1863 assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1864 to_cp->klass_index_at_put(to_i, name_index);
1865 } break;
1866
1867 case JVM_CONSTANT_String:
1868 {
1869 Symbol* s = from_cp->unresolved_string_at(from_i);
1870 to_cp->unresolved_string_at_put(to_i, s);
1871 } break;
1872
1873 case JVM_CONSTANT_Utf8:
1874 {
1875 Symbol* s = from_cp->symbol_at(from_i);
1876 // Need to increase refcount, the old one will be thrown away and deferenced
1877 s->increment_refcount();
1878 to_cp->symbol_at_put(to_i, s);
1879 } break;
1880
1881 case JVM_CONSTANT_MethodType:
1882 case JVM_CONSTANT_MethodTypeInError:
1883 {
1884 jint k = from_cp->method_type_index_at(from_i);
1885 to_cp->method_type_index_at_put(to_i, k);
1886 } break;
1887
1888 case JVM_CONSTANT_MethodHandle:
1889 case JVM_CONSTANT_MethodHandleInError:
1890 {
1891 int k1 = from_cp->method_handle_ref_kind_at(from_i);
1892 int k2 = from_cp->method_handle_index_at(from_i);
1893 to_cp->method_handle_index_at_put(to_i, k1, k2);
1894 } break;
1895
1896 case JVM_CONSTANT_Dynamic:
1897 case JVM_CONSTANT_DynamicInError:
1898 {
1899 int k1 = from_cp->bootstrap_methods_attribute_index(from_i);
1900 int k2 = from_cp->bootstrap_name_and_type_ref_index_at(from_i);
1901 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands
1902 to_cp->dynamic_constant_at_put(to_i, k1, k2);
1903 } break;
1904
1905 case JVM_CONSTANT_InvokeDynamic:
1906 {
1907 int k1 = from_cp->bootstrap_methods_attribute_index(from_i);
1908 int k2 = from_cp->bootstrap_name_and_type_ref_index_at(from_i);
1909 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands
1910 to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1911 } break;
1912
1913 // Invalid is used as the tag for the second constant pool entry
1914 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1915 // not be seen by itself.
1916 case JVM_CONSTANT_Invalid: // fall through
1917
1918 default:
1919 {
1920 ShouldNotReachHere();
1921 } break;
1922 }
1923 } // end copy_entry_to()
1924
1925 // Search constant pool search_cp for an entry that matches this
1926 // constant pool's entry at pattern_i. Returns the index of a
1927 // matching entry or zero (0) if there is no matching entry.
1928 int ConstantPool::find_matching_entry(int pattern_i,
1929 const constantPoolHandle& search_cp) {
1930
1931 // index zero (0) is not used
1932 for (int i = 1; i < search_cp->length(); i++) {
1933 bool found = compare_entry_to(pattern_i, search_cp, i);
1934 if (found) {
1935 return i;
1936 }
1937 }
1938
1939 return 0; // entry not found; return unused index zero (0)
1940 } // end find_matching_entry()
1941
1942
1943 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1944 // cp2's bootstrap specifier at idx2.
1945 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2) {
1946 BSMAttributeEntry* e1 = bsm_attribute_entry(idx1);
1947 BSMAttributeEntry* e2 = cp2->bsm_attribute_entry(idx2);
1948 int k1 = e1->bootstrap_method_index();
1949 int k2 = e2->bootstrap_method_index();
1950 bool match = compare_entry_to(k1, cp2, k2);
1951
1952 if (!match) {
1953 return false;
1954 }
1955 int argc = e1->argument_count();
1956 if (argc == e2->argument_count()) {
1957 for (int j = 0; j < argc; j++) {
1958 k1 = e1->argument_index(j);
1959 k2 = e2->argument_index(j);
1960 match = compare_entry_to(k1, cp2, k2);
1961 if (!match) {
1962 return false;
1963 }
1964 }
1965 return true; // got through loop; all elements equal
1966 }
1967 return false;
1968 } // end compare_operand_to()
1969
1970 // Search constant pool search_cp for a bootstrap specifier that matches
1971 // this constant pool's bootstrap specifier data at pattern_i index.
1972 // Return the index of a matching bootstrap attribute record or (-1) if there is no match.
1973 int ConstantPool::find_matching_operand(int pattern_i,
1974 const constantPoolHandle& search_cp, int search_len) {
1975 for (int i = 0; i < search_len; i++) {
1976 bool found = compare_operand_to(pattern_i, search_cp, i);
1977 if (found) {
1978 return i;
1979 }
1980 }
1981 return -1; // bootstrap specifier data not found; return unused index (-1)
1982 } // end find_matching_operand()
1983
1984
1985 #ifndef PRODUCT
1986
1987 const char* ConstantPool::printable_name_at(int cp_index) {
1988
1989 constantTag tag = tag_at(cp_index);
1990
1991 if (tag.is_string()) {
1992 return string_at_noresolve(cp_index);
1993 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1994 return klass_name_at(cp_index)->as_C_string();
1995 } else if (tag.is_symbol()) {
1996 return symbol_at(cp_index)->as_C_string();
1997 }
1998 return "";
1999 }
2000
2001 #endif // PRODUCT
2002
2003
2004 // Returns size of constant pool entry.
2005 jint ConstantPool::cpool_entry_size(jint idx) {
2006 switch(tag_at(idx).value()) {
2007 case JVM_CONSTANT_Invalid:
2008 case JVM_CONSTANT_Unicode:
2009 return 1;
2010
2011 case JVM_CONSTANT_Utf8:
2012 return 3 + symbol_at(idx)->utf8_length();
2013
2014 case JVM_CONSTANT_Class:
2015 case JVM_CONSTANT_String:
2016 case JVM_CONSTANT_ClassIndex:
2017 case JVM_CONSTANT_UnresolvedClass:
2018 case JVM_CONSTANT_UnresolvedClassInError:
2019 case JVM_CONSTANT_StringIndex:
2020 case JVM_CONSTANT_MethodType:
2021 case JVM_CONSTANT_MethodTypeInError:
2022 return 3;
2023
2024 case JVM_CONSTANT_MethodHandle:
2025 case JVM_CONSTANT_MethodHandleInError:
2026 return 4; //tag, ref_kind, ref_index
2027
2028 case JVM_CONSTANT_Integer:
2029 case JVM_CONSTANT_Float:
2030 case JVM_CONSTANT_Fieldref:
2031 case JVM_CONSTANT_Methodref:
2032 case JVM_CONSTANT_InterfaceMethodref:
2033 case JVM_CONSTANT_NameAndType:
2034 return 5;
2035
2036 case JVM_CONSTANT_Dynamic:
2037 case JVM_CONSTANT_DynamicInError:
2038 case JVM_CONSTANT_InvokeDynamic:
2039 // u1 tag, u2 bsm, u2 nt
2040 return 5;
2041
2042 case JVM_CONSTANT_Long:
2043 case JVM_CONSTANT_Double:
2044 return 9;
2045 }
2046 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
2047 return 1;
2048 } /* end cpool_entry_size */
2049
2050
2051 // SymbolHash is used to find a constant pool index from a string.
2052 // This function fills in SymbolHashs, one for utf8s and one for
2053 // class names, returns size of the cpool raw bytes.
2054 jint ConstantPool::hash_entries_to(SymbolHash *symmap,
2055 SymbolHash *classmap) {
2056 jint size = 0;
2057
2058 for (u2 idx = 1; idx < length(); idx++) {
2059 u2 tag = tag_at(idx).value();
2060 size += cpool_entry_size(idx);
2061
2062 switch(tag) {
2063 case JVM_CONSTANT_Utf8: {
2064 Symbol* sym = symbol_at(idx);
2065 symmap->add_if_absent(sym, idx);
2066 break;
2067 }
2068 case JVM_CONSTANT_Class:
2069 case JVM_CONSTANT_UnresolvedClass:
2070 case JVM_CONSTANT_UnresolvedClassInError: {
2071 Symbol* sym = klass_name_at(idx);
2072 classmap->add_if_absent(sym, idx);
2073 break;
2074 }
2075 case JVM_CONSTANT_Long:
2076 case JVM_CONSTANT_Double: {
2077 idx++; // Both Long and Double take two cpool slots
2078 break;
2079 }
2080 }
2081 }
2082 return size;
2083 } /* end hash_utf8_entries_to */
2084
2085
2086 // Copy cpool bytes.
2087 // Returns:
2088 // 0, in case of OutOfMemoryError
2089 // -1, in case of internal error
2090 // > 0, count of the raw cpool bytes that have been copied
2091 int ConstantPool::copy_cpool_bytes(int cpool_size,
2092 SymbolHash* tbl,
2093 unsigned char *bytes) {
2094 u2 idx1, idx2;
2095 jint size = 0;
2096 jint cnt = length();
2097 unsigned char *start_bytes = bytes;
2098
2099 for (jint idx = 1; idx < cnt; idx++) {
2100 u1 tag = tag_at(idx).value();
2101 jint ent_size = cpool_entry_size(idx);
2102
2103 assert(size + ent_size <= cpool_size, "Size mismatch");
2104
2105 *bytes = tag;
2106 switch(tag) {
2107 case JVM_CONSTANT_Invalid: {
2108 break;
2109 }
2110 case JVM_CONSTANT_Unicode: {
2111 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
2112 break;
2113 }
2114 case JVM_CONSTANT_Utf8: {
2115 Symbol* sym = symbol_at(idx);
2116 char* str = sym->as_utf8();
2117 // Warning! It's crashing on x86 with len = sym->utf8_length()
2118 int len = (int) strlen(str);
2119 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
2120 for (int i = 0; i < len; i++) {
2121 bytes[3+i] = (u1) str[i];
2122 }
2123 break;
2124 }
2125 case JVM_CONSTANT_Integer: {
2126 jint val = int_at(idx);
2127 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
2128 break;
2129 }
2130 case JVM_CONSTANT_Float: {
2131 jfloat val = float_at(idx);
2132 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
2133 break;
2134 }
2135 case JVM_CONSTANT_Long: {
2136 jlong val = long_at(idx);
2137 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2138 idx++; // Long takes two cpool slots
2139 break;
2140 }
2141 case JVM_CONSTANT_Double: {
2142 jdouble val = double_at(idx);
2143 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2144 idx++; // Double takes two cpool slots
2145 break;
2146 }
2147 case JVM_CONSTANT_Class:
2148 case JVM_CONSTANT_UnresolvedClass:
2149 case JVM_CONSTANT_UnresolvedClassInError: {
2150 *bytes = JVM_CONSTANT_Class;
2151 Symbol* sym = klass_name_at(idx);
2152 idx1 = tbl->symbol_to_value(sym);
2153 assert(idx1 != 0, "Have not found a hashtable entry");
2154 Bytes::put_Java_u2((address) (bytes+1), idx1);
2155 break;
2156 }
2157 case JVM_CONSTANT_String: {
2158 *bytes = JVM_CONSTANT_String;
2159 Symbol* sym = unresolved_string_at(idx);
2160 idx1 = tbl->symbol_to_value(sym);
2161 assert(idx1 != 0, "Have not found a hashtable entry");
2162 Bytes::put_Java_u2((address) (bytes+1), idx1);
2163 break;
2164 }
2165 case JVM_CONSTANT_Fieldref:
2166 case JVM_CONSTANT_Methodref:
2167 case JVM_CONSTANT_InterfaceMethodref: {
2168 idx1 = uncached_klass_ref_index_at(idx);
2169 idx2 = uncached_name_and_type_ref_index_at(idx);
2170 Bytes::put_Java_u2((address) (bytes+1), idx1);
2171 Bytes::put_Java_u2((address) (bytes+3), idx2);
2172 break;
2173 }
2174 case JVM_CONSTANT_NameAndType: {
2175 idx1 = name_ref_index_at(idx);
2176 idx2 = signature_ref_index_at(idx);
2177 Bytes::put_Java_u2((address) (bytes+1), idx1);
2178 Bytes::put_Java_u2((address) (bytes+3), idx2);
2179 break;
2180 }
2181 case JVM_CONSTANT_ClassIndex: {
2182 *bytes = JVM_CONSTANT_Class;
2183 idx1 = checked_cast<u2>(klass_index_at(idx));
2184 Bytes::put_Java_u2((address) (bytes+1), idx1);
2185 break;
2186 }
2187 case JVM_CONSTANT_StringIndex: {
2188 *bytes = JVM_CONSTANT_String;
2189 idx1 = checked_cast<u2>(string_index_at(idx));
2190 Bytes::put_Java_u2((address) (bytes+1), idx1);
2191 break;
2192 }
2193 case JVM_CONSTANT_MethodHandle:
2194 case JVM_CONSTANT_MethodHandleInError: {
2195 *bytes = JVM_CONSTANT_MethodHandle;
2196 int kind = method_handle_ref_kind_at(idx);
2197 idx1 = checked_cast<u2>(method_handle_index_at(idx));
2198 *(bytes+1) = (unsigned char) kind;
2199 Bytes::put_Java_u2((address) (bytes+2), idx1);
2200 break;
2201 }
2202 case JVM_CONSTANT_MethodType:
2203 case JVM_CONSTANT_MethodTypeInError: {
2204 *bytes = JVM_CONSTANT_MethodType;
2205 idx1 = checked_cast<u2>(method_type_index_at(idx));
2206 Bytes::put_Java_u2((address) (bytes+1), idx1);
2207 break;
2208 }
2209 case JVM_CONSTANT_Dynamic:
2210 case JVM_CONSTANT_DynamicInError: {
2211 *bytes = tag;
2212 idx1 = extract_low_short_from_int(*int_at_addr(idx));
2213 idx2 = extract_high_short_from_int(*int_at_addr(idx));
2214 assert(idx2 == bootstrap_name_and_type_ref_index_at(idx), "correct half of u4");
2215 Bytes::put_Java_u2((address) (bytes+1), idx1);
2216 Bytes::put_Java_u2((address) (bytes+3), idx2);
2217 break;
2218 }
2219 case JVM_CONSTANT_InvokeDynamic: {
2220 *bytes = tag;
2221 idx1 = extract_low_short_from_int(*int_at_addr(idx));
2222 idx2 = extract_high_short_from_int(*int_at_addr(idx));
2223 assert(idx2 == bootstrap_name_and_type_ref_index_at(idx), "correct half of u4");
2224 Bytes::put_Java_u2((address) (bytes+1), idx1);
2225 Bytes::put_Java_u2((address) (bytes+3), idx2);
2226 break;
2227 }
2228 }
2229 bytes += ent_size;
2230 size += ent_size;
2231 }
2232 assert(size == cpool_size, "Size mismatch");
2233
2234 return (int)(bytes - start_bytes);
2235 } /* end copy_cpool_bytes */
2236
2237 bool ConstantPool::is_maybe_on_stack() const {
2238 // This method uses the similar logic as nmethod::is_maybe_on_stack()
2239 if (!Continuations::enabled()) {
2240 return false;
2241 }
2242
2243 // If the condition below is true, it means that the nmethod was found to
2244 // be alive the previous completed marking cycle.
2245 return cache()->gc_epoch() >= CodeCache::previous_completed_gc_marking_cycle();
2246 }
2247
2248 // For redefinition, if any methods found in loom stack chunks, the gc_epoch is
2249 // recorded in their constant pool cache. The on_stack-ness of the constant pool controls whether
2250 // memory for the method is reclaimed.
2251 bool ConstantPool::on_stack() const {
2252 if ((_flags &_on_stack) != 0) {
2253 return true;
2254 }
2255
2256 if (_cache == nullptr) {
2257 return false;
2258 }
2259
2260 return is_maybe_on_stack();
2261 }
2262
2263 void ConstantPool::set_on_stack(const bool value) {
2264 if (value) {
2265 // Only record if it's not already set.
2266 if (!on_stack()) {
2267 assert(!in_aot_cache(), "should always be set for constant pools in AOT cache");
2268 _flags |= _on_stack;
2269 MetadataOnStackMark::record(this);
2270 }
2271 } else {
2272 // Clearing is done single-threadedly.
2273 if (!in_aot_cache()) {
2274 _flags &= (u2)(~_on_stack);
2275 }
2276 }
2277 }
2278
2279 // Printing
2280
2281 void ConstantPool::print_on(outputStream* st) const {
2282 assert(is_constantPool(), "must be constantPool");
2283 st->print_cr("%s", internal_name());
2284 if (flags() != 0) {
2285 st->print(" - flags: 0x%x", flags());
2286 if (has_preresolution()) st->print(" has_preresolution");
2287 if (on_stack()) st->print(" on_stack");
2288 st->cr();
2289 }
2290 if (pool_holder() != nullptr) {
2291 st->print_cr(" - holder: " PTR_FORMAT, p2i(pool_holder()));
2292 }
2293 st->print_cr(" - cache: " PTR_FORMAT, p2i(cache()));
2294 st->print_cr(" - resolved_references: " PTR_FORMAT, p2i(resolved_references_or_null()));
2295 st->print_cr(" - reference_map: " PTR_FORMAT, p2i(reference_map()));
2296 st->print_cr(" - resolved_klasses: " PTR_FORMAT, p2i(resolved_klasses()));
2297 st->print_cr(" - cp length: %d", length());
2298
2299 for (int index = 1; index < length(); index++) { // Index 0 is unused
2300 ((ConstantPool*)this)->print_entry_on(index, st);
2301 switch (tag_at(index).value()) {
2302 case JVM_CONSTANT_Long :
2303 case JVM_CONSTANT_Double :
2304 index++; // Skip entry following eigth-byte constant
2305 }
2306
2307 }
2308 st->cr();
2309 }
2310
2311 // Print one constant pool entry
2312 void ConstantPool::print_entry_on(const int cp_index, outputStream* st) {
2313 EXCEPTION_MARK;
2314 st->print(" - %3d : ", cp_index);
2315 tag_at(cp_index).print_on(st);
2316 st->print(" : ");
2317 switch (tag_at(cp_index).value()) {
2318 case JVM_CONSTANT_Class :
2319 { Klass* k = klass_at(cp_index, CATCH);
2320 guarantee(k != nullptr, "need klass");
2321 k->print_value_on(st);
2322 st->print(" {" PTR_FORMAT "}", p2i(k));
2323 }
2324 break;
2325 case JVM_CONSTANT_Fieldref :
2326 case JVM_CONSTANT_Methodref :
2327 case JVM_CONSTANT_InterfaceMethodref :
2328 st->print("klass_index=%d", uncached_klass_ref_index_at(cp_index));
2329 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(cp_index));
2330 break;
2331 case JVM_CONSTANT_String :
2332 unresolved_string_at(cp_index)->print_value_on(st);
2333 break;
2334 case JVM_CONSTANT_Integer :
2335 st->print("%d", int_at(cp_index));
2336 break;
2337 case JVM_CONSTANT_Float :
2338 st->print("%f", float_at(cp_index));
2339 break;
2340 case JVM_CONSTANT_Long :
2341 st->print_jlong(long_at(cp_index));
2342 break;
2343 case JVM_CONSTANT_Double :
2344 st->print("%lf", double_at(cp_index));
2345 break;
2346 case JVM_CONSTANT_NameAndType :
2347 st->print("name_index=%d", name_ref_index_at(cp_index));
2348 st->print(" signature_index=%d", signature_ref_index_at(cp_index));
2349 break;
2350 case JVM_CONSTANT_Utf8 :
2351 symbol_at(cp_index)->print_value_on(st);
2352 break;
2353 case JVM_CONSTANT_ClassIndex: {
2354 int name_index = *int_at_addr(cp_index);
2355 st->print("klass_index=%d ", name_index);
2356 symbol_at(name_index)->print_value_on(st);
2357 }
2358 break;
2359 case JVM_CONSTANT_UnresolvedClass : // fall-through
2360 case JVM_CONSTANT_UnresolvedClassInError: {
2361 CPKlassSlot kslot = klass_slot_at(cp_index);
2362 int resolved_klass_index = kslot.resolved_klass_index();
2363 int name_index = kslot.name_index();
2364 assert(tag_at(name_index).is_symbol(), "sanity");
2365 symbol_at(name_index)->print_value_on(st);
2366 }
2367 break;
2368 case JVM_CONSTANT_MethodHandle :
2369 case JVM_CONSTANT_MethodHandleInError :
2370 st->print("ref_kind=%d", method_handle_ref_kind_at(cp_index));
2371 st->print(" ref_index=%d", method_handle_index_at(cp_index));
2372 break;
2373 case JVM_CONSTANT_MethodType :
2374 case JVM_CONSTANT_MethodTypeInError :
2375 st->print("signature_index=%d", method_type_index_at(cp_index));
2376 break;
2377 case JVM_CONSTANT_Dynamic :
2378 case JVM_CONSTANT_DynamicInError :
2379 {
2380 st->print("bootstrap_method_index=%d", bootstrap_method_ref_index_at(cp_index));
2381 st->print(" type_index=%d", bootstrap_name_and_type_ref_index_at(cp_index));
2382 int argc = bootstrap_argument_count_at(cp_index);
2383 if (argc > 0) {
2384 for (int arg_i = 0; arg_i < argc; arg_i++) {
2385 int arg = bootstrap_argument_index_at(cp_index, arg_i);
2386 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2387 }
2388 st->print("}");
2389 }
2390 }
2391 break;
2392 case JVM_CONSTANT_InvokeDynamic :
2393 {
2394 st->print("bootstrap_method_index=%d", bootstrap_method_ref_index_at(cp_index));
2395 st->print(" name_and_type_index=%d", bootstrap_name_and_type_ref_index_at(cp_index));
2396 int argc = bootstrap_argument_count_at(cp_index);
2397 if (argc > 0) {
2398 for (int arg_i = 0; arg_i < argc; arg_i++) {
2399 int arg = bootstrap_argument_index_at(cp_index, arg_i);
2400 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2401 }
2402 st->print("}");
2403 }
2404 }
2405 break;
2406 default:
2407 ShouldNotReachHere();
2408 break;
2409 }
2410 st->cr();
2411 }
2412
2413 void ConstantPool::print_value_on(outputStream* st) const {
2414 assert(is_constantPool(), "must be constantPool");
2415 st->print("constant pool [%d]", length());
2416 if (has_preresolution()) st->print("/preresolution");
2417 if (operands() != nullptr) st->print("/operands[%d]", operands()->length());
2418 print_address_on(st);
2419 if (pool_holder() != nullptr) {
2420 st->print(" for ");
2421 pool_holder()->print_value_on(st);
2422 bool extra = (pool_holder()->constants() != this);
2423 if (extra) st->print(" (extra)");
2424 }
2425 if (cache() != nullptr) {
2426 st->print(" cache=" PTR_FORMAT, p2i(cache()));
2427 }
2428 }
2429
2430 // Verification
2431
2432 void ConstantPool::verify_on(outputStream* st) {
2433 guarantee(is_constantPool(), "object must be constant pool");
2434 for (int i = 0; i< length(); i++) {
2435 constantTag tag = tag_at(i);
2436 if (tag.is_klass() || tag.is_unresolved_klass()) {
2437 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
2438 } else if (tag.is_symbol()) {
2439 Symbol* entry = symbol_at(i);
2440 guarantee(entry->refcount() != 0, "should have nonzero reference count");
2441 } else if (tag.is_string()) {
2442 Symbol* entry = unresolved_string_at(i);
2443 guarantee(entry->refcount() != 0, "should have nonzero reference count");
2444 }
2445 }
2446 if (pool_holder() != nullptr) {
2447 // Note: pool_holder() can be null in temporary constant pools
2448 // used during constant pool merging
2449 guarantee(pool_holder()->is_klass(), "should be klass");
2450 }
2451 }