< 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 "classfile/classFileParser.hpp"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/classLoaderData.inline.hpp"
  30 #include "classfile/classLoadInfo.hpp"
  31 #include "classfile/defaultMethods.hpp"
  32 #include "classfile/fieldLayoutBuilder.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/vmClasses.hpp"
  41 #include "classfile/vmSymbols.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/allocation.hpp"
  45 #include "memory/metadataFactory.hpp"
  46 #include "memory/oopFactory.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "memory/universe.hpp"
  49 #include "oops/annotations.hpp"
  50 #include "oops/constantPool.inline.hpp"
  51 #include "oops/fieldStreams.inline.hpp"

  52 #include "oops/instanceKlass.inline.hpp"
  53 #include "oops/instanceMirrorKlass.hpp"
  54 #include "oops/klass.inline.hpp"
  55 #include "oops/klassVtable.hpp"
  56 #include "oops/metadata.hpp"
  57 #include "oops/method.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/recordComponent.hpp"
  60 #include "oops/symbol.hpp"
  61 #include "prims/jvmtiExport.hpp"
  62 #include "prims/jvmtiThreadState.hpp"
  63 #include "runtime/arguments.hpp"
  64 #include "runtime/fieldDescriptor.inline.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/javaCalls.hpp"
  67 #include "runtime/os.hpp"
  68 #include "runtime/perfData.hpp"
  69 #include "runtime/reflection.hpp"
  70 #include "runtime/safepointVerifiers.hpp"
  71 #include "runtime/signature.hpp"
  72 #include "runtime/timer.hpp"
  73 #include "services/classLoadingService.hpp"
  74 #include "services/threadService.hpp"
  75 #include "utilities/align.hpp"
  76 #include "utilities/bitMap.inline.hpp"
  77 #include "utilities/copy.hpp"
  78 #include "utilities/formatBuffer.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"
  86 
  87 #if INCLUDE_CDS
  88 #include "classfile/systemDictionaryShared.hpp"
  89 #endif
  90 #if INCLUDE_JFR
  91 #include "jfr/support/jfrTraceIdExtension.hpp"
  92 #endif
  93 
  94 // We generally try to create the oops directly when parsing, rather than
  95 // allocating temporary data structures and copying the bytes twice. A
  96 // temporary area is only needed when parsing utf8 entries in the constant
  97 // pool and when parsing line number tables.
  98 
  99 // We add assert in debug mode when class format is not checked.
 100 
 101 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
 102 #define JAVA_MIN_SUPPORTED_VERSION        45
 103 #define JAVA_PREVIEW_MINOR_VERSION        65535
 104 

 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 JAVA_15_VERSION                   59
 135 
 136 #define JAVA_16_VERSION                   60
 137 
 138 #define JAVA_17_VERSION                   61
 139 
 140 #define JAVA_18_VERSION                   62
 141 



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

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







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

 682           cp->signature_ref_index_at(name_and_type_ref_index);
 683         const Symbol* const name = cp->symbol_at(name_ref_index);
 684         const Symbol* const signature = cp->symbol_at(signature_ref_index);
 685         if (tag == JVM_CONSTANT_Fieldref) {
 686           if (_need_verify) {
 687             // Field name and signature are verified above, when iterating NameAndType_info.
 688             // Need only to be sure signature is non-zero length and the right type.
 689             if (Signature::is_method(signature)) {
 690               throwIllegalSignature("Field", name, signature, CHECK);
 691             }
 692           }
 693         } else {
 694           if (_need_verify) {
 695             // Method name and signature are individually verified above, when iterating
 696             // NameAndType_info.  Need to check here that signature is non-zero length and
 697             // the right type.
 698             if (!Signature::is_method(signature)) {
 699               throwIllegalSignature("Method", name, signature, CHECK);
 700             }
 701           }
 702           // If a class method name begins with '<', it must be "<init>" and have void signature.

 703           const unsigned int name_len = name->utf8_length();
 704           if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
 705               name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
 706             if (name != vmSymbols::object_initializer_name()) {
 707               classfile_parse_error(
 708                 "Bad method name at constant pool index %u in class file %s",
 709                 name_ref_index, THREAD);
 710               return;
 711             } else if (!Signature::is_void_method(signature)) { // must have void signature.
 712               throwIllegalSignature("Method", name, signature, CHECK);




 713             }
 714           }
 715         }
 716         break;
 717       }
 718       case JVM_CONSTANT_MethodHandle: {
 719         const int ref_index = cp->method_handle_index_at(index);
 720         const int ref_kind = cp->method_handle_ref_kind_at(index);
 721         switch (ref_kind) {
 722           case JVM_REF_invokeVirtual:
 723           case JVM_REF_invokeStatic:
 724           case JVM_REF_invokeSpecial:
 725           case JVM_REF_newInvokeSpecial: {
 726             const int name_and_type_ref_index =
 727               cp->name_and_type_ref_index_at(ref_index);
 728             const int name_ref_index =
 729               cp->name_ref_index_at(name_and_type_ref_index);
 730             const Symbol* const name = cp->symbol_at(name_ref_index);
 731             if (ref_kind == JVM_REF_newInvokeSpecial) {
 732               if (name != vmSymbols::object_initializer_name()) {
 733                 classfile_parse_error(
 734                   "Bad constructor name at constant pool index %u in class file %s",
 735                     name_ref_index, THREAD);
 736                 return;
 737               }
 738             } else {
 739               if (name == vmSymbols::object_initializer_name()) {











 740                 classfile_parse_error(
 741                   "Bad method name at constant pool index %u in class file %s",
 742                   name_ref_index, THREAD);
 743                 return;
 744               }
 745             }
 746             break;
 747           }
 748           // Other ref_kinds are already fully checked in previous pass.
 749         } // switch(ref_kind)
 750         break;
 751       }
 752       case JVM_CONSTANT_MethodType: {
 753         const Symbol* const no_name = vmSymbols::type_name(); // place holder
 754         const Symbol* const signature = cp->method_type_signature_at(index);
 755         verify_legal_method_signature(no_name, signature, CHECK);
 756         break;
 757       }
 758       case JVM_CONSTANT_Utf8: {
 759         assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");

 797   while (entry != NULL) {
 798     if (entry->_name == name && entry->_sig == sig) {
 799       return false;
 800     }
 801     entry = entry->_next;
 802   }
 803 
 804   // No duplicate is found, allocate a new entry and fill it.
 805   entry = new NameSigHash();
 806   entry->_name = name;
 807   entry->_sig = sig;
 808 
 809   // Insert into hash table
 810   entry->_next = table[index];
 811   table[index] = entry;
 812 
 813   return true;
 814 }
 815 
 816 // Side-effects: populates the _local_interfaces field
 817 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
 818                                        const int itfs_len,
 819                                        ConstantPool* const cp,
 820                                        bool* const has_nonstatic_concrete_methods,







 821                                        TRAPS) {
 822   assert(stream != NULL, "invariant");
 823   assert(cp != NULL, "invariant");
 824   assert(has_nonstatic_concrete_methods != NULL, "invariant");
 825 
 826   if (itfs_len == 0) {
 827     _local_interfaces = Universe::the_empty_instance_klass_array();
 828   } else {
 829     assert(itfs_len > 0, "only called for len>0");
 830     _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, NULL, CHECK);
 831 
 832     int index;
 833     for (index = 0; index < itfs_len; index++) {
 834       const u2 interface_index = stream->get_u2(CHECK);
 835       Klass* interf;
 836       check_property(
 837         valid_klass_reference_at(interface_index),
 838         "Interface name has bad constant pool index %u in class file %s",
 839         interface_index, CHECK);
 840       if (cp->tag_at(interface_index).is_klass()) {
 841         interf = cp->resolved_klass_at(interface_index);
 842       } else {
 843         Symbol* const unresolved_klass  = cp->klass_name_at(interface_index);
 844 
 845         // Don't need to check legal name because it's checked when parsing constant pool.
 846         // But need to make sure it's not an array type.
 847         guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
 848                            "Bad interface name in class file %s", CHECK);
 849 
 850         // Call resolve_super so class circularity is checked
 851         interf = SystemDictionary::resolve_super_or_fail(
 852                                                   _class_name,
 853                                                   unresolved_klass,
 854                                                   Handle(THREAD, _loader_data->class_loader()),
 855                                                   _protection_domain,
 856                                                   false,
 857                                                   CHECK);
 858       }
 859 
 860       if (!interf->is_interface()) {
 861         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
 862                   err_msg("class %s can not implement %s, because it is not an interface (%s)",
 863                           _class_name->as_klass_external_name(),
 864                           interf->external_name(),
 865                           interf->class_in_module_of_loader()));
 866       }
 867 
 868       if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {













 869         *has_nonstatic_concrete_methods = true;
 870       }
 871       _local_interfaces->at_put(index, InstanceKlass::cast(interf));











 872     }
 873 
 874     if (!_need_verify || itfs_len <= 1) {
 875       return;
 876     }
 877 
 878     // Check if there's any duplicates in interfaces
 879     ResourceMark rm(THREAD);
 880     NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
 881                                                                  NameSigHash*,
 882                                                                  HASH_ROW_SIZE);
 883     initialize_hashtable(interface_names);
 884     bool dup = false;
 885     const Symbol* name = NULL;
 886     {
 887       debug_only(NoSafepointVerifier nsv;)
 888       for (index = 0; index < itfs_len; index++) {
 889         const InstanceKlass* const k = _local_interfaces->at(index);
 890         name = k->name();
 891         // If no duplicates, add (name, NULL) in hashtable interface_names.
 892         if (!put_after_lookup(name, NULL, interface_names)) {
 893           dup = true;
 894           break;
 895         }
 896       }
 897     }
 898     if (dup) {
 899       classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
 900                              name->as_C_string(), THREAD);
 901     }
 902   }
 903 }
 904 
 905 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
 906                                            int constantvalue_index,
 907                                            int signature_index,
 908                                            TRAPS) const {
 909   // Make sure the constant pool entry is of a type appropriate to this field

1354                                             CHECK);
1355   parsed_annotations->set_field_annotations(a);
1356   a = assemble_annotations(runtime_visible_type_annotations,
1357                            runtime_visible_type_annotations_length,
1358                            runtime_invisible_type_annotations,
1359                            runtime_invisible_type_annotations_length,
1360                            CHECK);
1361   parsed_annotations->set_field_type_annotations(a);
1362   return;
1363 }
1364 
1365 
1366 // Field allocation types. Used for computing field offsets.
1367 
1368 enum FieldAllocationType {
1369   STATIC_OOP,           // Oops
1370   STATIC_BYTE,          // Boolean, Byte, char
1371   STATIC_SHORT,         // shorts
1372   STATIC_WORD,          // ints
1373   STATIC_DOUBLE,        // aligned long or double

1374   NONSTATIC_OOP,
1375   NONSTATIC_BYTE,
1376   NONSTATIC_SHORT,
1377   NONSTATIC_WORD,
1378   NONSTATIC_DOUBLE,

1379   MAX_FIELD_ALLOCATION_TYPE,
1380   BAD_ALLOCATION_TYPE = -1
1381 };
1382 
1383 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1384   BAD_ALLOCATION_TYPE, // 0
1385   BAD_ALLOCATION_TYPE, // 1
1386   BAD_ALLOCATION_TYPE, // 2
1387   BAD_ALLOCATION_TYPE, // 3
1388   NONSTATIC_BYTE ,     // T_BOOLEAN     =  4,
1389   NONSTATIC_SHORT,     // T_CHAR        =  5,
1390   NONSTATIC_WORD,      // T_FLOAT       =  6,
1391   NONSTATIC_DOUBLE,    // T_DOUBLE      =  7,
1392   NONSTATIC_BYTE,      // T_BYTE        =  8,
1393   NONSTATIC_SHORT,     // T_SHORT       =  9,
1394   NONSTATIC_WORD,      // T_INT         = 10,
1395   NONSTATIC_DOUBLE,    // T_LONG        = 11,
1396   NONSTATIC_OOP,       // T_OBJECT      = 12,
1397   NONSTATIC_OOP,       // T_ARRAY       = 13,
1398   BAD_ALLOCATION_TYPE, // T_VOID        = 14,
1399   BAD_ALLOCATION_TYPE, // T_ADDRESS     = 15,
1400   BAD_ALLOCATION_TYPE, // T_NARROWOOP   = 16,
1401   BAD_ALLOCATION_TYPE, // T_METADATA    = 17,
1402   BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1403   BAD_ALLOCATION_TYPE, // T_CONFLICT    = 19,

1404   BAD_ALLOCATION_TYPE, // 0
1405   BAD_ALLOCATION_TYPE, // 1
1406   BAD_ALLOCATION_TYPE, // 2
1407   BAD_ALLOCATION_TYPE, // 3
1408   STATIC_BYTE ,        // T_BOOLEAN     =  4,
1409   STATIC_SHORT,        // T_CHAR        =  5,
1410   STATIC_WORD,         // T_FLOAT       =  6,
1411   STATIC_DOUBLE,       // T_DOUBLE      =  7,
1412   STATIC_BYTE,         // T_BYTE        =  8,
1413   STATIC_SHORT,        // T_SHORT       =  9,
1414   STATIC_WORD,         // T_INT         = 10,
1415   STATIC_DOUBLE,       // T_LONG        = 11,
1416   STATIC_OOP,          // T_OBJECT      = 12,
1417   STATIC_OOP,          // T_ARRAY       = 13,
1418   BAD_ALLOCATION_TYPE, // T_VOID        = 14,
1419   BAD_ALLOCATION_TYPE, // T_ADDRESS     = 15,
1420   BAD_ALLOCATION_TYPE, // T_NARROWOOP   = 16,
1421   BAD_ALLOCATION_TYPE, // T_METADATA    = 17,
1422   BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1423   BAD_ALLOCATION_TYPE, // T_CONFLICT    = 19,

1424 };
1425 
1426 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {
1427   assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1428   FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1429   assert(result != BAD_ALLOCATION_TYPE, "bad type");



1430   return result;
1431 }
1432 
1433 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1434  public:
1435   u2 count[MAX_FIELD_ALLOCATION_TYPE];
1436 
1437   FieldAllocationCount() {
1438     for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1439       count[i] = 0;
1440     }
1441   }
1442 
1443   void update(bool is_static, BasicType type) {
1444     FieldAllocationType atype = basic_type_to_atype(is_static, type);
1445     if (atype != BAD_ALLOCATION_TYPE) {
1446       // Make sure there is no overflow with injected fields.
1447       assert(count[atype] < 0xFFFF, "More than 65535 fields");
1448       count[atype]++;
1449     }
1450   }
1451 };
1452 
1453 // Side-effects: populates the _fields, _fields_annotations,
1454 // _fields_type_annotations fields
1455 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1456                                    bool is_interface,

1457                                    FieldAllocationCount* const fac,
1458                                    ConstantPool* cp,
1459                                    const int cp_size,
1460                                    u2* const java_fields_count_ptr,
1461                                    TRAPS) {
1462 
1463   assert(cfs != NULL, "invariant");
1464   assert(fac != NULL, "invariant");
1465   assert(cp != NULL, "invariant");
1466   assert(java_fields_count_ptr != NULL, "invariant");
1467 
1468   assert(NULL == _fields, "invariant");
1469   assert(NULL == _fields_annotations, "invariant");
1470   assert(NULL == _fields_type_annotations, "invariant");
1471 
1472   cfs->guarantee_more(2, CHECK);  // length
1473   const u2 length = cfs->get_u2_fast();
1474   *java_fields_count_ptr = length;
1475 
1476   int num_injected = 0;
1477   const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1478                                                                   &num_injected);
1479   const int total_fields = length + num_injected;




1480 
1481   // The field array starts with tuples of shorts
1482   // [access, name index, sig index, initial value index, byte offset].
1483   // A generic signature slot only exists for field with generic
1484   // signature attribute. And the access flag is set with
1485   // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic
1486   // signature slots are at the end of the field array and after all
1487   // other fields data.
1488   //
1489   //   f1: [access, name index, sig index, initial value index, low_offset, high_offset]
1490   //   f2: [access, name index, sig index, initial value index, low_offset, high_offset]
1491   //       ...
1492   //   fn: [access, name index, sig index, initial value index, low_offset, high_offset]
1493   //       [generic signature index]
1494   //       [generic signature index]
1495   //       ...
1496   //
1497   // Allocate a temporary resource array for field data. For each field,
1498   // a slot is reserved in the temporary array for the generic signature
1499   // index. After parsing all fields, the data are copied to a permanent
1500   // array and any unused slots will be discarded.
1501   ResourceMark rm(THREAD);
1502   u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
1503                                               u2,
1504                                               total_fields * (FieldInfo::field_slots + 1));
1505 
1506   // The generic signature slots start after all other fields' data.
1507   int generic_signature_slot = total_fields * FieldInfo::field_slots;
1508   int num_generic_signature = 0;

