< prev index next >

src/hotspot/share/classfile/classFileParser.cpp

Print this page




   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 #include "precompiled.hpp"
  25 #include "jvm.h"
  26 #include "aot/aotLoader.hpp"
  27 #include "classfile/classFileParser.hpp"
  28 #include "classfile/classFileStream.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/classLoaderData.inline.hpp"
  31 #include "classfile/defaultMethods.hpp"
  32 #include "classfile/dictionary.hpp"

  33 #include "classfile/javaClasses.inline.hpp"
  34 #include "classfile/moduleEntry.hpp"
  35 #include "classfile/packageEntry.hpp"
  36 #include "classfile/symbolTable.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/verificationType.hpp"
  39 #include "classfile/verifier.hpp"
  40 #include "classfile/vmSymbols.hpp"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/allocation.hpp"
  44 #include "memory/metadataFactory.hpp"
  45 #include "memory/oopFactory.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "memory/universe.hpp"
  48 #include "oops/annotations.hpp"
  49 #include "oops/constantPool.inline.hpp"
  50 #include "oops/fieldStreams.hpp"
  51 #include "oops/instanceKlass.hpp"
  52 #include "oops/instanceMirrorKlass.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/klassVtable.hpp"
  55 #include "oops/metadata.hpp"
  56 #include "oops/method.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "oops/symbol.hpp"

  59 #include "prims/jvmtiExport.hpp"
  60 #include "prims/jvmtiThreadState.hpp"
  61 #include "runtime/arguments.hpp"

  62 #include "runtime/handles.inline.hpp"
  63 #include "runtime/javaCalls.hpp"
  64 #include "runtime/os.hpp"
  65 #include "runtime/perfData.hpp"
  66 #include "runtime/reflection.hpp"
  67 #include "runtime/safepointVerifiers.hpp"
  68 #include "runtime/signature.hpp"
  69 #include "runtime/timer.hpp"
  70 #include "services/classLoadingService.hpp"
  71 #include "services/threadService.hpp"
  72 #include "utilities/align.hpp"
  73 #include "utilities/bitMap.inline.hpp"
  74 #include "utilities/copy.hpp"
  75 #include "utilities/exceptions.hpp"
  76 #include "utilities/globalDefinitions.hpp"
  77 #include "utilities/growableArray.hpp"
  78 #include "utilities/macros.hpp"
  79 #include "utilities/ostream.hpp"
  80 #include "utilities/resourceHash.hpp"
  81 #include "utilities/utf8.hpp"


 110 
 111 // Used for backward compatibility reasons:
 112 // - to disallow argument and require ACC_STATIC for <clinit> methods
 113 #define JAVA_7_VERSION                    51
 114 
 115 // Extension method support.
 116 #define JAVA_8_VERSION                    52
 117 
 118 #define JAVA_9_VERSION                    53
 119 
 120 #define JAVA_10_VERSION                   54
 121 
 122 #define JAVA_11_VERSION                   55
 123 
 124 #define JAVA_12_VERSION                   56
 125 
 126 #define JAVA_13_VERSION                   57
 127 
 128 #define JAVA_14_VERSION                   58
 129 


 130 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
 131   assert((bad_constant == JVM_CONSTANT_Module ||
 132           bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
 133          "Unexpected bad constant pool entry");
 134   if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
 135 }
 136 
 137 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
 138                                                   ConstantPool* cp,
 139                                                   const int length,
 140                                                   TRAPS) {
 141   assert(stream != NULL, "invariant");
 142   assert(cp != NULL, "invariant");
 143 
 144   // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
 145   // this function (_current can be allocated in a register, with scalar
 146   // replacement of aggregates). The _current pointer is copied back to
 147   // stream() when this function returns. DON'T call another method within
 148   // this method that uses stream().
 149   const ClassFileStream cfs1 = *stream;
 150   const ClassFileStream* const cfs = &cfs1;
 151 
 152   assert(cfs->allocated_on_stack(), "should be local");
 153   debug_only(const u1* const old_current = stream->current();)
 154 
 155   // Used for batching symbol allocations.
 156   const char* names[SymbolTable::symbol_alloc_batch_size];
 157   int lengths[SymbolTable::symbol_alloc_batch_size];
 158   int indices[SymbolTable::symbol_alloc_batch_size];
 159   unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
 160   int names_count = 0;
 161 
 162   // parsing  Index 0 is unused
 163   for (int index = 1; index < length; index++) {
 164     // Each of the following case guarantees one more byte in the stream
 165     // for the following tag or the access_flags following constant pool,
 166     // so we don't need bounds-check for reading tag.
 167     const u1 tag = cfs->get_u1_fast();
 168     switch (tag) {
 169       case JVM_CONSTANT_Class : {
 170         cfs->guarantee_more(3, CHECK);  // name_index, tag/access_flags
 171         const u2 name_index = cfs->get_u2_fast();
 172         cp->klass_index_at_put(index, name_index);
 173         break;
 174       }
 175       case JVM_CONSTANT_Fieldref: {
 176         cfs->guarantee_more(5, CHECK);  // class_index, name_and_type_index, tag/access_flags
 177         const u2 class_index = cfs->get_u2_fast();
 178         const u2 name_and_type_index = cfs->get_u2_fast();
 179         cp->field_at_put(index, class_index, name_and_type_index);
 180         break;
 181       }
 182       case JVM_CONSTANT_Methodref: {
 183         cfs->guarantee_more(5, CHECK);  // class_index, name_and_type_index, tag/access_flags
 184         const u2 class_index = cfs->get_u2_fast();
 185         const u2 name_and_type_index = cfs->get_u2_fast();
 186         cp->method_at_put(index, class_index, name_and_type_index);
 187         break;
 188       }
 189       case JVM_CONSTANT_InterfaceMethodref: {


 479         check_property(valid_symbol_at(name_ref_index),
 480           "Invalid constant pool index %u in class file %s",
 481           name_ref_index, CHECK);
 482         check_property(valid_symbol_at(signature_ref_index),
 483           "Invalid constant pool index %u in class file %s",
 484           signature_ref_index, CHECK);
 485         break;
 486       }
 487       case JVM_CONSTANT_Utf8:
 488         break;
 489       case JVM_CONSTANT_UnresolvedClass:         // fall-through
 490       case JVM_CONSTANT_UnresolvedClassInError: {
 491         ShouldNotReachHere();     // Only JVM_CONSTANT_ClassIndex should be present
 492         break;
 493       }
 494       case JVM_CONSTANT_ClassIndex: {
 495         const int class_index = cp->klass_index_at(index);
 496         check_property(valid_symbol_at(class_index),
 497           "Invalid constant pool index %u in class file %s",
 498           class_index, CHECK);
 499         cp->unresolved_klass_at_put(index, class_index, num_klasses++);







 500         break;
 501       }
 502       case JVM_CONSTANT_StringIndex: {
 503         const int string_index = cp->string_index_at(index);
 504         check_property(valid_symbol_at(string_index),
 505           "Invalid constant pool index %u in class file %s",
 506           string_index, CHECK);
 507         Symbol* const sym = cp->symbol_at(string_index);
 508         cp->unresolved_string_at_put(index, sym);
 509         break;
 510       }
 511       case JVM_CONSTANT_MethodHandle: {
 512         const int ref_index = cp->method_handle_index_at(index);
 513         check_property(valid_cp_range(ref_index, length),
 514           "Invalid constant pool index %u in class file %s",
 515           ref_index, CHECK);
 516         const constantTag tag = cp->tag_at(ref_index);
 517         const int ref_kind = cp->method_handle_ref_kind_at(index);
 518 
 519         switch (ref_kind) {


 736             classfile_parse_error(
 737               "Bad method name at constant pool index %u in class file %s",
 738               name_ref_index, CHECK);
 739           }
 740         }
 741         break;
 742       }
 743       case JVM_CONSTANT_MethodHandle: {
 744         const int ref_index = cp->method_handle_index_at(index);
 745         const int ref_kind = cp->method_handle_ref_kind_at(index);
 746         switch (ref_kind) {
 747           case JVM_REF_invokeVirtual:
 748           case JVM_REF_invokeStatic:
 749           case JVM_REF_invokeSpecial:
 750           case JVM_REF_newInvokeSpecial: {
 751             const int name_and_type_ref_index =
 752               cp->name_and_type_ref_index_at(ref_index);
 753             const int name_ref_index =
 754               cp->name_ref_index_at(name_and_type_ref_index);
 755             const Symbol* const name = cp->symbol_at(name_ref_index);
 756             if (ref_kind == JVM_REF_newInvokeSpecial) {
 757               if (name != vmSymbols::object_initializer_name()) {
 758                 classfile_parse_error(
 759                   "Bad constructor name at constant pool index %u in class file %s",
 760                     name_ref_index, CHECK);
 761               }
 762             } else {
 763               if (name == vmSymbols::object_initializer_name()) {











 764                 classfile_parse_error(
 765                   "Bad method name at constant pool index %u in class file %s",
 766                   name_ref_index, CHECK);
 767               }
 768             }
 769             break;
 770           }
 771           // Other ref_kinds are already fully checked in previous pass.
 772         } // switch(ref_kind)
 773         break;
 774       }
 775       case JVM_CONSTANT_MethodType: {
 776         const Symbol* const no_name = vmSymbols::type_name(); // place holder
 777         const Symbol* const signature = cp->method_type_signature_at(index);
 778         verify_legal_method_signature(no_name, signature, CHECK);
 779         break;
 780       }
 781       case JVM_CONSTANT_Utf8: {
 782         assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
 783       }


1046       guarantee_property(value_type.is_int(),
1047                          "Inconsistent constant value type in class file %s",
1048                          CHECK);
1049       break;
1050     }
1051     case T_OBJECT: {
1052       guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;")
1053                          && value_type.is_string()),
1054                          "Bad string initial value in class file %s",
1055                          CHECK);
1056       break;
1057     }
1058     default: {
1059       classfile_parse_error("Unable to set initial value %u in class file %s",
1060                              constantvalue_index,
1061                              CHECK);
1062     }
1063   }
1064 }
1065 
1066 class AnnotationCollector : public ResourceObj{
1067 public:
1068   enum Location { _in_field, _in_method, _in_class };
1069   enum ID {
1070     _unknown = 0,
1071     _method_CallerSensitive,
1072     _method_ForceInline,
1073     _method_DontInline,
1074     _method_InjectedProfile,
1075     _method_LambdaForm_Compiled,
1076     _method_Hidden,
1077     _method_HotSpotIntrinsicCandidate,
1078     _jdk_internal_vm_annotation_Contended,
1079     _field_Stable,
1080     _jdk_internal_vm_annotation_ReservedStackAccess,
1081     _annotation_LIMIT
1082   };
1083   const Location _location;
1084   int _annotations_present;
1085   u2 _contended_group;
1086 
1087   AnnotationCollector(Location location)
1088     : _location(location), _annotations_present(0)
1089   {
1090     assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
1091   }
1092   // If this annotation name has an ID, report it (or _none).
1093   ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name);
1094   // Set the annotation name:
1095   void set_annotation(ID id) {
1096     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1097     _annotations_present |= nth_bit((int)id);
1098   }
1099 
1100   void remove_annotation(ID id) {
1101     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1102     _annotations_present &= ~nth_bit((int)id);
1103   }
1104 
1105   // Report if the annotation is present.
1106   bool has_any_annotations() const { return _annotations_present != 0; }
1107   bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; }
1108 
1109   void set_contended_group(u2 group) { _contended_group = group; }
1110   u2 contended_group() const { return _contended_group; }
1111 
1112   bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); }
1113 
1114   void set_stable(bool stable) { set_annotation(_field_Stable); }
1115   bool is_stable() const { return has_annotation(_field_Stable); }
1116 };
1117 
1118 // This class also doubles as a holder for metadata cleanup.
1119 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector {
1120 private:
1121   ClassLoaderData* _loader_data;
1122   AnnotationArray* _field_annotations;
1123   AnnotationArray* _field_type_annotations;
1124 public:
1125   FieldAnnotationCollector(ClassLoaderData* loader_data) :
1126     AnnotationCollector(_in_field),
1127     _loader_data(loader_data),
1128     _field_annotations(NULL),
1129     _field_type_annotations(NULL) {}
1130   ~FieldAnnotationCollector();
1131   void apply_to(FieldInfo* f);
1132   AnnotationArray* field_annotations()      { return _field_annotations; }
1133   AnnotationArray* field_type_annotations() { return _field_type_annotations; }
1134 
1135   void set_field_annotations(AnnotationArray* a)      { _field_annotations = a; }
1136   void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; }
1137 };
1138 
1139 class MethodAnnotationCollector : public AnnotationCollector{
1140 public:
1141   MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
1142   void apply_to(const methodHandle& m);
1143 };
1144 
1145 class ClassFileParser::ClassAnnotationCollector : public AnnotationCollector{
1146 public:
1147   ClassAnnotationCollector() : AnnotationCollector(_in_class) { }
1148   void apply_to(InstanceKlass* ik);
1149 };
1150 
1151 
1152 static int skip_annotation_value(const u1*, int, int); // fwd decl
1153 
1154 // Safely increment index by val if does not pass limit
1155 #define SAFE_ADD(index, limit, val) \
1156 if (index >= limit - val) return limit; \
1157 index += val;
1158 
1159 // Skip an annotation.  Return >=limit if there is any problem.
1160 static int skip_annotation(const u1* buffer, int limit, int index) {
1161   assert(buffer != NULL, "invariant");
1162   // annotation := atype:u2 do(nmem:u2) {member:u2 value}
1163   // value := switch (tag:u1) { ... }
1164   SAFE_ADD(index, limit, 4); // skip atype and read nmem
1165   int nmem = Bytes::get_Java_u2((address)buffer + index - 2);
1166   while (--nmem >= 0 && index < limit) {
1167     SAFE_ADD(index, limit, 2); // skip member
1168     index = skip_annotation_value(buffer, limit, index);
1169   }
1170   return index;


1448                                             CHECK);
1449   parsed_annotations->set_field_annotations(a);
1450   a = assemble_annotations(runtime_visible_type_annotations,
1451                            runtime_visible_type_annotations_length,
1452                            runtime_invisible_type_annotations,
1453                            runtime_invisible_type_annotations_length,
1454                            CHECK);
1455   parsed_annotations->set_field_type_annotations(a);
1456   return;
1457 }
1458 
1459 
1460 // Field allocation types. Used for computing field offsets.
1461 
1462 enum FieldAllocationType {
1463   STATIC_OOP,           // Oops
1464   STATIC_BYTE,          // Boolean, Byte, char
1465   STATIC_SHORT,         // shorts
1466   STATIC_WORD,          // ints
1467   STATIC_DOUBLE,        // aligned long or double

1468   NONSTATIC_OOP,
1469   NONSTATIC_BYTE,
1470   NONSTATIC_SHORT,
1471   NONSTATIC_WORD,
1472   NONSTATIC_DOUBLE,

1473   MAX_FIELD_ALLOCATION_TYPE,
1474   BAD_ALLOCATION_TYPE = -1
1475 };
1476 
1477 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1478   BAD_ALLOCATION_TYPE, // 0
1479   BAD_ALLOCATION_TYPE, // 1
1480   BAD_ALLOCATION_TYPE, // 2
1481   BAD_ALLOCATION_TYPE, // 3
1482   NONSTATIC_BYTE ,     // T_BOOLEAN     =  4,
1483   NONSTATIC_SHORT,     // T_CHAR        =  5,
1484   NONSTATIC_WORD,      // T_FLOAT       =  6,
1485   NONSTATIC_DOUBLE,    // T_DOUBLE      =  7,
1486   NONSTATIC_BYTE,      // T_BYTE        =  8,
1487   NONSTATIC_SHORT,     // T_SHORT       =  9,
1488   NONSTATIC_WORD,      // T_INT         = 10,
1489   NONSTATIC_DOUBLE,    // T_LONG        = 11,
1490   NONSTATIC_OOP,       // T_OBJECT      = 12,
1491   NONSTATIC_OOP,       // T_ARRAY       = 13,
1492   BAD_ALLOCATION_TYPE, // T_VOID        = 14,
1493   BAD_ALLOCATION_TYPE, // T_ADDRESS     = 15,
1494   BAD_ALLOCATION_TYPE, // T_NARROWOOP   = 16,
1495   BAD_ALLOCATION_TYPE, // T_METADATA    = 17,
1496   BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1497   BAD_ALLOCATION_TYPE, // T_CONFLICT    = 19,

1498   BAD_ALLOCATION_TYPE, // 0
1499   BAD_ALLOCATION_TYPE, // 1
1500   BAD_ALLOCATION_TYPE, // 2
1501   BAD_ALLOCATION_TYPE, // 3
1502   STATIC_BYTE ,        // T_BOOLEAN     =  4,
1503   STATIC_SHORT,        // T_CHAR        =  5,
1504   STATIC_WORD,         // T_FLOAT       =  6,
1505   STATIC_DOUBLE,       // T_DOUBLE      =  7,
1506   STATIC_BYTE,         // T_BYTE        =  8,
1507   STATIC_SHORT,        // T_SHORT       =  9,
1508   STATIC_WORD,         // T_INT         = 10,
1509   STATIC_DOUBLE,       // T_LONG        = 11,
1510   STATIC_OOP,          // T_OBJECT      = 12,
1511   STATIC_OOP,          // T_ARRAY       = 13,
1512   BAD_ALLOCATION_TYPE, // T_VOID        = 14,
1513   BAD_ALLOCATION_TYPE, // T_ADDRESS     = 15,
1514   BAD_ALLOCATION_TYPE, // T_NARROWOOP   = 16,
1515   BAD_ALLOCATION_TYPE, // T_METADATA    = 17,
1516   BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1517   BAD_ALLOCATION_TYPE, // T_CONFLICT    = 19,

1518 };
1519 
1520 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {
1521   assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1522   FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1523   assert(result != BAD_ALLOCATION_TYPE, "bad type");



1524   return result;
1525 }
1526 
1527 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1528  public:
1529   u2 count[MAX_FIELD_ALLOCATION_TYPE];
1530 
1531   FieldAllocationCount() {
1532     for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1533       count[i] = 0;
1534     }
1535   }
1536 
1537   FieldAllocationType update(bool is_static, BasicType type) {
1538     FieldAllocationType atype = basic_type_to_atype(is_static, type);
1539     if (atype != BAD_ALLOCATION_TYPE) {
1540       // Make sure there is no overflow with injected fields.
1541       assert(count[atype] < 0xFFFF, "More than 65535 fields");
1542       count[atype]++;
1543     }
1544     return atype;
1545   }
1546 };
1547 
1548 // Side-effects: populates the _fields, _fields_annotations,
1549 // _fields_type_annotations fields
1550 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1551                                    bool is_interface,

1552                                    FieldAllocationCount* const fac,
1553                                    ConstantPool* cp,
1554                                    const int cp_size,
1555                                    u2* const java_fields_count_ptr,
1556                                    TRAPS) {
1557 
1558   assert(cfs != NULL, "invariant");
1559   assert(fac != NULL, "invariant");
1560   assert(cp != NULL, "invariant");
1561   assert(java_fields_count_ptr != NULL, "invariant");
1562 
1563   assert(NULL == _fields, "invariant");
1564   assert(NULL == _fields_annotations, "invariant");
1565   assert(NULL == _fields_type_annotations, "invariant");
1566 
1567   cfs->guarantee_more(2, CHECK);  // length
1568   const u2 length = cfs->get_u2_fast();
1569   *java_fields_count_ptr = length;
1570 
1571   int num_injected = 0;
1572   const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1573                                                                   &num_injected);
1574   const int total_fields = length + num_injected;




1575 
1576   // The field array starts with tuples of shorts
1577   // [access, name index, sig index, initial value index, byte offset].
1578   // A generic signature slot only exists for field with generic
1579   // signature attribute. And the access flag is set with
1580   // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic
1581   // signature slots are at the end of the field array and after all
1582   // other fields data.
1583   //
1584   //   f1: [access, name index, sig index, initial value index, low_offset, high_offset]
1585   //   f2: [access, name index, sig index, initial value index, low_offset, high_offset]
1586   //       ...
1587   //   fn: [access, name index, sig index, initial value index, low_offset, high_offset]
1588   //       [generic signature index]
1589   //       [generic signature index]
1590   //       ...
1591   //
1592   // Allocate a temporary resource array for field data. For each field,
1593   // a slot is reserved in the temporary array for the generic signature
1594   // index. After parsing all fields, the data are copied to a permanent
1595   // array and any unused slots will be discarded.
1596   ResourceMark rm(THREAD);
1597   u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
1598                                               u2,
1599                                               total_fields * (FieldInfo::field_slots + 1));
1600 
1601   // The generic signature slots start after all other fields' data.
1602   int generic_signature_slot = total_fields * FieldInfo::field_slots;
1603   int num_generic_signature = 0;

1604   for (int n = 0; n < length; n++) {
1605     // access_flags, name_index, descriptor_index, attributes_count
1606     cfs->guarantee_more(8, CHECK);
1607 




1608     AccessFlags access_flags;
1609     const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1610     verify_legal_field_modifiers(flags, is_interface, CHECK);
1611     access_flags.set_flags(flags);
1612 
1613     const u2 name_index = cfs->get_u2_fast();
1614     check_property(valid_symbol_at(name_index),
1615       "Invalid constant pool index %u for field name in class file %s",
1616       name_index, CHECK);
1617     const Symbol* const name = cp->symbol_at(name_index);
1618     verify_legal_field_name(name, CHECK);
1619 
1620     const u2 signature_index = cfs->get_u2_fast();
1621     check_property(valid_symbol_at(signature_index),
1622       "Invalid constant pool index %u for field signature in class file %s",
1623       signature_index, CHECK);
1624     const Symbol* const sig = cp->symbol_at(signature_index);
1625     verify_legal_field_signature(name, sig, CHECK);

















1626 
1627     u2 constantvalue_index = 0;
1628     bool is_synthetic = false;
1629     u2 generic_signature_index = 0;
1630     const bool is_static = access_flags.is_static();
1631     FieldAnnotationCollector parsed_annotations(_loader_data);
1632 
1633     const u2 attributes_count = cfs->get_u2_fast();
1634     if (attributes_count > 0) {
1635       parse_field_attributes(cfs,
1636                              attributes_count,
1637                              is_static,
1638                              signature_index,
1639                              &constantvalue_index,
1640                              &is_synthetic,
1641                              &generic_signature_index,
1642                              &parsed_annotations,
1643                              CHECK);
1644 
1645       if (parsed_annotations.field_annotations() != NULL) {


1665 
1666       if (is_synthetic) {
1667         access_flags.set_is_synthetic();
1668       }
1669       if (generic_signature_index != 0) {
1670         access_flags.set_field_has_generic_signature();
1671         fa[generic_signature_slot] = generic_signature_index;
1672         generic_signature_slot ++;
1673         num_generic_signature ++;
1674       }
1675     }
1676 
1677     FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1678     field->initialize(access_flags.as_short(),
1679                       name_index,
1680                       signature_index,
1681                       constantvalue_index);
1682     const BasicType type = cp->basic_type_for_signature_at(signature_index);
1683 
1684     // Remember how many oops we encountered and compute allocation type
1685     const FieldAllocationType atype = fac->update(is_static, type);
1686     field->set_allocation_type(atype);
1687 
1688     // After field is initialized with type, we can augment it with aux info
1689     if (parsed_annotations.has_any_annotations())
1690       parsed_annotations.apply_to(field);
1691   }
1692 
1693   int index = length;
1694   if (num_injected != 0) {
1695     for (int n = 0; n < num_injected; n++) {
1696       // Check for duplicates
1697       if (injected[n].may_be_java) {
1698         const Symbol* const name      = injected[n].name();
1699         const Symbol* const signature = injected[n].signature();
1700         bool duplicate = false;
1701         for (int i = 0; i < length; i++) {
1702           const FieldInfo* const f = FieldInfo::from_field_array(fa, i);
1703           if (name      == cp->symbol_at(f->name_index()) &&
1704               signature == cp->symbol_at(f->signature_index())) {
1705             // Symbol is desclared in Java so skip this one
1706             duplicate = true;
1707             break;
1708           }
1709         }
1710         if (duplicate) {
1711           // These will be removed from the field array at the end
1712           continue;
1713         }
1714       }
1715 
1716       // Injected field
1717       FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1718       field->initialize(JVM_ACC_FIELD_INTERNAL,
1719                         injected[n].name_index,
1720                         injected[n].signature_index,
1721                         0);
1722 
1723       const BasicType type = FieldType::basic_type(injected[n].signature());
1724 
1725       // Remember how many oops we encountered and compute allocation type
1726       const FieldAllocationType atype = fac->update(false, type);
1727       field->set_allocation_type(atype);
1728       index++;
1729     }
1730   }
1731 

























1732   assert(NULL == _fields, "invariant");
1733 
1734   _fields =
1735     MetadataFactory::new_array<u2>(_loader_data,
1736                                    index * FieldInfo::field_slots + num_generic_signature,
1737                                    CHECK);
1738   // Sometimes injected fields already exist in the Java source so
1739   // the fields array could be too long.  In that case the
1740   // fields array is trimed. Also unused slots that were reserved
1741   // for generic signature indexes are discarded.
1742   {
1743     int i = 0;
1744     for (; i < index * FieldInfo::field_slots; i++) {
1745       _fields->at_put(i, fa[i]);
1746     }
1747     for (int j = total_fields * FieldInfo::field_slots;
1748          j < generic_signature_slot; j++) {
1749       _fields->at_put(i++, fa[j]);
1750     }
1751     assert(_fields->length() == i, "");


2027         "Exception name has bad type at constant pool %u in class file %s",
2028         checked_exception, CHECK_NULL);
2029     }
2030   }
2031   // check exceptions attribute length
2032   if (_need_verify) {
2033     guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
2034                                                    sizeof(u2) * size),
2035                       "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
2036   }
2037   return checked_exceptions_start;
2038 }
2039 
2040 void ClassFileParser::throwIllegalSignature(const char* type,
2041                                             const Symbol* name,
2042                                             const Symbol* sig,
2043                                             TRAPS) const {
2044   assert(name != NULL, "invariant");
2045   assert(sig != NULL, "invariant");
2046 





2047   ResourceMark rm(THREAD);
2048   Exceptions::fthrow(THREAD_AND_LOCATION,
2049       vmSymbols::java_lang_ClassFormatError(),
2050       "%s \"%s\" in class %s has illegal signature \"%s\"", type,
2051       name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
2052 }
2053 
2054 AnnotationCollector::ID
2055 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
2056                                       const Symbol* name) {
2057   const vmSymbols::SID sid = vmSymbols::find_sid(name);
2058   // Privileged code can use all annotations.  Other code silently drops some.
2059   const bool privileged = loader_data->is_the_null_class_loader_data() ||
2060                           loader_data->is_platform_class_loader_data() ||
2061                           loader_data->is_unsafe_anonymous();
2062   switch (sid) {
2063     case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
2064       if (_location != _in_method)  break;  // only allow for methods
2065       if (!privileged)              break;  // only allow in privileged code
2066       return _method_CallerSensitive;
2067     }
2068     case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
2069       if (_location != _in_method)  break;  // only allow for methods
2070       if (!privileged)              break;  // only allow in privileged code
2071       return _method_ForceInline;


2295                              runtime_visible_type_annotations_length,
2296                              runtime_invisible_type_annotations,
2297                              runtime_invisible_type_annotations_length,
2298                              CHECK);
2299     cm->set_type_annotations(a);
2300   }
2301 }
2302 
2303 
2304 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2305 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2306 // Method* to save footprint, so we only know the size of the resulting Method* when the
2307 // entire method attribute is parsed.
2308 //
2309 // The promoted_flags parameter is used to pass relevant access_flags
2310 // from the method back up to the containing klass. These flag values
2311 // are added to klass's access_flags.
2312 
2313 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2314                                       bool is_interface,

