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 "memory/referenceType.hpp"
29 #include "oops/annotations.hpp"
30 #include "oops/constMethod.hpp"
31 #include "oops/fieldInfo.hpp"
32 #include "oops/instanceKlassMiscStatus.hpp"
33 #include "oops/instanceOop.hpp"
34 #include "runtime/handles.hpp"
35 #include "utilities/accessFlags.hpp"
36 #include "utilities/align.hpp"
37 #include "utilities/macros.hpp"
38 #if INCLUDE_JFR
39 #include "jfr/support/jfrKlassExtension.hpp"
40 #endif
41
42 class klassItable;
43 class RecordComponent;
44
45 // An InstanceKlass is the VM level representation of a Java class.
46 // It contains all information needed for at class at execution runtime.
47
48 // InstanceKlass embedded field layout (after declared fields):
49 // [EMBEDDED Java vtable ] size in words = vtable_len
50 // [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size
51 // The embedded nonstatic oop-map blocks are short pairs (offset, length)
52 // indicating where oops are located in instances of this klass.
53 // [EMBEDDED implementor of the interface] only exist for interface
54
55
56 // forward declaration for class -- see below for definition
57 #if INCLUDE_JVMTI
58 class BreakpointInfo;
59 #endif
60 class ClassFileParser;
61 class ClassFileStream;
62 class KlassDepChange;
63 class DependencyContext;
64 class fieldDescriptor;
65 class jniIdMapBase;
66 class JNIid;
67 class JvmtiCachedClassFieldMap;
68 class nmethodBucket;
69 class OopMapCache;
70 class InterpreterOopMap;
71 class PackageEntry;
72 class ModuleEntry;
73
74 // This is used in iterators below.
75 class FieldClosure: public StackObj {
76 public:
77 virtual void do_field(fieldDescriptor* fd) = 0;
78 };
79
80 // Print fields.
81 // If "obj" argument to constructor is NULL, prints static fields, otherwise prints non-static fields.
82 class FieldPrinter: public FieldClosure {
83 oop _obj;
84 outputStream* _st;
85 public:
86 FieldPrinter(outputStream* st, oop obj = NULL) : _obj(obj), _st(st) {}
87 void do_field(fieldDescriptor* fd);
88 };
89
110 return another_offset == end_offset();
111 }
112
113 // sizeof(OopMapBlock) in words.
114 static const int size_in_words() {
115 return align_up((int)sizeof(OopMapBlock), wordSize) >>
116 LogBytesPerWord;
117 }
118
119 static int compare_offset(const OopMapBlock* a, const OopMapBlock* b) {
120 return a->offset() - b->offset();
121 }
122
123 private:
124 int _offset;
125 uint _count;
126 };
127
128 struct JvmtiCachedClassFileData;
129
130 class InstanceKlass: public Klass {
131 friend class VMStructs;
132 friend class JVMCIVMStructs;
133 friend class ClassFileParser;
134 friend class CompileReplay;
135
136 public:
137 static const KlassKind Kind = InstanceKlassKind;
138
139 protected:
140 InstanceKlass(const ClassFileParser& parser, KlassKind kind = Kind, ReferenceType reference_type = REF_NONE);
141
142 public:
143 InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
144
145 // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description
146 // of the class loading & initialization procedure, and the use of the states.
147 enum ClassState : u1 {
148 allocated, // allocated (but not yet linked)
149 loaded, // loaded and inserted in class hierarchy (but not linked yet)
150 being_linked, // currently running verifier and rewriter
151 linked, // successfully linked/verified (but not initialized yet)
152 being_initialized, // currently running class initializer
153 fully_initialized, // initialized (successful final state)
154 initialization_error // error happened during initialization
155 };
156
157 private:
158 static InstanceKlass* allocate_instance_klass(const ClassFileParser& parser, TRAPS);
159
160 protected:
161 // If you add a new field that points to any metaspace object, you
162 // must add this field to InstanceKlass::metaspace_pointers_do().
163
164 // Annotations for this class
165 Annotations* _annotations;
166 // Package this class is defined in
167 PackageEntry* _package_entry;
168 // Array classes holding elements of this class.
169 ObjArrayKlass* volatile _array_klasses;
170 // Constant pool for this class.
171 ConstantPool* _constants;
172 // The InnerClasses attribute and EnclosingMethod attribute. The
173 // _inner_classes is an array of shorts. If the class has InnerClasses
174 // attribute, then the _inner_classes array begins with 4-tuples of shorts
175 // [inner_class_info_index, outer_class_info_index,
176 // inner_name_index, inner_class_access_flags] for the InnerClasses
177 // attribute. If the EnclosingMethod attribute exists, it occupies the
178 // last two shorts [class_index, method_index] of the array. If only
179 // the InnerClasses attribute exists, the _inner_classes array length is
180 // number_of_inner_classes * 4. If the class has both InnerClasses
181 // and EnclosingMethod attributes the _inner_classes array length is
182 // number_of_inner_classes * 4 + enclosing_method_attribute_size.
183 Array<jushort>* _inner_classes;
184
185 // The NestMembers attribute. An array of shorts, where each is a
186 // class info index for the class that is a nest member. This data
187 // has not been validated.
188 Array<jushort>* _nest_members;
189
268 // Int array containing the original order of method in the class file (for JVMTI).
269 Array<int>* _method_ordering;
270 // Int array containing the vtable_indices for default_methods
271 // offset matches _default_methods offset
272 Array<int>* _default_vtable_indices;
273
274 // Instance and static variable information, starts with 6-tuples of shorts
275 // [access, name index, sig index, initval index, low_offset, high_offset]
276 // for all fields, followed by the generic signature data at the end of
277 // the array. Only fields with generic signature attributes have the generic
278 // signature data set in the array. The fields array looks like following:
279 //
280 // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
281 // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
282 // ...
283 // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
284 // [generic signature index]
285 // [generic signature index]
286 // ...
287 Array<u2>* _fields;
288
289 // embedded Java vtable follows here
290 // embedded Java itables follows here
291 // embedded static fields follows here
292 // embedded nonstatic oop-map blocks follows here
293 // embedded implementor of this interface follows here
294 // The embedded implementor only exists if the current klass is an
295 // interface. The possible values of the implementor fall into following
296 // three cases:
297 // NULL: no implementor.
298 // A Klass* that's not itself: one implementor.
299 // Itself: more than one implementors.
300 //
301
302 friend class SystemDictionary;
303
304 static bool _disable_method_binary_search;
305
306 // Controls finalizer registration
307 static bool _finalization_enabled;
322 bool is_shared_unregistered_class() const { return _misc_status.is_shared_unregistered_class(); }
323
324 // Check if the class can be shared in CDS
325 bool is_shareable() const;
326
327 bool shared_loading_failed() const { return _misc_status.shared_loading_failed(); }
328
329 void set_shared_loading_failed() { _misc_status.set_shared_loading_failed(true); }
330
331 #if INCLUDE_CDS
332 void set_shared_class_loader_type(s2 loader_type) { _misc_status.set_shared_class_loader_type(loader_type); }
333 void assign_class_loader_type() { _misc_status.assign_class_loader_type(_class_loader_data); }
334 #endif
335
336 bool has_nonstatic_fields() const { return _misc_status.has_nonstatic_fields(); }
337 void set_has_nonstatic_fields(bool b) { _misc_status.set_has_nonstatic_fields(b); }
338
339 bool has_localvariable_table() const { return _misc_status.has_localvariable_table(); }
340 void set_has_localvariable_table(bool b) { _misc_status.set_has_localvariable_table(b); }
341
342 // field sizes
343 int nonstatic_field_size() const { return _nonstatic_field_size; }
344 void set_nonstatic_field_size(int size) { _nonstatic_field_size = size; }
345
346 int static_field_size() const { return _static_field_size; }
347 void set_static_field_size(int size) { _static_field_size = size; }
348
349 int static_oop_field_count() const { return (int)_static_oop_field_count; }
350 void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
351
352 // Java itable
353 int itable_length() const { return _itable_len; }
354 void set_itable_length(int len) { _itable_len = len; }
355
356 // array klasses
357 ObjArrayKlass* array_klasses() const { return _array_klasses; }
358 inline ObjArrayKlass* array_klasses_acquire() const; // load with acquire semantics
359 inline void release_set_array_klasses(ObjArrayKlass* k); // store with release semantics
360
361 // methods
362 Array<Method*>* methods() const { return _methods; }
363 void set_methods(Array<Method*>* a) { _methods = a; }
364 Method* method_with_idnum(int idnum);
365 Method* method_with_orig_idnum(int idnum);
366 Method* method_with_orig_idnum(int idnum, int version);
367
368 // method ordering
369 Array<int>* method_ordering() const { return _method_ordering; }
370 void set_method_ordering(Array<int>* m) { _method_ordering = m; }
371 void copy_method_ordering(const intArray* m, TRAPS);
372
373 // default_methods
374 Array<Method*>* default_methods() const { return _default_methods; }
375 void set_default_methods(Array<Method*>* a) { _default_methods = a; }
376
377 // default method vtable_indices
378 Array<int>* default_vtable_indices() const { return _default_vtable_indices; }
379 void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; }
383 Array<InstanceKlass*>* local_interfaces() const { return _local_interfaces; }
384 void set_local_interfaces(Array<InstanceKlass*>* a) {
385 guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
386 _local_interfaces = a; }
387
388 Array<InstanceKlass*>* transitive_interfaces() const { return _transitive_interfaces; }
389 void set_transitive_interfaces(Array<InstanceKlass*>* a) {
390 guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
391 _transitive_interfaces = a;
392 }
393
394 private:
395 friend class fieldDescriptor;
396 FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
397
398 public:
399 int field_offset (int index) const { return field(index)->offset(); }
400 int field_access_flags(int index) const { return field(index)->access_flags(); }
401 Symbol* field_name (int index) const { return field(index)->name(constants()); }
402 Symbol* field_signature (int index) const { return field(index)->signature(constants()); }
403
404 // Number of Java declared fields
405 int java_fields_count() const { return (int)_java_fields_count; }
406
407 Array<u2>* fields() const { return _fields; }
408 void set_fields(Array<u2>* f, u2 java_fields_count) {
409 guarantee(_fields == NULL || f == NULL, "Just checking");
410 _fields = f;
411 _java_fields_count = java_fields_count;
412 }
413
414 // inner classes
415 Array<u2>* inner_classes() const { return _inner_classes; }
416 void set_inner_classes(Array<u2>* f) { _inner_classes = f; }
417
418 // nest members
419 Array<u2>* nest_members() const { return _nest_members; }
420 void set_nest_members(Array<u2>* m) { _nest_members = m; }
421
422 // nest-host index
423 jushort nest_host_index() const { return _nest_host_index; }
424 void set_nest_host_index(u2 i) { _nest_host_index = i; }
425 // dynamic nest member support
426 void set_nest_host(InstanceKlass* host);
427
428 // record components
429 Array<RecordComponent*>* record_components() const { return _record_components; }
430 void set_record_components(Array<RecordComponent*>* record_components) {
431 _record_components = record_components;
432 }
433 bool is_record() const;
525 }
526 ~LockLinkState() {
527 if (!_ik->is_linked()) {
528 // Reset to loaded if linking failed.
529 _ik->set_initialization_state_and_notify(loaded, _current);
530 }
531 }
532 };
533
534 // is this a sealed class
535 bool is_sealed() const;
536
537 // defineClass specified verification
538 bool should_verify_class() const { return _misc_status.should_verify_class(); }
539 void set_should_verify_class(bool value) { _misc_status.set_should_verify_class(value); }
540
541 // marking
542 bool is_marked_dependent() const { return _is_marked_dependent; }
543 void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
544
545 // initialization (virtuals from Klass)
546 bool should_be_initialized() const; // means that initialize should be called
547 void initialize(TRAPS);
548 void link_class(TRAPS);
549 bool link_class_or_fail(TRAPS); // returns false on failure
550 void rewrite_class(TRAPS);
551 void link_methods(TRAPS);
552 Method* class_initializer() const;
553
554 // reference type
555 ReferenceType reference_type() const { return (ReferenceType)_reference_type; }
556
557 // this class cp index
558 u2 this_class_index() const { return _this_class_index; }
559 void set_this_class_index(u2 index) { _this_class_index = index; }
560
561 static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
562
563 // find local field, returns true if found
564 bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
867
868 // On-stack replacement support
869 nmethod* osr_nmethods_head() const { return _osr_nmethods_head; };
870 void set_osr_nmethods_head(nmethod* h) { _osr_nmethods_head = h; };
871 void add_osr_nmethod(nmethod* n);
872 bool remove_osr_nmethod(nmethod* n);
873 int mark_osr_nmethods(const Method* m);
874 nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
875
876 #if INCLUDE_JVMTI
877 // Breakpoint support (see methods on Method* for details)
878 BreakpointInfo* breakpoints() const { return _breakpoints; };
879 void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
880 #endif
881
882 // support for stub routines
883 static ByteSize init_state_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
884 JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)
885 static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
886
887 // subclass/subinterface checks
888 bool implements_interface(Klass* k) const;
889 bool is_same_or_direct_interface(Klass* k) const;
890
891 #ifdef ASSERT
892 // check whether this class or one of its superclasses was redefined
893 bool has_redefined_this_or_super() const;
894 #endif
895
896 // Access to the implementor of an interface.
897 InstanceKlass* implementor() const;
898 void set_implementor(InstanceKlass* ik);
899 int nof_implementors() const;
900 void add_implementor(InstanceKlass* ik); // ik is a new class that implements this interface
901 void init_implementor(); // initialize
902
903 // link this class into the implementors list of every interface it implements
904 void process_interfaces();
905
906 // virtual operations from Klass
922
923 static InstanceKlass* cast(Klass* k) {
924 return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k)));
925 }
926
927 static const InstanceKlass* cast(const Klass* k) {
928 assert(k != NULL, "k should not be null");
929 assert(k->is_instance_klass(), "cast to InstanceKlass");
930 return static_cast<const InstanceKlass*>(k);
931 }
932
933 virtual InstanceKlass* java_super() const {
934 return (super() == NULL) ? NULL : cast(super());
935 }
936
937 // Sizing (in words)
938 static int header_size() { return sizeof(InstanceKlass)/wordSize; }
939
940 static int size(int vtable_length, int itable_length,
941 int nonstatic_oop_map_size,
942 bool is_interface) {
943 return align_metadata_size(header_size() +
944 vtable_length +
945 itable_length +
946 nonstatic_oop_map_size +
947 (is_interface ? (int)sizeof(Klass*)/wordSize : 0));
948 }
949
950 int size() const { return size(vtable_length(),
951 itable_length(),
952 nonstatic_oop_map_size(),
953 is_interface());
954 }
955
956
957 inline intptr_t* start_of_itable() const;
958 inline intptr_t* end_of_itable() const;
959 inline int itable_offset_in_words() const;
960 inline oop static_field_base_raw();
961
962 inline OopMapBlock* start_of_nonstatic_oop_maps() const;
963 inline Klass** end_of_nonstatic_oop_maps() const;
964
965 inline InstanceKlass* volatile* adr_implementor() const;
966
967 // Use this to return the size of an instance in heap words:
968 int size_helper() const {
969 return layout_helper_to_size_helper(layout_helper());
970 }
971
972 // This bit is initialized in classFileParser.cpp.
973 // It is false under any of the following conditions:
974 // - the class is abstract (including any interface)
975 // - the class has a finalizer (if !RegisterFinalizersAtInit)
976 // - the class size is larger than FastAllocateSizeLimit
977 // - the class is java/lang/Class, which cannot be allocated directly
978 bool can_be_fastpath_allocated() const {
979 return !layout_helper_needs_slow_path(layout_helper());
980 }
981
982 // Java itable
983 klassItable itable() const; // return klassItable wrapper
984 Method* method_at_itable(InstanceKlass* holder, int index, TRAPS);
985 Method* method_at_itable_or_null(InstanceKlass* holder, int index, bool& itable_entry_found);
986 int vtable_index_of_interface_method(Method* method);
987
988 #if INCLUDE_JVMTI
1002 static void deallocate_methods(ClassLoaderData* loader_data,
1003 Array<Method*>* methods);
1004 void static deallocate_interfaces(ClassLoaderData* loader_data,
1005 const Klass* super_klass,
1006 Array<InstanceKlass*>* local_interfaces,
1007 Array<InstanceKlass*>* transitive_interfaces);
1008 void static deallocate_record_components(ClassLoaderData* loader_data,
1009 Array<RecordComponent*>* record_component);
1010
1011 // The constant pool is on stack if any of the methods are executing or
1012 // referenced by handles.
1013 bool on_stack() const { return _constants->on_stack(); }
1014
1015 // callbacks for actions during class unloading
1016 static void unload_class(InstanceKlass* ik);
1017
1018 virtual void release_C_heap_structures(bool release_sub_metadata = true);
1019
1020 // Naming
1021 const char* signature_name() const;
1022
1023 // Oop fields (and metadata) iterators
1024 //
1025 // The InstanceKlass iterators also visits the Object's klass.
1026
1027 // Forward iteration
1028 public:
1029 // Iterate over all oop fields in the oop maps.
1030 template <typename T, class OopClosureType>
1031 inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1032
1033 // Iterate over all oop fields and metadata.
1034 template <typename T, class OopClosureType>
1035 inline void oop_oop_iterate(oop obj, OopClosureType* closure);
1036
1037 // Iterate over all oop fields in one oop map.
1038 template <typename T, class OopClosureType>
1039 inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1040
1041
1123 static Method* find_method_impl(const Array<Method*>* methods,
1124 const Symbol* name,
1125 const Symbol* signature,
1126 OverpassLookupMode overpass_mode,
1127 StaticLookupMode static_mode,
1128 PrivateLookupMode private_mode);
1129
1130 #if INCLUDE_JVMTI
1131 // RedefineClasses support
1132 void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; }
1133 void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count);
1134 #endif
1135 // log class name to classlist
1136 void log_to_classlist() const;
1137 public:
1138
1139 #if INCLUDE_CDS
1140 // CDS support - remove and restore oops from metadata. Oops are not shared.
1141 virtual void remove_unshareable_info();
1142 virtual void remove_java_mirror();
1143 void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
1144 void init_shared_package_entry();
1145 bool can_be_verified_at_dumptime() const;
1146 #endif
1147
1148 jint compute_modifier_flags() const;
1149
1150 public:
1151 // JVMTI support
1152 jint jvmti_class_status() const;
1153
1154 virtual void metaspace_pointers_do(MetaspaceClosure* iter);
1155
1156 public:
1157 // Printing
1158 void print_on(outputStream* st) const;
1159 void print_value_on(outputStream* st) const;
1160
1161 void oop_print_value_on(oop obj, outputStream* st);
1162
1163 void oop_print_on (oop obj, outputStream* st);
|
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 "code/vmreg.hpp"
29 #include "memory/referenceType.hpp"
30 #include "oops/annotations.hpp"
31 #include "oops/constMethod.hpp"
32 #include "oops/fieldInfo.hpp"
33 #include "oops/instanceKlassMiscStatus.hpp"
34 #include "oops/instanceOop.hpp"
35 #include "runtime/handles.hpp"
36 #include "utilities/accessFlags.hpp"
37 #include "utilities/align.hpp"
38 #include "utilities/macros.hpp"
39 #if INCLUDE_JFR
40 #include "jfr/support/jfrKlassExtension.hpp"
41 #endif
42
43 class klassItable;
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 inline_type_field_klasses] only if has_inline_fields() == true
56 // [EMBEDDED InlineKlassFixedBlock] only if is an InlineKlass instance
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 BufferedInlineTypeBlob;
74 class InterpreterOopMap;
75 class PackageEntry;
76 class ModuleEntry;
77
78 // This is used in iterators below.
79 class FieldClosure: public StackObj {
80 public:
81 virtual void do_field(fieldDescriptor* fd) = 0;
82 };
83
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
114 return another_offset == end_offset();
115 }
116
117 // sizeof(OopMapBlock) in words.
118 static const int size_in_words() {
119 return align_up((int)sizeof(OopMapBlock), wordSize) >>
120 LogBytesPerWord;
121 }
122
123 static int compare_offset(const OopMapBlock* a, const OopMapBlock* b) {
124 return a->offset() - b->offset();
125 }
126
127 private:
128 int _offset;
129 uint _count;
130 };
131
132 struct JvmtiCachedClassFileData;
133
134 class SigEntry;
135
136 class InlineKlassFixedBlock {
137 Array<SigEntry>** _extended_sig;
138 Array<VMRegPair>** _return_regs;
139 address* _pack_handler;
140 address* _pack_handler_jobject;
141 address* _unpack_handler;
142 int* _default_value_offset;
143 ArrayKlass** _null_free_inline_array_klasses;
144 int _alignment;
145 int _first_field_offset;
146 int _exact_size_in_bytes;
147
148 friend class InlineKlass;
149 };
150
151 class InstanceKlass: public Klass {
152 friend class VMStructs;
153 friend class JVMCIVMStructs;
154 friend class ClassFileParser;
155 friend class CompileReplay;
156 friend class TemplateTable;
157
158 public:
159 static const KlassKind Kind = InstanceKlassKind;
160
161 protected:
162 InstanceKlass(const ClassFileParser& parser, KlassKind kind = Kind, ReferenceType reference_type = REF_NONE);
163
164 public:
165 InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
166
167 // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description
168 // of the class loading & initialization procedure, and the use of the states.
169 enum ClassState : u1 {
170 allocated, // allocated (but not yet linked)
171 loaded, // loaded and inserted in class hierarchy (but not linked yet)
172 being_linked, // currently running verifier and rewriter
173 linked, // successfully linked/verified (but not initialized yet)
174 being_initialized, // currently running class initializer
175 fully_initialized, // initialized (successful final state)
176 initialization_error // error happened during initialization
177 };
178
179 private:
180 static InstanceKlass* allocate_instance_klass(const ClassFileParser& parser, TRAPS);
181
182 protected:
183 // If you add a new field that points to any metaspace object, you
184 // must add this field to InstanceKlass::metaspace_pointers_do().
185
186 // Annotations for this class
187 Annotations* _annotations;
188 // Package this class is defined in
189 PackageEntry* _package_entry;
190 // Array classes holding elements of this class.
191 ArrayKlass* volatile _array_klasses;
192 // Constant pool for this class.
193 ConstantPool* _constants;
194 // The InnerClasses attribute and EnclosingMethod attribute. The
195 // _inner_classes is an array of shorts. If the class has InnerClasses
196 // attribute, then the _inner_classes array begins with 4-tuples of shorts
197 // [inner_class_info_index, outer_class_info_index,
198 // inner_name_index, inner_class_access_flags] for the InnerClasses
199 // attribute. If the EnclosingMethod attribute exists, it occupies the
200 // last two shorts [class_index, method_index] of the array. If only
201 // the InnerClasses attribute exists, the _inner_classes array length is
202 // number_of_inner_classes * 4. If the class has both InnerClasses
203 // and EnclosingMethod attributes the _inner_classes array length is
204 // number_of_inner_classes * 4 + enclosing_method_attribute_size.
205 Array<jushort>* _inner_classes;
206
207 // The NestMembers attribute. An array of shorts, where each is a
208 // class info index for the class that is a nest member. This data
209 // has not been validated.
210 Array<jushort>* _nest_members;
211
290 // Int array containing the original order of method in the class file (for JVMTI).
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 const Klass** _inline_type_field_klasses; // For "inline class" fields, NULL if none present
311 Array<u2>* _preload_classes;
312 const InlineKlassFixedBlock* _adr_inlineklass_fixed_block;
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 // interface. 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 //
326
327 friend class SystemDictionary;
328
329 static bool _disable_method_binary_search;
330
331 // Controls finalizer registration
332 static bool _finalization_enabled;
347 bool is_shared_unregistered_class() const { return _misc_status.is_shared_unregistered_class(); }
348
349 // Check if the class can be shared in CDS
350 bool is_shareable() const;
351
352 bool shared_loading_failed() const { return _misc_status.shared_loading_failed(); }
353
354 void set_shared_loading_failed() { _misc_status.set_shared_loading_failed(true); }
355
356 #if INCLUDE_CDS
357 void set_shared_class_loader_type(s2 loader_type) { _misc_status.set_shared_class_loader_type(loader_type); }
358 void assign_class_loader_type() { _misc_status.assign_class_loader_type(_class_loader_data); }
359 #endif
360
361 bool has_nonstatic_fields() const { return _misc_status.has_nonstatic_fields(); }
362 void set_has_nonstatic_fields(bool b) { _misc_status.set_has_nonstatic_fields(b); }
363
364 bool has_localvariable_table() const { return _misc_status.has_localvariable_table(); }
365 void set_has_localvariable_table(bool b) { _misc_status.set_has_localvariable_table(b); }
366
367 bool has_inline_type_fields() const { return _misc_status.has_inline_type_fields(); }
368 void set_has_inline_type_fields() { _misc_status.set_has_inline_type_fields(true); }
369
370 bool is_empty_inline_type() const { return _misc_status.is_empty_inline_type(); }
371 void set_is_empty_inline_type() { _misc_status.set_is_empty_inline_type(true); }
372
373 // Note: The naturally_atomic property only applies to
374 // inline classes; it is never true on identity classes.
375 // The bit is placed on instanceKlass for convenience.
376
377 // Query if h/w provides atomic load/store for instances.
378 bool is_naturally_atomic() const { return _misc_status.is_naturally_atomic(); }
379 void set_is_naturally_atomic() { _misc_status.set_is_naturally_atomic(true); }
380
381 // Query if this class implements jl.NonTearable or was
382 // mentioned in the JVM option ForceNonTearable.
383 // This bit can occur anywhere, but is only significant
384 // for inline classes *and* their super types.
385 // It inherits from supers along with NonTearable.
386 bool is_declared_atomic() const { return _misc_status.is_declared_atomic(); }
387 void set_is_declared_atomic() { _misc_status.set_is_declared_atomic(true); }
388
389 bool carries_value_modifier() const { return _misc_status.carries_value_modifier(); }
390 void set_carries_value_modifier() { _misc_status.set_carries_value_modifier(true); }
391
392 bool carries_identity_modifier() const { return _misc_status.carries_identity_modifier(); }
393 void set_carries_identity_modifier() { _misc_status.set_carries_identity_modifier(true); }
394
395 // field sizes
396 int nonstatic_field_size() const { return _nonstatic_field_size; }
397 void set_nonstatic_field_size(int size) { _nonstatic_field_size = size; }
398
399 int static_field_size() const { return _static_field_size; }
400 void set_static_field_size(int size) { _static_field_size = size; }
401
402 int static_oop_field_count() const { return (int)_static_oop_field_count; }
403 void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
404
405 // Java itable
406 int itable_length() const { return _itable_len; }
407 void set_itable_length(int len) { _itable_len = len; }
408
409 // array klasses
410 ArrayKlass* array_klasses() const { return _array_klasses; }
411 inline ArrayKlass* array_klasses_acquire() const; // load with acquire semantics
412 inline void release_set_array_klasses(ArrayKlass* k); // store with release semantics
413
414 // methods
415 Array<Method*>* methods() const { return _methods; }
416 void set_methods(Array<Method*>* a) { _methods = a; }
417 Method* method_with_idnum(int idnum);
418 Method* method_with_orig_idnum(int idnum);
419 Method* method_with_orig_idnum(int idnum, int version);
420
421 // method ordering
422 Array<int>* method_ordering() const { return _method_ordering; }
423 void set_method_ordering(Array<int>* m) { _method_ordering = m; }
424 void copy_method_ordering(const intArray* m, TRAPS);
425
426 // default_methods
427 Array<Method*>* default_methods() const { return _default_methods; }
428 void set_default_methods(Array<Method*>* a) { _default_methods = a; }
429
430 // default method vtable_indices
431 Array<int>* default_vtable_indices() const { return _default_vtable_indices; }
432 void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; }
436 Array<InstanceKlass*>* local_interfaces() const { return _local_interfaces; }
437 void set_local_interfaces(Array<InstanceKlass*>* a) {
438 guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
439 _local_interfaces = a; }
440
441 Array<InstanceKlass*>* transitive_interfaces() const { return _transitive_interfaces; }
442 void set_transitive_interfaces(Array<InstanceKlass*>* a) {
443 guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
444 _transitive_interfaces = a;
445 }
446
447 private:
448 friend class fieldDescriptor;
449 FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
450
451 public:
452 int field_offset (int index) const { return field(index)->offset(); }
453 int field_access_flags(int index) const { return field(index)->access_flags(); }
454 Symbol* field_name (int index) const { return field(index)->name(constants()); }
455 Symbol* field_signature (int index) const { return field(index)->signature(constants()); }
456 bool field_is_inlined(int index) const { return field(index)->is_inlined(); }
457 bool field_is_null_free_inline_type(int index) const;
458
459 // Number of Java declared fields
460 int java_fields_count() const { return (int)_java_fields_count; }
461
462 Array<u2>* fields() const { return _fields; }
463 void set_fields(Array<u2>* f, u2 java_fields_count) {
464 guarantee(_fields == NULL || f == NULL, "Just checking");
465 _fields = f;
466 _java_fields_count = java_fields_count;
467 }
468
469 Array<u2>* preload_classes() const { return _preload_classes; }
470 void set_preload_classes(Array<u2>* c) { _preload_classes = c; }
471
472 // inner classes
473 Array<u2>* inner_classes() const { return _inner_classes; }
474 void set_inner_classes(Array<u2>* f) { _inner_classes = f; }
475
476 // nest members
477 Array<u2>* nest_members() const { return _nest_members; }
478 void set_nest_members(Array<u2>* m) { _nest_members = m; }
479
480 // nest-host index
481 jushort nest_host_index() const { return _nest_host_index; }
482 void set_nest_host_index(u2 i) { _nest_host_index = i; }
483 // dynamic nest member support
484 void set_nest_host(InstanceKlass* host);
485
486 // record components
487 Array<RecordComponent*>* record_components() const { return _record_components; }
488 void set_record_components(Array<RecordComponent*>* record_components) {
489 _record_components = record_components;
490 }
491 bool is_record() const;
583 }
584 ~LockLinkState() {
585 if (!_ik->is_linked()) {
586 // Reset to loaded if linking failed.
587 _ik->set_initialization_state_and_notify(loaded, _current);
588 }
589 }
590 };
591
592 // is this a sealed class
593 bool is_sealed() const;
594
595 // defineClass specified verification
596 bool should_verify_class() const { return _misc_status.should_verify_class(); }
597 void set_should_verify_class(bool value) { _misc_status.set_should_verify_class(value); }
598
599 // marking
600 bool is_marked_dependent() const { return _is_marked_dependent; }
601 void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
602
603 static ByteSize kind_offset() { return in_ByteSize(offset_of(InstanceKlass, _kind)); }
604 static ByteSize misc_status_offset() { return in_ByteSize(offset_of(InstanceKlass, _misc_status)); }
605
606 // initialization (virtuals from Klass)
607 bool should_be_initialized() const; // means that initialize should be called
608 void initialize(TRAPS);
609 void link_class(TRAPS);
610 bool link_class_or_fail(TRAPS); // returns false on failure
611 void rewrite_class(TRAPS);
612 void link_methods(TRAPS);
613 Method* class_initializer() const;
614
615 // reference type
616 ReferenceType reference_type() const { return (ReferenceType)_reference_type; }
617
618 // this class cp index
619 u2 this_class_index() const { return _this_class_index; }
620 void set_this_class_index(u2 index) { _this_class_index = index; }
621
622 static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
623
624 // find local field, returns true if found
625 bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
928
929 // On-stack replacement support
930 nmethod* osr_nmethods_head() const { return _osr_nmethods_head; };
931 void set_osr_nmethods_head(nmethod* h) { _osr_nmethods_head = h; };
932 void add_osr_nmethod(nmethod* n);
933 bool remove_osr_nmethod(nmethod* n);
934 int mark_osr_nmethods(const Method* m);
935 nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
936
937 #if INCLUDE_JVMTI
938 // Breakpoint support (see methods on Method* for details)
939 BreakpointInfo* breakpoints() const { return _breakpoints; };
940 void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
941 #endif
942
943 // support for stub routines
944 static ByteSize init_state_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
945 JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)
946 static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
947
948 static ByteSize inline_type_field_klasses_offset() { return in_ByteSize(offset_of(InstanceKlass, _inline_type_field_klasses)); }
949 static ByteSize adr_inlineklass_fixed_block_offset() { return in_ByteSize(offset_of(InstanceKlass, _adr_inlineklass_fixed_block)); }
950
951 // subclass/subinterface checks
952 bool implements_interface(Klass* k) const;
953 bool is_same_or_direct_interface(Klass* k) const;
954
955 #ifdef ASSERT
956 // check whether this class or one of its superclasses was redefined
957 bool has_redefined_this_or_super() const;
958 #endif
959
960 // Access to the implementor of an interface.
961 InstanceKlass* implementor() const;
962 void set_implementor(InstanceKlass* ik);
963 int nof_implementors() const;
964 void add_implementor(InstanceKlass* ik); // ik is a new class that implements this interface
965 void init_implementor(); // initialize
966
967 // link this class into the implementors list of every interface it implements
968 void process_interfaces();
969
970 // virtual operations from Klass
986
987 static InstanceKlass* cast(Klass* k) {
988 return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k)));
989 }
990
991 static const InstanceKlass* cast(const Klass* k) {
992 assert(k != NULL, "k should not be null");
993 assert(k->is_instance_klass(), "cast to InstanceKlass");
994 return static_cast<const InstanceKlass*>(k);
995 }
996
997 virtual InstanceKlass* java_super() const {
998 return (super() == NULL) ? NULL : cast(super());
999 }
1000
1001 // Sizing (in words)
1002 static int header_size() { return sizeof(InstanceKlass)/wordSize; }
1003
1004 static int size(int vtable_length, int itable_length,
1005 int nonstatic_oop_map_size,
1006 bool is_interface,
1007 int java_fields, bool is_inline_type) {
1008 return align_metadata_size(header_size() +
1009 vtable_length +
1010 itable_length +
1011 nonstatic_oop_map_size +
1012 (is_interface ? (int)sizeof(Klass*)/wordSize : 0) +
1013 (java_fields * (int)sizeof(Klass*)/wordSize) +
1014 (is_inline_type ? (int)sizeof(InlineKlassFixedBlock) : 0));
1015 }
1016
1017 int size() const { return size(vtable_length(),
1018 itable_length(),
1019 nonstatic_oop_map_size(),
1020 is_interface(),
1021 has_inline_type_fields() ? java_fields_count() : 0,
1022 is_inline_klass());
1023 }
1024
1025
1026 inline intptr_t* start_of_itable() const;
1027 inline intptr_t* end_of_itable() const;
1028 inline int itable_offset_in_words() const;
1029 inline oop static_field_base_raw();
1030 bool bounds_check(address addr, bool edge_ok = false, intptr_t size_in_bytes = -1) const PRODUCT_RETURN0;
1031
1032 inline OopMapBlock* start_of_nonstatic_oop_maps() const;
1033 inline Klass** end_of_nonstatic_oop_maps() const;
1034
1035 inline InstanceKlass* volatile* adr_implementor() const;
1036
1037 inline address adr_inline_type_field_klasses() const;
1038 inline Klass* get_inline_type_field_klass(int idx) const;
1039 inline Klass* get_inline_type_field_klass_or_null(int idx) const;
1040 inline void set_inline_type_field_klass(int idx, Klass* k);
1041 inline void reset_inline_type_field_klass(int idx);
1042
1043 // Use this to return the size of an instance in heap words:
1044 virtual int size_helper() const {
1045 return layout_helper_to_size_helper(layout_helper());
1046 }
1047
1048 // This bit is initialized in classFileParser.cpp.
1049 // It is false under any of the following conditions:
1050 // - the class is abstract (including any interface)
1051 // - the class has a finalizer (if !RegisterFinalizersAtInit)
1052 // - the class size is larger than FastAllocateSizeLimit
1053 // - the class is java/lang/Class, which cannot be allocated directly
1054 bool can_be_fastpath_allocated() const {
1055 return !layout_helper_needs_slow_path(layout_helper());
1056 }
1057
1058 // Java itable
1059 klassItable itable() const; // return klassItable wrapper
1060 Method* method_at_itable(InstanceKlass* holder, int index, TRAPS);
1061 Method* method_at_itable_or_null(InstanceKlass* holder, int index, bool& itable_entry_found);
1062 int vtable_index_of_interface_method(Method* method);
1063
1064 #if INCLUDE_JVMTI
1078 static void deallocate_methods(ClassLoaderData* loader_data,
1079 Array<Method*>* methods);
1080 void static deallocate_interfaces(ClassLoaderData* loader_data,
1081 const Klass* super_klass,
1082 Array<InstanceKlass*>* local_interfaces,
1083 Array<InstanceKlass*>* transitive_interfaces);
1084 void static deallocate_record_components(ClassLoaderData* loader_data,
1085 Array<RecordComponent*>* record_component);
1086
1087 // The constant pool is on stack if any of the methods are executing or
1088 // referenced by handles.
1089 bool on_stack() const { return _constants->on_stack(); }
1090
1091 // callbacks for actions during class unloading
1092 static void unload_class(InstanceKlass* ik);
1093
1094 virtual void release_C_heap_structures(bool release_sub_metadata = true);
1095
1096 // Naming
1097 const char* signature_name() const;
1098 const char* signature_name_of_carrier(char c) const;
1099
1100 // Oop fields (and metadata) iterators
1101 //
1102 // The InstanceKlass iterators also visits the Object's klass.
1103
1104 // Forward iteration
1105 public:
1106 // Iterate over all oop fields in the oop maps.
1107 template <typename T, class OopClosureType>
1108 inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1109
1110 // Iterate over all oop fields and metadata.
1111 template <typename T, class OopClosureType>
1112 inline void oop_oop_iterate(oop obj, OopClosureType* closure);
1113
1114 // Iterate over all oop fields in one oop map.
1115 template <typename T, class OopClosureType>
1116 inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1117
1118
1200 static Method* find_method_impl(const Array<Method*>* methods,
1201 const Symbol* name,
1202 const Symbol* signature,
1203 OverpassLookupMode overpass_mode,
1204 StaticLookupMode static_mode,
1205 PrivateLookupMode private_mode);
1206
1207 #if INCLUDE_JVMTI
1208 // RedefineClasses support
1209 void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; }
1210 void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count);
1211 #endif
1212 // log class name to classlist
1213 void log_to_classlist() const;
1214 public:
1215
1216 #if INCLUDE_CDS
1217 // CDS support - remove and restore oops from metadata. Oops are not shared.
1218 virtual void remove_unshareable_info();
1219 virtual void remove_java_mirror();
1220 virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
1221 void init_shared_package_entry();
1222 bool can_be_verified_at_dumptime() const;
1223 #endif
1224
1225 jint compute_modifier_flags() const;
1226
1227 public:
1228 // JVMTI support
1229 jint jvmti_class_status() const;
1230
1231 virtual void metaspace_pointers_do(MetaspaceClosure* iter);
1232
1233 public:
1234 // Printing
1235 void print_on(outputStream* st) const;
1236 void print_value_on(outputStream* st) const;
1237
1238 void oop_print_value_on(oop obj, outputStream* st);
1239
1240 void oop_print_on (oop obj, outputStream* st);
|