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:
133 #define JAVA_14_VERSION 58
134
135 #define JAVA_15_VERSION 59
136
137 #define JAVA_16_VERSION 60
138
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 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
158 assert((bad_constant == JVM_CONSTANT_Module ||
159 bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
160 "Unexpected bad constant pool entry");
161 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
162 }
163
164 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
165 ConstantPool* cp,
166 const int length,
167 TRAPS) {
168 assert(stream != nullptr, "invariant");
169 assert(cp != nullptr, "invariant");
170
171 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
172 // this function (_current can be allocated in a register, with scalar
175 // this method that uses stream().
176 const ClassFileStream cfs1 = *stream;
177 const ClassFileStream* const cfs = &cfs1;
178
179 debug_only(const u1* const old_current = stream->current();)
180
181 // Used for batching symbol allocations.
182 const char* names[SymbolTable::symbol_alloc_batch_size];
183 int lengths[SymbolTable::symbol_alloc_batch_size];
184 int indices[SymbolTable::symbol_alloc_batch_size];
185 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
186 int names_count = 0;
187
188 // parsing Index 0 is unused
189 for (int index = 1; index < length; index++) {
190 // Each of the following case guarantees one more byte in the stream
191 // for the following tag or the access_flags following constant pool,
192 // so we don't need bounds-check for reading tag.
193 const u1 tag = cfs->get_u1_fast();
194 switch (tag) {
195 case JVM_CONSTANT_Class : {
196 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
197 const u2 name_index = cfs->get_u2_fast();
198 cp->klass_index_at_put(index, name_index);
199 break;
200 }
201 case JVM_CONSTANT_Fieldref: {
202 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
203 const u2 class_index = cfs->get_u2_fast();
204 const u2 name_and_type_index = cfs->get_u2_fast();
205 cp->field_at_put(index, class_index, name_and_type_index);
206 break;
207 }
208 case JVM_CONSTANT_Methodref: {
209 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
210 const u2 class_index = cfs->get_u2_fast();
211 const u2 name_and_type_index = cfs->get_u2_fast();
212 cp->method_at_put(index, class_index, name_and_type_index);
213 break;
214 }
215 case JVM_CONSTANT_InterfaceMethodref: {
479 guarantee_property(valid_symbol_at(name_ref_index),
480 "Invalid constant pool index %u in class file %s",
481 name_ref_index, CHECK);
482 guarantee_property(valid_symbol_at(signature_ref_index),
483 "Invalid constant pool index %u in class file %s",
484 signature_ref_index, CHECK);
485 break;
486 }
487 case JVM_CONSTANT_Utf8:
488 break;
489 case JVM_CONSTANT_UnresolvedClass: // fall-through
490 case JVM_CONSTANT_UnresolvedClassInError: {
491 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
492 break;
493 }
494 case JVM_CONSTANT_ClassIndex: {
495 const int class_index = cp->klass_index_at(index);
496 guarantee_property(valid_symbol_at(class_index),
497 "Invalid constant pool index %u in class file %s",
498 class_index, CHECK);
499 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
500 break;
501 }
502 case JVM_CONSTANT_StringIndex: {
503 const int string_index = cp->string_index_at(index);
504 guarantee_property(valid_symbol_at(string_index),
505 "Invalid constant pool index %u in class file %s",
506 string_index, CHECK);
507 Symbol* const sym = cp->symbol_at(string_index);
508 cp->unresolved_string_at_put(index, sym);
509 break;
510 }
511 case JVM_CONSTANT_MethodHandle: {
512 const int ref_index = cp->method_handle_index_at(index);
513 guarantee_property(valid_cp_range(ref_index, length),
514 "Invalid constant pool index %u in class file %s",
515 ref_index, CHECK);
516 const constantTag tag = cp->tag_at(ref_index);
517 const int ref_kind = cp->method_handle_ref_kind_at(index);
518
688 }
689 }
690 } else {
691 if (_need_verify) {
692 // Method name and signature are individually verified above, when iterating
693 // NameAndType_info. Need to check here that signature is non-zero length and
694 // the right type.
695 if (!Signature::is_method(signature)) {
696 throwIllegalSignature("Method", name, signature, CHECK);
697 }
698 }
699 // If a class method name begins with '<', it must be "<init>" and have void signature.
700 const unsigned int name_len = name->utf8_length();
701 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
702 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
703 if (name != vmSymbols::object_initializer_name()) {
704 classfile_parse_error(
705 "Bad method name at constant pool index %u in class file %s",
706 name_ref_index, THREAD);
707 return;
708 } else if (!Signature::is_void_method(signature)) { // must have void signature.
709 throwIllegalSignature("Method", name, signature, CHECK);
710 }
711 }
712 }
713 break;
714 }
715 case JVM_CONSTANT_MethodHandle: {
716 const int ref_index = cp->method_handle_index_at(index);
717 const int ref_kind = cp->method_handle_ref_kind_at(index);
718 switch (ref_kind) {
719 case JVM_REF_invokeVirtual:
720 case JVM_REF_invokeStatic:
721 case JVM_REF_invokeSpecial:
722 case JVM_REF_newInvokeSpecial: {
723 const int name_and_type_ref_index =
724 cp->uncached_name_and_type_ref_index_at(ref_index);
725 const int name_ref_index =
726 cp->name_ref_index_at(name_and_type_ref_index);
727 const Symbol* const name = cp->symbol_at(name_ref_index);
728 if (ref_kind == JVM_REF_newInvokeSpecial) {
729 if (name != vmSymbols::object_initializer_name()) {
730 classfile_parse_error(
731 "Bad constructor name at constant pool index %u in class file %s",
732 name_ref_index, THREAD);
733 return;
734 }
735 } else {
736 if (name == vmSymbols::object_initializer_name()) {
737 classfile_parse_error(
738 "Bad method name at constant pool index %u in class file %s",
739 name_ref_index, THREAD);
740 return;
741 }
742 }
743 break;
744 }
745 // Other ref_kinds are already fully checked in previous pass.
746 } // switch(ref_kind)
747 break;
748 }
749 case JVM_CONSTANT_MethodType: {
750 const Symbol* const no_name = vmSymbols::type_name(); // place holder
751 const Symbol* const signature = cp->method_type_signature_at(index);
752 verify_legal_method_signature(no_name, signature, CHECK);
753 break;
754 }
755 case JVM_CONSTANT_Utf8: {
756 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
768
769 NameSigHash(Symbol* name, Symbol* sig) :
770 _name(name),
771 _sig(sig) {}
772
773 static unsigned int hash(NameSigHash const& namesig) {
774 return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
775 }
776
777 static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
778 return (e0._name == e1._name) &&
779 (e0._sig == e1._sig);
780 }
781 };
782
783 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
784 NameSigHash::HASH_ROW_SIZE,
785 AnyObj::RESOURCE_AREA, mtInternal,
786 &NameSigHash::hash, &NameSigHash::equals>;
787
788 // Side-effects: populates the _local_interfaces field
789 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
790 const int itfs_len,
791 ConstantPool* const cp,
792 bool* const has_nonstatic_concrete_methods,
793 TRAPS) {
794 assert(stream != nullptr, "invariant");
795 assert(cp != nullptr, "invariant");
796 assert(has_nonstatic_concrete_methods != nullptr, "invariant");
797
798 if (itfs_len == 0) {
799 _local_interfaces = Universe::the_empty_instance_klass_array();
800 } else {
801 assert(itfs_len > 0, "only called for len>0");
802 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK);
803
804 int index;
805 for (index = 0; index < itfs_len; index++) {
806 const u2 interface_index = stream->get_u2(CHECK);
807 Klass* interf;
808 guarantee_property(
809 valid_klass_reference_at(interface_index),
810 "Interface name has bad constant pool index %u in class file %s",
811 interface_index, CHECK);
812 if (cp->tag_at(interface_index).is_klass()) {
813 interf = cp->resolved_klass_at(interface_index);
814 } else {
815 Symbol* const unresolved_klass = cp->klass_name_at(interface_index);
816
817 // Don't need to check legal name because it's checked when parsing constant pool.
818 // But need to make sure it's not an array type.
819 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
820 "Bad interface name in class file %s", CHECK);
821
822 // Call resolve on the interface class name with class circularity checking
823 interf = SystemDictionary::resolve_super_or_fail(_class_name,
824 unresolved_klass,
825 Handle(THREAD, _loader_data->class_loader()),
826 false, CHECK);
827 }
828
829 if (!interf->is_interface()) {
830 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
831 err_msg("class %s can not implement %s, because it is not an interface (%s)",
832 _class_name->as_klass_external_name(),
833 interf->external_name(),
834 interf->class_in_module_of_loader()));
835 }
836
837 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
838 *has_nonstatic_concrete_methods = true;
839 }
840 _local_interfaces->at_put(index, InstanceKlass::cast(interf));
841 }
842
843 if (!_need_verify || itfs_len <= 1) {
844 return;
845 }
846
847 // Check if there's any duplicates in interfaces
848 ResourceMark rm(THREAD);
849 // Set containing interface names
850 ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
851 for (index = 0; index < itfs_len; index++) {
852 const InstanceKlass* const k = _local_interfaces->at(index);
853 Symbol* interface_name = k->name();
854 // If no duplicates, add (name, nullptr) in hashtable interface_names.
855 if (!interface_names->put(interface_name, 0)) {
856 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
857 interface_name->as_C_string(), THREAD);
858 return;
859 }
860 }
861 }
862 }
863
864 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
865 int constantvalue_index,
866 int signature_index,
867 TRAPS) const {
868 // Make sure the constant pool entry is of a type appropriate to this field
869 guarantee_property(
870 (constantvalue_index > 0 &&
871 constantvalue_index < cp->length()),
872 "Bad initial value index %u in ConstantValue attribute in class file %s",
873 constantvalue_index, CHECK);
920 class AnnotationCollector : public ResourceObj{
921 public:
922 enum Location { _in_field, _in_method, _in_class };
923 enum ID {
924 _unknown = 0,
925 _method_CallerSensitive,
926 _method_ForceInline,
927 _method_DontInline,
928 _method_ChangesCurrentThread,
929 _method_JvmtiHideEvents,
930 _method_JvmtiMountTransition,
931 _method_InjectedProfile,
932 _method_LambdaForm_Compiled,
933 _method_Hidden,
934 _method_Scoped,
935 _method_IntrinsicCandidate,
936 _jdk_internal_vm_annotation_Contended,
937 _field_Stable,
938 _jdk_internal_vm_annotation_ReservedStackAccess,
939 _jdk_internal_ValueBased,
940 _java_lang_Deprecated,
941 _java_lang_Deprecated_for_removal,
942 _annotation_LIMIT
943 };
944 const Location _location;
945 int _annotations_present;
946 u2 _contended_group;
947
948 AnnotationCollector(Location location)
949 : _location(location), _annotations_present(0), _contended_group(0)
950 {
951 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
952 }
953 // If this annotation name has an ID, report it (or _none).
954 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
955 // Set the annotation name:
956 void set_annotation(ID id) {
957 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
958 _annotations_present |= (int)nth_bit((int)id);
959 }
1342 }
1343
1344 *constantvalue_index_addr = constantvalue_index;
1345 *is_synthetic_addr = is_synthetic;
1346 *generic_signature_index_addr = generic_signature_index;
1347 AnnotationArray* a = allocate_annotations(runtime_visible_annotations,
1348 runtime_visible_annotations_length,
1349 CHECK);
1350 parsed_annotations->set_field_annotations(a);
1351 a = allocate_annotations(runtime_visible_type_annotations,
1352 runtime_visible_type_annotations_length,
1353 CHECK);
1354 parsed_annotations->set_field_type_annotations(a);
1355 return;
1356 }
1357
1358
1359 // Side-effects: populates the _fields, _fields_annotations,
1360 // _fields_type_annotations fields
1361 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1362 bool is_interface,
1363 ConstantPool* cp,
1364 const int cp_size,
1365 u2* const java_fields_count_ptr,
1366 TRAPS) {
1367
1368 assert(cfs != nullptr, "invariant");
1369 assert(cp != nullptr, "invariant");
1370 assert(java_fields_count_ptr != nullptr, "invariant");
1371
1372 assert(nullptr == _fields_annotations, "invariant");
1373 assert(nullptr == _fields_type_annotations, "invariant");
1374
1375 cfs->guarantee_more(2, CHECK); // length
1376 const u2 length = cfs->get_u2_fast();
1377 *java_fields_count_ptr = length;
1378
1379 int num_injected = 0;
1380 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1381 &num_injected);
1382 const int total_fields = length + num_injected;
1383
1384 // Allocate a temporary resource array to collect field data.
1385 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1386 _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1387
1388 ResourceMark rm(THREAD);
1389 for (int n = 0; n < length; n++) {
1390 // access_flags, name_index, descriptor_index, attributes_count
1391 cfs->guarantee_more(8, CHECK);
1392
1393 AccessFlags access_flags;
1394 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1395 verify_legal_field_modifiers(flags, is_interface, CHECK);
1396 access_flags.set_flags(flags);
1397 FieldInfo::FieldFlags fieldFlags(0);
1398
1399 const u2 name_index = cfs->get_u2_fast();
1400 guarantee_property(valid_symbol_at(name_index),
1401 "Invalid constant pool index %u for field name in class file %s",
1402 name_index, CHECK);
1403 const Symbol* const name = cp->symbol_at(name_index);
1404 verify_legal_field_name(name, CHECK);
1405
1406 const u2 signature_index = cfs->get_u2_fast();
1407 guarantee_property(valid_symbol_at(signature_index),
1408 "Invalid constant pool index %u for field signature in class file %s",
1409 signature_index, CHECK);
1410 const Symbol* const sig = cp->symbol_at(signature_index);
1411 verify_legal_field_signature(name, sig, CHECK);
1412
1413 u2 constantvalue_index = 0;
1414 bool is_synthetic = false;
1415 u2 generic_signature_index = 0;
1416 const bool is_static = access_flags.is_static();
1417 FieldAnnotationCollector parsed_annotations(_loader_data);
1418
1419 const u2 attributes_count = cfs->get_u2_fast();
1420 if (attributes_count > 0) {
1421 parse_field_attributes(cfs,
1422 attributes_count,
1423 is_static,
1424 signature_index,
1425 &constantvalue_index,
1426 &is_synthetic,
1427 &generic_signature_index,
1428 &parsed_annotations,
1429 CHECK);
1430
1431 if (parsed_annotations.field_annotations() != nullptr) {
1432 if (_fields_annotations == nullptr) {
1433 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1434 _loader_data, length, nullptr,
1435 CHECK);
1436 }
1437 _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1438 parsed_annotations.set_field_annotations(nullptr);
1439 }
1440 if (parsed_annotations.field_type_annotations() != nullptr) {
1441 if (_fields_type_annotations == nullptr) {
1442 _fields_type_annotations =
1443 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1444 length,
1445 nullptr,
1446 CHECK);
1447 }
1448 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1449 parsed_annotations.set_field_type_annotations(nullptr);
1450 }
1451
1452 if (is_synthetic) {
1453 access_flags.set_is_synthetic();
1454 }
1455 if (generic_signature_index != 0) {
1456 fieldFlags.update_generic(true);
1457 }
1458 }
1459
1460 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1461
1462 // Update number of static oop fields.
1463 if (is_static && is_reference_type(type)) {
1464 _static_oop_count++;
1465 }
1466
1467 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1468 fi.set_index(n);
1469 if (fieldFlags.is_generic()) {
1470 fi.set_generic_signature_index(generic_signature_index);
1471 }
1472 parsed_annotations.apply_to(&fi);
1473 if (fi.field_flags().is_contended()) {
1474 _has_contended_fields = true;
1475 }
1476 _temp_field_info->append(fi);
1477 }
1478 assert(_temp_field_info->length() == length, "Must be");
1479
1480 int index = length;
1481 if (num_injected != 0) {
1482 for (int n = 0; n < num_injected; n++) {
1483 // Check for duplicates
1484 if (injected[n].may_be_java) {
1485 const Symbol* const name = injected[n].name();
1486 const Symbol* const signature = injected[n].signature();
1487 bool duplicate = false;
1488 for (int i = 0; i < length; i++) {
1489 const FieldInfo* const f = _temp_field_info->adr_at(i);
1490 if (name == cp->symbol_at(f->name_index()) &&
1491 signature == cp->symbol_at(f->signature_index())) {
1492 // Symbol is desclared in Java so skip this one
1493 duplicate = true;
1494 break;
1495 }
1496 }
1497 if (duplicate) {
1498 // These will be removed from the field array at the end
1499 continue;
1500 }
1501 }
1502
1503 // Injected field
1504 FieldInfo::FieldFlags fflags(0);
1505 fflags.update_injected(true);
1506 AccessFlags aflags;
1507 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1508 fi.set_index(index);
1509 _temp_field_info->append(fi);
1510 index++;
1511 }
1512 }
1513
1514 assert(_temp_field_info->length() == index, "Must be");
1515
1516 if (_need_verify && length > 1) {
1517 // Check duplicated fields
1518 ResourceMark rm(THREAD);
1519 // Set containing name-signature pairs
1520 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1521 for (int i = 0; i < _temp_field_info->length(); i++) {
1522 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1523 _temp_field_info->adr_at(i)->signature(_cp));
1524 // If no duplicates, add name/signature in hashtable names_and_sigs.
1525 if(!names_and_sigs->put(name_and_sig, 0)) {
1526 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1527 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1528 return;
1529 }
1530 }
1531 }
1532 }
1533
1534
1874 }
1875 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
1876 if (_location != _in_field && _location != _in_class) {
1877 break; // only allow for fields and classes
1878 }
1879 if (!EnableContended || (RestrictContended && !privileged)) {
1880 break; // honor privileges
1881 }
1882 return _jdk_internal_vm_annotation_Contended;
1883 }
1884 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
1885 if (_location != _in_method) break; // only allow for methods
1886 if (RestrictReservedStack && !privileged) break; // honor privileges
1887 return _jdk_internal_vm_annotation_ReservedStackAccess;
1888 }
1889 case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
1890 if (_location != _in_class) break; // only allow for classes
1891 if (!privileged) break; // only allow in privileged code
1892 return _jdk_internal_ValueBased;
1893 }
1894 case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
1895 return _java_lang_Deprecated;
1896 }
1897 default: {
1898 break;
1899 }
1900 }
1901 return AnnotationCollector::_unknown;
1902 }
1903
1904 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
1905 if (is_contended())
1906 // Setting the contended group also sets the contended bit in field flags
1907 f->set_contended_group(contended_group());
1908 if (is_stable())
1909 (f->field_flags_addr())->update_stable(true);
1910 }
1911
1912 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {
1913 // If there's an error deallocate metadata for field annotations
2097 }
2098
2099 if (runtime_visible_type_annotations_length > 0) {
2100 a = allocate_annotations(runtime_visible_type_annotations,
2101 runtime_visible_type_annotations_length,
2102 CHECK);
2103 cm->set_type_annotations(a);
2104 }
2105 }
2106
2107
2108 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2109 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2110 // Method* to save footprint, so we only know the size of the resulting Method* when the
2111 // entire method attribute is parsed.
2112 //
2113 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2114
2115 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2116 bool is_interface,
2117 const ConstantPool* cp,
2118 bool* const has_localvariable_table,
2119 TRAPS) {
2120 assert(cfs != nullptr, "invariant");
2121 assert(cp != nullptr, "invariant");
2122 assert(has_localvariable_table != nullptr, "invariant");
2123
2124 ResourceMark rm(THREAD);
2125 // Parse fixed parts:
2126 // access_flags, name_index, descriptor_index, attributes_count
2127 cfs->guarantee_more(8, CHECK_NULL);
2128
2129 u2 flags = cfs->get_u2_fast();
2130 const u2 name_index = cfs->get_u2_fast();
2131 const int cp_size = cp->length();
2132 guarantee_property(
2133 valid_symbol_at(name_index),
2134 "Illegal constant pool index %u for method name in class file %s",
2135 name_index, CHECK_NULL);
2136 const Symbol* const name = cp->symbol_at(name_index);
2138
2139 const u2 signature_index = cfs->get_u2_fast();
2140 guarantee_property(
2141 valid_symbol_at(signature_index),
2142 "Illegal constant pool index %u for method signature in class file %s",
2143 signature_index, CHECK_NULL);
2144 const Symbol* const signature = cp->symbol_at(signature_index);
2145
2146 if (name == vmSymbols::class_initializer_name()) {
2147 // We ignore the other access flags for a valid class initializer.
2148 // (JVM Spec 2nd ed., chapter 4.6)
2149 if (_major_version < 51) { // backward compatibility
2150 flags = JVM_ACC_STATIC;
2151 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2152 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2153 } else {
2154 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2155 return nullptr;
2156 }
2157 } else {
2158 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2159 }
2160
2161 if (name == vmSymbols::object_initializer_name() && is_interface) {
2162 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2163 return nullptr;
2164 }
2165
2166 int args_size = -1; // only used when _need_verify is true
2167 if (_need_verify) {
2168 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2169 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2170 verify_legal_method_signature(name, signature, CHECK_NULL);
2171 if (args_size > MAX_ARGS_SIZE) {
2172 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2173 return nullptr;
2174 }
2175 }
2176
2177 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2178
2179 // Default values for code and exceptions attribute elements
2180 u2 max_stack = 0;
2181 u2 max_locals = 0;
2182 u4 code_length = 0;
2183 const u1* code_start = nullptr;
2184 u2 exception_table_length = 0;
2185 const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements
2673 CHECK_NULL);
2674
2675 if (InstanceKlass::is_finalization_enabled() &&
2676 name == vmSymbols::finalize_method_name() &&
2677 signature == vmSymbols::void_method_signature()) {
2678 if (m->is_empty_method()) {
2679 _has_empty_finalizer = true;
2680 } else {
2681 _has_finalizer = true;
2682 }
2683 }
2684
2685 NOT_PRODUCT(m->verify());
2686 return m;
2687 }
2688
2689
2690 // Side-effects: populates the _methods field in the parser
2691 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2692 bool is_interface,
2693 bool* const has_localvariable_table,
2694 bool* has_final_method,
2695 bool* declares_nonstatic_concrete_methods,
2696 TRAPS) {
2697 assert(cfs != nullptr, "invariant");
2698 assert(has_localvariable_table != nullptr, "invariant");
2699 assert(has_final_method != nullptr, "invariant");
2700 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2701
2702 assert(nullptr == _methods, "invariant");
2703
2704 cfs->guarantee_more(2, CHECK); // length
2705 const u2 length = cfs->get_u2_fast();
2706 if (length == 0) {
2707 _methods = Universe::the_empty_method_array();
2708 } else {
2709 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2710 length,
2711 nullptr,
2712 CHECK);
2713
2714 for (int index = 0; index < length; index++) {
2715 Method* method = parse_method(cfs,
2716 is_interface,
2717 _cp,
2718 has_localvariable_table,
2719 CHECK);
2720
2721 if (method->is_final()) {
2722 *has_final_method = true;
2723 }
2724 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2725 // used for interface initialization, and default method inheritance analysis
2726 if (is_interface && !(*declares_nonstatic_concrete_methods)
2727 && !method->is_abstract() && !method->is_static()) {
2728 *declares_nonstatic_concrete_methods = true;
2729 }
2730 _methods->at_put(index, method);
2731 }
2732
2733 if (_need_verify && length > 1) {
2734 // Check duplicated methods
2735 ResourceMark rm(THREAD);
2736 // Set containing name-signature pairs
2962 valid_klass_reference_at(outer_class_info_index),
2963 "outer_class_info_index %u has bad constant type in class file %s",
2964 outer_class_info_index, CHECK_0);
2965
2966 if (outer_class_info_index != 0) {
2967 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
2968 char* bytes = (char*)outer_class_name->bytes();
2969 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
2970 "Outer class is an array class in class file %s", CHECK_0);
2971 }
2972 // Inner class name
2973 const u2 inner_name_index = cfs->get_u2_fast();
2974 guarantee_property(
2975 inner_name_index == 0 || valid_symbol_at(inner_name_index),
2976 "inner_name_index %u has bad constant type in class file %s",
2977 inner_name_index, CHECK_0);
2978 if (_need_verify) {
2979 guarantee_property(inner_class_info_index != outer_class_info_index,
2980 "Class is both outer and inner class in class file %s", CHECK_0);
2981 }
2982 // Access flags
2983 u2 flags;
2984 // JVM_ACC_MODULE is defined in JDK-9 and later.
2985 if (_major_version >= JAVA_9_VERSION) {
2986 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
2987 } else {
2988 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
2989 }
2990 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2991 // Set abstract bit for old class files for backward compatibility
2992 flags |= JVM_ACC_ABSTRACT;
2993 }
2994 verify_legal_class_modifiers(flags, CHECK_0);
2995 AccessFlags inner_access_flags(flags);
2996
2997 inner_classes->at_put(index++, inner_class_info_index);
2998 inner_classes->at_put(index++, outer_class_info_index);
2999 inner_classes->at_put(index++, inner_name_index);
3000 inner_classes->at_put(index++, inner_access_flags.as_unsigned_short());
3001 }
3002
3003 // Check for circular and duplicate entries.
3004 bool has_circularity = false;
3005 if (_need_verify) {
3006 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3007 if (has_circularity) {
3008 // If circularity check failed then ignore InnerClasses attribute.
3009 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3010 index = 0;
3011 if (parsed_enclosingmethod_attribute) {
3012 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3013 _inner_classes = inner_classes;
3014 } else {
3078 if (length > 0) {
3079 int index = 0;
3080 cfs->guarantee_more(2 * length, CHECK_0);
3081 for (int n = 0; n < length; n++) {
3082 const u2 class_info_index = cfs->get_u2_fast();
3083 guarantee_property(
3084 valid_klass_reference_at(class_info_index),
3085 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3086 class_info_index, CHECK_0);
3087 permitted_subclasses->at_put(index++, class_info_index);
3088 }
3089 assert(index == size, "wrong size");
3090 }
3091
3092 // Restore buffer's current position.
3093 cfs->set_current(current_mark);
3094
3095 return length;
3096 }
3097
3098 // Record {
3099 // u2 attribute_name_index;
3100 // u4 attribute_length;
3101 // u2 components_count;
3102 // component_info components[components_count];
3103 // }
3104 // component_info {
3105 // u2 name_index;
3106 // u2 descriptor_index
3107 // u2 attributes_count;
3108 // attribute_info_attributes[attributes_count];
3109 // }
3110 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3111 const ConstantPool* cp,
3112 const u1* const record_attribute_start,
3113 TRAPS) {
3114 const u1* const current_mark = cfs->current();
3115 int components_count = 0;
3116 unsigned int calculate_attr_size = 0;
3117 if (record_attribute_start != nullptr) {
3343 }
3344 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3345 "Bad length on BootstrapMethods in class file %s",
3346 CHECK);
3347 }
3348
3349 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3350 ConstantPool* cp,
3351 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3352 TRAPS) {
3353 assert(cfs != nullptr, "invariant");
3354 assert(cp != nullptr, "invariant");
3355 assert(parsed_annotations != nullptr, "invariant");
3356
3357 // Set inner classes attribute to default sentinel
3358 _inner_classes = Universe::the_empty_short_array();
3359 // Set nest members attribute to default sentinel
3360 _nest_members = Universe::the_empty_short_array();
3361 // Set _permitted_subclasses attribute to default sentinel
3362 _permitted_subclasses = Universe::the_empty_short_array();
3363 cfs->guarantee_more(2, CHECK); // attributes_count
3364 u2 attributes_count = cfs->get_u2_fast();
3365 bool parsed_sourcefile_attribute = false;
3366 bool parsed_innerclasses_attribute = false;
3367 bool parsed_nest_members_attribute = false;
3368 bool parsed_permitted_subclasses_attribute = false;
3369 bool parsed_nest_host_attribute = false;
3370 bool parsed_record_attribute = false;
3371 bool parsed_enclosingmethod_attribute = false;
3372 bool parsed_bootstrap_methods_attribute = false;
3373 const u1* runtime_visible_annotations = nullptr;
3374 int runtime_visible_annotations_length = 0;
3375 const u1* runtime_visible_type_annotations = nullptr;
3376 int runtime_visible_type_annotations_length = 0;
3377 bool runtime_invisible_type_annotations_exists = false;
3378 bool runtime_invisible_annotations_exists = false;
3379 bool parsed_source_debug_ext_annotations_exist = false;
3380 const u1* inner_classes_attribute_start = nullptr;
3381 u4 inner_classes_attribute_length = 0;
3382 u2 enclosing_method_class_index = 0;
3383 u2 enclosing_method_method_index = 0;
3384 const u1* nest_members_attribute_start = nullptr;
3385 u4 nest_members_attribute_length = 0;
3386 const u1* record_attribute_start = nullptr;
3387 u4 record_attribute_length = 0;
3388 const u1* permitted_subclasses_attribute_start = nullptr;
3389 u4 permitted_subclasses_attribute_length = 0;
3390
3391 // Iterate over attributes
3392 while (attributes_count--) {
3393 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3394 const u2 attribute_name_index = cfs->get_u2_fast();
3395 const u4 attribute_length = cfs->get_u4_fast();
3396 guarantee_property(
3397 valid_symbol_at(attribute_name_index),
3398 "Attribute name has bad constant pool index %u in class file %s",
3399 attribute_name_index, CHECK);
3400 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3401 if (tag == vmSymbols::tag_source_file()) {
3402 // Check for SourceFile tag
3403 if (_need_verify) {
3404 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3405 }
3406 if (parsed_sourcefile_attribute) {
3407 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3408 return;
3409 } else {
3585 return;
3586 }
3587 parsed_record_attribute = true;
3588 record_attribute_start = cfs->current();
3589 record_attribute_length = attribute_length;
3590 } else if (_major_version >= JAVA_17_VERSION) {
3591 if (tag == vmSymbols::tag_permitted_subclasses()) {
3592 if (parsed_permitted_subclasses_attribute) {
3593 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3594 return;
3595 }
3596 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3597 if (_access_flags.is_final()) {
3598 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3599 return;
3600 }
3601 parsed_permitted_subclasses_attribute = true;
3602 permitted_subclasses_attribute_start = cfs->current();
3603 permitted_subclasses_attribute_length = attribute_length;
3604 }
3605 }
3606 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3607 cfs->skip_u1(attribute_length, CHECK);
3608 } else {
3609 // Unknown attribute
3610 cfs->skip_u1(attribute_length, CHECK);
3611 }
3612 } else {
3613 // Unknown attribute
3614 cfs->skip_u1(attribute_length, CHECK);
3615 }
3616 } else {
3617 // Unknown attribute
3618 cfs->skip_u1(attribute_length, CHECK);
3619 }
3620 }
3621 _class_annotations = allocate_annotations(runtime_visible_annotations,
3622 runtime_visible_annotations_length,
3623 CHECK);
3624 _class_type_annotations = allocate_annotations(runtime_visible_type_annotations,
3661 CHECK);
3662 if (_need_verify) {
3663 guarantee_property(record_attribute_length == calculated_attr_length,
3664 "Record attribute has wrong length in class file %s",
3665 CHECK);
3666 }
3667 }
3668
3669 if (parsed_permitted_subclasses_attribute) {
3670 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3671 cfs,
3672 permitted_subclasses_attribute_start,
3673 CHECK);
3674 if (_need_verify) {
3675 guarantee_property(
3676 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3677 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3678 }
3679 }
3680
3681 if (_max_bootstrap_specifier_index >= 0) {
3682 guarantee_property(parsed_bootstrap_methods_attribute,
3683 "Missing BootstrapMethods attribute in class file %s", CHECK);
3684 }
3685 }
3686
3687 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3688 assert(k != nullptr, "invariant");
3689
3690 if (_synthetic_flag)
3691 k->set_is_synthetic();
3692 if (_sourcefile_index != 0) {
3693 k->set_source_file_name_index(_sourcefile_index);
3694 }
3695 if (_generic_signature_index != 0) {
3696 k->set_generic_signature_index(_generic_signature_index);
3697 }
3698 if (_sde_buffer != nullptr) {
3699 k->set_source_debug_extension(_sde_buffer, _sde_length);
3700 }
3726 _class_annotations = nullptr;
3727 _class_type_annotations = nullptr;
3728 _fields_annotations = nullptr;
3729 _fields_type_annotations = nullptr;
3730 }
3731
3732 // Transfer ownership of metadata allocated to the InstanceKlass.
3733 void ClassFileParser::apply_parsed_class_metadata(
3734 InstanceKlass* this_klass,
3735 int java_fields_count) {
3736 assert(this_klass != nullptr, "invariant");
3737
3738 _cp->set_pool_holder(this_klass);
3739 this_klass->set_constants(_cp);
3740 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3741 this_klass->set_fields_status(_fields_status);
3742 this_klass->set_methods(_methods);
3743 this_klass->set_inner_classes(_inner_classes);
3744 this_klass->set_nest_members(_nest_members);
3745 this_klass->set_nest_host_index(_nest_host);
3746 this_klass->set_annotations(_combined_annotations);
3747 this_klass->set_permitted_subclasses(_permitted_subclasses);
3748 this_klass->set_record_components(_record_components);
3749
3750 // Delay the setting of _local_interfaces and _transitive_interfaces until after
3751 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3752 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3753 // its _super. If an OOM occurs while loading the current klass, its _super field
3754 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3755 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3756 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3757
3758 // Clear out these fields so they don't get deallocated by the destructor
3759 clear_class_metadata();
3760 }
3761
3762 AnnotationArray* ClassFileParser::allocate_annotations(const u1* const anno,
3763 int anno_length,
3764 TRAPS) {
3765 AnnotationArray* annotations = nullptr;
3766 if (anno != nullptr) {
3767 annotations = MetadataFactory::new_array<u1>(_loader_data,
3768 anno_length,
3769 CHECK_(annotations));
3770 for (int i = 0; i < anno_length; i++) {
3771 annotations->at_put(i, anno[i]);
3772 }
3773 }
3774 return annotations;
3775 }
3776
3777 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3778 const int super_class_index,
3779 const bool need_verify,
3780 TRAPS) {
3781 assert(cp != nullptr, "invariant");
3782 const InstanceKlass* super_klass = nullptr;
3783
3784 if (super_class_index == 0) {
3785 guarantee_property(_class_name == vmSymbols::java_lang_Object(),
3786 "Invalid superclass index %u in class file %s",
3787 super_class_index,
3788 CHECK_NULL);
3789 } else {
3790 guarantee_property(valid_klass_reference_at(super_class_index),
3791 "Invalid superclass index %u in class file %s",
3792 super_class_index,
3793 CHECK_NULL);
3794 // The class name should be legal because it is checked when parsing constant pool.
3795 // However, make sure it is not an array type.
3796 bool is_array = false;
3797 if (cp->tag_at(super_class_index).is_klass()) {
3798 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3799 if (need_verify)
3800 is_array = super_klass->is_array_klass();
3801 } else if (need_verify) {
3802 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3803 }
3804 if (need_verify) {
3805 guarantee_property(!is_array,
3806 "Bad superclass name in class file %s", CHECK_NULL);
3807 }
3808 }
3809 return super_klass;
3810 }
3811
3812 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
3813 _max_nonstatic_oop_maps = max_blocks;
3814 _nonstatic_oop_map_count = 0;
3815 if (max_blocks == 0) {
3816 _nonstatic_oop_maps = nullptr;
3817 } else {
3818 _nonstatic_oop_maps =
3819 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
3820 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3821 }
3822 }
3823
3824 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
3958
3959 // Check if this klass supports the java.lang.Cloneable interface
3960 if (vmClasses::Cloneable_klass_loaded()) {
3961 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
3962 ik->set_is_cloneable();
3963 }
3964 }
3965
3966 // If it cannot be fast-path allocated, set a bit in the layout helper.
3967 // See documentation of InstanceKlass::can_be_fastpath_allocated().
3968 assert(ik->size_helper() > 0, "layout_helper is initialized");
3969 if (ik->is_abstract() || ik->is_interface()
3970 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
3971 || ik->size_helper() >= FastAllocateSizeLimit) {
3972 // Forbid fast-path allocation.
3973 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
3974 ik->set_layout_helper(lh);
3975 }
3976 }
3977
3978 // utility methods for appending an array with check for duplicates
3979
3980 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
3981 const Array<InstanceKlass*>* const ifs) {
3982 // iterate over new interfaces
3983 for (int i = 0; i < ifs->length(); i++) {
3984 InstanceKlass* const e = ifs->at(i);
3985 assert(e->is_klass() && e->is_interface(), "just checking");
3986 // add new interface
3987 result->append_if_missing(e);
3988 }
3989 }
3990
3991 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
3992 Array<InstanceKlass*>* local_ifs,
3993 ClassLoaderData* loader_data,
3994 TRAPS) {
3995 assert(local_ifs != nullptr, "invariant");
3996 assert(loader_data != nullptr, "invariant");
3997
4001 // Add superclass transitive interfaces size
4002 if (super != nullptr) {
4003 super_size = super->transitive_interfaces()->length();
4004 max_transitive_size += super_size;
4005 }
4006 // Add local interfaces' super interfaces
4007 const int local_size = local_ifs->length();
4008 for (int i = 0; i < local_size; i++) {
4009 InstanceKlass* const l = local_ifs->at(i);
4010 max_transitive_size += l->transitive_interfaces()->length();
4011 }
4012 // Finally add local interfaces
4013 max_transitive_size += local_size;
4014 // Construct array
4015 if (max_transitive_size == 0) {
4016 // no interfaces, use canonicalized array
4017 return Universe::the_empty_instance_klass_array();
4018 } else if (max_transitive_size == super_size) {
4019 // no new local interfaces added, share superklass' transitive interface array
4020 return super->transitive_interfaces();
4021 } else if (max_transitive_size == local_size) {
4022 // only local interfaces added, share local interface array
4023 return local_ifs;
4024 } else {
4025 ResourceMark rm;
4026 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4027
4028 // Copy down from superclass
4029 if (super != nullptr) {
4030 append_interfaces(result, super->transitive_interfaces());
4031 }
4032
4033 // Copy down from local interfaces' superinterfaces
4034 for (int i = 0; i < local_size; i++) {
4035 InstanceKlass* const l = local_ifs->at(i);
4036 append_interfaces(result, l->transitive_interfaces());
4037 }
4038 // Finally add local interfaces
4039 append_interfaces(result, local_ifs);
4040
4041 // length will be less than the max_transitive_size if duplicates were removed
4042 const int length = result->length();
4043 assert(length <= max_transitive_size, "just checking");
4044 Array<InstanceKlass*>* const new_result =
4045 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4046 for (int i = 0; i < length; i++) {
4047 InstanceKlass* const e = result->at(i);
4048 assert(e != nullptr, "just checking");
4049 new_result->at_put(i, e);
4050 }
4051 return new_result;
4052 }
4053 }
4054
4055 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4056 assert(this_klass != nullptr, "invariant");
4057 const Klass* const super = this_klass->super();
4058
4059 if (super != nullptr) {
4060 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4061
4062 if (super->is_final()) {
4063 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4064 return;
4065 }
4066
4067 if (super_ik->is_sealed()) {
4068 stringStream ss;
4069 ResourceMark rm(THREAD);
4070 if (!super_ik->has_as_permitted_subclass(this_klass, ss)) {
4071 classfile_icce_error(ss.as_string(), THREAD);
4072 return;
4073 }
4074 }
4075
4076 Reflection::VerifyClassAccessResults vca_result =
4077 Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false);
4078 if (vca_result != Reflection::ACCESS_OK) {
4079 ResourceMark rm(THREAD);
4080 char* msg = Reflection::verify_class_access_msg(this_klass,
4081 InstanceKlass::cast(super),
4082 vca_result);
4083
4084 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4085 if (msg == nullptr) {
4086 bool same_module = (this_klass->module() == super->module());
4087 Exceptions::fthrow(
4088 THREAD_AND_LOCATION,
4089 vmSymbols::java_lang_IllegalAccessError(),
4090 "class %s cannot access its %ssuperclass %s (%s%s%s)",
4091 this_klass->external_name(),
4092 super->is_abstract() ? "abstract " : "",
4093 super->external_name(),
4094 (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),
4095 (same_module) ? "" : "; ",
4228 const Method* const m = methods->at(index);
4229 // if m is static and not the init method, throw a verify error
4230 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4231 ResourceMark rm(THREAD);
4232
4233 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4234 Exceptions::fthrow(
4235 THREAD_AND_LOCATION,
4236 vmSymbols::java_lang_VerifyError(),
4237 "Illegal static method %s in interface %s",
4238 m->name()->as_C_string(),
4239 this_klass->external_name()
4240 );
4241 return;
4242 }
4243 }
4244 }
4245
4246 // utility methods for format checking
4247
4248 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4249 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4250 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4251 if (is_module) {
4252 ResourceMark rm(THREAD);
4253 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4254 Exceptions::fthrow(
4255 THREAD_AND_LOCATION,
4256 vmSymbols::java_lang_NoClassDefFoundError(),
4257 "%s is not a class because access_flag ACC_MODULE is set",
4258 _class_name->as_C_string());
4259 return;
4260 }
4261
4262 if (!_need_verify) { return; }
4263
4264 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4265 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4266 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4267 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4268 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4269 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4270 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4271
4272 if ((is_abstract && is_final) ||
4273 (is_interface && !is_abstract) ||
4274 (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4275 (!is_interface && major_gte_1_5 && is_annotation)) {
4276 ResourceMark rm(THREAD);
4277 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4278 Exceptions::fthrow(
4279 THREAD_AND_LOCATION,
4280 vmSymbols::java_lang_ClassFormatError(),
4281 "Illegal class modifiers in class %s: 0x%X",
4282 _class_name->as_C_string(), flags
4283 );
4284 return;
4285 }
4286 }
4287
4288 static bool has_illegal_visibility(jint flags) {
4289 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4290 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4291 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4292
4293 return ((is_public && is_protected) ||
4294 (is_public && is_private) ||
4295 (is_protected && is_private));
4296 }
4297
4298 // A legal major_version.minor_version must be one of the following:
4299 //
4300 // Major_version >= 45 and major_version < 56, any minor_version.
4301 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4302 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4303 //
4304 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4332 THREAD_AND_LOCATION,
4333 vmSymbols::java_lang_UnsupportedClassVersionError(),
4334 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4335 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4336 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4337 return;
4338 }
4339
4340 if (!Arguments::enable_preview()) {
4341 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4342 class_name, major, minor, THREAD);
4343 return;
4344 }
4345
4346 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4347 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4348 class_name, major, minor, THREAD);
4349 }
4350 }
4351
4352 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4353 bool is_interface,
4354 TRAPS) const {
4355 if (!_need_verify) { return; }
4356
4357 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4358 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4359 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4360 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4361 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4362 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4363 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4364 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4365 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4366
4367 bool is_illegal = false;
4368
4369 if (is_interface) {
4370 if (!is_public || !is_static || !is_final || is_private ||
4371 is_protected || is_volatile || is_transient ||
4372 (major_gte_1_5 && is_enum)) {
4373 is_illegal = true;
4374 }
4375 } else { // not interface
4376 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4377 is_illegal = true;
4378 }
4379 }
4380
4381 if (is_illegal) {
4382 ResourceMark rm(THREAD);
4383 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4384 Exceptions::fthrow(
4385 THREAD_AND_LOCATION,
4386 vmSymbols::java_lang_ClassFormatError(),
4387 "Illegal field modifiers in class %s: 0x%X",
4388 _class_name->as_C_string(), flags);
4389 return;
4390 }
4391 }
4392
4393 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4394 bool is_interface,
4395 const Symbol* name,
4396 TRAPS) const {
4397 if (!_need_verify) { return; }
4398
4399 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4400 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4401 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4402 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4403 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4404 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4405 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4406 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4407 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4408 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4409 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4410 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4411 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4412 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4413
4414 bool is_illegal = false;
4415
4416 if (is_interface) {
4417 if (major_gte_8) {
4418 // Class file version is JAVA_8_VERSION or later Methods of
4419 // interfaces may set any of the flags except ACC_PROTECTED,
4420 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4421 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4422 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4423 (is_native || is_protected || is_final || is_synchronized) ||
4424 // If a specific method of a class or interface has its
4425 // ACC_ABSTRACT flag set, it must not have any of its
4426 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4427 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4428 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4429 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4430 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4431 is_illegal = true;
4432 }
4433 } else if (major_gte_1_5) {
4434 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4435 if (!is_public || is_private || is_protected || is_static || is_final ||
4436 is_synchronized || is_native || !is_abstract || is_strict) {
4437 is_illegal = true;
4438 }
4439 } else {
4440 // Class file version is pre-JAVA_1_5_VERSION
4441 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4442 is_illegal = true;
4443 }
4444 }
4445 } else { // not interface
4446 if (has_illegal_visibility(flags)) {
4447 is_illegal = true;
4448 } else {
4449 if (is_initializer) {
4450 if (is_static || is_final || is_synchronized || is_native ||
4451 is_abstract || (major_gte_1_5 && is_bridge)) {
4452 is_illegal = true;
4453 }
4454 } else { // not initializer
4455 if (is_abstract) {
4456 if ((is_final || is_native || is_private || is_static ||
4457 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4458 is_illegal = true;
4459 }
4460 }
4461 }
4462 }
4463 }
4464
4465 if (is_illegal) {
4466 ResourceMark rm(THREAD);
4467 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4468 Exceptions::fthrow(
4469 THREAD_AND_LOCATION,
4470 vmSymbols::java_lang_ClassFormatError(),
4471 "Method %s in class %s has illegal modifiers: 0x%X",
4472 name->as_C_string(), _class_name->as_C_string(), flags);
4473 return;
4474 }
4475 }
4476
4477 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4478 int length,
4479 TRAPS) const {
4480 assert(_need_verify, "only called when _need_verify is true");
4481 // Note: 0 <= length < 64K, as it comes from a u2 entry in the CP.
4482 if (!UTF8::is_legal_utf8(buffer, static_cast<size_t>(length), _major_version <= 47)) {
4483 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4484 }
4485 }
4486
4487 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4488 // In class names, '/' separates unqualified names. This is verified in this function also.
4489 // Method names also may not contain the characters '<' or '>', unless <init>
4490 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4491 // method. Because these names have been checked as special cases before
4492 // calling this method in verify_legal_method_name.
4510 if (type == ClassFileParser::LegalClass) {
4511 if (p == name || p+1 >= name+length ||
4512 *(p+1) == JVM_SIGNATURE_SLASH) {
4513 return false;
4514 }
4515 } else {
4516 return false; // do not permit '/' unless it's class name
4517 }
4518 break;
4519 case JVM_SIGNATURE_SPECIAL:
4520 case JVM_SIGNATURE_ENDSPECIAL:
4521 // do not permit '<' or '>' in method names
4522 if (type == ClassFileParser::LegalMethod) {
4523 return false;
4524 }
4525 }
4526 }
4527 return true;
4528 }
4529
4530 // Take pointer to a UTF8 byte string (not NUL-terminated).
4531 // Skip over the longest part of the string that could
4532 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
4533 // Return a pointer to just past the fieldname.
4534 // Return null if no fieldname at all was found, or in the case of slash_ok
4535 // being true, we saw consecutive slashes (meaning we were looking for a
4536 // qualified path but found something that was badly-formed).
4537 static const char* skip_over_field_name(const char* const name,
4538 bool slash_ok,
4539 unsigned int length) {
4540 const char* p;
4541 jboolean last_is_slash = false;
4542 jboolean not_first_ch = false;
4543
4544 for (p = name; p != name + length; not_first_ch = true) {
4545 const char* old_p = p;
4546 jchar ch = *p;
4547 if (ch < 128) {
4548 p++;
4549 // quick check for ascii
4611 // be taken as a field signature. Allow "void" if void_ok.
4612 // Return a pointer to just past the signature.
4613 // Return null if no legal signature is found.
4614 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4615 bool void_ok,
4616 unsigned int length,
4617 TRAPS) const {
4618 unsigned int array_dim = 0;
4619 while (length > 0) {
4620 switch (signature[0]) {
4621 case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
4622 case JVM_SIGNATURE_BOOLEAN:
4623 case JVM_SIGNATURE_BYTE:
4624 case JVM_SIGNATURE_CHAR:
4625 case JVM_SIGNATURE_SHORT:
4626 case JVM_SIGNATURE_INT:
4627 case JVM_SIGNATURE_FLOAT:
4628 case JVM_SIGNATURE_LONG:
4629 case JVM_SIGNATURE_DOUBLE:
4630 return signature + 1;
4631 case JVM_SIGNATURE_CLASS: {
4632 if (_major_version < JAVA_1_5_VERSION) {
4633 // Skip over the class name if one is there
4634 const char* const p = skip_over_field_name(signature + 1, true, --length);
4635
4636 // The next character better be a semicolon
4637 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4638 return p + 1;
4639 }
4640 }
4641 else {
4642 // Skip leading 'L' and ignore first appearance of ';'
4643 signature++;
4644 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4645 // Format check signature
4646 if (c != nullptr) {
4647 int newlen = pointer_delta_as_int(c, (char*) signature);
4648 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4649 if (!legal) {
4650 classfile_parse_error("Class name is empty or contains illegal character "
4651 "in descriptor in class file %s",
4652 THREAD);
4653 return nullptr;
4654 }
4655 return signature + newlen + 1;
4656 }
4657 }
4658 return nullptr;
4659 }
4660 case JVM_SIGNATURE_ARRAY:
4661 array_dim++;
4662 if (array_dim > 255) {
4678
4679 // Checks if name is a legal class name.
4680 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4681 if (!_need_verify) { return; }
4682
4683 assert(name->refcount() > 0, "symbol must be kept alive");
4684 char* bytes = (char*)name->bytes();
4685 unsigned int length = name->utf8_length();
4686 bool legal = false;
4687
4688 if (length > 0) {
4689 const char* p;
4690 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4691 p = skip_over_field_signature(bytes, false, length, CHECK);
4692 legal = (p != nullptr) && ((p - bytes) == (int)length);
4693 } else if (_major_version < JAVA_1_5_VERSION) {
4694 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4695 p = skip_over_field_name(bytes, true, length);
4696 legal = (p != nullptr) && ((p - bytes) == (int)length);
4697 }
4698 } else {
4699 // 4900761: relax the constraints based on JSR202 spec
4700 // Class names may be drawn from the entire Unicode character set.
4701 // Identifiers between '/' must be unqualified names.
4702 // The utf8 string has been verified when parsing cpool entries.
4703 legal = verify_unqualified_name(bytes, length, LegalClass);
4704 }
4705 }
4706 if (!legal) {
4707 ResourceMark rm(THREAD);
4708 assert(_class_name != nullptr, "invariant");
4709 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4710 Exceptions::fthrow(
4711 THREAD_AND_LOCATION,
4712 vmSymbols::java_lang_ClassFormatError(),
4713 "Illegal class name \"%.*s\" in class file %s", length, bytes,
4714 _class_name->as_C_string()
4715 );
4716 return;
4717 }
4745 THREAD_AND_LOCATION,
4746 vmSymbols::java_lang_ClassFormatError(),
4747 "Illegal field name \"%.*s\" in class %s", length, bytes,
4748 _class_name->as_C_string()
4749 );
4750 return;
4751 }
4752 }
4753
4754 // Checks if name is a legal method name.
4755 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
4756 if (!_need_verify) { return; }
4757
4758 assert(name != nullptr, "method name is null");
4759 char* bytes = (char*)name->bytes();
4760 unsigned int length = name->utf8_length();
4761 bool legal = false;
4762
4763 if (length > 0) {
4764 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
4765 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
4766 legal = true;
4767 }
4768 } else if (_major_version < JAVA_1_5_VERSION) {
4769 const char* p;
4770 p = skip_over_field_name(bytes, false, length);
4771 legal = (p != nullptr) && ((p - bytes) == (int)length);
4772 } else {
4773 // 4881221: relax the constraints based on JSR202 spec
4774 legal = verify_unqualified_name(bytes, length, LegalMethod);
4775 }
4776 }
4777
4778 if (!legal) {
4779 ResourceMark rm(THREAD);
4780 assert(_class_name != nullptr, "invariant");
4781 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4782 Exceptions::fthrow(
4783 THREAD_AND_LOCATION,
4784 vmSymbols::java_lang_ClassFormatError(),
4785 "Illegal method name \"%.*s\" in class %s", length, bytes,
4786 _class_name->as_C_string()
4787 );
4788 return;
4789 }
4790 }
4791
4792
4793 // Checks if signature is a legal field signature.
4794 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
4795 const Symbol* signature,
4796 TRAPS) const {
4797 if (!_need_verify) { return; }
4798
4799 const char* const bytes = (const char*)signature->bytes();
4800 const unsigned int length = signature->utf8_length();
4801 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
4802
4803 if (p == nullptr || (p - bytes) != (int)length) {
4804 throwIllegalSignature("Field", name, signature, CHECK);
4805 }
4806 }
4807
4808 // Check that the signature is compatible with the method name. For example,
4809 // check that <init> has a void signature.
4810 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
4811 const Symbol* signature,
4812 TRAPS) const {
4813 if (!_need_verify) {
4814 return;
4815 }
4816
4817 // Class initializers cannot have args for class format version >= 51.
4818 if (name == vmSymbols::class_initializer_name() &&
4819 signature != vmSymbols::void_method_signature() &&
4820 _major_version >= JAVA_7_VERSION) {
4821 throwIllegalSignature("Method", name, signature, THREAD);
4822 return;
4823 }
4824
4825 int sig_length = signature->utf8_length();
4826 if (name->utf8_length() > 0 &&
4827 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
4828 sig_length > 0 &&
4829 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
4830 throwIllegalSignature("Method", name, signature, THREAD);
4831 }
4832 }
4833
4834 // Checks if signature is a legal method signature.
4835 // Returns number of parameters
4836 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
4837 const Symbol* signature,
4838 TRAPS) const {
4839 if (!_need_verify) {
4840 // make sure caller's args_size will be less than 0 even for non-static
4841 // method so it will be recomputed in compute_size_of_parameters().
4842 return -2;
4843 }
4844
4845 unsigned int args_size = 0;
4846 const char* p = (const char*)signature->bytes();
4847 unsigned int length = signature->utf8_length();
4848 const char* nextp;
4849
4860 length -= pointer_delta_as_int(nextp, p);
4861 p = nextp;
4862 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4863 }
4864 // The first non-signature thing better be a ')'
4865 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
4866 length--;
4867 // Now we better just have a return value
4868 nextp = skip_over_field_signature(p, true, length, CHECK_0);
4869 if (nextp && ((int)length == (nextp - p))) {
4870 return args_size;
4871 }
4872 }
4873 }
4874 // Report error
4875 throwIllegalSignature("Method", name, signature, THREAD);
4876 return 0;
4877 }
4878
4879 int ClassFileParser::static_field_size() const {
4880 assert(_field_info != nullptr, "invariant");
4881 return _field_info->_static_field_size;
4882 }
4883
4884 int ClassFileParser::total_oop_map_count() const {
4885 assert(_field_info != nullptr, "invariant");
4886 return _field_info->oop_map_blocks->_nonstatic_oop_map_count;
4887 }
4888
4889 jint ClassFileParser::layout_size() const {
4890 assert(_field_info != nullptr, "invariant");
4891 return _field_info->_instance_size;
4892 }
4893
4894 static void check_methods_for_intrinsics(const InstanceKlass* ik,
4895 const Array<Method*>* methods) {
4896 assert(ik != nullptr, "invariant");
4897 assert(methods != nullptr, "invariant");
4898
4899 // Set up Method*::intrinsic_id as soon as we know the names of methods.
4900 // (We used to do this lazily, but now we query it in Rewriter,
4901 // which is eagerly done for every method, so we might as well do it now,
4902 // when everything is fresh in memory.)
4903 const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);
4904
4905 if (klass_id != vmSymbolID::NO_SID) {
4906 for (int j = 0; j < methods->length(); ++j) {
4907 Method* method = methods->at(j);
4908 method->init_intrinsic_id(klass_id);
4909
4910 if (CheckIntrinsics) {
4911 // Check if an intrinsic is defined for method 'method',
4986 }
4987 }
4988
4989 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
4990 const ClassInstanceInfo& cl_inst_info,
4991 TRAPS) {
4992 if (_klass != nullptr) {
4993 return _klass;
4994 }
4995
4996 InstanceKlass* const ik =
4997 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
4998
4999 if (is_hidden()) {
5000 mangle_hidden_class_name(ik);
5001 }
5002
5003 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5004
5005 assert(_klass == ik, "invariant");
5006
5007 return ik;
5008 }
5009
5010 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5011 bool changed_by_loadhook,
5012 const ClassInstanceInfo& cl_inst_info,
5013 TRAPS) {
5014 assert(ik != nullptr, "invariant");
5015
5016 // Set name and CLD before adding to CLD
5017 ik->set_class_loader_data(_loader_data);
5018 ik->set_name(_class_name);
5019
5020 // Add all classes to our internal class loader list here,
5021 // including classes in the bootstrap (null) class loader.
5022 const bool publicize = !is_internal();
5023
5024 _loader_data->add_class(ik, publicize);
5025
5026 set_klass_to_deallocate(ik);
5027
5028 assert(_field_info != nullptr, "invariant");
5029 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5030 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5031 "sanity");
5032
5033 assert(ik->is_instance_klass(), "sanity");
5034 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5035
5036 // Fill in information already parsed
5037 ik->set_should_verify_class(_need_verify);
5038
5039 // Not yet: supers are done below to support the new subtype-checking fields
5040 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5041 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5042 ik->set_static_oop_field_count(_static_oop_count);
5043
5044 // this transfers ownership of a lot of arrays from
5045 // the parser onto the InstanceKlass*
5046 apply_parsed_class_metadata(ik, _java_fields_count);
5047
5048 // can only set dynamic nest-host after static nest information is set
5049 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5050 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5051 }
5052
5053 // note that is not safe to use the fields in the parser from this point on
5054 assert(nullptr == _cp, "invariant");
5055 assert(nullptr == _fieldinfo_stream, "invariant");
5056 assert(nullptr == _fields_status, "invariant");
5057 assert(nullptr == _methods, "invariant");
5058 assert(nullptr == _inner_classes, "invariant");
5059 assert(nullptr == _nest_members, "invariant");
5060 assert(nullptr == _combined_annotations, "invariant");
5061 assert(nullptr == _record_components, "invariant");
5062 assert(nullptr == _permitted_subclasses, "invariant");
5063
5064 if (_has_localvariable_table) {
5065 ik->set_has_localvariable_table(true);
5066 }
5067
5068 if (_has_final_method) {
5069 ik->set_has_final_method();
5070 }
5071
5072 ik->copy_method_ordering(_method_ordering, CHECK);
5073 // The InstanceKlass::_methods_jmethod_ids cache
5074 // is managed on the assumption that the initial cache
5075 // size is equal to the number of methods in the class. If
5076 // that changes, then InstanceKlass::idnum_can_increment()
5077 // has to be changed accordingly.
5078 ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5079
5080 ik->set_this_class_index(_this_class_index);
5081
5082 if (_is_hidden) {
5120 if ((_num_miranda_methods > 0) ||
5121 // if this class introduced new miranda methods or
5122 (_super_klass != nullptr && _super_klass->has_miranda_methods())
5123 // super class exists and this class inherited miranda methods
5124 ) {
5125 ik->set_has_miranda_methods(); // then set a flag
5126 }
5127
5128 // Fill in information needed to compute superclasses.
5129 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5130 ik->set_transitive_interfaces(_transitive_interfaces);
5131 ik->set_local_interfaces(_local_interfaces);
5132 _transitive_interfaces = nullptr;
5133 _local_interfaces = nullptr;
5134
5135 // Initialize itable offset tables
5136 klassItable::setup_itable_offset_table(ik);
5137
5138 // Compute transitive closure of interfaces this class implements
5139 // Do final class setup
5140 OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks;
5141 if (oop_map_blocks->_nonstatic_oop_map_count > 0) {
5142 oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5143 }
5144
5145 if (_has_contended_fields || _parsed_annotations->is_contended() ||
5146 ( _super_klass != nullptr && _super_klass->has_contended_annotations())) {
5147 ik->set_has_contended_annotations(true);
5148 }
5149
5150 // Fill in has_finalizer and layout_helper
5151 set_precomputed_flags(ik);
5152
5153 // check if this class can access its super class
5154 check_super_class_access(ik, CHECK);
5155
5156 // check if this class can access its superinterfaces
5157 check_super_interface_access(ik, CHECK);
5158
5159 // check if this class overrides any final method
5160 check_final_method_override(ik, CHECK);
5181
5182 assert(_all_mirandas != nullptr, "invariant");
5183
5184 // Generate any default methods - default methods are public interface methods
5185 // that have a default implementation. This is new with Java 8.
5186 if (_has_nonstatic_concrete_methods) {
5187 DefaultMethods::generate_default_methods(ik,
5188 _all_mirandas,
5189 CHECK);
5190 }
5191
5192 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5193 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5194 !module_entry->has_default_read_edges()) {
5195 if (!module_entry->set_has_default_read_edges()) {
5196 // We won a potential race
5197 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5198 }
5199 }
5200
5201 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5202
5203 if (!is_internal()) {
5204 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5205
5206 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5207 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5208 log_is_enabled(Info, class, preview)) {
5209 ResourceMark rm;
5210 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5211 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5212 }
5213
5214 if (log_is_enabled(Debug, class, resolve)) {
5215 ResourceMark rm;
5216 // print out the superclass.
5217 const char * from = ik->external_name();
5218 if (ik->java_super() != nullptr) {
5219 log_debug(class, resolve)("%s %s (super)",
5220 from,
5262 ClassLoaderData* loader_data,
5263 const ClassLoadInfo* cl_info,
5264 Publicity pub_level,
5265 TRAPS) :
5266 _stream(stream),
5267 _class_name(nullptr),
5268 _loader_data(loader_data),
5269 _is_hidden(cl_info->is_hidden()),
5270 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5271 _orig_cp_size(0),
5272 _static_oop_count(0),
5273 _super_klass(),
5274 _cp(nullptr),
5275 _fieldinfo_stream(nullptr),
5276 _fields_status(nullptr),
5277 _methods(nullptr),
5278 _inner_classes(nullptr),
5279 _nest_members(nullptr),
5280 _nest_host(0),
5281 _permitted_subclasses(nullptr),
5282 _record_components(nullptr),
5283 _local_interfaces(nullptr),
5284 _transitive_interfaces(nullptr),
5285 _combined_annotations(nullptr),
5286 _class_annotations(nullptr),
5287 _class_type_annotations(nullptr),
5288 _fields_annotations(nullptr),
5289 _fields_type_annotations(nullptr),
5290 _klass(nullptr),
5291 _klass_to_deallocate(nullptr),
5292 _parsed_annotations(nullptr),
5293 _field_info(nullptr),
5294 _temp_field_info(nullptr),
5295 _method_ordering(nullptr),
5296 _all_mirandas(nullptr),
5297 _vtable_size(0),
5298 _itable_size(0),
5299 _num_miranda_methods(0),
5300 _protection_domain(cl_info->protection_domain()),
5301 _access_flags(),
5302 _pub_level(pub_level),
5303 _bad_constant_seen(0),
5304 _synthetic_flag(false),
5305 _sde_length(false),
5306 _sde_buffer(nullptr),
5307 _sourcefile_index(0),
5308 _generic_signature_index(0),
5309 _major_version(0),
5310 _minor_version(0),
5311 _this_class_index(0),
5312 _super_class_index(0),
5313 _itfs_len(0),
5314 _java_fields_count(0),
5315 _need_verify(false),
5316 _has_nonstatic_concrete_methods(false),
5317 _declares_nonstatic_concrete_methods(false),
5318 _has_localvariable_table(false),
5319 _has_final_method(false),
5320 _has_contended_fields(false),
5321 _has_finalizer(false),
5322 _has_empty_finalizer(false),
5323 _max_bootstrap_specifier_index(-1) {
5324
5325 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5326 _class_name->increment_refcount();
5327
5328 assert(_loader_data != nullptr, "invariant");
5329 assert(stream != nullptr, "invariant");
5330 assert(_stream != nullptr, "invariant");
5331 assert(_stream->buffer() == _stream->current(), "invariant");
5332 assert(_class_name != nullptr, "invariant");
5333 assert(0 == _access_flags.as_unsigned_short(), "invariant");
5334
5335 // Figure out whether we can skip format checking (matching classic VM behavior)
5336 _need_verify = Verifier::should_verify_for(_loader_data->class_loader());
5337
5338 // synch back verification state to stream to check for truncation.
5339 stream->set_need_verify(_need_verify);
5340
5341 parse_stream(stream, CHECK);
5342
5343 post_process_parsed_stream(stream, _cp, CHECK);
5344 }
5345
5346 void ClassFileParser::clear_class_metadata() {
5347 // metadata created before the instance klass is created. Must be
5348 // deallocated if classfile parsing returns an error.
5349 _cp = nullptr;
5350 _fieldinfo_stream = nullptr;
5351 _fields_status = nullptr;
5352 _methods = nullptr;
5353 _inner_classes = nullptr;
5354 _nest_members = nullptr;
5355 _permitted_subclasses = nullptr;
5356 _combined_annotations = nullptr;
5357 _class_annotations = _class_type_annotations = nullptr;
5358 _fields_annotations = _fields_type_annotations = nullptr;
5359 _record_components = nullptr;
5360 }
5361
5362 // Destructor to clean up
5363 ClassFileParser::~ClassFileParser() {
5364 _class_name->decrement_refcount();
5365
5366 if (_cp != nullptr) {
5367 MetadataFactory::free_metadata(_loader_data, _cp);
5368 }
5369
5370 if (_fieldinfo_stream != nullptr) {
5371 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5372 }
5373
5374 if (_fields_status != nullptr) {
5375 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5376 }
5377
5378 if (_methods != nullptr) {
5379 // Free methods
5380 InstanceKlass::deallocate_methods(_loader_data, _methods);
5381 }
5382
5383 // beware of the Universe::empty_blah_array!!
5384 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5385 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5386 }
5387
5388 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5389 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5390 }
5391
5392 if (_record_components != nullptr) {
5393 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5394 }
5395
5396 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5397 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5398 }
5399
5400 // Free interfaces
5401 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5402 _local_interfaces, _transitive_interfaces);
5403
5404 if (_combined_annotations != nullptr) {
5405 // After all annotations arrays have been created, they are installed into the
5406 // Annotations object that will be assigned to the InstanceKlass being created.
5407
5408 // Deallocate the Annotations object and the installed annotations arrays.
5409 _combined_annotations->deallocate_contents(_loader_data);
5410
5411 // If the _combined_annotations pointer is non-null,
5412 // then the other annotations fields should have been cleared.
5413 assert(_class_annotations == nullptr, "Should have been cleared");
5414 assert(_class_type_annotations == nullptr, "Should have been cleared");
5415 assert(_fields_annotations == nullptr, "Should have been cleared");
5416 assert(_fields_type_annotations == nullptr, "Should have been cleared");
5417 } else {
5418 // If the annotations arrays were not installed into the Annotations object,
5419 // then they have to be deallocated explicitly.
5464 cp_size, CHECK);
5465
5466 _orig_cp_size = cp_size;
5467 if (is_hidden()) { // Add a slot for hidden class name.
5468 cp_size++;
5469 }
5470
5471 _cp = ConstantPool::allocate(_loader_data,
5472 cp_size,
5473 CHECK);
5474
5475 ConstantPool* const cp = _cp;
5476
5477 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
5478
5479 assert(cp_size == (u2)cp->length(), "invariant");
5480
5481 // ACCESS FLAGS
5482 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5483
5484 // Access flags
5485 u2 flags;
5486 // JVM_ACC_MODULE is defined in JDK-9 and later.
5487 if (_major_version >= JAVA_9_VERSION) {
5488 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5489 } else {
5490 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5491 }
5492
5493 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5494 // Set abstract bit for old class files for backward compatibility
5495 flags |= JVM_ACC_ABSTRACT;
5496 }
5497
5498 verify_legal_class_modifiers(flags, CHECK);
5499
5500 short bad_constant = class_bad_constant_seen();
5501 if (bad_constant != 0) {
5502 // Do not throw CFE until after the access_flags are checked because if
5503 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5504 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5505 return;
5506 }
5507
5508 _access_flags.set_flags(flags);
5509
5510 // This class and superclass
5511 _this_class_index = stream->get_u2_fast();
5512 guarantee_property(
5513 valid_cp_range(_this_class_index, cp_size) &&
5514 cp->tag_at(_this_class_index).is_unresolved_klass(),
5515 "Invalid this class index %u in constant pool in class file %s",
5516 _this_class_index, CHECK);
5517
5518 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
5519 assert(class_name_in_cp != nullptr, "class_name can't be null");
5520
5521 // Don't need to check whether this class name is legal or not.
5522 // It has been checked when constant pool is parsed.
5523 // However, make sure it is not an array type.
5524 if (_need_verify) {
5525 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
5526 "Bad class name in class file %s",
5527 CHECK);
5528 }
5529
5530 #ifdef ASSERT
5531 // Basic sanity checks
5532 if (_is_hidden) {
5533 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
5534 }
5535 #endif
5536
5537 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
5538
5539 if (_is_hidden) {
5540 assert(_class_name != nullptr, "Unexpected null _class_name");
5581 }
5582 ls.cr();
5583 }
5584 }
5585
5586 // SUPERKLASS
5587 _super_class_index = stream->get_u2_fast();
5588 _super_klass = parse_super_class(cp,
5589 _super_class_index,
5590 _need_verify,
5591 CHECK);
5592
5593 // Interfaces
5594 _itfs_len = stream->get_u2_fast();
5595 parse_interfaces(stream,
5596 _itfs_len,
5597 cp,
5598 &_has_nonstatic_concrete_methods,
5599 CHECK);
5600
5601 assert(_local_interfaces != nullptr, "invariant");
5602
5603 // Fields (offsets are filled in later)
5604 parse_fields(stream,
5605 _access_flags.is_interface(),
5606 cp,
5607 cp_size,
5608 &_java_fields_count,
5609 CHECK);
5610
5611 assert(_temp_field_info != nullptr, "invariant");
5612
5613 // Methods
5614 parse_methods(stream,
5615 _access_flags.is_interface(),
5616 &_has_localvariable_table,
5617 &_has_final_method,
5618 &_declares_nonstatic_concrete_methods,
5619 CHECK);
5620
5621 assert(_methods != nullptr, "invariant");
5622
5623 if (_declares_nonstatic_concrete_methods) {
5624 _has_nonstatic_concrete_methods = true;
5625 }
5626
5627 // Additional attributes/annotations
5628 _parsed_annotations = new ClassAnnotationCollector();
5629 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
5630
5631 assert(_inner_classes != nullptr, "invariant");
5632
5633 // Finalize the Annotations metadata object,
5634 // now that all annotation arrays have been created.
5635 create_combined_annotations(CHECK);
5675 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
5676 // We have to update the resolved_klass_index and the name_index together
5677 // so extract the existing resolved_klass_index first.
5678 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
5679 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
5680 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
5681 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
5682 "Bad name_index");
5683 }
5684
5685 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
5686 ConstantPool* cp,
5687 TRAPS) {
5688 assert(stream != nullptr, "invariant");
5689 assert(stream->at_eos(), "invariant");
5690 assert(cp != nullptr, "invariant");
5691 assert(_loader_data != nullptr, "invariant");
5692
5693 if (_class_name == vmSymbols::java_lang_Object()) {
5694 guarantee_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
5695 "java.lang.Object cannot implement an interface in class file %s",
5696 CHECK);
5697 }
5698 // We check super class after class file is parsed and format is checked
5699 if (_super_class_index > 0 && nullptr == _super_klass) {
5700 Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
5701 if (_access_flags.is_interface()) {
5702 // Before attempting to resolve the superclass, check for class format
5703 // errors not checked yet.
5704 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
5705 "Interfaces must have java.lang.Object as superclass in class file %s",
5706 CHECK);
5707 }
5708 Handle loader(THREAD, _loader_data->class_loader());
5709 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
5710 _super_klass = vmClasses::Object_klass();
5711 } else {
5712 _super_klass = (const InstanceKlass*)
5713 SystemDictionary::resolve_super_or_fail(_class_name,
5714 super_class_name,
5715 loader,
5716 true,
5717 CHECK);
5718 }
5719 }
5720
5721 if (_super_klass != nullptr) {
5722 if (_super_klass->has_nonstatic_concrete_methods()) {
5723 _has_nonstatic_concrete_methods = true;
5724 }
5725
5726 if (_super_klass->is_interface()) {
5727 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
5728 return;
5729 }
5730 }
5731
5732 // Compute the transitive list of all unique interfaces implemented by this class
5733 _transitive_interfaces =
5734 compute_transitive_interfaces(_super_klass,
5735 _local_interfaces,
5736 _loader_data,
5737 CHECK);
5738
5739 assert(_transitive_interfaces != nullptr, "invariant");
5740
5741 // sort methods
5742 _method_ordering = sort_methods(_methods);
5743
5744 _all_mirandas = new GrowableArray<Method*>(20);
5745
5746 Handle loader(THREAD, _loader_data->class_loader());
5747 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
5748 &_num_miranda_methods,
5749 _all_mirandas,
5750 _super_klass,
5751 _methods,
5752 _access_flags,
5753 _major_version,
5754 loader,
5755 _class_name,
5756 _local_interfaces);
5757
5758 // Size of Java itable (in words)
5759 _itable_size = _access_flags.is_interface() ? 0 :
5760 klassItable::compute_itable_size(_transitive_interfaces);
5761
5762 assert(_parsed_annotations != nullptr, "invariant");
5763
5764 _field_info = new FieldLayoutInfo();
5765 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, /*_fields*/ _temp_field_info,
5766 _parsed_annotations->is_contended(), _field_info);
5767 lb.build_layout();
5768
5769 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
5770 _fieldinfo_stream =
5771 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
5772 injected_fields_count, loader_data(), CHECK);
5773 _fields_status =
5774 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
5775 FieldStatus(0), CHECK);
5776 }
5777
5778 void ClassFileParser::set_klass(InstanceKlass* klass) {
5779
5780 #ifdef ASSERT
5781 if (klass != nullptr) {
5782 assert(nullptr == _klass, "leaking?");
5783 }
5784 #endif
5785
5786 _klass = klass;
5787 }
5788
5789 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
5790
5791 #ifdef ASSERT
5792 if (klass != nullptr) {
|
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:
137 #define JAVA_14_VERSION 58
138
139 #define JAVA_15_VERSION 59
140
141 #define JAVA_16_VERSION 60
142
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 CONSTANT_CLASS_DESCRIPTORS 69
158
159 #define JAVA_24_VERSION 68
160
161 #define JAVA_25_VERSION 69
162
163 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
164 assert((bad_constant == JVM_CONSTANT_Module ||
165 bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
166 "Unexpected bad constant pool entry");
167 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
168 }
169
170 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
171 ConstantPool* cp,
172 const int length,
173 TRAPS) {
174 assert(stream != nullptr, "invariant");
175 assert(cp != nullptr, "invariant");
176
177 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
178 // this function (_current can be allocated in a register, with scalar
181 // this method that uses stream().
182 const ClassFileStream cfs1 = *stream;
183 const ClassFileStream* const cfs = &cfs1;
184
185 debug_only(const u1* const old_current = stream->current();)
186
187 // Used for batching symbol allocations.
188 const char* names[SymbolTable::symbol_alloc_batch_size];
189 int lengths[SymbolTable::symbol_alloc_batch_size];
190 int indices[SymbolTable::symbol_alloc_batch_size];
191 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
192 int names_count = 0;
193
194 // parsing Index 0 is unused
195 for (int index = 1; index < length; index++) {
196 // Each of the following case guarantees one more byte in the stream
197 // for the following tag or the access_flags following constant pool,
198 // so we don't need bounds-check for reading tag.
199 const u1 tag = cfs->get_u1_fast();
200 switch (tag) {
201 case JVM_CONSTANT_Class: {
202 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
203 const u2 name_index = cfs->get_u2_fast();
204 cp->klass_index_at_put(index, name_index);
205 break;
206 }
207 case JVM_CONSTANT_Fieldref: {
208 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
209 const u2 class_index = cfs->get_u2_fast();
210 const u2 name_and_type_index = cfs->get_u2_fast();
211 cp->field_at_put(index, class_index, name_and_type_index);
212 break;
213 }
214 case JVM_CONSTANT_Methodref: {
215 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
216 const u2 class_index = cfs->get_u2_fast();
217 const u2 name_and_type_index = cfs->get_u2_fast();
218 cp->method_at_put(index, class_index, name_and_type_index);
219 break;
220 }
221 case JVM_CONSTANT_InterfaceMethodref: {
485 guarantee_property(valid_symbol_at(name_ref_index),
486 "Invalid constant pool index %u in class file %s",
487 name_ref_index, CHECK);
488 guarantee_property(valid_symbol_at(signature_ref_index),
489 "Invalid constant pool index %u in class file %s",
490 signature_ref_index, CHECK);
491 break;
492 }
493 case JVM_CONSTANT_Utf8:
494 break;
495 case JVM_CONSTANT_UnresolvedClass: // fall-through
496 case JVM_CONSTANT_UnresolvedClassInError: {
497 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
498 break;
499 }
500 case JVM_CONSTANT_ClassIndex: {
501 const int class_index = cp->klass_index_at(index);
502 guarantee_property(valid_symbol_at(class_index),
503 "Invalid constant pool index %u in class file %s",
504 class_index, CHECK);
505
506 Symbol* const name = cp->symbol_at(class_index);
507 const unsigned int name_len = name->utf8_length();
508 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
509 break;
510 }
511 case JVM_CONSTANT_StringIndex: {
512 const int string_index = cp->string_index_at(index);
513 guarantee_property(valid_symbol_at(string_index),
514 "Invalid constant pool index %u in class file %s",
515 string_index, CHECK);
516 Symbol* const sym = cp->symbol_at(string_index);
517 cp->unresolved_string_at_put(index, sym);
518 break;
519 }
520 case JVM_CONSTANT_MethodHandle: {
521 const int ref_index = cp->method_handle_index_at(index);
522 guarantee_property(valid_cp_range(ref_index, length),
523 "Invalid constant pool index %u in class file %s",
524 ref_index, CHECK);
525 const constantTag tag = cp->tag_at(ref_index);
526 const int ref_kind = cp->method_handle_ref_kind_at(index);
527
697 }
698 }
699 } else {
700 if (_need_verify) {
701 // Method name and signature are individually verified above, when iterating
702 // NameAndType_info. Need to check here that signature is non-zero length and
703 // the right type.
704 if (!Signature::is_method(signature)) {
705 throwIllegalSignature("Method", name, signature, CHECK);
706 }
707 }
708 // If a class method name begins with '<', it must be "<init>" and have void signature.
709 const unsigned int name_len = name->utf8_length();
710 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
711 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
712 if (name != vmSymbols::object_initializer_name()) {
713 classfile_parse_error(
714 "Bad method name at constant pool index %u in class file %s",
715 name_ref_index, THREAD);
716 return;
717 } else if (!Signature::is_void_method(signature)) { // must have void signature.
718 throwIllegalSignature("Method", name, signature, CHECK);
719 }
720 }
721 }
722 break;
723 }
724 case JVM_CONSTANT_MethodHandle: {
725 const int ref_index = cp->method_handle_index_at(index);
726 const int ref_kind = cp->method_handle_ref_kind_at(index);
727 switch (ref_kind) {
728 case JVM_REF_invokeVirtual:
729 case JVM_REF_invokeStatic:
730 case JVM_REF_invokeSpecial:
731 case JVM_REF_newInvokeSpecial: {
732 const int name_and_type_ref_index =
733 cp->uncached_name_and_type_ref_index_at(ref_index);
734 const int name_ref_index =
735 cp->name_ref_index_at(name_and_type_ref_index);
736 const Symbol* const name = cp->symbol_at(name_ref_index);
737
738 if (name != vmSymbols::object_initializer_name()) { // !<init>
739 if (ref_kind == JVM_REF_newInvokeSpecial) {
740 classfile_parse_error(
741 "Bad constructor name at constant pool index %u in class file %s",
742 name_ref_index, THREAD);
743 return;
744 }
745 } else { // <init>
746 // The allowed invocation mode of <init> depends on its signature.
747 // This test corresponds to verify_invoke_instructions in the verifier.
748 const int signature_ref_index =
749 cp->signature_ref_index_at(name_and_type_ref_index);
750 const Symbol* const signature = cp->symbol_at(signature_ref_index);
751 if (signature->is_void_method_signature()
752 && ref_kind == JVM_REF_newInvokeSpecial) {
753 // OK, could be a constructor call
754 } else {
755 classfile_parse_error(
756 "Bad method name at constant pool index %u in class file %s",
757 name_ref_index, THREAD);
758 return;
759 }
760 }
761 break;
762 }
763 // Other ref_kinds are already fully checked in previous pass.
764 } // switch(ref_kind)
765 break;
766 }
767 case JVM_CONSTANT_MethodType: {
768 const Symbol* const no_name = vmSymbols::type_name(); // place holder
769 const Symbol* const signature = cp->method_type_signature_at(index);
770 verify_legal_method_signature(no_name, signature, CHECK);
771 break;
772 }
773 case JVM_CONSTANT_Utf8: {
774 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
786
787 NameSigHash(Symbol* name, Symbol* sig) :
788 _name(name),
789 _sig(sig) {}
790
791 static unsigned int hash(NameSigHash const& namesig) {
792 return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
793 }
794
795 static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
796 return (e0._name == e1._name) &&
797 (e0._sig == e1._sig);
798 }
799 };
800
801 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
802 NameSigHash::HASH_ROW_SIZE,
803 AnyObj::RESOURCE_AREA, mtInternal,
804 &NameSigHash::hash, &NameSigHash::equals>;
805
806 static void check_identity_and_value_modifiers(ClassFileParser* current, const InstanceKlass* super_type, TRAPS) {
807 assert(super_type != nullptr,"Method doesn't support null super type");
808 if (super_type->access_flags().is_identity_class() && !current->access_flags().is_identity_class()
809 && super_type->name() != vmSymbols::java_lang_Object()) {
810 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
811 err_msg("Value type %s has an identity type as supertype",
812 current->class_name()->as_klass_external_name()));
813 }
814 }
815
816 void ClassFileParser::parse_interfaces(const ClassFileStream* stream,
817 int itfs_len,
818 ConstantPool* cp,
819 bool* const has_nonstatic_concrete_methods,
820 // FIXME: lots of these functions
821 // declare their parameters as const,
822 // which adds only noise to the code.
823 // Remove the spurious const modifiers.
824 // Many are of the form "const int x"
825 // or "T* const x".
826 TRAPS) {
827 assert(stream != nullptr, "invariant");
828 assert(cp != nullptr, "invariant");
829 assert(has_nonstatic_concrete_methods != nullptr, "invariant");
830
831 if (itfs_len == 0) {
832 _local_interfaces = Universe::the_empty_instance_klass_array();
833
834 } else {
835 assert(itfs_len > 0, "only called for len>0");
836 _local_interface_indexes = new GrowableArray<u2>(itfs_len);
837 int index = 0;
838 for (index = 0; index < itfs_len; index++) {
839 const u2 interface_index = stream->get_u2(CHECK);
840 guarantee_property(
841 valid_klass_reference_at(interface_index),
842 "Interface name has bad constant pool index %u in class file %s",
843 interface_index, CHECK);
844 _local_interface_indexes->at_put_grow(index, interface_index);
845 }
846
847 if (!_need_verify || itfs_len <= 1) {
848 return;
849 }
850
851 // Check if there's any duplicates in interfaces
852 ResourceMark rm(THREAD);
853 // Set containing interface names
854 ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
855 for (index = 0; index < itfs_len; index++) {
856 Symbol* interface_name = cp->klass_name_at(_local_interface_indexes->at(index));
857 // If no duplicates, add (name, nullptr) in hashtable interface_names.
858 if (!interface_names->put(interface_name, 0)) {
859 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
860 interface_name->as_C_string(), THREAD);
861 return;
862 }
863 }
864 }
865 }
866
867 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
868 int constantvalue_index,
869 int signature_index,
870 TRAPS) const {
871 // Make sure the constant pool entry is of a type appropriate to this field
872 guarantee_property(
873 (constantvalue_index > 0 &&
874 constantvalue_index < cp->length()),
875 "Bad initial value index %u in ConstantValue attribute in class file %s",
876 constantvalue_index, CHECK);
923 class AnnotationCollector : public ResourceObj{
924 public:
925 enum Location { _in_field, _in_method, _in_class };
926 enum ID {
927 _unknown = 0,
928 _method_CallerSensitive,
929 _method_ForceInline,
930 _method_DontInline,
931 _method_ChangesCurrentThread,
932 _method_JvmtiHideEvents,
933 _method_JvmtiMountTransition,
934 _method_InjectedProfile,
935 _method_LambdaForm_Compiled,
936 _method_Hidden,
937 _method_Scoped,
938 _method_IntrinsicCandidate,
939 _jdk_internal_vm_annotation_Contended,
940 _field_Stable,
941 _jdk_internal_vm_annotation_ReservedStackAccess,
942 _jdk_internal_ValueBased,
943 _jdk_internal_LooselyConsistentValue,
944 _jdk_internal_NullRestricted,
945 _java_lang_Deprecated,
946 _java_lang_Deprecated_for_removal,
947 _annotation_LIMIT
948 };
949 const Location _location;
950 int _annotations_present;
951 u2 _contended_group;
952
953 AnnotationCollector(Location location)
954 : _location(location), _annotations_present(0), _contended_group(0)
955 {
956 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
957 }
958 // If this annotation name has an ID, report it (or _none).
959 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
960 // Set the annotation name:
961 void set_annotation(ID id) {
962 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
963 _annotations_present |= (int)nth_bit((int)id);
964 }
1347 }
1348
1349 *constantvalue_index_addr = constantvalue_index;
1350 *is_synthetic_addr = is_synthetic;
1351 *generic_signature_index_addr = generic_signature_index;
1352 AnnotationArray* a = allocate_annotations(runtime_visible_annotations,
1353 runtime_visible_annotations_length,
1354 CHECK);
1355 parsed_annotations->set_field_annotations(a);
1356 a = allocate_annotations(runtime_visible_type_annotations,
1357 runtime_visible_type_annotations_length,
1358 CHECK);
1359 parsed_annotations->set_field_type_annotations(a);
1360 return;
1361 }
1362
1363
1364 // Side-effects: populates the _fields, _fields_annotations,
1365 // _fields_type_annotations fields
1366 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1367 AccessFlags class_access_flags,
1368 ConstantPool* cp,
1369 const int cp_size,
1370 u2* const java_fields_count_ptr,
1371 TRAPS) {
1372
1373 assert(cfs != nullptr, "invariant");
1374 assert(cp != nullptr, "invariant");
1375 assert(java_fields_count_ptr != nullptr, "invariant");
1376
1377 assert(nullptr == _fields_annotations, "invariant");
1378 assert(nullptr == _fields_type_annotations, "invariant");
1379
1380 bool is_inline_type = !class_access_flags.is_identity_class() && !class_access_flags.is_abstract();
1381 cfs->guarantee_more(2, CHECK); // length
1382 const u2 length = cfs->get_u2_fast();
1383 *java_fields_count_ptr = length;
1384
1385 int num_injected = 0;
1386 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1387 &num_injected);
1388
1389 // two more slots are required for inline classes:
1390 // one for the static field with a reference to the pre-allocated default value
1391 // one for the field the JVM injects when detecting an empty inline class
1392 const int total_fields = length + num_injected + (is_inline_type ? 2 : 0);
1393
1394 // Allocate a temporary resource array to collect field data.
1395 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1396 _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1397
1398 int instance_fields_count = 0;
1399 ResourceMark rm(THREAD);
1400 for (int n = 0; n < length; n++) {
1401 // access_flags, name_index, descriptor_index, attributes_count
1402 cfs->guarantee_more(8, CHECK);
1403
1404 jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS;
1405 if (!supports_inline_types()) {
1406 recognized_modifiers &= ~JVM_ACC_STRICT;
1407 }
1408
1409 const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1410 verify_legal_field_modifiers(flags, class_access_flags, CHECK);
1411 AccessFlags access_flags;
1412 access_flags.set_flags(flags);
1413 FieldInfo::FieldFlags fieldFlags(0);
1414
1415 const u2 name_index = cfs->get_u2_fast();
1416 guarantee_property(valid_symbol_at(name_index),
1417 "Invalid constant pool index %u for field name in class file %s",
1418 name_index, CHECK);
1419 const Symbol* const name = cp->symbol_at(name_index);
1420 verify_legal_field_name(name, CHECK);
1421
1422 const u2 signature_index = cfs->get_u2_fast();
1423 guarantee_property(valid_symbol_at(signature_index),
1424 "Invalid constant pool index %u for field signature in class file %s",
1425 signature_index, CHECK);
1426 const Symbol* const sig = cp->symbol_at(signature_index);
1427 verify_legal_field_signature(name, sig, CHECK);
1428 if (!access_flags.is_static()) instance_fields_count++;
1429
1430 u2 constantvalue_index = 0;
1431 bool is_synthetic = false;
1432 u2 generic_signature_index = 0;
1433 const bool is_static = access_flags.is_static();
1434 FieldAnnotationCollector parsed_annotations(_loader_data);
1435
1436 bool is_null_restricted = false;
1437
1438 const u2 attributes_count = cfs->get_u2_fast();
1439 if (attributes_count > 0) {
1440 parse_field_attributes(cfs,
1441 attributes_count,
1442 is_static,
1443 signature_index,
1444 &constantvalue_index,
1445 &is_synthetic,
1446 &generic_signature_index,
1447 &parsed_annotations,
1448 CHECK);
1449
1450 if (parsed_annotations.field_annotations() != nullptr) {
1451 if (_fields_annotations == nullptr) {
1452 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1453 _loader_data, length, nullptr,
1454 CHECK);
1455 }
1456 _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1457 if (parsed_annotations.has_annotation(AnnotationCollector::_jdk_internal_NullRestricted)) {
1458 if (!Signature::has_envelope(sig)) {
1459 Exceptions::fthrow(
1460 THREAD_AND_LOCATION,
1461 vmSymbols::java_lang_ClassFormatError(),
1462 "Illegal use of @jdk.internal.vm.annotation.NullRestricted annotation on field %s.%s with signature %s (primitive types can never be null)",
1463 class_name()->as_C_string(), name->as_C_string(), sig->as_C_string());
1464 }
1465 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
1466 if (!is_strict) {
1467 Exceptions::fthrow(
1468 THREAD_AND_LOCATION,
1469 vmSymbols::java_lang_ClassFormatError(),
1470 "Illegal use of @jdk.internal.vm.annotation.NullRestricted annotation on field %s.%s which doesn't have the @jdk.internal.vm.annotation.Strict annotation",
1471 class_name()->as_C_string(), name->as_C_string());
1472 }
1473 is_null_restricted = true;
1474 }
1475 parsed_annotations.set_field_annotations(nullptr);
1476 }
1477 if (parsed_annotations.field_type_annotations() != nullptr) {
1478 if (_fields_type_annotations == nullptr) {
1479 _fields_type_annotations =
1480 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1481 length,
1482 nullptr,
1483 CHECK);
1484 }
1485 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1486 parsed_annotations.set_field_type_annotations(nullptr);
1487 }
1488
1489 if (is_synthetic) {
1490 access_flags.set_is_synthetic();
1491 }
1492 if (generic_signature_index != 0) {
1493 fieldFlags.update_generic(true);
1494 }
1495 }
1496
1497 if (is_null_restricted) {
1498 fieldFlags.update_null_free_inline_type(true);
1499 }
1500
1501 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1502
1503 // Update number of static oop fields.
1504 if (is_static && is_reference_type(type)) {
1505 _static_oop_count++;
1506 }
1507
1508 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1509 fi.set_index(n);
1510 if (fieldFlags.is_generic()) {
1511 fi.set_generic_signature_index(generic_signature_index);
1512 }
1513 parsed_annotations.apply_to(&fi);
1514 if (fi.field_flags().is_contended()) {
1515 _has_contended_fields = true;
1516 }
1517 _temp_field_info->append(fi);
1518 }
1519 assert(_temp_field_info->length() == length, "Must be");
1520
1521 if (num_injected != 0) {
1522 for (int n = 0; n < num_injected; n++) {
1523 // Check for duplicates
1524 if (injected[n].may_be_java) {
1525 const Symbol* const name = injected[n].name();
1526 const Symbol* const signature = injected[n].signature();
1527 bool duplicate = false;
1528 for (int i = 0; i < length; i++) {
1529 const FieldInfo* const f = _temp_field_info->adr_at(i);
1530 if (name == cp->symbol_at(f->name_index()) &&
1531 signature == cp->symbol_at(f->signature_index())) {
1532 // Symbol is desclared in Java so skip this one
1533 duplicate = true;
1534 break;
1535 }
1536 }
1537 if (duplicate) {
1538 // These will be removed from the field array at the end
1539 continue;
1540 }
1541 }
1542
1543 // Injected field
1544 FieldInfo::FieldFlags fflags(0);
1545 fflags.update_injected(true);
1546 AccessFlags aflags;
1547 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1548 int idx = _temp_field_info->append(fi);
1549 _temp_field_info->adr_at(idx)->set_index(idx);
1550 }
1551 }
1552
1553 if (is_inline_type) {
1554 // Inject static ".null_reset" field. This is an all-zero value with its null-channel set to zero.
1555 // IT should never be seen by user code, it is used when writing "null" to a nullable flat field
1556 // The all-zero value ensure that any embedded oop will be set to null, to avoid keeping dead objects
1557 // alive.
1558 FieldInfo::FieldFlags fflags2(0);
1559 fflags2.update_injected(true);
1560 AccessFlags aflags2(JVM_ACC_STATIC);
1561 FieldInfo fi2(aflags2,
1562 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(null_reset_value_name)),
1563 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(object_signature)),
1564 0,
1565 fflags2);
1566 int idx2 = _temp_field_info->append(fi2);
1567 _temp_field_info->adr_at(idx2)->set_index(idx2);
1568 _static_oop_count++;
1569 }
1570
1571 if (_need_verify && length > 1) {
1572 // Check duplicated fields
1573 ResourceMark rm(THREAD);
1574 // Set containing name-signature pairs
1575 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1576 for (int i = 0; i < _temp_field_info->length(); i++) {
1577 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1578 _temp_field_info->adr_at(i)->signature(_cp));
1579 // If no duplicates, add name/signature in hashtable names_and_sigs.
1580 if(!names_and_sigs->put(name_and_sig, 0)) {
1581 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1582 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1583 return;
1584 }
1585 }
1586 }
1587 }
1588
1589
1929 }
1930 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
1931 if (_location != _in_field && _location != _in_class) {
1932 break; // only allow for fields and classes
1933 }
1934 if (!EnableContended || (RestrictContended && !privileged)) {
1935 break; // honor privileges
1936 }
1937 return _jdk_internal_vm_annotation_Contended;
1938 }
1939 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
1940 if (_location != _in_method) break; // only allow for methods
1941 if (RestrictReservedStack && !privileged) break; // honor privileges
1942 return _jdk_internal_vm_annotation_ReservedStackAccess;
1943 }
1944 case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
1945 if (_location != _in_class) break; // only allow for classes
1946 if (!privileged) break; // only allow in privileged code
1947 return _jdk_internal_ValueBased;
1948 }
1949 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_LooselyConsistentValue_signature): {
1950 if (_location != _in_class) break; // only allow for classes
1951 return _jdk_internal_LooselyConsistentValue;
1952 }
1953 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_NullRestricted_signature): {
1954 if (_location != _in_field) break; // only allow for fields
1955 return _jdk_internal_NullRestricted;
1956 }
1957 case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
1958 return _java_lang_Deprecated;
1959 }
1960 default: {
1961 break;
1962 }
1963 }
1964 return AnnotationCollector::_unknown;
1965 }
1966
1967 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
1968 if (is_contended())
1969 // Setting the contended group also sets the contended bit in field flags
1970 f->set_contended_group(contended_group());
1971 if (is_stable())
1972 (f->field_flags_addr())->update_stable(true);
1973 }
1974
1975 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {
1976 // If there's an error deallocate metadata for field annotations
2160 }
2161
2162 if (runtime_visible_type_annotations_length > 0) {
2163 a = allocate_annotations(runtime_visible_type_annotations,
2164 runtime_visible_type_annotations_length,
2165 CHECK);
2166 cm->set_type_annotations(a);
2167 }
2168 }
2169
2170
2171 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2172 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2173 // Method* to save footprint, so we only know the size of the resulting Method* when the
2174 // entire method attribute is parsed.
2175 //
2176 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2177
2178 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2179 bool is_interface,
2180 bool is_value_class,
2181 bool is_abstract_class,
2182 const ConstantPool* cp,
2183 bool* const has_localvariable_table,
2184 TRAPS) {
2185 assert(cfs != nullptr, "invariant");
2186 assert(cp != nullptr, "invariant");
2187 assert(has_localvariable_table != nullptr, "invariant");
2188
2189 ResourceMark rm(THREAD);
2190 // Parse fixed parts:
2191 // access_flags, name_index, descriptor_index, attributes_count
2192 cfs->guarantee_more(8, CHECK_NULL);
2193
2194 u2 flags = cfs->get_u2_fast();
2195 const u2 name_index = cfs->get_u2_fast();
2196 const int cp_size = cp->length();
2197 guarantee_property(
2198 valid_symbol_at(name_index),
2199 "Illegal constant pool index %u for method name in class file %s",
2200 name_index, CHECK_NULL);
2201 const Symbol* const name = cp->symbol_at(name_index);
2203
2204 const u2 signature_index = cfs->get_u2_fast();
2205 guarantee_property(
2206 valid_symbol_at(signature_index),
2207 "Illegal constant pool index %u for method signature in class file %s",
2208 signature_index, CHECK_NULL);
2209 const Symbol* const signature = cp->symbol_at(signature_index);
2210
2211 if (name == vmSymbols::class_initializer_name()) {
2212 // We ignore the other access flags for a valid class initializer.
2213 // (JVM Spec 2nd ed., chapter 4.6)
2214 if (_major_version < 51) { // backward compatibility
2215 flags = JVM_ACC_STATIC;
2216 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2217 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2218 } else {
2219 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2220 return nullptr;
2221 }
2222 } else {
2223 verify_legal_method_modifiers(flags, access_flags() , name, CHECK_NULL);
2224 }
2225
2226 if (name == vmSymbols::object_initializer_name() && is_interface) {
2227 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2228 return nullptr;
2229 }
2230
2231 if (EnableValhalla) {
2232 if (((flags & JVM_ACC_SYNCHRONIZED) == JVM_ACC_SYNCHRONIZED)
2233 && ((flags & JVM_ACC_STATIC) == 0 )
2234 && !_access_flags.is_identity_class()) {
2235 classfile_parse_error("Invalid synchronized method in non-identity class %s", THREAD);
2236 return nullptr;
2237 }
2238 }
2239
2240 int args_size = -1; // only used when _need_verify is true
2241 if (_need_verify) {
2242 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2243 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2244 verify_legal_method_signature(name, signature, CHECK_NULL);
2245 if (args_size > MAX_ARGS_SIZE) {
2246 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2247 return nullptr;
2248 }
2249 }
2250
2251 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2252
2253 // Default values for code and exceptions attribute elements
2254 u2 max_stack = 0;
2255 u2 max_locals = 0;
2256 u4 code_length = 0;
2257 const u1* code_start = nullptr;
2258 u2 exception_table_length = 0;
2259 const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements
2747 CHECK_NULL);
2748
2749 if (InstanceKlass::is_finalization_enabled() &&
2750 name == vmSymbols::finalize_method_name() &&
2751 signature == vmSymbols::void_method_signature()) {
2752 if (m->is_empty_method()) {
2753 _has_empty_finalizer = true;
2754 } else {
2755 _has_finalizer = true;
2756 }
2757 }
2758
2759 NOT_PRODUCT(m->verify());
2760 return m;
2761 }
2762
2763
2764 // Side-effects: populates the _methods field in the parser
2765 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2766 bool is_interface,
2767 bool is_value_class,
2768 bool is_abstract_type,
2769 bool* const has_localvariable_table,
2770 bool* has_final_method,
2771 bool* declares_nonstatic_concrete_methods,
2772 TRAPS) {
2773 assert(cfs != nullptr, "invariant");
2774 assert(has_localvariable_table != nullptr, "invariant");
2775 assert(has_final_method != nullptr, "invariant");
2776 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2777
2778 assert(nullptr == _methods, "invariant");
2779
2780 cfs->guarantee_more(2, CHECK); // length
2781 const u2 length = cfs->get_u2_fast();
2782 if (length == 0) {
2783 _methods = Universe::the_empty_method_array();
2784 } else {
2785 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2786 length,
2787 nullptr,
2788 CHECK);
2789
2790 for (int index = 0; index < length; index++) {
2791 Method* method = parse_method(cfs,
2792 is_interface,
2793 is_value_class,
2794 is_abstract_type,
2795 _cp,
2796 has_localvariable_table,
2797 CHECK);
2798
2799 if (method->is_final()) {
2800 *has_final_method = true;
2801 }
2802 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2803 // used for interface initialization, and default method inheritance analysis
2804 if (is_interface && !(*declares_nonstatic_concrete_methods)
2805 && !method->is_abstract() && !method->is_static()) {
2806 *declares_nonstatic_concrete_methods = true;
2807 }
2808 _methods->at_put(index, method);
2809 }
2810
2811 if (_need_verify && length > 1) {
2812 // Check duplicated methods
2813 ResourceMark rm(THREAD);
2814 // Set containing name-signature pairs
3040 valid_klass_reference_at(outer_class_info_index),
3041 "outer_class_info_index %u has bad constant type in class file %s",
3042 outer_class_info_index, CHECK_0);
3043
3044 if (outer_class_info_index != 0) {
3045 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3046 char* bytes = (char*)outer_class_name->bytes();
3047 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3048 "Outer class is an array class in class file %s", CHECK_0);
3049 }
3050 // Inner class name
3051 const u2 inner_name_index = cfs->get_u2_fast();
3052 guarantee_property(
3053 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3054 "inner_name_index %u has bad constant type in class file %s",
3055 inner_name_index, CHECK_0);
3056 if (_need_verify) {
3057 guarantee_property(inner_class_info_index != outer_class_info_index,
3058 "Class is both outer and inner class in class file %s", CHECK_0);
3059 }
3060
3061 u2 recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS;
3062 // JVM_ACC_MODULE is defined in JDK-9 and later.
3063 if (_major_version >= JAVA_9_VERSION) {
3064 recognized_modifiers |= JVM_ACC_MODULE;
3065 }
3066
3067 // Access flags
3068 u2 flags = cfs->get_u2_fast() & recognized_modifiers;
3069
3070 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3071 // Set abstract bit for old class files for backward compatibility
3072 flags |= JVM_ACC_ABSTRACT;
3073 }
3074
3075 if (!supports_inline_types()) {
3076 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
3077 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3078 if (!is_module && !is_interface) {
3079 flags |= JVM_ACC_IDENTITY;
3080 }
3081 }
3082
3083 const char* name = inner_name_index == 0 ? "unnamed" : cp->symbol_at(inner_name_index)->as_utf8();
3084 verify_legal_class_modifiers(flags, name, false, CHECK_0);
3085 AccessFlags inner_access_flags(flags);
3086
3087 inner_classes->at_put(index++, inner_class_info_index);
3088 inner_classes->at_put(index++, outer_class_info_index);
3089 inner_classes->at_put(index++, inner_name_index);
3090 inner_classes->at_put(index++, inner_access_flags.as_unsigned_short());
3091 }
3092
3093 // Check for circular and duplicate entries.
3094 bool has_circularity = false;
3095 if (_need_verify) {
3096 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3097 if (has_circularity) {
3098 // If circularity check failed then ignore InnerClasses attribute.
3099 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3100 index = 0;
3101 if (parsed_enclosingmethod_attribute) {
3102 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3103 _inner_classes = inner_classes;
3104 } else {
3168 if (length > 0) {
3169 int index = 0;
3170 cfs->guarantee_more(2 * length, CHECK_0);
3171 for (int n = 0; n < length; n++) {
3172 const u2 class_info_index = cfs->get_u2_fast();
3173 guarantee_property(
3174 valid_klass_reference_at(class_info_index),
3175 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3176 class_info_index, CHECK_0);
3177 permitted_subclasses->at_put(index++, class_info_index);
3178 }
3179 assert(index == size, "wrong size");
3180 }
3181
3182 // Restore buffer's current position.
3183 cfs->set_current(current_mark);
3184
3185 return length;
3186 }
3187
3188 u2 ClassFileParser::parse_classfile_loadable_descriptors_attribute(const ClassFileStream* const cfs,
3189 const u1* const loadable_descriptors_attribute_start,
3190 TRAPS) {
3191 const u1* const current_mark = cfs->current();
3192 u2 length = 0;
3193 if (loadable_descriptors_attribute_start != nullptr) {
3194 cfs->set_current(loadable_descriptors_attribute_start);
3195 cfs->guarantee_more(2, CHECK_0); // length
3196 length = cfs->get_u2_fast();
3197 }
3198 const int size = length;
3199 Array<u2>* const loadable_descriptors = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3200 _loadable_descriptors = loadable_descriptors;
3201 if (length > 0) {
3202 int index = 0;
3203 cfs->guarantee_more(2 * length, CHECK_0);
3204 for (int n = 0; n < length; n++) {
3205 const u2 descriptor_index = cfs->get_u2_fast();
3206 guarantee_property(
3207 valid_symbol_at(descriptor_index),
3208 "LoadableDescriptors descriptor_index %u has bad constant type in class file %s",
3209 descriptor_index, CHECK_0);
3210 Symbol* descriptor = _cp->symbol_at(descriptor_index);
3211 bool valid = legal_field_signature(descriptor, CHECK_0);
3212 if(!valid) {
3213 ResourceMark rm(THREAD);
3214 Exceptions::fthrow(THREAD_AND_LOCATION,
3215 vmSymbols::java_lang_ClassFormatError(),
3216 "Descriptor from LoadableDescriptors attribute at index \"%d\" in class %s has illegal signature \"%s\"",
3217 descriptor_index, _class_name->as_C_string(), descriptor->as_C_string());
3218 return 0;
3219 }
3220 loadable_descriptors->at_put(index++, descriptor_index);
3221 }
3222 assert(index == size, "wrong size");
3223 }
3224
3225 // Restore buffer's current position.
3226 cfs->set_current(current_mark);
3227
3228 return length;
3229 }
3230
3231 // Record {
3232 // u2 attribute_name_index;
3233 // u4 attribute_length;
3234 // u2 components_count;
3235 // component_info components[components_count];
3236 // }
3237 // component_info {
3238 // u2 name_index;
3239 // u2 descriptor_index
3240 // u2 attributes_count;
3241 // attribute_info_attributes[attributes_count];
3242 // }
3243 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3244 const ConstantPool* cp,
3245 const u1* const record_attribute_start,
3246 TRAPS) {
3247 const u1* const current_mark = cfs->current();
3248 int components_count = 0;
3249 unsigned int calculate_attr_size = 0;
3250 if (record_attribute_start != nullptr) {
3476 }
3477 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3478 "Bad length on BootstrapMethods in class file %s",
3479 CHECK);
3480 }
3481
3482 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3483 ConstantPool* cp,
3484 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3485 TRAPS) {
3486 assert(cfs != nullptr, "invariant");
3487 assert(cp != nullptr, "invariant");
3488 assert(parsed_annotations != nullptr, "invariant");
3489
3490 // Set inner classes attribute to default sentinel
3491 _inner_classes = Universe::the_empty_short_array();
3492 // Set nest members attribute to default sentinel
3493 _nest_members = Universe::the_empty_short_array();
3494 // Set _permitted_subclasses attribute to default sentinel
3495 _permitted_subclasses = Universe::the_empty_short_array();
3496 // Set _loadable_descriptors attribute to default sentinel
3497 _loadable_descriptors = Universe::the_empty_short_array();
3498 cfs->guarantee_more(2, CHECK); // attributes_count
3499 u2 attributes_count = cfs->get_u2_fast();
3500 bool parsed_sourcefile_attribute = false;
3501 bool parsed_innerclasses_attribute = false;
3502 bool parsed_nest_members_attribute = false;
3503 bool parsed_permitted_subclasses_attribute = false;
3504 bool parsed_loadable_descriptors_attribute = false;
3505 bool parsed_nest_host_attribute = false;
3506 bool parsed_record_attribute = false;
3507 bool parsed_enclosingmethod_attribute = false;
3508 bool parsed_bootstrap_methods_attribute = false;
3509 const u1* runtime_visible_annotations = nullptr;
3510 int runtime_visible_annotations_length = 0;
3511 const u1* runtime_visible_type_annotations = nullptr;
3512 int runtime_visible_type_annotations_length = 0;
3513 bool runtime_invisible_type_annotations_exists = false;
3514 bool runtime_invisible_annotations_exists = false;
3515 bool parsed_source_debug_ext_annotations_exist = false;
3516 const u1* inner_classes_attribute_start = nullptr;
3517 u4 inner_classes_attribute_length = 0;
3518 u2 enclosing_method_class_index = 0;
3519 u2 enclosing_method_method_index = 0;
3520 const u1* nest_members_attribute_start = nullptr;
3521 u4 nest_members_attribute_length = 0;
3522 const u1* record_attribute_start = nullptr;
3523 u4 record_attribute_length = 0;
3524 const u1* permitted_subclasses_attribute_start = nullptr;
3525 u4 permitted_subclasses_attribute_length = 0;
3526 const u1* loadable_descriptors_attribute_start = nullptr;
3527 u4 loadable_descriptors_attribute_length = 0;
3528
3529 // Iterate over attributes
3530 while (attributes_count--) {
3531 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3532 const u2 attribute_name_index = cfs->get_u2_fast();
3533 const u4 attribute_length = cfs->get_u4_fast();
3534 guarantee_property(
3535 valid_symbol_at(attribute_name_index),
3536 "Attribute name has bad constant pool index %u in class file %s",
3537 attribute_name_index, CHECK);
3538 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3539 if (tag == vmSymbols::tag_source_file()) {
3540 // Check for SourceFile tag
3541 if (_need_verify) {
3542 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3543 }
3544 if (parsed_sourcefile_attribute) {
3545 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3546 return;
3547 } else {
3723 return;
3724 }
3725 parsed_record_attribute = true;
3726 record_attribute_start = cfs->current();
3727 record_attribute_length = attribute_length;
3728 } else if (_major_version >= JAVA_17_VERSION) {
3729 if (tag == vmSymbols::tag_permitted_subclasses()) {
3730 if (parsed_permitted_subclasses_attribute) {
3731 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3732 return;
3733 }
3734 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3735 if (_access_flags.is_final()) {
3736 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3737 return;
3738 }
3739 parsed_permitted_subclasses_attribute = true;
3740 permitted_subclasses_attribute_start = cfs->current();
3741 permitted_subclasses_attribute_length = attribute_length;
3742 }
3743 if (EnableValhalla && tag == vmSymbols::tag_loadable_descriptors()) {
3744 if (parsed_loadable_descriptors_attribute) {
3745 classfile_parse_error("Multiple LoadableDescriptors attributes in class file %s", CHECK);
3746 return;
3747 }
3748 parsed_loadable_descriptors_attribute = true;
3749 loadable_descriptors_attribute_start = cfs->current();
3750 loadable_descriptors_attribute_length = attribute_length;
3751 }
3752 }
3753 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3754 cfs->skip_u1(attribute_length, CHECK);
3755 } else {
3756 // Unknown attribute
3757 cfs->skip_u1(attribute_length, CHECK);
3758 }
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 }
3768 _class_annotations = allocate_annotations(runtime_visible_annotations,
3769 runtime_visible_annotations_length,
3770 CHECK);
3771 _class_type_annotations = allocate_annotations(runtime_visible_type_annotations,
3808 CHECK);
3809 if (_need_verify) {
3810 guarantee_property(record_attribute_length == calculated_attr_length,
3811 "Record attribute has wrong length in class file %s",
3812 CHECK);
3813 }
3814 }
3815
3816 if (parsed_permitted_subclasses_attribute) {
3817 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3818 cfs,
3819 permitted_subclasses_attribute_start,
3820 CHECK);
3821 if (_need_verify) {
3822 guarantee_property(
3823 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3824 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3825 }
3826 }
3827
3828 if (parsed_loadable_descriptors_attribute) {
3829 const u2 num_classes = parse_classfile_loadable_descriptors_attribute(
3830 cfs,
3831 loadable_descriptors_attribute_start,
3832 CHECK);
3833 if (_need_verify) {
3834 guarantee_property(
3835 loadable_descriptors_attribute_length == sizeof(num_classes) + sizeof(u2) * num_classes,
3836 "Wrong LoadableDescriptors attribute length in class file %s", CHECK);
3837 }
3838 }
3839
3840 if (_max_bootstrap_specifier_index >= 0) {
3841 guarantee_property(parsed_bootstrap_methods_attribute,
3842 "Missing BootstrapMethods attribute in class file %s", CHECK);
3843 }
3844 }
3845
3846 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3847 assert(k != nullptr, "invariant");
3848
3849 if (_synthetic_flag)
3850 k->set_is_synthetic();
3851 if (_sourcefile_index != 0) {
3852 k->set_source_file_name_index(_sourcefile_index);
3853 }
3854 if (_generic_signature_index != 0) {
3855 k->set_generic_signature_index(_generic_signature_index);
3856 }
3857 if (_sde_buffer != nullptr) {
3858 k->set_source_debug_extension(_sde_buffer, _sde_length);
3859 }
3885 _class_annotations = nullptr;
3886 _class_type_annotations = nullptr;
3887 _fields_annotations = nullptr;
3888 _fields_type_annotations = nullptr;
3889 }
3890
3891 // Transfer ownership of metadata allocated to the InstanceKlass.
3892 void ClassFileParser::apply_parsed_class_metadata(
3893 InstanceKlass* this_klass,
3894 int java_fields_count) {
3895 assert(this_klass != nullptr, "invariant");
3896
3897 _cp->set_pool_holder(this_klass);
3898 this_klass->set_constants(_cp);
3899 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3900 this_klass->set_fields_status(_fields_status);
3901 this_klass->set_methods(_methods);
3902 this_klass->set_inner_classes(_inner_classes);
3903 this_klass->set_nest_members(_nest_members);
3904 this_klass->set_nest_host_index(_nest_host);
3905 this_klass->set_loadable_descriptors(_loadable_descriptors);
3906 this_klass->set_annotations(_combined_annotations);
3907 this_klass->set_permitted_subclasses(_permitted_subclasses);
3908 this_klass->set_record_components(_record_components);
3909 this_klass->set_inline_layout_info_array(_inline_layout_info_array);
3910
3911 // Delay the setting of _local_interfaces and _transitive_interfaces until after
3912 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3913 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3914 // its _super. If an OOM occurs while loading the current klass, its _super field
3915 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3916 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3917 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3918
3919 // Clear out these fields so they don't get deallocated by the destructor
3920 clear_class_metadata();
3921 }
3922
3923 AnnotationArray* ClassFileParser::allocate_annotations(const u1* const anno,
3924 int anno_length,
3925 TRAPS) {
3926 AnnotationArray* annotations = nullptr;
3927 if (anno != nullptr) {
3928 annotations = MetadataFactory::new_array<u1>(_loader_data,
3929 anno_length,
3930 CHECK_(annotations));
3931 for (int i = 0; i < anno_length; i++) {
3932 annotations->at_put(i, anno[i]);
3933 }
3934 }
3935 return annotations;
3936 }
3937
3938 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3939 const int super_class_index,
3940 const bool need_verify,
3941 TRAPS) {
3942 assert(cp != nullptr, "invariant");
3943 const InstanceKlass* super_klass = nullptr;
3944
3945 if (super_class_index == 0) {
3946 guarantee_property(_class_name == vmSymbols::java_lang_Object(),
3947 "Invalid superclass index 0 in class file %s",
3948 CHECK_NULL);
3949 } else {
3950 guarantee_property(valid_klass_reference_at(super_class_index),
3951 "Invalid superclass index %u in class file %s",
3952 super_class_index,
3953 CHECK_NULL);
3954 // The class name should be legal because it is checked when parsing constant pool.
3955 // However, make sure it is not an array type.
3956 if (cp->tag_at(super_class_index).is_klass()) {
3957 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3958 }
3959 if (need_verify) {
3960 bool is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3961 guarantee_property(!is_array,
3962 "Bad superclass name in class file %s", CHECK_NULL);
3963 }
3964 }
3965 return super_klass;
3966 }
3967
3968 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
3969 _max_nonstatic_oop_maps = max_blocks;
3970 _nonstatic_oop_map_count = 0;
3971 if (max_blocks == 0) {
3972 _nonstatic_oop_maps = nullptr;
3973 } else {
3974 _nonstatic_oop_maps =
3975 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
3976 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3977 }
3978 }
3979
3980 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
4114
4115 // Check if this klass supports the java.lang.Cloneable interface
4116 if (vmClasses::Cloneable_klass_loaded()) {
4117 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4118 ik->set_is_cloneable();
4119 }
4120 }
4121
4122 // If it cannot be fast-path allocated, set a bit in the layout helper.
4123 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4124 assert(ik->size_helper() > 0, "layout_helper is initialized");
4125 if (ik->is_abstract() || ik->is_interface()
4126 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
4127 || ik->size_helper() >= FastAllocateSizeLimit) {
4128 // Forbid fast-path allocation.
4129 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4130 ik->set_layout_helper(lh);
4131 }
4132 }
4133
4134 bool ClassFileParser::supports_inline_types() const {
4135 // Inline types are only supported by class file version 69.65535 and later
4136 return _major_version > JAVA_25_VERSION ||
4137 (_major_version == JAVA_25_VERSION && _minor_version == JAVA_PREVIEW_MINOR_VERSION);
4138 }
4139
4140 // utility methods for appending an array with check for duplicates
4141
4142 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4143 const Array<InstanceKlass*>* const ifs) {
4144 // iterate over new interfaces
4145 for (int i = 0; i < ifs->length(); i++) {
4146 InstanceKlass* const e = ifs->at(i);
4147 assert(e->is_klass() && e->is_interface(), "just checking");
4148 // add new interface
4149 result->append_if_missing(e);
4150 }
4151 }
4152
4153 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4154 Array<InstanceKlass*>* local_ifs,
4155 ClassLoaderData* loader_data,
4156 TRAPS) {
4157 assert(local_ifs != nullptr, "invariant");
4158 assert(loader_data != nullptr, "invariant");
4159
4163 // Add superclass transitive interfaces size
4164 if (super != nullptr) {
4165 super_size = super->transitive_interfaces()->length();
4166 max_transitive_size += super_size;
4167 }
4168 // Add local interfaces' super interfaces
4169 const int local_size = local_ifs->length();
4170 for (int i = 0; i < local_size; i++) {
4171 InstanceKlass* const l = local_ifs->at(i);
4172 max_transitive_size += l->transitive_interfaces()->length();
4173 }
4174 // Finally add local interfaces
4175 max_transitive_size += local_size;
4176 // Construct array
4177 if (max_transitive_size == 0) {
4178 // no interfaces, use canonicalized array
4179 return Universe::the_empty_instance_klass_array();
4180 } else if (max_transitive_size == super_size) {
4181 // no new local interfaces added, share superklass' transitive interface array
4182 return super->transitive_interfaces();
4183 // The three lines below are commented to work around bug JDK-8245487
4184 // } else if (max_transitive_size == local_size) {
4185 // // only local interfaces added, share local interface array
4186 // return local_ifs;
4187 } else {
4188 ResourceMark rm;
4189 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4190
4191 // Copy down from superclass
4192 if (super != nullptr) {
4193 append_interfaces(result, super->transitive_interfaces());
4194 }
4195
4196 // Copy down from local interfaces' superinterfaces
4197 for (int i = 0; i < local_size; i++) {
4198 InstanceKlass* const l = local_ifs->at(i);
4199 append_interfaces(result, l->transitive_interfaces());
4200 }
4201 // Finally add local interfaces
4202 append_interfaces(result, local_ifs);
4203
4204 // length will be less than the max_transitive_size if duplicates were removed
4205 const int length = result->length();
4206 assert(length <= max_transitive_size, "just checking");
4207
4208 Array<InstanceKlass*>* const new_result =
4209 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4210 for (int i = 0; i < length; i++) {
4211 InstanceKlass* const e = result->at(i);
4212 assert(e != nullptr, "just checking");
4213 new_result->at_put(i, e);
4214 }
4215 return new_result;
4216 }
4217 }
4218
4219 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4220 assert(this_klass != nullptr, "invariant");
4221 const Klass* const super = this_klass->super();
4222
4223 if (super != nullptr) {
4224 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4225
4226 if (super->is_final()) {
4227 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4228 return;
4229 }
4230
4231 if (super_ik->is_sealed()) {
4232 stringStream ss;
4233 ResourceMark rm(THREAD);
4234 if (!super_ik->has_as_permitted_subclass(this_klass, ss)) {
4235 classfile_icce_error(ss.as_string(), THREAD);
4236 return;
4237 }
4238 }
4239
4240 // The JVMS says that super classes for value types must not have the ACC_IDENTITY
4241 // flag set. But, java.lang.Object must still be allowed to be a direct super class
4242 // for a value classes. So, it is treated as a special case for now.
4243 if (!this_klass->access_flags().is_identity_class() &&
4244 super_ik->name() != vmSymbols::java_lang_Object() &&
4245 super_ik->is_identity_class()) {
4246 classfile_icce_error("value class %s cannot inherit from class %s", super_ik, THREAD);
4247 return;
4248 }
4249
4250 Reflection::VerifyClassAccessResults vca_result =
4251 Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false);
4252 if (vca_result != Reflection::ACCESS_OK) {
4253 ResourceMark rm(THREAD);
4254 char* msg = Reflection::verify_class_access_msg(this_klass,
4255 InstanceKlass::cast(super),
4256 vca_result);
4257
4258 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4259 if (msg == nullptr) {
4260 bool same_module = (this_klass->module() == super->module());
4261 Exceptions::fthrow(
4262 THREAD_AND_LOCATION,
4263 vmSymbols::java_lang_IllegalAccessError(),
4264 "class %s cannot access its %ssuperclass %s (%s%s%s)",
4265 this_klass->external_name(),
4266 super->is_abstract() ? "abstract " : "",
4267 super->external_name(),
4268 (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),
4269 (same_module) ? "" : "; ",
4402 const Method* const m = methods->at(index);
4403 // if m is static and not the init method, throw a verify error
4404 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4405 ResourceMark rm(THREAD);
4406
4407 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4408 Exceptions::fthrow(
4409 THREAD_AND_LOCATION,
4410 vmSymbols::java_lang_VerifyError(),
4411 "Illegal static method %s in interface %s",
4412 m->name()->as_C_string(),
4413 this_klass->external_name()
4414 );
4415 return;
4416 }
4417 }
4418 }
4419
4420 // utility methods for format checking
4421
4422 void ClassFileParser::verify_legal_class_modifiers(jint flags, const char* name, bool is_Object, TRAPS) const {
4423 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4424 const bool is_inner_class = name != nullptr;
4425 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4426 if (is_module) {
4427 ResourceMark rm(THREAD);
4428 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4429 Exceptions::fthrow(
4430 THREAD_AND_LOCATION,
4431 vmSymbols::java_lang_NoClassDefFoundError(),
4432 "%s is not a class because access_flag ACC_MODULE is set",
4433 _class_name->as_C_string());
4434 return;
4435 }
4436
4437 if (!_need_verify) { return; }
4438
4439 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4440 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4441 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4442 const bool is_identity = (flags & JVM_ACC_IDENTITY) != 0;
4443 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4444 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4445 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4446 const bool valid_value_class = is_identity || is_interface ||
4447 (supports_inline_types() && (!is_identity && (is_abstract || is_final)));
4448
4449 if ((is_abstract && is_final) ||
4450 (is_interface && !is_abstract) ||
4451 (is_interface && major_gte_1_5 && (is_identity || is_enum)) || // ACC_SUPER (now ACC_IDENTITY) was illegal for interfaces
4452 (!is_interface && major_gte_1_5 && is_annotation) ||
4453 (!valid_value_class)) {
4454 ResourceMark rm(THREAD);
4455 const char* class_note = "";
4456 if (!valid_value_class) {
4457 class_note = " (a value class must be final or else abstract)";
4458 }
4459 if (name == nullptr) { // Not an inner class
4460 Exceptions::fthrow(
4461 THREAD_AND_LOCATION,
4462 vmSymbols::java_lang_ClassFormatError(),
4463 "Illegal class modifiers in class %s%s: 0x%X",
4464 _class_name->as_C_string(), class_note, flags
4465 );
4466 return;
4467 } else {
4468 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4469 Exceptions::fthrow(
4470 THREAD_AND_LOCATION,
4471 vmSymbols::java_lang_ClassFormatError(),
4472 "Illegal class modifiers in declaration of inner class %s%s of class %s: 0x%X",
4473 name, class_note, _class_name->as_C_string(), flags
4474 );
4475 return;
4476 }
4477 }
4478 }
4479
4480 static bool has_illegal_visibility(jint flags) {
4481 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4482 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4483 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4484
4485 return ((is_public && is_protected) ||
4486 (is_public && is_private) ||
4487 (is_protected && is_private));
4488 }
4489
4490 // A legal major_version.minor_version must be one of the following:
4491 //
4492 // Major_version >= 45 and major_version < 56, any minor_version.
4493 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4494 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4495 //
4496 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4524 THREAD_AND_LOCATION,
4525 vmSymbols::java_lang_UnsupportedClassVersionError(),
4526 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4527 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4528 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4529 return;
4530 }
4531
4532 if (!Arguments::enable_preview()) {
4533 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4534 class_name, major, minor, THREAD);
4535 return;
4536 }
4537
4538 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4539 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4540 class_name, major, minor, THREAD);
4541 }
4542 }
4543
4544 void ClassFileParser:: verify_legal_field_modifiers(jint flags,
4545 AccessFlags class_access_flags,
4546 TRAPS) const {
4547 if (!_need_verify) { return; }
4548
4549 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4550 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4551 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4552 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4553 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4554 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4555 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4556 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4557 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4558 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4559
4560 const bool is_interface = class_access_flags.is_interface();
4561 const bool is_identity_class = class_access_flags.is_identity_class();
4562
4563 bool is_illegal = false;
4564 const char* error_msg = "";
4565
4566 // There is some overlap in the checks that apply, for example interface fields
4567 // must be static, static fields can't be strict, and therefore interfaces can't
4568 // have strict fields. So we don't have to check every possible invalid combination
4569 // individually as long as all are covered. Once we have found an illegal combination
4570 // we can stop checking.
4571
4572 if (!is_illegal) {
4573 if (is_interface) {
4574 if (!is_public || !is_static || !is_final || is_private ||
4575 is_protected || is_volatile || is_transient ||
4576 (major_gte_1_5 && is_enum)) {
4577 is_illegal = true;
4578 error_msg = "interface fields must be public, static and final, and may be synthetic";
4579 }
4580 } else { // not interface
4581 if (has_illegal_visibility(flags)) {
4582 is_illegal = true;
4583 error_msg = "invalid visibility flags for class field";
4584 } else if (is_final && is_volatile) {
4585 is_illegal = true;
4586 error_msg = "fields cannot be final and volatile";
4587 } else if (supports_inline_types()) {
4588 if (!is_identity_class && !is_static && (!is_strict || !is_final)) {
4589 is_illegal = true;
4590 error_msg = "value class fields must be either non-static final and strict, or static";
4591 }
4592 }
4593 }
4594 }
4595
4596 if (is_illegal) {
4597 ResourceMark rm(THREAD);
4598 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4599 Exceptions::fthrow(
4600 THREAD_AND_LOCATION,
4601 vmSymbols::java_lang_ClassFormatError(),
4602 "Illegal field modifiers (%s) in class %s: 0x%X",
4603 error_msg, _class_name->as_C_string(), flags);
4604 return;
4605 }
4606 }
4607
4608 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4609 AccessFlags class_access_flags,
4610 const Symbol* name,
4611 TRAPS) const {
4612 if (!_need_verify) { return; }
4613
4614 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4615 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4616 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4617 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4618 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4619 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4620 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4621 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4622 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4623 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4624 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4625 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4626 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4627 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4628 // LW401 CR required: removal of value factories support
4629 const bool is_interface = class_access_flags.is_interface();
4630 const bool is_identity_class = class_access_flags.is_identity_class();
4631 const bool is_abstract_class = class_access_flags.is_abstract();
4632
4633 bool is_illegal = false;
4634
4635 const char* class_note = "";
4636 if (is_interface) {
4637 if (major_gte_8) {
4638 // Class file version is JAVA_8_VERSION or later Methods of
4639 // interfaces may set any of the flags except ACC_PROTECTED,
4640 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4641 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4642 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4643 (is_native || is_protected || is_final || is_synchronized) ||
4644 // If a specific method of a class or interface has its
4645 // ACC_ABSTRACT flag set, it must not have any of its
4646 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4647 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4648 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4649 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4650 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4651 is_illegal = true;
4652 }
4653 } else if (major_gte_1_5) {
4654 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4655 if (!is_public || is_private || is_protected || is_static || is_final ||
4656 is_synchronized || is_native || !is_abstract || is_strict) {
4657 is_illegal = true;
4658 }
4659 } else {
4660 // Class file version is pre-JAVA_1_5_VERSION
4661 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4662 is_illegal = true;
4663 }
4664 }
4665 } else { // not interface
4666 if (has_illegal_visibility(flags)) {
4667 is_illegal = true;
4668 } else {
4669 if (is_initializer) {
4670 if (is_static || is_final || is_synchronized || is_native ||
4671 is_abstract || (major_gte_1_5 && is_bridge)) {
4672 is_illegal = true;
4673 }
4674 } else { // not initializer
4675 if (!is_identity_class && is_synchronized && !is_static) {
4676 is_illegal = true;
4677 class_note = " (not an identity class)";
4678 } else {
4679 if (is_abstract) {
4680 if ((is_final || is_native || is_private || is_static ||
4681 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4682 is_illegal = true;
4683 }
4684 }
4685 }
4686 }
4687 }
4688 }
4689
4690 if (is_illegal) {
4691 ResourceMark rm(THREAD);
4692 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4693 Exceptions::fthrow(
4694 THREAD_AND_LOCATION,
4695 vmSymbols::java_lang_ClassFormatError(),
4696 "Method %s in class %s%s has illegal modifiers: 0x%X",
4697 name->as_C_string(), _class_name->as_C_string(),
4698 class_note, flags);
4699 return;
4700 }
4701 }
4702
4703 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4704 int length,
4705 TRAPS) const {
4706 assert(_need_verify, "only called when _need_verify is true");
4707 // Note: 0 <= length < 64K, as it comes from a u2 entry in the CP.
4708 if (!UTF8::is_legal_utf8(buffer, static_cast<size_t>(length), _major_version <= 47)) {
4709 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4710 }
4711 }
4712
4713 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4714 // In class names, '/' separates unqualified names. This is verified in this function also.
4715 // Method names also may not contain the characters '<' or '>', unless <init>
4716 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4717 // method. Because these names have been checked as special cases before
4718 // calling this method in verify_legal_method_name.
4736 if (type == ClassFileParser::LegalClass) {
4737 if (p == name || p+1 >= name+length ||
4738 *(p+1) == JVM_SIGNATURE_SLASH) {
4739 return false;
4740 }
4741 } else {
4742 return false; // do not permit '/' unless it's class name
4743 }
4744 break;
4745 case JVM_SIGNATURE_SPECIAL:
4746 case JVM_SIGNATURE_ENDSPECIAL:
4747 // do not permit '<' or '>' in method names
4748 if (type == ClassFileParser::LegalMethod) {
4749 return false;
4750 }
4751 }
4752 }
4753 return true;
4754 }
4755
4756 bool ClassFileParser::is_class_in_loadable_descriptors_attribute(Symbol *klass) {
4757 if (_loadable_descriptors == nullptr) return false;
4758 for (int i = 0; i < _loadable_descriptors->length(); i++) {
4759 Symbol* class_name = _cp->symbol_at(_loadable_descriptors->at(i));
4760 if (class_name == klass) return true;
4761 }
4762 return false;
4763 }
4764
4765 // Take pointer to a UTF8 byte string (not NUL-terminated).
4766 // Skip over the longest part of the string that could
4767 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
4768 // Return a pointer to just past the fieldname.
4769 // Return null if no fieldname at all was found, or in the case of slash_ok
4770 // being true, we saw consecutive slashes (meaning we were looking for a
4771 // qualified path but found something that was badly-formed).
4772 static const char* skip_over_field_name(const char* const name,
4773 bool slash_ok,
4774 unsigned int length) {
4775 const char* p;
4776 jboolean last_is_slash = false;
4777 jboolean not_first_ch = false;
4778
4779 for (p = name; p != name + length; not_first_ch = true) {
4780 const char* old_p = p;
4781 jchar ch = *p;
4782 if (ch < 128) {
4783 p++;
4784 // quick check for ascii
4846 // be taken as a field signature. Allow "void" if void_ok.
4847 // Return a pointer to just past the signature.
4848 // Return null if no legal signature is found.
4849 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4850 bool void_ok,
4851 unsigned int length,
4852 TRAPS) const {
4853 unsigned int array_dim = 0;
4854 while (length > 0) {
4855 switch (signature[0]) {
4856 case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
4857 case JVM_SIGNATURE_BOOLEAN:
4858 case JVM_SIGNATURE_BYTE:
4859 case JVM_SIGNATURE_CHAR:
4860 case JVM_SIGNATURE_SHORT:
4861 case JVM_SIGNATURE_INT:
4862 case JVM_SIGNATURE_FLOAT:
4863 case JVM_SIGNATURE_LONG:
4864 case JVM_SIGNATURE_DOUBLE:
4865 return signature + 1;
4866 case JVM_SIGNATURE_CLASS:
4867 {
4868 if (_major_version < JAVA_1_5_VERSION) {
4869 // Skip over the class name if one is there
4870 const char* const p = skip_over_field_name(signature + 1, true, --length);
4871
4872 // The next character better be a semicolon
4873 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4874 return p + 1;
4875 }
4876 }
4877 else {
4878 // Skip leading 'L' or 'Q' and ignore first appearance of ';'
4879 signature++;
4880 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4881 // Format check signature
4882 if (c != nullptr) {
4883 int newlen = pointer_delta_as_int(c, (char*) signature);
4884 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4885 if (!legal) {
4886 classfile_parse_error("Class name is empty or contains illegal character "
4887 "in descriptor in class file %s",
4888 THREAD);
4889 return nullptr;
4890 }
4891 return signature + newlen + 1;
4892 }
4893 }
4894 return nullptr;
4895 }
4896 case JVM_SIGNATURE_ARRAY:
4897 array_dim++;
4898 if (array_dim > 255) {
4914
4915 // Checks if name is a legal class name.
4916 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4917 if (!_need_verify) { return; }
4918
4919 assert(name->refcount() > 0, "symbol must be kept alive");
4920 char* bytes = (char*)name->bytes();
4921 unsigned int length = name->utf8_length();
4922 bool legal = false;
4923
4924 if (length > 0) {
4925 const char* p;
4926 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4927 p = skip_over_field_signature(bytes, false, length, CHECK);
4928 legal = (p != nullptr) && ((p - bytes) == (int)length);
4929 } else if (_major_version < JAVA_1_5_VERSION) {
4930 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4931 p = skip_over_field_name(bytes, true, length);
4932 legal = (p != nullptr) && ((p - bytes) == (int)length);
4933 }
4934 } else if ((_major_version >= CONSTANT_CLASS_DESCRIPTORS || _class_name->starts_with("jdk/internal/reflect/"))
4935 && bytes[length - 1] == ';' ) {
4936 // Support for L...; descriptors
4937 legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
4938 } else {
4939 // 4900761: relax the constraints based on JSR202 spec
4940 // Class names may be drawn from the entire Unicode character set.
4941 // Identifiers between '/' must be unqualified names.
4942 // The utf8 string has been verified when parsing cpool entries.
4943 legal = verify_unqualified_name(bytes, length, LegalClass);
4944 }
4945 }
4946 if (!legal) {
4947 ResourceMark rm(THREAD);
4948 assert(_class_name != nullptr, "invariant");
4949 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4950 Exceptions::fthrow(
4951 THREAD_AND_LOCATION,
4952 vmSymbols::java_lang_ClassFormatError(),
4953 "Illegal class name \"%.*s\" in class file %s", length, bytes,
4954 _class_name->as_C_string()
4955 );
4956 return;
4957 }
4985 THREAD_AND_LOCATION,
4986 vmSymbols::java_lang_ClassFormatError(),
4987 "Illegal field name \"%.*s\" in class %s", length, bytes,
4988 _class_name->as_C_string()
4989 );
4990 return;
4991 }
4992 }
4993
4994 // Checks if name is a legal method name.
4995 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
4996 if (!_need_verify) { return; }
4997
4998 assert(name != nullptr, "method name is null");
4999 char* bytes = (char*)name->bytes();
5000 unsigned int length = name->utf8_length();
5001 bool legal = false;
5002
5003 if (length > 0) {
5004 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
5005 if (name == vmSymbols::object_initializer_name() ||
5006 name == vmSymbols::class_initializer_name()) {
5007 legal = true;
5008 }
5009 } else if (_major_version < JAVA_1_5_VERSION) {
5010 const char* p;
5011 p = skip_over_field_name(bytes, false, length);
5012 legal = (p != nullptr) && ((p - bytes) == (int)length);
5013 } else {
5014 // 4881221: relax the constraints based on JSR202 spec
5015 legal = verify_unqualified_name(bytes, length, LegalMethod);
5016 }
5017 }
5018
5019 if (!legal) {
5020 ResourceMark rm(THREAD);
5021 assert(_class_name != nullptr, "invariant");
5022 // Names are all known to be < 64k so we know this formatted message is not excessively large.
5023 Exceptions::fthrow(
5024 THREAD_AND_LOCATION,
5025 vmSymbols::java_lang_ClassFormatError(),
5026 "Illegal method name \"%.*s\" in class %s", length, bytes,
5027 _class_name->as_C_string()
5028 );
5029 return;
5030 }
5031 }
5032
5033 bool ClassFileParser::legal_field_signature(const Symbol* signature, TRAPS) const {
5034 const char* const bytes = (const char*)signature->bytes();
5035 const unsigned int length = signature->utf8_length();
5036 const char* const p = skip_over_field_signature(bytes, false, length, CHECK_false);
5037
5038 if (p == nullptr || (p - bytes) != (int)length) {
5039 return false;
5040 }
5041 return true;
5042 }
5043
5044 // Checks if signature is a legal field signature.
5045 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5046 const Symbol* signature,
5047 TRAPS) const {
5048 if (!_need_verify) { return; }
5049
5050 const char* const bytes = (const char*)signature->bytes();
5051 const unsigned int length = signature->utf8_length();
5052 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5053
5054 if (p == nullptr || (p - bytes) != (int)length) {
5055 throwIllegalSignature("Field", name, signature, CHECK);
5056 }
5057 }
5058
5059 // Check that the signature is compatible with the method name. For example,
5060 // check that <init> has a void signature.
5061 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5062 const Symbol* signature,
5063 TRAPS) const {
5064 if (!_need_verify) {
5065 return;
5066 }
5067
5068 // Class initializers cannot have args for class format version >= 51.
5069 if (name == vmSymbols::class_initializer_name() &&
5070 signature != vmSymbols::void_method_signature() &&
5071 _major_version >= JAVA_7_VERSION) {
5072 throwIllegalSignature("Method", name, signature, THREAD);
5073 return;
5074 }
5075
5076 int sig_length = signature->utf8_length();
5077 if (name->utf8_length() > 0 &&
5078 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5079 sig_length > 0 &&
5080 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5081 throwIllegalSignature("Method", name, signature, THREAD);
5082 }
5083 }
5084
5085 // Checks if signature is a legal method signature.
5086 // Returns number of parameters
5087 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5088 const Symbol* signature,
5089 TRAPS) const {
5090 if (!_need_verify) {
5091 // make sure caller's args_size will be less than 0 even for non-static
5092 // method so it will be recomputed in compute_size_of_parameters().
5093 return -2;
5094 }
5095
5096 unsigned int args_size = 0;
5097 const char* p = (const char*)signature->bytes();
5098 unsigned int length = signature->utf8_length();
5099 const char* nextp;
5100
5111 length -= pointer_delta_as_int(nextp, p);
5112 p = nextp;
5113 nextp = skip_over_field_signature(p, false, length, CHECK_0);
5114 }
5115 // The first non-signature thing better be a ')'
5116 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
5117 length--;
5118 // Now we better just have a return value
5119 nextp = skip_over_field_signature(p, true, length, CHECK_0);
5120 if (nextp && ((int)length == (nextp - p))) {
5121 return args_size;
5122 }
5123 }
5124 }
5125 // Report error
5126 throwIllegalSignature("Method", name, signature, THREAD);
5127 return 0;
5128 }
5129
5130 int ClassFileParser::static_field_size() const {
5131 assert(_layout_info != nullptr, "invariant");
5132 return _layout_info->_static_field_size;
5133 }
5134
5135 int ClassFileParser::total_oop_map_count() const {
5136 assert(_layout_info != nullptr, "invariant");
5137 return _layout_info->oop_map_blocks->_nonstatic_oop_map_count;
5138 }
5139
5140 jint ClassFileParser::layout_size() const {
5141 assert(_layout_info != nullptr, "invariant");
5142 return _layout_info->_instance_size;
5143 }
5144
5145 static void check_methods_for_intrinsics(const InstanceKlass* ik,
5146 const Array<Method*>* methods) {
5147 assert(ik != nullptr, "invariant");
5148 assert(methods != nullptr, "invariant");
5149
5150 // Set up Method*::intrinsic_id as soon as we know the names of methods.
5151 // (We used to do this lazily, but now we query it in Rewriter,
5152 // which is eagerly done for every method, so we might as well do it now,
5153 // when everything is fresh in memory.)
5154 const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);
5155
5156 if (klass_id != vmSymbolID::NO_SID) {
5157 for (int j = 0; j < methods->length(); ++j) {
5158 Method* method = methods->at(j);
5159 method->init_intrinsic_id(klass_id);
5160
5161 if (CheckIntrinsics) {
5162 // Check if an intrinsic is defined for method 'method',
5237 }
5238 }
5239
5240 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5241 const ClassInstanceInfo& cl_inst_info,
5242 TRAPS) {
5243 if (_klass != nullptr) {
5244 return _klass;
5245 }
5246
5247 InstanceKlass* const ik =
5248 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5249
5250 if (is_hidden()) {
5251 mangle_hidden_class_name(ik);
5252 }
5253
5254 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5255
5256 assert(_klass == ik, "invariant");
5257 return ik;
5258 }
5259
5260 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5261 bool changed_by_loadhook,
5262 const ClassInstanceInfo& cl_inst_info,
5263 TRAPS) {
5264 assert(ik != nullptr, "invariant");
5265
5266 // Set name and CLD before adding to CLD
5267 ik->set_class_loader_data(_loader_data);
5268 ik->set_name(_class_name);
5269
5270 // Add all classes to our internal class loader list here,
5271 // including classes in the bootstrap (null) class loader.
5272 const bool publicize = !is_internal();
5273
5274 _loader_data->add_class(ik, publicize);
5275
5276 set_klass_to_deallocate(ik);
5277
5278 assert(_layout_info != nullptr, "invariant");
5279 assert(ik->static_field_size() == _layout_info->_static_field_size, "sanity");
5280 assert(ik->nonstatic_oop_map_count() == _layout_info->oop_map_blocks->_nonstatic_oop_map_count,
5281 "sanity");
5282
5283 assert(ik->is_instance_klass(), "sanity");
5284 assert(ik->size_helper() == _layout_info->_instance_size, "sanity");
5285
5286 // Fill in information already parsed
5287 ik->set_should_verify_class(_need_verify);
5288
5289 // Not yet: supers are done below to support the new subtype-checking fields
5290 ik->set_nonstatic_field_size(_layout_info->_nonstatic_field_size);
5291 ik->set_has_nonstatic_fields(_layout_info->_has_nonstatic_fields);
5292
5293 if (_layout_info->_is_naturally_atomic) {
5294 ik->set_is_naturally_atomic();
5295 }
5296
5297 if (_layout_info->_must_be_atomic) {
5298 ik->set_must_be_atomic();
5299 }
5300
5301 ik->set_static_oop_field_count(_static_oop_count);
5302
5303 // this transfers ownership of a lot of arrays from
5304 // the parser onto the InstanceKlass*
5305 apply_parsed_class_metadata(ik, _java_fields_count);
5306 if (ik->is_inline_klass()) {
5307 InlineKlass::cast(ik)->init_fixed_block();
5308 }
5309
5310 // can only set dynamic nest-host after static nest information is set
5311 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5312 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5313 }
5314
5315 // note that is not safe to use the fields in the parser from this point on
5316 assert(nullptr == _cp, "invariant");
5317 assert(nullptr == _fieldinfo_stream, "invariant");
5318 assert(nullptr == _fields_status, "invariant");
5319 assert(nullptr == _methods, "invariant");
5320 assert(nullptr == _inner_classes, "invariant");
5321 assert(nullptr == _nest_members, "invariant");
5322 assert(nullptr == _loadable_descriptors, "invariant");
5323 assert(nullptr == _combined_annotations, "invariant");
5324 assert(nullptr == _record_components, "invariant");
5325 assert(nullptr == _permitted_subclasses, "invariant");
5326 assert(nullptr == _inline_layout_info_array, "invariant");
5327
5328 if (_has_localvariable_table) {
5329 ik->set_has_localvariable_table(true);
5330 }
5331
5332 if (_has_final_method) {
5333 ik->set_has_final_method();
5334 }
5335
5336 ik->copy_method_ordering(_method_ordering, CHECK);
5337 // The InstanceKlass::_methods_jmethod_ids cache
5338 // is managed on the assumption that the initial cache
5339 // size is equal to the number of methods in the class. If
5340 // that changes, then InstanceKlass::idnum_can_increment()
5341 // has to be changed accordingly.
5342 ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5343
5344 ik->set_this_class_index(_this_class_index);
5345
5346 if (_is_hidden) {
5384 if ((_num_miranda_methods > 0) ||
5385 // if this class introduced new miranda methods or
5386 (_super_klass != nullptr && _super_klass->has_miranda_methods())
5387 // super class exists and this class inherited miranda methods
5388 ) {
5389 ik->set_has_miranda_methods(); // then set a flag
5390 }
5391
5392 // Fill in information needed to compute superclasses.
5393 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5394 ik->set_transitive_interfaces(_transitive_interfaces);
5395 ik->set_local_interfaces(_local_interfaces);
5396 _transitive_interfaces = nullptr;
5397 _local_interfaces = nullptr;
5398
5399 // Initialize itable offset tables
5400 klassItable::setup_itable_offset_table(ik);
5401
5402 // Compute transitive closure of interfaces this class implements
5403 // Do final class setup
5404 OopMapBlocksBuilder* oop_map_blocks = _layout_info->oop_map_blocks;
5405 if (oop_map_blocks->_nonstatic_oop_map_count > 0) {
5406 oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5407 }
5408
5409 if (_has_contended_fields || _parsed_annotations->is_contended() ||
5410 ( _super_klass != nullptr && _super_klass->has_contended_annotations())) {
5411 ik->set_has_contended_annotations(true);
5412 }
5413
5414 // Fill in has_finalizer and layout_helper
5415 set_precomputed_flags(ik);
5416
5417 // check if this class can access its super class
5418 check_super_class_access(ik, CHECK);
5419
5420 // check if this class can access its superinterfaces
5421 check_super_interface_access(ik, CHECK);
5422
5423 // check if this class overrides any final method
5424 check_final_method_override(ik, CHECK);
5445
5446 assert(_all_mirandas != nullptr, "invariant");
5447
5448 // Generate any default methods - default methods are public interface methods
5449 // that have a default implementation. This is new with Java 8.
5450 if (_has_nonstatic_concrete_methods) {
5451 DefaultMethods::generate_default_methods(ik,
5452 _all_mirandas,
5453 CHECK);
5454 }
5455
5456 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5457 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5458 !module_entry->has_default_read_edges()) {
5459 if (!module_entry->set_has_default_read_edges()) {
5460 // We won a potential race
5461 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5462 }
5463 }
5464
5465 if (is_inline_type()) {
5466 InlineKlass* vk = InlineKlass::cast(ik);
5467 vk->set_payload_alignment(_layout_info->_payload_alignment);
5468 vk->set_payload_offset(_layout_info->_payload_offset);
5469 vk->set_payload_size_in_bytes(_layout_info->_payload_size_in_bytes);
5470 vk->set_non_atomic_size_in_bytes(_layout_info->_non_atomic_size_in_bytes);
5471 vk->set_non_atomic_alignment(_layout_info->_non_atomic_alignment);
5472 vk->set_atomic_size_in_bytes(_layout_info->_atomic_layout_size_in_bytes);
5473 vk->set_nullable_size_in_bytes(_layout_info->_nullable_layout_size_in_bytes);
5474 vk->set_null_marker_offset(_layout_info->_null_marker_offset);
5475 vk->set_null_reset_value_offset(_layout_info->_null_reset_value_offset);
5476 if (_layout_info->_is_empty_inline_klass) vk->set_is_empty_inline_type();
5477 vk->initialize_calling_convention(CHECK);
5478 }
5479
5480 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5481
5482 if (!is_internal()) {
5483 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5484
5485 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5486 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5487 log_is_enabled(Info, class, preview)) {
5488 ResourceMark rm;
5489 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5490 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5491 }
5492
5493 if (log_is_enabled(Debug, class, resolve)) {
5494 ResourceMark rm;
5495 // print out the superclass.
5496 const char * from = ik->external_name();
5497 if (ik->java_super() != nullptr) {
5498 log_debug(class, resolve)("%s %s (super)",
5499 from,
5541 ClassLoaderData* loader_data,
5542 const ClassLoadInfo* cl_info,
5543 Publicity pub_level,
5544 TRAPS) :
5545 _stream(stream),
5546 _class_name(nullptr),
5547 _loader_data(loader_data),
5548 _is_hidden(cl_info->is_hidden()),
5549 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5550 _orig_cp_size(0),
5551 _static_oop_count(0),
5552 _super_klass(),
5553 _cp(nullptr),
5554 _fieldinfo_stream(nullptr),
5555 _fields_status(nullptr),
5556 _methods(nullptr),
5557 _inner_classes(nullptr),
5558 _nest_members(nullptr),
5559 _nest_host(0),
5560 _permitted_subclasses(nullptr),
5561 _loadable_descriptors(nullptr),
5562 _record_components(nullptr),
5563 _local_interfaces(nullptr),
5564 _local_interface_indexes(nullptr),
5565 _transitive_interfaces(nullptr),
5566 _combined_annotations(nullptr),
5567 _class_annotations(nullptr),
5568 _class_type_annotations(nullptr),
5569 _fields_annotations(nullptr),
5570 _fields_type_annotations(nullptr),
5571 _klass(nullptr),
5572 _klass_to_deallocate(nullptr),
5573 _parsed_annotations(nullptr),
5574 _layout_info(nullptr),
5575 _inline_layout_info_array(nullptr),
5576 _temp_field_info(nullptr),
5577 _method_ordering(nullptr),
5578 _all_mirandas(nullptr),
5579 _vtable_size(0),
5580 _itable_size(0),
5581 _num_miranda_methods(0),
5582 _protection_domain(cl_info->protection_domain()),
5583 _access_flags(),
5584 _pub_level(pub_level),
5585 _bad_constant_seen(0),
5586 _synthetic_flag(false),
5587 _sde_length(false),
5588 _sde_buffer(nullptr),
5589 _sourcefile_index(0),
5590 _generic_signature_index(0),
5591 _major_version(0),
5592 _minor_version(0),
5593 _this_class_index(0),
5594 _super_class_index(0),
5595 _itfs_len(0),
5596 _java_fields_count(0),
5597 _need_verify(false),
5598 _has_nonstatic_concrete_methods(false),
5599 _declares_nonstatic_concrete_methods(false),
5600 _has_localvariable_table(false),
5601 _has_final_method(false),
5602 _has_contended_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 _need_verify = Verifier::should_verify_for(_loader_data->class_loader());
5623
5624 // synch back verification state to stream to check for truncation.
5625 stream->set_need_verify(_need_verify);
5626
5627 parse_stream(stream, CHECK);
5628
5629 post_process_parsed_stream(stream, _cp, CHECK);
5630 }
5631
5632 void ClassFileParser::clear_class_metadata() {
5633 // metadata created before the instance klass is created. Must be
5634 // deallocated if classfile parsing returns an error.
5635 _cp = nullptr;
5636 _fieldinfo_stream = nullptr;
5637 _fields_status = nullptr;
5638 _methods = nullptr;
5639 _inner_classes = nullptr;
5640 _nest_members = nullptr;
5641 _permitted_subclasses = nullptr;
5642 _loadable_descriptors = nullptr;
5643 _combined_annotations = nullptr;
5644 _class_annotations = _class_type_annotations = nullptr;
5645 _fields_annotations = _fields_type_annotations = nullptr;
5646 _record_components = nullptr;
5647 _inline_layout_info_array = nullptr;
5648 }
5649
5650 // Destructor to clean up
5651 ClassFileParser::~ClassFileParser() {
5652 _class_name->decrement_refcount();
5653
5654 if (_cp != nullptr) {
5655 MetadataFactory::free_metadata(_loader_data, _cp);
5656 }
5657
5658 if (_fieldinfo_stream != nullptr) {
5659 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5660 }
5661
5662 if (_fields_status != nullptr) {
5663 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5664 }
5665
5666 if (_inline_layout_info_array != nullptr) {
5667 MetadataFactory::free_array<InlineLayoutInfo>(_loader_data, _inline_layout_info_array);
5668 }
5669
5670 if (_methods != nullptr) {
5671 // Free methods
5672 InstanceKlass::deallocate_methods(_loader_data, _methods);
5673 }
5674
5675 // beware of the Universe::empty_blah_array!!
5676 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5677 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5678 }
5679
5680 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5681 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5682 }
5683
5684 if (_record_components != nullptr) {
5685 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5686 }
5687
5688 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5689 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5690 }
5691
5692 if (_loadable_descriptors != nullptr && _loadable_descriptors != Universe::the_empty_short_array()) {
5693 MetadataFactory::free_array<u2>(_loader_data, _loadable_descriptors);
5694 }
5695
5696 // Free interfaces
5697 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5698 _local_interfaces, _transitive_interfaces);
5699
5700 if (_combined_annotations != nullptr) {
5701 // After all annotations arrays have been created, they are installed into the
5702 // Annotations object that will be assigned to the InstanceKlass being created.
5703
5704 // Deallocate the Annotations object and the installed annotations arrays.
5705 _combined_annotations->deallocate_contents(_loader_data);
5706
5707 // If the _combined_annotations pointer is non-null,
5708 // then the other annotations fields should have been cleared.
5709 assert(_class_annotations == nullptr, "Should have been cleared");
5710 assert(_class_type_annotations == nullptr, "Should have been cleared");
5711 assert(_fields_annotations == nullptr, "Should have been cleared");
5712 assert(_fields_type_annotations == nullptr, "Should have been cleared");
5713 } else {
5714 // If the annotations arrays were not installed into the Annotations object,
5715 // then they have to be deallocated explicitly.
5760 cp_size, CHECK);
5761
5762 _orig_cp_size = cp_size;
5763 if (is_hidden()) { // Add a slot for hidden class name.
5764 cp_size++;
5765 }
5766
5767 _cp = ConstantPool::allocate(_loader_data,
5768 cp_size,
5769 CHECK);
5770
5771 ConstantPool* const cp = _cp;
5772
5773 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
5774
5775 assert(cp_size == (u2)cp->length(), "invariant");
5776
5777 // ACCESS FLAGS
5778 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5779
5780 u2 recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS;
5781 // JVM_ACC_MODULE is defined in JDK-9 and later.
5782 if (_major_version >= JAVA_9_VERSION) {
5783 recognized_modifiers |= JVM_ACC_MODULE;
5784 }
5785
5786 // Access flags
5787 u2 flags = stream->get_u2_fast() & recognized_modifiers;
5788
5789 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5790 // Set abstract bit for old class files for backward compatibility
5791 flags |= JVM_ACC_ABSTRACT;
5792 }
5793
5794 // Fixing ACC_SUPER/ACC_IDENTITY for old class files
5795 if (!supports_inline_types()) {
5796 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
5797 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
5798 if (!is_module && !is_interface) {
5799 flags |= JVM_ACC_IDENTITY;
5800 }
5801 }
5802
5803
5804 // This class and superclass
5805 _this_class_index = stream->get_u2_fast();
5806 guarantee_property(
5807 valid_cp_range(_this_class_index, cp_size) &&
5808 cp->tag_at(_this_class_index).is_unresolved_klass(),
5809 "Invalid this class index %u in constant pool in class file %s",
5810 _this_class_index, CHECK);
5811
5812 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
5813 assert(class_name_in_cp != nullptr, "class_name can't be null");
5814
5815 bool is_java_lang_Object = class_name_in_cp == vmSymbols::java_lang_Object();
5816
5817 verify_legal_class_modifiers(flags, nullptr, is_java_lang_Object, CHECK);
5818
5819 _access_flags.set_flags(flags);
5820
5821 short bad_constant = class_bad_constant_seen();
5822 if (bad_constant != 0) {
5823 // Do not throw CFE until after the access_flags are checked because if
5824 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5825 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5826 return;
5827 }
5828
5829 // Don't need to check whether this class name is legal or not.
5830 // It has been checked when constant pool is parsed.
5831 // However, make sure it is not an array type.
5832 if (_need_verify) {
5833 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
5834 "Bad class name in class file %s",
5835 CHECK);
5836 }
5837
5838 #ifdef ASSERT
5839 // Basic sanity checks
5840 if (_is_hidden) {
5841 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
5842 }
5843 #endif
5844
5845 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
5846
5847 if (_is_hidden) {
5848 assert(_class_name != nullptr, "Unexpected null _class_name");
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(), err_msg("Class %s cannot have a null-free non-static field of its own type", _class_name->as_C_string()));
6167 }
6168 log_info(class, preload)("Preloading class %s during loading of class %s. Cause: a null-free non-static field is declared with this type", s->as_C_string(), _class_name->as_C_string());
6169 Klass* klass = SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name, s, Handle(THREAD, _loader_data->class_loader()), false, THREAD);
6170 if (HAS_PENDING_EXCEPTION) {
6171 log_warning(class, preload)("Preloading of class %s during loading of class %s (cause: null-free non-static field) failed: %s",
6172 s->as_C_string(), _class_name->as_C_string(), PENDING_EXCEPTION->klass()->name()->as_C_string());
6173 return; // Exception is still pending
6174 }
6175 assert(klass != nullptr, "Sanity check");
6176 if (klass->access_flags().is_identity_class()) {
6177 assert(klass->is_instance_klass(), "Sanity check");
6178 ResourceMark rm(THREAD);
6179 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6180 err_msg("Class %s expects class %s to be a value class, but it is an identity class",
6181 _class_name->as_C_string(),
6182 InstanceKlass::cast(klass)->external_name()));
6183 }
6184 if (klass->is_abstract()) {
6185 assert(klass->is_instance_klass(), "Sanity check");
6186 ResourceMark rm(THREAD);
6187 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6188 err_msg("Class %s expects class %s to be concrete value type, but it is an abstract class",
6189 _class_name->as_C_string(),
6190 InstanceKlass::cast(klass)->external_name()));
6191 }
6192 InlineKlass* vk = InlineKlass::cast(klass);
6193 _inline_layout_info_array->adr_at(fieldinfo.index())->set_klass(vk);
6194 log_info(class, preload)("Preloading of class %s during loading of class %s (cause: null-free non-static field) succeeded", s->as_C_string(), _class_name->as_C_string());
6195 } else if (Signature::has_envelope(sig)) {
6196 // Preloading classes for nullable fields that are listed in the LoadableDescriptors attribute
6197 // Those classes would be required later for the flattening of nullable inline type fields
6198 TempNewSymbol name = Signature::strip_envelope(sig);
6199 if (name != _class_name && is_class_in_loadable_descriptors_attribute(sig)) {
6200 log_info(class, preload)("Preloading class %s during loading of class %s. Cause: field type in LoadableDescriptors attribute", name->as_C_string(), _class_name->as_C_string());
6201 oop loader = loader_data()->class_loader();
6202 Klass* klass = SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name, name, Handle(THREAD, loader), false, THREAD);
6203 if (klass != nullptr) {
6204 if (klass->is_inline_klass()) {
6205 _inline_layout_info_array->adr_at(fieldinfo.index())->set_klass(InlineKlass::cast(klass));
6206 log_info(class, preload)("Preloading of class %s during loading of class %s (cause: field type in LoadableDescriptors attribute) succeeded", name->as_C_string(), _class_name->as_C_string());
6207 } else {
6208 // Non value class are allowed by the current spec, but it could be an indication of an issue so let's log a warning
6209 log_warning(class, preload)("Preloading class %s during loading of class %s (cause: field type in LoadableDescriptors attribute) but loaded class is not a value class", name->as_C_string(), _class_name->as_C_string());
6210 }
6211 } else {
6212 log_warning(class, preload)("Preloading of class %s during loading of class %s (cause: field type in LoadableDescriptors attribute) failed : %s",
6213 name->as_C_string(), _class_name->as_C_string(), PENDING_EXCEPTION->klass()->name()->as_C_string());
6214 }
6215 // Loads triggered by the LoadableDescriptors attribute are speculative, failures must not impact loading of current class
6216 if (HAS_PENDING_EXCEPTION) {
6217 CLEAR_PENDING_EXCEPTION;
6218 }
6219 }
6220 }
6221 }
6222 }
6223
6224 _layout_info = new FieldLayoutInfo();
6225 FieldLayoutBuilder lb(class_name(), loader_data(), super_klass(), _cp, /*_fields*/ _temp_field_info,
6226 _parsed_annotations->is_contended(), is_inline_type(),
6227 access_flags().is_abstract() && !access_flags().is_identity_class() && !access_flags().is_interface(),
6228 _must_be_atomic, _layout_info, _inline_layout_info_array);
6229 lb.build_layout();
6230 _has_inline_type_fields = _layout_info->_has_inline_fields;
6231
6232 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
6233 _fieldinfo_stream =
6234 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
6235 injected_fields_count, loader_data(), CHECK);
6236
6237 _fields_status =
6238 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
6239 FieldStatus(0), CHECK);
6240 }
6241
6242 void ClassFileParser::set_klass(InstanceKlass* klass) {
6243
6244 #ifdef ASSERT
6245 if (klass != nullptr) {
6246 assert(nullptr == _klass, "leaking?");
6247 }
6248 #endif
6249
6250 _klass = klass;
6251 }
6252
6253 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6254
6255 #ifdef ASSERT
6256 if (klass != nullptr) {
|