< 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 "cds/cdsConfig.hpp"
  25 #include "classfile/classFileParser.hpp"
  26 #include "classfile/classFileStream.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/classLoaderData.inline.hpp"
  29 #include "classfile/classLoadInfo.hpp"
  30 #include "classfile/defaultMethods.hpp"
  31 #include "classfile/fieldLayoutBuilder.hpp"
  32 #include "classfile/javaClasses.inline.hpp"
  33 #include "classfile/moduleEntry.hpp"
  34 #include "classfile/packageEntry.hpp"
  35 #include "classfile/symbolTable.hpp"
  36 #include "classfile/systemDictionary.hpp"
  37 #include "classfile/verificationType.hpp"
  38 #include "classfile/verifier.hpp"
  39 #include "classfile/vmClasses.hpp"
  40 #include "classfile/vmSymbols.hpp"
  41 #include "jvm.h"
  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/fieldInfo.hpp"
  52 #include "oops/fieldStreams.inline.hpp"

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

 139 #define JAVA_17_VERSION                   61
 140 
 141 #define JAVA_18_VERSION                   62
 142 
 143 #define JAVA_19_VERSION                   63
 144 
 145 #define JAVA_20_VERSION                   64
 146 
 147 #define JAVA_21_VERSION                   65
 148 
 149 #define JAVA_22_VERSION                   66
 150 
 151 #define JAVA_23_VERSION                   67
 152 
 153 #define JAVA_24_VERSION                   68
 154 
 155 #define JAVA_25_VERSION                   69
 156 
 157 #define JAVA_26_VERSION                   70
 158 


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

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



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

 690             }
 691           }
 692         } else {
 693           if (_need_verify) {
 694             // Method name and signature are individually verified above, when iterating
 695             // NameAndType_info.  Need to check here that signature is non-zero length and
 696             // the right type.
 697             if (!Signature::is_method(signature)) {
 698               throwIllegalSignature("Method", name, signature, CHECK);
 699             }
 700           }
 701           // If a class method name begins with '<', it must be "<init>" and have void signature.
 702           const unsigned int name_len = name->utf8_length();
 703           if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
 704               name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
 705             if (name != vmSymbols::object_initializer_name()) {
 706               classfile_parse_error(
 707                 "Bad method name at constant pool index %u in class file %s",
 708                 name_ref_index, THREAD);
 709               return;
 710             } else if (!Signature::is_void_method(signature)) { // must have void signature.
 711               throwIllegalSignature("Method", name, signature, CHECK);
 712             }
 713           }
 714         }
 715         break;
 716       }
 717       case JVM_CONSTANT_MethodHandle: {
 718         const int ref_index = cp->method_handle_index_at(index);
 719         const int ref_kind = cp->method_handle_ref_kind_at(index);
 720         switch (ref_kind) {
 721           case JVM_REF_invokeVirtual:
 722           case JVM_REF_invokeStatic:
 723           case JVM_REF_invokeSpecial:
 724           case JVM_REF_newInvokeSpecial: {
 725             const int name_and_type_ref_index =
 726               cp->uncached_name_and_type_ref_index_at(ref_index);
 727             const int name_ref_index =
 728               cp->name_ref_index_at(name_and_type_ref_index);
 729             const Symbol* const name = cp->symbol_at(name_ref_index);
 730             if (ref_kind == JVM_REF_newInvokeSpecial) {
 731               if (name != vmSymbols::object_initializer_name()) {

 732                 classfile_parse_error(
 733                   "Bad constructor name at constant pool index %u in class file %s",
 734                     name_ref_index, THREAD);
 735                 return;
 736               }
 737             } else {
 738               if (name == vmSymbols::object_initializer_name()) {








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

 770 
 771   NameSigHash(Symbol* name, Symbol* sig) :
 772     _name(name),
 773     _sig(sig) {}
 774 
 775   static unsigned int hash(NameSigHash const& namesig) {
 776     return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
 777   }
 778 
 779   static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
 780     return (e0._name == e1._name) &&
 781           (e0._sig  == e1._sig);
 782   }
 783 };
 784 
 785 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
 786                                            NameSigHash::HASH_ROW_SIZE,
 787                                            AnyObj::RESOURCE_AREA, mtInternal,
 788                                            &NameSigHash::hash, &NameSigHash::equals>;
 789 
 790 // Side-effects: populates the _local_interfaces field
 791 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
 792                                        const int itfs_len,
 793                                        ConstantPool* const cp,









 794                                        bool* const has_nonstatic_concrete_methods,






 795                                        TRAPS) {
 796   assert(stream != nullptr, "invariant");
 797   assert(cp != nullptr, "invariant");
 798   assert(has_nonstatic_concrete_methods != nullptr, "invariant");
 799 
 800   if (itfs_len == 0) {
 801     _local_interfaces = Universe::the_empty_instance_klass_array();

 802   } else {
 803     assert(itfs_len > 0, "only called for len>0");
 804     _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK);
 805 
 806     int index;
 807     for (index = 0; index < itfs_len; index++) {
 808       const u2 interface_index = stream->get_u2(CHECK);
 809       Klass* interf;
 810       guarantee_property(
 811         valid_klass_reference_at(interface_index),
 812         "Interface name has bad constant pool index %u in class file %s",
 813         interface_index, CHECK);
 814       if (cp->tag_at(interface_index).is_klass()) {
 815         interf = cp->resolved_klass_at(interface_index);
 816       } else {
 817         Symbol* const unresolved_klass  = cp->klass_name_at(interface_index);
 818 
 819         // Don't need to check legal name because it's checked when parsing constant pool.
 820         // But need to make sure it's not an array type.
 821         guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
 822                            "Bad interface name in class file %s", CHECK);
 823 
 824         // Call resolve on the interface class name with class circularity checking
 825         interf = SystemDictionary::resolve_super_or_fail(_class_name,
 826                                                          unresolved_klass,
 827                                                          Handle(THREAD, _loader_data->class_loader()),
 828                                                          false, CHECK);
 829       }
 830 
 831       if (!interf->is_interface()) {
 832         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
 833                   err_msg("class %s can not implement %s, because it is not an interface (%s)",
 834                           _class_name->as_klass_external_name(),
 835                           interf->external_name(),
 836                           interf->class_in_module_of_loader()));
 837       }
 838 
 839       if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
 840         *has_nonstatic_concrete_methods = true;
 841       }
 842       _local_interfaces->at_put(index, InstanceKlass::cast(interf));
 843     }
 844 
 845     if (!_need_verify || itfs_len <= 1) {
 846       return;
 847     }
 848 
 849     // Check if there's any duplicates in interfaces
 850     ResourceMark rm(THREAD);
 851     // Set containing interface names
 852     ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
 853     for (index = 0; index < itfs_len; index++) {
 854       const InstanceKlass* const k = _local_interfaces->at(index);
 855       Symbol* interface_name = k->name();
 856       // If no duplicates, add (name, nullptr) in hashtable interface_names.
 857       if (!interface_names->put(interface_name, 0)) {
 858         classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
 859                                interface_name->as_C_string(), THREAD);
 860         return;
 861       }
 862     }
 863   }
 864 }
 865 
 866 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
 867                                            int constantvalue_index,
 868                                            int signature_index,
 869                                            TRAPS) const {
 870   // Make sure the constant pool entry is of a type appropriate to this field
 871   guarantee_property(
 872     (constantvalue_index > 0 &&
 873       constantvalue_index < cp->length()),
 874     "Bad initial value index %u in ConstantValue attribute in class file %s",
 875     constantvalue_index, CHECK);

 922 class AnnotationCollector : public ResourceObj{
 923 public:
 924   enum Location { _in_field, _in_method, _in_class };
 925   enum ID {
 926     _unknown = 0,
 927     _method_CallerSensitive,
 928     _method_ForceInline,
 929     _method_DontInline,
 930     _method_ChangesCurrentThread,
 931     _method_JvmtiHideEvents,
 932     _method_JvmtiMountTransition,
 933     _method_InjectedProfile,
 934     _method_LambdaForm_Compiled,
 935     _method_Hidden,
 936     _method_Scoped,
 937     _method_IntrinsicCandidate,
 938     _jdk_internal_vm_annotation_Contended,
 939     _field_Stable,
 940     _jdk_internal_vm_annotation_ReservedStackAccess,
 941     _jdk_internal_ValueBased,


 942     _java_lang_Deprecated,
 943     _java_lang_Deprecated_for_removal,
 944     _annotation_LIMIT
 945   };
 946   const Location _location;
 947   int _annotations_present;
 948   u2 _contended_group;
 949 
 950   AnnotationCollector(Location location)
 951     : _location(location), _annotations_present(0), _contended_group(0)
 952   {
 953     assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
 954   }
 955   // If this annotation name has an ID, report it (or _none).
 956   ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
 957   // Set the annotation name:
 958   void set_annotation(ID id) {
 959     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
 960     _annotations_present |= (int)nth_bit((int)id);
 961   }

1344   }
1345 
1346   *constantvalue_index_addr = constantvalue_index;
1347   *is_synthetic_addr = is_synthetic;
1348   *generic_signature_index_addr = generic_signature_index;
1349   AnnotationArray* a = allocate_annotations(runtime_visible_annotations,
1350                                             runtime_visible_annotations_length,
1351                                             CHECK);
1352   parsed_annotations->set_field_annotations(a);
1353   a = allocate_annotations(runtime_visible_type_annotations,
1354                            runtime_visible_type_annotations_length,
1355                            CHECK);
1356   parsed_annotations->set_field_type_annotations(a);
1357   return;
1358 }
1359 
1360 
1361 // Side-effects: populates the _fields, _fields_annotations,
1362 // _fields_type_annotations fields
1363 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1364                                    bool is_interface,
1365                                    ConstantPool* cp,
1366                                    const int cp_size,
1367                                    u2* const java_fields_count_ptr,
1368                                    TRAPS) {
1369 
1370   assert(cfs != nullptr, "invariant");
1371   assert(cp != nullptr, "invariant");
1372   assert(java_fields_count_ptr != nullptr, "invariant");
1373 
1374   assert(nullptr == _fields_annotations, "invariant");
1375   assert(nullptr == _fields_type_annotations, "invariant");
1376 

1377   cfs->guarantee_more(2, CHECK);  // length
1378   const u2 length = cfs->get_u2_fast();
1379   *java_fields_count_ptr = length;
1380 
1381   int num_injected = 0;
1382   const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1383                                                                   &num_injected);
1384   const int total_fields = length + num_injected;




1385 
1386   // Allocate a temporary resource array to collect field data.
1387   // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1388   _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1389 

1390   ResourceMark rm(THREAD);
1391   for (int n = 0; n < length; n++) {
1392     // access_flags, name_index, descriptor_index, attributes_count
1393     cfs->guarantee_more(8, CHECK);
1394 







1395     AccessFlags access_flags;
1396     const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1397     verify_legal_field_modifiers(flags, is_interface, CHECK);
1398     access_flags.set_flags(flags);
1399     FieldInfo::FieldFlags fieldFlags(0);
1400 
1401     const u2 name_index = cfs->get_u2_fast();
1402     guarantee_property(valid_symbol_at(name_index),
1403       "Invalid constant pool index %u for field name in class file %s",
1404       name_index, CHECK);
1405     const Symbol* const name = cp->symbol_at(name_index);
1406     verify_legal_field_name(name, CHECK);
1407 
1408     const u2 signature_index = cfs->get_u2_fast();
1409     guarantee_property(valid_symbol_at(signature_index),
1410       "Invalid constant pool index %u for field signature in class file %s",
1411       signature_index, CHECK);
1412     const Symbol* const sig = cp->symbol_at(signature_index);
1413     verify_legal_field_signature(name, sig, CHECK);

1414 
1415     u2 constantvalue_index = 0;
1416     bool is_synthetic = false;
1417     u2 generic_signature_index = 0;
1418     const bool is_static = access_flags.is_static();
1419     FieldAnnotationCollector parsed_annotations(_loader_data);
1420 


1421     const u2 attributes_count = cfs->get_u2_fast();
1422     if (attributes_count > 0) {
1423       parse_field_attributes(cfs,
1424                              attributes_count,
1425                              is_static,
1426                              signature_index,
1427                              &constantvalue_index,
1428                              &is_synthetic,
1429                              &generic_signature_index,
1430                              &parsed_annotations,
1431                              CHECK);
1432 
1433       if (parsed_annotations.field_annotations() != nullptr) {
1434         if (_fields_annotations == nullptr) {
1435           _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1436                                              _loader_data, length, nullptr,
1437                                              CHECK);
1438         }
1439         _fields_annotations->at_put(n, parsed_annotations.field_annotations());


















1440         parsed_annotations.set_field_annotations(nullptr);
1441       }
1442       if (parsed_annotations.field_type_annotations() != nullptr) {
1443         if (_fields_type_annotations == nullptr) {
1444           _fields_type_annotations =
1445             MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1446                                                          length,
1447                                                          nullptr,
1448                                                          CHECK);
1449         }
1450         _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1451         parsed_annotations.set_field_type_annotations(nullptr);
1452       }
1453 
1454       if (is_synthetic) {
1455         access_flags.set_is_synthetic();
1456       }
1457       if (generic_signature_index != 0) {
1458         fieldFlags.update_generic(true);
1459       }
1460     }
1461 




1462     const BasicType type = cp->basic_type_for_signature_at(signature_index);
1463 
1464     // Update number of static oop fields.
1465     if (is_static && is_reference_type(type)) {
1466       _static_oop_count++;
1467     }
1468 
1469     FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1470     fi.set_index(n);
1471     if (fieldFlags.is_generic()) {
1472       fi.set_generic_signature_index(generic_signature_index);
1473     }
1474     parsed_annotations.apply_to(&fi);
1475     if (fi.field_flags().is_contended()) {
1476       _has_contended_fields = true;
1477     }



1478     _temp_field_info->append(fi);
1479   }
1480   assert(_temp_field_info->length() == length, "Must be");
1481 
1482   int index = length;
1483   if (num_injected != 0) {
1484     for (int n = 0; n < num_injected; n++) {
1485       // Check for duplicates
1486       if (injected[n].may_be_java) {
1487         const Symbol* const name      = injected[n].name();
1488         const Symbol* const signature = injected[n].signature();
1489         bool duplicate = false;
1490         for (int i = 0; i < length; i++) {
1491           const FieldInfo* const f = _temp_field_info->adr_at(i);
1492           if (name      == cp->symbol_at(f->name_index()) &&
1493               signature == cp->symbol_at(f->signature_index())) {
1494             // Symbol is desclared in Java so skip this one
1495             duplicate = true;
1496             break;
1497           }
1498         }
1499         if (duplicate) {
1500           // These will be removed from the field array at the end
1501           continue;
1502         }
1503       }
1504 
1505       // Injected field
1506       FieldInfo::FieldFlags fflags(0);
1507       fflags.update_injected(true);
1508       AccessFlags aflags;
1509       FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1510       fi.set_index(index);
1511       _temp_field_info->append(fi);
1512       index++;
1513     }
1514   }
1515 
1516   assert(_temp_field_info->length() == index, "Must be");
















1517 
1518   if (_need_verify && length > 1) {
1519     // Check duplicated fields
1520     ResourceMark rm(THREAD);
1521     // Set containing name-signature pairs
1522     NameSigHashtable* names_and_sigs = new NameSigHashtable();
1523     for (int i = 0; i < _temp_field_info->length(); i++) {
1524       NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1525                                _temp_field_info->adr_at(i)->signature(_cp));
1526       // If no duplicates, add name/signature in hashtable names_and_sigs.
1527       if(!names_and_sigs->put(name_and_sig, 0)) {
1528         classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1529                                name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1530         return;
1531       }
1532     }
1533   }
1534 }
1535 
1536 

1876     }
1877     case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
1878       if (_location != _in_field && _location != _in_class) {
1879         break;  // only allow for fields and classes
1880       }
1881       if (!EnableContended || (RestrictContended && !privileged)) {
1882         break;  // honor privileges
1883       }
1884       return _jdk_internal_vm_annotation_Contended;
1885     }
1886     case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
1887       if (_location != _in_method)  break;  // only allow for methods
1888       if (RestrictReservedStack && !privileged) break; // honor privileges
1889       return _jdk_internal_vm_annotation_ReservedStackAccess;
1890     }
1891     case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
1892       if (_location != _in_class)   break;  // only allow for classes
1893       if (!privileged)              break;  // only allow in privileged code
1894       return _jdk_internal_ValueBased;
1895     }








1896     case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
1897       return _java_lang_Deprecated;
1898     }
1899     default: {
1900       break;
1901     }
1902   }
1903   return AnnotationCollector::_unknown;
1904 }
1905 
1906 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
1907   if (is_contended())
1908     // Setting the contended group also sets the contended bit in field flags
1909     f->set_contended_group(contended_group());
1910   if (is_stable())
1911     (f->field_flags_addr())->update_stable(true);
1912 }
1913 
1914 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {
1915   // If there's an error deallocate metadata for field annotations

2099   }
2100 
2101   if (runtime_visible_type_annotations_length > 0) {
2102     a = allocate_annotations(runtime_visible_type_annotations,
2103                              runtime_visible_type_annotations_length,
2104                              CHECK);
2105     cm->set_type_annotations(a);
2106   }
2107 }
2108 
2109 
2110 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2111 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2112 // Method* to save footprint, so we only know the size of the resulting Method* when the
2113 // entire method attribute is parsed.
2114 //
2115 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2116 
2117 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2118                                       bool is_interface,


2119                                       const ConstantPool* cp,
2120                                       bool* const has_localvariable_table,
2121                                       TRAPS) {
2122   assert(cfs != nullptr, "invariant");
2123   assert(cp != nullptr, "invariant");
2124   assert(has_localvariable_table != nullptr, "invariant");
2125 
2126   ResourceMark rm(THREAD);
2127   // Parse fixed parts:
2128   // access_flags, name_index, descriptor_index, attributes_count
2129   cfs->guarantee_more(8, CHECK_NULL);
2130 
2131   u2 flags = cfs->get_u2_fast();
2132   const u2 name_index = cfs->get_u2_fast();
2133   const int cp_size = cp->length();
2134   guarantee_property(
2135     valid_symbol_at(name_index),
2136     "Illegal constant pool index %u for method name in class file %s",
2137     name_index, CHECK_NULL);
2138   const Symbol* const name = cp->symbol_at(name_index);

2140 
2141   const u2 signature_index = cfs->get_u2_fast();
2142   guarantee_property(
2143     valid_symbol_at(signature_index),
2144     "Illegal constant pool index %u for method signature in class file %s",
2145     signature_index, CHECK_NULL);
2146   const Symbol* const signature = cp->symbol_at(signature_index);
2147 
2148   if (name == vmSymbols::class_initializer_name()) {
2149     // We ignore the other access flags for a valid class initializer.
2150     // (JVM Spec 2nd ed., chapter 4.6)
2151     if (_major_version < 51) { // backward compatibility
2152       flags = JVM_ACC_STATIC;
2153     } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2154       flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2155     } else {
2156       classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2157       return nullptr;
2158     }
2159   } else {
2160     verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2161   }
2162 
2163   if (name == vmSymbols::object_initializer_name() && is_interface) {
2164     classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2165     return nullptr;
2166   }
2167 









2168   int args_size = -1;  // only used when _need_verify is true
2169   if (_need_verify) {
2170     verify_legal_name_with_signature(name, signature, CHECK_NULL);
2171     args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2172                  verify_legal_method_signature(name, signature, CHECK_NULL);
2173     if (args_size > MAX_ARGS_SIZE) {
2174       classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2175       return nullptr;
2176     }
2177   }
2178 
2179   AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2180 
2181   // Default values for code and exceptions attribute elements
2182   u2 max_stack = 0;
2183   u2 max_locals = 0;
2184   u4 code_length = 0;
2185   const u1* code_start = nullptr;
2186   u2 exception_table_length = 0;
2187   const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements

2675                           CHECK_NULL);
2676 
2677   if (InstanceKlass::is_finalization_enabled() &&
2678       name == vmSymbols::finalize_method_name() &&
2679       signature == vmSymbols::void_method_signature()) {
2680     if (m->is_empty_method()) {
2681       _has_empty_finalizer = true;
2682     } else {
2683       _has_finalizer = true;
2684     }
2685   }
2686 
2687   NOT_PRODUCT(m->verify());
2688   return m;
2689 }
2690 
2691 
2692 // Side-effects: populates the _methods field in the parser
2693 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2694                                     bool is_interface,


2695                                     bool* const has_localvariable_table,
2696                                     bool* has_final_method,
2697                                     bool* declares_nonstatic_concrete_methods,
2698                                     TRAPS) {
2699   assert(cfs != nullptr, "invariant");
2700   assert(has_localvariable_table != nullptr, "invariant");
2701   assert(has_final_method != nullptr, "invariant");
2702   assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2703 
2704   assert(nullptr == _methods, "invariant");
2705 
2706   cfs->guarantee_more(2, CHECK);  // length
2707   const u2 length = cfs->get_u2_fast();
2708   if (length == 0) {
2709     _methods = Universe::the_empty_method_array();
2710   } else {
2711     _methods = MetadataFactory::new_array<Method*>(_loader_data,
2712                                                    length,
2713                                                    nullptr,
2714                                                    CHECK);
2715 
2716     for (int index = 0; index < length; index++) {
2717       Method* method = parse_method(cfs,
2718                                     is_interface,


2719                                     _cp,
2720                                     has_localvariable_table,
2721                                     CHECK);
2722 
2723       if (method->is_final()) {
2724         *has_final_method = true;
2725       }
2726       // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2727       // used for interface initialization, and default method inheritance analysis
2728       if (is_interface && !(*declares_nonstatic_concrete_methods)
2729         && !method->is_abstract() && !method->is_static()) {
2730         *declares_nonstatic_concrete_methods = true;
2731       }
2732       _methods->at_put(index, method);
2733     }
2734 
2735     if (_need_verify && length > 1) {
2736       // Check duplicated methods
2737       ResourceMark rm(THREAD);
2738       // Set containing name-signature pairs

2964         valid_klass_reference_at(outer_class_info_index),
2965       "outer_class_info_index %u has bad constant type in class file %s",
2966       outer_class_info_index, CHECK_0);
2967 
2968     if (outer_class_info_index != 0) {
2969       const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
2970       char* bytes = (char*)outer_class_name->bytes();
2971       guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
2972                          "Outer class is an array class in class file %s", CHECK_0);
2973     }
2974     // Inner class name
2975     const u2 inner_name_index = cfs->get_u2_fast();
2976     guarantee_property(
2977       inner_name_index == 0 || valid_symbol_at(inner_name_index),
2978       "inner_name_index %u has bad constant type in class file %s",
2979       inner_name_index, CHECK_0);
2980     if (_need_verify) {
2981       guarantee_property(inner_class_info_index != outer_class_info_index,
2982                          "Class is both outer and inner class in class file %s", CHECK_0);
2983     }

2984     // Access flags
2985     u2 flags;
2986     // JVM_ACC_MODULE is defined in JDK-9 and later.
2987     if (_major_version >= JAVA_9_VERSION) {
2988       flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
2989     } else {
2990       flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
2991     }

2992     if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2993       // Set abstract bit for old class files for backward compatibility
2994       flags |= JVM_ACC_ABSTRACT;
2995     }