2315                                       const ConstantPool* cp,
2316                                       AccessFlags* const promoted_flags,
2317                                       TRAPS) {
2318   assert(cfs != NULL, "invariant");
2319   assert(cp != NULL, "invariant");
2320   assert(promoted_flags != NULL, "invariant");
2321 
2322   ResourceMark rm(THREAD);
2323   // Parse fixed parts:
2324   // access_flags, name_index, descriptor_index, attributes_count
2325   cfs->guarantee_more(8, CHECK_NULL);
2326 
2327   int flags = cfs->get_u2_fast();
2328   const u2 name_index = cfs->get_u2_fast();
2329   const int cp_size = cp->length();
2330   check_property(
2331     valid_symbol_at(name_index),
2332     "Illegal constant pool index %u for method name in class file %s",
2333     name_index, CHECK_NULL);
2334   const Symbol* const name = cp->symbol_at(name_index);
2335   verify_legal_method_name(name, CHECK_NULL);
2336 
2337   const u2 signature_index = cfs->get_u2_fast();
2338   guarantee_property(
2339     valid_symbol_at(signature_index),
2340     "Illegal constant pool index %u for method signature in class file %s",
2341     signature_index, CHECK_NULL);
2342   const Symbol* const signature = cp->symbol_at(signature_index);
2343 
2344   if (name == vmSymbols::class_initializer_name()) {
2345     // We ignore the other access flags for a valid class initializer.
2346     // (JVM Spec 2nd ed., chapter 4.6)
2347     if (_major_version < 51) { // backward compatibility
2348       flags = JVM_ACC_STATIC;
2349     } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2350       flags &= JVM_ACC_STATIC | JVM_ACC_STRICT;
2351     } else {
2352       classfile_parse_error("Method <clinit> is not static in class file %s", CHECK_NULL);
2353     }
2354   } else {
2355     verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2356   }
2357 
2358   if (name == vmSymbols::object_initializer_name() && is_interface) {
2359     classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL);










































2360   }
2361 
2362   int args_size = -1;  // only used when _need_verify is true
2363   if (_need_verify) {
2364     args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2365                  verify_legal_method_signature(name, signature, CHECK_NULL);
2366     if (args_size > MAX_ARGS_SIZE) {
2367       classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_NULL);
2368     }
2369   }
2370 
2371   AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2372 
2373   // Default values for code and exceptions attribute elements
2374   u2 max_stack = 0;
2375   u2 max_locals = 0;
2376   u4 code_length = 0;
2377   const u1* code_start = 0;
2378   u2 exception_table_length = 0;
2379   const unsafe_u2* exception_table_start = NULL; // (potentially unaligned) pointer to array of u2 elements


2910       _has_finalizer = true;
2911     }
2912   }
2913   if (name == vmSymbols::object_initializer_name() &&
2914       signature == vmSymbols::void_method_signature() &&
2915       m->is_vanilla_constructor()) {
2916     _has_vanilla_constructor = true;
2917   }
2918 
2919   NOT_PRODUCT(m->verify());
2920   return m;
2921 }
2922 
2923 
2924 // The promoted_flags parameter is used to pass relevant access_flags
2925 // from the methods back up to the containing klass. These flag values
2926 // are added to klass's access_flags.
2927 // Side-effects: populates the _methods field in the parser
2928 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2929                                     bool is_interface,

2930                                     AccessFlags* promoted_flags,
2931                                     bool* has_final_method,
2932                                     bool* declares_nonstatic_concrete_methods,
2933                                     TRAPS) {
2934   assert(cfs != NULL, "invariant");
2935   assert(promoted_flags != NULL, "invariant");
2936   assert(has_final_method != NULL, "invariant");
2937   assert(declares_nonstatic_concrete_methods != NULL, "invariant");
2938 
2939   assert(NULL == _methods, "invariant");
2940 
2941   cfs->guarantee_more(2, CHECK);  // length
2942   const u2 length = cfs->get_u2_fast();
2943   if (length == 0) {
2944     _methods = Universe::the_empty_method_array();
2945   } else {
2946     _methods = MetadataFactory::new_array<Method*>(_loader_data,
2947                                                    length,
2948                                                    NULL,
2949                                                    CHECK);
2950 
2951     for (int index = 0; index < length; index++) {
2952       Method* method = parse_method(cfs,
2953                                     is_interface,

2954                                     _cp,
2955                                     promoted_flags,
2956                                     CHECK);
2957 
2958       if (method->is_final()) {
2959         *has_final_method = true;
2960       }
2961       // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2962       // used for interface initialization, and default method inheritance analysis
2963       if (is_interface && !(*declares_nonstatic_concrete_methods)
2964         && !method->is_abstract() && !method->is_static()) {
2965         *declares_nonstatic_concrete_methods = true;
2966       }
2967       _methods->at_put(index, method);
2968     }
2969 
2970     if (_need_verify && length > 1) {
2971       // Check duplicated methods
2972       ResourceMark rm(THREAD);
2973       NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(


3126       valid_klass_reference_at(inner_class_info_index),
3127       "inner_class_info_index %u has bad constant type in class file %s",
3128       inner_class_info_index, CHECK_0);
3129     // Outer class index
3130     const u2 outer_class_info_index = cfs->get_u2_fast();
3131     check_property(
3132       outer_class_info_index == 0 ||
3133         valid_klass_reference_at(outer_class_info_index),
3134       "outer_class_info_index %u has bad constant type in class file %s",
3135       outer_class_info_index, CHECK_0);
3136     // Inner class name
3137     const u2 inner_name_index = cfs->get_u2_fast();
3138     check_property(
3139       inner_name_index == 0 || valid_symbol_at(inner_name_index),
3140       "inner_name_index %u has bad constant type in class file %s",
3141       inner_name_index, CHECK_0);
3142     if (_need_verify) {
3143       guarantee_property(inner_class_info_index != outer_class_info_index,
3144                          "Class is both outer and inner class in class file %s", CHECK_0);
3145     }
3146     // Access flags
3147     jint flags;
3148     // JVM_ACC_MODULE is defined in JDK-9 and later.
3149     if (_major_version >= JAVA_9_VERSION) {
3150       flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3151     } else {
3152       flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3153     }








3154     if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3155       // Set abstract bit for old class files for backward compatibility
3156       flags |= JVM_ACC_ABSTRACT;
3157     }
3158     verify_legal_class_modifiers(flags, CHECK_0);
3159     AccessFlags inner_access_flags(flags);
3160 
3161     inner_classes->at_put(index++, inner_class_info_index);
3162     inner_classes->at_put(index++, outer_class_info_index);
3163     inner_classes->at_put(index++, inner_name_index);
3164     inner_classes->at_put(index++, inner_access_flags.as_short());
3165   }
3166 
3167   // 4347400: make sure there's no duplicate entry in the classes array
3168   if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3169     for(int i = 0; i < length * 4; i += 4) {
3170       for(int j = i + 4; j < length * 4; j += 4) {
3171         guarantee_property((inner_classes->at(i)   != inner_classes->at(j) ||
3172                             inner_classes->at(i+1) != inner_classes->at(j+1) ||
3173                             inner_classes->at(i+2) != inner_classes->at(j+2) ||


3336   u2 attributes_count = cfs->get_u2_fast();
3337   bool parsed_sourcefile_attribute = false;
3338   bool parsed_innerclasses_attribute = false;
3339   bool parsed_nest_members_attribute = false;
3340   bool parsed_nest_host_attribute = false;
3341   bool parsed_enclosingmethod_attribute = false;
3342   bool parsed_bootstrap_methods_attribute = false;
3343   const u1* runtime_visible_annotations = NULL;
3344   int runtime_visible_annotations_length = 0;
3345   const u1* runtime_invisible_annotations = NULL;
3346   int runtime_invisible_annotations_length = 0;
3347   const u1* runtime_visible_type_annotations = NULL;
3348   int runtime_visible_type_annotations_length = 0;
3349   const u1* runtime_invisible_type_annotations = NULL;
3350   int runtime_invisible_type_annotations_length = 0;
3351   bool runtime_invisible_type_annotations_exists = false;
3352   bool runtime_invisible_annotations_exists = false;
3353   bool parsed_source_debug_ext_annotations_exist = false;
3354   const u1* inner_classes_attribute_start = NULL;
3355   u4  inner_classes_attribute_length = 0;


3356   u2  enclosing_method_class_index = 0;
3357   u2  enclosing_method_method_index = 0;
3358   const u1* nest_members_attribute_start = NULL;
3359   u4  nest_members_attribute_length = 0;
3360 
3361   // Iterate over attributes
3362   while (attributes_count--) {
3363     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3364     const u2 attribute_name_index = cfs->get_u2_fast();
3365     const u4 attribute_length = cfs->get_u4_fast();
3366     check_property(
3367       valid_symbol_at(attribute_name_index),
3368       "Attribute name has bad constant pool index %u in class file %s",
3369       attribute_name_index, CHECK);
3370     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3371     if (tag == vmSymbols::tag_source_file()) {
3372       // Check for SourceFile tag
3373       if (_need_verify) {
3374         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3375       }


3685       }
3686     }
3687     if (runtime_invisible_annotations != NULL) {
3688       for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3689         int append = runtime_visible_annotations_length+i;
3690         annotations->at_put(append, runtime_invisible_annotations[i]);
3691       }
3692     }
3693   }
3694   return annotations;
3695 }
3696 
3697 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3698                                                         const int super_class_index,
3699                                                         const bool need_verify,
3700                                                         TRAPS) {
3701   assert(cp != NULL, "invariant");
3702   const InstanceKlass* super_klass = NULL;
3703 
3704   if (super_class_index == 0) {
3705     check_property(_class_name == vmSymbols::java_lang_Object(),

3706                    "Invalid superclass index %u in class file %s",
3707                    super_class_index,
3708                    CHECK_NULL);
3709   } else {
3710     check_property(valid_klass_reference_at(super_class_index),
3711                    "Invalid superclass index %u in class file %s",
3712                    super_class_index,
3713                    CHECK_NULL);
3714     // The class name should be legal because it is checked when parsing constant pool.
3715     // However, make sure it is not an array type.
3716     bool is_array = false;
3717     if (cp->tag_at(super_class_index).is_klass()) {
3718       super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3719       if (need_verify)
3720         is_array = super_klass->is_array_klass();
3721     } else if (need_verify) {
3722       is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3723     }
3724     if (need_verify) {
3725       guarantee_property(!is_array,
3726                         "Bad superclass name in class file %s", CHECK_NULL);
3727     }
3728   }
3729   return super_klass;
3730 }
3731 
3732 static unsigned int compute_oop_map_count(const InstanceKlass* super,
3733                                           unsigned int nonstatic_oop_map_count,
3734                                           int first_nonstatic_oop_offset) {
3735 
3736   unsigned int map_count =
3737     NULL == super ? 0 : super->nonstatic_oop_map_count();
3738   if (nonstatic_oop_map_count > 0) {
3739     // We have oops to add to map
3740     if (map_count == 0) {
3741       map_count = nonstatic_oop_map_count;
3742     }
3743     else {
3744       // Check whether we should add a new map block or whether the last one can
3745       // be extended
3746       const OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps();
3747       const OopMapBlock* const last_map = first_map + map_count - 1;
3748 
3749       const int next_offset = last_map->offset() + last_map->count() * heapOopSize;
3750       if (next_offset == first_nonstatic_oop_offset) {
3751         // There is no gap bettwen superklass's last oop field and first
3752         // local oop field, merge maps.
3753         nonstatic_oop_map_count -= 1;
3754       }
3755       else {
3756         // Superklass didn't end with a oop field, add extra maps
3757         assert(next_offset < first_nonstatic_oop_offset, "just checking");
3758       }
3759       map_count += nonstatic_oop_map_count;
3760     }
3761   }
3762   return map_count;
3763 }
3764 
3765 #ifndef PRODUCT
3766 static void print_field_layout(const Symbol* name,
3767                                Array<u2>* fields,
3768                                const constantPoolHandle& cp,
3769                                int instance_size,
3770                                int instance_fields_start,
3771                                int instance_fields_end,
3772                                int static_fields_end) {
3773 
3774   assert(name != NULL, "invariant");
3775 
3776   tty->print("%s: field layout\n", name->as_klass_external_name());
3777   tty->print("  @%3d %s\n", instance_fields_start, "--- instance fields start ---");
3778   for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3779     if (!fs.access_flags().is_static()) {
3780       tty->print("  @%3d \"%s\" %s\n",
3781         fs.offset(),
3782         fs.name()->as_klass_external_name(),
3783         fs.signature()->as_klass_external_name());
3784     }
3785   }
3786   tty->print("  @%3d %s\n", instance_fields_end, "--- instance fields end ---");
3787   tty->print("  @%3d %s\n", instance_size * wordSize, "--- instance ends ---");
3788   tty->print("  @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---");
3789   for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3790     if (fs.access_flags().is_static()) {
3791       tty->print("  @%3d \"%s\" %s\n",
3792         fs.offset(),
3793         fs.name()->as_klass_external_name(),
3794         fs.signature()->as_klass_external_name());
3795     }
3796   }
3797   tty->print("  @%3d %s\n", static_fields_end, "--- static fields end ---");
3798   tty->print("\n");
3799 }
3800 #endif
3801 
3802 // Values needed for oopmap and InstanceKlass creation
3803 class ClassFileParser::FieldLayoutInfo : public ResourceObj {
3804  public:
3805   int*          nonstatic_oop_offsets;
3806   unsigned int* nonstatic_oop_counts;
3807   unsigned int  nonstatic_oop_map_count;
3808   unsigned int  total_oop_map_count;
3809   int           instance_size;
3810   int           nonstatic_field_size;
3811   int           static_field_size;
3812   bool          has_nonstatic_fields;
3813 };


























































































































3814 
3815 // Layout fields and fill in FieldLayoutInfo.  Could use more refactoring!
3816 void ClassFileParser::layout_fields(ConstantPool* cp,
3817                                     const FieldAllocationCount* fac,
3818                                     const ClassAnnotationCollector* parsed_annotations,
3819                                     FieldLayoutInfo* info,
3820                                     TRAPS) {
3821 
3822   assert(cp != NULL, "invariant");
3823 
3824   // Field size and offset computation
3825   int nonstatic_field_size = _super_klass == NULL ? 0 :
3826                                _super_klass->nonstatic_field_size();






3827 
3828   // Count the contended fields by type.
3829   //
3830   // We ignore static fields, because @Contended is not supported for them.
3831   // The layout code below will also ignore the static fields.
3832   int nonstatic_contended_count = 0;
3833   FieldAllocationCount fac_contended;
3834   for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
3835     FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3836     if (fs.is_contended()) {
3837       fac_contended.count[atype]++;
3838       if (!fs.access_flags().is_static()) {
3839         nonstatic_contended_count++;
3840       }
3841     }
3842   }
3843 
3844 
3845   // Calculate the starting byte offsets
3846   int next_static_oop_offset    = InstanceMirrorKlass::offset_of_static_fields();

3847   int next_static_double_offset = next_static_oop_offset +
3848                                       ((fac->count[STATIC_OOP]) * heapOopSize);
3849   if (fac->count[STATIC_DOUBLE]) {
3850     next_static_double_offset = align_up(next_static_double_offset, BytesPerLong);
3851   }
3852 
3853   int next_static_word_offset   = next_static_double_offset +
3854                                     ((fac->count[STATIC_DOUBLE]) * BytesPerLong);
3855   int next_static_short_offset  = next_static_word_offset +
3856                                     ((fac->count[STATIC_WORD]) * BytesPerInt);
3857   int next_static_byte_offset   = next_static_short_offset +
3858                                   ((fac->count[STATIC_SHORT]) * BytesPerShort);
3859 
3860   int nonstatic_fields_start  = instanceOopDesc::base_offset_in_bytes() +
3861                                 nonstatic_field_size * heapOopSize;
3862 










3863   int next_nonstatic_field_offset = nonstatic_fields_start;
3864 
3865   const bool is_contended_class     = parsed_annotations->is_contended();
3866 
3867   // Class is contended, pad before all the fields
3868   if (is_contended_class) {
3869     next_nonstatic_field_offset += ContendedPaddingWidth;
3870   }
3871 








3872   // Compute the non-contended fields count.
3873   // The packing code below relies on these counts to determine if some field
3874   // can be squeezed into the alignment gap. Contended fields are obviously
3875   // exempt from that.
3876   unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE];
3877   unsigned int nonstatic_word_count   = fac->count[NONSTATIC_WORD]   - fac_contended.count[NONSTATIC_WORD];
3878   unsigned int nonstatic_short_count  = fac->count[NONSTATIC_SHORT]  - fac_contended.count[NONSTATIC_SHORT];
3879   unsigned int nonstatic_byte_count   = fac->count[NONSTATIC_BYTE]   - fac_contended.count[NONSTATIC_BYTE];
3880   unsigned int nonstatic_oop_count    = fac->count[NONSTATIC_OOP]    - fac_contended.count[NONSTATIC_OOP];
3881 




























































3882   // Total non-static fields count, including every contended field
3883   unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] +
3884                                         fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] +
3885                                         fac->count[NONSTATIC_OOP];
3886 
3887   const bool super_has_nonstatic_fields =
3888           (_super_klass != NULL && _super_klass->has_nonstatic_fields());
3889   const bool has_nonstatic_fields =
3890     super_has_nonstatic_fields || (nonstatic_fields_count != 0);

3891 





3892 
3893   // Prepare list of oops for oop map generation.
3894   //
3895   // "offset" and "count" lists are describing the set of contiguous oop
3896   // regions. offset[i] is the start of the i-th region, which then has
3897   // count[i] oops following. Before we know how many regions are required,
3898   // we pessimistically allocate the maps to fit all the oops into the
3899   // distinct regions.
3900   //
3901   // TODO: We add +1 to always allocate non-zero resource arrays; we need
3902   // to figure out if we still need to do this.
3903   unsigned int nonstatic_oop_map_count = 0;
3904   unsigned int max_nonstatic_oop_maps  = fac->count[NONSTATIC_OOP] + 1;
3905 
3906   int* nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3907             THREAD, int, max_nonstatic_oop_maps);
3908   unsigned int* const nonstatic_oop_counts  = NEW_RESOURCE_ARRAY_IN_THREAD(
3909             THREAD, unsigned int, max_nonstatic_oop_maps);



3910 
3911   int first_nonstatic_oop_offset = 0; // will be set for first oop field
3912 
3913   bool compact_fields   = CompactFields;
3914   int allocation_style = FieldsAllocationStyle;
3915   if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3916     assert(false, "0 <= FieldsAllocationStyle <= 2");
3917     allocation_style = 1; // Optimistic
3918   }
3919 
3920   // The next classes have predefined hard-coded fields offsets
3921   // (see in JavaClasses::compute_hard_coded_offsets()).
3922   // Use default fields allocation order for them.
3923   if( (allocation_style != 0 || compact_fields ) && _loader_data->class_loader() == NULL &&
3924       (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
3925        _class_name == vmSymbols::java_lang_Class() ||
3926        _class_name == vmSymbols::java_lang_ClassLoader() ||
3927        _class_name == vmSymbols::java_lang_ref_Reference() ||
3928        _class_name == vmSymbols::java_lang_ref_SoftReference() ||
3929        _class_name == vmSymbols::java_lang_StackTraceElement() ||


3938        _class_name == vmSymbols::java_lang_Integer() ||
3939        _class_name == vmSymbols::java_lang_Long())) {
3940     allocation_style = 0;     // Allocate oops first
3941     compact_fields   = false; // Don't compact fields
3942   }
3943 
3944   int next_nonstatic_oop_offset = 0;
3945   int next_nonstatic_double_offset = 0;
3946 
3947   // Rearrange fields for a given allocation style
3948   if( allocation_style == 0 ) {
3949     // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
3950     next_nonstatic_oop_offset    = next_nonstatic_field_offset;
3951     next_nonstatic_double_offset = next_nonstatic_oop_offset +
3952                                     (nonstatic_oop_count * heapOopSize);
3953   } else if( allocation_style == 1 ) {
3954     // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
3955     next_nonstatic_double_offset = next_nonstatic_field_offset;
3956   } else if( allocation_style == 2 ) {
3957     // Fields allocation: oops fields in super and sub classes are together.
3958     if( nonstatic_field_size > 0 && _super_klass != NULL &&
3959         _super_klass->nonstatic_oop_map_size() > 0 ) {
3960       const unsigned int map_count = _super_klass->nonstatic_oop_map_count();
3961       const OopMapBlock* const first_map = _super_klass->start_of_nonstatic_oop_maps();
3962       const OopMapBlock* const last_map = first_map + map_count - 1;
3963       const int next_offset = last_map->offset() + (last_map->count() * heapOopSize);
3964       if (next_offset == next_nonstatic_field_offset) {
3965         allocation_style = 0;   // allocate oops first
3966         next_nonstatic_oop_offset    = next_nonstatic_field_offset;
3967         next_nonstatic_double_offset = next_nonstatic_oop_offset +
3968                                        (nonstatic_oop_count * heapOopSize);
3969       }
3970     }
3971     if( allocation_style == 2 ) {
3972       allocation_style = 1;     // allocate oops last
3973       next_nonstatic_double_offset = next_nonstatic_field_offset;
3974     }
3975   } else {
3976     ShouldNotReachHere();
3977   }
3978 
3979   int nonstatic_oop_space_count   = 0;
3980   int nonstatic_word_space_count  = 0;
3981   int nonstatic_short_space_count = 0;
3982   int nonstatic_byte_space_count  = 0;
3983   int nonstatic_oop_space_offset = 0;
3984   int nonstatic_word_space_offset = 0;


4027   }
4028 
4029   int next_nonstatic_word_offset = next_nonstatic_double_offset +
4030                                      (nonstatic_double_count * BytesPerLong);
4031   int next_nonstatic_short_offset = next_nonstatic_word_offset +
4032                                       (nonstatic_word_count * BytesPerInt);
4033   int next_nonstatic_byte_offset = next_nonstatic_short_offset +
4034                                      (nonstatic_short_count * BytesPerShort);
4035   int next_nonstatic_padded_offset = next_nonstatic_byte_offset +
4036                                        nonstatic_byte_count;
4037 
4038   // let oops jump before padding with this allocation style
4039   if( allocation_style == 1 ) {
4040     next_nonstatic_oop_offset = next_nonstatic_padded_offset;
4041     if( nonstatic_oop_count > 0 ) {
4042       next_nonstatic_oop_offset = align_up(next_nonstatic_oop_offset, heapOopSize);
4043     }
4044     next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
4045   }
4046 










