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/classLoader.hpp"
29 #include "classfile/classLoaderData.hpp"
30 #include "classfile/moduleEntry.hpp"
31 #include "classfile/packageEntry.hpp"
32 #include "memory/referenceType.hpp"
33 #include "oops/annotations.hpp"
34 #include "oops/constMethod.hpp"
35 #include "oops/fieldInfo.hpp"
36 #include "oops/instanceOop.hpp"
37 #include "oops/klassVtable.hpp"
38 #include "runtime/handles.hpp"
39 #include "runtime/os.hpp"
40 #include "utilities/accessFlags.hpp"
41 #include "utilities/align.hpp"
42 #include "utilities/macros.hpp"
43 #if INCLUDE_JFR
44 #include "jfr/support/jfrKlassExtension.hpp"
45 #endif
46
47
48 // An InstanceKlass is the VM level representation of a Java class.
49 // It contains all information needed for at class at execution runtime.
50
51 // InstanceKlass embedded field layout (after declared fields):
52 // [EMBEDDED Java vtable ] size in words = vtable_len
53 // [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size
54 // The embedded nonstatic oop-map blocks are short pairs (offset, length)
55 // indicating where oops are located in instances of this klass.
56 // [EMBEDDED implementor of the interface] only exist for interface
57 // [EMBEDDED unsafe_anonymous_host klass] only exist for an unsafe anonymous class (JSR 292 enabled)
58 // [EMBEDDED fingerprint ] only if should_store_fingerprint()==true
59
60
61 // forward declaration for class -- see below for definition
62 #if INCLUDE_JVMTI
63 class BreakpointInfo;
64 #endif
65 class ClassFileParser;
66 class KlassDepChange;
67 class DependencyContext;
68 class fieldDescriptor;
69 class jniIdMapBase;
70 class JNIid;
71 class JvmtiCachedClassFieldMap;
72 class nmethodBucket;
73 class SuperTypeClosure;
74 class OopMapCache;
75 class InterpreterOopMap;
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:
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.
99 int offset() const { return _offset; }
100 void set_offset(int offset) { _offset = offset; }
101
102 // Number of oops in this block.
103 uint count() const { return _count; }
104 void set_count(uint count) { _count = count; }
105
106 // sizeof(OopMapBlock) 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:
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)
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 source debug extension for this klass, NULL if not specified.
187 // Specified as UTF-8 string without terminating zero byte in the classfile,
188 // it is stored in the instanceklass as a NULL-terminated UTF-8 string
189 const char* _source_debug_extension;
190 // Array name derived from this class which needs unreferencing
191 // if this class is unloaded.
192 Symbol* _array_name;
193
194 // Number of heapOopSize words used by non-static fields in this klass
195 // (including inherited fields but after header_size()).
196 int _nonstatic_field_size;
197 int _static_field_size; // number words used by static fields (oop and non-oop) in this klass
198 // Constant pool index to the utf8 entry of the Generic signature,
199 // or 0 if none.
200 u2 _generic_signature_index;
201 // Constant pool index to the utf8 entry for the name of source file
202 // containing this klass, 0 if not specified.
203 u2 _source_file_name_index;
204 u2 _static_oop_field_count;// number of static oop fields in this klass
205 u2 _java_fields_count; // The number of declared Java fields
206 int _nonstatic_oop_map_size;// size in words of nonstatic oop map blocks
207
208 int _itable_len; // length of Java itable (in words)
209 // _is_marked_dependent can be set concurrently, thus cannot be part of the
210 // _misc_flags.
211 bool _is_marked_dependent; // used for marking during flushing and deoptimization
212 bool _is_being_redefined; // used for locking redefinition
213
214 // The low two bits of _misc_flags contains the kind field.
215 // This can be used to quickly discriminate among the four kinds of
216 // InstanceKlass.
217
218 static const unsigned _misc_kind_field_size = 2;
219 static const unsigned _misc_kind_field_pos = 0;
220 static const unsigned _misc_kind_field_mask = (1u << _misc_kind_field_size) - 1u;
221
222 static const unsigned _misc_kind_other = 0; // concrete InstanceKlass
223 static const unsigned _misc_kind_reference = 1; // InstanceRefKlass
224 static const unsigned _misc_kind_class_loader = 2; // InstanceClassLoaderKlass
225 static const unsigned _misc_kind_mirror = 3; // InstanceMirrorKlass
226
227 // Start after _misc_kind field.
228 enum {
229 _misc_rewritten = 1 << 2, // methods rewritten.
230 _misc_has_nonstatic_fields = 1 << 3, // for sizing with UseCompressedOops
231 _misc_should_verify_class = 1 << 4, // allow caching of preverification
232 _misc_is_unsafe_anonymous = 1 << 5, // has embedded _unsafe_anonymous_host field
233 _misc_is_contended = 1 << 6, // marked with contended annotation
234 _misc_has_nonstatic_concrete_methods = 1 << 7, // class/superclass/implemented interfaces has non-static, concrete methods
235 _misc_declares_nonstatic_concrete_methods = 1 << 8, // directly declares non-static, concrete methods
236 _misc_has_been_redefined = 1 << 9, // class has been redefined
237 _misc_has_passed_fingerprint_check = 1 << 10, // when this class was loaded, the fingerprint computed from its
238 // code source was found to be matching the value recorded by AOT.
239 _misc_is_scratch_class = 1 << 11, // class is the redefined scratch class
240 _misc_is_shared_boot_class = 1 << 12, // defining class loader is boot class loader
241 _misc_is_shared_platform_class = 1 << 13, // defining class loader is platform class loader
242 _misc_is_shared_app_class = 1 << 14, // defining class loader is app class loader
243 _misc_has_resolved_methods = 1 << 15 // resolved methods table entries added for this class
244 };
245 u2 loader_type_bits() {
246 return _misc_is_shared_boot_class|_misc_is_shared_platform_class|_misc_is_shared_app_class;
247 }
248 u2 _misc_flags;
249 u2 _minor_version; // minor version number of class file
250 u2 _major_version; // major version number of class file
251 Thread* _init_thread; // Pointer to current thread doing initialization (to handle recusive initialization)
252 OopMapCache* volatile _oop_map_cache; // OopMapCache for all methods in the klass (allocated lazily)
253 JNIid* _jni_ids; // First JNI identifier for static fields in this class
254 jmethodID* volatile _methods_jmethod_ids; // jmethodIDs corresponding to method_idnum, or NULL if none
255 nmethodBucket* volatile _dep_context; // packed DependencyContext structure
256 uint64_t volatile _dep_context_last_cleaned;
257 nmethod* _osr_nmethods_head; // Head of list of on-stack replacement nmethods for this class
258 #if INCLUDE_JVMTI
259 BreakpointInfo* _breakpoints; // bpt lists, managed by Method*
260 // Linked instanceKlasses of previous versions
261 InstanceKlass* _previous_versions;
262 // JVMTI fields can be moved to their own structure - see 6315920
263 // JVMTI: cached class file, before retransformable agent modified it in CFLH
291 Array<int>* _method_ordering;
292 // Int array containing the vtable_indices for default_methods
293 // offset matches _default_methods offset
294 Array<int>* _default_vtable_indices;
295
296 // Instance and static variable information, starts with 6-tuples of shorts
297 // [access, name index, sig index, initval index, low_offset, high_offset]
298 // for all fields, followed by the generic signature data at the end of
299 // the array. Only fields with generic signature attributes have the generic
300 // signature data set in the array. The fields array looks like following:
301 //
302 // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
303 // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
304 // ...
305 // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
306 // [generic signature index]
307 // [generic signature index]
308 // ...
309 Array<u2>* _fields;
310
311 // embedded Java vtable follows here
312 // embedded Java itables follows here
313 // embedded static fields follows here
314 // embedded nonstatic oop-map blocks follows here
315 // embedded implementor of this interface follows here
316 // The embedded implementor only exists if the current klass is an
317 // iterface. The possible values of the implementor fall into following
318 // three cases:
319 // NULL: no implementor.
320 // A Klass* that's not itself: one implementor.
321 // Itself: more than one implementors.
322 // embedded unsafe_anonymous_host klass follows here
323 // The embedded host klass only exists in an unsafe anonymous class for
324 // dynamic language support (JSR 292 enabled). The host class grants
325 // its access privileges to this class also. The host class is either
326 // named, or a previously loaded unsafe anonymous class. A non-anonymous class
327 // or an anonymous class loaded through normal classloading does not
328 // have this embedded field.
329 //
330
360 case ClassLoader::APP_LOADER:
361 _misc_flags |= _misc_is_shared_app_class;
362 break;
363 default:
364 ShouldNotReachHere();
365 break;
366 }
367 }
368
369 bool has_nonstatic_fields() const {
370 return (_misc_flags & _misc_has_nonstatic_fields) != 0;
371 }
372 void set_has_nonstatic_fields(bool b) {
373 if (b) {
374 _misc_flags |= _misc_has_nonstatic_fields;
375 } else {
376 _misc_flags &= ~_misc_has_nonstatic_fields;
377 }
378 }
379
380 // field sizes
381 int nonstatic_field_size() const { return _nonstatic_field_size; }
382 void set_nonstatic_field_size(int size) { _nonstatic_field_size = size; }
383
384 int static_field_size() const { return _static_field_size; }
385 void set_static_field_size(int size) { _static_field_size = size; }
386
387 int static_oop_field_count() const { return (int)_static_oop_field_count; }
388 void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
389
390 // Java itable
391 int itable_length() const { return _itable_len; }
392 void set_itable_length(int len) { _itable_len = len; }
393
394 // array klasses
395 Klass* array_klasses() const { return _array_klasses; }
396 inline Klass* array_klasses_acquire() const; // load with acquire semantics
397 void set_array_klasses(Klass* k) { _array_klasses = k; }
398 inline void release_set_array_klasses(Klass* k); // store with release semantics
399
422 Array<InstanceKlass*>* local_interfaces() const { return _local_interfaces; }
423 void set_local_interfaces(Array<InstanceKlass*>* a) {
424 guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
425 _local_interfaces = a; }
426
427 Array<InstanceKlass*>* transitive_interfaces() const { return _transitive_interfaces; }
428 void set_transitive_interfaces(Array<InstanceKlass*>* a) {
429 guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
430 _transitive_interfaces = a;
431 }
432
433 private:
434 friend class fieldDescriptor;
435 FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
436
437 public:
438 int field_offset (int index) const { return field(index)->offset(); }
439 int field_access_flags(int index) const { return field(index)->access_flags(); }
440 Symbol* field_name (int index) const { return field(index)->name(constants()); }
441 Symbol* field_signature (int index) const { return field(index)->signature(constants()); }
442
443 // Number of Java declared fields
444 int java_fields_count() const { return (int)_java_fields_count; }
445
446 Array<u2>* fields() const { return _fields; }
447 void set_fields(Array<u2>* f, u2 java_fields_count) {
448 guarantee(_fields == NULL || f == NULL, "Just checking");
449 _fields = f;
450 _java_fields_count = java_fields_count;
451 }
452
453 // inner classes
454 Array<u2>* inner_classes() const { return _inner_classes; }
455 void set_inner_classes(Array<u2>* f) { _inner_classes = f; }
456
457 // nest members
458 Array<u2>* nest_members() const { return _nest_members; }
459 void set_nest_members(Array<u2>* m) { _nest_members = m; }
460
461 // nest-host index
521 bool is_reentrant_initialization(Thread *thread) { return thread == _init_thread; }
522 ClassState init_state() { return (ClassState)_init_state; }
523 bool is_rewritten() const { return (_misc_flags & _misc_rewritten) != 0; }
524
525 // defineClass specified verification
526 bool should_verify_class() const {
527 return (_misc_flags & _misc_should_verify_class) != 0;
528 }
529 void set_should_verify_class(bool value) {
530 if (value) {
531 _misc_flags |= _misc_should_verify_class;
532 } else {
533 _misc_flags &= ~_misc_should_verify_class;
534 }
535 }
536
537 // marking
538 bool is_marked_dependent() const { return _is_marked_dependent; }
539 void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
540
541 // initialization (virtuals from Klass)
542 bool should_be_initialized() const; // means that initialize should be called
543 void initialize(TRAPS);
544 void link_class(TRAPS);
545 bool link_class_or_fail(TRAPS); // returns false on failure
546 void rewrite_class(TRAPS);
547 void link_methods(TRAPS);
548 Method* class_initializer() const;
549
550 // set the class to initialized if no static initializer is present
551 void eager_initialize(Thread *thread);
552
553 // reference type
554 ReferenceType reference_type() const { return (ReferenceType)_reference_type; }
555 void set_reference_type(ReferenceType t) {
556 assert(t == (u1)t, "overflow");
557 _reference_type = (u1)t;
558 }
559
560 // this class cp index
561 u2 this_class_index() const { return _this_class_index; }
562 void set_this_class_index(u2 index) { _this_class_index = index; }
563
564 static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
565
566 // find local field, returns true if found
567 bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
568 // find field in direct superinterfaces, returns the interface in which the field is defined
569 Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
570 // find field 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, fieldDescriptor* fd) const;
572 // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined
573 Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const;
574
575 // find a non-static or static field given its offset within the class.
576 bool contains_field_offset(int offset) {
577 return instanceOopDesc::contains_field_offset(offset, nonstatic_field_size());
578 }
579
580 bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
581 bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
582
583 // find a local method (returns NULL if not found)
584 Method* find_method(const Symbol* name, const Symbol* signature) const;
585 static Method* find_method(const Array<Method*>* methods,
586 const Symbol* name,
587 const Symbol* signature);
588
589 // find a local method, but skip static methods
590 Method* find_instance_method(const Symbol* name, const Symbol* signature,
591 PrivateLookupMode private_mode = find_private) const;
592 static Method* find_instance_method(const Array<Method*>* methods,
593 const Symbol* name,
594 const Symbol* signature,
595 PrivateLookupMode private_mode = find_private);
596
597 // find a local method (returns NULL if not found)
714 void set_source_debug_extension(const char* array, int length);
715
716 // symbol unloading support (refcount already added)
717 Symbol* array_name() { return _array_name; }
718 void set_array_name(Symbol* name) { assert(_array_name == NULL || name == NULL, "name already created"); _array_name = name; }
719
720 // nonstatic oop-map blocks
721 static int nonstatic_oop_map_size(unsigned int oop_map_count) {
722 return oop_map_count * OopMapBlock::size_in_words();
723 }
724 unsigned int nonstatic_oop_map_count() const {
725 return _nonstatic_oop_map_size / OopMapBlock::size_in_words();
726 }
727 int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; }
728 void set_nonstatic_oop_map_size(int words) {
729 _nonstatic_oop_map_size = words;
730 }
731
732 #if INCLUDE_JVMTI
733 // Redefinition locking. Class can only be redefined by one thread at a time.
734 bool is_being_redefined() const { return _is_being_redefined; }
735 void set_is_being_redefined(bool value) { _is_being_redefined = value; }
736
737 // RedefineClasses() support for previous versions:
738 void add_previous_version(InstanceKlass* ik, int emcp_method_count);
739 void purge_previous_version_list();
740
741 InstanceKlass* previous_versions() const { return _previous_versions; }
742 #else
743 InstanceKlass* previous_versions() const { return NULL; }
744 #endif
745
746 InstanceKlass* get_klass_version(int version) {
747 for (InstanceKlass* ik = this; ik != NULL; ik = ik->previous_versions()) {
748 if (ik->constants()->version() == version) {
749 return ik;
750 }
751 }
752 return NULL;
753 }
754
755 bool has_been_redefined() const {
769 _misc_flags &= ~_misc_has_passed_fingerprint_check;
770 }
771 }
772 bool supers_have_passed_fingerprint_checks();
773
774 static bool should_store_fingerprint(bool is_unsafe_anonymous);
775 bool should_store_fingerprint() const { return should_store_fingerprint(is_unsafe_anonymous()); }
776 bool has_stored_fingerprint() const;
777 uint64_t get_stored_fingerprint() const;
778 void store_fingerprint(uint64_t fingerprint);
779
780 bool is_scratch_class() const {
781 return (_misc_flags & _misc_is_scratch_class) != 0;
782 }
783
784 void set_is_scratch_class() {
785 _misc_flags |= _misc_is_scratch_class;
786 }
787
788 bool has_resolved_methods() const {
789 return (_misc_flags & _misc_has_resolved_methods) != 0;
790 }
791
792 void set_has_resolved_methods() {
793 _misc_flags |= _misc_has_resolved_methods;
794 }
795 private:
796
797 void set_kind(unsigned kind) {
798 assert(kind <= _misc_kind_field_mask, "Invalid InstanceKlass kind");
799 unsigned fmask = _misc_kind_field_mask << _misc_kind_field_pos;
800 unsigned flags = _misc_flags & ~fmask;
801 _misc_flags = (flags | (kind << _misc_kind_field_pos));
802 }
803
804 bool is_kind(unsigned desired) const {
805 unsigned kind = (_misc_flags >> _misc_kind_field_pos) & _misc_kind_field_mask;
806 return kind == desired;
807 }
808
809 public:
810
811 // Other is anything that is not one of the more specialized kinds of InstanceKlass.
812 bool is_other_instance_klass() const { return is_kind(_misc_kind_other); }
813 bool is_reference_instance_klass() const { return is_kind(_misc_kind_reference); }
814 bool is_mirror_instance_klass() const { return is_kind(_misc_kind_mirror); }
815 bool is_class_loader_instance_klass() const { return is_kind(_misc_kind_class_loader); }
816
817 #if INCLUDE_JVMTI
818
819 void init_previous_versions() {
820 _previous_versions = NULL;
821 }
822
823 private:
824 static bool _has_previous_versions;
825 public:
826 static void purge_previous_versions(InstanceKlass* ik) {
827 if (ik->has_been_redefined()) {
828 ik->purge_previous_version_list();
829 }
830 }
831
832 static bool has_previous_versions_and_reset();
833 static bool has_previous_versions() { return _has_previous_versions; }
834
835 // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation
971
972 // On-stack replacement support
973 nmethod* osr_nmethods_head() const { return _osr_nmethods_head; };
974 void set_osr_nmethods_head(nmethod* h) { _osr_nmethods_head = h; };
975 void add_osr_nmethod(nmethod* n);
976 bool remove_osr_nmethod(nmethod* n);
977 int mark_osr_nmethods(const Method* m);
978 nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
979
980 #if INCLUDE_JVMTI
981 // Breakpoint support (see methods on Method* for details)
982 BreakpointInfo* breakpoints() const { return _breakpoints; };
983 void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
984 #endif
985
986 // support for stub routines
987 static ByteSize init_state_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
988 JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)
989 static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
990
991 // subclass/subinterface checks
992 bool implements_interface(Klass* k) const;
993 bool is_same_or_direct_interface(Klass* k) const;
994
995 #ifdef ASSERT
996 // check whether this class or one of its superclasses was redefined
997 bool has_redefined_this_or_super() const;
998 #endif
999
1000 // Access to the implementor of an interface.
1001 Klass* implementor() const;
1002 void set_implementor(Klass* k);
1003 int nof_implementors() const;
1004 void add_implementor(Klass* k); // k is a new class that implements this interface
1005 void init_implementor(); // initialize
1006
1007 // link this class into the implementors list of every interface it implements
1008 void process_interfaces(Thread *thread);
1009
1010 // virtual operations from Klass
1029
1030 static InstanceKlass* cast(Klass* k) {
1031 return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k)));
1032 }
1033
1034 static const InstanceKlass* cast(const Klass* k) {
1035 assert(k != NULL, "k should not be null");
1036 assert(k->is_instance_klass(), "cast to InstanceKlass");
1037 return static_cast<const InstanceKlass*>(k);
1038 }
1039
1040 virtual InstanceKlass* java_super() const {
1041 return (super() == NULL) ? NULL : cast(super());
1042 }
1043
1044 // Sizing (in words)
1045 static int header_size() { return sizeof(InstanceKlass)/wordSize; }
1046
1047 static int size(int vtable_length, int itable_length,
1048 int nonstatic_oop_map_size,
1049 bool is_interface, bool is_unsafe_anonymous, bool has_stored_fingerprint) {
1050 return align_metadata_size(header_size() +
1051 vtable_length +
1052 itable_length +
1053 nonstatic_oop_map_size +
1054 (is_interface ? (int)sizeof(Klass*)/wordSize : 0) +
1055 (is_unsafe_anonymous ? (int)sizeof(Klass*)/wordSize : 0) +
1056 (has_stored_fingerprint ? (int)sizeof(uint64_t*)/wordSize : 0));
1057 }
1058 int size() const { return size(vtable_length(),
1059 itable_length(),
1060 nonstatic_oop_map_size(),
1061 is_interface(),
1062 is_unsafe_anonymous(),
1063 has_stored_fingerprint());
1064 }
1065 #if INCLUDE_SERVICES
1066 virtual void collect_statistics(KlassSizeStats *sz) const;
1067 #endif
1068
1069 intptr_t* start_of_itable() const { return (intptr_t*)start_of_vtable() + vtable_length(); }
1070 intptr_t* end_of_itable() const { return start_of_itable() + itable_length(); }
1071
1072 int itable_offset_in_words() const { return start_of_itable() - (intptr_t*)this; }
1073
1074 oop static_field_base_raw() { return java_mirror(); }
1075
1076 OopMapBlock* start_of_nonstatic_oop_maps() const {
1077 return (OopMapBlock*)(start_of_itable() + itable_length());
1078 }
1079
1080 Klass** end_of_nonstatic_oop_maps() const {
1081 return (Klass**)(start_of_nonstatic_oop_maps() +
1082 nonstatic_oop_map_count());
1083 }
1084
1085 Klass* volatile* adr_implementor() const {
1086 if (is_interface()) {
1087 return (Klass* volatile*)end_of_nonstatic_oop_maps();
1088 } else {
1089 return NULL;
1090 }
1091 };
1092
1093 InstanceKlass** adr_unsafe_anonymous_host() const {
1094 if (is_unsafe_anonymous()) {
1095 InstanceKlass** adr_impl = (InstanceKlass**)adr_implementor();
1104 }
1105
1106 address adr_fingerprint() const {
1107 if (has_stored_fingerprint()) {
1108 InstanceKlass** adr_host = adr_unsafe_anonymous_host();
1109 if (adr_host != NULL) {
1110 return (address)(adr_host + 1);
1111 }
1112
1113 Klass* volatile* adr_impl = adr_implementor();
1114 if (adr_impl != NULL) {
1115 return (address)(adr_impl + 1);
1116 }
1117
1118 return (address)end_of_nonstatic_oop_maps();
1119 } else {
1120 return NULL;
1121 }
1122 }
1123
1124 // Use this to return the size of an instance in heap words:
1125 int size_helper() const {
1126 return layout_helper_to_size_helper(layout_helper());
1127 }
1128
1129 // This bit is initialized in classFileParser.cpp.
1130 // It is false under any of the following conditions:
1131 // - the class is abstract (including any interface)
1132 // - the class has a finalizer (if !RegisterFinalizersAtInit)
1133 // - the class size is larger than FastAllocateSizeLimit
1134 // - the class is java/lang/Class, which cannot be allocated directly
1135 bool can_be_fastpath_allocated() const {
1136 return !layout_helper_needs_slow_path(layout_helper());
1137 }
1138
1139 // Java itable
1140 klassItable itable() const; // return klassItable wrapper
1141 Method* method_at_itable(Klass* holder, int index, TRAPS);
1142
1143 #if INCLUDE_JVMTI
1144 void adjust_default_methods(bool* trace_name_printed);
1145 #endif // INCLUDE_JVMTI
1154 // For RedefineClasses and class file parsing errors, we need to deallocate
1155 // instanceKlasses and the metadata they point to.
1156 void deallocate_contents(ClassLoaderData* loader_data);
1157 static void deallocate_methods(ClassLoaderData* loader_data,
1158 Array<Method*>* methods);
1159 void static deallocate_interfaces(ClassLoaderData* loader_data,
1160 const Klass* super_klass,
1161 Array<InstanceKlass*>* local_interfaces,
1162 Array<InstanceKlass*>* transitive_interfaces);
1163
1164 // The constant pool is on stack if any of the methods are executing or
1165 // referenced by handles.
1166 bool on_stack() const { return _constants->on_stack(); }
1167
1168 // callbacks for actions during class unloading
1169 static void unload_class(InstanceKlass* ik);
1170 static void release_C_heap_structures(InstanceKlass* ik);
1171
1172 // Naming
1173 const char* signature_name() const;
1174 static Symbol* package_from_name(const Symbol* name, TRAPS);
1175
1176 // Oop fields (and metadata) iterators
1177 //
1178 // The InstanceKlass iterators also visits the Object's klass.
1179
1180 // Forward iteration
1181 public:
1182 // Iterate over all oop fields in the oop maps.
1183 template <typename T, class OopClosureType>
1184 inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1185
1186 // Iterate over all oop fields and metadata.
1187 template <typename T, class OopClosureType>
1188 inline void oop_oop_iterate(oop obj, OopClosureType* closure);
1189
1190 // Iterate over all oop fields in one oop map.
1191 template <typename T, class OopClosureType>
1192 inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1193
1247 inline jmethodID* methods_jmethod_ids_acquire() const;
1248 inline void release_set_methods_jmethod_ids(jmethodID* jmeths);
1249
1250 // Lock during initialization
1251 public:
1252 // Lock for (1) initialization; (2) access to the ConstantPool of this class.
1253 // Must be one per class and it has to be a VM internal object so java code
1254 // cannot lock it (like the mirror).
1255 // It has to be an object not a Mutex because it's held through java calls.
1256 oop init_lock() const;
1257 private:
1258 void fence_and_clear_init_lock();
1259
1260 bool link_class_impl (TRAPS);
1261 bool verify_code (TRAPS);
1262 void initialize_impl (TRAPS);
1263 void initialize_super_interfaces (TRAPS);
1264 void eager_initialize_impl ();
1265 /* jni_id_for_impl for jfieldID only */
1266 JNIid* jni_id_for_impl (int offset);
1267
1268 // Returns the array class for the n'th dimension
1269 Klass* array_klass_impl(bool or_null, int n, TRAPS);
1270
1271 // Returns the array class with this class as element type
1272 Klass* array_klass_impl(bool or_null, TRAPS);
1273
1274 // find a local method (returns NULL if not found)
1275 Method* find_method_impl(const Symbol* name,
1276 const Symbol* signature,
1277 OverpassLookupMode overpass_mode,
1278 StaticLookupMode static_mode,
1279 PrivateLookupMode private_mode) const;
1280
1281 static Method* find_method_impl(const Array<Method*>* methods,
1282 const Symbol* name,
1283 const Symbol* signature,
1284 OverpassLookupMode overpass_mode,
1285 StaticLookupMode static_mode,
1286 PrivateLookupMode private_mode);
1287
1288 // Free CHeap allocated fields.
1289 void release_C_heap_structures();
1290
1291 #if INCLUDE_JVMTI
1292 // RedefineClasses support
|
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/classLoader.hpp"
29 #include "classfile/classLoaderData.hpp"
30 #include "classfile/moduleEntry.hpp"
31 #include "classfile/packageEntry.hpp"
32 #include "code/vmreg.hpp"
33 #include "memory/referenceType.hpp"
34 #include "oops/annotations.hpp"
35 #include "oops/constMethod.hpp"
36 #include "oops/fieldInfo.hpp"
37 #include "oops/instanceOop.hpp"
38 #include "oops/klassVtable.hpp"
39 #include "runtime/handles.hpp"
40 #include "runtime/os.hpp"
41 #include "utilities/accessFlags.hpp"
42 #include "utilities/align.hpp"
43 #include "utilities/macros.hpp"
44 #if INCLUDE_JFR
45 #include "jfr/support/jfrKlassExtension.hpp"
46 #endif
47
48
49 // An InstanceKlass is the VM level representation of a Java class.
50 // It contains all information needed for at class at execution runtime.
51
52 // InstanceKlass embedded field layout (after declared fields):
53 // [EMBEDDED Java vtable ] size in words = vtable_len
54 // [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size
55 // The embedded nonstatic oop-map blocks are short pairs (offset, length)
56 // indicating where oops are located in instances of this klass.
57 // [EMBEDDED implementor of the interface] only exist for interface
58 // [EMBEDDED unsafe_anonymous_host klass] only exist for an unsafe anonymous class (JSR 292 enabled)
59 // [EMBEDDED fingerprint ] only if should_store_fingerprint()==true
60 // [EMBEDDED ValueKlassFixedBlock] only if is a ValueKlass instance
61
62
63 // forward declaration for class -- see below for definition
64 #if INCLUDE_JVMTI
65 class BreakpointInfo;
66 #endif
67 class ClassFileParser;
68 class KlassDepChange;
69 class DependencyContext;
70 class fieldDescriptor;
71 class jniIdMapBase;
72 class JNIid;
73 class JvmtiCachedClassFieldMap;
74 class nmethodBucket;
75 class SuperTypeClosure;
76 class OopMapCache;
77 class BufferedValueTypeBlob;
78 class InterpreterOopMap;
79
80 // This is used in iterators below.
81 class FieldClosure: public StackObj {
82 public:
83 virtual void do_field(fieldDescriptor* fd) = 0;
84 };
85
86 #ifndef PRODUCT
87 // Print fields.
88 // If "obj" argument to constructor is NULL, prints static fields, otherwise prints non-static fields.
89 class FieldPrinter: public FieldClosure {
90 oop _obj;
91 outputStream* _st;
92 public:
93 FieldPrinter(outputStream* st, oop obj = NULL) : _obj(obj), _st(st) {}
94 void do_field(fieldDescriptor* fd);
95 };
96 #endif // !PRODUCT
97
98 // Describes where oops are located in instances of this klass.
99 class OopMapBlock {
100 public:
101 // Byte offset of the first oop mapped by this block.
102 int offset() const { return _offset; }
103 void set_offset(int offset) { _offset = offset; }
104
105 // Number of oops in this block.
106 uint count() const { return _count; }
107 void set_count(uint count) { _count = count; }
108
109 void increment_count(int diff) { _count += diff; }
110
111 int offset_span() const { return _count * heapOopSize; }
112
113 int end_offset() const {
114 return offset() + offset_span();
115 }
116
117 bool is_contiguous(int another_offset) const {
118 return another_offset == end_offset();
119 }
120
121 // sizeof(OopMapBlock) in words.
122 static const int size_in_words() {
123 return align_up((int)sizeof(OopMapBlock), wordSize) >>
124 LogBytesPerWord;
125 }
126
127 static int compare_offset(const OopMapBlock* a, const OopMapBlock* b) {
128 return a->offset() - b->offset();
129 }
130
131 private:
132 int _offset;
133 uint _count;
134 };
135
136 struct JvmtiCachedClassFileData;
137
138 class SigEntry;
139
140 class ValueKlassFixedBlock {
141 Array<SigEntry>** _extended_sig;
142 Array<VMRegPair>** _return_regs;
143 address* _pack_handler;
144 address* _unpack_handler;
145 int* _default_value_offset;
146
147 friend class ValueKlass;
148 };
149
150 class ValueTypes {
151 public:
152 u2 _class_info_index;
153 Symbol* _class_name;
154 };
155
156 class InstanceKlass: public Klass {
157 friend class VMStructs;
158 friend class JVMCIVMStructs;
159 friend class ClassFileParser;
160 friend class CompileReplay;
161
162 public:
163 static const KlassID ID = InstanceKlassID;
164
165 protected:
166 InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id = ID);
167
168 public:
169 InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
170
171 // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description
172 // of the class loading & initialization procedure, and the use of the states.
173 enum ClassState {
174 allocated, // allocated (but not yet linked)
175 loaded, // loaded and inserted in class hierarchy (but not linked yet)
203 // last two shorts [class_index, method_index] of the array. If only
204 // the InnerClasses attribute exists, the _inner_classes array length is
205 // number_of_inner_classes * 4. If the class has both InnerClasses
206 // and EnclosingMethod attributes the _inner_classes array length is
207 // number_of_inner_classes * 4 + enclosing_method_attribute_size.
208 Array<jushort>* _inner_classes;
209
210 // The NestMembers attribute. An array of shorts, where each is a
211 // class info index for the class that is a nest member. This data
212 // has not been validated.
213 Array<jushort>* _nest_members;
214
215 // The NestHost attribute. The class info index for the class
216 // that is the nest-host of this class. This data has not been validated.
217 jushort _nest_host_index;
218
219 // Resolved nest-host klass: either true nest-host or self if we are not nested.
220 // By always being set it makes nest-member access checks simpler.
221 InstanceKlass* _nest_host;
222
223 Array<ValueTypes>* _value_types;
224
225 // the source debug extension for this klass, NULL if not specified.
226 // Specified as UTF-8 string without terminating zero byte in the classfile,
227 // it is stored in the instanceklass as a NULL-terminated UTF-8 string
228 const char* _source_debug_extension;
229 // Array name derived from this class which needs unreferencing
230 // if this class is unloaded.
231 Symbol* _array_name;
232
233 // Number of heapOopSize words used by non-static fields in this klass
234 // (including inherited fields but after header_size()).
235 int _nonstatic_field_size;
236 int _static_field_size; // number words used by static fields (oop and non-oop) in this klass
237 // Constant pool index to the utf8 entry of the Generic signature,
238 // or 0 if none.
239 u2 _generic_signature_index;
240 // Constant pool index to the utf8 entry for the name of source file
241 // containing this klass, 0 if not specified.
242 u2 _source_file_name_index;
243 u2 _static_oop_field_count;// number of static oop fields in this klass
244 u2 _java_fields_count; // The number of declared Java fields
245 int _nonstatic_oop_map_size;// size in words of nonstatic oop map blocks
246
247 int _itable_len; // length of Java itable (in words)
248 // _is_marked_dependent can be set concurrently, thus cannot be part of the
249 // _misc_flags.
250 bool _is_marked_dependent; // used for marking during flushing and deoptimization
251
252 public:
253 enum {
254 _extra_is_being_redefined = 1 << 0, // used for locking redefinition
255 _extra_has_resolved_methods = 1 << 1, // resolved methods table entries added for this class
256 _extra_has_value_fields = 1 << 2, // has value fields and related embedded section is not empty
257 _extra_is_bufferable = 1 << 3 // value can be buffered out side of the Java heap
258 };
259
260 protected:
261 u1 _extra_flags;
262
263 // The low three bits of _misc_flags contains the kind field.
264 // This can be used to quickly discriminate among the five kinds of
265 // InstanceKlass.
266
267 static const unsigned _misc_kind_field_size = 3;
268 static const unsigned _misc_kind_field_pos = 0;
269 static const unsigned _misc_kind_field_mask = (1u << _misc_kind_field_size) - 1u;
270
271 static const unsigned _misc_kind_other = 0; // concrete InstanceKlass
272 static const unsigned _misc_kind_reference = 1; // InstanceRefKlass
273 static const unsigned _misc_kind_class_loader = 2; // InstanceClassLoaderKlass
274 static const unsigned _misc_kind_mirror = 3; // InstanceMirrorKlass
275 static const unsigned _misc_kind_value_type = 4; // ValueKlass
276
277 // Start after _misc_kind field.
278 enum {
279 _misc_rewritten = 1 << 3, // methods rewritten.
280 _misc_has_nonstatic_fields = 1 << 4, // for sizing with UseCompressedOops
281 _misc_should_verify_class = 1 << 5, // allow caching of preverification
282 _misc_is_unsafe_anonymous = 1 << 6, // has embedded _unsafe_anonymous_host field
283 _misc_is_contended = 1 << 7, // marked with contended annotation
284 _misc_has_nonstatic_concrete_methods = 1 << 8, // class/superclass/implemented interfaces has non-static, concrete methods
285 _misc_declares_nonstatic_concrete_methods = 1 << 9, // directly declares non-static, concrete methods
286 _misc_has_been_redefined = 1 << 10, // class has been redefined
287 _misc_has_passed_fingerprint_check = 1 << 11, // when this class was loaded, the fingerprint computed from its
288 // code source was found to be matching the value recorded by AOT.
289 _misc_is_scratch_class = 1 << 12, // class is the redefined scratch class
290 _misc_is_shared_boot_class = 1 << 13, // defining class loader is boot class loader
291 _misc_is_shared_platform_class = 1 << 14, // defining class loader is platform class loader
292 _misc_is_shared_app_class = 1 << 15 // defining class loader is app class loader
293 // u2 _misc_flags full (see _extra_flags)
294 };
295 u2 loader_type_bits() {
296 return _misc_is_shared_boot_class|_misc_is_shared_platform_class|_misc_is_shared_app_class;
297 }
298 u2 _misc_flags;
299 u2 _minor_version; // minor version number of class file
300 u2 _major_version; // major version number of class file
301 Thread* _init_thread; // Pointer to current thread doing initialization (to handle recusive initialization)
302 OopMapCache* volatile _oop_map_cache; // OopMapCache for all methods in the klass (allocated lazily)
303 JNIid* _jni_ids; // First JNI identifier for static fields in this class
304 jmethodID* volatile _methods_jmethod_ids; // jmethodIDs corresponding to method_idnum, or NULL if none
305 nmethodBucket* volatile _dep_context; // packed DependencyContext structure
306 uint64_t volatile _dep_context_last_cleaned;
307 nmethod* _osr_nmethods_head; // Head of list of on-stack replacement nmethods for this class
308 #if INCLUDE_JVMTI
309 BreakpointInfo* _breakpoints; // bpt lists, managed by Method*
310 // Linked instanceKlasses of previous versions
311 InstanceKlass* _previous_versions;
312 // JVMTI fields can be moved to their own structure - see 6315920
313 // JVMTI: cached class file, before retransformable agent modified it in CFLH
341 Array<int>* _method_ordering;
342 // Int array containing the vtable_indices for default_methods
343 // offset matches _default_methods offset
344 Array<int>* _default_vtable_indices;
345
346 // Instance and static variable information, starts with 6-tuples of shorts
347 // [access, name index, sig index, initval index, low_offset, high_offset]
348 // for all fields, followed by the generic signature data at the end of
349 // the array. Only fields with generic signature attributes have the generic
350 // signature data set in the array. The fields array looks like following:
351 //
352 // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
353 // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
354 // ...
355 // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
356 // [generic signature index]
357 // [generic signature index]
358 // ...
359 Array<u2>* _fields;
360
361 const ValueKlassFixedBlock* _adr_valueklass_fixed_block;
362
363 // embedded Java vtable follows here
364 // embedded Java itables follows here
365 // embedded static fields follows here
366 // embedded nonstatic oop-map blocks follows here
367 // embedded implementor of this interface follows here
368 // The embedded implementor only exists if the current klass is an
369 // iterface. The possible values of the implementor fall into following
370 // three cases:
371 // NULL: no implementor.
372 // A Klass* that's not itself: one implementor.
373 // Itself: more than one implementors.
374 // embedded unsafe_anonymous_host klass follows here
375 // The embedded host klass only exists in an unsafe anonymous class for
376 // dynamic language support (JSR 292 enabled). The host class grants
377 // its access privileges to this class also. The host class is either
378 // named, or a previously loaded unsafe anonymous class. A non-anonymous class
379 // or an anonymous class loaded through normal classloading does not
380 // have this embedded field.
381 //
382
412 case ClassLoader::APP_LOADER:
413 _misc_flags |= _misc_is_shared_app_class;
414 break;
415 default:
416 ShouldNotReachHere();
417 break;
418 }
419 }
420
421 bool has_nonstatic_fields() const {
422 return (_misc_flags & _misc_has_nonstatic_fields) != 0;
423 }
424 void set_has_nonstatic_fields(bool b) {
425 if (b) {
426 _misc_flags |= _misc_has_nonstatic_fields;
427 } else {
428 _misc_flags &= ~_misc_has_nonstatic_fields;
429 }
430 }
431
432 bool has_value_fields() const {
433 return (_extra_flags & _extra_has_value_fields) != 0;
434 }
435 void set_has_value_fields() {
436 _extra_flags |= _extra_has_value_fields;
437 }
438
439 // field sizes
440 int nonstatic_field_size() const { return _nonstatic_field_size; }
441 void set_nonstatic_field_size(int size) { _nonstatic_field_size = size; }
442
443 int static_field_size() const { return _static_field_size; }
444 void set_static_field_size(int size) { _static_field_size = size; }
445
446 int static_oop_field_count() const { return (int)_static_oop_field_count; }
447 void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
448
449 // Java itable
450 int itable_length() const { return _itable_len; }
451 void set_itable_length(int len) { _itable_len = len; }
452
453 // array klasses
454 Klass* array_klasses() const { return _array_klasses; }
455 inline Klass* array_klasses_acquire() const; // load with acquire semantics
456 void set_array_klasses(Klass* k) { _array_klasses = k; }
457 inline void release_set_array_klasses(Klass* k); // store with release semantics
458
481 Array<InstanceKlass*>* local_interfaces() const { return _local_interfaces; }
482 void set_local_interfaces(Array<InstanceKlass*>* a) {
483 guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
484 _local_interfaces = a; }
485
486 Array<InstanceKlass*>* transitive_interfaces() const { return _transitive_interfaces; }
487 void set_transitive_interfaces(Array<InstanceKlass*>* a) {
488 guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
489 _transitive_interfaces = a;
490 }
491
492 private:
493 friend class fieldDescriptor;
494 FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
495
496 public:
497 int field_offset (int index) const { return field(index)->offset(); }
498 int field_access_flags(int index) const { return field(index)->access_flags(); }
499 Symbol* field_name (int index) const { return field(index)->name(constants()); }
500 Symbol* field_signature (int index) const { return field(index)->signature(constants()); }
501 bool field_is_flattened(int index) const { return field(index)->is_flattened(); }
502
503 // Number of Java declared fields
504 int java_fields_count() const { return (int)_java_fields_count; }
505
506 Array<u2>* fields() const { return _fields; }
507 void set_fields(Array<u2>* f, u2 java_fields_count) {
508 guarantee(_fields == NULL || f == NULL, "Just checking");
509 _fields = f;
510 _java_fields_count = java_fields_count;
511 }
512
513 // inner classes
514 Array<u2>* inner_classes() const { return _inner_classes; }
515 void set_inner_classes(Array<u2>* f) { _inner_classes = f; }
516
517 // nest members
518 Array<u2>* nest_members() const { return _nest_members; }
519 void set_nest_members(Array<u2>* m) { _nest_members = m; }
520
521 // nest-host index
581 bool is_reentrant_initialization(Thread *thread) { return thread == _init_thread; }
582 ClassState init_state() { return (ClassState)_init_state; }
583 bool is_rewritten() const { return (_misc_flags & _misc_rewritten) != 0; }
584
585 // defineClass specified verification
586 bool should_verify_class() const {
587 return (_misc_flags & _misc_should_verify_class) != 0;
588 }
589 void set_should_verify_class(bool value) {
590 if (value) {
591 _misc_flags |= _misc_should_verify_class;
592 } else {
593 _misc_flags &= ~_misc_should_verify_class;
594 }
595 }
596
597 // marking
598 bool is_marked_dependent() const { return _is_marked_dependent; }
599 void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
600
601 static ByteSize extra_flags_offset() { return in_ByteSize(offset_of(InstanceKlass, _extra_flags)); }
602
603 // initialization (virtuals from Klass)
604 bool should_be_initialized() const; // means that initialize should be called
605 void initialize(TRAPS);
606 void link_class(TRAPS);
607 bool link_class_or_fail(TRAPS); // returns false on failure
608 void rewrite_class(TRAPS);
609 void link_methods(TRAPS);
610 Method* class_initializer() const;
611
612 // set the class to initialized if no static initializer is present
613 void eager_initialize(Thread *thread);
614
615 // reference type
616 ReferenceType reference_type() const { return (ReferenceType)_reference_type; }
617 void set_reference_type(ReferenceType t) {
618 assert(t == (u1)t, "overflow");
619 _reference_type = (u1)t;
620 }
621
622 // this class cp index
623 u2 this_class_index() const { return _this_class_index; }
624 void set_this_class_index(u2 index) { _this_class_index = index; }
625
626 static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
627
628 // find local field, returns true if found
629 bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
630 // find field in direct superinterfaces, returns the interface in which the field is defined
631 Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
632 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
633 Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
634 // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined
635 Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const;
636
637 // find a non-static or static field given its offset within the class.
638 bool contains_field_offset(int offset) {
639 return instanceOopDesc::contains_field_offset(offset, nonstatic_field_size(), is_value());
640 }
641
642 bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
643 bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
644
645 // find a local method (returns NULL if not found)
646 Method* find_method(const Symbol* name, const Symbol* signature) const;
647 static Method* find_method(const Array<Method*>* methods,
648 const Symbol* name,
649 const Symbol* signature);
650
651 // find a local method, but skip static methods
652 Method* find_instance_method(const Symbol* name, const Symbol* signature,
653 PrivateLookupMode private_mode = find_private) const;
654 static Method* find_instance_method(const Array<Method*>* methods,
655 const Symbol* name,
656 const Symbol* signature,
657 PrivateLookupMode private_mode = find_private);
658
659 // find a local method (returns NULL if not found)
776 void set_source_debug_extension(const char* array, int length);
777
778 // symbol unloading support (refcount already added)
779 Symbol* array_name() { return _array_name; }
780 void set_array_name(Symbol* name) { assert(_array_name == NULL || name == NULL, "name already created"); _array_name = name; }
781
782 // nonstatic oop-map blocks
783 static int nonstatic_oop_map_size(unsigned int oop_map_count) {
784 return oop_map_count * OopMapBlock::size_in_words();
785 }
786 unsigned int nonstatic_oop_map_count() const {
787 return _nonstatic_oop_map_size / OopMapBlock::size_in_words();
788 }
789 int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; }
790 void set_nonstatic_oop_map_size(int words) {
791 _nonstatic_oop_map_size = words;
792 }
793
794 #if INCLUDE_JVMTI
795 // Redefinition locking. Class can only be redefined by one thread at a time.
796 bool is_being_redefined() const {
797 return (_extra_flags & _extra_is_being_redefined);
798 }
799 void set_is_being_redefined(bool value) {
800 if (value) {
801 _extra_flags |= _extra_is_being_redefined;
802 } else {
803 _extra_flags &= ~_extra_is_being_redefined;
804 }
805 }
806
807 // RedefineClasses() support for previous versions:
808 void add_previous_version(InstanceKlass* ik, int emcp_method_count);
809 void purge_previous_version_list();
810
811 InstanceKlass* previous_versions() const { return _previous_versions; }
812 #else
813 InstanceKlass* previous_versions() const { return NULL; }
814 #endif
815
816 InstanceKlass* get_klass_version(int version) {
817 for (InstanceKlass* ik = this; ik != NULL; ik = ik->previous_versions()) {
818 if (ik->constants()->version() == version) {
819 return ik;
820 }
821 }
822 return NULL;
823 }
824
825 bool has_been_redefined() const {
839 _misc_flags &= ~_misc_has_passed_fingerprint_check;
840 }
841 }
842 bool supers_have_passed_fingerprint_checks();
843
844 static bool should_store_fingerprint(bool is_unsafe_anonymous);
845 bool should_store_fingerprint() const { return should_store_fingerprint(is_unsafe_anonymous()); }
846 bool has_stored_fingerprint() const;
847 uint64_t get_stored_fingerprint() const;
848 void store_fingerprint(uint64_t fingerprint);
849
850 bool is_scratch_class() const {
851 return (_misc_flags & _misc_is_scratch_class) != 0;
852 }
853
854 void set_is_scratch_class() {
855 _misc_flags |= _misc_is_scratch_class;
856 }
857
858 bool has_resolved_methods() const {
859 return (_extra_flags & _extra_has_resolved_methods) != 0;
860 }
861
862 void set_has_resolved_methods() {
863 _extra_flags |= _extra_has_resolved_methods;
864 }
865 private:
866
867 void set_kind(unsigned kind) {
868 assert(kind <= _misc_kind_field_mask, "Invalid InstanceKlass kind");
869 unsigned fmask = _misc_kind_field_mask << _misc_kind_field_pos;
870 unsigned flags = _misc_flags & ~fmask;
871 _misc_flags = (flags | (kind << _misc_kind_field_pos));
872 }
873
874 bool is_kind(unsigned desired) const {
875 unsigned kind = (_misc_flags >> _misc_kind_field_pos) & _misc_kind_field_mask;
876 return kind == desired;
877 }
878
879 public:
880
881 // Other is anything that is not one of the more specialized kinds of InstanceKlass.
882 bool is_other_instance_klass() const { return is_kind(_misc_kind_other); }
883 bool is_reference_instance_klass() const { return is_kind(_misc_kind_reference); }
884 bool is_mirror_instance_klass() const { return is_kind(_misc_kind_mirror); }
885 bool is_class_loader_instance_klass() const { return is_kind(_misc_kind_class_loader); }
886 bool is_value_type_klass() const { return is_kind(_misc_kind_value_type); }
887
888 #if INCLUDE_JVMTI
889
890 void init_previous_versions() {
891 _previous_versions = NULL;
892 }
893
894 private:
895 static bool _has_previous_versions;
896 public:
897 static void purge_previous_versions(InstanceKlass* ik) {
898 if (ik->has_been_redefined()) {
899 ik->purge_previous_version_list();
900 }
901 }
902
903 static bool has_previous_versions_and_reset();
904 static bool has_previous_versions() { return _has_previous_versions; }
905
906 // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation
1042
1043 // On-stack replacement support
1044 nmethod* osr_nmethods_head() const { return _osr_nmethods_head; };
1045 void set_osr_nmethods_head(nmethod* h) { _osr_nmethods_head = h; };
1046 void add_osr_nmethod(nmethod* n);
1047 bool remove_osr_nmethod(nmethod* n);
1048 int mark_osr_nmethods(const Method* m);
1049 nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
1050
1051 #if INCLUDE_JVMTI
1052 // Breakpoint support (see methods on Method* for details)
1053 BreakpointInfo* breakpoints() const { return _breakpoints; };
1054 void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
1055 #endif
1056
1057 // support for stub routines
1058 static ByteSize init_state_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
1059 JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)
1060 static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
1061
1062 static ByteSize adr_valueklass_fixed_block_offset() { return in_ByteSize(offset_of(InstanceKlass, _adr_valueklass_fixed_block)); }
1063
1064 // subclass/subinterface checks
1065 bool implements_interface(Klass* k) const;
1066 bool is_same_or_direct_interface(Klass* k) const;
1067
1068 #ifdef ASSERT
1069 // check whether this class or one of its superclasses was redefined
1070 bool has_redefined_this_or_super() const;
1071 #endif
1072
1073 // Access to the implementor of an interface.
1074 Klass* implementor() const;
1075 void set_implementor(Klass* k);
1076 int nof_implementors() const;
1077 void add_implementor(Klass* k); // k is a new class that implements this interface
1078 void init_implementor(); // initialize
1079
1080 // link this class into the implementors list of every interface it implements
1081 void process_interfaces(Thread *thread);
1082
1083 // virtual operations from Klass
1102
1103 static InstanceKlass* cast(Klass* k) {
1104 return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k)));
1105 }
1106
1107 static const InstanceKlass* cast(const Klass* k) {
1108 assert(k != NULL, "k should not be null");
1109 assert(k->is_instance_klass(), "cast to InstanceKlass");
1110 return static_cast<const InstanceKlass*>(k);
1111 }
1112
1113 virtual InstanceKlass* java_super() const {
1114 return (super() == NULL) ? NULL : cast(super());
1115 }
1116
1117 // Sizing (in words)
1118 static int header_size() { return sizeof(InstanceKlass)/wordSize; }
1119
1120 static int size(int vtable_length, int itable_length,
1121 int nonstatic_oop_map_size,
1122 bool is_interface, bool is_unsafe_anonymous, bool has_stored_fingerprint,
1123 int java_fields, bool is_value_type) {
1124 return align_metadata_size(header_size() +
1125 vtable_length +
1126 itable_length +
1127 nonstatic_oop_map_size +
1128 (is_interface ? (int)sizeof(Klass*)/wordSize : 0) +
1129 (is_unsafe_anonymous ? (int)sizeof(Klass*)/wordSize : 0) +
1130 (has_stored_fingerprint ? (int)sizeof(uint64_t*)/wordSize : 0) +
1131 (java_fields * (int)sizeof(Klass*)/wordSize) +
1132 (is_value_type ? (int)sizeof(ValueKlassFixedBlock) : 0));
1133 }
1134 int size() const { return size(vtable_length(),
1135 itable_length(),
1136 nonstatic_oop_map_size(),
1137 is_interface(),
1138 is_unsafe_anonymous(),
1139 has_stored_fingerprint(),
1140 has_value_fields() ? java_fields_count() : 0,
1141 is_value());
1142 }
1143 #if INCLUDE_SERVICES
1144 virtual void collect_statistics(KlassSizeStats *sz) const;
1145 #endif
1146
1147 intptr_t* start_of_itable() const { return (intptr_t*)start_of_vtable() + vtable_length(); }
1148 intptr_t* end_of_itable() const { return start_of_itable() + itable_length(); }
1149
1150 int itable_offset_in_words() const { return start_of_itable() - (intptr_t*)this; }
1151
1152 oop static_field_base_raw() { return java_mirror(); }
1153
1154 bool bounds_check(address addr, bool edge_ok = false, intptr_t size_in_bytes = -1) const PRODUCT_RETURN0;
1155
1156 OopMapBlock* start_of_nonstatic_oop_maps() const {
1157 return (OopMapBlock*)(start_of_itable() + itable_length());
1158 }
1159
1160 Klass** end_of_nonstatic_oop_maps() const {
1161 return (Klass**)(start_of_nonstatic_oop_maps() +
1162 nonstatic_oop_map_count());
1163 }
1164
1165 Klass* volatile* adr_implementor() const {
1166 if (is_interface()) {
1167 return (Klass* volatile*)end_of_nonstatic_oop_maps();
1168 } else {
1169 return NULL;
1170 }
1171 };
1172
1173 InstanceKlass** adr_unsafe_anonymous_host() const {
1174 if (is_unsafe_anonymous()) {
1175 InstanceKlass** adr_impl = (InstanceKlass**)adr_implementor();
1184 }
1185
1186 address adr_fingerprint() const {
1187 if (has_stored_fingerprint()) {
1188 InstanceKlass** adr_host = adr_unsafe_anonymous_host();
1189 if (adr_host != NULL) {
1190 return (address)(adr_host + 1);
1191 }
1192
1193 Klass* volatile* adr_impl = adr_implementor();
1194 if (adr_impl != NULL) {
1195 return (address)(adr_impl + 1);
1196 }
1197
1198 return (address)end_of_nonstatic_oop_maps();
1199 } else {
1200 return NULL;
1201 }
1202 }
1203
1204 address adr_value_fields_klasses() const {
1205 if (has_value_fields()) {
1206 address adr_fing = adr_fingerprint();
1207 if (adr_fing != NULL) {
1208 return adr_fingerprint() + sizeof(u8);
1209 }
1210
1211 InstanceKlass** adr_host = adr_unsafe_anonymous_host();
1212 if (adr_host != NULL) {
1213 return (address)(adr_host + 1);
1214 }
1215
1216 Klass* volatile* adr_impl = adr_implementor();
1217 if (adr_impl != NULL) {
1218 return (address)(adr_impl + 1);
1219 }
1220
1221 return (address)end_of_nonstatic_oop_maps();
1222 } else {
1223 return NULL;
1224 }
1225 }
1226
1227 Klass* get_value_field_klass(int idx) const {
1228 assert(has_value_fields(), "Sanity checking");
1229 Klass* k = ((Klass**)adr_value_fields_klasses())[idx];
1230 assert(k != NULL, "Should always be set before being read");
1231 assert(k->is_value(), "Must be a value type");
1232 return k;
1233 }
1234
1235 Klass* get_value_field_klass_or_null(int idx) const {
1236 assert(has_value_fields(), "Sanity checking");
1237 Klass* k = ((Klass**)adr_value_fields_klasses())[idx];
1238 assert(k == NULL || k->is_value(), "Must be a value type");
1239 return k;
1240 }
1241
1242 void set_value_field_klass(int idx, Klass* k) {
1243 assert(has_value_fields(), "Sanity checking");
1244 assert(k != NULL, "Should not be set to NULL");
1245 assert(((Klass**)adr_value_fields_klasses())[idx] == NULL, "Should not be set twice");
1246 ((Klass**)adr_value_fields_klasses())[idx] = k;
1247 }
1248
1249 // Use this to return the size of an instance in heap words:
1250 virtual int size_helper() const {
1251 return layout_helper_to_size_helper(layout_helper());
1252 }
1253
1254 // This bit is initialized in classFileParser.cpp.
1255 // It is false under any of the following conditions:
1256 // - the class is abstract (including any interface)
1257 // - the class has a finalizer (if !RegisterFinalizersAtInit)
1258 // - the class size is larger than FastAllocateSizeLimit
1259 // - the class is java/lang/Class, which cannot be allocated directly
1260 bool can_be_fastpath_allocated() const {
1261 return !layout_helper_needs_slow_path(layout_helper());
1262 }
1263
1264 // Java itable
1265 klassItable itable() const; // return klassItable wrapper
1266 Method* method_at_itable(Klass* holder, int index, TRAPS);
1267
1268 #if INCLUDE_JVMTI
1269 void adjust_default_methods(bool* trace_name_printed);
1270 #endif // INCLUDE_JVMTI
1279 // For RedefineClasses and class file parsing errors, we need to deallocate
1280 // instanceKlasses and the metadata they point to.
1281 void deallocate_contents(ClassLoaderData* loader_data);
1282 static void deallocate_methods(ClassLoaderData* loader_data,
1283 Array<Method*>* methods);
1284 void static deallocate_interfaces(ClassLoaderData* loader_data,
1285 const Klass* super_klass,
1286 Array<InstanceKlass*>* local_interfaces,
1287 Array<InstanceKlass*>* transitive_interfaces);
1288
1289 // The constant pool is on stack if any of the methods are executing or
1290 // referenced by handles.
1291 bool on_stack() const { return _constants->on_stack(); }
1292
1293 // callbacks for actions during class unloading
1294 static void unload_class(InstanceKlass* ik);
1295 static void release_C_heap_structures(InstanceKlass* ik);
1296
1297 // Naming
1298 const char* signature_name() const;
1299 const char* signature_name_of(char c) const;
1300 static Symbol* package_from_name(const Symbol* name, TRAPS);
1301
1302 // Oop fields (and metadata) iterators
1303 //
1304 // The InstanceKlass iterators also visits the Object's klass.
1305
1306 // Forward iteration
1307 public:
1308 // Iterate over all oop fields in the oop maps.
1309 template <typename T, class OopClosureType>
1310 inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1311
1312 // Iterate over all oop fields and metadata.
1313 template <typename T, class OopClosureType>
1314 inline void oop_oop_iterate(oop obj, OopClosureType* closure);
1315
1316 // Iterate over all oop fields in one oop map.
1317 template <typename T, class OopClosureType>
1318 inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1319
1373 inline jmethodID* methods_jmethod_ids_acquire() const;
1374 inline void release_set_methods_jmethod_ids(jmethodID* jmeths);
1375
1376 // Lock during initialization
1377 public:
1378 // Lock for (1) initialization; (2) access to the ConstantPool of this class.
1379 // Must be one per class and it has to be a VM internal object so java code
1380 // cannot lock it (like the mirror).
1381 // It has to be an object not a Mutex because it's held through java calls.
1382 oop init_lock() const;
1383 private:
1384 void fence_and_clear_init_lock();
1385
1386 bool link_class_impl (TRAPS);
1387 bool verify_code (TRAPS);
1388 void initialize_impl (TRAPS);
1389 void initialize_super_interfaces (TRAPS);
1390 void eager_initialize_impl ();
1391 /* jni_id_for_impl for jfieldID only */
1392 JNIid* jni_id_for_impl (int offset);
1393 protected:
1394 // Returns the array class for the n'th dimension
1395 virtual Klass* array_klass_impl(bool or_null, int n, TRAPS);
1396
1397 // Returns the array class with this class as element type
1398 virtual Klass* array_klass_impl(bool or_null, TRAPS);
1399
1400 private:
1401
1402 // find a local method (returns NULL if not found)
1403 Method* find_method_impl(const Symbol* name,
1404 const Symbol* signature,
1405 OverpassLookupMode overpass_mode,
1406 StaticLookupMode static_mode,
1407 PrivateLookupMode private_mode) const;
1408
1409 static Method* find_method_impl(const Array<Method*>* methods,
1410 const Symbol* name,
1411 const Symbol* signature,
1412 OverpassLookupMode overpass_mode,
1413 StaticLookupMode static_mode,
1414 PrivateLookupMode private_mode);
1415
1416 // Free CHeap allocated fields.
1417 void release_C_heap_structures();
1418
1419 #if INCLUDE_JVMTI
1420 // RedefineClasses support
|