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