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_KLASS_HPP
  26 #define SHARE_OOPS_KLASS_HPP
  27 
  28 #include "classfile/classLoaderData.hpp"
  29 #include "memory/iterator.hpp"
  30 #include "memory/memRegion.hpp"
  31 #include "oops/arrayStorageProperties.hpp"
  32 #include "oops/markOop.hpp"
  33 #include "oops/metadata.hpp"
  34 #include "oops/oop.hpp"
  35 #include "oops/oopHandle.hpp"
  36 #include "utilities/accessFlags.hpp"
  37 #include "utilities/macros.hpp"
  38 #if INCLUDE_JFR
  39 #include "jfr/support/jfrTraceIdExtension.hpp"
  40 #endif
  41 
  42 // Klass IDs for all subclasses of Klass
  43 enum KlassID {
  44   InstanceKlassID,
  45   InstanceRefKlassID,
  46   InstanceMirrorKlassID,
  47   InstanceClassLoaderKlassID,
  48   TypeArrayKlassID,
  49   ValueArrayKlassID,
  50   ObjArrayKlassID
  51 };
  52 
  53 const uint KLASS_ID_COUNT = 7;
  54 
  55 //
  56 // A Klass provides:
  57 //  1: language level class object (method dictionary etc.)
  58 //  2: provide vm dispatch behavior for the object
  59 // Both functions are combined into one C++ class.
  60 
  61 // One reason for the oop/klass dichotomy in the implementation is
  62 // that we don't want a C++ vtbl pointer in every object.  Thus,
  63 // normal oops don't have any virtual functions.  Instead, they
  64 // forward all "virtual" functions to their klass, which does have
  65 // a vtbl and does the C++ dispatch depending on the object's
  66 // actual type.  (See oop.inline.hpp for some of the forwarding code.)
  67 // ALL FUNCTIONS IMPLEMENTING THIS DISPATCH ARE PREFIXED WITH "oop_"!
  68 
  69 // Forward declarations.
  70 template <class T> class Array;
  71 template <class T> class GrowableArray;
  72 class fieldDescriptor;
  73 class KlassSizeStats;
  74 class klassVtable;
  75 class ModuleEntry;
  76 class PackageEntry;
  77 class ParCompactionManager;
  78 class PSPromotionManager;
  79 class vtableEntry;
  80 
  81 class Klass : public Metadata {
  82   friend class VMStructs;
  83   friend class JVMCIVMStructs;
  84  protected:
  85   // If you add a new field that points to any metaspace object, you
  86   // must add this field to Klass::metaspace_pointers_do().
  87 
  88   // note: put frequently-used fields together at start of klass structure
  89   // for better cache behavior (may not make much of a difference but sure won't hurt)
  90   enum { _primary_super_limit = 8 };
  91 
  92   // The "layout helper" is a combined descriptor of object layout.
  93   // For klasses which are neither instance nor array, the value is zero.
  94   //
  95   // For instances, layout helper is a positive number, the instance size.
  96   // This size is already passed through align_object_size and scaled to bytes.
  97   // The low order bit is set if instances of this class cannot be
  98   // allocated using the fastpath.
  99   //
 100   // For arrays, layout helper is a negative number, containing four
 101   // distinct bytes, as follows:
 102   //    MSB:[tag, hsz, ebt, log2(esz)]:LSB
 103   // where:
 104   //    tag is 0x80 if the elements are oops, 0xC0 if non-oops, 0xA0 if value types
 105   //    hsz is array header size in bytes (i.e., offset of first element)
 106   //    ebt is the BasicType of the elements
 107   //    esz is the element size in bytes
 108   // This packed word is arranged so as to be quickly unpacked by the
 109   // various fast paths that use the various subfields.
 110   //
 111   // The esz bits can be used directly by a SLL instruction, without masking.
 112   //
 113   // Note that the array-kind tag looks like 0x00 for instance klasses,
 114   // since their length in bytes is always less than 24Mb.
 115   //
 116   // Final note:  This comes first, immediately after C++ vtable,
 117   // because it is frequently queried.
 118   jint        _layout_helper;
 119 
 120   // Klass identifier used to implement devirtualized oop closure dispatching.
 121   const KlassID _id;
 122 
 123   // The fields _super_check_offset, _secondary_super_cache, _secondary_supers
 124   // and _primary_supers all help make fast subtype checks.  See big discussion
 125   // in doc/server_compiler/checktype.txt
 126   //
 127   // Where to look to observe a supertype (it is &_secondary_super_cache for
 128   // secondary supers, else is &_primary_supers[depth()].
 129   juint       _super_check_offset;
 130 
 131   // Class name.  Instance classes: java/lang/String, etc.  Array classes: [I,
 132   // [Ljava/lang/String;, etc.  Set to zero for all other kinds of classes.
 133   Symbol*     _name;
 134 
 135   // Cache of last observed secondary supertype
 136   Klass*      _secondary_super_cache;
 137   // Array of all secondary supertypes
 138   Array<Klass*>* _secondary_supers;
 139   // Ordered list of all primary supertypes
 140   Klass*      _primary_supers[_primary_super_limit];
 141   // java/lang/Class instance mirroring this class
 142   OopHandle _java_mirror;
 143   // Superclass
 144   Klass*      _super;
 145   // First subclass (NULL if none); _subklass->next_sibling() is next one
 146   Klass* volatile _subklass;
 147   // Sibling link (or NULL); links all subklasses of a klass
 148   Klass* volatile _next_sibling;
 149 
 150   // All klasses loaded by a class loader are chained through these links
 151   Klass*      _next_link;
 152 
 153   // The VM's representation of the ClassLoader used to load this class.
 154   // Provide access the corresponding instance java.lang.ClassLoader.
 155   ClassLoaderData* _class_loader_data;
 156 
 157   jint        _modifier_flags;  // Processed access flags, for use by Class.getModifiers.
 158   AccessFlags _access_flags;    // Access flags. The class/interface distinction is stored here.
 159 
 160   JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
 161 
 162   // Biased locking implementation and statistics
 163   // (the 64-bit chunk goes first, to avoid some fragmentation)
 164   jlong    _last_biased_lock_bulk_revocation_time;
 165   markOop  _prototype_header;   // Used when biased locking is both enabled and disabled for this type
 166   jint     _biased_lock_revocation_count;
 167 
 168   // vtable length
 169   int _vtable_len;
 170 
 171 private:
 172   // This is an index into FileMapHeader::_shared_path_table[], to
 173   // associate this class with the JAR file where it's loaded from during
 174   // dump time. If a class is not loaded from the shared archive, this field is
 175   // -1.
 176   jshort _shared_class_path_index;
 177 
 178 #if INCLUDE_CDS
 179   // Flags of the current shared class.
 180   u2     _shared_class_flags;
 181   enum {
 182     _has_raw_archived_mirror = 1
 183   };
 184 #endif
 185   // The _archived_mirror is set at CDS dump time pointing to the cached mirror
 186   // in the open archive heap region when archiving java object is supported.
 187   CDS_JAVA_HEAP_ONLY(narrowOop _archived_mirror;)
 188 
 189 protected:
 190 
 191   // Constructor
 192   Klass(KlassID id);
 193   Klass() : _id(KlassID(-1)) { assert(DumpSharedSpaces || UseSharedSpaces, "only for cds"); }
 194 
 195   void* operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw();
 196 
 197  public:
 198   int id() { return _id; }
 199 
 200   enum DefaultsLookupMode { find_defaults, skip_defaults };
 201   enum OverpassLookupMode { find_overpass, skip_overpass };
 202   enum StaticLookupMode   { find_static,   skip_static };
 203   enum PrivateLookupMode  { find_private,  skip_private };
 204 
 205   bool is_klass() const volatile { return true; }
 206 
 207   // super() cannot be InstanceKlass* -- Java arrays are covariant, and _super is used
 208   // to implement that. NB: the _super of "[Ljava/lang/Integer;" is "[Ljava/lang/Number;"
 209   // If this is not what your code expects, you're probably looking for Klass::java_super().
 210   Klass* super() const               { return _super; }
 211   void set_super(Klass* k)           { _super = k; }
 212 
 213   // initializes _super link, _primary_supers & _secondary_supers arrays
 214   void initialize_supers(Klass* k, Array<InstanceKlass*>* transitive_interfaces, TRAPS);
 215 
 216   // klass-specific helper for initializing _secondary_supers
 217   virtual GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
 218                                                           Array<InstanceKlass*>* transitive_interfaces);
 219 
 220   // java_super is the Java-level super type as specified by Class.getSuperClass.
 221   virtual InstanceKlass* java_super() const  { return NULL; }
 222 
 223   juint    super_check_offset() const  { return _super_check_offset; }
 224   void set_super_check_offset(juint o) { _super_check_offset = o; }
 225 
 226   Klass* secondary_super_cache() const     { return _secondary_super_cache; }
 227   void set_secondary_super_cache(Klass* k) { _secondary_super_cache = k; }
 228 
 229   Array<Klass*>* secondary_supers() const { return _secondary_supers; }
 230   void set_secondary_supers(Array<Klass*>* k) { _secondary_supers = k; }
 231 
 232   // Return the element of the _super chain of the given depth.
 233   // If there is no such element, return either NULL or this.
 234   Klass* primary_super_of_depth(juint i) const {
 235     assert(i < primary_super_limit(), "oob");
 236     Klass* super = _primary_supers[i];
 237     assert(super == NULL || super->super_depth() == i, "correct display");
 238     return super;
 239   }
 240 
 241   // Can this klass be a primary super?  False for interfaces and arrays of
 242   // interfaces.  False also for arrays or classes with long super chains.
 243   bool can_be_primary_super() const {
 244     const juint secondary_offset = in_bytes(secondary_super_cache_offset());
 245     return super_check_offset() != secondary_offset;
 246   }
 247   virtual bool can_be_primary_super_slow() const;
 248 
 249   // Returns number of primary supers; may be a number in the inclusive range [0, primary_super_limit].
 250   juint super_depth() const {
 251     if (!can_be_primary_super()) {
 252       return primary_super_limit();
 253     } else {
 254       juint d = (super_check_offset() - in_bytes(primary_supers_offset())) / sizeof(Klass*);
 255       assert(d < primary_super_limit(), "oob");
 256       assert(_primary_supers[d] == this, "proper init");
 257       return d;
 258     }
 259   }
 260 
 261   // java mirror
 262   oop java_mirror() const;
 263   oop java_mirror_no_keepalive() const;
 264   void set_java_mirror(Handle m);
 265 
 266   oop archived_java_mirror_raw() NOT_CDS_JAVA_HEAP_RETURN_(NULL); // no GC barrier
 267   narrowOop archived_java_mirror_raw_narrow() NOT_CDS_JAVA_HEAP_RETURN_(0); // no GC barrier
 268   void set_archived_java_mirror_raw(oop m) NOT_CDS_JAVA_HEAP_RETURN; // no GC barrier
 269 
 270   // Temporary mirror switch used by RedefineClasses
 271   // Both mirrors are on the ClassLoaderData::_handles list already so no
 272   // barriers are needed.
 273   void set_java_mirror_handle(OopHandle mirror) { _java_mirror = mirror; }
 274   OopHandle java_mirror_handle() const          {
 275     return _java_mirror;
 276   }
 277 
 278   // modifier flags
 279   jint modifier_flags() const          { return _modifier_flags; }
 280   void set_modifier_flags(jint flags)  { _modifier_flags = flags; }
 281 
 282   // size helper
 283   int layout_helper() const            { return _layout_helper; }
 284   void set_layout_helper(int lh)       { _layout_helper = lh; }
 285 
 286   // Note: for instances layout_helper() may include padding.
 287   // Use InstanceKlass::contains_field_offset to classify field offsets.
 288 
 289   // sub/superklass links
 290   Klass* subklass(bool log = false) const;
 291   Klass* next_sibling(bool log = false) const;
 292 
 293   InstanceKlass* superklass() const;
 294   void append_to_sibling_list();           // add newly created receiver to superklass' subklass list
 295 
 296   void set_next_link(Klass* k) { _next_link = k; }
 297   Klass* next_link() const { return _next_link; }   // The next klass defined by the class loader.
 298 
 299   // class loader data
 300   ClassLoaderData* class_loader_data() const               { return _class_loader_data; }
 301   void set_class_loader_data(ClassLoaderData* loader_data) {  _class_loader_data = loader_data; }
 302 
 303   int shared_classpath_index() const   {
 304     return _shared_class_path_index;
 305   };
 306 
 307   void set_shared_classpath_index(int index) {
 308     _shared_class_path_index = index;
 309   };
 310 
 311   void set_has_raw_archived_mirror() {
 312     CDS_ONLY(_shared_class_flags |= _has_raw_archived_mirror;)
 313   }
 314   void clear_has_raw_archived_mirror() {
 315     CDS_ONLY(_shared_class_flags &= ~_has_raw_archived_mirror;)
 316   }
 317   bool has_raw_archived_mirror() const {
 318     CDS_ONLY(return (_shared_class_flags & _has_raw_archived_mirror) != 0;)
 319     NOT_CDS(return false;)
 320   }
 321 
 322   // Obtain the module or package for this class
 323   virtual ModuleEntry* module() const = 0;
 324   virtual PackageEntry* package() const = 0;
 325 
 326  protected:                                // internal accessors
 327   void     set_subklass(Klass* s);
 328   void     set_next_sibling(Klass* s);
 329 
 330  public:
 331 
 332   // Compiler support
 333   static ByteSize super_offset()                 { return in_ByteSize(offset_of(Klass, _super)); }
 334   static ByteSize super_check_offset_offset()    { return in_ByteSize(offset_of(Klass, _super_check_offset)); }
 335   static ByteSize primary_supers_offset()        { return in_ByteSize(offset_of(Klass, _primary_supers)); }
 336   static ByteSize secondary_super_cache_offset() { return in_ByteSize(offset_of(Klass, _secondary_super_cache)); }
 337   static ByteSize secondary_supers_offset()      { return in_ByteSize(offset_of(Klass, _secondary_supers)); }
 338   static ByteSize java_mirror_offset()           { return in_ByteSize(offset_of(Klass, _java_mirror)); }
 339   static ByteSize class_loader_data_offset()     { return in_ByteSize(offset_of(Klass, _class_loader_data)); }
 340   static ByteSize modifier_flags_offset()        { return in_ByteSize(offset_of(Klass, _modifier_flags)); }
 341   static ByteSize layout_helper_offset()         { return in_ByteSize(offset_of(Klass, _layout_helper)); }
 342   static ByteSize access_flags_offset()          { return in_ByteSize(offset_of(Klass, _access_flags)); }
 343 
 344   // Unpacking layout_helper:
 345   enum {
 346     _lh_neutral_value           = 0,  // neutral non-array non-instance value
 347     _lh_instance_slow_path_bit  = 0x01,
 348     _lh_log2_element_size_shift = BitsPerByte*0,
 349     _lh_log2_element_size_mask  = BitsPerLong-1,
 350     _lh_element_type_shift      = BitsPerByte*1,
 351     _lh_element_type_mask       = right_n_bits(BitsPerByte),  // shifted mask
 352     _lh_header_size_shift       = BitsPerByte*2,
 353     _lh_header_size_mask        = right_n_bits(BitsPerByte),  // shifted mask
 354     _lh_array_tag_bits          = 3,
 355     _lh_array_tag_shift         = BitsPerInt - _lh_array_tag_bits
 356   };
 357 
 358   static const unsigned int _lh_array_tag_type_value = 0Xfffffffc;
 359   static const unsigned int _lh_array_tag_vt_value   = 0Xfffffffd;
 360   static const unsigned int _lh_array_tag_obj_value  = 0Xfffffffe;
 361 
 362   static int layout_helper_size_in_bytes(jint lh) {
 363     assert(lh > (jint)_lh_neutral_value, "must be instance");
 364     return (int) lh & ~_lh_instance_slow_path_bit;
 365   }
 366   static bool layout_helper_needs_slow_path(jint lh) {
 367     assert(lh > (jint)_lh_neutral_value, "must be instance");
 368     return (lh & _lh_instance_slow_path_bit) != 0;
 369   }
 370   static bool layout_helper_is_instance(jint lh) {
 371     return (jint)lh > (jint)_lh_neutral_value;
 372   }
 373   static bool layout_helper_is_array(jint lh) {
 374     return (jint)lh < (jint)_lh_neutral_value;
 375   }
 376   static bool layout_helper_is_typeArray(jint lh) {
 377     return (juint) _lh_array_tag_type_value == (juint)(lh >> _lh_array_tag_shift);
 378   }
 379   static bool layout_helper_is_objArray(jint lh) {
 380     return (juint)_lh_array_tag_obj_value == (juint)(lh >> _lh_array_tag_shift);
 381   }
 382   static bool layout_helper_is_valueArray(jint lh) {
 383     return (juint)_lh_array_tag_vt_value == (juint)(lh >> _lh_array_tag_shift);
 384   }
 385   static int layout_helper_header_size(jint lh) {
 386     assert(lh < (jint)_lh_neutral_value, "must be array");
 387     int hsize = (lh >> _lh_header_size_shift) & _lh_header_size_mask;
 388     assert(hsize > 0 && hsize < (int)sizeof(oopDesc)*3, "sanity");
 389     return hsize;
 390   }
 391   static BasicType layout_helper_element_type(jint lh) {
 392     assert(lh < (jint)_lh_neutral_value, "must be array");
 393     int btvalue = (lh >> _lh_element_type_shift) & _lh_element_type_mask;
 394     assert((btvalue >= T_BOOLEAN && btvalue <= T_OBJECT) || btvalue == T_VALUETYPE, "sanity");
 395     return (BasicType) btvalue;
 396   }
 397 
 398   // Want a pattern to quickly diff against layout header in register
 399   // find something less clever!
 400   static int layout_helper_boolean_diffbit() {
 401     jint zlh = array_layout_helper(T_BOOLEAN);
 402     jint blh = array_layout_helper(T_BYTE);
 403     assert(zlh != blh, "array layout helpers must differ");
 404     int diffbit = 1;
 405     while ((diffbit & (zlh ^ blh)) == 0 && (diffbit & zlh) == 0) {
 406       diffbit <<= 1;
 407       assert(diffbit != 0, "make sure T_BOOLEAN has a different bit than T_BYTE");
 408     }
 409     return diffbit;
 410   }
 411 
 412   static int layout_helper_log2_element_size(jint lh) {
 413     assert(lh < (jint)_lh_neutral_value, "must be array");
 414     int l2esz = (lh >> _lh_log2_element_size_shift) & _lh_log2_element_size_mask;
 415     assert(layout_helper_element_type(lh) == T_VALUETYPE || l2esz <= LogBytesPerLong,
 416            "sanity. l2esz: 0x%x for lh: 0x%x", (uint)l2esz, (uint)lh);
 417     return l2esz;
 418   }
 419   static jint array_layout_helper(jint tag, int hsize, BasicType etype, int log2_esize) {
 420     return (tag        << _lh_array_tag_shift)
 421       |    (hsize      << _lh_header_size_shift)
 422       |    ((int)etype << _lh_element_type_shift)
 423       |    (log2_esize << _lh_log2_element_size_shift);
 424   }
 425   static jint instance_layout_helper(jint size, bool slow_path_flag) {
 426     return (size << LogBytesPerWord)
 427       |    (slow_path_flag ? _lh_instance_slow_path_bit : 0);
 428   }
 429   static int layout_helper_to_size_helper(jint lh) {
 430     assert(lh > (jint)_lh_neutral_value, "must be instance");
 431     // Note that the following expression discards _lh_instance_slow_path_bit.
 432     return lh >> LogBytesPerWord;
 433   }
 434   // Out-of-line version computes everything based on the etype:
 435   static jint array_layout_helper(BasicType etype);
 436 
 437   // What is the maximum number of primary superclasses any klass can have?
 438   static juint primary_super_limit()         { return _primary_super_limit; }
 439 
 440   // vtables
 441   klassVtable vtable() const;
 442   int vtable_length() const { return _vtable_len; }
 443 
 444   // subclass check
 445   bool is_subclass_of(const Klass* k) const;
 446   // subtype check: true if is_subclass_of, or if k is interface and receiver implements it
 447   bool is_subtype_of(Klass* k) const {
 448     juint    off = k->super_check_offset();
 449     Klass* sup = *(Klass**)( (address)this + off );
 450     const juint secondary_offset = in_bytes(secondary_super_cache_offset());
 451     if (sup == k) {
 452       return true;
 453     } else if (off != secondary_offset) {
 454       return false;
 455     } else {
 456       return search_secondary_supers(k);
 457     }
 458   }
 459 
 460   bool search_secondary_supers(Klass* k) const;
 461 
 462   // Find LCA in class hierarchy
 463   Klass *LCA( Klass *k );
 464 
 465   // Check whether reflection/jni/jvm code is allowed to instantiate this class;
 466   // if not, throw either an Error or an Exception.
 467   virtual void check_valid_for_instantiation(bool throwError, TRAPS);
 468 
 469   // array copying
 470   virtual void  copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS);
 471 
 472   // tells if the class should be initialized
 473   virtual bool should_be_initialized() const    { return false; }
 474   // initializes the klass
 475   virtual void initialize(TRAPS);
 476   // lookup operation for MethodLookupCache
 477   friend class MethodLookupCache;
 478   virtual Klass* find_field(Symbol* name, Symbol* signature, fieldDescriptor* fd) const;
 479   virtual Method* uncached_lookup_method(const Symbol* name, const Symbol* signature,
 480                                          OverpassLookupMode overpass_mode,
 481                                          PrivateLookupMode = find_private) const;
 482  public:
 483   Method* lookup_method(const Symbol* name, const Symbol* signature) const {
 484     return uncached_lookup_method(name, signature, find_overpass);
 485   }
 486 
 487   // array class with specific rank
 488   Klass* array_klass(int rank, TRAPS) {
 489     return array_klass_impl(ArrayStorageProperties::empty, false, rank, THREAD);
 490   }
 491 
 492   Klass* array_klass(ArrayStorageProperties storage_props, int rank, TRAPS) {
 493     return array_klass_impl(storage_props, false, rank, THREAD);
 494   }
 495 
 496   // array class with this klass as element type
 497   Klass* array_klass(TRAPS) {
 498     return array_klass_impl(ArrayStorageProperties::empty, false, THREAD);
 499   }
 500 
 501   Klass* array_klass(ArrayStorageProperties storage_props, TRAPS) {
 502     return array_klass_impl(storage_props, false, THREAD);
 503   }
 504 
 505   // These will return NULL instead of allocating on the heap:
 506   // NB: these can block for a mutex, like other functions with TRAPS arg.
 507   Klass* array_klass_or_null(ArrayStorageProperties storage_props, int rank);
 508   Klass* array_klass_or_null(ArrayStorageProperties storage_props);
 509 
 510   virtual oop protection_domain() const = 0;
 511 
 512   oop class_loader() const;
 513 
 514   // This loads the klass's holder as a phantom. This is useful when a weak Klass
 515   // pointer has been "peeked" and then must be kept alive before it may
 516   // be used safely.  All uses of klass_holder need to apply the appropriate barriers,
 517   // except during GC.
 518   oop klass_holder() const { return class_loader_data()->holder_phantom(); }
 519 
 520  protected:
 521   virtual Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, int rank, TRAPS);
 522   virtual Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, TRAPS);
 523 
 524   // Error handling when length > max_length or length < 0
 525   static void check_array_allocation_length(int length, int max_length, TRAPS);
 526 
 527   void set_vtable_length(int len) { _vtable_len= len; }
 528 
 529   vtableEntry* start_of_vtable() const;
 530  public:
 531   Method* method_at_vtable(int index);
 532 
 533   static ByteSize vtable_start_offset();
 534   static ByteSize vtable_length_offset() {
 535     return byte_offset_of(Klass, _vtable_len);
 536   }
 537 
 538   // CDS support - remove and restore oops from metadata. Oops are not shared.
 539   virtual void remove_unshareable_info();
 540   virtual void remove_java_mirror();
 541   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
 542 
 543  public:
 544   // subclass accessor (here for convenience; undefined for non-klass objects)
 545   virtual bool is_leaf_class() const { fatal("not a class"); return false; }
 546  public:
 547   // ALL FUNCTIONS BELOW THIS POINT ARE DISPATCHED FROM AN OOP
 548   // These functions describe behavior for the oop not the KLASS.
 549 
 550   // actual oop size of obj in memory
 551   virtual int oop_size(oop obj) const = 0;
 552 
 553   // Size of klass in word size.
 554   virtual int size() const = 0;
 555 #if INCLUDE_SERVICES
 556   virtual void collect_statistics(KlassSizeStats *sz) const;
 557 #endif
 558 
 559   // Returns the Java name for a class (Resource allocated)
 560   // For arrays, this returns the name of the element with a leading '['.
 561   // For classes, this returns the name with the package separators
 562   //     turned into '.'s.
 563   const char* external_name() const;
 564   // Returns the name for a class (Resource allocated) as the class
 565   // would appear in a signature.
 566   // For arrays, this returns the name of the element with a leading '['.
 567   // For classes, this returns the name with a leading 'L' and a trailing ';'
 568   //     and the package separators as '/'.
 569   // For value classes, this returns the name with a leading 'Q' and a trailing ';'
 570   //     and the package separators as '/'.
 571   virtual const char* signature_name() const;
 572 
 573   const char* joint_in_module_of_loader(const Klass* class2, bool include_parent_loader = false) const;
 574   const char* class_in_module_of_loader(bool use_are = false, bool include_parent_loader = false) const;
 575 
 576   // Returns "interface", "abstract class" or "class".
 577   const char* external_kind() const;
 578 
 579   // type testing operations
 580 #ifdef ASSERT
 581  protected:
 582   virtual bool is_instance_klass_slow()     const { return false; }
 583   virtual bool is_array_klass_slow()        const { return false; }
 584   virtual bool is_objArray_klass_slow()     const { return false; }
 585   virtual bool is_typeArray_klass_slow()    const { return false; }
 586   virtual bool is_valueArray_klass_slow()   const { return false; }
 587 #endif // ASSERT
 588   // current implementation uses this method even in non debug builds
 589   virtual bool is_value_slow()          const { return false; }
 590  public:
 591 
 592   // Fast non-virtual versions
 593   #ifndef ASSERT
 594   #define assert_same_query(xval, xcheck) xval
 595   #else
 596  private:
 597   static bool assert_same_query(bool xval, bool xslow) {
 598     assert(xval == xslow, "slow and fast queries agree");
 599     return xval;
 600   }
 601  public:
 602   #endif
 603   inline  bool is_instance_klass()            const { return assert_same_query(
 604                                                       layout_helper_is_instance(layout_helper()),
 605                                                       is_instance_klass_slow()); }
 606   inline  bool is_array_klass()               const { return assert_same_query(
 607                                                     layout_helper_is_array(layout_helper()),
 608                                                     is_array_klass_slow()); }
 609   inline  bool is_objArray_klass()            const { return assert_same_query(
 610                                                     layout_helper_is_objArray(layout_helper()),
 611                                                     is_objArray_klass_slow()); }
 612   inline  bool is_typeArray_klass()           const { return assert_same_query(
 613                                                     layout_helper_is_typeArray(layout_helper()),
 614                                                     is_typeArray_klass_slow()); }
 615   inline  bool is_value()                     const { return is_value_slow(); } //temporary hack
 616   inline  bool is_valueArray_klass()          const { return assert_same_query(
 617                                                     layout_helper_is_valueArray(layout_helper()),
 618                                                     is_valueArray_klass_slow()); }
 619 
 620   #undef assert_same_query
 621 
 622   // Access flags
 623   AccessFlags access_flags() const         { return _access_flags;  }
 624   void set_access_flags(AccessFlags flags) { _access_flags = flags; }
 625 
 626   bool is_public() const                { return _access_flags.is_public(); }
 627   bool is_final() const                 { return _access_flags.is_final(); }
 628   bool is_interface() const             { return _access_flags.is_interface(); }
 629   bool is_abstract() const              { return _access_flags.is_abstract(); }
 630   bool is_super() const                 { return _access_flags.is_super(); }
 631   bool is_synthetic() const             { return _access_flags.is_synthetic(); }
 632   void set_is_synthetic()               { _access_flags.set_is_synthetic(); }
 633   bool has_finalizer() const            { return _access_flags.has_finalizer(); }
 634   bool has_final_method() const         { return _access_flags.has_final_method(); }
 635   void set_has_finalizer()              { _access_flags.set_has_finalizer(); }
 636   void set_has_final_method()           { _access_flags.set_has_final_method(); }
 637   bool has_vanilla_constructor() const  { return _access_flags.has_vanilla_constructor(); }
 638   void set_has_vanilla_constructor()    { _access_flags.set_has_vanilla_constructor(); }
 639   bool has_miranda_methods () const     { return access_flags().has_miranda_methods(); }
 640   void set_has_miranda_methods()        { _access_flags.set_has_miranda_methods(); }
 641   bool is_shared() const                { return access_flags().is_shared_class(); } // shadows MetaspaceObj::is_shared)()
 642   void set_is_shared()                  { _access_flags.set_is_shared_class(); }
 643 
 644   bool is_cloneable() const;
 645   void set_is_cloneable();
 646 
 647   // Biased locking support
 648   // Note: the prototype header is always set up to be at least the
 649   // prototype markOop. If biased locking is enabled it may further be
 650   // biasable and have an epoch.
 651   markOop prototype_header() const      { return _prototype_header; }
 652   static inline markOop default_prototype_header(Klass* k) {
 653     return (k == NULL) ? markOopDesc::prototype() : k->prototype_header();
 654   }
 655 
 656   // NOTE: once instances of this klass are floating around in the
 657   // system, this header must only be updated at a safepoint.
 658   // NOTE 2: currently we only ever set the prototype header to the
 659   // biasable prototype for instanceKlasses. There is no technical
 660   // reason why it could not be done for arrayKlasses aside from
 661   // wanting to reduce the initial scope of this optimization. There
 662   // are potential problems in setting the bias pattern for
 663   // JVM-internal oops.
 664   inline void set_prototype_header(markOop header);
 665   static ByteSize prototype_header_offset() { return in_ByteSize(offset_of(Klass, _prototype_header)); }
 666 
 667   int  biased_lock_revocation_count() const { return (int) _biased_lock_revocation_count; }
 668   // Atomically increments biased_lock_revocation_count and returns updated value
 669   int atomic_incr_biased_lock_revocation_count();
 670   void set_biased_lock_revocation_count(int val) { _biased_lock_revocation_count = (jint) val; }
 671   jlong last_biased_lock_bulk_revocation_time() { return _last_biased_lock_bulk_revocation_time; }
 672   void  set_last_biased_lock_bulk_revocation_time(jlong cur_time) { _last_biased_lock_bulk_revocation_time = cur_time; }
 673 
 674   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
 675 
 676   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
 677   virtual MetaspaceObj::Type type() const { return ClassType; }
 678 
 679   // Iff the class loader (or mirror for unsafe anonymous classes) is alive the
 680   // Klass is considered alive. This is safe to call before the CLD is marked as
 681   // unloading, and hence during concurrent class unloading.
 682   bool is_loader_alive() const { return class_loader_data()->is_alive(); }
 683 
 684   void clean_subklass();
 685 
 686   static void clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses = true);
 687   static void clean_subklass_tree() {
 688     clean_weak_klass_links(/*unloading_occurred*/ true , /* clean_alive_klasses */ false);
 689   }
 690 
 691   virtual void array_klasses_do(void f(Klass* k)) {}
 692 
 693   // Return self, except for abstract classes with exactly 1
 694   // implementor.  Then return the 1 concrete implementation.
 695   Klass *up_cast_abstract();
 696 
 697   // klass name
 698   Symbol* name() const                   { return _name; }
 699   void set_name(Symbol* n);
 700 
 701  public:
 702   // jvm support
 703   virtual jint compute_modifier_flags(TRAPS) const;
 704 
 705   // JVMTI support
 706   virtual jint jvmti_class_status() const;
 707 
 708   // Printing
 709   virtual void print_on(outputStream* st) const;
 710 
 711   virtual void oop_print_value_on(oop obj, outputStream* st);
 712   virtual void oop_print_on      (oop obj, outputStream* st);
 713 
 714   virtual const char* internal_name() const = 0;
 715 
 716   // Verification
 717   virtual void verify_on(outputStream* st);
 718   void verify() { verify_on(tty); }
 719 
 720 #ifndef PRODUCT
 721   bool verify_vtable_index(int index);
 722 #endif
 723 
 724   virtual void oop_verify_on(oop obj, outputStream* st);
 725 
 726   // for error reporting
 727   static bool is_valid(Klass* k);
 728 };
 729 
 730 #endif // SHARE_OOPS_KLASS_HPP