2996     verify_legal_class_modifiers(flags, CHECK_0);
2997     AccessFlags inner_access_flags(flags);
2998 
2999     inner_classes->at_put(index++, inner_class_info_index);
3000     inner_classes->at_put(index++, outer_class_info_index);
3001     inner_classes->at_put(index++, inner_name_index);
3002     inner_classes->at_put(index++, inner_access_flags.as_unsigned_short());
3003   }
3004 
3005   // Check for circular and duplicate entries.
3006   bool has_circularity = false;
3007   if (_need_verify) {
3008     has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3009     if (has_circularity) {
3010       // If circularity check failed then ignore InnerClasses attribute.
3011       MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3012       index = 0;
3013       if (parsed_enclosingmethod_attribute) {
3014         inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3015         _inner_classes = inner_classes;

3080   if (length > 0) {
3081     int index = 0;
3082     cfs->guarantee_more(2 * length, CHECK_0);
3083     for (int n = 0; n < length; n++) {
3084       const u2 class_info_index = cfs->get_u2_fast();
3085       guarantee_property(
3086         valid_klass_reference_at(class_info_index),
3087         "Permitted subclass class_info_index %u has bad constant type in class file %s",
3088         class_info_index, CHECK_0);
3089       permitted_subclasses->at_put(index++, class_info_index);
3090     }
3091     assert(index == size, "wrong size");
3092   }
3093 
3094   // Restore buffer's current position.
3095   cfs->set_current(current_mark);
3096 
3097   return length;
3098 }
3099 











































3100 //  Record {
3101 //    u2 attribute_name_index;
3102 //    u4 attribute_length;
3103 //    u2 components_count;
3104 //    component_info components[components_count];
3105 //  }
3106 //  component_info {
3107 //    u2 name_index;
3108 //    u2 descriptor_index
3109 //    u2 attributes_count;
3110 //    attribute_info_attributes[attributes_count];
3111 //  }
3112 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3113                                                      const ConstantPool* cp,
3114                                                      const u1* const record_attribute_start,
3115                                                      TRAPS) {
3116   const u1* const current_mark = cfs->current();
3117   int components_count = 0;
3118   unsigned int calculate_attr_size = 0;
3119   if (record_attribute_start != nullptr) {

3345   }
3346   guarantee_property(current_start + attribute_byte_length == cfs->current(),
3347                      "Bad length on BootstrapMethods in class file %s",
3348                      CHECK);
3349 }
3350 
3351 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3352                                                  ConstantPool* cp,
3353                  ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3354                                                  TRAPS) {
3355   assert(cfs != nullptr, "invariant");
3356   assert(cp != nullptr, "invariant");
3357   assert(parsed_annotations != nullptr, "invariant");
3358 
3359   // Set inner classes attribute to default sentinel
3360   _inner_classes = Universe::the_empty_short_array();
3361   // Set nest members attribute to default sentinel
3362   _nest_members = Universe::the_empty_short_array();
3363   // Set _permitted_subclasses attribute to default sentinel
3364   _permitted_subclasses = Universe::the_empty_short_array();


3365   cfs->guarantee_more(2, CHECK);  // attributes_count
3366   u2 attributes_count = cfs->get_u2_fast();
3367   bool parsed_sourcefile_attribute = false;
3368   bool parsed_innerclasses_attribute = false;
3369   bool parsed_nest_members_attribute = false;
3370   bool parsed_permitted_subclasses_attribute = false;

3371   bool parsed_nest_host_attribute = false;
3372   bool parsed_record_attribute = false;
3373   bool parsed_enclosingmethod_attribute = false;
3374   bool parsed_bootstrap_methods_attribute = false;
3375   const u1* runtime_visible_annotations = nullptr;
3376   int runtime_visible_annotations_length = 0;
3377   const u1* runtime_visible_type_annotations = nullptr;
3378   int runtime_visible_type_annotations_length = 0;
3379   bool runtime_invisible_type_annotations_exists = false;
3380   bool runtime_invisible_annotations_exists = false;
3381   bool parsed_source_debug_ext_annotations_exist = false;
3382   const u1* inner_classes_attribute_start = nullptr;
3383   u4  inner_classes_attribute_length = 0;
3384   u2  enclosing_method_class_index = 0;
3385   u2  enclosing_method_method_index = 0;
3386   const u1* nest_members_attribute_start = nullptr;
3387   u4  nest_members_attribute_length = 0;
3388   const u1* record_attribute_start = nullptr;
3389   u4  record_attribute_length = 0;
3390   const u1* permitted_subclasses_attribute_start = nullptr;
3391   u4  permitted_subclasses_attribute_length = 0;


3392 
3393   // Iterate over attributes
3394   while (attributes_count--) {
3395     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3396     const u2 attribute_name_index = cfs->get_u2_fast();
3397     const u4 attribute_length = cfs->get_u4_fast();
3398     guarantee_property(
3399       valid_symbol_at(attribute_name_index),
3400       "Attribute name has bad constant pool index %u in class file %s",
3401       attribute_name_index, CHECK);
3402     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3403     if (tag == vmSymbols::tag_source_file()) {
3404       // Check for SourceFile tag
3405       if (_need_verify) {
3406         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3407       }
3408       if (parsed_sourcefile_attribute) {
3409         classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3410         return;
3411       } else {

3587               return;
3588             }
3589             parsed_record_attribute = true;
3590             record_attribute_start = cfs->current();
3591             record_attribute_length = attribute_length;
3592           } else if (_major_version >= JAVA_17_VERSION) {
3593             if (tag == vmSymbols::tag_permitted_subclasses()) {
3594               if (parsed_permitted_subclasses_attribute) {
3595                 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3596                 return;
3597               }
3598               // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3599               if (_access_flags.is_final()) {
3600                 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3601                 return;
3602               }
3603               parsed_permitted_subclasses_attribute = true;
3604               permitted_subclasses_attribute_start = cfs->current();
3605               permitted_subclasses_attribute_length = attribute_length;
3606             }









3607           }
3608           // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3609           cfs->skip_u1(attribute_length, CHECK);
3610         } else {
3611           // Unknown attribute
3612           cfs->skip_u1(attribute_length, CHECK);
3613         }
3614       } else {
3615         // Unknown attribute
3616         cfs->skip_u1(attribute_length, CHECK);
3617       }
3618     } else {
3619       // Unknown attribute
3620       cfs->skip_u1(attribute_length, CHECK);
3621     }
3622   }
3623   _class_annotations = allocate_annotations(runtime_visible_annotations,
3624                                             runtime_visible_annotations_length,
3625                                             CHECK);
3626   _class_type_annotations = allocate_annotations(runtime_visible_type_annotations,

3663                             CHECK);
3664     if (_need_verify) {
3665       guarantee_property(record_attribute_length == calculated_attr_length,
3666                          "Record attribute has wrong length in class file %s",
3667                          CHECK);
3668     }
3669   }
3670 
3671   if (parsed_permitted_subclasses_attribute) {
3672     const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3673                             cfs,
3674                             permitted_subclasses_attribute_start,
3675                             CHECK);
3676     if (_need_verify) {
3677       guarantee_property(
3678         permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3679         "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3680     }
3681   }
3682 












3683   if (_max_bootstrap_specifier_index >= 0) {
3684     guarantee_property(parsed_bootstrap_methods_attribute,
3685                        "Missing BootstrapMethods attribute in class file %s", CHECK);
3686   }
3687 }
3688 
3689 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3690   assert(k != nullptr, "invariant");
3691 
3692   if (_synthetic_flag)
3693     k->set_is_synthetic();
3694   if (_sourcefile_index != 0) {
3695     k->set_source_file_name_index(_sourcefile_index);
3696   }
3697   if (_generic_signature_index != 0) {
3698     k->set_generic_signature_index(_generic_signature_index);
3699   }
3700   if (_sde_buffer != nullptr) {
3701     k->set_source_debug_extension(_sde_buffer, _sde_length);
3702   }

3729     _class_type_annotations  = nullptr;
3730     _fields_annotations      = nullptr;
3731     _fields_type_annotations = nullptr;
3732 }
3733 
3734 // Transfer ownership of metadata allocated to the InstanceKlass.
3735 void ClassFileParser::apply_parsed_class_metadata(
3736                                             InstanceKlass* this_klass,
3737                                             int java_fields_count) {
3738   assert(this_klass != nullptr, "invariant");
3739 
3740   _cp->set_pool_holder(this_klass);
3741   this_klass->set_constants(_cp);
3742   this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3743   this_klass->set_fieldinfo_search_table(_fieldinfo_search_table);
3744   this_klass->set_fields_status(_fields_status);
3745   this_klass->set_methods(_methods);
3746   this_klass->set_inner_classes(_inner_classes);
3747   this_klass->set_nest_members(_nest_members);
3748   this_klass->set_nest_host_index(_nest_host);

3749   this_klass->set_annotations(_combined_annotations);
3750   this_klass->set_permitted_subclasses(_permitted_subclasses);
3751   this_klass->set_record_components(_record_components);

3752 
3753   DEBUG_ONLY(FieldInfoStream::validate_search_table(_cp, _fieldinfo_stream, _fieldinfo_search_table));
3754 
3755   // Delay the setting of _local_interfaces and _transitive_interfaces until after
3756   // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3757   // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3758   // its _super. If an OOM occurs while loading the current klass, its _super field
3759   // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3760   // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3761   // dereferences to the deallocated _transitive_interfaces will result in a crash.
3762 
3763   // Clear out these fields so they don't get deallocated by the destructor
3764   clear_class_metadata();
3765 }
3766 
3767 AnnotationArray* ClassFileParser::allocate_annotations(const u1* const anno,
3768                                                        int anno_length,
3769                                                        TRAPS) {
3770   AnnotationArray* annotations = nullptr;
3771   if (anno != nullptr) {
3772     annotations = MetadataFactory::new_array<u1>(_loader_data,
3773                                                  anno_length,
3774                                                  CHECK_(annotations));
3775     for (int i = 0; i < anno_length; i++) {
3776       annotations->at_put(i, anno[i]);
3777     }
3778   }
3779   return annotations;
3780 }
3781 
3782 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3783                                                         const int super_class_index,
3784                                                         const bool need_verify,
3785                                                         TRAPS) {
3786   assert(cp != nullptr, "invariant");
3787   const InstanceKlass* super_klass = nullptr;
3788 
3789   if (super_class_index == 0) {
3790     guarantee_property(_class_name == vmSymbols::java_lang_Object(),
3791                        "Invalid superclass index %u in class file %s",
3792                        super_class_index,
3793                        CHECK_NULL);
3794   } else {
3795     guarantee_property(valid_klass_reference_at(super_class_index),
3796                        "Invalid superclass index %u in class file %s",
3797                        super_class_index,
3798                        CHECK_NULL);
3799     // The class name should be legal because it is checked when parsing constant pool.
3800     // However, make sure it is not an array type.
3801     bool is_array = false;
3802     if (cp->tag_at(super_class_index).is_klass()) {
3803       super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3804       if (need_verify)
3805         is_array = super_klass->is_array_klass();
3806     } else if (need_verify) {
3807       is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3808     }
3809     if (need_verify) {

3810       guarantee_property(!is_array,
3811                         "Bad superclass name in class file %s", CHECK_NULL);
3812     }
3813   }
3814   return super_klass;
3815 }
3816 
3817 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
3818   _max_nonstatic_oop_maps = max_blocks;
3819   _nonstatic_oop_map_count = 0;
3820   if (max_blocks == 0) {
3821     _nonstatic_oop_maps = nullptr;
3822   } else {
3823     _nonstatic_oop_maps =
3824         NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
3825     memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3826   }
3827 }
3828 
3829 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {

3963 
3964   // Check if this klass supports the java.lang.Cloneable interface
3965   if (vmClasses::Cloneable_klass_loaded()) {
3966     if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
3967       ik->set_is_cloneable();
3968     }
3969   }
3970 
3971   // If it cannot be fast-path allocated, set a bit in the layout helper.
3972   // See documentation of InstanceKlass::can_be_fastpath_allocated().
3973   assert(ik->size_helper() > 0, "layout_helper is initialized");
3974   if (ik->is_abstract() || ik->is_interface()
3975       || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
3976       || ik->size_helper() >= FastAllocateSizeLimit) {
3977     // Forbid fast-path allocation.
3978     const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
3979     ik->set_layout_helper(lh);
3980   }
3981 }
3982 






3983 // utility methods for appending an array with check for duplicates
3984 
3985 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
3986                               const Array<InstanceKlass*>* const ifs) {
3987   // iterate over new interfaces
3988   for (int i = 0; i < ifs->length(); i++) {
3989     InstanceKlass* const e = ifs->at(i);
3990     assert(e->is_klass() && e->is_interface(), "just checking");
3991     // add new interface
3992     result->append_if_missing(e);
3993   }
3994 }
3995 
3996 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
3997                                                             Array<InstanceKlass*>* local_ifs,
3998                                                             ClassLoaderData* loader_data,
3999                                                             TRAPS) {
4000   assert(local_ifs != nullptr, "invariant");
4001   assert(loader_data != nullptr, "invariant");
4002 

4006   // Add superclass transitive interfaces size
4007   if (super != nullptr) {
4008     super_size = super->transitive_interfaces()->length();
4009     max_transitive_size += super_size;
4010   }
4011   // Add local interfaces' super interfaces
4012   const int local_size = local_ifs->length();
4013   for (int i = 0; i < local_size; i++) {
4014     InstanceKlass* const l = local_ifs->at(i);
4015     max_transitive_size += l->transitive_interfaces()->length();
4016   }
4017   // Finally add local interfaces
4018   max_transitive_size += local_size;
4019   // Construct array
4020   if (max_transitive_size == 0) {
4021     // no interfaces, use canonicalized array
4022     return Universe::the_empty_instance_klass_array();
4023   } else if (max_transitive_size == super_size) {
4024     // no new local interfaces added, share superklass' transitive interface array
4025     return super->transitive_interfaces();
4026   } else if (max_transitive_size == local_size) {
4027     // only local interfaces added, share local interface array
4028     return local_ifs;

4029   } else {
4030     ResourceMark rm;
4031     GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4032 
4033     // Copy down from superclass
4034     if (super != nullptr) {
4035       append_interfaces(result, super->transitive_interfaces());
4036     }
4037 
4038     // Copy down from local interfaces' superinterfaces
4039     for (int i = 0; i < local_size; i++) {
4040       InstanceKlass* const l = local_ifs->at(i);
4041       append_interfaces(result, l->transitive_interfaces());
4042     }
4043     // Finally add local interfaces
4044     append_interfaces(result, local_ifs);
4045 
4046     // length will be less than the max_transitive_size if duplicates were removed
4047     const int length = result->length();
4048     assert(length <= max_transitive_size, "just checking");

4049     Array<InstanceKlass*>* const new_result =
4050       MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4051     for (int i = 0; i < length; i++) {
4052       InstanceKlass* const e = result->at(i);
4053       assert(e != nullptr, "just checking");
4054       new_result->at_put(i, e);
4055     }
4056     return new_result;
4057   }
4058 }
4059 
4060 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4061   assert(this_klass != nullptr, "invariant");
4062   const Klass* const super = this_klass->super();
4063 
4064   if (super != nullptr) {
4065     const InstanceKlass* super_ik = InstanceKlass::cast(super);
4066 
4067     if (super->is_final()) {
4068       classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4069       return;
4070     }
4071 
4072     if (super_ik->is_sealed()) {
4073       stringStream ss;
4074       ResourceMark rm(THREAD);
4075       if (!super_ik->has_as_permitted_subclass(this_klass, ss)) {
4076         classfile_icce_error(ss.as_string(), THREAD);
4077         return;
4078       }
4079     }
4080 










4081     Reflection::VerifyClassAccessResults vca_result =
4082       Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false);
4083     if (vca_result != Reflection::ACCESS_OK) {
4084       ResourceMark rm(THREAD);
4085       char* msg = Reflection::verify_class_access_msg(this_klass,
4086                                                       InstanceKlass::cast(super),
4087                                                       vca_result);
4088 
4089       // Names are all known to be < 64k so we know this formatted message is not excessively large.
4090       if (msg == nullptr) {
4091         bool same_module = (this_klass->module() == super->module());
4092         Exceptions::fthrow(
4093           THREAD_AND_LOCATION,
4094           vmSymbols::java_lang_IllegalAccessError(),
4095           "class %s cannot access its %ssuperclass %s (%s%s%s)",
4096           this_klass->external_name(),
4097           super->is_abstract() ? "abstract " : "",
4098           super->external_name(),
4099           (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),
4100           (same_module) ? "" : "; ",

4252 
4253 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4254   const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4255   assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4256   if (is_module) {
4257     ResourceMark rm(THREAD);
4258     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4259     Exceptions::fthrow(
4260       THREAD_AND_LOCATION,
4261       vmSymbols::java_lang_NoClassDefFoundError(),
4262       "%s is not a class because access_flag ACC_MODULE is set",
4263       _class_name->as_C_string());
4264     return;
4265   }
4266 
4267   if (!_need_verify) { return; }
4268 
4269   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4270   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4271   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4272   const bool is_super      = (flags & JVM_ACC_SUPER)      != 0;
4273   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4274   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4275   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;


4276 
4277   if ((is_abstract && is_final) ||
4278       (is_interface && !is_abstract) ||
4279       (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4280       (!is_interface && major_gte_1_5 && is_annotation)) {

4281     ResourceMark rm(THREAD);
4282     // Names are all known to be < 64k so we know this formatted message is not excessively large.



4283     Exceptions::fthrow(
4284       THREAD_AND_LOCATION,
4285       vmSymbols::java_lang_ClassFormatError(),
4286       "Illegal class modifiers in class %s: 0x%X",
4287       _class_name->as_C_string(), flags
4288     );
4289     return;
4290   }
4291 }
4292 
4293 static bool has_illegal_visibility(jint flags) {
4294   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4295   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4296   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4297 
4298   return ((is_public && is_protected) ||
4299           (is_public && is_private) ||
4300           (is_protected && is_private));
4301 }
4302 
4303 // A legal major_version.minor_version must be one of the following:
4304 //
4305 //  Major_version >= 45 and major_version < 56, any minor_version.
4306 //  Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4307 //  Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.

4337         THREAD_AND_LOCATION,
4338         vmSymbols::java_lang_UnsupportedClassVersionError(),
4339         "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4340         "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4341         class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4342       return;
4343     }
4344 
4345     if (!Arguments::enable_preview()) {
4346       classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4347                            class_name, major, minor, THREAD);
4348       return;
4349     }
4350 
4351   } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4352     classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4353                          class_name, major, minor, THREAD);
4354   }
4355 }
4356 
4357 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4358                                                    bool is_interface,
4359                                                    TRAPS) const {
4360   if (!_need_verify) { return; }
4361 
4362   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4363   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4364   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4365   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4366   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4367   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4368   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4369   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;

4370   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4371 
4372   bool is_illegal = false;

4373 
4374   if (is_interface) {
4375     if (!is_public || !is_static || !is_final || is_private ||
4376         is_protected || is_volatile || is_transient ||
4377         (major_gte_1_5 && is_enum)) {
4378       is_illegal = true;
4379     }
4380   } else { // not interface
4381     if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4382       is_illegal = true;





















4383     }
4384   }
4385 
4386   if (is_illegal) {
4387     ResourceMark rm(THREAD);
4388     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4389     Exceptions::fthrow(
4390       THREAD_AND_LOCATION,
4391       vmSymbols::java_lang_ClassFormatError(),
4392       "Illegal field modifiers in class %s: 0x%X",
4393       _class_name->as_C_string(), flags);
4394     return;
4395   }
4396 }
4397 
4398 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4399                                                     bool is_interface,
4400                                                     const Symbol* name,
4401                                                     TRAPS) const {
4402   if (!_need_verify) { return; }
4403 
4404   const bool is_public       = (flags & JVM_ACC_PUBLIC)       != 0;
4405   const bool is_private      = (flags & JVM_ACC_PRIVATE)      != 0;
4406   const bool is_static       = (flags & JVM_ACC_STATIC)       != 0;
4407   const bool is_final        = (flags & JVM_ACC_FINAL)        != 0;
4408   const bool is_native       = (flags & JVM_ACC_NATIVE)       != 0;
4409   const bool is_abstract     = (flags & JVM_ACC_ABSTRACT)     != 0;
4410   const bool is_bridge       = (flags & JVM_ACC_BRIDGE)       != 0;
4411   const bool is_strict       = (flags & JVM_ACC_STRICT)       != 0;
4412   const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4413   const bool is_protected    = (flags & JVM_ACC_PROTECTED)    != 0;
4414   const bool major_gte_1_5   = _major_version >= JAVA_1_5_VERSION;
4415   const bool major_gte_8     = _major_version >= JAVA_8_VERSION;
4416   const bool major_gte_17    = _major_version >= JAVA_17_VERSION;
4417   const bool is_initializer  = (name == vmSymbols::object_initializer_name());




4418 
4419   bool is_illegal = false;
4420 

4421   if (is_interface) {
4422     if (major_gte_8) {
4423       // Class file version is JAVA_8_VERSION or later Methods of
4424       // interfaces may set any of the flags except ACC_PROTECTED,
4425       // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4426       // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4427       if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4428           (is_native || is_protected || is_final || is_synchronized) ||
4429           // If a specific method of a class or interface has its
4430           // ACC_ABSTRACT flag set, it must not have any of its
4431           // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4432           // ACC_STRICT, or ACC_SYNCHRONIZED flags set.  No need to
4433           // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4434           // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4435           (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4436         is_illegal = true;
4437       }
4438     } else if (major_gte_1_5) {
4439       // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4440       if (!is_public || is_private || is_protected || is_static || is_final ||
4441           is_synchronized || is_native || !is_abstract || is_strict) {
4442         is_illegal = true;
4443       }
4444     } else {
4445       // Class file version is pre-JAVA_1_5_VERSION
4446       if (!is_public || is_static || is_final || is_native || !is_abstract) {
4447         is_illegal = true;
4448       }
4449     }
4450   } else { // not interface
4451     if (has_illegal_visibility(flags)) {
4452       is_illegal = true;
4453     } else {
4454       if (is_initializer) {
4455         if (is_static || is_final || is_synchronized || is_native ||
4456             is_abstract || (major_gte_1_5 && is_bridge)) {
4457           is_illegal = true;
4458         }
4459       } else { // not initializer
4460         if (is_abstract) {
4461           if ((is_final || is_native || is_private || is_static ||
4462               (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4463             is_illegal = true;





4464           }
4465         }
4466       }
4467     }
4468   }
4469 
4470   if (is_illegal) {
4471     ResourceMark rm(THREAD);
4472     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4473     Exceptions::fthrow(
4474       THREAD_AND_LOCATION,
4475       vmSymbols::java_lang_ClassFormatError(),
4476       "Method %s in class %s has illegal modifiers: 0x%X",
4477       name->as_C_string(), _class_name->as_C_string(), flags);

4478     return;
4479   }
4480 }
4481 
4482 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4483                                         int length,
4484                                         TRAPS) const {
4485   assert(_need_verify, "only called when _need_verify is true");
4486   // Note: 0 <= length < 64K, as it comes from a u2 entry in the CP.
4487   if (!UTF8::is_legal_utf8(buffer, static_cast<size_t>(length), _major_version <= 47)) {
4488     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4489   }
4490 }
4491 
4492 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4493 // In class names, '/' separates unqualified names.  This is verified in this function also.
4494 // Method names also may not contain the characters '<' or '>', unless <init>
4495 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
4496 // method.  Because these names have been checked as special cases before
4497 // calling this method in verify_legal_method_name.