4047   // Iterate over fields again and compute correct offsets.
4048   // The field allocation type was temporarily stored in the offset slot.
4049   // oop fields are located before non-oop fields (static and non-static).
4050   for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
4051 
4052     // skip already laid out fields
4053     if (fs.is_offset_set()) continue;
4054 
4055     // contended instance fields are handled below
4056     if (fs.is_contended() && !fs.access_flags().is_static()) continue;
4057 
4058     int real_offset = 0;
4059     const FieldAllocationType atype = (const FieldAllocationType) fs.allocation_type();
4060 
4061     // pack the rest of the fields
4062     switch (atype) {


4063       case STATIC_OOP:
4064         real_offset = next_static_oop_offset;
4065         next_static_oop_offset += heapOopSize;
4066         break;
4067       case STATIC_BYTE:
4068         real_offset = next_static_byte_offset;
4069         next_static_byte_offset += 1;
4070         break;
4071       case STATIC_SHORT:
4072         real_offset = next_static_short_offset;
4073         next_static_short_offset += BytesPerShort;
4074         break;
4075       case STATIC_WORD:
4076         real_offset = next_static_word_offset;
4077         next_static_word_offset += BytesPerInt;
4078         break;
4079       case STATIC_DOUBLE:
4080         real_offset = next_static_double_offset;
4081         next_static_double_offset += BytesPerLong;
4082         break;

























4083       case NONSTATIC_OOP:
4084         if( nonstatic_oop_space_count > 0 ) {
4085           real_offset = nonstatic_oop_space_offset;
4086           nonstatic_oop_space_offset += heapOopSize;
4087           nonstatic_oop_space_count  -= 1;
4088         } else {
4089           real_offset = next_nonstatic_oop_offset;
4090           next_nonstatic_oop_offset += heapOopSize;
4091         }
4092 
4093         // Record this oop in the oop maps
4094         if( nonstatic_oop_map_count > 0 &&
4095             nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
4096             real_offset -
4097             int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
4098             heapOopSize ) {
4099           // This oop is adjacent to the previous one, add to current oop map
4100           assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check");
4101           nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
4102         } else {
4103           // This oop is not adjacent to the previous one, create new oop map
4104           assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check");
4105           nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
4106           nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
4107           nonstatic_oop_map_count += 1;
4108           if( first_nonstatic_oop_offset == 0 ) { // Undefined
4109             first_nonstatic_oop_offset = real_offset;
4110           }
4111         }
4112         break;
4113       case NONSTATIC_BYTE:
4114         if( nonstatic_byte_space_count > 0 ) {
4115           real_offset = nonstatic_byte_space_offset;
4116           nonstatic_byte_space_offset += 1;
4117           nonstatic_byte_space_count  -= 1;
4118         } else {
4119           real_offset = next_nonstatic_byte_offset;
4120           next_nonstatic_byte_offset += 1;
4121         }
4122         break;
4123       case NONSTATIC_SHORT:
4124         if( nonstatic_short_space_count > 0 ) {
4125           real_offset = nonstatic_short_space_offset;
4126           nonstatic_short_space_offset += BytesPerShort;
4127           nonstatic_short_space_count  -= 1;
4128         } else {
4129           real_offset = next_nonstatic_short_offset;
4130           next_nonstatic_short_offset += BytesPerShort;
4131         }


4200             break;
4201 
4202           case NONSTATIC_SHORT:
4203             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerShort);
4204             real_offset = next_nonstatic_padded_offset;
4205             next_nonstatic_padded_offset += BytesPerShort;
4206             break;
4207 
4208           case NONSTATIC_WORD:
4209             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerInt);
4210             real_offset = next_nonstatic_padded_offset;
4211             next_nonstatic_padded_offset += BytesPerInt;
4212             break;
4213 
4214           case NONSTATIC_DOUBLE:
4215             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerLong);
4216             real_offset = next_nonstatic_padded_offset;
4217             next_nonstatic_padded_offset += BytesPerLong;
4218             break;
4219 






4220           case NONSTATIC_OOP:
4221             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, heapOopSize);
4222             real_offset = next_nonstatic_padded_offset;
4223             next_nonstatic_padded_offset += heapOopSize;
4224 
4225             // Record this oop in the oop maps
4226             if( nonstatic_oop_map_count > 0 &&
4227                 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
4228                 real_offset -
4229                 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
4230                 heapOopSize ) {
4231               // This oop is adjacent to the previous one, add to current oop map
4232               assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check");
4233               nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
4234             } else {
4235               // This oop is not adjacent to the previous one, create new oop map
4236               assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check");
4237               nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
4238               nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
4239               nonstatic_oop_map_count += 1;
4240               if( first_nonstatic_oop_offset == 0 ) { // Undefined
4241                 first_nonstatic_oop_offset = real_offset;
4242               }
4243             }
4244             break;
4245 
4246           default:
4247             ShouldNotReachHere();
4248         }
4249 
4250         if (fs.contended_group() == 0) {
4251           // Contended group defines the equivalence class over the fields:
4252           // the fields within the same contended group are not inter-padded.
4253           // The only exception is default group, which does not incur the
4254           // equivalence, and so requires intra-padding.
4255           next_nonstatic_padded_offset += ContendedPaddingWidth;
4256         }
4257 
4258         fs.set_offset(real_offset);
4259       } // for
4260 
4261       // Start laying out the next group.
4262       // Note that this will effectively pad the last group in the back;
4263       // this is expected to alleviate memory contention effects for
4264       // subclass fields and/or adjacent object.
4265       // If this was the default group, the padding is already in place.
4266       if (current_group != 0) {
4267         next_nonstatic_padded_offset += ContendedPaddingWidth;
4268       }
4269     }
4270 
4271     // handle static fields
4272   }
4273 
4274   // Entire class is contended, pad in the back.
4275   // This helps to alleviate memory contention effects for subclass fields
4276   // and/or adjacent object.
4277   if (is_contended_class) {

4278     next_nonstatic_padded_offset += ContendedPaddingWidth;
4279   }
4280 
4281   int notaligned_nonstatic_fields_end = next_nonstatic_padded_offset;





4282 
4283   int nonstatic_fields_end      = align_up(notaligned_nonstatic_fields_end, heapOopSize);






4284   int instance_end              = align_up(notaligned_nonstatic_fields_end, wordSize);
4285   int static_fields_end         = align_up(next_static_byte_offset, wordSize);
4286 
4287   int static_field_size         = (static_fields_end -
4288                                    InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
4289   nonstatic_field_size          = nonstatic_field_size +
4290                                   (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize;
4291 
4292   int instance_size             = align_object_size(instance_end / wordSize);
4293 
4294   assert(instance_size == align_object_size(align_up(
4295          (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize),
4296           wordSize) / wordSize), "consistent layout helper value");

4297 
4298   // Invariant: nonstatic_field end/start should only change if there are
4299   // nonstatic fields in the class, or if the class is contended. We compare
4300   // against the non-aligned value, so that end alignment will not fail the
4301   // assert without actually having the fields.
4302   assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) ||
4303          is_contended_class ||
4304          (nonstatic_fields_count > 0), "double-check nonstatic start/end");
4305 
4306   // Number of non-static oop map blocks allocated at end of klass.
4307   const unsigned int total_oop_map_count =
4308     compute_oop_map_count(_super_klass, nonstatic_oop_map_count,
4309                           first_nonstatic_oop_offset);
4310 
4311 #ifndef PRODUCT
4312   if (PrintFieldLayout) {

4313     print_field_layout(_class_name,
4314           _fields,
4315           cp,
4316           instance_size,
4317           nonstatic_fields_start,
4318           nonstatic_fields_end,
4319           static_fields_end);







4320   }
4321 
4322 #endif
4323   // Pass back information needed for InstanceKlass creation
4324   info->nonstatic_oop_offsets = nonstatic_oop_offsets;
4325   info->nonstatic_oop_counts = nonstatic_oop_counts;
4326   info->nonstatic_oop_map_count = nonstatic_oop_map_count;
4327   info->total_oop_map_count = total_oop_map_count;
4328   info->instance_size = instance_size;
4329   info->static_field_size = static_field_size;
4330   info->nonstatic_field_size = nonstatic_field_size;
4331   info->has_nonstatic_fields = has_nonstatic_fields;
4332 }
4333 
4334 static void fill_oop_maps(const InstanceKlass* k,
4335                           unsigned int nonstatic_oop_map_count,
4336                           const int* nonstatic_oop_offsets,
4337                           const unsigned int* nonstatic_oop_counts) {
4338 
4339   assert(k != NULL, "invariant");
4340 
4341   OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps();
4342   const InstanceKlass* const super = k->superklass();
4343   const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0;
4344   if (super_count > 0) {
4345     // Copy maps from superklass
4346     OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps();
4347     for (unsigned int i = 0; i < super_count; ++i) {
4348       *this_oop_map++ = *super_oop_map++;
4349     }
4350   }
4351 
4352   if (nonstatic_oop_map_count > 0) {
4353     if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) {
4354       // The counts differ because there is no gap between superklass's last oop
4355       // field and the first local oop field.  Extend the last oop map copied
4356       // from the superklass instead of creating new one.
4357       nonstatic_oop_map_count--;
4358       nonstatic_oop_offsets++;
4359       this_oop_map--;
4360       this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++);
4361       this_oop_map++;
4362     }
4363 
4364     // Add new map blocks, fill them
4365     while (nonstatic_oop_map_count-- > 0) {
4366       this_oop_map->set_offset(*nonstatic_oop_offsets++);
4367       this_oop_map->set_count(*nonstatic_oop_counts++);
4368       this_oop_map++;
4369     }
4370     assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() ==
4371            this_oop_map, "sanity");
4372   }
4373 }
4374 
4375 
4376 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4377   assert(ik != NULL, "invariant");
4378 
4379   const Klass* const super = ik->super();
4380 
4381   // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4382   // in which case we don't have to register objects as finalizable
4383   if (!_has_empty_finalizer) {
4384     if (_has_finalizer ||
4385         (super != NULL && super->has_finalizer())) {
4386       ik->set_has_finalizer();
4387     }
4388   }
4389 
4390 #ifdef ASSERT
4391   bool f = false;
4392   const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4393                                            vmSymbols::void_method_signature());
4394   if (m != NULL && !m->is_empty_method()) {
4395       f = true;
4396   }
4397 
4398   // Spec doesn't prevent agent from redefinition of empty finalizer.
4399   // Despite the fact that it's generally bad idea and redefined finalizer
4400   // will not work as expected we shouldn't abort vm in this case
4401   if (!ik->has_redefined_this_or_super()) {
4402     assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4403   }
4404 #endif
4405 
4406   // Check if this klass supports the java.lang.Cloneable interface
4407   if (SystemDictionary::Cloneable_klass_loaded()) {
4408     if (ik->is_subtype_of(SystemDictionary::Cloneable_klass())) {




4409       ik->set_is_cloneable();
4410     }
4411   }
4412 
4413   // Check if this klass has a vanilla default constructor
4414   if (super == NULL) {
4415     // java.lang.Object has empty default constructor
4416     ik->set_has_vanilla_constructor();
4417   } else {
4418     if (super->has_vanilla_constructor() &&
4419         _has_vanilla_constructor) {
4420       ik->set_has_vanilla_constructor();
4421     }
4422 #ifdef ASSERT
4423     bool v = false;
4424     if (super->has_vanilla_constructor()) {
4425       const Method* const constructor =
4426         ik->find_method(vmSymbols::object_initializer_name(),
4427                        vmSymbols::void_method_signature());
4428       if (constructor != NULL && constructor->is_vanilla_constructor()) {
4429         v = true;
4430       }
4431     }
4432     assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4433 #endif
4434   }
4435 
4436   // If it cannot be fast-path allocated, set a bit in the layout helper.
4437   // See documentation of InstanceKlass::can_be_fastpath_allocated().
4438   assert(ik->size_helper() > 0, "layout_helper is initialized");
4439   if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4440       || ik->is_abstract() || ik->is_interface()
4441       || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4442       || ik->size_helper() >= FastAllocateSizeLimit) {
4443     // Forbid fast-path allocation.
4444     const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4445     ik->set_layout_helper(lh);
4446   }
4447 }
4448 





4449 // utility methods for appending an array with check for duplicates
4450 
4451 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4452                               const Array<InstanceKlass*>* const ifs) {
4453   // iterate over new interfaces
4454   for (int i = 0; i < ifs->length(); i++) {
4455     InstanceKlass* const e = ifs->at(i);
4456     assert(e->is_klass() && e->is_interface(), "just checking");
4457     // add new interface
4458     result->append_if_missing(e);
4459   }
4460 }
4461 
4462 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4463                                                             Array<InstanceKlass*>* local_ifs,
4464                                                             ClassLoaderData* loader_data,
4465                                                             TRAPS) {
4466   assert(local_ifs != NULL, "invariant");
4467   assert(loader_data != NULL, "invariant");
4468 


4691     const Method* const m = methods->at(index);
4692     // if m is static and not the init method, throw a verify error
4693     if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4694       ResourceMark rm(THREAD);
4695       Exceptions::fthrow(
4696         THREAD_AND_LOCATION,
4697         vmSymbols::java_lang_VerifyError(),
4698         "Illegal static method %s in interface %s",
4699         m->name()->as_C_string(),
4700         this_klass->external_name()
4701       );
4702       return;
4703     }
4704   }
4705 }
4706 
4707 // utility methods for format checking
4708 
4709 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4710   const bool is_module = (flags & JVM_ACC_MODULE) != 0;

4711   assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");

4712   if (is_module) {
4713     ResourceMark rm(THREAD);
4714     Exceptions::fthrow(
4715       THREAD_AND_LOCATION,
4716       vmSymbols::java_lang_NoClassDefFoundError(),
4717       "%s is not a class because access_flag ACC_MODULE is set",
4718       _class_name->as_C_string());
4719     return;
4720   }
4721 










4722   if (!_need_verify) { return; }
4723 
4724   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4725   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4726   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4727   const bool is_super      = (flags & JVM_ACC_SUPER)      != 0;
4728   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4729   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4730   const bool major_gte_15  = _major_version >= JAVA_1_5_VERSION;
4731 
4732   if ((is_abstract && is_final) ||
4733       (is_interface && !is_abstract) ||
4734       (is_interface && major_gte_15 && (is_super || is_enum)) ||
4735       (!is_interface && major_gte_15 && is_annotation)) {

4736     ResourceMark rm(THREAD);


4737     Exceptions::fthrow(
4738       THREAD_AND_LOCATION,
4739       vmSymbols::java_lang_ClassFormatError(),
4740       "Illegal class modifiers in class %s: 0x%X",
4741       _class_name->as_C_string(), flags
4742     );
4743     return;
4744   }
4745 }
4746 
4747 static bool has_illegal_visibility(jint flags) {
4748   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4749   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4750   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4751 
4752   return ((is_public && is_protected) ||
4753           (is_public && is_private) ||
4754           (is_protected && is_private));
4755 }
4756 
4757 // A legal major_version.minor_version must be one of the following:
4758 //
4759 //  Major_version >= 45 and major_version < 56, any minor_version.
4760 //  Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4761 //  Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.


4800     if (!Arguments::enable_preview()) {
4801       Exceptions::fthrow(
4802         THREAD_AND_LOCATION,
4803         vmSymbols::java_lang_UnsupportedClassVersionError(),
4804         "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4805         class_name->as_C_string(), major, minor);
4806       return;
4807     }
4808 
4809   } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4810     Exceptions::fthrow(
4811         THREAD_AND_LOCATION,
4812         vmSymbols::java_lang_UnsupportedClassVersionError(),
4813         "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4814         class_name->as_C_string(), major, minor);
4815   }
4816 }
4817 
4818 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4819                                                    bool is_interface,

4820                                                    TRAPS) const {
4821   if (!_need_verify) { return; }
4822 
4823   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4824   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4825   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4826   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4827   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4828   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4829   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4830   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4831   const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4832 
4833   bool is_illegal = false;
4834 
4835   if (is_interface) {
4836     if (!is_public || !is_static || !is_final || is_private ||
4837         is_protected || is_volatile || is_transient ||
4838         (major_gte_15 && is_enum)) {
4839       is_illegal = true;
4840     }
4841   } else { // not interface
4842     if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4843       is_illegal = true;




4844     }
4845   }
4846 
4847   if (is_illegal) {
4848     ResourceMark rm(THREAD);
4849     Exceptions::fthrow(
4850       THREAD_AND_LOCATION,
4851       vmSymbols::java_lang_ClassFormatError(),
4852       "Illegal field modifiers in class %s: 0x%X",
4853       _class_name->as_C_string(), flags);
4854     return;
4855   }
4856 }
4857 
4858 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4859                                                     bool is_interface,

4860                                                     const Symbol* name,
4861                                                     TRAPS) const {
4862   if (!_need_verify) { return; }
4863 
4864   const bool is_public       = (flags & JVM_ACC_PUBLIC)       != 0;
4865   const bool is_private      = (flags & JVM_ACC_PRIVATE)      != 0;
4866   const bool is_static       = (flags & JVM_ACC_STATIC)       != 0;
4867   const bool is_final        = (flags & JVM_ACC_FINAL)        != 0;
4868   const bool is_native       = (flags & JVM_ACC_NATIVE)       != 0;
4869   const bool is_abstract     = (flags & JVM_ACC_ABSTRACT)     != 0;
4870   const bool is_bridge       = (flags & JVM_ACC_BRIDGE)       != 0;
4871   const bool is_strict       = (flags & JVM_ACC_STRICT)       != 0;
4872   const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4873   const bool is_protected    = (flags & JVM_ACC_PROTECTED)    != 0;
4874   const bool major_gte_15    = _major_version >= JAVA_1_5_VERSION;
4875   const bool major_gte_8     = _major_version >= JAVA_8_VERSION;
4876   const bool is_initializer  = (name == vmSymbols::object_initializer_name());
4877 
4878   bool is_illegal = false;
4879 


4880   if (is_interface) {
4881     if (major_gte_8) {
4882       // Class file version is JAVA_8_VERSION or later Methods of
4883       // interfaces may set any of the flags except ACC_PROTECTED,
4884       // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4885       // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4886       if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4887           (is_native || is_protected || is_final || is_synchronized) ||
4888           // If a specific method of a class or interface has its
4889           // ACC_ABSTRACT flag set, it must not have any of its
4890           // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4891           // ACC_STRICT, or ACC_SYNCHRONIZED flags set.  No need to
4892           // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4893           // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4894           (is_abstract && (is_private || is_static || is_strict))) {
4895         is_illegal = true;
4896       }
4897     } else if (major_gte_15) {
4898       // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4899       if (!is_public || is_private || is_protected || is_static || is_final ||
4900           is_synchronized || is_native || !is_abstract || is_strict) {
4901         is_illegal = true;
4902       }
4903     } else {
4904       // Class file version is pre-JAVA_1_5_VERSION
4905       if (!is_public || is_static || is_final || is_native || !is_abstract) {
4906         is_illegal = true;
4907       }
4908     }
4909   } else { // not interface
4910     if (has_illegal_visibility(flags)) {
4911       is_illegal = true;
4912     } else {
4913       if (is_initializer) {
4914         if (is_static || is_final || is_synchronized || is_native ||
4915             is_abstract || (major_gte_15 && is_bridge)) {
4916           is_illegal = true;
4917         }









4918       } else { // not initializer
4919         if (is_abstract) {
4920           if ((is_final || is_native || is_private || is_static ||
4921               (major_gte_15 && (is_synchronized || is_strict)))) {
4922             is_illegal = true;





4923           }
4924         }
4925       }
4926     }
4927   }
4928 
4929   if (is_illegal) {
4930     ResourceMark rm(THREAD);
4931     Exceptions::fthrow(
4932       THREAD_AND_LOCATION,
4933       vmSymbols::java_lang_ClassFormatError(),
4934       "Method %s in class %s has illegal modifiers: 0x%X",
4935       name->as_C_string(), _class_name->as_C_string(), flags);
4936     return;
4937   }
4938 }
4939 
4940 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4941                                         int length,
4942                                         TRAPS) const {
4943   assert(_need_verify, "only called when _need_verify is true");
4944   if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4945     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4946   }
4947 }
4948 
4949 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4950 // In class names, '/' separates unqualified names.  This is verified in this function also.
4951 // Method names also may not contain the characters '<' or '>', unless <init>
4952 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
4953 // method.  Because these names have been checked as special cases before
4954 // calling this method in verify_legal_method_name.
4955 //


5072 // be taken as a field signature. Allow "void" if void_ok.
5073 // Return a pointer to just past the signature.
5074 // Return NULL if no legal signature is found.
5075 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5076                                                        bool void_ok,
5077                                                        unsigned int length,
5078                                                        TRAPS) const {
5079   unsigned int array_dim = 0;
5080   while (length > 0) {
5081     switch (signature[0]) {
5082     case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
5083     case JVM_SIGNATURE_BOOLEAN:
5084     case JVM_SIGNATURE_BYTE:
5085     case JVM_SIGNATURE_CHAR:
5086     case JVM_SIGNATURE_SHORT:
5087     case JVM_SIGNATURE_INT:
5088     case JVM_SIGNATURE_FLOAT:
5089     case JVM_SIGNATURE_LONG:
5090     case JVM_SIGNATURE_DOUBLE:
5091       return signature + 1;
5092     case JVM_SIGNATURE_CLASS: {









5093       if (_major_version < JAVA_1_5_VERSION) {
5094         // Skip over the class name if one is there
5095         const char* const p = skip_over_field_name(signature + 1, true, --length);
5096 
5097         // The next character better be a semicolon
5098         if (p && (p - signature) > 1 && p[0] == ';') {
5099           return p + 1;
5100         }
5101       }
5102       else {
5103         // Skip leading 'L' and ignore first appearance of ';'
5104         signature++;
5105         const char* c = (const char*) memchr(signature, ';', length - 1);
5106         // Format check signature
5107         if (c != NULL) {
5108           int newlen = c - (char*) signature;
5109           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5110           if (!legal) {
5111             classfile_parse_error("Class name is empty or contains illegal character "
5112                                   "in descriptor in class file %s",
5113                                   CHECK_0);
5114             return NULL;
5115           }
5116           return signature + newlen + 1;
5117         }
5118       }
5119       return NULL;
5120     }
5121     case JVM_SIGNATURE_ARRAY:
5122       array_dim++;
5123       if (array_dim > 255) {


5138 
5139 // Checks if name is a legal class name.
5140 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5141   if (!_need_verify || _relax_verify) { return; }
5142 
5143   assert(name->refcount() > 0, "symbol must be kept alive");
5144   char* bytes = (char*)name->bytes();
5145   unsigned int length = name->utf8_length();
5146   bool legal = false;
5147 
5148   if (length > 0) {
5149     const char* p;
5150     if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5151       p = skip_over_field_signature(bytes, false, length, CHECK);
5152       legal = (p != NULL) && ((p - bytes) == (int)length);
5153     } else if (_major_version < JAVA_1_5_VERSION) {
5154       if (bytes[0] != '<') {
5155         p = skip_over_field_name(bytes, true, length);
5156         legal = (p != NULL) && ((p - bytes) == (int)length);
5157       }



5158     } else {
5159       // 4900761: relax the constraints based on JSR202 spec
5160       // Class names may be drawn from the entire Unicode character set.
5161       // Identifiers between '/' must be unqualified names.
5162       // The utf8 string has been verified when parsing cpool entries.
5163       legal = verify_unqualified_name(bytes, length, LegalClass);
5164     }
5165   }
5166   if (!legal) {
5167     ResourceMark rm(THREAD);
5168     assert(_class_name != NULL, "invariant");
5169     Exceptions::fthrow(
5170       THREAD_AND_LOCATION,
5171       vmSymbols::java_lang_ClassFormatError(),
5172       "Illegal class name \"%.*s\" in class file %s", length, bytes,
5173       _class_name->as_C_string()
5174     );
5175     return;
5176   }
5177 }


5287   const char* nextp;
5288 
5289   // The first character must be a '('
5290   if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) {
5291     length--;
5292     // Skip over legal field signatures
5293     nextp = skip_over_field_signature(p, false, length, CHECK_0);
5294     while ((length > 0) && (nextp != NULL)) {
5295       args_size++;
5296       if (p[0] == 'J' || p[0] == 'D') {
5297         args_size++;
5298       }
5299       length -= nextp - p;
5300       p = nextp;
5301       nextp = skip_over_field_signature(p, false, length, CHECK_0);
5302     }
5303     // The first non-signature thing better be a ')'
5304     if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
5305       length--;
5306       if (name->utf8_length() > 0 && name->char_at(0) == '<') {
5307         // All internal methods must return void
5308         if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {
5309           return args_size;
5310         }
















5311       } else {
5312         // Now we better just have a return value
5313         nextp = skip_over_field_signature(p, true, length, CHECK_0);
5314         if (nextp && ((int)length == (nextp - p))) {
5315           return args_size;
5316         }
5317       }
5318     }
5319   }
5320   // Report error
5321   throwIllegalSignature("Method", name, signature, CHECK_0);
5322   return 0;
5323 }
5324 
5325 int ClassFileParser::static_field_size() const {
5326   assert(_field_info != NULL, "invariant");
5327   return _field_info->static_field_size;
5328 }
5329 
5330 int ClassFileParser::total_oop_map_count() const {
5331   assert(_field_info != NULL, "invariant");
5332   return _field_info->total_oop_map_count;
5333 }
5334 
5335 jint ClassFileParser::layout_size() const {
5336   assert(_field_info != NULL, "invariant");
5337   return _field_info->instance_size;
5338 }
5339 
5340 static void check_methods_for_intrinsics(const InstanceKlass* ik,
5341                                          const Array<Method*>* methods) {
5342   assert(ik != NULL, "invariant");
5343   assert(methods != NULL, "invariant");
5344 
5345   // Set up Method*::intrinsic_id as soon as we know the names of methods.
5346   // (We used to do this lazily, but now we query it in Rewriter,
5347   // which is eagerly done for every method, so we might as well do it now,
5348   // when everything is fresh in memory.)
5349   const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik);
5350 
5351   if (klass_id != vmSymbols::NO_SID) {
5352     for (int j = 0; j < methods->length(); ++j) {


5442 
5443 
5444   if (ik->should_store_fingerprint()) {
5445     ik->store_fingerprint(_stream->compute_fingerprint());
5446   }
5447 
5448   ik->set_has_passed_fingerprint_check(false);
5449   if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5450     uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5451     uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5452     if (aot_fp != 0 && aot_fp == fp) {
5453       // This class matches with a class saved in an AOT library
5454       ik->set_has_passed_fingerprint_check(true);
5455     } else {
5456       ResourceMark rm;
5457       log_info(class, fingerprint)("%s :  expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5458                                  ik->external_name(), aot_fp, _stream->compute_fingerprint());
5459     }
5460   }
5461 






5462   return ik;
5463 }
5464 
5465 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) {
5466   assert(ik != NULL, "invariant");
5467 
5468   // Set name and CLD before adding to CLD
5469   ik->set_class_loader_data(_loader_data);
5470   ik->set_name(_class_name);
5471 
5472   // Add all classes to our internal class loader list here,
5473   // including classes in the bootstrap (NULL) class loader.
5474   const bool publicize = !is_internal();
5475 
5476   _loader_data->add_class(ik, publicize);
5477 
5478   set_klass_to_deallocate(ik);
5479 
5480   assert(_field_info != NULL, "invariant");
5481   assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5482   assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count,
5483     "sanity");
5484 
5485   assert(ik->is_instance_klass(), "sanity");
5486   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5487 
5488   // Fill in information already parsed
5489   ik->set_should_verify_class(_need_verify);
5490 
5491   // Not yet: supers are done below to support the new subtype-checking fields
5492   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5493   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);



