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