4515         if (type == ClassFileParser::LegalClass) {
4516           if (p == name || p+1 >= name+length ||
4517               *(p+1) == JVM_SIGNATURE_SLASH) {
4518             return false;
4519           }
4520         } else {
4521           return false;   // do not permit '/' unless it's class name
4522         }
4523         break;
4524       case JVM_SIGNATURE_SPECIAL:
4525       case JVM_SIGNATURE_ENDSPECIAL:
4526         // do not permit '<' or '>' in method names
4527         if (type == ClassFileParser::LegalMethod) {
4528           return false;
4529         }
4530     }
4531   }
4532   return true;
4533 }
4534 









4535 // Take pointer to a UTF8 byte string (not NUL-terminated).
4536 // Skip over the longest part of the string that could
4537 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
4538 // Return a pointer to just past the fieldname.
4539 // Return null if no fieldname at all was found, or in the case of slash_ok
4540 // being true, we saw consecutive slashes (meaning we were looking for a
4541 // qualified path but found something that was badly-formed).
4542 static const char* skip_over_field_name(const char* const name,
4543                                         bool slash_ok,
4544                                         unsigned int length) {
4545   const char* p;
4546   jboolean last_is_slash = false;
4547   jboolean not_first_ch = false;
4548 
4549   for (p = name; p != name + length; not_first_ch = true) {
4550     const char* old_p = p;
4551     jchar ch = *p;
4552     if (ch < 128) {
4553       p++;
4554       // quick check for ascii

4616 // be taken as a field signature. Allow "void" if void_ok.
4617 // Return a pointer to just past the signature.
4618 // Return null if no legal signature is found.
4619 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4620                                                        bool void_ok,
4621                                                        unsigned int length,
4622                                                        TRAPS) const {
4623   unsigned int array_dim = 0;
4624   while (length > 0) {
4625     switch (signature[0]) {
4626     case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
4627     case JVM_SIGNATURE_BOOLEAN:
4628     case JVM_SIGNATURE_BYTE:
4629     case JVM_SIGNATURE_CHAR:
4630     case JVM_SIGNATURE_SHORT:
4631     case JVM_SIGNATURE_INT:
4632     case JVM_SIGNATURE_FLOAT:
4633     case JVM_SIGNATURE_LONG:
4634     case JVM_SIGNATURE_DOUBLE:
4635       return signature + 1;
4636     case JVM_SIGNATURE_CLASS: {

4637       if (_major_version < JAVA_1_5_VERSION) {
4638         // Skip over the class name if one is there
4639         const char* const p = skip_over_field_name(signature + 1, true, --length);
4640 
4641         // The next character better be a semicolon
4642         if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4643           return p + 1;
4644         }
4645       }
4646       else {
4647         // Skip leading 'L' and ignore first appearance of ';'
4648         signature++;
4649         const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4650         // Format check signature
4651         if (c != nullptr) {
4652           int newlen = pointer_delta_as_int(c, (char*) signature);
4653           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4654           if (!legal) {
4655             classfile_parse_error("Class name is empty or contains illegal character "
4656                                   "in descriptor in class file %s",
4657                                   THREAD);
4658             return nullptr;
4659           }
4660           return signature + newlen + 1;
4661         }
4662       }
4663       return nullptr;
4664     }
4665     case JVM_SIGNATURE_ARRAY:
4666       array_dim++;
4667       if (array_dim > 255) {

4683 
4684 // Checks if name is a legal class name.
4685 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4686   if (!_need_verify) { return; }
4687 
4688   assert(name->refcount() > 0, "symbol must be kept alive");
4689   char* bytes = (char*)name->bytes();
4690   unsigned int length = name->utf8_length();
4691   bool legal = false;
4692 
4693   if (length > 0) {
4694     const char* p;
4695     if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4696       p = skip_over_field_signature(bytes, false, length, CHECK);
4697       legal = (p != nullptr) && ((p - bytes) == (int)length);
4698     } else if (_major_version < JAVA_1_5_VERSION) {
4699       if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4700         p = skip_over_field_name(bytes, true, length);
4701         legal = (p != nullptr) && ((p - bytes) == (int)length);
4702       }




4703     } else {
4704       // 4900761: relax the constraints based on JSR202 spec
4705       // Class names may be drawn from the entire Unicode character set.
4706       // Identifiers between '/' must be unqualified names.
4707       // The utf8 string has been verified when parsing cpool entries.
4708       legal = verify_unqualified_name(bytes, length, LegalClass);
4709     }
4710   }
4711   if (!legal) {
4712     ResourceMark rm(THREAD);
4713     assert(_class_name != nullptr, "invariant");
4714     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4715     Exceptions::fthrow(
4716       THREAD_AND_LOCATION,
4717       vmSymbols::java_lang_ClassFormatError(),
4718       "Illegal class name \"%.*s\" in class file %s", length, bytes,
4719       _class_name->as_C_string()
4720     );
4721     return;
4722   }

4750       THREAD_AND_LOCATION,
4751       vmSymbols::java_lang_ClassFormatError(),
4752       "Illegal field name \"%.*s\" in class %s", length, bytes,
4753       _class_name->as_C_string()
4754     );
4755     return;
4756   }
4757 }
4758 
4759 // Checks if name is a legal method name.
4760 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
4761   if (!_need_verify) { return; }
4762 
4763   assert(name != nullptr, "method name is null");
4764   char* bytes = (char*)name->bytes();
4765   unsigned int length = name->utf8_length();
4766   bool legal = false;
4767 
4768   if (length > 0) {
4769     if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
4770       if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {

4771         legal = true;
4772       }
4773     } else if (_major_version < JAVA_1_5_VERSION) {
4774       const char* p;
4775       p = skip_over_field_name(bytes, false, length);
4776       legal = (p != nullptr) && ((p - bytes) == (int)length);
4777     } else {
4778       // 4881221: relax the constraints based on JSR202 spec
4779       legal = verify_unqualified_name(bytes, length, LegalMethod);
4780     }
4781   }
4782 
4783   if (!legal) {
4784     ResourceMark rm(THREAD);
4785     assert(_class_name != nullptr, "invariant");
4786     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4787     Exceptions::fthrow(
4788       THREAD_AND_LOCATION,
4789       vmSymbols::java_lang_ClassFormatError(),
4790       "Illegal method name \"%.*s\" in class %s", length, bytes,
4791       _class_name->as_C_string()
4792     );
4793     return;
4794   }
4795 }
4796 










4797 
4798 // Checks if signature is a legal field signature.
4799 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
4800                                                    const Symbol* signature,
4801                                                    TRAPS) const {
4802   if (!_need_verify) { return; }
4803 
4804   const char* const bytes = (const char*)signature->bytes();
4805   const unsigned int length = signature->utf8_length();
4806   const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
4807 
4808   if (p == nullptr || (p - bytes) != (int)length) {
4809     throwIllegalSignature("Field", name, signature, CHECK);
4810   }
4811 }
4812 
4813 // Check that the signature is compatible with the method name.  For example,
4814 // check that <init> has a void signature.
4815 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
4816                                                        const Symbol* signature,
4817                                                        TRAPS) const {
4818   if (!_need_verify) {
4819     return;
4820   }
4821 
4822   // Class initializers cannot have args for class format version >= 51.
4823   if (name == vmSymbols::class_initializer_name() &&
4824       signature != vmSymbols::void_method_signature() &&
4825       _major_version >= JAVA_7_VERSION) {
4826     throwIllegalSignature("Method", name, signature, THREAD);
4827     return;
4828   }
4829 
4830   int sig_length = signature->utf8_length();
4831   if (name->utf8_length() > 0 &&
4832       name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
4833       sig_length > 0 &&
4834       signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
4835     throwIllegalSignature("Method", name, signature, THREAD);
4836   }
4837 }
4838 
4839 // Checks if signature is a legal method signature.
4840 // Returns number of parameters
4841 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
4842                                                    const Symbol* signature,
4843                                                    TRAPS) const {
4844   if (!_need_verify) {
4845     // make sure caller's args_size will be less than 0 even for non-static
4846     // method so it will be recomputed in compute_size_of_parameters().
4847     return -2;
4848   }
4849 
4850   unsigned int args_size = 0;
4851   const char* p = (const char*)signature->bytes();
4852   unsigned int length = signature->utf8_length();
4853   const char* nextp;
4854 

4865       length -= pointer_delta_as_int(nextp, p);
4866       p = nextp;
4867       nextp = skip_over_field_signature(p, false, length, CHECK_0);
4868     }
4869     // The first non-signature thing better be a ')'
4870     if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
4871       length--;
4872       // Now we better just have a return value
4873       nextp = skip_over_field_signature(p, true, length, CHECK_0);
4874       if (nextp && ((int)length == (nextp - p))) {
4875         return args_size;
4876       }
4877     }
4878   }
4879   // Report error
4880   throwIllegalSignature("Method", name, signature, THREAD);
4881   return 0;
4882 }
4883 
4884 int ClassFileParser::static_field_size() const {
4885   assert(_field_info != nullptr, "invariant");
4886   return _field_info->_static_field_size;
4887 }
4888 
4889 int ClassFileParser::total_oop_map_count() const {
4890   assert(_field_info != nullptr, "invariant");
4891   return _field_info->oop_map_blocks->_nonstatic_oop_map_count;
4892 }
4893 
4894 jint ClassFileParser::layout_size() const {
4895   assert(_field_info != nullptr, "invariant");
4896   return _field_info->_instance_size;
4897 }
4898 
4899 static void check_methods_for_intrinsics(const InstanceKlass* ik,
4900                                          const Array<Method*>* methods) {
4901   assert(ik != nullptr, "invariant");
4902   assert(methods != nullptr, "invariant");
4903 
4904   // Set up Method*::intrinsic_id as soon as we know the names of methods.
4905   // (We used to do this lazily, but now we query it in Rewriter,
4906   // which is eagerly done for every method, so we might as well do it now,
4907   // when everything is fresh in memory.)
4908   const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);
4909 
4910   if (klass_id != vmSymbolID::NO_SID) {
4911     for (int j = 0; j < methods->length(); ++j) {
4912       Method* method = methods->at(j);
4913       method->init_intrinsic_id(klass_id);
4914 
4915       if (CheckIntrinsics) {
4916         // Check if an intrinsic is defined for method 'method',

4991   }
4992 }
4993 
4994 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
4995                                                       const ClassInstanceInfo& cl_inst_info,
4996                                                       TRAPS) {
4997   if (_klass != nullptr) {
4998     return _klass;
4999   }
5000 
5001   InstanceKlass* const ik =
5002     InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5003 
5004   if (is_hidden()) {
5005     mangle_hidden_class_name(ik);
5006   }
5007 
5008   fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5009 
5010   assert(_klass == ik, "invariant");
5011 
5012   return ik;
5013 }
5014 
5015 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5016                                           bool changed_by_loadhook,
5017                                           const ClassInstanceInfo& cl_inst_info,
5018                                           TRAPS) {
5019   assert(ik != nullptr, "invariant");
5020 
5021   // Set name and CLD before adding to CLD
5022   ik->set_class_loader_data(_loader_data);
5023   ik->set_class_loader_type();
5024   ik->set_name(_class_name);
5025 
5026   // Add all classes to our internal class loader list here,
5027   // including classes in the bootstrap (null) class loader.
5028   const bool publicize = !is_internal();
5029 
5030   _loader_data->add_class(ik, publicize);
5031 
5032   set_klass_to_deallocate(ik);
5033 
5034   assert(_field_info != nullptr, "invariant");
5035   assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5036   assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5037          "sanity");
5038 
5039   assert(ik->is_instance_klass(), "sanity");
5040   assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5041 
5042   // Fill in information already parsed
5043   ik->set_should_verify_class(_need_verify);
5044 
5045   // Not yet: supers are done below to support the new subtype-checking fields
5046   ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5047   ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);










5048   ik->set_static_oop_field_count(_static_oop_count);
5049 
5050   // this transfers ownership of a lot of arrays from
5051   // the parser onto the InstanceKlass*
5052   apply_parsed_class_metadata(ik, _java_fields_count);



5053 
5054   // can only set dynamic nest-host after static nest information is set
5055   if (cl_inst_info.dynamic_nest_host() != nullptr) {
5056     ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5057   }
5058 
5059   // note that is not safe to use the fields in the parser from this point on
5060   assert(nullptr == _cp, "invariant");
5061   assert(nullptr == _fieldinfo_stream, "invariant");
5062   assert(nullptr == _fieldinfo_search_table, "invariant");
5063   assert(nullptr == _fields_status, "invariant");
5064   assert(nullptr == _methods, "invariant");
5065   assert(nullptr == _inner_classes, "invariant");
5066   assert(nullptr == _nest_members, "invariant");

5067   assert(nullptr == _combined_annotations, "invariant");
5068   assert(nullptr == _record_components, "invariant");
5069   assert(nullptr == _permitted_subclasses, "invariant");

5070 
5071   if (_has_localvariable_table) {
5072     ik->set_has_localvariable_table(true);
5073   }
5074 
5075   if (_has_final_method) {
5076     ik->set_has_final_method();
5077   }
5078 
5079   ik->copy_method_ordering(_method_ordering, CHECK);
5080   // The InstanceKlass::_methods_jmethod_ids cache
5081   // is managed on the assumption that the initial cache
5082   // size is equal to the number of methods in the class. If
5083   // that changes, then InstanceKlass::idnum_can_increment()
5084   // has to be changed accordingly.
5085   ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5086 
5087   ik->set_this_class_index(_this_class_index);
5088 
5089   if (_is_hidden) {

5127   if ((_num_miranda_methods > 0) ||
5128       // if this class introduced new miranda methods or
5129       (_super_klass != nullptr && _super_klass->has_miranda_methods())
5130         // super class exists and this class inherited miranda methods
5131      ) {
5132        ik->set_has_miranda_methods(); // then set a flag
5133   }
5134 
5135   // Fill in information needed to compute superclasses.
5136   ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5137   ik->set_transitive_interfaces(_transitive_interfaces);
5138   ik->set_local_interfaces(_local_interfaces);
5139   _transitive_interfaces = nullptr;
5140   _local_interfaces = nullptr;
5141 
5142   // Initialize itable offset tables
5143   klassItable::setup_itable_offset_table(ik);
5144 
5145   // Compute transitive closure of interfaces this class implements
5146   // Do final class setup
5147   OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks;
5148   if (oop_map_blocks->_nonstatic_oop_map_count > 0) {
5149     oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5150   }
5151 
5152   if (_has_contended_fields || _parsed_annotations->is_contended() ||
5153       ( _super_klass != nullptr && _super_klass->has_contended_annotations())) {
5154     ik->set_has_contended_annotations(true);
5155   }
5156 
5157   // Fill in has_finalizer and layout_helper
5158   set_precomputed_flags(ik);
5159 
5160   // check if this class can access its super class
5161   check_super_class_access(ik, CHECK);
5162 
5163   // check if this class can access its superinterfaces
5164   check_super_interface_access(ik, CHECK);
5165 
5166   // check if this class overrides any final method
5167   check_final_method_override(ik, CHECK);

5188 
5189   assert(_all_mirandas != nullptr, "invariant");
5190 
5191   // Generate any default methods - default methods are public interface methods
5192   // that have a default implementation.  This is new with Java 8.
5193   if (_has_nonstatic_concrete_methods) {
5194     DefaultMethods::generate_default_methods(ik,
5195                                              _all_mirandas,
5196                                              CHECK);
5197   }
5198 
5199   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5200   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5201       !module_entry->has_default_read_edges()) {
5202     if (!module_entry->set_has_default_read_edges()) {
5203       // We won a potential race
5204       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5205     }
5206   }
5207 















5208   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5209 
5210   if (!is_internal()) {
5211     ik->print_class_load_logging(_loader_data, module_entry, _stream);
5212 
5213     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5214         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5215         log_is_enabled(Info, class, preview)) {
5216       ResourceMark rm;
5217       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5218                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5219     }
5220 
5221     if (log_is_enabled(Debug, class, resolve))  {
5222       ResourceMark rm;
5223       // print out the superclass.
5224       const char * from = ik->external_name();
5225       if (ik->java_super() != nullptr) {
5226         log_debug(class, resolve)("%s %s (super)",
5227                    from,

5270                                  const ClassLoadInfo* cl_info,
5271                                  Publicity pub_level,
5272                                  TRAPS) :
5273   _stream(stream),
5274   _class_name(nullptr),
5275   _loader_data(loader_data),
5276   _is_hidden(cl_info->is_hidden()),
5277   _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5278   _orig_cp_size(0),
5279   _static_oop_count(0),
5280   _super_klass(),
5281   _cp(nullptr),
5282   _fieldinfo_stream(nullptr),
5283   _fieldinfo_search_table(nullptr),
5284   _fields_status(nullptr),
5285   _methods(nullptr),
5286   _inner_classes(nullptr),
5287   _nest_members(nullptr),
5288   _nest_host(0),
5289   _permitted_subclasses(nullptr),

5290   _record_components(nullptr),
5291   _local_interfaces(nullptr),

5292   _transitive_interfaces(nullptr),
5293   _combined_annotations(nullptr),
5294   _class_annotations(nullptr),
5295   _class_type_annotations(nullptr),
5296   _fields_annotations(nullptr),
5297   _fields_type_annotations(nullptr),
5298   _klass(nullptr),
5299   _klass_to_deallocate(nullptr),
5300   _parsed_annotations(nullptr),
5301   _field_info(nullptr),

5302   _temp_field_info(nullptr),
5303   _method_ordering(nullptr),
5304   _all_mirandas(nullptr),
5305   _vtable_size(0),
5306   _itable_size(0),
5307   _num_miranda_methods(0),
5308   _protection_domain(cl_info->protection_domain()),
5309   _access_flags(),
5310   _pub_level(pub_level),
5311   _bad_constant_seen(0),
5312   _synthetic_flag(false),
5313   _sde_length(false),
5314   _sde_buffer(nullptr),
5315   _sourcefile_index(0),
5316   _generic_signature_index(0),
5317   _major_version(0),
5318   _minor_version(0),
5319   _this_class_index(0),
5320   _super_class_index(0),
5321   _itfs_len(0),
5322   _java_fields_count(0),
5323   _need_verify(false),
5324   _has_nonstatic_concrete_methods(false),
5325   _declares_nonstatic_concrete_methods(false),
5326   _has_localvariable_table(false),
5327   _has_final_method(false),
5328   _has_contended_fields(false),





5329   _has_finalizer(false),
5330   _has_empty_finalizer(false),
5331   _max_bootstrap_specifier_index(-1) {
5332 
5333   _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5334   _class_name->increment_refcount();
5335 
5336   assert(_loader_data != nullptr, "invariant");
5337   assert(stream != nullptr, "invariant");
5338   assert(_stream != nullptr, "invariant");
5339   assert(_stream->buffer() == _stream->current(), "invariant");
5340   assert(_class_name != nullptr, "invariant");
5341   assert(0 == _access_flags.as_unsigned_short(), "invariant");
5342 
5343   // Figure out whether we can skip format checking (matching classic VM behavior)
5344   // Always verify CFLH bytes from the user agents.
5345   _need_verify = stream->from_class_file_load_hook() ? true : Verifier::should_verify_for(_loader_data->class_loader());
5346 
5347   // synch back verification state to stream to check for truncation.
5348   stream->set_need_verify(_need_verify);
5349 
5350   parse_stream(stream, CHECK);
5351 
5352   post_process_parsed_stream(stream, _cp, CHECK);
5353 }
5354 
5355 void ClassFileParser::clear_class_metadata() {
5356   // metadata created before the instance klass is created.  Must be
5357   // deallocated if classfile parsing returns an error.
5358   _cp = nullptr;
5359   _fieldinfo_stream = nullptr;
5360   _fieldinfo_search_table = nullptr;
5361   _fields_status = nullptr;
5362   _methods = nullptr;
5363   _inner_classes = nullptr;
5364   _nest_members = nullptr;
5365   _permitted_subclasses = nullptr;

5366   _combined_annotations = nullptr;
5367   _class_annotations = _class_type_annotations = nullptr;
5368   _fields_annotations = _fields_type_annotations = nullptr;
5369   _record_components = nullptr;

5370 }
5371 
5372 // Destructor to clean up
5373 ClassFileParser::~ClassFileParser() {
5374   _class_name->decrement_refcount();
5375 
5376   if (_cp != nullptr) {
5377     MetadataFactory::free_metadata(_loader_data, _cp);
5378   }
5379 
5380   if (_fieldinfo_stream != nullptr) {
5381     MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5382   }
5383   MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_search_table);
5384 
5385   if (_fields_status != nullptr) {
5386     MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5387   }
5388 




5389   if (_methods != nullptr) {
5390     // Free methods
5391     InstanceKlass::deallocate_methods(_loader_data, _methods);
5392   }
5393 
5394   // beware of the Universe::empty_blah_array!!
5395   if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5396     MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5397   }
5398 
5399   if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5400     MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5401   }
5402 
5403   if (_record_components != nullptr) {
5404     InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5405   }
5406 
5407   if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5408     MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5409   }
5410 