5494   assert(_fac != NULL, "invariant");
5495   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5496 
5497   // this transfers ownership of a lot of arrays from
5498   // the parser onto the InstanceKlass*
5499   apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5500 
5501   // note that is not safe to use the fields in the parser from this point on
5502   assert(NULL == _cp, "invariant");
5503   assert(NULL == _fields, "invariant");
5504   assert(NULL == _methods, "invariant");
5505   assert(NULL == _inner_classes, "invariant");
5506   assert(NULL == _nest_members, "invariant");
5507   assert(NULL == _local_interfaces, "invariant");
5508   assert(NULL == _combined_annotations, "invariant");
5509 
5510   if (_has_final_method) {
5511     ik->set_has_final_method();
5512   }
5513 
5514   ik->copy_method_ordering(_method_ordering, CHECK);
5515   // The InstanceKlass::_methods_jmethod_ids cache


5563 
5564   // Miranda methods
5565   if ((_num_miranda_methods > 0) ||
5566       // if this class introduced new miranda methods or
5567       (_super_klass != NULL && _super_klass->has_miranda_methods())
5568         // super class exists and this class inherited miranda methods
5569      ) {
5570        ik->set_has_miranda_methods(); // then set a flag
5571   }
5572 
5573   // Fill in information needed to compute superclasses.
5574   ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5575   ik->set_transitive_interfaces(_transitive_interfaces);
5576   _transitive_interfaces = NULL;
5577 
5578   // Initialize itable offset tables
5579   klassItable::setup_itable_offset_table(ik);
5580 
5581   // Compute transitive closure of interfaces this class implements
5582   // Do final class setup
5583   fill_oop_maps(ik,
5584                 _field_info->nonstatic_oop_map_count,
5585                 _field_info->nonstatic_oop_offsets,
5586                 _field_info->nonstatic_oop_counts);
5587 
5588   // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5589   set_precomputed_flags(ik);
5590 
5591   // check if this class can access its super class
5592   check_super_class_access(ik, CHECK);
5593 
5594   // check if this class can access its superinterfaces
5595   check_super_interface_access(ik, CHECK);
5596 
5597   // check if this class overrides any final method
5598   check_final_method_override(ik, CHECK);
5599 
5600   // reject static interface methods prior to Java 8
5601   if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5602     check_illegal_static_method(ik, CHECK);
5603   }
5604 
5605   // Obtain this_klass' module entry
5606   ModuleEntry* module_entry = ik->module();
5607   assert(module_entry != NULL, "module_entry should always be set");
5608 
5609   // Obtain java.lang.Module


5619 
5620   assert(_all_mirandas != NULL, "invariant");
5621 
5622   // Generate any default methods - default methods are public interface methods
5623   // that have a default implementation.  This is new with Java 8.
5624   if (_has_nonstatic_concrete_methods) {
5625     DefaultMethods::generate_default_methods(ik,
5626                                              _all_mirandas,
5627                                              CHECK);
5628   }
5629 
5630   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5631   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5632       !module_entry->has_default_read_edges()) {
5633     if (!module_entry->set_has_default_read_edges()) {
5634       // We won a potential race
5635       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5636     }
5637   }
5638 


































5639   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5640 
5641   if (!is_internal()) {
5642     if (log_is_enabled(Info, class, load)) {
5643       ResourceMark rm;
5644       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
5645       ik->print_class_load_logging(_loader_data, module_name, _stream);
5646     }
5647 
5648     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5649         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5650         log_is_enabled(Info, class, preview)) {
5651       ResourceMark rm;
5652       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5653                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5654     }
5655 
5656     if (log_is_enabled(Debug, class, resolve))  {
5657       ResourceMark rm;
5658       // print out the superclass.


5808   _protection_domain(protection_domain),
5809   _access_flags(),
5810   _pub_level(pub_level),
5811   _bad_constant_seen(0),
5812   _synthetic_flag(false),
5813   _sde_length(false),
5814   _sde_buffer(NULL),
5815   _sourcefile_index(0),
5816   _generic_signature_index(0),
5817   _major_version(0),
5818   _minor_version(0),
5819   _this_class_index(0),
5820   _super_class_index(0),
5821   _itfs_len(0),
5822   _java_fields_count(0),
5823   _need_verify(false),
5824   _relax_verify(false),
5825   _has_nonstatic_concrete_methods(false),
5826   _declares_nonstatic_concrete_methods(false),
5827   _has_final_method(false),


5828   _has_finalizer(false),
5829   _has_empty_finalizer(false),
5830   _has_vanilla_constructor(false),
5831   _max_bootstrap_specifier_index(-1) {
5832 
5833   _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
5834   _class_name->increment_refcount();
5835 
5836   assert(THREAD->is_Java_thread(), "invariant");
5837   assert(_loader_data != NULL, "invariant");
5838   assert(stream != NULL, "invariant");
5839   assert(_stream != NULL, "invariant");
5840   assert(_stream->buffer() == _stream->current(), "invariant");
5841   assert(_class_name != NULL, "invariant");
5842   assert(0 == _access_flags.as_int(), "invariant");
5843 
5844   // Figure out whether we can skip format checking (matching classic VM behavior)
5845   if (DumpSharedSpaces) {
5846     // verify == true means it's a 'remote' class (i.e., non-boot class)
5847     // Verification decision is based on BytecodeVerificationRemote flag


6003 
6004   _orig_cp_size = cp_size;
6005   if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
6006     THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
6007   }
6008   cp_size += _max_num_patched_klasses;
6009 
6010   _cp = ConstantPool::allocate(_loader_data,
6011                                cp_size,
6012                                CHECK);
6013 
6014   ConstantPool* const cp = _cp;
6015 
6016   parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6017 
6018   assert(cp_size == (const u2)cp->length(), "invariant");
6019 
6020   // ACCESS FLAGS
6021   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
6022 
6023   // Access flags
6024   jint flags;
6025   // JVM_ACC_MODULE is defined in JDK-9 and later.
6026   if (_major_version >= JAVA_9_VERSION) {
6027     flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
6028   } else {
6029     flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;


6030   }
6031 



6032   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6033     // Set abstract bit for old class files for backward compatibility
6034     flags |= JVM_ACC_ABSTRACT;
6035   }
6036 
6037   verify_legal_class_modifiers(flags, CHECK);
6038 
6039   short bad_constant = class_bad_constant_seen();
6040   if (bad_constant != 0) {
6041     // Do not throw CFE until after the access_flags are checked because if
6042     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6043     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6044   }
6045 
6046   _access_flags.set_flags(flags);
6047 
6048   // This class and superclass
6049   _this_class_index = stream->get_u2_fast();
6050   check_property(
6051     valid_cp_range(_this_class_index, cp_size) &&


6146   _super_class_index = stream->get_u2_fast();
6147   _super_klass = parse_super_class(cp,
6148                                    _super_class_index,
6149                                    _need_verify,
6150                                    CHECK);
6151 
6152   // Interfaces
6153   _itfs_len = stream->get_u2_fast();
6154   parse_interfaces(stream,
6155                    _itfs_len,
6156                    cp,
6157                    &_has_nonstatic_concrete_methods,
6158                    CHECK);
6159 
6160   assert(_local_interfaces != NULL, "invariant");
6161 
6162   // Fields (offsets are filled in later)
6163   _fac = new FieldAllocationCount();
6164   parse_fields(stream,
6165                _access_flags.is_interface(),

6166                _fac,
6167                cp,
6168                cp_size,
6169                &_java_fields_count,
6170                CHECK);
6171 
6172   assert(_fields != NULL, "invariant");
6173 
6174   // Methods
6175   AccessFlags promoted_flags;
6176   parse_methods(stream,
6177                 _access_flags.is_interface(),

6178                 &promoted_flags,
6179                 &_has_final_method,
6180                 &_declares_nonstatic_concrete_methods,
6181                 CHECK);
6182 
6183   assert(_methods != NULL, "invariant");
6184 
6185   // promote flags from parse_methods() to the klass' flags
6186   _access_flags.add_promoted_flags(promoted_flags.as_int());
6187 
6188   if (_declares_nonstatic_concrete_methods) {
6189     _has_nonstatic_concrete_methods = true;
6190   }
6191 
6192   // Additional attributes/annotations
6193   _parsed_annotations = new ClassAnnotationCollector();
6194   parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6195 
6196   assert(_inner_classes != NULL, "invariant");
6197 


6239                                                                true,
6240                                                                CHECK);
6241   }
6242 
6243   if (_super_klass != NULL) {
6244     if (_super_klass->has_nonstatic_concrete_methods()) {
6245       _has_nonstatic_concrete_methods = true;
6246     }
6247 
6248     if (_super_klass->is_interface()) {
6249       ResourceMark rm(THREAD);
6250       Exceptions::fthrow(
6251         THREAD_AND_LOCATION,
6252         vmSymbols::java_lang_IncompatibleClassChangeError(),
6253         "class %s has interface %s as super class",
6254         _class_name->as_klass_external_name(),
6255         _super_klass->external_name()
6256       );
6257       return;
6258     }








6259     // Make sure super class is not final
6260     if (_super_klass->is_final()) {
6261       THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class");
6262     }
6263   }
6264 
6265   // Compute the transitive list of all unique interfaces implemented by this class
6266   _transitive_interfaces =
6267     compute_transitive_interfaces(_super_klass,
6268                                   _local_interfaces,
6269                                   _loader_data,
6270                                   CHECK);
6271 
6272   assert(_transitive_interfaces != NULL, "invariant");
6273 
6274   // sort methods
6275   _method_ordering = sort_methods(_methods);
6276 
6277   _all_mirandas = new GrowableArray<Method*>(20);
6278 
6279   Handle loader(THREAD, _loader_data->class_loader());
6280   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6281                                                     &_num_miranda_methods,
6282                                                     _all_mirandas,
6283                                                     _super_klass,
6284                                                     _methods,
6285                                                     _access_flags,
6286                                                     _major_version,
6287                                                     loader,
6288                                                     _class_name,
6289                                                     _local_interfaces,
6290                                                     CHECK);
6291 
6292   // Size of Java itable (in words)
6293   _itable_size = _access_flags.is_interface() ? 0 :
6294     klassItable::compute_itable_size(_transitive_interfaces);
6295 
6296   assert(_fac != NULL, "invariant");
6297   assert(_parsed_annotations != NULL, "invariant");
6298 













6299   _field_info = new FieldLayoutInfo();
6300   layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);












6301 
6302   // Compute reference typ
6303   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6304 
6305 }
6306 
6307 void ClassFileParser::set_klass(InstanceKlass* klass) {
6308 
6309 #ifdef ASSERT
6310   if (klass != NULL) {
6311     assert(NULL == _klass, "leaking?");
6312   }
6313 #endif
6314 
6315   _klass = klass;
6316 }
6317 
6318 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6319 
6320 #ifdef ASSERT
6321   if (klass != NULL) {
6322     assert(NULL == _klass_to_deallocate, "leaking?");
6323   }
6324 #endif
6325 
6326   _klass_to_deallocate = klass;
6327 }
6328 
6329 // Caller responsible for ResourceMark
6330 // clone stream with rewound position
6331 const ClassFileStream* ClassFileParser::clone_stream() const {
6332   assert(_stream != NULL, "invariant");
6333 
6334   return _stream->clone();
6335 }

6336 // ----------------------------------------------------------------------------
6337 // debugging
6338 
6339 #ifdef ASSERT
6340 
6341 // return true if class_name contains no '.' (internal format is '/')
6342 bool ClassFileParser::is_internal_format(Symbol* class_name) {
6343   if (class_name != NULL) {
6344     ResourceMark rm;
6345     char* name = class_name->as_C_string();
6346     return strchr(name, '.') == NULL;
6347   } else {
6348     return true;
6349   }
6350 }
6351 
6352 #endif


   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 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/classFileParser.hpp"
  29 #include "classfile/classFileStream.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/defaultMethods.hpp"
  33 #include "classfile/dictionary.hpp"
  34 #include <classfile/fieldLayoutBuilder.hpp>
  35 #include "classfile/javaClasses.inline.hpp"
  36 #include "classfile/moduleEntry.hpp"
  37 #include "classfile/packageEntry.hpp"
  38 #include "classfile/symbolTable.hpp"
  39 #include "classfile/systemDictionary.hpp"
  40 #include "classfile/verificationType.hpp"
  41 #include "classfile/verifier.hpp"
  42 #include "classfile/vmSymbols.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/allocation.hpp"
  46 #include "memory/metadataFactory.hpp"
  47 #include "memory/oopFactory.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "memory/universe.hpp"
  50 #include "oops/annotations.hpp"
  51 #include "oops/constantPool.inline.hpp"
  52 #include "oops/fieldStreams.hpp"
  53 #include "oops/instanceKlass.hpp"
  54 #include "oops/instanceMirrorKlass.hpp"
  55 #include "oops/klass.inline.hpp"
  56 #include "oops/klassVtable.hpp"
  57 #include "oops/metadata.hpp"
  58 #include "oops/method.inline.hpp"
  59 #include "oops/oop.inline.hpp"
  60 #include "oops/symbol.hpp"
  61 #include "oops/valueKlass.hpp"
  62 #include "prims/jvmtiExport.hpp"
  63 #include "prims/jvmtiThreadState.hpp"
  64 #include "runtime/arguments.hpp"
  65 #include "runtime/fieldDescriptor.inline.hpp"
  66 #include "runtime/handles.inline.hpp"
  67 #include "runtime/javaCalls.hpp"
  68 #include "runtime/os.hpp"
  69 #include "runtime/perfData.hpp"
  70 #include "runtime/reflection.hpp"
  71 #include "runtime/safepointVerifiers.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/timer.hpp"
  74 #include "services/classLoadingService.hpp"
  75 #include "services/threadService.hpp"
  76 #include "utilities/align.hpp"
  77 #include "utilities/bitMap.inline.hpp"
  78 #include "utilities/copy.hpp"
  79 #include "utilities/exceptions.hpp"
  80 #include "utilities/globalDefinitions.hpp"
  81 #include "utilities/growableArray.hpp"
  82 #include "utilities/macros.hpp"
  83 #include "utilities/ostream.hpp"
  84 #include "utilities/resourceHash.hpp"
  85 #include "utilities/utf8.hpp"


 114 
 115 // Used for backward compatibility reasons:
 116 // - to disallow argument and require ACC_STATIC for <clinit> methods
 117 #define JAVA_7_VERSION                    51
 118 
 119 // Extension method support.
 120 #define JAVA_8_VERSION                    52
 121 
 122 #define JAVA_9_VERSION                    53
 123 
 124 #define JAVA_10_VERSION                   54
 125 
 126 #define JAVA_11_VERSION                   55
 127 
 128 #define JAVA_12_VERSION                   56
 129 
 130 #define JAVA_13_VERSION                   57
 131 
 132 #define JAVA_14_VERSION                   58
 133 
 134 #define CONSTANT_CLASS_DESCRIPTORS        58
 135 
 136 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
 137   assert((bad_constant == JVM_CONSTANT_Module ||
 138           bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
 139          "Unexpected bad constant pool entry");
 140   if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
 141 }
 142 
 143 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
 144                                                   ConstantPool* cp,
 145                                                   const int length,
 146                                                   TRAPS) {
 147   assert(stream != NULL, "invariant");
 148   assert(cp != NULL, "invariant");
 149 
 150   // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
 151   // this function (_current can be allocated in a register, with scalar
 152   // replacement of aggregates). The _current pointer is copied back to
 153   // stream() when this function returns. DON'T call another method within
 154   // this method that uses stream().
 155   const ClassFileStream cfs1 = *stream;
 156   const ClassFileStream* const cfs = &cfs1;
 157 
 158   assert(cfs->allocated_on_stack(), "should be local");
 159   debug_only(const u1* const old_current = stream->current();)
 160 
 161   // Used for batching symbol allocations.
 162   const char* names[SymbolTable::symbol_alloc_batch_size];
 163   int lengths[SymbolTable::symbol_alloc_batch_size];
 164   int indices[SymbolTable::symbol_alloc_batch_size];
 165   unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
 166   int names_count = 0;
 167 
 168   // parsing  Index 0 is unused
 169   for (int index = 1; index < length; index++) {
 170     // Each of the following case guarantees one more byte in the stream
 171     // for the following tag or the access_flags following constant pool,
 172     // so we don't need bounds-check for reading tag.
 173     const u1 tag = cfs->get_u1_fast();
 174     switch (tag) {
 175       case JVM_CONSTANT_Class: {
 176         cfs->guarantee_more(3, CHECK);  // name_index, tag/access_flags
 177         const u2 name_index = cfs->get_u2_fast();
 178         cp->klass_index_at_put(index, name_index);
 179         break;
 180       }
 181       case JVM_CONSTANT_Fieldref: {
 182         cfs->guarantee_more(5, CHECK);  // class_index, name_and_type_index, tag/access_flags
 183         const u2 class_index = cfs->get_u2_fast();
 184         const u2 name_and_type_index = cfs->get_u2_fast();
 185         cp->field_at_put(index, class_index, name_and_type_index);
 186         break;
 187       }
 188       case JVM_CONSTANT_Methodref: {
 189         cfs->guarantee_more(5, CHECK);  // class_index, name_and_type_index, tag/access_flags
 190         const u2 class_index = cfs->get_u2_fast();
 191         const u2 name_and_type_index = cfs->get_u2_fast();
 192         cp->method_at_put(index, class_index, name_and_type_index);
 193         break;
 194       }
 195       case JVM_CONSTANT_InterfaceMethodref: {


 485         check_property(valid_symbol_at(name_ref_index),
 486           "Invalid constant pool index %u in class file %s",
 487           name_ref_index, CHECK);
 488         check_property(valid_symbol_at(signature_ref_index),
 489           "Invalid constant pool index %u in class file %s",
 490           signature_ref_index, CHECK);
 491         break;
 492       }
 493       case JVM_CONSTANT_Utf8:
 494         break;
 495       case JVM_CONSTANT_UnresolvedClass:         // fall-through
 496       case JVM_CONSTANT_UnresolvedClassInError: {
 497         ShouldNotReachHere();     // Only JVM_CONSTANT_ClassIndex should be present
 498         break;
 499       }
 500       case JVM_CONSTANT_ClassIndex: {
 501         const int class_index = cp->klass_index_at(index);
 502         check_property(valid_symbol_at(class_index),
 503           "Invalid constant pool index %u in class file %s",
 504           class_index, CHECK);
 505 
 506         Symbol* const name = cp->symbol_at(class_index);
 507         const unsigned int name_len = name->utf8_length();
 508         if (name->is_Q_signature()) {
 509           cp->unresolved_qdescriptor_at_put(index, class_index, num_klasses++);
 510         } else {
 511           cp->unresolved_klass_at_put(index, class_index, num_klasses++);
 512         }
 513         break;
 514       }
 515       case JVM_CONSTANT_StringIndex: {
 516         const int string_index = cp->string_index_at(index);
 517         check_property(valid_symbol_at(string_index),
 518           "Invalid constant pool index %u in class file %s",
 519           string_index, CHECK);
 520         Symbol* const sym = cp->symbol_at(string_index);
 521         cp->unresolved_string_at_put(index, sym);
 522         break;
 523       }
 524       case JVM_CONSTANT_MethodHandle: {
 525         const int ref_index = cp->method_handle_index_at(index);
 526         check_property(valid_cp_range(ref_index, length),
 527           "Invalid constant pool index %u in class file %s",
 528           ref_index, CHECK);
 529         const constantTag tag = cp->tag_at(ref_index);
 530         const int ref_kind = cp->method_handle_ref_kind_at(index);
 531 
 532         switch (ref_kind) {


 749             classfile_parse_error(
 750               "Bad method name at constant pool index %u in class file %s",
 751               name_ref_index, CHECK);
 752           }
 753         }
 754         break;
 755       }
 756       case JVM_CONSTANT_MethodHandle: {
 757         const int ref_index = cp->method_handle_index_at(index);
 758         const int ref_kind = cp->method_handle_ref_kind_at(index);
 759         switch (ref_kind) {
 760           case JVM_REF_invokeVirtual:
 761           case JVM_REF_invokeStatic:
 762           case JVM_REF_invokeSpecial:
 763           case JVM_REF_newInvokeSpecial: {
 764             const int name_and_type_ref_index =
 765               cp->name_and_type_ref_index_at(ref_index);
 766             const int name_ref_index =
 767               cp->name_ref_index_at(name_and_type_ref_index);
 768             const Symbol* const name = cp->symbol_at(name_ref_index);
 769             if (name != vmSymbols::object_initializer_name()) {
 770               if (ref_kind == JVM_REF_newInvokeSpecial) {
 771                 classfile_parse_error(
 772                   "Bad constructor name at constant pool index %u in class file %s",
 773                     name_ref_index, CHECK);
 774               }
 775             } else {
 776               // The allowed invocation mode of <init> depends on its signature.
 777               // This test corresponds to verify_invoke_instructions in the verifier.
 778               const int signature_ref_index =
 779                 cp->signature_ref_index_at(name_and_type_ref_index);
 780               const Symbol* const signature = cp->symbol_at(signature_ref_index);
 781               if (signature->is_void_method_signature()
 782                   && ref_kind == JVM_REF_newInvokeSpecial) {
 783                 // OK, could be a constructor call
 784               } else if (!signature->is_void_method_signature()
 785                          && ref_kind == JVM_REF_invokeStatic) {
 786                 // also OK, could be a static factory call
 787               } else {
 788                 classfile_parse_error(
 789                   "Bad method name at constant pool index %u in class file %s",
 790                   name_ref_index, CHECK);
 791               }
 792             }
 793             break;
 794           }
 795           // Other ref_kinds are already fully checked in previous pass.
 796         } // switch(ref_kind)
 797         break;
 798       }
 799       case JVM_CONSTANT_MethodType: {
 800         const Symbol* const no_name = vmSymbols::type_name(); // place holder
 801         const Symbol* const signature = cp->method_type_signature_at(index);
 802         verify_legal_method_signature(no_name, signature, CHECK);
 803         break;
 804       }
 805       case JVM_CONSTANT_Utf8: {
 806         assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
 807       }


1070       guarantee_property(value_type.is_int(),
1071                          "Inconsistent constant value type in class file %s",
1072                          CHECK);
1073       break;
1074     }
1075     case T_OBJECT: {
1076       guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;")
1077                          && value_type.is_string()),
1078                          "Bad string initial value in class file %s",
1079                          CHECK);
1080       break;
1081     }
1082     default: {
1083       classfile_parse_error("Unable to set initial value %u in class file %s",
1084                              constantvalue_index,
1085                              CHECK);
1086     }
1087   }
1088 }
1089 




















































