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 "classfile/classFileParser.hpp"
26 #include "classfile/classFileStream.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/classLoaderData.inline.hpp"
29 #include "classfile/classLoadInfo.hpp"
30 #include "classfile/defaultMethods.hpp"
31 #include "classfile/fieldLayoutBuilder.hpp"
32 #include "classfile/javaClasses.inline.hpp"
33 #include "classfile/moduleEntry.hpp"
34 #include "classfile/packageEntry.hpp"
35 #include "classfile/symbolTable.hpp"
36 #include "classfile/systemDictionary.hpp"
37 #include "classfile/verificationType.hpp"
38 #include "classfile/verifier.hpp"
39 #include "classfile/vmClasses.hpp"
40 #include "classfile/vmSymbols.hpp"
41 #include "jvm.h"
42 #include "logging/log.hpp"
43 #include "logging/logStream.hpp"
44 #include "memory/allocation.hpp"
45 #include "memory/metadataFactory.hpp"
46 #include "memory/oopFactory.hpp"
47 #include "memory/resourceArea.hpp"
48 #include "memory/universe.hpp"
49 #include "oops/annotations.hpp"
50 #include "oops/constantPool.inline.hpp"
51 #include "oops/fieldInfo.hpp"
52 #include "oops/fieldStreams.inline.hpp"
53 #include "oops/instanceKlass.inline.hpp"
54 #include "oops/instanceMirrorKlass.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/klassVtable.hpp"
57 #include "oops/metadata.hpp"
58 #include "oops/method.inline.hpp"
59 #include "oops/oop.inline.hpp"
60 #include "oops/recordComponent.hpp"
61 #include "oops/symbol.hpp"
62 #include "prims/jvmtiExport.hpp"
63 #include "prims/jvmtiThreadState.hpp"
64 #include "runtime/arguments.hpp"
65 #include "runtime/fieldDescriptor.inline.hpp"
66 #include "runtime/handles.inline.hpp"
67 #include "runtime/javaCalls.hpp"
68 #include "runtime/os.hpp"
69 #include "runtime/perfData.hpp"
70 #include "runtime/reflection.hpp"
71 #include "runtime/safepointVerifiers.hpp"
72 #include "runtime/signature.hpp"
73 #include "runtime/timer.hpp"
74 #include "services/classLoadingService.hpp"
75 #include "services/threadService.hpp"
76 #include "utilities/align.hpp"
77 #include "utilities/bitMap.inline.hpp"
78 #include "utilities/copy.hpp"
79 #include "utilities/formatBuffer.hpp"
80 #include "utilities/exceptions.hpp"
81 #include "utilities/globalDefinitions.hpp"
82 #include "utilities/growableArray.hpp"
83 #include "utilities/macros.hpp"
84 #include "utilities/ostream.hpp"
85 #include "utilities/resourceHash.hpp"
86 #include "utilities/utf8.hpp"
87 #if INCLUDE_CDS
88 #include "classfile/systemDictionaryShared.hpp"
89 #endif
90 #if INCLUDE_JFR
91 #include "jfr/support/jfrTraceIdExtension.hpp"
92 #endif
93
94 // We generally try to create the oops directly when parsing, rather than
95 // allocating temporary data structures and copying the bytes twice. A
96 // temporary area is only needed when parsing utf8 entries in the constant
97 // pool and when parsing line number tables.
98
99 // We add assert in debug mode when class format is not checked.
100
101 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
102 #define JAVA_MIN_SUPPORTED_VERSION 45
103 #define JAVA_PREVIEW_MINOR_VERSION 65535
104
105 // Used for two backward compatibility reasons:
128 #define JAVA_12_VERSION 56
129
130 #define JAVA_13_VERSION 57
131
132 #define JAVA_14_VERSION 58
133
134 #define JAVA_15_VERSION 59
135
136 #define JAVA_16_VERSION 60
137
138 #define JAVA_17_VERSION 61
139
140 #define JAVA_18_VERSION 62
141
142 #define JAVA_19_VERSION 63
143
144 #define JAVA_20_VERSION 64
145
146 #define JAVA_21_VERSION 65
147
148 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
149 assert((bad_constant == JVM_CONSTANT_Module ||
150 bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
151 "Unexpected bad constant pool entry");
152 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
153 }
154
155 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
156 ConstantPool* cp,
157 const int length,
158 TRAPS) {
159 assert(stream != nullptr, "invariant");
160 assert(cp != nullptr, "invariant");
161
162 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
163 // this function (_current can be allocated in a register, with scalar
164 // replacement of aggregates). The _current pointer is copied back to
165 // stream() when this function returns. DON'T call another method within
166 // this method that uses stream().
167 const ClassFileStream cfs1 = *stream;
168 const ClassFileStream* const cfs = &cfs1;
169
170 debug_only(const u1* const old_current = stream->current();)
171
172 // Used for batching symbol allocations.
173 const char* names[SymbolTable::symbol_alloc_batch_size];
174 int lengths[SymbolTable::symbol_alloc_batch_size];
175 int indices[SymbolTable::symbol_alloc_batch_size];
176 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
177 int names_count = 0;
178
179 // parsing Index 0 is unused
180 for (int index = 1; index < length; index++) {
181 // Each of the following case guarantees one more byte in the stream
182 // for the following tag or the access_flags following constant pool,
183 // so we don't need bounds-check for reading tag.
184 const u1 tag = cfs->get_u1_fast();
185 switch (tag) {
186 case JVM_CONSTANT_Class : {
187 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
188 const u2 name_index = cfs->get_u2_fast();
189 cp->klass_index_at_put(index, name_index);
190 break;
191 }
192 case JVM_CONSTANT_Fieldref: {
193 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
194 const u2 class_index = cfs->get_u2_fast();
195 const u2 name_and_type_index = cfs->get_u2_fast();
196 cp->field_at_put(index, class_index, name_and_type_index);
197 break;
198 }
199 case JVM_CONSTANT_Methodref: {
200 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
201 const u2 class_index = cfs->get_u2_fast();
202 const u2 name_and_type_index = cfs->get_u2_fast();
203 cp->method_at_put(index, class_index, name_and_type_index);
204 break;
205 }
206 case JVM_CONSTANT_InterfaceMethodref: {
487 check_property(valid_symbol_at(name_ref_index),
488 "Invalid constant pool index %u in class file %s",
489 name_ref_index, CHECK);
490 check_property(valid_symbol_at(signature_ref_index),
491 "Invalid constant pool index %u in class file %s",
492 signature_ref_index, CHECK);
493 break;
494 }
495 case JVM_CONSTANT_Utf8:
496 break;
497 case JVM_CONSTANT_UnresolvedClass: // fall-through
498 case JVM_CONSTANT_UnresolvedClassInError: {
499 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
500 break;
501 }
502 case JVM_CONSTANT_ClassIndex: {
503 const int class_index = cp->klass_index_at(index);
504 check_property(valid_symbol_at(class_index),
505 "Invalid constant pool index %u in class file %s",
506 class_index, CHECK);
507 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
508 break;
509 }
510 case JVM_CONSTANT_StringIndex: {
511 const int string_index = cp->string_index_at(index);
512 check_property(valid_symbol_at(string_index),
513 "Invalid constant pool index %u in class file %s",
514 string_index, CHECK);
515 Symbol* const sym = cp->symbol_at(string_index);
516 cp->unresolved_string_at_put(index, sym);
517 break;
518 }
519 case JVM_CONSTANT_MethodHandle: {
520 const int ref_index = cp->method_handle_index_at(index);
521 check_property(valid_cp_range(ref_index, length),
522 "Invalid constant pool index %u in class file %s",
523 ref_index, CHECK);
524 const constantTag tag = cp->tag_at(ref_index);
525 const int ref_kind = cp->method_handle_ref_kind_at(index);
526
527 switch (ref_kind) {
687 cp->signature_ref_index_at(name_and_type_ref_index);
688 const Symbol* const name = cp->symbol_at(name_ref_index);
689 const Symbol* const signature = cp->symbol_at(signature_ref_index);
690 if (tag == JVM_CONSTANT_Fieldref) {
691 if (_need_verify) {
692 // Field name and signature are verified above, when iterating NameAndType_info.
693 // Need only to be sure signature is non-zero length and the right type.
694 if (Signature::is_method(signature)) {
695 throwIllegalSignature("Field", name, signature, CHECK);
696 }
697 }
698 } else {
699 if (_need_verify) {
700 // Method name and signature are individually verified above, when iterating
701 // NameAndType_info. Need to check here that signature is non-zero length and
702 // the right type.
703 if (!Signature::is_method(signature)) {
704 throwIllegalSignature("Method", name, signature, CHECK);
705 }
706 }
707 // If a class method name begins with '<', it must be "<init>" and have void signature.
708 const unsigned int name_len = name->utf8_length();
709 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
710 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
711 if (name != vmSymbols::object_initializer_name()) {
712 classfile_parse_error(
713 "Bad method name at constant pool index %u in class file %s",
714 name_ref_index, THREAD);
715 return;
716 } else if (!Signature::is_void_method(signature)) { // must have void signature.
717 throwIllegalSignature("Method", name, signature, CHECK);
718 }
719 }
720 }
721 break;
722 }
723 case JVM_CONSTANT_MethodHandle: {
724 const int ref_index = cp->method_handle_index_at(index);
725 const int ref_kind = cp->method_handle_ref_kind_at(index);
726 switch (ref_kind) {
727 case JVM_REF_invokeVirtual:
728 case JVM_REF_invokeStatic:
729 case JVM_REF_invokeSpecial:
730 case JVM_REF_newInvokeSpecial: {
731 const int name_and_type_ref_index =
732 cp->name_and_type_ref_index_at(ref_index);
733 const int name_ref_index =
734 cp->name_ref_index_at(name_and_type_ref_index);
735 const Symbol* const name = cp->symbol_at(name_ref_index);
736 if (ref_kind == JVM_REF_newInvokeSpecial) {
737 if (name != vmSymbols::object_initializer_name()) {
738 classfile_parse_error(
739 "Bad constructor name at constant pool index %u in class file %s",
740 name_ref_index, THREAD);
741 return;
742 }
743 } else {
744 if (name == vmSymbols::object_initializer_name()) {
745 classfile_parse_error(
746 "Bad method name at constant pool index %u in class file %s",
747 name_ref_index, THREAD);
748 return;
749 }
750 }
751 break;
752 }
753 // Other ref_kinds are already fully checked in previous pass.
754 } // switch(ref_kind)
755 break;
756 }
757 case JVM_CONSTANT_MethodType: {
758 const Symbol* const no_name = vmSymbols::type_name(); // place holder
759 const Symbol* const signature = cp->method_type_signature_at(index);
760 verify_legal_method_signature(no_name, signature, CHECK);
761 break;
762 }
763 case JVM_CONSTANT_Utf8: {
764 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
776
777 NameSigHash(Symbol* name, Symbol* sig) :
778 _name(name),
779 _sig(sig) {}
780
781 static unsigned int hash(NameSigHash const& namesig) {
782 return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
783 }
784
785 static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
786 return (e0._name == e1._name) &&
787 (e0._sig == e1._sig);
788 }
789 };
790
791 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
792 NameSigHash::HASH_ROW_SIZE,
793 AnyObj::RESOURCE_AREA, mtInternal,
794 &NameSigHash::hash, &NameSigHash::equals>;
795
796 // Side-effects: populates the _local_interfaces field
797 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
798 const int itfs_len,
799 ConstantPool* const cp,
800 bool* const has_nonstatic_concrete_methods,
801 TRAPS) {
802 assert(stream != nullptr, "invariant");
803 assert(cp != nullptr, "invariant");
804 assert(has_nonstatic_concrete_methods != nullptr, "invariant");
805
806 if (itfs_len == 0) {
807 _local_interfaces = Universe::the_empty_instance_klass_array();
808 } else {
809 assert(itfs_len > 0, "only called for len>0");
810 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK);
811
812 int index;
813 for (index = 0; index < itfs_len; index++) {
814 const u2 interface_index = stream->get_u2(CHECK);
815 Klass* interf;
816 check_property(
817 valid_klass_reference_at(interface_index),
818 "Interface name has bad constant pool index %u in class file %s",
819 interface_index, CHECK);
820 if (cp->tag_at(interface_index).is_klass()) {
821 interf = cp->resolved_klass_at(interface_index);
822 } else {
823 Symbol* const unresolved_klass = cp->klass_name_at(interface_index);
824
825 // Don't need to check legal name because it's checked when parsing constant pool.
826 // But need to make sure it's not an array type.
827 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
828 "Bad interface name in class file %s", CHECK);
829
830 // Call resolve_super so class circularity is checked
831 interf = SystemDictionary::resolve_super_or_fail(
832 _class_name,
833 unresolved_klass,
834 Handle(THREAD, _loader_data->class_loader()),
835 _protection_domain,
836 false,
837 CHECK);
838 }
839
840 if (!interf->is_interface()) {
841 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
842 err_msg("class %s can not implement %s, because it is not an interface (%s)",
843 _class_name->as_klass_external_name(),
844 interf->external_name(),
845 interf->class_in_module_of_loader()));
846 }
847
848 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
849 *has_nonstatic_concrete_methods = true;
850 }
851 _local_interfaces->at_put(index, InstanceKlass::cast(interf));
852 }
853
854 if (!_need_verify || itfs_len <= 1) {
855 return;
856 }
857
858 // Check if there's any duplicates in interfaces
859 ResourceMark rm(THREAD);
860 // Set containing interface names
861 ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
862 for (index = 0; index < itfs_len; index++) {
863 const InstanceKlass* const k = _local_interfaces->at(index);
864 Symbol* interface_name = k->name();
865 // If no duplicates, add (name, nullptr) in hashtable interface_names.
866 if (!interface_names->put(interface_name, 0)) {
867 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
868 interface_name->as_C_string(), THREAD);
869 return;
870 }
871 }
872 }
873 }
874
875 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
876 int constantvalue_index,
877 int signature_index,
878 TRAPS) const {
879 // Make sure the constant pool entry is of a type appropriate to this field
880 guarantee_property(
881 (constantvalue_index > 0 &&
882 constantvalue_index < cp->length()),
883 "Bad initial value index %u in ConstantValue attribute in class file %s",
884 constantvalue_index, CHECK);
1326 CHECK);
1327 parsed_annotations->set_field_annotations(a);
1328 a = assemble_annotations(runtime_visible_type_annotations,
1329 runtime_visible_type_annotations_length,
1330 runtime_invisible_type_annotations,
1331 runtime_invisible_type_annotations_length,
1332 CHECK);
1333 parsed_annotations->set_field_type_annotations(a);
1334 return;
1335 }
1336
1337
1338 // Field allocation types. Used for computing field offsets.
1339
1340 enum FieldAllocationType {
1341 STATIC_OOP, // Oops
1342 STATIC_BYTE, // Boolean, Byte, char
1343 STATIC_SHORT, // shorts
1344 STATIC_WORD, // ints
1345 STATIC_DOUBLE, // aligned long or double
1346 NONSTATIC_OOP,
1347 NONSTATIC_BYTE,
1348 NONSTATIC_SHORT,
1349 NONSTATIC_WORD,
1350 NONSTATIC_DOUBLE,
1351 MAX_FIELD_ALLOCATION_TYPE,
1352 BAD_ALLOCATION_TYPE = -1
1353 };
1354
1355 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1356 BAD_ALLOCATION_TYPE, // 0
1357 BAD_ALLOCATION_TYPE, // 1
1358 BAD_ALLOCATION_TYPE, // 2
1359 BAD_ALLOCATION_TYPE, // 3
1360 NONSTATIC_BYTE , // T_BOOLEAN = 4,
1361 NONSTATIC_SHORT, // T_CHAR = 5,
1362 NONSTATIC_WORD, // T_FLOAT = 6,
1363 NONSTATIC_DOUBLE, // T_DOUBLE = 7,
1364 NONSTATIC_BYTE, // T_BYTE = 8,
1365 NONSTATIC_SHORT, // T_SHORT = 9,
1366 NONSTATIC_WORD, // T_INT = 10,
1367 NONSTATIC_DOUBLE, // T_LONG = 11,
1368 NONSTATIC_OOP, // T_OBJECT = 12,
1369 NONSTATIC_OOP, // T_ARRAY = 13,
1370 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1371 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1372 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1373 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1374 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1375 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1376 BAD_ALLOCATION_TYPE, // 0
1377 BAD_ALLOCATION_TYPE, // 1
1378 BAD_ALLOCATION_TYPE, // 2
1379 BAD_ALLOCATION_TYPE, // 3
1380 STATIC_BYTE , // T_BOOLEAN = 4,
1381 STATIC_SHORT, // T_CHAR = 5,
1382 STATIC_WORD, // T_FLOAT = 6,
1383 STATIC_DOUBLE, // T_DOUBLE = 7,
1384 STATIC_BYTE, // T_BYTE = 8,
1385 STATIC_SHORT, // T_SHORT = 9,
1386 STATIC_WORD, // T_INT = 10,
1387 STATIC_DOUBLE, // T_LONG = 11,
1388 STATIC_OOP, // T_OBJECT = 12,
1389 STATIC_OOP, // T_ARRAY = 13,
1390 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1391 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1392 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1393 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1394 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1395 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1396 };
1397
1398 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {
1399 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1400 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1401 assert(result != BAD_ALLOCATION_TYPE, "bad type");
1402 return result;
1403 }
1404
1405 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1406 public:
1407 u2 count[MAX_FIELD_ALLOCATION_TYPE];
1408
1409 FieldAllocationCount() {
1410 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1411 count[i] = 0;
1412 }
1413 }
1414
1415 void update(bool is_static, BasicType type) {
1416 FieldAllocationType atype = basic_type_to_atype(is_static, type);
1417 if (atype != BAD_ALLOCATION_TYPE) {
1418 // Make sure there is no overflow with injected fields.
1419 assert(count[atype] < 0xFFFF, "More than 65535 fields");
1420 count[atype]++;
1421 }
1422 }
1423 };
1424
1425 // Side-effects: populates the _fields, _fields_annotations,
1426 // _fields_type_annotations fields
1427 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1428 bool is_interface,
1429 FieldAllocationCount* const fac,
1430 ConstantPool* cp,
1431 const int cp_size,
1432 u2* const java_fields_count_ptr,
1433 TRAPS) {
1434
1435 assert(cfs != nullptr, "invariant");
1436 assert(fac != nullptr, "invariant");
1437 assert(cp != nullptr, "invariant");
1438 assert(java_fields_count_ptr != nullptr, "invariant");
1439
1440 assert(nullptr == _fields_annotations, "invariant");
1441 assert(nullptr == _fields_type_annotations, "invariant");
1442
1443 cfs->guarantee_more(2, CHECK); // length
1444 const u2 length = cfs->get_u2_fast();
1445 *java_fields_count_ptr = length;
1446
1447 int num_injected = 0;
1448 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1449 &num_injected);
1450 const int total_fields = length + num_injected;
1451
1452 // Allocate a temporary resource array to collect field data.
1453 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1454 _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1455
1456 ResourceMark rm(THREAD);
1457 for (int n = 0; n < length; n++) {
1458 // access_flags, name_index, descriptor_index, attributes_count
1459 cfs->guarantee_more(8, CHECK);
1460
1461 AccessFlags access_flags;
1462 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1463 verify_legal_field_modifiers(flags, is_interface, CHECK);
1464 access_flags.set_flags(flags);
1465 FieldInfo::FieldFlags fieldFlags(0);
1466
1467 const u2 name_index = cfs->get_u2_fast();
1468 check_property(valid_symbol_at(name_index),
1469 "Invalid constant pool index %u for field name in class file %s",
1470 name_index, CHECK);
1471 const Symbol* const name = cp->symbol_at(name_index);
1472 verify_legal_field_name(name, CHECK);
1473
1474 const u2 signature_index = cfs->get_u2_fast();
1475 check_property(valid_symbol_at(signature_index),
1476 "Invalid constant pool index %u for field signature in class file %s",
1477 signature_index, CHECK);
1478 const Symbol* const sig = cp->symbol_at(signature_index);
1479 verify_legal_field_signature(name, sig, CHECK);
1480
1481 u2 constantvalue_index = 0;
1482 bool is_synthetic = false;
1483 u2 generic_signature_index = 0;
1484 const bool is_static = access_flags.is_static();
1485 FieldAnnotationCollector parsed_annotations(_loader_data);
1486
1487 const u2 attributes_count = cfs->get_u2_fast();
1488 if (attributes_count > 0) {
1489 parse_field_attributes(cfs,
1490 attributes_count,
1491 is_static,
1492 signature_index,
1493 &constantvalue_index,
1494 &is_synthetic,
1495 &generic_signature_index,
1496 &parsed_annotations,
1497 CHECK);
1498
1499 if (parsed_annotations.field_annotations() != nullptr) {
1511 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1512 length,
1513 nullptr,
1514 CHECK);
1515 }
1516 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1517 parsed_annotations.set_field_type_annotations(nullptr);
1518 }
1519
1520 if (is_synthetic) {
1521 access_flags.set_is_synthetic();
1522 }
1523 if (generic_signature_index != 0) {
1524 fieldFlags.update_generic(true);
1525 }
1526 }
1527
1528 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1529
1530 // Update FieldAllocationCount for this kind of field
1531 fac->update(is_static, type);
1532
1533 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1534 fi.set_index(n);
1535 if (fieldFlags.is_generic()) {
1536 fi.set_generic_signature_index(generic_signature_index);
1537 }
1538 parsed_annotations.apply_to(&fi);
1539 if (fi.field_flags().is_contended()) {
1540 _has_contended_fields = true;
1541 }
1542 _temp_field_info->append(fi);
1543 }
1544 assert(_temp_field_info->length() == length, "Must be");
1545
1546 int index = length;
1547 if (num_injected != 0) {
1548 for (int n = 0; n < num_injected; n++) {
1549 // Check for duplicates
1550 if (injected[n].may_be_java) {
1551 const Symbol* const name = injected[n].name();
1559 duplicate = true;
1560 break;
1561 }
1562 }
1563 if (duplicate) {
1564 // These will be removed from the field array at the end
1565 continue;
1566 }
1567 }
1568
1569 // Injected field
1570 FieldInfo::FieldFlags fflags(0);
1571 fflags.update_injected(true);
1572 AccessFlags aflags;
1573 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1574 fi.set_index(index);
1575 _temp_field_info->append(fi);
1576
1577 // Update FieldAllocationCount for this kind of field
1578 const BasicType type = Signature::basic_type(injected[n].signature());
1579 fac->update(false, type);
1580 index++;
1581 }
1582 }
1583
1584 assert(_temp_field_info->length() == index, "Must be");
1585
1586 if (_need_verify && length > 1) {
1587 // Check duplicated fields
1588 ResourceMark rm(THREAD);
1589 // Set containing name-signature pairs
1590 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1591 for (int i = 0; i < _temp_field_info->length(); i++) {
1592 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1593 _temp_field_info->adr_at(i)->signature(_cp));
1594 // If no duplicates, add name/signature in hashtable names_and_sigs.
1595 if(!names_and_sigs->put(name_and_sig, 0)) {
1596 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1597 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1598 return;
1599 }
1600 }
1601 }
1602 }
1603
1847 "Exception name has bad type at constant pool %u in class file %s",
1848 checked_exception, CHECK_NULL);
1849 }
1850 }
1851 // check exceptions attribute length
1852 if (_need_verify) {
1853 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1854 sizeof(u2) * size),
1855 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1856 }
1857 return checked_exceptions_start;
1858 }
1859
1860 void ClassFileParser::throwIllegalSignature(const char* type,
1861 const Symbol* name,
1862 const Symbol* sig,
1863 TRAPS) const {
1864 assert(name != nullptr, "invariant");
1865 assert(sig != nullptr, "invariant");
1866
1867 ResourceMark rm(THREAD);
1868 Exceptions::fthrow(THREAD_AND_LOCATION,
1869 vmSymbols::java_lang_ClassFormatError(),
1870 "%s \"%s\" in class %s has illegal signature \"%s\"", type,
1871 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
1872 }
1873
1874 AnnotationCollector::ID
1875 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
1876 const Symbol* name,
1877 const bool can_access_vm_annotations) {
1878 const vmSymbolID sid = vmSymbols::find_sid(name);
1879 // Privileged code can use all annotations. Other code silently drops some.
1880 const bool privileged = loader_data->is_boot_class_loader_data() ||
1881 loader_data->is_platform_class_loader_data() ||
1882 can_access_vm_annotations;
1883 switch (sid) {
1884 case VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
1885 if (_location != _in_method) break; // only allow for methods
1886 if (!privileged) break; // only allow in privileged code
1887 return _method_CallerSensitive;
1888 }
1889 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
1890 if (_location != _in_method) break; // only allow for methods
1891 if (!privileged) break; // only allow in privileged code
2153 runtime_invisible_type_annotations_length > 0) {
2154 a = assemble_annotations(runtime_visible_type_annotations,
2155 runtime_visible_type_annotations_length,
2156 runtime_invisible_type_annotations,
2157 runtime_invisible_type_annotations_length,
2158 CHECK);
2159 cm->set_type_annotations(a);
2160 }
2161 }
2162
2163
2164 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2165 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2166 // Method* to save footprint, so we only know the size of the resulting Method* when the
2167 // entire method attribute is parsed.
2168 //
2169 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2170
2171 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2172 bool is_interface,
2173 const ConstantPool* cp,
2174 bool* const has_localvariable_table,
2175 TRAPS) {
2176 assert(cfs != nullptr, "invariant");
2177 assert(cp != nullptr, "invariant");
2178 assert(has_localvariable_table != nullptr, "invariant");
2179
2180 ResourceMark rm(THREAD);
2181 // Parse fixed parts:
2182 // access_flags, name_index, descriptor_index, attributes_count
2183 cfs->guarantee_more(8, CHECK_NULL);
2184
2185 int flags = cfs->get_u2_fast();
2186 const u2 name_index = cfs->get_u2_fast();
2187 const int cp_size = cp->length();
2188 check_property(
2189 valid_symbol_at(name_index),
2190 "Illegal constant pool index %u for method name in class file %s",
2191 name_index, CHECK_NULL);
2192 const Symbol* const name = cp->symbol_at(name_index);
2194
2195 const u2 signature_index = cfs->get_u2_fast();
2196 guarantee_property(
2197 valid_symbol_at(signature_index),
2198 "Illegal constant pool index %u for method signature in class file %s",
2199 signature_index, CHECK_NULL);
2200 const Symbol* const signature = cp->symbol_at(signature_index);
2201
2202 if (name == vmSymbols::class_initializer_name()) {
2203 // We ignore the other access flags for a valid class initializer.
2204 // (JVM Spec 2nd ed., chapter 4.6)
2205 if (_major_version < 51) { // backward compatibility
2206 flags = JVM_ACC_STATIC;
2207 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2208 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2209 } else {
2210 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2211 return nullptr;
2212 }
2213 } else {
2214 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2215 }
2216
2217 if (name == vmSymbols::object_initializer_name() && is_interface) {
2218 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2219 return nullptr;
2220 }
2221
2222 int args_size = -1; // only used when _need_verify is true
2223 if (_need_verify) {
2224 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2225 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2226 verify_legal_method_signature(name, signature, CHECK_NULL);
2227 if (args_size > MAX_ARGS_SIZE) {
2228 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2229 return nullptr;
2230 }
2231 }
2232
2233 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2234
2235 // Default values for code and exceptions attribute elements
2236 u2 max_stack = 0;
2237 u2 max_locals = 0;
2238 u4 code_length = 0;
2239 const u1* code_start = 0;
2766 if (m->is_empty_method()) {
2767 _has_empty_finalizer = true;
2768 } else {
2769 _has_finalizer = true;
2770 }
2771 }
2772 if (name == vmSymbols::object_initializer_name() &&
2773 signature == vmSymbols::void_method_signature() &&
2774 m->is_vanilla_constructor()) {
2775 _has_vanilla_constructor = true;
2776 }
2777
2778 NOT_PRODUCT(m->verify());
2779 return m;
2780 }
2781
2782
2783 // Side-effects: populates the _methods field in the parser
2784 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2785 bool is_interface,
2786 bool* const has_localvariable_table,
2787 bool* has_final_method,
2788 bool* declares_nonstatic_concrete_methods,
2789 TRAPS) {
2790 assert(cfs != nullptr, "invariant");
2791 assert(has_localvariable_table != nullptr, "invariant");
2792 assert(has_final_method != nullptr, "invariant");
2793 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2794
2795 assert(nullptr == _methods, "invariant");
2796
2797 cfs->guarantee_more(2, CHECK); // length
2798 const u2 length = cfs->get_u2_fast();
2799 if (length == 0) {
2800 _methods = Universe::the_empty_method_array();
2801 } else {
2802 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2803 length,
2804 nullptr,
2805 CHECK);
2806
2807 for (int index = 0; index < length; index++) {
2808 Method* method = parse_method(cfs,
2809 is_interface,
2810 _cp,
2811 has_localvariable_table,
2812 CHECK);
2813
2814 if (method->is_final()) {
2815 *has_final_method = true;
2816 }
2817 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2818 // used for interface initialization, and default method inheritance analysis
2819 if (is_interface && !(*declares_nonstatic_concrete_methods)
2820 && !method->is_abstract() && !method->is_static()) {
2821 *declares_nonstatic_concrete_methods = true;
2822 }
2823 _methods->at_put(index, method);
2824 }
2825
2826 if (_need_verify && length > 1) {
2827 // Check duplicated methods
2828 ResourceMark rm(THREAD);
2829 // Set containing name-signature pairs
3055 valid_klass_reference_at(outer_class_info_index),
3056 "outer_class_info_index %u has bad constant type in class file %s",
3057 outer_class_info_index, CHECK_0);
3058
3059 if (outer_class_info_index != 0) {
3060 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3061 char* bytes = (char*)outer_class_name->bytes();
3062 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3063 "Outer class is an array class in class file %s", CHECK_0);
3064 }
3065 // Inner class name
3066 const u2 inner_name_index = cfs->get_u2_fast();
3067 check_property(
3068 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3069 "inner_name_index %u has bad constant type in class file %s",
3070 inner_name_index, CHECK_0);
3071 if (_need_verify) {
3072 guarantee_property(inner_class_info_index != outer_class_info_index,
3073 "Class is both outer and inner class in class file %s", CHECK_0);
3074 }
3075 // Access flags
3076 jint flags;
3077 // JVM_ACC_MODULE is defined in JDK-9 and later.
3078 if (_major_version >= JAVA_9_VERSION) {
3079 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3080 } else {
3081 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3082 }
3083 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3084 // Set abstract bit for old class files for backward compatibility
3085 flags |= JVM_ACC_ABSTRACT;
3086 }
3087 verify_legal_class_modifiers(flags, CHECK_0);
3088 AccessFlags inner_access_flags(flags);
3089
3090 inner_classes->at_put(index++, inner_class_info_index);
3091 inner_classes->at_put(index++, outer_class_info_index);
3092 inner_classes->at_put(index++, inner_name_index);
3093 inner_classes->at_put(index++, inner_access_flags.as_short());
3094 }
3095
3096 // Check for circular and duplicate entries.
3097 bool has_circularity = false;
3098 if (_need_verify) {
3099 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3100 if (has_circularity) {
3101 // If circularity check failed then ignore InnerClasses attribute.
3102 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3103 index = 0;
3104 if (parsed_enclosingmethod_attribute) {
3105 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3106 _inner_classes = inner_classes;
3107 } else {
3171 if (length > 0) {
3172 int index = 0;
3173 cfs->guarantee_more(2 * length, CHECK_0);
3174 for (int n = 0; n < length; n++) {
3175 const u2 class_info_index = cfs->get_u2_fast();
3176 check_property(
3177 valid_klass_reference_at(class_info_index),
3178 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3179 class_info_index, CHECK_0);
3180 permitted_subclasses->at_put(index++, class_info_index);
3181 }
3182 assert(index == size, "wrong size");
3183 }
3184
3185 // Restore buffer's current position.
3186 cfs->set_current(current_mark);
3187
3188 return length;
3189 }
3190
3191 // Record {
3192 // u2 attribute_name_index;
3193 // u4 attribute_length;
3194 // u2 components_count;
3195 // component_info components[components_count];
3196 // }
3197 // component_info {
3198 // u2 name_index;
3199 // u2 descriptor_index
3200 // u2 attributes_count;
3201 // attribute_info_attributes[attributes_count];
3202 // }
3203 u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3204 const ConstantPool* cp,
3205 const u1* const record_attribute_start,
3206 TRAPS) {
3207 const u1* const current_mark = cfs->current();
3208 int components_count = 0;
3209 unsigned int calculate_attr_size = 0;
3210 if (record_attribute_start != nullptr) {
3455 }
3456 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3457 "Bad length on BootstrapMethods in class file %s",
3458 CHECK);
3459 }
3460
3461 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3462 ConstantPool* cp,
3463 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3464 TRAPS) {
3465 assert(cfs != nullptr, "invariant");
3466 assert(cp != nullptr, "invariant");
3467 assert(parsed_annotations != nullptr, "invariant");
3468
3469 // Set inner classes attribute to default sentinel
3470 _inner_classes = Universe::the_empty_short_array();
3471 // Set nest members attribute to default sentinel
3472 _nest_members = Universe::the_empty_short_array();
3473 // Set _permitted_subclasses attribute to default sentinel
3474 _permitted_subclasses = Universe::the_empty_short_array();
3475 cfs->guarantee_more(2, CHECK); // attributes_count
3476 u2 attributes_count = cfs->get_u2_fast();
3477 bool parsed_sourcefile_attribute = false;
3478 bool parsed_innerclasses_attribute = false;
3479 bool parsed_nest_members_attribute = false;
3480 bool parsed_permitted_subclasses_attribute = false;
3481 bool parsed_nest_host_attribute = false;
3482 bool parsed_record_attribute = false;
3483 bool parsed_enclosingmethod_attribute = false;
3484 bool parsed_bootstrap_methods_attribute = false;
3485 const u1* runtime_visible_annotations = nullptr;
3486 int runtime_visible_annotations_length = 0;
3487 const u1* runtime_invisible_annotations = nullptr;
3488 int runtime_invisible_annotations_length = 0;
3489 const u1* runtime_visible_type_annotations = nullptr;
3490 int runtime_visible_type_annotations_length = 0;
3491 const u1* runtime_invisible_type_annotations = nullptr;
3492 int runtime_invisible_type_annotations_length = 0;
3493 bool runtime_invisible_type_annotations_exists = false;
3494 bool runtime_invisible_annotations_exists = false;
3495 bool parsed_source_debug_ext_annotations_exist = false;
3496 const u1* inner_classes_attribute_start = nullptr;
3497 u4 inner_classes_attribute_length = 0;
3498 u2 enclosing_method_class_index = 0;
3499 u2 enclosing_method_method_index = 0;
3500 const u1* nest_members_attribute_start = nullptr;
3501 u4 nest_members_attribute_length = 0;
3502 const u1* record_attribute_start = nullptr;
3503 u4 record_attribute_length = 0;
3504 const u1* permitted_subclasses_attribute_start = nullptr;
3505 u4 permitted_subclasses_attribute_length = 0;
3506
3507 // Iterate over attributes
3508 while (attributes_count--) {
3509 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3510 const u2 attribute_name_index = cfs->get_u2_fast();
3511 const u4 attribute_length = cfs->get_u4_fast();
3512 check_property(
3513 valid_symbol_at(attribute_name_index),
3514 "Attribute name has bad constant pool index %u in class file %s",
3515 attribute_name_index, CHECK);
3516 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3517 if (tag == vmSymbols::tag_source_file()) {
3518 // Check for SourceFile tag
3519 if (_need_verify) {
3520 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3521 }
3522 if (parsed_sourcefile_attribute) {
3523 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3524 return;
3525 } else {
3712 return;
3713 }
3714 parsed_record_attribute = true;
3715 record_attribute_start = cfs->current();
3716 record_attribute_length = attribute_length;
3717 } else if (_major_version >= JAVA_17_VERSION) {
3718 if (tag == vmSymbols::tag_permitted_subclasses()) {
3719 if (parsed_permitted_subclasses_attribute) {
3720 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3721 return;
3722 }
3723 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3724 if (_access_flags.is_final()) {
3725 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3726 return;
3727 }
3728 parsed_permitted_subclasses_attribute = true;
3729 permitted_subclasses_attribute_start = cfs->current();
3730 permitted_subclasses_attribute_length = attribute_length;
3731 }
3732 }
3733 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3734 cfs->skip_u1(attribute_length, CHECK);
3735 } else {
3736 // Unknown attribute
3737 cfs->skip_u1(attribute_length, CHECK);
3738 }
3739 } else {
3740 // Unknown attribute
3741 cfs->skip_u1(attribute_length, CHECK);
3742 }
3743 } else {
3744 // Unknown attribute
3745 cfs->skip_u1(attribute_length, CHECK);
3746 }
3747 }
3748 _class_annotations = assemble_annotations(runtime_visible_annotations,
3749 runtime_visible_annotations_length,
3750 runtime_invisible_annotations,
3751 runtime_invisible_annotations_length,
3792 CHECK);
3793 if (_need_verify) {
3794 guarantee_property(record_attribute_length == calculated_attr_length,
3795 "Record attribute has wrong length in class file %s",
3796 CHECK);
3797 }
3798 }
3799
3800 if (parsed_permitted_subclasses_attribute) {
3801 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3802 cfs,
3803 permitted_subclasses_attribute_start,
3804 CHECK);
3805 if (_need_verify) {
3806 guarantee_property(
3807 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3808 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3809 }
3810 }
3811
3812 if (_max_bootstrap_specifier_index >= 0) {
3813 guarantee_property(parsed_bootstrap_methods_attribute,
3814 "Missing BootstrapMethods attribute in class file %s", CHECK);
3815 }
3816 }
3817
3818 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3819 assert(k != nullptr, "invariant");
3820
3821 if (_synthetic_flag)
3822 k->set_is_synthetic();
3823 if (_sourcefile_index != 0) {
3824 k->set_source_file_name_index(_sourcefile_index);
3825 }
3826 if (_generic_signature_index != 0) {
3827 k->set_generic_signature_index(_generic_signature_index);
3828 }
3829 if (_sde_buffer != nullptr) {
3830 k->set_source_debug_extension(_sde_buffer, _sde_length);
3831 }
3857 _class_annotations = nullptr;
3858 _class_type_annotations = nullptr;
3859 _fields_annotations = nullptr;
3860 _fields_type_annotations = nullptr;
3861 }
3862
3863 // Transfer ownership of metadata allocated to the InstanceKlass.
3864 void ClassFileParser::apply_parsed_class_metadata(
3865 InstanceKlass* this_klass,
3866 int java_fields_count) {
3867 assert(this_klass != nullptr, "invariant");
3868
3869 _cp->set_pool_holder(this_klass);
3870 this_klass->set_constants(_cp);
3871 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3872 this_klass->set_fields_status(_fields_status);
3873 this_klass->set_methods(_methods);
3874 this_klass->set_inner_classes(_inner_classes);
3875 this_klass->set_nest_members(_nest_members);
3876 this_klass->set_nest_host_index(_nest_host);
3877 this_klass->set_annotations(_combined_annotations);
3878 this_klass->set_permitted_subclasses(_permitted_subclasses);
3879 this_klass->set_record_components(_record_components);
3880 // Delay the setting of _local_interfaces and _transitive_interfaces until after
3881 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3882 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3883 // its _super. If an OOM occurs while loading the current klass, its _super field
3884 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3885 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3886 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3887
3888 // Clear out these fields so they don't get deallocated by the destructor
3889 clear_class_metadata();
3890 }
3891
3892 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3893 int runtime_visible_annotations_length,
3894 const u1* const runtime_invisible_annotations,
3895 int runtime_invisible_annotations_length,
3896 TRAPS) {
3908 }
3909 if (runtime_invisible_annotations != nullptr) {
3910 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3911 int append = runtime_visible_annotations_length+i;
3912 annotations->at_put(append, runtime_invisible_annotations[i]);
3913 }
3914 }
3915 }
3916 return annotations;
3917 }
3918
3919 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3920 const int super_class_index,
3921 const bool need_verify,
3922 TRAPS) {
3923 assert(cp != nullptr, "invariant");
3924 const InstanceKlass* super_klass = nullptr;
3925
3926 if (super_class_index == 0) {
3927 check_property(_class_name == vmSymbols::java_lang_Object(),
3928 "Invalid superclass index %u in class file %s",
3929 super_class_index,
3930 CHECK_NULL);
3931 } else {
3932 check_property(valid_klass_reference_at(super_class_index),
3933 "Invalid superclass index %u in class file %s",
3934 super_class_index,
3935 CHECK_NULL);
3936 // The class name should be legal because it is checked when parsing constant pool.
3937 // However, make sure it is not an array type.
3938 bool is_array = false;
3939 if (cp->tag_at(super_class_index).is_klass()) {
3940 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3941 if (need_verify)
3942 is_array = super_klass->is_array_klass();
3943 } else if (need_verify) {
3944 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3945 }
3946 if (need_verify) {
3947 guarantee_property(!is_array,
3948 "Bad superclass name in class file %s", CHECK_NULL);
3949 }
3950 }
3951 return super_klass;
3952 }
3953
3954 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
3955 _max_nonstatic_oop_maps = max_blocks;
3956 _nonstatic_oop_map_count = 0;
3957 if (max_blocks == 0) {
3958 _nonstatic_oop_maps = nullptr;
3959 } else {
3960 _nonstatic_oop_maps =
3961 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
3962 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3963 }
3964 }
3965
3966 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
4050 }
4051
4052 void OopMapBlocksBuilder::print_on(outputStream* st) const {
4053 st->print_cr(" OopMapBlocks: %3d /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);
4054 if (_nonstatic_oop_map_count > 0) {
4055 OopMapBlock* map = _nonstatic_oop_maps;
4056 OopMapBlock* last_map = last_oop_map();
4057 assert(map <= last_map, "Last less than first");
4058 while (map <= last_map) {
4059 st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(),
4060 map->offset() + map->offset_span() - heapOopSize, map->count());
4061 map++;
4062 }
4063 }
4064 }
4065
4066 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
4067 print_on(st);
4068 }
4069
4070 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4071 assert(ik != nullptr, "invariant");
4072
4073 const InstanceKlass* const super = ik->java_super();
4074
4075 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4076 // in which case we don't have to register objects as finalizable
4077 if (!_has_empty_finalizer) {
4078 if (_has_finalizer ||
4079 (super != nullptr && super->has_finalizer())) {
4080 ik->set_has_finalizer();
4081 }
4082 }
4083
4084 #ifdef ASSERT
4085 bool f = false;
4086 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4087 vmSymbols::void_method_signature());
4088 if (InstanceKlass::is_finalization_enabled() &&
4089 (m != nullptr) && !m->is_empty_method()) {
4090 f = true;
4091 }
4092
4093 // Spec doesn't prevent agent from redefinition of empty finalizer.
4094 // Despite the fact that it's generally bad idea and redefined finalizer
4095 // will not work as expected we shouldn't abort vm in this case
4096 if (!ik->has_redefined_this_or_super()) {
4097 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4098 }
4099 #endif
4100
4101 // Check if this klass supports the java.lang.Cloneable interface
4102 if (vmClasses::Cloneable_klass_loaded()) {
4103 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4104 ik->set_is_cloneable();
4105 }
4106 }
4107
4108 // Check if this klass has a vanilla default constructor
4109 if (super == nullptr) {
4110 // java.lang.Object has empty default constructor
4111 ik->set_has_vanilla_constructor();
4112 } else {
4113 if (super->has_vanilla_constructor() &&
4114 _has_vanilla_constructor) {
4115 ik->set_has_vanilla_constructor();
4116 }
4117 #ifdef ASSERT
4118 bool v = false;
4119 if (super->has_vanilla_constructor()) {
4120 const Method* const constructor =
4121 ik->find_method(vmSymbols::object_initializer_name(),
4122 vmSymbols::void_method_signature());
4123 if (constructor != nullptr && constructor->is_vanilla_constructor()) {
4124 v = true;
4125 }
4126 }
4127 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4128 #endif
4129 }
4130
4131 // If it cannot be fast-path allocated, set a bit in the layout helper.
4132 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4133 assert(ik->size_helper() > 0, "layout_helper is initialized");
4134 if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4135 || ik->is_abstract() || ik->is_interface()
4136 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
4137 || ik->size_helper() >= FastAllocateSizeLimit) {
4138 // Forbid fast-path allocation.
4139 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4140 ik->set_layout_helper(lh);
4141 }
4142 }
4143
4144 // utility methods for appending an array with check for duplicates
4145
4146 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4147 const Array<InstanceKlass*>* const ifs) {
4148 // iterate over new interfaces
4149 for (int i = 0; i < ifs->length(); i++) {
4150 InstanceKlass* const e = ifs->at(i);
4151 assert(e->is_klass() && e->is_interface(), "just checking");
4152 // add new interface
4153 result->append_if_missing(e);
4154 }
4155 }
4156
4157 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4158 Array<InstanceKlass*>* local_ifs,
4159 ClassLoaderData* loader_data,
4160 TRAPS) {
4161 assert(local_ifs != nullptr, "invariant");
4162 assert(loader_data != nullptr, "invariant");
4163
4167 // Add superclass transitive interfaces size
4168 if (super != nullptr) {
4169 super_size = super->transitive_interfaces()->length();
4170 max_transitive_size += super_size;
4171 }
4172 // Add local interfaces' super interfaces
4173 const int local_size = local_ifs->length();
4174 for (int i = 0; i < local_size; i++) {
4175 InstanceKlass* const l = local_ifs->at(i);
4176 max_transitive_size += l->transitive_interfaces()->length();
4177 }
4178 // Finally add local interfaces
4179 max_transitive_size += local_size;
4180 // Construct array
4181 if (max_transitive_size == 0) {
4182 // no interfaces, use canonicalized array
4183 return Universe::the_empty_instance_klass_array();
4184 } else if (max_transitive_size == super_size) {
4185 // no new local interfaces added, share superklass' transitive interface array
4186 return super->transitive_interfaces();
4187 } else if (max_transitive_size == local_size) {
4188 // only local interfaces added, share local interface array
4189 return local_ifs;
4190 } else {
4191 ResourceMark rm;
4192 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4193
4194 // Copy down from superclass
4195 if (super != nullptr) {
4196 append_interfaces(result, super->transitive_interfaces());
4197 }
4198
4199 // Copy down from local interfaces' superinterfaces
4200 for (int i = 0; i < local_size; i++) {
4201 InstanceKlass* const l = local_ifs->at(i);
4202 append_interfaces(result, l->transitive_interfaces());
4203 }
4204 // Finally add local interfaces
4205 append_interfaces(result, local_ifs);
4206
4207 // length will be less than the max_transitive_size if duplicates were removed
4208 const int length = result->length();
4209 assert(length <= max_transitive_size, "just checking");
4210 Array<InstanceKlass*>* const new_result =
4211 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4212 for (int i = 0; i < length; i++) {
4213 InstanceKlass* const e = result->at(i);
4214 assert(e != nullptr, "just checking");
4215 new_result->at_put(i, e);
4216 }
4217 return new_result;
4218 }
4219 }
4220
4221 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4222 assert(this_klass != nullptr, "invariant");
4223 const Klass* const super = this_klass->super();
4224
4225 if (super != nullptr) {
4226 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4227
4228 if (super->is_final()) {
4229 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4230 return;
4231 }
4232
4233 if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {
4234 classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);
4235 return;
4236 }
4237
4238 // If the loader is not the boot loader then throw an exception if its
4239 // superclass is in package jdk.internal.reflect and its loader is not a
4240 // special reflection class loader
4241 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
4242 PackageEntry* super_package = super->package();
4243 if (super_package != nullptr &&
4244 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
4245 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
4246 ResourceMark rm(THREAD);
4247 Exceptions::fthrow(
4248 THREAD_AND_LOCATION,
4249 vmSymbols::java_lang_IllegalAccessError(),
4250 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
4251 this_klass->external_name(),
4252 this_klass->class_loader_data()->loader_name_and_id(),
4253 super->external_name());
4254 return;
4255 }
4256 }
4257
4401
4402 for (int index = 0; index < num_methods; index++) {
4403 const Method* const m = methods->at(index);
4404 // if m is static and not the init method, throw a verify error
4405 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4406 ResourceMark rm(THREAD);
4407 Exceptions::fthrow(
4408 THREAD_AND_LOCATION,
4409 vmSymbols::java_lang_VerifyError(),
4410 "Illegal static method %s in interface %s",
4411 m->name()->as_C_string(),
4412 this_klass->external_name()
4413 );
4414 return;
4415 }
4416 }
4417 }
4418
4419 // utility methods for format checking
4420
4421 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4422 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4423 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4424 if (is_module) {
4425 ResourceMark rm(THREAD);
4426 Exceptions::fthrow(
4427 THREAD_AND_LOCATION,
4428 vmSymbols::java_lang_NoClassDefFoundError(),
4429 "%s is not a class because access_flag ACC_MODULE is set",
4430 _class_name->as_C_string());
4431 return;
4432 }
4433
4434 if (!_need_verify) { return; }
4435
4436 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4437 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4438 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4439 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4440 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4441 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4442 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4443
4444 if ((is_abstract && is_final) ||
4445 (is_interface && !is_abstract) ||
4446 (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4447 (!is_interface && major_gte_1_5 && is_annotation)) {
4448 ResourceMark rm(THREAD);
4449 Exceptions::fthrow(
4450 THREAD_AND_LOCATION,
4451 vmSymbols::java_lang_ClassFormatError(),
4452 "Illegal class modifiers in class %s: 0x%X",
4453 _class_name->as_C_string(), flags
4454 );
4455 return;
4456 }
4457 }
4458
4459 static bool has_illegal_visibility(jint flags) {
4460 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4461 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4462 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4463
4464 return ((is_public && is_protected) ||
4465 (is_public && is_private) ||
4466 (is_protected && is_private));
4467 }
4468
4469 // A legal major_version.minor_version must be one of the following:
4470 //
4471 // Major_version >= 45 and major_version < 56, any minor_version.
4472 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4473 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4474 //
4475 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4501 THREAD_AND_LOCATION,
4502 vmSymbols::java_lang_UnsupportedClassVersionError(),
4503 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4504 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4505 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4506 return;
4507 }
4508
4509 if (!Arguments::enable_preview()) {
4510 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4511 class_name, major, minor, THREAD);
4512 return;
4513 }
4514
4515 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4516 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4517 class_name, major, minor, THREAD);
4518 }
4519 }
4520
4521 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4522 bool is_interface,
4523 TRAPS) const {
4524 if (!_need_verify) { return; }
4525
4526 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4527 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4528 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4529 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4530 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4531 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4532 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4533 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4534 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4535
4536 bool is_illegal = false;
4537
4538 if (is_interface) {
4539 if (!is_public || !is_static || !is_final || is_private ||
4540 is_protected || is_volatile || is_transient ||
4541 (major_gte_1_5 && is_enum)) {
4542 is_illegal = true;
4543 }
4544 } else { // not interface
4545 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4546 is_illegal = true;
4547 }
4548 }
4549
4550 if (is_illegal) {
4551 ResourceMark rm(THREAD);
4552 Exceptions::fthrow(
4553 THREAD_AND_LOCATION,
4554 vmSymbols::java_lang_ClassFormatError(),
4555 "Illegal field modifiers in class %s: 0x%X",
4556 _class_name->as_C_string(), flags);
4557 return;
4558 }
4559 }
4560
4561 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4562 bool is_interface,
4563 const Symbol* name,
4564 TRAPS) const {
4565 if (!_need_verify) { return; }
4566
4567 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4568 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4569 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4570 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4571 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4572 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4573 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4574 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4575 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4576 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4577 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4578 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4579 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4580 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4581
4582 bool is_illegal = false;
4583
4584 if (is_interface) {
4585 if (major_gte_8) {
4586 // Class file version is JAVA_8_VERSION or later Methods of
4587 // interfaces may set any of the flags except ACC_PROTECTED,
4588 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4589 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4590 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4591 (is_native || is_protected || is_final || is_synchronized) ||
4592 // If a specific method of a class or interface has its
4593 // ACC_ABSTRACT flag set, it must not have any of its
4594 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4595 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4596 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4597 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4598 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4599 is_illegal = true;
4600 }
4601 } else if (major_gte_1_5) {
4602 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4603 if (!is_public || is_private || is_protected || is_static || is_final ||
4604 is_synchronized || is_native || !is_abstract || is_strict) {
4605 is_illegal = true;
4606 }
4607 } else {
4608 // Class file version is pre-JAVA_1_5_VERSION
4609 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4610 is_illegal = true;
4611 }
4612 }
4613 } else { // not interface
4614 if (has_illegal_visibility(flags)) {
4615 is_illegal = true;
4616 } else {
4617 if (is_initializer) {
4618 if (is_static || is_final || is_synchronized || is_native ||
4619 is_abstract || (major_gte_1_5 && is_bridge)) {
4620 is_illegal = true;
4621 }
4622 } else { // not initializer
4623 if (is_abstract) {
4624 if ((is_final || is_native || is_private || is_static ||
4625 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4626 is_illegal = true;
4627 }
4628 }
4629 }
4630 }
4631 }
4632
4633 if (is_illegal) {
4634 ResourceMark rm(THREAD);
4635 Exceptions::fthrow(
4636 THREAD_AND_LOCATION,
4637 vmSymbols::java_lang_ClassFormatError(),
4638 "Method %s in class %s has illegal modifiers: 0x%X",
4639 name->as_C_string(), _class_name->as_C_string(), flags);
4640 return;
4641 }
4642 }
4643
4644 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4645 int length,
4646 TRAPS) const {
4647 assert(_need_verify, "only called when _need_verify is true");
4648 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4649 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4650 }
4651 }
4652
4653 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4654 // In class names, '/' separates unqualified names. This is verified in this function also.
4655 // Method names also may not contain the characters '<' or '>', unless <init>
4656 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4657 // method. Because these names have been checked as special cases before
4658 // calling this method in verify_legal_method_name.
4659 //
4777 // be taken as a field signature. Allow "void" if void_ok.
4778 // Return a pointer to just past the signature.
4779 // Return null if no legal signature is found.
4780 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4781 bool void_ok,
4782 unsigned int length,
4783 TRAPS) const {
4784 unsigned int array_dim = 0;
4785 while (length > 0) {
4786 switch (signature[0]) {
4787 case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
4788 case JVM_SIGNATURE_BOOLEAN:
4789 case JVM_SIGNATURE_BYTE:
4790 case JVM_SIGNATURE_CHAR:
4791 case JVM_SIGNATURE_SHORT:
4792 case JVM_SIGNATURE_INT:
4793 case JVM_SIGNATURE_FLOAT:
4794 case JVM_SIGNATURE_LONG:
4795 case JVM_SIGNATURE_DOUBLE:
4796 return signature + 1;
4797 case JVM_SIGNATURE_CLASS: {
4798 if (_major_version < JAVA_1_5_VERSION) {
4799 // Skip over the class name if one is there
4800 const char* const p = skip_over_field_name(signature + 1, true, --length);
4801
4802 // The next character better be a semicolon
4803 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4804 return p + 1;
4805 }
4806 }
4807 else {
4808 // Skip leading 'L' and ignore first appearance of ';'
4809 signature++;
4810 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4811 // Format check signature
4812 if (c != nullptr) {
4813 int newlen = c - (char*) signature;
4814 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4815 if (!legal) {
4816 classfile_parse_error("Class name is empty or contains illegal character "
4817 "in descriptor in class file %s",
4818 THREAD);
4819 return nullptr;
4820 }
4821 return signature + newlen + 1;
4822 }
4823 }
4824 return nullptr;
4825 }
4826 case JVM_SIGNATURE_ARRAY:
4827 array_dim++;
4828 if (array_dim > 255) {
4844
4845 // Checks if name is a legal class name.
4846 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4847 if (!_need_verify || _relax_verify) { return; }
4848
4849 assert(name->refcount() > 0, "symbol must be kept alive");
4850 char* bytes = (char*)name->bytes();
4851 unsigned int length = name->utf8_length();
4852 bool legal = false;
4853
4854 if (length > 0) {
4855 const char* p;
4856 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4857 p = skip_over_field_signature(bytes, false, length, CHECK);
4858 legal = (p != nullptr) && ((p - bytes) == (int)length);
4859 } else if (_major_version < JAVA_1_5_VERSION) {
4860 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4861 p = skip_over_field_name(bytes, true, length);
4862 legal = (p != nullptr) && ((p - bytes) == (int)length);
4863 }
4864 } else {
4865 // 4900761: relax the constraints based on JSR202 spec
4866 // Class names may be drawn from the entire Unicode character set.
4867 // Identifiers between '/' must be unqualified names.
4868 // The utf8 string has been verified when parsing cpool entries.
4869 legal = verify_unqualified_name(bytes, length, LegalClass);
4870 }
4871 }
4872 if (!legal) {
4873 ResourceMark rm(THREAD);
4874 assert(_class_name != nullptr, "invariant");
4875 Exceptions::fthrow(
4876 THREAD_AND_LOCATION,
4877 vmSymbols::java_lang_ClassFormatError(),
4878 "Illegal class name \"%.*s\" in class file %s", length, bytes,
4879 _class_name->as_C_string()
4880 );
4881 return;
4882 }
4883 }
4909 THREAD_AND_LOCATION,
4910 vmSymbols::java_lang_ClassFormatError(),
4911 "Illegal field name \"%.*s\" in class %s", length, bytes,
4912 _class_name->as_C_string()
4913 );
4914 return;
4915 }
4916 }
4917
4918 // Checks if name is a legal method name.
4919 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
4920 if (!_need_verify || _relax_verify) { return; }
4921
4922 assert(name != nullptr, "method name is null");
4923 char* bytes = (char*)name->bytes();
4924 unsigned int length = name->utf8_length();
4925 bool legal = false;
4926
4927 if (length > 0) {
4928 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
4929 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
4930 legal = true;
4931 }
4932 } else if (_major_version < JAVA_1_5_VERSION) {
4933 const char* p;
4934 p = skip_over_field_name(bytes, false, length);
4935 legal = (p != nullptr) && ((p - bytes) == (int)length);
4936 } else {
4937 // 4881221: relax the constraints based on JSR202 spec
4938 legal = verify_unqualified_name(bytes, length, LegalMethod);
4939 }
4940 }
4941
4942 if (!legal) {
4943 ResourceMark rm(THREAD);
4944 assert(_class_name != nullptr, "invariant");
4945 Exceptions::fthrow(
4946 THREAD_AND_LOCATION,
4947 vmSymbols::java_lang_ClassFormatError(),
4948 "Illegal method name \"%.*s\" in class %s", length, bytes,
4949 _class_name->as_C_string()
4950 );
4951 return;
4952 }
4953 }
4954
4955
4956 // Checks if signature is a legal field signature.
4957 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
4958 const Symbol* signature,
4959 TRAPS) const {
4960 if (!_need_verify) { return; }
4961
4962 const char* const bytes = (const char* const)signature->bytes();
4963 const unsigned int length = signature->utf8_length();
4964 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
4965
4966 if (p == nullptr || (p - bytes) != (int)length) {
4967 throwIllegalSignature("Field", name, signature, CHECK);
4968 }
4969 }
4970
4971 // Check that the signature is compatible with the method name. For example,
4972 // check that <init> has a void signature.
4973 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
4974 const Symbol* signature,
4975 TRAPS) const {
4976 if (!_need_verify) {
4977 return;
4978 }
4979
4980 // Class initializers cannot have args for class format version >= 51.
4981 if (name == vmSymbols::class_initializer_name() &&
4982 signature != vmSymbols::void_method_signature() &&
4983 _major_version >= JAVA_7_VERSION) {
4984 throwIllegalSignature("Method", name, signature, THREAD);
4985 return;
4986 }
4987
4988 int sig_length = signature->utf8_length();
4989 if (name->utf8_length() > 0 &&
4990 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
4991 sig_length > 0 &&
4992 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
4993 throwIllegalSignature("Method", name, signature, THREAD);
4994 }
4995 }
4996
4997 // Checks if signature is a legal method signature.
4998 // Returns number of parameters
4999 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5000 const Symbol* signature,
5001 TRAPS) const {
5002 if (!_need_verify) {
5003 // make sure caller's args_size will be less than 0 even for non-static
5004 // method so it will be recomputed in compute_size_of_parameters().
5005 return -2;
5006 }
5007
5008 unsigned int args_size = 0;
5009 const char* p = (const char*)signature->bytes();
5010 unsigned int length = signature->utf8_length();
5011 const char* nextp;
5012
5013 // The first character must be a '('
5149 }
5150 }
5151
5152 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5153 const ClassInstanceInfo& cl_inst_info,
5154 TRAPS) {
5155 if (_klass != nullptr) {
5156 return _klass;
5157 }
5158
5159 InstanceKlass* const ik =
5160 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5161
5162 if (is_hidden()) {
5163 mangle_hidden_class_name(ik);
5164 }
5165
5166 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5167
5168 assert(_klass == ik, "invariant");
5169
5170 return ik;
5171 }
5172
5173 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5174 bool changed_by_loadhook,
5175 const ClassInstanceInfo& cl_inst_info,
5176 TRAPS) {
5177 assert(ik != nullptr, "invariant");
5178
5179 // Set name and CLD before adding to CLD
5180 ik->set_class_loader_data(_loader_data);
5181 ik->set_name(_class_name);
5182
5183 // Add all classes to our internal class loader list here,
5184 // including classes in the bootstrap (null) class loader.
5185 const bool publicize = !is_internal();
5186
5187 _loader_data->add_class(ik, publicize);
5188
5189 set_klass_to_deallocate(ik);
5190
5191 assert(_field_info != nullptr, "invariant");
5192 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5193 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5194 "sanity");
5195
5196 assert(ik->is_instance_klass(), "sanity");
5197 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5198
5199 // Fill in information already parsed
5200 ik->set_should_verify_class(_need_verify);
5201
5202 // Not yet: supers are done below to support the new subtype-checking fields
5203 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5204 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5205 assert(_fac != nullptr, "invariant");
5206 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5207
5208 // this transfers ownership of a lot of arrays from
5209 // the parser onto the InstanceKlass*
5210 apply_parsed_class_metadata(ik, _java_fields_count);
5211
5212 // can only set dynamic nest-host after static nest information is set
5213 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5214 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5215 }
5216
5217 // note that is not safe to use the fields in the parser from this point on
5218 assert(nullptr == _cp, "invariant");
5219 assert(nullptr == _fieldinfo_stream, "invariant");
5220 assert(nullptr == _fields_status, "invariant");
5221 assert(nullptr == _methods, "invariant");
5222 assert(nullptr == _inner_classes, "invariant");
5223 assert(nullptr == _nest_members, "invariant");
5224 assert(nullptr == _combined_annotations, "invariant");
5225 assert(nullptr == _record_components, "invariant");
5226 assert(nullptr == _permitted_subclasses, "invariant");
5227
5228 if (_has_localvariable_table) {
5229 ik->set_has_localvariable_table(true);
5230 }
5231
5232 if (_has_final_method) {
5233 ik->set_has_final_method();
5234 }
5235
5236 ik->copy_method_ordering(_method_ordering, CHECK);
5237 // The InstanceKlass::_methods_jmethod_ids cache
5238 // is managed on the assumption that the initial cache
5239 // size is equal to the number of methods in the class. If
5240 // that changes, then InstanceKlass::idnum_can_increment()
5241 // has to be changed accordingly.
5242 ik->set_initial_method_idnum(ik->methods()->length());
5243
5244 ik->set_this_class_index(_this_class_index);
5245
5246 if (_is_hidden) {
5247 // _this_class_index is a CONSTANT_Class entry that refers to this
5248 // hidden class itself. If this class needs to refer to its own methods
5249 // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5250 // _this_class_index. However, because this class is hidden (it's
5251 // not stored in SystemDictionary), _this_class_index cannot be resolved
5252 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5253 // Therefore, we must eagerly resolve _this_class_index now.
5254 ik->constants()->klass_at_put(_this_class_index, ik);
5255 }
5256
5257 ik->set_minor_version(_minor_version);
5258 ik->set_major_version(_major_version);
5259 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5260 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5261
5262 if (_is_hidden) {
5263 ik->set_is_hidden();
5264 }
5265
5266 // Set PackageEntry for this_klass
5267 oop cl = ik->class_loader();
5268 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5269 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5270 ik->set_package(cld, nullptr, CHECK);
5271
5272 const Array<Method*>* const methods = ik->methods();
5273 assert(methods != nullptr, "invariant");
5274 const int methods_len = methods->length();
5275
5276 check_methods_for_intrinsics(ik, methods);
5277
5278 // Fill in field values obtained by parse_classfile_attributes
5279 if (_parsed_annotations->has_any_annotations()) {
5280 _parsed_annotations->apply_to(ik);
5348
5349 assert(_all_mirandas != nullptr, "invariant");
5350
5351 // Generate any default methods - default methods are public interface methods
5352 // that have a default implementation. This is new with Java 8.
5353 if (_has_nonstatic_concrete_methods) {
5354 DefaultMethods::generate_default_methods(ik,
5355 _all_mirandas,
5356 CHECK);
5357 }
5358
5359 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5360 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5361 !module_entry->has_default_read_edges()) {
5362 if (!module_entry->set_has_default_read_edges()) {
5363 // We won a potential race
5364 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5365 }
5366 }
5367
5368 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5369
5370 if (!is_internal()) {
5371 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5372
5373 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5374 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5375 log_is_enabled(Info, class, preview)) {
5376 ResourceMark rm;
5377 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5378 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5379 }
5380
5381 if (log_is_enabled(Debug, class, resolve)) {
5382 ResourceMark rm;
5383 // print out the superclass.
5384 const char * from = ik->external_name();
5385 if (ik->java_super() != nullptr) {
5386 log_debug(class, resolve)("%s %s (super)",
5387 from,
5439 Symbol* name,
5440 ClassLoaderData* loader_data,
5441 const ClassLoadInfo* cl_info,
5442 Publicity pub_level,
5443 TRAPS) :
5444 _stream(stream),
5445 _class_name(nullptr),
5446 _loader_data(loader_data),
5447 _is_hidden(cl_info->is_hidden()),
5448 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5449 _orig_cp_size(0),
5450 _super_klass(),
5451 _cp(nullptr),
5452 _fieldinfo_stream(nullptr),
5453 _fields_status(nullptr),
5454 _methods(nullptr),
5455 _inner_classes(nullptr),
5456 _nest_members(nullptr),
5457 _nest_host(0),
5458 _permitted_subclasses(nullptr),
5459 _record_components(nullptr),
5460 _local_interfaces(nullptr),
5461 _transitive_interfaces(nullptr),
5462 _combined_annotations(nullptr),
5463 _class_annotations(nullptr),
5464 _class_type_annotations(nullptr),
5465 _fields_annotations(nullptr),
5466 _fields_type_annotations(nullptr),
5467 _klass(nullptr),
5468 _klass_to_deallocate(nullptr),
5469 _parsed_annotations(nullptr),
5470 _fac(nullptr),
5471 _field_info(nullptr),
5472 _temp_field_info(nullptr),
5473 _method_ordering(nullptr),
5474 _all_mirandas(nullptr),
5475 _vtable_size(0),
5476 _itable_size(0),
5477 _num_miranda_methods(0),
5478 _protection_domain(cl_info->protection_domain()),
5479 _access_flags(),
5480 _pub_level(pub_level),
5481 _bad_constant_seen(0),
5482 _synthetic_flag(false),
5483 _sde_length(false),
5484 _sde_buffer(nullptr),
5485 _sourcefile_index(0),
5486 _generic_signature_index(0),
5487 _major_version(0),
5488 _minor_version(0),
5489 _this_class_index(0),
5490 _super_class_index(0),
5491 _itfs_len(0),
5492 _java_fields_count(0),
5493 _need_verify(false),
5494 _relax_verify(false),
5495 _has_nonstatic_concrete_methods(false),
5496 _declares_nonstatic_concrete_methods(false),
5497 _has_localvariable_table(false),
5498 _has_final_method(false),
5499 _has_contended_fields(false),
5500 _has_finalizer(false),
5501 _has_empty_finalizer(false),
5502 _has_vanilla_constructor(false),
5503 _max_bootstrap_specifier_index(-1) {
5504
5505 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5506 _class_name->increment_refcount();
5507
5508 assert(_loader_data != nullptr, "invariant");
5509 assert(stream != nullptr, "invariant");
5510 assert(_stream != nullptr, "invariant");
5511 assert(_stream->buffer() == _stream->current(), "invariant");
5512 assert(_class_name != nullptr, "invariant");
5513 assert(0 == _access_flags.as_int(), "invariant");
5514
5515 // Figure out whether we can skip format checking (matching classic VM behavior)
5516 if (DumpSharedSpaces) {
5517 // verify == true means it's a 'remote' class (i.e., non-boot class)
5518 // Verification decision is based on BytecodeVerificationRemote flag
5519 // for those classes.
5530
5531 // Check if verification needs to be relaxed for this class file
5532 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5533 _relax_verify = relax_format_check_for(_loader_data);
5534
5535 parse_stream(stream, CHECK);
5536
5537 post_process_parsed_stream(stream, _cp, CHECK);
5538 }
5539
5540 void ClassFileParser::clear_class_metadata() {
5541 // metadata created before the instance klass is created. Must be
5542 // deallocated if classfile parsing returns an error.
5543 _cp = nullptr;
5544 _fieldinfo_stream = nullptr;
5545 _fields_status = nullptr;
5546 _methods = nullptr;
5547 _inner_classes = nullptr;
5548 _nest_members = nullptr;
5549 _permitted_subclasses = nullptr;
5550 _combined_annotations = nullptr;
5551 _class_annotations = _class_type_annotations = nullptr;
5552 _fields_annotations = _fields_type_annotations = nullptr;
5553 _record_components = nullptr;
5554 }
5555
5556 // Destructor to clean up
5557 ClassFileParser::~ClassFileParser() {
5558 _class_name->decrement_refcount();
5559
5560 if (_cp != nullptr) {
5561 MetadataFactory::free_metadata(_loader_data, _cp);
5562 }
5563
5564 if (_fieldinfo_stream != nullptr) {
5565 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5566 }
5567
5568 if (_fields_status != nullptr) {
5569 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5570 }
5571
5572 if (_methods != nullptr) {
5573 // Free methods
5574 InstanceKlass::deallocate_methods(_loader_data, _methods);
5575 }
5576
5577 // beware of the Universe::empty_blah_array!!
5578 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5579 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5580 }
5581
5582 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5583 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5584 }
5585
5586 if (_record_components != nullptr) {
5587 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5588 }
5589
5590 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5591 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5592 }
5593
5594 // Free interfaces
5595 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5596 _local_interfaces, _transitive_interfaces);
5597
5598 if (_combined_annotations != nullptr) {
5599 // After all annotations arrays have been created, they are installed into the
5600 // Annotations object that will be assigned to the InstanceKlass being created.
5601
5602 // Deallocate the Annotations object and the installed annotations arrays.
5603 _combined_annotations->deallocate_contents(_loader_data);
5604
5605 // If the _combined_annotations pointer is non-null,
5606 // then the other annotations fields should have been cleared.
5607 assert(_class_annotations == nullptr, "Should have been cleared");
5608 assert(_class_type_annotations == nullptr, "Should have been cleared");
5609 assert(_fields_annotations == nullptr, "Should have been cleared");
5610 assert(_fields_type_annotations == nullptr, "Should have been cleared");
5611 } else {
5612 // If the annotations arrays were not installed into the Annotations object,
5613 // then they have to be deallocated explicitly.
5658 cp_size, CHECK);
5659
5660 _orig_cp_size = cp_size;
5661 if (is_hidden()) { // Add a slot for hidden class name.
5662 cp_size++;
5663 }
5664
5665 _cp = ConstantPool::allocate(_loader_data,
5666 cp_size,
5667 CHECK);
5668
5669 ConstantPool* const cp = _cp;
5670
5671 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
5672
5673 assert(cp_size == (const u2)cp->length(), "invariant");
5674
5675 // ACCESS FLAGS
5676 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5677
5678 // Access flags
5679 jint flags;
5680 // JVM_ACC_MODULE is defined in JDK-9 and later.
5681 if (_major_version >= JAVA_9_VERSION) {
5682 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5683 } else {
5684 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5685 }
5686
5687 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5688 // Set abstract bit for old class files for backward compatibility
5689 flags |= JVM_ACC_ABSTRACT;
5690 }
5691
5692 verify_legal_class_modifiers(flags, CHECK);
5693
5694 short bad_constant = class_bad_constant_seen();
5695 if (bad_constant != 0) {
5696 // Do not throw CFE until after the access_flags are checked because if
5697 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5698 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5699 return;
5700 }
5701
5702 _access_flags.set_flags(flags);
5703
5704 // This class and superclass
5705 _this_class_index = stream->get_u2_fast();
5706 check_property(
5707 valid_cp_range(_this_class_index, cp_size) &&
5708 cp->tag_at(_this_class_index).is_unresolved_klass(),
5709 "Invalid this class index %u in constant pool in class file %s",
5710 _this_class_index, CHECK);
5711
5712 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
5713 assert(class_name_in_cp != nullptr, "class_name can't be null");
5714
5715 // Don't need to check whether this class name is legal or not.
5716 // It has been checked when constant pool is parsed.
5717 // However, make sure it is not an array type.
5718 if (_need_verify) {
5719 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
5720 "Bad class name in class file %s",
5721 CHECK);
5722 }
5723
5724 #ifdef ASSERT
5725 // Basic sanity checks
5726 if (_is_hidden) {
5727 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
5728 }
5729 #endif
5730
5731 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
5732
5733 if (_is_hidden) {
5734 assert(_class_name != nullptr, "Unexpected null _class_name");
5772 if (stream->source() != nullptr) {
5773 ls.print(" source: %s", stream->source());
5774 }
5775 ls.cr();
5776 }
5777 }
5778
5779 // SUPERKLASS
5780 _super_class_index = stream->get_u2_fast();
5781 _super_klass = parse_super_class(cp,
5782 _super_class_index,
5783 _need_verify,
5784 CHECK);
5785
5786 // Interfaces
5787 _itfs_len = stream->get_u2_fast();
5788 parse_interfaces(stream,
5789 _itfs_len,
5790 cp,
5791 &_has_nonstatic_concrete_methods,
5792 CHECK);
5793
5794 assert(_local_interfaces != nullptr, "invariant");
5795
5796 // Fields (offsets are filled in later)
5797 _fac = new FieldAllocationCount();
5798 parse_fields(stream,
5799 _access_flags.is_interface(),
5800 _fac,
5801 cp,
5802 cp_size,
5803 &_java_fields_count,
5804 CHECK);
5805
5806 assert(_temp_field_info != nullptr, "invariant");
5807
5808 // Methods
5809 parse_methods(stream,
5810 _access_flags.is_interface(),
5811 &_has_localvariable_table,
5812 &_has_final_method,
5813 &_declares_nonstatic_concrete_methods,
5814 CHECK);
5815
5816 assert(_methods != nullptr, "invariant");
5817
5818 if (_declares_nonstatic_concrete_methods) {
5819 _has_nonstatic_concrete_methods = true;
5820 }
5821
5822 // Additional attributes/annotations
5823 _parsed_annotations = new ClassAnnotationCollector();
5824 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
5825
5826 assert(_inner_classes != nullptr, "invariant");
5827
5828 // Finalize the Annotations metadata object,
5829 // now that all annotation arrays have been created.
5830 create_combined_annotations(CHECK);
5870 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
5871 // We have to update the resolved_klass_index and the name_index together
5872 // so extract the existing resolved_klass_index first.
5873 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
5874 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
5875 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
5876 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
5877 "Bad name_index");
5878 }
5879
5880 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
5881 ConstantPool* cp,
5882 TRAPS) {
5883 assert(stream != nullptr, "invariant");
5884 assert(stream->at_eos(), "invariant");
5885 assert(cp != nullptr, "invariant");
5886 assert(_loader_data != nullptr, "invariant");
5887
5888 if (_class_name == vmSymbols::java_lang_Object()) {
5889 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
5890 "java.lang.Object cannot implement an interface in class file %s",
5891 CHECK);
5892 }
5893 // We check super class after class file is parsed and format is checked
5894 if (_super_class_index > 0 && nullptr == _super_klass) {
5895 Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
5896 if (_access_flags.is_interface()) {
5897 // Before attempting to resolve the superclass, check for class format
5898 // errors not checked yet.
5899 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
5900 "Interfaces must have java.lang.Object as superclass in class file %s",
5901 CHECK);
5902 }
5903 Handle loader(THREAD, _loader_data->class_loader());
5904 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
5905 _super_klass = vmClasses::Object_klass();
5906 } else {
5907 _super_klass = (const InstanceKlass*)
5908 SystemDictionary::resolve_super_or_fail(_class_name,
5909 super_class_name,
5910 loader,
5911 _protection_domain,
5912 true,
5913 CHECK);
5914 }
5915 }
5916
5917 if (_super_klass != nullptr) {
5918 if (_super_klass->has_nonstatic_concrete_methods()) {
5919 _has_nonstatic_concrete_methods = true;
5920 }
5921
5922 if (_super_klass->is_interface()) {
5923 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
5924 return;
5925 }
5926 }
5927
5928 // Compute the transitive list of all unique interfaces implemented by this class
5929 _transitive_interfaces =
5930 compute_transitive_interfaces(_super_klass,
5931 _local_interfaces,
5932 _loader_data,
5933 CHECK);
5934
5935 assert(_transitive_interfaces != nullptr, "invariant");
5936
5937 // sort methods
5938 _method_ordering = sort_methods(_methods);
5939
5940 _all_mirandas = new GrowableArray<Method*>(20);
5941
5942 Handle loader(THREAD, _loader_data->class_loader());
5943 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
5944 &_num_miranda_methods,
5945 _all_mirandas,
5946 _super_klass,
5947 _methods,
5948 _access_flags,
5949 _major_version,
5950 loader,
5951 _class_name,
5952 _local_interfaces);
5953
5954 // Size of Java itable (in words)
5955 _itable_size = _access_flags.is_interface() ? 0 :
5956 klassItable::compute_itable_size(_transitive_interfaces);
5957
5958 assert(_fac != nullptr, "invariant");
5959 assert(_parsed_annotations != nullptr, "invariant");
5960
5961 _field_info = new FieldLayoutInfo();
5962 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, /*_fields*/ _temp_field_info,
5963 _parsed_annotations->is_contended(), _field_info);
5964 lb.build_layout();
5965
5966 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
5967 _fieldinfo_stream =
5968 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
5969 injected_fields_count, loader_data(), CHECK);
5970 _fields_status =
5971 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
5972 FieldStatus(0), CHECK);
5973 }
5974
5975 void ClassFileParser::set_klass(InstanceKlass* klass) {
5976
5977 #ifdef ASSERT
5978 if (klass != nullptr) {
5979 assert(nullptr == _klass, "leaking?");
5980 }
5981 #endif
5982
5983 _klass = klass;
5984 }
|
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 "classfile/classFileParser.hpp"
28 #include "classfile/classFileStream.hpp"
29 #include "classfile/classLoader.hpp"
30 #include "classfile/classLoaderData.inline.hpp"
31 #include "classfile/classLoadInfo.hpp"
32 #include "classfile/defaultMethods.hpp"
33 #include "classfile/fieldLayoutBuilder.hpp"
34 #include "classfile/javaClasses.inline.hpp"
35 #include "classfile/moduleEntry.hpp"
36 #include "classfile/packageEntry.hpp"
37 #include "classfile/symbolTable.hpp"
38 #include "classfile/systemDictionary.hpp"
39 #include "classfile/verificationType.hpp"
40 #include "classfile/verifier.hpp"
41 #include "classfile/vmClasses.hpp"
42 #include "classfile/vmSymbols.hpp"
43 #include "jvm.h"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "memory/allocation.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/oopFactory.hpp"
49 #include "memory/resourceArea.hpp"
50 #include "memory/universe.hpp"
51 #include "oops/annotations.hpp"
52 #include "oops/constantPool.inline.hpp"
53 #include "oops/fieldInfo.hpp"
54 #include "oops/fieldStreams.inline.hpp"
55 #include "oops/inlineKlass.inline.hpp"
56 #include "oops/instanceKlass.inline.hpp"
57 #include "oops/instanceMirrorKlass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/klassVtable.hpp"
60 #include "oops/metadata.hpp"
61 #include "oops/method.inline.hpp"
62 #include "oops/oop.inline.hpp"
63 #include "oops/recordComponent.hpp"
64 #include "oops/symbol.hpp"
65 #include "prims/jvmtiExport.hpp"
66 #include "prims/jvmtiThreadState.hpp"
67 #include "runtime/arguments.hpp"
68 #include "runtime/fieldDescriptor.inline.hpp"
69 #include "runtime/handles.inline.hpp"
70 #include "runtime/javaCalls.hpp"
71 #include "runtime/os.hpp"
72 #include "runtime/perfData.hpp"
73 #include "runtime/reflection.hpp"
74 #include "runtime/safepointVerifiers.hpp"
75 #include "runtime/signature.hpp"
76 #include "runtime/timer.hpp"
77 #include "services/classLoadingService.hpp"
78 #include "services/threadService.hpp"
79 #include "utilities/align.hpp"
80 #include "utilities/bitMap.inline.hpp"
81 #include "utilities/copy.hpp"
82 #include "utilities/formatBuffer.hpp"
83 #include "utilities/exceptions.hpp"
84 #include "utilities/globalDefinitions.hpp"
85 #include "utilities/growableArray.hpp"
86 #include "utilities/macros.hpp"
87 #include "utilities/ostream.hpp"
88 #include "utilities/resourceHash.hpp"
89 #include "utilities/stringUtils.hpp"
90 #include "utilities/utf8.hpp"
91 #if INCLUDE_CDS
92 #include "classfile/systemDictionaryShared.hpp"
93 #endif
94 #if INCLUDE_JFR
95 #include "jfr/support/jfrTraceIdExtension.hpp"
96 #endif
97
98 // We generally try to create the oops directly when parsing, rather than
99 // allocating temporary data structures and copying the bytes twice. A
100 // temporary area is only needed when parsing utf8 entries in the constant
101 // pool and when parsing line number tables.
102
103 // We add assert in debug mode when class format is not checked.
104
105 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
106 #define JAVA_MIN_SUPPORTED_VERSION 45
107 #define JAVA_PREVIEW_MINOR_VERSION 65535
108
109 // Used for two backward compatibility reasons:
132 #define JAVA_12_VERSION 56
133
134 #define JAVA_13_VERSION 57
135
136 #define JAVA_14_VERSION 58
137
138 #define JAVA_15_VERSION 59
139
140 #define JAVA_16_VERSION 60
141
142 #define JAVA_17_VERSION 61
143
144 #define JAVA_18_VERSION 62
145
146 #define JAVA_19_VERSION 63
147
148 #define JAVA_20_VERSION 64
149
150 #define JAVA_21_VERSION 65
151
152 #define CONSTANT_CLASS_DESCRIPTORS 65
153
154 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
155 assert((bad_constant == JVM_CONSTANT_Module ||
156 bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
157 "Unexpected bad constant pool entry");
158 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
159 }
160
161 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
162 ConstantPool* cp,
163 const int length,
164 TRAPS) {
165 assert(stream != nullptr, "invariant");
166 assert(cp != nullptr, "invariant");
167
168 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
169 // this function (_current can be allocated in a register, with scalar
170 // replacement of aggregates). The _current pointer is copied back to
171 // stream() when this function returns. DON'T call another method within
172 // this method that uses stream().
173 const ClassFileStream cfs1 = *stream;
174 const ClassFileStream* const cfs = &cfs1;
175
176 debug_only(const u1* const old_current = stream->current();)
177
178 // Used for batching symbol allocations.
179 const char* names[SymbolTable::symbol_alloc_batch_size];
180 int lengths[SymbolTable::symbol_alloc_batch_size];
181 int indices[SymbolTable::symbol_alloc_batch_size];
182 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
183 int names_count = 0;
184
185 // parsing Index 0 is unused
186 for (int index = 1; index < length; index++) {
187 // Each of the following case guarantees one more byte in the stream
188 // for the following tag or the access_flags following constant pool,
189 // so we don't need bounds-check for reading tag.
190 const u1 tag = cfs->get_u1_fast();
191 switch (tag) {
192 case JVM_CONSTANT_Class: {
193 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
194 const u2 name_index = cfs->get_u2_fast();
195 cp->klass_index_at_put(index, name_index);
196 break;
197 }
198 case JVM_CONSTANT_Fieldref: {
199 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
200 const u2 class_index = cfs->get_u2_fast();
201 const u2 name_and_type_index = cfs->get_u2_fast();
202 cp->field_at_put(index, class_index, name_and_type_index);
203 break;
204 }
205 case JVM_CONSTANT_Methodref: {
206 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
207 const u2 class_index = cfs->get_u2_fast();
208 const u2 name_and_type_index = cfs->get_u2_fast();
209 cp->method_at_put(index, class_index, name_and_type_index);
210 break;
211 }
212 case JVM_CONSTANT_InterfaceMethodref: {
493 check_property(valid_symbol_at(name_ref_index),
494 "Invalid constant pool index %u in class file %s",
495 name_ref_index, CHECK);
496 check_property(valid_symbol_at(signature_ref_index),
497 "Invalid constant pool index %u in class file %s",
498 signature_ref_index, CHECK);
499 break;
500 }
501 case JVM_CONSTANT_Utf8:
502 break;
503 case JVM_CONSTANT_UnresolvedClass: // fall-through
504 case JVM_CONSTANT_UnresolvedClassInError: {
505 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
506 break;
507 }
508 case JVM_CONSTANT_ClassIndex: {
509 const int class_index = cp->klass_index_at(index);
510 check_property(valid_symbol_at(class_index),
511 "Invalid constant pool index %u in class file %s",
512 class_index, CHECK);
513
514 Symbol* const name = cp->symbol_at(class_index);
515 const unsigned int name_len = name->utf8_length();
516 if (name->is_Q_signature()) {
517 cp->unresolved_qdescriptor_at_put(index, class_index, num_klasses++);
518 } else {
519 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
520 }
521 break;
522 }
523 case JVM_CONSTANT_StringIndex: {
524 const int string_index = cp->string_index_at(index);
525 check_property(valid_symbol_at(string_index),
526 "Invalid constant pool index %u in class file %s",
527 string_index, CHECK);
528 Symbol* const sym = cp->symbol_at(string_index);
529 cp->unresolved_string_at_put(index, sym);
530 break;
531 }
532 case JVM_CONSTANT_MethodHandle: {
533 const int ref_index = cp->method_handle_index_at(index);
534 check_property(valid_cp_range(ref_index, length),
535 "Invalid constant pool index %u in class file %s",
536 ref_index, CHECK);
537 const constantTag tag = cp->tag_at(ref_index);
538 const int ref_kind = cp->method_handle_ref_kind_at(index);
539
540 switch (ref_kind) {
700 cp->signature_ref_index_at(name_and_type_ref_index);
701 const Symbol* const name = cp->symbol_at(name_ref_index);
702 const Symbol* const signature = cp->symbol_at(signature_ref_index);
703 if (tag == JVM_CONSTANT_Fieldref) {
704 if (_need_verify) {
705 // Field name and signature are verified above, when iterating NameAndType_info.
706 // Need only to be sure signature is non-zero length and the right type.
707 if (Signature::is_method(signature)) {
708 throwIllegalSignature("Field", name, signature, CHECK);
709 }
710 }
711 } else {
712 if (_need_verify) {
713 // Method name and signature are individually verified above, when iterating
714 // NameAndType_info. Need to check here that signature is non-zero length and
715 // the right type.
716 if (!Signature::is_method(signature)) {
717 throwIllegalSignature("Method", name, signature, CHECK);
718 }
719 }
720 // If a class method name begins with '<', it must be "<init>" and have void signature,
721 // or if it is an inline type, <vnew> with return.
722 const unsigned int name_len = name->utf8_length();
723 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
724 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
725 if (name != vmSymbols::object_initializer_name() &&
726 name != vmSymbols::inline_factory_name()) {
727 classfile_parse_error(
728 "Bad method name at constant pool index %u in class file %s",
729 name_ref_index, THREAD);
730 return;
731 } else if (!Signature::is_void_method(signature)) {
732 // if return type is non-void then it must be an inline type
733 if (name == vmSymbols::object_initializer_name() ||
734 !EnableValhalla || !supports_inline_types() ||
735 !signature->ends_with(JVM_SIGNATURE_ENDCLASS)) {
736 throwIllegalSignature("Method", name, signature, CHECK);
737 }
738 }
739 }
740 }
741 break;
742 }
743 case JVM_CONSTANT_MethodHandle: {
744 const int ref_index = cp->method_handle_index_at(index);
745 const int ref_kind = cp->method_handle_ref_kind_at(index);
746 switch (ref_kind) {
747 case JVM_REF_invokeVirtual:
748 case JVM_REF_invokeStatic:
749 case JVM_REF_invokeSpecial:
750 case JVM_REF_newInvokeSpecial: {
751 const int name_and_type_ref_index =
752 cp->name_and_type_ref_index_at(ref_index);
753 const int name_ref_index =
754 cp->name_ref_index_at(name_and_type_ref_index);
755 const Symbol* const name = cp->symbol_at(name_ref_index);
756
757 if (EnableValhalla && supports_inline_types() && name == vmSymbols::inline_factory_name()) { // <vnew>
758 // <vnew> factory methods must be non-void return and invokeStatic.
759 const int signature_ref_index =
760 cp->signature_ref_index_at(name_and_type_ref_index);
761 const Symbol* const signature = cp->symbol_at(signature_ref_index);
762 if (signature->is_void_method_signature() || ref_kind != JVM_REF_invokeStatic) {
763 classfile_parse_error(
764 "Bad factory method name at constant pool index %u in class file %s",
765 name_ref_index, CHECK);
766 }
767 } else if (name != vmSymbols::object_initializer_name()) { // !<init>
768 if (ref_kind == JVM_REF_newInvokeSpecial) {
769 classfile_parse_error(
770 "Bad constructor name at constant pool index %u in class file %s",
771 name_ref_index, THREAD);
772 return;
773 }
774 } else { // <init>
775 // The allowed invocation mode of <init> depends on its signature.
776 // This test corresponds to verify_invoke_instructions in the verifier.
777 const int signature_ref_index =
778 cp->signature_ref_index_at(name_and_type_ref_index);
779 const Symbol* const signature = cp->symbol_at(signature_ref_index);
780 if (signature->is_void_method_signature()
781 && ref_kind == JVM_REF_newInvokeSpecial) {
782 // OK, could be a constructor call
783 } else {
784 classfile_parse_error(
785 "Bad method name at constant pool index %u in class file %s",
786 name_ref_index, THREAD);
787 return;
788 }
789 }
790 break;
791 }
792 // Other ref_kinds are already fully checked in previous pass.
793 } // switch(ref_kind)
794 break;
795 }
796 case JVM_CONSTANT_MethodType: {
797 const Symbol* const no_name = vmSymbols::type_name(); // place holder
798 const Symbol* const signature = cp->method_type_signature_at(index);
799 verify_legal_method_signature(no_name, signature, CHECK);
800 break;
801 }
802 case JVM_CONSTANT_Utf8: {
803 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
815
816 NameSigHash(Symbol* name, Symbol* sig) :
817 _name(name),
818 _sig(sig) {}
819
820 static unsigned int hash(NameSigHash const& namesig) {
821 return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
822 }
823
824 static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
825 return (e0._name == e1._name) &&
826 (e0._sig == e1._sig);
827 }
828 };
829
830 using NameSigHashtable = ResourceHashtable<NameSigHash, int,
831 NameSigHash::HASH_ROW_SIZE,
832 AnyObj::RESOURCE_AREA, mtInternal,
833 &NameSigHash::hash, &NameSigHash::equals>;
834
835 static void check_identity_and_value_modifiers(ClassFileParser* current, const InstanceKlass* super_type, TRAPS) {
836 assert(super_type != nullptr,"Method doesn't support null super type");
837 if (super_type->carries_identity_modifier()) {
838 if (current->carries_value_modifier()) {
839 ResourceMark rm(THREAD);
840 Exceptions::fthrow(
841 THREAD_AND_LOCATION,
842 vmSymbols::java_lang_IncompatibleClassChangeError(),
843 "Value type %s has an identity type as supertype",
844 current->class_name()->as_klass_external_name());
845 return;
846 }
847 current->set_carries_identity_modifier();
848 }
849 if (super_type->carries_value_modifier()) {
850 if (current->carries_identity_modifier()) {
851 ResourceMark rm(THREAD);
852 Exceptions::fthrow(
853 THREAD_AND_LOCATION,
854 vmSymbols::java_lang_IncompatibleClassChangeError(),
855 "Identity type %s has a value type as supertype",
856 current->class_name()->as_klass_external_name());
857 return;
858 }
859 current->set_carries_value_modifier();
860 }
861 }
862
863 void ClassFileParser::parse_interfaces(const ClassFileStream* stream,
864 int itfs_len,
865 ConstantPool* cp,
866 bool* const has_nonstatic_concrete_methods,
867 // FIXME: lots of these functions
868 // declare their parameters as const,
869 // which adds only noise to the code.
870 // Remove the spurious const modifiers.
871 // Many are of the form "const int x"
872 // or "T* const x".
873 bool* const is_declared_atomic,
874 TRAPS) {
875 assert(stream != nullptr, "invariant");
876 assert(cp != nullptr, "invariant");
877 assert(has_nonstatic_concrete_methods != nullptr, "invariant");
878
879 if (itfs_len == 0) {
880 _local_interfaces = Universe::the_empty_instance_klass_array();
881
882 } else {
883 assert(itfs_len > 0, "only called for len>0");
884 _local_interface_indexes = new GrowableArray<u2>(itfs_len);
885 int index = 0;
886 for (index = 0; index < itfs_len; index++) {
887 const u2 interface_index = stream->get_u2(CHECK);
888 check_property(
889 valid_klass_reference_at(interface_index),
890 "Interface name has bad constant pool index %u in class file %s",
891 interface_index, CHECK);
892 _local_interface_indexes->at_put_grow(index, interface_index);
893 }
894
895 if (!_need_verify || itfs_len <= 1) {
896 return;
897 }
898
899 // Check if there's any duplicates in interfaces
900 ResourceMark rm(THREAD);
901 // Set containing interface names
902 ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
903 for (index = 0; index < itfs_len; index++) {
904 Symbol* interface_name = cp->klass_name_at(_local_interface_indexes->at(index));
905 // If no duplicates, add (name, nullptr) in hashtable interface_names.
906 if (!interface_names->put(interface_name, 0)) {
907 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
908 interface_name->as_C_string(), THREAD);
909 return;
910 }
911 }
912 }
913 }
914
915 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
916 int constantvalue_index,
917 int signature_index,
918 TRAPS) const {
919 // Make sure the constant pool entry is of a type appropriate to this field
920 guarantee_property(
921 (constantvalue_index > 0 &&
922 constantvalue_index < cp->length()),
923 "Bad initial value index %u in ConstantValue attribute in class file %s",
924 constantvalue_index, CHECK);
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_value_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
1516 const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1517 verify_legal_field_modifiers(flags, class_access_flags, CHECK);
1518 AccessFlags access_flags;
1519 access_flags.set_flags(flags);
1520 FieldInfo::FieldFlags fieldFlags(0);
1521
1522 const u2 name_index = cfs->get_u2_fast();
1523 check_property(valid_symbol_at(name_index),
1524 "Invalid constant pool index %u for field name in class file %s",
1525 name_index, CHECK);
1526 const Symbol* const name = cp->symbol_at(name_index);
1527 verify_legal_field_name(name, CHECK);
1528
1529 const u2 signature_index = cfs->get_u2_fast();
1530 check_property(valid_symbol_at(signature_index),
1531 "Invalid constant pool index %u for field signature in class file %s",
1532 signature_index, CHECK);
1533 const Symbol* const sig = cp->symbol_at(signature_index);
1534 verify_legal_field_signature(name, sig, CHECK);
1535 if (!access_flags.is_static()) instance_fields_count++;
1536
1537 u2 constantvalue_index = 0;
1538 bool is_synthetic = false;
1539 u2 generic_signature_index = 0;
1540 const bool is_static = access_flags.is_static();
1541 FieldAnnotationCollector parsed_annotations(_loader_data);
1542
1543 const u2 attributes_count = cfs->get_u2_fast();
1544 if (attributes_count > 0) {
1545 parse_field_attributes(cfs,
1546 attributes_count,
1547 is_static,
1548 signature_index,
1549 &constantvalue_index,
1550 &is_synthetic,
1551 &generic_signature_index,
1552 &parsed_annotations,
1553 CHECK);
1554
1555 if (parsed_annotations.field_annotations() != nullptr) {
1567 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1568 length,
1569 nullptr,
1570 CHECK);
1571 }
1572 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1573 parsed_annotations.set_field_type_annotations(nullptr);
1574 }
1575
1576 if (is_synthetic) {
1577 access_flags.set_is_synthetic();
1578 }
1579 if (generic_signature_index != 0) {
1580 fieldFlags.update_generic(true);
1581 }
1582 }
1583
1584 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1585
1586 // Update FieldAllocationCount for this kind of field
1587 fac->update(is_static, type, type == T_PRIMITIVE_OBJECT);
1588
1589 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1590 fi.set_index(n);
1591 if (fieldFlags.is_generic()) {
1592 fi.set_generic_signature_index(generic_signature_index);
1593 }
1594 parsed_annotations.apply_to(&fi);
1595 if (fi.field_flags().is_contended()) {
1596 _has_contended_fields = true;
1597 }
1598 _temp_field_info->append(fi);
1599 }
1600 assert(_temp_field_info->length() == length, "Must be");
1601
1602 int index = length;
1603 if (num_injected != 0) {
1604 for (int n = 0; n < num_injected; n++) {
1605 // Check for duplicates
1606 if (injected[n].may_be_java) {
1607 const Symbol* const name = injected[n].name();
1615 duplicate = true;
1616 break;
1617 }
1618 }
1619 if (duplicate) {
1620 // These will be removed from the field array at the end
1621 continue;
1622 }
1623 }
1624
1625 // Injected field
1626 FieldInfo::FieldFlags fflags(0);
1627 fflags.update_injected(true);
1628 AccessFlags aflags;
1629 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1630 fi.set_index(index);
1631 _temp_field_info->append(fi);
1632
1633 // Update FieldAllocationCount for this kind of field
1634 const BasicType type = Signature::basic_type(injected[n].signature());
1635 fac->update(false, type, false);
1636 index++;
1637 }
1638 }
1639
1640 if (is_inline_type) {
1641 // Inject static ".default" field
1642 FieldInfo::FieldFlags fflags(0);
1643 fflags.update_injected(true);
1644 AccessFlags aflags(JVM_ACC_STATIC);
1645 FieldInfo fi(aflags,
1646 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(default_value_name)),
1647 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(object_signature)),
1648 0,
1649 fflags);
1650 fi.set_index(index);
1651 _temp_field_info->append(fi);
1652
1653 const BasicType type = Signature::basic_type(vmSymbols::object_signature());
1654 fac->update(true, type, false);
1655 index++;
1656 }
1657
1658 if (is_inline_type && instance_fields_count == 0) {
1659 // Inject ".empty" dummy field
1660 _is_empty_inline_type = true;
1661
1662 FieldInfo::FieldFlags fflags(0);
1663 fflags.update_injected(true);
1664 AccessFlags aflags;
1665 FieldInfo fi(aflags,
1666 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(empty_marker_name)),
1667 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(byte_signature)),
1668 0,
1669 fflags);
1670 fi.set_index(index);
1671 _temp_field_info->append(fi);
1672
1673 const BasicType type = Signature::basic_type(vmSymbols::byte_signature());
1674 fac->update(false, type, false);
1675 index++;
1676 }
1677
1678 if (instance_fields_count > 0) {
1679 _has_nonstatic_fields = true;
1680 }
1681
1682 assert(_temp_field_info->length() == index, "Must be");
1683
1684 if (_need_verify && length > 1) {
1685 // Check duplicated fields
1686 ResourceMark rm(THREAD);
1687 // Set containing name-signature pairs
1688 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1689 for (int i = 0; i < _temp_field_info->length(); i++) {
1690 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1691 _temp_field_info->adr_at(i)->signature(_cp));
1692 // If no duplicates, add name/signature in hashtable names_and_sigs.
1693 if(!names_and_sigs->put(name_and_sig, 0)) {
1694 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1695 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1696 return;
1697 }
1698 }
1699 }
1700 }
1701
1945 "Exception name has bad type at constant pool %u in class file %s",
1946 checked_exception, CHECK_NULL);
1947 }
1948 }
1949 // check exceptions attribute length
1950 if (_need_verify) {
1951 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1952 sizeof(u2) * size),
1953 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1954 }
1955 return checked_exceptions_start;
1956 }
1957
1958 void ClassFileParser::throwIllegalSignature(const char* type,
1959 const Symbol* name,
1960 const Symbol* sig,
1961 TRAPS) const {
1962 assert(name != nullptr, "invariant");
1963 assert(sig != nullptr, "invariant");
1964
1965 const char* class_note = "";
1966 if (is_inline_type() && name == vmSymbols::object_initializer_name()) {
1967 class_note = " (an inline class)";
1968 }
1969
1970 ResourceMark rm(THREAD);
1971 Exceptions::fthrow(THREAD_AND_LOCATION,
1972 vmSymbols::java_lang_ClassFormatError(),
1973 "%s \"%s\" in class %s%s has illegal signature \"%s\"", type,
1974 name->as_C_string(), _class_name->as_C_string(), class_note, sig->as_C_string());
1975 }
1976
1977 AnnotationCollector::ID
1978 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
1979 const Symbol* name,
1980 const bool can_access_vm_annotations) {
1981 const vmSymbolID sid = vmSymbols::find_sid(name);
1982 // Privileged code can use all annotations. Other code silently drops some.
1983 const bool privileged = loader_data->is_boot_class_loader_data() ||
1984 loader_data->is_platform_class_loader_data() ||
1985 can_access_vm_annotations;
1986 switch (sid) {
1987 case VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
1988 if (_location != _in_method) break; // only allow for methods
1989 if (!privileged) break; // only allow in privileged code
1990 return _method_CallerSensitive;
1991 }
1992 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
1993 if (_location != _in_method) break; // only allow for methods
1994 if (!privileged) break; // only allow in privileged code
2256 runtime_invisible_type_annotations_length > 0) {
2257 a = assemble_annotations(runtime_visible_type_annotations,
2258 runtime_visible_type_annotations_length,
2259 runtime_invisible_type_annotations,
2260 runtime_invisible_type_annotations_length,
2261 CHECK);
2262 cm->set_type_annotations(a);
2263 }
2264 }
2265
2266
2267 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2268 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2269 // Method* to save footprint, so we only know the size of the resulting Method* when the
2270 // entire method attribute is parsed.
2271 //
2272 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2273
2274 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2275 bool is_interface,
2276 bool is_value_class,
2277 bool is_abstract_class,
2278 const ConstantPool* cp,
2279 bool* const has_localvariable_table,
2280 TRAPS) {
2281 assert(cfs != nullptr, "invariant");
2282 assert(cp != nullptr, "invariant");
2283 assert(has_localvariable_table != nullptr, "invariant");
2284
2285 ResourceMark rm(THREAD);
2286 // Parse fixed parts:
2287 // access_flags, name_index, descriptor_index, attributes_count
2288 cfs->guarantee_more(8, CHECK_NULL);
2289
2290 int flags = cfs->get_u2_fast();
2291 const u2 name_index = cfs->get_u2_fast();
2292 const int cp_size = cp->length();
2293 check_property(
2294 valid_symbol_at(name_index),
2295 "Illegal constant pool index %u for method name in class file %s",
2296 name_index, CHECK_NULL);
2297 const Symbol* const name = cp->symbol_at(name_index);
2299
2300 const u2 signature_index = cfs->get_u2_fast();
2301 guarantee_property(
2302 valid_symbol_at(signature_index),
2303 "Illegal constant pool index %u for method signature in class file %s",
2304 signature_index, CHECK_NULL);
2305 const Symbol* const signature = cp->symbol_at(signature_index);
2306
2307 if (name == vmSymbols::class_initializer_name()) {
2308 // We ignore the other access flags for a valid class initializer.
2309 // (JVM Spec 2nd ed., chapter 4.6)
2310 if (_major_version < 51) { // backward compatibility
2311 flags = JVM_ACC_STATIC;
2312 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2313 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2314 } else {
2315 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2316 return nullptr;
2317 }
2318 } else {
2319 verify_legal_method_modifiers(flags, access_flags() , name, CHECK_NULL);
2320 }
2321
2322 if (EnableValhalla && supports_inline_types() && name == vmSymbols::inline_factory_name()) {
2323 if (is_interface) {
2324 classfile_parse_error("Interface cannot have a method named <vnew>, class file %s", CHECK_NULL);
2325 } else if (!is_value_class) {
2326 classfile_parse_error("Identity class cannot have a method <vnew>, class file %s", CHECK_NULL);
2327 } else if (signature->is_void_method_signature()) {
2328 classfile_parse_error("Factory method <vnew> must have a non-void return type, class file %s", CHECK_NULL);
2329 } else { // also OK, a static factory, as long as the return value is good
2330 bool ok = false;
2331 SignatureStream ss((Symbol*) signature, true);
2332 while (!ss.at_return_type()) ss.next();
2333 if (ss.is_reference()) {
2334 Symbol* ret = ss.as_symbol();
2335 const Symbol* required = class_name();
2336 if (is_hidden()) {
2337 // The original class name for hidden classes changed.
2338 /// So using the original name in the return type is no longer valid.
2339 required = vmSymbols::java_lang_Object();
2340 }
2341 ok = (ret == required);
2342 }
2343 if (!ok) {
2344 throwIllegalSignature("Method", name, signature, CHECK_0);
2345 }
2346 // factory method, with a non-void return. No other
2347 // definition of <vnew> is possible.
2348 //
2349 // The verifier (in verify_invoke_instructions) will inspect the
2350 // signature of any attempt to invoke <vnew>, and ensure that it
2351 // returns non-void.
2352 }
2353 }
2354
2355 if (name == vmSymbols::object_initializer_name()) {
2356 if (is_interface) {
2357 classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL);
2358 } else if ((!is_value_class || is_abstract_class) && signature->is_void_method_signature()) {
2359 // OK, a constructor
2360 } else {
2361 // not OK, so throw the same error as in verify_legal_method_signature.
2362 throwIllegalSignature("Method", name, signature, CHECK_0);
2363 }
2364 // A declared <init> method must always be a non-static
2365 // object constructor, with a void return.
2366 //
2367 // The verifier (in verify_invoke_instructions) will inspect the
2368 // signature of any attempt to invoke <init>, and ensure that it
2369 // returns void.
2370 }
2371
2372 if (EnableValhalla) {
2373 if (((flags & JVM_ACC_SYNCHRONIZED) == JVM_ACC_SYNCHRONIZED)
2374 && ((flags & JVM_ACC_STATIC) == 0 )
2375 && !carries_identity_modifier()) {
2376 classfile_parse_error("Invalid synchronized method in non-identity class %s", THREAD);
2377 return nullptr;
2378 }
2379 }
2380
2381 int args_size = -1; // only used when _need_verify is true
2382 if (_need_verify) {
2383 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2384 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2385 verify_legal_method_signature(name, signature, CHECK_NULL);
2386 if (args_size > MAX_ARGS_SIZE) {
2387 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2388 return nullptr;
2389 }
2390 }
2391
2392 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2393
2394 // Default values for code and exceptions attribute elements
2395 u2 max_stack = 0;
2396 u2 max_locals = 0;
2397 u4 code_length = 0;
2398 const u1* code_start = 0;
2925 if (m->is_empty_method()) {
2926 _has_empty_finalizer = true;
2927 } else {
2928 _has_finalizer = true;
2929 }
2930 }
2931 if (name == vmSymbols::object_initializer_name() &&
2932 signature == vmSymbols::void_method_signature() &&
2933 m->is_vanilla_constructor()) {
2934 _has_vanilla_constructor = true;
2935 }
2936
2937 NOT_PRODUCT(m->verify());
2938 return m;
2939 }
2940
2941
2942 // Side-effects: populates the _methods field in the parser
2943 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2944 bool is_interface,
2945 bool is_value_class,
2946 bool is_abstract_type,
2947 bool* const has_localvariable_table,
2948 bool* has_final_method,
2949 bool* declares_nonstatic_concrete_methods,
2950 TRAPS) {
2951 assert(cfs != nullptr, "invariant");
2952 assert(has_localvariable_table != nullptr, "invariant");
2953 assert(has_final_method != nullptr, "invariant");
2954 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2955
2956 assert(nullptr == _methods, "invariant");
2957
2958 cfs->guarantee_more(2, CHECK); // length
2959 const u2 length = cfs->get_u2_fast();
2960 if (length == 0) {
2961 _methods = Universe::the_empty_method_array();
2962 } else {
2963 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2964 length,
2965 nullptr,
2966 CHECK);
2967
2968 for (int index = 0; index < length; index++) {
2969 Method* method = parse_method(cfs,
2970 is_interface,
2971 is_value_class,
2972 is_abstract_type,
2973 _cp,
2974 has_localvariable_table,
2975 CHECK);
2976
2977 if (method->is_final()) {
2978 *has_final_method = true;
2979 }
2980 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2981 // used for interface initialization, and default method inheritance analysis
2982 if (is_interface && !(*declares_nonstatic_concrete_methods)
2983 && !method->is_abstract() && !method->is_static()) {
2984 *declares_nonstatic_concrete_methods = true;
2985 }
2986 _methods->at_put(index, method);
2987 }
2988
2989 if (_need_verify && length > 1) {
2990 // Check duplicated methods
2991 ResourceMark rm(THREAD);
2992 // Set containing name-signature pairs
3218 valid_klass_reference_at(outer_class_info_index),
3219 "outer_class_info_index %u has bad constant type in class file %s",
3220 outer_class_info_index, CHECK_0);
3221
3222 if (outer_class_info_index != 0) {
3223 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3224 char* bytes = (char*)outer_class_name->bytes();
3225 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3226 "Outer class is an array class in class file %s", CHECK_0);
3227 }
3228 // Inner class name
3229 const u2 inner_name_index = cfs->get_u2_fast();
3230 check_property(
3231 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3232 "inner_name_index %u has bad constant type in class file %s",
3233 inner_name_index, CHECK_0);
3234 if (_need_verify) {
3235 guarantee_property(inner_class_info_index != outer_class_info_index,
3236 "Class is both outer and inner class in class file %s", CHECK_0);
3237 }
3238
3239 jint recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS;
3240 // JVM_ACC_MODULE is defined in JDK-9 and later.
3241 if (_major_version >= JAVA_9_VERSION) {
3242 recognized_modifiers |= JVM_ACC_MODULE;
3243 }
3244 if (supports_inline_types()) {
3245 recognized_modifiers |= JVM_ACC_PRIMITIVE | JVM_ACC_VALUE | JVM_ACC_IDENTITY;
3246 }
3247
3248 // Access flags
3249 jint flags = cfs->get_u2_fast() & recognized_modifiers;
3250
3251 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3252 // Set abstract bit for old class files for backward compatibility
3253 flags |= JVM_ACC_ABSTRACT;
3254 }
3255
3256 if (EnableValhalla) {
3257 if (!supports_inline_types()) {
3258 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
3259 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3260 if (!is_module && !is_interface) {
3261 flags |= JVM_ACC_IDENTITY;
3262 }
3263 }
3264 }
3265
3266 const char* name = inner_name_index == 0 ? "unnamed" : cp->symbol_at(inner_name_index)->as_utf8();
3267 verify_legal_class_modifiers(flags, name, false, CHECK_0);
3268 AccessFlags inner_access_flags(flags);
3269
3270 inner_classes->at_put(index++, inner_class_info_index);
3271 inner_classes->at_put(index++, outer_class_info_index);
3272 inner_classes->at_put(index++, inner_name_index);
3273 inner_classes->at_put(index++, inner_access_flags.as_short());
3274 }
3275
3276 // Check for circular and duplicate entries.
3277 bool has_circularity = false;
3278 if (_need_verify) {
3279 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3280 if (has_circularity) {
3281 // If circularity check failed then ignore InnerClasses attribute.
3282 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3283 index = 0;
3284 if (parsed_enclosingmethod_attribute) {
3285 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3286 _inner_classes = inner_classes;
3287 } else {
3351 if (length > 0) {
3352 int index = 0;
3353 cfs->guarantee_more(2 * length, CHECK_0);
3354 for (int n = 0; n < length; n++) {
3355 const u2 class_info_index = cfs->get_u2_fast();
3356 check_property(
3357 valid_klass_reference_at(class_info_index),
3358 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3359 class_info_index, CHECK_0);
3360 permitted_subclasses->at_put(index++, class_info_index);
3361 }
3362 assert(index == size, "wrong size");
3363 }
3364
3365 // Restore buffer's current position.
3366 cfs->set_current(current_mark);
3367
3368 return length;
3369 }
3370
3371 u2 ClassFileParser::parse_classfile_preload_attribute(const ClassFileStream* const cfs,
3372 const u1* const preload_attribute_start,
3373 TRAPS) {
3374 const u1* const current_mark = cfs->current();
3375 u2 length = 0;
3376 if (preload_attribute_start != nullptr) {
3377 cfs->set_current(preload_attribute_start);
3378 cfs->guarantee_more(2, CHECK_0); // length
3379 length = cfs->get_u2_fast();
3380 }
3381 const int size = length;
3382 Array<u2>* const preload_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3383 _preload_classes = preload_classes;
3384 if (length > 0) {
3385 int index = 0;
3386 cfs->guarantee_more(2 * length, CHECK_0);
3387 for (int n = 0; n < length; n++) {
3388 const u2 class_info_index = cfs->get_u2_fast();
3389 check_property(
3390 valid_klass_reference_at(class_info_index),
3391 "Preload class_info_index %u has bad constant type in class file %s",
3392 class_info_index, CHECK_0);
3393 preload_classes->at_put(index++, class_info_index);
3394 }
3395 assert(index == size, "wrong size");
3396 }
3397
3398 // Restore buffer's current position.
3399 cfs->set_current(current_mark);
3400
3401 return length;
3402 }
3403
3404 // Record {
3405 // u2 attribute_name_index;
3406 // u4 attribute_length;
3407 // u2 components_count;
3408 // component_info components[components_count];
3409 // }
3410 // component_info {
3411 // u2 name_index;
3412 // u2 descriptor_index
3413 // u2 attributes_count;
3414 // attribute_info_attributes[attributes_count];
3415 // }
3416 u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3417 const ConstantPool* cp,
3418 const u1* const record_attribute_start,
3419 TRAPS) {
3420 const u1* const current_mark = cfs->current();
3421 int components_count = 0;
3422 unsigned int calculate_attr_size = 0;
3423 if (record_attribute_start != nullptr) {
3668 }
3669 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3670 "Bad length on BootstrapMethods in class file %s",
3671 CHECK);
3672 }
3673
3674 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3675 ConstantPool* cp,
3676 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3677 TRAPS) {
3678 assert(cfs != nullptr, "invariant");
3679 assert(cp != nullptr, "invariant");
3680 assert(parsed_annotations != nullptr, "invariant");
3681
3682 // Set inner classes attribute to default sentinel
3683 _inner_classes = Universe::the_empty_short_array();
3684 // Set nest members attribute to default sentinel
3685 _nest_members = Universe::the_empty_short_array();
3686 // Set _permitted_subclasses attribute to default sentinel
3687 _permitted_subclasses = Universe::the_empty_short_array();
3688 // Set _preload_classes attribute to default sentinel
3689 _preload_classes = Universe::the_empty_short_array();
3690 cfs->guarantee_more(2, CHECK); // attributes_count
3691 u2 attributes_count = cfs->get_u2_fast();
3692 bool parsed_sourcefile_attribute = false;
3693 bool parsed_innerclasses_attribute = false;
3694 bool parsed_nest_members_attribute = false;
3695 bool parsed_permitted_subclasses_attribute = false;
3696 bool parsed_preload_attribute = false;
3697 bool parsed_nest_host_attribute = false;
3698 bool parsed_record_attribute = false;
3699 bool parsed_enclosingmethod_attribute = false;
3700 bool parsed_bootstrap_methods_attribute = false;
3701 const u1* runtime_visible_annotations = nullptr;
3702 int runtime_visible_annotations_length = 0;
3703 const u1* runtime_invisible_annotations = nullptr;
3704 int runtime_invisible_annotations_length = 0;
3705 const u1* runtime_visible_type_annotations = nullptr;
3706 int runtime_visible_type_annotations_length = 0;
3707 const u1* runtime_invisible_type_annotations = nullptr;
3708 int runtime_invisible_type_annotations_length = 0;
3709 bool runtime_invisible_type_annotations_exists = false;
3710 bool runtime_invisible_annotations_exists = false;
3711 bool parsed_source_debug_ext_annotations_exist = false;
3712 const u1* inner_classes_attribute_start = nullptr;
3713 u4 inner_classes_attribute_length = 0;
3714 u2 enclosing_method_class_index = 0;
3715 u2 enclosing_method_method_index = 0;
3716 const u1* nest_members_attribute_start = nullptr;
3717 u4 nest_members_attribute_length = 0;
3718 const u1* record_attribute_start = nullptr;
3719 u4 record_attribute_length = 0;
3720 const u1* permitted_subclasses_attribute_start = nullptr;
3721 u4 permitted_subclasses_attribute_length = 0;
3722 const u1* preload_attribute_start = nullptr;
3723 u4 preload_attribute_length = 0;
3724
3725 // Iterate over attributes
3726 while (attributes_count--) {
3727 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3728 const u2 attribute_name_index = cfs->get_u2_fast();
3729 const u4 attribute_length = cfs->get_u4_fast();
3730 check_property(
3731 valid_symbol_at(attribute_name_index),
3732 "Attribute name has bad constant pool index %u in class file %s",
3733 attribute_name_index, CHECK);
3734 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3735 if (tag == vmSymbols::tag_source_file()) {
3736 // Check for SourceFile tag
3737 if (_need_verify) {
3738 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3739 }
3740 if (parsed_sourcefile_attribute) {
3741 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3742 return;
3743 } else {
3930 return;
3931 }
3932 parsed_record_attribute = true;
3933 record_attribute_start = cfs->current();
3934 record_attribute_length = attribute_length;
3935 } else if (_major_version >= JAVA_17_VERSION) {
3936 if (tag == vmSymbols::tag_permitted_subclasses()) {
3937 if (parsed_permitted_subclasses_attribute) {
3938 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3939 return;
3940 }
3941 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3942 if (_access_flags.is_final()) {
3943 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3944 return;
3945 }
3946 parsed_permitted_subclasses_attribute = true;
3947 permitted_subclasses_attribute_start = cfs->current();
3948 permitted_subclasses_attribute_length = attribute_length;
3949 }
3950 if (EnableValhalla && tag == vmSymbols::tag_preload()) {
3951 if (parsed_preload_attribute) {
3952 classfile_parse_error("Multiple Preload attributes in class file %s", CHECK);
3953 return;
3954 }
3955 parsed_preload_attribute = true;
3956 preload_attribute_start = cfs->current();
3957 preload_attribute_length = attribute_length;
3958 }
3959 }
3960 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3961 cfs->skip_u1(attribute_length, CHECK);
3962 } else {
3963 // Unknown attribute
3964 cfs->skip_u1(attribute_length, CHECK);
3965 }
3966 } else {
3967 // Unknown attribute
3968 cfs->skip_u1(attribute_length, CHECK);
3969 }
3970 } else {
3971 // Unknown attribute
3972 cfs->skip_u1(attribute_length, CHECK);
3973 }
3974 }
3975 _class_annotations = assemble_annotations(runtime_visible_annotations,
3976 runtime_visible_annotations_length,
3977 runtime_invisible_annotations,
3978 runtime_invisible_annotations_length,
4019 CHECK);
4020 if (_need_verify) {
4021 guarantee_property(record_attribute_length == calculated_attr_length,
4022 "Record attribute has wrong length in class file %s",
4023 CHECK);
4024 }
4025 }
4026
4027 if (parsed_permitted_subclasses_attribute) {
4028 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
4029 cfs,
4030 permitted_subclasses_attribute_start,
4031 CHECK);
4032 if (_need_verify) {
4033 guarantee_property(
4034 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
4035 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
4036 }
4037 }
4038
4039 if (parsed_preload_attribute) {
4040 const u2 num_classes = parse_classfile_preload_attribute(
4041 cfs,
4042 preload_attribute_start,
4043 CHECK);
4044 if (_need_verify) {
4045 guarantee_property(
4046 preload_attribute_length == sizeof(num_classes) + sizeof(u2) * num_classes,
4047 "Wrong Preload attribute length in class file %s", CHECK);
4048 }
4049 }
4050
4051 if (_max_bootstrap_specifier_index >= 0) {
4052 guarantee_property(parsed_bootstrap_methods_attribute,
4053 "Missing BootstrapMethods attribute in class file %s", CHECK);
4054 }
4055 }
4056
4057 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
4058 assert(k != nullptr, "invariant");
4059
4060 if (_synthetic_flag)
4061 k->set_is_synthetic();
4062 if (_sourcefile_index != 0) {
4063 k->set_source_file_name_index(_sourcefile_index);
4064 }
4065 if (_generic_signature_index != 0) {
4066 k->set_generic_signature_index(_generic_signature_index);
4067 }
4068 if (_sde_buffer != nullptr) {
4069 k->set_source_debug_extension(_sde_buffer, _sde_length);
4070 }
4096 _class_annotations = nullptr;
4097 _class_type_annotations = nullptr;
4098 _fields_annotations = nullptr;
4099 _fields_type_annotations = nullptr;
4100 }
4101
4102 // Transfer ownership of metadata allocated to the InstanceKlass.
4103 void ClassFileParser::apply_parsed_class_metadata(
4104 InstanceKlass* this_klass,
4105 int java_fields_count) {
4106 assert(this_klass != nullptr, "invariant");
4107
4108 _cp->set_pool_holder(this_klass);
4109 this_klass->set_constants(_cp);
4110 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
4111 this_klass->set_fields_status(_fields_status);
4112 this_klass->set_methods(_methods);
4113 this_klass->set_inner_classes(_inner_classes);
4114 this_klass->set_nest_members(_nest_members);
4115 this_klass->set_nest_host_index(_nest_host);
4116 this_klass->set_preload_classes(_preload_classes);
4117 this_klass->set_annotations(_combined_annotations);
4118 this_klass->set_permitted_subclasses(_permitted_subclasses);
4119 this_klass->set_record_components(_record_components);
4120 // Delay the setting of _local_interfaces and _transitive_interfaces until after
4121 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
4122 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
4123 // its _super. If an OOM occurs while loading the current klass, its _super field
4124 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
4125 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
4126 // dereferences to the deallocated _transitive_interfaces will result in a crash.
4127
4128 // Clear out these fields so they don't get deallocated by the destructor
4129 clear_class_metadata();
4130 }
4131
4132 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
4133 int runtime_visible_annotations_length,
4134 const u1* const runtime_invisible_annotations,
4135 int runtime_invisible_annotations_length,
4136 TRAPS) {
4148 }
4149 if (runtime_invisible_annotations != nullptr) {
4150 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
4151 int append = runtime_visible_annotations_length+i;
4152 annotations->at_put(append, runtime_invisible_annotations[i]);
4153 }
4154 }
4155 }
4156 return annotations;
4157 }
4158
4159 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
4160 const int super_class_index,
4161 const bool need_verify,
4162 TRAPS) {
4163 assert(cp != nullptr, "invariant");
4164 const InstanceKlass* super_klass = nullptr;
4165
4166 if (super_class_index == 0) {
4167 check_property(_class_name == vmSymbols::java_lang_Object(),
4168 "Invalid superclass index 0 in class file %s",
4169 CHECK_NULL);
4170 } else {
4171 check_property(valid_klass_reference_at(super_class_index),
4172 "Invalid superclass index %u in class file %s",
4173 super_class_index,
4174 CHECK_NULL);
4175 // The class name should be legal because it is checked when parsing constant pool.
4176 // However, make sure it is not an array type.
4177 if (cp->tag_at(super_class_index).is_klass()) {
4178 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
4179 }
4180 if (need_verify) {
4181 bool is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
4182 guarantee_property(!is_array,
4183 "Bad superclass name in class file %s", CHECK_NULL);
4184 }
4185 }
4186 return super_klass;
4187 }
4188
4189 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
4190 _max_nonstatic_oop_maps = max_blocks;
4191 _nonstatic_oop_map_count = 0;
4192 if (max_blocks == 0) {
4193 _nonstatic_oop_maps = nullptr;
4194 } else {
4195 _nonstatic_oop_maps =
4196 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
4197 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
4198 }
4199 }
4200
4201 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
4285 }
4286
4287 void OopMapBlocksBuilder::print_on(outputStream* st) const {
4288 st->print_cr(" OopMapBlocks: %3d /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);
4289 if (_nonstatic_oop_map_count > 0) {
4290 OopMapBlock* map = _nonstatic_oop_maps;
4291 OopMapBlock* last_map = last_oop_map();
4292 assert(map <= last_map, "Last less than first");
4293 while (map <= last_map) {
4294 st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(),
4295 map->offset() + map->offset_span() - heapOopSize, map->count());
4296 map++;
4297 }
4298 }
4299 }
4300
4301 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
4302 print_on(st);
4303 }
4304
4305 void ClassFileParser::throwInlineTypeLimitation(THREAD_AND_LOCATION_DECL,
4306 const char* msg,
4307 const Symbol* name,
4308 const Symbol* sig) const {
4309
4310 ResourceMark rm(THREAD);
4311 if (name == nullptr || sig == nullptr) {
4312 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4313 vmSymbols::java_lang_ClassFormatError(),
4314 "class: %s - %s", _class_name->as_C_string(), msg);
4315 }
4316 else {
4317 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4318 vmSymbols::java_lang_ClassFormatError(),
4319 "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(),
4320 _class_name->as_C_string(), msg);
4321 }
4322 }
4323
4324 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4325 assert(ik != nullptr, "invariant");
4326
4327 const InstanceKlass* const super = ik->java_super();
4328
4329 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4330 // in which case we don't have to register objects as finalizable
4331 if (!_has_empty_finalizer) {
4332 if (_has_finalizer ||
4333 (super != nullptr && super->has_finalizer())) {
4334 ik->set_has_finalizer();
4335 }
4336 }
4337
4338 #ifdef ASSERT
4339 bool f = false;
4340 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4341 vmSymbols::void_method_signature());
4342 if (InstanceKlass::is_finalization_enabled() &&
4343 (m != nullptr) && !m->is_empty_method()) {
4344 f = true;
4345 }
4346
4347 // Spec doesn't prevent agent from redefinition of empty finalizer.
4348 // Despite the fact that it's generally bad idea and redefined finalizer
4349 // will not work as expected we shouldn't abort vm in this case
4350 if (!ik->has_redefined_this_or_super()) {
4351 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4352 }
4353 #endif
4354
4355 // Check if this klass supports the java.lang.Cloneable interface
4356 if (vmClasses::Cloneable_klass_loaded()) {
4357 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4358 if (ik->is_inline_klass()) {
4359 JavaThread *THREAD = JavaThread::current();
4360 throwInlineTypeLimitation(THREAD_AND_LOCATION, "Inline Types do not support Cloneable");
4361 return;
4362 }
4363 ik->set_is_cloneable();
4364 }
4365 }
4366
4367 // Check if this klass has a vanilla default constructor
4368 if (super == nullptr) {
4369 // java.lang.Object has empty default constructor
4370 ik->set_has_vanilla_constructor();
4371 } else {
4372 if (super->has_vanilla_constructor() &&
4373 _has_vanilla_constructor) {
4374 ik->set_has_vanilla_constructor();
4375 }
4376 #ifdef ASSERT
4377 bool v = false;
4378 if (super->has_vanilla_constructor()) {
4379 const Method* const constructor =
4380 ik->find_method(vmSymbols::object_initializer_name(),
4381 vmSymbols::void_method_signature());
4382 if (constructor != nullptr && constructor->is_vanilla_constructor()) {
4383 v = true;
4384 }
4385 }
4386 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4387 #endif
4388 }
4389
4390 // If it cannot be fast-path allocated, set a bit in the layout helper.
4391 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4392 assert(ik->size_helper() > 0, "layout_helper is initialized");
4393 if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4394 || ik->is_abstract() || ik->is_interface()
4395 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
4396 || ik->size_helper() >= FastAllocateSizeLimit) {
4397 // Forbid fast-path allocation.
4398 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4399 ik->set_layout_helper(lh);
4400 }
4401 }
4402
4403 bool ClassFileParser::supports_inline_types() const {
4404 // Inline types are only supported by class file version 61.65535 and later
4405 return _major_version > JAVA_21_VERSION ||
4406 (_major_version == JAVA_21_VERSION /*&& _minor_version == JAVA_PREVIEW_MINOR_VERSION*/); // JAVA_PREVIEW_MINOR_VERSION not yet implemented by javac, check JVMS draft
4407 }
4408
4409 // utility methods for appending an array with check for duplicates
4410
4411 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4412 const Array<InstanceKlass*>* const ifs) {
4413 // iterate over new interfaces
4414 for (int i = 0; i < ifs->length(); i++) {
4415 InstanceKlass* const e = ifs->at(i);
4416 assert(e->is_klass() && e->is_interface(), "just checking");
4417 // add new interface
4418 result->append_if_missing(e);
4419 }
4420 }
4421
4422 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4423 Array<InstanceKlass*>* local_ifs,
4424 ClassLoaderData* loader_data,
4425 TRAPS) {
4426 assert(local_ifs != nullptr, "invariant");
4427 assert(loader_data != nullptr, "invariant");
4428
4432 // Add superclass transitive interfaces size
4433 if (super != nullptr) {
4434 super_size = super->transitive_interfaces()->length();
4435 max_transitive_size += super_size;
4436 }
4437 // Add local interfaces' super interfaces
4438 const int local_size = local_ifs->length();
4439 for (int i = 0; i < local_size; i++) {
4440 InstanceKlass* const l = local_ifs->at(i);
4441 max_transitive_size += l->transitive_interfaces()->length();
4442 }
4443 // Finally add local interfaces
4444 max_transitive_size += local_size;
4445 // Construct array
4446 if (max_transitive_size == 0) {
4447 // no interfaces, use canonicalized array
4448 return Universe::the_empty_instance_klass_array();
4449 } else if (max_transitive_size == super_size) {
4450 // no new local interfaces added, share superklass' transitive interface array
4451 return super->transitive_interfaces();
4452 // The three lines below are commented to work around bug JDK-8245487
4453 // } else if (max_transitive_size == local_size) {
4454 // // only local interfaces added, share local interface array
4455 // return local_ifs;
4456 } else {
4457 ResourceMark rm;
4458 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4459
4460 // Copy down from superclass
4461 if (super != nullptr) {
4462 append_interfaces(result, super->transitive_interfaces());
4463 }
4464
4465 // Copy down from local interfaces' superinterfaces
4466 for (int i = 0; i < local_size; i++) {
4467 InstanceKlass* const l = local_ifs->at(i);
4468 append_interfaces(result, l->transitive_interfaces());
4469 }
4470 // Finally add local interfaces
4471 append_interfaces(result, local_ifs);
4472
4473 // length will be less than the max_transitive_size if duplicates were removed
4474 const int length = result->length();
4475 assert(length <= max_transitive_size, "just checking");
4476
4477 Array<InstanceKlass*>* const new_result =
4478 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4479 for (int i = 0; i < length; i++) {
4480 InstanceKlass* const e = result->at(i);
4481 assert(e != nullptr, "just checking");
4482 new_result->at_put(i, e);
4483 }
4484 return new_result;
4485 }
4486 }
4487
4488 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4489 assert(this_klass != nullptr, "invariant");
4490 const Klass* const super = this_klass->super();
4491
4492 if (super != nullptr) {
4493 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4494
4495 if (super->is_final()) {
4496 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4497 return;
4498 }
4499
4500 if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {
4501 classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);
4502 return;
4503 }
4504
4505 // The JVMS says that super classes for value types must not have the ACC_IDENTITY
4506 // flag set. But, java.lang.Object must still be allowed to be a direct super class
4507 // for a value classes. So, it is treated as a special case for now.
4508 if (this_klass->access_flags().is_value_class() &&
4509 super_ik->name() != vmSymbols::java_lang_Object() &&
4510 super_ik->is_identity_class()) {
4511 classfile_icce_error("value class %s cannot inherit from class %s", super_ik, THREAD);
4512 return;
4513 }
4514
4515 // If the loader is not the boot loader then throw an exception if its
4516 // superclass is in package jdk.internal.reflect and its loader is not a
4517 // special reflection class loader
4518 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
4519 PackageEntry* super_package = super->package();
4520 if (super_package != nullptr &&
4521 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
4522 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
4523 ResourceMark rm(THREAD);
4524 Exceptions::fthrow(
4525 THREAD_AND_LOCATION,
4526 vmSymbols::java_lang_IllegalAccessError(),
4527 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
4528 this_klass->external_name(),
4529 this_klass->class_loader_data()->loader_name_and_id(),
4530 super->external_name());
4531 return;
4532 }
4533 }
4534
4678
4679 for (int index = 0; index < num_methods; index++) {
4680 const Method* const m = methods->at(index);
4681 // if m is static and not the init method, throw a verify error
4682 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4683 ResourceMark rm(THREAD);
4684 Exceptions::fthrow(
4685 THREAD_AND_LOCATION,
4686 vmSymbols::java_lang_VerifyError(),
4687 "Illegal static method %s in interface %s",
4688 m->name()->as_C_string(),
4689 this_klass->external_name()
4690 );
4691 return;
4692 }
4693 }
4694 }
4695
4696 // utility methods for format checking
4697
4698 void ClassFileParser::verify_legal_class_modifiers(jint flags, const char* name, bool is_Object, TRAPS) const {
4699 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4700 const bool is_value_class = (flags & JVM_ACC_VALUE) != 0;
4701 const bool is_primitive_class = (flags & JVM_ACC_PRIMITIVE) != 0;
4702 const bool is_identity_class = (flags & JVM_ACC_IDENTITY) != 0;
4703 const bool is_inner_class = name != nullptr;
4704 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4705 if (is_module) {
4706 ResourceMark rm(THREAD);
4707 Exceptions::fthrow(
4708 THREAD_AND_LOCATION,
4709 vmSymbols::java_lang_NoClassDefFoundError(),
4710 "%s is not a class because access_flag ACC_MODULE is set",
4711 _class_name->as_C_string());
4712 return;
4713 }
4714
4715 if (is_value_class && !EnableValhalla) {
4716 ResourceMark rm(THREAD);
4717 Exceptions::fthrow(
4718 THREAD_AND_LOCATION,
4719 vmSymbols::java_lang_ClassFormatError(),
4720 "Class modifier ACC_VALUE in class %s requires option -XX:+EnableValhalla",
4721 _class_name->as_C_string()
4722 );
4723 return;
4724 }
4725
4726 if (is_primitive_class && !EnablePrimitiveClasses) {
4727 ResourceMark rm(THREAD);
4728 Exceptions::fthrow(
4729 THREAD_AND_LOCATION,
4730 vmSymbols::java_lang_ClassFormatError(),
4731 "Class modifier ACC_PRIMITIVE in class %s requires option -XX:+EnablePrimitiveClasses",
4732 _class_name->as_C_string()
4733 );
4734 return;
4735 }
4736
4737 // if (!_need_verify) { return; }
4738
4739 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4740 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4741 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4742 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4743 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4744 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4745 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4746
4747 if ((is_abstract && is_final) ||
4748 (is_interface && !is_abstract) ||
4749 (is_interface && major_gte_1_5 && ((is_super && (!EnableValhalla || !supports_inline_types())) || is_enum)) || // ACC_SUPER (now ACC_IDENTITY) was illegal for interfaces
4750 (!is_interface && major_gte_1_5 && is_annotation) ||
4751 (is_value_class && is_enum) ||
4752 (is_identity_class && is_value_class) ||
4753 (EnableValhalla && supports_inline_types() && !is_module && !is_abstract && !is_Object && !(is_identity_class || is_value_class) && !is_inner_class) ||
4754 (EnablePrimitiveClasses && supports_inline_types() && is_primitive_class && (!is_value_class || !is_final || is_interface || is_abstract))) {
4755 ResourceMark rm(THREAD);
4756 const char* class_note = "";
4757 if (is_value_class) class_note = " (a value class)";
4758 if (is_primitive_class) class_note = " (a primitive class)";
4759 if (is_value_class && is_identity_class) class_note = " (a value and identity class)";
4760 if (name == nullptr) { // Not an inner class
4761 Exceptions::fthrow(
4762 THREAD_AND_LOCATION,
4763 vmSymbols::java_lang_ClassFormatError(),
4764 "Illegal class modifiers in class %s%s: 0x%X",
4765 _class_name->as_C_string(), class_note, flags
4766 );
4767 return;
4768 } else {
4769 Exceptions::fthrow(
4770 THREAD_AND_LOCATION,
4771 vmSymbols::java_lang_ClassFormatError(),
4772 "Illegal class modifiers in declaration of inner class %s%s of class %s: 0x%X",
4773 name, class_note, _class_name->as_C_string(), flags
4774 );
4775 return;
4776 }
4777 }
4778 }
4779
4780 static bool has_illegal_visibility(jint flags) {
4781 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4782 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4783 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4784
4785 return ((is_public && is_protected) ||
4786 (is_public && is_private) ||
4787 (is_protected && is_private));
4788 }
4789
4790 // A legal major_version.minor_version must be one of the following:
4791 //
4792 // Major_version >= 45 and major_version < 56, any minor_version.
4793 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4794 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4795 //
4796 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4822 THREAD_AND_LOCATION,
4823 vmSymbols::java_lang_UnsupportedClassVersionError(),
4824 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4825 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4826 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4827 return;
4828 }
4829
4830 if (!Arguments::enable_preview()) {
4831 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4832 class_name, major, minor, THREAD);
4833 return;
4834 }
4835
4836 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4837 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4838 class_name, major, minor, THREAD);
4839 }
4840 }
4841
4842 void ClassFileParser:: verify_legal_field_modifiers(jint flags,
4843 AccessFlags class_access_flags,
4844 TRAPS) const {
4845 if (!_need_verify) { return; }
4846
4847 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4848 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4849 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4850 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4851 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4852 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4853 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4854 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4855 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4856
4857 const bool is_interface = class_access_flags.is_interface();
4858 const bool is_abstract = class_access_flags.is_abstract();
4859 const bool is_value_class = class_access_flags.is_value_class();
4860 const bool is_identity_class = class_access_flags.is_identity_class();
4861
4862 bool is_illegal = false;
4863
4864 if (is_interface) {
4865 if (!is_public || !is_static || !is_final || is_private ||
4866 is_protected || is_volatile || is_transient ||
4867 (major_gte_1_5 && is_enum)) {
4868 is_illegal = true;
4869 }
4870 } else { // not interface
4871 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4872 is_illegal = true;
4873 } else {
4874 if (is_value_class && !is_abstract && !is_static && !is_final) {
4875 is_illegal = true;
4876 } else if (is_abstract && !is_identity_class && !is_static) {
4877 is_illegal = true;
4878 }
4879 }
4880 }
4881
4882 if (is_illegal) {
4883 ResourceMark rm(THREAD);
4884 Exceptions::fthrow(
4885 THREAD_AND_LOCATION,
4886 vmSymbols::java_lang_ClassFormatError(),
4887 "Illegal field modifiers in class %s: 0x%X",
4888 _class_name->as_C_string(), flags);
4889 return;
4890 }
4891 }
4892
4893 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4894 AccessFlags class_access_flags,
4895 const Symbol* name,
4896 TRAPS) const {
4897 if (!_need_verify) { return; }
4898
4899 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4900 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4901 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4902 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4903 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4904 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4905 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4906 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4907 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4908 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4909 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4910 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4911 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4912 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4913 const bool is_factory = (name == vmSymbols::inline_factory_name() && supports_inline_types());
4914 const bool is_interface = class_access_flags.is_interface();
4915 const bool is_value_class = class_access_flags.is_value_class();
4916 const bool is_identity_class = class_access_flags.is_identity_class();
4917 const bool is_abstract_class = class_access_flags.is_abstract();
4918
4919 bool is_illegal = false;
4920
4921 const char* class_note = "";
4922 if (is_interface) {
4923 if (major_gte_8) {
4924 // Class file version is JAVA_8_VERSION or later Methods of
4925 // interfaces may set any of the flags except ACC_PROTECTED,
4926 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4927 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4928 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4929 (is_native || is_protected || is_final || is_synchronized) ||
4930 // If a specific method of a class or interface has its
4931 // ACC_ABSTRACT flag set, it must not have any of its
4932 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4933 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4934 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4935 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4936 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4937 is_illegal = true;
4938 }
4939 } else if (major_gte_1_5) {
4940 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4941 if (!is_public || is_private || is_protected || is_static || is_final ||
4942 is_synchronized || is_native || !is_abstract || is_strict) {
4943 is_illegal = true;
4944 }
4945 } else {
4946 // Class file version is pre-JAVA_1_5_VERSION
4947 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4948 is_illegal = true;
4949 }
4950 }
4951 } else { // not interface
4952 if (has_illegal_visibility(flags)) {
4953 is_illegal = true;
4954 } else {
4955 if (is_factory) { // <vnew> factory method
4956 if (is_final || is_synchronized || is_native || !is_static ||
4957 is_abstract || is_bridge) {
4958 is_illegal = true;
4959 class_note = (is_value_class ? " (a value class)" : " (not a value class)");
4960 }
4961 } else if (is_initializer) {
4962 if (is_static || is_final || is_synchronized || is_native ||
4963 is_abstract || (major_gte_1_5 && is_bridge)) {
4964 is_illegal = true;
4965 }
4966 } else { // not initializer
4967 if (!is_identity_class && is_synchronized && !is_static) {
4968 is_illegal = true;
4969 class_note = " (not an identity class)";
4970 } else {
4971 if (is_abstract) {
4972 if ((is_final || is_native || is_private || is_static ||
4973 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4974 is_illegal = true;
4975 }
4976 }
4977 }
4978 }
4979 }
4980 }
4981
4982 if (is_illegal) {
4983 ResourceMark rm(THREAD);
4984 if (is_value_class && is_initializer) {
4985 Exceptions::fthrow(
4986 THREAD_AND_LOCATION,
4987 vmSymbols::java_lang_ClassFormatError(),
4988 "Method <init> is not allowed in value class %s",
4989 _class_name->as_C_string());
4990 } else {
4991 Exceptions::fthrow(
4992 THREAD_AND_LOCATION,
4993 vmSymbols::java_lang_ClassFormatError(),
4994 "Method %s in class %s%s has illegal modifiers: 0x%X",
4995 name->as_C_string(), _class_name->as_C_string(),
4996 class_note, flags);
4997 }
4998 return;
4999 }
5000 }
5001
5002 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
5003 int length,
5004 TRAPS) const {
5005 assert(_need_verify, "only called when _need_verify is true");
5006 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
5007 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
5008 }
5009 }
5010
5011 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
5012 // In class names, '/' separates unqualified names. This is verified in this function also.
5013 // Method names also may not contain the characters '<' or '>', unless <init>
5014 // or <clinit>. Note that method names may not be <init> or <clinit> in this
5015 // method. Because these names have been checked as special cases before
5016 // calling this method in verify_legal_method_name.
5017 //
5135 // be taken as a field signature. Allow "void" if void_ok.
5136 // Return a pointer to just past the signature.
5137 // Return null if no legal signature is found.
5138 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5139 bool void_ok,
5140 unsigned int length,
5141 TRAPS) const {
5142 unsigned int array_dim = 0;
5143 while (length > 0) {
5144 switch (signature[0]) {
5145 case JVM_SIGNATURE_VOID: if (!void_ok) { return nullptr; }
5146 case JVM_SIGNATURE_BOOLEAN:
5147 case JVM_SIGNATURE_BYTE:
5148 case JVM_SIGNATURE_CHAR:
5149 case JVM_SIGNATURE_SHORT:
5150 case JVM_SIGNATURE_INT:
5151 case JVM_SIGNATURE_FLOAT:
5152 case JVM_SIGNATURE_LONG:
5153 case JVM_SIGNATURE_DOUBLE:
5154 return signature + 1;
5155 case JVM_SIGNATURE_PRIMITIVE_OBJECT:
5156 // Can't enable this check fully until JDK upgrades the bytecode generators (TODO: JDK-8270852).
5157 // For now, compare to class file version 51 so old verifier doesn't see Q signatures.
5158 if ( (_major_version < 51 /* CONSTANT_CLASS_DESCRIPTORS */ ) || (!EnablePrimitiveClasses)) {
5159 classfile_parse_error("Class name contains illegal Q-signature "
5160 "in descriptor in class file %s, requires option -XX:+EnablePrimitiveClasses",
5161 CHECK_0);
5162 return nullptr;
5163 }
5164 // fall through
5165 case JVM_SIGNATURE_CLASS:
5166 {
5167 if (_major_version < JAVA_1_5_VERSION) {
5168 // Skip over the class name if one is there
5169 const char* const p = skip_over_field_name(signature + 1, true, --length);
5170
5171 // The next character better be a semicolon
5172 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
5173 return p + 1;
5174 }
5175 }
5176 else {
5177 // Skip leading 'L' or 'Q' and ignore first appearance of ';'
5178 signature++;
5179 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
5180 // Format check signature
5181 if (c != nullptr) {
5182 int newlen = c - (char*) signature;
5183 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5184 if (!legal) {
5185 classfile_parse_error("Class name is empty or contains illegal character "
5186 "in descriptor in class file %s",
5187 THREAD);
5188 return nullptr;
5189 }
5190 return signature + newlen + 1;
5191 }
5192 }
5193 return nullptr;
5194 }
5195 case JVM_SIGNATURE_ARRAY:
5196 array_dim++;
5197 if (array_dim > 255) {
5213
5214 // Checks if name is a legal class name.
5215 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5216 if (!_need_verify || _relax_verify) { return; }
5217
5218 assert(name->refcount() > 0, "symbol must be kept alive");
5219 char* bytes = (char*)name->bytes();
5220 unsigned int length = name->utf8_length();
5221 bool legal = false;
5222
5223 if (length > 0) {
5224 const char* p;
5225 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5226 p = skip_over_field_signature(bytes, false, length, CHECK);
5227 legal = (p != nullptr) && ((p - bytes) == (int)length);
5228 } else if (_major_version < JAVA_1_5_VERSION) {
5229 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
5230 p = skip_over_field_name(bytes, true, length);
5231 legal = (p != nullptr) && ((p - bytes) == (int)length);
5232 }
5233 } else if ((_major_version >= CONSTANT_CLASS_DESCRIPTORS || _class_name->starts_with("jdk/internal/reflect/"))
5234 && bytes[length - 1] == ';' ) {
5235 // Support for L...; and Q...; descriptors
5236 legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
5237 } else {
5238 // 4900761: relax the constraints based on JSR202 spec
5239 // Class names may be drawn from the entire Unicode character set.
5240 // Identifiers between '/' must be unqualified names.
5241 // The utf8 string has been verified when parsing cpool entries.
5242 legal = verify_unqualified_name(bytes, length, LegalClass);
5243 }
5244 }
5245 if (!legal) {
5246 ResourceMark rm(THREAD);
5247 assert(_class_name != nullptr, "invariant");
5248 Exceptions::fthrow(
5249 THREAD_AND_LOCATION,
5250 vmSymbols::java_lang_ClassFormatError(),
5251 "Illegal class name \"%.*s\" in class file %s", length, bytes,
5252 _class_name->as_C_string()
5253 );
5254 return;
5255 }
5256 }
5282 THREAD_AND_LOCATION,
5283 vmSymbols::java_lang_ClassFormatError(),
5284 "Illegal field name \"%.*s\" in class %s", length, bytes,
5285 _class_name->as_C_string()
5286 );
5287 return;
5288 }
5289 }
5290
5291 // Checks if name is a legal method name.
5292 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
5293 if (!_need_verify || _relax_verify) { return; }
5294
5295 assert(name != nullptr, "method name is null");
5296 char* bytes = (char*)name->bytes();
5297 unsigned int length = name->utf8_length();
5298 bool legal = false;
5299
5300 if (length > 0) {
5301 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
5302 if (name == vmSymbols::object_initializer_name() ||
5303 name == vmSymbols::class_initializer_name() ||
5304 (EnableValhalla && supports_inline_types() &&
5305 name == vmSymbols::inline_factory_name())) {
5306 legal = true;
5307 }
5308 } else if (_major_version < JAVA_1_5_VERSION) {
5309 const char* p;
5310 p = skip_over_field_name(bytes, false, length);
5311 legal = (p != nullptr) && ((p - bytes) == (int)length);
5312 } else {
5313 // 4881221: relax the constraints based on JSR202 spec
5314 legal = verify_unqualified_name(bytes, length, LegalMethod);
5315 }
5316 }
5317
5318 if (!legal) {
5319 ResourceMark rm(THREAD);
5320 assert(_class_name != nullptr, "invariant");
5321 Exceptions::fthrow(
5322 THREAD_AND_LOCATION,
5323 vmSymbols::java_lang_ClassFormatError(),
5324 "Illegal method name \"%.*s\" in class %s", length, bytes,
5325 _class_name->as_C_string()
5326 );
5327 return;
5328 }
5329 }
5330
5331
5332 // Checks if signature is a legal field signature.
5333 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5334 const Symbol* signature,
5335 TRAPS) const {
5336 if (!_need_verify) { return; }
5337 if ((!supports_inline_types() || !EnablePrimitiveClasses) && (signature->is_Q_signature() || signature->is_Q_array_signature())) {
5338 throwIllegalSignature("Field", name, signature, CHECK);
5339 }
5340
5341 const char* const bytes = (const char* const)signature->bytes();
5342 const unsigned int length = signature->utf8_length();
5343 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5344
5345 if (p == nullptr || (p - bytes) != (int)length) {
5346 throwIllegalSignature("Field", name, signature, CHECK);
5347 }
5348 }
5349
5350 // Check that the signature is compatible with the method name. For example,
5351 // check that <init> has a void signature.
5352 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5353 const Symbol* signature,
5354 TRAPS) const {
5355 if (!_need_verify) {
5356 return;
5357 }
5358
5359 // Class initializers cannot have args for class format version >= 51.
5360 if (name == vmSymbols::class_initializer_name() &&
5361 signature != vmSymbols::void_method_signature() &&
5362 _major_version >= JAVA_7_VERSION) {
5363 throwIllegalSignature("Method", name, signature, THREAD);
5364 return;
5365 }
5366
5367 if (!is_value_class()) {
5368 int sig_length = signature->utf8_length();
5369 if (name->utf8_length() > 0 &&
5370 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5371 sig_length > 0 &&
5372 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5373 throwIllegalSignature("Method", name, signature, THREAD);
5374 }
5375 }
5376 }
5377
5378 // Checks if signature is a legal method signature.
5379 // Returns number of parameters
5380 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5381 const Symbol* signature,
5382 TRAPS) const {
5383 if (!_need_verify) {
5384 // make sure caller's args_size will be less than 0 even for non-static
5385 // method so it will be recomputed in compute_size_of_parameters().
5386 return -2;
5387 }
5388
5389 unsigned int args_size = 0;
5390 const char* p = (const char*)signature->bytes();
5391 unsigned int length = signature->utf8_length();
5392 const char* nextp;
5393
5394 // The first character must be a '('
5530 }
5531 }
5532
5533 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5534 const ClassInstanceInfo& cl_inst_info,
5535 TRAPS) {
5536 if (_klass != nullptr) {
5537 return _klass;
5538 }
5539
5540 InstanceKlass* const ik =
5541 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5542
5543 if (is_hidden()) {
5544 mangle_hidden_class_name(ik);
5545 }
5546
5547 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5548
5549 assert(_klass == ik, "invariant");
5550 return ik;
5551 }
5552
5553 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5554 bool changed_by_loadhook,
5555 const ClassInstanceInfo& cl_inst_info,
5556 TRAPS) {
5557 assert(ik != nullptr, "invariant");
5558
5559 // Set name and CLD before adding to CLD
5560 ik->set_class_loader_data(_loader_data);
5561 ik->set_name(_class_name);
5562
5563 // Add all classes to our internal class loader list here,
5564 // including classes in the bootstrap (null) class loader.
5565 const bool publicize = !is_internal();
5566
5567 _loader_data->add_class(ik, publicize);
5568
5569 set_klass_to_deallocate(ik);
5570
5571 assert(_field_info != nullptr, "invariant");
5572 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5573 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5574 "sanity");
5575
5576 assert(ik->is_instance_klass(), "sanity");
5577 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5578
5579 // Fill in information already parsed
5580 ik->set_should_verify_class(_need_verify);
5581
5582 // Not yet: supers are done below to support the new subtype-checking fields
5583 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5584 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5585 if (_field_info->_is_naturally_atomic && ik->is_inline_klass()) {
5586 ik->set_is_naturally_atomic();
5587 }
5588
5589 if (carries_identity_modifier()) {
5590 ik->set_carries_identity_modifier();
5591 } else if (carries_value_modifier()) {
5592 ik->set_carries_value_modifier();
5593 }
5594
5595 assert(_fac != nullptr, "invariant");
5596 ik->set_static_oop_field_count(_fac->count[STATIC_OOP] + _fac->count[STATIC_INLINE]);
5597
5598 // this transfers ownership of a lot of arrays from
5599 // the parser onto the InstanceKlass*
5600 apply_parsed_class_metadata(ik, _java_fields_count);
5601 if (ik->is_inline_klass()) {
5602 InlineKlass::cast(ik)->init_fixed_block();
5603 }
5604
5605 // can only set dynamic nest-host after static nest information is set
5606 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5607 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5608 }
5609
5610 // note that is not safe to use the fields in the parser from this point on
5611 assert(nullptr == _cp, "invariant");
5612 assert(nullptr == _fieldinfo_stream, "invariant");
5613 assert(nullptr == _fields_status, "invariant");
5614 assert(nullptr == _methods, "invariant");
5615 assert(nullptr == _inner_classes, "invariant");
5616 assert(nullptr == _nest_members, "invariant");
5617 assert(nullptr == _preload_classes, "invariant");
5618 assert(nullptr == _combined_annotations, "invariant");
5619 assert(nullptr == _record_components, "invariant");
5620 assert(nullptr == _permitted_subclasses, "invariant");
5621
5622 if (_has_localvariable_table) {
5623 ik->set_has_localvariable_table(true);
5624 }
5625
5626 if (_has_final_method) {
5627 ik->set_has_final_method();
5628 }
5629
5630 ik->copy_method_ordering(_method_ordering, CHECK);
5631 // The InstanceKlass::_methods_jmethod_ids cache
5632 // is managed on the assumption that the initial cache
5633 // size is equal to the number of methods in the class. If
5634 // that changes, then InstanceKlass::idnum_can_increment()
5635 // has to be changed accordingly.
5636 ik->set_initial_method_idnum(ik->methods()->length());
5637
5638 ik->set_this_class_index(_this_class_index);
5639
5640 if (_is_hidden) {
5641 // _this_class_index is a CONSTANT_Class entry that refers to this
5642 // hidden class itself. If this class needs to refer to its own methods
5643 // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5644 // _this_class_index. However, because this class is hidden (it's
5645 // not stored in SystemDictionary), _this_class_index cannot be resolved
5646 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5647 // Therefore, we must eagerly resolve _this_class_index now.
5648 ik->constants()->klass_at_put(_this_class_index, ik);
5649 }
5650
5651 ik->set_minor_version(_minor_version);
5652 ik->set_major_version(_major_version);
5653 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5654 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5655 if (_is_declared_atomic) {
5656 ik->set_is_declared_atomic();
5657 }
5658
5659 if (_is_hidden) {
5660 ik->set_is_hidden();
5661 }
5662
5663 // Set PackageEntry for this_klass
5664 oop cl = ik->class_loader();
5665 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5666 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5667 ik->set_package(cld, nullptr, CHECK);
5668
5669 const Array<Method*>* const methods = ik->methods();
5670 assert(methods != nullptr, "invariant");
5671 const int methods_len = methods->length();
5672
5673 check_methods_for_intrinsics(ik, methods);
5674
5675 // Fill in field values obtained by parse_classfile_attributes
5676 if (_parsed_annotations->has_any_annotations()) {
5677 _parsed_annotations->apply_to(ik);
5745
5746 assert(_all_mirandas != nullptr, "invariant");
5747
5748 // Generate any default methods - default methods are public interface methods
5749 // that have a default implementation. This is new with Java 8.
5750 if (_has_nonstatic_concrete_methods) {
5751 DefaultMethods::generate_default_methods(ik,
5752 _all_mirandas,
5753 CHECK);
5754 }
5755
5756 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5757 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5758 !module_entry->has_default_read_edges()) {
5759 if (!module_entry->set_has_default_read_edges()) {
5760 // We won a potential race
5761 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5762 }
5763 }
5764
5765 bool all_fields_empty = true;
5766 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5767 if (!fs.access_flags().is_static()) {
5768 if (fs.field_descriptor().is_inline_type()) {
5769 Klass* k = _inline_type_field_klasses->at(fs.index());
5770 ik->set_inline_type_field_klass(fs.index(), k);
5771 if (!InlineKlass::cast(k)->is_empty_inline_type()) { all_fields_empty = false; }
5772 } else {
5773 all_fields_empty = false;
5774 }
5775 } else if (is_inline_type() && (fs.name() == vmSymbols::default_value_name())) {
5776 InlineKlass::cast(ik)->set_default_value_offset(ik->field_offset(fs.index()));
5777 }
5778 }
5779
5780 if (_is_empty_inline_type || (is_inline_type() && all_fields_empty)) {
5781 ik->set_is_empty_inline_type();
5782 }
5783
5784 if (is_inline_type()) {
5785 InlineKlass* vk = InlineKlass::cast(ik);
5786 vk->set_alignment(_alignment);
5787 vk->set_first_field_offset(_first_field_offset);
5788 vk->set_exact_size_in_bytes(_exact_size_in_bytes);
5789 InlineKlass::cast(ik)->initialize_calling_convention(CHECK);
5790 }
5791
5792 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5793
5794 if (!is_internal()) {
5795 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5796
5797 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5798 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5799 log_is_enabled(Info, class, preview)) {
5800 ResourceMark rm;
5801 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5802 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5803 }
5804
5805 if (log_is_enabled(Debug, class, resolve)) {
5806 ResourceMark rm;
5807 // print out the superclass.
5808 const char * from = ik->external_name();
5809 if (ik->java_super() != nullptr) {
5810 log_debug(class, resolve)("%s %s (super)",
5811 from,
5863 Symbol* name,
5864 ClassLoaderData* loader_data,
5865 const ClassLoadInfo* cl_info,
5866 Publicity pub_level,
5867 TRAPS) :
5868 _stream(stream),
5869 _class_name(nullptr),
5870 _loader_data(loader_data),
5871 _is_hidden(cl_info->is_hidden()),
5872 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5873 _orig_cp_size(0),
5874 _super_klass(),
5875 _cp(nullptr),
5876 _fieldinfo_stream(nullptr),
5877 _fields_status(nullptr),
5878 _methods(nullptr),
5879 _inner_classes(nullptr),
5880 _nest_members(nullptr),
5881 _nest_host(0),
5882 _permitted_subclasses(nullptr),
5883 _preload_classes(nullptr),
5884 _record_components(nullptr),
5885 _local_interfaces(nullptr),
5886 _local_interface_indexes(nullptr),
5887 _transitive_interfaces(nullptr),
5888 _combined_annotations(nullptr),
5889 _class_annotations(nullptr),
5890 _class_type_annotations(nullptr),
5891 _fields_annotations(nullptr),
5892 _fields_type_annotations(nullptr),
5893 _klass(nullptr),
5894 _klass_to_deallocate(nullptr),
5895 _parsed_annotations(nullptr),
5896 _fac(nullptr),
5897 _field_info(nullptr),
5898 _inline_type_field_klasses(nullptr),
5899 _temp_field_info(nullptr),
5900 _method_ordering(nullptr),
5901 _all_mirandas(nullptr),
5902 _vtable_size(0),
5903 _itable_size(0),
5904 _num_miranda_methods(0),
5905 _protection_domain(cl_info->protection_domain()),
5906 _access_flags(),
5907 _pub_level(pub_level),
5908 _bad_constant_seen(0),
5909 _synthetic_flag(false),
5910 _sde_length(false),
5911 _sde_buffer(nullptr),
5912 _sourcefile_index(0),
5913 _generic_signature_index(0),
5914 _major_version(0),
5915 _minor_version(0),
5916 _this_class_index(0),
5917 _super_class_index(0),
5918 _itfs_len(0),
5919 _java_fields_count(0),
5920 _need_verify(false),
5921 _relax_verify(false),
5922 _has_nonstatic_concrete_methods(false),
5923 _declares_nonstatic_concrete_methods(false),
5924 _has_localvariable_table(false),
5925 _has_final_method(false),
5926 _has_contended_fields(false),
5927 _has_inline_type_fields(false),
5928 _has_nonstatic_fields(false),
5929 _is_empty_inline_type(false),
5930 _is_naturally_atomic(false),
5931 _is_declared_atomic(false),
5932 _carries_value_modifier(false),
5933 _carries_identity_modifier(false),
5934 _has_finalizer(false),
5935 _has_empty_finalizer(false),
5936 _has_vanilla_constructor(false),
5937 _max_bootstrap_specifier_index(-1) {
5938
5939 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5940 _class_name->increment_refcount();
5941
5942 assert(_loader_data != nullptr, "invariant");
5943 assert(stream != nullptr, "invariant");
5944 assert(_stream != nullptr, "invariant");
5945 assert(_stream->buffer() == _stream->current(), "invariant");
5946 assert(_class_name != nullptr, "invariant");
5947 assert(0 == _access_flags.as_int(), "invariant");
5948
5949 // Figure out whether we can skip format checking (matching classic VM behavior)
5950 if (DumpSharedSpaces) {
5951 // verify == true means it's a 'remote' class (i.e., non-boot class)
5952 // Verification decision is based on BytecodeVerificationRemote flag
5953 // for those classes.
5964
5965 // Check if verification needs to be relaxed for this class file
5966 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5967 _relax_verify = relax_format_check_for(_loader_data);
5968
5969 parse_stream(stream, CHECK);
5970
5971 post_process_parsed_stream(stream, _cp, CHECK);
5972 }
5973
5974 void ClassFileParser::clear_class_metadata() {
5975 // metadata created before the instance klass is created. Must be
5976 // deallocated if classfile parsing returns an error.
5977 _cp = nullptr;
5978 _fieldinfo_stream = nullptr;
5979 _fields_status = nullptr;
5980 _methods = nullptr;
5981 _inner_classes = nullptr;
5982 _nest_members = nullptr;
5983 _permitted_subclasses = nullptr;
5984 _preload_classes = nullptr;
5985 _combined_annotations = nullptr;
5986 _class_annotations = _class_type_annotations = nullptr;
5987 _fields_annotations = _fields_type_annotations = nullptr;
5988 _record_components = nullptr;
5989 }
5990
5991 // Destructor to clean up
5992 ClassFileParser::~ClassFileParser() {
5993 _class_name->decrement_refcount();
5994
5995 if (_cp != nullptr) {
5996 MetadataFactory::free_metadata(_loader_data, _cp);
5997 }
5998
5999 if (_fieldinfo_stream != nullptr) {
6000 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
6001 }
6002
6003 if (_fields_status != nullptr) {
6004 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
6005 }
6006
6007 if (_inline_type_field_klasses != nullptr) {
6008 MetadataFactory::free_array<InlineKlass*>(_loader_data, _inline_type_field_klasses);
6009 }
6010
6011 if (_methods != nullptr) {
6012 // Free methods
6013 InstanceKlass::deallocate_methods(_loader_data, _methods);
6014 }
6015
6016 // beware of the Universe::empty_blah_array!!
6017 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
6018 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
6019 }
6020
6021 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
6022 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
6023 }
6024
6025 if (_record_components != nullptr) {
6026 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
6027 }
6028
6029 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
6030 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
6031 }
6032
6033 if (_preload_classes != nullptr && _preload_classes != Universe::the_empty_short_array()) {
6034 MetadataFactory::free_array<u2>(_loader_data, _preload_classes);
6035 }
6036
6037 // Free interfaces
6038 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
6039 _local_interfaces, _transitive_interfaces);
6040
6041 if (_combined_annotations != nullptr) {
6042 // After all annotations arrays have been created, they are installed into the
6043 // Annotations object that will be assigned to the InstanceKlass being created.
6044
6045 // Deallocate the Annotations object and the installed annotations arrays.
6046 _combined_annotations->deallocate_contents(_loader_data);
6047
6048 // If the _combined_annotations pointer is non-null,
6049 // then the other annotations fields should have been cleared.
6050 assert(_class_annotations == nullptr, "Should have been cleared");
6051 assert(_class_type_annotations == nullptr, "Should have been cleared");
6052 assert(_fields_annotations == nullptr, "Should have been cleared");
6053 assert(_fields_type_annotations == nullptr, "Should have been cleared");
6054 } else {
6055 // If the annotations arrays were not installed into the Annotations object,
6056 // then they have to be deallocated explicitly.
6101 cp_size, CHECK);
6102
6103 _orig_cp_size = cp_size;
6104 if (is_hidden()) { // Add a slot for hidden class name.
6105 cp_size++;
6106 }
6107
6108 _cp = ConstantPool::allocate(_loader_data,
6109 cp_size,
6110 CHECK);
6111
6112 ConstantPool* const cp = _cp;
6113
6114 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6115
6116 assert(cp_size == (const u2)cp->length(), "invariant");
6117
6118 // ACCESS FLAGS
6119 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
6120
6121 jint recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS;
6122 // JVM_ACC_MODULE is defined in JDK-9 and later.
6123 if (_major_version >= JAVA_9_VERSION) {
6124 recognized_modifiers |= JVM_ACC_MODULE;
6125 }
6126 // JVM_ACC_VALUE and JVM_ACC_PRIMITIVE supported version
6127 if (supports_inline_types()) {
6128 recognized_modifiers |= JVM_ACC_PRIMITIVE | JVM_ACC_VALUE;
6129 }
6130
6131 // Access flags
6132 jint flags = stream->get_u2_fast() & recognized_modifiers;
6133
6134 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6135 // Set abstract bit for old class files for backward compatibility
6136 flags |= JVM_ACC_ABSTRACT;
6137 }
6138
6139 // This class and superclass
6140 _this_class_index = stream->get_u2_fast();
6141 check_property(
6142 valid_cp_range(_this_class_index, cp_size) &&
6143 cp->tag_at(_this_class_index).is_unresolved_klass(),
6144 "Invalid this class index %u in constant pool in class file %s",
6145 _this_class_index, CHECK);
6146
6147 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
6148 assert(class_name_in_cp != nullptr, "class_name can't be null");
6149
6150 bool is_java_lang_Object = class_name_in_cp == vmSymbols::java_lang_Object();
6151
6152 verify_legal_class_modifiers(flags, nullptr, is_java_lang_Object, CHECK);
6153
6154 if (EnableValhalla) {
6155 if(!supports_inline_types()) {
6156 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
6157 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
6158 if (!is_module && !is_interface && !is_java_lang_Object) {
6159 flags |= JVM_ACC_IDENTITY;
6160 }
6161 }
6162 }
6163
6164 _access_flags.set_flags(flags);
6165
6166 if (EnableValhalla) {
6167 if (_access_flags.is_identity_class()) set_carries_identity_modifier();
6168 if (_access_flags.is_value_class()) set_carries_value_modifier();
6169 if (carries_identity_modifier() && carries_value_modifier()) {
6170 classfile_parse_error("Class %s has both ACC_IDENTITY and ACC_VALUE modifiers", THREAD);
6171 }
6172 }
6173
6174 short bad_constant = class_bad_constant_seen();
6175 if (bad_constant != 0) {
6176 // Do not throw CFE until after the access_flags are checked because if
6177 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6178 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
6179 return;
6180 }
6181
6182 // Don't need to check whether this class name is legal or not.
6183 // It has been checked when constant pool is parsed.
6184 // However, make sure it is not an array type.
6185 if (_need_verify) {
6186 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
6187 "Bad class name in class file %s",
6188 CHECK);
6189 }
6190
6191 #ifdef ASSERT
6192 // Basic sanity checks
6193 if (_is_hidden) {
6194 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
6195 }
6196 #endif
6197
6198 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
6199
6200 if (_is_hidden) {
6201 assert(_class_name != nullptr, "Unexpected null _class_name");
6239 if (stream->source() != nullptr) {
6240 ls.print(" source: %s", stream->source());
6241 }
6242 ls.cr();
6243 }
6244 }
6245
6246 // SUPERKLASS
6247 _super_class_index = stream->get_u2_fast();
6248 _super_klass = parse_super_class(cp,
6249 _super_class_index,
6250 _need_verify,
6251 CHECK);
6252
6253 // Interfaces
6254 _itfs_len = stream->get_u2_fast();
6255 parse_interfaces(stream,
6256 _itfs_len,
6257 cp,
6258 &_has_nonstatic_concrete_methods,
6259 &_is_declared_atomic,
6260 CHECK);
6261
6262 // Fields (offsets are filled in later)
6263 _fac = new FieldAllocationCount();
6264 parse_fields(stream,
6265 _access_flags,
6266 _fac,
6267 cp,
6268 cp_size,
6269 &_java_fields_count,
6270 CHECK);
6271
6272 assert(_temp_field_info != nullptr, "invariant");
6273
6274 // Methods
6275 parse_methods(stream,
6276 is_interface(),
6277 is_value_class(),
6278 is_abstract_class(),
6279 &_has_localvariable_table,
6280 &_has_final_method,
6281 &_declares_nonstatic_concrete_methods,
6282 CHECK);
6283
6284 assert(_methods != nullptr, "invariant");
6285
6286 if (_declares_nonstatic_concrete_methods) {
6287 _has_nonstatic_concrete_methods = true;
6288 }
6289
6290 // Additional attributes/annotations
6291 _parsed_annotations = new ClassAnnotationCollector();
6292 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6293
6294 assert(_inner_classes != nullptr, "invariant");
6295
6296 // Finalize the Annotations metadata object,
6297 // now that all annotation arrays have been created.
6298 create_combined_annotations(CHECK);
6338 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
6339 // We have to update the resolved_klass_index and the name_index together
6340 // so extract the existing resolved_klass_index first.
6341 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
6342 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
6343 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
6344 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
6345 "Bad name_index");
6346 }
6347
6348 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
6349 ConstantPool* cp,
6350 TRAPS) {
6351 assert(stream != nullptr, "invariant");
6352 assert(stream->at_eos(), "invariant");
6353 assert(cp != nullptr, "invariant");
6354 assert(_loader_data != nullptr, "invariant");
6355
6356 if (_class_name == vmSymbols::java_lang_Object()) {
6357 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
6358 "java.lang.Object cannot implement an interface in class file %s",
6359 CHECK);
6360 }
6361 // We check super class after class file is parsed and format is checked
6362 if (_super_class_index > 0 && nullptr == _super_klass) {
6363 Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
6364 if (is_interface()) {
6365 // Before attempting to resolve the superclass, check for class format
6366 // errors not checked yet.
6367 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
6368 "Interfaces must have java.lang.Object as superclass in class file %s",
6369 CHECK);
6370 }
6371 Handle loader(THREAD, _loader_data->class_loader());
6372 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
6373 _super_klass = vmClasses::Object_klass();
6374 } else {
6375 _super_klass = (const InstanceKlass*)
6376 SystemDictionary::resolve_super_or_fail(_class_name,
6377 super_class_name,
6378 loader,
6379 _protection_domain,
6380 true,
6381 CHECK);
6382 }
6383 }
6384
6385 if (_super_klass != nullptr) {
6386 if (_super_klass->is_interface()) {
6387 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
6388 return;
6389 }
6390
6391 if (EnableValhalla) {
6392 check_identity_and_value_modifiers(this, _super_klass, CHECK);
6393 }
6394
6395 if (_super_klass->has_nonstatic_concrete_methods()) {
6396 _has_nonstatic_concrete_methods = true;
6397 }
6398 if (_super_klass->is_declared_atomic()) {
6399 _is_declared_atomic = true;
6400 }
6401 }
6402
6403 if (*ForceNonTearable != '\0') {
6404 // Allow a command line switch to force the same atomicity property:
6405 const char* class_name_str = _class_name->as_C_string();
6406 if (StringUtils::class_list_match(ForceNonTearable, class_name_str)) {
6407 _is_declared_atomic = true;
6408 }
6409 }
6410
6411 int itfs_len = _local_interface_indexes == nullptr ? 0 : _local_interface_indexes->length();
6412 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, nullptr, CHECK);
6413 if (_local_interface_indexes != nullptr) {
6414 for (int i = 0; i < _local_interface_indexes->length(); i++) {
6415 u2 interface_index = _local_interface_indexes->at(i);
6416 Klass* interf;
6417 if (cp->tag_at(interface_index).is_klass()) {
6418 interf = cp->resolved_klass_at(interface_index);
6419 } else {
6420 Symbol* const unresolved_klass = cp->klass_name_at(interface_index);
6421
6422 // Don't need to check legal name because it's checked when parsing constant pool.
6423 // But need to make sure it's not an array type.
6424 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
6425 "Bad interface name in class file %s", CHECK);
6426
6427 // Call resolve_super so class circularity is checked
6428 interf = SystemDictionary::resolve_super_or_fail(
6429 _class_name,
6430 unresolved_klass,
6431 Handle(THREAD, _loader_data->class_loader()),
6432 _protection_domain,
6433 false,
6434 CHECK);
6435 }
6436
6437 if (!interf->is_interface()) {
6438 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6439 err_msg("class %s can not implement %s, because it is not an interface (%s)",
6440 _class_name->as_klass_external_name(),
6441 interf->external_name(),
6442 interf->class_in_module_of_loader()));
6443 }
6444
6445 if (EnableValhalla) {
6446 // Check modifiers and set carries_identity_modifier/carries_value_modifier flags
6447 check_identity_and_value_modifiers(this, InstanceKlass::cast(interf), CHECK);
6448 }
6449
6450 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
6451 _has_nonstatic_concrete_methods = true;
6452 }
6453 if (InstanceKlass::cast(interf)->is_declared_atomic()) {
6454 _is_declared_atomic = true;
6455 }
6456 _local_interfaces->at_put(i, InstanceKlass::cast(interf));
6457 }
6458 }
6459 assert(_local_interfaces != nullptr, "invariant");
6460
6461 // Compute the transitive list of all unique interfaces implemented by this class
6462 _transitive_interfaces =
6463 compute_transitive_interfaces(_super_klass,
6464 _local_interfaces,
6465 _loader_data,
6466 CHECK);
6467
6468 assert(_transitive_interfaces != nullptr, "invariant");
6469
6470 // sort methods
6471 _method_ordering = sort_methods(_methods);
6472
6473 _all_mirandas = new GrowableArray<Method*>(20);
6474
6475 Handle loader(THREAD, _loader_data->class_loader());
6476 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6477 &_num_miranda_methods,
6478 _all_mirandas,
6479 _super_klass,
6480 _methods,
6481 _access_flags,
6482 _major_version,
6483 loader,
6484 _class_name,
6485 _local_interfaces);
6486
6487 // Size of Java itable (in words)
6488 _itable_size = is_interface() ? 0 :
6489 klassItable::compute_itable_size(_transitive_interfaces);
6490
6491 assert(_fac != nullptr, "invariant");
6492 assert(_parsed_annotations != nullptr, "invariant");
6493
6494
6495 if (EnablePrimitiveClasses) {
6496 _inline_type_field_klasses = MetadataFactory::new_array<InlineKlass*>(_loader_data,
6497 java_fields_count(),
6498 nullptr,
6499 CHECK);
6500 for (GrowableArrayIterator<FieldInfo> it = _temp_field_info->begin(); it != _temp_field_info->end(); ++it) {
6501 FieldInfo fieldinfo = *it;
6502 Symbol* sig = fieldinfo.signature(cp);
6503
6504 if (Signature::basic_type(sig) == T_PRIMITIVE_OBJECT && !fieldinfo.access_flags().is_static()) {
6505 // Pre-load inline class
6506 Klass* klass = SystemDictionary::resolve_inline_type_field_or_fail(sig,
6507 Handle(THREAD, _loader_data->class_loader()),
6508 _protection_domain, true, CHECK);
6509 assert(klass != nullptr, "Sanity check");
6510 if (!klass->access_flags().is_value_class()) {
6511 assert(klass->is_instance_klass(), "Sanity check");
6512 ResourceMark rm(THREAD);
6513 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6514 err_msg("Class %s expects class %s to be an inline type, but it is not",
6515 _class_name->as_C_string(),
6516 InstanceKlass::cast(klass)->external_name()));
6517 }
6518 _inline_type_field_klasses->at_put(fieldinfo.index(), InlineKlass::cast(klass));
6519 }
6520 }
6521 }
6522
6523 _field_info = new FieldLayoutInfo();
6524 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, /*_fields*/ _temp_field_info,
6525 _parsed_annotations->is_contended(), is_inline_type(),
6526 _field_info, _inline_type_field_klasses);
6527 lb.build_layout(CHECK);
6528 if (is_inline_type()) {
6529 _alignment = lb.get_alignment();
6530 _first_field_offset = lb.get_first_field_offset();
6531 _exact_size_in_bytes = lb.get_exact_size_in_byte();
6532 }
6533 _has_inline_type_fields = _field_info->_has_inline_fields;
6534
6535 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
6536 _fieldinfo_stream =
6537 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
6538 injected_fields_count, loader_data(), CHECK);
6539 _fields_status =
6540 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
6541 FieldStatus(0), CHECK);
6542 }
6543
6544 void ClassFileParser::set_klass(InstanceKlass* klass) {
6545
6546 #ifdef ASSERT
6547 if (klass != nullptr) {
6548 assert(nullptr == _klass, "leaking?");
6549 }
6550 #endif
6551
6552 _klass = klass;
6553 }
|