5411   // Free interfaces
5412   InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5413                                        _local_interfaces, _transitive_interfaces);
5414 
5415   if (_combined_annotations != nullptr) {
5416     // After all annotations arrays have been created, they are installed into the
5417     // Annotations object that will be assigned to the InstanceKlass being created.
5418 
5419     // Deallocate the Annotations object and the installed annotations arrays.
5420     _combined_annotations->deallocate_contents(_loader_data);
5421 
5422     // If the _combined_annotations pointer is non-null,
5423     // then the other annotations fields should have been cleared.
5424     assert(_class_annotations       == nullptr, "Should have been cleared");
5425     assert(_class_type_annotations  == nullptr, "Should have been cleared");
5426     assert(_fields_annotations      == nullptr, "Should have been cleared");
5427     assert(_fields_type_annotations == nullptr, "Should have been cleared");
5428   } else {
5429     // If the annotations arrays were not installed into the Annotations object,
5430     // then they have to be deallocated explicitly.

5489 
5490   assert(cp_size == (u2)cp->length(), "invariant");
5491 
5492   // ACCESS FLAGS
5493   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
5494 
5495   // Access flags
5496   u2 flags;
5497   // JVM_ACC_MODULE is defined in JDK-9 and later.
5498   if (_major_version >= JAVA_9_VERSION) {
5499     flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5500   } else {
5501     flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5502   }
5503 
5504   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5505     // Set abstract bit for old class files for backward compatibility
5506     flags |= JVM_ACC_ABSTRACT;
5507   }
5508 









5509   verify_legal_class_modifiers(flags, CHECK);
5510 
5511   short bad_constant = class_bad_constant_seen();
5512   if (bad_constant != 0) {
5513     // Do not throw CFE until after the access_flags are checked because if
5514     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5515     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5516     return;
5517   }
5518 
5519   _access_flags.set_flags(flags);
5520 
5521   // This class and superclass
5522   _this_class_index = stream->get_u2_fast();
5523   guarantee_property(
5524     valid_cp_range(_this_class_index, cp_size) &&
5525       cp->tag_at(_this_class_index).is_unresolved_klass(),
5526     "Invalid this class index %u in constant pool in class file %s",
5527     _this_class_index, CHECK);
5528 

5592       }
5593       ls.cr();
5594     }
5595   }
5596 
5597   // SUPERKLASS
5598   _super_class_index = stream->get_u2_fast();
5599   _super_klass = parse_super_class(cp,
5600                                    _super_class_index,
5601                                    _need_verify,
5602                                    CHECK);
5603 
5604   // Interfaces
5605   _itfs_len = stream->get_u2_fast();
5606   parse_interfaces(stream,
5607                    _itfs_len,
5608                    cp,
5609                    &_has_nonstatic_concrete_methods,
5610                    CHECK);
5611 
5612   assert(_local_interfaces != nullptr, "invariant");
5613 
5614   // Fields (offsets are filled in later)
5615   parse_fields(stream,
5616                _access_flags.is_interface(),
5617                cp,
5618                cp_size,
5619                &_java_fields_count,
5620                CHECK);
5621 
5622   assert(_temp_field_info != nullptr, "invariant");
5623 
5624   // Methods
5625   parse_methods(stream,
5626                 _access_flags.is_interface(),


5627                 &_has_localvariable_table,
5628                 &_has_final_method,
5629                 &_declares_nonstatic_concrete_methods,
5630                 CHECK);
5631 
5632   assert(_methods != nullptr, "invariant");
5633 
5634   if (_declares_nonstatic_concrete_methods) {
5635     _has_nonstatic_concrete_methods = true;
5636   }
5637 
5638   // Additional attributes/annotations
5639   _parsed_annotations = new ClassAnnotationCollector();
5640   parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
5641 
5642   assert(_inner_classes != nullptr, "invariant");
5643 
5644   // Finalize the Annotations metadata object,
5645   // now that all annotation arrays have been created.
5646   create_combined_annotations(CHECK);

5686   // Update this_class_index's slot in the constant pool with the new Utf8 entry.
5687   // We have to update the resolved_klass_index and the name_index together
5688   // so extract the existing resolved_klass_index first.
5689   CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
5690   int resolved_klass_index = cp_klass_slot.resolved_klass_index();
5691   _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
5692   assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
5693          "Bad name_index");
5694 }
5695 
5696 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
5697                                                  ConstantPool* cp,
5698                                                  TRAPS) {
5699   assert(stream != nullptr, "invariant");
5700   assert(stream->at_eos(), "invariant");
5701   assert(cp != nullptr, "invariant");
5702   assert(_loader_data != nullptr, "invariant");
5703 
5704   if (_class_name == vmSymbols::java_lang_Object()) {
5705     guarantee_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
5706                        "java.lang.Object cannot implement an interface in class file %s",
5707                        CHECK);
5708   }
5709   // We check super class after class file is parsed and format is checked
5710   if (_super_class_index > 0 && nullptr == _super_klass) {
5711     Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
5712     if (_access_flags.is_interface()) {
5713       // Before attempting to resolve the superclass, check for class format
5714       // errors not checked yet.
5715       guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
5716         "Interfaces must have java.lang.Object as superclass in class file %s",
5717         CHECK);
5718     }
5719     Handle loader(THREAD, _loader_data->class_loader());
5720     if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
5721       _super_klass = vmClasses::Object_klass();
5722     } else {
5723       _super_klass = (const InstanceKlass*)
5724                        SystemDictionary::resolve_super_or_fail(_class_name,
5725                                                                super_class_name,
5726                                                                loader,
5727                                                                true,
5728                                                                CHECK);
5729     }
5730   }
5731 
5732   if (_super_klass != nullptr) {














5733     if (_super_klass->has_nonstatic_concrete_methods()) {
5734       _has_nonstatic_concrete_methods = true;
5735     }

5736 
5737     if (_super_klass->is_interface()) {
5738       classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
5739       return;





















5740     }
5741   }
5742 














































5743   // Compute the transitive list of all unique interfaces implemented by this class
5744   _transitive_interfaces =
5745     compute_transitive_interfaces(_super_klass,
5746                                   _local_interfaces,
5747                                   _loader_data,
5748                                   CHECK);
5749 
5750   assert(_transitive_interfaces != nullptr, "invariant");
5751 
5752   // sort methods
5753   _method_ordering = sort_methods(_methods);
5754 
5755   _all_mirandas = new GrowableArray<Method*>(20);
5756 
5757   Handle loader(THREAD, _loader_data->class_loader());
5758   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
5759                                                     &_num_miranda_methods,
5760                                                     _all_mirandas,
5761                                                     _super_klass,
5762                                                     _methods,
5763                                                     _access_flags,
5764                                                     _major_version,
5765                                                     loader,
5766                                                     _class_name,
5767                                                     _local_interfaces);
5768 
5769   // Size of Java itable (in words)
5770   _itable_size = _access_flags.is_interface() ? 0 :
5771     klassItable::compute_itable_size(_transitive_interfaces);
5772 
5773   assert(_parsed_annotations != nullptr, "invariant");
5774 
5775   _field_info = new FieldLayoutInfo();
5776   FieldLayoutBuilder lb(class_name(), super_klass(), _cp, /*_fields*/ _temp_field_info,
5777                         _parsed_annotations->is_contended(), _field_info);

























































































5778   lb.build_layout();

5779 
5780   int injected_fields_count = _temp_field_info->length() - _java_fields_count;
5781   _fieldinfo_stream =
5782     FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
5783                                             injected_fields_count, loader_data(), CHECK);
5784   _fieldinfo_search_table = FieldInfoStream::create_search_table(_cp, _fieldinfo_stream, _loader_data, CHECK);
5785   _fields_status =
5786     MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
5787                                             FieldStatus(0), CHECK);





















5788 }
5789 
5790 void ClassFileParser::set_klass(InstanceKlass* klass) {
5791 
5792 #ifdef ASSERT
5793   if (klass != nullptr) {
5794     assert(nullptr == _klass, "leaking?");
5795   }
5796 #endif
5797 
5798   _klass = klass;
5799 }
5800 
5801 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
5802 
5803 #ifdef ASSERT
5804   if (klass != nullptr) {
5805     assert(nullptr == _klass_to_deallocate, "leaking?");
5806   }
5807 #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 "oops/inlineKlass.hpp"
  26 #include "cds/cdsConfig.hpp"
  27 #include "classfile/classFileParser.hpp"
  28 #include "classfile/classFileStream.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/classLoaderData.inline.hpp"
  31 #include "classfile/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 "jvm.h"
  44 #include "logging/log.hpp"
  45 #include "logging/logStream.hpp"
  46 #include "memory/allocation.hpp"
  47 #include "memory/metadataFactory.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/annotations.hpp"
  52 #include "oops/constantPool.inline.hpp"
  53 #include "oops/fieldInfo.hpp"
  54 #include "oops/fieldStreams.inline.hpp"
  55 #include "oops/inlineKlass.inline.hpp"
  56 #include "oops/instanceKlass.inline.hpp"
  57 #include "oops/instanceMirrorKlass.hpp"
  58 #include "oops/klass.inline.hpp"
  59 #include "oops/klassVtable.hpp"
  60 #include "oops/metadata.hpp"
  61 #include "oops/method.inline.hpp"
  62 #include "oops/oop.inline.hpp"
  63 #include "oops/recordComponent.hpp"
  64 #include "oops/symbol.hpp"
  65 #include "prims/jvmtiExport.hpp"
  66 #include "prims/jvmtiThreadState.hpp"
  67 #include "runtime/arguments.hpp"
  68 #include "runtime/fieldDescriptor.inline.hpp"
  69 #include "runtime/handles.inline.hpp"
  70 #include "runtime/javaCalls.hpp"
  71 #include "runtime/os.hpp"
  72 #include "runtime/perfData.hpp"
  73 #include "runtime/reflection.hpp"
  74 #include "runtime/safepointVerifiers.hpp"
  75 #include "runtime/signature.hpp"
  76 #include "runtime/timer.hpp"
  77 #include "services/classLoadingService.hpp"
  78 #include "services/threadService.hpp"
  79 #include "utilities/align.hpp"
  80 #include "utilities/bitMap.inline.hpp"
  81 #include "utilities/checkedCast.hpp"
  82 #include "utilities/copy.hpp"
  83 #include "utilities/formatBuffer.hpp"
  84 #include "utilities/exceptions.hpp"
  85 #include "utilities/globalDefinitions.hpp"
  86 #include "utilities/growableArray.hpp"
  87 #include "utilities/macros.hpp"
  88 #include "utilities/ostream.hpp"
  89 #include "utilities/resourceHash.hpp"
  90 #include "utilities/stringUtils.hpp"
  91 #include "utilities/utf8.hpp"
  92 #if INCLUDE_CDS
  93 #include "classfile/systemDictionaryShared.hpp"
  94 #endif
  95 #if INCLUDE_JFR
  96 #include "jfr/support/jfrTraceIdExtension.hpp"
  97 #endif
  98 
  99 // We generally try to create the oops directly when parsing, rather than
 100 // allocating temporary data structures and copying the bytes twice. A
 101 // temporary area is only needed when parsing utf8 entries in the constant
 102 // pool and when parsing line number tables.
 103 
 104 // We add assert in debug mode when class format is not checked.
 105 
 106 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
 107 #define JAVA_MIN_SUPPORTED_VERSION        45
 108 #define JAVA_PREVIEW_MINOR_VERSION        65535
 109 
 110 // Used for two backward compatibility reasons:

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

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

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

 788 
 789   NameSigHash(Symbol* name, Symbol* sig) :
 790     _name(name),
 791     _sig(sig) {}
 792 
 793   static unsigned int hash(NameSigHash const& namesig) {
 794     return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
 795   }
 796 
 797   static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
 798     return (e0._name == e1._name) &&
 799           (e0._sig  == e1._sig);
 800   }
 801 };
 802 
 803 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
 804                                            NameSigHash::HASH_ROW_SIZE,
 805                                            AnyObj::RESOURCE_AREA, mtInternal,
 806                                            &NameSigHash::hash, &NameSigHash::equals>;
 807 
 808 static void check_identity_and_value_modifiers(ClassFileParser* current, const InstanceKlass* super_type, TRAPS) {
 809   assert(super_type != nullptr,"Method doesn't support null super type");
 810   if (super_type->access_flags().is_identity_class() && !current->access_flags().is_identity_class()
 811       && super_type->name() != vmSymbols::java_lang_Object()) {
 812       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
 813                 err_msg("Value type %s has an identity type as supertype",
 814                 current->class_name()->as_klass_external_name()));
 815   }
 816 }
 817 
 818 void ClassFileParser::parse_interfaces(const ClassFileStream* stream,
 819                                        int itfs_len,
 820                                        ConstantPool* cp,
 821                                        bool* const has_nonstatic_concrete_methods,
 822                                        // FIXME: lots of these functions
 823                                        // declare their parameters as const,
 824                                        // which adds only noise to the code.
 825                                        // Remove the spurious const modifiers.
 826                                        // Many are of the form "const int x"
 827                                        // or "T* const x".
 828                                        TRAPS) {
 829   assert(stream != nullptr, "invariant");
 830   assert(cp != nullptr, "invariant");
 831   assert(has_nonstatic_concrete_methods != nullptr, "invariant");
 832 
 833   if (itfs_len == 0) {
 834     _local_interfaces = Universe::the_empty_instance_klass_array();
 835 
 836   } else {
 837     assert(itfs_len > 0, "only called for len>0");
 838     _local_interface_indexes = new GrowableArray<u2>(itfs_len);
 839     int index = 0;

 840     for (index = 0; index < itfs_len; index++) {
 841       const u2 interface_index = stream->get_u2(CHECK);

 842       guarantee_property(
 843         valid_klass_reference_at(interface_index),
 844         "Interface name has bad constant pool index %u in class file %s",
 845         interface_index, CHECK);
 846       _local_interface_indexes->at_put_grow(index, interface_index);




























 847     }
 848 
 849     if (!_need_verify || itfs_len <= 1) {
 850       return;
 851     }
 852 
 853     // Check if there's any duplicates in interfaces
 854     ResourceMark rm(THREAD);
 855     // Set containing interface names
 856     ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
 857     for (index = 0; index < itfs_len; index++) {
 858       Symbol* interface_name = cp->klass_name_at(_local_interface_indexes->at(index));

 859       // If no duplicates, add (name, nullptr) in hashtable interface_names.
 860       if (!interface_names->put(interface_name, 0)) {
 861         classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
 862                                interface_name->as_C_string(), THREAD);
 863         return;
 864       }
 865     }
 866   }
 867 }
 868 
 869 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
 870                                            int constantvalue_index,
 871                                            int signature_index,
 872                                            TRAPS) const {
 873   // Make sure the constant pool entry is of a type appropriate to this field
 874   guarantee_property(
 875     (constantvalue_index > 0 &&
 876       constantvalue_index < cp->length()),
 877     "Bad initial value index %u in ConstantValue attribute in class file %s",
 878     constantvalue_index, CHECK);

 925 class AnnotationCollector : public ResourceObj{
 926 public:
 927   enum Location { _in_field, _in_method, _in_class };
 928   enum ID {
 929     _unknown = 0,
 930     _method_CallerSensitive,
 931     _method_ForceInline,
 932     _method_DontInline,
 933     _method_ChangesCurrentThread,
 934     _method_JvmtiHideEvents,
 935     _method_JvmtiMountTransition,
 936     _method_InjectedProfile,
 937     _method_LambdaForm_Compiled,
 938     _method_Hidden,
 939     _method_Scoped,
 940     _method_IntrinsicCandidate,
 941     _jdk_internal_vm_annotation_Contended,
 942     _field_Stable,
 943     _jdk_internal_vm_annotation_ReservedStackAccess,
 944     _jdk_internal_ValueBased,
 945     _jdk_internal_LooselyConsistentValue,
 946     _jdk_internal_NullRestricted,
 947     _java_lang_Deprecated,
 948     _java_lang_Deprecated_for_removal,
 949     _annotation_LIMIT
 950   };
 951   const Location _location;
 952   int _annotations_present;
 953   u2 _contended_group;
 954 
 955   AnnotationCollector(Location location)
 956     : _location(location), _annotations_present(0), _contended_group(0)
 957   {
 958     assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
 959   }
 960   // If this annotation name has an ID, report it (or _none).
 961   ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
 962   // Set the annotation name:
 963   void set_annotation(ID id) {
 964     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
 965     _annotations_present |= (int)nth_bit((int)id);
 966   }

1349   }
1350 
1351   *constantvalue_index_addr = constantvalue_index;
1352   *is_synthetic_addr = is_synthetic;
1353   *generic_signature_index_addr = generic_signature_index;
1354   AnnotationArray* a = allocate_annotations(runtime_visible_annotations,
1355                                             runtime_visible_annotations_length,
1356                                             CHECK);
1357   parsed_annotations->set_field_annotations(a);
1358   a = allocate_annotations(runtime_visible_type_annotations,
1359                            runtime_visible_type_annotations_length,
1360                            CHECK);
1361   parsed_annotations->set_field_type_annotations(a);
1362   return;
1363 }
1364 
1365 
1366 // Side-effects: populates the _fields, _fields_annotations,
1367 // _fields_type_annotations fields
1368 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1369                                    AccessFlags class_access_flags,
1370                                    ConstantPool* cp,
1371                                    const int cp_size,
1372                                    u2* const java_fields_count_ptr,
1373                                    TRAPS) {
1374 
1375   assert(cfs != nullptr, "invariant");
1376   assert(cp != nullptr, "invariant");
1377   assert(java_fields_count_ptr != nullptr, "invariant");
1378 
1379   assert(nullptr == _fields_annotations, "invariant");
1380   assert(nullptr == _fields_type_annotations, "invariant");
1381 
1382   bool is_inline_type = !class_access_flags.is_identity_class() && !class_access_flags.is_abstract();
1383   cfs->guarantee_more(2, CHECK);  // length
1384   const u2 length = cfs->get_u2_fast();
1385   *java_fields_count_ptr = length;
1386 
1387   int num_injected = 0;
1388   const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1389                                                                   &num_injected);
1390 
1391   // two more slots are required for inline classes:
1392   // one for the static field with a reference to the pre-allocated default value
1393   // one for the field the JVM injects when detecting an empty inline class
1394   const int total_fields = length + num_injected + (is_inline_type ? 2 : 0);
1395 
1396   // Allocate a temporary resource array to collect field data.
1397   // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1398   _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1399 
1400   int instance_fields_count = 0;
1401   ResourceMark rm(THREAD);
1402   for (int n = 0; n < length; n++) {
1403     // access_flags, name_index, descriptor_index, attributes_count
1404     cfs->guarantee_more(8, CHECK);
1405 
1406     jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS;
1407     if (!supports_inline_types()) {
1408       recognized_modifiers &= ~JVM_ACC_STRICT;
1409     }
1410 
1411     const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1412     verify_legal_field_modifiers(flags, class_access_flags, CHECK);
1413     AccessFlags access_flags;


1414     access_flags.set_flags(flags);
1415     FieldInfo::FieldFlags fieldFlags(0);
1416 
1417     const u2 name_index = cfs->get_u2_fast();
1418     guarantee_property(valid_symbol_at(name_index),
1419       "Invalid constant pool index %u for field name in class file %s",
1420       name_index, CHECK);
1421     const Symbol* const name = cp->symbol_at(name_index);
1422     verify_legal_field_name(name, CHECK);
1423 
1424     const u2 signature_index = cfs->get_u2_fast();
1425     guarantee_property(valid_symbol_at(signature_index),
1426       "Invalid constant pool index %u for field signature in class file %s",
1427       signature_index, CHECK);
1428     const Symbol* const sig = cp->symbol_at(signature_index);
1429     verify_legal_field_signature(name, sig, CHECK);
1430     if (!access_flags.is_static()) instance_fields_count++;
1431 
1432     u2 constantvalue_index = 0;
1433     bool is_synthetic = false;
1434     u2 generic_signature_index = 0;
1435     const bool is_static = access_flags.is_static();
1436     FieldAnnotationCollector parsed_annotations(_loader_data);
1437 
1438     bool is_null_restricted = false;
1439 
1440     const u2 attributes_count = cfs->get_u2_fast();
1441     if (attributes_count > 0) {
1442       parse_field_attributes(cfs,
1443                              attributes_count,
1444                              is_static,
1445                              signature_index,
1446                              &constantvalue_index,
1447                              &is_synthetic,
1448                              &generic_signature_index,
1449                              &parsed_annotations,
1450                              CHECK);
1451 
1452       if (parsed_annotations.field_annotations() != nullptr) {
1453         if (_fields_annotations == nullptr) {
1454           _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1455                                              _loader_data, length, nullptr,
1456                                              CHECK);
1457         }
1458         _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1459         if (parsed_annotations.has_annotation(AnnotationCollector::_jdk_internal_NullRestricted)) {
1460           if (!Signature::has_envelope(sig)) {
1461             Exceptions::fthrow(
1462               THREAD_AND_LOCATION,
1463               vmSymbols::java_lang_ClassFormatError(),
1464               "Illegal use of @jdk.internal.vm.annotation.NullRestricted annotation on field %s.%s with signature %s (primitive types can never be null)",
1465               class_name()->as_C_string(), name->as_C_string(), sig->as_C_string());
1466           }
1467           const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
1468           if (!is_strict) {
1469             Exceptions::fthrow(
1470               THREAD_AND_LOCATION,
1471               vmSymbols::java_lang_ClassFormatError(),
1472               "Illegal use of @jdk.internal.vm.annotation.NullRestricted annotation on field %s.%s which doesn't have the @jdk.internal.vm.annotation.Strict annotation",
1473               class_name()->as_C_string(), name->as_C_string());
1474           }
1475           is_null_restricted = true;
1476         }
1477         parsed_annotations.set_field_annotations(nullptr);
1478       }
1479       if (parsed_annotations.field_type_annotations() != nullptr) {
1480         if (_fields_type_annotations == nullptr) {
1481           _fields_type_annotations =
1482             MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1483                                                          length,
1484                                                          nullptr,
1485                                                          CHECK);
1486         }
1487         _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1488         parsed_annotations.set_field_type_annotations(nullptr);
1489       }
1490 
1491       if (is_synthetic) {
1492         access_flags.set_is_synthetic();
1493       }
1494       if (generic_signature_index != 0) {
1495         fieldFlags.update_generic(true);
1496       }
1497     }
1498 
1499     if (is_null_restricted) {
1500       fieldFlags.update_null_free_inline_type(true);
1501     }
1502 
1503     const BasicType type = cp->basic_type_for_signature_at(signature_index);
1504 
1505     // Update number of static oop fields.
1506     if (is_static && is_reference_type(type)) {
1507       _static_oop_count++;
1508     }
1509 
1510     FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1511     fi.set_index(n);
1512     if (fieldFlags.is_generic()) {
1513       fi.set_generic_signature_index(generic_signature_index);
1514     }
1515     parsed_annotations.apply_to(&fi);
1516     if (fi.field_flags().is_contended()) {
1517       _has_contended_fields = true;
1518     }
1519     if (access_flags.is_strict() && access_flags.is_static()) {
1520       _has_strict_static_fields = true;
1521     }
1522     _temp_field_info->append(fi);
1523   }
1524   assert(_temp_field_info->length() == length, "Must be");
1525 