1090 // This class also doubles as a holder for metadata cleanup.
1091 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector {
1092 private:
1093   ClassLoaderData* _loader_data;
1094   AnnotationArray* _field_annotations;
1095   AnnotationArray* _field_type_annotations;
1096 public:
1097   FieldAnnotationCollector(ClassLoaderData* loader_data) :
1098     AnnotationCollector(_in_field),
1099     _loader_data(loader_data),
1100     _field_annotations(NULL),
1101     _field_type_annotations(NULL) {}
1102   ~FieldAnnotationCollector();
1103   void apply_to(FieldInfo* f);
1104   AnnotationArray* field_annotations()      { return _field_annotations; }
1105   AnnotationArray* field_type_annotations() { return _field_type_annotations; }
1106 
1107   void set_field_annotations(AnnotationArray* a)      { _field_annotations = a; }
1108   void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; }
1109 };
1110 
1111 class MethodAnnotationCollector : public AnnotationCollector{
1112 public:
1113   MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
1114   void apply_to(const methodHandle& m);
1115 };
1116 






1117 
1118 static int skip_annotation_value(const u1*, int, int); // fwd decl
1119 
1120 // Safely increment index by val if does not pass limit
1121 #define SAFE_ADD(index, limit, val) \
1122 if (index >= limit - val) return limit; \
1123 index += val;
1124 
1125 // Skip an annotation.  Return >=limit if there is any problem.
1126 static int skip_annotation(const u1* buffer, int limit, int index) {
1127   assert(buffer != NULL, "invariant");
1128   // annotation := atype:u2 do(nmem:u2) {member:u2 value}
1129   // value := switch (tag:u1) { ... }
1130   SAFE_ADD(index, limit, 4); // skip atype and read nmem
1131   int nmem = Bytes::get_Java_u2((address)buffer + index - 2);
1132   while (--nmem >= 0 && index < limit) {
1133     SAFE_ADD(index, limit, 2); // skip member
1134     index = skip_annotation_value(buffer, limit, index);
1135   }
1136   return index;


1414                                             CHECK);
1415   parsed_annotations->set_field_annotations(a);
1416   a = assemble_annotations(runtime_visible_type_annotations,
1417                            runtime_visible_type_annotations_length,
1418                            runtime_invisible_type_annotations,
1419                            runtime_invisible_type_annotations_length,
1420                            CHECK);
1421   parsed_annotations->set_field_type_annotations(a);
1422   return;
1423 }
1424 
1425 
1426 // Field allocation types. Used for computing field offsets.
1427 
1428 enum FieldAllocationType {
1429   STATIC_OOP,           // Oops
1430   STATIC_BYTE,          // Boolean, Byte, char
1431   STATIC_SHORT,         // shorts
1432   STATIC_WORD,          // ints
1433   STATIC_DOUBLE,        // aligned long or double
1434   STATIC_FLATTENABLE,   // flattenable field
1435   NONSTATIC_OOP,
1436   NONSTATIC_BYTE,
1437   NONSTATIC_SHORT,
1438   NONSTATIC_WORD,
1439   NONSTATIC_DOUBLE,
1440   NONSTATIC_FLATTENABLE,
1441   MAX_FIELD_ALLOCATION_TYPE,
1442   BAD_ALLOCATION_TYPE = -1
1443 };
1444 
1445 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1446   BAD_ALLOCATION_TYPE, // 0
1447   BAD_ALLOCATION_TYPE, // 1
1448   BAD_ALLOCATION_TYPE, // 2
1449   BAD_ALLOCATION_TYPE, // 3
1450   NONSTATIC_BYTE ,     // T_BOOLEAN     =  4,
1451   NONSTATIC_SHORT,     // T_CHAR        =  5,
1452   NONSTATIC_WORD,      // T_FLOAT       =  6,
1453   NONSTATIC_DOUBLE,    // T_DOUBLE      =  7,
1454   NONSTATIC_BYTE,      // T_BYTE        =  8,
1455   NONSTATIC_SHORT,     // T_SHORT       =  9,
1456   NONSTATIC_WORD,      // T_INT         = 10,
1457   NONSTATIC_DOUBLE,    // T_LONG        = 11,
1458   NONSTATIC_OOP,       // T_OBJECT      = 12,
1459   NONSTATIC_OOP,       // T_ARRAY       = 13,
1460   NONSTATIC_OOP,       // T_VALUETYPE   = 14,
1461   BAD_ALLOCATION_TYPE, // T_VOID        = 15,
1462   BAD_ALLOCATION_TYPE, // T_ADDRESS     = 16,
1463   BAD_ALLOCATION_TYPE, // T_NARROWOOP   = 17,
1464   BAD_ALLOCATION_TYPE, // T_METADATA    = 18,
1465   BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1466   BAD_ALLOCATION_TYPE, // T_CONFLICT    = 20,
1467   BAD_ALLOCATION_TYPE, // 0
1468   BAD_ALLOCATION_TYPE, // 1
1469   BAD_ALLOCATION_TYPE, // 2
1470   BAD_ALLOCATION_TYPE, // 3
1471   STATIC_BYTE ,        // T_BOOLEAN     =  4,
1472   STATIC_SHORT,        // T_CHAR        =  5,
1473   STATIC_WORD,         // T_FLOAT       =  6,
1474   STATIC_DOUBLE,       // T_DOUBLE      =  7,
1475   STATIC_BYTE,         // T_BYTE        =  8,
1476   STATIC_SHORT,        // T_SHORT       =  9,
1477   STATIC_WORD,         // T_INT         = 10,
1478   STATIC_DOUBLE,       // T_LONG        = 11,
1479   STATIC_OOP,          // T_OBJECT      = 12,
1480   STATIC_OOP,          // T_ARRAY       = 13,
1481   STATIC_OOP,          // T_VALUETYPE   = 14,
1482   BAD_ALLOCATION_TYPE, // T_VOID        = 15,
1483   BAD_ALLOCATION_TYPE, // T_ADDRESS     = 16,
1484   BAD_ALLOCATION_TYPE, // T_NARROWOOP   = 17,
1485   BAD_ALLOCATION_TYPE, // T_METADATA    = 18,
1486   BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1487   BAD_ALLOCATION_TYPE, // T_CONFLICT    = 20
1488 };
1489 
1490 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type, bool is_flattenable) {
1491   assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1492   FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1493   assert(result != BAD_ALLOCATION_TYPE, "bad type");
1494   if (is_flattenable) {
1495     result = is_static ? STATIC_FLATTENABLE : NONSTATIC_FLATTENABLE;
1496   }
1497   return result;
1498 }
1499 
1500 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1501  public:
1502   u2 count[MAX_FIELD_ALLOCATION_TYPE];
1503 
1504   FieldAllocationCount() {
1505     for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1506       count[i] = 0;
1507     }
1508   }
1509 
1510   FieldAllocationType update(bool is_static, BasicType type, bool is_flattenable) {
1511     FieldAllocationType atype = basic_type_to_atype(is_static, type, is_flattenable);
1512     if (atype != BAD_ALLOCATION_TYPE) {
1513       // Make sure there is no overflow with injected fields.
1514       assert(count[atype] < 0xFFFF, "More than 65535 fields");
1515       count[atype]++;
1516     }
1517     return atype;
1518   }
1519 };
1520 
1521 // Side-effects: populates the _fields, _fields_annotations,
1522 // _fields_type_annotations fields
1523 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1524                                    bool is_interface,
1525                                    bool is_value_type,
1526                                    FieldAllocationCount* const fac,
1527                                    ConstantPool* cp,
1528                                    const int cp_size,
1529                                    u2* const java_fields_count_ptr,
1530                                    TRAPS) {
1531 
1532   assert(cfs != NULL, "invariant");
1533   assert(fac != NULL, "invariant");
1534   assert(cp != NULL, "invariant");
1535   assert(java_fields_count_ptr != NULL, "invariant");
1536 
1537   assert(NULL == _fields, "invariant");
1538   assert(NULL == _fields_annotations, "invariant");
1539   assert(NULL == _fields_type_annotations, "invariant");
1540 
1541   cfs->guarantee_more(2, CHECK);  // length
1542   const u2 length = cfs->get_u2_fast();
1543   *java_fields_count_ptr = length;
1544 
1545   int num_injected = 0;
1546   const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1547                                                                   &num_injected);
1548 
1549   // two more slots are required for inline classes:
1550   // one for the static field with a reference to the pre-allocated default value
1551   // one for the field the JVM injects when detecting an empty inline class
1552   const int total_fields = length + num_injected + (is_value_type ? 2 : 0);
1553 
1554   // The field array starts with tuples of shorts
1555   // [access, name index, sig index, initial value index, byte offset].
1556   // A generic signature slot only exists for field with generic
1557   // signature attribute. And the access flag is set with
1558   // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic
1559   // signature slots are at the end of the field array and after all
1560   // other fields data.
1561   //
1562   //   f1: [access, name index, sig index, initial value index, low_offset, high_offset]
1563   //   f2: [access, name index, sig index, initial value index, low_offset, high_offset]
1564   //       ...
1565   //   fn: [access, name index, sig index, initial value index, low_offset, high_offset]
1566   //       [generic signature index]
1567   //       [generic signature index]
1568   //       ...
1569   //
1570   // Allocate a temporary resource array for field data. For each field,
1571   // a slot is reserved in the temporary array for the generic signature
1572   // index. After parsing all fields, the data are copied to a permanent
1573   // array and any unused slots will be discarded.
1574   ResourceMark rm(THREAD);
1575   u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
1576                                               u2,
1577                                               total_fields * (FieldInfo::field_slots + 1));
1578 
1579   // The generic signature slots start after all other fields' data.
1580   int generic_signature_slot = total_fields * FieldInfo::field_slots;
1581   int num_generic_signature = 0;
1582   int instance_fields_count = 0;
1583   for (int n = 0; n < length; n++) {
1584     // access_flags, name_index, descriptor_index, attributes_count
1585     cfs->guarantee_more(8, CHECK);
1586 
1587     jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS;
1588 
1589     const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1590     verify_legal_field_modifiers(flags, is_interface, is_value_type, CHECK);
1591     AccessFlags access_flags;


1592     access_flags.set_flags(flags);
1593 
1594     const u2 name_index = cfs->get_u2_fast();
1595     check_property(valid_symbol_at(name_index),
1596       "Invalid constant pool index %u for field name in class file %s",
1597       name_index, CHECK);
1598     const Symbol* const name = cp->symbol_at(name_index);
1599     verify_legal_field_name(name, CHECK);
1600 
1601     const u2 signature_index = cfs->get_u2_fast();
1602     check_property(valid_symbol_at(signature_index),
1603       "Invalid constant pool index %u for field signature in class file %s",
1604       signature_index, CHECK);
1605     const Symbol* const sig = cp->symbol_at(signature_index);
1606     verify_legal_field_signature(name, sig, CHECK);
1607     assert(!access_flags.is_flattenable(), "ACC_FLATTENABLE should have been filtered out");
1608     if (sig->is_Q_signature()) {
1609       // assert(_major_version >= CONSTANT_CLASS_DESCRIPTORS, "Q-descriptors are only supported in recent classfiles");
1610       access_flags.set_is_flattenable();
1611     }
1612     if (access_flags.is_flattenable()) {
1613       // Array flattenability cannot be specified.  Arrays of value classes are
1614       // are always flattenable.  Arrays of other classes are not flattenable.
1615       if (sig->utf8_length() > 1 && sig->char_at(0) == '[') {
1616         classfile_parse_error(
1617             "Field \"%s\" with signature \"%s\" in class file %s is invalid."
1618             " ACC_FLATTENABLE cannot be specified for an array",
1619             name->as_C_string(), sig->as_klass_external_name(), CHECK);
1620       }
1621       _has_flattenable_fields = true;
1622     }
1623     if (!access_flags.is_static()) instance_fields_count++;
1624 
1625     u2 constantvalue_index = 0;
1626     bool is_synthetic = false;
1627     u2 generic_signature_index = 0;
1628     const bool is_static = access_flags.is_static();
1629     FieldAnnotationCollector parsed_annotations(_loader_data);
1630 
1631     const u2 attributes_count = cfs->get_u2_fast();
1632     if (attributes_count > 0) {
1633       parse_field_attributes(cfs,
1634                              attributes_count,
1635                              is_static,
1636                              signature_index,
1637                              &constantvalue_index,
1638                              &is_synthetic,
1639                              &generic_signature_index,
1640                              &parsed_annotations,
1641                              CHECK);
1642 
1643       if (parsed_annotations.field_annotations() != NULL) {


1663 
1664       if (is_synthetic) {
1665         access_flags.set_is_synthetic();
1666       }
1667       if (generic_signature_index != 0) {
1668         access_flags.set_field_has_generic_signature();
1669         fa[generic_signature_slot] = generic_signature_index;
1670         generic_signature_slot ++;
1671         num_generic_signature ++;
1672       }
1673     }
1674 
1675     FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1676     field->initialize(access_flags.as_short(),
1677                       name_index,
1678                       signature_index,
1679                       constantvalue_index);
1680     const BasicType type = cp->basic_type_for_signature_at(signature_index);
1681 
1682     // Remember how many oops we encountered and compute allocation type
1683     const FieldAllocationType atype = fac->update(is_static, type, access_flags.is_flattenable());
1684     field->set_allocation_type(atype);
1685 
1686     // After field is initialized with type, we can augment it with aux info
1687     if (parsed_annotations.has_any_annotations())
1688       parsed_annotations.apply_to(field);
1689   }
1690 
1691   int index = length;
1692   if (num_injected != 0) {
1693     for (int n = 0; n < num_injected; n++) {
1694       // Check for duplicates
1695       if (injected[n].may_be_java) {
1696         const Symbol* const name      = injected[n].name();
1697         const Symbol* const signature = injected[n].signature();
1698         bool duplicate = false;
1699         for (int i = 0; i < length; i++) {
1700           const FieldInfo* const f = FieldInfo::from_field_array(fa, i);
1701           if (name      == cp->symbol_at(f->name_index()) &&
1702               signature == cp->symbol_at(f->signature_index())) {
1703             // Symbol is desclared in Java so skip this one
1704             duplicate = true;
1705             break;
1706           }
1707         }
1708         if (duplicate) {
1709           // These will be removed from the field array at the end
1710           continue;
1711         }
1712       }
1713 
1714       // Injected field
1715       FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1716       field->initialize(JVM_ACC_FIELD_INTERNAL,
1717                         injected[n].name_index,
1718                         injected[n].signature_index,
1719                         0);
1720 
1721       const BasicType type = FieldType::basic_type(injected[n].signature());
1722 
1723       // Remember how many oops we encountered and compute allocation type
1724       const FieldAllocationType atype = fac->update(false, type, false);
1725       field->set_allocation_type(atype);
1726       index++;
1727     }
1728   }
1729 
1730   if (is_value_type) {
1731     FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1732     field->initialize(JVM_ACC_FIELD_INTERNAL | JVM_ACC_STATIC,
1733                       vmSymbols::default_value_name_enum,
1734                       vmSymbols::java_lang_Object_enum,
1735                       0);
1736     const BasicType type = FieldType::basic_type(vmSymbols::object_signature());
1737     const FieldAllocationType atype = fac->update(true, type, false);
1738     field->set_allocation_type(atype);
1739     index++;
1740   }
1741 
1742   if (is_value_type && instance_fields_count == 0) {
1743     _is_empty_value = true;
1744     FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1745     field->initialize(JVM_ACC_FIELD_INTERNAL,
1746         vmSymbols::empty_marker_name_enum,
1747         vmSymbols::byte_signature_enum,
1748         0);
1749     const BasicType type = FieldType::basic_type(vmSymbols::byte_signature());
1750     const FieldAllocationType atype = fac->update(false, type, false);
1751     field->set_allocation_type(atype);
1752     index++;
1753   }
1754 
1755   assert(NULL == _fields, "invariant");
1756 
1757   _fields =
1758     MetadataFactory::new_array<u2>(_loader_data,
1759                                    index * FieldInfo::field_slots + num_generic_signature,
1760                                    CHECK);
1761   // Sometimes injected fields already exist in the Java source so
1762   // the fields array could be too long.  In that case the
1763   // fields array is trimed. Also unused slots that were reserved
1764   // for generic signature indexes are discarded.
1765   {
1766     int i = 0;
1767     for (; i < index * FieldInfo::field_slots; i++) {
1768       _fields->at_put(i, fa[i]);
1769     }
1770     for (int j = total_fields * FieldInfo::field_slots;
1771          j < generic_signature_slot; j++) {
1772       _fields->at_put(i++, fa[j]);
1773     }
1774     assert(_fields->length() == i, "");


2050         "Exception name has bad type at constant pool %u in class file %s",
2051         checked_exception, CHECK_NULL);
2052     }
2053   }
2054   // check exceptions attribute length
2055   if (_need_verify) {
2056     guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
2057                                                    sizeof(u2) * size),
2058                       "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
2059   }
2060   return checked_exceptions_start;
2061 }
2062 
2063 void ClassFileParser::throwIllegalSignature(const char* type,
2064                                             const Symbol* name,
2065                                             const Symbol* sig,
2066                                             TRAPS) const {
2067   assert(name != NULL, "invariant");
2068   assert(sig != NULL, "invariant");
2069 
2070   const char* class_note = "";
2071   if (is_value_type() && name == vmSymbols::object_initializer_name()) {
2072     class_note = " (an inline class)";
2073   }
2074 
2075   ResourceMark rm(THREAD);
2076   Exceptions::fthrow(THREAD_AND_LOCATION,
2077       vmSymbols::java_lang_ClassFormatError(),
2078       "%s \"%s\" in class %s%s has illegal signature \"%s\"", type,
2079       name->as_C_string(), _class_name->as_C_string(), class_note, sig->as_C_string());
2080 }
2081 
2082 AnnotationCollector::ID
2083 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
2084                                       const Symbol* name) {
2085   const vmSymbols::SID sid = vmSymbols::find_sid(name);
2086   // Privileged code can use all annotations.  Other code silently drops some.
2087   const bool privileged = loader_data->is_the_null_class_loader_data() ||
2088                           loader_data->is_platform_class_loader_data() ||
2089                           loader_data->is_unsafe_anonymous();
2090   switch (sid) {
2091     case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
2092       if (_location != _in_method)  break;  // only allow for methods
2093       if (!privileged)              break;  // only allow in privileged code
2094       return _method_CallerSensitive;
2095     }
2096     case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
2097       if (_location != _in_method)  break;  // only allow for methods
2098       if (!privileged)              break;  // only allow in privileged code
2099       return _method_ForceInline;


2323                              runtime_visible_type_annotations_length,
2324                              runtime_invisible_type_annotations,
2325                              runtime_invisible_type_annotations_length,
2326                              CHECK);
2327     cm->set_type_annotations(a);
2328   }
2329 }
2330 
2331 
2332 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2333 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2334 // Method* to save footprint, so we only know the size of the resulting Method* when the
2335 // entire method attribute is parsed.
2336 //
2337 // The promoted_flags parameter is used to pass relevant access_flags
2338 // from the method back up to the containing klass. These flag values
2339 // are added to klass's access_flags.
2340 
2341 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2342                                       bool is_interface,
2343                                       bool is_value_type,
2344                                       const ConstantPool* cp,
2345                                       AccessFlags* const promoted_flags,
2346                                       TRAPS) {
2347   assert(cfs != NULL, "invariant");
2348   assert(cp != NULL, "invariant");
2349   assert(promoted_flags != NULL, "invariant");
2350 
2351   ResourceMark rm(THREAD);
2352   // Parse fixed parts:
2353   // access_flags, name_index, descriptor_index, attributes_count
2354   cfs->guarantee_more(8, CHECK_NULL);
2355 
2356   int flags = cfs->get_u2_fast();
2357   const u2 name_index = cfs->get_u2_fast();
2358   const int cp_size = cp->length();
2359   check_property(
2360     valid_symbol_at(name_index),
2361     "Illegal constant pool index %u for method name in class file %s",
2362     name_index, CHECK_NULL);
2363   const Symbol* const name = cp->symbol_at(name_index);
2364   verify_legal_method_name(name, CHECK_NULL);
2365 
2366   const u2 signature_index = cfs->get_u2_fast();
2367   guarantee_property(
2368     valid_symbol_at(signature_index),
2369     "Illegal constant pool index %u for method signature in class file %s",
2370     signature_index, CHECK_NULL);
2371   const Symbol* const signature = cp->symbol_at(signature_index);
2372 
2373   if (name == vmSymbols::class_initializer_name()) {
2374     // We ignore the other access flags for a valid class initializer.
2375     // (JVM Spec 2nd ed., chapter 4.6)
2376     if (_major_version < 51) { // backward compatibility
2377       flags = JVM_ACC_STATIC;
2378     } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2379       flags &= JVM_ACC_STATIC | JVM_ACC_STRICT;
2380     } else {
2381       classfile_parse_error("Method <clinit> is not static in class file %s", CHECK_NULL);
2382     }
2383   } else {
2384     verify_legal_method_modifiers(flags, is_interface, is_value_type, name, CHECK_NULL);
2385   }
2386 
2387   if (name == vmSymbols::object_initializer_name()) {
2388     if (is_interface) {
2389       classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL);
2390     } else if (!is_value_type && signature->is_void_method_signature()) {
2391       // OK, a constructor
2392     } else if (is_value_type && !signature->is_void_method_signature()) {
2393       // also OK, a static factory, as long as the return value is good
2394       bool ok = false;
2395       SignatureStream ss((Symbol*) signature, true);
2396       while (!ss.at_return_type())  ss.next();
2397       if (ss.is_object()) {
2398         TempNewSymbol ret = ss.as_symbol_or_null();
2399         const Symbol* required = class_name();
2400         if (is_unsafe_anonymous()) {
2401           // The original class name in the UAC byte stream gets changed.  So
2402           // using the original name in the return type is no longer valid.
2403           required = vmSymbols::java_lang_Object();
2404         }
2405         ok = (ret == required);
2406       }
2407       if (!ok) {
2408         throwIllegalSignature("Method", name, signature, CHECK_0);
2409       }
2410     } else {
2411       // not OK, so throw the same error as in verify_legal_method_signature.
2412       throwIllegalSignature("Method", name, signature, CHECK_0);
2413     }
2414     // A declared <init> method must always be either a non-static
2415     // object constructor, with a void return, or else it must be a
2416     // static factory method, with a non-void return.  No other
2417     // definition of <init> is possible.
2418     //
2419     // The verifier (in verify_invoke_instructions) will inspect the
2420     // signature of any attempt to invoke <init>, and ensures that it
2421     // returns non-void if and only if it is being invoked by
2422     // invokestatic, and void if and only if it is being invoked by
2423     // invokespecial.
2424     //
2425     // When a symbolic reference to <init> is resolved for a
2426     // particular invocation mode (special or static), the mode is
2427     // matched to the JVM_ACC_STATIC modifier of the <init> method.
2428     // Thus, it is impossible to statically invoke a constructor, and
2429     // impossible to "new + invokespecial" a static factory, either
2430     // through bytecode or through reflection.
2431   }
2432 
2433   int args_size = -1;  // only used when _need_verify is true
2434   if (_need_verify) {
2435     args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2436                  verify_legal_method_signature(name, signature, CHECK_NULL);
2437     if (args_size > MAX_ARGS_SIZE) {
2438       classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_NULL);
2439     }
2440   }
2441 
2442   AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2443 
2444   // Default values for code and exceptions attribute elements
2445   u2 max_stack = 0;
2446   u2 max_locals = 0;
2447   u4 code_length = 0;
2448   const u1* code_start = 0;
2449   u2 exception_table_length = 0;
2450   const unsafe_u2* exception_table_start = NULL; // (potentially unaligned) pointer to array of u2 elements