1509   for (int n = 0; n < length; n++) {
1510     // access_flags, name_index, descriptor_index, attributes_count
1511     cfs->guarantee_more(8, CHECK);
1512 




1513     AccessFlags access_flags;
1514     const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1515     verify_legal_field_modifiers(flags, is_interface, CHECK);
1516     access_flags.set_flags(flags);
1517 
1518     const u2 name_index = cfs->get_u2_fast();
1519     check_property(valid_symbol_at(name_index),
1520       "Invalid constant pool index %u for field name in class file %s",
1521       name_index, CHECK);
1522     const Symbol* const name = cp->symbol_at(name_index);
1523     verify_legal_field_name(name, CHECK);
1524 
1525     const u2 signature_index = cfs->get_u2_fast();
1526     check_property(valid_symbol_at(signature_index),
1527       "Invalid constant pool index %u for field signature in class file %s",
1528       signature_index, CHECK);
1529     const Symbol* const sig = cp->symbol_at(signature_index);
1530     verify_legal_field_signature(name, sig, CHECK);

1531 
1532     u2 constantvalue_index = 0;
1533     bool is_synthetic = false;
1534     u2 generic_signature_index = 0;
1535     const bool is_static = access_flags.is_static();
1536     FieldAnnotationCollector parsed_annotations(_loader_data);
1537 
1538     const u2 attributes_count = cfs->get_u2_fast();
1539     if (attributes_count > 0) {
1540       parse_field_attributes(cfs,
1541                              attributes_count,
1542                              is_static,
1543                              signature_index,
1544                              &constantvalue_index,
1545                              &is_synthetic,
1546                              &generic_signature_index,
1547                              &parsed_annotations,
1548                              CHECK);
1549 
1550       if (parsed_annotations.field_annotations() != NULL) {

1570 
1571       if (is_synthetic) {
1572         access_flags.set_is_synthetic();
1573       }
1574       if (generic_signature_index != 0) {
1575         access_flags.set_field_has_generic_signature();
1576         fa[generic_signature_slot] = generic_signature_index;
1577         generic_signature_slot ++;
1578         num_generic_signature ++;
1579       }
1580     }
1581 
1582     FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1583     field->initialize(access_flags.as_short(),
1584                       name_index,
1585                       signature_index,
1586                       constantvalue_index);
1587     const BasicType type = cp->basic_type_for_signature_at(signature_index);
1588 
1589     // Update FieldAllocationCount for this kind of field
1590     fac->update(is_static, type);
1591 
1592     // After field is initialized with type, we can augment it with aux info
1593     if (parsed_annotations.has_any_annotations()) {
1594       parsed_annotations.apply_to(field);
1595       if (field->is_contended()) {
1596         _has_contended_fields = true;
1597       }
1598     }
1599   }
1600 
1601   int index = length;
1602   if (num_injected != 0) {
1603     for (int n = 0; n < num_injected; n++) {
1604       // Check for duplicates
1605       if (injected[n].may_be_java) {
1606         const Symbol* const name      = injected[n].name();
1607         const Symbol* const signature = injected[n].signature();
1608         bool duplicate = false;
1609         for (int i = 0; i < length; i++) {
1610           const FieldInfo* const f = FieldInfo::from_field_array(fa, i);

1614             duplicate = true;
1615             break;
1616           }
1617         }
1618         if (duplicate) {
1619           // These will be removed from the field array at the end
1620           continue;
1621         }
1622       }
1623 
1624       // Injected field
1625       FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1626       field->initialize((u2)JVM_ACC_FIELD_INTERNAL,
1627                         (u2)(injected[n].name_index),
1628                         (u2)(injected[n].signature_index),
1629                         0);
1630 
1631       const BasicType type = Signature::basic_type(injected[n].signature());
1632 
1633       // Update FieldAllocationCount for this kind of field
1634       fac->update(false, type);
1635       index++;
1636     }
1637   }
1638 



























1639   assert(NULL == _fields, "invariant");
1640 
1641   _fields =
1642     MetadataFactory::new_array<u2>(_loader_data,
1643                                    index * FieldInfo::field_slots + num_generic_signature,
1644                                    CHECK);
1645   // Sometimes injected fields already exist in the Java source so
1646   // the fields array could be too long.  In that case the
1647   // fields array is trimed. Also unused slots that were reserved
1648   // for generic signature indexes are discarded.
1649   {
1650     int i = 0;
1651     for (; i < index * FieldInfo::field_slots; i++) {
1652       _fields->at_put(i, fa[i]);
1653     }
1654     for (int j = total_fields * FieldInfo::field_slots;
1655          j < generic_signature_slot; j++) {
1656       _fields->at_put(i++, fa[j]);
1657     }
1658     assert(_fields->length() == i, "");

1936         "Exception name has bad type at constant pool %u in class file %s",
1937         checked_exception, CHECK_NULL);
1938     }
1939   }
1940   // check exceptions attribute length
1941   if (_need_verify) {
1942     guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1943                                                    sizeof(u2) * size),
1944                       "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1945   }
1946   return checked_exceptions_start;
1947 }
1948 
1949 void ClassFileParser::throwIllegalSignature(const char* type,
1950                                             const Symbol* name,
1951                                             const Symbol* sig,
1952                                             TRAPS) const {
1953   assert(name != NULL, "invariant");
1954   assert(sig != NULL, "invariant");
1955 





1956   ResourceMark rm(THREAD);
1957   Exceptions::fthrow(THREAD_AND_LOCATION,
1958       vmSymbols::java_lang_ClassFormatError(),
1959       "%s \"%s\" in class %s has illegal signature \"%s\"", type,
1960       name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
1961 }
1962 
1963 AnnotationCollector::ID
1964 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
1965                                       const Symbol* name,
1966                                       const bool can_access_vm_annotations) {
1967   const vmSymbolID sid = vmSymbols::find_sid(name);
1968   // Privileged code can use all annotations.  Other code silently drops some.
1969   const bool privileged = loader_data->is_boot_class_loader_data() ||
1970                           loader_data->is_platform_class_loader_data() ||
1971                           can_access_vm_annotations;
1972   switch (sid) {
1973     case VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
1974       if (_location != _in_method)  break;  // only allow for methods
1975       if (!privileged)              break;  // only allow in privileged code
1976       return _method_CallerSensitive;
1977     }
1978     case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
1979       if (_location != _in_method)  break;  // only allow for methods
1980       if (!privileged)              break;  // only allow in privileged code

2229                              runtime_visible_type_annotations_length,
2230                              runtime_invisible_type_annotations,
2231                              runtime_invisible_type_annotations_length,
2232                              CHECK);
2233     cm->set_type_annotations(a);
2234   }
2235 }
2236 
2237 
2238 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2239 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2240 // Method* to save footprint, so we only know the size of the resulting Method* when the
2241 // entire method attribute is parsed.
2242 //
2243 // The promoted_flags parameter is used to pass relevant access_flags
2244 // from the method back up to the containing klass. These flag values
2245 // are added to klass's access_flags.
2246 
2247 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2248                                       bool is_interface,

2249                                       const ConstantPool* cp,
2250                                       AccessFlags* const promoted_flags,
2251                                       TRAPS) {
2252   assert(cfs != NULL, "invariant");
2253   assert(cp != NULL, "invariant");
2254   assert(promoted_flags != NULL, "invariant");
2255 
2256   ResourceMark rm(THREAD);
2257   // Parse fixed parts:
2258   // access_flags, name_index, descriptor_index, attributes_count
2259   cfs->guarantee_more(8, CHECK_NULL);
2260 
2261   int flags = cfs->get_u2_fast();
2262   const u2 name_index = cfs->get_u2_fast();
2263   const int cp_size = cp->length();
2264   check_property(
2265     valid_symbol_at(name_index),
2266     "Illegal constant pool index %u for method name in class file %s",
2267     name_index, CHECK_NULL);
2268   const Symbol* const name = cp->symbol_at(name_index);

2270 
2271   const u2 signature_index = cfs->get_u2_fast();
2272   guarantee_property(
2273     valid_symbol_at(signature_index),
2274     "Illegal constant pool index %u for method signature in class file %s",
2275     signature_index, CHECK_NULL);
2276   const Symbol* const signature = cp->symbol_at(signature_index);
2277 
2278   if (name == vmSymbols::class_initializer_name()) {
2279     // We ignore the other access flags for a valid class initializer.
2280     // (JVM Spec 2nd ed., chapter 4.6)
2281     if (_major_version < 51) { // backward compatibility
2282       flags = JVM_ACC_STATIC;
2283     } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2284       flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2285     } else {
2286       classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2287       return NULL;
2288     }
2289   } else {
2290     verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2291   }
2292 
2293   if (name == vmSymbols::object_initializer_name() && is_interface) {
2294     classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2295     return NULL;













































2296   }
2297 
2298   int args_size = -1;  // only used when _need_verify is true
2299   if (_need_verify) {
2300     verify_legal_name_with_signature(name, signature, CHECK_NULL);
2301     args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2302                  verify_legal_method_signature(name, signature, CHECK_NULL);
2303     if (args_size > MAX_ARGS_SIZE) {
2304       classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2305       return NULL;
2306     }
2307   }
2308 
2309   AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2310 
2311   // Default values for code and exceptions attribute elements
2312   u2 max_stack = 0;
2313   u2 max_locals = 0;
2314   u4 code_length = 0;
2315   const u1* code_start = 0;

2843       _has_finalizer = true;
2844     }
2845   }
2846   if (name == vmSymbols::object_initializer_name() &&
2847       signature == vmSymbols::void_method_signature() &&
2848       m->is_vanilla_constructor()) {
2849     _has_vanilla_constructor = true;
2850   }
2851 
2852   NOT_PRODUCT(m->verify());
2853   return m;
2854 }
2855 
2856 
2857 // The promoted_flags parameter is used to pass relevant access_flags
2858 // from the methods back up to the containing klass. These flag values
2859 // are added to klass's access_flags.
2860 // Side-effects: populates the _methods field in the parser
2861 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2862                                     bool is_interface,

2863                                     AccessFlags* promoted_flags,
2864                                     bool* has_final_method,
2865                                     bool* declares_nonstatic_concrete_methods,
2866                                     TRAPS) {
2867   assert(cfs != NULL, "invariant");
2868   assert(promoted_flags != NULL, "invariant");
2869   assert(has_final_method != NULL, "invariant");
2870   assert(declares_nonstatic_concrete_methods != NULL, "invariant");
2871 
2872   assert(NULL == _methods, "invariant");
2873 
2874   cfs->guarantee_more(2, CHECK);  // length
2875   const u2 length = cfs->get_u2_fast();
2876   if (length == 0) {
2877     _methods = Universe::the_empty_method_array();
2878   } else {
2879     _methods = MetadataFactory::new_array<Method*>(_loader_data,
2880                                                    length,
2881                                                    NULL,
2882                                                    CHECK);
2883 
2884     for (int index = 0; index < length; index++) {
2885       Method* method = parse_method(cfs,
2886                                     is_interface,

2887                                     _cp,
2888                                     promoted_flags,
2889                                     CHECK);
2890 
2891       if (method->is_final()) {
2892         *has_final_method = true;
2893       }
2894       // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2895       // used for interface initialization, and default method inheritance analysis
2896       if (is_interface && !(*declares_nonstatic_concrete_methods)
2897         && !method->is_abstract() && !method->is_static()) {
2898         *declares_nonstatic_concrete_methods = true;
2899       }
2900       _methods->at_put(index, method);
2901     }
2902 
2903     if (_need_verify && length > 1) {
2904       // Check duplicated methods
2905       ResourceMark rm(THREAD);
2906       NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(

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








3162     if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3163       // Set abstract bit for old class files for backward compatibility
3164       flags |= JVM_ACC_ABSTRACT;
3165     }
3166     verify_legal_class_modifiers(flags, CHECK_0);
3167     AccessFlags inner_access_flags(flags);
3168 
3169     inner_classes->at_put(index++, inner_class_info_index);
3170     inner_classes->at_put(index++, outer_class_info_index);
3171     inner_classes->at_put(index++, inner_name_index);
3172     inner_classes->at_put(index++, inner_access_flags.as_short());
3173   }
3174 
3175   // 4347400: make sure there's no duplicate entry in the classes array
3176   // Also, check for circular entries.
3177   bool has_circularity = false;
3178   if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3179     has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3180     if (has_circularity) {
3181       // If circularity check failed then ignore InnerClasses attribute.

3987     }
3988     if (runtime_invisible_annotations != NULL) {
3989       for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3990         int append = runtime_visible_annotations_length+i;
3991         annotations->at_put(append, runtime_invisible_annotations[i]);
3992       }
3993     }
3994   }
3995   return annotations;
3996 }
3997 
3998 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3999                                                         const int super_class_index,
4000                                                         const bool need_verify,
4001                                                         TRAPS) {
4002   assert(cp != NULL, "invariant");
4003   const InstanceKlass* super_klass = NULL;
4004 
4005   if (super_class_index == 0) {
4006     check_property(_class_name == vmSymbols::java_lang_Object(),
4007                    "Invalid superclass index %u in class file %s",
4008                    super_class_index,
4009                    CHECK_NULL);
4010   } else {
4011     check_property(valid_klass_reference_at(super_class_index),
4012                    "Invalid superclass index %u in class file %s",
4013                    super_class_index,
4014                    CHECK_NULL);
4015     // The class name should be legal because it is checked when parsing constant pool.
4016     // However, make sure it is not an array type.
4017     bool is_array = false;
4018     if (cp->tag_at(super_class_index).is_klass()) {
4019       super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
4020       if (need_verify)
4021         is_array = super_klass->is_array_klass();
4022     } else if (need_verify) {
4023       is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
4024     }
4025     if (need_verify) {
4026       guarantee_property(!is_array,
4027                         "Bad superclass name in class file %s", CHECK_NULL);
4028     }

4129 }
4130 
4131 void OopMapBlocksBuilder::print_on(outputStream* st) const {
4132   st->print_cr("  OopMapBlocks: %3d  /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);
4133   if (_nonstatic_oop_map_count > 0) {
4134     OopMapBlock* map = _nonstatic_oop_maps;
4135     OopMapBlock* last_map = last_oop_map();
4136     assert(map <= last_map, "Last less than first");
4137     while (map <= last_map) {
4138       st->print_cr("    Offset: %3d  -%3d Count: %3d", map->offset(),
4139                    map->offset() + map->offset_span() - heapOopSize, map->count());
4140       map++;
4141     }
4142   }
4143 }
4144 
4145 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
4146   print_on(st);
4147 }
4148 



















4149 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4150   assert(ik != NULL, "invariant");
4151 
4152   const Klass* const super = ik->super();
4153 
4154   // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4155   // in which case we don't have to register objects as finalizable
4156   if (!_has_empty_finalizer) {
4157     if (_has_finalizer ||
4158         (super != NULL && super->has_finalizer())) {
4159       ik->set_has_finalizer();
4160     }
4161   }
4162 
4163 #ifdef ASSERT
4164   bool f = false;
4165   const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4166                                            vmSymbols::void_method_signature());
4167   if (m != NULL && !m->is_empty_method()) {
4168       f = true;
4169   }
4170 
4171   // Spec doesn't prevent agent from redefinition of empty finalizer.
4172   // Despite the fact that it's generally bad idea and redefined finalizer
4173   // will not work as expected we shouldn't abort vm in this case
4174   if (!ik->has_redefined_this_or_super()) {
4175     assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4176   }
4177 #endif
4178 
4179   // Check if this klass supports the java.lang.Cloneable interface
4180   if (vmClasses::Cloneable_klass_loaded()) {
4181     if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {





4182       ik->set_is_cloneable();
4183     }
4184   }
4185 
4186   // Check if this klass has a vanilla default constructor
4187   if (super == NULL) {
4188     // java.lang.Object has empty default constructor
4189     ik->set_has_vanilla_constructor();
4190   } else {
4191     if (super->has_vanilla_constructor() &&
4192         _has_vanilla_constructor) {
4193       ik->set_has_vanilla_constructor();
4194     }
4195 #ifdef ASSERT
4196     bool v = false;
4197     if (super->has_vanilla_constructor()) {
4198       const Method* const constructor =
4199         ik->find_method(vmSymbols::object_initializer_name(),
4200                        vmSymbols::void_method_signature());
4201       if (constructor != NULL && constructor->is_vanilla_constructor()) {
4202         v = true;
4203       }
4204     }
4205     assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4206 #endif
4207   }
4208 
4209   // If it cannot be fast-path allocated, set a bit in the layout helper.
4210   // See documentation of InstanceKlass::can_be_fastpath_allocated().
4211   assert(ik->size_helper() > 0, "layout_helper is initialized");
4212   if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4213       || ik->is_abstract() || ik->is_interface()
4214       || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4215       || ik->size_helper() >= FastAllocateSizeLimit) {
4216     // Forbid fast-path allocation.
4217     const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4218     ik->set_layout_helper(lh);
4219   }
4220 }
4221 





4222 // utility methods for appending an array with check for duplicates
4223 
4224 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4225                               const Array<InstanceKlass*>* const ifs) {
4226   // iterate over new interfaces
4227   for (int i = 0; i < ifs->length(); i++) {
4228     InstanceKlass* const e = ifs->at(i);
4229     assert(e->is_klass() && e->is_interface(), "just checking");
4230     // add new interface
4231     result->append_if_missing(e);
4232   }
4233 }
4234 
4235 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4236                                                             Array<InstanceKlass*>* local_ifs,
4237                                                             ClassLoaderData* loader_data,
4238                                                             TRAPS) {
4239   assert(local_ifs != NULL, "invariant");
4240   assert(loader_data != NULL, "invariant");
4241 

4245   // Add superclass transitive interfaces size
4246   if (super != NULL) {
4247     super_size = super->transitive_interfaces()->length();
4248     max_transitive_size += super_size;
4249   }
4250   // Add local interfaces' super interfaces
4251   const int local_size = local_ifs->length();
4252   for (int i = 0; i < local_size; i++) {
4253     InstanceKlass* const l = local_ifs->at(i);
4254     max_transitive_size += l->transitive_interfaces()->length();
4255   }
4256   // Finally add local interfaces
4257   max_transitive_size += local_size;
4258   // Construct array
4259   if (max_transitive_size == 0) {
4260     // no interfaces, use canonicalized array
4261     return Universe::the_empty_instance_klass_array();
4262   } else if (max_transitive_size == super_size) {
4263     // no new local interfaces added, share superklass' transitive interface array
4264     return super->transitive_interfaces();
4265   } else if (max_transitive_size == local_size) {
4266     // only local interfaces added, share local interface array
4267     return local_ifs;

4268   } else {
4269     ResourceMark rm;
4270     GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4271 
4272     // Copy down from superclass
4273     if (super != NULL) {
4274       append_interfaces(result, super->transitive_interfaces());
4275     }
4276 
4277     // Copy down from local interfaces' superinterfaces
4278     for (int i = 0; i < local_size; i++) {
4279       InstanceKlass* const l = local_ifs->at(i);
4280       append_interfaces(result, l->transitive_interfaces());
4281     }
4282     // Finally add local interfaces
4283     append_interfaces(result, local_ifs);
4284 
4285     // length will be less than the max_transitive_size if duplicates were removed
4286     const int length = result->length();
4287     assert(length <= max_transitive_size, "just checking");








4288     Array<InstanceKlass*>* const new_result =
4289       MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4290     for (int i = 0; i < length; i++) {
4291       InstanceKlass* const e = result->at(i);
4292       assert(e != NULL, "just checking");
4293       new_result->at_put(i, e);
4294     }
4295     return new_result;
4296   }
4297 }
4298 
4299 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4300   assert(this_klass != NULL, "invariant");
4301   const Klass* const super = this_klass->super();
4302 
4303   if (super != NULL) {
4304     const InstanceKlass* super_ik = InstanceKlass::cast(super);
4305 
4306     if (super->is_final()) {
4307       classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);

4481     const Method* const m = methods->at(index);
4482     // if m is static and not the init method, throw a verify error
4483     if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4484       ResourceMark rm(THREAD);
4485       Exceptions::fthrow(
4486         THREAD_AND_LOCATION,
4487         vmSymbols::java_lang_VerifyError(),
4488         "Illegal static method %s in interface %s",
4489         m->name()->as_C_string(),
4490         this_klass->external_name()
4491       );
4492       return;
4493     }
4494   }
4495 }
4496 
4497 // utility methods for format checking
4498 
4499 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4500   const bool is_module = (flags & JVM_ACC_MODULE) != 0;

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

