1 /*
2  * Copyright (c) 1997, 2019, 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_INSTANCEKLASS_HPP
26 #define SHARE_OOPS_INSTANCEKLASS_HPP
27 
28 #include "classfile/classLoaderData.hpp"
29 #include "memory/referenceType.hpp"
30 #include "oops/annotations.hpp"
31 #include "oops/constMethod.hpp"
32 #include "oops/fieldInfo.hpp"
33 #include "oops/instanceOop.hpp"
34 #include "oops/klassVtable.hpp"
35 #include "runtime/handles.hpp"
36 #include "runtime/os.hpp"
37 #include "utilities/accessFlags.hpp"
38 #include "utilities/align.hpp"
39 #include "utilities/macros.hpp"
40 #if INCLUDE_JFR
41 #include "jfr/support/jfrKlassExtension.hpp"
42 #endif
43 
44 class RecordComponent;
45 
46 // An InstanceKlass is the VM level representation of a Java class.
47 // It contains all information needed for at class at execution runtime.
48 
49 //  InstanceKlass embedded field layout (after declared fields):
50 //    [EMBEDDED Java vtable             ] size in words = vtable_len
51 //    [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size
52 //      The embedded nonstatic oop-map blocks are short pairs (offset, length)
53 //      indicating where oops are located in instances of this klass.
54 //    [EMBEDDED implementor of the interface] only exist for interface
55 //    [EMBEDDED unsafe_anonymous_host klass] only exist for an unsafe anonymous class (JSR 292 enabled)
56 //    [EMBEDDED fingerprint       ] only if should_store_fingerprint()==true
57 
58 
59 // forward declaration for class -- see below for definition
60 #if INCLUDE_JVMTI
61 class BreakpointInfo;
62 #endif
63 class ClassFileParser;
64 class ClassFileStream;
65 class KlassDepChange;
66 class DependencyContext;
67 class fieldDescriptor;
68 class jniIdMapBase;
69 class JNIid;
70 class JvmtiCachedClassFieldMap;
71 class nmethodBucket;
72 class OopMapCache;
73 class InterpreterOopMap;
74 class PackageEntry;
75 class ModuleEntry;
76 
77 // This is used in iterators below.
78 class FieldClosure: public StackObj {
79 public:
80   virtual void do_field(fieldDescriptor* fd) = 0;
81 };
82 
83 #ifndef PRODUCT
84 // Print fields.
85 // If "obj" argument to constructor is NULL, prints static fields, otherwise prints non-static fields.
86 class FieldPrinter: public FieldClosure {
87    oop _obj;
88    outputStream* _st;
89  public:
FieldPrinter(outputStream * st,oop obj=NULL)90    FieldPrinter(outputStream* st, oop obj = NULL) : _obj(obj), _st(st) {}
91    void do_field(fieldDescriptor* fd);
92 };
93 #endif  // !PRODUCT
94 
95 // Describes where oops are located in instances of this klass.
96 class OopMapBlock {
97  public:
98   // Byte offset of the first oop mapped by this block.
offset() const99   int offset() const          { return _offset; }
set_offset(int offset)100   void set_offset(int offset) { _offset = offset; }
101 
102   // Number of oops in this block.
count() const103   uint count() const         { return _count; }
set_count(uint count)104   void set_count(uint count) { _count = count; }
105 
106   // sizeof(OopMapBlock) in words.
size_in_words()107   static const int size_in_words() {
108     return align_up((int)sizeof(OopMapBlock), wordSize) >>
109       LogBytesPerWord;
110   }
111 
112  private:
113   int  _offset;
114   uint _count;
115 };
116 
117 struct JvmtiCachedClassFileData;
118 
119 class InstanceKlass: public Klass {
120   friend class VMStructs;
121   friend class JVMCIVMStructs;
122   friend class ClassFileParser;
123   friend class CompileReplay;
124 
125  public:
126   static const KlassID ID = InstanceKlassID;
127 
128  protected:
129   InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id = ID);
130 
131  public:
InstanceKlass()132   InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
133 
134   // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description
135   // of the class loading & initialization procedure, and the use of the states.
136   enum ClassState {
137     allocated,                          // allocated (but not yet linked)
138     loaded,                             // loaded and inserted in class hierarchy (but not linked yet)
139     linked,                             // successfully linked/verified (but not initialized yet)
140     being_initialized,                  // currently running class initializer
141     fully_initialized,                  // initialized (successfull final state)
142     initialization_error                // error happened during initialization
143   };
144 
145  private:
146   static InstanceKlass* allocate_instance_klass(const ClassFileParser& parser, TRAPS);
147 
148  protected:
149   // If you add a new field that points to any metaspace object, you
150   // must add this field to InstanceKlass::metaspace_pointers_do().
151 
152   // Annotations for this class
153   Annotations*    _annotations;
154   // Package this class is defined in
155   PackageEntry*   _package_entry;
156   // Array classes holding elements of this class.
157   Klass* volatile _array_klasses;
158   // Constant pool for this class.
159   ConstantPool* _constants;
160   // The InnerClasses attribute and EnclosingMethod attribute. The
161   // _inner_classes is an array of shorts. If the class has InnerClasses
162   // attribute, then the _inner_classes array begins with 4-tuples of shorts
163   // [inner_class_info_index, outer_class_info_index,
164   // inner_name_index, inner_class_access_flags] for the InnerClasses
165   // attribute. If the EnclosingMethod attribute exists, it occupies the
166   // last two shorts [class_index, method_index] of the array. If only
167   // the InnerClasses attribute exists, the _inner_classes array length is
168   // number_of_inner_classes * 4. If the class has both InnerClasses
169   // and EnclosingMethod attributes the _inner_classes array length is
170   // number_of_inner_classes * 4 + enclosing_method_attribute_size.
171   Array<jushort>* _inner_classes;
172 
173   // The NestMembers attribute. An array of shorts, where each is a
174   // class info index for the class that is a nest member. This data
175   // has not been validated.
176   Array<jushort>* _nest_members;
177 
178   // The NestHost attribute. The class info index for the class
179   // that is the nest-host of this class. This data has not been validated.
180   jushort _nest_host_index;
181 
182   // Resolved nest-host klass: either true nest-host or self if we are not nested.
183   // By always being set it makes nest-member access checks simpler.
184   InstanceKlass* _nest_host;
185 
186   // The contents of the Record attribute.
187   Array<RecordComponent*>* _record_components;
188 
189   // the source debug extension for this klass, NULL if not specified.
190   // Specified as UTF-8 string without terminating zero byte in the classfile,
191   // it is stored in the instanceklass as a NULL-terminated UTF-8 string
192   const char*     _source_debug_extension;
193   // Array name derived from this class which needs unreferencing
194   // if this class is unloaded.
195   Symbol*         _array_name;
196 
197   // Number of heapOopSize words used by non-static fields in this klass
198   // (including inherited fields but after header_size()).
199   int             _nonstatic_field_size;
200   int             _static_field_size;    // number words used by static fields (oop and non-oop) in this klass
201   // Constant pool index to the utf8 entry of the Generic signature,
202   // or 0 if none.
203   u2              _generic_signature_index;
204   // Constant pool index to the utf8 entry for the name of source file
205   // containing this klass, 0 if not specified.
206   u2              _source_file_name_index;
207   u2              _static_oop_field_count;// number of static oop fields in this klass
208   u2              _java_fields_count;    // The number of declared Java fields
209   int             _nonstatic_oop_map_size;// size in words of nonstatic oop map blocks
210 
211   int             _itable_len;           // length of Java itable (in words)
212   // _is_marked_dependent can be set concurrently, thus cannot be part of the
213   // _misc_flags.
214   bool            _is_marked_dependent;  // used for marking during flushing and deoptimization
215   bool            _is_being_redefined;   // used for locking redefinition
216 
217   // The low two bits of _misc_flags contains the kind field.
218   // This can be used to quickly discriminate among the four kinds of
219   // InstanceKlass.
220 
221   static const unsigned _misc_kind_field_size = 2;
222   static const unsigned _misc_kind_field_pos  = 0;
223   static const unsigned _misc_kind_field_mask = (1u << _misc_kind_field_size) - 1u;
224 
225   static const unsigned _misc_kind_other        = 0; // concrete InstanceKlass
226   static const unsigned _misc_kind_reference    = 1; // InstanceRefKlass
227   static const unsigned _misc_kind_class_loader = 2; // InstanceClassLoaderKlass
228   static const unsigned _misc_kind_mirror       = 3; // InstanceMirrorKlass
229 
230   // Start after _misc_kind field.
231   enum {
232     _misc_rewritten                           = 1 << 2,  // methods rewritten.
233     _misc_has_nonstatic_fields                = 1 << 3,  // for sizing with UseCompressedOops
234     _misc_should_verify_class                 = 1 << 4,  // allow caching of preverification
235     _misc_is_unsafe_anonymous                 = 1 << 5,  // has embedded _unsafe_anonymous_host field
236     _misc_is_contended                        = 1 << 6,  // marked with contended annotation
237     _misc_has_nonstatic_concrete_methods      = 1 << 7,  // class/superclass/implemented interfaces has non-static, concrete methods
238     _misc_declares_nonstatic_concrete_methods = 1 << 8,  // directly declares non-static, concrete methods
239     _misc_has_been_redefined                  = 1 << 9,  // class has been redefined
240     _misc_has_passed_fingerprint_check        = 1 << 10, // when this class was loaded, the fingerprint computed from its
241                                                          // code source was found to be matching the value recorded by AOT.
242     _misc_is_scratch_class                    = 1 << 11, // class is the redefined scratch class
243     _misc_is_shared_boot_class                = 1 << 12, // defining class loader is boot class loader
244     _misc_is_shared_platform_class            = 1 << 13, // defining class loader is platform class loader
245     _misc_is_shared_app_class                 = 1 << 14, // defining class loader is app class loader
246     _misc_has_resolved_methods                = 1 << 15  // resolved methods table entries added for this class
247   };
loader_type_bits()248   u2 loader_type_bits() {
249     return _misc_is_shared_boot_class|_misc_is_shared_platform_class|_misc_is_shared_app_class;
250   }
251   u2              _misc_flags;
252   u2              _minor_version;        // minor version number of class file
253   u2              _major_version;        // major version number of class file
254   Thread*         _init_thread;          // Pointer to current thread doing initialization (to handle recursive initialization)
255   OopMapCache*    volatile _oop_map_cache;   // OopMapCache for all methods in the klass (allocated lazily)
256   JNIid*          _jni_ids;              // First JNI identifier for static fields in this class
257   jmethodID*      volatile _methods_jmethod_ids;  // jmethodIDs corresponding to method_idnum, or NULL if none
258   nmethodBucket*  volatile _dep_context;          // packed DependencyContext structure
259   uint64_t        volatile _dep_context_last_cleaned;
260   nmethod*        _osr_nmethods_head;    // Head of list of on-stack replacement nmethods for this class
261 #if INCLUDE_JVMTI
262   BreakpointInfo* _breakpoints;          // bpt lists, managed by Method*
263   // Linked instanceKlasses of previous versions
264   InstanceKlass* _previous_versions;
265   // JVMTI fields can be moved to their own structure - see 6315920
266   // JVMTI: cached class file, before retransformable agent modified it in CFLH
267   JvmtiCachedClassFileData* _cached_class_file;
268 #endif
269 
270   volatile u2     _idnum_allocated_count;         // JNI/JVMTI: increments with the addition of methods, old ids don't change
271 
272   // Class states are defined as ClassState (see above).
273   // Place the _init_state here to utilize the unused 2-byte after
274   // _idnum_allocated_count.
275   u1              _init_state;                    // state of class
276   u1              _reference_type;                // reference type
277 
278   u2              _this_class_index;              // constant pool entry
279 #if INCLUDE_JVMTI
280   JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map;  // JVMTI: used during heap iteration
281 #endif
282 
283   NOT_PRODUCT(int _verify_count;)  // to avoid redundant verifies
284 
285   // Method array.
286   Array<Method*>* _methods;
287   // Default Method Array, concrete methods inherited from interfaces
288   Array<Method*>* _default_methods;
289   // Interfaces (InstanceKlass*s) this class declares locally to implement.
290   Array<InstanceKlass*>* _local_interfaces;
291   // Interfaces (InstanceKlass*s) this class implements transitively.
292   Array<InstanceKlass*>* _transitive_interfaces;
293   // Int array containing the original order of method in the class file (for JVMTI).
294   Array<int>*     _method_ordering;
295   // Int array containing the vtable_indices for default_methods
296   // offset matches _default_methods offset
297   Array<int>*     _default_vtable_indices;
298 
299   // Instance and static variable information, starts with 6-tuples of shorts
300   // [access, name index, sig index, initval index, low_offset, high_offset]
301   // for all fields, followed by the generic signature data at the end of
302   // the array. Only fields with generic signature attributes have the generic
303   // signature data set in the array. The fields array looks like following:
304   //
305   // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
306   // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
307   //      ...
308   // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
309   //     [generic signature index]
310   //     [generic signature index]
311   //     ...
312   Array<u2>*      _fields;
313 
314   // embedded Java vtable follows here
315   // embedded Java itables follows here
316   // embedded static fields follows here
317   // embedded nonstatic oop-map blocks follows here
318   // embedded implementor of this interface follows here
319   //   The embedded implementor only exists if the current klass is an
320   //   iterface. The possible values of the implementor fall into following
321   //   three cases:
322   //     NULL: no implementor.
323   //     A Klass* that's not itself: one implementor.
324   //     Itself: more than one implementors.
325   // embedded unsafe_anonymous_host klass follows here
326   //   The embedded host klass only exists in an unsafe anonymous class for
327   //   dynamic language support (JSR 292 enabled). The host class grants
328   //   its access privileges to this class also. The host class is either
329   //   named, or a previously loaded unsafe anonymous class. A non-anonymous class
330   //   or an anonymous class loaded through normal classloading does not
331   //   have this embedded field.
332   //
333 
334   friend class SystemDictionary;
335 
336   static bool _disable_method_binary_search;
337 
338  public:
loader_type()339   u2 loader_type() {
340     return _misc_flags & loader_type_bits();
341   }
342 
is_shared_boot_class() const343   bool is_shared_boot_class() const {
344     return (_misc_flags & _misc_is_shared_boot_class) != 0;
345   }
is_shared_platform_class() const346   bool is_shared_platform_class() const {
347     return (_misc_flags & _misc_is_shared_platform_class) != 0;
348   }
is_shared_app_class() const349   bool is_shared_app_class() const {
350     return (_misc_flags & _misc_is_shared_app_class) != 0;
351   }
352 
clear_class_loader_type()353   void clear_class_loader_type() {
354     _misc_flags &= ~loader_type_bits();
355   }
356 
357   void set_class_loader_type(s2 loader_type);
358 
has_nonstatic_fields() const359   bool has_nonstatic_fields() const        {
360     return (_misc_flags & _misc_has_nonstatic_fields) != 0;
361   }
set_has_nonstatic_fields(bool b)362   void set_has_nonstatic_fields(bool b)    {
363     if (b) {
364       _misc_flags |= _misc_has_nonstatic_fields;
365     } else {
366       _misc_flags &= ~_misc_has_nonstatic_fields;
367     }
368   }
369 
370   // field sizes
nonstatic_field_size() const371   int nonstatic_field_size() const         { return _nonstatic_field_size; }
set_nonstatic_field_size(int size)372   void set_nonstatic_field_size(int size)  { _nonstatic_field_size = size; }
373 
static_field_size() const374   int static_field_size() const            { return _static_field_size; }
set_static_field_size(int size)375   void set_static_field_size(int size)     { _static_field_size = size; }
376 
static_oop_field_count() const377   int static_oop_field_count() const       { return (int)_static_oop_field_count; }
set_static_oop_field_count(u2 size)378   void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
379 
380   // Java itable
itable_length() const381   int  itable_length() const               { return _itable_len; }
set_itable_length(int len)382   void set_itable_length(int len)          { _itable_len = len; }
383 
384   // array klasses
array_klasses() const385   Klass* array_klasses() const             { return _array_klasses; }
386   inline Klass* array_klasses_acquire() const; // load with acquire semantics
set_array_klasses(Klass * k)387   void set_array_klasses(Klass* k)         { _array_klasses = k; }
388   inline void release_set_array_klasses(Klass* k); // store with release semantics
389 
390   // methods
methods() const391   Array<Method*>* methods() const          { return _methods; }
set_methods(Array<Method * > * a)392   void set_methods(Array<Method*>* a)      { _methods = a; }
393   Method* method_with_idnum(int idnum);
394   Method* method_with_orig_idnum(int idnum);
395   Method* method_with_orig_idnum(int idnum, int version);
396 
397   // method ordering
method_ordering() const398   Array<int>* method_ordering() const     { return _method_ordering; }
set_method_ordering(Array<int> * m)399   void set_method_ordering(Array<int>* m) { _method_ordering = m; }
400   void copy_method_ordering(const intArray* m, TRAPS);
401 
402   // default_methods
default_methods() const403   Array<Method*>* default_methods() const  { return _default_methods; }
set_default_methods(Array<Method * > * a)404   void set_default_methods(Array<Method*>* a) { _default_methods = a; }
405 
406   // default method vtable_indices
default_vtable_indices() const407   Array<int>* default_vtable_indices() const { return _default_vtable_indices; }
set_default_vtable_indices(Array<int> * v)408   void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; }
409   Array<int>* create_new_default_vtable_indices(int len, TRAPS);
410 
411   // interfaces
local_interfaces() const412   Array<InstanceKlass*>* local_interfaces() const          { return _local_interfaces; }
set_local_interfaces(Array<InstanceKlass * > * a)413   void set_local_interfaces(Array<InstanceKlass*>* a)      {
414     guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
415     _local_interfaces = a; }
416 
transitive_interfaces() const417   Array<InstanceKlass*>* transitive_interfaces() const     { return _transitive_interfaces; }
set_transitive_interfaces(Array<InstanceKlass * > * a)418   void set_transitive_interfaces(Array<InstanceKlass*>* a) {
419     guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
420     _transitive_interfaces = a;
421   }
422 
423  private:
424   friend class fieldDescriptor;
field(int index) const425   FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
426 
427  public:
field_offset(int index) const428   int     field_offset      (int index) const { return field(index)->offset(); }
field_access_flags(int index) const429   int     field_access_flags(int index) const { return field(index)->access_flags(); }
field_name(int index) const430   Symbol* field_name        (int index) const { return field(index)->name(constants()); }
field_signature(int index) const431   Symbol* field_signature   (int index) const { return field(index)->signature(constants()); }
432 
433   // Number of Java declared fields
java_fields_count() const434   int java_fields_count() const           { return (int)_java_fields_count; }
435 
fields() const436   Array<u2>* fields() const            { return _fields; }
set_fields(Array<u2> * f,u2 java_fields_count)437   void set_fields(Array<u2>* f, u2 java_fields_count) {
438     guarantee(_fields == NULL || f == NULL, "Just checking");
439     _fields = f;
440     _java_fields_count = java_fields_count;
441   }
442 
443   // inner classes
inner_classes() const444   Array<u2>* inner_classes() const       { return _inner_classes; }
set_inner_classes(Array<u2> * f)445   void set_inner_classes(Array<u2>* f)   { _inner_classes = f; }
446 
447   // nest members
nest_members() const448   Array<u2>* nest_members() const     { return _nest_members; }
set_nest_members(Array<u2> * m)449   void set_nest_members(Array<u2>* m) { _nest_members = m; }
450 
451   // nest-host index
nest_host_index() const452   jushort nest_host_index() const { return _nest_host_index; }
set_nest_host_index(u2 i)453   void set_nest_host_index(u2 i)  { _nest_host_index = i; }
454 
455   // record components
record_components() const456   Array<RecordComponent*>* record_components() const { return _record_components; }
set_record_components(Array<RecordComponent * > * record_components)457   void set_record_components(Array<RecordComponent*>* record_components) {
458     _record_components = record_components;
459   }
is_record() const460   bool is_record() const { return _record_components != NULL; }
461 
462 private:
463   // Called to verify that k is a member of this nest - does not look at k's nest-host
464   bool has_nest_member(InstanceKlass* k, TRAPS) const;
465 
466 public:
467   // Returns nest-host class, resolving and validating it if needed
468   // Returns NULL if an exception occurs during loading, or validation fails
469   InstanceKlass* nest_host(Symbol* validationException, TRAPS);
470   // Check if this klass is a nestmate of k - resolves this nest-host and k's
471   bool has_nestmate_access_to(InstanceKlass* k, TRAPS);
472 
473   enum InnerClassAttributeOffset {
474     // From http://mirror.eng/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc10.html#18814
475     inner_class_inner_class_info_offset = 0,
476     inner_class_outer_class_info_offset = 1,
477     inner_class_inner_name_offset = 2,
478     inner_class_access_flags_offset = 3,
479     inner_class_next_offset = 4
480   };
481 
482   enum EnclosingMethodAttributeOffset {
483     enclosing_method_class_index_offset = 0,
484     enclosing_method_method_index_offset = 1,
485     enclosing_method_attribute_size = 2
486   };
487 
488   // method override check
489   bool is_override(const methodHandle& super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS);
490 
491   // package
package() const492   PackageEntry* package() const     { return _package_entry; }
493   ModuleEntry* module() const;
in_unnamed_package() const494   bool in_unnamed_package() const   { return (_package_entry == NULL); }
set_package(PackageEntry * p)495   void set_package(PackageEntry* p) { _package_entry = p; }
496   void set_package(ClassLoaderData* loader_data, TRAPS);
497   bool is_same_class_package(const Klass* class2) const;
498   bool is_same_class_package(oop other_class_loader, const Symbol* other_class_name) const;
499 
500   // find an enclosing class
501   InstanceKlass* compute_enclosing_class(bool* inner_is_member, TRAPS) const;
502 
503   // Find InnerClasses attribute and return outer_class_info_index & inner_name_index.
504   bool find_inner_classes_attr(int* ooff, int* noff, TRAPS) const;
505 
506  private:
507   // Check prohibited package ("java/" only loadable by boot or platform loaders)
508   static void check_prohibited_package(Symbol* class_name,
509                                        ClassLoaderData* loader_data,
510                                        TRAPS);
511  public:
512   // initialization state
is_loaded() const513   bool is_loaded() const                   { return _init_state >= loaded; }
is_linked() const514   bool is_linked() const                   { return _init_state >= linked; }
is_initialized() const515   bool is_initialized() const              { return _init_state == fully_initialized; }
is_not_initialized() const516   bool is_not_initialized() const          { return _init_state <  being_initialized; }
is_being_initialized() const517   bool is_being_initialized() const        { return _init_state == being_initialized; }
is_in_error_state() const518   bool is_in_error_state() const           { return _init_state == initialization_error; }
is_reentrant_initialization(Thread * thread)519   bool is_reentrant_initialization(Thread *thread)  { return thread == _init_thread; }
init_state()520   ClassState  init_state()                 { return (ClassState)_init_state; }
is_rewritten() const521   bool is_rewritten() const                { return (_misc_flags & _misc_rewritten) != 0; }
522 
523   // defineClass specified verification
should_verify_class() const524   bool should_verify_class() const         {
525     return (_misc_flags & _misc_should_verify_class) != 0;
526   }
set_should_verify_class(bool value)527   void set_should_verify_class(bool value) {
528     if (value) {
529       _misc_flags |= _misc_should_verify_class;
530     } else {
531       _misc_flags &= ~_misc_should_verify_class;
532     }
533   }
534 
535   // marking
is_marked_dependent() const536   bool is_marked_dependent() const         { return _is_marked_dependent; }
set_is_marked_dependent(bool value)537   void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
538 
539   // initialization (virtuals from Klass)
540   bool should_be_initialized() const;  // means that initialize should be called
541   void initialize(TRAPS);
542   void link_class(TRAPS);
543   bool link_class_or_fail(TRAPS); // returns false on failure
544   void rewrite_class(TRAPS);
545   void link_methods(TRAPS);
546   Method* class_initializer() const;
547 
548   // set the class to initialized if no static initializer is present
549   void eager_initialize(Thread *thread);
550 
551   // reference type
reference_type() const552   ReferenceType reference_type() const     { return (ReferenceType)_reference_type; }
set_reference_type(ReferenceType t)553   void set_reference_type(ReferenceType t) {
554     assert(t == (u1)t, "overflow");
555     _reference_type = (u1)t;
556   }
557 
558   // this class cp index
this_class_index() const559   u2 this_class_index() const             { return _this_class_index; }
set_this_class_index(u2 index)560   void set_this_class_index(u2 index)     { _this_class_index = index; }
561 
reference_type_offset()562   static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
563 
564   // find local field, returns true if found
565   bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
566   // find field in direct superinterfaces, returns the interface in which the field is defined
567   Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
568   // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
569   Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
570   // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined
571   Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const;
572 
573   // find a non-static or static field given its offset within the class.
contains_field_offset(int offset)574   bool contains_field_offset(int offset) {
575     return instanceOopDesc::contains_field_offset(offset, nonstatic_field_size());
576   }
577 
578   bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
579   bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
580 
581  private:
582   inline static int quick_search(const Array<Method*>* methods, const Symbol* name);
583 
584  public:
disable_method_binary_search()585   static void disable_method_binary_search() {
586     _disable_method_binary_search = true;
587   }
588 
589   // find a local method (returns NULL if not found)
590   Method* find_method(const Symbol* name, const Symbol* signature) const;
591   static Method* find_method(const Array<Method*>* methods,
592                              const Symbol* name,
593                              const Symbol* signature);
594 
595   // find a local method, but skip static methods
596   Method* find_instance_method(const Symbol* name, const Symbol* signature,
597                                PrivateLookupMode private_mode) const;
598   static Method* find_instance_method(const Array<Method*>* methods,
599                                       const Symbol* name,
600                                       const Symbol* signature,
601                                       PrivateLookupMode private_mode);
602 
603   // find a local method (returns NULL if not found)
604   Method* find_local_method(const Symbol* name,
605                             const Symbol* signature,
606                             OverpassLookupMode overpass_mode,
607                             StaticLookupMode static_mode,
608                             PrivateLookupMode private_mode) const;
609 
610   // find a local method from given methods array (returns NULL if not found)
611   static Method* find_local_method(const Array<Method*>* methods,
612                                    const Symbol* name,
613                                    const Symbol* signature,
614                                    OverpassLookupMode overpass_mode,
615                                    StaticLookupMode static_mode,
616                                    PrivateLookupMode private_mode);
617 
618   // find a local method index in methods or default_methods (returns -1 if not found)
619   static int find_method_index(const Array<Method*>* methods,
620                                const Symbol* name,
621                                const Symbol* signature,
622                                OverpassLookupMode overpass_mode,
623                                StaticLookupMode static_mode,
624                                PrivateLookupMode private_mode);
625 
626   // lookup operation (returns NULL if not found)
627   Method* uncached_lookup_method(const Symbol* name,
628                                  const Symbol* signature,
629                                  OverpassLookupMode overpass_mode,
630                                  PrivateLookupMode private_mode = find_private) const;
631 
632   // lookup a method in all the interfaces that this class implements
633   // (returns NULL if not found)
634   Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature, DefaultsLookupMode defaults_mode) const;
635 
636   // lookup a method in local defaults then in all interfaces
637   // (returns NULL if not found)
638   Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const;
639 
640   // Find method indices by name.  If a method with the specified name is
641   // found the index to the first method is returned, and 'end' is filled in
642   // with the index of first non-name-matching method.  If no method is found
643   // -1 is returned.
644   int find_method_by_name(const Symbol* name, int* end) const;
645   static int find_method_by_name(const Array<Method*>* methods,
646                                  const Symbol* name, int* end);
647 
648   // constant pool
constants() const649   ConstantPool* constants() const        { return _constants; }
set_constants(ConstantPool * c)650   void set_constants(ConstantPool* c)    { _constants = c; }
651 
652   // protection domain
653   oop protection_domain() const;
654 
655   // signers
656   objArrayOop signers() const;
657 
658   // host class
unsafe_anonymous_host() const659   InstanceKlass* unsafe_anonymous_host() const {
660     InstanceKlass** hk = adr_unsafe_anonymous_host();
661     if (hk == NULL) {
662       assert(!is_unsafe_anonymous(), "Unsafe anonymous classes have host klasses");
663       return NULL;
664     } else {
665       assert(*hk != NULL, "host klass should always be set if the address is not null");
666       assert(is_unsafe_anonymous(), "Only unsafe anonymous classes have host klasses");
667       return *hk;
668     }
669   }
set_unsafe_anonymous_host(const InstanceKlass * host)670   void set_unsafe_anonymous_host(const InstanceKlass* host) {
671     assert(is_unsafe_anonymous(), "not unsafe anonymous");
672     const InstanceKlass** addr = (const InstanceKlass **)adr_unsafe_anonymous_host();
673     assert(addr != NULL, "no reversed space");
674     if (addr != NULL) {
675       *addr = host;
676     }
677   }
is_unsafe_anonymous() const678   bool is_unsafe_anonymous() const                {
679     return (_misc_flags & _misc_is_unsafe_anonymous) != 0;
680   }
set_is_unsafe_anonymous(bool value)681   void set_is_unsafe_anonymous(bool value)        {
682     if (value) {
683       _misc_flags |= _misc_is_unsafe_anonymous;
684     } else {
685       _misc_flags &= ~_misc_is_unsafe_anonymous;
686     }
687   }
688 
is_contended() const689   bool is_contended() const                {
690     return (_misc_flags & _misc_is_contended) != 0;
691   }
set_is_contended(bool value)692   void set_is_contended(bool value)        {
693     if (value) {
694       _misc_flags |= _misc_is_contended;
695     } else {
696       _misc_flags &= ~_misc_is_contended;
697     }
698   }
699 
700   // source file name
source_file_name() const701   Symbol* source_file_name() const               {
702     return (_source_file_name_index == 0) ?
703       (Symbol*)NULL : _constants->symbol_at(_source_file_name_index);
704   }
source_file_name_index() const705   u2 source_file_name_index() const              {
706     return _source_file_name_index;
707   }
set_source_file_name_index(u2 sourcefile_index)708   void set_source_file_name_index(u2 sourcefile_index) {
709     _source_file_name_index = sourcefile_index;
710   }
711 
712   // minor and major version numbers of class file
minor_version() const713   u2 minor_version() const                 { return _minor_version; }
set_minor_version(u2 minor_version)714   void set_minor_version(u2 minor_version) { _minor_version = minor_version; }
major_version() const715   u2 major_version() const                 { return _major_version; }
set_major_version(u2 major_version)716   void set_major_version(u2 major_version) { _major_version = major_version; }
717 
718   // source debug extension
source_debug_extension() const719   const char* source_debug_extension() const { return _source_debug_extension; }
720   void set_source_debug_extension(const char* array, int length);
721 
722   // symbol unloading support (refcount already added)
array_name()723   Symbol* array_name()                     { return _array_name; }
set_array_name(Symbol * name)724   void set_array_name(Symbol* name)        { assert(_array_name == NULL  || name == NULL, "name already created"); _array_name = name; }
725 
726   // nonstatic oop-map blocks
nonstatic_oop_map_size(unsigned int oop_map_count)727   static int nonstatic_oop_map_size(unsigned int oop_map_count) {
728     return oop_map_count * OopMapBlock::size_in_words();
729   }
nonstatic_oop_map_count() const730   unsigned int nonstatic_oop_map_count() const {
731     return _nonstatic_oop_map_size / OopMapBlock::size_in_words();
732   }
nonstatic_oop_map_size() const733   int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; }
set_nonstatic_oop_map_size(int words)734   void set_nonstatic_oop_map_size(int words) {
735     _nonstatic_oop_map_size = words;
736   }
737 
738 #if INCLUDE_JVMTI
739   // Redefinition locking.  Class can only be redefined by one thread at a time.
is_being_redefined() const740   bool is_being_redefined() const          { return _is_being_redefined; }
set_is_being_redefined(bool value)741   void set_is_being_redefined(bool value)  { _is_being_redefined = value; }
742 
743   // RedefineClasses() support for previous versions:
744   void add_previous_version(InstanceKlass* ik, int emcp_method_count);
745   void purge_previous_version_list();
746 
previous_versions() const747   InstanceKlass* previous_versions() const { return _previous_versions; }
748 #else
previous_versions() const749   InstanceKlass* previous_versions() const { return NULL; }
750 #endif
751 
get_klass_version(int version)752   InstanceKlass* get_klass_version(int version) {
753     for (InstanceKlass* ik = this; ik != NULL; ik = ik->previous_versions()) {
754       if (ik->constants()->version() == version) {
755         return ik;
756       }
757     }
758     return NULL;
759   }
760 
has_been_redefined() const761   bool has_been_redefined() const {
762     return (_misc_flags & _misc_has_been_redefined) != 0;
763   }
set_has_been_redefined()764   void set_has_been_redefined() {
765     _misc_flags |= _misc_has_been_redefined;
766   }
767 
has_passed_fingerprint_check() const768   bool has_passed_fingerprint_check() const {
769     return (_misc_flags & _misc_has_passed_fingerprint_check) != 0;
770   }
set_has_passed_fingerprint_check(bool b)771   void set_has_passed_fingerprint_check(bool b) {
772     if (b) {
773       _misc_flags |= _misc_has_passed_fingerprint_check;
774     } else {
775       _misc_flags &= ~_misc_has_passed_fingerprint_check;
776     }
777   }
778   bool supers_have_passed_fingerprint_checks();
779 
780   static bool should_store_fingerprint(bool is_unsafe_anonymous);
should_store_fingerprint() const781   bool should_store_fingerprint() const { return should_store_fingerprint(is_unsafe_anonymous()); }
782   bool has_stored_fingerprint() const;
783   uint64_t get_stored_fingerprint() const;
784   void store_fingerprint(uint64_t fingerprint);
785 
is_scratch_class() const786   bool is_scratch_class() const {
787     return (_misc_flags & _misc_is_scratch_class) != 0;
788   }
789 
set_is_scratch_class()790   void set_is_scratch_class() {
791     _misc_flags |= _misc_is_scratch_class;
792   }
793 
has_resolved_methods() const794   bool has_resolved_methods() const {
795     return (_misc_flags & _misc_has_resolved_methods) != 0;
796   }
797 
set_has_resolved_methods()798   void set_has_resolved_methods() {
799     _misc_flags |= _misc_has_resolved_methods;
800   }
801 private:
802 
set_kind(unsigned kind)803   void set_kind(unsigned kind) {
804     assert(kind <= _misc_kind_field_mask, "Invalid InstanceKlass kind");
805     unsigned fmask = _misc_kind_field_mask << _misc_kind_field_pos;
806     unsigned flags = _misc_flags & ~fmask;
807     _misc_flags = (flags | (kind << _misc_kind_field_pos));
808   }
809 
is_kind(unsigned desired) const810   bool is_kind(unsigned desired) const {
811     unsigned kind = (_misc_flags >> _misc_kind_field_pos) & _misc_kind_field_mask;
812     return kind == desired;
813   }
814 
815 public:
816 
817   // Other is anything that is not one of the more specialized kinds of InstanceKlass.
is_other_instance_klass() const818   bool is_other_instance_klass() const        { return is_kind(_misc_kind_other); }
is_reference_instance_klass() const819   bool is_reference_instance_klass() const    { return is_kind(_misc_kind_reference); }
is_mirror_instance_klass() const820   bool is_mirror_instance_klass() const       { return is_kind(_misc_kind_mirror); }
is_class_loader_instance_klass() const821   bool is_class_loader_instance_klass() const { return is_kind(_misc_kind_class_loader); }
822 
823 #if INCLUDE_JVMTI
824 
init_previous_versions()825   void init_previous_versions() {
826     _previous_versions = NULL;
827   }
828 
829  private:
830   static bool  _has_previous_versions;
831  public:
purge_previous_versions(InstanceKlass * ik)832   static void purge_previous_versions(InstanceKlass* ik) {
833     if (ik->has_been_redefined()) {
834       ik->purge_previous_version_list();
835     }
836   }
837 
838   static bool has_previous_versions_and_reset();
has_previous_versions()839   static bool has_previous_versions() { return _has_previous_versions; }
840 
841   // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation
set_cached_class_file(JvmtiCachedClassFileData * data)842   void set_cached_class_file(JvmtiCachedClassFileData *data) {
843     _cached_class_file = data;
844   }
845   JvmtiCachedClassFileData * get_cached_class_file();
846   jint get_cached_class_file_len();
847   unsigned char * get_cached_class_file_bytes();
848 
849   // JVMTI: Support for caching of field indices, types, and offsets
set_jvmti_cached_class_field_map(JvmtiCachedClassFieldMap * descriptor)850   void set_jvmti_cached_class_field_map(JvmtiCachedClassFieldMap* descriptor) {
851     _jvmti_cached_class_field_map = descriptor;
852   }
jvmti_cached_class_field_map() const853   JvmtiCachedClassFieldMap* jvmti_cached_class_field_map() const {
854     return _jvmti_cached_class_field_map;
855   }
856 #else // INCLUDE_JVMTI
857 
purge_previous_versions(InstanceKlass * ik)858   static void purge_previous_versions(InstanceKlass* ik) { return; };
has_previous_versions_and_reset()859   static bool has_previous_versions_and_reset() { return false; }
860 
set_cached_class_file(JvmtiCachedClassFileData * data)861   void set_cached_class_file(JvmtiCachedClassFileData *data) {
862     assert(data == NULL, "unexpected call with JVMTI disabled");
863   }
get_cached_class_file()864   JvmtiCachedClassFileData * get_cached_class_file() { return (JvmtiCachedClassFileData *)NULL; }
865 
866 #endif // INCLUDE_JVMTI
867 
has_nonstatic_concrete_methods() const868   bool has_nonstatic_concrete_methods() const {
869     return (_misc_flags & _misc_has_nonstatic_concrete_methods) != 0;
870   }
set_has_nonstatic_concrete_methods(bool b)871   void set_has_nonstatic_concrete_methods(bool b) {
872     if (b) {
873       _misc_flags |= _misc_has_nonstatic_concrete_methods;
874     } else {
875       _misc_flags &= ~_misc_has_nonstatic_concrete_methods;
876     }
877   }
878 
declares_nonstatic_concrete_methods() const879   bool declares_nonstatic_concrete_methods() const {
880     return (_misc_flags & _misc_declares_nonstatic_concrete_methods) != 0;
881   }
set_declares_nonstatic_concrete_methods(bool b)882   void set_declares_nonstatic_concrete_methods(bool b) {
883     if (b) {
884       _misc_flags |= _misc_declares_nonstatic_concrete_methods;
885     } else {
886       _misc_flags &= ~_misc_declares_nonstatic_concrete_methods;
887     }
888   }
889 
890   // for adding methods, ConstMethod::UNSET_IDNUM means no more ids available
891   inline u2 next_method_idnum();
set_initial_method_idnum(u2 value)892   void set_initial_method_idnum(u2 value)             { _idnum_allocated_count = value; }
893 
894   // generics support
generic_signature() const895   Symbol* generic_signature() const                   {
896     return (_generic_signature_index == 0) ?
897       (Symbol*)NULL : _constants->symbol_at(_generic_signature_index);
898   }
generic_signature_index() const899   u2 generic_signature_index() const                  {
900     return _generic_signature_index;
901   }
set_generic_signature_index(u2 sig_index)902   void set_generic_signature_index(u2 sig_index)      {
903     _generic_signature_index = sig_index;
904   }
905 
906   u2 enclosing_method_data(int offset) const;
enclosing_method_class_index() const907   u2 enclosing_method_class_index() const {
908     return enclosing_method_data(enclosing_method_class_index_offset);
909   }
enclosing_method_method_index()910   u2 enclosing_method_method_index() {
911     return enclosing_method_data(enclosing_method_method_index_offset);
912   }
913   void set_enclosing_method_indices(u2 class_index,
914                                     u2 method_index);
915 
916   // jmethodID support
917   jmethodID get_jmethod_id(const methodHandle& method_h);
918   jmethodID get_jmethod_id_fetch_or_update(size_t idnum,
919                      jmethodID new_id, jmethodID* new_jmeths,
920                      jmethodID* to_dealloc_id_p,
921                      jmethodID** to_dealloc_jmeths_p);
922   static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum,
923                 size_t *length_p, jmethodID* id_p);
924   void ensure_space_for_methodids(int start_offset = 0);
925   jmethodID jmethod_id_or_null(Method* method);
926 
927   // annotations support
annotations() const928   Annotations* annotations() const          { return _annotations; }
set_annotations(Annotations * anno)929   void set_annotations(Annotations* anno)   { _annotations = anno; }
930 
class_annotations() const931   AnnotationArray* class_annotations() const {
932     return (_annotations != NULL) ? _annotations->class_annotations() : NULL;
933   }
fields_annotations() const934   Array<AnnotationArray*>* fields_annotations() const {
935     return (_annotations != NULL) ? _annotations->fields_annotations() : NULL;
936   }
class_type_annotations() const937   AnnotationArray* class_type_annotations() const {
938     return (_annotations != NULL) ? _annotations->class_type_annotations() : NULL;
939   }
fields_type_annotations() const940   Array<AnnotationArray*>* fields_type_annotations() const {
941     return (_annotations != NULL) ? _annotations->fields_type_annotations() : NULL;
942   }
943   // allocation
944   instanceOop allocate_instance(TRAPS);
945 
946   // additional member function to return a handle
947   instanceHandle allocate_instance_handle(TRAPS);
948 
949   objArrayOop allocate_objArray(int n, int length, TRAPS);
950   // Helper function
951   static instanceOop register_finalizer(instanceOop i, TRAPS);
952 
953   // Check whether reflection/jni/jvm code is allowed to instantiate this class;
954   // if not, throw either an Error or an Exception.
955   virtual void check_valid_for_instantiation(bool throwError, TRAPS);
956 
957   // initialization
958   void call_class_initializer(TRAPS);
959   void set_initialization_state_and_notify(ClassState state, TRAPS);
960 
961   // OopMapCache support
oop_map_cache()962   OopMapCache* oop_map_cache()               { return _oop_map_cache; }
set_oop_map_cache(OopMapCache * cache)963   void set_oop_map_cache(OopMapCache *cache) { _oop_map_cache = cache; }
964   void mask_for(const methodHandle& method, int bci, InterpreterOopMap* entry);
965 
966   // JNI identifier support (for static fields - for jni performance)
jni_ids()967   JNIid* jni_ids()                               { return _jni_ids; }
set_jni_ids(JNIid * ids)968   void set_jni_ids(JNIid* ids)                   { _jni_ids = ids; }
969   JNIid* jni_id_for(int offset);
970 
971   // maintenance of deoptimization dependencies
972   inline DependencyContext dependencies();
973   int  mark_dependent_nmethods(KlassDepChange& changes);
974   void add_dependent_nmethod(nmethod* nm);
975   void remove_dependent_nmethod(nmethod* nm);
976   void clean_dependency_context();
977 
978   // On-stack replacement support
osr_nmethods_head() const979   nmethod* osr_nmethods_head() const         { return _osr_nmethods_head; };
set_osr_nmethods_head(nmethod * h)980   void set_osr_nmethods_head(nmethod* h)     { _osr_nmethods_head = h; };
981   void add_osr_nmethod(nmethod* n);
982   bool remove_osr_nmethod(nmethod* n);
983   int mark_osr_nmethods(const Method* m);
984   nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
985 
986 #if INCLUDE_JVMTI
987   // Breakpoint support (see methods on Method* for details)
breakpoints() const988   BreakpointInfo* breakpoints() const       { return _breakpoints; };
set_breakpoints(BreakpointInfo * bps)989   void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
990 #endif
991 
992   // support for stub routines
init_state_offset()993   static ByteSize init_state_offset()  { return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)994   JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)
995   static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
996 
997   // subclass/subinterface checks
998   bool implements_interface(Klass* k) const;
999   bool is_same_or_direct_interface(Klass* k) const;
1000 
1001 #ifdef ASSERT
1002   // check whether this class or one of its superclasses was redefined
1003   bool has_redefined_this_or_super() const;
1004 #endif
1005 
1006   // Access to the implementor of an interface.
1007   Klass* implementor() const;
1008   void set_implementor(Klass* k);
1009   int  nof_implementors() const;
1010   void add_implementor(Klass* k);  // k is a new class that implements this interface
1011   void init_implementor();           // initialize
1012 
1013   // link this class into the implementors list of every interface it implements
1014   void process_interfaces(Thread *thread);
1015 
1016   // virtual operations from Klass
1017   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
1018                                                   Array<InstanceKlass*>* transitive_interfaces);
1019   bool can_be_primary_super_slow() const;
oop_size(oop obj) const1020   int oop_size(oop obj)  const             { return size_helper(); }
1021   // slow because it's a virtual call and used for verifying the layout_helper.
1022   // Using the layout_helper bits, we can call is_instance_klass without a virtual call.
1023   DEBUG_ONLY(bool is_instance_klass_slow() const      { return true; })
1024 
1025   // Iterators
1026   void do_local_static_fields(FieldClosure* cl);
1027   void do_nonstatic_fields(FieldClosure* cl); // including inherited fields
1028   void do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle, TRAPS);
1029 
1030   void methods_do(void f(Method* method));
1031   void array_klasses_do(void f(Klass* k));
1032   void array_klasses_do(void f(Klass* k, TRAPS), TRAPS);
1033 
cast(Klass * k)1034   static InstanceKlass* cast(Klass* k) {
1035     return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k)));
1036   }
1037 
cast(const Klass * k)1038   static const InstanceKlass* cast(const Klass* k) {
1039     assert(k != NULL, "k should not be null");
1040     assert(k->is_instance_klass(), "cast to InstanceKlass");
1041     return static_cast<const InstanceKlass*>(k);
1042   }
1043 
java_super() const1044   virtual InstanceKlass* java_super() const {
1045     return (super() == NULL) ? NULL : cast(super());
1046   }
1047 
1048   // Sizing (in words)
header_size()1049   static int header_size()            { return sizeof(InstanceKlass)/wordSize; }
1050 
size(int vtable_length,int itable_length,int nonstatic_oop_map_size,bool is_interface,bool is_unsafe_anonymous,bool has_stored_fingerprint)1051   static int size(int vtable_length, int itable_length,
1052                   int nonstatic_oop_map_size,
1053                   bool is_interface, bool is_unsafe_anonymous, bool has_stored_fingerprint) {
1054     return align_metadata_size(header_size() +
1055            vtable_length +
1056            itable_length +
1057            nonstatic_oop_map_size +
1058            (is_interface ? (int)sizeof(Klass*)/wordSize : 0) +
1059            (is_unsafe_anonymous ? (int)sizeof(Klass*)/wordSize : 0) +
1060            (has_stored_fingerprint ? (int)sizeof(uint64_t*)/wordSize : 0));
1061   }
size() const1062   int size() const                    { return size(vtable_length(),
1063                                                itable_length(),
1064                                                nonstatic_oop_map_size(),
1065                                                is_interface(),
1066                                                is_unsafe_anonymous(),
1067                                                has_stored_fingerprint());
1068   }
1069 #if INCLUDE_SERVICES
1070   virtual void collect_statistics(KlassSizeStats *sz) const;
1071 #endif
1072 
start_of_itable() const1073   intptr_t* start_of_itable()   const { return (intptr_t*)start_of_vtable() + vtable_length(); }
end_of_itable() const1074   intptr_t* end_of_itable()     const { return start_of_itable() + itable_length(); }
1075 
itable_offset_in_words() const1076   int  itable_offset_in_words() const { return start_of_itable() - (intptr_t*)this; }
1077 
static_field_base_raw()1078   oop static_field_base_raw() { return java_mirror(); }
1079 
start_of_nonstatic_oop_maps() const1080   OopMapBlock* start_of_nonstatic_oop_maps() const {
1081     return (OopMapBlock*)(start_of_itable() + itable_length());
1082   }
1083 
end_of_nonstatic_oop_maps() const1084   Klass** end_of_nonstatic_oop_maps() const {
1085     return (Klass**)(start_of_nonstatic_oop_maps() +
1086                      nonstatic_oop_map_count());
1087   }
1088 
adr_implementor() const1089   Klass* volatile* adr_implementor() const {
1090     if (is_interface()) {
1091       return (Klass* volatile*)end_of_nonstatic_oop_maps();
1092     } else {
1093       return NULL;
1094     }
1095   };
1096 
adr_unsafe_anonymous_host() const1097   InstanceKlass** adr_unsafe_anonymous_host() const {
1098     if (is_unsafe_anonymous()) {
1099       InstanceKlass** adr_impl = (InstanceKlass**)adr_implementor();
1100       if (adr_impl != NULL) {
1101         return adr_impl + 1;
1102       } else {
1103         return (InstanceKlass **)end_of_nonstatic_oop_maps();
1104       }
1105     } else {
1106       return NULL;
1107     }
1108   }
1109 
adr_fingerprint() const1110   address adr_fingerprint() const {
1111     if (has_stored_fingerprint()) {
1112       InstanceKlass** adr_host = adr_unsafe_anonymous_host();
1113       if (adr_host != NULL) {
1114         return (address)(adr_host + 1);
1115       }
1116 
1117       Klass* volatile* adr_impl = adr_implementor();
1118       if (adr_impl != NULL) {
1119         return (address)(adr_impl + 1);
1120       }
1121 
1122       return (address)end_of_nonstatic_oop_maps();
1123     } else {
1124       return NULL;
1125     }
1126   }
1127 
1128   // Use this to return the size of an instance in heap words:
size_helper() const1129   int size_helper() const {
1130     return layout_helper_to_size_helper(layout_helper());
1131   }
1132 
1133   // This bit is initialized in classFileParser.cpp.
1134   // It is false under any of the following conditions:
1135   //  - the class is abstract (including any interface)
1136   //  - the class has a finalizer (if !RegisterFinalizersAtInit)
1137   //  - the class size is larger than FastAllocateSizeLimit
1138   //  - the class is java/lang/Class, which cannot be allocated directly
can_be_fastpath_allocated() const1139   bool can_be_fastpath_allocated() const {
1140     return !layout_helper_needs_slow_path(layout_helper());
1141   }
1142 
1143   // Java itable
1144   klassItable itable() const;        // return klassItable wrapper
1145   Method* method_at_itable(Klass* holder, int index, TRAPS);
1146 
1147 #if INCLUDE_JVMTI
1148   void adjust_default_methods(bool* trace_name_printed);
1149 #endif // INCLUDE_JVMTI
1150 
1151   void clean_weak_instanceklass_links();
1152  private:
1153   void clean_implementors_list();
1154   void clean_method_data();
1155 
1156  public:
1157   // Explicit metaspace deallocation of fields
1158   // For RedefineClasses and class file parsing errors, we need to deallocate
1159   // instanceKlasses and the metadata they point to.
1160   void deallocate_contents(ClassLoaderData* loader_data);
1161   static void deallocate_methods(ClassLoaderData* loader_data,
1162                                  Array<Method*>* methods);
1163   void static deallocate_interfaces(ClassLoaderData* loader_data,
1164                                     const Klass* super_klass,
1165                                     Array<InstanceKlass*>* local_interfaces,
1166                                     Array<InstanceKlass*>* transitive_interfaces);
1167   void static deallocate_record_components(ClassLoaderData* loader_data,
1168                                            Array<RecordComponent*>* record_component);
1169 
1170   // The constant pool is on stack if any of the methods are executing or
1171   // referenced by handles.
on_stack() const1172   bool on_stack() const { return _constants->on_stack(); }
1173 
1174   // callbacks for actions during class unloading
1175   static void unload_class(InstanceKlass* ik);
1176   static void release_C_heap_structures(InstanceKlass* ik);
1177 
1178   // Naming
1179   const char* signature_name() const;
1180   static Symbol* package_from_name(const Symbol* name, TRAPS);
1181 
1182   // Oop fields (and metadata) iterators
1183   //
1184   // The InstanceKlass iterators also visits the Object's klass.
1185 
1186   // Forward iteration
1187  public:
1188   // Iterate over all oop fields in the oop maps.
1189   template <typename T, class OopClosureType>
1190   inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1191 
1192   // Iterate over all oop fields and metadata.
1193   template <typename T, class OopClosureType>
1194   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
1195 
1196   // Iterate over all oop fields in one oop map.
1197   template <typename T, class OopClosureType>
1198   inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1199 
1200 
1201   // Reverse iteration
1202   // Iterate over all oop fields and metadata.
1203   template <typename T, class OopClosureType>
1204   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
1205 
1206  private:
1207   // Iterate over all oop fields in the oop maps.
1208   template <typename T, class OopClosureType>
1209   inline void oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure);
1210 
1211   // Iterate over all oop fields in one oop map.
1212   template <typename T, class OopClosureType>
1213   inline void oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure);
1214 
1215 
1216   // Bounded range iteration
1217  public:
1218   // Iterate over all oop fields in the oop maps.
1219   template <typename T, class OopClosureType>
1220   inline void oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1221 
1222   // Iterate over all oop fields and metadata.
1223   template <typename T, class OopClosureType>
1224   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1225 
1226  private:
1227   // Iterate over all oop fields in one oop map.
1228   template <typename T, class OopClosureType>
1229   inline void oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr);
1230 
1231 
1232  public:
idnum_allocated_count() const1233   u2 idnum_allocated_count() const      { return _idnum_allocated_count; }
1234 
1235 public:
set_in_error_state()1236   void set_in_error_state() {
1237     assert(DumpSharedSpaces, "only call this when dumping archive");
1238     _init_state = initialization_error;
1239   }
1240   bool check_sharing_error_state();
1241 
1242 private:
1243   // initialization state
1244   void set_init_state(ClassState state);
set_rewritten()1245   void set_rewritten()                  { _misc_flags |= _misc_rewritten; }
set_init_thread(Thread * thread)1246   void set_init_thread(Thread *thread)  { _init_thread = thread; }
1247 
1248   // The RedefineClasses() API can cause new method idnums to be needed
1249   // which will cause the caches to grow. Safety requires different
1250   // cache management logic if the caches can grow instead of just
1251   // going from NULL to non-NULL.
idnum_can_increment() const1252   bool idnum_can_increment() const      { return has_been_redefined(); }
1253   inline jmethodID* methods_jmethod_ids_acquire() const;
1254   inline void release_set_methods_jmethod_ids(jmethodID* jmeths);
1255 
1256   // Lock during initialization
1257 public:
1258   // Lock for (1) initialization; (2) access to the ConstantPool of this class.
1259   // Must be one per class and it has to be a VM internal object so java code
1260   // cannot lock it (like the mirror).
1261   // It has to be an object not a Mutex because it's held through java calls.
1262   oop init_lock() const;
1263 private:
1264   void fence_and_clear_init_lock();
1265 
1266   bool link_class_impl                           (TRAPS);
1267   bool verify_code                               (TRAPS);
1268   void initialize_impl                           (TRAPS);
1269   void initialize_super_interfaces               (TRAPS);
1270   void eager_initialize_impl                     ();
1271   /* jni_id_for_impl for jfieldID only */
1272   JNIid* jni_id_for_impl                         (int offset);
1273 
1274   // Returns the array class for the n'th dimension
1275   Klass* array_klass_impl(bool or_null, int n, TRAPS);
1276 
1277   // Returns the array class with this class as element type
1278   Klass* array_klass_impl(bool or_null, TRAPS);
1279 
1280   // find a local method (returns NULL if not found)
1281   Method* find_method_impl(const Symbol* name,
1282                            const Symbol* signature,
1283                            OverpassLookupMode overpass_mode,
1284                            StaticLookupMode static_mode,
1285                            PrivateLookupMode private_mode) const;
1286 
1287   static Method* find_method_impl(const Array<Method*>* methods,
1288                                   const Symbol* name,
1289                                   const Symbol* signature,
1290                                   OverpassLookupMode overpass_mode,
1291                                   StaticLookupMode static_mode,
1292                                   PrivateLookupMode private_mode);
1293 
1294   // Free CHeap allocated fields.
1295   void release_C_heap_structures();
1296 
1297 #if INCLUDE_JVMTI
1298   // RedefineClasses support
link_previous_versions(InstanceKlass * pv)1299   void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; }
1300   void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count);
1301 #endif
1302 public:
1303   // CDS support - remove and restore oops from metadata. Oops are not shared.
1304   virtual void remove_unshareable_info();
1305   virtual void remove_java_mirror();
1306   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
1307 
1308   // jvm support
1309   jint compute_modifier_flags(TRAPS) const;
1310 
1311 public:
1312   // JVMTI support
1313   jint jvmti_class_status() const;
1314 
1315   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
1316 
1317  public:
1318   // Printing
1319 #ifndef PRODUCT
1320   void print_on(outputStream* st) const;
1321 #endif
1322   void print_value_on(outputStream* st) const;
1323 
1324   void oop_print_value_on(oop obj, outputStream* st);
1325 
1326 #ifndef PRODUCT
1327   void oop_print_on      (oop obj, outputStream* st);
1328 
1329   void print_dependent_nmethods(bool verbose = false);
1330   bool is_dependent_nmethod(nmethod* nm);
1331   bool verify_itable_index(int index);
1332 #endif
1333 
1334   const char* internal_name() const;
1335 
1336   // Verification
1337   void verify_on(outputStream* st);
1338 
1339   void oop_verify_on(oop obj, outputStream* st);
1340 
1341   // Logging
1342   void print_class_load_logging(ClassLoaderData* loader_data,
1343                                 const char* module_name,
1344                                 const ClassFileStream* cfs) const;
1345 };
1346 
1347 // for adding methods
1348 // UNSET_IDNUM return means no more ids available
next_method_idnum()1349 inline u2 InstanceKlass::next_method_idnum() {
1350   if (_idnum_allocated_count == ConstMethod::MAX_IDNUM) {
1351     return ConstMethod::UNSET_IDNUM; // no more ids available
1352   } else {
1353     return _idnum_allocated_count++;
1354   }
1355 }
1356 
1357 
1358 /* JNIid class for jfieldIDs only */
1359 class JNIid: public CHeapObj<mtClass> {
1360   friend class VMStructs;
1361  private:
1362   Klass*             _holder;
1363   JNIid*             _next;
1364   int                _offset;
1365 #ifdef ASSERT
1366   bool               _is_static_field_id;
1367 #endif
1368 
1369  public:
1370   // Accessors
holder() const1371   Klass* holder() const           { return _holder; }
offset() const1372   int offset() const              { return _offset; }
next()1373   JNIid* next()                   { return _next; }
1374   // Constructor
1375   JNIid(Klass* holder, int offset, JNIid* next);
1376   // Identifier lookup
1377   JNIid* find(int offset);
1378 
find_local_field(fieldDescriptor * fd)1379   bool find_local_field(fieldDescriptor* fd) {
1380     return InstanceKlass::cast(holder())->find_local_field_from_offset(offset(), true, fd);
1381   }
1382 
1383   static void deallocate(JNIid* id);
1384   // Debugging
1385 #ifdef ASSERT
is_static_field_id() const1386   bool is_static_field_id() const { return _is_static_field_id; }
set_is_static_field_id()1387   void set_is_static_field_id()   { _is_static_field_id = true; }
1388 #endif
1389   void verify(Klass* holder);
1390 };
1391 
1392 // An iterator that's used to access the inner classes indices in the
1393 // InstanceKlass::_inner_classes array.
1394 class InnerClassesIterator : public StackObj {
1395  private:
1396   Array<jushort>* _inner_classes;
1397   int _length;
1398   int _idx;
1399  public:
1400 
InnerClassesIterator(const InstanceKlass * k)1401   InnerClassesIterator(const InstanceKlass* k) {
1402     _inner_classes = k->inner_classes();
1403     if (k->inner_classes() != NULL) {
1404       _length = _inner_classes->length();
1405       // The inner class array's length should be the multiple of
1406       // inner_class_next_offset if it only contains the InnerClasses
1407       // attribute data, or it should be
1408       // n*inner_class_next_offset+enclosing_method_attribute_size
1409       // if it also contains the EnclosingMethod data.
1410       assert((_length % InstanceKlass::inner_class_next_offset == 0 ||
1411               _length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size),
1412              "just checking");
1413       // Remove the enclosing_method portion if exists.
1414       if (_length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size) {
1415         _length -= InstanceKlass::enclosing_method_attribute_size;
1416       }
1417     } else {
1418       _length = 0;
1419     }
1420     _idx = 0;
1421   }
1422 
length() const1423   int length() const {
1424     return _length;
1425   }
1426 
next()1427   void next() {
1428     _idx += InstanceKlass::inner_class_next_offset;
1429   }
1430 
done() const1431   bool done() const {
1432     return (_idx >= _length);
1433   }
1434 
inner_class_info_index() const1435   u2 inner_class_info_index() const {
1436     return _inner_classes->at(
1437                _idx + InstanceKlass::inner_class_inner_class_info_offset);
1438   }
1439 
set_inner_class_info_index(u2 index)1440   void set_inner_class_info_index(u2 index) {
1441     _inner_classes->at_put(
1442                _idx + InstanceKlass::inner_class_inner_class_info_offset, index);
1443   }
1444 
outer_class_info_index() const1445   u2 outer_class_info_index() const {
1446     return _inner_classes->at(
1447                _idx + InstanceKlass::inner_class_outer_class_info_offset);
1448   }
1449 
set_outer_class_info_index(u2 index)1450   void set_outer_class_info_index(u2 index) {
1451     _inner_classes->at_put(
1452                _idx + InstanceKlass::inner_class_outer_class_info_offset, index);
1453   }
1454 
inner_name_index() const1455   u2 inner_name_index() const {
1456     return _inner_classes->at(
1457                _idx + InstanceKlass::inner_class_inner_name_offset);
1458   }
1459 
set_inner_name_index(u2 index)1460   void set_inner_name_index(u2 index) {
1461     _inner_classes->at_put(
1462                _idx + InstanceKlass::inner_class_inner_name_offset, index);
1463   }
1464 
inner_access_flags() const1465   u2 inner_access_flags() const {
1466     return _inner_classes->at(
1467                _idx + InstanceKlass::inner_class_access_flags_offset);
1468   }
1469 };
1470 
1471 #endif // SHARE_OOPS_INSTANCEKLASS_HPP
1472