2981       _has_finalizer = true;
2982     }
2983   }
2984   if (name == vmSymbols::object_initializer_name() &&
2985       signature == vmSymbols::void_method_signature() &&
2986       m->is_vanilla_constructor()) {
2987     _has_vanilla_constructor = true;
2988   }
2989 
2990   NOT_PRODUCT(m->verify());
2991   return m;
2992 }
2993 
2994 
2995 // The promoted_flags parameter is used to pass relevant access_flags
2996 // from the methods back up to the containing klass. These flag values
2997 // are added to klass's access_flags.
2998 // Side-effects: populates the _methods field in the parser
2999 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
3000                                     bool is_interface,
3001                                     bool is_value_type,
3002                                     AccessFlags* promoted_flags,
3003                                     bool* has_final_method,
3004                                     bool* declares_nonstatic_concrete_methods,
3005                                     TRAPS) {
3006   assert(cfs != NULL, "invariant");
3007   assert(promoted_flags != NULL, "invariant");
3008   assert(has_final_method != NULL, "invariant");
3009   assert(declares_nonstatic_concrete_methods != NULL, "invariant");
3010 
3011   assert(NULL == _methods, "invariant");
3012 
3013   cfs->guarantee_more(2, CHECK);  // length
3014   const u2 length = cfs->get_u2_fast();
3015   if (length == 0) {
3016     _methods = Universe::the_empty_method_array();
3017   } else {
3018     _methods = MetadataFactory::new_array<Method*>(_loader_data,
3019                                                    length,
3020                                                    NULL,
3021                                                    CHECK);
3022 
3023     for (int index = 0; index < length; index++) {
3024       Method* method = parse_method(cfs,
3025                                     is_interface,
3026                                     is_value_type,
3027                                     _cp,
3028                                     promoted_flags,
3029                                     CHECK);
3030 
3031       if (method->is_final()) {
3032         *has_final_method = true;
3033       }
3034       // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
3035       // used for interface initialization, and default method inheritance analysis
3036       if (is_interface && !(*declares_nonstatic_concrete_methods)
3037         && !method->is_abstract() && !method->is_static()) {
3038         *declares_nonstatic_concrete_methods = true;
3039       }
3040       _methods->at_put(index, method);
3041     }
3042 
3043     if (_need_verify && length > 1) {
3044       // Check duplicated methods
3045       ResourceMark rm(THREAD);
3046       NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(


3199       valid_klass_reference_at(inner_class_info_index),
3200       "inner_class_info_index %u has bad constant type in class file %s",
3201       inner_class_info_index, CHECK_0);
3202     // Outer class index
3203     const u2 outer_class_info_index = cfs->get_u2_fast();
3204     check_property(
3205       outer_class_info_index == 0 ||
3206         valid_klass_reference_at(outer_class_info_index),
3207       "outer_class_info_index %u has bad constant type in class file %s",
3208       outer_class_info_index, CHECK_0);
3209     // Inner class name
3210     const u2 inner_name_index = cfs->get_u2_fast();
3211     check_property(
3212       inner_name_index == 0 || valid_symbol_at(inner_name_index),
3213       "inner_name_index %u has bad constant type in class file %s",
3214       inner_name_index, CHECK_0);
3215     if (_need_verify) {
3216       guarantee_property(inner_class_info_index != outer_class_info_index,
3217                          "Class is both outer and inner class in class file %s", CHECK_0);
3218     }
3219 
3220     jint recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS;
3221     // JVM_ACC_MODULE is defined in JDK-9 and later.
3222     if (_major_version >= JAVA_9_VERSION) {
3223       recognized_modifiers |= JVM_ACC_MODULE;


3224     }
3225     // JVM_ACC_VALUE is defined for class file version 55 and later
3226     if (supports_value_types()) {
3227       recognized_modifiers |= JVM_ACC_VALUE;
3228     }
3229 
3230     // Access flags
3231     jint flags = cfs->get_u2_fast() & recognized_modifiers;
3232 
3233     if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3234       // Set abstract bit for old class files for backward compatibility
3235       flags |= JVM_ACC_ABSTRACT;
3236     }
3237     verify_legal_class_modifiers(flags, CHECK_0);
3238     AccessFlags inner_access_flags(flags);
3239 
3240     inner_classes->at_put(index++, inner_class_info_index);
3241     inner_classes->at_put(index++, outer_class_info_index);
3242     inner_classes->at_put(index++, inner_name_index);
3243     inner_classes->at_put(index++, inner_access_flags.as_short());
3244   }
3245 
3246   // 4347400: make sure there's no duplicate entry in the classes array
3247   if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3248     for(int i = 0; i < length * 4; i += 4) {
3249       for(int j = i + 4; j < length * 4; j += 4) {
3250         guarantee_property((inner_classes->at(i)   != inner_classes->at(j) ||
3251                             inner_classes->at(i+1) != inner_classes->at(j+1) ||
3252                             inner_classes->at(i+2) != inner_classes->at(j+2) ||


3415   u2 attributes_count = cfs->get_u2_fast();
3416   bool parsed_sourcefile_attribute = false;
3417   bool parsed_innerclasses_attribute = false;
3418   bool parsed_nest_members_attribute = false;
3419   bool parsed_nest_host_attribute = false;
3420   bool parsed_enclosingmethod_attribute = false;
3421   bool parsed_bootstrap_methods_attribute = false;
3422   const u1* runtime_visible_annotations = NULL;
3423   int runtime_visible_annotations_length = 0;
3424   const u1* runtime_invisible_annotations = NULL;
3425   int runtime_invisible_annotations_length = 0;
3426   const u1* runtime_visible_type_annotations = NULL;
3427   int runtime_visible_type_annotations_length = 0;
3428   const u1* runtime_invisible_type_annotations = NULL;
3429   int runtime_invisible_type_annotations_length = 0;
3430   bool runtime_invisible_type_annotations_exists = false;
3431   bool runtime_invisible_annotations_exists = false;
3432   bool parsed_source_debug_ext_annotations_exist = false;
3433   const u1* inner_classes_attribute_start = NULL;
3434   u4  inner_classes_attribute_length = 0;
3435   const u1* value_types_attribute_start = NULL;
3436   u4 value_types_attribute_length = 0;
3437   u2  enclosing_method_class_index = 0;
3438   u2  enclosing_method_method_index = 0;
3439   const u1* nest_members_attribute_start = NULL;
3440   u4  nest_members_attribute_length = 0;
3441 
3442   // Iterate over attributes
3443   while (attributes_count--) {
3444     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3445     const u2 attribute_name_index = cfs->get_u2_fast();
3446     const u4 attribute_length = cfs->get_u4_fast();
3447     check_property(
3448       valid_symbol_at(attribute_name_index),
3449       "Attribute name has bad constant pool index %u in class file %s",
3450       attribute_name_index, CHECK);
3451     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3452     if (tag == vmSymbols::tag_source_file()) {
3453       // Check for SourceFile tag
3454       if (_need_verify) {
3455         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3456       }


3766       }
3767     }
3768     if (runtime_invisible_annotations != NULL) {
3769       for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3770         int append = runtime_visible_annotations_length+i;
3771         annotations->at_put(append, runtime_invisible_annotations[i]);
3772       }
3773     }
3774   }
3775   return annotations;
3776 }
3777 
3778 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3779                                                         const int super_class_index,
3780                                                         const bool need_verify,
3781                                                         TRAPS) {
3782   assert(cp != NULL, "invariant");
3783   const InstanceKlass* super_klass = NULL;
3784 
3785   if (super_class_index == 0) {
3786     check_property(_class_name == vmSymbols::java_lang_Object()
3787                    || (_access_flags.get_flags() & JVM_ACC_VALUE),
3788                    "Invalid superclass index %u in class file %s",
3789                    super_class_index,
3790                    CHECK_NULL);
3791   } else {
3792     check_property(valid_klass_reference_at(super_class_index),
3793                    "Invalid superclass index %u in class file %s",
3794                    super_class_index,
3795                    CHECK_NULL);
3796     // The class name should be legal because it is checked when parsing constant pool.
3797     // However, make sure it is not an array type.
3798     bool is_array = false;
3799     if (cp->tag_at(super_class_index).is_klass()) {
3800       super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3801       if (need_verify)
3802         is_array = super_klass->is_array_klass();
3803     } else if (need_verify) {
3804       is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3805     }
3806     if (need_verify) {
3807       guarantee_property(!is_array,
3808                         "Bad superclass name in class file %s", CHECK_NULL);
3809     }
3810   }
3811   return super_klass;
3812 }
3813 

































3814 #ifndef PRODUCT
3815 static void print_field_layout(const Symbol* name,
3816                                Array<u2>* fields,
3817                                const constantPoolHandle& cp,
3818                                int instance_size,
3819                                int instance_fields_start,
3820                                int instance_fields_end,
3821                                int static_fields_end) {
3822 
3823   assert(name != NULL, "invariant");
3824 
3825   tty->print("%s: field layout\n", name->as_klass_external_name());
3826   tty->print("  @%3d %s\n", instance_fields_start, "--- instance fields start ---");
3827   for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3828     if (!fs.access_flags().is_static()) {
3829       tty->print("  @%3d \"%s\" %s\n",
3830         fs.offset(),
3831         fs.name()->as_klass_external_name(),
3832         fs.signature()->as_klass_external_name());
3833     }
3834   }
3835   tty->print("  @%3d %s\n", instance_fields_end, "--- instance fields end ---");
3836   tty->print("  @%3d %s\n", instance_size * wordSize, "--- instance ends ---");
3837   tty->print("  @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---");
3838   for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3839     if (fs.access_flags().is_static()) {
3840       tty->print("  @%3d \"%s\" %s\n",
3841         fs.offset(),
3842         fs.name()->as_klass_external_name(),
3843         fs.signature()->as_klass_external_name());
3844     }
3845   }
3846   tty->print("  @%3d %s\n", static_fields_end, "--- static fields end ---");
3847   tty->print("\n");
3848 }
3849 #endif
3850 
3851 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int  max_blocks, TRAPS) {
3852   max_nonstatic_oop_maps = max_blocks;
3853   nonstatic_oop_map_count = 0;
3854   if (max_blocks == 0) {
3855     nonstatic_oop_maps = NULL;
3856   } else {
3857     nonstatic_oop_maps = NEW_RESOURCE_ARRAY_IN_THREAD(
3858         THREAD, OopMapBlock, max_nonstatic_oop_maps);
3859     memset(nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3860   }
3861 }
3862 
3863 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
3864   assert(nonstatic_oop_map_count > 0, "Has no oop maps");
3865   return nonstatic_oop_maps + (nonstatic_oop_map_count - 1);
3866 }
3867 
3868 // addition of super oop maps
3869 void OopMapBlocksBuilder::initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks) {
3870   assert(nof_blocks && nonstatic_oop_map_count == 0 &&
3871       nof_blocks <= max_nonstatic_oop_maps, "invariant");
3872 
3873   memcpy(nonstatic_oop_maps, blocks, sizeof(OopMapBlock) * nof_blocks);
3874   nonstatic_oop_map_count += nof_blocks;
3875 }
3876 
3877 // collection of oops
3878 void OopMapBlocksBuilder::add(int offset, int count) {
3879   if (nonstatic_oop_map_count == 0) {
3880     nonstatic_oop_map_count++;
3881   }
3882   OopMapBlock*  nonstatic_oop_map = last_oop_map();
3883   if (nonstatic_oop_map->count() == 0) {  // Unused map, set it up
3884     nonstatic_oop_map->set_offset(offset);
3885     nonstatic_oop_map->set_count(count);
3886   } else if (nonstatic_oop_map->is_contiguous(offset)) { // contiguous, add
3887     nonstatic_oop_map->increment_count(count);
3888   } else { // Need a new one...
3889     nonstatic_oop_map_count++;
3890     assert(nonstatic_oop_map_count <= max_nonstatic_oop_maps, "range check");
3891     nonstatic_oop_map = last_oop_map();
3892     nonstatic_oop_map->set_offset(offset);
3893     nonstatic_oop_map->set_count(count);
3894   }
3895 }
3896 
3897 // general purpose copy, e.g. into allocated instanceKlass
3898 void OopMapBlocksBuilder::copy(OopMapBlock* dst) {
3899   if (nonstatic_oop_map_count != 0) {
3900     memcpy(dst, nonstatic_oop_maps, sizeof(OopMapBlock) * nonstatic_oop_map_count);
3901   }
3902 }
3903 
3904 // Sort and compact adjacent blocks
3905 void OopMapBlocksBuilder::compact(TRAPS) {
3906   if (nonstatic_oop_map_count <= 1) {
3907     return;
3908   }
3909   /*
3910    * Since field layout sneeks in oops before values, we will be able to condense
3911    * blocks. There is potential to compact between super, own refs and values
3912    * containing refs.
3913    *
3914    * Currently compaction is slightly limited due to values being 8 byte aligned.
3915    * This may well change: FixMe if doesn't, the code below is fairly general purpose
3916    * and maybe it doesn't need to be.
3917    */
3918   qsort(nonstatic_oop_maps, nonstatic_oop_map_count, sizeof(OopMapBlock),
3919       (_sort_Fn)OopMapBlock::compare_offset);
3920   if (nonstatic_oop_map_count < 2) {
3921     return;
3922   }
3923 
3924   //Make a temp copy, and iterate through and copy back into the orig
3925   ResourceMark rm(THREAD);
3926   OopMapBlock* oop_maps_copy = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, OopMapBlock,
3927       nonstatic_oop_map_count);
3928   OopMapBlock* oop_maps_copy_end = oop_maps_copy + nonstatic_oop_map_count;
3929   copy(oop_maps_copy);
3930   OopMapBlock*  nonstatic_oop_map = nonstatic_oop_maps;
3931   unsigned int new_count = 1;
3932   oop_maps_copy++;
3933   while(oop_maps_copy < oop_maps_copy_end) {
3934     assert(nonstatic_oop_map->offset() < oop_maps_copy->offset(), "invariant");
3935     if (nonstatic_oop_map->is_contiguous(oop_maps_copy->offset())) {
3936       nonstatic_oop_map->increment_count(oop_maps_copy->count());
3937     } else {
3938       nonstatic_oop_map++;
3939       new_count++;
3940       nonstatic_oop_map->set_offset(oop_maps_copy->offset());
3941       nonstatic_oop_map->set_count(oop_maps_copy->count());
3942     }
3943     oop_maps_copy++;
3944   }
3945   assert(new_count <= nonstatic_oop_map_count, "end up with more maps after compact() ?");
3946   nonstatic_oop_map_count = new_count;
3947 }
3948 
3949 void OopMapBlocksBuilder::print_on(outputStream* st) const {
3950   st->print_cr("  OopMapBlocks: %3d  /%3d", nonstatic_oop_map_count, max_nonstatic_oop_maps);
3951   if (nonstatic_oop_map_count > 0) {
3952     OopMapBlock* map = nonstatic_oop_maps;
3953     OopMapBlock* last_map = last_oop_map();
3954     assert(map <= last_map, "Last less than first");
3955     while (map <= last_map) {
3956       st->print_cr("    Offset: %3d  -%3d Count: %3d", map->offset(),
3957           map->offset() + map->offset_span() - heapOopSize, map->count());
3958       map++;
3959     }
3960   }
3961 }
3962 
3963 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
3964   print_on(st);
3965 }
3966 
3967 void ClassFileParser::throwValueTypeLimitation(THREAD_AND_LOCATION_DECL,
3968                                                const char* msg,
3969                                                const Symbol* name,
3970                                                const Symbol* sig) const {
3971 
3972   ResourceMark rm(THREAD);
3973   if (name == NULL || sig == NULL) {
3974     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
3975         vmSymbols::java_lang_ClassFormatError(),
3976         "class: %s - %s", _class_name->as_C_string(), msg);
3977   }
3978   else {
3979     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
3980         vmSymbols::java_lang_ClassFormatError(),
3981         "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(),
3982         _class_name->as_C_string(), msg);
3983   }
3984 }
3985 
3986 // Layout fields and fill in FieldLayoutInfo.  Could use more refactoring!
3987 void ClassFileParser::layout_fields(ConstantPool* cp,
3988                                     const FieldAllocationCount* fac,
3989                                     const ClassAnnotationCollector* parsed_annotations,
3990                                     FieldLayoutInfo* info,
3991                                     TRAPS) {
3992 
3993   assert(cp != NULL, "invariant");
3994 
3995   // Field size and offset computation
3996   int nonstatic_field_size = _super_klass == NULL ? 0 :
3997                                _super_klass->nonstatic_field_size();
3998   int next_nonstatic_valuetype_offset = 0;
3999   int first_nonstatic_valuetype_offset = 0;
4000 
4001   // Fields that are value types are handled differently depending if they are static or not:
4002   // - static fields are oops
4003   // - non-static fields are embedded
4004 
4005   // Count the contended fields by type.
4006   //
4007   // We ignore static fields, because @Contended is not supported for them.
4008   // The layout code below will also ignore the static fields.
4009   int nonstatic_contended_count = 0;
4010   FieldAllocationCount fac_contended;
4011   for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
4012     FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
4013     if (fs.is_contended()) {
4014       fac_contended.count[atype]++;
4015       if (!fs.access_flags().is_static()) {
4016         nonstatic_contended_count++;
4017       }
4018     }
4019   }
4020 
4021 
4022   // Calculate the starting byte offsets
4023   int next_static_oop_offset    = InstanceMirrorKlass::offset_of_static_fields();
4024   // Value types in static fields are not embedded, they are handled with oops
4025   int next_static_double_offset = next_static_oop_offset +
4026                                   ((fac->count[STATIC_OOP] + fac->count[STATIC_FLATTENABLE]) * heapOopSize);
4027   if (fac->count[STATIC_DOUBLE]) {
4028     next_static_double_offset = align_up(next_static_double_offset, BytesPerLong);
4029   }
4030 
4031   int next_static_word_offset   = next_static_double_offset +
4032                                     ((fac->count[STATIC_DOUBLE]) * BytesPerLong);
4033   int next_static_short_offset  = next_static_word_offset +
4034                                     ((fac->count[STATIC_WORD]) * BytesPerInt);
4035   int next_static_byte_offset   = next_static_short_offset +
4036                                   ((fac->count[STATIC_SHORT]) * BytesPerShort);
4037 
4038   int nonstatic_fields_start  = instanceOopDesc::base_offset_in_bytes() +
4039                                 nonstatic_field_size * heapOopSize;
4040 
4041   // First field of value types is aligned on a long boundary in order to ease
4042   // in-lining of value types (with header removal) in packed arrays and
4043   // flatten value types
4044   int initial_value_type_padding = 0;
4045   if (is_value_type()) {
4046     int old = nonstatic_fields_start;
4047     nonstatic_fields_start = align_up(nonstatic_fields_start, BytesPerLong);
4048     initial_value_type_padding = nonstatic_fields_start - old;
4049   }
4050 
4051   int next_nonstatic_field_offset = nonstatic_fields_start;
4052 
4053   const bool is_contended_class     = parsed_annotations->is_contended();
4054 
4055   // Class is contended, pad before all the fields
4056   if (is_contended_class) {
4057     next_nonstatic_field_offset += ContendedPaddingWidth;
4058   }
4059 
4060   // Temporary value types restrictions
4061   if (is_value_type()) {
4062     if (is_contended_class) {
4063       throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support @Contended annotation yet");
4064       return;
4065     }
4066   }
4067 
4068   // Compute the non-contended fields count.
4069   // The packing code below relies on these counts to determine if some field
4070   // can be squeezed into the alignment gap. Contended fields are obviously
4071   // exempt from that.
4072   unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE];
4073   unsigned int nonstatic_word_count   = fac->count[NONSTATIC_WORD]   - fac_contended.count[NONSTATIC_WORD];
4074   unsigned int nonstatic_short_count  = fac->count[NONSTATIC_SHORT]  - fac_contended.count[NONSTATIC_SHORT];
4075   unsigned int nonstatic_byte_count   = fac->count[NONSTATIC_BYTE]   - fac_contended.count[NONSTATIC_BYTE];
4076   unsigned int nonstatic_oop_count    = fac->count[NONSTATIC_OOP]    - fac_contended.count[NONSTATIC_OOP];
4077 
4078   int static_value_type_count = 0;
4079   int nonstatic_value_type_count = 0;
4080   int* nonstatic_value_type_indexes = NULL;
4081   Klass** nonstatic_value_type_klasses = NULL;
4082   unsigned int value_type_oop_map_count = 0;
4083   int not_flattened_value_types = 0;
4084 
4085   int max_nonstatic_value_type = fac->count[NONSTATIC_FLATTENABLE] + 1;
4086 
4087   nonstatic_value_type_indexes = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, int,
4088                                                               max_nonstatic_value_type);
4089   for (int i = 0; i < max_nonstatic_value_type; i++) {
4090     nonstatic_value_type_indexes[i] = -1;
4091   }
4092   nonstatic_value_type_klasses = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, Klass*,
4093                                                               max_nonstatic_value_type);
4094 
4095   for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) {
4096     if (fs.allocation_type() == STATIC_FLATTENABLE) {
4097       ResourceMark rm;
4098       if (!fs.signature()->is_Q_signature()) {
4099         THROW(vmSymbols::java_lang_ClassFormatError());
4100       }
4101       static_value_type_count++;
4102     } else if (fs.allocation_type() == NONSTATIC_FLATTENABLE) {
4103       // Pre-resolve the flattenable field and check for value type circularity issues.
4104       ResourceMark rm;
4105       if (!fs.signature()->is_Q_signature()) {
4106         THROW(vmSymbols::java_lang_ClassFormatError());
4107       }
4108       Klass* klass =
4109         SystemDictionary::resolve_flattenable_field_or_fail(&fs,
4110                                                             Handle(THREAD, _loader_data->class_loader()),
4111                                                             _protection_domain, true, CHECK);
4112       assert(klass != NULL, "Sanity check");
4113       if (!klass->access_flags().is_value_type()) {
4114         THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
4115       }
4116       ValueKlass* vk = ValueKlass::cast(klass);
4117       // Conditions to apply flattening or not should be defined in a single place
4118       if ((ValueFieldMaxFlatSize < 0) || (vk->size_helper() * HeapWordSize) <= ValueFieldMaxFlatSize) {
4119         nonstatic_value_type_indexes[nonstatic_value_type_count] = fs.index();
4120         nonstatic_value_type_klasses[nonstatic_value_type_count] = klass;
4121         nonstatic_value_type_count++;
4122 
4123         ValueKlass* vklass = ValueKlass::cast(klass);
4124         if (vklass->contains_oops()) {
4125           value_type_oop_map_count += vklass->nonstatic_oop_map_count();
4126         }
4127         fs.set_flattened(true);
4128       } else {
4129         not_flattened_value_types++;
4130         fs.set_flattened(false);
4131       }
4132     }
4133   }
4134 
4135   // Adjusting non_static_oop_count to take into account not flattened value types;
4136   nonstatic_oop_count += not_flattened_value_types;
4137 
4138   // Total non-static fields count, including every contended field
4139   unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] +
4140                                         fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] +
4141                                         fac->count[NONSTATIC_OOP] + fac->count[NONSTATIC_FLATTENABLE];
4142 
4143   const bool super_has_nonstatic_fields =
4144           (_super_klass != NULL && _super_klass->has_nonstatic_fields());
4145   const bool has_nonstatic_fields =
4146     super_has_nonstatic_fields || (nonstatic_fields_count != 0);
4147   const bool has_nonstatic_value_fields = nonstatic_value_type_count > 0;
4148 
4149   if (is_value_type() && (!has_nonstatic_fields)) {
4150     // There are a number of fixes required throughout the type system and JIT
4151     throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support zero instance size yet");
4152     return;
4153   }
4154 
4155   // Prepare list of oops for oop map generation.
4156   //
4157   // "offset" and "count" lists are describing the set of contiguous oop
4158   // regions. offset[i] is the start of the i-th region, which then has
4159   // count[i] oops following. Before we know how many regions are required,
4160   // we pessimistically allocate the maps to fit all the oops into the
4161   // distinct regions.
4162   //
4163   int super_oop_map_count = (_super_klass == NULL) ? 0 :_super_klass->nonstatic_oop_map_count();
4164   int max_oop_map_count =
4165       super_oop_map_count +
4166       fac->count[NONSTATIC_OOP] +
4167       value_type_oop_map_count +
4168       not_flattened_value_types;
4169 
4170   OopMapBlocksBuilder* nonstatic_oop_maps = new OopMapBlocksBuilder(max_oop_map_count, THREAD);
4171   if (super_oop_map_count > 0) {
4172     nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(),
4173                                                     _super_klass->nonstatic_oop_map_count());
4174   }
4175 
4176   int first_nonstatic_oop_offset = 0; // will be set for first oop field
4177 
4178   bool compact_fields   = CompactFields;
4179   int allocation_style = FieldsAllocationStyle;
4180   if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
4181     assert(false, "0 <= FieldsAllocationStyle <= 2");
4182     allocation_style = 1; // Optimistic
4183   }
4184 
4185   // The next classes have predefined hard-coded fields offsets
4186   // (see in JavaClasses::compute_hard_coded_offsets()).
4187   // Use default fields allocation order for them.
4188   if( (allocation_style != 0 || compact_fields ) && _loader_data->class_loader() == NULL &&
4189       (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
4190        _class_name == vmSymbols::java_lang_Class() ||
4191        _class_name == vmSymbols::java_lang_ClassLoader() ||
4192        _class_name == vmSymbols::java_lang_ref_Reference() ||
4193        _class_name == vmSymbols::java_lang_ref_SoftReference() ||
4194        _class_name == vmSymbols::java_lang_StackTraceElement() ||


4203        _class_name == vmSymbols::java_lang_Integer() ||
4204        _class_name == vmSymbols::java_lang_Long())) {
4205     allocation_style = 0;     // Allocate oops first
4206     compact_fields   = false; // Don't compact fields
4207   }
4208 
4209   int next_nonstatic_oop_offset = 0;
4210   int next_nonstatic_double_offset = 0;
4211 
4212   // Rearrange fields for a given allocation style
4213   if( allocation_style == 0 ) {
4214     // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
4215     next_nonstatic_oop_offset    = next_nonstatic_field_offset;
4216     next_nonstatic_double_offset = next_nonstatic_oop_offset +
4217                                     (nonstatic_oop_count * heapOopSize);
4218   } else if( allocation_style == 1 ) {
4219     // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
4220     next_nonstatic_double_offset = next_nonstatic_field_offset;
4221   } else if( allocation_style == 2 ) {
4222     // Fields allocation: oops fields in super and sub classes are together.
4223     if( nonstatic_field_size > 0 && super_oop_map_count > 0 ) {
4224       if (next_nonstatic_field_offset == nonstatic_oop_maps->last_oop_map()->end_offset()) {





4225         allocation_style = 0;   // allocate oops first
4226         next_nonstatic_oop_offset    = next_nonstatic_field_offset;
4227         next_nonstatic_double_offset = next_nonstatic_oop_offset +
4228                                        (nonstatic_oop_count * heapOopSize);
4229       }
4230     }
4231     if( allocation_style == 2 ) {
4232       allocation_style = 1;     // allocate oops last
4233       next_nonstatic_double_offset = next_nonstatic_field_offset;
4234     }
4235   } else {
4236     ShouldNotReachHere();
4237   }
4238 
4239   int nonstatic_oop_space_count   = 0;
4240   int nonstatic_word_space_count  = 0;
4241   int nonstatic_short_space_count = 0;
4242   int nonstatic_byte_space_count  = 0;
4243   int nonstatic_oop_space_offset = 0;
4244   int nonstatic_word_space_offset = 0;


