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