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: {
495 check_property(valid_symbol_at(name_ref_index),
496 "Invalid constant pool index %u in class file %s",
497 name_ref_index, CHECK);
498 check_property(valid_symbol_at(signature_ref_index),
499 "Invalid constant pool index %u in class file %s",
500 signature_ref_index, CHECK);
501 break;
502 }
503 case JVM_CONSTANT_Utf8:
504 break;
505 case JVM_CONSTANT_UnresolvedClass: // fall-through
506 case JVM_CONSTANT_UnresolvedClassInError: {
507 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
508 break;
509 }
510 case JVM_CONSTANT_ClassIndex: {
511 const int class_index = cp->klass_index_at(index);
512 check_property(valid_symbol_at(class_index),
513 "Invalid constant pool index %u in class file %s",
514 class_index, CHECK);
515 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
516 break;
517 }
518 case JVM_CONSTANT_StringIndex: {
519 const int string_index = cp->string_index_at(index);
520 check_property(valid_symbol_at(string_index),
521 "Invalid constant pool index %u in class file %s",
522 string_index, CHECK);
523 Symbol* const sym = cp->symbol_at(string_index);
524 cp->unresolved_string_at_put(index, sym);
525 break;
526 }
527 case JVM_CONSTANT_MethodHandle: {
528 const int ref_index = cp->method_handle_index_at(index);
529 check_property(valid_cp_range(ref_index, length),
530 "Invalid constant pool index %u in class file %s",
531 ref_index, CHECK);
532 const constantTag tag = cp->tag_at(ref_index);
533 const int ref_kind = cp->method_handle_ref_kind_at(index);
534
704 }
705 }
706 } else {
707 if (_need_verify) {
708 // Method name and signature are individually verified above, when iterating
709 // NameAndType_info. Need to check here that signature is non-zero length and
710 // the right type.
711 if (!Signature::is_method(signature)) {
712 throwIllegalSignature("Method", name, signature, CHECK);
713 }
714 }
715 // If a class method name begins with '<', it must be "<init>" and have void signature.
716 const unsigned int name_len = name->utf8_length();
717 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
718 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
719 if (name != vmSymbols::object_initializer_name()) {
720 classfile_parse_error(
721 "Bad method name at constant pool index %u in class file %s",
722 name_ref_index, THREAD);
723 return;
724 } else if (!Signature::is_void_method(signature)) { // must have void signature.
725 throwIllegalSignature("Method", name, signature, CHECK);
726 }
727 }
728 }
729 break;
730 }
731 case JVM_CONSTANT_MethodHandle: {
732 const int ref_index = cp->method_handle_index_at(index);
733 const int ref_kind = cp->method_handle_ref_kind_at(index);
734 switch (ref_kind) {
735 case JVM_REF_invokeVirtual:
736 case JVM_REF_invokeStatic:
737 case JVM_REF_invokeSpecial:
738 case JVM_REF_newInvokeSpecial: {
739 const int name_and_type_ref_index =
740 cp->uncached_name_and_type_ref_index_at(ref_index);
741 const int name_ref_index =
742 cp->name_ref_index_at(name_and_type_ref_index);
743 const Symbol* const name = cp->symbol_at(name_ref_index);
744 if (ref_kind == JVM_REF_newInvokeSpecial) {
745 if (name != vmSymbols::object_initializer_name()) {
746 classfile_parse_error(
747 "Bad constructor name at constant pool index %u in class file %s",
748 name_ref_index, THREAD);
749 return;
750 }
751 } else {
752 if (name == vmSymbols::object_initializer_name()) {
753 classfile_parse_error(
754 "Bad method name at constant pool index %u in class file %s",
755 name_ref_index, THREAD);
756 return;
757 }
758 }
759 break;
760 }
761 // Other ref_kinds are already fully checked in previous pass.
762 } // switch(ref_kind)
763 break;
764 }
765 case JVM_CONSTANT_MethodType: {
766 const Symbol* const no_name = vmSymbols::type_name(); // place holder
767 const Symbol* const signature = cp->method_type_signature_at(index);
768 verify_legal_method_signature(no_name, signature, CHECK);
769 break;
770 }
771 case JVM_CONSTANT_Utf8: {
772 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
784
785 NameSigHash(Symbol* name, Symbol* sig) :
786 _name(name),
787 _sig(sig) {}
788
789 static unsigned int hash(NameSigHash const& namesig) {
790 return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
791 }
792
793 static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
794 return (e0._name == e1._name) &&
795 (e0._sig == e1._sig);
796 }
797 };
798
799 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
800 NameSigHash::HASH_ROW_SIZE,
801 AnyObj::RESOURCE_AREA, mtInternal,
802 &NameSigHash::hash, &NameSigHash::equals>;
803
804 // Side-effects: populates the _local_interfaces field
805 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
806 const int itfs_len,
807 ConstantPool* const cp,
808 bool* const has_nonstatic_concrete_methods,
809 TRAPS) {
810 assert(stream != nullptr, "invariant");
811 assert(cp != nullptr, "invariant");
812 assert(has_nonstatic_concrete_methods != nullptr, "invariant");
813
814 if (itfs_len == 0) {
815 _local_interfaces = Universe::the_empty_instance_klass_array();
816 } else {
817 assert(itfs_len > 0, "only called for len>0");
818 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK);
819
820 int index;
821 for (index = 0; index < itfs_len; index++) {
822 const u2 interface_index = stream->get_u2(CHECK);
823 Klass* interf;
824 check_property(
825 valid_klass_reference_at(interface_index),
826 "Interface name has bad constant pool index %u in class file %s",
827 interface_index, CHECK);
828 if (cp->tag_at(interface_index).is_klass()) {
829 interf = cp->resolved_klass_at(interface_index);
830 } else {
831 Symbol* const unresolved_klass = cp->klass_name_at(interface_index);
832
833 // Don't need to check legal name because it's checked when parsing constant pool.
834 // But need to make sure it's not an array type.
835 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
836 "Bad interface name in class file %s", CHECK);
837
838 // Call resolve_super so class circularity is checked
839 interf = SystemDictionary::resolve_super_or_fail(
840 _class_name,
841 unresolved_klass,
842 Handle(THREAD, _loader_data->class_loader()),
843 _protection_domain,
844 false,
845 CHECK);
846 }
847
848 if (!interf->is_interface()) {
849 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
850 err_msg("class %s can not implement %s, because it is not an interface (%s)",
851 _class_name->as_klass_external_name(),
852 interf->external_name(),
853 interf->class_in_module_of_loader()));
854 }
855
856 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
857 *has_nonstatic_concrete_methods = true;
858 }
859 _local_interfaces->at_put(index, InstanceKlass::cast(interf));
860 }
861
862 if (!_need_verify || itfs_len <= 1) {
863 return;
864 }
865
866 // Check if there's any duplicates in interfaces
867 ResourceMark rm(THREAD);
868 // Set containing interface names
869 ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
870 for (index = 0; index < itfs_len; index++) {
871 const InstanceKlass* const k = _local_interfaces->at(index);
872 Symbol* interface_name = k->name();
873 // If no duplicates, add (name, nullptr) in hashtable interface_names.
874 if (!interface_names->put(interface_name, 0)) {
875 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
876 interface_name->as_C_string(), THREAD);
877 return;
878 }
879 }
880 }
881 }
882
883 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
884 int constantvalue_index,
885 int signature_index,
886 TRAPS) const {
887 // Make sure the constant pool entry is of a type appropriate to this field
888 guarantee_property(
889 (constantvalue_index > 0 &&
890 constantvalue_index < cp->length()),
891 "Bad initial value index %u in ConstantValue attribute in class file %s",
892 constantvalue_index, CHECK);
938
939 class AnnotationCollector : public ResourceObj{
940 public:
941 enum Location { _in_field, _in_method, _in_class };
942 enum ID {
943 _unknown = 0,
944 _method_CallerSensitive,
945 _method_ForceInline,
946 _method_DontInline,
947 _method_ChangesCurrentThread,
948 _method_JvmtiMountTransition,
949 _method_InjectedProfile,
950 _method_LambdaForm_Compiled,
951 _method_Hidden,
952 _method_Scoped,
953 _method_IntrinsicCandidate,
954 _jdk_internal_vm_annotation_Contended,
955 _field_Stable,
956 _jdk_internal_vm_annotation_ReservedStackAccess,
957 _jdk_internal_ValueBased,
958 _java_lang_Deprecated,
959 _java_lang_Deprecated_for_removal,
960 _annotation_LIMIT
961 };
962 const Location _location;
963 int _annotations_present;
964 u2 _contended_group;
965
966 AnnotationCollector(Location location)
967 : _location(location), _annotations_present(0), _contended_group(0)
968 {
969 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
970 }
971 // If this annotation name has an ID, report it (or _none).
972 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
973 // Set the annotation name:
974 void set_annotation(ID id) {
975 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
976 _annotations_present |= (int)nth_bit((int)id);
977 }
1363 CHECK);
1364 parsed_annotations->set_field_annotations(a);
1365 a = assemble_annotations(runtime_visible_type_annotations,
1366 runtime_visible_type_annotations_length,
1367 runtime_invisible_type_annotations,
1368 runtime_invisible_type_annotations_length,
1369 CHECK);
1370 parsed_annotations->set_field_type_annotations(a);
1371 return;
1372 }
1373
1374
1375 // Field allocation types. Used for computing field offsets.
1376
1377 enum FieldAllocationType {
1378 STATIC_OOP, // Oops
1379 STATIC_BYTE, // Boolean, Byte, char
1380 STATIC_SHORT, // shorts
1381 STATIC_WORD, // ints
1382 STATIC_DOUBLE, // aligned long or double
1383 NONSTATIC_OOP,
1384 NONSTATIC_BYTE,
1385 NONSTATIC_SHORT,
1386 NONSTATIC_WORD,
1387 NONSTATIC_DOUBLE,
1388 MAX_FIELD_ALLOCATION_TYPE,
1389 BAD_ALLOCATION_TYPE = -1
1390 };
1391
1392 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1393 BAD_ALLOCATION_TYPE, // 0
1394 BAD_ALLOCATION_TYPE, // 1
1395 BAD_ALLOCATION_TYPE, // 2
1396 BAD_ALLOCATION_TYPE, // 3
1397 NONSTATIC_BYTE , // T_BOOLEAN = 4,
1398 NONSTATIC_SHORT, // T_CHAR = 5,
1399 NONSTATIC_WORD, // T_FLOAT = 6,
1400 NONSTATIC_DOUBLE, // T_DOUBLE = 7,
1401 NONSTATIC_BYTE, // T_BYTE = 8,
1402 NONSTATIC_SHORT, // T_SHORT = 9,
1403 NONSTATIC_WORD, // T_INT = 10,
1404 NONSTATIC_DOUBLE, // T_LONG = 11,
1405 NONSTATIC_OOP, // T_OBJECT = 12,
1406 NONSTATIC_OOP, // T_ARRAY = 13,
1407 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1408 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1409 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1410 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1411 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1412 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1413 BAD_ALLOCATION_TYPE, // 0
1414 BAD_ALLOCATION_TYPE, // 1
1415 BAD_ALLOCATION_TYPE, // 2
1416 BAD_ALLOCATION_TYPE, // 3
1417 STATIC_BYTE , // T_BOOLEAN = 4,
1418 STATIC_SHORT, // T_CHAR = 5,
1419 STATIC_WORD, // T_FLOAT = 6,
1420 STATIC_DOUBLE, // T_DOUBLE = 7,
1421 STATIC_BYTE, // T_BYTE = 8,
1422 STATIC_SHORT, // T_SHORT = 9,
1423 STATIC_WORD, // T_INT = 10,
1424 STATIC_DOUBLE, // T_LONG = 11,
1425 STATIC_OOP, // T_OBJECT = 12,
1426 STATIC_OOP, // T_ARRAY = 13,
1427 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1428 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1429 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1430 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1431 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1432 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1433 };
1434
1435 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {
1436 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1437 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1438 assert(result != BAD_ALLOCATION_TYPE, "bad type");
1439 return result;
1440 }
1441
1442 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1443 public:
1444 u2 count[MAX_FIELD_ALLOCATION_TYPE];
1445
1446 FieldAllocationCount() {
1447 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1448 count[i] = 0;
1449 }
1450 }
1451
1452 void update(bool is_static, BasicType type) {
1453 FieldAllocationType atype = basic_type_to_atype(is_static, type);
1454 if (atype != BAD_ALLOCATION_TYPE) {
1455 // Make sure there is no overflow with injected fields.
1456 assert(count[atype] < 0xFFFF, "More than 65535 fields");
1457 count[atype]++;
1458 }
1459 }
1460 };
1461
1462 // Side-effects: populates the _fields, _fields_annotations,
1463 // _fields_type_annotations fields
1464 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1465 bool is_interface,
1466 FieldAllocationCount* const fac,
1467 ConstantPool* cp,
1468 const int cp_size,
1469 u2* const java_fields_count_ptr,
1470 TRAPS) {
1471
1472 assert(cfs != nullptr, "invariant");
1473 assert(fac != nullptr, "invariant");
1474 assert(cp != nullptr, "invariant");
1475 assert(java_fields_count_ptr != nullptr, "invariant");
1476
1477 assert(nullptr == _fields_annotations, "invariant");
1478 assert(nullptr == _fields_type_annotations, "invariant");
1479
1480 cfs->guarantee_more(2, CHECK); // length
1481 const u2 length = cfs->get_u2_fast();
1482 *java_fields_count_ptr = length;
1483
1484 int num_injected = 0;
1485 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1486 &num_injected);
1487 const int total_fields = length + num_injected;
1488
1489 // Allocate a temporary resource array to collect field data.
1490 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1491 _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1492
1493 ResourceMark rm(THREAD);
1494 for (int n = 0; n < length; n++) {
1495 // access_flags, name_index, descriptor_index, attributes_count
1496 cfs->guarantee_more(8, CHECK);
1497
1498 AccessFlags access_flags;
1499 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1500 verify_legal_field_modifiers(flags, is_interface, CHECK);
1501 access_flags.set_flags(flags);
1502 FieldInfo::FieldFlags fieldFlags(0);
1503
1504 const u2 name_index = cfs->get_u2_fast();
1505 check_property(valid_symbol_at(name_index),
1506 "Invalid constant pool index %u for field name in class file %s",
1507 name_index, CHECK);
1508 const Symbol* const name = cp->symbol_at(name_index);
1509 verify_legal_field_name(name, CHECK);
1510
1511 const u2 signature_index = cfs->get_u2_fast();
1512 check_property(valid_symbol_at(signature_index),
1513 "Invalid constant pool index %u for field signature in class file %s",
1514 signature_index, CHECK);
1515 const Symbol* const sig = cp->symbol_at(signature_index);
1516 verify_legal_field_signature(name, sig, CHECK);
1517
1518 u2 constantvalue_index = 0;
1519 bool is_synthetic = false;
1520 u2 generic_signature_index = 0;
1521 const bool is_static = access_flags.is_static();
1522 FieldAnnotationCollector parsed_annotations(_loader_data);
1523
1524 const u2 attributes_count = cfs->get_u2_fast();
1525 if (attributes_count > 0) {
1526 parse_field_attributes(cfs,
1527 attributes_count,
1528 is_static,
1529 signature_index,
1530 &constantvalue_index,
1531 &is_synthetic,
1532 &generic_signature_index,
1533 &parsed_annotations,
1534 CHECK);
1535
1536 if (parsed_annotations.field_annotations() != nullptr) {
1537 if (_fields_annotations == nullptr) {
1538 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1539 _loader_data, length, nullptr,
1540 CHECK);
1541 }
1542 _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1543 parsed_annotations.set_field_annotations(nullptr);
1544 }
1545 if (parsed_annotations.field_type_annotations() != nullptr) {
1546 if (_fields_type_annotations == nullptr) {
1547 _fields_type_annotations =
1548 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1549 length,
1550 nullptr,
1551 CHECK);
1552 }
1553 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1554 parsed_annotations.set_field_type_annotations(nullptr);
1555 }
1556
1557 if (is_synthetic) {
1558 access_flags.set_is_synthetic();
1559 }
1560 if (generic_signature_index != 0) {
1561 fieldFlags.update_generic(true);
1562 }
1563 }
1564
1565 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1566
1567 // Update FieldAllocationCount for this kind of field
1568 fac->update(is_static, type);
1569
1570 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1571 fi.set_index(n);
1572 if (fieldFlags.is_generic()) {
1573 fi.set_generic_signature_index(generic_signature_index);
1574 }
1575 parsed_annotations.apply_to(&fi);
1576 if (fi.field_flags().is_contended()) {
1577 _has_contended_fields = true;
1578 }
1579 _temp_field_info->append(fi);
1580 }
1581 assert(_temp_field_info->length() == length, "Must be");
1582
1583 int index = length;
1584 if (num_injected != 0) {
1585 for (int n = 0; n < num_injected; n++) {
1586 // Check for duplicates
1587 if (injected[n].may_be_java) {
1588 const Symbol* const name = injected[n].name();
1596 duplicate = true;
1597 break;
1598 }
1599 }
1600 if (duplicate) {
1601 // These will be removed from the field array at the end
1602 continue;
1603 }
1604 }
1605
1606 // Injected field
1607 FieldInfo::FieldFlags fflags(0);
1608 fflags.update_injected(true);
1609 AccessFlags aflags;
1610 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1611 fi.set_index(index);
1612 _temp_field_info->append(fi);
1613
1614 // Update FieldAllocationCount for this kind of field
1615 const BasicType type = Signature::basic_type(injected[n].signature());
1616 fac->update(false, type);
1617 index++;
1618 }
1619 }
1620
1621 assert(_temp_field_info->length() == index, "Must be");
1622
1623 if (_need_verify && length > 1) {
1624 // Check duplicated fields
1625 ResourceMark rm(THREAD);
1626 // Set containing name-signature pairs
1627 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1628 for (int i = 0; i < _temp_field_info->length(); i++) {
1629 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1630 _temp_field_info->adr_at(i)->signature(_cp));
1631 // If no duplicates, add name/signature in hashtable names_and_sigs.
1632 if(!names_and_sigs->put(name_and_sig, 0)) {
1633 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1634 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1635 return;
1636 }
1637 }
1638 }
1639 }
1640
1975 }
1976 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
1977 if (_location != _in_field && _location != _in_class) {
1978 break; // only allow for fields and classes
1979 }
1980 if (!EnableContended || (RestrictContended && !privileged)) {
1981 break; // honor privileges
1982 }
1983 return _jdk_internal_vm_annotation_Contended;
1984 }
1985 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
1986 if (_location != _in_method) break; // only allow for methods
1987 if (RestrictReservedStack && !privileged) break; // honor privileges
1988 return _jdk_internal_vm_annotation_ReservedStackAccess;
1989 }
1990 case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
1991 if (_location != _in_class) break; // only allow for classes
1992 if (!privileged) break; // only allow in privileged code
1993 return _jdk_internal_ValueBased;
1994 }
1995 case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
1996 return _java_lang_Deprecated;
1997 }
1998 default: {
1999 break;
2000 }
2001 }
2002 return AnnotationCollector::_unknown;
2003 }
2004
2005 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
2006 if (is_contended())
2007 // Setting the contended group also sets the contended bit in field flags
2008 f->set_contended_group(contended_group());
2009 if (is_stable())
2010 (f->field_flags_addr())->update_stable(true);
2011 }
2012
2013 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {
2014 // If there's an error deallocate metadata for field annotations
2213 runtime_invisible_type_annotations_length > 0) {
2214 a = assemble_annotations(runtime_visible_type_annotations,
2215 runtime_visible_type_annotations_length,
2216 runtime_invisible_type_annotations,
2217 runtime_invisible_type_annotations_length,
2218 CHECK);
2219 cm->set_type_annotations(a);
2220 }
2221 }
2222
2223
2224 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2225 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2226 // Method* to save footprint, so we only know the size of the resulting Method* when the
2227 // entire method attribute is parsed.
2228 //
2229 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2230
2231 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2232 bool is_interface,
2233 const ConstantPool* cp,
2234 bool* const has_localvariable_table,
2235 TRAPS) {
2236 assert(cfs != nullptr, "invariant");
2237 assert(cp != nullptr, "invariant");
2238 assert(has_localvariable_table != nullptr, "invariant");
2239
2240 ResourceMark rm(THREAD);
2241 // Parse fixed parts:
2242 // access_flags, name_index, descriptor_index, attributes_count
2243 cfs->guarantee_more(8, CHECK_NULL);
2244
2245 int flags = cfs->get_u2_fast();
2246 const u2 name_index = cfs->get_u2_fast();
2247 const int cp_size = cp->length();
2248 check_property(
2249 valid_symbol_at(name_index),
2250 "Illegal constant pool index %u for method name in class file %s",
2251 name_index, CHECK_NULL);
2252 const Symbol* const name = cp->symbol_at(name_index);
2254
2255 const u2 signature_index = cfs->get_u2_fast();
2256 guarantee_property(
2257 valid_symbol_at(signature_index),
2258 "Illegal constant pool index %u for method signature in class file %s",
2259 signature_index, CHECK_NULL);
2260 const Symbol* const signature = cp->symbol_at(signature_index);
2261
2262 if (name == vmSymbols::class_initializer_name()) {
2263 // We ignore the other access flags for a valid class initializer.
2264 // (JVM Spec 2nd ed., chapter 4.6)
2265 if (_major_version < 51) { // backward compatibility
2266 flags = JVM_ACC_STATIC;
2267 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2268 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2269 } else {
2270 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2271 return nullptr;
2272 }
2273 } else {
2274 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2275 }
2276
2277 if (name == vmSymbols::object_initializer_name() && is_interface) {
2278 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2279 return nullptr;
2280 }
2281
2282 int args_size = -1; // only used when _need_verify is true
2283 if (_need_verify) {
2284 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2285 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2286 verify_legal_method_signature(name, signature, CHECK_NULL);
2287 if (args_size > MAX_ARGS_SIZE) {
2288 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2289 return nullptr;
2290 }
2291 }
2292
2293 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2294
2295 // Default values for code and exceptions attribute elements
2296 u2 max_stack = 0;
2297 u2 max_locals = 0;
2298 u4 code_length = 0;
2299 const u1* code_start = 0;
2300 u2 exception_table_length = 0;
2301 const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements
2821 CHECK_NULL);
2822
2823 if (InstanceKlass::is_finalization_enabled() &&
2824 name == vmSymbols::finalize_method_name() &&
2825 signature == vmSymbols::void_method_signature()) {
2826 if (m->is_empty_method()) {
2827 _has_empty_finalizer = true;
2828 } else {
2829 _has_finalizer = true;
2830 }
2831 }
2832
2833 NOT_PRODUCT(m->verify());
2834 return m;
2835 }
2836
2837
2838 // Side-effects: populates the _methods field in the parser
2839 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2840 bool is_interface,
2841 bool* const has_localvariable_table,
2842 bool* has_final_method,
2843 bool* declares_nonstatic_concrete_methods,
2844 TRAPS) {
2845 assert(cfs != nullptr, "invariant");
2846 assert(has_localvariable_table != nullptr, "invariant");
2847 assert(has_final_method != nullptr, "invariant");
2848 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2849
2850 assert(nullptr == _methods, "invariant");
2851
2852 cfs->guarantee_more(2, CHECK); // length
2853 const u2 length = cfs->get_u2_fast();
2854 if (length == 0) {
2855 _methods = Universe::the_empty_method_array();
2856 } else {
2857 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2858 length,
2859 nullptr,
2860 CHECK);
2861
2862 for (int index = 0; index < length; index++) {
2863 Method* method = parse_method(cfs,
2864 is_interface,
2865 _cp,
2866 has_localvariable_table,
2867 CHECK);
2868
2869 if (method->is_final()) {
2870 *has_final_method = true;
2871 }
2872 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2873 // used for interface initialization, and default method inheritance analysis
2874 if (is_interface && !(*declares_nonstatic_concrete_methods)
2875 && !method->is_abstract() && !method->is_static()) {
2876 *declares_nonstatic_concrete_methods = true;
2877 }
2878 _methods->at_put(index, method);
2879 }
2880
2881 if (_need_verify && length > 1) {
2882 // Check duplicated methods
2883 ResourceMark rm(THREAD);
2884 // Set containing name-signature pairs
3110 valid_klass_reference_at(outer_class_info_index),
3111 "outer_class_info_index %u has bad constant type in class file %s",
3112 outer_class_info_index, CHECK_0);
3113
3114 if (outer_class_info_index != 0) {
3115 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3116 char* bytes = (char*)outer_class_name->bytes();
3117 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3118 "Outer class is an array class in class file %s", CHECK_0);
3119 }
3120 // Inner class name
3121 const u2 inner_name_index = cfs->get_u2_fast();
3122 check_property(
3123 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3124 "inner_name_index %u has bad constant type in class file %s",
3125 inner_name_index, CHECK_0);
3126 if (_need_verify) {
3127 guarantee_property(inner_class_info_index != outer_class_info_index,
3128 "Class is both outer and inner class in class file %s", CHECK_0);
3129 }
3130 // Access flags
3131 jint flags;
3132 // JVM_ACC_MODULE is defined in JDK-9 and later.
3133 if (_major_version >= JAVA_9_VERSION) {
3134 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3135 } else {
3136 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3137 }
3138 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3139 // Set abstract bit for old class files for backward compatibility
3140 flags |= JVM_ACC_ABSTRACT;
3141 }
3142 verify_legal_class_modifiers(flags, CHECK_0);
3143 AccessFlags inner_access_flags(flags);
3144
3145 inner_classes->at_put(index++, inner_class_info_index);
3146 inner_classes->at_put(index++, outer_class_info_index);
3147 inner_classes->at_put(index++, inner_name_index);
3148 inner_classes->at_put(index++, inner_access_flags.as_short());
3149 }
3150
3151 // Check for circular and duplicate entries.
3152 bool has_circularity = false;
3153 if (_need_verify) {
3154 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3155 if (has_circularity) {
3156 // If circularity check failed then ignore InnerClasses attribute.
3157 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3158 index = 0;
3159 if (parsed_enclosingmethod_attribute) {
3160 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3161 _inner_classes = inner_classes;
3162 } else {
3226 if (length > 0) {
3227 int index = 0;
3228 cfs->guarantee_more(2 * length, CHECK_0);
3229 for (int n = 0; n < length; n++) {
3230 const u2 class_info_index = cfs->get_u2_fast();
3231 check_property(
3232 valid_klass_reference_at(class_info_index),
3233 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3234 class_info_index, CHECK_0);
3235 permitted_subclasses->at_put(index++, class_info_index);
3236 }
3237 assert(index == size, "wrong size");
3238 }
3239
3240 // Restore buffer's current position.
3241 cfs->set_current(current_mark);
3242
3243 return length;
3244 }
3245
3246 // Record {
3247 // u2 attribute_name_index;
3248 // u4 attribute_length;
3249 // u2 components_count;
3250 // component_info components[components_count];
3251 // }
3252 // component_info {
3253 // u2 name_index;
3254 // u2 descriptor_index
3255 // u2 attributes_count;
3256 // attribute_info_attributes[attributes_count];
3257 // }
3258 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3259 const ConstantPool* cp,
3260 const u1* const record_attribute_start,
3261 TRAPS) {
3262 const u1* const current_mark = cfs->current();
3263 int components_count = 0;
3264 unsigned int calculate_attr_size = 0;
3265 if (record_attribute_start != nullptr) {
3509 }
3510 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3511 "Bad length on BootstrapMethods in class file %s",
3512 CHECK);
3513 }
3514
3515 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3516 ConstantPool* cp,
3517 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3518 TRAPS) {
3519 assert(cfs != nullptr, "invariant");
3520 assert(cp != nullptr, "invariant");
3521 assert(parsed_annotations != nullptr, "invariant");
3522
3523 // Set inner classes attribute to default sentinel
3524 _inner_classes = Universe::the_empty_short_array();
3525 // Set nest members attribute to default sentinel
3526 _nest_members = Universe::the_empty_short_array();
3527 // Set _permitted_subclasses attribute to default sentinel
3528 _permitted_subclasses = Universe::the_empty_short_array();
3529 cfs->guarantee_more(2, CHECK); // attributes_count
3530 u2 attributes_count = cfs->get_u2_fast();
3531 bool parsed_sourcefile_attribute = false;
3532 bool parsed_innerclasses_attribute = false;
3533 bool parsed_nest_members_attribute = false;
3534 bool parsed_permitted_subclasses_attribute = false;
3535 bool parsed_nest_host_attribute = false;
3536 bool parsed_record_attribute = false;
3537 bool parsed_enclosingmethod_attribute = false;
3538 bool parsed_bootstrap_methods_attribute = false;
3539 const u1* runtime_visible_annotations = nullptr;
3540 int runtime_visible_annotations_length = 0;
3541 const u1* runtime_invisible_annotations = nullptr;
3542 int runtime_invisible_annotations_length = 0;
3543 const u1* runtime_visible_type_annotations = nullptr;
3544 int runtime_visible_type_annotations_length = 0;
3545 const u1* runtime_invisible_type_annotations = nullptr;
3546 int runtime_invisible_type_annotations_length = 0;
3547 bool runtime_invisible_type_annotations_exists = false;
3548 bool runtime_invisible_annotations_exists = false;
3549 bool parsed_source_debug_ext_annotations_exist = false;
3550 const u1* inner_classes_attribute_start = nullptr;
3551 u4 inner_classes_attribute_length = 0;
3552 u2 enclosing_method_class_index = 0;
3553 u2 enclosing_method_method_index = 0;
3554 const u1* nest_members_attribute_start = nullptr;
3555 u4 nest_members_attribute_length = 0;
3556 const u1* record_attribute_start = nullptr;
3557 u4 record_attribute_length = 0;
3558 const u1* permitted_subclasses_attribute_start = nullptr;
3559 u4 permitted_subclasses_attribute_length = 0;
3560
3561 // Iterate over attributes
3562 while (attributes_count--) {
3563 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3564 const u2 attribute_name_index = cfs->get_u2_fast();
3565 const u4 attribute_length = cfs->get_u4_fast();
3566 check_property(
3567 valid_symbol_at(attribute_name_index),
3568 "Attribute name has bad constant pool index %u in class file %s",
3569 attribute_name_index, CHECK);
3570 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3571 if (tag == vmSymbols::tag_source_file()) {
3572 // Check for SourceFile tag
3573 if (_need_verify) {
3574 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3575 }
3576 if (parsed_sourcefile_attribute) {
3577 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3578 return;
3579 } else {
3766 return;
3767 }
3768 parsed_record_attribute = true;
3769 record_attribute_start = cfs->current();
3770 record_attribute_length = attribute_length;
3771 } else if (_major_version >= JAVA_17_VERSION) {
3772 if (tag == vmSymbols::tag_permitted_subclasses()) {
3773 if (parsed_permitted_subclasses_attribute) {
3774 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3775 return;
3776 }
3777 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3778 if (_access_flags.is_final()) {
3779 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3780 return;
3781 }
3782 parsed_permitted_subclasses_attribute = true;
3783 permitted_subclasses_attribute_start = cfs->current();
3784 permitted_subclasses_attribute_length = attribute_length;
3785 }
3786 }
3787 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3788 cfs->skip_u1(attribute_length, CHECK);
3789 } else {
3790 // Unknown attribute
3791 cfs->skip_u1(attribute_length, CHECK);
3792 }
3793 } else {
3794 // Unknown attribute
3795 cfs->skip_u1(attribute_length, CHECK);
3796 }
3797 } else {
3798 // Unknown attribute
3799 cfs->skip_u1(attribute_length, CHECK);
3800 }
3801 }
3802 _class_annotations = assemble_annotations(runtime_visible_annotations,
3803 runtime_visible_annotations_length,
3804 runtime_invisible_annotations,
3805 runtime_invisible_annotations_length,
3846 CHECK);
3847 if (_need_verify) {
3848 guarantee_property(record_attribute_length == calculated_attr_length,
3849 "Record attribute has wrong length in class file %s",
3850 CHECK);
3851 }
3852 }
3853
3854 if (parsed_permitted_subclasses_attribute) {
3855 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3856 cfs,
3857 permitted_subclasses_attribute_start,
3858 CHECK);
3859 if (_need_verify) {
3860 guarantee_property(
3861 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3862 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3863 }
3864 }
3865
3866 if (_max_bootstrap_specifier_index >= 0) {
3867 guarantee_property(parsed_bootstrap_methods_attribute,
3868 "Missing BootstrapMethods attribute in class file %s", CHECK);
3869 }
3870 }
3871
3872 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3873 assert(k != nullptr, "invariant");
3874
3875 if (_synthetic_flag)
3876 k->set_is_synthetic();
3877 if (_sourcefile_index != 0) {
3878 k->set_source_file_name_index(_sourcefile_index);
3879 }
3880 if (_generic_signature_index != 0) {
3881 k->set_generic_signature_index(_generic_signature_index);
3882 }
3883 if (_sde_buffer != nullptr) {
3884 k->set_source_debug_extension(_sde_buffer, _sde_length);
3885 }
3911 _class_annotations = nullptr;
3912 _class_type_annotations = nullptr;
3913 _fields_annotations = nullptr;
3914 _fields_type_annotations = nullptr;
3915 }
3916
3917 // Transfer ownership of metadata allocated to the InstanceKlass.
3918 void ClassFileParser::apply_parsed_class_metadata(
3919 InstanceKlass* this_klass,
3920 int java_fields_count) {
3921 assert(this_klass != nullptr, "invariant");
3922
3923 _cp->set_pool_holder(this_klass);
3924 this_klass->set_constants(_cp);
3925 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3926 this_klass->set_fields_status(_fields_status);
3927 this_klass->set_methods(_methods);
3928 this_klass->set_inner_classes(_inner_classes);
3929 this_klass->set_nest_members(_nest_members);
3930 this_klass->set_nest_host_index(_nest_host);
3931 this_klass->set_annotations(_combined_annotations);
3932 this_klass->set_permitted_subclasses(_permitted_subclasses);
3933 this_klass->set_record_components(_record_components);
3934 // Delay the setting of _local_interfaces and _transitive_interfaces until after
3935 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3936 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3937 // its _super. If an OOM occurs while loading the current klass, its _super field
3938 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3939 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3940 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3941
3942 // Clear out these fields so they don't get deallocated by the destructor
3943 clear_class_metadata();
3944 }
3945
3946 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3947 int runtime_visible_annotations_length,
3948 const u1* const runtime_invisible_annotations,
3949 int runtime_invisible_annotations_length,
3950 TRAPS) {
3951 AnnotationArray* annotations = nullptr;
3952 if (runtime_visible_annotations != nullptr ||
3953 runtime_invisible_annotations != nullptr) {
3962 }
3963 if (runtime_invisible_annotations != nullptr) {
3964 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3965 int append = runtime_visible_annotations_length+i;
3966 annotations->at_put(append, runtime_invisible_annotations[i]);
3967 }
3968 }
3969 }
3970 return annotations;
3971 }
3972
3973 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3974 const int super_class_index,
3975 const bool need_verify,
3976 TRAPS) {
3977 assert(cp != nullptr, "invariant");
3978 const InstanceKlass* super_klass = nullptr;
3979
3980 if (super_class_index == 0) {
3981 check_property(_class_name == vmSymbols::java_lang_Object(),
3982 "Invalid superclass index %u in class file %s",
3983 super_class_index,
3984 CHECK_NULL);
3985 } else {
3986 check_property(valid_klass_reference_at(super_class_index),
3987 "Invalid superclass index %u in class file %s",
3988 super_class_index,
3989 CHECK_NULL);
3990 // The class name should be legal because it is checked when parsing constant pool.
3991 // However, make sure it is not an array type.
3992 bool is_array = false;
3993 if (cp->tag_at(super_class_index).is_klass()) {
3994 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3995 if (need_verify)
3996 is_array = super_klass->is_array_klass();
3997 } else if (need_verify) {
3998 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3999 }
4000 if (need_verify) {
4001 guarantee_property(!is_array,
4002 "Bad superclass name in class file %s", CHECK_NULL);
4003 }
4004 }
4005 return super_klass;
4006 }
4007
4008 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
4009 _max_nonstatic_oop_maps = max_blocks;
4010 _nonstatic_oop_map_count = 0;
4011 if (max_blocks == 0) {
4012 _nonstatic_oop_maps = nullptr;
4013 } else {
4014 _nonstatic_oop_maps =
4015 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
4016 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
4017 }
4018 }
4019
4020 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
4154
4155 // Check if this klass supports the java.lang.Cloneable interface
4156 if (vmClasses::Cloneable_klass_loaded()) {
4157 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4158 ik->set_is_cloneable();
4159 }
4160 }
4161
4162 // If it cannot be fast-path allocated, set a bit in the layout helper.
4163 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4164 assert(ik->size_helper() > 0, "layout_helper is initialized");
4165 if (ik->is_abstract() || ik->is_interface()
4166 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
4167 || ik->size_helper() >= FastAllocateSizeLimit) {
4168 // Forbid fast-path allocation.
4169 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4170 ik->set_layout_helper(lh);
4171 }
4172 }
4173
4174 // utility methods for appending an array with check for duplicates
4175
4176 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4177 const Array<InstanceKlass*>* const ifs) {
4178 // iterate over new interfaces
4179 for (int i = 0; i < ifs->length(); i++) {
4180 InstanceKlass* const e = ifs->at(i);
4181 assert(e->is_klass() && e->is_interface(), "just checking");
4182 // add new interface
4183 result->append_if_missing(e);
4184 }
4185 }
4186
4187 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4188 Array<InstanceKlass*>* local_ifs,
4189 ClassLoaderData* loader_data,
4190 TRAPS) {
4191 assert(local_ifs != nullptr, "invariant");
4192 assert(loader_data != nullptr, "invariant");
4193
4197 // Add superclass transitive interfaces size
4198 if (super != nullptr) {
4199 super_size = super->transitive_interfaces()->length();
4200 max_transitive_size += super_size;
4201 }
4202 // Add local interfaces' super interfaces
4203 const int local_size = local_ifs->length();
4204 for (int i = 0; i < local_size; i++) {
4205 InstanceKlass* const l = local_ifs->at(i);
4206 max_transitive_size += l->transitive_interfaces()->length();
4207 }
4208 // Finally add local interfaces
4209 max_transitive_size += local_size;
4210 // Construct array
4211 if (max_transitive_size == 0) {
4212 // no interfaces, use canonicalized array
4213 return Universe::the_empty_instance_klass_array();
4214 } else if (max_transitive_size == super_size) {
4215 // no new local interfaces added, share superklass' transitive interface array
4216 return super->transitive_interfaces();
4217 } else if (max_transitive_size == local_size) {
4218 // only local interfaces added, share local interface array
4219 return local_ifs;
4220 } else {
4221 ResourceMark rm;
4222 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4223
4224 // Copy down from superclass
4225 if (super != nullptr) {
4226 append_interfaces(result, super->transitive_interfaces());
4227 }
4228
4229 // Copy down from local interfaces' superinterfaces
4230 for (int i = 0; i < local_size; i++) {
4231 InstanceKlass* const l = local_ifs->at(i);
4232 append_interfaces(result, l->transitive_interfaces());
4233 }
4234 // Finally add local interfaces
4235 append_interfaces(result, local_ifs);
4236
4237 // length will be less than the max_transitive_size if duplicates were removed
4238 const int length = result->length();
4239 assert(length <= max_transitive_size, "just checking");
4240 Array<InstanceKlass*>* const new_result =
4241 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4242 for (int i = 0; i < length; i++) {
4243 InstanceKlass* const e = result->at(i);
4244 assert(e != nullptr, "just checking");
4245 new_result->at_put(i, e);
4246 }
4247 return new_result;
4248 }
4249 }
4250
4251 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4252 assert(this_klass != nullptr, "invariant");
4253 const Klass* const super = this_klass->super();
4254
4255 if (super != nullptr) {
4256 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4257
4258 if (super->is_final()) {
4259 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4260 return;
4261 }
4262
4263 if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {
4264 classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);
4265 return;
4266 }
4267
4268 // If the loader is not the boot loader then throw an exception if its
4269 // superclass is in package jdk.internal.reflect and its loader is not a
4270 // special reflection class loader
4271 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
4272 PackageEntry* super_package = super->package();
4273 if (super_package != nullptr &&
4274 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
4275 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
4276 ResourceMark rm(THREAD);
4277 Exceptions::fthrow(
4278 THREAD_AND_LOCATION,
4279 vmSymbols::java_lang_IllegalAccessError(),
4280 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
4281 this_klass->external_name(),
4282 this_klass->class_loader_data()->loader_name_and_id(),
4283 super->external_name());
4284 return;
4285 }
4286 }
4287
4433
4434 for (int index = 0; index < num_methods; index++) {
4435 const Method* const m = methods->at(index);
4436 // if m is static and not the init method, throw a verify error
4437 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4438 ResourceMark rm(THREAD);
4439 Exceptions::fthrow(
4440 THREAD_AND_LOCATION,
4441 vmSymbols::java_lang_VerifyError(),
4442 "Illegal static method %s in interface %s",
4443 m->name()->as_C_string(),
4444 this_klass->external_name()
4445 );
4446 return;
4447 }
4448 }
4449 }
4450
4451 // utility methods for format checking
4452
4453 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4454 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4455 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4456 if (is_module) {
4457 ResourceMark rm(THREAD);
4458 Exceptions::fthrow(
4459 THREAD_AND_LOCATION,
4460 vmSymbols::java_lang_NoClassDefFoundError(),
4461 "%s is not a class because access_flag ACC_MODULE is set",
4462 _class_name->as_C_string());
4463 return;
4464 }
4465
4466 if (!_need_verify) { return; }
4467
4468 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4469 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4470 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4471 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4472 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4473 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4474 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4475
4476 if ((is_abstract && is_final) ||
4477 (is_interface && !is_abstract) ||
4478 (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4479 (!is_interface && major_gte_1_5 && is_annotation)) {
4480 ResourceMark rm(THREAD);
4481 Exceptions::fthrow(
4482 THREAD_AND_LOCATION,
4483 vmSymbols::java_lang_ClassFormatError(),
4484 "Illegal class modifiers in class %s: 0x%X",
4485 _class_name->as_C_string(), flags
4486 );
4487 return;
4488 }
4489 }
4490
4491 static bool has_illegal_visibility(jint flags) {
4492 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4493 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4494 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4495
4496 return ((is_public && is_protected) ||
4497 (is_public && is_private) ||
4498 (is_protected && is_private));
4499 }
4500
4501 // A legal major_version.minor_version must be one of the following:
4502 //
4503 // Major_version >= 45 and major_version < 56, any minor_version.
4504 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4505 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4506 //
4507 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4533 THREAD_AND_LOCATION,
4534 vmSymbols::java_lang_UnsupportedClassVersionError(),
4535 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4536 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4537 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4538 return;
4539 }
4540
4541 if (!Arguments::enable_preview()) {
4542 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4543 class_name, major, minor, THREAD);
4544 return;
4545 }
4546
4547 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4548 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4549 class_name, major, minor, THREAD);
4550 }
4551 }
4552
4553 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4554 bool is_interface,
4555 TRAPS) const {
4556 if (!_need_verify) { return; }
4557
4558 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4559 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4560 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4561 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4562 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4563 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4564 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4565 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4566 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4567
4568 bool is_illegal = false;
4569
4570 if (is_interface) {
4571 if (!is_public || !is_static || !is_final || is_private ||
4572 is_protected || is_volatile || is_transient ||
4573 (major_gte_1_5 && is_enum)) {
4574 is_illegal = true;
4575 }
4576 } else { // not interface
4577 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4578 is_illegal = true;
4579 }
4580 }
4581
4582 if (is_illegal) {
4583 ResourceMark rm(THREAD);
4584 Exceptions::fthrow(
4585 THREAD_AND_LOCATION,
4586 vmSymbols::java_lang_ClassFormatError(),
4587 "Illegal field modifiers in class %s: 0x%X",
4588 _class_name->as_C_string(), flags);
4589 return;
4590 }
4591 }
4592
4593 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4594 bool is_interface,
4595 const Symbol* name,
4596 TRAPS) const {
4597 if (!_need_verify) { return; }
4598
4599 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4600 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4601 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4602 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4603 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4604 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4605 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4606 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4607 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4608 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4609 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4610 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4611 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4612 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4613
4614 bool is_illegal = false;
4615
4616 if (is_interface) {
4617 if (major_gte_8) {
4618 // Class file version is JAVA_8_VERSION or later Methods of
4619 // interfaces may set any of the flags except ACC_PROTECTED,
4620 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4621 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4622 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4623 (is_native || is_protected || is_final || is_synchronized) ||
4624 // If a specific method of a class or interface has its
4625 // ACC_ABSTRACT flag set, it must not have any of its
4626 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4627 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4628 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4629 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4630 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4631 is_illegal = true;
4632 }
4633 } else if (major_gte_1_5) {
4634 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4635 if (!is_public || is_private || is_protected || is_static || is_final ||
4636 is_synchronized || is_native || !is_abstract || is_strict) {
4637 is_illegal = true;
4638 }
4639 } else {
4640 // Class file version is pre-JAVA_1_5_VERSION
4641 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4642 is_illegal = true;
4643 }
4644 }
4645 } else { // not interface
4646 if (has_illegal_visibility(flags)) {
4647 is_illegal = true;
4648 } else {
4649 if (is_initializer) {
4650 if (is_static || is_final || is_synchronized || is_native ||
4651 is_abstract || (major_gte_1_5 && is_bridge)) {
4652 is_illegal = true;
4653 }
4654 } else { // not initializer
4655 if (is_abstract) {
4656 if ((is_final || is_native || is_private || is_static ||
4657 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4658 is_illegal = true;
4659 }
4660 }
4661 }
4662 }
4663 }
4664
4665 if (is_illegal) {
4666 ResourceMark rm(THREAD);
4667 Exceptions::fthrow(
4668 THREAD_AND_LOCATION,
4669 vmSymbols::java_lang_ClassFormatError(),
4670 "Method %s in class %s has illegal modifiers: 0x%X",
4671 name->as_C_string(), _class_name->as_C_string(), flags);
4672 return;
4673 }
4674 }
4675
4676 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4677 int length,
4678 TRAPS) const {
4679 assert(_need_verify, "only called when _need_verify is true");
4680 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4681 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4682 }
4683 }
4684
4685 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4686 // In class names, '/' separates unqualified names. This is verified in this function also.
4687 // Method names also may not contain the characters '<' or '>', unless <init>
4688 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4689 // method. Because these names have been checked as special cases before
4690 // calling this method in verify_legal_method_name.
4691 //
4708 if (type == ClassFileParser::LegalClass) {
4709 if (p == name || p+1 >= name+length ||
4710 *(p+1) == JVM_SIGNATURE_SLASH) {
4711 return false;
4712 }
4713 } else {
4714 return false; // do not permit '/' unless it's class name
4715 }
4716 break;
4717 case JVM_SIGNATURE_SPECIAL:
4718 case JVM_SIGNATURE_ENDSPECIAL:
4719 // do not permit '<' or '>' in method names
4720 if (type == ClassFileParser::LegalMethod) {
4721 return false;
4722 }
4723 }
4724 }
4725 return true;
4726 }
4727
4728 // Take pointer to a UTF8 byte string (not NUL-terminated).
4729 // Skip over the longest part of the string that could
4730 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
4731 // Return a pointer to just past the fieldname.
4732 // Return null if no fieldname at all was found, or in the case of slash_ok
4733 // being true, we saw consecutive slashes (meaning we were looking for a
4734 // qualified path but found something that was badly-formed).
4735 static const char* skip_over_field_name(const char* const name,
4736 bool slash_ok,
4737 unsigned int length) {
4738 const char* p;
4739 jboolean last_is_slash = false;
4740 jboolean not_first_ch = false;
4741
4742 for (p = name; p != name + length; not_first_ch = true) {
4743 const char* old_p = p;
4744 jchar ch = *p;
4745 if (ch < 128) {
4746 p++;
4747 // quick check for ascii
4809 // be taken as a field signature. Allow "void" if void_ok.
4810 // Return a pointer to just past the signature.
4811 // Return null if no legal signature is found.
4812 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4813 bool void_ok,
4814 unsigned int length,
4815 TRAPS) const {
4816 unsigned int array_dim = 0;
4817 while (length > 0) {
4818 switch (signature[0]) {
4819 case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
4820 case JVM_SIGNATURE_BOOLEAN:
4821 case JVM_SIGNATURE_BYTE:
4822 case JVM_SIGNATURE_CHAR:
4823 case JVM_SIGNATURE_SHORT:
4824 case JVM_SIGNATURE_INT:
4825 case JVM_SIGNATURE_FLOAT:
4826 case JVM_SIGNATURE_LONG:
4827 case JVM_SIGNATURE_DOUBLE:
4828 return signature + 1;
4829 case JVM_SIGNATURE_CLASS: {
4830 if (_major_version < JAVA_1_5_VERSION) {
4831 // Skip over the class name if one is there
4832 const char* const p = skip_over_field_name(signature + 1, true, --length);
4833
4834 // The next character better be a semicolon
4835 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4836 return p + 1;
4837 }
4838 }
4839 else {
4840 // Skip leading 'L' and ignore first appearance of ';'
4841 signature++;
4842 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4843 // Format check signature
4844 if (c != nullptr) {
4845 int newlen = pointer_delta_as_int(c, (char*) signature);
4846 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4847 if (!legal) {
4848 classfile_parse_error("Class name is empty or contains illegal character "
4849 "in descriptor in class file %s",
4850 THREAD);
4851 return nullptr;
4852 }
4853 return signature + newlen + 1;
4854 }
4855 }
4856 return nullptr;
4857 }
4858 case JVM_SIGNATURE_ARRAY:
4859 array_dim++;
4860 if (array_dim > 255) {
4876
4877 // Checks if name is a legal class name.
4878 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4879 if (!_need_verify || _relax_verify) { return; }
4880
4881 assert(name->refcount() > 0, "symbol must be kept alive");
4882 char* bytes = (char*)name->bytes();
4883 unsigned int length = name->utf8_length();
4884 bool legal = false;
4885
4886 if (length > 0) {
4887 const char* p;
4888 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4889 p = skip_over_field_signature(bytes, false, length, CHECK);
4890 legal = (p != nullptr) && ((p - bytes) == (int)length);
4891 } else if (_major_version < JAVA_1_5_VERSION) {
4892 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4893 p = skip_over_field_name(bytes, true, length);
4894 legal = (p != nullptr) && ((p - bytes) == (int)length);
4895 }
4896 } else {
4897 // 4900761: relax the constraints based on JSR202 spec
4898 // Class names may be drawn from the entire Unicode character set.
4899 // Identifiers between '/' must be unqualified names.
4900 // The utf8 string has been verified when parsing cpool entries.
4901 legal = verify_unqualified_name(bytes, length, LegalClass);
4902 }
4903 }
4904 if (!legal) {
4905 ResourceMark rm(THREAD);
4906 assert(_class_name != nullptr, "invariant");
4907 Exceptions::fthrow(
4908 THREAD_AND_LOCATION,
4909 vmSymbols::java_lang_ClassFormatError(),
4910 "Illegal class name \"%.*s\" in class file %s", length, bytes,
4911 _class_name->as_C_string()
4912 );
4913 return;
4914 }
4915 }
4941 THREAD_AND_LOCATION,
4942 vmSymbols::java_lang_ClassFormatError(),
4943 "Illegal field name \"%.*s\" in class %s", length, bytes,
4944 _class_name->as_C_string()
4945 );
4946 return;
4947 }
4948 }
4949
4950 // Checks if name is a legal method name.
4951 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
4952 if (!_need_verify || _relax_verify) { return; }
4953
4954 assert(name != nullptr, "method name is null");
4955 char* bytes = (char*)name->bytes();
4956 unsigned int length = name->utf8_length();
4957 bool legal = false;
4958
4959 if (length > 0) {
4960 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
4961 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
4962 legal = true;
4963 }
4964 } else if (_major_version < JAVA_1_5_VERSION) {
4965 const char* p;
4966 p = skip_over_field_name(bytes, false, length);
4967 legal = (p != nullptr) && ((p - bytes) == (int)length);
4968 } else {
4969 // 4881221: relax the constraints based on JSR202 spec
4970 legal = verify_unqualified_name(bytes, length, LegalMethod);
4971 }
4972 }
4973
4974 if (!legal) {
4975 ResourceMark rm(THREAD);
4976 assert(_class_name != nullptr, "invariant");
4977 Exceptions::fthrow(
4978 THREAD_AND_LOCATION,
4979 vmSymbols::java_lang_ClassFormatError(),
4980 "Illegal method name \"%.*s\" in class %s", length, bytes,
4981 _class_name->as_C_string()
4982 );
4983 return;
4984 }
4985 }
4986
4987
4988 // Checks if signature is a legal field signature.
4989 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
4990 const Symbol* signature,
4991 TRAPS) const {
4992 if (!_need_verify) { return; }
4993
4994 const char* const bytes = (const char*)signature->bytes();
4995 const unsigned int length = signature->utf8_length();
4996 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
4997
4998 if (p == nullptr || (p - bytes) != (int)length) {
4999 throwIllegalSignature("Field", name, signature, CHECK);
5000 }
5001 }
5002
5003 // Check that the signature is compatible with the method name. For example,
5004 // check that <init> has a void signature.
5005 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5006 const Symbol* signature,
5007 TRAPS) const {
5008 if (!_need_verify) {
5009 return;
5010 }
5011
5012 // Class initializers cannot have args for class format version >= 51.
5013 if (name == vmSymbols::class_initializer_name() &&
5014 signature != vmSymbols::void_method_signature() &&
5015 _major_version >= JAVA_7_VERSION) {
5016 throwIllegalSignature("Method", name, signature, THREAD);
5017 return;
5018 }
5019
5020 int sig_length = signature->utf8_length();
5021 if (name->utf8_length() > 0 &&
5022 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5023 sig_length > 0 &&
5024 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5025 throwIllegalSignature("Method", name, signature, THREAD);
5026 }
5027 }
5028
5029 // Checks if signature is a legal method signature.
5030 // Returns number of parameters
5031 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5032 const Symbol* signature,
5033 TRAPS) const {
5034 if (!_need_verify) {
5035 // make sure caller's args_size will be less than 0 even for non-static
5036 // method so it will be recomputed in compute_size_of_parameters().
5037 return -2;
5038 }
5039
5040 unsigned int args_size = 0;
5041 const char* p = (const char*)signature->bytes();
5042 unsigned int length = signature->utf8_length();
5043 const char* nextp;
5044
5181 }
5182 }
5183
5184 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5185 const ClassInstanceInfo& cl_inst_info,
5186 TRAPS) {
5187 if (_klass != nullptr) {
5188 return _klass;
5189 }
5190
5191 InstanceKlass* const ik =
5192 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5193
5194 if (is_hidden()) {
5195 mangle_hidden_class_name(ik);
5196 }
5197
5198 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5199
5200 assert(_klass == ik, "invariant");
5201
5202 return ik;
5203 }
5204
5205 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5206 bool changed_by_loadhook,
5207 const ClassInstanceInfo& cl_inst_info,
5208 TRAPS) {
5209 assert(ik != nullptr, "invariant");
5210
5211 // Set name and CLD before adding to CLD
5212 ik->set_class_loader_data(_loader_data);
5213 ik->set_name(_class_name);
5214
5215 // Add all classes to our internal class loader list here,
5216 // including classes in the bootstrap (null) class loader.
5217 const bool publicize = !is_internal();
5218
5219 _loader_data->add_class(ik, publicize);
5220
5221 set_klass_to_deallocate(ik);
5222
5223 assert(_field_info != nullptr, "invariant");
5224 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5225 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5226 "sanity");
5227
5228 assert(ik->is_instance_klass(), "sanity");
5229 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5230
5231 // Fill in information already parsed
5232 ik->set_should_verify_class(_need_verify);
5233
5234 // Not yet: supers are done below to support the new subtype-checking fields
5235 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5236 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5237 assert(_fac != nullptr, "invariant");
5238 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5239
5240 // this transfers ownership of a lot of arrays from
5241 // the parser onto the InstanceKlass*
5242 apply_parsed_class_metadata(ik, _java_fields_count);
5243
5244 // can only set dynamic nest-host after static nest information is set
5245 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5246 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5247 }
5248
5249 // note that is not safe to use the fields in the parser from this point on
5250 assert(nullptr == _cp, "invariant");
5251 assert(nullptr == _fieldinfo_stream, "invariant");
5252 assert(nullptr == _fields_status, "invariant");
5253 assert(nullptr == _methods, "invariant");
5254 assert(nullptr == _inner_classes, "invariant");
5255 assert(nullptr == _nest_members, "invariant");
5256 assert(nullptr == _combined_annotations, "invariant");
5257 assert(nullptr == _record_components, "invariant");
5258 assert(nullptr == _permitted_subclasses, "invariant");
5259
5260 if (_has_localvariable_table) {
5261 ik->set_has_localvariable_table(true);
5262 }
5263
5264 if (_has_final_method) {
5265 ik->set_has_final_method();
5266 }
5267
5268 ik->copy_method_ordering(_method_ordering, CHECK);
5269 // The InstanceKlass::_methods_jmethod_ids cache
5270 // is managed on the assumption that the initial cache
5271 // size is equal to the number of methods in the class. If
5272 // that changes, then InstanceKlass::idnum_can_increment()
5273 // has to be changed accordingly.
5274 ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5275
5276 ik->set_this_class_index(_this_class_index);
5277
5278 if (_is_hidden) {
5279 // _this_class_index is a CONSTANT_Class entry that refers to this
5280 // hidden class itself. If this class needs to refer to its own methods
5281 // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5282 // _this_class_index. However, because this class is hidden (it's
5283 // not stored in SystemDictionary), _this_class_index cannot be resolved
5284 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5285 // Therefore, we must eagerly resolve _this_class_index now.
5286 ik->constants()->klass_at_put(_this_class_index, ik);
5287 }
5288
5289 ik->set_minor_version(_minor_version);
5290 ik->set_major_version(_major_version);
5291 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5292 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5293
5294 if (_is_hidden) {
5295 ik->set_is_hidden();
5296 }
5297
5298 // Set PackageEntry for this_klass
5299 oop cl = ik->class_loader();
5300 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5301 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5302 ik->set_package(cld, nullptr, CHECK);
5303
5304 const Array<Method*>* const methods = ik->methods();
5305 assert(methods != nullptr, "invariant");
5306 const int methods_len = methods->length();
5307
5308 check_methods_for_intrinsics(ik, methods);
5309
5310 // Fill in field values obtained by parse_classfile_attributes
5311 if (_parsed_annotations->has_any_annotations()) {
5312 _parsed_annotations->apply_to(ik);
5313 }
5380
5381 assert(_all_mirandas != nullptr, "invariant");
5382
5383 // Generate any default methods - default methods are public interface methods
5384 // that have a default implementation. This is new with Java 8.
5385 if (_has_nonstatic_concrete_methods) {
5386 DefaultMethods::generate_default_methods(ik,
5387 _all_mirandas,
5388 CHECK);
5389 }
5390
5391 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5392 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5393 !module_entry->has_default_read_edges()) {
5394 if (!module_entry->set_has_default_read_edges()) {
5395 // We won a potential race
5396 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5397 }
5398 }
5399
5400 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5401
5402 if (!is_internal()) {
5403 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5404
5405 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5406 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5407 log_is_enabled(Info, class, preview)) {
5408 ResourceMark rm;
5409 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5410 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5411 }
5412
5413 if (log_is_enabled(Debug, class, resolve)) {
5414 ResourceMark rm;
5415 // print out the superclass.
5416 const char * from = ik->external_name();
5417 if (ik->java_super() != nullptr) {
5418 log_debug(class, resolve)("%s %s (super)",
5419 from,
5471 Symbol* name,
5472 ClassLoaderData* loader_data,
5473 const ClassLoadInfo* cl_info,
5474 Publicity pub_level,
5475 TRAPS) :
5476 _stream(stream),
5477 _class_name(nullptr),
5478 _loader_data(loader_data),
5479 _is_hidden(cl_info->is_hidden()),
5480 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5481 _orig_cp_size(0),
5482 _super_klass(),
5483 _cp(nullptr),
5484 _fieldinfo_stream(nullptr),
5485 _fields_status(nullptr),
5486 _methods(nullptr),
5487 _inner_classes(nullptr),
5488 _nest_members(nullptr),
5489 _nest_host(0),
5490 _permitted_subclasses(nullptr),
5491 _record_components(nullptr),
5492 _local_interfaces(nullptr),
5493 _transitive_interfaces(nullptr),
5494 _combined_annotations(nullptr),
5495 _class_annotations(nullptr),
5496 _class_type_annotations(nullptr),
5497 _fields_annotations(nullptr),
5498 _fields_type_annotations(nullptr),
5499 _klass(nullptr),
5500 _klass_to_deallocate(nullptr),
5501 _parsed_annotations(nullptr),
5502 _fac(nullptr),
5503 _field_info(nullptr),
5504 _temp_field_info(nullptr),
5505 _method_ordering(nullptr),
5506 _all_mirandas(nullptr),
5507 _vtable_size(0),
5508 _itable_size(0),
5509 _num_miranda_methods(0),
5510 _protection_domain(cl_info->protection_domain()),
5511 _access_flags(),
5512 _pub_level(pub_level),
5513 _bad_constant_seen(0),
5514 _synthetic_flag(false),
5515 _sde_length(false),
5516 _sde_buffer(nullptr),
5517 _sourcefile_index(0),
5518 _generic_signature_index(0),
5519 _major_version(0),
5520 _minor_version(0),
5521 _this_class_index(0),
5522 _super_class_index(0),
5523 _itfs_len(0),
5524 _java_fields_count(0),
5525 _need_verify(false),
5526 _relax_verify(false),
5527 _has_nonstatic_concrete_methods(false),
5528 _declares_nonstatic_concrete_methods(false),
5529 _has_localvariable_table(false),
5530 _has_final_method(false),
5531 _has_contended_fields(false),
5532 _has_finalizer(false),
5533 _has_empty_finalizer(false),
5534 _max_bootstrap_specifier_index(-1) {
5535
5536 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5537 _class_name->increment_refcount();
5538
5539 assert(_loader_data != nullptr, "invariant");
5540 assert(stream != nullptr, "invariant");
5541 assert(_stream != nullptr, "invariant");
5542 assert(_stream->buffer() == _stream->current(), "invariant");
5543 assert(_class_name != nullptr, "invariant");
5544 assert(0 == _access_flags.as_int(), "invariant");
5545
5546 // Figure out whether we can skip format checking (matching classic VM behavior)
5547 if (CDSConfig::is_dumping_static_archive()) {
5548 // verify == true means it's a 'remote' class (i.e., non-boot class)
5549 // Verification decision is based on BytecodeVerificationRemote flag
5550 // for those classes.
5551 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote :
5561
5562 // Check if verification needs to be relaxed for this class file
5563 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5564 _relax_verify = relax_format_check_for(_loader_data);
5565
5566 parse_stream(stream, CHECK);
5567
5568 post_process_parsed_stream(stream, _cp, CHECK);
5569 }
5570
5571 void ClassFileParser::clear_class_metadata() {
5572 // metadata created before the instance klass is created. Must be
5573 // deallocated if classfile parsing returns an error.
5574 _cp = nullptr;
5575 _fieldinfo_stream = nullptr;
5576 _fields_status = nullptr;
5577 _methods = nullptr;
5578 _inner_classes = nullptr;
5579 _nest_members = nullptr;
5580 _permitted_subclasses = nullptr;
5581 _combined_annotations = nullptr;
5582 _class_annotations = _class_type_annotations = nullptr;
5583 _fields_annotations = _fields_type_annotations = nullptr;
5584 _record_components = nullptr;
5585 }
5586
5587 // Destructor to clean up
5588 ClassFileParser::~ClassFileParser() {
5589 _class_name->decrement_refcount();
5590
5591 if (_cp != nullptr) {
5592 MetadataFactory::free_metadata(_loader_data, _cp);
5593 }
5594
5595 if (_fieldinfo_stream != nullptr) {
5596 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5597 }
5598
5599 if (_fields_status != nullptr) {
5600 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5601 }
5602
5603 if (_methods != nullptr) {
5604 // Free methods
5605 InstanceKlass::deallocate_methods(_loader_data, _methods);
5606 }
5607
5608 // beware of the Universe::empty_blah_array!!
5609 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5610 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5611 }
5612
5613 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5614 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5615 }
5616
5617 if (_record_components != nullptr) {
5618 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5619 }
5620
5621 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5622 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5623 }
5624
5625 // Free interfaces
5626 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5627 _local_interfaces, _transitive_interfaces);
5628
5629 if (_combined_annotations != nullptr) {
5630 // After all annotations arrays have been created, they are installed into the
5631 // Annotations object that will be assigned to the InstanceKlass being created.
5632
5633 // Deallocate the Annotations object and the installed annotations arrays.
5634 _combined_annotations->deallocate_contents(_loader_data);
5635
5636 // If the _combined_annotations pointer is non-null,
5637 // then the other annotations fields should have been cleared.
5638 assert(_class_annotations == nullptr, "Should have been cleared");
5639 assert(_class_type_annotations == nullptr, "Should have been cleared");
5640 assert(_fields_annotations == nullptr, "Should have been cleared");
5641 assert(_fields_type_annotations == nullptr, "Should have been cleared");
5642 } else {
5643 // If the annotations arrays were not installed into the Annotations object,
5644 // then they have to be deallocated explicitly.
5689 cp_size, CHECK);
5690
5691 _orig_cp_size = cp_size;
5692 if (is_hidden()) { // Add a slot for hidden class name.
5693 cp_size++;
5694 }
5695
5696 _cp = ConstantPool::allocate(_loader_data,
5697 cp_size,
5698 CHECK);
5699
5700 ConstantPool* const cp = _cp;
5701
5702 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
5703
5704 assert(cp_size == (u2)cp->length(), "invariant");
5705
5706 // ACCESS FLAGS
5707 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5708
5709 // Access flags
5710 jint flags;
5711 // JVM_ACC_MODULE is defined in JDK-9 and later.
5712 if (_major_version >= JAVA_9_VERSION) {
5713 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5714 } else {
5715 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5716 }
5717
5718 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5719 // Set abstract bit for old class files for backward compatibility
5720 flags |= JVM_ACC_ABSTRACT;
5721 }
5722
5723 verify_legal_class_modifiers(flags, CHECK);
5724
5725 short bad_constant = class_bad_constant_seen();
5726 if (bad_constant != 0) {
5727 // Do not throw CFE until after the access_flags are checked because if
5728 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5729 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5730 return;
5731 }
5732
5733 _access_flags.set_flags(flags);
5734
5735 // This class and superclass
5736 _this_class_index = stream->get_u2_fast();
5737 check_property(
5738 valid_cp_range(_this_class_index, cp_size) &&
5739 cp->tag_at(_this_class_index).is_unresolved_klass(),
5740 "Invalid this class index %u in constant pool in class file %s",
5741 _this_class_index, CHECK);
5742
5743 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
5744 assert(class_name_in_cp != nullptr, "class_name can't be null");
5745
5746 // Don't need to check whether this class name is legal or not.
5747 // It has been checked when constant pool is parsed.
5748 // However, make sure it is not an array type.
5749 if (_need_verify) {
5750 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
5751 "Bad class name in class file %s",
5752 CHECK);
5753 }
5754
5755 #ifdef ASSERT
5756 // Basic sanity checks
5757 if (_is_hidden) {
5758 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
5759 }
5760 #endif
5761
5762 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
5763
5764 if (_is_hidden) {
5765 assert(_class_name != nullptr, "Unexpected null _class_name");
5805 }
5806 ls.cr();
5807 }
5808 }
5809
5810 // SUPERKLASS
5811 _super_class_index = stream->get_u2_fast();
5812 _super_klass = parse_super_class(cp,
5813 _super_class_index,
5814 _need_verify,
5815 CHECK);
5816
5817 // Interfaces
5818 _itfs_len = stream->get_u2_fast();
5819 parse_interfaces(stream,
5820 _itfs_len,
5821 cp,
5822 &_has_nonstatic_concrete_methods,
5823 CHECK);
5824
5825 assert(_local_interfaces != nullptr, "invariant");
5826
5827 // Fields (offsets are filled in later)
5828 _fac = new FieldAllocationCount();
5829 parse_fields(stream,
5830 _access_flags.is_interface(),
5831 _fac,
5832 cp,
5833 cp_size,
5834 &_java_fields_count,
5835 CHECK);
5836
5837 assert(_temp_field_info != nullptr, "invariant");
5838
5839 // Methods
5840 parse_methods(stream,
5841 _access_flags.is_interface(),
5842 &_has_localvariable_table,
5843 &_has_final_method,
5844 &_declares_nonstatic_concrete_methods,
5845 CHECK);
5846
5847 assert(_methods != nullptr, "invariant");
5848
5849 if (_declares_nonstatic_concrete_methods) {
5850 _has_nonstatic_concrete_methods = true;
5851 }
5852
5853 // Additional attributes/annotations
5854 _parsed_annotations = new ClassAnnotationCollector();
5855 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
5856
5857 assert(_inner_classes != nullptr, "invariant");
5858
5859 // Finalize the Annotations metadata object,
5860 // now that all annotation arrays have been created.
5861 create_combined_annotations(CHECK);
5901 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
5902 // We have to update the resolved_klass_index and the name_index together
5903 // so extract the existing resolved_klass_index first.
5904 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
5905 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
5906 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
5907 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
5908 "Bad name_index");
5909 }
5910
5911 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
5912 ConstantPool* cp,
5913 TRAPS) {
5914 assert(stream != nullptr, "invariant");
5915 assert(stream->at_eos(), "invariant");
5916 assert(cp != nullptr, "invariant");
5917 assert(_loader_data != nullptr, "invariant");
5918
5919 if (_class_name == vmSymbols::java_lang_Object()) {
5920 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
5921 "java.lang.Object cannot implement an interface in class file %s",
5922 CHECK);
5923 }
5924 // We check super class after class file is parsed and format is checked
5925 if (_super_class_index > 0 && nullptr == _super_klass) {
5926 Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
5927 if (_access_flags.is_interface()) {
5928 // Before attempting to resolve the superclass, check for class format
5929 // errors not checked yet.
5930 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
5931 "Interfaces must have java.lang.Object as superclass in class file %s",
5932 CHECK);
5933 }
5934 Handle loader(THREAD, _loader_data->class_loader());
5935 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
5936 _super_klass = vmClasses::Object_klass();
5937 } else {
5938 _super_klass = (const InstanceKlass*)
5939 SystemDictionary::resolve_super_or_fail(_class_name,
5940 super_class_name,
5941 loader,
5942 _protection_domain,
5943 true,
5944 CHECK);
5945 }
5946 }
5947
5948 if (_super_klass != nullptr) {
5949 if (_super_klass->has_nonstatic_concrete_methods()) {
5950 _has_nonstatic_concrete_methods = true;
5951 }
5952
5953 if (_super_klass->is_interface()) {
5954 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
5955 return;
5956 }
5957 }
5958
5959 // Compute the transitive list of all unique interfaces implemented by this class
5960 _transitive_interfaces =
5961 compute_transitive_interfaces(_super_klass,
5962 _local_interfaces,
5963 _loader_data,
5964 CHECK);
5965
5966 assert(_transitive_interfaces != nullptr, "invariant");
5967
5968 // sort methods
5969 _method_ordering = sort_methods(_methods);
5970
5971 _all_mirandas = new GrowableArray<Method*>(20);
5972
5973 Handle loader(THREAD, _loader_data->class_loader());
5974 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
5975 &_num_miranda_methods,
5976 _all_mirandas,
5977 _super_klass,
5978 _methods,
5979 _access_flags,
5980 _major_version,
5981 loader,
5982 _class_name,
5983 _local_interfaces);
5984
5985 // Size of Java itable (in words)
5986 _itable_size = _access_flags.is_interface() ? 0 :
5987 klassItable::compute_itable_size(_transitive_interfaces);
5988
5989 assert(_fac != nullptr, "invariant");
5990 assert(_parsed_annotations != nullptr, "invariant");
5991
5992 _field_info = new FieldLayoutInfo();
5993 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, /*_fields*/ _temp_field_info,
5994 _parsed_annotations->is_contended(), _field_info);
5995 lb.build_layout();
5996
5997 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
5998 _fieldinfo_stream =
5999 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
6000 injected_fields_count, loader_data(), CHECK);
6001 _fields_status =
6002 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
6003 FieldStatus(0), CHECK);
6004 }
6005
6006 void ClassFileParser::set_klass(InstanceKlass* klass) {
6007
6008 #ifdef ASSERT
6009 if (klass != nullptr) {
6010 assert(nullptr == _klass, "leaking?");
6011 }
6012 #endif
6013
6014 _klass = klass;
6015 }
6016
6017 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6018
6019 #ifdef ASSERT
6020 if (klass != nullptr) {
6021 assert(nullptr == _klass_to_deallocate, "leaking?");
6022 }
6023 #endif
|
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "oops/inlineKlass.hpp"
26 #include "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: {
501 check_property(valid_symbol_at(name_ref_index),
502 "Invalid constant pool index %u in class file %s",
503 name_ref_index, CHECK);
504 check_property(valid_symbol_at(signature_ref_index),
505 "Invalid constant pool index %u in class file %s",
506 signature_ref_index, CHECK);
507 break;
508 }
509 case JVM_CONSTANT_Utf8:
510 break;
511 case JVM_CONSTANT_UnresolvedClass: // fall-through
512 case JVM_CONSTANT_UnresolvedClassInError: {
513 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
514 break;
515 }
516 case JVM_CONSTANT_ClassIndex: {
517 const int class_index = cp->klass_index_at(index);
518 check_property(valid_symbol_at(class_index),
519 "Invalid constant pool index %u in class file %s",
520 class_index, CHECK);
521
522 Symbol* const name = cp->symbol_at(class_index);
523 const unsigned int name_len = name->utf8_length();
524 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
525 break;
526 }
527 case JVM_CONSTANT_StringIndex: {
528 const int string_index = cp->string_index_at(index);
529 check_property(valid_symbol_at(string_index),
530 "Invalid constant pool index %u in class file %s",
531 string_index, CHECK);
532 Symbol* const sym = cp->symbol_at(string_index);
533 cp->unresolved_string_at_put(index, sym);
534 break;
535 }
536 case JVM_CONSTANT_MethodHandle: {
537 const int ref_index = cp->method_handle_index_at(index);
538 check_property(valid_cp_range(ref_index, length),
539 "Invalid constant pool index %u in class file %s",
540 ref_index, CHECK);
541 const constantTag tag = cp->tag_at(ref_index);
542 const int ref_kind = cp->method_handle_ref_kind_at(index);
543
713 }
714 }
715 } else {
716 if (_need_verify) {
717 // Method name and signature are individually verified above, when iterating
718 // NameAndType_info. Need to check here that signature is non-zero length and
719 // the right type.
720 if (!Signature::is_method(signature)) {
721 throwIllegalSignature("Method", name, signature, CHECK);
722 }
723 }
724 // If a class method name begins with '<', it must be "<init>" and have void signature.
725 const unsigned int name_len = name->utf8_length();
726 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
727 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
728 if (name != vmSymbols::object_initializer_name()) {
729 classfile_parse_error(
730 "Bad method name at constant pool index %u in class file %s",
731 name_ref_index, THREAD);
732 return;
733 } else if (!Signature::is_void_method(signature)) { // must have void signature.
734 throwIllegalSignature("Method", name, signature, CHECK);
735 }
736 }
737 }
738 break;
739 }
740 case JVM_CONSTANT_MethodHandle: {
741 const int ref_index = cp->method_handle_index_at(index);
742 const int ref_kind = cp->method_handle_ref_kind_at(index);
743 switch (ref_kind) {
744 case JVM_REF_invokeVirtual:
745 case JVM_REF_invokeStatic:
746 case JVM_REF_invokeSpecial:
747 case JVM_REF_newInvokeSpecial: {
748 const int name_and_type_ref_index =
749 cp->uncached_name_and_type_ref_index_at(ref_index);
750 const int name_ref_index =
751 cp->name_ref_index_at(name_and_type_ref_index);
752 const Symbol* const name = cp->symbol_at(name_ref_index);
753
754 if (name != vmSymbols::object_initializer_name()) { // !<init>
755 if (ref_kind == JVM_REF_newInvokeSpecial) {
756 classfile_parse_error(
757 "Bad constructor name at constant pool index %u in class file %s",
758 name_ref_index, THREAD);
759 return;
760 }
761 } else { // <init>
762 // The allowed invocation mode of <init> depends on its signature.
763 // This test corresponds to verify_invoke_instructions in the verifier.
764 const int signature_ref_index =
765 cp->signature_ref_index_at(name_and_type_ref_index);
766 const Symbol* const signature = cp->symbol_at(signature_ref_index);
767 if (signature->is_void_method_signature()
768 && ref_kind == JVM_REF_newInvokeSpecial) {
769 // OK, could be a constructor call
770 } else {
771 classfile_parse_error(
772 "Bad method name at constant pool index %u in class file %s",
773 name_ref_index, THREAD);
774 return;
775 }
776 }
777 break;
778 }
779 // Other ref_kinds are already fully checked in previous pass.
780 } // switch(ref_kind)
781 break;
782 }
783 case JVM_CONSTANT_MethodType: {
784 const Symbol* const no_name = vmSymbols::type_name(); // place holder
785 const Symbol* const signature = cp->method_type_signature_at(index);
786 verify_legal_method_signature(no_name, signature, CHECK);
787 break;
788 }
789 case JVM_CONSTANT_Utf8: {
790 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
802
803 NameSigHash(Symbol* name, Symbol* sig) :
804 _name(name),
805 _sig(sig) {}
806
807 static unsigned int hash(NameSigHash const& namesig) {
808 return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
809 }
810
811 static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
812 return (e0._name == e1._name) &&
813 (e0._sig == e1._sig);
814 }
815 };
816
817 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
818 NameSigHash::HASH_ROW_SIZE,
819 AnyObj::RESOURCE_AREA, mtInternal,
820 &NameSigHash::hash, &NameSigHash::equals>;
821
822 static void check_identity_and_value_modifiers(ClassFileParser* current, const InstanceKlass* super_type, TRAPS) {
823 assert(super_type != nullptr,"Method doesn't support null super type");
824 if (super_type->access_flags().is_identity_class() && !current->access_flags().is_identity_class()
825 && super_type->name() != vmSymbols::java_lang_Object()) {
826 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
827 err_msg("Value type %s has an identity type as supertype",
828 current->class_name()->as_klass_external_name()));
829 }
830 }
831
832 void ClassFileParser::parse_interfaces(const ClassFileStream* stream,
833 int itfs_len,
834 ConstantPool* cp,
835 bool* const has_nonstatic_concrete_methods,
836 // FIXME: lots of these functions
837 // declare their parameters as const,
838 // which adds only noise to the code.
839 // Remove the spurious const modifiers.
840 // Many are of the form "const int x"
841 // or "T* const x".
842 TRAPS) {
843 assert(stream != nullptr, "invariant");
844 assert(cp != nullptr, "invariant");
845 assert(has_nonstatic_concrete_methods != nullptr, "invariant");
846
847 if (itfs_len == 0) {
848 _local_interfaces = Universe::the_empty_instance_klass_array();
849
850 } else {
851 assert(itfs_len > 0, "only called for len>0");
852 _local_interface_indexes = new GrowableArray<u2>(itfs_len);
853 int index = 0;
854 for (index = 0; index < itfs_len; index++) {
855 const u2 interface_index = stream->get_u2(CHECK);
856 check_property(
857 valid_klass_reference_at(interface_index),
858 "Interface name has bad constant pool index %u in class file %s",
859 interface_index, CHECK);
860 _local_interface_indexes->at_put_grow(index, interface_index);
861 }
862
863 if (!_need_verify || itfs_len <= 1) {
864 return;
865 }
866
867 // Check if there's any duplicates in interfaces
868 ResourceMark rm(THREAD);
869 // Set containing interface names
870 ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
871 for (index = 0; index < itfs_len; index++) {
872 Symbol* interface_name = cp->klass_name_at(_local_interface_indexes->at(index));
873 // If no duplicates, add (name, nullptr) in hashtable interface_names.
874 if (!interface_names->put(interface_name, 0)) {
875 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
876 interface_name->as_C_string(), THREAD);
877 return;
878 }
879 }
880 }
881 }
882
883 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
884 int constantvalue_index,
885 int signature_index,
886 TRAPS) const {
887 // Make sure the constant pool entry is of a type appropriate to this field
888 guarantee_property(
889 (constantvalue_index > 0 &&
890 constantvalue_index < cp->length()),
891 "Bad initial value index %u in ConstantValue attribute in class file %s",
892 constantvalue_index, CHECK);
938
939 class AnnotationCollector : public ResourceObj{
940 public:
941 enum Location { _in_field, _in_method, _in_class };
942 enum ID {
943 _unknown = 0,
944 _method_CallerSensitive,
945 _method_ForceInline,
946 _method_DontInline,
947 _method_ChangesCurrentThread,
948 _method_JvmtiMountTransition,
949 _method_InjectedProfile,
950 _method_LambdaForm_Compiled,
951 _method_Hidden,
952 _method_Scoped,
953 _method_IntrinsicCandidate,
954 _jdk_internal_vm_annotation_Contended,
955 _field_Stable,
956 _jdk_internal_vm_annotation_ReservedStackAccess,
957 _jdk_internal_ValueBased,
958 _jdk_internal_ImplicitlyConstructible,
959 _jdk_internal_LooselyConsistentValue,
960 _jdk_internal_NullRestricted,
961 _java_lang_Deprecated,
962 _java_lang_Deprecated_for_removal,
963 _annotation_LIMIT
964 };
965 const Location _location;
966 int _annotations_present;
967 u2 _contended_group;
968
969 AnnotationCollector(Location location)
970 : _location(location), _annotations_present(0), _contended_group(0)
971 {
972 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
973 }
974 // If this annotation name has an ID, report it (or _none).
975 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
976 // Set the annotation name:
977 void set_annotation(ID id) {
978 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
979 _annotations_present |= (int)nth_bit((int)id);
980 }
1366 CHECK);
1367 parsed_annotations->set_field_annotations(a);
1368 a = assemble_annotations(runtime_visible_type_annotations,
1369 runtime_visible_type_annotations_length,
1370 runtime_invisible_type_annotations,
1371 runtime_invisible_type_annotations_length,
1372 CHECK);
1373 parsed_annotations->set_field_type_annotations(a);
1374 return;
1375 }
1376
1377
1378 // Field allocation types. Used for computing field offsets.
1379
1380 enum FieldAllocationType {
1381 STATIC_OOP, // Oops
1382 STATIC_BYTE, // Boolean, Byte, char
1383 STATIC_SHORT, // shorts
1384 STATIC_WORD, // ints
1385 STATIC_DOUBLE, // aligned long or double
1386 STATIC_INLINE, // inline type field
1387 NONSTATIC_OOP,
1388 NONSTATIC_BYTE,
1389 NONSTATIC_SHORT,
1390 NONSTATIC_WORD,
1391 NONSTATIC_DOUBLE,
1392 NONSTATIC_INLINE,
1393 MAX_FIELD_ALLOCATION_TYPE,
1394 BAD_ALLOCATION_TYPE = -1
1395 };
1396
1397 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1398 BAD_ALLOCATION_TYPE, // 0
1399 BAD_ALLOCATION_TYPE, // 1
1400 BAD_ALLOCATION_TYPE, // 2
1401 BAD_ALLOCATION_TYPE, // 3
1402 NONSTATIC_BYTE , // T_BOOLEAN = 4,
1403 NONSTATIC_SHORT, // T_CHAR = 5,
1404 NONSTATIC_WORD, // T_FLOAT = 6,
1405 NONSTATIC_DOUBLE, // T_DOUBLE = 7,
1406 NONSTATIC_BYTE, // T_BYTE = 8,
1407 NONSTATIC_SHORT, // T_SHORT = 9,
1408 NONSTATIC_WORD, // T_INT = 10,
1409 NONSTATIC_DOUBLE, // T_LONG = 11,
1410 NONSTATIC_OOP, // T_OBJECT = 12,
1411 NONSTATIC_OOP, // T_ARRAY = 13,
1412 NONSTATIC_OOP, // T_PRIMITIVE_OBJECT = 14,
1413 BAD_ALLOCATION_TYPE, // T_VOID = 15,
1414 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16,
1415 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17,
1416 BAD_ALLOCATION_TYPE, // T_METADATA = 18,
1417 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1418 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20,
1419 BAD_ALLOCATION_TYPE, // 0
1420 BAD_ALLOCATION_TYPE, // 1
1421 BAD_ALLOCATION_TYPE, // 2
1422 BAD_ALLOCATION_TYPE, // 3
1423 STATIC_BYTE , // T_BOOLEAN = 4,
1424 STATIC_SHORT, // T_CHAR = 5,
1425 STATIC_WORD, // T_FLOAT = 6,
1426 STATIC_DOUBLE, // T_DOUBLE = 7,
1427 STATIC_BYTE, // T_BYTE = 8,
1428 STATIC_SHORT, // T_SHORT = 9,
1429 STATIC_WORD, // T_INT = 10,
1430 STATIC_DOUBLE, // T_LONG = 11,
1431 STATIC_OOP, // T_OBJECT = 12,
1432 STATIC_OOP, // T_ARRAY = 13,
1433 STATIC_OOP, // T_PRIMITIVE_OBJECT = 14,
1434 BAD_ALLOCATION_TYPE, // T_VOID = 15,
1435 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16,
1436 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17,
1437 BAD_ALLOCATION_TYPE, // T_METADATA = 18,
1438 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1439 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20
1440 };
1441
1442 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type, bool is_inline_type) {
1443 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1444 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1445 assert(result != BAD_ALLOCATION_TYPE, "bad type");
1446 if (is_inline_type) {
1447 result = is_static ? STATIC_INLINE : NONSTATIC_INLINE;
1448 }
1449 return result;
1450 }
1451
1452 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1453 public:
1454 u2 count[MAX_FIELD_ALLOCATION_TYPE];
1455
1456 FieldAllocationCount() {
1457 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1458 count[i] = 0;
1459 }
1460 }
1461
1462 void update(bool is_static, BasicType type, bool is_inline_type) {
1463 FieldAllocationType atype = basic_type_to_atype(is_static, type, is_inline_type);
1464 if (atype != BAD_ALLOCATION_TYPE) {
1465 // Make sure there is no overflow with injected fields.
1466 assert(count[atype] < 0xFFFF, "More than 65535 fields");
1467 count[atype]++;
1468 }
1469 }
1470 };
1471
1472 // Side-effects: populates the _fields, _fields_annotations,
1473 // _fields_type_annotations fields
1474 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1475 AccessFlags class_access_flags,
1476 FieldAllocationCount* const fac,
1477 ConstantPool* cp,
1478 const int cp_size,
1479 u2* const java_fields_count_ptr,
1480 TRAPS) {
1481
1482 assert(cfs != nullptr, "invariant");
1483 assert(fac != nullptr, "invariant");
1484 assert(cp != nullptr, "invariant");
1485 assert(java_fields_count_ptr != nullptr, "invariant");
1486
1487 assert(nullptr == _fields_annotations, "invariant");
1488 assert(nullptr == _fields_type_annotations, "invariant");
1489
1490 bool is_inline_type = !class_access_flags.is_identity_class() && !class_access_flags.is_abstract();
1491 cfs->guarantee_more(2, CHECK); // length
1492 const u2 length = cfs->get_u2_fast();
1493 *java_fields_count_ptr = length;
1494
1495 int num_injected = 0;
1496 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1497 &num_injected);
1498
1499 // two more slots are required for inline classes:
1500 // one for the static field with a reference to the pre-allocated default value
1501 // one for the field the JVM injects when detecting an empty inline class
1502 const int total_fields = length + num_injected + (is_inline_type ? 2 : 0);
1503
1504 // Allocate a temporary resource array to collect field data.
1505 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1506 _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1507
1508 int instance_fields_count = 0;
1509 ResourceMark rm(THREAD);
1510 for (int n = 0; n < length; n++) {
1511 // access_flags, name_index, descriptor_index, attributes_count
1512 cfs->guarantee_more(8, CHECK);
1513
1514 jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS;
1515 if (!supports_inline_types()) {
1516 recognized_modifiers &= ~JVM_ACC_STRICT;
1517 }
1518
1519 const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1520 verify_legal_field_modifiers(flags, class_access_flags, CHECK);
1521 AccessFlags access_flags;
1522 access_flags.set_flags(flags);
1523 FieldInfo::FieldFlags fieldFlags(0);
1524
1525 const u2 name_index = cfs->get_u2_fast();
1526 check_property(valid_symbol_at(name_index),
1527 "Invalid constant pool index %u for field name in class file %s",
1528 name_index, CHECK);
1529 const Symbol* const name = cp->symbol_at(name_index);
1530 verify_legal_field_name(name, CHECK);
1531
1532 const u2 signature_index = cfs->get_u2_fast();
1533 check_property(valid_symbol_at(signature_index),
1534 "Invalid constant pool index %u for field signature in class file %s",
1535 signature_index, CHECK);
1536 const Symbol* const sig = cp->symbol_at(signature_index);
1537 verify_legal_field_signature(name, sig, CHECK);
1538 if (!access_flags.is_static()) instance_fields_count++;
1539
1540 u2 constantvalue_index = 0;
1541 bool is_synthetic = false;
1542 u2 generic_signature_index = 0;
1543 const bool is_static = access_flags.is_static();
1544 FieldAnnotationCollector parsed_annotations(_loader_data);
1545
1546 bool is_null_restricted = false;
1547
1548 const u2 attributes_count = cfs->get_u2_fast();
1549 if (attributes_count > 0) {
1550 parse_field_attributes(cfs,
1551 attributes_count,
1552 is_static,
1553 signature_index,
1554 &constantvalue_index,
1555 &is_synthetic,
1556 &generic_signature_index,
1557 &parsed_annotations,
1558 CHECK);
1559
1560 if (parsed_annotations.field_annotations() != nullptr) {
1561 if (_fields_annotations == nullptr) {
1562 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1563 _loader_data, length, nullptr,
1564 CHECK);
1565 }
1566 _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1567 if (parsed_annotations.has_annotation(AnnotationCollector::_jdk_internal_NullRestricted)) {
1568 if (!Signature::has_envelope(sig)) {
1569 Exceptions::fthrow(
1570 THREAD_AND_LOCATION,
1571 vmSymbols::java_lang_ClassFormatError(),
1572 "Illegal use of @jdk.internal.vm.annotation.NullRestricted annotation on field %s with signature %s (primitive types can never be null)",
1573 name->as_C_string(), sig->as_C_string());
1574 }
1575 is_null_restricted = true;
1576 }
1577 parsed_annotations.set_field_annotations(nullptr);
1578 }
1579 if (parsed_annotations.field_type_annotations() != nullptr) {
1580 if (_fields_type_annotations == nullptr) {
1581 _fields_type_annotations =
1582 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1583 length,
1584 nullptr,
1585 CHECK);
1586 }
1587 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1588 parsed_annotations.set_field_type_annotations(nullptr);
1589 }
1590
1591 if (is_synthetic) {
1592 access_flags.set_is_synthetic();
1593 }
1594 if (generic_signature_index != 0) {
1595 fieldFlags.update_generic(true);
1596 }
1597 }
1598
1599 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1600
1601 // Update FieldAllocationCount for this kind of field
1602 // This use of T_PRIMITIVE_OBJECT is not valid anymore => FIXME (relate to cleanup (removal?) of FiedAllocationCount
1603 fac->update(is_static, type, type == T_PRIMITIVE_OBJECT);
1604
1605 if (is_null_restricted) fieldFlags.update_null_free_inline_type(true);
1606
1607 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1608 fi.set_index(n);
1609 if (fieldFlags.is_generic()) {
1610 fi.set_generic_signature_index(generic_signature_index);
1611 }
1612 parsed_annotations.apply_to(&fi);
1613 if (fi.field_flags().is_contended()) {
1614 _has_contended_fields = true;
1615 }
1616 _temp_field_info->append(fi);
1617 }
1618 assert(_temp_field_info->length() == length, "Must be");
1619
1620 int index = length;
1621 if (num_injected != 0) {
1622 for (int n = 0; n < num_injected; n++) {
1623 // Check for duplicates
1624 if (injected[n].may_be_java) {
1625 const Symbol* const name = injected[n].name();
1633 duplicate = true;
1634 break;
1635 }
1636 }
1637 if (duplicate) {
1638 // These will be removed from the field array at the end
1639 continue;
1640 }
1641 }
1642
1643 // Injected field
1644 FieldInfo::FieldFlags fflags(0);
1645 fflags.update_injected(true);
1646 AccessFlags aflags;
1647 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1648 fi.set_index(index);
1649 _temp_field_info->append(fi);
1650
1651 // Update FieldAllocationCount for this kind of field
1652 const BasicType type = Signature::basic_type(injected[n].signature());
1653 fac->update(false, type, false);
1654 index++;
1655 }
1656 }
1657
1658 if (is_inline_type) {
1659 // Inject static ".default" field
1660 FieldInfo::FieldFlags fflags(0);
1661 fflags.update_injected(true);
1662 AccessFlags aflags(JVM_ACC_STATIC);
1663 FieldInfo fi(aflags,
1664 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(default_value_name)),
1665 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(object_signature)),
1666 0,
1667 fflags);
1668 fi.set_index(index);
1669 _temp_field_info->append(fi);
1670
1671 const BasicType type = Signature::basic_type(vmSymbols::object_signature());
1672 fac->update(true, type, false);
1673 index++;
1674 }
1675
1676 if (is_inline_type && instance_fields_count == 0) {
1677 // Inject ".empty" dummy field
1678 _is_empty_inline_type = true;
1679
1680 FieldInfo::FieldFlags fflags(0);
1681 fflags.update_injected(true);
1682 AccessFlags aflags;
1683 FieldInfo fi(aflags,
1684 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(empty_marker_name)),
1685 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(byte_signature)),
1686 0,
1687 fflags);
1688 fi.set_index(index);
1689 _temp_field_info->append(fi);
1690
1691 const BasicType type = Signature::basic_type(vmSymbols::byte_signature());
1692 fac->update(false, type, false);
1693 index++;
1694 }
1695
1696 if (instance_fields_count > 0) {
1697 _has_nonstatic_fields = true;
1698 }
1699
1700 assert(_temp_field_info->length() == index, "Must be");
1701
1702 if (_need_verify && length > 1) {
1703 // Check duplicated fields
1704 ResourceMark rm(THREAD);
1705 // Set containing name-signature pairs
1706 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1707 for (int i = 0; i < _temp_field_info->length(); i++) {
1708 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1709 _temp_field_info->adr_at(i)->signature(_cp));
1710 // If no duplicates, add name/signature in hashtable names_and_sigs.
1711 if(!names_and_sigs->put(name_and_sig, 0)) {
1712 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1713 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1714 return;
1715 }
1716 }
1717 }
1718 }
1719
2054 }
2055 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
2056 if (_location != _in_field && _location != _in_class) {
2057 break; // only allow for fields and classes
2058 }
2059 if (!EnableContended || (RestrictContended && !privileged)) {
2060 break; // honor privileges
2061 }
2062 return _jdk_internal_vm_annotation_Contended;
2063 }
2064 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
2065 if (_location != _in_method) break; // only allow for methods
2066 if (RestrictReservedStack && !privileged) break; // honor privileges
2067 return _jdk_internal_vm_annotation_ReservedStackAccess;
2068 }
2069 case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
2070 if (_location != _in_class) break; // only allow for classes
2071 if (!privileged) break; // only allow in privileged code
2072 return _jdk_internal_ValueBased;
2073 }
2074 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ImplicitlyConstructible_signature): {
2075 if (_location != _in_class) break; // only allow for classes
2076 return _jdk_internal_ImplicitlyConstructible;
2077 }
2078 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_LooselyConsistentValue_signature): {
2079 if (_location != _in_class) break; // only allow for classes
2080 return _jdk_internal_LooselyConsistentValue;
2081 }
2082 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_NullRestricted_signature): {
2083 if (_location != _in_field) break; // only allow for fields
2084 return _jdk_internal_NullRestricted;
2085 }
2086 case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
2087 return _java_lang_Deprecated;
2088 }
2089 default: {
2090 break;
2091 }
2092 }
2093 return AnnotationCollector::_unknown;
2094 }
2095
2096 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
2097 if (is_contended())
2098 // Setting the contended group also sets the contended bit in field flags
2099 f->set_contended_group(contended_group());
2100 if (is_stable())
2101 (f->field_flags_addr())->update_stable(true);
2102 }
2103
2104 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {
2105 // If there's an error deallocate metadata for field annotations
2304 runtime_invisible_type_annotations_length > 0) {
2305 a = assemble_annotations(runtime_visible_type_annotations,
2306 runtime_visible_type_annotations_length,
2307 runtime_invisible_type_annotations,
2308 runtime_invisible_type_annotations_length,
2309 CHECK);
2310 cm->set_type_annotations(a);
2311 }
2312 }
2313
2314
2315 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2316 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2317 // Method* to save footprint, so we only know the size of the resulting Method* when the
2318 // entire method attribute is parsed.
2319 //
2320 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2321
2322 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2323 bool is_interface,
2324 bool is_value_class,
2325 bool is_abstract_class,
2326 const ConstantPool* cp,
2327 bool* const has_localvariable_table,
2328 TRAPS) {
2329 assert(cfs != nullptr, "invariant");
2330 assert(cp != nullptr, "invariant");
2331 assert(has_localvariable_table != nullptr, "invariant");
2332
2333 ResourceMark rm(THREAD);
2334 // Parse fixed parts:
2335 // access_flags, name_index, descriptor_index, attributes_count
2336 cfs->guarantee_more(8, CHECK_NULL);
2337
2338 int flags = cfs->get_u2_fast();
2339 const u2 name_index = cfs->get_u2_fast();
2340 const int cp_size = cp->length();
2341 check_property(
2342 valid_symbol_at(name_index),
2343 "Illegal constant pool index %u for method name in class file %s",
2344 name_index, CHECK_NULL);
2345 const Symbol* const name = cp->symbol_at(name_index);
2347
2348 const u2 signature_index = cfs->get_u2_fast();
2349 guarantee_property(
2350 valid_symbol_at(signature_index),
2351 "Illegal constant pool index %u for method signature in class file %s",
2352 signature_index, CHECK_NULL);
2353 const Symbol* const signature = cp->symbol_at(signature_index);
2354
2355 if (name == vmSymbols::class_initializer_name()) {
2356 // We ignore the other access flags for a valid class initializer.
2357 // (JVM Spec 2nd ed., chapter 4.6)
2358 if (_major_version < 51) { // backward compatibility
2359 flags = JVM_ACC_STATIC;
2360 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2361 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2362 } else {
2363 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2364 return nullptr;
2365 }
2366 } else {
2367 verify_legal_method_modifiers(flags, access_flags() , name, CHECK_NULL);
2368 }
2369
2370 if (name == vmSymbols::object_initializer_name() && is_interface) {
2371 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2372 return nullptr;
2373 }
2374
2375 if (EnableValhalla) {
2376 if (((flags & JVM_ACC_SYNCHRONIZED) == JVM_ACC_SYNCHRONIZED)
2377 && ((flags & JVM_ACC_STATIC) == 0 )
2378 && !_access_flags.is_identity_class()) {
2379 classfile_parse_error("Invalid synchronized method in non-identity class %s", THREAD);
2380 return nullptr;
2381 }
2382 }
2383
2384 int args_size = -1; // only used when _need_verify is true
2385 if (_need_verify) {
2386 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2387 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2388 verify_legal_method_signature(name, signature, CHECK_NULL);
2389 if (args_size > MAX_ARGS_SIZE) {
2390 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2391 return nullptr;
2392 }
2393 }
2394
2395 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2396
2397 // Default values for code and exceptions attribute elements
2398 u2 max_stack = 0;
2399 u2 max_locals = 0;
2400 u4 code_length = 0;
2401 const u1* code_start = 0;
2402 u2 exception_table_length = 0;
2403 const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements
2923 CHECK_NULL);
2924
2925 if (InstanceKlass::is_finalization_enabled() &&
2926 name == vmSymbols::finalize_method_name() &&
2927 signature == vmSymbols::void_method_signature()) {
2928 if (m->is_empty_method()) {
2929 _has_empty_finalizer = true;
2930 } else {
2931 _has_finalizer = true;
2932 }
2933 }
2934
2935 NOT_PRODUCT(m->verify());
2936 return m;
2937 }
2938
2939
2940 // Side-effects: populates the _methods field in the parser
2941 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2942 bool is_interface,
2943 bool is_value_class,
2944 bool is_abstract_type,
2945 bool* const has_localvariable_table,
2946 bool* has_final_method,
2947 bool* declares_nonstatic_concrete_methods,
2948 TRAPS) {
2949 assert(cfs != nullptr, "invariant");
2950 assert(has_localvariable_table != nullptr, "invariant");
2951 assert(has_final_method != nullptr, "invariant");
2952 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2953
2954 assert(nullptr == _methods, "invariant");
2955
2956 cfs->guarantee_more(2, CHECK); // length
2957 const u2 length = cfs->get_u2_fast();
2958 if (length == 0) {
2959 _methods = Universe::the_empty_method_array();
2960 } else {
2961 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2962 length,
2963 nullptr,
2964 CHECK);
2965
2966 for (int index = 0; index < length; index++) {
2967 Method* method = parse_method(cfs,
2968 is_interface,
2969 is_value_class,
2970 is_abstract_type,
2971 _cp,
2972 has_localvariable_table,
2973 CHECK);
2974
2975 if (method->is_final()) {
2976 *has_final_method = true;
2977 }
2978 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2979 // used for interface initialization, and default method inheritance analysis
2980 if (is_interface && !(*declares_nonstatic_concrete_methods)
2981 && !method->is_abstract() && !method->is_static()) {
2982 *declares_nonstatic_concrete_methods = true;
2983 }
2984 _methods->at_put(index, method);
2985 }
2986
2987 if (_need_verify && length > 1) {
2988 // Check duplicated methods
2989 ResourceMark rm(THREAD);
2990 // Set containing name-signature pairs
3216 valid_klass_reference_at(outer_class_info_index),
3217 "outer_class_info_index %u has bad constant type in class file %s",
3218 outer_class_info_index, CHECK_0);
3219
3220 if (outer_class_info_index != 0) {
3221 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3222 char* bytes = (char*)outer_class_name->bytes();
3223 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3224 "Outer class is an array class in class file %s", CHECK_0);
3225 }
3226 // Inner class name
3227 const u2 inner_name_index = cfs->get_u2_fast();
3228 check_property(
3229 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3230 "inner_name_index %u has bad constant type in class file %s",
3231 inner_name_index, CHECK_0);
3232 if (_need_verify) {
3233 guarantee_property(inner_class_info_index != outer_class_info_index,
3234 "Class is both outer and inner class in class file %s", CHECK_0);
3235 }
3236
3237 jint recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS;
3238 // JVM_ACC_MODULE is defined in JDK-9 and later.
3239 if (_major_version >= JAVA_9_VERSION) {
3240 recognized_modifiers |= JVM_ACC_MODULE;
3241 }
3242
3243 // Access flags
3244 jint flags = cfs->get_u2_fast() & recognized_modifiers;
3245
3246 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3247 // Set abstract bit for old class files for backward compatibility
3248 flags |= JVM_ACC_ABSTRACT;
3249 }
3250
3251 if (!supports_inline_types()) {
3252 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
3253 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3254 if (!is_module && !is_interface) {
3255 flags |= JVM_ACC_IDENTITY;
3256 }
3257 }
3258
3259 const char* name = inner_name_index == 0 ? "unnamed" : cp->symbol_at(inner_name_index)->as_utf8();
3260 verify_legal_class_modifiers(flags, name, false, CHECK_0);
3261 AccessFlags inner_access_flags(flags);
3262
3263 inner_classes->at_put(index++, inner_class_info_index);
3264 inner_classes->at_put(index++, outer_class_info_index);
3265 inner_classes->at_put(index++, inner_name_index);
3266 inner_classes->at_put(index++, inner_access_flags.as_short());
3267 }
3268
3269 // Check for circular and duplicate entries.
3270 bool has_circularity = false;
3271 if (_need_verify) {
3272 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3273 if (has_circularity) {
3274 // If circularity check failed then ignore InnerClasses attribute.
3275 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3276 index = 0;
3277 if (parsed_enclosingmethod_attribute) {
3278 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3279 _inner_classes = inner_classes;
3280 } else {
3344 if (length > 0) {
3345 int index = 0;
3346 cfs->guarantee_more(2 * length, CHECK_0);
3347 for (int n = 0; n < length; n++) {
3348 const u2 class_info_index = cfs->get_u2_fast();
3349 check_property(
3350 valid_klass_reference_at(class_info_index),
3351 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3352 class_info_index, CHECK_0);
3353 permitted_subclasses->at_put(index++, class_info_index);
3354 }
3355 assert(index == size, "wrong size");
3356 }
3357
3358 // Restore buffer's current position.
3359 cfs->set_current(current_mark);
3360
3361 return length;
3362 }
3363
3364 u2 ClassFileParser::parse_classfile_loadable_descriptors_attribute(const ClassFileStream* const cfs,
3365 const u1* const loadable_descriptors_attribute_start,
3366 TRAPS) {
3367 const u1* const current_mark = cfs->current();
3368 u2 length = 0;
3369 if (loadable_descriptors_attribute_start != nullptr) {
3370 cfs->set_current(loadable_descriptors_attribute_start);
3371 cfs->guarantee_more(2, CHECK_0); // length
3372 length = cfs->get_u2_fast();
3373 }
3374 const int size = length;
3375 Array<u2>* const loadable_descriptors = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3376 _loadable_descriptors = loadable_descriptors;
3377 if (length > 0) {
3378 int index = 0;
3379 cfs->guarantee_more(2 * length, CHECK_0);
3380 for (int n = 0; n < length; n++) {
3381 const u2 descriptor_index = cfs->get_u2_fast();
3382 check_property(
3383 valid_symbol_at(descriptor_index),
3384 "LoadableDescriptors descriptor_index %u has bad constant type in class file %s",
3385 descriptor_index, CHECK_0);
3386 Symbol* descriptor = _cp->symbol_at(descriptor_index);
3387 bool valid = legal_field_signature(descriptor, CHECK_0);
3388 if(!valid) {
3389 ResourceMark rm(THREAD);
3390 Exceptions::fthrow(THREAD_AND_LOCATION,
3391 vmSymbols::java_lang_ClassFormatError(),
3392 "Descriptor from LoadableDescriptors attribute at index \"%d\" in class %s has illegal signature \"%s\"",
3393 descriptor_index, _class_name->as_C_string(), descriptor->as_C_string());
3394 return 0;
3395 }
3396 loadable_descriptors->at_put(index++, descriptor_index);
3397 }
3398 assert(index == size, "wrong size");
3399 }
3400
3401 // Restore buffer's current position.
3402 cfs->set_current(current_mark);
3403
3404 return length;
3405 }
3406
3407 // Record {
3408 // u2 attribute_name_index;
3409 // u4 attribute_length;
3410 // u2 components_count;
3411 // component_info components[components_count];
3412 // }
3413 // component_info {
3414 // u2 name_index;
3415 // u2 descriptor_index
3416 // u2 attributes_count;
3417 // attribute_info_attributes[attributes_count];
3418 // }
3419 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3420 const ConstantPool* cp,
3421 const u1* const record_attribute_start,
3422 TRAPS) {
3423 const u1* const current_mark = cfs->current();
3424 int components_count = 0;
3425 unsigned int calculate_attr_size = 0;
3426 if (record_attribute_start != nullptr) {
3670 }
3671 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3672 "Bad length on BootstrapMethods in class file %s",
3673 CHECK);
3674 }
3675
3676 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3677 ConstantPool* cp,
3678 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3679 TRAPS) {
3680 assert(cfs != nullptr, "invariant");
3681 assert(cp != nullptr, "invariant");
3682 assert(parsed_annotations != nullptr, "invariant");
3683
3684 // Set inner classes attribute to default sentinel
3685 _inner_classes = Universe::the_empty_short_array();
3686 // Set nest members attribute to default sentinel
3687 _nest_members = Universe::the_empty_short_array();
3688 // Set _permitted_subclasses attribute to default sentinel
3689 _permitted_subclasses = Universe::the_empty_short_array();
3690 // Set _loadable_descriptors attribute to default sentinel
3691 _loadable_descriptors = Universe::the_empty_short_array();
3692 cfs->guarantee_more(2, CHECK); // attributes_count
3693 u2 attributes_count = cfs->get_u2_fast();
3694 bool parsed_sourcefile_attribute = false;
3695 bool parsed_innerclasses_attribute = false;
3696 bool parsed_nest_members_attribute = false;
3697 bool parsed_permitted_subclasses_attribute = false;
3698 bool parsed_loadable_descriptors_attribute = false;
3699 bool parsed_nest_host_attribute = false;
3700 bool parsed_record_attribute = false;
3701 bool parsed_enclosingmethod_attribute = false;
3702 bool parsed_bootstrap_methods_attribute = false;
3703 const u1* runtime_visible_annotations = nullptr;
3704 int runtime_visible_annotations_length = 0;
3705 const u1* runtime_invisible_annotations = nullptr;
3706 int runtime_invisible_annotations_length = 0;
3707 const u1* runtime_visible_type_annotations = nullptr;
3708 int runtime_visible_type_annotations_length = 0;
3709 const u1* runtime_invisible_type_annotations = nullptr;
3710 int runtime_invisible_type_annotations_length = 0;
3711 bool runtime_invisible_type_annotations_exists = false;
3712 bool runtime_invisible_annotations_exists = false;
3713 bool parsed_source_debug_ext_annotations_exist = false;
3714 const u1* inner_classes_attribute_start = nullptr;
3715 u4 inner_classes_attribute_length = 0;
3716 u2 enclosing_method_class_index = 0;
3717 u2 enclosing_method_method_index = 0;
3718 const u1* nest_members_attribute_start = nullptr;
3719 u4 nest_members_attribute_length = 0;
3720 const u1* record_attribute_start = nullptr;
3721 u4 record_attribute_length = 0;
3722 const u1* permitted_subclasses_attribute_start = nullptr;
3723 u4 permitted_subclasses_attribute_length = 0;
3724 const u1* loadable_descriptors_attribute_start = nullptr;
3725 u4 loadable_descriptors_attribute_length = 0;
3726
3727 // Iterate over attributes
3728 while (attributes_count--) {
3729 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3730 const u2 attribute_name_index = cfs->get_u2_fast();
3731 const u4 attribute_length = cfs->get_u4_fast();
3732 check_property(
3733 valid_symbol_at(attribute_name_index),
3734 "Attribute name has bad constant pool index %u in class file %s",
3735 attribute_name_index, CHECK);
3736 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3737 if (tag == vmSymbols::tag_source_file()) {
3738 // Check for SourceFile tag
3739 if (_need_verify) {
3740 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3741 }
3742 if (parsed_sourcefile_attribute) {
3743 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3744 return;
3745 } else {
3932 return;
3933 }
3934 parsed_record_attribute = true;
3935 record_attribute_start = cfs->current();
3936 record_attribute_length = attribute_length;
3937 } else if (_major_version >= JAVA_17_VERSION) {
3938 if (tag == vmSymbols::tag_permitted_subclasses()) {
3939 if (parsed_permitted_subclasses_attribute) {
3940 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3941 return;
3942 }
3943 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3944 if (_access_flags.is_final()) {
3945 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3946 return;
3947 }
3948 parsed_permitted_subclasses_attribute = true;
3949 permitted_subclasses_attribute_start = cfs->current();
3950 permitted_subclasses_attribute_length = attribute_length;
3951 }
3952 if (EnableValhalla && tag == vmSymbols::tag_loadable_descriptors()) {
3953 if (parsed_loadable_descriptors_attribute) {
3954 classfile_parse_error("Multiple LoadableDescriptors attributes in class file %s", CHECK);
3955 return;
3956 }
3957 parsed_loadable_descriptors_attribute = true;
3958 loadable_descriptors_attribute_start = cfs->current();
3959 loadable_descriptors_attribute_length = attribute_length;
3960 }
3961 }
3962 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3963 cfs->skip_u1(attribute_length, CHECK);
3964 } else {
3965 // Unknown attribute
3966 cfs->skip_u1(attribute_length, CHECK);
3967 }
3968 } else {
3969 // Unknown attribute
3970 cfs->skip_u1(attribute_length, CHECK);
3971 }
3972 } else {
3973 // Unknown attribute
3974 cfs->skip_u1(attribute_length, CHECK);
3975 }
3976 }
3977 _class_annotations = assemble_annotations(runtime_visible_annotations,
3978 runtime_visible_annotations_length,
3979 runtime_invisible_annotations,
3980 runtime_invisible_annotations_length,
4021 CHECK);
4022 if (_need_verify) {
4023 guarantee_property(record_attribute_length == calculated_attr_length,
4024 "Record attribute has wrong length in class file %s",
4025 CHECK);
4026 }
4027 }
4028
4029 if (parsed_permitted_subclasses_attribute) {
4030 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
4031 cfs,
4032 permitted_subclasses_attribute_start,
4033 CHECK);
4034 if (_need_verify) {
4035 guarantee_property(
4036 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
4037 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
4038 }
4039 }
4040
4041 if (parsed_loadable_descriptors_attribute) {
4042 const u2 num_classes = parse_classfile_loadable_descriptors_attribute(
4043 cfs,
4044 loadable_descriptors_attribute_start,
4045 CHECK);
4046 if (_need_verify) {
4047 guarantee_property(
4048 loadable_descriptors_attribute_length == sizeof(num_classes) + sizeof(u2) * num_classes,
4049 "Wrong LoadableDescriptors attribute length in class file %s", CHECK);
4050 }
4051 }
4052
4053 if (_max_bootstrap_specifier_index >= 0) {
4054 guarantee_property(parsed_bootstrap_methods_attribute,
4055 "Missing BootstrapMethods attribute in class file %s", CHECK);
4056 }
4057 }
4058
4059 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
4060 assert(k != nullptr, "invariant");
4061
4062 if (_synthetic_flag)
4063 k->set_is_synthetic();
4064 if (_sourcefile_index != 0) {
4065 k->set_source_file_name_index(_sourcefile_index);
4066 }
4067 if (_generic_signature_index != 0) {
4068 k->set_generic_signature_index(_generic_signature_index);
4069 }
4070 if (_sde_buffer != nullptr) {
4071 k->set_source_debug_extension(_sde_buffer, _sde_length);
4072 }
4098 _class_annotations = nullptr;
4099 _class_type_annotations = nullptr;
4100 _fields_annotations = nullptr;
4101 _fields_type_annotations = nullptr;
4102 }
4103
4104 // Transfer ownership of metadata allocated to the InstanceKlass.
4105 void ClassFileParser::apply_parsed_class_metadata(
4106 InstanceKlass* this_klass,
4107 int java_fields_count) {
4108 assert(this_klass != nullptr, "invariant");
4109
4110 _cp->set_pool_holder(this_klass);
4111 this_klass->set_constants(_cp);
4112 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
4113 this_klass->set_fields_status(_fields_status);
4114 this_klass->set_methods(_methods);
4115 this_klass->set_inner_classes(_inner_classes);
4116 this_klass->set_nest_members(_nest_members);
4117 this_klass->set_nest_host_index(_nest_host);
4118 this_klass->set_loadable_descriptors(_loadable_descriptors);
4119 this_klass->set_annotations(_combined_annotations);
4120 this_klass->set_permitted_subclasses(_permitted_subclasses);
4121 this_klass->set_record_components(_record_components);
4122 this_klass->set_inline_type_field_klasses_array(_inline_type_field_klasses);
4123 this_klass->set_null_marker_offsets_array(_null_marker_offsets);
4124 // Delay the setting of _local_interfaces and _transitive_interfaces until after
4125 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
4126 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
4127 // its _super. If an OOM occurs while loading the current klass, its _super field
4128 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
4129 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
4130 // dereferences to the deallocated _transitive_interfaces will result in a crash.
4131
4132 // Clear out these fields so they don't get deallocated by the destructor
4133 clear_class_metadata();
4134 }
4135
4136 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
4137 int runtime_visible_annotations_length,
4138 const u1* const runtime_invisible_annotations,
4139 int runtime_invisible_annotations_length,
4140 TRAPS) {
4141 AnnotationArray* annotations = nullptr;
4142 if (runtime_visible_annotations != nullptr ||
4143 runtime_invisible_annotations != nullptr) {
4152 }
4153 if (runtime_invisible_annotations != nullptr) {
4154 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
4155 int append = runtime_visible_annotations_length+i;
4156 annotations->at_put(append, runtime_invisible_annotations[i]);
4157 }
4158 }
4159 }
4160 return annotations;
4161 }
4162
4163 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
4164 const int super_class_index,
4165 const bool need_verify,
4166 TRAPS) {
4167 assert(cp != nullptr, "invariant");
4168 const InstanceKlass* super_klass = nullptr;
4169
4170 if (super_class_index == 0) {
4171 check_property(_class_name == vmSymbols::java_lang_Object(),
4172 "Invalid superclass index 0 in class file %s",
4173 CHECK_NULL);
4174 } else {
4175 check_property(valid_klass_reference_at(super_class_index),
4176 "Invalid superclass index %u in class file %s",
4177 super_class_index,
4178 CHECK_NULL);
4179 // The class name should be legal because it is checked when parsing constant pool.
4180 // However, make sure it is not an array type.
4181 if (cp->tag_at(super_class_index).is_klass()) {
4182 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
4183 }
4184 if (need_verify) {
4185 bool is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
4186 guarantee_property(!is_array,
4187 "Bad superclass name in class file %s", CHECK_NULL);
4188 }
4189 }
4190 return super_klass;
4191 }
4192
4193 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
4194 _max_nonstatic_oop_maps = max_blocks;
4195 _nonstatic_oop_map_count = 0;
4196 if (max_blocks == 0) {
4197 _nonstatic_oop_maps = nullptr;
4198 } else {
4199 _nonstatic_oop_maps =
4200 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
4201 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
4202 }
4203 }
4204
4205 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
4339
4340 // Check if this klass supports the java.lang.Cloneable interface
4341 if (vmClasses::Cloneable_klass_loaded()) {
4342 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4343 ik->set_is_cloneable();
4344 }
4345 }
4346
4347 // If it cannot be fast-path allocated, set a bit in the layout helper.
4348 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4349 assert(ik->size_helper() > 0, "layout_helper is initialized");
4350 if (ik->is_abstract() || ik->is_interface()
4351 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
4352 || ik->size_helper() >= FastAllocateSizeLimit) {
4353 // Forbid fast-path allocation.
4354 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4355 ik->set_layout_helper(lh);
4356 }
4357 }
4358
4359 bool ClassFileParser::supports_inline_types() const {
4360 // Inline types are only supported by class file version 68.65535 and later
4361 return _major_version > JAVA_24_VERSION ||
4362 (_major_version == JAVA_24_VERSION && _minor_version == JAVA_PREVIEW_MINOR_VERSION);
4363 }
4364
4365 // utility methods for appending an array with check for duplicates
4366
4367 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4368 const Array<InstanceKlass*>* const ifs) {
4369 // iterate over new interfaces
4370 for (int i = 0; i < ifs->length(); i++) {
4371 InstanceKlass* const e = ifs->at(i);
4372 assert(e->is_klass() && e->is_interface(), "just checking");
4373 // add new interface
4374 result->append_if_missing(e);
4375 }
4376 }
4377
4378 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4379 Array<InstanceKlass*>* local_ifs,
4380 ClassLoaderData* loader_data,
4381 TRAPS) {
4382 assert(local_ifs != nullptr, "invariant");
4383 assert(loader_data != nullptr, "invariant");
4384
4388 // Add superclass transitive interfaces size
4389 if (super != nullptr) {
4390 super_size = super->transitive_interfaces()->length();
4391 max_transitive_size += super_size;
4392 }
4393 // Add local interfaces' super interfaces
4394 const int local_size = local_ifs->length();
4395 for (int i = 0; i < local_size; i++) {
4396 InstanceKlass* const l = local_ifs->at(i);
4397 max_transitive_size += l->transitive_interfaces()->length();
4398 }
4399 // Finally add local interfaces
4400 max_transitive_size += local_size;
4401 // Construct array
4402 if (max_transitive_size == 0) {
4403 // no interfaces, use canonicalized array
4404 return Universe::the_empty_instance_klass_array();
4405 } else if (max_transitive_size == super_size) {
4406 // no new local interfaces added, share superklass' transitive interface array
4407 return super->transitive_interfaces();
4408 // The three lines below are commented to work around bug JDK-8245487
4409 // } else if (max_transitive_size == local_size) {
4410 // // only local interfaces added, share local interface array
4411 // return local_ifs;
4412 } else {
4413 ResourceMark rm;
4414 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4415
4416 // Copy down from superclass
4417 if (super != nullptr) {
4418 append_interfaces(result, super->transitive_interfaces());
4419 }
4420
4421 // Copy down from local interfaces' superinterfaces
4422 for (int i = 0; i < local_size; i++) {
4423 InstanceKlass* const l = local_ifs->at(i);
4424 append_interfaces(result, l->transitive_interfaces());
4425 }
4426 // Finally add local interfaces
4427 append_interfaces(result, local_ifs);
4428
4429 // length will be less than the max_transitive_size if duplicates were removed
4430 const int length = result->length();
4431 assert(length <= max_transitive_size, "just checking");
4432
4433 Array<InstanceKlass*>* const new_result =
4434 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4435 for (int i = 0; i < length; i++) {
4436 InstanceKlass* const e = result->at(i);
4437 assert(e != nullptr, "just checking");
4438 new_result->at_put(i, e);
4439 }
4440 return new_result;
4441 }
4442 }
4443
4444 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4445 assert(this_klass != nullptr, "invariant");
4446 const Klass* const super = this_klass->super();
4447
4448 if (super != nullptr) {
4449 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4450
4451 if (super->is_final()) {
4452 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4453 return;
4454 }
4455
4456 if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {
4457 classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);
4458 return;
4459 }
4460
4461 // The JVMS says that super classes for value types must not have the ACC_IDENTITY
4462 // flag set. But, java.lang.Object must still be allowed to be a direct super class
4463 // for a value classes. So, it is treated as a special case for now.
4464 if (!this_klass->access_flags().is_identity_class() &&
4465 super_ik->name() != vmSymbols::java_lang_Object() &&
4466 super_ik->is_identity_class()) {
4467 classfile_icce_error("value class %s cannot inherit from class %s", super_ik, THREAD);
4468 return;
4469 }
4470
4471 // If the loader is not the boot loader then throw an exception if its
4472 // superclass is in package jdk.internal.reflect and its loader is not a
4473 // special reflection class loader
4474 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
4475 PackageEntry* super_package = super->package();
4476 if (super_package != nullptr &&
4477 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
4478 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
4479 ResourceMark rm(THREAD);
4480 Exceptions::fthrow(
4481 THREAD_AND_LOCATION,
4482 vmSymbols::java_lang_IllegalAccessError(),
4483 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
4484 this_klass->external_name(),
4485 this_klass->class_loader_data()->loader_name_and_id(),
4486 super->external_name());
4487 return;
4488 }
4489 }
4490
4636
4637 for (int index = 0; index < num_methods; index++) {
4638 const Method* const m = methods->at(index);
4639 // if m is static and not the init method, throw a verify error
4640 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4641 ResourceMark rm(THREAD);
4642 Exceptions::fthrow(
4643 THREAD_AND_LOCATION,
4644 vmSymbols::java_lang_VerifyError(),
4645 "Illegal static method %s in interface %s",
4646 m->name()->as_C_string(),
4647 this_klass->external_name()
4648 );
4649 return;
4650 }
4651 }
4652 }
4653
4654 // utility methods for format checking
4655
4656 void ClassFileParser::verify_legal_class_modifiers(jint flags, const char* name, bool is_Object, TRAPS) const {
4657 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4658 const bool is_inner_class = name != nullptr;
4659 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4660 if (is_module) {
4661 ResourceMark rm(THREAD);
4662 Exceptions::fthrow(
4663 THREAD_AND_LOCATION,
4664 vmSymbols::java_lang_NoClassDefFoundError(),
4665 "%s is not a class because access_flag ACC_MODULE is set",
4666 _class_name->as_C_string());
4667 return;
4668 }
4669
4670 if (!_need_verify) { return; }
4671
4672 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4673 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4674 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4675 const bool is_identity = (flags & JVM_ACC_IDENTITY) != 0;
4676 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4677 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4678 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4679 const bool valid_value_class = is_identity || is_interface ||
4680 (supports_inline_types() && (!is_identity && (is_abstract || is_final)));
4681
4682 if ((is_abstract && is_final) ||
4683 (is_interface && !is_abstract) ||
4684 (is_interface && major_gte_1_5 && (is_identity || is_enum)) || // ACC_SUPER (now ACC_IDENTITY) was illegal for interfaces
4685 (!is_interface && major_gte_1_5 && is_annotation) ||
4686 (!valid_value_class)) {
4687 ResourceMark rm(THREAD);
4688 const char* class_note = "";
4689 if (!valid_value_class) {
4690 class_note = " (a value class must be final or else abstract)";
4691 }
4692 if (name == nullptr) { // Not an inner class
4693 Exceptions::fthrow(
4694 THREAD_AND_LOCATION,
4695 vmSymbols::java_lang_ClassFormatError(),
4696 "Illegal class modifiers in class %s%s: 0x%X",
4697 _class_name->as_C_string(), class_note, flags
4698 );
4699 return;
4700 } else {
4701 Exceptions::fthrow(
4702 THREAD_AND_LOCATION,
4703 vmSymbols::java_lang_ClassFormatError(),
4704 "Illegal class modifiers in declaration of inner class %s%s of class %s: 0x%X",
4705 name, class_note, _class_name->as_C_string(), flags
4706 );
4707 return;
4708 }
4709 }
4710 }
4711
4712 static bool has_illegal_visibility(jint flags) {
4713 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4714 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4715 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4716
4717 return ((is_public && is_protected) ||
4718 (is_public && is_private) ||
4719 (is_protected && is_private));
4720 }
4721
4722 // A legal major_version.minor_version must be one of the following:
4723 //
4724 // Major_version >= 45 and major_version < 56, any minor_version.
4725 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4726 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4727 //
4728 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4754 THREAD_AND_LOCATION,
4755 vmSymbols::java_lang_UnsupportedClassVersionError(),
4756 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4757 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4758 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4759 return;
4760 }
4761
4762 if (!Arguments::enable_preview()) {
4763 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4764 class_name, major, minor, THREAD);
4765 return;
4766 }
4767
4768 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4769 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4770 class_name, major, minor, THREAD);
4771 }
4772 }
4773
4774 void ClassFileParser:: verify_legal_field_modifiers(jint flags,
4775 AccessFlags class_access_flags,
4776 TRAPS) const {
4777 if (!_need_verify) { return; }
4778
4779 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4780 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4781 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4782 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4783 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4784 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4785 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4786 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4787 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4788 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4789
4790 const bool is_interface = class_access_flags.is_interface();
4791 const bool is_identity_class = class_access_flags.is_identity_class();
4792
4793 bool is_illegal = false;
4794 const char* error_msg = "";
4795
4796 // There is some overlap in the checks that apply, for example interface fields
4797 // must be static, static fields can't be strict, and therefore interfaces can't
4798 // have strict fields. So we don't have to check every possible invalid combination
4799 // individually as long as all are covered. Once we have found an illegal combination
4800 // we can stop checking.
4801
4802 if (supports_inline_types()) {
4803 if (is_strict && is_static) {
4804 is_illegal = true;
4805 error_msg = "field cannot be strict and static";
4806 }
4807 else if (is_strict && !is_final) {
4808 is_illegal = true;
4809 error_msg = "strict field must be final";
4810 }
4811 }
4812
4813 if (!is_illegal) {
4814 if (is_interface) {
4815 if (!is_public || !is_static || !is_final || is_private ||
4816 is_protected || is_volatile || is_transient ||
4817 (major_gte_1_5 && is_enum)) {
4818 is_illegal = true;
4819 error_msg = "interface fields must be public, static and final, and may be synthetic";
4820 }
4821 } else { // not interface
4822 if (has_illegal_visibility(flags)) {
4823 is_illegal = true;
4824 error_msg = "invalid visibility flags for class field";
4825 } else if (is_final && is_volatile) {
4826 is_illegal = true;
4827 error_msg = "fields cannot be final and volatile";
4828 } else if (supports_inline_types()) {
4829 if (!is_identity_class && !is_static && !is_strict) {
4830 is_illegal = true;
4831 error_msg = "value class fields must be either strict or static";
4832 }
4833 }
4834 }
4835 }
4836
4837 if (is_illegal) {
4838 ResourceMark rm(THREAD);
4839 Exceptions::fthrow(
4840 THREAD_AND_LOCATION,
4841 vmSymbols::java_lang_ClassFormatError(),
4842 "Illegal field modifiers (%s) in class %s: 0x%X",
4843 error_msg, _class_name->as_C_string(), flags);
4844 return;
4845 }
4846 }
4847
4848 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4849 AccessFlags class_access_flags,
4850 const Symbol* name,
4851 TRAPS) const {
4852 if (!_need_verify) { return; }
4853
4854 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4855 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4856 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4857 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4858 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4859 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4860 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4861 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4862 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4863 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4864 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4865 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4866 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4867 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4868 // LW401 CR required: removal of value factories support
4869 const bool is_interface = class_access_flags.is_interface();
4870 const bool is_identity_class = class_access_flags.is_identity_class();
4871 const bool is_abstract_class = class_access_flags.is_abstract();
4872
4873 bool is_illegal = false;
4874
4875 const char* class_note = "";
4876 if (is_interface) {
4877 if (major_gte_8) {
4878 // Class file version is JAVA_8_VERSION or later Methods of
4879 // interfaces may set any of the flags except ACC_PROTECTED,
4880 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4881 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4882 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4883 (is_native || is_protected || is_final || is_synchronized) ||
4884 // If a specific method of a class or interface has its
4885 // ACC_ABSTRACT flag set, it must not have any of its
4886 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4887 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4888 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4889 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4890 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4891 is_illegal = true;
4892 }
4893 } else if (major_gte_1_5) {
4894 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4895 if (!is_public || is_private || is_protected || is_static || is_final ||
4896 is_synchronized || is_native || !is_abstract || is_strict) {
4897 is_illegal = true;
4898 }
4899 } else {
4900 // Class file version is pre-JAVA_1_5_VERSION
4901 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4902 is_illegal = true;
4903 }
4904 }
4905 } else { // not interface
4906 if (has_illegal_visibility(flags)) {
4907 is_illegal = true;
4908 } else {
4909 if (is_initializer) {
4910 if (is_static || is_final || is_synchronized || is_native ||
4911 is_abstract || (major_gte_1_5 && is_bridge)) {
4912 is_illegal = true;
4913 }
4914 } else { // not initializer
4915 if (!is_identity_class && is_synchronized && !is_static) {
4916 is_illegal = true;
4917 class_note = " (not an identity class)";
4918 } else {
4919 if (is_abstract) {
4920 if ((is_final || is_native || is_private || is_static ||
4921 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4922 is_illegal = true;
4923 }
4924 }
4925 }
4926 }
4927 }
4928 }
4929
4930 if (is_illegal) {
4931 ResourceMark rm(THREAD);
4932 Exceptions::fthrow(
4933 THREAD_AND_LOCATION,
4934 vmSymbols::java_lang_ClassFormatError(),
4935 "Method %s in class %s%s has illegal modifiers: 0x%X",
4936 name->as_C_string(), _class_name->as_C_string(),
4937 class_note, flags);
4938 return;
4939 }
4940 }
4941
4942 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4943 int length,
4944 TRAPS) const {
4945 assert(_need_verify, "only called when _need_verify is true");
4946 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4947 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4948 }
4949 }
4950
4951 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4952 // In class names, '/' separates unqualified names. This is verified in this function also.
4953 // Method names also may not contain the characters '<' or '>', unless <init>
4954 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4955 // method. Because these names have been checked as special cases before
4956 // calling this method in verify_legal_method_name.
4957 //
4974 if (type == ClassFileParser::LegalClass) {
4975 if (p == name || p+1 >= name+length ||
4976 *(p+1) == JVM_SIGNATURE_SLASH) {
4977 return false;
4978 }
4979 } else {
4980 return false; // do not permit '/' unless it's class name
4981 }
4982 break;
4983 case JVM_SIGNATURE_SPECIAL:
4984 case JVM_SIGNATURE_ENDSPECIAL:
4985 // do not permit '<' or '>' in method names
4986 if (type == ClassFileParser::LegalMethod) {
4987 return false;
4988 }
4989 }
4990 }
4991 return true;
4992 }
4993
4994 bool ClassFileParser::is_class_in_loadable_descriptors_attribute(Symbol *klass) {
4995 if (_loadable_descriptors == nullptr) return false;
4996 for (int i = 0; i < _loadable_descriptors->length(); i++) {
4997 Symbol* class_name = _cp->symbol_at(_loadable_descriptors->at(i));
4998 if (class_name == klass) return true;
4999 }
5000 return false;
5001 }
5002
5003 // Take pointer to a UTF8 byte string (not NUL-terminated).
5004 // Skip over the longest part of the string that could
5005 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
5006 // Return a pointer to just past the fieldname.
5007 // Return null if no fieldname at all was found, or in the case of slash_ok
5008 // being true, we saw consecutive slashes (meaning we were looking for a
5009 // qualified path but found something that was badly-formed).
5010 static const char* skip_over_field_name(const char* const name,
5011 bool slash_ok,
5012 unsigned int length) {
5013 const char* p;
5014 jboolean last_is_slash = false;
5015 jboolean not_first_ch = false;
5016
5017 for (p = name; p != name + length; not_first_ch = true) {
5018 const char* old_p = p;
5019 jchar ch = *p;
5020 if (ch < 128) {
5021 p++;
5022 // quick check for ascii
5084 // be taken as a field signature. Allow "void" if void_ok.
5085 // Return a pointer to just past the signature.
5086 // Return null if no legal signature is found.
5087 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5088 bool void_ok,
5089 unsigned int length,
5090 TRAPS) const {
5091 unsigned int array_dim = 0;
5092 while (length > 0) {
5093 switch (signature[0]) {
5094 case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
5095 case JVM_SIGNATURE_BOOLEAN:
5096 case JVM_SIGNATURE_BYTE:
5097 case JVM_SIGNATURE_CHAR:
5098 case JVM_SIGNATURE_SHORT:
5099 case JVM_SIGNATURE_INT:
5100 case JVM_SIGNATURE_FLOAT:
5101 case JVM_SIGNATURE_LONG:
5102 case JVM_SIGNATURE_DOUBLE:
5103 return signature + 1;
5104 case JVM_SIGNATURE_CLASS:
5105 {
5106 if (_major_version < JAVA_1_5_VERSION) {
5107 // Skip over the class name if one is there
5108 const char* const p = skip_over_field_name(signature + 1, true, --length);
5109
5110 // The next character better be a semicolon
5111 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
5112 return p + 1;
5113 }
5114 }
5115 else {
5116 // Skip leading 'L' or 'Q' and ignore first appearance of ';'
5117 signature++;
5118 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
5119 // Format check signature
5120 if (c != nullptr) {
5121 int newlen = pointer_delta_as_int(c, (char*) signature);
5122 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5123 if (!legal) {
5124 classfile_parse_error("Class name is empty or contains illegal character "
5125 "in descriptor in class file %s",
5126 THREAD);
5127 return nullptr;
5128 }
5129 return signature + newlen + 1;
5130 }
5131 }
5132 return nullptr;
5133 }
5134 case JVM_SIGNATURE_ARRAY:
5135 array_dim++;
5136 if (array_dim > 255) {
5152
5153 // Checks if name is a legal class name.
5154 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5155 if (!_need_verify || _relax_verify) { return; }
5156
5157 assert(name->refcount() > 0, "symbol must be kept alive");
5158 char* bytes = (char*)name->bytes();
5159 unsigned int length = name->utf8_length();
5160 bool legal = false;
5161
5162 if (length > 0) {
5163 const char* p;
5164 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5165 p = skip_over_field_signature(bytes, false, length, CHECK);
5166 legal = (p != nullptr) && ((p - bytes) == (int)length);
5167 } else if (_major_version < JAVA_1_5_VERSION) {
5168 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
5169 p = skip_over_field_name(bytes, true, length);
5170 legal = (p != nullptr) && ((p - bytes) == (int)length);
5171 }
5172 } else if ((_major_version >= CONSTANT_CLASS_DESCRIPTORS || _class_name->starts_with("jdk/internal/reflect/"))
5173 && bytes[length - 1] == ';' ) {
5174 // Support for L...; descriptors
5175 legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
5176 } else {
5177 // 4900761: relax the constraints based on JSR202 spec
5178 // Class names may be drawn from the entire Unicode character set.
5179 // Identifiers between '/' must be unqualified names.
5180 // The utf8 string has been verified when parsing cpool entries.
5181 legal = verify_unqualified_name(bytes, length, LegalClass);
5182 }
5183 }
5184 if (!legal) {
5185 ResourceMark rm(THREAD);
5186 assert(_class_name != nullptr, "invariant");
5187 Exceptions::fthrow(
5188 THREAD_AND_LOCATION,
5189 vmSymbols::java_lang_ClassFormatError(),
5190 "Illegal class name \"%.*s\" in class file %s", length, bytes,
5191 _class_name->as_C_string()
5192 );
5193 return;
5194 }
5195 }
5221 THREAD_AND_LOCATION,
5222 vmSymbols::java_lang_ClassFormatError(),
5223 "Illegal field name \"%.*s\" in class %s", length, bytes,
5224 _class_name->as_C_string()
5225 );
5226 return;
5227 }
5228 }
5229
5230 // Checks if name is a legal method name.
5231 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
5232 if (!_need_verify || _relax_verify) { return; }
5233
5234 assert(name != nullptr, "method name is null");
5235 char* bytes = (char*)name->bytes();
5236 unsigned int length = name->utf8_length();
5237 bool legal = false;
5238
5239 if (length > 0) {
5240 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
5241 if (name == vmSymbols::object_initializer_name() ||
5242 name == vmSymbols::class_initializer_name()) {
5243 legal = true;
5244 }
5245 } else if (_major_version < JAVA_1_5_VERSION) {
5246 const char* p;
5247 p = skip_over_field_name(bytes, false, length);
5248 legal = (p != nullptr) && ((p - bytes) == (int)length);
5249 } else {
5250 // 4881221: relax the constraints based on JSR202 spec
5251 legal = verify_unqualified_name(bytes, length, LegalMethod);
5252 }
5253 }
5254
5255 if (!legal) {
5256 ResourceMark rm(THREAD);
5257 assert(_class_name != nullptr, "invariant");
5258 Exceptions::fthrow(
5259 THREAD_AND_LOCATION,
5260 vmSymbols::java_lang_ClassFormatError(),
5261 "Illegal method name \"%.*s\" in class %s", length, bytes,
5262 _class_name->as_C_string()
5263 );
5264 return;
5265 }
5266 }
5267
5268 bool ClassFileParser::legal_field_signature(const Symbol* signature, TRAPS) const {
5269 const char* const bytes = (const char*)signature->bytes();
5270 const unsigned int length = signature->utf8_length();
5271 const char* const p = skip_over_field_signature(bytes, false, length, CHECK_false);
5272
5273 if (p == nullptr || (p - bytes) != (int)length) {
5274 return false;
5275 }
5276 return true;
5277 }
5278
5279 // Checks if signature is a legal field signature.
5280 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5281 const Symbol* signature,
5282 TRAPS) const {
5283 if (!_need_verify) { return; }
5284
5285 const char* const bytes = (const char*)signature->bytes();
5286 const unsigned int length = signature->utf8_length();
5287 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5288
5289 if (p == nullptr || (p - bytes) != (int)length) {
5290 throwIllegalSignature("Field", name, signature, CHECK);
5291 }
5292 }
5293
5294 // Check that the signature is compatible with the method name. For example,
5295 // check that <init> has a void signature.
5296 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5297 const Symbol* signature,
5298 TRAPS) const {
5299 if (!_need_verify) {
5300 return;
5301 }
5302
5303 // Class initializers cannot have args for class format version >= 51.
5304 if (name == vmSymbols::class_initializer_name() &&
5305 signature != vmSymbols::void_method_signature() &&
5306 _major_version >= JAVA_7_VERSION) {
5307 throwIllegalSignature("Method", name, signature, THREAD);
5308 return;
5309 }
5310
5311 int sig_length = signature->utf8_length();
5312 if (name->utf8_length() > 0 &&
5313 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5314 sig_length > 0 &&
5315 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5316 throwIllegalSignature("Method", name, signature, THREAD);
5317 }
5318 }
5319
5320 // Checks if signature is a legal method signature.
5321 // Returns number of parameters
5322 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5323 const Symbol* signature,
5324 TRAPS) const {
5325 if (!_need_verify) {
5326 // make sure caller's args_size will be less than 0 even for non-static
5327 // method so it will be recomputed in compute_size_of_parameters().
5328 return -2;
5329 }
5330
5331 unsigned int args_size = 0;
5332 const char* p = (const char*)signature->bytes();
5333 unsigned int length = signature->utf8_length();
5334 const char* nextp;
5335
5472 }
5473 }
5474
5475 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5476 const ClassInstanceInfo& cl_inst_info,
5477 TRAPS) {
5478 if (_klass != nullptr) {
5479 return _klass;
5480 }
5481
5482 InstanceKlass* const ik =
5483 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5484
5485 if (is_hidden()) {
5486 mangle_hidden_class_name(ik);
5487 }
5488
5489 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5490
5491 assert(_klass == ik, "invariant");
5492 return ik;
5493 }
5494
5495 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5496 bool changed_by_loadhook,
5497 const ClassInstanceInfo& cl_inst_info,
5498 TRAPS) {
5499 assert(ik != nullptr, "invariant");
5500
5501 // Set name and CLD before adding to CLD
5502 ik->set_class_loader_data(_loader_data);
5503 ik->set_name(_class_name);
5504
5505 // Add all classes to our internal class loader list here,
5506 // including classes in the bootstrap (null) class loader.
5507 const bool publicize = !is_internal();
5508
5509 _loader_data->add_class(ik, publicize);
5510
5511 set_klass_to_deallocate(ik);
5512
5513 assert(_field_info != nullptr, "invariant");
5514 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5515 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5516 "sanity");
5517
5518 assert(ik->is_instance_klass(), "sanity");
5519 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5520
5521 // Fill in information already parsed
5522 ik->set_should_verify_class(_need_verify);
5523
5524 // Not yet: supers are done below to support the new subtype-checking fields
5525 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5526 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5527 if (_field_info->_is_naturally_atomic && ik->is_inline_klass()) {
5528 ik->set_is_naturally_atomic();
5529 }
5530
5531 assert(_fac != nullptr, "invariant");
5532 ik->set_static_oop_field_count(_fac->count[STATIC_OOP] + _fac->count[STATIC_INLINE]);
5533
5534 // this transfers ownership of a lot of arrays from
5535 // the parser onto the InstanceKlass*
5536 apply_parsed_class_metadata(ik, _java_fields_count);
5537 if (ik->is_inline_klass()) {
5538 InlineKlass::cast(ik)->init_fixed_block();
5539 }
5540
5541 // can only set dynamic nest-host after static nest information is set
5542 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5543 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5544 }
5545
5546 // note that is not safe to use the fields in the parser from this point on
5547 assert(nullptr == _cp, "invariant");
5548 assert(nullptr == _fieldinfo_stream, "invariant");
5549 assert(nullptr == _fields_status, "invariant");
5550 assert(nullptr == _methods, "invariant");
5551 assert(nullptr == _inner_classes, "invariant");
5552 assert(nullptr == _nest_members, "invariant");
5553 assert(nullptr == _loadable_descriptors, "invariant");
5554 assert(nullptr == _combined_annotations, "invariant");
5555 assert(nullptr == _record_components, "invariant");
5556 assert(nullptr == _permitted_subclasses, "invariant");
5557 assert(nullptr == _inline_type_field_klasses, "invariant");
5558
5559 if (_has_localvariable_table) {
5560 ik->set_has_localvariable_table(true);
5561 }
5562
5563 if (_has_final_method) {
5564 ik->set_has_final_method();
5565 }
5566
5567 ik->copy_method_ordering(_method_ordering, CHECK);
5568 // The InstanceKlass::_methods_jmethod_ids cache
5569 // is managed on the assumption that the initial cache
5570 // size is equal to the number of methods in the class. If
5571 // that changes, then InstanceKlass::idnum_can_increment()
5572 // has to be changed accordingly.
5573 ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5574
5575 ik->set_this_class_index(_this_class_index);
5576
5577 if (_is_hidden) {
5578 // _this_class_index is a CONSTANT_Class entry that refers to this
5579 // hidden class itself. If this class needs to refer to its own methods
5580 // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5581 // _this_class_index. However, because this class is hidden (it's
5582 // not stored in SystemDictionary), _this_class_index cannot be resolved
5583 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5584 // Therefore, we must eagerly resolve _this_class_index now.
5585 ik->constants()->klass_at_put(_this_class_index, ik);
5586 }
5587
5588 ik->set_minor_version(_minor_version);
5589 ik->set_major_version(_major_version);
5590 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5591 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5592
5593 if (_must_be_atomic) {
5594 ik->set_must_be_atomic();
5595 }
5596 if (_is_implicitly_constructible) {
5597 ik->set_is_implicitly_constructible();
5598 }
5599 if (_is_hidden) {
5600 ik->set_is_hidden();
5601 }
5602
5603 // Set PackageEntry for this_klass
5604 oop cl = ik->class_loader();
5605 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5606 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5607 ik->set_package(cld, nullptr, CHECK);
5608
5609 const Array<Method*>* const methods = ik->methods();
5610 assert(methods != nullptr, "invariant");
5611 const int methods_len = methods->length();
5612
5613 check_methods_for_intrinsics(ik, methods);
5614
5615 // Fill in field values obtained by parse_classfile_attributes
5616 if (_parsed_annotations->has_any_annotations()) {
5617 _parsed_annotations->apply_to(ik);
5618 }
5685
5686 assert(_all_mirandas != nullptr, "invariant");
5687
5688 // Generate any default methods - default methods are public interface methods
5689 // that have a default implementation. This is new with Java 8.
5690 if (_has_nonstatic_concrete_methods) {
5691 DefaultMethods::generate_default_methods(ik,
5692 _all_mirandas,
5693 CHECK);
5694 }
5695
5696 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5697 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5698 !module_entry->has_default_read_edges()) {
5699 if (!module_entry->set_has_default_read_edges()) {
5700 // We won a potential race
5701 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5702 }
5703 }
5704
5705 bool all_fields_empty = true;
5706 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5707 if (!fs.access_flags().is_static()) {
5708 if (fs.field_descriptor().is_null_free_inline_type()) {
5709 Klass* k = ik->inline_type_field_klasses_array()->at(fs.index());
5710 assert(k->is_inline_klass(), "must be");
5711 if (!InlineKlass::cast(k)->is_empty_inline_type()) { all_fields_empty = false; }
5712 } else {
5713 all_fields_empty = false;
5714 }
5715 } else if (is_inline_type() && (fs.name() == vmSymbols::default_value_name())) {
5716 InlineKlass::cast(ik)->set_default_value_offset(ik->field_offset(fs.index()));
5717 }
5718 }
5719
5720 if (_is_empty_inline_type || (is_inline_type() && all_fields_empty)) {
5721 ik->set_is_empty_inline_type();
5722 }
5723
5724 if (is_inline_type()) {
5725 InlineKlass* vk = InlineKlass::cast(ik);
5726 vk->set_alignment(_alignment);
5727 vk->set_first_field_offset(_first_field_offset);
5728 vk->set_payload_size_in_bytes(_payload_size_in_bytes);
5729 vk->set_internal_null_marker_offset(_internal_null_marker_offset);
5730 InlineKlass::cast(ik)->initialize_calling_convention(CHECK);
5731 }
5732
5733 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5734
5735 if (!is_internal()) {
5736 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5737
5738 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5739 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5740 log_is_enabled(Info, class, preview)) {
5741 ResourceMark rm;
5742 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5743 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5744 }
5745
5746 if (log_is_enabled(Debug, class, resolve)) {
5747 ResourceMark rm;
5748 // print out the superclass.
5749 const char * from = ik->external_name();
5750 if (ik->java_super() != nullptr) {
5751 log_debug(class, resolve)("%s %s (super)",
5752 from,
5804 Symbol* name,
5805 ClassLoaderData* loader_data,
5806 const ClassLoadInfo* cl_info,
5807 Publicity pub_level,
5808 TRAPS) :
5809 _stream(stream),
5810 _class_name(nullptr),
5811 _loader_data(loader_data),
5812 _is_hidden(cl_info->is_hidden()),
5813 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5814 _orig_cp_size(0),
5815 _super_klass(),
5816 _cp(nullptr),
5817 _fieldinfo_stream(nullptr),
5818 _fields_status(nullptr),
5819 _methods(nullptr),
5820 _inner_classes(nullptr),
5821 _nest_members(nullptr),
5822 _nest_host(0),
5823 _permitted_subclasses(nullptr),
5824 _loadable_descriptors(nullptr),
5825 _record_components(nullptr),
5826 _local_interfaces(nullptr),
5827 _local_interface_indexes(nullptr),
5828 _transitive_interfaces(nullptr),
5829 _combined_annotations(nullptr),
5830 _class_annotations(nullptr),
5831 _class_type_annotations(nullptr),
5832 _fields_annotations(nullptr),
5833 _fields_type_annotations(nullptr),
5834 _klass(nullptr),
5835 _klass_to_deallocate(nullptr),
5836 _parsed_annotations(nullptr),
5837 _fac(nullptr),
5838 _field_info(nullptr),
5839 _inline_type_field_klasses(nullptr),
5840 _null_marker_offsets(nullptr),
5841 _temp_field_info(nullptr),
5842 _method_ordering(nullptr),
5843 _all_mirandas(nullptr),
5844 _vtable_size(0),
5845 _itable_size(0),
5846 _num_miranda_methods(0),
5847 _protection_domain(cl_info->protection_domain()),
5848 _access_flags(),
5849 _pub_level(pub_level),
5850 _bad_constant_seen(0),
5851 _synthetic_flag(false),
5852 _sde_length(false),
5853 _sde_buffer(nullptr),
5854 _sourcefile_index(0),
5855 _generic_signature_index(0),
5856 _major_version(0),
5857 _minor_version(0),
5858 _this_class_index(0),
5859 _super_class_index(0),
5860 _itfs_len(0),
5861 _java_fields_count(0),
5862 _need_verify(false),
5863 _relax_verify(false),
5864 _has_nonstatic_concrete_methods(false),
5865 _declares_nonstatic_concrete_methods(false),
5866 _has_localvariable_table(false),
5867 _has_final_method(false),
5868 _has_contended_fields(false),
5869 _has_inline_type_fields(false),
5870 _has_nonstatic_fields(false),
5871 _is_empty_inline_type(false),
5872 _is_naturally_atomic(false),
5873 _must_be_atomic(true),
5874 _is_implicitly_constructible(false),
5875 _has_loosely_consistent_annotation(false),
5876 _has_implicitly_constructible_annotation(false),
5877 _has_finalizer(false),
5878 _has_empty_finalizer(false),
5879 _max_bootstrap_specifier_index(-1) {
5880
5881 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5882 _class_name->increment_refcount();
5883
5884 assert(_loader_data != nullptr, "invariant");
5885 assert(stream != nullptr, "invariant");
5886 assert(_stream != nullptr, "invariant");
5887 assert(_stream->buffer() == _stream->current(), "invariant");
5888 assert(_class_name != nullptr, "invariant");
5889 assert(0 == _access_flags.as_int(), "invariant");
5890
5891 // Figure out whether we can skip format checking (matching classic VM behavior)
5892 if (CDSConfig::is_dumping_static_archive()) {
5893 // verify == true means it's a 'remote' class (i.e., non-boot class)
5894 // Verification decision is based on BytecodeVerificationRemote flag
5895 // for those classes.
5896 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote :
5906
5907 // Check if verification needs to be relaxed for this class file
5908 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5909 _relax_verify = relax_format_check_for(_loader_data);
5910
5911 parse_stream(stream, CHECK);
5912
5913 post_process_parsed_stream(stream, _cp, CHECK);
5914 }
5915
5916 void ClassFileParser::clear_class_metadata() {
5917 // metadata created before the instance klass is created. Must be
5918 // deallocated if classfile parsing returns an error.
5919 _cp = nullptr;
5920 _fieldinfo_stream = nullptr;
5921 _fields_status = nullptr;
5922 _methods = nullptr;
5923 _inner_classes = nullptr;
5924 _nest_members = nullptr;
5925 _permitted_subclasses = nullptr;
5926 _loadable_descriptors = nullptr;
5927 _combined_annotations = nullptr;
5928 _class_annotations = _class_type_annotations = nullptr;
5929 _fields_annotations = _fields_type_annotations = nullptr;
5930 _record_components = nullptr;
5931 _inline_type_field_klasses = nullptr;
5932 _null_marker_offsets = nullptr;
5933 }
5934
5935 // Destructor to clean up
5936 ClassFileParser::~ClassFileParser() {
5937 _class_name->decrement_refcount();
5938
5939 if (_cp != nullptr) {
5940 MetadataFactory::free_metadata(_loader_data, _cp);
5941 }
5942
5943 if (_fieldinfo_stream != nullptr) {
5944 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5945 }
5946
5947 if (_fields_status != nullptr) {
5948 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5949 }
5950
5951 if (_inline_type_field_klasses != nullptr) {
5952 MetadataFactory::free_array<InlineKlass*>(_loader_data, _inline_type_field_klasses);
5953 }
5954
5955 if (_null_marker_offsets != nullptr) {
5956 MetadataFactory::free_array<int>(_loader_data, _null_marker_offsets);
5957 }
5958
5959 if (_methods != nullptr) {
5960 // Free methods
5961 InstanceKlass::deallocate_methods(_loader_data, _methods);
5962 }
5963
5964 // beware of the Universe::empty_blah_array!!
5965 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5966 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5967 }
5968
5969 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5970 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5971 }
5972
5973 if (_record_components != nullptr) {
5974 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5975 }
5976
5977 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5978 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5979 }
5980
5981 if (_loadable_descriptors != nullptr && _loadable_descriptors != Universe::the_empty_short_array()) {
5982 MetadataFactory::free_array<u2>(_loader_data, _loadable_descriptors);
5983 }
5984
5985 // Free interfaces
5986 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5987 _local_interfaces, _transitive_interfaces);
5988
5989 if (_combined_annotations != nullptr) {
5990 // After all annotations arrays have been created, they are installed into the
5991 // Annotations object that will be assigned to the InstanceKlass being created.
5992
5993 // Deallocate the Annotations object and the installed annotations arrays.
5994 _combined_annotations->deallocate_contents(_loader_data);
5995
5996 // If the _combined_annotations pointer is non-null,
5997 // then the other annotations fields should have been cleared.
5998 assert(_class_annotations == nullptr, "Should have been cleared");
5999 assert(_class_type_annotations == nullptr, "Should have been cleared");
6000 assert(_fields_annotations == nullptr, "Should have been cleared");
6001 assert(_fields_type_annotations == nullptr, "Should have been cleared");
6002 } else {
6003 // If the annotations arrays were not installed into the Annotations object,
6004 // then they have to be deallocated explicitly.
6049 cp_size, CHECK);
6050
6051 _orig_cp_size = cp_size;
6052 if (is_hidden()) { // Add a slot for hidden class name.
6053 cp_size++;
6054 }
6055
6056 _cp = ConstantPool::allocate(_loader_data,
6057 cp_size,
6058 CHECK);
6059
6060 ConstantPool* const cp = _cp;
6061
6062 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6063
6064 assert(cp_size == (u2)cp->length(), "invariant");
6065
6066 // ACCESS FLAGS
6067 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
6068
6069 jint recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS;
6070 // JVM_ACC_MODULE is defined in JDK-9 and later.
6071 if (_major_version >= JAVA_9_VERSION) {
6072 recognized_modifiers |= JVM_ACC_MODULE;
6073 }
6074
6075 // Access flags
6076 jint flags = stream->get_u2_fast() & recognized_modifiers;
6077
6078 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6079 // Set abstract bit for old class files for backward compatibility
6080 flags |= JVM_ACC_ABSTRACT;
6081 }
6082
6083 // Fixing ACC_SUPER/ACC_IDENTITY for old class files
6084 if (!supports_inline_types()) {
6085 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
6086 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
6087 if (!is_module && !is_interface) {
6088 flags |= JVM_ACC_IDENTITY;
6089 }
6090 }
6091
6092
6093 // This class and superclass
6094 _this_class_index = stream->get_u2_fast();
6095 check_property(
6096 valid_cp_range(_this_class_index, cp_size) &&
6097 cp->tag_at(_this_class_index).is_unresolved_klass(),
6098 "Invalid this class index %u in constant pool in class file %s",
6099 _this_class_index, CHECK);
6100
6101 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
6102 assert(class_name_in_cp != nullptr, "class_name can't be null");
6103
6104 bool is_java_lang_Object = class_name_in_cp == vmSymbols::java_lang_Object();
6105
6106 verify_legal_class_modifiers(flags, nullptr, is_java_lang_Object, CHECK);
6107
6108 _access_flags.set_flags(flags);
6109
6110 short bad_constant = class_bad_constant_seen();
6111 if (bad_constant != 0) {
6112 // Do not throw CFE until after the access_flags are checked because if
6113 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6114 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
6115 return;
6116 }
6117
6118 // Don't need to check whether this class name is legal or not.
6119 // It has been checked when constant pool is parsed.
6120 // However, make sure it is not an array type.
6121 if (_need_verify) {
6122 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
6123 "Bad class name in class file %s",
6124 CHECK);
6125 }
6126
6127 #ifdef ASSERT
6128 // Basic sanity checks
6129 if (_is_hidden) {
6130 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
6131 }
6132 #endif
6133
6134 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
6135
6136 if (_is_hidden) {
6137 assert(_class_name != nullptr, "Unexpected null _class_name");
6177 }
6178 ls.cr();
6179 }
6180 }
6181
6182 // SUPERKLASS
6183 _super_class_index = stream->get_u2_fast();
6184 _super_klass = parse_super_class(cp,
6185 _super_class_index,
6186 _need_verify,
6187 CHECK);
6188
6189 // Interfaces
6190 _itfs_len = stream->get_u2_fast();
6191 parse_interfaces(stream,
6192 _itfs_len,
6193 cp,
6194 &_has_nonstatic_concrete_methods,
6195 CHECK);
6196
6197 // Fields (offsets are filled in later)
6198 _fac = new FieldAllocationCount();
6199 parse_fields(stream,
6200 _access_flags,
6201 _fac,
6202 cp,
6203 cp_size,
6204 &_java_fields_count,
6205 CHECK);
6206
6207 assert(_temp_field_info != nullptr, "invariant");
6208
6209 // Methods
6210 parse_methods(stream,
6211 is_interface(),
6212 !is_identity_class(),
6213 is_abstract_class(),
6214 &_has_localvariable_table,
6215 &_has_final_method,
6216 &_declares_nonstatic_concrete_methods,
6217 CHECK);
6218
6219 assert(_methods != nullptr, "invariant");
6220
6221 if (_declares_nonstatic_concrete_methods) {
6222 _has_nonstatic_concrete_methods = true;
6223 }
6224
6225 // Additional attributes/annotations
6226 _parsed_annotations = new ClassAnnotationCollector();
6227 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6228
6229 assert(_inner_classes != nullptr, "invariant");
6230
6231 // Finalize the Annotations metadata object,
6232 // now that all annotation arrays have been created.
6233 create_combined_annotations(CHECK);
6273 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
6274 // We have to update the resolved_klass_index and the name_index together
6275 // so extract the existing resolved_klass_index first.
6276 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
6277 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
6278 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
6279 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
6280 "Bad name_index");
6281 }
6282
6283 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
6284 ConstantPool* cp,
6285 TRAPS) {
6286 assert(stream != nullptr, "invariant");
6287 assert(stream->at_eos(), "invariant");
6288 assert(cp != nullptr, "invariant");
6289 assert(_loader_data != nullptr, "invariant");
6290
6291 if (_class_name == vmSymbols::java_lang_Object()) {
6292 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
6293 "java.lang.Object cannot implement an interface in class file %s",
6294 CHECK);
6295 }
6296 // We check super class after class file is parsed and format is checked
6297 if (_super_class_index > 0 && nullptr == _super_klass) {
6298 Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
6299 if (is_interface()) {
6300 // Before attempting to resolve the superclass, check for class format
6301 // errors not checked yet.
6302 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
6303 "Interfaces must have java.lang.Object as superclass in class file %s",
6304 CHECK);
6305 }
6306 Handle loader(THREAD, _loader_data->class_loader());
6307 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
6308 _super_klass = vmClasses::Object_klass();
6309 } else {
6310 _super_klass = (const InstanceKlass*)
6311 SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name,
6312 super_class_name,
6313 loader,
6314 _protection_domain,
6315 true,
6316 CHECK);
6317 }
6318 }
6319
6320 if (_super_klass != nullptr) {
6321 if (_super_klass->is_interface()) {
6322 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
6323 return;
6324 }
6325
6326 if (_super_klass->is_final()) {
6327 classfile_icce_error("class %s cannot inherit from final class %s", _super_klass, THREAD);
6328 return;
6329 }
6330
6331 if (EnableValhalla) {
6332 check_identity_and_value_modifiers(this, _super_klass, CHECK);
6333 }
6334
6335 if (_super_klass->has_nonstatic_concrete_methods()) {
6336 _has_nonstatic_concrete_methods = true;
6337 }
6338 }
6339
6340 if (_parsed_annotations->has_annotation(AnnotationCollector::_jdk_internal_LooselyConsistentValue) && _access_flags.is_identity_class()) {
6341 THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
6342 err_msg("class %s cannot have annotation jdk.internal.vm.annotation.LooselyConsistentValue, because it is not a value class",
6343 _class_name->as_klass_external_name()));
6344 }
6345 if (_parsed_annotations->has_annotation(AnnotationCollector::_jdk_internal_ImplicitlyConstructible) && _access_flags.is_identity_class()) {
6346 THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
6347 err_msg("class %s cannot have annotation jdk.internal.vm.annotation.ImplicitlyConstructible, because it is not a value class",
6348 _class_name->as_klass_external_name()));
6349 }
6350
6351 // Determining is the class allows tearing or not (default is not)
6352 // Test might need extensions when field inheritance is added for value classes
6353 if (EnableValhalla && !_access_flags.is_identity_class()) {
6354 if (_parsed_annotations->has_annotation(ClassAnnotationCollector::_jdk_internal_LooselyConsistentValue)
6355 && (_super_klass == vmClasses::Object_klass() || !_super_klass->must_be_atomic())) {
6356 _must_be_atomic = false;
6357 }
6358 if (_parsed_annotations->has_annotation(ClassAnnotationCollector::_jdk_internal_ImplicitlyConstructible)
6359 && (_super_klass == vmClasses::Object_klass() || _super_klass->is_implicitly_constructible())) {
6360 _is_implicitly_constructible = true;
6361 }
6362 // Apply VM options override
6363 if (*ForceNonTearable != '\0') {
6364 // Allow a command line switch to force the same atomicity property:
6365 const char* class_name_str = _class_name->as_C_string();
6366 if (StringUtils::class_list_match(ForceNonTearable, class_name_str)) {
6367 _must_be_atomic = true;
6368 }
6369 }
6370 }
6371
6372 int itfs_len = _local_interface_indexes == nullptr ? 0 : _local_interface_indexes->length();
6373 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK);
6374 if (_local_interface_indexes != nullptr) {
6375 for (int i = 0; i < _local_interface_indexes->length(); i++) {
6376 u2 interface_index = _local_interface_indexes->at(i);
6377 Klass* interf;
6378 if (cp->tag_at(interface_index).is_klass()) {
6379 interf = cp->resolved_klass_at(interface_index);
6380 } else {
6381 Symbol* const unresolved_klass = cp->klass_name_at(interface_index);
6382
6383 // Don't need to check legal name because it's checked when parsing constant pool.
6384 // But need to make sure it's not an array type.
6385 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
6386 "Bad interface name in class file %s", CHECK);
6387
6388 // Call resolve_super so class circularity is checked
6389 interf = SystemDictionary::resolve_with_circularity_detection_or_fail(
6390 _class_name,
6391 unresolved_klass,
6392 Handle(THREAD, _loader_data->class_loader()),
6393 _protection_domain,
6394 false,
6395 CHECK);
6396 }
6397
6398 if (!interf->is_interface()) {
6399 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6400 err_msg("class %s can not implement %s, because it is not an interface (%s)",
6401 _class_name->as_klass_external_name(),
6402 interf->external_name(),
6403 interf->class_in_module_of_loader()));
6404 }
6405
6406 if (EnableValhalla) {
6407 // Check modifiers and set carries_identity_modifier/carries_value_modifier flags
6408 check_identity_and_value_modifiers(this, InstanceKlass::cast(interf), CHECK);
6409 }
6410
6411 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
6412 _has_nonstatic_concrete_methods = true;
6413 }
6414 _local_interfaces->at_put(i, InstanceKlass::cast(interf));
6415 }
6416 }
6417 assert(_local_interfaces != nullptr, "invariant");
6418
6419 // Compute the transitive list of all unique interfaces implemented by this class
6420 _transitive_interfaces =
6421 compute_transitive_interfaces(_super_klass,
6422 _local_interfaces,
6423 _loader_data,
6424 CHECK);
6425
6426 assert(_transitive_interfaces != nullptr, "invariant");
6427
6428 // sort methods
6429 _method_ordering = sort_methods(_methods);
6430
6431 _all_mirandas = new GrowableArray<Method*>(20);
6432
6433 Handle loader(THREAD, _loader_data->class_loader());
6434 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6435 &_num_miranda_methods,
6436 _all_mirandas,
6437 _super_klass,
6438 _methods,
6439 _access_flags,
6440 _major_version,
6441 loader,
6442 _class_name,
6443 _local_interfaces);
6444
6445 // Size of Java itable (in words)
6446 _itable_size = is_interface() ? 0 :
6447 klassItable::compute_itable_size(_transitive_interfaces);
6448
6449 assert(_fac != nullptr, "invariant");
6450 assert(_parsed_annotations != nullptr, "invariant");
6451
6452 if (EnableValhalla) {
6453 _inline_type_field_klasses = MetadataFactory::new_array<InlineKlass*>(_loader_data,
6454 java_fields_count(),
6455 nullptr,
6456 CHECK);
6457 for (GrowableArrayIterator<FieldInfo> it = _temp_field_info->begin(); it != _temp_field_info->end(); ++it) {
6458 FieldInfo fieldinfo = *it;
6459 if (fieldinfo.access_flags().is_static()) continue; // Only non-static fields are processed at load time
6460 Symbol* sig = fieldinfo.signature(cp);
6461 if (fieldinfo.field_flags().is_null_free_inline_type()) {
6462 // Pre-load classes of null-free fields that are candidate for flattening
6463 TempNewSymbol s = Signature::strip_envelope(sig);
6464 if (s == _class_name) {
6465 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()));
6466 }
6467 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());
6468 Klass* klass = SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name, s, Handle(THREAD, _loader_data->class_loader()), _protection_domain, false, THREAD);
6469 if (HAS_PENDING_EXCEPTION) {
6470 log_warning(class, preload)("Preloading of class %s during loading of class %s (cause: null-free non-static field) failed: %s",
6471 s->as_C_string(), _class_name->as_C_string(), PENDING_EXCEPTION->klass()->name()->as_C_string());
6472 return; // Exception is still pending
6473 }
6474 assert(klass != nullptr, "Sanity check");
6475 if (klass->access_flags().is_identity_class()) {
6476 assert(klass->is_instance_klass(), "Sanity check");
6477 ResourceMark rm(THREAD);
6478 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6479 err_msg("Class %s expects class %s to be a value class, but it is an identity class",
6480 _class_name->as_C_string(),
6481 InstanceKlass::cast(klass)->external_name()));
6482 }
6483 if (klass->is_abstract()) {
6484 assert(klass->is_instance_klass(), "Sanity check");
6485 ResourceMark rm(THREAD);
6486 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6487 err_msg("Class %s expects class %s to be concrete value type, but it is an abstract class",
6488 _class_name->as_C_string(),
6489 InstanceKlass::cast(klass)->external_name()));
6490 }
6491 InlineKlass* vk = InlineKlass::cast(klass);
6492 if (!vk->is_implicitly_constructible()) {
6493 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6494 err_msg("class %s is not implicitly constructible and it is used in a null restricted non-static field (not supported)",
6495 klass->name()->as_C_string()));
6496 }
6497 _inline_type_field_klasses->at_put(fieldinfo.index(), vk);
6498 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());
6499 } else if (Signature::has_envelope(sig)) {
6500 // Preloading classes for nullable fields that are listed in the LoadableDescriptors attribute
6501 // Those classes would be required later for the flattening of nullable inline type fields
6502 TempNewSymbol name = Signature::strip_envelope(sig);
6503 if (name != _class_name && is_class_in_loadable_descriptors_attribute(sig)) {
6504 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());
6505 oop loader = loader_data()->class_loader();
6506 Klass* klass = SystemDictionary::resolve_with_circularity_detection_or_fail(_class_name, name, Handle(THREAD, loader), _protection_domain, false, THREAD);
6507 if (klass != nullptr) {
6508 if (klass->is_inline_klass()) {
6509 _inline_type_field_klasses->at_put(fieldinfo.index(), InlineKlass::cast(klass));
6510 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());
6511 } else {
6512 // Non value class are allowed by the current spec, but it could be an indication of an issue so let's log a warning
6513 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());
6514 }
6515 } else {
6516 log_warning(class, preload)("Preloading of class %s during loading of class %s (cause: field type in LoadableDescriptors attribute) failed : %s",
6517 name->as_C_string(), _class_name->as_C_string(), PENDING_EXCEPTION->klass()->name()->as_C_string());
6518 }
6519 // Loads triggered by the LoadableDescriptors attribute are speculative, failures must not impact loading of current class
6520 if (HAS_PENDING_EXCEPTION) {
6521 CLEAR_PENDING_EXCEPTION;
6522 }
6523 }
6524 }
6525 }
6526 }
6527
6528 _field_info = new FieldLayoutInfo();
6529 FieldLayoutBuilder lb(class_name(), loader_data(), super_klass(), _cp, /*_fields*/ _temp_field_info,
6530 _parsed_annotations->is_contended(), is_inline_type(),
6531 access_flags().is_abstract() && !access_flags().is_identity_class() && !access_flags().is_interface(),
6532 _field_info, _inline_type_field_klasses);
6533 lb.build_layout();
6534 if (is_inline_type()) {
6535 _alignment = lb.get_alignment();
6536 _first_field_offset = lb.get_first_field_offset();
6537 _payload_size_in_bytes = lb.get_payload_size_in_byte();
6538 _internal_null_marker_offset = lb.get_internal_null_marker_offset();
6539 }
6540 _has_inline_type_fields = _field_info->_has_inline_fields;
6541
6542 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
6543 _fieldinfo_stream =
6544 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
6545 injected_fields_count, loader_data(), CHECK);
6546
6547 _fields_status =
6548 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
6549 FieldStatus(0), CHECK);
6550 if (_field_info->_has_null_marker_offsets) {
6551 int idx = 0;
6552 _null_marker_offsets = MetadataFactory::new_array<int>(_loader_data, _temp_field_info->length(), 0, CHECK);
6553 for (GrowableArrayIterator<FieldInfo> it = _temp_field_info->begin(); it != _temp_field_info->end(); ++it, ++idx) {
6554 FieldInfo fieldinfo = *it;
6555 if (fieldinfo.field_flags().has_null_marker()) {
6556 assert(fieldinfo.null_marker_offset() != 0, "Invalid value");
6557 _null_marker_offsets->at_put(idx, fieldinfo.null_marker_offset());
6558 }
6559 }
6560 }
6561 }
6562
6563 void ClassFileParser::set_klass(InstanceKlass* klass) {
6564
6565 #ifdef ASSERT
6566 if (klass != nullptr) {
6567 assert(nullptr == _klass, "leaking?");
6568 }
6569 #endif
6570
6571 _klass = klass;
6572 }
6573
6574 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6575
6576 #ifdef ASSERT
6577 if (klass != nullptr) {
6578 assert(nullptr == _klass_to_deallocate, "leaking?");
6579 }
6580 #endif
|