4502   if (is_module) {
4503     ResourceMark rm(THREAD);
4504     Exceptions::fthrow(
4505       THREAD_AND_LOCATION,
4506       vmSymbols::java_lang_NoClassDefFoundError(),
4507       "%s is not a class because access_flag ACC_MODULE is set",
4508       _class_name->as_C_string());
4509     return;
4510   }
4511 











4512   if (!_need_verify) { return; }
4513 
4514   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4515   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4516   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4517   const bool is_super      = (flags & JVM_ACC_SUPER)      != 0;
4518   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4519   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4520   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4521   const bool major_gte_14  = _major_version >= JAVA_14_VERSION;
4522 
4523   if ((is_abstract && is_final) ||
4524       (is_interface && !is_abstract) ||
4525       (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4526       (!is_interface && major_gte_1_5 && is_annotation)) {

4527     ResourceMark rm(THREAD);


4528     Exceptions::fthrow(
4529       THREAD_AND_LOCATION,
4530       vmSymbols::java_lang_ClassFormatError(),
4531       "Illegal class modifiers in class %s: 0x%X",
4532       _class_name->as_C_string(), flags
4533     );
4534     return;
4535   }
4536 }
4537 
4538 static bool has_illegal_visibility(jint flags) {
4539   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4540   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4541   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4542 
4543   return ((is_public && is_protected) ||
4544           (is_public && is_private) ||
4545           (is_protected && is_private));
4546 }
4547 
4548 // A legal major_version.minor_version must be one of the following:
4549 //
4550 //  Major_version >= 45 and major_version < 56, any minor_version.
4551 //  Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4552 //  Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.

4582         "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4583         "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4584         class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4585       return;
4586     }
4587 
4588     if (!Arguments::enable_preview()) {
4589       classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4590                            class_name, major, minor, THREAD);
4591       return;
4592     }
4593 
4594   } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4595     classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4596                          class_name, major, minor, THREAD);
4597   }
4598 }
4599 
4600 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4601                                                    bool is_interface,

4602                                                    TRAPS) const {
4603   if (!_need_verify) { return; }
4604 
4605   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4606   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4607   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4608   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4609   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4610   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4611   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4612   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4613   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4614 
4615   bool is_illegal = false;
4616 
4617   if (is_interface) {
4618     if (!is_public || !is_static || !is_final || is_private ||
4619         is_protected || is_volatile || is_transient ||
4620         (major_gte_1_5 && is_enum)) {
4621       is_illegal = true;
4622     }
4623   } else { // not interface
4624     if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4625       is_illegal = true;




4626     }
4627   }
4628 
4629   if (is_illegal) {
4630     ResourceMark rm(THREAD);
4631     Exceptions::fthrow(
4632       THREAD_AND_LOCATION,
4633       vmSymbols::java_lang_ClassFormatError(),
4634       "Illegal field modifiers in class %s: 0x%X",
4635       _class_name->as_C_string(), flags);
4636     return;
4637   }
4638 }
4639 
4640 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4641                                                     bool is_interface,

4642                                                     const Symbol* name,
4643                                                     TRAPS) const {
4644   if (!_need_verify) { return; }
4645 
4646   const bool is_public       = (flags & JVM_ACC_PUBLIC)       != 0;
4647   const bool is_private      = (flags & JVM_ACC_PRIVATE)      != 0;
4648   const bool is_static       = (flags & JVM_ACC_STATIC)       != 0;
4649   const bool is_final        = (flags & JVM_ACC_FINAL)        != 0;
4650   const bool is_native       = (flags & JVM_ACC_NATIVE)       != 0;
4651   const bool is_abstract     = (flags & JVM_ACC_ABSTRACT)     != 0;
4652   const bool is_bridge       = (flags & JVM_ACC_BRIDGE)       != 0;
4653   const bool is_strict       = (flags & JVM_ACC_STRICT)       != 0;
4654   const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4655   const bool is_protected    = (flags & JVM_ACC_PROTECTED)    != 0;
4656   const bool major_gte_1_5   = _major_version >= JAVA_1_5_VERSION;
4657   const bool major_gte_8     = _major_version >= JAVA_8_VERSION;
4658   const bool major_gte_17    = _major_version >= JAVA_17_VERSION;
4659   const bool is_initializer  = (name == vmSymbols::object_initializer_name());
4660 
4661   bool is_illegal = false;
4662 

4663   if (is_interface) {
4664     if (major_gte_8) {
4665       // Class file version is JAVA_8_VERSION or later Methods of
4666       // interfaces may set any of the flags except ACC_PROTECTED,
4667       // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4668       // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4669       if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4670           (is_native || is_protected || is_final || is_synchronized) ||
4671           // If a specific method of a class or interface has its
4672           // ACC_ABSTRACT flag set, it must not have any of its
4673           // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4674           // ACC_STRICT, or ACC_SYNCHRONIZED flags set.  No need to
4675           // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4676           // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4677           (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4678         is_illegal = true;
4679       }
4680     } else if (major_gte_1_5) {
4681       // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4682       if (!is_public || is_private || is_protected || is_static || is_final ||
4683           is_synchronized || is_native || !is_abstract || is_strict) {
4684         is_illegal = true;
4685       }
4686     } else {
4687       // Class file version is pre-JAVA_1_5_VERSION
4688       if (!is_public || is_static || is_final || is_native || !is_abstract) {
4689         is_illegal = true;
4690       }
4691     }
4692   } else { // not interface
4693     if (has_illegal_visibility(flags)) {
4694       is_illegal = true;
4695     } else {
4696       if (is_initializer) {
4697         if (is_static || is_final || is_synchronized || is_native ||
4698             is_abstract || (major_gte_1_5 && is_bridge)) {
4699           is_illegal = true;
4700         }









4701       } else { // not initializer
4702         if (is_abstract) {
4703           if ((is_final || is_native || is_private || is_static ||
4704               (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4705             is_illegal = true;





4706           }
4707         }
4708       }
4709     }
4710   }
4711 
4712   if (is_illegal) {
4713     ResourceMark rm(THREAD);
4714     Exceptions::fthrow(
4715       THREAD_AND_LOCATION,
4716       vmSymbols::java_lang_ClassFormatError(),
4717       "Method %s in class %s has illegal modifiers: 0x%X",
4718       name->as_C_string(), _class_name->as_C_string(), flags);
4719     return;
4720   }
4721 }
4722 
4723 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4724                                         int length,
4725                                         TRAPS) const {
4726   assert(_need_verify, "only called when _need_verify is true");
4727   if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4728     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4729   }
4730 }
4731 
4732 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4733 // In class names, '/' separates unqualified names.  This is verified in this function also.
4734 // Method names also may not contain the characters '<' or '>', unless <init>
4735 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
4736 // method.  Because these names have been checked as special cases before
4737 // calling this method in verify_legal_method_name.
4738 //

4856 // be taken as a field signature. Allow "void" if void_ok.
4857 // Return a pointer to just past the signature.
4858 // Return NULL if no legal signature is found.
4859 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4860                                                        bool void_ok,
4861                                                        unsigned int length,
4862                                                        TRAPS) const {
4863   unsigned int array_dim = 0;
4864   while (length > 0) {
4865     switch (signature[0]) {
4866     case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
4867     case JVM_SIGNATURE_BOOLEAN:
4868     case JVM_SIGNATURE_BYTE:
4869     case JVM_SIGNATURE_CHAR:
4870     case JVM_SIGNATURE_SHORT:
4871     case JVM_SIGNATURE_INT:
4872     case JVM_SIGNATURE_FLOAT:
4873     case JVM_SIGNATURE_LONG:
4874     case JVM_SIGNATURE_DOUBLE:
4875       return signature + 1;
4876     case JVM_SIGNATURE_CLASS: {









4877       if (_major_version < JAVA_1_5_VERSION) {
4878         // Skip over the class name if one is there
4879         const char* const p = skip_over_field_name(signature + 1, true, --length);
4880 
4881         // The next character better be a semicolon
4882         if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4883           return p + 1;
4884         }
4885       }
4886       else {
4887         // Skip leading 'L' and ignore first appearance of ';'
4888         signature++;
4889         const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4890         // Format check signature
4891         if (c != NULL) {
4892           int newlen = c - (char*) signature;
4893           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4894           if (!legal) {
4895             classfile_parse_error("Class name is empty or contains illegal character "
4896                                   "in descriptor in class file %s",
4897                                   THREAD);
4898             return NULL;
4899           }
4900           return signature + newlen + 1;
4901         }
4902       }
4903       return NULL;
4904     }
4905     case JVM_SIGNATURE_ARRAY:
4906       array_dim++;
4907       if (array_dim > 255) {

4923 
4924 // Checks if name is a legal class name.
4925 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4926   if (!_need_verify || _relax_verify) { return; }
4927 
4928   assert(name->refcount() > 0, "symbol must be kept alive");
4929   char* bytes = (char*)name->bytes();
4930   unsigned int length = name->utf8_length();
4931   bool legal = false;
4932 
4933   if (length > 0) {
4934     const char* p;
4935     if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4936       p = skip_over_field_signature(bytes, false, length, CHECK);
4937       legal = (p != NULL) && ((p - bytes) == (int)length);
4938     } else if (_major_version < JAVA_1_5_VERSION) {
4939       if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4940         p = skip_over_field_name(bytes, true, length);
4941         legal = (p != NULL) && ((p - bytes) == (int)length);
4942       }




4943     } else {
4944       // 4900761: relax the constraints based on JSR202 spec
4945       // Class names may be drawn from the entire Unicode character set.
4946       // Identifiers between '/' must be unqualified names.
4947       // The utf8 string has been verified when parsing cpool entries.
4948       legal = verify_unqualified_name(bytes, length, LegalClass);
4949     }
4950   }
4951   if (!legal) {
4952     ResourceMark rm(THREAD);
4953     assert(_class_name != NULL, "invariant");
4954     Exceptions::fthrow(
4955       THREAD_AND_LOCATION,
4956       vmSymbols::java_lang_ClassFormatError(),
4957       "Illegal class name \"%.*s\" in class file %s", length, bytes,
4958       _class_name->as_C_string()
4959     );
4960     return;
4961   }
4962 }

5020 
5021   if (!legal) {
5022     ResourceMark rm(THREAD);
5023     assert(_class_name != NULL, "invariant");
5024     Exceptions::fthrow(
5025       THREAD_AND_LOCATION,
5026       vmSymbols::java_lang_ClassFormatError(),
5027       "Illegal method name \"%.*s\" in class %s", length, bytes,
5028       _class_name->as_C_string()
5029     );
5030     return;
5031   }
5032 }
5033 
5034 
5035 // Checks if signature is a legal field signature.
5036 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5037                                                    const Symbol* signature,
5038                                                    TRAPS) const {
5039   if (!_need_verify) { return; }



5040 
5041   const char* const bytes = (const char* const)signature->bytes();
5042   const unsigned int length = signature->utf8_length();
5043   const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5044 
5045   if (p == NULL || (p - bytes) != (int)length) {
5046     throwIllegalSignature("Field", name, signature, CHECK);
5047   }
5048 }
5049 
5050 // Check that the signature is compatible with the method name.  For example,
5051 // check that <init> has a void signature.
5052 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5053                                                        const Symbol* signature,
5054                                                        TRAPS) const {
5055   if (!_need_verify) {
5056     return;
5057   }
5058 
5059   // Class initializers cannot have args for class format version >= 51.
5060   if (name == vmSymbols::class_initializer_name() &&
5061       signature != vmSymbols::void_method_signature() &&
5062       _major_version >= JAVA_7_VERSION) {
5063     throwIllegalSignature("Method", name, signature, THREAD);
5064     return;
5065   }
5066 
5067   int sig_length = signature->utf8_length();
5068   if (name->utf8_length() > 0 &&

5069       name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5070       sig_length > 0 &&
5071       signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5072     throwIllegalSignature("Method", name, signature, THREAD);

5073   }
5074 }
5075 
5076 // Checks if signature is a legal method signature.
5077 // Returns number of parameters
5078 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5079                                                    const Symbol* signature,
5080                                                    TRAPS) const {
5081   if (!_need_verify) {
5082     // make sure caller's args_size will be less than 0 even for non-static
5083     // method so it will be recomputed in compute_size_of_parameters().
5084     return -2;
5085   }
5086 
5087   unsigned int args_size = 0;
5088   const char* p = (const char*)signature->bytes();
5089   unsigned int length = signature->utf8_length();
5090   const char* nextp;
5091 
5092   // The first character must be a '('

5229 }
5230 
5231 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5232                                                       const ClassInstanceInfo& cl_inst_info,
5233                                                       TRAPS) {
5234   if (_klass != NULL) {
5235     return _klass;
5236   }
5237 
5238   InstanceKlass* const ik =
5239     InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5240 
5241   if (is_hidden()) {
5242     mangle_hidden_class_name(ik);
5243   }
5244 
5245   fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5246 
5247   assert(_klass == ik, "invariant");
5248 






5249   return ik;
5250 }
5251 


































5252 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5253                                           bool changed_by_loadhook,
5254                                           const ClassInstanceInfo& cl_inst_info,
5255                                           TRAPS) {
5256   assert(ik != NULL, "invariant");
5257 
5258   // Set name and CLD before adding to CLD
5259   ik->set_class_loader_data(_loader_data);
5260   ik->set_name(_class_name);
5261 
5262   // Add all classes to our internal class loader list here,
5263   // including classes in the bootstrap (NULL) class loader.
5264   const bool publicize = !is_internal();
5265 
5266   _loader_data->add_class(ik, publicize);
5267 
5268   set_klass_to_deallocate(ik);
5269 
5270   assert(_field_info != NULL, "invariant");
5271   assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5272   assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5273          "sanity");
5274 
5275   assert(ik->is_instance_klass(), "sanity");
5276   assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5277 
5278   // Fill in information already parsed
5279   ik->set_should_verify_class(_need_verify);
5280 
5281   // Not yet: supers are done below to support the new subtype-checking fields
5282   ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5283   ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);















5284   assert(_fac != NULL, "invariant");
5285   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5286 
5287   // this transfers ownership of a lot of arrays from
5288   // the parser onto the InstanceKlass*
5289   apply_parsed_class_metadata(ik, _java_fields_count);
5290 
5291   // can only set dynamic nest-host after static nest information is set
5292   if (cl_inst_info.dynamic_nest_host() != NULL) {
5293     ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5294   }
5295 
5296   // note that is not safe to use the fields in the parser from this point on
5297   assert(NULL == _cp, "invariant");
5298   assert(NULL == _fields, "invariant");
5299   assert(NULL == _methods, "invariant");
5300   assert(NULL == _inner_classes, "invariant");
5301   assert(NULL == _nest_members, "invariant");
5302   assert(NULL == _combined_annotations, "invariant");
5303   assert(NULL == _record_components, "invariant");
5304   assert(NULL == _permitted_subclasses, "invariant");
5305 

5315   // has to be changed accordingly.
5316   ik->set_initial_method_idnum(ik->methods()->length());
5317 
5318   ik->set_this_class_index(_this_class_index);
5319 
5320   if (_is_hidden) {
5321     // _this_class_index is a CONSTANT_Class entry that refers to this
5322     // hidden class itself. If this class needs to refer to its own methods
5323     // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5324     // _this_class_index. However, because this class is hidden (it's
5325     // not stored in SystemDictionary), _this_class_index cannot be resolved
5326     // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5327     // Therefore, we must eagerly resolve _this_class_index now.
5328     ik->constants()->klass_at_put(_this_class_index, ik);
5329   }
5330 
5331   ik->set_minor_version(_minor_version);
5332   ik->set_major_version(_major_version);
5333   ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5334   ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);



5335 
5336   if (_is_hidden) {
5337     ik->set_is_hidden();
5338   }
5339 
5340   // Set PackageEntry for this_klass
5341   oop cl = ik->class_loader();
5342   Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5343   ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5344   ik->set_package(cld, NULL, CHECK);
5345 
5346   const Array<Method*>* const methods = ik->methods();
5347   assert(methods != NULL, "invariant");
5348   const int methods_len = methods->length();
5349 
5350   check_methods_for_intrinsics(ik, methods);
5351 
5352   // Fill in field values obtained by parse_classfile_attributes
5353   if (_parsed_annotations->has_any_annotations()) {
5354     _parsed_annotations->apply_to(ik);

5422 
5423   assert(_all_mirandas != NULL, "invariant");
5424 
5425   // Generate any default methods - default methods are public interface methods
5426   // that have a default implementation.  This is new with Java 8.
5427   if (_has_nonstatic_concrete_methods) {
5428     DefaultMethods::generate_default_methods(ik,
5429                                              _all_mirandas,
5430                                              CHECK);
5431   }
5432 
5433   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5434   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5435       !module_entry->has_default_read_edges()) {
5436     if (!module_entry->set_has_default_read_edges()) {
5437       // We won a potential race
5438       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5439     }
5440   }
5441 





































