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