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