5442   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5443 
5444   if (!is_internal()) {
5445     ik->print_class_load_logging(_loader_data, module_entry, _stream);
5446 
5447     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5448         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5449         log_is_enabled(Info, class, preview)) {
5450       ResourceMark rm;
5451       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5452                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5453     }
5454 
5455     if (log_is_enabled(Debug, class, resolve))  {
5456       ResourceMark rm;
5457       // print out the superclass.
5458       const char * from = ik->external_name();
5459       if (ik->java_super() != NULL) {
5460         log_debug(class, resolve)("%s %s (super)",
5461                    from,

5467         const int length = local_interfaces->length();
5468         for (int i = 0; i < length; i++) {
5469           const InstanceKlass* const k = local_interfaces->at(i);
5470           const char * to = k->external_name();
5471           log_debug(class, resolve)("%s %s (interface)", from, to);
5472         }
5473       }
5474     }
5475   }
5476 
5477   JFR_ONLY(INIT_ID(ik);)
5478 
5479   // If we reach here, all is well.
5480   // Now remove the InstanceKlass* from the _klass_to_deallocate field
5481   // in order for it to not be destroyed in the ClassFileParser destructor.
5482   set_klass_to_deallocate(NULL);
5483 
5484   // it's official
5485   set_klass(ik);
5486 










5487   debug_only(ik->verify();)
5488 }
5489 
5490 void ClassFileParser::update_class_name(Symbol* new_class_name) {
5491   // Decrement the refcount in the old name, since we're clobbering it.
5492   _class_name->decrement_refcount();
5493 
5494   _class_name = new_class_name;
5495   // Increment the refcount of the new name.
5496   // Now the ClassFileParser owns this name and will decrement in
5497   // the destructor.
5498   _class_name->increment_refcount();
5499 }
5500 
5501 static bool relax_format_check_for(ClassLoaderData* loader_data) {
5502   bool trusted = loader_data->is_boot_class_loader_data() ||
5503                  loader_data->is_platform_class_loader_data();
5504   bool need_verify =
5505     // verifyAll
5506     (BytecodeVerificationLocal && BytecodeVerificationRemote) ||

5513                                  Symbol* name,
5514                                  ClassLoaderData* loader_data,
5515                                  const ClassLoadInfo* cl_info,
5516                                  Publicity pub_level,
5517                                  TRAPS) :
5518   _stream(stream),
5519   _class_name(NULL),
5520   _loader_data(loader_data),
5521   _is_hidden(cl_info->is_hidden()),
5522   _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5523   _orig_cp_size(0),
5524   _super_klass(),
5525   _cp(NULL),
5526   _fields(NULL),
5527   _methods(NULL),
5528   _inner_classes(NULL),
5529   _nest_members(NULL),
5530   _nest_host(0),
5531   _permitted_subclasses(NULL),
5532   _record_components(NULL),

5533   _local_interfaces(NULL),
5534   _transitive_interfaces(NULL),
5535   _combined_annotations(NULL),
5536   _class_annotations(NULL),
5537   _class_type_annotations(NULL),
5538   _fields_annotations(NULL),
5539   _fields_type_annotations(NULL),
5540   _klass(NULL),
5541   _klass_to_deallocate(NULL),
5542   _parsed_annotations(NULL),
5543   _fac(NULL),
5544   _field_info(NULL),
5545   _method_ordering(NULL),
5546   _all_mirandas(NULL),
5547   _vtable_size(0),
5548   _itable_size(0),
5549   _num_miranda_methods(0),
5550   _rt(REF_NONE),
5551   _protection_domain(cl_info->protection_domain()),
5552   _access_flags(),
5553   _pub_level(pub_level),
5554   _bad_constant_seen(0),
5555   _synthetic_flag(false),
5556   _sde_length(false),
5557   _sde_buffer(NULL),
5558   _sourcefile_index(0),
5559   _generic_signature_index(0),
5560   _major_version(0),
5561   _minor_version(0),
5562   _this_class_index(0),
5563   _super_class_index(0),
5564   _itfs_len(0),
5565   _java_fields_count(0),
5566   _need_verify(false),
5567   _relax_verify(false),
5568   _has_nonstatic_concrete_methods(false),
5569   _declares_nonstatic_concrete_methods(false),
5570   _has_final_method(false),
5571   _has_contended_fields(false),











5572   _has_finalizer(false),
5573   _has_empty_finalizer(false),
5574   _has_vanilla_constructor(false),
5575   _max_bootstrap_specifier_index(-1) {
5576 
5577   _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
5578   _class_name->increment_refcount();
5579 
5580   assert(_loader_data != NULL, "invariant");
5581   assert(stream != NULL, "invariant");
5582   assert(_stream != NULL, "invariant");
5583   assert(_stream->buffer() == _stream->current(), "invariant");
5584   assert(_class_name != NULL, "invariant");
5585   assert(0 == _access_flags.as_int(), "invariant");
5586 
5587   // Figure out whether we can skip format checking (matching classic VM behavior)
5588   if (DumpSharedSpaces) {
5589     // verify == true means it's a 'remote' class (i.e., non-boot class)
5590     // Verification decision is based on BytecodeVerificationRemote flag
5591     // for those classes.

5724     cp_size, CHECK);
5725 
5726   _orig_cp_size = cp_size;
5727   if (is_hidden()) { // Add a slot for hidden class name.
5728     cp_size++;
5729   }
5730 
5731   _cp = ConstantPool::allocate(_loader_data,
5732                                cp_size,
5733                                CHECK);
5734 
5735   ConstantPool* const cp = _cp;
5736 
5737   parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
5738 
5739   assert(cp_size == (const u2)cp->length(), "invariant");
5740 
5741   // ACCESS FLAGS
5742   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
5743 
5744   // Access flags
5745   jint flags;
5746   // JVM_ACC_MODULE is defined in JDK-9 and later.
5747   if (_major_version >= JAVA_9_VERSION) {
5748     flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5749   } else {
5750     flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5751   }







5752 
5753   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5754     // Set abstract bit for old class files for backward compatibility
5755     flags |= JVM_ACC_ABSTRACT;
5756   }
5757 
5758   verify_legal_class_modifiers(flags, CHECK);
5759 
5760   short bad_constant = class_bad_constant_seen();
5761   if (bad_constant != 0) {
5762     // Do not throw CFE until after the access_flags are checked because if
5763     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5764     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5765     return;
5766   }
5767 
5768   _access_flags.set_flags(flags);
5769 
5770   // This class and superclass
5771   _this_class_index = stream->get_u2_fast();

5838       if (stream->source() != NULL) {
5839         ls.print(" source: %s", stream->source());
5840       }
5841       ls.cr();
5842     }
5843   }
5844 
5845   // SUPERKLASS
5846   _super_class_index = stream->get_u2_fast();
5847   _super_klass = parse_super_class(cp,
5848                                    _super_class_index,
5849                                    _need_verify,
5850                                    CHECK);
5851 
5852   // Interfaces
5853   _itfs_len = stream->get_u2_fast();
5854   parse_interfaces(stream,
5855                    _itfs_len,
5856                    cp,
5857                    &_has_nonstatic_concrete_methods,

5858                    CHECK);
5859 
5860   assert(_local_interfaces != NULL, "invariant");
5861 
5862   // Fields (offsets are filled in later)
5863   _fac = new FieldAllocationCount();
5864   parse_fields(stream,
5865                _access_flags.is_interface(),

5866                _fac,
5867                cp,
5868                cp_size,
5869                &_java_fields_count,
5870                CHECK);
5871 
5872   assert(_fields != NULL, "invariant");
5873 
5874   // Methods
5875   AccessFlags promoted_flags;
5876   parse_methods(stream,
5877                 _access_flags.is_interface(),

5878                 &promoted_flags,
5879                 &_has_final_method,
5880                 &_declares_nonstatic_concrete_methods,
5881                 CHECK);
5882 
5883   assert(_methods != NULL, "invariant");
5884 
5885   // promote flags from parse_methods() to the klass' flags
5886   _access_flags.add_promoted_flags(promoted_flags.as_int());
5887 
5888   if (_declares_nonstatic_concrete_methods) {
5889     _has_nonstatic_concrete_methods = true;
5890   }
5891 
5892   // Additional attributes/annotations
5893   _parsed_annotations = new ClassAnnotationCollector();
5894   parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
5895 
5896   assert(_inner_classes != NULL, "invariant");
5897 

5939 
5940   // Update this_class_index's slot in the constant pool with the new Utf8 entry.
5941   // We have to update the resolved_klass_index and the name_index together
5942   // so extract the existing resolved_klass_index first.
5943   CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
5944   int resolved_klass_index = cp_klass_slot.resolved_klass_index();
5945   _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
5946   assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
5947          "Bad name_index");
5948 }
5949 
5950 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
5951                                                  ConstantPool* cp,
5952                                                  TRAPS) {
5953   assert(stream != NULL, "invariant");
5954   assert(stream->at_eos(), "invariant");
5955   assert(cp != NULL, "invariant");
5956   assert(_loader_data != NULL, "invariant");
5957 
5958   if (_class_name == vmSymbols::java_lang_Object()) {
5959     check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
5960                    "java.lang.Object cannot implement an interface in class file %s",
5961                    CHECK);
5962   }
5963   // We check super class after class file is parsed and format is checked
5964   if (_super_class_index > 0 && NULL == _super_klass) {
5965     Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
5966     if (_access_flags.is_interface()) {
5967       // Before attempting to resolve the superclass, check for class format
5968       // errors not checked yet.
5969       guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
5970         "Interfaces must have java.lang.Object as superclass in class file %s",
5971         CHECK);
5972     }
5973     Handle loader(THREAD, _loader_data->class_loader());
5974     _super_klass = (const InstanceKlass*)
5975                        SystemDictionary::resolve_super_or_fail(_class_name,
5976                                                                super_class_name,
5977                                                                loader,
5978                                                                _protection_domain,
5979                                                                true,
5980                                                                CHECK);
5981   }
5982 
5983   if (_super_klass != NULL) {
5984     if (_super_klass->has_nonstatic_concrete_methods()) {
5985       _has_nonstatic_concrete_methods = true;
5986     }



5987 
5988     if (_super_klass->is_interface()) {
5989       classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
5990       return;
5991     }































5992   }
5993 

































5994   // Compute the transitive list of all unique interfaces implemented by this class
5995   _transitive_interfaces =
5996     compute_transitive_interfaces(_super_klass,
5997                                   _local_interfaces,
5998                                   _loader_data,
5999                                   CHECK);
6000 
6001   assert(_transitive_interfaces != NULL, "invariant");
6002 
6003   // sort methods
6004   _method_ordering = sort_methods(_methods);
6005 
6006   _all_mirandas = new GrowableArray<Method*>(20);
6007 
6008   Handle loader(THREAD, _loader_data->class_loader());
6009   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6010                                                     &_num_miranda_methods,
6011                                                     _all_mirandas,
6012                                                     _super_klass,
6013                                                     _methods,
6014                                                     _access_flags,
6015                                                     _major_version,
6016                                                     loader,
6017                                                     _class_name,
6018                                                     _local_interfaces);
6019 
6020   // Size of Java itable (in words)
6021   _itable_size = _access_flags.is_interface() ? 0 :
6022     klassItable::compute_itable_size(_transitive_interfaces);
6023 
6024   assert(_fac != NULL, "invariant");
6025   assert(_parsed_annotations != NULL, "invariant");
6026 





















6027   _field_info = new FieldLayoutInfo();
6028   FieldLayoutBuilder lb(class_name(), super_klass(), _cp, _fields,
6029                         _parsed_annotations->is_contended(), _field_info);
6030   lb.build_layout();







6031 
6032   // Compute reference typ
6033   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6034 
6035 }
6036 
6037 void ClassFileParser::set_klass(InstanceKlass* klass) {
6038 
6039 #ifdef ASSERT
6040   if (klass != NULL) {
6041     assert(NULL == _klass, "leaking?");
6042   }
6043 #endif
6044 
6045   _klass = klass;
6046 }
6047 
6048 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6049 
6050 #ifdef ASSERT
6051   if (klass != NULL) {
6052     assert(NULL == _klass_to_deallocate, "leaking?");
6053   }
6054 #endif
6055 
6056   _klass_to_deallocate = klass;
6057 }
6058 
6059 // Caller responsible for ResourceMark
6060 // clone stream with rewound position
6061 const ClassFileStream* ClassFileParser::clone_stream() const {
6062   assert(_stream != NULL, "invariant");
6063 
6064   return _stream->clone();
6065 }