4287   }
4288 
4289   int next_nonstatic_word_offset = next_nonstatic_double_offset +
4290                                      (nonstatic_double_count * BytesPerLong);
4291   int next_nonstatic_short_offset = next_nonstatic_word_offset +
4292                                       (nonstatic_word_count * BytesPerInt);
4293   int next_nonstatic_byte_offset = next_nonstatic_short_offset +
4294                                      (nonstatic_short_count * BytesPerShort);
4295   int next_nonstatic_padded_offset = next_nonstatic_byte_offset +
4296                                        nonstatic_byte_count;
4297 
4298   // let oops jump before padding with this allocation style
4299   if( allocation_style == 1 ) {
4300     next_nonstatic_oop_offset = next_nonstatic_padded_offset;
4301     if( nonstatic_oop_count > 0 ) {
4302       next_nonstatic_oop_offset = align_up(next_nonstatic_oop_offset, heapOopSize);
4303     }
4304     next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
4305   }
4306 
4307   // Aligning embedded value types
4308   // bug below, the current algorithm to layout embedded value types always put them at the
4309   // end of the layout, which doesn't match the different allocation policies the VM is
4310   // supposed to provide => FixMe
4311   // Note also that the current alignment policy is to make each value type starting on a
4312   // 64 bits boundary. This could be optimized later. For instance, it could be nice to
4313   // align value types according to their most constrained internal type.
4314   next_nonstatic_valuetype_offset = align_up(next_nonstatic_padded_offset, BytesPerLong);
4315   int next_value_type_index = 0;
4316 
4317   // Iterate over fields again and compute correct offsets.
4318   // The field allocation type was temporarily stored in the offset slot.
4319   // oop fields are located before non-oop fields (static and non-static).
4320   for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
4321 
4322     // skip already laid out fields
4323     if (fs.is_offset_set()) continue;
4324 
4325     // contended instance fields are handled below
4326     if (fs.is_contended() && !fs.access_flags().is_static()) continue;
4327 
4328     int real_offset = 0;
4329     const FieldAllocationType atype = (const FieldAllocationType) fs.allocation_type();
4330 
4331     // pack the rest of the fields
4332     switch (atype) {
4333       // Value types in static fields are handled with oops
4334       case STATIC_FLATTENABLE:   // Fallthrough
4335       case STATIC_OOP:
4336         real_offset = next_static_oop_offset;
4337         next_static_oop_offset += heapOopSize;
4338         break;
4339       case STATIC_BYTE:
4340         real_offset = next_static_byte_offset;
4341         next_static_byte_offset += 1;
4342         break;
4343       case STATIC_SHORT:
4344         real_offset = next_static_short_offset;
4345         next_static_short_offset += BytesPerShort;
4346         break;
4347       case STATIC_WORD:
4348         real_offset = next_static_word_offset;
4349         next_static_word_offset += BytesPerInt;
4350         break;
4351       case STATIC_DOUBLE:
4352         real_offset = next_static_double_offset;
4353         next_static_double_offset += BytesPerLong;
4354         break;
4355       case NONSTATIC_FLATTENABLE:
4356         if (fs.is_flattened()) {
4357           Klass* klass = nonstatic_value_type_klasses[next_value_type_index];
4358           assert(klass != NULL, "Klass should have been loaded and resolved earlier");
4359           assert(klass->access_flags().is_value_type(),"Must be a value type");
4360           ValueKlass* vklass = ValueKlass::cast(klass);
4361           real_offset = next_nonstatic_valuetype_offset;
4362           next_nonstatic_valuetype_offset += (vklass->size_helper()) * wordSize - vklass->first_field_offset();
4363           // aligning next value type on a 64 bits boundary
4364           next_nonstatic_valuetype_offset = align_up(next_nonstatic_valuetype_offset, BytesPerLong);
4365           next_value_type_index += 1;
4366 
4367           if (vklass->contains_oops()) { // add flatten oop maps
4368             int diff = real_offset - vklass->first_field_offset();
4369             const OopMapBlock* map = vklass->start_of_nonstatic_oop_maps();
4370             const OopMapBlock* const last_map = map + vklass->nonstatic_oop_map_count();
4371             while (map < last_map) {
4372               nonstatic_oop_maps->add(map->offset() + diff, map->count());
4373               map++;
4374             }
4375           }
4376           break;
4377         } else {
4378           // Fall through
4379         }
4380       case NONSTATIC_OOP:
4381         if( nonstatic_oop_space_count > 0 ) {
4382           real_offset = nonstatic_oop_space_offset;
4383           nonstatic_oop_space_offset += heapOopSize;
4384           nonstatic_oop_space_count  -= 1;
4385         } else {
4386           real_offset = next_nonstatic_oop_offset;
4387           next_nonstatic_oop_offset += heapOopSize;
4388         }
4389         nonstatic_oop_maps->add(real_offset, 1);



















4390         break;
4391       case NONSTATIC_BYTE:
4392         if( nonstatic_byte_space_count > 0 ) {
4393           real_offset = nonstatic_byte_space_offset;
4394           nonstatic_byte_space_offset += 1;
4395           nonstatic_byte_space_count  -= 1;
4396         } else {
4397           real_offset = next_nonstatic_byte_offset;
4398           next_nonstatic_byte_offset += 1;
4399         }
4400         break;
4401       case NONSTATIC_SHORT:
4402         if( nonstatic_short_space_count > 0 ) {
4403           real_offset = nonstatic_short_space_offset;
4404           nonstatic_short_space_offset += BytesPerShort;
4405           nonstatic_short_space_count  -= 1;
4406         } else {
4407           real_offset = next_nonstatic_short_offset;
4408           next_nonstatic_short_offset += BytesPerShort;
4409         }


4478             break;
4479 
4480           case NONSTATIC_SHORT:
4481             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerShort);
4482             real_offset = next_nonstatic_padded_offset;
4483             next_nonstatic_padded_offset += BytesPerShort;
4484             break;
4485 
4486           case NONSTATIC_WORD:
4487             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerInt);
4488             real_offset = next_nonstatic_padded_offset;
4489             next_nonstatic_padded_offset += BytesPerInt;
4490             break;
4491 
4492           case NONSTATIC_DOUBLE:
4493             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerLong);
4494             real_offset = next_nonstatic_padded_offset;
4495             next_nonstatic_padded_offset += BytesPerLong;
4496             break;
4497 
4498             // Value types in static fields are handled with oops
4499           case NONSTATIC_FLATTENABLE:
4500             throwValueTypeLimitation(THREAD_AND_LOCATION,
4501                                      "@Contended annotation not supported for value types yet", fs.name(), fs.signature());
4502             return;
4503 
4504           case NONSTATIC_OOP:
4505             next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, heapOopSize);
4506             real_offset = next_nonstatic_padded_offset;
4507             next_nonstatic_padded_offset += heapOopSize;
4508             nonstatic_oop_maps->add(real_offset, 1);



















4509             break;
4510 
4511           default:
4512             ShouldNotReachHere();
4513         }
4514 
4515         if (fs.contended_group() == 0) {
4516           // Contended group defines the equivalence class over the fields:
4517           // the fields within the same contended group are not inter-padded.
4518           // The only exception is default group, which does not incur the
4519           // equivalence, and so requires intra-padding.
4520           next_nonstatic_padded_offset += ContendedPaddingWidth;
4521         }
4522 
4523         fs.set_offset(real_offset);
4524       } // for
4525 
4526       // Start laying out the next group.
4527       // Note that this will effectively pad the last group in the back;
4528       // this is expected to alleviate memory contention effects for
4529       // subclass fields and/or adjacent object.
4530       // If this was the default group, the padding is already in place.
4531       if (current_group != 0) {
4532         next_nonstatic_padded_offset += ContendedPaddingWidth;
4533       }
4534     }
4535 
4536     // handle static fields
4537   }
4538 
4539   // Entire class is contended, pad in the back.
4540   // This helps to alleviate memory contention effects for subclass fields
4541   // and/or adjacent object.
4542   if (is_contended_class) {
4543     assert(!is_value_type(), "@Contended not supported for value types yet");
4544     next_nonstatic_padded_offset += ContendedPaddingWidth;
4545   }
4546 
4547   int notaligned_nonstatic_fields_end;
4548   if (nonstatic_value_type_count != 0) {
4549     notaligned_nonstatic_fields_end = next_nonstatic_valuetype_offset;
4550   } else {
4551     notaligned_nonstatic_fields_end = next_nonstatic_padded_offset;
4552   }
4553 
4554   int nonstatic_field_sz_align = heapOopSize;
4555   if (is_value_type()) {
4556     if ((notaligned_nonstatic_fields_end - nonstatic_fields_start) > heapOopSize) {
4557       nonstatic_field_sz_align = BytesPerLong; // value copy of fields only uses jlong copy
4558     }
4559   }
4560   int nonstatic_fields_end      = align_up(notaligned_nonstatic_fields_end, nonstatic_field_sz_align);
4561   int instance_end              = align_up(notaligned_nonstatic_fields_end, wordSize);
4562   int static_fields_end         = align_up(next_static_byte_offset, wordSize);
4563 
4564   int static_field_size         = (static_fields_end -
4565                                    InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
4566   nonstatic_field_size          = nonstatic_field_size +
4567                                   (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize;
4568 
4569   int instance_size             = align_object_size(instance_end / wordSize);
4570 
4571   assert(instance_size == align_object_size(align_up(
4572          (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize)
4573          + initial_value_type_padding, wordSize) / wordSize), "consistent layout helper value");
4574 
4575 
4576   // Invariant: nonstatic_field end/start should only change if there are
4577   // nonstatic fields in the class, or if the class is contended. We compare
4578   // against the non-aligned value, so that end alignment will not fail the
4579   // assert without actually having the fields.
4580   assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) ||
4581          is_contended_class ||
4582          (nonstatic_fields_count > 0), "double-check nonstatic start/end");
4583 
4584   // Number of non-static oop map blocks allocated at end of klass.
4585   nonstatic_oop_maps->compact(THREAD);


4586 
4587 #ifndef PRODUCT
4588   if ((PrintFieldLayout && !is_value_type()) ||
4589       (PrintValueLayout && (is_value_type() || has_nonstatic_value_fields))) {
4590     print_field_layout(_class_name,
4591           _fields,
4592           cp,
4593           instance_size,
4594           nonstatic_fields_start,
4595           nonstatic_fields_end,
4596           static_fields_end);
4597     nonstatic_oop_maps->print_on(tty);
4598     tty->print("\n");
4599     tty->print_cr("Instance size = %d", instance_size);
4600     tty->print_cr("Nonstatic_field_size = %d", nonstatic_field_size);
4601     tty->print_cr("Static_field_size = %d", static_field_size);
4602     tty->print_cr("Has nonstatic fields = %d", has_nonstatic_fields);
4603     tty->print_cr("---");
4604   }
4605 
4606 #endif
4607   // Pass back information needed for InstanceKlass creation
4608   info->oop_map_blocks = nonstatic_oop_maps;



4609   info->instance_size = instance_size;
4610   info->static_field_size = static_field_size;
4611   info->nonstatic_field_size = nonstatic_field_size;
4612   info->has_nonstatic_fields = has_nonstatic_fields;
4613 }
4614 
4615 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik, TRAPS) {










































4616   assert(ik != NULL, "invariant");
4617 
4618   const Klass* const super = ik->super();
4619 
4620   // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4621   // in which case we don't have to register objects as finalizable
4622   if (!_has_empty_finalizer) {
4623     if (_has_finalizer ||
4624         (super != NULL && super->has_finalizer())) {
4625       ik->set_has_finalizer();
4626     }
4627   }
4628 
4629 #ifdef ASSERT
4630   bool f = false;
4631   const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4632                                            vmSymbols::void_method_signature());
4633   if (m != NULL && !m->is_empty_method()) {
4634       f = true;
4635   }
4636 
4637   // Spec doesn't prevent agent from redefinition of empty finalizer.
4638   // Despite the fact that it's generally bad idea and redefined finalizer
4639   // will not work as expected we shouldn't abort vm in this case
4640   if (!ik->has_redefined_this_or_super()) {
4641     assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4642   }
4643 #endif
4644 
4645   // Check if this klass supports the java.lang.Cloneable interface
4646   if (SystemDictionary::Cloneable_klass_loaded()) {
4647     if (ik->is_subtype_of(SystemDictionary::Cloneable_klass())) {
4648       if (ik->is_value()) {
4649         throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support Cloneable");
4650         return;
4651       }
4652       ik->set_is_cloneable();
4653     }
4654   }
4655 
4656   // Check if this klass has a vanilla default constructor
4657   if (super == NULL) {
4658     // java.lang.Object has empty default constructor
4659     ik->set_has_vanilla_constructor();
4660   } else {
4661     if (super->has_vanilla_constructor() &&
4662         _has_vanilla_constructor) {
4663       ik->set_has_vanilla_constructor();
4664     }
4665 #ifdef ASSERT
4666     bool v = false;
4667     if (super->has_vanilla_constructor()) {
4668       const Method* const constructor =
4669         ik->find_method(vmSymbols::object_initializer_name(),
4670                        vmSymbols::void_method_signature());
4671       if (constructor != NULL && constructor->is_vanilla_constructor()) {
4672         v = true;
4673       }
4674     }
4675     assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4676 #endif
4677   }
4678 
4679   // If it cannot be fast-path allocated, set a bit in the layout helper.
4680   // See documentation of InstanceKlass::can_be_fastpath_allocated().
4681   assert(ik->size_helper() > 0, "layout_helper is initialized");
4682   if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4683       || ik->is_abstract() || ik->is_interface()
4684       || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4685       || ik->size_helper() >= FastAllocateSizeLimit) {
4686     // Forbid fast-path allocation.
4687     const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4688     ik->set_layout_helper(lh);
4689   }
4690 }
4691 
4692 bool ClassFileParser::supports_value_types() const {
4693   // Value types are only supported by class file version 55 and later
4694   return _major_version >= JAVA_11_VERSION;
4695 }
4696 
4697 // utility methods for appending an array with check for duplicates
4698 
4699 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4700                               const Array<InstanceKlass*>* const ifs) {
4701   // iterate over new interfaces
4702   for (int i = 0; i < ifs->length(); i++) {
4703     InstanceKlass* const e = ifs->at(i);
4704     assert(e->is_klass() && e->is_interface(), "just checking");
4705     // add new interface
4706     result->append_if_missing(e);
4707   }
4708 }
4709 
4710 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4711                                                             Array<InstanceKlass*>* local_ifs,
4712                                                             ClassLoaderData* loader_data,
4713                                                             TRAPS) {
4714   assert(local_ifs != NULL, "invariant");
4715   assert(loader_data != NULL, "invariant");
4716 


4939     const Method* const m = methods->at(index);
4940     // if m is static and not the init method, throw a verify error
4941     if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4942       ResourceMark rm(THREAD);
4943       Exceptions::fthrow(
4944         THREAD_AND_LOCATION,
4945         vmSymbols::java_lang_VerifyError(),
4946         "Illegal static method %s in interface %s",
4947         m->name()->as_C_string(),
4948         this_klass->external_name()
4949       );
4950       return;
4951     }
4952   }
4953 }
4954 
4955 // utility methods for format checking
4956 
4957 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4958   const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4959   const bool is_value_type = (flags & JVM_ACC_VALUE) != 0;
4960   assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4961   assert(supports_value_types() || !is_value_type, "JVM_ACC_VALUE should not be set");
4962   if (is_module) {
4963     ResourceMark rm(THREAD);
4964     Exceptions::fthrow(
4965       THREAD_AND_LOCATION,
4966       vmSymbols::java_lang_NoClassDefFoundError(),
4967       "%s is not a class because access_flag ACC_MODULE is set",
4968       _class_name->as_C_string());
4969     return;
4970   }
4971 
4972   if (is_value_type && !EnableValhalla) {
4973     ResourceMark rm(THREAD);
4974     Exceptions::fthrow(
4975       THREAD_AND_LOCATION,
4976       vmSymbols::java_lang_ClassFormatError(),
4977       "Class modifier ACC_VALUE in class %s requires option -XX:+EnableValhalla",
4978       _class_name->as_C_string()
4979     );
4980   }
4981 
4982   if (!_need_verify) { return; }
4983 
4984   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4985   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4986   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4987   const bool is_super      = (flags & JVM_ACC_SUPER)      != 0;
4988   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4989   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4990   const bool major_gte_15  = _major_version >= JAVA_1_5_VERSION;
4991 
4992   if ((is_abstract && is_final) ||
4993       (is_interface && !is_abstract) ||
4994       (is_interface && major_gte_15 && (is_super || is_enum)) ||
4995       (!is_interface && major_gte_15 && is_annotation) ||
4996       (is_value_type && (is_interface || is_abstract || is_enum || !is_final))) {
4997     ResourceMark rm(THREAD);
4998     const char* class_note = "";
4999     if (is_value_type)  class_note = " (an inline class)";
5000     Exceptions::fthrow(
5001       THREAD_AND_LOCATION,
5002       vmSymbols::java_lang_ClassFormatError(),
5003       "Illegal class modifiers in class %s%s: 0x%X",
5004       _class_name->as_C_string(), class_note, flags
5005     );
5006     return;
5007   }
5008 }
5009 
5010 static bool has_illegal_visibility(jint flags) {
5011   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
5012   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
5013   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
5014 
5015   return ((is_public && is_protected) ||
5016           (is_public && is_private) ||
5017           (is_protected && is_private));
5018 }
5019 
5020 // A legal major_version.minor_version must be one of the following:
5021 //
5022 //  Major_version >= 45 and major_version < 56, any minor_version.
5023 //  Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
5024 //  Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.


5063     if (!Arguments::enable_preview()) {
5064       Exceptions::fthrow(
5065         THREAD_AND_LOCATION,
5066         vmSymbols::java_lang_UnsupportedClassVersionError(),
5067         "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
5068         class_name->as_C_string(), major, minor);
5069       return;
5070     }
5071 
5072   } else { // minor != JAVA_PREVIEW_MINOR_VERSION
5073     Exceptions::fthrow(
5074         THREAD_AND_LOCATION,
5075         vmSymbols::java_lang_UnsupportedClassVersionError(),
5076         "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
5077         class_name->as_C_string(), major, minor);
5078   }
5079 }
5080 
5081 void ClassFileParser::verify_legal_field_modifiers(jint flags,
5082                                                    bool is_interface,
5083                                                    bool is_value_type,
5084                                                    TRAPS) const {
5085   if (!_need_verify) { return; }
5086 
5087   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
5088   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
5089   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
5090   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
5091   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
5092   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
5093   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
5094   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
5095   const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
5096 
5097   bool is_illegal = false;
5098 
5099   if (is_interface) {
5100     if (!is_public || !is_static || !is_final || is_private ||
5101         is_protected || is_volatile || is_transient ||
5102         (major_gte_15 && is_enum)) {
5103       is_illegal = true;
5104     }
5105   } else { // not interface
5106     if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
5107       is_illegal = true;
5108     } else {
5109       if (is_value_type && !is_static && !is_final) {
5110         is_illegal = true;
5111       }
5112     }
5113   }
5114 
5115   if (is_illegal) {
5116     ResourceMark rm(THREAD);
5117     Exceptions::fthrow(
5118       THREAD_AND_LOCATION,
5119       vmSymbols::java_lang_ClassFormatError(),
5120       "Illegal field modifiers in class %s: 0x%X",
5121       _class_name->as_C_string(), flags);
5122     return;
5123   }
5124 }
5125 
5126 void ClassFileParser::verify_legal_method_modifiers(jint flags,
5127                                                     bool is_interface,
5128                                                     bool is_value_type,
5129                                                     const Symbol* name,
5130                                                     TRAPS) const {
5131   if (!_need_verify) { return; }
5132 
5133   const bool is_public       = (flags & JVM_ACC_PUBLIC)       != 0;
5134   const bool is_private      = (flags & JVM_ACC_PRIVATE)      != 0;
5135   const bool is_static       = (flags & JVM_ACC_STATIC)       != 0;
5136   const bool is_final        = (flags & JVM_ACC_FINAL)        != 0;
5137   const bool is_native       = (flags & JVM_ACC_NATIVE)       != 0;
5138   const bool is_abstract     = (flags & JVM_ACC_ABSTRACT)     != 0;
5139   const bool is_bridge       = (flags & JVM_ACC_BRIDGE)       != 0;
5140   const bool is_strict       = (flags & JVM_ACC_STRICT)       != 0;
5141   const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
5142   const bool is_protected    = (flags & JVM_ACC_PROTECTED)    != 0;
5143   const bool major_gte_15    = _major_version >= JAVA_1_5_VERSION;
5144   const bool major_gte_8     = _major_version >= JAVA_8_VERSION;
5145   const bool is_initializer  = (name == vmSymbols::object_initializer_name());
5146 
5147   bool is_illegal = false;
5148 
5149   const char* class_note = "";
5150 
5151   if (is_interface) {
5152     if (major_gte_8) {
5153       // Class file version is JAVA_8_VERSION or later Methods of
5154       // interfaces may set any of the flags except ACC_PROTECTED,
5155       // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
5156       // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
5157       if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
5158           (is_native || is_protected || is_final || is_synchronized) ||
5159           // If a specific method of a class or interface has its
5160           // ACC_ABSTRACT flag set, it must not have any of its
5161           // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
5162           // ACC_STRICT, or ACC_SYNCHRONIZED flags set.  No need to
5163           // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
5164           // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
5165           (is_abstract && (is_private || is_static || is_strict))) {
5166         is_illegal = true;
5167       }
5168     } else if (major_gte_15) {
5169       // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
5170       if (!is_public || is_private || is_protected || is_static || is_final ||
5171           is_synchronized || is_native || !is_abstract || is_strict) {
5172         is_illegal = true;
5173       }
5174     } else {
5175       // Class file version is pre-JAVA_1_5_VERSION
5176       if (!is_public || is_static || is_final || is_native || !is_abstract) {
5177         is_illegal = true;
5178       }
5179     }
5180   } else { // not interface
5181     if (has_illegal_visibility(flags)) {
5182       is_illegal = true;
5183     } else {
5184       if (is_initializer) {
5185         if (is_final || is_synchronized || is_native ||
5186             is_abstract || (major_gte_15 && is_bridge)) {
5187           is_illegal = true;
5188         }
5189         if (!is_static && !is_value_type) {
5190           // OK, an object constructor in a regular class
5191         } else if (is_static && is_value_type) {
5192           // OK, a static init factory in an inline class
5193         } else {
5194           // but no other combinations are allowed
5195           is_illegal = true;
5196           class_note = (is_value_type ? " (an inline class)" : " (not an inline class)");
5197         }
5198       } else { // not initializer
5199         if (is_value_type && is_synchronized && !is_static) {
5200           is_illegal = true;
5201           class_note = " (an inline class)";
5202         } else {
5203           if (is_abstract) {
5204             if ((is_final || is_native || is_private || is_static ||
5205                 (major_gte_15 && (is_synchronized || is_strict)))) {
5206               is_illegal = true;
5207             }
5208           }
5209         }
5210       }
5211     }
5212   }
5213 
5214   if (is_illegal) {
5215     ResourceMark rm(THREAD);
5216     Exceptions::fthrow(
5217       THREAD_AND_LOCATION,
5218       vmSymbols::java_lang_ClassFormatError(),
5219       "Method %s in class %s%s has illegal modifiers: 0x%X",
5220       name->as_C_string(), _class_name->as_C_string(), class_note, flags);
5221     return;
5222   }
5223 }
5224 
5225 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
5226                                         int length,
5227                                         TRAPS) const {
5228   assert(_need_verify, "only called when _need_verify is true");
5229   if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
5230     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
5231   }
5232 }
5233 
5234 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
5235 // In class names, '/' separates unqualified names.  This is verified in this function also.
5236 // Method names also may not contain the characters '<' or '>', unless <init>
5237 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
5238 // method.  Because these names have been checked as special cases before
5239 // calling this method in verify_legal_method_name.
5240 //


