1 /* 2 * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_OOPS_INSTANCEKLASS_HPP 26 #define SHARE_OOPS_INSTANCEKLASS_HPP 27 28 #include "classfile/classLoaderData.hpp" 29 #include "code/vmreg.hpp" 30 #include "memory/referenceType.hpp" 31 #include "oops/annotations.hpp" 32 #include "oops/constMethod.hpp" 33 #include "oops/fieldInfo.hpp" 34 #include "oops/instanceOop.hpp" 35 #include "oops/klassVtable.hpp" 36 #include "runtime/handles.hpp" 37 #include "runtime/os.hpp" 38 #include "utilities/accessFlags.hpp" 39 #include "utilities/align.hpp" 40 #include "utilities/macros.hpp" 41 #if INCLUDE_JFR 42 #include "jfr/support/jfrKlassExtension.hpp" 43 #endif 44 45 class RecordComponent; 46 47 // An InstanceKlass is the VM level representation of a Java class. 48 // It contains all information needed for at class at execution runtime. 49 50 // InstanceKlass embedded field layout (after declared fields): 51 // [EMBEDDED Java vtable ] size in words = vtable_len 52 // [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size 53 // The embedded nonstatic oop-map blocks are short pairs (offset, length) 54 // indicating where oops are located in instances of this klass. 55 // [EMBEDDED implementor of the interface] only exist for interface 56 // [EMBEDDED unsafe_anonymous_host klass] only exist for an unsafe anonymous class (JSR 292 enabled) 57 // [EMBEDDED fingerprint ] only if should_store_fingerprint()==true 58 // [EMBEDDED ValueKlassFixedBlock] only if is a ValueKlass instance 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 ClassFileStream; 67 class KlassDepChange; 68 class DependencyContext; 69 class fieldDescriptor; 70 class jniIdMapBase; 71 class JNIid; 72 class JvmtiCachedClassFieldMap; 73 class nmethodBucket; 74 class OopMapCache; 75 class BufferedValueTypeBlob; 76 class InterpreterOopMap; 77 class PackageEntry; 78 class ModuleEntry; 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* _pack_handler_jobject; 145 address* _unpack_handler; 146 int* _default_value_offset; 147 Klass** _value_array_klass; 148 int _alignment; 149 int _first_field_offset; 150 int _exact_size_in_bytes; 151 152 friend class ValueKlass; 153 }; 154 155 class ValueTypes { 156 public: 157 u2 _class_info_index; 158 Symbol* _class_name; 159 }; 160 161 class InstanceKlass: public Klass { 162 friend class VMStructs; 163 friend class JVMCIVMStructs; 164 friend class ClassFileParser; 165 friend class CompileReplay; 166 167 public: 168 static const KlassID ID = InstanceKlassID; 169 170 protected: 171 InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id = ID); 172 173 public: 174 InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); } 175 176 // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description 177 // of the class loading & initialization procedure, and the use of the states. 178 enum ClassState { 179 allocated, // allocated (but not yet linked) 180 loaded, // loaded and inserted in class hierarchy (but not linked yet) 181 linked, // successfully linked/verified (but not initialized yet) 182 being_initialized, // currently running class initializer 183 fully_initialized, // initialized (successful final state) 184 initialization_error // error happened during initialization 185 }; 186 187 private: 188 static InstanceKlass* allocate_instance_klass(const ClassFileParser& parser, TRAPS); 189 190 protected: 191 // If you add a new field that points to any metaspace object, you 192 // must add this field to InstanceKlass::metaspace_pointers_do(). 193 194 // Annotations for this class 195 Annotations* _annotations; 196 // Package this class is defined in 197 PackageEntry* _package_entry; 198 // Array classes holding elements of this class. 199 Klass* volatile _array_klasses; 200 // Constant pool for this class. 201 ConstantPool* _constants; 202 // The InnerClasses attribute and EnclosingMethod attribute. The 203 // _inner_classes is an array of shorts. If the class has InnerClasses 204 // attribute, then the _inner_classes array begins with 4-tuples of shorts 205 // [inner_class_info_index, outer_class_info_index, 206 // inner_name_index, inner_class_access_flags] for the InnerClasses 207 // attribute. If the EnclosingMethod attribute exists, it occupies the 208 // last two shorts [class_index, method_index] of the array. If only 209 // the InnerClasses attribute exists, the _inner_classes array length is 210 // number_of_inner_classes * 4. If the class has both InnerClasses 211 // and EnclosingMethod attributes the _inner_classes array length is 212 // number_of_inner_classes * 4 + enclosing_method_attribute_size. 213 Array<jushort>* _inner_classes; 214 215 // The NestMembers attribute. An array of shorts, where each is a 216 // class info index for the class that is a nest member. This data 217 // has not been validated. 218 Array<jushort>* _nest_members; 219 220 // The NestHost attribute. The class info index for the class 221 // that is the nest-host of this class. This data has not been validated. 222 jushort _nest_host_index; 223 224 // Resolved nest-host klass: either true nest-host or self if we are not nested. 225 // By always being set it makes nest-member access checks simpler. 226 InstanceKlass* _nest_host; 227 228 Array<ValueTypes>* _value_types; 229 230 // The contents of the Record attribute. 231 Array<RecordComponent*>* _record_components; 232 233 // the source debug extension for this klass, NULL if not specified. 234 // Specified as UTF-8 string without terminating zero byte in the classfile, 235 // it is stored in the instanceklass as a NULL-terminated UTF-8 string 236 const char* _source_debug_extension; 237 // Array name derived from this class which needs unreferencing 238 // if this class is unloaded. 239 Symbol* _array_name; 240 241 // Number of heapOopSize words used by non-static fields in this klass 242 // (including inherited fields but after header_size()). 243 int _nonstatic_field_size; 244 int _static_field_size; // number words used by static fields (oop and non-oop) in this klass 245 // Constant pool index to the utf8 entry of the Generic signature, 246 // or 0 if none. 247 u2 _generic_signature_index; 248 // Constant pool index to the utf8 entry for the name of source file 249 // containing this klass, 0 if not specified. 250 u2 _source_file_name_index; 251 u2 _static_oop_field_count;// number of static oop fields in this klass 252 u2 _java_fields_count; // The number of declared Java fields 253 int _nonstatic_oop_map_size;// size in words of nonstatic oop map blocks 254 255 int _itable_len; // length of Java itable (in words) 256 // _is_marked_dependent can be set concurrently, thus cannot be part of the 257 // _misc_flags. 258 bool _is_marked_dependent; // used for marking during flushing and deoptimization 259 260 // The low three bits of _misc_flags contains the kind field. 261 // This can be used to quickly discriminate among the five kinds of 262 // InstanceKlass. 263 264 static const unsigned _misc_kind_field_size = 3; 265 static const unsigned _misc_kind_field_pos = 0; 266 static const unsigned _misc_kind_field_mask = (1u << _misc_kind_field_size) - 1u; 267 268 static const unsigned _misc_kind_other = 0; // concrete InstanceKlass 269 static const unsigned _misc_kind_reference = 1; // InstanceRefKlass 270 static const unsigned _misc_kind_class_loader = 2; // InstanceClassLoaderKlass 271 static const unsigned _misc_kind_mirror = 3; // InstanceMirrorKlass 272 static const unsigned _misc_kind_value_type = 4; // ValueKlass 273 274 // Start after _misc_kind field. 275 enum { 276 _misc_rewritten = 1 << 3, // methods rewritten. 277 _misc_has_nonstatic_fields = 1 << 4, // for sizing with UseCompressedOops 278 _misc_should_verify_class = 1 << 5, // allow caching of preverification 279 _misc_is_unsafe_anonymous = 1 << 6, // has embedded _unsafe_anonymous_host field 280 _misc_is_contended = 1 << 7, // marked with contended annotation 281 _misc_has_nonstatic_concrete_methods = 1 << 8, // class/superclass/implemented interfaces has non-static, concrete methods 282 _misc_declares_nonstatic_concrete_methods = 1 << 9, // directly declares non-static, concrete methods 283 _misc_has_been_redefined = 1 << 10, // class has been redefined 284 _misc_has_passed_fingerprint_check = 1 << 11, // when this class was loaded, the fingerprint computed from its 285 // code source was found to be matching the value recorded by AOT. 286 _misc_is_scratch_class = 1 << 12, // class is the redefined scratch class 287 _misc_is_shared_boot_class = 1 << 13, // defining class loader is boot class loader 288 _misc_is_shared_platform_class = 1 << 14, // defining class loader is platform class loader 289 _misc_is_shared_app_class = 1 << 15, // defining class loader is app class loader 290 _misc_has_resolved_methods = 1 << 16, // resolved methods table entries added for this class 291 _misc_is_being_redefined = 1 << 17, // used for locking redefinition 292 _misc_has_contended_annotations = 1 << 18, // has @Contended annotation 293 _misc_has_value_fields = 1 << 19, // has value fields and related embedded section is not empty 294 _misc_is_empty_value = 1 << 20, // empty value type 295 _misc_is_naturally_atomic = 1 << 21, // loaded/stored in one instruction 296 _misc_is_declared_atomic = 1 << 22 // implements jl.NonTearable 297 }; 298 u2 loader_type_bits() { 299 return _misc_is_shared_boot_class|_misc_is_shared_platform_class|_misc_is_shared_app_class; 300 } 301 u4 _misc_flags; 302 u2 _minor_version; // minor version number of class file 303 u2 _major_version; // major version number of class file 304 Thread* _init_thread; // Pointer to current thread doing initialization (to handle recursive initialization) 305 OopMapCache* volatile _oop_map_cache; // OopMapCache for all methods in the klass (allocated lazily) 306 JNIid* _jni_ids; // First JNI identifier for static fields in this class 307 jmethodID* volatile _methods_jmethod_ids; // jmethodIDs corresponding to method_idnum, or NULL if none 308 nmethodBucket* volatile _dep_context; // packed DependencyContext structure 309 uint64_t volatile _dep_context_last_cleaned; 310 nmethod* _osr_nmethods_head; // Head of list of on-stack replacement nmethods for this class 311 #if INCLUDE_JVMTI 312 BreakpointInfo* _breakpoints; // bpt lists, managed by Method* 313 // Linked instanceKlasses of previous versions 314 InstanceKlass* _previous_versions; 315 // JVMTI fields can be moved to their own structure - see 6315920 316 // JVMTI: cached class file, before retransformable agent modified it in CFLH 317 JvmtiCachedClassFileData* _cached_class_file; 318 #endif 319 320 volatile u2 _idnum_allocated_count; // JNI/JVMTI: increments with the addition of methods, old ids don't change 321 322 // Class states are defined as ClassState (see above). 323 // Place the _init_state here to utilize the unused 2-byte after 324 // _idnum_allocated_count. 325 u1 _init_state; // state of class 326 u1 _reference_type; // reference type 327 328 u2 _this_class_index; // constant pool entry 329 #if INCLUDE_JVMTI 330 JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map; // JVMTI: used during heap iteration 331 #endif 332 333 NOT_PRODUCT(int _verify_count;) // to avoid redundant verifies 334 335 // Method array. 336 Array<Method*>* _methods; 337 // Default Method Array, concrete methods inherited from interfaces 338 Array<Method*>* _default_methods; 339 // Interfaces (InstanceKlass*s) this class declares locally to implement. 340 Array<InstanceKlass*>* _local_interfaces; 341 // Interfaces (InstanceKlass*s) this class implements transitively. 342 Array<InstanceKlass*>* _transitive_interfaces; 343 // Int array containing the original order of method in the class file (for JVMTI). 344 Array<int>* _method_ordering; 345 // Int array containing the vtable_indices for default_methods 346 // offset matches _default_methods offset 347 Array<int>* _default_vtable_indices; 348 349 // Instance and static variable information, starts with 6-tuples of shorts 350 // [access, name index, sig index, initval index, low_offset, high_offset] 351 // for all fields, followed by the generic signature data at the end of 352 // the array. Only fields with generic signature attributes have the generic 353 // signature data set in the array. The fields array looks like following: 354 // 355 // f1: [access, name index, sig index, initial value index, low_offset, high_offset] 356 // f2: [access, name index, sig index, initial value index, low_offset, high_offset] 357 // ... 358 // fn: [access, name index, sig index, initial value index, low_offset, high_offset] 359 // [generic signature index] 360 // [generic signature index] 361 // ... 362 Array<u2>* _fields; 363 const Klass** _value_field_klasses; // For "inline class" fields, NULL if none present 364 365 const ValueKlassFixedBlock* _adr_valueklass_fixed_block; 366 367 // embedded Java vtable follows here 368 // embedded Java itables follows here 369 // embedded static fields follows here 370 // embedded nonstatic oop-map blocks follows here 371 // embedded implementor of this interface follows here 372 // The embedded implementor only exists if the current klass is an 373 // iterface. The possible values of the implementor fall into following 374 // three cases: 375 // NULL: no implementor. 376 // A Klass* that's not itself: one implementor. 377 // Itself: more than one implementors. 378 // embedded unsafe_anonymous_host klass follows here 379 // The embedded host klass only exists in an unsafe anonymous class for 380 // dynamic language support (JSR 292 enabled). The host class grants 381 // its access privileges to this class also. The host class is either 382 // named, or a previously loaded unsafe anonymous class. A non-anonymous class 383 // or an anonymous class loaded through normal classloading does not 384 // have this embedded field. 385 // 386 387 friend class SystemDictionary; 388 389 static bool _disable_method_binary_search; 390 391 public: 392 u2 loader_type() { 393 return _misc_flags & loader_type_bits(); 394 } 395 396 bool is_shared_boot_class() const { 397 return (_misc_flags & _misc_is_shared_boot_class) != 0; 398 } 399 bool is_shared_platform_class() const { 400 return (_misc_flags & _misc_is_shared_platform_class) != 0; 401 } 402 bool is_shared_app_class() const { 403 return (_misc_flags & _misc_is_shared_app_class) != 0; 404 } 405 406 void clear_class_loader_type() { 407 _misc_flags &= ~loader_type_bits(); 408 } 409 410 void set_class_loader_type(s2 loader_type); 411 412 bool has_nonstatic_fields() const { 413 return (_misc_flags & _misc_has_nonstatic_fields) != 0; 414 } 415 void set_has_nonstatic_fields(bool b) { 416 if (b) { 417 _misc_flags |= _misc_has_nonstatic_fields; 418 } else { 419 _misc_flags &= ~_misc_has_nonstatic_fields; 420 } 421 } 422 423 bool has_value_fields() const { 424 return (_misc_flags & _misc_has_value_fields) != 0; 425 } 426 void set_has_value_fields() { 427 _misc_flags |= _misc_has_value_fields; 428 } 429 430 bool is_empty_value() const { 431 return (_misc_flags & _misc_is_empty_value) != 0; 432 } 433 void set_is_empty_value() { 434 _misc_flags |= _misc_is_empty_value; 435 } 436 437 // Note: The naturally_atomic property only applies to 438 // inline classes; it is never true on identity classes. 439 // The bit is placed on instanceKlass for convenience. 440 441 // Query if h/w provides atomic load/store for instances. 442 bool is_naturally_atomic() const { 443 return (_misc_flags & _misc_is_naturally_atomic) != 0; 444 } 445 // Initialized in the class file parser, not changed later. 446 void set_is_naturally_atomic() { 447 _misc_flags |= _misc_is_naturally_atomic; 448 } 449 450 // Query if this class implements jl.NonTearable or was 451 // mentioned in the JVM option AlwaysAtomicValueTypes. 452 // This bit can occur anywhere, but is only significant 453 // for inline classes *and* their super types. 454 // It inherits from supers along with NonTearable. 455 bool is_declared_atomic() const { 456 return (_misc_flags & _misc_is_declared_atomic) != 0; 457 } 458 // Initialized in the class file parser, not changed later. 459 void set_is_declared_atomic() { 460 _misc_flags |= _misc_is_declared_atomic; 461 } 462 463 // field sizes 464 int nonstatic_field_size() const { return _nonstatic_field_size; } 465 void set_nonstatic_field_size(int size) { _nonstatic_field_size = size; } 466 467 int static_field_size() const { return _static_field_size; } 468 void set_static_field_size(int size) { _static_field_size = size; } 469 470 int static_oop_field_count() const { return (int)_static_oop_field_count; } 471 void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; } 472 473 // Java itable 474 int itable_length() const { return _itable_len; } 475 void set_itable_length(int len) { _itable_len = len; } 476 477 // array klasses 478 Klass* array_klasses() const { return _array_klasses; } 479 inline Klass* array_klasses_acquire() const; // load with acquire semantics 480 void set_array_klasses(Klass* k) { _array_klasses = k; } 481 inline void release_set_array_klasses(Klass* k); // store with release semantics 482 483 // methods 484 Array<Method*>* methods() const { return _methods; } 485 void set_methods(Array<Method*>* a) { _methods = a; } 486 Method* method_with_idnum(int idnum); 487 Method* method_with_orig_idnum(int idnum); 488 Method* method_with_orig_idnum(int idnum, int version); 489 490 // method ordering 491 Array<int>* method_ordering() const { return _method_ordering; } 492 void set_method_ordering(Array<int>* m) { _method_ordering = m; } 493 void copy_method_ordering(const intArray* m, TRAPS); 494 495 // default_methods 496 Array<Method*>* default_methods() const { return _default_methods; } 497 void set_default_methods(Array<Method*>* a) { _default_methods = a; } 498 499 // default method vtable_indices 500 Array<int>* default_vtable_indices() const { return _default_vtable_indices; } 501 void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; } 502 Array<int>* create_new_default_vtable_indices(int len, TRAPS); 503 504 // interfaces 505 Array<InstanceKlass*>* local_interfaces() const { return _local_interfaces; } 506 void set_local_interfaces(Array<InstanceKlass*>* a) { 507 guarantee(_local_interfaces == NULL || a == NULL, "Just checking"); 508 _local_interfaces = a; } 509 510 Array<InstanceKlass*>* transitive_interfaces() const { return _transitive_interfaces; } 511 void set_transitive_interfaces(Array<InstanceKlass*>* a) { 512 guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking"); 513 _transitive_interfaces = a; 514 } 515 516 private: 517 friend class fieldDescriptor; 518 FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); } 519 520 public: 521 int field_offset (int index) const { return field(index)->offset(); } 522 int field_access_flags(int index) const { return field(index)->access_flags(); } 523 Symbol* field_name (int index) const { return field(index)->name(constants()); } 524 Symbol* field_signature (int index) const { return field(index)->signature(constants()); } 525 bool field_is_flattened(int index) const { return field(index)->is_flattened(); } 526 bool field_is_flattenable(int index) const { return field(index)->is_flattenable(); } 527 528 // Number of Java declared fields 529 int java_fields_count() const { return (int)_java_fields_count; } 530 531 Array<u2>* fields() const { return _fields; } 532 void set_fields(Array<u2>* f, u2 java_fields_count) { 533 guarantee(_fields == NULL || f == NULL, "Just checking"); 534 _fields = f; 535 _java_fields_count = java_fields_count; 536 } 537 538 // inner classes 539 Array<u2>* inner_classes() const { return _inner_classes; } 540 void set_inner_classes(Array<u2>* f) { _inner_classes = f; } 541 542 // nest members 543 Array<u2>* nest_members() const { return _nest_members; } 544 void set_nest_members(Array<u2>* m) { _nest_members = m; } 545 546 // nest-host index 547 jushort nest_host_index() const { return _nest_host_index; } 548 void set_nest_host_index(u2 i) { _nest_host_index = i; } 549 550 // record components 551 Array<RecordComponent*>* record_components() const { return _record_components; } 552 void set_record_components(Array<RecordComponent*>* record_components) { 553 _record_components = record_components; 554 } 555 bool is_record() const { return _record_components != NULL; } 556 557 private: 558 // Called to verify that k is a member of this nest - does not look at k's nest-host 559 bool has_nest_member(InstanceKlass* k, TRAPS) const; 560 561 public: 562 // Returns nest-host class, resolving and validating it if needed 563 // Returns NULL if an exception occurs during loading, or validation fails 564 InstanceKlass* nest_host(Symbol* validationException, TRAPS); 565 // Check if this klass is a nestmate of k - resolves this nest-host and k's 566 bool has_nestmate_access_to(InstanceKlass* k, TRAPS); 567 568 enum InnerClassAttributeOffset { 569 // From http://mirror.eng/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc10.html#18814 570 inner_class_inner_class_info_offset = 0, 571 inner_class_outer_class_info_offset = 1, 572 inner_class_inner_name_offset = 2, 573 inner_class_access_flags_offset = 3, 574 inner_class_next_offset = 4 575 }; 576 577 enum EnclosingMethodAttributeOffset { 578 enclosing_method_class_index_offset = 0, 579 enclosing_method_method_index_offset = 1, 580 enclosing_method_attribute_size = 2 581 }; 582 583 // method override check 584 bool is_override(const methodHandle& super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS); 585 586 // package 587 PackageEntry* package() const { return _package_entry; } 588 ModuleEntry* module() const; 589 bool in_unnamed_package() const { return (_package_entry == NULL); } 590 void set_package(PackageEntry* p) { _package_entry = p; } 591 void set_package(ClassLoaderData* loader_data, TRAPS); 592 bool is_same_class_package(const Klass* class2) const; 593 bool is_same_class_package(oop other_class_loader, const Symbol* other_class_name) const; 594 595 // find an enclosing class 596 InstanceKlass* compute_enclosing_class(bool* inner_is_member, TRAPS) const; 597 598 // Find InnerClasses attribute and return outer_class_info_index & inner_name_index. 599 bool find_inner_classes_attr(int* ooff, int* noff, TRAPS) const; 600 601 private: 602 // Check prohibited package ("java/" only loadable by boot or platform loaders) 603 static void check_prohibited_package(Symbol* class_name, 604 ClassLoaderData* loader_data, 605 TRAPS); 606 public: 607 // initialization state 608 bool is_loaded() const { return _init_state >= loaded; } 609 bool is_linked() const { return _init_state >= linked; } 610 bool is_initialized() const { return _init_state == fully_initialized; } 611 bool is_not_initialized() const { return _init_state < being_initialized; } 612 bool is_being_initialized() const { return _init_state == being_initialized; } 613 bool is_in_error_state() const { return _init_state == initialization_error; } 614 bool is_reentrant_initialization(Thread *thread) { return thread == _init_thread; } 615 ClassState init_state() { return (ClassState)_init_state; } 616 bool is_rewritten() const { return (_misc_flags & _misc_rewritten) != 0; } 617 618 // defineClass specified verification 619 bool should_verify_class() const { 620 return (_misc_flags & _misc_should_verify_class) != 0; 621 } 622 void set_should_verify_class(bool value) { 623 if (value) { 624 _misc_flags |= _misc_should_verify_class; 625 } else { 626 _misc_flags &= ~_misc_should_verify_class; 627 } 628 } 629 630 // marking 631 bool is_marked_dependent() const { return _is_marked_dependent; } 632 void set_is_marked_dependent(bool value) { _is_marked_dependent = value; } 633 634 static ByteSize misc_flags_offset() { return in_ByteSize(offset_of(InstanceKlass, _misc_flags)); } 635 static u4 misc_flags_is_empty_value() { return _misc_is_empty_value; } 636 637 // initialization (virtuals from Klass) 638 bool should_be_initialized() const; // means that initialize should be called 639 void initialize(TRAPS); 640 void link_class(TRAPS); 641 bool link_class_or_fail(TRAPS); // returns false on failure 642 void rewrite_class(TRAPS); 643 void link_methods(TRAPS); 644 Method* class_initializer() const; 645 646 // set the class to initialized if no static initializer is present 647 void eager_initialize(Thread *thread); 648 649 // reference type 650 ReferenceType reference_type() const { return (ReferenceType)_reference_type; } 651 void set_reference_type(ReferenceType t) { 652 assert(t == (u1)t, "overflow"); 653 _reference_type = (u1)t; 654 } 655 656 // this class cp index 657 u2 this_class_index() const { return _this_class_index; } 658 void set_this_class_index(u2 index) { _this_class_index = index; } 659 660 static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); } 661 662 // find local field, returns true if found 663 bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const; 664 // find field in direct superinterfaces, returns the interface in which the field is defined 665 Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const; 666 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined 667 Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const; 668 // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined 669 Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const; 670 671 // find a non-static or static field given its offset within the class. 672 bool contains_field_offset(int offset); 673 674 bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const; 675 bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const; 676 677 private: 678 inline static int quick_search(const Array<Method*>* methods, const Symbol* name); 679 680 public: 681 static void disable_method_binary_search() { 682 _disable_method_binary_search = true; 683 } 684 685 // find a local method (returns NULL if not found) 686 Method* find_method(const Symbol* name, const Symbol* signature) const; 687 static Method* find_method(const Array<Method*>* methods, 688 const Symbol* name, 689 const Symbol* signature); 690 691 // find a local method, but skip static methods 692 Method* find_instance_method(const Symbol* name, const Symbol* signature, 693 PrivateLookupMode private_mode = find_private) const; 694 static Method* find_instance_method(const Array<Method*>* methods, 695 const Symbol* name, 696 const Symbol* signature, 697 PrivateLookupMode private_mode = find_private); 698 699 // find a local method (returns NULL if not found) 700 Method* find_local_method(const Symbol* name, 701 const Symbol* signature, 702 OverpassLookupMode overpass_mode, 703 StaticLookupMode static_mode, 704 PrivateLookupMode private_mode) const; 705 706 // find a local method from given methods array (returns NULL if not found) 707 static Method* find_local_method(const Array<Method*>* methods, 708 const Symbol* name, 709 const Symbol* signature, 710 OverpassLookupMode overpass_mode, 711 StaticLookupMode static_mode, 712 PrivateLookupMode private_mode); 713 714 // find a local method index in methods or default_methods (returns -1 if not found) 715 static int find_method_index(const Array<Method*>* methods, 716 const Symbol* name, 717 const Symbol* signature, 718 OverpassLookupMode overpass_mode, 719 StaticLookupMode static_mode, 720 PrivateLookupMode private_mode); 721 722 // lookup operation (returns NULL if not found) 723 Method* uncached_lookup_method(const Symbol* name, 724 const Symbol* signature, 725 OverpassLookupMode overpass_mode, 726 PrivateLookupMode private_mode = find_private) const; 727 728 // lookup a method in all the interfaces that this class implements 729 // (returns NULL if not found) 730 Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature, DefaultsLookupMode defaults_mode) const; 731 732 // lookup a method in local defaults then in all interfaces 733 // (returns NULL if not found) 734 Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const; 735 736 // Find method indices by name. If a method with the specified name is 737 // found the index to the first method is returned, and 'end' is filled in 738 // with the index of first non-name-matching method. If no method is found 739 // -1 is returned. 740 int find_method_by_name(const Symbol* name, int* end) const; 741 static int find_method_by_name(const Array<Method*>* methods, 742 const Symbol* name, int* end); 743 744 // constant pool 745 ConstantPool* constants() const { return _constants; } 746 void set_constants(ConstantPool* c) { _constants = c; } 747 748 // protection domain 749 oop protection_domain() const; 750 751 // signers 752 objArrayOop signers() const; 753 754 // host class 755 InstanceKlass* unsafe_anonymous_host() const { 756 InstanceKlass** hk = adr_unsafe_anonymous_host(); 757 if (hk == NULL) { 758 assert(!is_unsafe_anonymous(), "Unsafe anonymous classes have host klasses"); 759 return NULL; 760 } else { 761 assert(*hk != NULL, "host klass should always be set if the address is not null"); 762 assert(is_unsafe_anonymous(), "Only unsafe anonymous classes have host klasses"); 763 return *hk; 764 } 765 } 766 void set_unsafe_anonymous_host(const InstanceKlass* host) { 767 assert(is_unsafe_anonymous(), "not unsafe anonymous"); 768 const InstanceKlass** addr = (const InstanceKlass **)adr_unsafe_anonymous_host(); 769 assert(addr != NULL, "no reversed space"); 770 if (addr != NULL) { 771 *addr = host; 772 } 773 } 774 bool is_unsafe_anonymous() const { 775 return (_misc_flags & _misc_is_unsafe_anonymous) != 0; 776 } 777 void set_is_unsafe_anonymous(bool value) { 778 if (value) { 779 _misc_flags |= _misc_is_unsafe_anonymous; 780 } else { 781 _misc_flags &= ~_misc_is_unsafe_anonymous; 782 } 783 } 784 785 bool is_contended() const { 786 return (_misc_flags & _misc_is_contended) != 0; 787 } 788 void set_is_contended(bool value) { 789 if (value) { 790 _misc_flags |= _misc_is_contended; 791 } else { 792 _misc_flags &= ~_misc_is_contended; 793 } 794 } 795 796 // source file name 797 Symbol* source_file_name() const { 798 return (_source_file_name_index == 0) ? 799 (Symbol*)NULL : _constants->symbol_at(_source_file_name_index); 800 } 801 u2 source_file_name_index() const { 802 return _source_file_name_index; 803 } 804 void set_source_file_name_index(u2 sourcefile_index) { 805 _source_file_name_index = sourcefile_index; 806 } 807 808 // minor and major version numbers of class file 809 u2 minor_version() const { return _minor_version; } 810 void set_minor_version(u2 minor_version) { _minor_version = minor_version; } 811 u2 major_version() const { return _major_version; } 812 void set_major_version(u2 major_version) { _major_version = major_version; } 813 814 // source debug extension 815 const char* source_debug_extension() const { return _source_debug_extension; } 816 void set_source_debug_extension(const char* array, int length); 817 818 // symbol unloading support (refcount already added) 819 Symbol* array_name() { return _array_name; } 820 void set_array_name(Symbol* name) { assert(_array_name == NULL || name == NULL, "name already created"); _array_name = name; } 821 822 // nonstatic oop-map blocks 823 static int nonstatic_oop_map_size(unsigned int oop_map_count) { 824 return oop_map_count * OopMapBlock::size_in_words(); 825 } 826 unsigned int nonstatic_oop_map_count() const { 827 return _nonstatic_oop_map_size / OopMapBlock::size_in_words(); 828 } 829 int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; } 830 void set_nonstatic_oop_map_size(int words) { 831 _nonstatic_oop_map_size = words; 832 } 833 834 bool has_contended_annotations() const { 835 return ((_misc_flags & _misc_has_contended_annotations) != 0); 836 } 837 void set_has_contended_annotations(bool value) { 838 if (value) { 839 _misc_flags |= _misc_has_contended_annotations; 840 } else { 841 _misc_flags &= ~_misc_has_contended_annotations; 842 } 843 } 844 845 #if INCLUDE_JVMTI 846 // Redefinition locking. Class can only be redefined by one thread at a time. 847 848 bool is_being_redefined() const { 849 return (_misc_flags & _misc_is_being_redefined); 850 } 851 void set_is_being_redefined(bool value) { 852 if (value) { 853 _misc_flags |= _misc_is_being_redefined; 854 } else { 855 _misc_flags &= ~_misc_is_being_redefined; 856 } 857 } 858 859 // RedefineClasses() support for previous versions: 860 void add_previous_version(InstanceKlass* ik, int emcp_method_count); 861 void purge_previous_version_list(); 862 863 InstanceKlass* previous_versions() const { return _previous_versions; } 864 #else 865 InstanceKlass* previous_versions() const { return NULL; } 866 #endif 867 868 InstanceKlass* get_klass_version(int version) { 869 for (InstanceKlass* ik = this; ik != NULL; ik = ik->previous_versions()) { 870 if (ik->constants()->version() == version) { 871 return ik; 872 } 873 } 874 return NULL; 875 } 876 877 bool has_been_redefined() const { 878 return (_misc_flags & _misc_has_been_redefined) != 0; 879 } 880 void set_has_been_redefined() { 881 _misc_flags |= _misc_has_been_redefined; 882 } 883 884 bool has_passed_fingerprint_check() const { 885 return (_misc_flags & _misc_has_passed_fingerprint_check) != 0; 886 } 887 void set_has_passed_fingerprint_check(bool b) { 888 if (b) { 889 _misc_flags |= _misc_has_passed_fingerprint_check; 890 } else { 891 _misc_flags &= ~_misc_has_passed_fingerprint_check; 892 } 893 } 894 bool supers_have_passed_fingerprint_checks(); 895 896 static bool should_store_fingerprint(bool is_unsafe_anonymous); 897 bool should_store_fingerprint() const { return should_store_fingerprint(is_unsafe_anonymous()); } 898 bool has_stored_fingerprint() const; 899 uint64_t get_stored_fingerprint() const; 900 void store_fingerprint(uint64_t fingerprint); 901 902 bool is_scratch_class() const { 903 return (_misc_flags & _misc_is_scratch_class) != 0; 904 } 905 906 void set_is_scratch_class() { 907 _misc_flags |= _misc_is_scratch_class; 908 } 909 910 bool has_resolved_methods() const { 911 return (_misc_flags & _misc_has_resolved_methods) != 0; 912 } 913 914 void set_has_resolved_methods() { 915 _misc_flags |= _misc_has_resolved_methods; 916 } 917 private: 918 919 void set_kind(unsigned kind) { 920 assert(kind <= _misc_kind_field_mask, "Invalid InstanceKlass kind"); 921 unsigned fmask = _misc_kind_field_mask << _misc_kind_field_pos; 922 unsigned flags = _misc_flags & ~fmask; 923 _misc_flags = (flags | (kind << _misc_kind_field_pos)); 924 } 925 926 bool is_kind(unsigned desired) const { 927 unsigned kind = (_misc_flags >> _misc_kind_field_pos) & _misc_kind_field_mask; 928 return kind == desired; 929 } 930 931 public: 932 933 // Other is anything that is not one of the more specialized kinds of InstanceKlass. 934 bool is_other_instance_klass() const { return is_kind(_misc_kind_other); } 935 bool is_reference_instance_klass() const { return is_kind(_misc_kind_reference); } 936 bool is_mirror_instance_klass() const { return is_kind(_misc_kind_mirror); } 937 bool is_class_loader_instance_klass() const { return is_kind(_misc_kind_class_loader); } 938 bool is_value_type_klass() const { return is_kind(_misc_kind_value_type); } 939 940 #if INCLUDE_JVMTI 941 942 void init_previous_versions() { 943 _previous_versions = NULL; 944 } 945 946 private: 947 static bool _has_previous_versions; 948 public: 949 static void purge_previous_versions(InstanceKlass* ik) { 950 if (ik->has_been_redefined()) { 951 ik->purge_previous_version_list(); 952 } 953 } 954 955 static bool has_previous_versions_and_reset(); 956 static bool has_previous_versions() { return _has_previous_versions; } 957 958 // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation 959 void set_cached_class_file(JvmtiCachedClassFileData *data) { 960 _cached_class_file = data; 961 } 962 JvmtiCachedClassFileData * get_cached_class_file(); 963 jint get_cached_class_file_len(); 964 unsigned char * get_cached_class_file_bytes(); 965 966 // JVMTI: Support for caching of field indices, types, and offsets 967 void set_jvmti_cached_class_field_map(JvmtiCachedClassFieldMap* descriptor) { 968 _jvmti_cached_class_field_map = descriptor; 969 } 970 JvmtiCachedClassFieldMap* jvmti_cached_class_field_map() const { 971 return _jvmti_cached_class_field_map; 972 } 973 #else // INCLUDE_JVMTI 974 975 static void purge_previous_versions(InstanceKlass* ik) { return; }; 976 static bool has_previous_versions_and_reset() { return false; } 977 978 void set_cached_class_file(JvmtiCachedClassFileData *data) { 979 assert(data == NULL, "unexpected call with JVMTI disabled"); 980 } 981 JvmtiCachedClassFileData * get_cached_class_file() { return (JvmtiCachedClassFileData *)NULL; } 982 983 #endif // INCLUDE_JVMTI 984 985 bool has_nonstatic_concrete_methods() const { 986 return (_misc_flags & _misc_has_nonstatic_concrete_methods) != 0; 987 } 988 void set_has_nonstatic_concrete_methods(bool b) { 989 if (b) { 990 _misc_flags |= _misc_has_nonstatic_concrete_methods; 991 } else { 992 _misc_flags &= ~_misc_has_nonstatic_concrete_methods; 993 } 994 } 995 996 bool declares_nonstatic_concrete_methods() const { 997 return (_misc_flags & _misc_declares_nonstatic_concrete_methods) != 0; 998 } 999 void set_declares_nonstatic_concrete_methods(bool b) { 1000 if (b) { 1001 _misc_flags |= _misc_declares_nonstatic_concrete_methods; 1002 } else { 1003 _misc_flags &= ~_misc_declares_nonstatic_concrete_methods; 1004 } 1005 } 1006 1007 // for adding methods, ConstMethod::UNSET_IDNUM means no more ids available 1008 inline u2 next_method_idnum(); 1009 void set_initial_method_idnum(u2 value) { _idnum_allocated_count = value; } 1010 1011 // generics support 1012 Symbol* generic_signature() const { 1013 return (_generic_signature_index == 0) ? 1014 (Symbol*)NULL : _constants->symbol_at(_generic_signature_index); 1015 } 1016 u2 generic_signature_index() const { 1017 return _generic_signature_index; 1018 } 1019 void set_generic_signature_index(u2 sig_index) { 1020 _generic_signature_index = sig_index; 1021 } 1022 1023 u2 enclosing_method_data(int offset) const; 1024 u2 enclosing_method_class_index() const { 1025 return enclosing_method_data(enclosing_method_class_index_offset); 1026 } 1027 u2 enclosing_method_method_index() { 1028 return enclosing_method_data(enclosing_method_method_index_offset); 1029 } 1030 void set_enclosing_method_indices(u2 class_index, 1031 u2 method_index); 1032 1033 // jmethodID support 1034 jmethodID get_jmethod_id(const methodHandle& method_h); 1035 jmethodID get_jmethod_id_fetch_or_update(size_t idnum, 1036 jmethodID new_id, jmethodID* new_jmeths, 1037 jmethodID* to_dealloc_id_p, 1038 jmethodID** to_dealloc_jmeths_p); 1039 static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum, 1040 size_t *length_p, jmethodID* id_p); 1041 void ensure_space_for_methodids(int start_offset = 0); 1042 jmethodID jmethod_id_or_null(Method* method); 1043 1044 // annotations support 1045 Annotations* annotations() const { return _annotations; } 1046 void set_annotations(Annotations* anno) { _annotations = anno; } 1047 1048 AnnotationArray* class_annotations() const { 1049 return (_annotations != NULL) ? _annotations->class_annotations() : NULL; 1050 } 1051 Array<AnnotationArray*>* fields_annotations() const { 1052 return (_annotations != NULL) ? _annotations->fields_annotations() : NULL; 1053 } 1054 AnnotationArray* class_type_annotations() const { 1055 return (_annotations != NULL) ? _annotations->class_type_annotations() : NULL; 1056 } 1057 Array<AnnotationArray*>* fields_type_annotations() const { 1058 return (_annotations != NULL) ? _annotations->fields_type_annotations() : NULL; 1059 } 1060 // allocation 1061 instanceOop allocate_instance(TRAPS); 1062 1063 // additional member function to return a handle 1064 instanceHandle allocate_instance_handle(TRAPS); 1065 1066 objArrayOop allocate_objArray(int n, int length, TRAPS); 1067 // Helper function 1068 static instanceOop register_finalizer(instanceOop i, TRAPS); 1069 1070 // Check whether reflection/jni/jvm code is allowed to instantiate this class; 1071 // if not, throw either an Error or an Exception. 1072 virtual void check_valid_for_instantiation(bool throwError, TRAPS); 1073 1074 // initialization 1075 void call_class_initializer(TRAPS); 1076 void set_initialization_state_and_notify(ClassState state, TRAPS); 1077 1078 // OopMapCache support 1079 OopMapCache* oop_map_cache() { return _oop_map_cache; } 1080 void set_oop_map_cache(OopMapCache *cache) { _oop_map_cache = cache; } 1081 void mask_for(const methodHandle& method, int bci, InterpreterOopMap* entry); 1082 1083 // JNI identifier support (for static fields - for jni performance) 1084 JNIid* jni_ids() { return _jni_ids; } 1085 void set_jni_ids(JNIid* ids) { _jni_ids = ids; } 1086 JNIid* jni_id_for(int offset); 1087 1088 // maintenance of deoptimization dependencies 1089 inline DependencyContext dependencies(); 1090 int mark_dependent_nmethods(KlassDepChange& changes); 1091 void add_dependent_nmethod(nmethod* nm); 1092 void remove_dependent_nmethod(nmethod* nm); 1093 void clean_dependency_context(); 1094 1095 // On-stack replacement support 1096 nmethod* osr_nmethods_head() const { return _osr_nmethods_head; }; 1097 void set_osr_nmethods_head(nmethod* h) { _osr_nmethods_head = h; }; 1098 void add_osr_nmethod(nmethod* n); 1099 bool remove_osr_nmethod(nmethod* n); 1100 int mark_osr_nmethods(const Method* m); 1101 nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const; 1102 1103 #if INCLUDE_JVMTI 1104 // Breakpoint support (see methods on Method* for details) 1105 BreakpointInfo* breakpoints() const { return _breakpoints; }; 1106 void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; }; 1107 #endif 1108 1109 // support for stub routines 1110 static ByteSize init_state_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_state)); } 1111 JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;) 1112 static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); } 1113 1114 static ByteSize value_field_klasses_offset() { return in_ByteSize(offset_of(InstanceKlass, _value_field_klasses)); } 1115 static ByteSize adr_valueklass_fixed_block_offset() { return in_ByteSize(offset_of(InstanceKlass, _adr_valueklass_fixed_block)); } 1116 1117 // subclass/subinterface checks 1118 bool implements_interface(Klass* k) const; 1119 bool is_same_or_direct_interface(Klass* k) const; 1120 1121 #ifdef ASSERT 1122 // check whether this class or one of its superclasses was redefined 1123 bool has_redefined_this_or_super() const; 1124 #endif 1125 1126 // Access to the implementor of an interface. 1127 Klass* implementor() const; 1128 void set_implementor(Klass* k); 1129 int nof_implementors() const; 1130 void add_implementor(Klass* k); // k is a new class that implements this interface 1131 void init_implementor(); // initialize 1132 1133 // link this class into the implementors list of every interface it implements 1134 void process_interfaces(Thread *thread); 1135 1136 // virtual operations from Klass 1137 GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots, 1138 Array<InstanceKlass*>* transitive_interfaces); 1139 bool can_be_primary_super_slow() const; 1140 int oop_size(oop obj) const { return size_helper(); } 1141 // slow because it's a virtual call and used for verifying the layout_helper. 1142 // Using the layout_helper bits, we can call is_instance_klass without a virtual call. 1143 DEBUG_ONLY(bool is_instance_klass_slow() const { return true; }) 1144 1145 // Iterators 1146 void do_local_static_fields(FieldClosure* cl); 1147 void do_nonstatic_fields(FieldClosure* cl); // including inherited fields 1148 void do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle, TRAPS); 1149 1150 void methods_do(void f(Method* method)); 1151 virtual void array_klasses_do(void f(Klass* k)); 1152 1153 static InstanceKlass* cast(Klass* k) { 1154 return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k))); 1155 } 1156 1157 static const InstanceKlass* cast(const Klass* k) { 1158 assert(k != NULL, "k should not be null"); 1159 assert(k->is_instance_klass(), "cast to InstanceKlass"); 1160 return static_cast<const InstanceKlass*>(k); 1161 } 1162 1163 virtual InstanceKlass* java_super() const { 1164 return (super() == NULL) ? NULL : cast(super()); 1165 } 1166 1167 // Sizing (in words) 1168 static int header_size() { return sizeof(InstanceKlass)/wordSize; } 1169 1170 static int size(int vtable_length, int itable_length, 1171 int nonstatic_oop_map_size, 1172 bool is_interface, bool is_unsafe_anonymous, bool has_stored_fingerprint, 1173 int java_fields, bool is_value_type) { 1174 return align_metadata_size(header_size() + 1175 vtable_length + 1176 itable_length + 1177 nonstatic_oop_map_size + 1178 (is_interface ? (int)sizeof(Klass*)/wordSize : 0) + 1179 (is_unsafe_anonymous ? (int)sizeof(Klass*)/wordSize : 0) + 1180 (has_stored_fingerprint ? (int)sizeof(uint64_t*)/wordSize : 0) + 1181 (java_fields * (int)sizeof(Klass*)/wordSize) + 1182 (is_value_type ? (int)sizeof(ValueKlassFixedBlock) : 0)); 1183 } 1184 int size() const { return size(vtable_length(), 1185 itable_length(), 1186 nonstatic_oop_map_size(), 1187 is_interface(), 1188 is_unsafe_anonymous(), 1189 has_stored_fingerprint(), 1190 has_value_fields() ? java_fields_count() : 0, 1191 is_value()); 1192 } 1193 1194 intptr_t* start_of_itable() const { return (intptr_t*)start_of_vtable() + vtable_length(); } 1195 intptr_t* end_of_itable() const { return start_of_itable() + itable_length(); } 1196 1197 int itable_offset_in_words() const { return start_of_itable() - (intptr_t*)this; } 1198 1199 oop static_field_base_raw() { return java_mirror(); } 1200 1201 bool bounds_check(address addr, bool edge_ok = false, intptr_t size_in_bytes = -1) const PRODUCT_RETURN0; 1202 1203 OopMapBlock* start_of_nonstatic_oop_maps() const { 1204 return (OopMapBlock*)(start_of_itable() + itable_length()); 1205 } 1206 1207 Klass** end_of_nonstatic_oop_maps() const { 1208 return (Klass**)(start_of_nonstatic_oop_maps() + 1209 nonstatic_oop_map_count()); 1210 } 1211 1212 Klass* volatile* adr_implementor() const { 1213 if (is_interface()) { 1214 return (Klass* volatile*)end_of_nonstatic_oop_maps(); 1215 } else { 1216 return NULL; 1217 } 1218 }; 1219 1220 InstanceKlass** adr_unsafe_anonymous_host() const { 1221 if (is_unsafe_anonymous()) { 1222 InstanceKlass** adr_impl = (InstanceKlass**)adr_implementor(); 1223 if (adr_impl != NULL) { 1224 return adr_impl + 1; 1225 } else { 1226 return (InstanceKlass **)end_of_nonstatic_oop_maps(); 1227 } 1228 } else { 1229 return NULL; 1230 } 1231 } 1232 1233 address adr_fingerprint() const { 1234 if (has_stored_fingerprint()) { 1235 InstanceKlass** adr_host = adr_unsafe_anonymous_host(); 1236 if (adr_host != NULL) { 1237 return (address)(adr_host + 1); 1238 } 1239 1240 Klass* volatile* adr_impl = adr_implementor(); 1241 if (adr_impl != NULL) { 1242 return (address)(adr_impl + 1); 1243 } 1244 1245 return (address)end_of_nonstatic_oop_maps(); 1246 } else { 1247 return NULL; 1248 } 1249 } 1250 1251 address adr_value_fields_klasses() const { 1252 if (has_value_fields()) { 1253 address adr_fing = adr_fingerprint(); 1254 if (adr_fing != NULL) { 1255 return adr_fingerprint() + sizeof(u8); 1256 } 1257 1258 InstanceKlass** adr_host = adr_unsafe_anonymous_host(); 1259 if (adr_host != NULL) { 1260 return (address)(adr_host + 1); 1261 } 1262 1263 Klass* volatile* adr_impl = adr_implementor(); 1264 if (adr_impl != NULL) { 1265 return (address)(adr_impl + 1); 1266 } 1267 1268 return (address)end_of_nonstatic_oop_maps(); 1269 } else { 1270 return NULL; 1271 } 1272 } 1273 1274 Klass* get_value_field_klass(int idx) const { 1275 assert(has_value_fields(), "Sanity checking"); 1276 Klass* k = ((Klass**)adr_value_fields_klasses())[idx]; 1277 assert(k != NULL, "Should always be set before being read"); 1278 assert(k->is_value(), "Must be a value type"); 1279 return k; 1280 } 1281 1282 Klass* get_value_field_klass_or_null(int idx) const { 1283 assert(has_value_fields(), "Sanity checking"); 1284 Klass* k = ((Klass**)adr_value_fields_klasses())[idx]; 1285 assert(k == NULL || k->is_value(), "Must be a value type"); 1286 return k; 1287 } 1288 1289 void set_value_field_klass(int idx, Klass* k) { 1290 assert(has_value_fields(), "Sanity checking"); 1291 assert(k != NULL, "Should not be set to NULL"); 1292 assert(((Klass**)adr_value_fields_klasses())[idx] == NULL, "Should not be set twice"); 1293 ((Klass**)adr_value_fields_klasses())[idx] = k; 1294 } 1295 1296 // Use this to return the size of an instance in heap words: 1297 virtual int size_helper() const { 1298 return layout_helper_to_size_helper(layout_helper()); 1299 } 1300 1301 // This bit is initialized in classFileParser.cpp. 1302 // It is false under any of the following conditions: 1303 // - the class is abstract (including any interface) 1304 // - the class has a finalizer (if !RegisterFinalizersAtInit) 1305 // - the class size is larger than FastAllocateSizeLimit 1306 // - the class is java/lang/Class, which cannot be allocated directly 1307 bool can_be_fastpath_allocated() const { 1308 return !layout_helper_needs_slow_path(layout_helper()); 1309 } 1310 1311 // Java itable 1312 klassItable itable() const; // return klassItable wrapper 1313 Method* method_at_itable(Klass* holder, int index, TRAPS); 1314 1315 #if INCLUDE_JVMTI 1316 void adjust_default_methods(bool* trace_name_printed); 1317 #endif // INCLUDE_JVMTI 1318 1319 void clean_weak_instanceklass_links(); 1320 private: 1321 void clean_implementors_list(); 1322 void clean_method_data(); 1323 1324 public: 1325 // Explicit metaspace deallocation of fields 1326 // For RedefineClasses and class file parsing errors, we need to deallocate 1327 // instanceKlasses and the metadata they point to. 1328 void deallocate_contents(ClassLoaderData* loader_data); 1329 static void deallocate_methods(ClassLoaderData* loader_data, 1330 Array<Method*>* methods); 1331 void static deallocate_interfaces(ClassLoaderData* loader_data, 1332 const Klass* super_klass, 1333 Array<InstanceKlass*>* local_interfaces, 1334 Array<InstanceKlass*>* transitive_interfaces); 1335 void static deallocate_record_components(ClassLoaderData* loader_data, 1336 Array<RecordComponent*>* record_component); 1337 1338 // The constant pool is on stack if any of the methods are executing or 1339 // referenced by handles. 1340 bool on_stack() const { return _constants->on_stack(); } 1341 1342 // callbacks for actions during class unloading 1343 static void unload_class(InstanceKlass* ik); 1344 static void release_C_heap_structures(InstanceKlass* ik); 1345 1346 // Naming 1347 const char* signature_name() const; 1348 const char* signature_name_of(char c) const; 1349 static Symbol* package_from_name(const Symbol* name, TRAPS); 1350 1351 // Oop fields (and metadata) iterators 1352 // 1353 // The InstanceKlass iterators also visits the Object's klass. 1354 1355 // Forward iteration 1356 public: 1357 // Iterate over all oop fields in the oop maps. 1358 template <typename T, class OopClosureType> 1359 inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure); 1360 1361 // Iterate over all oop fields and metadata. 1362 template <typename T, class OopClosureType> 1363 inline void oop_oop_iterate(oop obj, OopClosureType* closure); 1364 1365 // Iterate over all oop fields in one oop map. 1366 template <typename T, class OopClosureType> 1367 inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure); 1368 1369 1370 // Reverse iteration 1371 // Iterate over all oop fields and metadata. 1372 template <typename T, class OopClosureType> 1373 inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure); 1374 1375 private: 1376 // Iterate over all oop fields in the oop maps. 1377 template <typename T, class OopClosureType> 1378 inline void oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure); 1379 1380 // Iterate over all oop fields in one oop map. 1381 template <typename T, class OopClosureType> 1382 inline void oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure); 1383 1384 1385 // Bounded range iteration 1386 public: 1387 // Iterate over all oop fields in the oop maps. 1388 template <typename T, class OopClosureType> 1389 inline void oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr); 1390 1391 // Iterate over all oop fields and metadata. 1392 template <typename T, class OopClosureType> 1393 inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr); 1394 1395 private: 1396 // Iterate over all oop fields in one oop map. 1397 template <typename T, class OopClosureType> 1398 inline void oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr); 1399 1400 1401 public: 1402 u2 idnum_allocated_count() const { return _idnum_allocated_count; } 1403 1404 public: 1405 void set_in_error_state() { 1406 assert(DumpSharedSpaces, "only call this when dumping archive"); 1407 _init_state = initialization_error; 1408 } 1409 bool check_sharing_error_state(); 1410 1411 private: 1412 // initialization state 1413 void set_init_state(ClassState state); 1414 void set_rewritten() { _misc_flags |= _misc_rewritten; } 1415 void set_init_thread(Thread *thread) { _init_thread = thread; } 1416 1417 // The RedefineClasses() API can cause new method idnums to be needed 1418 // which will cause the caches to grow. Safety requires different 1419 // cache management logic if the caches can grow instead of just 1420 // going from NULL to non-NULL. 1421 bool idnum_can_increment() const { return has_been_redefined(); } 1422 inline jmethodID* methods_jmethod_ids_acquire() const; 1423 inline void release_set_methods_jmethod_ids(jmethodID* jmeths); 1424 1425 // Lock during initialization 1426 public: 1427 // Lock for (1) initialization; (2) access to the ConstantPool of this class. 1428 // Must be one per class and it has to be a VM internal object so java code 1429 // cannot lock it (like the mirror). 1430 // It has to be an object not a Mutex because it's held through java calls. 1431 oop init_lock() const; 1432 private: 1433 void fence_and_clear_init_lock(); 1434 1435 bool link_class_impl (TRAPS); 1436 bool verify_code (TRAPS); 1437 void initialize_impl (TRAPS); 1438 void initialize_super_interfaces (TRAPS); 1439 void eager_initialize_impl (); 1440 /* jni_id_for_impl for jfieldID only */ 1441 JNIid* jni_id_for_impl (int offset); 1442 protected: 1443 // Returns the array class for the n'th dimension 1444 virtual Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, int n, TRAPS); 1445 1446 // Returns the array class with this class as element type 1447 virtual Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, TRAPS); 1448 1449 private: 1450 1451 // find a local method (returns NULL if not found) 1452 Method* find_method_impl(const Symbol* name, 1453 const Symbol* signature, 1454 OverpassLookupMode overpass_mode, 1455 StaticLookupMode static_mode, 1456 PrivateLookupMode private_mode) const; 1457 1458 static Method* find_method_impl(const Array<Method*>* methods, 1459 const Symbol* name, 1460 const Symbol* signature, 1461 OverpassLookupMode overpass_mode, 1462 StaticLookupMode static_mode, 1463 PrivateLookupMode private_mode); 1464 1465 // Free CHeap allocated fields. 1466 void release_C_heap_structures(); 1467 1468 #if INCLUDE_JVMTI 1469 // RedefineClasses support 1470 void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; } 1471 void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count); 1472 #endif 1473 public: 1474 // CDS support - remove and restore oops from metadata. Oops are not shared. 1475 virtual void remove_unshareable_info(); 1476 virtual void remove_java_mirror(); 1477 virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS); 1478 1479 // jvm support 1480 jint compute_modifier_flags(TRAPS) const; 1481 1482 public: 1483 // JVMTI support 1484 jint jvmti_class_status() const; 1485 1486 virtual void metaspace_pointers_do(MetaspaceClosure* iter); 1487 1488 public: 1489 // Printing 1490 #ifndef PRODUCT 1491 void print_on(outputStream* st) const; 1492 #endif 1493 void print_value_on(outputStream* st) const; 1494 1495 void oop_print_value_on(oop obj, outputStream* st); 1496 1497 #ifndef PRODUCT 1498 void oop_print_on (oop obj, outputStream* st); 1499 1500 void print_dependent_nmethods(bool verbose = false); 1501 bool is_dependent_nmethod(nmethod* nm); 1502 bool verify_itable_index(int index); 1503 #endif 1504 1505 const char* internal_name() const; 1506 1507 // Verification 1508 void verify_on(outputStream* st); 1509 1510 void oop_verify_on(oop obj, outputStream* st); 1511 1512 // Logging 1513 void print_class_load_logging(ClassLoaderData* loader_data, 1514 const char* module_name, 1515 const ClassFileStream* cfs) const; 1516 }; 1517 1518 // for adding methods 1519 // UNSET_IDNUM return means no more ids available 1520 inline u2 InstanceKlass::next_method_idnum() { 1521 if (_idnum_allocated_count == ConstMethod::MAX_IDNUM) { 1522 return ConstMethod::UNSET_IDNUM; // no more ids available 1523 } else { 1524 return _idnum_allocated_count++; 1525 } 1526 } 1527 1528 1529 /* JNIid class for jfieldIDs only */ 1530 class JNIid: public CHeapObj<mtClass> { 1531 friend class VMStructs; 1532 private: 1533 Klass* _holder; 1534 JNIid* _next; 1535 int _offset; 1536 #ifdef ASSERT 1537 bool _is_static_field_id; 1538 #endif 1539 1540 public: 1541 // Accessors 1542 Klass* holder() const { return _holder; } 1543 int offset() const { return _offset; } 1544 JNIid* next() { return _next; } 1545 // Constructor 1546 JNIid(Klass* holder, int offset, JNIid* next); 1547 // Identifier lookup 1548 JNIid* find(int offset); 1549 1550 bool find_local_field(fieldDescriptor* fd) { 1551 return InstanceKlass::cast(holder())->find_local_field_from_offset(offset(), true, fd); 1552 } 1553 1554 static void deallocate(JNIid* id); 1555 // Debugging 1556 #ifdef ASSERT 1557 bool is_static_field_id() const { return _is_static_field_id; } 1558 void set_is_static_field_id() { _is_static_field_id = true; } 1559 #endif 1560 void verify(Klass* holder); 1561 }; 1562 1563 // An iterator that's used to access the inner classes indices in the 1564 // InstanceKlass::_inner_classes array. 1565 class InnerClassesIterator : public StackObj { 1566 private: 1567 Array<jushort>* _inner_classes; 1568 int _length; 1569 int _idx; 1570 public: 1571 1572 InnerClassesIterator(const InstanceKlass* k) { 1573 _inner_classes = k->inner_classes(); 1574 if (k->inner_classes() != NULL) { 1575 _length = _inner_classes->length(); 1576 // The inner class array's length should be the multiple of 1577 // inner_class_next_offset if it only contains the InnerClasses 1578 // attribute data, or it should be 1579 // n*inner_class_next_offset+enclosing_method_attribute_size 1580 // if it also contains the EnclosingMethod data. 1581 assert((_length % InstanceKlass::inner_class_next_offset == 0 || 1582 _length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size), 1583 "just checking"); 1584 // Remove the enclosing_method portion if exists. 1585 if (_length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size) { 1586 _length -= InstanceKlass::enclosing_method_attribute_size; 1587 } 1588 } else { 1589 _length = 0; 1590 } 1591 _idx = 0; 1592 } 1593 1594 int length() const { 1595 return _length; 1596 } 1597 1598 void next() { 1599 _idx += InstanceKlass::inner_class_next_offset; 1600 } 1601 1602 bool done() const { 1603 return (_idx >= _length); 1604 } 1605 1606 u2 inner_class_info_index() const { 1607 return _inner_classes->at( 1608 _idx + InstanceKlass::inner_class_inner_class_info_offset); 1609 } 1610 1611 void set_inner_class_info_index(u2 index) { 1612 _inner_classes->at_put( 1613 _idx + InstanceKlass::inner_class_inner_class_info_offset, index); 1614 } 1615 1616 u2 outer_class_info_index() const { 1617 return _inner_classes->at( 1618 _idx + InstanceKlass::inner_class_outer_class_info_offset); 1619 } 1620 1621 void set_outer_class_info_index(u2 index) { 1622 _inner_classes->at_put( 1623 _idx + InstanceKlass::inner_class_outer_class_info_offset, index); 1624 } 1625 1626 u2 inner_name_index() const { 1627 return _inner_classes->at( 1628 _idx + InstanceKlass::inner_class_inner_name_offset); 1629 } 1630 1631 void set_inner_name_index(u2 index) { 1632 _inner_classes->at_put( 1633 _idx + InstanceKlass::inner_class_inner_name_offset, index); 1634 } 1635 1636 u2 inner_access_flags() const { 1637 return _inner_classes->at( 1638 _idx + InstanceKlass::inner_class_access_flags_offset); 1639 } 1640 }; 1641 1642 #endif // SHARE_OOPS_INSTANCEKLASS_HPP