6066 // ----------------------------------------------------------------------------
6067 // debugging
6068 
6069 #ifdef ASSERT
6070 
6071 // return true if class_name contains no '.' (internal format is '/')
6072 bool ClassFileParser::is_internal_format(Symbol* class_name) {
6073   if (class_name != NULL) {
6074     ResourceMark rm;
6075     char* name = class_name->as_C_string();
6076     return strchr(name, JVM_SIGNATURE_DOT) == NULL;
6077   } else {
6078     return true;
6079   }
6080 }
6081 
6082 #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 "classfile/classFileParser.hpp"
  28 #include "classfile/classFileStream.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/classLoaderData.inline.hpp"
  31 #include "classfile/classLoadInfo.hpp"
  32 #include "classfile/defaultMethods.hpp"
  33 #include "classfile/fieldLayoutBuilder.hpp"
  34 #include "classfile/javaClasses.inline.hpp"
  35 #include "classfile/moduleEntry.hpp"
  36 #include "classfile/packageEntry.hpp"
  37 #include "classfile/symbolTable.hpp"
  38 #include "classfile/systemDictionary.hpp"
  39 #include "classfile/verificationType.hpp"
  40 #include "classfile/verifier.hpp"
  41 #include "classfile/vmClasses.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.inline.hpp"
  53 #include "oops/inlineKlass.inline.hpp"
  54 #include "oops/instanceKlass.inline.hpp"
  55 #include "oops/instanceMirrorKlass.hpp"
  56 #include "oops/klass.inline.hpp"
  57 #include "oops/klassVtable.hpp"
  58 #include "oops/metadata.hpp"
  59 #include "oops/method.inline.hpp"
  60 #include "oops/oop.inline.hpp"
  61 #include "oops/recordComponent.hpp"
  62 #include "oops/symbol.hpp"
  63 #include "prims/jvmtiExport.hpp"
  64 #include "prims/jvmtiThreadState.hpp"
  65 #include "runtime/arguments.hpp"
  66 #include "runtime/fieldDescriptor.inline.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/javaCalls.hpp"
  69 #include "runtime/os.hpp"
  70 #include "runtime/perfData.hpp"
  71 #include "runtime/reflection.hpp"
  72 #include "runtime/safepointVerifiers.hpp"
  73 #include "runtime/signature.hpp"
  74 #include "runtime/timer.hpp"
  75 #include "services/classLoadingService.hpp"
  76 #include "services/threadService.hpp"
  77 #include "utilities/align.hpp"
  78 #include "utilities/bitMap.inline.hpp"
  79 #include "utilities/copy.hpp"
  80 #include "utilities/formatBuffer.hpp"
  81 #include "utilities/exceptions.hpp"
  82 #include "utilities/globalDefinitions.hpp"
  83 #include "utilities/growableArray.hpp"
  84 #include "utilities/macros.hpp"
  85 #include "utilities/ostream.hpp"
  86 #include "utilities/resourceHash.hpp"
  87 #include "utilities/stringUtils.hpp"
  88 #include "utilities/utf8.hpp"
  89 
  90 #if INCLUDE_CDS
  91 #include "classfile/systemDictionaryShared.hpp"
  92 #endif
  93 #if INCLUDE_JFR
  94 #include "jfr/support/jfrTraceIdExtension.hpp"
  95 #endif
  96 
  97 // We generally try to create the oops directly when parsing, rather than
  98 // allocating temporary data structures and copying the bytes twice. A
  99 // temporary area is only needed when parsing utf8 entries in the constant
 100 // pool and when parsing line number tables.
 101 
 102 // We add assert in debug mode when class format is not checked.
 103 
 104 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
 105 #define JAVA_MIN_SUPPORTED_VERSION        45
 106 #define JAVA_PREVIEW_MINOR_VERSION        65535
 107 

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

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

 695           cp->signature_ref_index_at(name_and_type_ref_index);
 696         const Symbol* const name = cp->symbol_at(name_ref_index);
 697         const Symbol* const signature = cp->symbol_at(signature_ref_index);
 698         if (tag == JVM_CONSTANT_Fieldref) {
 699           if (_need_verify) {
 700             // Field name and signature are verified above, when iterating NameAndType_info.
 701             // Need only to be sure signature is non-zero length and the right type.
 702             if (Signature::is_method(signature)) {
 703               throwIllegalSignature("Field", name, signature, CHECK);
 704             }
 705           }
 706         } else {
 707           if (_need_verify) {
 708             // Method name and signature are individually verified above, when iterating
 709             // NameAndType_info.  Need to check here that signature is non-zero length and
 710             // the right type.
 711             if (!Signature::is_method(signature)) {
 712               throwIllegalSignature("Method", name, signature, CHECK);
 713             }
 714           }
 715           // If a class method name begins with '<', it must be "<init>" and have void signature
 716           // unless it's an inline type.
 717           const unsigned int name_len = name->utf8_length();
 718           if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
 719               name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
 720             if (name != vmSymbols::object_initializer_name()) {
 721               classfile_parse_error(
 722                 "Bad method name at constant pool index %u in class file %s",
 723                 name_ref_index, THREAD);
 724               return;
 725             } else if (!Signature::is_void_method(signature)) {
 726               // if return type is non-void then it cannot be a basic primitive
 727               // and primitve types must be supported.
 728               if (!signature->ends_with(JVM_SIGNATURE_ENDCLASS) || !EnableValhalla) {
 729                 throwIllegalSignature("Method", name, signature, CHECK);
 730               }
 731             }
 732           }
 733         }
 734         break;
 735       }
 736       case JVM_CONSTANT_MethodHandle: {
 737         const int ref_index = cp->method_handle_index_at(index);
 738         const int ref_kind = cp->method_handle_ref_kind_at(index);
 739         switch (ref_kind) {
 740           case JVM_REF_invokeVirtual:
 741           case JVM_REF_invokeStatic:
 742           case JVM_REF_invokeSpecial:
 743           case JVM_REF_newInvokeSpecial: {
 744             const int name_and_type_ref_index =
 745               cp->name_and_type_ref_index_at(ref_index);
 746             const int name_ref_index =
 747               cp->name_ref_index_at(name_and_type_ref_index);
 748             const Symbol* const name = cp->symbol_at(name_ref_index);
 749             if (name != vmSymbols::object_initializer_name()) {
 750               if (ref_kind == JVM_REF_newInvokeSpecial) {
 751                 classfile_parse_error(
 752                   "Bad constructor name at constant pool index %u in class file %s",
 753                     name_ref_index, THREAD);
 754                 return;
 755               }
 756             } else {
 757               // The allowed invocation mode of <init> depends on its signature.
 758               // This test corresponds to verify_invoke_instructions in the verifier.
 759               const int signature_ref_index =
 760                 cp->signature_ref_index_at(name_and_type_ref_index);
 761               const Symbol* const signature = cp->symbol_at(signature_ref_index);
 762               if (signature->is_void_method_signature()
 763                   && ref_kind == JVM_REF_newInvokeSpecial) {
 764                 // OK, could be a constructor call
 765               } else if (!signature->is_void_method_signature()
 766                          && ref_kind == JVM_REF_invokeStatic) {
 767                 // also OK, could be a static factory call
 768               } else {
 769                 classfile_parse_error(
 770                   "Bad method name at constant pool index %u in class file %s",
 771                   name_ref_index, THREAD);
 772                 return;
 773               }
 774             }
 775             break;
 776           }
 777           // Other ref_kinds are already fully checked in previous pass.
 778         } // switch(ref_kind)
 779         break;
 780       }
 781       case JVM_CONSTANT_MethodType: {
 782         const Symbol* const no_name = vmSymbols::type_name(); // place holder
 783         const Symbol* const signature = cp->method_type_signature_at(index);
 784         verify_legal_method_signature(no_name, signature, CHECK);
 785         break;
 786       }
 787       case JVM_CONSTANT_Utf8: {
 788         assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");

 826   while (entry != NULL) {
 827     if (entry->_name == name && entry->_sig == sig) {
 828       return false;
 829     }
 830     entry = entry->_next;
 831   }
 832 
 833   // No duplicate is found, allocate a new entry and fill it.
 834   entry = new NameSigHash();
 835   entry->_name = name;
 836   entry->_sig = sig;
 837 
 838   // Insert into hash table
 839   entry->_next = table[index];
 840   table[index] = entry;
 841 
 842   return true;
 843 }
 844 
 845 // Side-effects: populates the _local_interfaces field
 846 void ClassFileParser::parse_interfaces(const ClassFileStream* stream,
 847                                        int itfs_len,
 848                                        ConstantPool* cp,
 849                                        bool* const has_nonstatic_concrete_methods,
 850                                        // FIXME: lots of these functions
 851                                        // declare their parameters as const,
 852                                        // which adds only noise to the code.
 853                                        // Remove the spurious const modifiers.
 854                                        // Many are of the form "const int x"
 855                                        // or "T* const x".
 856                                        bool* const is_declared_atomic,
 857                                        TRAPS) {
 858   assert(stream != NULL, "invariant");
 859   assert(cp != NULL, "invariant");
 860   assert(has_nonstatic_concrete_methods != NULL, "invariant");
 861 
 862   if (itfs_len == 0) {
 863     _temp_local_interfaces = new GrowableArray<InstanceKlass*>(0);
 864   } else {
 865     assert(itfs_len > 0, "only called for len>0");
 866     _temp_local_interfaces = new GrowableArray<InstanceKlass*>(itfs_len);
 867     int index = 0;

 868     for (index = 0; index < itfs_len; index++) {
 869       const u2 interface_index = stream->get_u2(CHECK);
 870       Klass* interf;
 871       check_property(
 872         valid_klass_reference_at(interface_index),
 873         "Interface name has bad constant pool index %u in class file %s",
 874         interface_index, CHECK);
 875       if (cp->tag_at(interface_index).is_klass()) {
 876         interf = cp->resolved_klass_at(interface_index);
 877       } else {
 878         Symbol* const unresolved_klass  = cp->klass_name_at(interface_index);
 879 
 880         // Don't need to check legal name because it's checked when parsing constant pool.
 881         // But need to make sure it's not an array type.
 882         guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
 883                            "Bad interface name in class file %s", CHECK);
 884 
 885         // Call resolve_super so class circularity is checked
 886         interf = SystemDictionary::resolve_super_or_fail(
 887                                                   _class_name,
 888                                                   unresolved_klass,
 889                                                   Handle(THREAD, _loader_data->class_loader()),
 890                                                   _protection_domain,
 891                                                   false,
 892                                                   CHECK);
 893       }
 894 
 895       if (!interf->is_interface()) {
 896         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
 897                   err_msg("class %s can not implement %s, because it is not an interface (%s)",
 898                           _class_name->as_klass_external_name(),
 899                           interf->external_name(),
 900                           interf->class_in_module_of_loader()));
 901       }
 902 
 903       InstanceKlass* ik = InstanceKlass::cast(interf);
 904       if (is_inline_type() && ik->invalid_inline_super()) {
 905         ResourceMark rm(THREAD);
 906         Exceptions::fthrow(
 907           THREAD_AND_LOCATION,
 908           vmSymbols::java_lang_IncompatibleClassChangeError(),
 909           "Inline type %s attempts to implement interface java.lang.IdentityObject",
 910           _class_name->as_klass_external_name());
 911         return;
 912       }
 913       if (ik->invalid_inline_super()) {
 914         set_invalid_inline_super();
 915       }
 916       if (ik->has_nonstatic_concrete_methods()) {
 917         *has_nonstatic_concrete_methods = true;
 918       }
 919       if (ik->is_declared_atomic()) {
 920         *is_declared_atomic = true;
 921       }
 922       if (ik->name() == vmSymbols::java_lang_IdentityObject()) {
 923         _implements_identityObject = true;
 924       }
 925       if (ik->name() == vmSymbols::java_lang_PrimitiveObject()) {
 926         // further checks for "is_invalid_super_for_inline_type()" needed later
 927         // needs field parsing, delay unitl post_process_parse_stream()
 928         _implements_primitiveObject = true;
 929       }
 930       _temp_local_interfaces->append(ik);
 931     }
 932 
 933     if (!_need_verify || itfs_len <= 1) {
 934       return;
 935     }
 936 
 937     // Check if there's any duplicates in interfaces
 938     ResourceMark rm(THREAD);
 939     NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
 940                                                                  NameSigHash*,
 941                                                                  HASH_ROW_SIZE);
 942     initialize_hashtable(interface_names);
 943     bool dup = false;
 944     const Symbol* name = NULL;
 945     {
 946       debug_only(NoSafepointVerifier nsv;)
 947       for (index = 0; index < itfs_len; index++) {
 948         const InstanceKlass* const k = _temp_local_interfaces->at(index);
 949         name = k->name();
 950         // If no duplicates, add (name, NULL) in hashtable interface_names.
 951         if (!put_after_lookup(name, NULL, interface_names)) {
 952           dup = true;
 953           break;
 954         }
 955       }
 956     }
 957     if (dup) {
 958       classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
 959                              name->as_C_string(), THREAD);
 960     }
 961   }
 962 }
 963 
 964 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
 965                                            int constantvalue_index,
 966                                            int signature_index,
 967                                            TRAPS) const {
 968   // Make sure the constant pool entry is of a type appropriate to this field

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


1590     access_flags.set_flags(flags);
1591 
1592     const u2 name_index = cfs->get_u2_fast();
1593     check_property(valid_symbol_at(name_index),
1594       "Invalid constant pool index %u for field name in class file %s",
1595       name_index, CHECK);
1596     const Symbol* const name = cp->symbol_at(name_index);
1597     verify_legal_field_name(name, CHECK);
1598 
1599     const u2 signature_index = cfs->get_u2_fast();
1600     check_property(valid_symbol_at(signature_index),
1601       "Invalid constant pool index %u for field signature in class file %s",
1602       signature_index, CHECK);
1603     const Symbol* const sig = cp->symbol_at(signature_index);
1604     verify_legal_field_signature(name, sig, CHECK);
1605     if (!access_flags.is_static()) instance_fields_count++;
1606 
1607     u2 constantvalue_index = 0;
1608     bool is_synthetic = false;
1609     u2 generic_signature_index = 0;
1610     const bool is_static = access_flags.is_static();
1611     FieldAnnotationCollector parsed_annotations(_loader_data);
1612 
1613     const u2 attributes_count = cfs->get_u2_fast();
1614     if (attributes_count > 0) {
1615       parse_field_attributes(cfs,
1616                              attributes_count,
1617                              is_static,
1618                              signature_index,
1619                              &constantvalue_index,
1620                              &is_synthetic,
1621                              &generic_signature_index,
1622                              &parsed_annotations,
1623                              CHECK);
1624 
1625       if (parsed_annotations.field_annotations() != NULL) {

1645 
1646       if (is_synthetic) {
1647         access_flags.set_is_synthetic();
1648       }
1649       if (generic_signature_index != 0) {
1650         access_flags.set_field_has_generic_signature();
1651         fa[generic_signature_slot] = generic_signature_index;
1652         generic_signature_slot ++;
1653         num_generic_signature ++;
1654       }
1655     }
1656 
1657     FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1658     field->initialize(access_flags.as_short(),
1659                       name_index,
1660                       signature_index,
1661                       constantvalue_index);
1662     const BasicType type = cp->basic_type_for_signature_at(signature_index);
1663 
1664     // Update FieldAllocationCount for this kind of field
1665     fac->update(is_static, type, type == T_INLINE_TYPE);
1666 
1667     // After field is initialized with type, we can augment it with aux info
1668     if (parsed_annotations.has_any_annotations()) {
1669       parsed_annotations.apply_to(field);
1670       if (field->is_contended()) {
1671         _has_contended_fields = true;
1672       }
1673     }
1674   }
1675 
1676   int index = length;
1677   if (num_injected != 0) {
1678     for (int n = 0; n < num_injected; n++) {
1679       // Check for duplicates
1680       if (injected[n].may_be_java) {
1681         const Symbol* const name      = injected[n].name();
1682         const Symbol* const signature = injected[n].signature();
1683         bool duplicate = false;
1684         for (int i = 0; i < length; i++) {
1685           const FieldInfo* const f = FieldInfo::from_field_array(fa, i);

1689             duplicate = true;
1690             break;
1691           }
1692         }
1693         if (duplicate) {
1694           // These will be removed from the field array at the end
1695           continue;
1696         }
1697       }
1698 
1699       // Injected field
1700       FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1701       field->initialize((u2)JVM_ACC_FIELD_INTERNAL,
1702                         (u2)(injected[n].name_index),
1703                         (u2)(injected[n].signature_index),
1704                         0);
1705 
1706       const BasicType type = Signature::basic_type(injected[n].signature());
1707 
1708       // Update FieldAllocationCount for this kind of field
1709       fac->update(false, type, false);
1710       index++;
1711     }
1712   }
1713 
1714   if (is_inline_type) {
1715     FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1716     field->initialize(JVM_ACC_FIELD_INTERNAL | JVM_ACC_STATIC,
1717                       (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(default_value_name)),
1718                       (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(object_signature)),
1719                       0);
1720     const BasicType type = Signature::basic_type(vmSymbols::object_signature());
1721     fac->update(true, type, false);
1722     index++;
1723   }
1724 
1725   if (is_inline_type && instance_fields_count == 0) {
1726     _is_empty_inline_type = true;
1727     FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1728     field->initialize(JVM_ACC_FIELD_INTERNAL,
1729         (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(empty_marker_name)),
1730         (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(byte_signature)),
1731         0);
1732     const BasicType type = Signature::basic_type(vmSymbols::byte_signature());
1733     fac->update(false, type, false);
1734     index++;
1735   }
1736 
1737   if (instance_fields_count > 0) {
1738     _has_nonstatic_fields = true;
1739   }
1740 
1741   assert(NULL == _fields, "invariant");
1742 
1743   _fields =
1744     MetadataFactory::new_array<u2>(_loader_data,
1745                                    index * FieldInfo::field_slots + num_generic_signature,
1746                                    CHECK);
1747   // Sometimes injected fields already exist in the Java source so
1748   // the fields array could be too long.  In that case the
1749   // fields array is trimed. Also unused slots that were reserved
1750   // for generic signature indexes are discarded.
1751   {
1752     int i = 0;
1753     for (; i < index * FieldInfo::field_slots; i++) {
1754       _fields->at_put(i, fa[i]);
1755     }
1756     for (int j = total_fields * FieldInfo::field_slots;
1757          j < generic_signature_slot; j++) {
1758       _fields->at_put(i++, fa[j]);
1759     }
1760     assert(_fields->length() == i, "");

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

2336                              runtime_visible_type_annotations_length,
2337                              runtime_invisible_type_annotations,
2338                              runtime_invisible_type_annotations_length,
2339                              CHECK);
2340     cm->set_type_annotations(a);
2341   }
2342 }
2343 
2344 
2345 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2346 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2347 // Method* to save footprint, so we only know the size of the resulting Method* when the
2348 // entire method attribute is parsed.
2349 //
2350 // The promoted_flags parameter is used to pass relevant access_flags
2351 // from the method back up to the containing klass. These flag values
2352 // are added to klass's access_flags.
2353 
2354 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2355                                       bool is_interface,
2356                                       bool is_inline_type,
2357                                       const ConstantPool* cp,
2358                                       AccessFlags* const promoted_flags,
2359                                       TRAPS) {
2360   assert(cfs != NULL, "invariant");
2361   assert(cp != NULL, "invariant");
2362   assert(promoted_flags != NULL, "invariant");
2363 
2364   ResourceMark rm(THREAD);
2365   // Parse fixed parts:
2366   // access_flags, name_index, descriptor_index, attributes_count
2367   cfs->guarantee_more(8, CHECK_NULL);
2368 
2369   int flags = cfs->get_u2_fast();
2370   const u2 name_index = cfs->get_u2_fast();
2371   const int cp_size = cp->length();
2372   check_property(
2373     valid_symbol_at(name_index),
2374     "Illegal constant pool index %u for method name in class file %s",
2375     name_index, CHECK_NULL);
2376   const Symbol* const name = cp->symbol_at(name_index);

2378 
2379   const u2 signature_index = cfs->get_u2_fast();
2380   guarantee_property(
2381     valid_symbol_at(signature_index),
2382     "Illegal constant pool index %u for method signature in class file %s",
2383     signature_index, CHECK_NULL);
2384   const Symbol* const signature = cp->symbol_at(signature_index);
2385 
2386   if (name == vmSymbols::class_initializer_name()) {
2387     // We ignore the other access flags for a valid class initializer.
2388     // (JVM Spec 2nd ed., chapter 4.6)
2389     if (_major_version < 51) { // backward compatibility
2390       flags = JVM_ACC_STATIC;
2391     } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2392       flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2393     } else {
2394       classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2395       return NULL;
2396     }
2397   } else {
2398     verify_legal_method_modifiers(flags, is_interface, is_inline_type, name, CHECK_NULL);
2399   }
2400 
2401   if (name == vmSymbols::object_initializer_name()) {
2402     if (is_interface) {
2403       classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2404       return NULL;
2405     } else if (!is_inline_type && signature->is_void_method_signature()) {
2406       // OK, a constructor
2407     } else if (is_inline_type && !signature->is_void_method_signature()) {
2408       // also OK, a static factory, as long as the return value is good
2409       bool ok = false;
2410       SignatureStream ss((Symbol*) signature, true);
2411       while (!ss.at_return_type())  ss.next();
2412       if (ss.is_reference()) {
2413         Symbol* ret = ss.as_symbol();
2414         const Symbol* required = class_name();
2415         if (is_hidden()) {
2416           // The original class name in hidden classes gets changed.  So using
2417           // the original name in the return type is no longer valid.
2418           // Note that expecting the return type for inline hidden class factory
2419           // methods to be java.lang.Object works around a JVM Spec issue for
2420           // hidden classes.
2421           required = vmSymbols::java_lang_Object();
2422         }
2423         ok = (ret == required);
2424       }
2425       if (!ok) {
2426         throwIllegalSignature("Method", name, signature, CHECK_0);
2427       }
2428     } else {
2429       // not OK, so throw the same error as in verify_legal_method_signature.
2430       throwIllegalSignature("Method", name, signature, CHECK_0);
2431     }
2432     // A declared <init> method must always be either a non-static
2433     // object constructor, with a void return, or else it must be a
2434     // static factory method, with a non-void return.  No other
2435     // definition of <init> is possible.
2436     //
2437     // The verifier (in verify_invoke_instructions) will inspect the
2438     // signature of any attempt to invoke <init>, and ensures that it
2439     // returns non-void if and only if it is being invoked by
2440     // invokestatic, and void if and only if it is being invoked by
2441     // invokespecial.
2442     //
2443     // When a symbolic reference to <init> is resolved for a
2444     // particular invocation mode (special or static), the mode is
2445     // matched to the JVM_ACC_STATIC modifier of the <init> method.
2446     // Thus, it is impossible to statically invoke a constructor, and
2447     // impossible to "new + invokespecial" a static factory, either
2448     // through bytecode or through reflection.
2449   }
2450 
2451   int args_size = -1;  // only used when _need_verify is true
2452   if (_need_verify) {
2453     verify_legal_name_with_signature(name, signature, CHECK_NULL);
2454     args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2455                  verify_legal_method_signature(name, signature, CHECK_NULL);
2456     if (args_size > MAX_ARGS_SIZE) {
2457       classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2458       return NULL;
2459     }
2460   }
2461 
2462   AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2463 
2464   // Default values for code and exceptions attribute elements
2465   u2 max_stack = 0;
2466   u2 max_locals = 0;
2467   u4 code_length = 0;
2468   const u1* code_start = 0;

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