5357 // be taken as a field signature. Allow "void" if void_ok.
5358 // Return a pointer to just past the signature.
5359 // Return NULL if no legal signature is found.
5360 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5361                                                        bool void_ok,
5362                                                        unsigned int length,
5363                                                        TRAPS) const {
5364   unsigned int array_dim = 0;
5365   while (length > 0) {
5366     switch (signature[0]) {
5367     case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
5368     case JVM_SIGNATURE_BOOLEAN:
5369     case JVM_SIGNATURE_BYTE:
5370     case JVM_SIGNATURE_CHAR:
5371     case JVM_SIGNATURE_SHORT:
5372     case JVM_SIGNATURE_INT:
5373     case JVM_SIGNATURE_FLOAT:
5374     case JVM_SIGNATURE_LONG:
5375     case JVM_SIGNATURE_DOUBLE:
5376       return signature + 1;
5377     case JVM_SIGNATURE_VALUETYPE:
5378       // Can't enable this check until JDK upgrades the bytecode generators
5379       // if (_major_version < CONSTANT_CLASS_DESCRIPTORS ) {
5380       //   classfile_parse_error("Class name contains illegal Q-signature "
5381       //                                    "in descriptor in class file %s",
5382       //                                    CHECK_0);
5383       // }
5384       // fall through
5385     case JVM_SIGNATURE_CLASS:
5386     {
5387       if (_major_version < JAVA_1_5_VERSION) {
5388         // Skip over the class name if one is there
5389         const char* const p = skip_over_field_name(signature + 1, true, --length);
5390 
5391         // The next character better be a semicolon
5392         if (p && (p - signature) > 1 && p[0] == ';') {
5393           return p + 1;
5394         }
5395       }
5396       else {
5397         // Skip leading 'L' or 'Q' and ignore first appearance of ';'
5398         signature++;
5399         const char* c = (const char*) memchr(signature, ';', length - 1);
5400         // Format check signature
5401         if (c != NULL) {
5402           int newlen = c - (char*) signature;
5403           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5404           if (!legal) {
5405             classfile_parse_error("Class name is empty or contains illegal character "
5406                                   "in descriptor in class file %s",
5407                                   CHECK_0);
5408             return NULL;
5409           }
5410           return signature + newlen + 1;
5411         }
5412       }
5413       return NULL;
5414     }
5415     case JVM_SIGNATURE_ARRAY:
5416       array_dim++;
5417       if (array_dim > 255) {


5432 
5433 // Checks if name is a legal class name.
5434 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5435   if (!_need_verify || _relax_verify) { return; }
5436 
5437   assert(name->refcount() > 0, "symbol must be kept alive");
5438   char* bytes = (char*)name->bytes();
5439   unsigned int length = name->utf8_length();
5440   bool legal = false;
5441 
5442   if (length > 0) {
5443     const char* p;
5444     if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5445       p = skip_over_field_signature(bytes, false, length, CHECK);
5446       legal = (p != NULL) && ((p - bytes) == (int)length);
5447     } else if (_major_version < JAVA_1_5_VERSION) {
5448       if (bytes[0] != '<') {
5449         p = skip_over_field_name(bytes, true, length);
5450         legal = (p != NULL) && ((p - bytes) == (int)length);
5451       }
5452     } else if (_major_version >= CONSTANT_CLASS_DESCRIPTORS && bytes[length - 1] == ';' ) {
5453       // Support for L...; and Q...; descriptors
5454       legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
5455     } else {
5456       // 4900761: relax the constraints based on JSR202 spec
5457       // Class names may be drawn from the entire Unicode character set.
5458       // Identifiers between '/' must be unqualified names.
5459       // The utf8 string has been verified when parsing cpool entries.
5460       legal = verify_unqualified_name(bytes, length, LegalClass);
5461     }
5462   }
5463   if (!legal) {
5464     ResourceMark rm(THREAD);
5465     assert(_class_name != NULL, "invariant");
5466     Exceptions::fthrow(
5467       THREAD_AND_LOCATION,
5468       vmSymbols::java_lang_ClassFormatError(),
5469       "Illegal class name \"%.*s\" in class file %s", length, bytes,
5470       _class_name->as_C_string()
5471     );
5472     return;
5473   }
5474 }


5584   const char* nextp;
5585 
5586   // The first character must be a '('
5587   if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) {
5588     length--;
5589     // Skip over legal field signatures
5590     nextp = skip_over_field_signature(p, false, length, CHECK_0);
5591     while ((length > 0) && (nextp != NULL)) {
5592       args_size++;
5593       if (p[0] == 'J' || p[0] == 'D') {
5594         args_size++;
5595       }
5596       length -= nextp - p;
5597       p = nextp;
5598       nextp = skip_over_field_signature(p, false, length, CHECK_0);
5599     }
5600     // The first non-signature thing better be a ')'
5601     if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
5602       length--;
5603       if (name->utf8_length() > 0 && name->char_at(0) == '<') {
5604         // All constructor methods must return void
5605         if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {
5606           return args_size;
5607         }
5608         // All static init methods must return the current class
5609         if ((length >= 3) && (p[length-1] == JVM_SIGNATURE_ENDCLASS)
5610             && name == vmSymbols::object_initializer_name()) {
5611           nextp = skip_over_field_signature(p, true, length, CHECK_0);
5612           if (nextp && ((int)length == (nextp - p))) {
5613             // The actual class will be checked against current class
5614             // when the method is defined (see parse_method).
5615             // A reference to a static init with a bad return type
5616             // will load and verify OK, but will fail to link.
5617             return args_size;
5618           }
5619         }
5620         // The distinction between static factory methods and
5621         // constructors depends on the JVM_ACC_STATIC modifier.
5622         // This distinction must be reflected in a void or non-void
5623         // return. For declared methods, the check is in parse_method.
5624       } else {
5625         // Now we better just have a return value
5626         nextp = skip_over_field_signature(p, true, length, CHECK_0);
5627         if (nextp && ((int)length == (nextp - p))) {
5628           return args_size;
5629         }
5630       }
5631     }
5632   }
5633   // Report error
5634   throwIllegalSignature("Method", name, signature, CHECK_0);
5635   return 0;
5636 }
5637 
5638 int ClassFileParser::static_field_size() const {
5639   assert(_field_info != NULL, "invariant");
5640   return _field_info->static_field_size;
5641 }
5642 
5643 int ClassFileParser::total_oop_map_count() const {
5644   assert(_field_info != NULL, "invariant");
5645   return _field_info->oop_map_blocks->nonstatic_oop_map_count;
5646 }
5647 
5648 jint ClassFileParser::layout_size() const {
5649   assert(_field_info != NULL, "invariant");
5650   return _field_info->instance_size;
5651 }
5652 
5653 static void check_methods_for_intrinsics(const InstanceKlass* ik,
5654                                          const Array<Method*>* methods) {
5655   assert(ik != NULL, "invariant");
5656   assert(methods != NULL, "invariant");
5657 
5658   // Set up Method*::intrinsic_id as soon as we know the names of methods.
5659   // (We used to do this lazily, but now we query it in Rewriter,
5660   // which is eagerly done for every method, so we might as well do it now,
5661   // when everything is fresh in memory.)
5662   const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik);
5663 
5664   if (klass_id != vmSymbols::NO_SID) {
5665     for (int j = 0; j < methods->length(); ++j) {


5755 
5756 
5757   if (ik->should_store_fingerprint()) {
5758     ik->store_fingerprint(_stream->compute_fingerprint());
5759   }
5760 
5761   ik->set_has_passed_fingerprint_check(false);
5762   if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5763     uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5764     uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5765     if (aot_fp != 0 && aot_fp == fp) {
5766       // This class matches with a class saved in an AOT library
5767       ik->set_has_passed_fingerprint_check(true);
5768     } else {
5769       ResourceMark rm;
5770       log_info(class, fingerprint)("%s :  expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5771                                  ik->external_name(), aot_fp, _stream->compute_fingerprint());
5772     }
5773   }
5774 
5775   if (ik->is_value()) {
5776     ValueKlass* vk = ValueKlass::cast(ik);
5777     oop val = ik->allocate_instance(CHECK_NULL);
5778     vk->set_default_value(val);
5779   }
5780 
5781   return ik;
5782 }
5783 
5784 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) {
5785   assert(ik != NULL, "invariant");
5786 
5787   // Set name and CLD before adding to CLD
5788   ik->set_class_loader_data(_loader_data);
5789   ik->set_name(_class_name);
5790 
5791   // Add all classes to our internal class loader list here,
5792   // including classes in the bootstrap (NULL) class loader.
5793   const bool publicize = !is_internal();
5794 
5795   _loader_data->add_class(ik, publicize);
5796 
5797   set_klass_to_deallocate(ik);
5798 
5799   assert(_field_info != NULL, "invariant");
5800   assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5801   assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->nonstatic_oop_map_count,
5802     "sanity");
5803 
5804   assert(ik->is_instance_klass(), "sanity");
5805   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5806 
5807   // Fill in information already parsed
5808   ik->set_should_verify_class(_need_verify);
5809 
5810   // Not yet: supers are done below to support the new subtype-checking fields
5811   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5812   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5813   if (_is_empty_value) {
5814     ik->set_is_empty_value();
5815   }
5816   assert(_fac != NULL, "invariant");
5817   ik->set_static_oop_field_count(_fac->count[STATIC_OOP] + _fac->count[STATIC_FLATTENABLE]);
5818 
5819   // this transfers ownership of a lot of arrays from
5820   // the parser onto the InstanceKlass*
5821   apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5822 
5823   // note that is not safe to use the fields in the parser from this point on
5824   assert(NULL == _cp, "invariant");
5825   assert(NULL == _fields, "invariant");
5826   assert(NULL == _methods, "invariant");
5827   assert(NULL == _inner_classes, "invariant");
5828   assert(NULL == _nest_members, "invariant");
5829   assert(NULL == _local_interfaces, "invariant");
5830   assert(NULL == _combined_annotations, "invariant");
5831 
5832   if (_has_final_method) {
5833     ik->set_has_final_method();
5834   }
5835 
5836   ik->copy_method_ordering(_method_ordering, CHECK);
5837   // The InstanceKlass::_methods_jmethod_ids cache


5885 
5886   // Miranda methods
5887   if ((_num_miranda_methods > 0) ||
5888       // if this class introduced new miranda methods or
5889       (_super_klass != NULL && _super_klass->has_miranda_methods())
5890         // super class exists and this class inherited miranda methods
5891      ) {
5892        ik->set_has_miranda_methods(); // then set a flag
5893   }
5894 
5895   // Fill in information needed to compute superclasses.
5896   ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5897   ik->set_transitive_interfaces(_transitive_interfaces);
5898   _transitive_interfaces = NULL;
5899 
5900   // Initialize itable offset tables
5901   klassItable::setup_itable_offset_table(ik);
5902 
5903   // Compute transitive closure of interfaces this class implements
5904   // Do final class setup
5905   OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks;
5906   if (oop_map_blocks->nonstatic_oop_map_count > 0) {
5907     oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5908   }
5909 
5910   // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5911   set_precomputed_flags(ik, CHECK);
5912 
5913   // check if this class can access its super class
5914   check_super_class_access(ik, CHECK);
5915 
5916   // check if this class can access its superinterfaces
5917   check_super_interface_access(ik, CHECK);
5918 
5919   // check if this class overrides any final method
5920   check_final_method_override(ik, CHECK);
5921 
5922   // reject static interface methods prior to Java 8
5923   if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5924     check_illegal_static_method(ik, CHECK);
5925   }
5926 
5927   // Obtain this_klass' module entry
5928   ModuleEntry* module_entry = ik->module();
5929   assert(module_entry != NULL, "module_entry should always be set");
5930 
5931   // Obtain java.lang.Module


5941 
5942   assert(_all_mirandas != NULL, "invariant");
5943 
5944   // Generate any default methods - default methods are public interface methods
5945   // that have a default implementation.  This is new with Java 8.
5946   if (_has_nonstatic_concrete_methods) {
5947     DefaultMethods::generate_default_methods(ik,
5948                                              _all_mirandas,
5949                                              CHECK);
5950   }
5951 
5952   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5953   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5954       !module_entry->has_default_read_edges()) {
5955     if (!module_entry->set_has_default_read_edges()) {
5956       // We won a potential race
5957       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5958     }
5959   }
5960 
5961   int nfields = ik->java_fields_count();
5962   if (ik->is_value()) nfields++;
5963   for (int i = 0; i < nfields; i++) {
5964     if (ik->field_is_flattenable(i)) {
5965       Symbol* klass_name = ik->field_signature(i)->fundamental_name(CHECK);
5966       // Inline classes for instance fields must have been pre-loaded
5967       // Inline classes for static fields might not have been loaded yet
5968       Klass* klass = SystemDictionary::find(klass_name,
5969           Handle(THREAD, ik->class_loader()),
5970           Handle(THREAD, ik->protection_domain()), CHECK);
5971       if (klass != NULL) {
5972         assert(klass->access_flags().is_value_type(), "Value type expected");
5973         ik->set_value_field_klass(i, klass);
5974       }
5975       klass_name->decrement_refcount();
5976     } else
5977       if (is_value_type() && ((ik->field_access_flags(i) & JVM_ACC_FIELD_INTERNAL) != 0)
5978         && ((ik->field_access_flags(i) & JVM_ACC_STATIC) != 0)) {
5979       ValueKlass::cast(ik)->set_default_value_offset(ik->field_offset(i));
5980     }
5981   }
5982 
5983   if (is_value_type()) {
5984     ValueKlass* vk = ValueKlass::cast(ik);
5985     if (UseNewLayout) {
5986       vk->set_alignment(_alignment);
5987       vk->set_first_field_offset(_first_field_offset);
5988       vk->set_exact_size_in_bytes(_exact_size_in_bytes);
5989     } else {
5990       vk->set_first_field_offset(vk->first_field_offset_old());
5991     }
5992     ValueKlass::cast(ik)->initialize_calling_convention(CHECK);
5993   }
5994 
5995   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5996 
5997   if (!is_internal()) {
5998     if (log_is_enabled(Info, class, load)) {
5999       ResourceMark rm;
6000       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
6001       ik->print_class_load_logging(_loader_data, module_name, _stream);
6002     }
6003 
6004     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
6005         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
6006         log_is_enabled(Info, class, preview)) {
6007       ResourceMark rm;
6008       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
6009                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
6010     }
6011 
6012     if (log_is_enabled(Debug, class, resolve))  {
6013       ResourceMark rm;
6014       // print out the superclass.


6164   _protection_domain(protection_domain),
6165   _access_flags(),
6166   _pub_level(pub_level),
6167   _bad_constant_seen(0),
6168   _synthetic_flag(false),
6169   _sde_length(false),
6170   _sde_buffer(NULL),
6171   _sourcefile_index(0),
6172   _generic_signature_index(0),
6173   _major_version(0),
6174   _minor_version(0),
6175   _this_class_index(0),
6176   _super_class_index(0),
6177   _itfs_len(0),
6178   _java_fields_count(0),
6179   _need_verify(false),
6180   _relax_verify(false),
6181   _has_nonstatic_concrete_methods(false),
6182   _declares_nonstatic_concrete_methods(false),
6183   _has_final_method(false),
6184   _has_flattenable_fields(false),
6185   _is_empty_value(false),
6186   _has_finalizer(false),
6187   _has_empty_finalizer(false),
6188   _has_vanilla_constructor(false),
6189   _max_bootstrap_specifier_index(-1) {
6190 
6191   _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
6192   _class_name->increment_refcount();
6193 
6194   assert(THREAD->is_Java_thread(), "invariant");
6195   assert(_loader_data != NULL, "invariant");
6196   assert(stream != NULL, "invariant");
6197   assert(_stream != NULL, "invariant");
6198   assert(_stream->buffer() == _stream->current(), "invariant");
6199   assert(_class_name != NULL, "invariant");
6200   assert(0 == _access_flags.as_int(), "invariant");
6201 
6202   // Figure out whether we can skip format checking (matching classic VM behavior)
6203   if (DumpSharedSpaces) {
6204     // verify == true means it's a 'remote' class (i.e., non-boot class)
6205     // Verification decision is based on BytecodeVerificationRemote flag


6361 
6362   _orig_cp_size = cp_size;
6363   if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
6364     THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
6365   }
6366   cp_size += _max_num_patched_klasses;
6367 
6368   _cp = ConstantPool::allocate(_loader_data,
6369                                cp_size,
6370                                CHECK);
6371 
6372   ConstantPool* const cp = _cp;
6373 
6374   parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6375 
6376   assert(cp_size == (const u2)cp->length(), "invariant");
6377 
6378   // ACCESS FLAGS
6379   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
6380 
6381   jint recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS;

6382   // JVM_ACC_MODULE is defined in JDK-9 and later.
6383   if (_major_version >= JAVA_9_VERSION) {
6384     recognized_modifiers |= JVM_ACC_MODULE;
6385   }
6386   // JVM_ACC_VALUE is defined for class file version 55 and later
6387   if (supports_value_types()) {
6388     recognized_modifiers |= JVM_ACC_VALUE;
6389   }
6390 
6391   // Access flags
6392   jint flags = stream->get_u2_fast() & recognized_modifiers;
6393 
6394   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6395     // Set abstract bit for old class files for backward compatibility
6396     flags |= JVM_ACC_ABSTRACT;
6397   }
6398 
6399   verify_legal_class_modifiers(flags, CHECK);
6400 
6401   short bad_constant = class_bad_constant_seen();
6402   if (bad_constant != 0) {
6403     // Do not throw CFE until after the access_flags are checked because if
6404     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6405     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6406   }
6407 
6408   _access_flags.set_flags(flags);
6409 
6410   // This class and superclass
6411   _this_class_index = stream->get_u2_fast();
6412   check_property(
6413     valid_cp_range(_this_class_index, cp_size) &&


6508   _super_class_index = stream->get_u2_fast();
6509   _super_klass = parse_super_class(cp,
6510                                    _super_class_index,
6511                                    _need_verify,
6512                                    CHECK);
6513 
6514   // Interfaces
6515   _itfs_len = stream->get_u2_fast();
6516   parse_interfaces(stream,
6517                    _itfs_len,
6518                    cp,
6519                    &_has_nonstatic_concrete_methods,
6520                    CHECK);
6521 
6522   assert(_local_interfaces != NULL, "invariant");
6523 
6524   // Fields (offsets are filled in later)
6525   _fac = new FieldAllocationCount();
6526   parse_fields(stream,
6527                _access_flags.is_interface(),
6528                _access_flags.is_value_type(),
6529                _fac,
6530                cp,
6531                cp_size,
6532                &_java_fields_count,
6533                CHECK);
6534 
6535   assert(_fields != NULL, "invariant");
6536 
6537   // Methods
6538   AccessFlags promoted_flags;
6539   parse_methods(stream,
6540                 _access_flags.is_interface(),
6541                 _access_flags.is_value_type(),
6542                 &promoted_flags,
6543                 &_has_final_method,
6544                 &_declares_nonstatic_concrete_methods,
6545                 CHECK);
6546 
6547   assert(_methods != NULL, "invariant");
6548 
6549   // promote flags from parse_methods() to the klass' flags
6550   _access_flags.add_promoted_flags(promoted_flags.as_int());
6551 
6552   if (_declares_nonstatic_concrete_methods) {
6553     _has_nonstatic_concrete_methods = true;
6554   }
6555 
6556   // Additional attributes/annotations
6557   _parsed_annotations = new ClassAnnotationCollector();
6558   parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6559 
6560   assert(_inner_classes != NULL, "invariant");
6561 


6603                                                                true,
6604                                                                CHECK);
6605   }
6606 
6607   if (_super_klass != NULL) {
6608     if (_super_klass->has_nonstatic_concrete_methods()) {
6609       _has_nonstatic_concrete_methods = true;
6610     }
6611 
6612     if (_super_klass->is_interface()) {
6613       ResourceMark rm(THREAD);
6614       Exceptions::fthrow(
6615         THREAD_AND_LOCATION,
6616         vmSymbols::java_lang_IncompatibleClassChangeError(),
6617         "class %s has interface %s as super class",
6618         _class_name->as_klass_external_name(),
6619         _super_klass->external_name()
6620       );
6621       return;
6622     }
6623 
6624     // For a value class, only java/lang/Object is an acceptable super class
6625     if (_access_flags.get_flags() & JVM_ACC_VALUE) {
6626       guarantee_property(_super_klass->name() == vmSymbols::java_lang_Object(),
6627         "Value type must have java.lang.Object as superclass in class file %s",
6628         CHECK);
6629     }
6630 
6631     // Make sure super class is not final
6632     if (_super_klass->is_final()) {
6633       THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class");
6634     }
6635   }
6636 
6637   // Compute the transitive list of all unique interfaces implemented by this class
6638   _transitive_interfaces =
6639     compute_transitive_interfaces(_super_klass,
6640                                   _local_interfaces,
6641                                   _loader_data,
6642                                   CHECK);
6643 
6644   assert(_transitive_interfaces != NULL, "invariant");
6645 
6646   // sort methods
6647   _method_ordering = sort_methods(_methods);
6648 
6649   _all_mirandas = new GrowableArray<Method*>(20);
6650 
6651   Handle loader(THREAD, _loader_data->class_loader());
6652   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6653                                                     &_num_miranda_methods,
6654                                                     _all_mirandas,
6655                                                     _super_klass,
6656                                                     _methods,
6657                                                     _access_flags,
6658                                                     _major_version,
6659                                                     loader,
6660                                                     _class_name,
6661                                                     _local_interfaces,
6662                                                     CHECK);
6663 
6664   // Size of Java itable (in words)
6665   _itable_size = _access_flags.is_interface() ? 0 :
6666     klassItable::compute_itable_size(_transitive_interfaces);
6667 
6668   assert(_fac != NULL, "invariant");
6669   assert(_parsed_annotations != NULL, "invariant");
6670 
6671 
6672   for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
6673     if (fs.is_flattenable() && !fs.access_flags().is_static()) {
6674       // Pre-load value class
6675       Klass* klass = SystemDictionary::resolve_flattenable_field_or_fail(&fs,
6676           Handle(THREAD, _loader_data->class_loader()),
6677           _protection_domain, true, CHECK);
6678       assert(klass != NULL, "Sanity check");
6679       assert(klass->access_flags().is_value_type(), "Value type expected");
6680       _has_flattenable_fields = true;
6681     }
6682   }
6683 
6684   _field_info = new FieldLayoutInfo();
6685   if (UseNewLayout) {
6686     FieldLayoutBuilder lb(this, _field_info);
6687     if (this->is_value_type()) {
6688       lb.compute_inline_class_layout(CHECK);
6689       _alignment = lb.get_alignment();
6690       _first_field_offset = lb.get_first_field_offset();
6691       _exact_size_in_bytes = lb.get_exact_size_in_byte();
6692     } else {
6693       lb.compute_regular_layout(CHECK);
6694     }
6695   } else {
6696     layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);
6697   }
6698 
6699   // Compute reference type
6700   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6701 
6702 }
6703 
6704 void ClassFileParser::set_klass(InstanceKlass* klass) {
6705 
6706 #ifdef ASSERT
6707   if (klass != NULL) {
6708     assert(NULL == _klass, "leaking?");
6709   }
6710 #endif
6711 
6712   _klass = klass;
6713 }
6714 
6715 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6716 
6717 #ifdef ASSERT
6718   if (klass != NULL) {
6719     assert(NULL == _klass_to_deallocate, "leaking?");
6720   }
6721 #endif
6722 
6723   _klass_to_deallocate = klass;
6724 }
6725 
6726 // Caller responsible for ResourceMark
6727 // clone stream with rewound position
6728 const ClassFileStream* ClassFileParser::clone_stream() const {
6729   assert(_stream != NULL, "invariant");
6730 
6731   return _stream->clone();
6732 }
6733 
6734 // ----------------------------------------------------------------------------
6735 // debugging
6736 
6737 #ifdef ASSERT
6738 
6739 // return true if class_name contains no '.' (internal format is '/')
6740 bool ClassFileParser::is_internal_format(Symbol* class_name) {
6741   if (class_name != NULL) {
6742     ResourceMark rm;
6743     char* name = class_name->as_C_string();
6744     return strchr(name, '.') == NULL;
6745   } else {
6746     return true;
6747   }
6748 }
6749 
6750 #endif
< prev index next >