1526   if (num_injected != 0) {
1527     for (int n = 0; n < num_injected; n++) {
1528       // Check for duplicates
1529       if (injected[n].may_be_java) {
1530         const Symbol* const name      = injected[n].name();
1531         const Symbol* const signature = injected[n].signature();
1532         bool duplicate = false;
1533         for (int i = 0; i < length; i++) {
1534           const FieldInfo* const f = _temp_field_info->adr_at(i);
1535           if (name      == cp->symbol_at(f->name_index()) &&
1536               signature == cp->symbol_at(f->signature_index())) {
1537             // Symbol is desclared in Java so skip this one
1538             duplicate = true;
1539             break;
1540           }
1541         }
1542         if (duplicate) {
1543           // These will be removed from the field array at the end
1544           continue;
1545         }
1546       }
1547 
1548       // Injected field
1549       FieldInfo::FieldFlags fflags(0);
1550       fflags.update_injected(true);
1551       AccessFlags aflags;
1552       FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1553       int idx = _temp_field_info->append(fi);
1554       _temp_field_info->adr_at(idx)->set_index(idx);

1555     }
1556   }
1557 
1558   if (is_inline_type) {
1559     // Inject static ".null_reset" field. This is an all-zero value with its null-channel set to zero.
1560     // IT should never be seen by user code, it is used when writing "null" to a nullable flat field
1561     // The all-zero value ensure that any embedded oop will be set to null, to avoid keeping dead objects
1562     // alive.
1563     FieldInfo::FieldFlags fflags2(0);
1564     fflags2.update_injected(true);
1565     AccessFlags aflags2(JVM_ACC_STATIC);
1566     FieldInfo fi2(aflags2,
1567                  (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(null_reset_value_name)),
1568                  (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(object_signature)),
1569                  0,
1570                  fflags2);
1571     int idx2 = _temp_field_info->append(fi2);
1572     _temp_field_info->adr_at(idx2)->set_index(idx2);
1573     _static_oop_count++;
1574   }
1575 
1576   if (_need_verify && length > 1) {
1577     // Check duplicated fields
1578     ResourceMark rm(THREAD);
1579     // Set containing name-signature pairs
1580     NameSigHashtable* names_and_sigs = new NameSigHashtable();
1581     for (int i = 0; i < _temp_field_info->length(); i++) {
1582       NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1583                                _temp_field_info->adr_at(i)->signature(_cp));
1584       // If no duplicates, add name/signature in hashtable names_and_sigs.
1585       if(!names_and_sigs->put(name_and_sig, 0)) {
1586         classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1587                                name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1588         return;
1589       }
1590     }
1591   }
1592 }
1593 
1594 

1934     }
1935     case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
1936       if (_location != _in_field && _location != _in_class) {
1937         break;  // only allow for fields and classes
1938       }
1939       if (!EnableContended || (RestrictContended && !privileged)) {
1940         break;  // honor privileges
1941       }
1942       return _jdk_internal_vm_annotation_Contended;
1943     }
1944     case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
1945       if (_location != _in_method)  break;  // only allow for methods
1946       if (RestrictReservedStack && !privileged) break; // honor privileges
1947       return _jdk_internal_vm_annotation_ReservedStackAccess;
1948     }
1949     case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
1950       if (_location != _in_class)   break;  // only allow for classes
1951       if (!privileged)              break;  // only allow in privileged code
1952       return _jdk_internal_ValueBased;
1953     }
1954     case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_LooselyConsistentValue_signature): {
1955       if (_location != _in_class)   break; // only allow for classes
1956       return _jdk_internal_LooselyConsistentValue;
1957     }
1958     case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_NullRestricted_signature): {
1959       if (_location != _in_field)   break; // only allow for fields
1960       return _jdk_internal_NullRestricted;
1961     }
1962     case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
1963       return _java_lang_Deprecated;
1964     }
1965     default: {
1966       break;
1967     }
1968   }
1969   return AnnotationCollector::_unknown;
1970 }
1971 
1972 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
1973   if (is_contended())
1974     // Setting the contended group also sets the contended bit in field flags
1975     f->set_contended_group(contended_group());
1976   if (is_stable())
1977     (f->field_flags_addr())->update_stable(true);
1978 }
1979 
1980 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {
1981   // If there's an error deallocate metadata for field annotations

2165   }
2166 
2167   if (runtime_visible_type_annotations_length > 0) {
2168     a = allocate_annotations(runtime_visible_type_annotations,
2169                              runtime_visible_type_annotations_length,
2170                              CHECK);
2171     cm->set_type_annotations(a);
2172   }
2173 }
2174 
2175 
2176 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2177 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2178 // Method* to save footprint, so we only know the size of the resulting Method* when the
2179 // entire method attribute is parsed.
2180 //
2181 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2182 
2183 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2184                                       bool is_interface,
2185                                       bool is_value_class,
2186                                       bool is_abstract_class,
2187                                       const ConstantPool* cp,
2188                                       bool* const has_localvariable_table,
2189                                       TRAPS) {
2190   assert(cfs != nullptr, "invariant");
2191   assert(cp != nullptr, "invariant");
2192   assert(has_localvariable_table != nullptr, "invariant");
2193 
2194   ResourceMark rm(THREAD);
2195   // Parse fixed parts:
2196   // access_flags, name_index, descriptor_index, attributes_count
2197   cfs->guarantee_more(8, CHECK_NULL);
2198 
2199   u2 flags = cfs->get_u2_fast();
2200   const u2 name_index = cfs->get_u2_fast();
2201   const int cp_size = cp->length();
2202   guarantee_property(
2203     valid_symbol_at(name_index),
2204     "Illegal constant pool index %u for method name in class file %s",
2205     name_index, CHECK_NULL);
2206   const Symbol* const name = cp->symbol_at(name_index);

2208 
2209   const u2 signature_index = cfs->get_u2_fast();
2210   guarantee_property(
2211     valid_symbol_at(signature_index),
2212     "Illegal constant pool index %u for method signature in class file %s",
2213     signature_index, CHECK_NULL);
2214   const Symbol* const signature = cp->symbol_at(signature_index);
2215 
2216   if (name == vmSymbols::class_initializer_name()) {
2217     // We ignore the other access flags for a valid class initializer.
2218     // (JVM Spec 2nd ed., chapter 4.6)
2219     if (_major_version < 51) { // backward compatibility
2220       flags = JVM_ACC_STATIC;
2221     } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2222       flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2223     } else {
2224       classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2225       return nullptr;
2226     }
2227   } else {
2228     verify_legal_method_modifiers(flags, access_flags() , name, CHECK_NULL);
2229   }
2230 
2231   if (name == vmSymbols::object_initializer_name() && is_interface) {
2232     classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2233     return nullptr;
2234   }
2235 
2236   if (EnableValhalla) {
2237     if (((flags & JVM_ACC_SYNCHRONIZED) == JVM_ACC_SYNCHRONIZED)
2238         && ((flags & JVM_ACC_STATIC) == 0 )
2239         && !_access_flags.is_identity_class()) {
2240       classfile_parse_error("Invalid synchronized method in non-identity class %s", THREAD);
2241         return nullptr;
2242     }
2243   }
2244 
2245   int args_size = -1;  // only used when _need_verify is true
2246   if (_need_verify) {
2247     verify_legal_name_with_signature(name, signature, CHECK_NULL);
2248     args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2249                  verify_legal_method_signature(name, signature, CHECK_NULL);
2250     if (args_size > MAX_ARGS_SIZE) {
2251       classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2252       return nullptr;
2253     }
2254   }
2255 
2256   AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2257 
2258   // Default values for code and exceptions attribute elements
2259   u2 max_stack = 0;
2260   u2 max_locals = 0;
2261   u4 code_length = 0;
2262   const u1* code_start = nullptr;
2263   u2 exception_table_length = 0;
2264   const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements

2752                           CHECK_NULL);
2753 
2754   if (InstanceKlass::is_finalization_enabled() &&
2755       name == vmSymbols::finalize_method_name() &&
2756       signature == vmSymbols::void_method_signature()) {
2757     if (m->is_empty_method()) {
2758       _has_empty_finalizer = true;
2759     } else {
2760       _has_finalizer = true;
2761     }
2762   }
2763 
2764   NOT_PRODUCT(m->verify());
2765   return m;
2766 }
2767 
2768 
2769 // Side-effects: populates the _methods field in the parser
2770 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2771                                     bool is_interface,
2772                                     bool is_value_class,
2773                                     bool is_abstract_type,
2774                                     bool* const has_localvariable_table,
2775                                     bool* has_final_method,
2776                                     bool* declares_nonstatic_concrete_methods,
2777                                     TRAPS) {
2778   assert(cfs != nullptr, "invariant");
2779   assert(has_localvariable_table != nullptr, "invariant");
2780   assert(has_final_method != nullptr, "invariant");
2781   assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2782 
2783   assert(nullptr == _methods, "invariant");
2784 
2785   cfs->guarantee_more(2, CHECK);  // length
2786   const u2 length = cfs->get_u2_fast();
2787   if (length == 0) {
2788     _methods = Universe::the_empty_method_array();
2789   } else {
2790     _methods = MetadataFactory::new_array<Method*>(_loader_data,
2791                                                    length,
2792                                                    nullptr,
2793                                                    CHECK);
2794 
2795     for (int index = 0; index < length; index++) {
2796       Method* method = parse_method(cfs,
2797                                     is_interface,
2798                                     is_value_class,
2799                                     is_abstract_type,
2800                                     _cp,
2801                                     has_localvariable_table,
2802                                     CHECK);
2803 
2804       if (method->is_final()) {
2805         *has_final_method = true;
2806       }
2807       // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2808       // used for interface initialization, and default method inheritance analysis
2809       if (is_interface && !(*declares_nonstatic_concrete_methods)
2810         && !method->is_abstract() && !method->is_static()) {
2811         *declares_nonstatic_concrete_methods = true;
2812       }
2813       _methods->at_put(index, method);
2814     }
2815 
2816     if (_need_verify && length > 1) {
2817       // Check duplicated methods
2818       ResourceMark rm(THREAD);
2819       // Set containing name-signature pairs

3045         valid_klass_reference_at(outer_class_info_index),
3046       "outer_class_info_index %u has bad constant type in class file %s",
3047       outer_class_info_index, CHECK_0);
3048 
3049     if (outer_class_info_index != 0) {
3050       const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3051       char* bytes = (char*)outer_class_name->bytes();
3052       guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3053                          "Outer class is an array class in class file %s", CHECK_0);
3054     }
3055     // Inner class name
3056     const u2 inner_name_index = cfs->get_u2_fast();
3057     guarantee_property(
3058       inner_name_index == 0 || valid_symbol_at(inner_name_index),
3059       "inner_name_index %u has bad constant type in class file %s",
3060       inner_name_index, CHECK_0);
3061     if (_need_verify) {
3062       guarantee_property(inner_class_info_index != outer_class_info_index,
3063                          "Class is both outer and inner class in class file %s", CHECK_0);
3064     }
3065 
3066     // Access flags
3067     u2 flags;
3068     // JVM_ACC_MODULE is defined in JDK-9 and later.
3069     if (_major_version >= JAVA_9_VERSION) {
3070       flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3071     } else {
3072       flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3073     }
3074 
3075     if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3076       // Set abstract bit for old class files for backward compatibility
3077       flags |= JVM_ACC_ABSTRACT;
3078     }
3079 
3080     if (!supports_inline_types()) {
3081       const bool is_module = (flags & JVM_ACC_MODULE) != 0;
3082       const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3083       if (!is_module && !is_interface) {
3084         flags |= JVM_ACC_IDENTITY;
3085       }
3086     }
3087 
3088     verify_legal_class_modifiers(flags, CHECK_0);
3089     AccessFlags inner_access_flags(flags);
3090 
3091     inner_classes->at_put(index++, inner_class_info_index);
3092     inner_classes->at_put(index++, outer_class_info_index);
3093     inner_classes->at_put(index++, inner_name_index);
3094     inner_classes->at_put(index++, inner_access_flags.as_unsigned_short());
3095   }
3096 
3097   // Check for circular and duplicate entries.
3098   bool has_circularity = false;
3099   if (_need_verify) {
3100     has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3101     if (has_circularity) {
3102       // If circularity check failed then ignore InnerClasses attribute.
3103       MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3104       index = 0;
3105       if (parsed_enclosingmethod_attribute) {
3106         inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3107         _inner_classes = inner_classes;

3172   if (length > 0) {
3173     int index = 0;
3174     cfs->guarantee_more(2 * length, CHECK_0);
3175     for (int n = 0; n < length; n++) {
3176       const u2 class_info_index = cfs->get_u2_fast();
3177       guarantee_property(
3178         valid_klass_reference_at(class_info_index),
3179         "Permitted subclass class_info_index %u has bad constant type in class file %s",
3180         class_info_index, CHECK_0);
3181       permitted_subclasses->at_put(index++, class_info_index);
3182     }
3183     assert(index == size, "wrong size");
3184   }
3185 
3186   // Restore buffer's current position.
3187   cfs->set_current(current_mark);
3188 
3189   return length;
3190 }
3191 
3192 u2 ClassFileParser::parse_classfile_loadable_descriptors_attribute(const ClassFileStream* const cfs,
3193                                                                    const u1* const loadable_descriptors_attribute_start,
3194                                                                    TRAPS) {
3195   const u1* const current_mark = cfs->current();
3196   u2 length = 0;
3197   if (loadable_descriptors_attribute_start != nullptr) {
3198     cfs->set_current(loadable_descriptors_attribute_start);
3199     cfs->guarantee_more(2, CHECK_0);  // length
3200     length = cfs->get_u2_fast();
3201   }
3202   const int size = length;
3203   Array<u2>* const loadable_descriptors = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3204   _loadable_descriptors = loadable_descriptors;
3205   if (length > 0) {
3206     int index = 0;
3207     cfs->guarantee_more(2 * length, CHECK_0);
3208     for (int n = 0; n < length; n++) {
3209       const u2 descriptor_index = cfs->get_u2_fast();
3210       guarantee_property(
3211         valid_symbol_at(descriptor_index),
3212         "LoadableDescriptors descriptor_index %u has bad constant type in class file %s",
3213         descriptor_index, CHECK_0);
3214       Symbol* descriptor = _cp->symbol_at(descriptor_index);
3215       bool valid = legal_field_signature(descriptor, CHECK_0);
3216       if(!valid) {
3217         ResourceMark rm(THREAD);
3218         Exceptions::fthrow(THREAD_AND_LOCATION,
3219           vmSymbols::java_lang_ClassFormatError(),
3220           "Descriptor from LoadableDescriptors attribute at index \"%d\" in class %s has illegal signature \"%s\"",
3221           descriptor_index, _class_name->as_C_string(), descriptor->as_C_string());
3222         return 0;
3223       }
3224       loadable_descriptors->at_put(index++, descriptor_index);
3225     }
3226     assert(index == size, "wrong size");
3227   }
3228 
3229   // Restore buffer's current position.
3230   cfs->set_current(current_mark);
3231 
3232   return length;
3233 }
3234 
3235 //  Record {
3236 //    u2 attribute_name_index;
3237 //    u4 attribute_length;
3238 //    u2 components_count;
3239 //    component_info components[components_count];
3240 //  }
3241 //  component_info {
3242 //    u2 name_index;
3243 //    u2 descriptor_index
3244 //    u2 attributes_count;
3245 //    attribute_info_attributes[attributes_count];
3246 //  }
3247 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3248                                                      const ConstantPool* cp,
3249                                                      const u1* const record_attribute_start,
3250                                                      TRAPS) {
3251   const u1* const current_mark = cfs->current();
3252   int components_count = 0;
3253   unsigned int calculate_attr_size = 0;
3254   if (record_attribute_start != nullptr) {

3480   }
3481   guarantee_property(current_start + attribute_byte_length == cfs->current(),
3482                      "Bad length on BootstrapMethods in class file %s",
3483                      CHECK);
3484 }
3485 
3486 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3487                                                  ConstantPool* cp,
3488                  ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3489                                                  TRAPS) {
3490   assert(cfs != nullptr, "invariant");
3491   assert(cp != nullptr, "invariant");
3492   assert(parsed_annotations != nullptr, "invariant");
3493 
3494   // Set inner classes attribute to default sentinel
3495   _inner_classes = Universe::the_empty_short_array();
3496   // Set nest members attribute to default sentinel
3497   _nest_members = Universe::the_empty_short_array();
3498   // Set _permitted_subclasses attribute to default sentinel
3499   _permitted_subclasses = Universe::the_empty_short_array();
3500   // Set _loadable_descriptors attribute to default sentinel
3501   _loadable_descriptors = Universe::the_empty_short_array();
3502   cfs->guarantee_more(2, CHECK);  // attributes_count
3503   u2 attributes_count = cfs->get_u2_fast();
3504   bool parsed_sourcefile_attribute = false;
3505   bool parsed_innerclasses_attribute = false;
3506   bool parsed_nest_members_attribute = false;
3507   bool parsed_permitted_subclasses_attribute = false;
3508   bool parsed_loadable_descriptors_attribute = false;
3509   bool parsed_nest_host_attribute = false;
3510   bool parsed_record_attribute = false;
3511   bool parsed_enclosingmethod_attribute = false;
3512   bool parsed_bootstrap_methods_attribute = false;
3513   const u1* runtime_visible_annotations = nullptr;
3514   int runtime_visible_annotations_length = 0;
3515   const u1* runtime_visible_type_annotations = nullptr;
3516   int runtime_visible_type_annotations_length = 0;
3517   bool runtime_invisible_type_annotations_exists = false;
3518   bool runtime_invisible_annotations_exists = false;
3519   bool parsed_source_debug_ext_annotations_exist = false;
3520   const u1* inner_classes_attribute_start = nullptr;
3521   u4  inner_classes_attribute_length = 0;
3522   u2  enclosing_method_class_index = 0;
3523   u2  enclosing_method_method_index = 0;
3524   const u1* nest_members_attribute_start = nullptr;
3525   u4  nest_members_attribute_length = 0;
3526   const u1* record_attribute_start = nullptr;
3527   u4  record_attribute_length = 0;
3528   const u1* permitted_subclasses_attribute_start = nullptr;
3529   u4  permitted_subclasses_attribute_length = 0;
3530   const u1* loadable_descriptors_attribute_start = nullptr;
3531   u4  loadable_descriptors_attribute_length = 0;
3532 
3533   // Iterate over attributes
3534   while (attributes_count--) {
3535     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3536     const u2 attribute_name_index = cfs->get_u2_fast();
3537     const u4 attribute_length = cfs->get_u4_fast();
3538     guarantee_property(
3539       valid_symbol_at(attribute_name_index),
3540       "Attribute name has bad constant pool index %u in class file %s",
3541       attribute_name_index, CHECK);
3542     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3543     if (tag == vmSymbols::tag_source_file()) {
3544       // Check for SourceFile tag
3545       if (_need_verify) {
3546         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3547       }
3548       if (parsed_sourcefile_attribute) {
3549         classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3550         return;
3551       } else {

3727               return;
3728             }
3729             parsed_record_attribute = true;
3730             record_attribute_start = cfs->current();
3731             record_attribute_length = attribute_length;
3732           } else if (_major_version >= JAVA_17_VERSION) {
3733             if (tag == vmSymbols::tag_permitted_subclasses()) {
3734               if (parsed_permitted_subclasses_attribute) {
3735                 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3736                 return;
3737               }
3738               // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3739               if (_access_flags.is_final()) {
3740                 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3741                 return;
3742               }
3743               parsed_permitted_subclasses_attribute = true;
3744               permitted_subclasses_attribute_start = cfs->current();
3745               permitted_subclasses_attribute_length = attribute_length;
3746             }
3747             if (EnableValhalla && tag == vmSymbols::tag_loadable_descriptors()) {
3748               if (parsed_loadable_descriptors_attribute) {
3749                 classfile_parse_error("Multiple LoadableDescriptors attributes in class file %s", CHECK);
3750                 return;
3751               }
3752               parsed_loadable_descriptors_attribute = true;
3753               loadable_descriptors_attribute_start = cfs->current();
3754               loadable_descriptors_attribute_length = attribute_length;
3755             }
3756           }
3757           // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3758           cfs->skip_u1(attribute_length, CHECK);
3759         } else {
3760           // Unknown attribute
3761           cfs->skip_u1(attribute_length, CHECK);
3762         }
3763       } else {
3764         // Unknown attribute
3765         cfs->skip_u1(attribute_length, CHECK);
3766       }
3767     } else {
3768       // Unknown attribute
3769       cfs->skip_u1(attribute_length, CHECK);
3770     }
3771   }
3772   _class_annotations = allocate_annotations(runtime_visible_annotations,
3773                                             runtime_visible_annotations_length,
3774                                             CHECK);
3775   _class_type_annotations = allocate_annotations(runtime_visible_type_annotations,

3812                             CHECK);
3813     if (_need_verify) {
3814       guarantee_property(record_attribute_length == calculated_attr_length,
3815                          "Record attribute has wrong length in class file %s",
3816                          CHECK);
3817     }
3818   }
3819 
3820   if (parsed_permitted_subclasses_attribute) {
3821     const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3822                             cfs,
3823                             permitted_subclasses_attribute_start,
3824                             CHECK);
3825     if (_need_verify) {
3826       guarantee_property(
3827         permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3828         "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3829     }
3830   }
3831 
3832   if (parsed_loadable_descriptors_attribute) {
3833     const u2 num_classes = parse_classfile_loadable_descriptors_attribute(
3834                             cfs,
3835                             loadable_descriptors_attribute_start,
3836                             CHECK);
3837     if (_need_verify) {
3838       guarantee_property(
3839         loadable_descriptors_attribute_length == sizeof(num_classes) + sizeof(u2) * num_classes,
3840         "Wrong LoadableDescriptors attribute length in class file %s", CHECK);
3841     }
3842   }
3843 
3844   if (_max_bootstrap_specifier_index >= 0) {
3845     guarantee_property(parsed_bootstrap_methods_attribute,
3846                        "Missing BootstrapMethods attribute in class file %s", CHECK);
3847   }
3848 }
3849 
3850 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3851   assert(k != nullptr, "invariant");
3852 
3853   if (_synthetic_flag)
3854     k->set_is_synthetic();
3855   if (_sourcefile_index != 0) {
3856     k->set_source_file_name_index(_sourcefile_index);
3857   }
3858   if (_generic_signature_index != 0) {
3859     k->set_generic_signature_index(_generic_signature_index);
3860   }
3861   if (_sde_buffer != nullptr) {
3862     k->set_source_debug_extension(_sde_buffer, _sde_length);
3863   }