3289       valid_klass_reference_at(inner_class_info_index),
3290       "inner_class_info_index %u has bad constant type in class file %s",
3291       inner_class_info_index, CHECK_0);
3292     // Outer class index
3293     const u2 outer_class_info_index = cfs->get_u2_fast();
3294     check_property(
3295       outer_class_info_index == 0 ||
3296         valid_klass_reference_at(outer_class_info_index),
3297       "outer_class_info_index %u has bad constant type in class file %s",
3298       outer_class_info_index, CHECK_0);
3299     // Inner class name
3300     const u2 inner_name_index = cfs->get_u2_fast();
3301     check_property(
3302       inner_name_index == 0 || valid_symbol_at(inner_name_index),
3303       "inner_name_index %u has bad constant type in class file %s",
3304       inner_name_index, CHECK_0);
3305     if (_need_verify) {
3306       guarantee_property(inner_class_info_index != outer_class_info_index,
3307                          "Class is both outer and inner class in class file %s", CHECK_0);
3308     }
3309 
3310     jint recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS;
3311     // JVM_ACC_MODULE is defined in JDK-9 and later.
3312     if (_major_version >= JAVA_9_VERSION) {
3313       recognized_modifiers |= JVM_ACC_MODULE;


3314     }
3315     // JVM_ACC_INLINE is defined for class file version 55 and later
3316     if (supports_inline_types()) {
3317       recognized_modifiers |= JVM_ACC_INLINE;
3318     }
3319 
3320     // Access flags
3321     jint flags = cfs->get_u2_fast() & recognized_modifiers;
3322 
3323     if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3324       // Set abstract bit for old class files for backward compatibility
3325       flags |= JVM_ACC_ABSTRACT;
3326     }
3327     verify_legal_class_modifiers(flags, CHECK_0);
3328     AccessFlags inner_access_flags(flags);
3329 
3330     inner_classes->at_put(index++, inner_class_info_index);
3331     inner_classes->at_put(index++, outer_class_info_index);
3332     inner_classes->at_put(index++, inner_name_index);
3333     inner_classes->at_put(index++, inner_access_flags.as_short());
3334   }
3335 
3336   // 4347400: make sure there's no duplicate entry in the classes array
3337   // Also, check for circular entries.
3338   bool has_circularity = false;
3339   if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3340     has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3341     if (has_circularity) {
3342       // If circularity check failed then ignore InnerClasses attribute.

4148     }
4149     if (runtime_invisible_annotations != NULL) {
4150       for (int i = 0; i < runtime_invisible_annotations_length; i++) {
4151         int append = runtime_visible_annotations_length+i;
4152         annotations->at_put(append, runtime_invisible_annotations[i]);
4153       }
4154     }
4155   }
4156   return annotations;
4157 }
4158 
4159 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
4160                                                         const int super_class_index,
4161                                                         const bool need_verify,
4162                                                         TRAPS) {
4163   assert(cp != NULL, "invariant");
4164   const InstanceKlass* super_klass = NULL;
4165 
4166   if (super_class_index == 0) {
4167     check_property(_class_name == vmSymbols::java_lang_Object(),
4168                    "Invalid superclass index 0 in class file %s",

4169                    CHECK_NULL);
4170   } else {
4171     check_property(valid_klass_reference_at(super_class_index),
4172                    "Invalid superclass index %u in class file %s",
4173                    super_class_index,
4174                    CHECK_NULL);
4175     // The class name should be legal because it is checked when parsing constant pool.
4176     // However, make sure it is not an array type.
4177     bool is_array = false;
4178     if (cp->tag_at(super_class_index).is_klass()) {
4179       super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
4180       if (need_verify)
4181         is_array = super_klass->is_array_klass();
4182     } else if (need_verify) {
4183       is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
4184     }
4185     if (need_verify) {
4186       guarantee_property(!is_array,
4187                         "Bad superclass name in class file %s", CHECK_NULL);
4188     }

4289 }
4290 
4291 void OopMapBlocksBuilder::print_on(outputStream* st) const {
4292   st->print_cr("  OopMapBlocks: %3d  /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);
4293   if (_nonstatic_oop_map_count > 0) {
4294     OopMapBlock* map = _nonstatic_oop_maps;
4295     OopMapBlock* last_map = last_oop_map();
4296     assert(map <= last_map, "Last less than first");
4297     while (map <= last_map) {
4298       st->print_cr("    Offset: %3d  -%3d Count: %3d", map->offset(),
4299                    map->offset() + map->offset_span() - heapOopSize, map->count());
4300       map++;
4301     }
4302   }
4303 }
4304 
4305 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
4306   print_on(st);
4307 }
4308 
4309 void ClassFileParser::throwInlineTypeLimitation(THREAD_AND_LOCATION_DECL,
4310                                                 const char* msg,
4311                                                 const Symbol* name,
4312                                                 const Symbol* sig) const {
4313 
4314   ResourceMark rm(THREAD);
4315   if (name == NULL || sig == NULL) {
4316     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4317         vmSymbols::java_lang_ClassFormatError(),
4318         "class: %s - %s", _class_name->as_C_string(), msg);
4319   }
4320   else {
4321     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4322         vmSymbols::java_lang_ClassFormatError(),
4323         "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(),
4324         _class_name->as_C_string(), msg);
4325   }
4326 }
4327 
4328 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4329   assert(ik != NULL, "invariant");
4330 
4331   const Klass* const super = ik->super();
4332 
4333   // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4334   // in which case we don't have to register objects as finalizable
4335   if (!_has_empty_finalizer) {
4336     if (_has_finalizer ||
4337         (super != NULL && super->has_finalizer())) {
4338       ik->set_has_finalizer();
4339     }
4340   }
4341 
4342 #ifdef ASSERT
4343   bool f = false;
4344   const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4345                                            vmSymbols::void_method_signature());
4346   if (m != NULL && !m->is_empty_method()) {
4347       f = true;
4348   }
4349 
4350   // Spec doesn't prevent agent from redefinition of empty finalizer.
4351   // Despite the fact that it's generally bad idea and redefined finalizer
4352   // will not work as expected we shouldn't abort vm in this case
4353   if (!ik->has_redefined_this_or_super()) {
4354     assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4355   }
4356 #endif
4357 
4358   // Check if this klass supports the java.lang.Cloneable interface
4359   if (vmClasses::Cloneable_klass_loaded()) {
4360     if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4361       if (ik->is_inline_klass()) {
4362         JavaThread *THREAD = JavaThread::current();
4363         throwInlineTypeLimitation(THREAD_AND_LOCATION, "Inline Types do not support Cloneable");
4364         return;
4365       }
4366       ik->set_is_cloneable();
4367     }
4368   }
4369 
4370   // Check if this klass has a vanilla default constructor
4371   if (super == NULL) {
4372     // java.lang.Object has empty default constructor
4373     ik->set_has_vanilla_constructor();
4374   } else {
4375     if (super->has_vanilla_constructor() &&
4376         _has_vanilla_constructor) {
4377       ik->set_has_vanilla_constructor();
4378     }
4379 #ifdef ASSERT
4380     bool v = false;
4381     if (super->has_vanilla_constructor()) {
4382       const Method* const constructor =
4383         ik->find_method(vmSymbols::object_initializer_name(),
4384                        vmSymbols::void_method_signature());
4385       if (constructor != NULL && constructor->is_vanilla_constructor()) {
4386         v = true;
4387       }
4388     }
4389     assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4390 #endif
4391   }
4392 
4393   // If it cannot be fast-path allocated, set a bit in the layout helper.
4394   // See documentation of InstanceKlass::can_be_fastpath_allocated().
4395   assert(ik->size_helper() > 0, "layout_helper is initialized");
4396   if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4397       || ik->is_abstract() || ik->is_interface()
4398       || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4399       || ik->size_helper() >= FastAllocateSizeLimit) {
4400     // Forbid fast-path allocation.
4401     const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4402     ik->set_layout_helper(lh);
4403   }
4404 }
4405 
4406 bool ClassFileParser::supports_inline_types() const {
4407   // Inline types are only supported by class file version 55 and later
4408   return _major_version >= JAVA_11_VERSION;
4409 }
4410 
4411 // utility methods for appending an array with check for duplicates
4412 
4413 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4414                               const Array<InstanceKlass*>* const ifs) {
4415   // iterate over new interfaces
4416   for (int i = 0; i < ifs->length(); i++) {
4417     InstanceKlass* const e = ifs->at(i);
4418     assert(e->is_klass() && e->is_interface(), "just checking");
4419     // add new interface
4420     result->append_if_missing(e);
4421   }
4422 }
4423 
4424 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4425                                                             Array<InstanceKlass*>* local_ifs,
4426                                                             ClassLoaderData* loader_data,
4427                                                             TRAPS) {
4428   assert(local_ifs != NULL, "invariant");
4429   assert(loader_data != NULL, "invariant");
4430 

4434   // Add superclass transitive interfaces size
4435   if (super != NULL) {
4436     super_size = super->transitive_interfaces()->length();
4437     max_transitive_size += super_size;
4438   }
4439   // Add local interfaces' super interfaces
4440   const int local_size = local_ifs->length();
4441   for (int i = 0; i < local_size; i++) {
4442     InstanceKlass* const l = local_ifs->at(i);
4443     max_transitive_size += l->transitive_interfaces()->length();
4444   }
4445   // Finally add local interfaces
4446   max_transitive_size += local_size;
4447   // Construct array
4448   if (max_transitive_size == 0) {
4449     // no interfaces, use canonicalized array
4450     return Universe::the_empty_instance_klass_array();
4451   } else if (max_transitive_size == super_size) {
4452     // no new local interfaces added, share superklass' transitive interface array
4453     return super->transitive_interfaces();
4454     // The three lines below are commented to work around bug JDK-8245487
4455 //  } else if (max_transitive_size == local_size) {
4456 //    // only local interfaces added, share local interface array
4457 //    return local_ifs;
4458   } else {
4459     ResourceMark rm;
4460     GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4461 
4462     // Copy down from superclass
4463     if (super != NULL) {
4464       append_interfaces(result, super->transitive_interfaces());
4465     }
4466 
4467     // Copy down from local interfaces' superinterfaces
4468     for (int i = 0; i < local_size; i++) {
4469       InstanceKlass* const l = local_ifs->at(i);
4470       append_interfaces(result, l->transitive_interfaces());
4471     }
4472     // Finally add local interfaces
4473     append_interfaces(result, local_ifs);
4474 
4475     // length will be less than the max_transitive_size if duplicates were removed
4476     const int length = result->length();
4477     assert(length <= max_transitive_size, "just checking");
4478 
4479     if (length == 1 && result->at(0) == vmClasses::IdentityObject_klass()) {
4480       return Universe::the_single_IdentityObject_klass_array();
4481     }
4482     if (length == 1 && result->at(0) == vmClasses::PrimitiveObject_klass()) {
4483       return Universe::the_single_PrimitiveObject_klass_array();
4484     }
4485 
4486     Array<InstanceKlass*>* const new_result =
4487       MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4488     for (int i = 0; i < length; i++) {
4489       InstanceKlass* const e = result->at(i);
4490       assert(e != NULL, "just checking");
4491       new_result->at_put(i, e);
4492     }
4493     return new_result;
4494   }
4495 }
4496 
4497 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4498   assert(this_klass != NULL, "invariant");
4499   const Klass* const super = this_klass->super();
4500 
4501   if (super != NULL) {
4502     const InstanceKlass* super_ik = InstanceKlass::cast(super);
4503 
4504     if (super->is_final()) {
4505       classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);

4679     const Method* const m = methods->at(index);
4680     // if m is static and not the init method, throw a verify error
4681     if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4682       ResourceMark rm(THREAD);
4683       Exceptions::fthrow(
4684         THREAD_AND_LOCATION,
4685         vmSymbols::java_lang_VerifyError(),
4686         "Illegal static method %s in interface %s",
4687         m->name()->as_C_string(),
4688         this_klass->external_name()
4689       );
4690       return;
4691     }
4692   }
4693 }
4694 
4695 // utility methods for format checking
4696 
4697 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4698   const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4699   const bool is_inline_type = (flags & JVM_ACC_INLINE) != 0;
4700   assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4701   assert(supports_inline_types() || !is_inline_type, "JVM_ACC_INLINE should not be set");
4702   if (is_module) {
4703     ResourceMark rm(THREAD);
4704     Exceptions::fthrow(
4705       THREAD_AND_LOCATION,
4706       vmSymbols::java_lang_NoClassDefFoundError(),
4707       "%s is not a class because access_flag ACC_MODULE is set",
4708       _class_name->as_C_string());
4709     return;
4710   }
4711 
4712   if (is_inline_type && !EnableValhalla) {
4713     ResourceMark rm(THREAD);
4714     Exceptions::fthrow(
4715       THREAD_AND_LOCATION,
4716       vmSymbols::java_lang_ClassFormatError(),
4717       "Class modifier ACC_INLINE in class %s requires option -XX:+EnableValhalla",
4718       _class_name->as_C_string()
4719     );
4720     return;
4721   }
4722 
4723   if (!_need_verify) { return; }
4724 
4725   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4726   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4727   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4728   const bool is_super      = (flags & JVM_ACC_SUPER)      != 0;
4729   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4730   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4731   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4732   const bool major_gte_14  = _major_version >= JAVA_14_VERSION;
4733 
4734   if ((is_abstract && is_final) ||
4735       (is_interface && !is_abstract) ||
4736       (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4737       (!is_interface && major_gte_1_5 && is_annotation) ||
4738       (is_inline_type && (is_interface || is_abstract || is_enum || !is_final))) {
4739     ResourceMark rm(THREAD);
4740     const char* class_note = "";
4741     if (is_inline_type)  class_note = " (an inline class)";
4742     Exceptions::fthrow(
4743       THREAD_AND_LOCATION,
4744       vmSymbols::java_lang_ClassFormatError(),
4745       "Illegal class modifiers in class %s%s: 0x%X",
4746       _class_name->as_C_string(), class_note, flags
4747     );
4748     return;
4749   }
4750 }
4751 
4752 static bool has_illegal_visibility(jint flags) {
4753   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4754   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4755   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4756 
4757   return ((is_public && is_protected) ||
4758           (is_public && is_private) ||
4759           (is_protected && is_private));
4760 }
4761 
4762 // A legal major_version.minor_version must be one of the following:
4763 //
4764 //  Major_version >= 45 and major_version < 56, any minor_version.
4765 //  Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4766 //  Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.

4796         "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4797         "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4798         class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4799       return;
4800     }
4801 
4802     if (!Arguments::enable_preview()) {
4803       classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4804                            class_name, major, minor, THREAD);
4805       return;
4806     }
4807 
4808   } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4809     classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4810                          class_name, major, minor, THREAD);
4811   }
4812 }
4813 
4814 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4815                                                    bool is_interface,
4816                                                    bool is_inline_type,
4817                                                    TRAPS) const {
4818   if (!_need_verify) { return; }
4819 
4820   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4821   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4822   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4823   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4824   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4825   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4826   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4827   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4828   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4829 
4830   bool is_illegal = false;
4831 
4832   if (is_interface) {
4833     if (!is_public || !is_static || !is_final || is_private ||
4834         is_protected || is_volatile || is_transient ||
4835         (major_gte_1_5 && is_enum)) {
4836       is_illegal = true;
4837     }
4838   } else { // not interface
4839     if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4840       is_illegal = true;
4841     } else {
4842       if (is_inline_type && !is_static && !is_final) {
4843         is_illegal = true;
4844       }
4845     }
4846   }
4847 
4848   if (is_illegal) {
4849     ResourceMark rm(THREAD);
4850     Exceptions::fthrow(
4851       THREAD_AND_LOCATION,
4852       vmSymbols::java_lang_ClassFormatError(),
4853       "Illegal field modifiers in class %s: 0x%X",
4854       _class_name->as_C_string(), flags);
4855     return;
4856   }
4857 }
4858 
4859 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4860                                                     bool is_interface,
4861                                                     bool is_inline_type,
4862                                                     const Symbol* name,
4863                                                     TRAPS) const {
4864   if (!_need_verify) { return; }
4865 
4866   const bool is_public       = (flags & JVM_ACC_PUBLIC)       != 0;
4867   const bool is_private      = (flags & JVM_ACC_PRIVATE)      != 0;
4868   const bool is_static       = (flags & JVM_ACC_STATIC)       != 0;
4869   const bool is_final        = (flags & JVM_ACC_FINAL)        != 0;
4870   const bool is_native       = (flags & JVM_ACC_NATIVE)       != 0;
4871   const bool is_abstract     = (flags & JVM_ACC_ABSTRACT)     != 0;
4872   const bool is_bridge       = (flags & JVM_ACC_BRIDGE)       != 0;
4873   const bool is_strict       = (flags & JVM_ACC_STRICT)       != 0;
4874   const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4875   const bool is_protected    = (flags & JVM_ACC_PROTECTED)    != 0;
4876   const bool major_gte_1_5   = _major_version >= JAVA_1_5_VERSION;
4877   const bool major_gte_8     = _major_version >= JAVA_8_VERSION;
4878   const bool major_gte_17    = _major_version >= JAVA_17_VERSION;
4879   const bool is_initializer  = (name == vmSymbols::object_initializer_name());
4880 
4881   bool is_illegal = false;
4882 
4883   const char* class_note = "";
4884   if (is_interface) {
4885     if (major_gte_8) {
4886       // Class file version is JAVA_8_VERSION or later Methods of
4887       // interfaces may set any of the flags except ACC_PROTECTED,
4888       // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4889       // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4890       if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4891           (is_native || is_protected || is_final || is_synchronized) ||
4892           // If a specific method of a class or interface has its
4893           // ACC_ABSTRACT flag set, it must not have any of its
4894           // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4895           // ACC_STRICT, or ACC_SYNCHRONIZED flags set.  No need to
4896           // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4897           // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4898           (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4899         is_illegal = true;
4900       }
4901     } else if (major_gte_1_5) {
4902       // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4903       if (!is_public || is_private || is_protected || is_static || is_final ||
4904           is_synchronized || is_native || !is_abstract || is_strict) {
4905         is_illegal = true;
4906       }
4907     } else {
4908       // Class file version is pre-JAVA_1_5_VERSION
4909       if (!is_public || is_static || is_final || is_native || !is_abstract) {
4910         is_illegal = true;
4911       }
4912     }
4913   } else { // not interface
4914     if (has_illegal_visibility(flags)) {
4915       is_illegal = true;
4916     } else {
4917       if (is_initializer) {
4918         if (is_final || is_synchronized || is_native ||
4919             is_abstract || (major_gte_1_5 && is_bridge)) {
4920           is_illegal = true;
4921         }
4922         if (!is_static && !is_inline_type) {
4923           // OK, an object constructor in a regular class
4924         } else if (is_static && is_inline_type) {
4925           // OK, a static init factory in an inline class
4926         } else {
4927           // but no other combinations are allowed
4928           is_illegal = true;
4929           class_note = (is_inline_type ? " (an inline class)" : " (not an inline class)");
4930         }
4931       } else { // not initializer
4932         if (is_inline_type && is_synchronized && !is_static) {
4933           is_illegal = true;
4934           class_note = " (an inline class)";
4935         } else {
4936           if (is_abstract) {
4937             if ((is_final || is_native || is_private || is_static ||
4938                 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4939               is_illegal = true;
4940             }
4941           }
4942         }
4943       }
4944     }
4945   }
4946 
4947   if (is_illegal) {
4948     ResourceMark rm(THREAD);
4949     Exceptions::fthrow(
4950       THREAD_AND_LOCATION,
4951       vmSymbols::java_lang_ClassFormatError(),
4952       "Method %s in class %s%s has illegal modifiers: 0x%X",
4953       name->as_C_string(), _class_name->as_C_string(), class_note, flags);
4954     return;
4955   }
4956 }
4957 
4958 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4959                                         int length,
4960                                         TRAPS) const {
4961   assert(_need_verify, "only called when _need_verify is true");
4962   if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4963     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4964   }
4965 }
4966 
4967 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4968 // In class names, '/' separates unqualified names.  This is verified in this function also.
4969 // Method names also may not contain the characters '<' or '>', unless <init>
4970 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
4971 // method.  Because these names have been checked as special cases before
4972 // calling this method in verify_legal_method_name.
4973 //

