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 #ifndef SHARE_OOPS_CONSTANTPOOL_HPP
26 #define SHARE_OOPS_CONSTANTPOOL_HPP
27
28 #include "memory/allocation.hpp"
29 #include "oops/arrayOop.hpp"
30 #include "oops/bsmAttribute.inline.hpp"
31 #include "oops/cpCache.hpp"
32 #include "oops/objArrayOop.hpp"
33 #include "oops/oopHandle.hpp"
34 #include "oops/symbol.hpp"
35 #include "oops/typeArrayOop.hpp"
36 #include "runtime/handles.hpp"
37 #include "runtime/javaThread.hpp"
38 #include "utilities/align.hpp"
39 #include "utilities/bytes.hpp"
40 #include "utilities/constantTag.hpp"
41 #include "utilities/hashTable.hpp"
42 #include "utilities/macros.hpp"
43
44 // A ConstantPool is an array containing class constants as described in the
45 // class file.
46 //
47 // Most of the constant pool entries are written during class parsing, which
48 // is safe. For klass types, the constant pool entry is
49 // modified when the entry is resolved. If a klass constant pool
50 // entry is read without a lock, only the resolved state guarantees that
51 // the entry in the constant pool is a klass object and not a Symbol*.
52
53 // This represents a JVM_CONSTANT_Class, JVM_CONSTANT_UnresolvedClass, or
54 // JVM_CONSTANT_UnresolvedClassInError slot in the constant pool.
55 class CPKlassSlot {
56 // cp->symbol_at(_name_index) gives the name of the class.
57 int _name_index;
58
59 // cp->_resolved_klasses->at(_resolved_klass_index) gives the Klass* for the class.
60 int _resolved_klass_index;
61 public:
62 enum {
63 // This is used during constant pool merging where the resolved klass index is
64 // not yet known, and will be computed at a later stage (during a call to
65 // initialize_unresolved_klasses()).
66 _temp_resolved_klass_index = 0xffff
67 };
68 CPKlassSlot(int n, int rk) {
69 _name_index = n;
70 _resolved_klass_index = rk;
71 }
72 int name_index() const {
73 return _name_index;
74 }
75 int resolved_klass_index() const {
76 assert(_resolved_klass_index != _temp_resolved_klass_index, "constant pool merging was incomplete");
77 return _resolved_klass_index;
78 }
79 };
80
81 class ConstantPool : public Metadata {
82 friend class VMStructs;
83 friend class JVMCIVMStructs;
84 friend class BytecodeInterpreter; // Directly extracts a klass in the pool for fast instanceof/checkcast
85 friend class Universe; // For null constructor
86 friend class AOTConstantPoolResolver;
87 private:
88 // If you add a new field that points to any metaspace object, you
89 // must add this field to ConstantPool::metaspace_pointers_do().
90 Array<u1>* _tags; // the tag array describing the constant pool's contents
91 ConstantPoolCache* _cache; // the cache holding interpreter runtime information
92 InstanceKlass* _pool_holder; // the corresponding class
93
94 BSMAttributeEntries _bsm_entries;
95
96 // Consider using an array of compressed klass pointers to
97 // save space on 64-bit platforms.
98 Array<Klass*>* _resolved_klasses;
99
100 u2 _major_version; // major version number of class file
101 u2 _minor_version; // minor version number of class file
102
103 // Constant pool index to the utf8 entry of the Generic signature,
104 // or 0 if none.
105 u2 _generic_signature_index;
106 // Constant pool index to the utf8 entry for the name of source file
107 // containing this klass, 0 if not specified.
108 u2 _source_file_name_index;
109
110 enum {
111 _has_preresolution = 1, // Flags
112 _on_stack = 2,
113 _in_aot_cache = 4,
114 _has_dynamic_constant = 8,
115 _is_for_method_handle_intrinsic = 16
116 };
117
118 u2 _flags; // old fashioned bit twiddling
119
120 int _length; // number of elements in the array
121
122 union {
123 // set for CDS to restore resolved references
124 int _resolved_reference_length;
125 // keeps version number for redefined classes (used in backtrace)
126 int _version;
127 } _saved;
128
129 void set_tags(Array<u1>* tags) { _tags = tags; }
130 void tag_at_put(int cp_index, jbyte t) { tags()->at_put(cp_index, t); }
131 void release_tag_at_put(int cp_index, jbyte t) { tags()->release_at_put(cp_index, t); }
132
133 u1* tag_addr_at(int cp_index) const { return tags()->adr_at(cp_index); }
134
135 u2 flags() const { return _flags; }
136 void set_flags(u2 f) { _flags = f; }
137
138 private:
139 intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(ConstantPool)); }
140
141 intptr_t* obj_at_addr(int cp_index) const {
142 assert(is_within_bounds(cp_index), "index out of bounds");
143 return (intptr_t*) &base()[cp_index];
144 }
145
146 jint* int_at_addr(int cp_index) const {
147 assert(is_within_bounds(cp_index), "index out of bounds");
148 return (jint*) &base()[cp_index];
149 }
150
151 jlong* long_at_addr(int cp_index) const {
152 assert(is_within_bounds(cp_index), "index out of bounds");
153 return (jlong*) &base()[cp_index];
154 }
155
156 jfloat* float_at_addr(int cp_index) const {
157 assert(is_within_bounds(cp_index), "index out of bounds");
158 return (jfloat*) &base()[cp_index];
159 }
160
161 jdouble* double_at_addr(int cp_index) const {
162 assert(is_within_bounds(cp_index), "index out of bounds");
163 return (jdouble*) &base()[cp_index];
164 }
165
166 ConstantPool(Array<u1>* tags);
167 ConstantPool();
168 public:
169 static ConstantPool* allocate(ClassLoaderData* loader_data, int length, TRAPS);
170
171 virtual bool is_constantPool() const { return true; }
172
173 Array<u1>* tags() const { return _tags; }
174
175 BSMAttributeEntries& bsm_entries() {
176 return _bsm_entries;
177 }
178 const BSMAttributeEntries& bsm_entries() const {
179 return _bsm_entries;
180 }
181
182 bool has_preresolution() const { return (_flags & _has_preresolution) != 0; }
183 void set_has_preresolution() {
184 assert(!in_aot_cache(), "should never be called on ConstantPools in AOT cache");
185 _flags |= _has_preresolution;
186 }
187
188 // minor and major version numbers of class file
189 u2 major_version() const { return _major_version; }
190 void set_major_version(u2 major_version) { _major_version = major_version; }
191 u2 minor_version() const { return _minor_version; }
192 void set_minor_version(u2 minor_version) { _minor_version = minor_version; }
193
194 // generics support
195 Symbol* generic_signature() const {
196 return (_generic_signature_index == 0) ?
197 nullptr : symbol_at(_generic_signature_index);
198 }
199 u2 generic_signature_index() const { return _generic_signature_index; }
200 void set_generic_signature_index(u2 sig_index) { _generic_signature_index = sig_index; }
201
202 // source file name
203 Symbol* source_file_name() const {
204 return (_source_file_name_index == 0) ?
205 nullptr : symbol_at(_source_file_name_index);
206 }
207 u2 source_file_name_index() const { return _source_file_name_index; }
208 void set_source_file_name_index(u2 sourcefile_index) { _source_file_name_index = sourcefile_index; }
209
210 void copy_fields(const ConstantPool* orig);
211
212 // Redefine classes support. If a method referring to this constant pool
213 // is on the executing stack, or as a handle in vm code, this constant pool
214 // can't be removed from the set of previous versions saved in the instance
215 // class.
216 bool on_stack() const;
217 bool is_maybe_on_stack() const;
218 void set_on_stack(const bool value);
219
220 // Shadows MetaspaceObj::in_aot_cache(). It's faster and is used by set_on_stack()
221 bool in_aot_cache() const { return (_flags & _in_aot_cache) != 0; }
222
223 bool has_dynamic_constant() const { return (_flags & _has_dynamic_constant) != 0; }
224 void set_has_dynamic_constant() { _flags |= _has_dynamic_constant; }
225
226 bool is_for_method_handle_intrinsic() const { return (_flags & _is_for_method_handle_intrinsic) != 0; }
227 void set_is_for_method_handle_intrinsic() { _flags |= _is_for_method_handle_intrinsic; }
228
229 // Klass holding pool
230 InstanceKlass* pool_holder() const { return _pool_holder; }
231 void set_pool_holder(InstanceKlass* k) { _pool_holder = k; }
232 InstanceKlass** pool_holder_addr() { return &_pool_holder; }
233
234 // Interpreter runtime support
235 ConstantPoolCache* cache() const { return _cache; }
236 void set_cache(ConstantPoolCache* cache){ _cache = cache; }
237
238 virtual void metaspace_pointers_do(MetaspaceClosure* iter);
239 virtual MetaspaceObj::Type type() const { return ConstantPoolType; }
240
241 // Create object cache in the constant pool
242 void initialize_resolved_references(ClassLoaderData* loader_data,
243 const intStack& reference_map,
244 int constant_pool_map_length,
245 TRAPS);
246
247 // resolved strings, methodHandles and callsite objects from the constant pool
248 objArrayOop resolved_references() const;
249 objArrayOop resolved_references_or_null() const;
250 oop resolved_reference_at(int obj_index) const;
251 oop set_resolved_reference_at(int index, oop new_value);
252
253 // mapping resolved object array indexes to cp indexes and back.
254 int object_to_cp_index(int index) { return reference_map()->at(index); }
255 int cp_to_object_index(int index);
256
257 void set_resolved_klasses(Array<Klass*>* rk) { _resolved_klasses = rk; }
258 Array<Klass*>* resolved_klasses() const { return _resolved_klasses; }
259 void allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS);
260 void initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS);
261
262 // Given the per-instruction index of an indy instruction, report the
263 // main constant pool entry for its bootstrap specifier.
264 // From there, uncached_name/signature_ref_at will get the name/type.
265 inline u2 invokedynamic_bootstrap_ref_index_at(int indy_index) const;
266
267 // Assembly code support
268 static ByteSize tags_offset() { return byte_offset_of(ConstantPool, _tags); }
269 static ByteSize cache_offset() { return byte_offset_of(ConstantPool, _cache); }
270 static ByteSize pool_holder_offset() { return byte_offset_of(ConstantPool, _pool_holder); }
271 static ByteSize resolved_klasses_offset() { return byte_offset_of(ConstantPool, _resolved_klasses); }
272
273 // Storing constants
274
275 // For temporary use while constructing constant pool
276 void klass_index_at_put(int cp_index, int name_index) {
277 tag_at_put(cp_index, JVM_CONSTANT_ClassIndex);
278 *int_at_addr(cp_index) = name_index;
279 }
280
281 // Hidden class support:
282 void klass_at_put(int class_index, Klass* k);
283
284 void unresolved_klass_at_put(int cp_index, int name_index, int resolved_klass_index) {
285 release_tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
286
287 assert((name_index & 0xffff0000) == 0, "must be");
288 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
289 *int_at_addr(cp_index) =
290 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
291 }
292
293 void method_handle_index_at_put(int cp_index, int ref_kind, int ref_index) {
294 tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
295 *int_at_addr(cp_index) = ((jint) ref_index<<16) | ref_kind;
296 }
297
298 void method_type_index_at_put(int cp_index, int ref_index) {
299 tag_at_put(cp_index, JVM_CONSTANT_MethodType);
300 *int_at_addr(cp_index) = ref_index;
301 }
302
303 void dynamic_constant_at_put(int cp_index, int bsms_attribute_index, int name_and_type_index) {
304 tag_at_put(cp_index, JVM_CONSTANT_Dynamic);
305 *int_at_addr(cp_index) = ((jint) name_and_type_index<<16) | bsms_attribute_index;
306 }
307
308 void invoke_dynamic_at_put(int cp_index, int bsms_attribute_index, int name_and_type_index) {
309 tag_at_put(cp_index, JVM_CONSTANT_InvokeDynamic);
310 *int_at_addr(cp_index) = ((jint) name_and_type_index<<16) | bsms_attribute_index;
311 }
312
313 void unresolved_string_at_put(int cp_index, Symbol* s) {
314 assert(s->refcount() != 0, "should have nonzero refcount");
315 // Note that release_tag_at_put is not needed here because this is called only
316 // when constructing a ConstantPool in a single thread, with no possibility
317 // of concurrent access.
318 tag_at_put(cp_index, JVM_CONSTANT_String);
319 *symbol_at_addr(cp_index) = s;
320 }
321
322 void int_at_put(int cp_index, jint i) {
323 tag_at_put(cp_index, JVM_CONSTANT_Integer);
324 *int_at_addr(cp_index) = i;
325 }
326
327 void long_at_put(int cp_index, jlong l) {
328 tag_at_put(cp_index, JVM_CONSTANT_Long);
329 // *long_at_addr(which) = l;
330 Bytes::put_native_u8((address)long_at_addr(cp_index), *((u8*) &l));
331 }
332
333 void float_at_put(int cp_index, jfloat f) {
334 tag_at_put(cp_index, JVM_CONSTANT_Float);
335 *float_at_addr(cp_index) = f;
336 }
337
338 void double_at_put(int cp_index, jdouble d) {
339 tag_at_put(cp_index, JVM_CONSTANT_Double);
340 // *double_at_addr(which) = d;
341 // u8 temp = *(u8*) &d;
342 Bytes::put_native_u8((address) double_at_addr(cp_index), *((u8*) &d));
343 }
344
345 Symbol** symbol_at_addr(int cp_index) const {
346 assert(is_within_bounds(cp_index), "index out of bounds");
347 return (Symbol**) &base()[cp_index];
348 }
349
350 void symbol_at_put(int cp_index, Symbol* s) {
351 assert(s->refcount() != 0, "should have nonzero refcount");
352 tag_at_put(cp_index, JVM_CONSTANT_Utf8);
353 *symbol_at_addr(cp_index) = s;
354 }
355
356 void string_at_put(int obj_index, oop str);
357
358 // For temporary use while constructing constant pool
359 void string_index_at_put(int cp_index, int string_index) {
360 tag_at_put(cp_index, JVM_CONSTANT_StringIndex);
361 *int_at_addr(cp_index) = string_index;
362 }
363
364 void field_at_put(int cp_index, int class_index, int name_and_type_index) {
365 tag_at_put(cp_index, JVM_CONSTANT_Fieldref);
366 *int_at_addr(cp_index) = ((jint) name_and_type_index<<16) | class_index;
367 }
368
369 void method_at_put(int cp_index, int class_index, int name_and_type_index) {
370 tag_at_put(cp_index, JVM_CONSTANT_Methodref);
371 *int_at_addr(cp_index) = ((jint) name_and_type_index<<16) | class_index;
372 }
373
374 void interface_method_at_put(int cp_index, int class_index, int name_and_type_index) {
375 tag_at_put(cp_index, JVM_CONSTANT_InterfaceMethodref);
376 *int_at_addr(cp_index) = ((jint) name_and_type_index<<16) | class_index; // Not so nice
377 }
378
379 void name_and_type_at_put(int cp_index, int name_index, int signature_index) {
380 tag_at_put(cp_index, JVM_CONSTANT_NameAndType);
381 *int_at_addr(cp_index) = ((jint) signature_index<<16) | name_index; // Not so nice
382 }
383
384 // Tag query
385
386 constantTag tag_at(int cp_index) const { return (constantTag)tags()->at_acquire(cp_index); }
387
388 // Fetching constants
389
390 Klass* klass_at(int cp_index, TRAPS) {
391 constantPoolHandle h_this(THREAD, this);
392 return klass_at_impl(h_this, cp_index, THREAD);
393 }
394
395 CPKlassSlot klass_slot_at(int cp_index) const {
396 assert(tag_at(cp_index).is_unresolved_klass() || tag_at(cp_index).is_klass(),
397 "Corrupted constant pool");
398 int value = *int_at_addr(cp_index);
399 int name_index = extract_high_short_from_int(value);
400 int resolved_klass_index = extract_low_short_from_int(value);
401 return CPKlassSlot(name_index, resolved_klass_index);
402 }
403
404 Symbol* klass_name_at(int cp_index) const; // Returns the name, w/o resolving.
405 int klass_name_index_at(int cp_index) const {
406 return klass_slot_at(cp_index).name_index();
407 }
408
409 Klass* resolved_klass_at(int cp_index) const; // Used by Compiler
410
411 // RedefineClasses() API support:
412 Symbol* klass_at_noresolve(int cp_index) { return klass_name_at(cp_index); }
413 void temp_unresolved_klass_at_put(int cp_index, int name_index) {
414 // Used only during constant pool merging for class redefinition. The resolved klass index
415 // will be initialized later by a call to initialize_unresolved_klasses().
416 unresolved_klass_at_put(cp_index, name_index, CPKlassSlot::_temp_resolved_klass_index);
417 }
418
419 jint int_at(int cp_index) const {
420 assert(tag_at(cp_index).is_int(), "Corrupted constant pool");
421 return *int_at_addr(cp_index);
422 }
423
424 jlong long_at(int cp_index) {
425 assert(tag_at(cp_index).is_long(), "Corrupted constant pool");
426 // return *long_at_addr(cp_index);
427 u8 tmp = Bytes::get_native_u8((address)&base()[cp_index]);
428 return *((jlong*)&tmp);
429 }
430
431 jfloat float_at(int cp_index) {
432 assert(tag_at(cp_index).is_float(), "Corrupted constant pool");
433 return *float_at_addr(cp_index);
434 }
435
436 jdouble double_at(int cp_index) {
437 assert(tag_at(cp_index).is_double(), "Corrupted constant pool");
438 u8 tmp = Bytes::get_native_u8((address)&base()[cp_index]);
439 return *((jdouble*)&tmp);
440 }
441
442 Symbol* symbol_at(int cp_index) const {
443 assert(tag_at(cp_index).is_utf8(), "Corrupted constant pool");
444 return *symbol_at_addr(cp_index);
445 }
446
447 oop string_at(int cp_index, int obj_index, TRAPS) {
448 constantPoolHandle h_this(THREAD, this);
449 return string_at_impl(h_this, cp_index, obj_index, THREAD);
450 }
451 oop string_at(int cp_index, TRAPS) {
452 int obj_index = cp_to_object_index(cp_index);
453 return string_at(cp_index, obj_index, THREAD);
454 }
455
456 // Version that can be used before string oop array is created.
457 oop uncached_string_at(int cp_index, TRAPS);
458
459 // only called when we are sure a string entry is already resolved (via an
460 // earlier string_at call.
461 oop resolved_string_at(int cp_index) {
462 assert(tag_at(cp_index).is_string(), "Corrupted constant pool");
463 // Must do an acquire here in case another thread resolved the klass
464 // behind our back, lest we later load stale values thru the oop.
465 // we might want a volatile_obj_at in ObjArrayKlass.
466 int obj_index = cp_to_object_index(cp_index);
467 return resolved_reference_at(obj_index);
468 }
469
470 Symbol* unresolved_string_at(int cp_index) {
471 assert(tag_at(cp_index).is_string(), "Corrupted constant pool");
472 return *symbol_at_addr(cp_index);
473 }
474
475 // Returns an UTF8 for a CONSTANT_String entry at a given index.
476 // UTF8 char* representation was chosen to avoid conversion of
477 // java_lang_Strings at resolved entries into Symbol*s
478 // or vice versa.
479 char* string_at_noresolve(int cp_index);
480
481 jint name_and_type_at(int cp_index) {
482 assert(tag_at(cp_index).is_name_and_type(), "Corrupted constant pool");
483 return *int_at_addr(cp_index);
484 }
485
486 int method_handle_ref_kind_at(int cp_index) {
487 assert(tag_at(cp_index).is_method_handle() ||
488 tag_at(cp_index).is_method_handle_in_error(), "Corrupted constant pool");
489 return extract_low_short_from_int(*int_at_addr(cp_index)); // mask out unwanted ref_index bits
490 }
491 int method_handle_index_at(int cp_index) {
492 assert(tag_at(cp_index).is_method_handle() ||
493 tag_at(cp_index).is_method_handle_in_error(), "Corrupted constant pool");
494 return extract_high_short_from_int(*int_at_addr(cp_index)); // shift out unwanted ref_kind bits
495 }
496 int method_type_index_at(int cp_index) {
497 assert(tag_at(cp_index).is_method_type() ||
498 tag_at(cp_index).is_method_type_in_error(), "Corrupted constant pool");
499 return *int_at_addr(cp_index);
500 }
501
502 // Derived queries:
503 Symbol* method_handle_name_ref_at(int cp_index) {
504 int member = method_handle_index_at(cp_index);
505 return uncached_name_ref_at(member);
506 }
507 Symbol* method_handle_signature_ref_at(int cp_index) {
508 int member = method_handle_index_at(cp_index);
509 return uncached_signature_ref_at(member);
510 }
511 u2 method_handle_klass_index_at(int cp_index) {
512 int member = method_handle_index_at(cp_index);
513 return uncached_klass_ref_index_at(member);
514 }
515 Symbol* method_type_signature_at(int cp_index) {
516 int sym = method_type_index_at(cp_index);
517 return symbol_at(sym);
518 }
519
520 u2 bootstrap_name_and_type_ref_index_at(int cp_index) {
521 assert(tag_at(cp_index).has_bootstrap(), "Corrupted constant pool");
522 return extract_high_short_from_int(*int_at_addr(cp_index));
523 }
524 u2 bootstrap_methods_attribute_index(int cp_index) {
525 assert(tag_at(cp_index).has_bootstrap(), "Corrupted constant pool");
526 return extract_low_short_from_int(*int_at_addr(cp_index));
527 }
528
529 BSMAttributeEntry* bsm_attribute_entry(int bsms_attribute_index) {
530 return _bsm_entries.entry(bsms_attribute_index);
531 }
532
533 bool compare_bootstrap_entry_to(int bsms_attribute_index1, const constantPoolHandle& cp2,
534 int bsms_attribute_index2);
535 // Find a BSM entry in search_cp that matches the BSM at bsm_attribute_index.
536 // Return -1 if not found.
537 int find_matching_bsm_entry(int bsms_attribute_index, const constantPoolHandle& search_cp,
538 int offset_limit);
539 // Extend the BSM attribute storage to fit both the current data and the BSM data in ext_cp.
540 // Use the returned InsertionIterator to fill out the newly allocated space.
541 BSMAttributeEntries::InsertionIterator start_extension(const constantPoolHandle& ext_cp, TRAPS);
542 void end_extension(BSMAttributeEntries::InsertionIterator iter, TRAPS);
543
544 u2 bootstrap_method_ref_index_at(int cp_index) {
545 assert(tag_at(cp_index).has_bootstrap(), "Corrupted constant pool");
546 int bsmai = bootstrap_methods_attribute_index(cp_index);
547 return bsm_attribute_entry(bsmai)->bootstrap_method_index();
548 }
549 u2 bootstrap_argument_count_at(int cp_index) {
550 assert(tag_at(cp_index).has_bootstrap(), "Corrupted constant pool");
551 int bsmai = bootstrap_methods_attribute_index(cp_index);
552 return bsm_attribute_entry(bsmai)->argument_count();
553 }
554 u2 bootstrap_argument_index_at(int cp_index, int j) {
555 int bsmai = bootstrap_methods_attribute_index(cp_index);
556 BSMAttributeEntry* bsme = bsm_attribute_entry(bsmai);
557 assert((uint)j < (uint)bsme->argument_count(), "oob");
558 return bsm_attribute_entry(bsmai)->argument(j);
559 }
560
561 // The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve,
562 // name_and_type_ref_index_at) all expect to be passed indices obtained
563 // directly from the bytecode.
564 // If the indices are meant to refer to fields or methods, they are
565 // actually rewritten indices that point to entries in their respective structures
566 // i.e. ResolvedMethodEntries or ResolvedFieldEntries.
567 // The routine to_cp_index manages the adjustment
568 // of these values back to constant pool indices.
569
570 // There are also "uncached" versions which do not adjust the operand index; see below.
571
572 // Lookup for entries consisting of (klass_index, name_and_type index)
573 Klass* klass_ref_at(int which, Bytecodes::Code code, TRAPS);
574 Symbol* klass_ref_at_noresolve(int which, Bytecodes::Code code);
575 Symbol* name_ref_at(int which, Bytecodes::Code code) {
576 int name_index = name_ref_index_at(name_and_type_ref_index_at(which, code));
577 return symbol_at(name_index);
578 }
579 Symbol* signature_ref_at(int which, Bytecodes::Code code) {
580 int signature_index = signature_ref_index_at(name_and_type_ref_index_at(which, code));
581 return symbol_at(signature_index);
582 }
583
584 u2 klass_ref_index_at(int which, Bytecodes::Code code);
585 u2 name_and_type_ref_index_at(int which, Bytecodes::Code code);
586
587 constantTag tag_ref_at(int cp_cache_index, Bytecodes::Code code);
588
589 int to_cp_index(int which, Bytecodes::Code code);
590
591 bool is_resolved(int which, Bytecodes::Code code);
592
593 // Lookup for entries consisting of (name_index, signature_index)
594 u2 name_ref_index_at(int cp_index); // == low-order jshort of name_and_type_at(cp_index)
595 u2 signature_ref_index_at(int cp_index); // == high-order jshort of name_and_type_at(cp_index)
596
597 BasicType basic_type_for_signature_at(int cp_index) const;
598
599 // Resolve string constants (to prevent allocation during compilation)
600 void resolve_string_constants(TRAPS) {
601 constantPoolHandle h_this(THREAD, this);
602 resolve_string_constants_impl(h_this, CHECK);
603 }
604
605 #if INCLUDE_CDS
606 // CDS support
607 objArrayOop prepare_resolved_references_for_archiving() NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
608 void remove_unshareable_info();
609 void restore_unshareable_info(TRAPS);
610 private:
611 void remove_unshareable_entries();
612 void remove_resolved_klass_if_non_deterministic(int cp_index);
613 template <typename Function> void iterate_archivable_resolved_references(Function function);
614 #endif
615
616 private:
617 enum { _no_index_sentinel = -1, _possible_index_sentinel = -2 };
618 public:
619
620 // Get the tag for a constant, which may involve a constant dynamic
621 constantTag constant_tag_at(int cp_index);
622 // Get the basic type for a constant, which may involve a constant dynamic
623 BasicType basic_type_for_constant_at(int cp_index);
624
625 // Resolve late bound constants.
626 oop resolve_constant_at(int cp_index, TRAPS) {
627 constantPoolHandle h_this(THREAD, this);
628 return resolve_constant_at_impl(h_this, cp_index, _no_index_sentinel, nullptr, THREAD);
629 }
630
631 oop resolve_cached_constant_at(int cache_index, TRAPS) {
632 constantPoolHandle h_this(THREAD, this);
633 return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, nullptr, THREAD);
634 }
635
636 oop resolve_possibly_cached_constant_at(int cp_index, TRAPS) {
637 constantPoolHandle h_this(THREAD, this);
638 return resolve_constant_at_impl(h_this, cp_index, _possible_index_sentinel, nullptr, THREAD);
639 }
640
641 oop find_cached_constant_at(int cp_index, bool& found_it, TRAPS) {
642 constantPoolHandle h_this(THREAD, this);
643 return resolve_constant_at_impl(h_this, cp_index, _possible_index_sentinel, &found_it, THREAD);
644 }
645
646 void copy_bootstrap_arguments_at(int cp_index,
647 int start_arg, int end_arg,
648 objArrayHandle info, int pos,
649 bool must_resolve, Handle if_not_available, TRAPS) {
650 constantPoolHandle h_this(THREAD, this);
651 copy_bootstrap_arguments_at_impl(h_this, cp_index, start_arg, end_arg,
652 info, pos, must_resolve, if_not_available, THREAD);
653 }
654
655 // Klass name matches name at offset
656 bool klass_name_at_matches(const InstanceKlass* k, int cp_index);
657
658 // Sizing
659 int length() const { return _length; }
660 void set_length(int length) { _length = length; }
661
662 // Tells whether index is within bounds.
663 bool is_within_bounds(int index) const {
664 return 0 <= index && index < length();
665 }
666
667 // Sizing (in words)
668 static int header_size() {
669 return align_up((int)sizeof(ConstantPool), wordSize) / wordSize;
670 }
671 static int size(int length) { return align_metadata_size(header_size() + length); }
672 int size() const { return size(length()); }
673
674 // ConstantPools should be stored in the read-only region of CDS archive.
675 static bool is_read_only_by_default() { return true; }
676
677 friend class ClassFileParser;
678 friend class SystemDictionary;
679
680 // Used by CDS. These classes need to access the private ConstantPool() constructor.
681 template <class T> friend class CppVtableTesterA;
682 template <class T> friend class CppVtableTesterB;
683 template <class T> friend class CppVtableCloner;
684
685 // Used by compiler to prevent classloading.
686 static Method* method_at_if_loaded (const constantPoolHandle& this_cp, int which);
687 static bool has_appendix_at_if_loaded (const constantPoolHandle& this_cp, int which, Bytecodes::Code code);
688 static oop appendix_at_if_loaded (const constantPoolHandle& this_cp, int which, Bytecodes::Code code);
689 static bool has_local_signature_at_if_loaded (const constantPoolHandle& this_cp, int which, Bytecodes::Code code);
690 static Klass* klass_at_if_loaded (const constantPoolHandle& this_cp, int which);
691
692 // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
693 // future by other Java code. These take constant pool indices rather than
694 // constant pool cache indices as do the peer methods above.
695 Symbol* uncached_klass_ref_at_noresolve(int cp_index);
696 Symbol* uncached_name_ref_at(int cp_index) {
697 int name_index = name_ref_index_at(uncached_name_and_type_ref_index_at(cp_index));
698 return symbol_at(name_index);
699 }
700 Symbol* uncached_signature_ref_at(int cp_index) {
701 int signature_index = signature_ref_index_at(uncached_name_and_type_ref_index_at(cp_index));
702 return symbol_at(signature_index);
703 }
704 u2 uncached_klass_ref_index_at(int cp_index);
705 u2 uncached_name_and_type_ref_index_at(int cp_index);
706
707 // Sharing
708 int pre_resolve_shared_klasses(TRAPS);
709
710 // Debugging
711 const char* printable_name_at(int cp_index) PRODUCT_RETURN_NULL;
712
713 private:
714
715 void set_resolved_references(OopHandle s) { _cache->set_resolved_references(s); }
716 Array<u2>* reference_map() const { return (_cache == nullptr) ? nullptr : _cache->reference_map(); }
717 void set_reference_map(Array<u2>* o) { _cache->set_reference_map(o); }
718
719 // Used while constructing constant pool (only by ClassFileParser)
720 jint klass_index_at(int cp_index) {
721 assert(tag_at(cp_index).is_klass_index(), "Corrupted constant pool");
722 return *int_at_addr(cp_index);
723 }
724
725 jint string_index_at(int cp_index) {
726 assert(tag_at(cp_index).is_string_index(), "Corrupted constant pool");
727 return *int_at_addr(cp_index);
728 }
729
730 // Performs the LinkResolver checks
731 static void verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* klass, TRAPS);
732
733 // Implementation of methods that needs an exposed 'this' pointer, in order to
734 // handle GC while executing the method
735 static Klass* klass_at_impl(const constantPoolHandle& this_cp, int cp_index, TRAPS);
736 static oop string_at_impl(const constantPoolHandle& this_cp, int cp_index, int obj_index, TRAPS);
737
738 static void trace_class_resolution(const constantPoolHandle& this_cp, Klass* k);
739
740 // Resolve string constants (to prevent allocation during compilation)
741 static void resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS);
742
743 static oop resolve_constant_at_impl(const constantPoolHandle& this_cp, int cp_index, int cache_index,
744 bool* status_return, TRAPS);
745 static void copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int cp_index,
746 int start_arg, int end_arg,
747 objArrayHandle info, int pos,
748 bool must_resolve, Handle if_not_available, TRAPS);
749
750 // Exception handling
751 static void save_and_throw_exception(const constantPoolHandle& this_cp, int cp_index, constantTag tag, TRAPS);
752
753 public:
754 // Exception handling
755 static void throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS);
756
757 // Merging ConstantPool* support:
758 bool compare_entry_to(int index1, const constantPoolHandle& cp2, int index2);
759 void copy_cp_to(int start_cpi, int end_cpi, const constantPoolHandle& to_cp, int to_cpi, TRAPS) {
760 constantPoolHandle h_this(THREAD, this);
761 copy_cp_to_impl(h_this, start_cpi, end_cpi, to_cp, to_cpi, THREAD);
762 }
763 static void copy_cp_to_impl(const constantPoolHandle& from_cp, int start_cpi, int end_cpi, const constantPoolHandle& to_cp, int to_cpi, TRAPS);
764 static void copy_entry_to(const constantPoolHandle& from_cp, int from_cpi, const constantPoolHandle& to_cp, int to_cpi);
765 static void copy_bsm_entries(const constantPoolHandle& from_cp, const constantPoolHandle& to_cp, TRAPS);
766 int find_matching_entry(int pattern_i, const constantPoolHandle& search_cp);
767 int version() const { return _saved._version; }
768 void set_version(int version) { _saved._version = version; }
769 void increment_and_save_version(int version) {
770 _saved._version = version >= 0 ? (version + 1) : version; // keep overflow
771 }
772
773 void set_resolved_reference_length(int length) { _saved._resolved_reference_length = length; }
774 int resolved_reference_length() const { return _saved._resolved_reference_length; }
775
776 // Decrease ref counts of symbols that are in the constant pool
777 // when the holder class is unloaded
778 void unreference_symbols();
779
780 // Deallocate constant pool for RedefineClasses
781 void deallocate_contents(ClassLoaderData* loader_data);
782 void release_C_heap_structures();
783
784 // JVMTI access - GetConstantPool, RetransformClasses, ...
785 friend class JvmtiConstantPoolReconstituter;
786
787 private:
788 class SymbolHash: public CHeapObj<mtSymbol> {
789 HashTable<const Symbol*, u2, 256, AnyObj::C_HEAP, mtSymbol, Symbol::compute_hash> _table;
790
791 public:
792 void add_if_absent(const Symbol* sym, u2 value) {
793 bool created;
794 _table.put_if_absent(sym, value, &created);
795 }
796
797 u2 symbol_to_value(const Symbol* sym) {
798 u2* value = _table.get(sym);
799 return (value == nullptr) ? 0 : *value;
800 }
801 }; // End SymbolHash class
802
803 jint cpool_entry_size(jint idx);
804 jint hash_entries_to(SymbolHash *symmap, SymbolHash *classmap);
805
806 // Copy cpool bytes into byte array.
807 // Returns:
808 // int > 0, count of the raw cpool bytes that have been copied
809 // 0, OutOfMemory error
810 // -1, Internal error
811 int copy_cpool_bytes(int cpool_size,
812 SymbolHash* tbl,
813 unsigned char *bytes);
814
815 public:
816 // Verify
817 void verify_on(outputStream* st);
818
819 // Printing
820 void print_on(outputStream* st) const;
821 void print_value_on(outputStream* st) const;
822 void print_entry_on(int index, outputStream* st);
823
824 const char* internal_name() const { return "{constant pool}"; }
825
826 // ResolvedFieldEntry getters
827 inline ResolvedFieldEntry* resolved_field_entry_at(int field_index);
828 inline int resolved_field_entries_length() const;
829
830 // ResolvedMethodEntry getters
831 inline ResolvedMethodEntry* resolved_method_entry_at(int method_index);
832 inline int resolved_method_entries_length() const;
833 inline oop appendix_if_resolved(int method_index) const;
834
835 // ResolvedIndyEntry getters
836 inline ResolvedIndyEntry* resolved_indy_entry_at(int index);
837 inline int resolved_indy_entries_length() const;
838 inline oop resolved_reference_from_indy(int index) const;
839 inline oop resolved_reference_from_method(int index) const;
840 };
841
842 #endif // SHARE_OOPS_CONSTANTPOOL_HPP