3890     _class_type_annotations  = nullptr;
3891     _fields_annotations      = nullptr;
3892     _fields_type_annotations = nullptr;
3893 }
3894 
3895 // Transfer ownership of metadata allocated to the InstanceKlass.
3896 void ClassFileParser::apply_parsed_class_metadata(
3897                                             InstanceKlass* this_klass,
3898                                             int java_fields_count) {
3899   assert(this_klass != nullptr, "invariant");
3900 
3901   _cp->set_pool_holder(this_klass);
3902   this_klass->set_constants(_cp);
3903   this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3904   this_klass->set_fieldinfo_search_table(_fieldinfo_search_table);
3905   this_klass->set_fields_status(_fields_status);
3906   this_klass->set_methods(_methods);
3907   this_klass->set_inner_classes(_inner_classes);
3908   this_klass->set_nest_members(_nest_members);
3909   this_klass->set_nest_host_index(_nest_host);
3910   this_klass->set_loadable_descriptors(_loadable_descriptors);
3911   this_klass->set_annotations(_combined_annotations);
3912   this_klass->set_permitted_subclasses(_permitted_subclasses);
3913   this_klass->set_record_components(_record_components);
3914   this_klass->set_inline_layout_info_array(_inline_layout_info_array);
3915 
3916   DEBUG_ONLY(FieldInfoStream::validate_search_table(_cp, _fieldinfo_stream, _fieldinfo_search_table));
3917 
3918   // Delay the setting of _local_interfaces and _transitive_interfaces until after
3919   // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3920   // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3921   // its _super. If an OOM occurs while loading the current klass, its _super field
3922   // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3923   // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3924   // dereferences to the deallocated _transitive_interfaces will result in a crash.
3925 
3926   // Clear out these fields so they don't get deallocated by the destructor
3927   clear_class_metadata();
3928 }
3929 
3930 AnnotationArray* ClassFileParser::allocate_annotations(const u1* const anno,
3931                                                        int anno_length,
3932                                                        TRAPS) {
3933   AnnotationArray* annotations = nullptr;
3934   if (anno != nullptr) {
3935     annotations = MetadataFactory::new_array<u1>(_loader_data,
3936                                                  anno_length,
3937                                                  CHECK_(annotations));
3938     for (int i = 0; i < anno_length; i++) {
3939       annotations->at_put(i, anno[i]);
3940     }
3941   }
3942   return annotations;
3943 }
3944 
3945 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3946                                                         const int super_class_index,
3947                                                         const bool need_verify,
3948                                                         TRAPS) {
3949   assert(cp != nullptr, "invariant");
3950   const InstanceKlass* super_klass = nullptr;
3951 
3952   if (super_class_index == 0) {
3953     guarantee_property(_class_name == vmSymbols::java_lang_Object(),
3954                    "Invalid superclass index 0 in class file %s",
3955                    CHECK_NULL);

3956   } else {
3957     guarantee_property(valid_klass_reference_at(super_class_index),
3958                        "Invalid superclass index %u in class file %s",
3959                        super_class_index,
3960                        CHECK_NULL);
3961     // The class name should be legal because it is checked when parsing constant pool.
3962     // However, make sure it is not an array type.

3963     if (cp->tag_at(super_class_index).is_klass()) {
3964       super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));




3965     }
3966     if (need_verify) {
3967       bool is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3968       guarantee_property(!is_array,
3969                         "Bad superclass name in class file %s", CHECK_NULL);
3970     }
3971   }
3972   return super_klass;
3973 }
3974 
3975 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
3976   _max_nonstatic_oop_maps = max_blocks;
3977   _nonstatic_oop_map_count = 0;
3978   if (max_blocks == 0) {
3979     _nonstatic_oop_maps = nullptr;
3980   } else {
3981     _nonstatic_oop_maps =
3982         NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
3983     memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3984   }
3985 }
3986 
3987 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {

4121 
4122   // Check if this klass supports the java.lang.Cloneable interface
4123   if (vmClasses::Cloneable_klass_loaded()) {
4124     if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4125       ik->set_is_cloneable();
4126     }
4127   }
4128 
4129   // If it cannot be fast-path allocated, set a bit in the layout helper.
4130   // See documentation of InstanceKlass::can_be_fastpath_allocated().
4131   assert(ik->size_helper() > 0, "layout_helper is initialized");
4132   if (ik->is_abstract() || ik->is_interface()
4133       || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
4134       || ik->size_helper() >= FastAllocateSizeLimit) {
4135     // Forbid fast-path allocation.
4136     const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4137     ik->set_layout_helper(lh);
4138   }
4139 }
4140 
4141 bool ClassFileParser::supports_inline_types() const {
4142   // Inline types are only supported by class file version 70.65535 and later
4143   return _major_version > JAVA_26_VERSION ||
4144          (_major_version == JAVA_26_VERSION && _minor_version == JAVA_PREVIEW_MINOR_VERSION);
4145 }
4146 
4147 // utility methods for appending an array with check for duplicates
4148 
4149 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4150                               const Array<InstanceKlass*>* const ifs) {
4151   // iterate over new interfaces
4152   for (int i = 0; i < ifs->length(); i++) {
4153     InstanceKlass* const e = ifs->at(i);
4154     assert(e->is_klass() && e->is_interface(), "just checking");
4155     // add new interface
4156     result->append_if_missing(e);
4157   }
4158 }
4159 
4160 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4161                                                             Array<InstanceKlass*>* local_ifs,
4162                                                             ClassLoaderData* loader_data,
4163                                                             TRAPS) {
4164   assert(local_ifs != nullptr, "invariant");
4165   assert(loader_data != nullptr, "invariant");
4166 

4170   // Add superclass transitive interfaces size
4171   if (super != nullptr) {
4172     super_size = super->transitive_interfaces()->length();
4173     max_transitive_size += super_size;
4174   }
4175   // Add local interfaces' super interfaces
4176   const int local_size = local_ifs->length();
4177   for (int i = 0; i < local_size; i++) {
4178     InstanceKlass* const l = local_ifs->at(i);
4179     max_transitive_size += l->transitive_interfaces()->length();
4180   }
4181   // Finally add local interfaces
4182   max_transitive_size += local_size;
4183   // Construct array
4184   if (max_transitive_size == 0) {
4185     // no interfaces, use canonicalized array
4186     return Universe::the_empty_instance_klass_array();
4187   } else if (max_transitive_size == super_size) {
4188     // no new local interfaces added, share superklass' transitive interface array
4189     return super->transitive_interfaces();
4190     // The three lines below are commented to work around bug JDK-8245487
4191 //  } else if (max_transitive_size == local_size) {
4192 //    // only local interfaces added, share local interface array
4193 //    return local_ifs;
4194   } else {
4195     ResourceMark rm;
4196     GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4197 
4198     // Copy down from superclass
4199     if (super != nullptr) {
4200       append_interfaces(result, super->transitive_interfaces());
4201     }
4202 
4203     // Copy down from local interfaces' superinterfaces
4204     for (int i = 0; i < local_size; i++) {
4205       InstanceKlass* const l = local_ifs->at(i);
4206       append_interfaces(result, l->transitive_interfaces());
4207     }
4208     // Finally add local interfaces
4209     append_interfaces(result, local_ifs);
4210 
4211     // length will be less than the max_transitive_size if duplicates were removed
4212     const int length = result->length();
4213     assert(length <= max_transitive_size, "just checking");
4214 
4215     Array<InstanceKlass*>* const new_result =
4216       MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4217     for (int i = 0; i < length; i++) {
4218       InstanceKlass* const e = result->at(i);
4219       assert(e != nullptr, "just checking");
4220       new_result->at_put(i, e);
4221     }
4222     return new_result;
4223   }
4224 }
4225 
4226 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4227   assert(this_klass != nullptr, "invariant");
4228   const Klass* const super = this_klass->super();
4229 
4230   if (super != nullptr) {
4231     const InstanceKlass* super_ik = InstanceKlass::cast(super);
4232 
4233     if (super->is_final()) {
4234       classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4235       return;
4236     }
4237 
4238     if (super_ik->is_sealed()) {
4239       stringStream ss;
4240       ResourceMark rm(THREAD);
4241       if (!super_ik->has_as_permitted_subclass(this_klass, ss)) {
4242         classfile_icce_error(ss.as_string(), THREAD);
4243         return;
4244       }
4245     }
4246 
4247     // The JVMS says that super classes for value types must not have the ACC_IDENTITY
4248     // flag set. But, java.lang.Object must still be allowed to be a direct super class
4249     // for a value classes.  So, it is treated as a special case for now.
4250     if (!this_klass->access_flags().is_identity_class() &&
4251         super_ik->name() != vmSymbols::java_lang_Object() &&
4252         super_ik->is_identity_class()) {
4253       classfile_icce_error("value class %s cannot inherit from class %s", super_ik, THREAD);
4254       return;
4255     }
4256 
4257     Reflection::VerifyClassAccessResults vca_result =
4258       Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false);
4259     if (vca_result != Reflection::ACCESS_OK) {
4260       ResourceMark rm(THREAD);
4261       char* msg = Reflection::verify_class_access_msg(this_klass,
4262                                                       InstanceKlass::cast(super),
4263                                                       vca_result);
4264 
4265       // Names are all known to be < 64k so we know this formatted message is not excessively large.
4266       if (msg == nullptr) {
4267         bool same_module = (this_klass->module() == super->module());
4268         Exceptions::fthrow(
4269           THREAD_AND_LOCATION,
4270           vmSymbols::java_lang_IllegalAccessError(),
4271           "class %s cannot access its %ssuperclass %s (%s%s%s)",
4272           this_klass->external_name(),
4273           super->is_abstract() ? "abstract " : "",
4274           super->external_name(),
4275           (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),
4276           (same_module) ? "" : "; ",

4428 
4429 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4430   const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4431   assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4432   if (is_module) {
4433     ResourceMark rm(THREAD);
4434     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4435     Exceptions::fthrow(
4436       THREAD_AND_LOCATION,
4437       vmSymbols::java_lang_NoClassDefFoundError(),
4438       "%s is not a class because access_flag ACC_MODULE is set",
4439       _class_name->as_C_string());
4440     return;
4441   }
4442 
4443   if (!_need_verify) { return; }
4444 
4445   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4446   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4447   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4448   const bool is_identity   = (flags & JVM_ACC_IDENTITY)   != 0;
4449   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4450   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4451   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4452   const bool valid_value_class = is_identity || is_interface ||
4453                                  (supports_inline_types() && (!is_identity && (is_abstract || is_final)));
4454 
4455   if ((is_abstract && is_final) ||
4456       (is_interface && !is_abstract) ||
4457       (is_interface && major_gte_1_5 && (is_identity || is_enum)) ||   //  ACC_SUPER (now ACC_IDENTITY) was illegal for interfaces
4458       (!is_interface && major_gte_1_5 && is_annotation) ||
4459       (!valid_value_class)) {
4460     ResourceMark rm(THREAD);
4461     const char* class_note = "";
4462     if (!valid_value_class) {
4463       class_note = " (a value class must be final or else abstract)";
4464     }
4465     Exceptions::fthrow(
4466       THREAD_AND_LOCATION,
4467       vmSymbols::java_lang_ClassFormatError(),
4468       "Illegal class modifiers in class %s%s: 0x%X",
4469       _class_name->as_C_string(), class_note, flags
4470     );
4471     return;
4472   }
4473 }
4474 
4475 static bool has_illegal_visibility(jint flags) {
4476   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4477   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4478   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4479 
4480   return ((is_public && is_protected) ||
4481           (is_public && is_private) ||
4482           (is_protected && is_private));
4483 }
4484 
4485 // A legal major_version.minor_version must be one of the following:
4486 //
4487 //  Major_version >= 45 and major_version < 56, any minor_version.
4488 //  Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4489 //  Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.

4519         THREAD_AND_LOCATION,
4520         vmSymbols::java_lang_UnsupportedClassVersionError(),
4521         "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4522         "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4523         class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4524       return;
4525     }
4526 
4527     if (!Arguments::enable_preview()) {
4528       classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4529                            class_name, major, minor, THREAD);
4530       return;
4531     }
4532 
4533   } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4534     classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4535                          class_name, major, minor, THREAD);
4536   }
4537 }
4538 
4539 void ClassFileParser:: verify_legal_field_modifiers(jint flags,
4540                                                    AccessFlags class_access_flags,
4541                                                    TRAPS) const {
4542   if (!_need_verify) { return; }
4543 
4544   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4545   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4546   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4547   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4548   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4549   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4550   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4551   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4552   const bool is_strict    = (flags & JVM_ACC_STRICT)    != 0;
4553   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4554 
4555   const bool is_interface = class_access_flags.is_interface();
4556   const bool is_identity_class = class_access_flags.is_identity_class();
4557 
4558   bool is_illegal = false;
4559   const char* error_msg = "";
4560 
4561   // There is some overlap in the checks that apply, for example interface fields
4562   // must be static, static fields can't be strict, and therefore interfaces can't
4563   // have strict fields. So we don't have to check every possible invalid combination
4564   // individually as long as all are covered. Once we have found an illegal combination
4565   // we can stop checking.
4566 
4567   if (!is_illegal) {
4568     if (is_interface) {
4569       if (!is_public || !is_static || !is_final || is_private ||
4570           is_protected || is_volatile || is_transient ||
4571           (major_gte_1_5 && is_enum)) {
4572         is_illegal = true;
4573         error_msg = "interface fields must be public, static and final, and may be synthetic";
4574       }
4575     } else { // not interface
4576       if (has_illegal_visibility(flags)) {
4577         is_illegal = true;
4578         error_msg = "invalid visibility flags for class field";
4579       } else if (is_final && is_volatile) {
4580         is_illegal = true;
4581         error_msg = "fields cannot be final and volatile";
4582       } else if (supports_inline_types()) {
4583         if (!is_identity_class && !is_static && (!is_strict || !is_final)) {
4584           is_illegal = true;
4585           error_msg = "value class fields must be either non-static final and strict, or static";
4586         }
4587       }
4588     }
4589   }
4590 
4591   if (is_illegal) {
4592     ResourceMark rm(THREAD);
4593     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4594     Exceptions::fthrow(
4595       THREAD_AND_LOCATION,
4596       vmSymbols::java_lang_ClassFormatError(),
4597       "Illegal field modifiers (%s) in class %s: 0x%X",
4598       error_msg, _class_name->as_C_string(), flags);
4599     return;
4600   }
4601 }
4602 
4603 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4604                                                     AccessFlags class_access_flags,
4605                                                     const Symbol* name,
4606                                                     TRAPS) const {
4607   if (!_need_verify) { return; }
4608 
4609   const bool is_public       = (flags & JVM_ACC_PUBLIC)       != 0;
4610   const bool is_private      = (flags & JVM_ACC_PRIVATE)      != 0;
4611   const bool is_static       = (flags & JVM_ACC_STATIC)       != 0;
4612   const bool is_final        = (flags & JVM_ACC_FINAL)        != 0;
4613   const bool is_native       = (flags & JVM_ACC_NATIVE)       != 0;
4614   const bool is_abstract     = (flags & JVM_ACC_ABSTRACT)     != 0;
4615   const bool is_bridge       = (flags & JVM_ACC_BRIDGE)       != 0;
4616   const bool is_strict       = (flags & JVM_ACC_STRICT)       != 0;
4617   const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4618   const bool is_protected    = (flags & JVM_ACC_PROTECTED)    != 0;
4619   const bool major_gte_1_5   = _major_version >= JAVA_1_5_VERSION;
4620   const bool major_gte_8     = _major_version >= JAVA_8_VERSION;
4621   const bool major_gte_17    = _major_version >= JAVA_17_VERSION;
4622   const bool is_initializer  = (name == vmSymbols::object_initializer_name());
4623   // LW401 CR required: removal of value factories support
4624   const bool is_interface    = class_access_flags.is_interface();
4625   const bool is_identity_class = class_access_flags.is_identity_class();
4626   const bool is_abstract_class = class_access_flags.is_abstract();
4627 
4628   bool is_illegal = false;
4629 
4630   const char* class_note = "";
4631   if (is_interface) {
4632     if (major_gte_8) {
4633       // Class file version is JAVA_8_VERSION or later Methods of
4634       // interfaces may set any of the flags except ACC_PROTECTED,
4635       // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4636       // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4637       if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4638           (is_native || is_protected || is_final || is_synchronized) ||
4639           // If a specific method of a class or interface has its
4640           // ACC_ABSTRACT flag set, it must not have any of its
4641           // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4642           // ACC_STRICT, or ACC_SYNCHRONIZED flags set.  No need to
4643           // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4644           // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4645           (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4646         is_illegal = true;
4647       }
4648     } else if (major_gte_1_5) {
4649       // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4650       if (!is_public || is_private || is_protected || is_static || is_final ||
4651           is_synchronized || is_native || !is_abstract || is_strict) {
4652         is_illegal = true;
4653       }
4654     } else {
4655       // Class file version is pre-JAVA_1_5_VERSION
4656       if (!is_public || is_static || is_final || is_native || !is_abstract) {
4657         is_illegal = true;
4658       }
4659     }
4660   } else { // not interface
4661     if (has_illegal_visibility(flags)) {
4662       is_illegal = true;
4663     } else {
4664       if (is_initializer) {
4665         if (is_static || is_final || is_synchronized || is_native ||
4666             is_abstract || (major_gte_1_5 && is_bridge)) {
4667           is_illegal = true;
4668         }
4669       } else { // not initializer
4670         if (!is_identity_class && is_synchronized && !is_static) {
4671           is_illegal = true;
4672           class_note = " (not an identity class)";
4673         } else {
4674           if (is_abstract) {
4675             if ((is_final || is_native || is_private || is_static ||
4676                 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4677               is_illegal = true;
4678             }
4679           }
4680         }
4681       }
4682     }
4683   }
4684 
4685   if (is_illegal) {
4686     ResourceMark rm(THREAD);
4687     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4688     Exceptions::fthrow(
4689       THREAD_AND_LOCATION,
4690       vmSymbols::java_lang_ClassFormatError(),
4691       "Method %s in class %s%s has illegal modifiers: 0x%X",
4692       name->as_C_string(), _class_name->as_C_string(),
4693       class_note, flags);
4694     return;
4695   }
4696 }
4697 
4698 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4699                                         int length,
4700                                         TRAPS) const {
4701   assert(_need_verify, "only called when _need_verify is true");
4702   // Note: 0 <= length < 64K, as it comes from a u2 entry in the CP.
4703   if (!UTF8::is_legal_utf8(buffer, static_cast<size_t>(length), _major_version <= 47)) {
4704     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4705   }
4706 }
4707 
4708 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4709 // In class names, '/' separates unqualified names.  This is verified in this function also.
4710 // Method names also may not contain the characters '<' or '>', unless <init>
4711 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
4712 // method.  Because these names have been checked as special cases before
4713 // calling this method in verify_legal_method_name.