5091 // be taken as a field signature. Allow "void" if void_ok.
5092 // Return a pointer to just past the signature.
5093 // Return NULL if no legal signature is found.
5094 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5095                                                        bool void_ok,
5096                                                        unsigned int length,
5097                                                        TRAPS) const {
5098   unsigned int array_dim = 0;
5099   while (length > 0) {
5100     switch (signature[0]) {
5101     case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
5102     case JVM_SIGNATURE_BOOLEAN:
5103     case JVM_SIGNATURE_BYTE:
5104     case JVM_SIGNATURE_CHAR:
5105     case JVM_SIGNATURE_SHORT:
5106     case JVM_SIGNATURE_INT:
5107     case JVM_SIGNATURE_FLOAT:
5108     case JVM_SIGNATURE_LONG:
5109     case JVM_SIGNATURE_DOUBLE:
5110       return signature + 1;
5111     case JVM_SIGNATURE_INLINE_TYPE:
5112       // Can't enable this check until JDK upgrades the bytecode generators
5113       // if (_major_version < CONSTANT_CLASS_DESCRIPTORS ) {
5114       //   classfile_parse_error("Class name contains illegal Q-signature "
5115       //                                    "in descriptor in class file %s",
5116       //                                    CHECK_0);
5117       // }
5118       // fall through
5119     case JVM_SIGNATURE_CLASS:
5120     {
5121       if (_major_version < JAVA_1_5_VERSION) {
5122         // Skip over the class name if one is there
5123         const char* const p = skip_over_field_name(signature + 1, true, --length);
5124 
5125         // The next character better be a semicolon
5126         if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
5127           return p + 1;
5128         }
5129       }
5130       else {
5131         // Skip leading 'L' or 'Q' and ignore first appearance of ';'
5132         signature++;
5133         const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
5134         // Format check signature
5135         if (c != NULL) {
5136           int newlen = c - (char*) signature;
5137           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5138           if (!legal) {
5139             classfile_parse_error("Class name is empty or contains illegal character "
5140                                   "in descriptor in class file %s",
5141                                   THREAD);
5142             return NULL;
5143           }
5144           return signature + newlen + 1;
5145         }
5146       }
5147       return NULL;
5148     }
5149     case JVM_SIGNATURE_ARRAY:
5150       array_dim++;
5151       if (array_dim > 255) {

5167 
5168 // Checks if name is a legal class name.
5169 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5170   if (!_need_verify || _relax_verify) { return; }
5171 
5172   assert(name->refcount() > 0, "symbol must be kept alive");
5173   char* bytes = (char*)name->bytes();
5174   unsigned int length = name->utf8_length();
5175   bool legal = false;
5176 
5177   if (length > 0) {
5178     const char* p;
5179     if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5180       p = skip_over_field_signature(bytes, false, length, CHECK);
5181       legal = (p != NULL) && ((p - bytes) == (int)length);
5182     } else if (_major_version < JAVA_1_5_VERSION) {
5183       if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
5184         p = skip_over_field_name(bytes, true, length);
5185         legal = (p != NULL) && ((p - bytes) == (int)length);
5186       }
5187     } else if ((_major_version >= CONSTANT_CLASS_DESCRIPTORS || _class_name->starts_with("jdk/internal/reflect/"))
5188                    && bytes[length - 1] == ';' ) {
5189       // Support for L...; and Q...; descriptors
5190       legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
5191     } else {
5192       // 4900761: relax the constraints based on JSR202 spec
5193       // Class names may be drawn from the entire Unicode character set.
5194       // Identifiers between '/' must be unqualified names.
5195       // The utf8 string has been verified when parsing cpool entries.
5196       legal = verify_unqualified_name(bytes, length, LegalClass);
5197     }
5198   }
5199   if (!legal) {
5200     ResourceMark rm(THREAD);
5201     assert(_class_name != NULL, "invariant");
5202     Exceptions::fthrow(
5203       THREAD_AND_LOCATION,
5204       vmSymbols::java_lang_ClassFormatError(),
5205       "Illegal class name \"%.*s\" in class file %s", length, bytes,
5206       _class_name->as_C_string()
5207     );
5208     return;
5209   }
5210 }

5268 
5269   if (!legal) {
5270     ResourceMark rm(THREAD);
5271     assert(_class_name != NULL, "invariant");
5272     Exceptions::fthrow(
5273       THREAD_AND_LOCATION,
5274       vmSymbols::java_lang_ClassFormatError(),
5275       "Illegal method name \"%.*s\" in class %s", length, bytes,
5276       _class_name->as_C_string()
5277     );
5278     return;
5279   }
5280 }
5281 
5282 
5283 // Checks if signature is a legal field signature.
5284 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5285                                                    const Symbol* signature,
5286                                                    TRAPS) const {
5287   if (!_need_verify) { return; }
5288   if (!supports_inline_types() && (signature->is_Q_signature() || signature->is_Q_array_signature())) {
5289     throwIllegalSignature("Field", name, signature, CHECK);
5290   }
5291 
5292   const char* const bytes = (const char* const)signature->bytes();
5293   const unsigned int length = signature->utf8_length();
5294   const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5295 
5296   if (p == NULL || (p - bytes) != (int)length) {
5297     throwIllegalSignature("Field", name, signature, CHECK);
5298   }
5299 }
5300 
5301 // Check that the signature is compatible with the method name.  For example,
5302 // check that <init> has a void signature.
5303 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5304                                                        const Symbol* signature,
5305                                                        TRAPS) const {
5306   if (!_need_verify) {
5307     return;
5308   }
5309 
5310   // Class initializers cannot have args for class format version >= 51.
5311   if (name == vmSymbols::class_initializer_name() &&
5312       signature != vmSymbols::void_method_signature() &&
5313       _major_version >= JAVA_7_VERSION) {
5314     throwIllegalSignature("Method", name, signature, THREAD);
5315     return;
5316   }
5317 
5318   if (!is_inline_type()) {
5319     int sig_length = signature->utf8_length();
5320     if (name->utf8_length() > 0 &&
5321       name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5322       sig_length > 0 &&
5323       signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5324       throwIllegalSignature("Method", name, signature, THREAD);
5325     }
5326   }
5327 }
5328 
5329 // Checks if signature is a legal method signature.
5330 // Returns number of parameters
5331 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5332                                                    const Symbol* signature,
5333                                                    TRAPS) const {
5334   if (!_need_verify) {
5335     // make sure caller's args_size will be less than 0 even for non-static
5336     // method so it will be recomputed in compute_size_of_parameters().
5337     return -2;
5338   }
5339 
5340   unsigned int args_size = 0;
5341   const char* p = (const char*)signature->bytes();
5342   unsigned int length = signature->utf8_length();
5343   const char* nextp;
5344 
5345   // The first character must be a '('

5482 }
5483 
5484 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5485                                                       const ClassInstanceInfo& cl_inst_info,
5486                                                       TRAPS) {
5487   if (_klass != NULL) {
5488     return _klass;
5489   }
5490 
5491   InstanceKlass* const ik =
5492     InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5493 
5494   if (is_hidden()) {
5495     mangle_hidden_class_name(ik);
5496   }
5497 
5498   fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5499 
5500   assert(_klass == ik, "invariant");
5501 
5502   if (ik->is_inline_klass()) {
5503     InlineKlass* vk = InlineKlass::cast(ik);
5504     oop val = ik->allocate_instance(CHECK_NULL);
5505     vk->set_default_value(val);
5506   }
5507 
5508   return ik;
5509 }
5510 
5511 // Return true if the specified class is not a valid super class for an inline type.
5512 // A valid super class for an inline type is abstract, has no instance fields,
5513 // does not implement interface java.lang.IdentityObject (checked elsewhere), has
5514 // an empty body-less no-arg constructor, and no synchronized instance methods.
5515 // This function doesn't check if the class's super types are invalid.  Those checks
5516 // are done elsewhere.  The final determination of whether or not a class is an
5517 // invalid super type for an inline class is done in fill_instance_klass().
5518 bool ClassFileParser::is_invalid_super_for_inline_type() {
5519   if (class_name() == vmSymbols::java_lang_IdentityObject()) {
5520     return true;
5521   }
5522   if (is_interface() || class_name() == vmSymbols::java_lang_Object()) {
5523     return false;
5524   }
5525   if (!access_flags().is_abstract() || _has_nonstatic_fields) {
5526     return true;
5527   } else {
5528     // Look at each method
5529     for (int x = 0; x < _methods->length(); x++) {
5530       const Method* const method = _methods->at(x);
5531       if (method->is_synchronized() && !method->is_static()) {
5532         return true;
5533 
5534       } else if (method->name() == vmSymbols::object_initializer_name()) {
5535         if (method->signature() != vmSymbols::void_method_signature() ||
5536             !method->is_vanilla_constructor()) {
5537           return true;
5538         }
5539       }
5540     }
5541   }
5542   return false;
5543 }
5544 
5545 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5546                                           bool changed_by_loadhook,
5547                                           const ClassInstanceInfo& cl_inst_info,
5548                                           TRAPS) {
5549   assert(ik != NULL, "invariant");
5550 
5551   // Set name and CLD before adding to CLD
5552   ik->set_class_loader_data(_loader_data);
5553   ik->set_name(_class_name);
5554 
5555   // Add all classes to our internal class loader list here,
5556   // including classes in the bootstrap (NULL) class loader.
5557   const bool publicize = !is_internal();
5558 
5559   _loader_data->add_class(ik, publicize);
5560 
5561   set_klass_to_deallocate(ik);
5562 
5563   assert(_field_info != NULL, "invariant");
5564   assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5565   assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5566          "sanity");
5567 
5568   assert(ik->is_instance_klass(), "sanity");
5569   assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5570 
5571   // Fill in information already parsed
5572   ik->set_should_verify_class(_need_verify);
5573 
5574   // Not yet: supers are done below to support the new subtype-checking fields
5575   ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5576   ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5577   if (_field_info->_is_naturally_atomic && ik->is_inline_klass()) {
5578     ik->set_is_naturally_atomic();
5579   }
5580 
5581   if (this->_invalid_inline_super) {
5582     ik->set_invalid_inline_super();
5583   }
5584 
5585   if (_has_injected_identityObject) {
5586     ik->set_has_injected_identityObject();
5587   }
5588   if (_has_injected_primitiveObject) {
5589     ik->set_has_injected_primitiveObject();
5590   }
5591 
5592   assert(_fac != NULL, "invariant");
5593   ik->set_static_oop_field_count(_fac->count[STATIC_OOP] + _fac->count[STATIC_INLINE]);
5594 
5595   // this transfers ownership of a lot of arrays from
5596   // the parser onto the InstanceKlass*
5597   apply_parsed_class_metadata(ik, _java_fields_count);
5598 
5599   // can only set dynamic nest-host after static nest information is set
5600   if (cl_inst_info.dynamic_nest_host() != NULL) {
5601     ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5602   }
5603 
5604   // note that is not safe to use the fields in the parser from this point on
5605   assert(NULL == _cp, "invariant");
5606   assert(NULL == _fields, "invariant");
5607   assert(NULL == _methods, "invariant");
5608   assert(NULL == _inner_classes, "invariant");
5609   assert(NULL == _nest_members, "invariant");
5610   assert(NULL == _combined_annotations, "invariant");
5611   assert(NULL == _record_components, "invariant");
5612   assert(NULL == _permitted_subclasses, "invariant");
5613 

