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