4731         if (type == ClassFileParser::LegalClass) {
4732           if (p == name || p+1 >= name+length ||
4733               *(p+1) == JVM_SIGNATURE_SLASH) {
4734             return false;
4735           }
4736         } else {
4737           return false;   // do not permit '/' unless it's class name
4738         }
4739         break;
4740       case JVM_SIGNATURE_SPECIAL:
4741       case JVM_SIGNATURE_ENDSPECIAL:
4742         // do not permit '<' or '>' in method names
4743         if (type == ClassFileParser::LegalMethod) {
4744           return false;
4745         }
4746     }
4747   }
4748   return true;
4749 }
4750 
4751 bool ClassFileParser::is_class_in_loadable_descriptors_attribute(Symbol *klass) {
4752   if (_loadable_descriptors == nullptr) return false;
4753   for (int i = 0; i < _loadable_descriptors->length(); i++) {
4754         Symbol* class_name = _cp->symbol_at(_loadable_descriptors->at(i));
4755         if (class_name == klass) return true;
4756   }
4757   return false;
4758 }
4759 
4760 // Take pointer to a UTF8 byte string (not NUL-terminated).
4761 // Skip over the longest part of the string that could
4762 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
4763 // Return a pointer to just past the fieldname.
4764 // Return null if no fieldname at all was found, or in the case of slash_ok
4765 // being true, we saw consecutive slashes (meaning we were looking for a
4766 // qualified path but found something that was badly-formed).
4767 static const char* skip_over_field_name(const char* const name,
4768                                         bool slash_ok,
4769                                         unsigned int length) {
4770   const char* p;
4771   jboolean last_is_slash = false;
4772   jboolean not_first_ch = false;
4773 
4774   for (p = name; p != name + length; not_first_ch = true) {
4775     const char* old_p = p;
4776     jchar ch = *p;
4777     if (ch < 128) {
4778       p++;
4779       // quick check for ascii

4841 // be taken as a field signature. Allow "void" if void_ok.
4842 // Return a pointer to just past the signature.
4843 // Return null if no legal signature is found.
4844 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4845                                                        bool void_ok,
4846                                                        unsigned int length,
4847                                                        TRAPS) const {
4848   unsigned int array_dim = 0;
4849   while (length > 0) {
4850     switch (signature[0]) {
4851     case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
4852     case JVM_SIGNATURE_BOOLEAN:
4853     case JVM_SIGNATURE_BYTE:
4854     case JVM_SIGNATURE_CHAR:
4855     case JVM_SIGNATURE_SHORT:
4856     case JVM_SIGNATURE_INT:
4857     case JVM_SIGNATURE_FLOAT:
4858     case JVM_SIGNATURE_LONG:
4859     case JVM_SIGNATURE_DOUBLE:
4860       return signature + 1;
4861     case JVM_SIGNATURE_CLASS:
4862     {
4863       if (_major_version < JAVA_1_5_VERSION) {
4864         // Skip over the class name if one is there
4865         const char* const p = skip_over_field_name(signature + 1, true, --length);
4866 
4867         // The next character better be a semicolon
4868         if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4869           return p + 1;
4870         }
4871       }
4872       else {
4873         // Skip leading 'L' or 'Q' and ignore first appearance of ';'
4874         signature++;
4875         const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4876         // Format check signature
4877         if (c != nullptr) {
4878           int newlen = pointer_delta_as_int(c, (char*) signature);
4879           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4880           if (!legal) {
4881             classfile_parse_error("Class name is empty or contains illegal character "
4882                                   "in descriptor in class file %s",
4883                                   THREAD);
4884             return nullptr;
4885           }
4886           return signature + newlen + 1;
4887         }
4888       }
4889       return nullptr;
4890     }
4891     case JVM_SIGNATURE_ARRAY:
4892       array_dim++;
4893       if (array_dim > 255) {

4909 
4910 // Checks if name is a legal class name.
4911 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4912   if (!_need_verify) { return; }
4913 
4914   assert(name->refcount() > 0, "symbol must be kept alive");
4915   char* bytes = (char*)name->bytes();
4916   unsigned int length = name->utf8_length();
4917   bool legal = false;
4918 
4919   if (length > 0) {
4920     const char* p;
4921     if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4922       p = skip_over_field_signature(bytes, false, length, CHECK);
4923       legal = (p != nullptr) && ((p - bytes) == (int)length);
4924     } else if (_major_version < JAVA_1_5_VERSION) {
4925       if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4926         p = skip_over_field_name(bytes, true, length);
4927         legal = (p != nullptr) && ((p - bytes) == (int)length);
4928       }
4929     } else if ((_major_version >= CONSTANT_CLASS_DESCRIPTORS || _class_name->starts_with("jdk/internal/reflect/"))
4930                    && bytes[length - 1] == ';' ) {
4931       // Support for L...; descriptors
4932       legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
4933     } else {
4934       // 4900761: relax the constraints based on JSR202 spec
4935       // Class names may be drawn from the entire Unicode character set.
4936       // Identifiers between '/' must be unqualified names.
4937       // The utf8 string has been verified when parsing cpool entries.
4938       legal = verify_unqualified_name(bytes, length, LegalClass);
4939     }
4940   }
4941   if (!legal) {
4942     ResourceMark rm(THREAD);
4943     assert(_class_name != nullptr, "invariant");
4944     // Names are all known to be < 64k so we know this formatted message is not excessively large.
4945     Exceptions::fthrow(
4946       THREAD_AND_LOCATION,
4947       vmSymbols::java_lang_ClassFormatError(),
4948       "Illegal class name \"%.*s\" in class file %s", length, bytes,
4949       _class_name->as_C_string()
4950     );
4951     return;
4952   }

4980       THREAD_AND_LOCATION,
4981       vmSymbols::java_lang_ClassFormatError(),
4982       "Illegal field name \"%.*s\" in class %s", length, bytes,
4983       _class_name->as_C_string()
4984     );
4985     return;
4986   }
4987 }
4988 
4989 // Checks if name is a legal method name.
4990 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
4991   if (!_need_verify) { return; }
4992 
4993   assert(name != nullptr, "method name is null");
4994   char* bytes = (char*)name->bytes();
4995   unsigned int length = name->utf8_length();
4996   bool legal = false;
4997 
4998   if (length > 0) {
4999     if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
5000       if (name == vmSymbols::object_initializer_name() ||
5001           name == vmSymbols::class_initializer_name()) {
5002         legal = true;
5003       }
5004     } else if (_major_version < JAVA_1_5_VERSION) {
5005       const char* p;
5006       p = skip_over_field_name(bytes, false, length);
5007       legal = (p != nullptr) && ((p - bytes) == (int)length);
5008     } else {
5009       // 4881221: relax the constraints based on JSR202 spec
5010       legal = verify_unqualified_name(bytes, length, LegalMethod);
5011     }
5012   }
5013 
5014   if (!legal) {
5015     ResourceMark rm(THREAD);
5016     assert(_class_name != nullptr, "invariant");
5017     // Names are all known to be < 64k so we know this formatted message is not excessively large.
5018     Exceptions::fthrow(
5019       THREAD_AND_LOCATION,
5020       vmSymbols::java_lang_ClassFormatError(),
5021       "Illegal method name \"%.*s\" in class %s", length, bytes,
5022       _class_name->as_C_string()
5023     );
5024     return;
5025   }
5026 }
5027 
5028 bool ClassFileParser::legal_field_signature(const Symbol* signature, TRAPS) const {
5029   const char* const bytes = (const char*)signature->bytes();
5030   const unsigned int length = signature->utf8_length();
5031   const char* const p = skip_over_field_signature(bytes, false, length, CHECK_false);
5032 
5033   if (p == nullptr || (p - bytes) != (int)length) {
5034     return false;
5035   }
5036   return true;
5037 }
5038 
5039 // Checks if signature is a legal field signature.
5040 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5041                                                    const Symbol* signature,
5042                                                    TRAPS) const {
5043   if (!_need_verify) { return; }
5044 
5045   const char* const bytes = (const char*)signature->bytes();
5046   const unsigned int length = signature->utf8_length();
5047   const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5048 
5049   if (p == nullptr || (p - bytes) != (int)length) {
5050     throwIllegalSignature("Field", name, signature, CHECK);
5051   }
5052 }
5053 
5054 // Check that the signature is compatible with the method name.  For example,
5055 // check that <init> has a void signature.
5056 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5057                                                        const Symbol* signature,
5058                                                        TRAPS) const {
5059   if (!_need_verify) {
5060     return;
5061   }
5062 
5063   // Class initializers cannot have args for class format version >= 51.
5064   if (name == vmSymbols::class_initializer_name() &&
5065       signature != vmSymbols::void_method_signature() &&
5066       _major_version >= JAVA_7_VERSION) {
5067     throwIllegalSignature("Method", name, signature, THREAD);
5068     return;
5069   }
5070 
5071   int sig_length = signature->utf8_length();
5072   if (name->utf8_length() > 0 &&
5073     name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5074     sig_length > 0 &&
5075     signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5076     throwIllegalSignature("Method", name, signature, THREAD);
5077   }
5078 }
5079 
5080 // Checks if signature is a legal method signature.
5081 // Returns number of parameters
5082 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5083                                                    const Symbol* signature,
5084                                                    TRAPS) const {
5085   if (!_need_verify) {
5086     // make sure caller's args_size will be less than 0 even for non-static
5087     // method so it will be recomputed in compute_size_of_parameters().
5088     return -2;
5089   }
5090 
5091   unsigned int args_size = 0;
5092   const char* p = (const char*)signature->bytes();
5093   unsigned int length = signature->utf8_length();
5094   const char* nextp;
5095 

5106       length -= pointer_delta_as_int(nextp, p);
5107       p = nextp;
5108       nextp = skip_over_field_signature(p, false, length, CHECK_0);
5109     }
5110     // The first non-signature thing better be a ')'
5111     if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
5112       length--;
5113       // Now we better just have a return value
5114       nextp = skip_over_field_signature(p, true, length, CHECK_0);
5115       if (nextp && ((int)length == (nextp - p))) {
5116         return args_size;
5117       }
5118     }
5119   }
5120   // Report error
5121   throwIllegalSignature("Method", name, signature, THREAD);
5122   return 0;
5123 }
5124 
5125 int ClassFileParser::static_field_size() const {
5126   assert(_layout_info != nullptr, "invariant");
5127   return _layout_info->_static_field_size;
5128 }
5129 
5130 int ClassFileParser::total_oop_map_count() const {
5131   assert(_layout_info != nullptr, "invariant");
5132   return _layout_info->oop_map_blocks->_nonstatic_oop_map_count;
5133 }
5134 
5135 jint ClassFileParser::layout_size() const {
5136   assert(_layout_info != nullptr, "invariant");
5137   return _layout_info->_instance_size;
5138 }
5139 
5140 static void check_methods_for_intrinsics(const InstanceKlass* ik,
5141                                          const Array<Method*>* methods) {
5142   assert(ik != nullptr, "invariant");
5143   assert(methods != nullptr, "invariant");
5144 
5145   // Set up Method*::intrinsic_id as soon as we know the names of methods.
5146   // (We used to do this lazily, but now we query it in Rewriter,
5147   // which is eagerly done for every method, so we might as well do it now,
5148   // when everything is fresh in memory.)
5149   const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);
5150 
5151   if (klass_id != vmSymbolID::NO_SID) {
5152     for (int j = 0; j < methods->length(); ++j) {
5153       Method* method = methods->at(j);
5154       method->init_intrinsic_id(klass_id);
5155 
5156       if (CheckIntrinsics) {
5157         // Check if an intrinsic is defined for method 'method',

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

5252   return ik;
5253 }
5254 
5255 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5256                                           bool changed_by_loadhook,
5257                                           const ClassInstanceInfo& cl_inst_info,
5258                                           TRAPS) {
5259   assert(ik != nullptr, "invariant");
5260 
5261   // Set name and CLD before adding to CLD
5262   ik->set_class_loader_data(_loader_data);
5263   ik->set_class_loader_type();
5264   ik->set_name(_class_name);
5265 
5266   // Add all classes to our internal class loader list here,
5267   // including classes in the bootstrap (null) class loader.
5268   const bool publicize = !is_internal();
5269 
5270   _loader_data->add_class(ik, publicize);
5271 
5272   set_klass_to_deallocate(ik);
5273 
5274   assert(_layout_info != nullptr, "invariant");
5275   assert(ik->static_field_size() == _layout_info->_static_field_size, "sanity");
5276   assert(ik->nonstatic_oop_map_count() == _layout_info->oop_map_blocks->_nonstatic_oop_map_count,
5277          "sanity");
5278 
5279   assert(ik->is_instance_klass(), "sanity");
5280   assert(ik->size_helper() == _layout_info->_instance_size, "sanity");
5281 
5282   // Fill in information already parsed
5283   ik->set_should_verify_class(_need_verify);
5284 
5285   // Not yet: supers are done below to support the new subtype-checking fields
5286   ik->set_nonstatic_field_size(_layout_info->_nonstatic_field_size);
5287   ik->set_has_nonstatic_fields(_layout_info->_has_nonstatic_fields);
5288   ik->set_has_strict_static_fields(_has_strict_static_fields);
5289 
5290   if (_layout_info->_is_naturally_atomic) {
5291     ik->set_is_naturally_atomic();
5292   }
5293 
5294   if (_layout_info->_must_be_atomic) {
5295     ik->set_must_be_atomic();
5296   }
5297 
5298   ik->set_static_oop_field_count(_static_oop_count);
5299 
5300   // this transfers ownership of a lot of arrays from
5301   // the parser onto the InstanceKlass*
5302   apply_parsed_class_metadata(ik, _java_fields_count);
5303   if (ik->is_inline_klass()) {
5304     InlineKlass::cast(ik)->init_fixed_block();
5305   }
5306 
5307   // can only set dynamic nest-host after static nest information is set
5308   if (cl_inst_info.dynamic_nest_host() != nullptr) {
5309     ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5310   }
5311 
5312   // note that is not safe to use the fields in the parser from this point on
5313   assert(nullptr == _cp, "invariant");
5314   assert(nullptr == _fieldinfo_stream, "invariant");
5315   assert(nullptr == _fieldinfo_search_table, "invariant");
5316   assert(nullptr == _fields_status, "invariant");
5317   assert(nullptr == _methods, "invariant");
5318   assert(nullptr == _inner_classes, "invariant");
5319   assert(nullptr == _nest_members, "invariant");
5320   assert(nullptr == _loadable_descriptors, "invariant");
5321   assert(nullptr == _combined_annotations, "invariant");
5322   assert(nullptr == _record_components, "invariant");
5323   assert(nullptr == _permitted_subclasses, "invariant");
5324   assert(nullptr == _inline_layout_info_array, "invariant");
5325 
5326   if (_has_localvariable_table) {
5327     ik->set_has_localvariable_table(true);
5328   }
5329 
5330   if (_has_final_method) {
5331     ik->set_has_final_method();
5332   }
5333 
5334   ik->copy_method_ordering(_method_ordering, CHECK);
5335   // The InstanceKlass::_methods_jmethod_ids cache
5336   // is managed on the assumption that the initial cache
5337   // size is equal to the number of methods in the class. If
5338   // that changes, then InstanceKlass::idnum_can_increment()
5339   // has to be changed accordingly.
5340   ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5341 
5342   ik->set_this_class_index(_this_class_index);
5343 
5344   if (_is_hidden) {

5382   if ((_num_miranda_methods > 0) ||
5383       // if this class introduced new miranda methods or
5384       (_super_klass != nullptr && _super_klass->has_miranda_methods())
5385         // super class exists and this class inherited miranda methods
5386      ) {
5387        ik->set_has_miranda_methods(); // then set a flag
5388   }
5389 
5390   // Fill in information needed to compute superclasses.
5391   ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5392   ik->set_transitive_interfaces(_transitive_interfaces);
5393   ik->set_local_interfaces(_local_interfaces);
5394   _transitive_interfaces = nullptr;
5395   _local_interfaces = nullptr;
5396 
5397   // Initialize itable offset tables
5398   klassItable::setup_itable_offset_table(ik);
5399 
5400   // Compute transitive closure of interfaces this class implements
5401   // Do final class setup
5402   OopMapBlocksBuilder* oop_map_blocks = _layout_info->oop_map_blocks;
5403   if (oop_map_blocks->_nonstatic_oop_map_count > 0) {
5404     oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5405   }
5406 
5407   if (_has_contended_fields || _parsed_annotations->is_contended() ||
5408       ( _super_klass != nullptr && _super_klass->has_contended_annotations())) {
5409     ik->set_has_contended_annotations(true);
5410   }
5411 
5412   // Fill in has_finalizer and layout_helper
5413   set_precomputed_flags(ik);
5414 
5415   // check if this class can access its super class
5416   check_super_class_access(ik, CHECK);
5417 
5418   // check if this class can access its superinterfaces
5419   check_super_interface_access(ik, CHECK);
5420 
5421   // check if this class overrides any final method
5422   check_final_method_override(ik, CHECK);

5443 
5444   assert(_all_mirandas != nullptr, "invariant");
5445 
5446   // Generate any default methods - default methods are public interface methods
5447   // that have a default implementation.  This is new with Java 8.
5448   if (_has_nonstatic_concrete_methods) {
5449     DefaultMethods::generate_default_methods(ik,
5450                                              _all_mirandas,
5451                                              CHECK);
5452   }
5453 
5454   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5455   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5456       !module_entry->has_default_read_edges()) {
5457     if (!module_entry->set_has_default_read_edges()) {
5458       // We won a potential race
5459       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5460     }
5461   }
5462 
5463   if (is_inline_type()) {
5464     InlineKlass* vk = InlineKlass::cast(ik);
5465     vk->set_payload_alignment(_layout_info->_payload_alignment);
5466     vk->set_payload_offset(_layout_info->_payload_offset);
5467     vk->set_payload_size_in_bytes(_layout_info->_payload_size_in_bytes);
5468     vk->set_non_atomic_size_in_bytes(_layout_info->_non_atomic_size_in_bytes);
5469     vk->set_non_atomic_alignment(_layout_info->_non_atomic_alignment);
5470     vk->set_atomic_size_in_bytes(_layout_info->_atomic_layout_size_in_bytes);
5471     vk->set_nullable_size_in_bytes(_layout_info->_nullable_layout_size_in_bytes);
5472     vk->set_null_marker_offset(_layout_info->_null_marker_offset);
5473     vk->set_null_reset_value_offset(_layout_info->_null_reset_value_offset);
5474     if (_layout_info->_is_empty_inline_klass) vk->set_is_empty_inline_type();
5475     vk->initialize_calling_convention(CHECK);
5476   }
5477 
5478   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5479 
5480   if (!is_internal()) {
5481     ik->print_class_load_logging(_loader_data, module_entry, _stream);
5482 
5483     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5484         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5485         log_is_enabled(Info, class, preview)) {
5486       ResourceMark rm;
5487       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5488                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5489     }
5490 
5491     if (log_is_enabled(Debug, class, resolve))  {
5492       ResourceMark rm;
5493       // print out the superclass.
5494       const char * from = ik->external_name();
5495       if (ik->java_super() != nullptr) {
5496         log_debug(class, resolve)("%s %s (super)",
5497                    from,

5540                                  const ClassLoadInfo* cl_info,
5541                                  Publicity pub_level,
5542                                  TRAPS) :
5543   _stream(stream),
5544   _class_name(nullptr),
5545   _loader_data(loader_data),
5546   _is_hidden(cl_info->is_hidden()),
5547   _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5548   _orig_cp_size(0),
5549   _static_oop_count(0),
5550   _super_klass(),
5551   _cp(nullptr),
5552   _fieldinfo_stream(nullptr),
5553   _fieldinfo_search_table(nullptr),
5554   _fields_status(nullptr),
5555   _methods(nullptr),
5556   _inner_classes(nullptr),
5557   _nest_members(nullptr),
5558   _nest_host(0),
5559   _permitted_subclasses(nullptr),
5560   _loadable_descriptors(nullptr),
5561   _record_components(nullptr),
5562   _local_interfaces(nullptr),
5563   _local_interface_indexes(nullptr),
5564   _transitive_interfaces(nullptr),
5565   _combined_annotations(nullptr),
5566   _class_annotations(nullptr),
5567   _class_type_annotations(nullptr),
5568   _fields_annotations(nullptr),
5569   _fields_type_annotations(nullptr),
5570   _klass(nullptr),
5571   _klass_to_deallocate(nullptr),
5572   _parsed_annotations(nullptr),
5573   _layout_info(nullptr),
5574   _inline_layout_info_array(nullptr),
5575   _temp_field_info(nullptr),
5576   _method_ordering(nullptr),
5577   _all_mirandas(nullptr),
5578   _vtable_size(0),
5579   _itable_size(0),
5580   _num_miranda_methods(0),
5581   _protection_domain(cl_info->protection_domain()),
5582   _access_flags(),
5583   _pub_level(pub_level),
5584   _bad_constant_seen(0),
5585   _synthetic_flag(false),
5586   _sde_length(false),
5587   _sde_buffer(nullptr),
5588   _sourcefile_index(0),
5589   _generic_signature_index(0),
5590   _major_version(0),
5591   _minor_version(0),
5592   _this_class_index(0),
5593   _super_class_index(0),
5594   _itfs_len(0),
5595   _java_fields_count(0),
5596   _need_verify(false),
5597   _has_nonstatic_concrete_methods(false),
5598   _declares_nonstatic_concrete_methods(false),
5599   _has_localvariable_table(false),
5600   _has_final_method(false),
5601   _has_contended_fields(false),
5602   _has_strict_static_fields(false),
5603   _has_inline_type_fields(false),
5604   _is_naturally_atomic(false),
5605   _must_be_atomic(true),
5606   _has_loosely_consistent_annotation(false),
5607   _has_finalizer(false),
5608   _has_empty_finalizer(false),
5609   _max_bootstrap_specifier_index(-1) {
5610 
5611   _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5612   _class_name->increment_refcount();
5613 
5614   assert(_loader_data != nullptr, "invariant");
5615   assert(stream != nullptr, "invariant");
5616   assert(_stream != nullptr, "invariant");
5617   assert(_stream->buffer() == _stream->current(), "invariant");
5618   assert(_class_name != nullptr, "invariant");
5619   assert(0 == _access_flags.as_unsigned_short(), "invariant");
5620 
5621   // Figure out whether we can skip format checking (matching classic VM behavior)
5622   // Always verify CFLH bytes from the user agents.
5623   _need_verify = stream->from_class_file_load_hook() ? true : Verifier::should_verify_for(_loader_data->class_loader());
5624 
5625   // synch back verification state to stream to check for truncation.
5626   stream->set_need_verify(_need_verify);
5627 
5628   parse_stream(stream, CHECK);
5629 
5630   post_process_parsed_stream(stream, _cp, CHECK);
5631 }
5632 
5633 void ClassFileParser::clear_class_metadata() {
5634   // metadata created before the instance klass is created.  Must be
5635   // deallocated if classfile parsing returns an error.
5636   _cp = nullptr;
5637   _fieldinfo_stream = nullptr;
5638   _fieldinfo_search_table = nullptr;
5639   _fields_status = nullptr;
5640   _methods = nullptr;
5641   _inner_classes = nullptr;
5642   _nest_members = nullptr;
5643   _permitted_subclasses = nullptr;
5644   _loadable_descriptors = nullptr;
5645   _combined_annotations = nullptr;
5646   _class_annotations = _class_type_annotations = nullptr;
5647   _fields_annotations = _fields_type_annotations = nullptr;
5648   _record_components = nullptr;
5649   _inline_layout_info_array = nullptr;
5650 }
5651 
5652 // Destructor to clean up
5653 ClassFileParser::~ClassFileParser() {
5654   _class_name->decrement_refcount();
5655 
5656   if (_cp != nullptr) {
5657     MetadataFactory::free_metadata(_loader_data, _cp);
5658   }
5659 
5660   if (_fieldinfo_stream != nullptr) {
5661     MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5662   }
5663   MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_search_table);
5664 
5665   if (_fields_status != nullptr) {
5666     MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5667   }
5668 
5669   if (_inline_layout_info_array != nullptr) {
5670     MetadataFactory::free_array<InlineLayoutInfo>(_loader_data, _inline_layout_info_array);
5671   }
5672 
5673   if (_methods != nullptr) {
5674     // Free methods
5675     InstanceKlass::deallocate_methods(_loader_data, _methods);
5676   }
5677 
5678   // beware of the Universe::empty_blah_array!!
5679   if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5680     MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5681   }
5682 
5683   if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5684     MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5685   }
5686 
5687   if (_record_components != nullptr) {
5688     InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5689   }
5690 
5691   if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5692     MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5693   }
5694 
5695   if (_loadable_descriptors != nullptr && _loadable_descriptors != Universe::the_empty_short_array()) {
5696     MetadataFactory::free_array<u2>(_loader_data, _loadable_descriptors);
5697   }
5698 
5699   // Free interfaces
5700   InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5701                                        _local_interfaces, _transitive_interfaces);
5702 
5703   if (_combined_annotations != nullptr) {
5704     // After all annotations arrays have been created, they are installed into the
5705     // Annotations object that will be assigned to the InstanceKlass being created.
5706 
5707     // Deallocate the Annotations object and the installed annotations arrays.
5708     _combined_annotations->deallocate_contents(_loader_data);
5709 
5710     // If the _combined_annotations pointer is non-null,
5711     // then the other annotations fields should have been cleared.
5712     assert(_class_annotations       == nullptr, "Should have been cleared");
5713     assert(_class_type_annotations  == nullptr, "Should have been cleared");
5714     assert(_fields_annotations      == nullptr, "Should have been cleared");
5715     assert(_fields_type_annotations == nullptr, "Should have been cleared");
5716   } else {
5717     // If the annotations arrays were not installed into the Annotations object,
5718     // then they have to be deallocated explicitly.

5777 
5778   assert(cp_size == (u2)cp->length(), "invariant");
5779 
5780   // ACCESS FLAGS
5781   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
5782 
5783   // Access flags
5784   u2 flags;
5785   // JVM_ACC_MODULE is defined in JDK-9 and later.
5786   if (_major_version >= JAVA_9_VERSION) {
5787     flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5788   } else {
5789     flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5790   }
5791 
5792   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5793     // Set abstract bit for old class files for backward compatibility
5794     flags |= JVM_ACC_ABSTRACT;
5795   }
5796 
5797   // Fixing ACC_SUPER/ACC_IDENTITY for old class files
5798   if (!supports_inline_types()) {
5799     const bool is_module = (flags & JVM_ACC_MODULE) != 0;
5800     const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
5801     if (!is_module && !is_interface) {
5802       flags |= JVM_ACC_IDENTITY;
5803     }
5804   }
5805 
5806   verify_legal_class_modifiers(flags, CHECK);
5807 
5808   short bad_constant = class_bad_constant_seen();
5809   if (bad_constant != 0) {
5810     // Do not throw CFE until after the access_flags are checked because if
5811     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5812     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5813     return;
5814   }
5815 
5816   _access_flags.set_flags(flags);
5817 
5818   // This class and superclass
5819   _this_class_index = stream->get_u2_fast();
5820   guarantee_property(
5821     valid_cp_range(_this_class_index, cp_size) &&
5822       cp->tag_at(_this_class_index).is_unresolved_klass(),
5823     "Invalid this class index %u in constant pool in class file %s",
5824     _this_class_index, CHECK);
5825 