5623   // has to be changed accordingly.
5624   ik->set_initial_method_idnum(ik->methods()->length());
5625 
5626   ik->set_this_class_index(_this_class_index);
5627 
5628   if (_is_hidden) {
5629     // _this_class_index is a CONSTANT_Class entry that refers to this
5630     // hidden class itself. If this class needs to refer to its own methods
5631     // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5632     // _this_class_index. However, because this class is hidden (it's
5633     // not stored in SystemDictionary), _this_class_index cannot be resolved
5634     // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5635     // Therefore, we must eagerly resolve _this_class_index now.
5636     ik->constants()->klass_at_put(_this_class_index, ik);
5637   }
5638 
5639   ik->set_minor_version(_minor_version);
5640   ik->set_major_version(_major_version);
5641   ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5642   ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5643   if (_is_declared_atomic) {
5644     ik->set_is_declared_atomic();
5645   }
5646 
5647   if (_is_hidden) {
5648     ik->set_is_hidden();
5649   }
5650 
5651   // Set PackageEntry for this_klass
5652   oop cl = ik->class_loader();
5653   Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5654   ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5655   ik->set_package(cld, NULL, CHECK);
5656 
5657   const Array<Method*>* const methods = ik->methods();
5658   assert(methods != NULL, "invariant");
5659   const int methods_len = methods->length();
5660 
5661   check_methods_for_intrinsics(ik, methods);
5662 
5663   // Fill in field values obtained by parse_classfile_attributes
5664   if (_parsed_annotations->has_any_annotations()) {
5665     _parsed_annotations->apply_to(ik);

5733 
5734   assert(_all_mirandas != NULL, "invariant");
5735 
5736   // Generate any default methods - default methods are public interface methods
5737   // that have a default implementation.  This is new with Java 8.
5738   if (_has_nonstatic_concrete_methods) {
5739     DefaultMethods::generate_default_methods(ik,
5740                                              _all_mirandas,
5741                                              CHECK);
5742   }
5743 
5744   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5745   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5746       !module_entry->has_default_read_edges()) {
5747     if (!module_entry->set_has_default_read_edges()) {
5748       // We won a potential race
5749       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5750     }
5751   }
5752 
5753   bool all_fields_empty = true;
5754   int nfields = ik->java_fields_count();
5755   if (ik->is_inline_klass()) nfields++;
5756   for (int i = 0; i < nfields; i++) {
5757     if (((ik->field_access_flags(i) & JVM_ACC_STATIC) == 0)) {
5758       if (ik->field_is_null_free_inline_type(i)) {
5759         Symbol* klass_name = ik->field_signature(i)->fundamental_name(CHECK);
5760         // Inline classes for instance fields must have been pre-loaded
5761         // Inline classes for static fields might not have been loaded yet
5762         InstanceKlass* klass = SystemDictionary::find_instance_klass(klass_name,
5763             Handle(THREAD, ik->class_loader()),
5764             Handle(THREAD, ik->protection_domain()));
5765         assert(klass != NULL, "Just checking");
5766         assert(klass->access_flags().is_inline_type(), "Inline type expected");
5767         ik->set_inline_type_field_klass(i, klass);
5768         klass_name->decrement_refcount();
5769         if (!InlineKlass::cast(klass)->is_empty_inline_type()) { all_fields_empty = false; }
5770       } else {
5771         all_fields_empty = false;
5772       }
5773     } else if (is_inline_type() && ((ik->field_access_flags(i) & JVM_ACC_FIELD_INTERNAL) != 0)) {
5774       InlineKlass::cast(ik)->set_default_value_offset(ik->field_offset(i));
5775     }
5776   }
5777 
5778   if (_is_empty_inline_type || (is_inline_type() && all_fields_empty)) {
5779     ik->set_is_empty_inline_type();
5780   }
5781 
5782   if (is_inline_type()) {
5783     InlineKlass* vk = InlineKlass::cast(ik);
5784     vk->set_alignment(_alignment);
5785     vk->set_first_field_offset(_first_field_offset);
5786     vk->set_exact_size_in_bytes(_exact_size_in_bytes);
5787     InlineKlass::cast(ik)->initialize_calling_convention(CHECK);
5788   }
5789 
5790   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5791 
5792   if (!is_internal()) {
5793     ik->print_class_load_logging(_loader_data, module_entry, _stream);
5794 
5795     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5796         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5797         log_is_enabled(Info, class, preview)) {
5798       ResourceMark rm;
5799       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5800                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5801     }
5802 
5803     if (log_is_enabled(Debug, class, resolve))  {
5804       ResourceMark rm;
5805       // print out the superclass.
5806       const char * from = ik->external_name();
5807       if (ik->java_super() != NULL) {
5808         log_debug(class, resolve)("%s %s (super)",
5809                    from,

5815         const int length = local_interfaces->length();
5816         for (int i = 0; i < length; i++) {
5817           const InstanceKlass* const k = local_interfaces->at(i);
5818           const char * to = k->external_name();
5819           log_debug(class, resolve)("%s %s (interface)", from, to);
5820         }
5821       }
5822     }
5823   }
5824 
5825   JFR_ONLY(INIT_ID(ik);)
5826 
5827   // If we reach here, all is well.
5828   // Now remove the InstanceKlass* from the _klass_to_deallocate field
5829   // in order for it to not be destroyed in the ClassFileParser destructor.
5830   set_klass_to_deallocate(NULL);
5831 
5832   // it's official
5833   set_klass(ik);
5834 
5835   // the common single interface arrays need setup here to provide the
5836   // correct answer to "compute_transitive_interfaces()", during
5837   // "SystemDictionary::initialize()"
5838   if (ik->name() == vmSymbols::java_lang_IdentityObject()) {
5839     Universe::initialize_the_single_IdentityObject_klass_array(ik, CHECK);
5840   }
5841   if (ik->name() == vmSymbols::java_lang_PrimitiveObject()) {
5842     Universe::initialize_the_single_PrimitiveObject_klass_array(ik, CHECK);
5843   }
5844 
5845   debug_only(ik->verify();)
5846 }
5847 
5848 void ClassFileParser::update_class_name(Symbol* new_class_name) {
5849   // Decrement the refcount in the old name, since we're clobbering it.
5850   _class_name->decrement_refcount();
5851 
5852   _class_name = new_class_name;
5853   // Increment the refcount of the new name.
5854   // Now the ClassFileParser owns this name and will decrement in
5855   // the destructor.
5856   _class_name->increment_refcount();
5857 }
5858 
5859 static bool relax_format_check_for(ClassLoaderData* loader_data) {
5860   bool trusted = loader_data->is_boot_class_loader_data() ||
5861                  loader_data->is_platform_class_loader_data();
5862   bool need_verify =
5863     // verifyAll
5864     (BytecodeVerificationLocal && BytecodeVerificationRemote) ||

5871                                  Symbol* name,
5872                                  ClassLoaderData* loader_data,
5873                                  const ClassLoadInfo* cl_info,
5874                                  Publicity pub_level,
5875                                  TRAPS) :
5876   _stream(stream),
5877   _class_name(NULL),
5878   _loader_data(loader_data),
5879   _is_hidden(cl_info->is_hidden()),
5880   _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5881   _orig_cp_size(0),
5882   _super_klass(),
5883   _cp(NULL),
5884   _fields(NULL),
5885   _methods(NULL),
5886   _inner_classes(NULL),
5887   _nest_members(NULL),
5888   _nest_host(0),
5889   _permitted_subclasses(NULL),
5890   _record_components(NULL),
5891   _temp_local_interfaces(NULL),
5892   _local_interfaces(NULL),
5893   _transitive_interfaces(NULL),
5894   _combined_annotations(NULL),
5895   _class_annotations(NULL),
5896   _class_type_annotations(NULL),
5897   _fields_annotations(NULL),
5898   _fields_type_annotations(NULL),
5899   _klass(NULL),
5900   _klass_to_deallocate(NULL),
5901   _parsed_annotations(NULL),
5902   _fac(NULL),
5903   _field_info(NULL),
5904   _method_ordering(NULL),
5905   _all_mirandas(NULL),
5906   _vtable_size(0),
5907   _itable_size(0),
5908   _num_miranda_methods(0),
5909   _rt(REF_NONE),
5910   _protection_domain(cl_info->protection_domain()),
5911   _access_flags(),
5912   _pub_level(pub_level),
5913   _bad_constant_seen(0),
5914   _synthetic_flag(false),
5915   _sde_length(false),
5916   _sde_buffer(NULL),
5917   _sourcefile_index(0),
5918   _generic_signature_index(0),
5919   _major_version(0),
5920   _minor_version(0),
5921   _this_class_index(0),
5922   _super_class_index(0),
5923   _itfs_len(0),
5924   _java_fields_count(0),
5925   _need_verify(false),
5926   _relax_verify(false),
5927   _has_nonstatic_concrete_methods(false),
5928   _declares_nonstatic_concrete_methods(false),
5929   _has_final_method(false),
5930   _has_contended_fields(false),
5931   _has_inline_type_fields(false),
5932   _has_nonstatic_fields(false),
5933   _is_empty_inline_type(false),
5934   _is_naturally_atomic(false),
5935   _is_declared_atomic(false),
5936   _invalid_inline_super(false),
5937   _invalid_identity_super(false),
5938   _implements_identityObject(false),
5939   _has_injected_identityObject(false),
5940   _implements_primitiveObject(false),
5941   _has_injected_primitiveObject(false),
5942   _has_finalizer(false),
5943   _has_empty_finalizer(false),
5944   _has_vanilla_constructor(false),
5945   _max_bootstrap_specifier_index(-1) {
5946 
5947   _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
5948   _class_name->increment_refcount();
5949 
5950   assert(_loader_data != NULL, "invariant");
5951   assert(stream != NULL, "invariant");
5952   assert(_stream != NULL, "invariant");
5953   assert(_stream->buffer() == _stream->current(), "invariant");
5954   assert(_class_name != NULL, "invariant");
5955   assert(0 == _access_flags.as_int(), "invariant");
5956 
5957   // Figure out whether we can skip format checking (matching classic VM behavior)
5958   if (DumpSharedSpaces) {
5959     // verify == true means it's a 'remote' class (i.e., non-boot class)
5960     // Verification decision is based on BytecodeVerificationRemote flag
5961     // for those classes.

6094     cp_size, CHECK);
6095 
6096   _orig_cp_size = cp_size;
6097   if (is_hidden()) { // Add a slot for hidden class name.
6098     cp_size++;
6099   }
6100 
6101   _cp = ConstantPool::allocate(_loader_data,
6102                                cp_size,
6103                                CHECK);
6104 
6105   ConstantPool* const cp = _cp;
6106 
6107   parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6108 
6109   assert(cp_size == (const u2)cp->length(), "invariant");
6110 
6111   // ACCESS FLAGS
6112   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
6113 
6114   jint recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS;

6115   // JVM_ACC_MODULE is defined in JDK-9 and later.
6116   if (_major_version >= JAVA_9_VERSION) {
6117     recognized_modifiers |= JVM_ACC_MODULE;


6118   }
6119   // JVM_ACC_INLINE is defined for class file version 55 and later
6120   if (supports_inline_types()) {
6121     recognized_modifiers |= JVM_ACC_INLINE;
6122   }
6123 
6124   // Access flags
6125   jint flags = stream->get_u2_fast() & recognized_modifiers;
6126 
6127   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6128     // Set abstract bit for old class files for backward compatibility
6129     flags |= JVM_ACC_ABSTRACT;
6130   }
6131 
6132   verify_legal_class_modifiers(flags, CHECK);
6133 
6134   short bad_constant = class_bad_constant_seen();
6135   if (bad_constant != 0) {
6136     // Do not throw CFE until after the access_flags are checked because if
6137     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6138     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
6139     return;
6140   }
6141 
6142   _access_flags.set_flags(flags);
6143 
6144   // This class and superclass
6145   _this_class_index = stream->get_u2_fast();

6212       if (stream->source() != NULL) {
6213         ls.print(" source: %s", stream->source());
6214       }
6215       ls.cr();
6216     }
6217   }
6218 
6219   // SUPERKLASS
6220   _super_class_index = stream->get_u2_fast();
6221   _super_klass = parse_super_class(cp,
6222                                    _super_class_index,
6223                                    _need_verify,
6224                                    CHECK);
6225 
6226   // Interfaces
6227   _itfs_len = stream->get_u2_fast();
6228   parse_interfaces(stream,
6229                    _itfs_len,
6230                    cp,
6231                    &_has_nonstatic_concrete_methods,
6232                    &_is_declared_atomic,
6233                    CHECK);
6234 
6235   assert(_temp_local_interfaces != NULL, "invariant");
6236 
6237   // Fields (offsets are filled in later)
6238   _fac = new FieldAllocationCount();
6239   parse_fields(stream,
6240                is_interface(),
6241                is_inline_type(),
6242                _fac,
6243                cp,
6244                cp_size,
6245                &_java_fields_count,
6246                CHECK);
6247 
6248   assert(_fields != NULL, "invariant");
6249 
6250   // Methods
6251   AccessFlags promoted_flags;
6252   parse_methods(stream,
6253                 is_interface(),
6254                 is_inline_type(),
6255                 &promoted_flags,
6256                 &_has_final_method,
6257                 &_declares_nonstatic_concrete_methods,
6258                 CHECK);
6259 
6260   assert(_methods != NULL, "invariant");
6261 
6262   // promote flags from parse_methods() to the klass' flags
6263   _access_flags.add_promoted_flags(promoted_flags.as_int());
6264 
6265   if (_declares_nonstatic_concrete_methods) {
6266     _has_nonstatic_concrete_methods = true;
6267   }
6268 
6269   // Additional attributes/annotations
6270   _parsed_annotations = new ClassAnnotationCollector();
6271   parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6272 
6273   assert(_inner_classes != NULL, "invariant");
6274 

6316 
6317   // Update this_class_index's slot in the constant pool with the new Utf8 entry.
6318   // We have to update the resolved_klass_index and the name_index together
6319   // so extract the existing resolved_klass_index first.
6320   CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
6321   int resolved_klass_index = cp_klass_slot.resolved_klass_index();
6322   _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
6323   assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
6324          "Bad name_index");
6325 }
6326 
6327 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
6328                                                  ConstantPool* cp,
6329                                                  TRAPS) {
6330   assert(stream != NULL, "invariant");
6331   assert(stream->at_eos(), "invariant");
6332   assert(cp != NULL, "invariant");
6333   assert(_loader_data != NULL, "invariant");
6334 
6335   if (_class_name == vmSymbols::java_lang_Object()) {
6336     check_property(_temp_local_interfaces->length() == 0,
6337         "java.lang.Object cannot implement an interface in class file %s",
6338         CHECK);
6339   }
6340   // We check super class after class file is parsed and format is checked
6341   if (_super_class_index > 0 && NULL == _super_klass) {
6342     Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
6343     if (is_interface()) {
6344       // Before attempting to resolve the superclass, check for class format
6345       // errors not checked yet.
6346       guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
6347         "Interfaces must have java.lang.Object as superclass in class file %s",
6348         CHECK);
6349     }
6350     Handle loader(THREAD, _loader_data->class_loader());
6351     _super_klass = (const InstanceKlass*)
6352                        SystemDictionary::resolve_super_or_fail(_class_name,
6353                                                                super_class_name,
6354                                                                loader,
6355                                                                _protection_domain,
6356                                                                true,
6357                                                                CHECK);
6358   }
6359 
6360   if (_super_klass != NULL) {
6361     if (_super_klass->has_nonstatic_concrete_methods()) {
6362       _has_nonstatic_concrete_methods = true;
6363     }
6364     if (_super_klass->is_declared_atomic()) {
6365       _is_declared_atomic = true;
6366     }
6367 
6368     if (_super_klass->is_interface()) {
6369       classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
6370       return;
6371     }
6372 
6373     // For an inline class, only java/lang/Object or special abstract classes
6374     // are acceptable super classes.
6375     if (is_inline_type()) {
6376       const InstanceKlass* super_ik = _super_klass;
6377       if (super_ik->invalid_inline_super()) {
6378         classfile_icce_error("inline class %s has an invalid super class %s", _super_klass, THREAD);
6379         return;
6380       }
6381     }
6382   }
6383 
6384   if (_class_name == vmSymbols::java_lang_NonTearable() && _loader_data->class_loader() == NULL) {
6385     // This is the original source of this condition.
6386     // It propagates by inheritance, as if testing "instanceof NonTearable".
6387     _is_declared_atomic = true;
6388   } else if (*ForceNonTearable != '\0') {
6389     // Allow a command line switch to force the same atomicity property:
6390     const char* class_name_str = _class_name->as_C_string();
6391     if (StringUtils::class_list_match(ForceNonTearable, class_name_str)) {
6392       _is_declared_atomic = true;
6393     }
6394   }
6395 
6396   // Set ik->invalid_inline_super field to TRUE if already marked as invalid,
6397   // if super is marked invalid, or if is_invalid_super_for_inline_type()
6398   // returns true
6399   if (invalid_inline_super() ||
6400       (_super_klass != NULL && _super_klass->invalid_inline_super()) ||
6401       is_invalid_super_for_inline_type()) {
6402     set_invalid_inline_super();
6403   }
6404 
6405   if (EnableValhalla && !is_inline_type() && invalid_inline_super() && (_super_klass == NULL || !_super_klass->invalid_inline_super())
6406       && !_implements_identityObject && class_name() != vmSymbols::java_lang_IdentityObject()) {
6407     _temp_local_interfaces->append(vmClasses::IdentityObject_klass());
6408     _has_injected_identityObject = true;
6409   }
6410   // Check if declared as PrimitiveObject...else add if needed
6411   if (_implements_primitiveObject) {
6412     if (!is_inline_type() && invalid_inline_super()) {
6413       classfile_icce_error("class %s can not implement %s, neither valid inline classes or valid supertype",
6414                             vmClasses::PrimitiveObject_klass(), THREAD);
6415       return;
6416     }
6417   } else if (is_inline_type()) {
6418     _temp_local_interfaces->append(vmClasses::PrimitiveObject_klass());
6419     _has_injected_primitiveObject = true;
6420   }
6421 
6422   int itfs_len = _temp_local_interfaces->length();
6423   if (itfs_len == 0) {
6424     _local_interfaces = Universe::the_empty_instance_klass_array();
6425   } else if (itfs_len == 1 && _temp_local_interfaces->at(0) == vmClasses::IdentityObject_klass()) {
6426     _local_interfaces = Universe::the_single_IdentityObject_klass_array();
6427   } else if (itfs_len == 1 && _temp_local_interfaces->at(0) == vmClasses::PrimitiveObject_klass()) {
6428     _local_interfaces = Universe::the_single_PrimitiveObject_klass_array();
6429   } else {
6430     _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, NULL, CHECK);
6431     for (int i = 0; i < itfs_len; i++) {
6432       _local_interfaces->at_put(i, _temp_local_interfaces->at(i));
6433     }
6434   }
6435   _temp_local_interfaces = NULL;
6436   assert(_local_interfaces != NULL, "invariant");
6437 
6438   // Compute the transitive list of all unique interfaces implemented by this class
6439   _transitive_interfaces =
6440     compute_transitive_interfaces(_super_klass,
6441                                   _local_interfaces,
6442                                   _loader_data,
6443                                   CHECK);
6444 
6445   assert(_transitive_interfaces != NULL, "invariant");
6446 
6447   // sort methods
6448   _method_ordering = sort_methods(_methods);
6449 
6450   _all_mirandas = new GrowableArray<Method*>(20);
6451 
6452   Handle loader(THREAD, _loader_data->class_loader());
6453   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6454                                                     &_num_miranda_methods,
6455                                                     _all_mirandas,
6456                                                     _super_klass,
6457                                                     _methods,
6458                                                     _access_flags,
6459                                                     _major_version,
6460                                                     loader,
6461                                                     _class_name,
6462                                                     _local_interfaces);
6463 
6464   // Size of Java itable (in words)
6465   _itable_size = is_interface() ? 0 :
6466     klassItable::compute_itable_size(_transitive_interfaces);
6467 
6468   assert(_fac != NULL, "invariant");
6469   assert(_parsed_annotations != NULL, "invariant");
6470 
6471 
6472   if (EnableValhalla) {
6473     for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
6474       if (Signature::basic_type(fs.signature()) == T_INLINE_TYPE && !fs.access_flags().is_static()) {
6475         // Pre-load inline class
6476         Klass* klass = SystemDictionary::resolve_inline_type_field_or_fail(&fs,
6477             Handle(THREAD, _loader_data->class_loader()),
6478             _protection_domain, true, CHECK);
6479         assert(klass != NULL, "Sanity check");
6480         if (!klass->access_flags().is_inline_type()) {
6481           assert(klass->is_instance_klass(), "Sanity check");
6482           ResourceMark rm(THREAD);
6483             THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6484                       err_msg("Class %s expects class %s to be an inline type, but it is not",
6485                       _class_name->as_C_string(),
6486                       InstanceKlass::cast(klass)->external_name()));
6487         }
6488       }
6489     }
6490   }
6491 
6492   _field_info = new FieldLayoutInfo();
6493   FieldLayoutBuilder lb(class_name(), super_klass(), _cp, _fields,
6494       _parsed_annotations->is_contended(), is_inline_type(),
6495       loader_data(), _protection_domain, _field_info);
6496   lb.build_layout(CHECK);
6497   if (is_inline_type()) {
6498     _alignment = lb.get_alignment();
6499     _first_field_offset = lb.get_first_field_offset();
6500     _exact_size_in_bytes = lb.get_exact_size_in_byte();
6501   }
6502   _has_inline_type_fields = _field_info->_has_inline_fields;
6503 
6504   // Compute reference type
6505   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();

6506 }
6507 
6508 void ClassFileParser::set_klass(InstanceKlass* klass) {
6509 
6510 #ifdef ASSERT
6511   if (klass != NULL) {
6512     assert(NULL == _klass, "leaking?");
6513   }
6514 #endif
6515 
6516   _klass = klass;
6517 }
6518 
6519 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6520 
6521 #ifdef ASSERT
6522   if (klass != NULL) {
6523     assert(NULL == _klass_to_deallocate, "leaking?");
6524   }
6525 #endif
6526 
6527   _klass_to_deallocate = klass;
6528 }
6529 
6530 // Caller responsible for ResourceMark
6531 // clone stream with rewound position
6532 const ClassFileStream* ClassFileParser::clone_stream() const {
6533   assert(_stream != NULL, "invariant");
6534 
6535   return _stream->clone();
6536 }
6537 
6538 // ----------------------------------------------------------------------------
6539 // debugging
6540 
6541 #ifdef ASSERT
6542 
6543 // return true if class_name contains no '.' (internal format is '/')
6544 bool ClassFileParser::is_internal_format(Symbol* class_name) {
6545   if (class_name != NULL) {
6546     ResourceMark rm;
6547     char* name = class_name->as_C_string();
6548     return strchr(name, JVM_SIGNATURE_DOT) == NULL;
6549   } else {
6550     return true;
6551   }
6552 }
6553 
6554 #endif
< prev index next >