5889       }
5890       ls.cr();
5891     }
5892   }
5893 
5894   // SUPERKLASS
5895   _super_class_index = stream->get_u2_fast();
5896   _super_klass = parse_super_class(cp,
5897                                    _super_class_index,
5898                                    _need_verify,
5899                                    CHECK);
5900 
5901   // Interfaces
5902   _itfs_len = stream->get_u2_fast();
5903   parse_interfaces(stream,
5904                    _itfs_len,
5905                    cp,
5906                    &_has_nonstatic_concrete_methods,
5907                    CHECK);
5908 


5909   // Fields (offsets are filled in later)
5910   parse_fields(stream,
5911                _access_flags,
5912                cp,
5913                cp_size,
5914                &_java_fields_count,
5915                CHECK);
5916 
5917   assert(_temp_field_info != nullptr, "invariant");
5918 
5919   // Methods
5920   parse_methods(stream,
5921                 is_interface(),
5922                 !is_identity_class(),
5923                 is_abstract_class(),
5924                 &_has_localvariable_table,
5925                 &_has_final_method,
5926                 &_declares_nonstatic_concrete_methods,
5927                 CHECK);
5928 
5929   assert(_methods != nullptr, "invariant");
5930 
5931   if (_declares_nonstatic_concrete_methods) {
5932     _has_nonstatic_concrete_methods = true;
5933   }
5934 
5935   // Additional attributes/annotations
5936   _parsed_annotations = new ClassAnnotationCollector();
5937   parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
5938 
5939   assert(_inner_classes != nullptr, "invariant");
5940 
5941   // Finalize the Annotations metadata object,
5942   // now that all annotation arrays have been created.
5943   create_combined_annotations(CHECK);

5983   // Update this_class_index's slot in the constant pool with the new Utf8 entry.
5984   // We have to update the resolved_klass_index and the name_index together
5985   // so extract the existing resolved_klass_index first.
5986   CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
5987   int resolved_klass_index = cp_klass_slot.resolved_klass_index();
5988   _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
5989   assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
5990          "Bad name_index");
5991 }
5992 
5993 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
5994                                                  ConstantPool* cp,
5995                                                  TRAPS) {
5996   assert(stream != nullptr, "invariant");
5997   assert(stream->at_eos(), "invariant");
5998   assert(cp != nullptr, "invariant");
5999   assert(_loader_data != nullptr, "invariant");
6000 
6001   if (_class_name == vmSymbols::java_lang_Object()) {
6002     guarantee_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
6003         "java.lang.Object cannot implement an interface in class file %s",
6004         CHECK);
6005   }
6006   // We check super class after class file is parsed and format is checked
6007   if (_super_class_index > 0 && nullptr == _super_klass) {
6008     Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
6009     if (is_interface()) {
6010       // Before attempting to resolve the superclass, check for class format
6011       // errors not checked yet.
6012       guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
6013         "Interfaces must have java.lang.Object as superclass in class file %s",
6014         CHECK);
6015     }
6016     Handle loader(THREAD, _loader_data->class_loader());
6017     if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
6018       _super_klass = vmClasses::Object_klass();
6019     } else {
6020       _super_klass = (const InstanceKlass*)
6021                        SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name,
6022                                                                super_class_name,
6023                                                                loader,
6024                                                                true,
6025                                                                CHECK);
6026     }
6027   }
6028 
6029   if (_super_klass != nullptr) {
6030     if (_super_klass->is_interface()) {
6031       classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
6032       return;
6033     }
6034 
6035     if (_super_klass->is_final()) {
6036       classfile_icce_error("class %s cannot inherit from final class %s", _super_klass, THREAD);
6037       return;
6038     }
6039 
6040     if (EnableValhalla) {
6041       check_identity_and_value_modifiers(this, _super_klass, CHECK);
6042     }
6043 
6044     if (_super_klass->has_nonstatic_concrete_methods()) {
6045       _has_nonstatic_concrete_methods = true;
6046     }
6047   }
6048 
6049   if (_parsed_annotations->has_annotation(AnnotationCollector::_jdk_internal_LooselyConsistentValue) && _access_flags.is_identity_class()) {
6050     THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
6051           err_msg("class %s cannot have annotation jdk.internal.vm.annotation.LooselyConsistentValue, because it is not a value class",
6052                   _class_name->as_klass_external_name()));
6053   }
6054 
6055   // Determining is the class allows tearing or not (default is not)
6056   if (EnableValhalla && !_access_flags.is_identity_class()) {
6057     if (_parsed_annotations->has_annotation(ClassAnnotationCollector::_jdk_internal_LooselyConsistentValue)
6058         && (_super_klass == vmClasses::Object_klass() || !_super_klass->must_be_atomic())) {
6059       // Conditions above are not sufficient to determine atomicity requirements,
6060       // the presence of fields with atomic requirements could force the current class to have atomicy requirements too
6061       // Marking as not needing atomicity for now, can be updated when computing the fields layout
6062       // The InstanceKlass must be filled with the value from the FieldLayoutInfo returned by
6063       // the FieldLayoutBuilder, not with this _must_be_atomic field.
6064       _must_be_atomic = false;
6065     }
6066     // Apply VM options override
6067     if (*ForceNonTearable != '\0') {
6068       // Allow a command line switch to force the same atomicity property:
6069       const char* class_name_str = _class_name->as_C_string();
6070       if (StringUtils::class_list_match(ForceNonTearable, class_name_str)) {
6071         _must_be_atomic = true;
6072       }
6073     }
6074   }
6075 
6076   int itfs_len = _local_interface_indexes == nullptr ? 0 : _local_interface_indexes->length();
6077   _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK);
6078   if (_local_interface_indexes != nullptr) {
6079     for (int i = 0; i < _local_interface_indexes->length(); i++) {
6080       u2 interface_index = _local_interface_indexes->at(i);
6081       Klass* interf;
6082       if (cp->tag_at(interface_index).is_klass()) {
6083         interf = cp->resolved_klass_at(interface_index);
6084       } else {
6085         Symbol* const unresolved_klass  = cp->klass_name_at(interface_index);
6086 
6087         // Don't need to check legal name because it's checked when parsing constant pool.
6088         // But need to make sure it's not an array type.
6089         guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
6090                             "Bad interface name in class file %s", CHECK);
6091 
6092         // Call resolve on the interface class name with class circularity checking
6093         interf = SystemDictionary::resolve_with_circularity_detection_or_fail(
6094                                                   _class_name,
6095                                                   unresolved_klass,
6096                                                   Handle(THREAD, _loader_data->class_loader()),
6097                                                   false,
6098                                                   CHECK);
6099       }
6100 
6101       if (!interf->is_interface()) {
6102         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6103                   err_msg("class %s can not implement %s, because it is not an interface (%s)",
6104                           _class_name->as_klass_external_name(),
6105                           interf->external_name(),
6106                           interf->class_in_module_of_loader()));
6107       }
6108 
6109       if (EnableValhalla) {
6110         // Check modifiers and set carries_identity_modifier/carries_value_modifier flags
6111         check_identity_and_value_modifiers(this, InstanceKlass::cast(interf), CHECK);
6112       }
6113 
6114       if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
6115         _has_nonstatic_concrete_methods = true;
6116       }
6117       _local_interfaces->at_put(i, InstanceKlass::cast(interf));
6118     }
6119   }
6120   assert(_local_interfaces != nullptr, "invariant");
6121 
6122   // Compute the transitive list of all unique interfaces implemented by this class
6123   _transitive_interfaces =
6124     compute_transitive_interfaces(_super_klass,
6125                                   _local_interfaces,
6126                                   _loader_data,
6127                                   CHECK);
6128 
6129   assert(_transitive_interfaces != nullptr, "invariant");
6130 
6131   // sort methods
6132   _method_ordering = sort_methods(_methods);
6133 
6134   _all_mirandas = new GrowableArray<Method*>(20);
6135 
6136   Handle loader(THREAD, _loader_data->class_loader());
6137   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6138                                                     &_num_miranda_methods,
6139                                                     _all_mirandas,
6140                                                     _super_klass,
6141                                                     _methods,
6142                                                     _access_flags,
6143                                                     _major_version,
6144                                                     loader,
6145                                                     _class_name,
6146                                                     _local_interfaces);
6147 
6148   // Size of Java itable (in words)
6149   _itable_size = is_interface() ? 0 :
6150     klassItable::compute_itable_size(_transitive_interfaces);
6151 
6152   assert(_parsed_annotations != nullptr, "invariant");
6153 
6154   if (EnableValhalla) {
6155     _inline_layout_info_array = MetadataFactory::new_array<InlineLayoutInfo>(_loader_data,
6156                                                    java_fields_count(),
6157                                                    CHECK);
6158     for (GrowableArrayIterator<FieldInfo> it = _temp_field_info->begin(); it != _temp_field_info->end(); ++it) {
6159       FieldInfo fieldinfo = *it;
6160       if (fieldinfo.access_flags().is_static()) continue;  // Only non-static fields are processed at load time
6161       Symbol* sig = fieldinfo.signature(cp);
6162       if (fieldinfo.field_flags().is_null_free_inline_type()) {
6163         // Pre-load classes of null-free fields that are candidate for flattening
6164         TempNewSymbol s = Signature::strip_envelope(sig);
6165         if (s == _class_name) {
6166           THROW_MSG(vmSymbols::java_lang_ClassCircularityError(),
6167                     err_msg("Class %s cannot have a null-free non-static field of its own type", _class_name->as_C_string()));
6168         }
6169         log_info(class, preload)("Preloading of class %s during loading of class %s. "
6170                                   "Cause: a null-free non-static field is declared with this type",
6171                                   s->as_C_string(), _class_name->as_C_string());
6172         InstanceKlass* klass = SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name, s,
6173                                                                                             Handle(THREAD,
6174                                                                                             _loader_data->class_loader()),
6175                                                                                             false, THREAD);
6176         if (HAS_PENDING_EXCEPTION) {
6177           log_warning(class, preload)("Preloading of class %s during loading of class %s "
6178                                       "(cause: null-free non-static field) failed: %s",
6179                                       s->as_C_string(), _class_name->as_C_string(),
6180                                       PENDING_EXCEPTION->klass()->name()->as_C_string());
6181           return; // Exception is still pending
6182         }
6183         assert(klass != nullptr, "Sanity check");
6184         InstanceKlass::check_can_be_annotated_with_NullRestricted(klass, _class_name, CHECK);
6185         InlineKlass* vk = InlineKlass::cast(klass);
6186         _inline_layout_info_array->adr_at(fieldinfo.index())->set_klass(vk);
6187         log_info(class, preload)("Preloading of class %s during loading of class %s "
6188                                  "(cause: null-free non-static field) succeeded",
6189                                  s->as_C_string(), _class_name->as_C_string());
6190       } else if (Signature::has_envelope(sig) && PreloadClasses) {
6191         // Preloading classes for nullable fields that are listed in the LoadableDescriptors attribute
6192         // Those classes would be required later for the flattening of nullable inline type fields
6193         TempNewSymbol name = Signature::strip_envelope(sig);
6194         if (name != _class_name && is_class_in_loadable_descriptors_attribute(sig)) {
6195           log_info(class, preload)("Preloading of class %s during loading of class %s. "
6196                                    "Cause: field type in LoadableDescriptors attribute",
6197                                    name->as_C_string(), _class_name->as_C_string());
6198           oop loader = loader_data()->class_loader();
6199           Klass* klass = SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name, name,
6200                                                                                       Handle(THREAD, loader),
6201                                                                                       false, THREAD);
6202           if (klass != nullptr) {
6203             if (klass->is_inline_klass()) {
6204               _inline_layout_info_array->adr_at(fieldinfo.index())->set_klass(InlineKlass::cast(klass));
6205               log_info(class, preload)("Preloading of class %s during loading of class %s "
6206                                        "(cause: field type in LoadableDescriptors attribute) succeeded",
6207                                        name->as_C_string(), _class_name->as_C_string());
6208             } else {
6209               // Non value class are allowed by the current spec, but it could be an indication of an issue so let's log a warning
6210               log_warning(class, preload)("Preloading of class %s during loading of class %s "
6211                                           "(cause: field type in LoadableDescriptors attribute) but loaded class is not a value class",
6212                                           name->as_C_string(), _class_name->as_C_string());
6213             }
6214           } else {
6215             log_warning(class, preload)("Preloading of class %s during loading of class %s "
6216                                         "(cause: field type in LoadableDescriptors attribute) failed : %s",
6217                                         name->as_C_string(), _class_name->as_C_string(),
6218                                         PENDING_EXCEPTION->klass()->name()->as_C_string());
6219           }
6220           // Loads triggered by the LoadableDescriptors attribute are speculative, failures must not impact loading of current class
6221           if (HAS_PENDING_EXCEPTION) {
6222             CLEAR_PENDING_EXCEPTION;
6223           }
6224         } else {
6225           // Just poking the system dictionary to see if the class has already be loaded. Looking for migrated classes
6226           // used when --enable-preview when jdk isn't compiled with --enable-preview so doesn't include LoadableDescriptors.
6227           // This is temporary.
6228           oop loader = loader_data()->class_loader();
6229           InstanceKlass* klass = SystemDictionary::find_instance_klass(THREAD, name, Handle(THREAD, loader));
6230           if (klass != nullptr && klass->is_inline_klass()) {
6231             _inline_layout_info_array->adr_at(fieldinfo.index())->set_klass(InlineKlass::cast(klass));
6232             log_info(class, preload)("Preloading of class %s during loading of class %s "
6233                                      "(cause: field type not in LoadableDescriptors attribute) succeeded",
6234                                      name->as_C_string(), _class_name->as_C_string());
6235           }
6236         }
6237       }
6238     }
6239   }
6240 
6241   _layout_info = new FieldLayoutInfo();
6242   FieldLayoutBuilder lb(class_name(), loader_data(), super_klass(), _cp, /*_fields*/ _temp_field_info,
6243       _parsed_annotations->is_contended(), is_inline_type(),
6244       access_flags().is_abstract() && !access_flags().is_identity_class() && !access_flags().is_interface(),
6245       _must_be_atomic, _layout_info, _inline_layout_info_array);
6246   lb.build_layout();
6247   _has_inline_type_fields = _layout_info->_has_inline_fields;
6248 
6249   int injected_fields_count = _temp_field_info->length() - _java_fields_count;
6250   _fieldinfo_stream =
6251     FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
6252                                             injected_fields_count, loader_data(), CHECK);
6253   _fieldinfo_search_table = FieldInfoStream::create_search_table(_cp, _fieldinfo_stream, _loader_data, CHECK);
6254   _fields_status =
6255     MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
6256                                             FieldStatus(0), CHECK);
6257 
6258   // Strict static fields track initialization status from the beginning of time.
6259   // After this class runs <clinit>, they will be verified as being "not unset".
6260   // See Step 8 of InstanceKlass::initialize_impl.
6261   if (_has_strict_static_fields) {
6262     bool found_one = false;
6263     for (int i = 0; i < _temp_field_info->length(); i++) {
6264       FieldInfo& fi = *_temp_field_info->adr_at(i);
6265       if (fi.access_flags().is_strict() && fi.access_flags().is_static()) {
6266         found_one = true;
6267         if (fi.initializer_index() != 0) {
6268           // skip strict static fields with ConstantValue attributes
6269         } else {
6270           _fields_status->adr_at(fi.index())->update_strict_static_unset(true);
6271           _fields_status->adr_at(fi.index())->update_strict_static_unread(true);
6272         }
6273       }
6274     }
6275     assert(found_one == _has_strict_static_fields,
6276            "correct prediction = %d", (int)_has_strict_static_fields);
6277   }
6278 }
6279 
6280 void ClassFileParser::set_klass(InstanceKlass* klass) {
6281 
6282 #ifdef ASSERT
6283   if (klass != nullptr) {
6284     assert(nullptr == _klass, "leaking?");
6285   }
6286 #endif
6287 
6288   _klass = klass;
6289 }
6290 
6291 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6292 
6293 #ifdef ASSERT
6294   if (klass != nullptr) {
6295     assert(nullptr == _klass_to_deallocate, "leaking?");
6296   }
6297 #endif
< prev index next >