4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "precompiled.hpp"
25 #include "classfile/classFileParser.hpp"
26 #include "classfile/classFileStream.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/classLoaderData.inline.hpp"
29 #include "classfile/classLoadInfo.hpp"
30 #include "classfile/defaultMethods.hpp"
31 #include "classfile/fieldLayoutBuilder.hpp"
32 #include "classfile/javaClasses.inline.hpp"
33 #include "classfile/moduleEntry.hpp"
34 #include "classfile/packageEntry.hpp"
35 #include "classfile/symbolTable.hpp"
36 #include "classfile/systemDictionary.hpp"
37 #include "classfile/verificationType.hpp"
38 #include "classfile/verifier.hpp"
39 #include "classfile/vmClasses.hpp"
40 #include "classfile/vmSymbols.hpp"
41 #include "jvm.h"
42 #include "logging/log.hpp"
43 #include "logging/logStream.hpp"
44 #include "memory/allocation.hpp"
45 #include "memory/metadataFactory.hpp"
46 #include "memory/oopFactory.hpp"
47 #include "memory/resourceArea.hpp"
48 #include "memory/universe.hpp"
49 #include "oops/annotations.hpp"
50 #include "oops/constantPool.inline.hpp"
51 #include "oops/fieldStreams.inline.hpp"
52 #include "oops/instanceKlass.inline.hpp"
53 #include "oops/instanceMirrorKlass.hpp"
54 #include "oops/klass.inline.hpp"
55 #include "oops/klassVtable.hpp"
56 #include "oops/metadata.hpp"
57 #include "oops/method.inline.hpp"
58 #include "oops/oop.inline.hpp"
59 #include "oops/recordComponent.hpp"
60 #include "oops/symbol.hpp"
61 #include "prims/jvmtiExport.hpp"
62 #include "prims/jvmtiThreadState.hpp"
63 #include "runtime/arguments.hpp"
64 #include "runtime/fieldDescriptor.inline.hpp"
65 #include "runtime/handles.inline.hpp"
66 #include "runtime/javaCalls.hpp"
67 #include "runtime/os.hpp"
68 #include "runtime/perfData.hpp"
69 #include "runtime/reflection.hpp"
70 #include "runtime/safepointVerifiers.hpp"
71 #include "runtime/signature.hpp"
72 #include "runtime/timer.hpp"
73 #include "services/classLoadingService.hpp"
74 #include "services/threadService.hpp"
75 #include "utilities/align.hpp"
76 #include "utilities/bitMap.inline.hpp"
77 #include "utilities/copy.hpp"
78 #include "utilities/formatBuffer.hpp"
79 #include "utilities/exceptions.hpp"
80 #include "utilities/globalDefinitions.hpp"
81 #include "utilities/growableArray.hpp"
82 #include "utilities/macros.hpp"
83 #include "utilities/ostream.hpp"
84 #include "utilities/resourceHash.hpp"
85 #include "utilities/utf8.hpp"
86 #if INCLUDE_CDS
87 #include "classfile/systemDictionaryShared.hpp"
88 #endif
89 #if INCLUDE_JFR
90 #include "jfr/support/jfrTraceIdExtension.hpp"
91 #endif
92
93 // We generally try to create the oops directly when parsing, rather than
94 // allocating temporary data structures and copying the bytes twice. A
95 // temporary area is only needed when parsing utf8 entries in the constant
96 // pool and when parsing line number tables.
97
98 // We add assert in debug mode when class format is not checked.
99
100 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
101 #define JAVA_MIN_SUPPORTED_VERSION 45
102 #define JAVA_PREVIEW_MINOR_VERSION 65535
103
104 // Used for two backward compatibility reasons:
127 #define JAVA_12_VERSION 56
128
129 #define JAVA_13_VERSION 57
130
131 #define JAVA_14_VERSION 58
132
133 #define JAVA_15_VERSION 59
134
135 #define JAVA_16_VERSION 60
136
137 #define JAVA_17_VERSION 61
138
139 #define JAVA_18_VERSION 62
140
141 #define JAVA_19_VERSION 63
142
143 #define JAVA_20_VERSION 64
144
145 #define JAVA_21_VERSION 65
146
147 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
148 assert((bad_constant == JVM_CONSTANT_Module ||
149 bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
150 "Unexpected bad constant pool entry");
151 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
152 }
153
154 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
155 ConstantPool* cp,
156 const int length,
157 TRAPS) {
158 assert(stream != NULL, "invariant");
159 assert(cp != NULL, "invariant");
160
161 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
162 // this function (_current can be allocated in a register, with scalar
163 // replacement of aggregates). The _current pointer is copied back to
164 // stream() when this function returns. DON'T call another method within
165 // this method that uses stream().
166 const ClassFileStream cfs1 = *stream;
167 const ClassFileStream* const cfs = &cfs1;
168
169 debug_only(const u1* const old_current = stream->current();)
170
171 // Used for batching symbol allocations.
172 const char* names[SymbolTable::symbol_alloc_batch_size];
173 int lengths[SymbolTable::symbol_alloc_batch_size];
174 int indices[SymbolTable::symbol_alloc_batch_size];
175 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
176 int names_count = 0;
177
178 // parsing Index 0 is unused
179 for (int index = 1; index < length; index++) {
180 // Each of the following case guarantees one more byte in the stream
181 // for the following tag or the access_flags following constant pool,
182 // so we don't need bounds-check for reading tag.
183 const u1 tag = cfs->get_u1_fast();
184 switch (tag) {
185 case JVM_CONSTANT_Class : {
186 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
187 const u2 name_index = cfs->get_u2_fast();
188 cp->klass_index_at_put(index, name_index);
189 break;
190 }
191 case JVM_CONSTANT_Fieldref: {
192 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
193 const u2 class_index = cfs->get_u2_fast();
194 const u2 name_and_type_index = cfs->get_u2_fast();
195 cp->field_at_put(index, class_index, name_and_type_index);
196 break;
197 }
198 case JVM_CONSTANT_Methodref: {
199 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
200 const u2 class_index = cfs->get_u2_fast();
201 const u2 name_and_type_index = cfs->get_u2_fast();
202 cp->method_at_put(index, class_index, name_and_type_index);
203 break;
204 }
205 case JVM_CONSTANT_InterfaceMethodref: {
486 check_property(valid_symbol_at(name_ref_index),
487 "Invalid constant pool index %u in class file %s",
488 name_ref_index, CHECK);
489 check_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 check_property(valid_symbol_at(class_index),
504 "Invalid constant pool index %u in class file %s",
505 class_index, CHECK);
506 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
507 break;
508 }
509 case JVM_CONSTANT_StringIndex: {
510 const int string_index = cp->string_index_at(index);
511 check_property(valid_symbol_at(string_index),
512 "Invalid constant pool index %u in class file %s",
513 string_index, CHECK);
514 Symbol* const sym = cp->symbol_at(string_index);
515 cp->unresolved_string_at_put(index, sym);
516 break;
517 }
518 case JVM_CONSTANT_MethodHandle: {
519 const int ref_index = cp->method_handle_index_at(index);
520 check_property(valid_cp_range(ref_index, length),
521 "Invalid constant pool index %u in class file %s",
522 ref_index, CHECK);
523 const constantTag tag = cp->tag_at(ref_index);
524 const int ref_kind = cp->method_handle_ref_kind_at(index);
525
526 switch (ref_kind) {
686 cp->signature_ref_index_at(name_and_type_ref_index);
687 const Symbol* const name = cp->symbol_at(name_ref_index);
688 const Symbol* const signature = cp->symbol_at(signature_ref_index);
689 if (tag == JVM_CONSTANT_Fieldref) {
690 if (_need_verify) {
691 // Field name and signature are verified above, when iterating NameAndType_info.
692 // Need only to be sure signature is non-zero length and the right type.
693 if (Signature::is_method(signature)) {
694 throwIllegalSignature("Field", name, signature, CHECK);
695 }
696 }
697 } else {
698 if (_need_verify) {
699 // Method name and signature are individually verified above, when iterating
700 // NameAndType_info. Need to check here that signature is non-zero length and
701 // the right type.
702 if (!Signature::is_method(signature)) {
703 throwIllegalSignature("Method", name, signature, CHECK);
704 }
705 }
706 // If a class method name begins with '<', it must be "<init>" and have void signature.
707 const unsigned int name_len = name->utf8_length();
708 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
709 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
710 if (name != vmSymbols::object_initializer_name()) {
711 classfile_parse_error(
712 "Bad method name at constant pool index %u in class file %s",
713 name_ref_index, THREAD);
714 return;
715 } else if (!Signature::is_void_method(signature)) { // must have void signature.
716 throwIllegalSignature("Method", name, signature, CHECK);
717 }
718 }
719 }
720 break;
721 }
722 case JVM_CONSTANT_MethodHandle: {
723 const int ref_index = cp->method_handle_index_at(index);
724 const int ref_kind = cp->method_handle_ref_kind_at(index);
725 switch (ref_kind) {
726 case JVM_REF_invokeVirtual:
727 case JVM_REF_invokeStatic:
728 case JVM_REF_invokeSpecial:
729 case JVM_REF_newInvokeSpecial: {
730 const int name_and_type_ref_index =
731 cp->name_and_type_ref_index_at(ref_index);
732 const int name_ref_index =
733 cp->name_ref_index_at(name_and_type_ref_index);
734 const Symbol* const name = cp->symbol_at(name_ref_index);
735 if (ref_kind == JVM_REF_newInvokeSpecial) {
736 if (name != vmSymbols::object_initializer_name()) {
737 classfile_parse_error(
738 "Bad constructor name at constant pool index %u in class file %s",
739 name_ref_index, THREAD);
740 return;
741 }
742 } else {
743 if (name == vmSymbols::object_initializer_name()) {
744 classfile_parse_error(
745 "Bad method name at constant pool index %u in class file %s",
746 name_ref_index, THREAD);
747 return;
748 }
749 }
750 break;
751 }
752 // Other ref_kinds are already fully checked in previous pass.
753 } // switch(ref_kind)
754 break;
755 }
756 case JVM_CONSTANT_MethodType: {
757 const Symbol* const no_name = vmSymbols::type_name(); // place holder
758 const Symbol* const signature = cp->method_type_signature_at(index);
759 verify_legal_method_signature(no_name, signature, CHECK);
760 break;
761 }
762 case JVM_CONSTANT_Utf8: {
763 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
800 NameSigHash* entry = table[index];
801 while (entry != NULL) {
802 if (entry->_name == name && entry->_sig == sig) {
803 return false;
804 }
805 entry = entry->_next;
806 }
807
808 // No duplicate is found, allocate a new entry and fill it.
809 entry = new NameSigHash();
810 entry->_name = name;
811 entry->_sig = sig;
812
813 // Insert into hash table
814 entry->_next = table[index];
815 table[index] = entry;
816
817 return true;
818 }
819
820 // Side-effects: populates the _local_interfaces field
821 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
822 const int itfs_len,
823 ConstantPool* const cp,
824 bool* const has_nonstatic_concrete_methods,
825 TRAPS) {
826 assert(stream != NULL, "invariant");
827 assert(cp != NULL, "invariant");
828 assert(has_nonstatic_concrete_methods != NULL, "invariant");
829
830 if (itfs_len == 0) {
831 _local_interfaces = Universe::the_empty_instance_klass_array();
832 } else {
833 assert(itfs_len > 0, "only called for len>0");
834 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, NULL, CHECK);
835
836 int index;
837 for (index = 0; index < itfs_len; index++) {
838 const u2 interface_index = stream->get_u2(CHECK);
839 Klass* interf;
840 check_property(
841 valid_klass_reference_at(interface_index),
842 "Interface name has bad constant pool index %u in class file %s",
843 interface_index, CHECK);
844 if (cp->tag_at(interface_index).is_klass()) {
845 interf = cp->resolved_klass_at(interface_index);
846 } else {
847 Symbol* const unresolved_klass = cp->klass_name_at(interface_index);
848
849 // Don't need to check legal name because it's checked when parsing constant pool.
850 // But need to make sure it's not an array type.
851 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
852 "Bad interface name in class file %s", CHECK);
853
854 // Call resolve_super so class circularity is checked
855 interf = SystemDictionary::resolve_super_or_fail(
856 _class_name,
857 unresolved_klass,
858 Handle(THREAD, _loader_data->class_loader()),
859 _protection_domain,
860 false,
861 CHECK);
862 }
863
864 if (!interf->is_interface()) {
865 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
866 err_msg("class %s can not implement %s, because it is not an interface (%s)",
867 _class_name->as_klass_external_name(),
868 interf->external_name(),
869 interf->class_in_module_of_loader()));
870 }
871
872 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
873 *has_nonstatic_concrete_methods = true;
874 }
875 _local_interfaces->at_put(index, InstanceKlass::cast(interf));
876 }
877
878 if (!_need_verify || itfs_len <= 1) {
879 return;
880 }
881
882 // Check if there's any duplicates in interfaces
883 ResourceMark rm(THREAD);
884 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
885 NameSigHash*,
886 HASH_ROW_SIZE);
887 initialize_hashtable(interface_names);
888 bool dup = false;
889 const Symbol* name = NULL;
890 {
891 debug_only(NoSafepointVerifier nsv;)
892 for (index = 0; index < itfs_len; index++) {
893 const InstanceKlass* const k = _local_interfaces->at(index);
894 name = k->name();
895 // If no duplicates, add (name, NULL) in hashtable interface_names.
896 if (!put_after_lookup(name, NULL, interface_names)) {
897 dup = true;
898 break;
899 }
900 }
901 }
902 if (dup) {
903 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
904 name->as_C_string(), THREAD);
905 }
906 }
907 }
908
909 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
910 int constantvalue_index,
911 int signature_index,
912 TRAPS) const {
913 // Make sure the constant pool entry is of a type appropriate to this field
914 guarantee_property(
1360 CHECK);
1361 parsed_annotations->set_field_annotations(a);
1362 a = assemble_annotations(runtime_visible_type_annotations,
1363 runtime_visible_type_annotations_length,
1364 runtime_invisible_type_annotations,
1365 runtime_invisible_type_annotations_length,
1366 CHECK);
1367 parsed_annotations->set_field_type_annotations(a);
1368 return;
1369 }
1370
1371
1372 // Field allocation types. Used for computing field offsets.
1373
1374 enum FieldAllocationType {
1375 STATIC_OOP, // Oops
1376 STATIC_BYTE, // Boolean, Byte, char
1377 STATIC_SHORT, // shorts
1378 STATIC_WORD, // ints
1379 STATIC_DOUBLE, // aligned long or double
1380 NONSTATIC_OOP,
1381 NONSTATIC_BYTE,
1382 NONSTATIC_SHORT,
1383 NONSTATIC_WORD,
1384 NONSTATIC_DOUBLE,
1385 MAX_FIELD_ALLOCATION_TYPE,
1386 BAD_ALLOCATION_TYPE = -1
1387 };
1388
1389 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1390 BAD_ALLOCATION_TYPE, // 0
1391 BAD_ALLOCATION_TYPE, // 1
1392 BAD_ALLOCATION_TYPE, // 2
1393 BAD_ALLOCATION_TYPE, // 3
1394 NONSTATIC_BYTE , // T_BOOLEAN = 4,
1395 NONSTATIC_SHORT, // T_CHAR = 5,
1396 NONSTATIC_WORD, // T_FLOAT = 6,
1397 NONSTATIC_DOUBLE, // T_DOUBLE = 7,
1398 NONSTATIC_BYTE, // T_BYTE = 8,
1399 NONSTATIC_SHORT, // T_SHORT = 9,
1400 NONSTATIC_WORD, // T_INT = 10,
1401 NONSTATIC_DOUBLE, // T_LONG = 11,
1402 NONSTATIC_OOP, // T_OBJECT = 12,
1403 NONSTATIC_OOP, // T_ARRAY = 13,
1404 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1405 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1406 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1407 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1408 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1409 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1410 BAD_ALLOCATION_TYPE, // 0
1411 BAD_ALLOCATION_TYPE, // 1
1412 BAD_ALLOCATION_TYPE, // 2
1413 BAD_ALLOCATION_TYPE, // 3
1414 STATIC_BYTE , // T_BOOLEAN = 4,
1415 STATIC_SHORT, // T_CHAR = 5,
1416 STATIC_WORD, // T_FLOAT = 6,
1417 STATIC_DOUBLE, // T_DOUBLE = 7,
1418 STATIC_BYTE, // T_BYTE = 8,
1419 STATIC_SHORT, // T_SHORT = 9,
1420 STATIC_WORD, // T_INT = 10,
1421 STATIC_DOUBLE, // T_LONG = 11,
1422 STATIC_OOP, // T_OBJECT = 12,
1423 STATIC_OOP, // T_ARRAY = 13,
1424 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1425 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1426 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1427 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1428 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1429 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1430 };
1431
1432 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {
1433 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1434 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1435 assert(result != BAD_ALLOCATION_TYPE, "bad type");
1436 return result;
1437 }
1438
1439 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1440 public:
1441 u2 count[MAX_FIELD_ALLOCATION_TYPE];
1442
1443 FieldAllocationCount() {
1444 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1445 count[i] = 0;
1446 }
1447 }
1448
1449 void update(bool is_static, BasicType type) {
1450 FieldAllocationType atype = basic_type_to_atype(is_static, type);
1451 if (atype != BAD_ALLOCATION_TYPE) {
1452 // Make sure there is no overflow with injected fields.
1453 assert(count[atype] < 0xFFFF, "More than 65535 fields");
1454 count[atype]++;
1455 }
1456 }
1457 };
1458
1459 // Side-effects: populates the _fields, _fields_annotations,
1460 // _fields_type_annotations fields
1461 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1462 bool is_interface,
1463 FieldAllocationCount* const fac,
1464 ConstantPool* cp,
1465 const int cp_size,
1466 u2* const java_fields_count_ptr,
1467 TRAPS) {
1468
1469 assert(cfs != NULL, "invariant");
1470 assert(fac != NULL, "invariant");
1471 assert(cp != NULL, "invariant");
1472 assert(java_fields_count_ptr != NULL, "invariant");
1473
1474 assert(NULL == _fields, "invariant");
1475 assert(NULL == _fields_annotations, "invariant");
1476 assert(NULL == _fields_type_annotations, "invariant");
1477
1478 cfs->guarantee_more(2, CHECK); // length
1479 const u2 length = cfs->get_u2_fast();
1480 *java_fields_count_ptr = length;
1481
1482 int num_injected = 0;
1483 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1484 &num_injected);
1485 const int total_fields = length + num_injected;
1486
1487 // The field array starts with tuples of shorts
1488 // [access, name index, sig index, initial value index, byte offset].
1489 // A generic signature slot only exists for field with generic
1490 // signature attribute. And the access flag is set with
1491 // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic
1492 // signature slots are at the end of the field array and after all
1493 // other fields data.
1494 //
1495 // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
1496 // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
1497 // ...
1498 // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
1499 // [generic signature index]
1500 // [generic signature index]
1501 // ...
1502 //
1503 // Allocate a temporary resource array for field data. For each field,
1504 // a slot is reserved in the temporary array for the generic signature
1505 // index. After parsing all fields, the data are copied to a permanent
1506 // array and any unused slots will be discarded.
1507 ResourceMark rm(THREAD);
1508 u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
1509 u2,
1510 total_fields * (FieldInfo::field_slots + 1));
1511
1512 // The generic signature slots start after all other fields' data.
1513 int generic_signature_slot = total_fields * FieldInfo::field_slots;
1514 int num_generic_signature = 0;
1515 for (int n = 0; n < length; n++) {
1516 // access_flags, name_index, descriptor_index, attributes_count
1517 cfs->guarantee_more(8, CHECK);
1518
1519 AccessFlags access_flags;
1520 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1521 verify_legal_field_modifiers(flags, is_interface, CHECK);
1522 access_flags.set_flags(flags);
1523
1524 const u2 name_index = cfs->get_u2_fast();
1525 check_property(valid_symbol_at(name_index),
1526 "Invalid constant pool index %u for field name in class file %s",
1527 name_index, CHECK);
1528 const Symbol* const name = cp->symbol_at(name_index);
1529 verify_legal_field_name(name, CHECK);
1530
1531 const u2 signature_index = cfs->get_u2_fast();
1532 check_property(valid_symbol_at(signature_index),
1533 "Invalid constant pool index %u for field signature in class file %s",
1534 signature_index, CHECK);
1535 const Symbol* const sig = cp->symbol_at(signature_index);
1536 verify_legal_field_signature(name, sig, CHECK);
1537
1538 u2 constantvalue_index = 0;
1539 bool is_synthetic = false;
1540 u2 generic_signature_index = 0;
1541 const bool is_static = access_flags.is_static();
1542 FieldAnnotationCollector parsed_annotations(_loader_data);
1543
1544 const u2 attributes_count = cfs->get_u2_fast();
1545 if (attributes_count > 0) {
1546 parse_field_attributes(cfs,
1547 attributes_count,
1548 is_static,
1549 signature_index,
1550 &constantvalue_index,
1551 &is_synthetic,
1552 &generic_signature_index,
1553 &parsed_annotations,
1554 CHECK);
1555
1556 if (parsed_annotations.field_annotations() != NULL) {
1576
1577 if (is_synthetic) {
1578 access_flags.set_is_synthetic();
1579 }
1580 if (generic_signature_index != 0) {
1581 access_flags.set_field_has_generic_signature();
1582 fa[generic_signature_slot] = generic_signature_index;
1583 generic_signature_slot ++;
1584 num_generic_signature ++;
1585 }
1586 }
1587
1588 FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1589 field->initialize(access_flags.as_short(),
1590 name_index,
1591 signature_index,
1592 constantvalue_index);
1593 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1594
1595 // Update FieldAllocationCount for this kind of field
1596 fac->update(is_static, type);
1597
1598 // After field is initialized with type, we can augment it with aux info
1599 if (parsed_annotations.has_any_annotations()) {
1600 parsed_annotations.apply_to(field);
1601 if (field->is_contended()) {
1602 _has_contended_fields = true;
1603 }
1604 }
1605 }
1606
1607 int index = length;
1608 if (num_injected != 0) {
1609 for (int n = 0; n < num_injected; n++) {
1610 // Check for duplicates
1611 if (injected[n].may_be_java) {
1612 const Symbol* const name = injected[n].name();
1613 const Symbol* const signature = injected[n].signature();
1614 bool duplicate = false;
1615 for (int i = 0; i < length; i++) {
1616 const FieldInfo* const f = FieldInfo::from_field_array(fa, i);
1620 duplicate = true;
1621 break;
1622 }
1623 }
1624 if (duplicate) {
1625 // These will be removed from the field array at the end
1626 continue;
1627 }
1628 }
1629
1630 // Injected field
1631 FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1632 field->initialize((u2)JVM_ACC_FIELD_INTERNAL,
1633 (u2)(injected[n].name_index),
1634 (u2)(injected[n].signature_index),
1635 0);
1636
1637 const BasicType type = Signature::basic_type(injected[n].signature());
1638
1639 // Update FieldAllocationCount for this kind of field
1640 fac->update(false, type);
1641 index++;
1642 }
1643 }
1644
1645 assert(NULL == _fields, "invariant");
1646
1647 _fields =
1648 MetadataFactory::new_array<u2>(_loader_data,
1649 index * FieldInfo::field_slots + num_generic_signature,
1650 CHECK);
1651 // Sometimes injected fields already exist in the Java source so
1652 // the fields array could be too long. In that case the
1653 // fields array is trimmed. Also unused slots that were reserved
1654 // for generic signature indexes are discarded.
1655 {
1656 int i = 0;
1657 for (; i < index * FieldInfo::field_slots; i++) {
1658 _fields->at_put(i, fa[i]);
1659 }
1660 for (int j = total_fields * FieldInfo::field_slots;
1661 j < generic_signature_slot; j++) {
1662 _fields->at_put(i++, fa[j]);
1663 }
1664 assert(_fields->length() == i, "");
1942 "Exception name has bad type at constant pool %u in class file %s",
1943 checked_exception, CHECK_NULL);
1944 }
1945 }
1946 // check exceptions attribute length
1947 if (_need_verify) {
1948 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1949 sizeof(u2) * size),
1950 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1951 }
1952 return checked_exceptions_start;
1953 }
1954
1955 void ClassFileParser::throwIllegalSignature(const char* type,
1956 const Symbol* name,
1957 const Symbol* sig,
1958 TRAPS) const {
1959 assert(name != NULL, "invariant");
1960 assert(sig != NULL, "invariant");
1961
1962 ResourceMark rm(THREAD);
1963 Exceptions::fthrow(THREAD_AND_LOCATION,
1964 vmSymbols::java_lang_ClassFormatError(),
1965 "%s \"%s\" in class %s has illegal signature \"%s\"", type,
1966 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
1967 }
1968
1969 AnnotationCollector::ID
1970 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
1971 const Symbol* name,
1972 const bool can_access_vm_annotations) {
1973 const vmSymbolID sid = vmSymbols::find_sid(name);
1974 // Privileged code can use all annotations. Other code silently drops some.
1975 const bool privileged = loader_data->is_boot_class_loader_data() ||
1976 loader_data->is_platform_class_loader_data() ||
1977 can_access_vm_annotations;
1978 switch (sid) {
1979 case VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
1980 if (_location != _in_method) break; // only allow for methods
1981 if (!privileged) break; // only allow in privileged code
1982 return _method_CallerSensitive;
1983 }
1984 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
1985 if (_location != _in_method) break; // only allow for methods
1986 if (!privileged) break; // only allow in privileged code
2249 runtime_visible_type_annotations_length,
2250 runtime_invisible_type_annotations,
2251 runtime_invisible_type_annotations_length,
2252 CHECK);
2253 cm->set_type_annotations(a);
2254 }
2255 }
2256
2257
2258 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2259 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2260 // Method* to save footprint, so we only know the size of the resulting Method* when the
2261 // entire method attribute is parsed.
2262 //
2263 // The promoted_flags parameter is used to pass relevant access_flags
2264 // from the method back up to the containing klass. These flag values
2265 // are added to klass's access_flags.
2266
2267 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2268 bool is_interface,
2269 const ConstantPool* cp,
2270 AccessFlags* const promoted_flags,
2271 TRAPS) {
2272 assert(cfs != NULL, "invariant");
2273 assert(cp != NULL, "invariant");
2274 assert(promoted_flags != NULL, "invariant");
2275
2276 ResourceMark rm(THREAD);
2277 // Parse fixed parts:
2278 // access_flags, name_index, descriptor_index, attributes_count
2279 cfs->guarantee_more(8, CHECK_NULL);
2280
2281 int flags = cfs->get_u2_fast();
2282 const u2 name_index = cfs->get_u2_fast();
2283 const int cp_size = cp->length();
2284 check_property(
2285 valid_symbol_at(name_index),
2286 "Illegal constant pool index %u for method name in class file %s",
2287 name_index, CHECK_NULL);
2288 const Symbol* const name = cp->symbol_at(name_index);
2290
2291 const u2 signature_index = cfs->get_u2_fast();
2292 guarantee_property(
2293 valid_symbol_at(signature_index),
2294 "Illegal constant pool index %u for method signature in class file %s",
2295 signature_index, CHECK_NULL);
2296 const Symbol* const signature = cp->symbol_at(signature_index);
2297
2298 if (name == vmSymbols::class_initializer_name()) {
2299 // We ignore the other access flags for a valid class initializer.
2300 // (JVM Spec 2nd ed., chapter 4.6)
2301 if (_major_version < 51) { // backward compatibility
2302 flags = JVM_ACC_STATIC;
2303 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2304 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2305 } else {
2306 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2307 return NULL;
2308 }
2309 } else {
2310 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2311 }
2312
2313 if (name == vmSymbols::object_initializer_name() && is_interface) {
2314 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2315 return NULL;
2316 }
2317
2318 int args_size = -1; // only used when _need_verify is true
2319 if (_need_verify) {
2320 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2321 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2322 verify_legal_method_signature(name, signature, CHECK_NULL);
2323 if (args_size > MAX_ARGS_SIZE) {
2324 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2325 return NULL;
2326 }
2327 }
2328
2329 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2330
2331 // Default values for code and exceptions attribute elements
2332 u2 max_stack = 0;
2333 u2 max_locals = 0;
2334 u4 code_length = 0;
2335 const u1* code_start = 0;
2865 _has_finalizer = true;
2866 }
2867 }
2868 if (name == vmSymbols::object_initializer_name() &&
2869 signature == vmSymbols::void_method_signature() &&
2870 m->is_vanilla_constructor()) {
2871 _has_vanilla_constructor = true;
2872 }
2873
2874 NOT_PRODUCT(m->verify());
2875 return m;
2876 }
2877
2878
2879 // The promoted_flags parameter is used to pass relevant access_flags
2880 // from the methods back up to the containing klass. These flag values
2881 // are added to klass's access_flags.
2882 // Side-effects: populates the _methods field in the parser
2883 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2884 bool is_interface,
2885 AccessFlags* promoted_flags,
2886 bool* has_final_method,
2887 bool* declares_nonstatic_concrete_methods,
2888 TRAPS) {
2889 assert(cfs != NULL, "invariant");
2890 assert(promoted_flags != NULL, "invariant");
2891 assert(has_final_method != NULL, "invariant");
2892 assert(declares_nonstatic_concrete_methods != NULL, "invariant");
2893
2894 assert(NULL == _methods, "invariant");
2895
2896 cfs->guarantee_more(2, CHECK); // length
2897 const u2 length = cfs->get_u2_fast();
2898 if (length == 0) {
2899 _methods = Universe::the_empty_method_array();
2900 } else {
2901 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2902 length,
2903 NULL,
2904 CHECK);
2905
2906 for (int index = 0; index < length; index++) {
2907 Method* method = parse_method(cfs,
2908 is_interface,
2909 _cp,
2910 promoted_flags,
2911 CHECK);
2912
2913 if (method->is_final()) {
2914 *has_final_method = true;
2915 }
2916 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2917 // used for interface initialization, and default method inheritance analysis
2918 if (is_interface && !(*declares_nonstatic_concrete_methods)
2919 && !method->is_abstract() && !method->is_static()) {
2920 *declares_nonstatic_concrete_methods = true;
2921 }
2922 _methods->at_put(index, method);
2923 }
2924
2925 if (_need_verify && length > 1) {
2926 // Check duplicated methods
2927 ResourceMark rm(THREAD);
2928 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
3165 valid_klass_reference_at(outer_class_info_index),
3166 "outer_class_info_index %u has bad constant type in class file %s",
3167 outer_class_info_index, CHECK_0);
3168
3169 if (outer_class_info_index != 0) {
3170 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3171 char* bytes = (char*)outer_class_name->bytes();
3172 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3173 "Outer class is an array class in class file %s", CHECK_0);
3174 }
3175 // Inner class name
3176 const u2 inner_name_index = cfs->get_u2_fast();
3177 check_property(
3178 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3179 "inner_name_index %u has bad constant type in class file %s",
3180 inner_name_index, CHECK_0);
3181 if (_need_verify) {
3182 guarantee_property(inner_class_info_index != outer_class_info_index,
3183 "Class is both outer and inner class in class file %s", CHECK_0);
3184 }
3185 // Access flags
3186 jint flags;
3187 // JVM_ACC_MODULE is defined in JDK-9 and later.
3188 if (_major_version >= JAVA_9_VERSION) {
3189 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3190 } else {
3191 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3192 }
3193 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3194 // Set abstract bit for old class files for backward compatibility
3195 flags |= JVM_ACC_ABSTRACT;
3196 }
3197 verify_legal_class_modifiers(flags, CHECK_0);
3198 AccessFlags inner_access_flags(flags);
3199
3200 inner_classes->at_put(index++, inner_class_info_index);
3201 inner_classes->at_put(index++, outer_class_info_index);
3202 inner_classes->at_put(index++, inner_name_index);
3203 inner_classes->at_put(index++, inner_access_flags.as_short());
3204 }
3205
3206 // Check for circular and duplicate entries.
3207 bool has_circularity = false;
3208 if (_need_verify) {
3209 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3210 if (has_circularity) {
3211 // If circularity check failed then ignore InnerClasses attribute.
3212 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3213 index = 0;
3214 if (parsed_enclosingmethod_attribute) {
3215 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3216 _inner_classes = inner_classes;
3217 } else {
3281 if (length > 0) {
3282 int index = 0;
3283 cfs->guarantee_more(2 * length, CHECK_0);
3284 for (int n = 0; n < length; n++) {
3285 const u2 class_info_index = cfs->get_u2_fast();
3286 check_property(
3287 valid_klass_reference_at(class_info_index),
3288 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3289 class_info_index, CHECK_0);
3290 permitted_subclasses->at_put(index++, class_info_index);
3291 }
3292 assert(index == size, "wrong size");
3293 }
3294
3295 // Restore buffer's current position.
3296 cfs->set_current(current_mark);
3297
3298 return length;
3299 }
3300
3301 // Record {
3302 // u2 attribute_name_index;
3303 // u4 attribute_length;
3304 // u2 components_count;
3305 // component_info components[components_count];
3306 // }
3307 // component_info {
3308 // u2 name_index;
3309 // u2 descriptor_index
3310 // u2 attributes_count;
3311 // attribute_info_attributes[attributes_count];
3312 // }
3313 u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3314 const ConstantPool* cp,
3315 const u1* const record_attribute_start,
3316 TRAPS) {
3317 const u1* const current_mark = cfs->current();
3318 int components_count = 0;
3319 unsigned int calculate_attr_size = 0;
3320 if (record_attribute_start != NULL) {
3565 }
3566 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3567 "Bad length on BootstrapMethods in class file %s",
3568 CHECK);
3569 }
3570
3571 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3572 ConstantPool* cp,
3573 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3574 TRAPS) {
3575 assert(cfs != NULL, "invariant");
3576 assert(cp != NULL, "invariant");
3577 assert(parsed_annotations != NULL, "invariant");
3578
3579 // Set inner classes attribute to default sentinel
3580 _inner_classes = Universe::the_empty_short_array();
3581 // Set nest members attribute to default sentinel
3582 _nest_members = Universe::the_empty_short_array();
3583 // Set _permitted_subclasses attribute to default sentinel
3584 _permitted_subclasses = Universe::the_empty_short_array();
3585 cfs->guarantee_more(2, CHECK); // attributes_count
3586 u2 attributes_count = cfs->get_u2_fast();
3587 bool parsed_sourcefile_attribute = false;
3588 bool parsed_innerclasses_attribute = false;
3589 bool parsed_nest_members_attribute = false;
3590 bool parsed_permitted_subclasses_attribute = false;
3591 bool parsed_nest_host_attribute = false;
3592 bool parsed_record_attribute = false;
3593 bool parsed_enclosingmethod_attribute = false;
3594 bool parsed_bootstrap_methods_attribute = false;
3595 const u1* runtime_visible_annotations = NULL;
3596 int runtime_visible_annotations_length = 0;
3597 const u1* runtime_invisible_annotations = NULL;
3598 int runtime_invisible_annotations_length = 0;
3599 const u1* runtime_visible_type_annotations = NULL;
3600 int runtime_visible_type_annotations_length = 0;
3601 const u1* runtime_invisible_type_annotations = NULL;
3602 int runtime_invisible_type_annotations_length = 0;
3603 bool runtime_invisible_type_annotations_exists = false;
3604 bool runtime_invisible_annotations_exists = false;
3605 bool parsed_source_debug_ext_annotations_exist = false;
3606 const u1* inner_classes_attribute_start = NULL;
3607 u4 inner_classes_attribute_length = 0;
3608 u2 enclosing_method_class_index = 0;
3609 u2 enclosing_method_method_index = 0;
3610 const u1* nest_members_attribute_start = NULL;
3611 u4 nest_members_attribute_length = 0;
3612 const u1* record_attribute_start = NULL;
3613 u4 record_attribute_length = 0;
3614 const u1* permitted_subclasses_attribute_start = NULL;
3615 u4 permitted_subclasses_attribute_length = 0;
3616
3617 // Iterate over attributes
3618 while (attributes_count--) {
3619 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3620 const u2 attribute_name_index = cfs->get_u2_fast();
3621 const u4 attribute_length = cfs->get_u4_fast();
3622 check_property(
3623 valid_symbol_at(attribute_name_index),
3624 "Attribute name has bad constant pool index %u in class file %s",
3625 attribute_name_index, CHECK);
3626 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3627 if (tag == vmSymbols::tag_source_file()) {
3628 // Check for SourceFile tag
3629 if (_need_verify) {
3630 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3631 }
3632 if (parsed_sourcefile_attribute) {
3633 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3634 return;
3635 } else {
3822 return;
3823 }
3824 parsed_record_attribute = true;
3825 record_attribute_start = cfs->current();
3826 record_attribute_length = attribute_length;
3827 } else if (_major_version >= JAVA_17_VERSION) {
3828 if (tag == vmSymbols::tag_permitted_subclasses()) {
3829 if (parsed_permitted_subclasses_attribute) {
3830 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3831 return;
3832 }
3833 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3834 if (_access_flags.is_final()) {
3835 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3836 return;
3837 }
3838 parsed_permitted_subclasses_attribute = true;
3839 permitted_subclasses_attribute_start = cfs->current();
3840 permitted_subclasses_attribute_length = attribute_length;
3841 }
3842 }
3843 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3844 cfs->skip_u1(attribute_length, CHECK);
3845 } else {
3846 // Unknown attribute
3847 cfs->skip_u1(attribute_length, CHECK);
3848 }
3849 } else {
3850 // Unknown attribute
3851 cfs->skip_u1(attribute_length, CHECK);
3852 }
3853 } else {
3854 // Unknown attribute
3855 cfs->skip_u1(attribute_length, CHECK);
3856 }
3857 }
3858 _class_annotations = assemble_annotations(runtime_visible_annotations,
3859 runtime_visible_annotations_length,
3860 runtime_invisible_annotations,
3861 runtime_invisible_annotations_length,
3902 CHECK);
3903 if (_need_verify) {
3904 guarantee_property(record_attribute_length == calculated_attr_length,
3905 "Record attribute has wrong length in class file %s",
3906 CHECK);
3907 }
3908 }
3909
3910 if (parsed_permitted_subclasses_attribute) {
3911 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3912 cfs,
3913 permitted_subclasses_attribute_start,
3914 CHECK);
3915 if (_need_verify) {
3916 guarantee_property(
3917 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3918 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3919 }
3920 }
3921
3922 if (_max_bootstrap_specifier_index >= 0) {
3923 guarantee_property(parsed_bootstrap_methods_attribute,
3924 "Missing BootstrapMethods attribute in class file %s", CHECK);
3925 }
3926 }
3927
3928 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3929 assert(k != NULL, "invariant");
3930
3931 if (_synthetic_flag)
3932 k->set_is_synthetic();
3933 if (_sourcefile_index != 0) {
3934 k->set_source_file_name_index(_sourcefile_index);
3935 }
3936 if (_generic_signature_index != 0) {
3937 k->set_generic_signature_index(_generic_signature_index);
3938 }
3939 if (_sde_buffer != NULL) {
3940 k->set_source_debug_extension(_sde_buffer, _sde_length);
3941 }
3966 // _combined_annotations so these fields can now be cleared.
3967 _class_annotations = NULL;
3968 _class_type_annotations = NULL;
3969 _fields_annotations = NULL;
3970 _fields_type_annotations = NULL;
3971 }
3972
3973 // Transfer ownership of metadata allocated to the InstanceKlass.
3974 void ClassFileParser::apply_parsed_class_metadata(
3975 InstanceKlass* this_klass,
3976 int java_fields_count) {
3977 assert(this_klass != NULL, "invariant");
3978
3979 _cp->set_pool_holder(this_klass);
3980 this_klass->set_constants(_cp);
3981 this_klass->set_fields(_fields, java_fields_count);
3982 this_klass->set_methods(_methods);
3983 this_klass->set_inner_classes(_inner_classes);
3984 this_klass->set_nest_members(_nest_members);
3985 this_klass->set_nest_host_index(_nest_host);
3986 this_klass->set_annotations(_combined_annotations);
3987 this_klass->set_permitted_subclasses(_permitted_subclasses);
3988 this_klass->set_record_components(_record_components);
3989 // Delay the setting of _local_interfaces and _transitive_interfaces until after
3990 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3991 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3992 // its _super. If an OOM occurs while loading the current klass, its _super field
3993 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3994 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3995 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3996
3997 // Clear out these fields so they don't get deallocated by the destructor
3998 clear_class_metadata();
3999 }
4000
4001 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
4002 int runtime_visible_annotations_length,
4003 const u1* const runtime_invisible_annotations,
4004 int runtime_invisible_annotations_length,
4005 TRAPS) {
4017 }
4018 if (runtime_invisible_annotations != NULL) {
4019 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
4020 int append = runtime_visible_annotations_length+i;
4021 annotations->at_put(append, runtime_invisible_annotations[i]);
4022 }
4023 }
4024 }
4025 return annotations;
4026 }
4027
4028 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
4029 const int super_class_index,
4030 const bool need_verify,
4031 TRAPS) {
4032 assert(cp != NULL, "invariant");
4033 const InstanceKlass* super_klass = NULL;
4034
4035 if (super_class_index == 0) {
4036 check_property(_class_name == vmSymbols::java_lang_Object(),
4037 "Invalid superclass index %u in class file %s",
4038 super_class_index,
4039 CHECK_NULL);
4040 } else {
4041 check_property(valid_klass_reference_at(super_class_index),
4042 "Invalid superclass index %u in class file %s",
4043 super_class_index,
4044 CHECK_NULL);
4045 // The class name should be legal because it is checked when parsing constant pool.
4046 // However, make sure it is not an array type.
4047 bool is_array = false;
4048 if (cp->tag_at(super_class_index).is_klass()) {
4049 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
4050 if (need_verify)
4051 is_array = super_klass->is_array_klass();
4052 } else if (need_verify) {
4053 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
4054 }
4055 if (need_verify) {
4056 guarantee_property(!is_array,
4057 "Bad superclass name in class file %s", CHECK_NULL);
4058 }
4059 }
4060 return super_klass;
4061 }
4062
4063 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
4064 _max_nonstatic_oop_maps = max_blocks;
4065 _nonstatic_oop_map_count = 0;
4066 if (max_blocks == 0) {
4067 _nonstatic_oop_maps = NULL;
4068 } else {
4069 _nonstatic_oop_maps =
4070 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
4071 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
4072 }
4073 }
4074
4075 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
4159 }
4160
4161 void OopMapBlocksBuilder::print_on(outputStream* st) const {
4162 st->print_cr(" OopMapBlocks: %3d /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);
4163 if (_nonstatic_oop_map_count > 0) {
4164 OopMapBlock* map = _nonstatic_oop_maps;
4165 OopMapBlock* last_map = last_oop_map();
4166 assert(map <= last_map, "Last less than first");
4167 while (map <= last_map) {
4168 st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(),
4169 map->offset() + map->offset_span() - heapOopSize, map->count());
4170 map++;
4171 }
4172 }
4173 }
4174
4175 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
4176 print_on(st);
4177 }
4178
4179 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4180 assert(ik != NULL, "invariant");
4181
4182 const Klass* const super = ik->super();
4183
4184 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4185 // in which case we don't have to register objects as finalizable
4186 if (!_has_empty_finalizer) {
4187 if (_has_finalizer ||
4188 (super != NULL && super->has_finalizer())) {
4189 ik->set_has_finalizer();
4190 }
4191 }
4192
4193 #ifdef ASSERT
4194 bool f = false;
4195 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4196 vmSymbols::void_method_signature());
4197 if (InstanceKlass::is_finalization_enabled() &&
4198 (m != NULL) && !m->is_empty_method()) {
4199 f = true;
4200 }
4201
4202 // Spec doesn't prevent agent from redefinition of empty finalizer.
4203 // Despite the fact that it's generally bad idea and redefined finalizer
4204 // will not work as expected we shouldn't abort vm in this case
4205 if (!ik->has_redefined_this_or_super()) {
4206 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4207 }
4208 #endif
4209
4210 // Check if this klass supports the java.lang.Cloneable interface
4211 if (vmClasses::Cloneable_klass_loaded()) {
4212 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4213 ik->set_is_cloneable();
4214 }
4215 }
4216
4217 // Check if this klass has a vanilla default constructor
4218 if (super == NULL) {
4219 // java.lang.Object has empty default constructor
4220 ik->set_has_vanilla_constructor();
4221 } else {
4222 if (super->has_vanilla_constructor() &&
4223 _has_vanilla_constructor) {
4224 ik->set_has_vanilla_constructor();
4225 }
4226 #ifdef ASSERT
4227 bool v = false;
4228 if (super->has_vanilla_constructor()) {
4229 const Method* const constructor =
4230 ik->find_method(vmSymbols::object_initializer_name(),
4231 vmSymbols::void_method_signature());
4232 if (constructor != NULL && constructor->is_vanilla_constructor()) {
4233 v = true;
4234 }
4235 }
4236 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4237 #endif
4238 }
4239
4240 // If it cannot be fast-path allocated, set a bit in the layout helper.
4241 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4242 assert(ik->size_helper() > 0, "layout_helper is initialized");
4243 if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4244 || ik->is_abstract() || ik->is_interface()
4245 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4246 || ik->size_helper() >= FastAllocateSizeLimit) {
4247 // Forbid fast-path allocation.
4248 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4249 ik->set_layout_helper(lh);
4250 }
4251 }
4252
4253 // utility methods for appending an array with check for duplicates
4254
4255 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4256 const Array<InstanceKlass*>* const ifs) {
4257 // iterate over new interfaces
4258 for (int i = 0; i < ifs->length(); i++) {
4259 InstanceKlass* const e = ifs->at(i);
4260 assert(e->is_klass() && e->is_interface(), "just checking");
4261 // add new interface
4262 result->append_if_missing(e);
4263 }
4264 }
4265
4266 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4267 Array<InstanceKlass*>* local_ifs,
4268 ClassLoaderData* loader_data,
4269 TRAPS) {
4270 assert(local_ifs != NULL, "invariant");
4271 assert(loader_data != NULL, "invariant");
4272
4276 // Add superclass transitive interfaces size
4277 if (super != NULL) {
4278 super_size = super->transitive_interfaces()->length();
4279 max_transitive_size += super_size;
4280 }
4281 // Add local interfaces' super interfaces
4282 const int local_size = local_ifs->length();
4283 for (int i = 0; i < local_size; i++) {
4284 InstanceKlass* const l = local_ifs->at(i);
4285 max_transitive_size += l->transitive_interfaces()->length();
4286 }
4287 // Finally add local interfaces
4288 max_transitive_size += local_size;
4289 // Construct array
4290 if (max_transitive_size == 0) {
4291 // no interfaces, use canonicalized array
4292 return Universe::the_empty_instance_klass_array();
4293 } else if (max_transitive_size == super_size) {
4294 // no new local interfaces added, share superklass' transitive interface array
4295 return super->transitive_interfaces();
4296 } else if (max_transitive_size == local_size) {
4297 // only local interfaces added, share local interface array
4298 return local_ifs;
4299 } else {
4300 ResourceMark rm;
4301 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4302
4303 // Copy down from superclass
4304 if (super != NULL) {
4305 append_interfaces(result, super->transitive_interfaces());
4306 }
4307
4308 // Copy down from local interfaces' superinterfaces
4309 for (int i = 0; i < local_size; i++) {
4310 InstanceKlass* const l = local_ifs->at(i);
4311 append_interfaces(result, l->transitive_interfaces());
4312 }
4313 // Finally add local interfaces
4314 append_interfaces(result, local_ifs);
4315
4316 // length will be less than the max_transitive_size if duplicates were removed
4317 const int length = result->length();
4318 assert(length <= max_transitive_size, "just checking");
4319 Array<InstanceKlass*>* const new_result =
4320 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4321 for (int i = 0; i < length; i++) {
4322 InstanceKlass* const e = result->at(i);
4323 assert(e != NULL, "just checking");
4324 new_result->at_put(i, e);
4325 }
4326 return new_result;
4327 }
4328 }
4329
4330 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4331 assert(this_klass != NULL, "invariant");
4332 const Klass* const super = this_klass->super();
4333
4334 if (super != NULL) {
4335 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4336
4337 if (super->is_final()) {
4338 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4339 return;
4340 }
4341
4342 if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {
4343 classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);
4344 return;
4345 }
4346
4347 // If the loader is not the boot loader then throw an exception if its
4348 // superclass is in package jdk.internal.reflect and its loader is not a
4349 // special reflection class loader
4350 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
4351 PackageEntry* super_package = super->package();
4352 if (super_package != NULL &&
4353 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
4354 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
4355 ResourceMark rm(THREAD);
4356 Exceptions::fthrow(
4357 THREAD_AND_LOCATION,
4358 vmSymbols::java_lang_IllegalAccessError(),
4359 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
4360 this_klass->external_name(),
4361 this_klass->class_loader_data()->loader_name_and_id(),
4362 super->external_name());
4363 return;
4364 }
4365 }
4366
4510
4511 for (int index = 0; index < num_methods; index++) {
4512 const Method* const m = methods->at(index);
4513 // if m is static and not the init method, throw a verify error
4514 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4515 ResourceMark rm(THREAD);
4516 Exceptions::fthrow(
4517 THREAD_AND_LOCATION,
4518 vmSymbols::java_lang_VerifyError(),
4519 "Illegal static method %s in interface %s",
4520 m->name()->as_C_string(),
4521 this_klass->external_name()
4522 );
4523 return;
4524 }
4525 }
4526 }
4527
4528 // utility methods for format checking
4529
4530 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4531 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4532 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4533 if (is_module) {
4534 ResourceMark rm(THREAD);
4535 Exceptions::fthrow(
4536 THREAD_AND_LOCATION,
4537 vmSymbols::java_lang_NoClassDefFoundError(),
4538 "%s is not a class because access_flag ACC_MODULE is set",
4539 _class_name->as_C_string());
4540 return;
4541 }
4542
4543 if (!_need_verify) { return; }
4544
4545 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4546 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4547 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4548 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4549 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4550 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4551 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4552
4553 if ((is_abstract && is_final) ||
4554 (is_interface && !is_abstract) ||
4555 (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4556 (!is_interface && major_gte_1_5 && is_annotation)) {
4557 ResourceMark rm(THREAD);
4558 Exceptions::fthrow(
4559 THREAD_AND_LOCATION,
4560 vmSymbols::java_lang_ClassFormatError(),
4561 "Illegal class modifiers in class %s: 0x%X",
4562 _class_name->as_C_string(), flags
4563 );
4564 return;
4565 }
4566 }
4567
4568 static bool has_illegal_visibility(jint flags) {
4569 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4570 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4571 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4572
4573 return ((is_public && is_protected) ||
4574 (is_public && is_private) ||
4575 (is_protected && is_private));
4576 }
4577
4578 // A legal major_version.minor_version must be one of the following:
4579 //
4580 // Major_version >= 45 and major_version < 56, any minor_version.
4581 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4582 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4583 //
4584 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4610 THREAD_AND_LOCATION,
4611 vmSymbols::java_lang_UnsupportedClassVersionError(),
4612 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4613 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4614 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4615 return;
4616 }
4617
4618 if (!Arguments::enable_preview()) {
4619 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4620 class_name, major, minor, THREAD);
4621 return;
4622 }
4623
4624 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4625 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4626 class_name, major, minor, THREAD);
4627 }
4628 }
4629
4630 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4631 bool is_interface,
4632 TRAPS) const {
4633 if (!_need_verify) { return; }
4634
4635 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4636 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4637 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4638 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4639 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4640 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4641 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4642 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4643 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4644
4645 bool is_illegal = false;
4646
4647 if (is_interface) {
4648 if (!is_public || !is_static || !is_final || is_private ||
4649 is_protected || is_volatile || is_transient ||
4650 (major_gte_1_5 && is_enum)) {
4651 is_illegal = true;
4652 }
4653 } else { // not interface
4654 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4655 is_illegal = true;
4656 }
4657 }
4658
4659 if (is_illegal) {
4660 ResourceMark rm(THREAD);
4661 Exceptions::fthrow(
4662 THREAD_AND_LOCATION,
4663 vmSymbols::java_lang_ClassFormatError(),
4664 "Illegal field modifiers in class %s: 0x%X",
4665 _class_name->as_C_string(), flags);
4666 return;
4667 }
4668 }
4669
4670 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4671 bool is_interface,
4672 const Symbol* name,
4673 TRAPS) const {
4674 if (!_need_verify) { return; }
4675
4676 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4677 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4678 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4679 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4680 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4681 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4682 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4683 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4684 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4685 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4686 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4687 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4688 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4689 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4690
4691 bool is_illegal = false;
4692
4693 if (is_interface) {
4694 if (major_gte_8) {
4695 // Class file version is JAVA_8_VERSION or later Methods of
4696 // interfaces may set any of the flags except ACC_PROTECTED,
4697 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4698 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4699 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4700 (is_native || is_protected || is_final || is_synchronized) ||
4701 // If a specific method of a class or interface has its
4702 // ACC_ABSTRACT flag set, it must not have any of its
4703 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4704 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4705 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4706 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4707 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4708 is_illegal = true;
4709 }
4710 } else if (major_gte_1_5) {
4711 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4712 if (!is_public || is_private || is_protected || is_static || is_final ||
4713 is_synchronized || is_native || !is_abstract || is_strict) {
4714 is_illegal = true;
4715 }
4716 } else {
4717 // Class file version is pre-JAVA_1_5_VERSION
4718 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4719 is_illegal = true;
4720 }
4721 }
4722 } else { // not interface
4723 if (has_illegal_visibility(flags)) {
4724 is_illegal = true;
4725 } else {
4726 if (is_initializer) {
4727 if (is_static || is_final || is_synchronized || is_native ||
4728 is_abstract || (major_gte_1_5 && is_bridge)) {
4729 is_illegal = true;
4730 }
4731 } else { // not initializer
4732 if (is_abstract) {
4733 if ((is_final || is_native || is_private || is_static ||
4734 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4735 is_illegal = true;
4736 }
4737 }
4738 }
4739 }
4740 }
4741
4742 if (is_illegal) {
4743 ResourceMark rm(THREAD);
4744 Exceptions::fthrow(
4745 THREAD_AND_LOCATION,
4746 vmSymbols::java_lang_ClassFormatError(),
4747 "Method %s in class %s has illegal modifiers: 0x%X",
4748 name->as_C_string(), _class_name->as_C_string(), flags);
4749 return;
4750 }
4751 }
4752
4753 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4754 int length,
4755 TRAPS) const {
4756 assert(_need_verify, "only called when _need_verify is true");
4757 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4758 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4759 }
4760 }
4761
4762 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4763 // In class names, '/' separates unqualified names. This is verified in this function also.
4764 // Method names also may not contain the characters '<' or '>', unless <init>
4765 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4766 // method. Because these names have been checked as special cases before
4767 // calling this method in verify_legal_method_name.
4768 //
4886 // be taken as a field signature. Allow "void" if void_ok.
4887 // Return a pointer to just past the signature.
4888 // Return NULL if no legal signature is found.
4889 const char* ClassFileParser::skip_over_field_signature(const char* signature,
4890 bool void_ok,
4891 unsigned int length,
4892 TRAPS) const {
4893 unsigned int array_dim = 0;
4894 while (length > 0) {
4895 switch (signature[0]) {
4896 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
4897 case JVM_SIGNATURE_BOOLEAN:
4898 case JVM_SIGNATURE_BYTE:
4899 case JVM_SIGNATURE_CHAR:
4900 case JVM_SIGNATURE_SHORT:
4901 case JVM_SIGNATURE_INT:
4902 case JVM_SIGNATURE_FLOAT:
4903 case JVM_SIGNATURE_LONG:
4904 case JVM_SIGNATURE_DOUBLE:
4905 return signature + 1;
4906 case JVM_SIGNATURE_CLASS: {
4907 if (_major_version < JAVA_1_5_VERSION) {
4908 // Skip over the class name if one is there
4909 const char* const p = skip_over_field_name(signature + 1, true, --length);
4910
4911 // The next character better be a semicolon
4912 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
4913 return p + 1;
4914 }
4915 }
4916 else {
4917 // Skip leading 'L' and ignore first appearance of ';'
4918 signature++;
4919 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
4920 // Format check signature
4921 if (c != NULL) {
4922 int newlen = c - (char*) signature;
4923 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
4924 if (!legal) {
4925 classfile_parse_error("Class name is empty or contains illegal character "
4926 "in descriptor in class file %s",
4927 THREAD);
4928 return NULL;
4929 }
4930 return signature + newlen + 1;
4931 }
4932 }
4933 return NULL;
4934 }
4935 case JVM_SIGNATURE_ARRAY:
4936 array_dim++;
4937 if (array_dim > 255) {
4953
4954 // Checks if name is a legal class name.
4955 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
4956 if (!_need_verify || _relax_verify) { return; }
4957
4958 assert(name->refcount() > 0, "symbol must be kept alive");
4959 char* bytes = (char*)name->bytes();
4960 unsigned int length = name->utf8_length();
4961 bool legal = false;
4962
4963 if (length > 0) {
4964 const char* p;
4965 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4966 p = skip_over_field_signature(bytes, false, length, CHECK);
4967 legal = (p != NULL) && ((p - bytes) == (int)length);
4968 } else if (_major_version < JAVA_1_5_VERSION) {
4969 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
4970 p = skip_over_field_name(bytes, true, length);
4971 legal = (p != NULL) && ((p - bytes) == (int)length);
4972 }
4973 } else {
4974 // 4900761: relax the constraints based on JSR202 spec
4975 // Class names may be drawn from the entire Unicode character set.
4976 // Identifiers between '/' must be unqualified names.
4977 // The utf8 string has been verified when parsing cpool entries.
4978 legal = verify_unqualified_name(bytes, length, LegalClass);
4979 }
4980 }
4981 if (!legal) {
4982 ResourceMark rm(THREAD);
4983 assert(_class_name != NULL, "invariant");
4984 Exceptions::fthrow(
4985 THREAD_AND_LOCATION,
4986 vmSymbols::java_lang_ClassFormatError(),
4987 "Illegal class name \"%.*s\" in class file %s", length, bytes,
4988 _class_name->as_C_string()
4989 );
4990 return;
4991 }
4992 }
5018 THREAD_AND_LOCATION,
5019 vmSymbols::java_lang_ClassFormatError(),
5020 "Illegal field name \"%.*s\" in class %s", length, bytes,
5021 _class_name->as_C_string()
5022 );
5023 return;
5024 }
5025 }
5026
5027 // Checks if name is a legal method name.
5028 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
5029 if (!_need_verify || _relax_verify) { return; }
5030
5031 assert(name != NULL, "method name is null");
5032 char* bytes = (char*)name->bytes();
5033 unsigned int length = name->utf8_length();
5034 bool legal = false;
5035
5036 if (length > 0) {
5037 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
5038 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
5039 legal = true;
5040 }
5041 } else if (_major_version < JAVA_1_5_VERSION) {
5042 const char* p;
5043 p = skip_over_field_name(bytes, false, length);
5044 legal = (p != NULL) && ((p - bytes) == (int)length);
5045 } else {
5046 // 4881221: relax the constraints based on JSR202 spec
5047 legal = verify_unqualified_name(bytes, length, LegalMethod);
5048 }
5049 }
5050
5051 if (!legal) {
5052 ResourceMark rm(THREAD);
5053 assert(_class_name != NULL, "invariant");
5054 Exceptions::fthrow(
5055 THREAD_AND_LOCATION,
5056 vmSymbols::java_lang_ClassFormatError(),
5057 "Illegal method name \"%.*s\" in class %s", length, bytes,
5058 _class_name->as_C_string()
5059 );
5060 return;
5061 }
5062 }
5063
5064
5065 // Checks if signature is a legal field signature.
5066 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5067 const Symbol* signature,
5068 TRAPS) const {
5069 if (!_need_verify) { return; }
5070
5071 const char* const bytes = (const char* const)signature->bytes();
5072 const unsigned int length = signature->utf8_length();
5073 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5074
5075 if (p == NULL || (p - bytes) != (int)length) {
5076 throwIllegalSignature("Field", name, signature, CHECK);
5077 }
5078 }
5079
5080 // Check that the signature is compatible with the method name. For example,
5081 // check that <init> has a void signature.
5082 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5083 const Symbol* signature,
5084 TRAPS) const {
5085 if (!_need_verify) {
5086 return;
5087 }
5088
5089 // Class initializers cannot have args for class format version >= 51.
5090 if (name == vmSymbols::class_initializer_name() &&
5091 signature != vmSymbols::void_method_signature() &&
5092 _major_version >= JAVA_7_VERSION) {
5093 throwIllegalSignature("Method", name, signature, THREAD);
5094 return;
5095 }
5096
5097 int sig_length = signature->utf8_length();
5098 if (name->utf8_length() > 0 &&
5099 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5100 sig_length > 0 &&
5101 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5102 throwIllegalSignature("Method", name, signature, THREAD);
5103 }
5104 }
5105
5106 // Checks if signature is a legal method signature.
5107 // Returns number of parameters
5108 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5109 const Symbol* signature,
5110 TRAPS) const {
5111 if (!_need_verify) {
5112 // make sure caller's args_size will be less than 0 even for non-static
5113 // method so it will be recomputed in compute_size_of_parameters().
5114 return -2;
5115 }
5116
5117 unsigned int args_size = 0;
5118 const char* p = (const char*)signature->bytes();
5119 unsigned int length = signature->utf8_length();
5120 const char* nextp;
5121
5122 // The first character must be a '('
5258 }
5259 }
5260
5261 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5262 const ClassInstanceInfo& cl_inst_info,
5263 TRAPS) {
5264 if (_klass != NULL) {
5265 return _klass;
5266 }
5267
5268 InstanceKlass* const ik =
5269 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5270
5271 if (is_hidden()) {
5272 mangle_hidden_class_name(ik);
5273 }
5274
5275 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5276
5277 assert(_klass == ik, "invariant");
5278
5279 return ik;
5280 }
5281
5282 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5283 bool changed_by_loadhook,
5284 const ClassInstanceInfo& cl_inst_info,
5285 TRAPS) {
5286 assert(ik != NULL, "invariant");
5287
5288 // Set name and CLD before adding to CLD
5289 ik->set_class_loader_data(_loader_data);
5290 ik->set_name(_class_name);
5291
5292 // Add all classes to our internal class loader list here,
5293 // including classes in the bootstrap (NULL) class loader.
5294 const bool publicize = !is_internal();
5295
5296 _loader_data->add_class(ik, publicize);
5297
5298 set_klass_to_deallocate(ik);
5299
5300 assert(_field_info != NULL, "invariant");
5301 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5302 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5303 "sanity");
5304
5305 assert(ik->is_instance_klass(), "sanity");
5306 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5307
5308 // Fill in information already parsed
5309 ik->set_should_verify_class(_need_verify);
5310
5311 // Not yet: supers are done below to support the new subtype-checking fields
5312 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5313 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5314 assert(_fac != NULL, "invariant");
5315 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5316
5317 // this transfers ownership of a lot of arrays from
5318 // the parser onto the InstanceKlass*
5319 apply_parsed_class_metadata(ik, _java_fields_count);
5320
5321 // can only set dynamic nest-host after static nest information is set
5322 if (cl_inst_info.dynamic_nest_host() != NULL) {
5323 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5324 }
5325
5326 // note that is not safe to use the fields in the parser from this point on
5327 assert(NULL == _cp, "invariant");
5328 assert(NULL == _fields, "invariant");
5329 assert(NULL == _methods, "invariant");
5330 assert(NULL == _inner_classes, "invariant");
5331 assert(NULL == _nest_members, "invariant");
5332 assert(NULL == _combined_annotations, "invariant");
5333 assert(NULL == _record_components, "invariant");
5334 assert(NULL == _permitted_subclasses, "invariant");
5335
5336 if (_has_final_method) {
5337 ik->set_has_final_method();
5338 }
5339
5340 ik->copy_method_ordering(_method_ordering, CHECK);
5341 // The InstanceKlass::_methods_jmethod_ids cache
5342 // is managed on the assumption that the initial cache
5343 // size is equal to the number of methods in the class. If
5344 // that changes, then InstanceKlass::idnum_can_increment()
5345 // has to be changed accordingly.
5346 ik->set_initial_method_idnum(ik->methods()->length());
5347
5348 ik->set_this_class_index(_this_class_index);
5349
5350 if (_is_hidden) {
5351 // _this_class_index is a CONSTANT_Class entry that refers to this
5352 // hidden class itself. If this class needs to refer to its own methods
5353 // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5354 // _this_class_index. However, because this class is hidden (it's
5355 // not stored in SystemDictionary), _this_class_index cannot be resolved
5356 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5357 // Therefore, we must eagerly resolve _this_class_index now.
5358 ik->constants()->klass_at_put(_this_class_index, ik);
5359 }
5360
5361 ik->set_minor_version(_minor_version);
5362 ik->set_major_version(_major_version);
5363 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5364 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5365
5366 if (_is_hidden) {
5367 ik->set_is_hidden();
5368 }
5369
5370 // Set PackageEntry for this_klass
5371 oop cl = ik->class_loader();
5372 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5373 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5374 ik->set_package(cld, NULL, CHECK);
5375
5376 const Array<Method*>* const methods = ik->methods();
5377 assert(methods != NULL, "invariant");
5378 const int methods_len = methods->length();
5379
5380 check_methods_for_intrinsics(ik, methods);
5381
5382 // Fill in field values obtained by parse_classfile_attributes
5383 if (_parsed_annotations->has_any_annotations()) {
5384 _parsed_annotations->apply_to(ik);
5452
5453 assert(_all_mirandas != NULL, "invariant");
5454
5455 // Generate any default methods - default methods are public interface methods
5456 // that have a default implementation. This is new with Java 8.
5457 if (_has_nonstatic_concrete_methods) {
5458 DefaultMethods::generate_default_methods(ik,
5459 _all_mirandas,
5460 CHECK);
5461 }
5462
5463 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5464 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5465 !module_entry->has_default_read_edges()) {
5466 if (!module_entry->set_has_default_read_edges()) {
5467 // We won a potential race
5468 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5469 }
5470 }
5471
5472 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5473
5474 if (!is_internal()) {
5475 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5476
5477 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5478 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5479 log_is_enabled(Info, class, preview)) {
5480 ResourceMark rm;
5481 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5482 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5483 }
5484
5485 if (log_is_enabled(Debug, class, resolve)) {
5486 ResourceMark rm;
5487 // print out the superclass.
5488 const char * from = ik->external_name();
5489 if (ik->java_super() != NULL) {
5490 log_debug(class, resolve)("%s %s (super)",
5491 from,
5542 ClassFileParser::ClassFileParser(ClassFileStream* stream,
5543 Symbol* name,
5544 ClassLoaderData* loader_data,
5545 const ClassLoadInfo* cl_info,
5546 Publicity pub_level,
5547 TRAPS) :
5548 _stream(stream),
5549 _class_name(NULL),
5550 _loader_data(loader_data),
5551 _is_hidden(cl_info->is_hidden()),
5552 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5553 _orig_cp_size(0),
5554 _super_klass(),
5555 _cp(NULL),
5556 _fields(NULL),
5557 _methods(NULL),
5558 _inner_classes(NULL),
5559 _nest_members(NULL),
5560 _nest_host(0),
5561 _permitted_subclasses(NULL),
5562 _record_components(NULL),
5563 _local_interfaces(NULL),
5564 _transitive_interfaces(NULL),
5565 _combined_annotations(NULL),
5566 _class_annotations(NULL),
5567 _class_type_annotations(NULL),
5568 _fields_annotations(NULL),
5569 _fields_type_annotations(NULL),
5570 _klass(NULL),
5571 _klass_to_deallocate(NULL),
5572 _parsed_annotations(NULL),
5573 _fac(NULL),
5574 _field_info(NULL),
5575 _method_ordering(NULL),
5576 _all_mirandas(NULL),
5577 _vtable_size(0),
5578 _itable_size(0),
5579 _num_miranda_methods(0),
5580 _protection_domain(cl_info->protection_domain()),
5581 _access_flags(),
5582 _pub_level(pub_level),
5583 _bad_constant_seen(0),
5584 _synthetic_flag(false),
5585 _sde_length(false),
5586 _sde_buffer(NULL),
5587 _sourcefile_index(0),
5588 _generic_signature_index(0),
5589 _major_version(0),
5590 _minor_version(0),
5591 _this_class_index(0),
5592 _super_class_index(0),
5593 _itfs_len(0),
5594 _java_fields_count(0),
5595 _need_verify(false),
5596 _relax_verify(false),
5597 _has_nonstatic_concrete_methods(false),
5598 _declares_nonstatic_concrete_methods(false),
5599 _has_final_method(false),
5600 _has_contended_fields(false),
5601 _has_finalizer(false),
5602 _has_empty_finalizer(false),
5603 _has_vanilla_constructor(false),
5604 _max_bootstrap_specifier_index(-1) {
5605
5606 _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
5607 _class_name->increment_refcount();
5608
5609 assert(_loader_data != NULL, "invariant");
5610 assert(stream != NULL, "invariant");
5611 assert(_stream != NULL, "invariant");
5612 assert(_stream->buffer() == _stream->current(), "invariant");
5613 assert(_class_name != NULL, "invariant");
5614 assert(0 == _access_flags.as_int(), "invariant");
5615
5616 // Figure out whether we can skip format checking (matching classic VM behavior)
5617 if (DumpSharedSpaces) {
5618 // verify == true means it's a 'remote' class (i.e., non-boot class)
5619 // Verification decision is based on BytecodeVerificationRemote flag
5620 // for those classes.
5630 stream->set_verify(_need_verify);
5631
5632 // Check if verification needs to be relaxed for this class file
5633 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5634 _relax_verify = relax_format_check_for(_loader_data);
5635
5636 parse_stream(stream, CHECK);
5637
5638 post_process_parsed_stream(stream, _cp, CHECK);
5639 }
5640
5641 void ClassFileParser::clear_class_metadata() {
5642 // metadata created before the instance klass is created. Must be
5643 // deallocated if classfile parsing returns an error.
5644 _cp = NULL;
5645 _fields = NULL;
5646 _methods = NULL;
5647 _inner_classes = NULL;
5648 _nest_members = NULL;
5649 _permitted_subclasses = NULL;
5650 _combined_annotations = NULL;
5651 _class_annotations = _class_type_annotations = NULL;
5652 _fields_annotations = _fields_type_annotations = NULL;
5653 _record_components = NULL;
5654 }
5655
5656 // Destructor to clean up
5657 ClassFileParser::~ClassFileParser() {
5658 _class_name->decrement_refcount();
5659
5660 if (_cp != NULL) {
5661 MetadataFactory::free_metadata(_loader_data, _cp);
5662 }
5663 if (_fields != NULL) {
5664 MetadataFactory::free_array<u2>(_loader_data, _fields);
5665 }
5666
5667 if (_methods != NULL) {
5668 // Free methods
5669 InstanceKlass::deallocate_methods(_loader_data, _methods);
5670 }
5671
5672 // beware of the Universe::empty_blah_array!!
5673 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
5674 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5675 }
5676
5677 if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
5678 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5679 }
5680
5681 if (_record_components != NULL) {
5682 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5683 }
5684
5685 if (_permitted_subclasses != NULL && _permitted_subclasses != Universe::the_empty_short_array()) {
5686 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5687 }
5688
5689 // Free interfaces
5690 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5691 _local_interfaces, _transitive_interfaces);
5692
5693 if (_combined_annotations != NULL) {
5694 // After all annotations arrays have been created, they are installed into the
5695 // Annotations object that will be assigned to the InstanceKlass being created.
5696
5697 // Deallocate the Annotations object and the installed annotations arrays.
5698 _combined_annotations->deallocate_contents(_loader_data);
5699
5700 // If the _combined_annotations pointer is non-NULL,
5701 // then the other annotations fields should have been cleared.
5702 assert(_class_annotations == NULL, "Should have been cleared");
5703 assert(_class_type_annotations == NULL, "Should have been cleared");
5704 assert(_fields_annotations == NULL, "Should have been cleared");
5705 assert(_fields_type_annotations == NULL, "Should have been cleared");
5706 } else {
5707 // If the annotations arrays were not installed into the Annotations object,
5708 // then they have to be deallocated explicitly.
5753 cp_size, CHECK);
5754
5755 _orig_cp_size = cp_size;
5756 if (is_hidden()) { // Add a slot for hidden class name.
5757 cp_size++;
5758 }
5759
5760 _cp = ConstantPool::allocate(_loader_data,
5761 cp_size,
5762 CHECK);
5763
5764 ConstantPool* const cp = _cp;
5765
5766 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
5767
5768 assert(cp_size == (const u2)cp->length(), "invariant");
5769
5770 // ACCESS FLAGS
5771 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5772
5773 // Access flags
5774 jint flags;
5775 // JVM_ACC_MODULE is defined in JDK-9 and later.
5776 if (_major_version >= JAVA_9_VERSION) {
5777 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5778 } else {
5779 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5780 }
5781
5782 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5783 // Set abstract bit for old class files for backward compatibility
5784 flags |= JVM_ACC_ABSTRACT;
5785 }
5786
5787 verify_legal_class_modifiers(flags, CHECK);
5788
5789 short bad_constant = class_bad_constant_seen();
5790 if (bad_constant != 0) {
5791 // Do not throw CFE until after the access_flags are checked because if
5792 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5793 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5794 return;
5795 }
5796
5797 _access_flags.set_flags(flags);
5798
5799 // This class and superclass
5800 _this_class_index = stream->get_u2_fast();
5801 check_property(
5802 valid_cp_range(_this_class_index, cp_size) &&
5803 cp->tag_at(_this_class_index).is_unresolved_klass(),
5804 "Invalid this class index %u in constant pool in class file %s",
5805 _this_class_index, CHECK);
5806
5807 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
5808 assert(class_name_in_cp != NULL, "class_name can't be null");
5809
5810 // Don't need to check whether this class name is legal or not.
5811 // It has been checked when constant pool is parsed.
5812 // However, make sure it is not an array type.
5813 if (_need_verify) {
5814 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
5815 "Bad class name in class file %s",
5816 CHECK);
5817 }
5818
5819 #ifdef ASSERT
5820 // Basic sanity checks
5821 if (_is_hidden) {
5822 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
5823 }
5824 #endif
5825
5826 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
5827
5828 if (_is_hidden) {
5829 assert(_class_name != NULL, "Unexpected null _class_name");
5867 if (stream->source() != NULL) {
5868 ls.print(" source: %s", stream->source());
5869 }
5870 ls.cr();
5871 }
5872 }
5873
5874 // SUPERKLASS
5875 _super_class_index = stream->get_u2_fast();
5876 _super_klass = parse_super_class(cp,
5877 _super_class_index,
5878 _need_verify,
5879 CHECK);
5880
5881 // Interfaces
5882 _itfs_len = stream->get_u2_fast();
5883 parse_interfaces(stream,
5884 _itfs_len,
5885 cp,
5886 &_has_nonstatic_concrete_methods,
5887 CHECK);
5888
5889 assert(_local_interfaces != NULL, "invariant");
5890
5891 // Fields (offsets are filled in later)
5892 _fac = new FieldAllocationCount();
5893 parse_fields(stream,
5894 _access_flags.is_interface(),
5895 _fac,
5896 cp,
5897 cp_size,
5898 &_java_fields_count,
5899 CHECK);
5900
5901 assert(_fields != NULL, "invariant");
5902
5903 // Methods
5904 AccessFlags promoted_flags;
5905 parse_methods(stream,
5906 _access_flags.is_interface(),
5907 &promoted_flags,
5908 &_has_final_method,
5909 &_declares_nonstatic_concrete_methods,
5910 CHECK);
5911
5912 assert(_methods != NULL, "invariant");
5913
5914 // promote flags from parse_methods() to the klass' flags
5915 _access_flags.add_promoted_flags(promoted_flags.as_int());
5916
5917 if (_declares_nonstatic_concrete_methods) {
5918 _has_nonstatic_concrete_methods = true;
5919 }
5920
5921 // Additional attributes/annotations
5922 _parsed_annotations = new ClassAnnotationCollector();
5923 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
5924
5925 assert(_inner_classes != NULL, "invariant");
5926
5969 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
5970 // We have to update the resolved_klass_index and the name_index together
5971 // so extract the existing resolved_klass_index first.
5972 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
5973 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
5974 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
5975 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
5976 "Bad name_index");
5977 }
5978
5979 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
5980 ConstantPool* cp,
5981 TRAPS) {
5982 assert(stream != NULL, "invariant");
5983 assert(stream->at_eos(), "invariant");
5984 assert(cp != NULL, "invariant");
5985 assert(_loader_data != NULL, "invariant");
5986
5987 if (_class_name == vmSymbols::java_lang_Object()) {
5988 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
5989 "java.lang.Object cannot implement an interface in class file %s",
5990 CHECK);
5991 }
5992 // We check super class after class file is parsed and format is checked
5993 if (_super_class_index > 0 && NULL == _super_klass) {
5994 Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
5995 if (_access_flags.is_interface()) {
5996 // Before attempting to resolve the superclass, check for class format
5997 // errors not checked yet.
5998 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
5999 "Interfaces must have java.lang.Object as superclass in class file %s",
6000 CHECK);
6001 }
6002 Handle loader(THREAD, _loader_data->class_loader());
6003 _super_klass = (const InstanceKlass*)
6004 SystemDictionary::resolve_super_or_fail(_class_name,
6005 super_class_name,
6006 loader,
6007 _protection_domain,
6008 true,
6009 CHECK);
6010 }
6011
6012 if (_super_klass != NULL) {
6013 if (_super_klass->has_nonstatic_concrete_methods()) {
6014 _has_nonstatic_concrete_methods = true;
6015 }
6016
6017 if (_super_klass->is_interface()) {
6018 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
6019 return;
6020 }
6021 }
6022
6023 // Compute the transitive list of all unique interfaces implemented by this class
6024 _transitive_interfaces =
6025 compute_transitive_interfaces(_super_klass,
6026 _local_interfaces,
6027 _loader_data,
6028 CHECK);
6029
6030 assert(_transitive_interfaces != NULL, "invariant");
6031
6032 // sort methods
6033 _method_ordering = sort_methods(_methods);
6034
6035 _all_mirandas = new GrowableArray<Method*>(20);
6036
6037 Handle loader(THREAD, _loader_data->class_loader());
6038 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6039 &_num_miranda_methods,
6040 _all_mirandas,
6041 _super_klass,
6042 _methods,
6043 _access_flags,
6044 _major_version,
6045 loader,
6046 _class_name,
6047 _local_interfaces);
6048
6049 // Size of Java itable (in words)
6050 _itable_size = _access_flags.is_interface() ? 0 :
6051 klassItable::compute_itable_size(_transitive_interfaces);
6052
6053 assert(_fac != NULL, "invariant");
6054 assert(_parsed_annotations != NULL, "invariant");
6055
6056 _field_info = new FieldLayoutInfo();
6057 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, _fields,
6058 _parsed_annotations->is_contended(), _field_info);
6059 lb.build_layout();
6060 }
6061
6062 void ClassFileParser::set_klass(InstanceKlass* klass) {
6063
6064 #ifdef ASSERT
6065 if (klass != NULL) {
6066 assert(NULL == _klass, "leaking?");
6067 }
6068 #endif
6069
6070 _klass = klass;
6071 }
6072
6073 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6074
6075 #ifdef ASSERT
6076 if (klass != NULL) {
6077 assert(NULL == _klass_to_deallocate, "leaking?");
6078 }
6079 #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 "precompiled.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/fieldStreams.inline.hpp"
53 #include "oops/inlineKlass.inline.hpp"
54 #include "oops/instanceKlass.inline.hpp"
55 #include "oops/instanceMirrorKlass.hpp"
56 #include "oops/klass.inline.hpp"
57 #include "oops/klassVtable.hpp"
58 #include "oops/metadata.hpp"
59 #include "oops/method.inline.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "oops/recordComponent.hpp"
62 #include "oops/symbol.hpp"
63 #include "prims/jvmtiExport.hpp"
64 #include "prims/jvmtiThreadState.hpp"
65 #include "runtime/arguments.hpp"
66 #include "runtime/fieldDescriptor.inline.hpp"
67 #include "runtime/handles.inline.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/os.hpp"
70 #include "runtime/perfData.hpp"
71 #include "runtime/reflection.hpp"
72 #include "runtime/safepointVerifiers.hpp"
73 #include "runtime/signature.hpp"
74 #include "runtime/timer.hpp"
75 #include "services/classLoadingService.hpp"
76 #include "services/threadService.hpp"
77 #include "utilities/align.hpp"
78 #include "utilities/bitMap.inline.hpp"
79 #include "utilities/copy.hpp"
80 #include "utilities/formatBuffer.hpp"
81 #include "utilities/exceptions.hpp"
82 #include "utilities/globalDefinitions.hpp"
83 #include "utilities/growableArray.hpp"
84 #include "utilities/macros.hpp"
85 #include "utilities/ostream.hpp"
86 #include "utilities/resourceHash.hpp"
87 #include "utilities/stringUtils.hpp"
88 #include "utilities/utf8.hpp"
89 #if INCLUDE_CDS
90 #include "classfile/systemDictionaryShared.hpp"
91 #endif
92 #if INCLUDE_JFR
93 #include "jfr/support/jfrTraceIdExtension.hpp"
94 #endif
95
96 // We generally try to create the oops directly when parsing, rather than
97 // allocating temporary data structures and copying the bytes twice. A
98 // temporary area is only needed when parsing utf8 entries in the constant
99 // pool and when parsing line number tables.
100
101 // We add assert in debug mode when class format is not checked.
102
103 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
104 #define JAVA_MIN_SUPPORTED_VERSION 45
105 #define JAVA_PREVIEW_MINOR_VERSION 65535
106
107 // Used for two backward compatibility reasons:
130 #define JAVA_12_VERSION 56
131
132 #define JAVA_13_VERSION 57
133
134 #define JAVA_14_VERSION 58
135
136 #define JAVA_15_VERSION 59
137
138 #define JAVA_16_VERSION 60
139
140 #define JAVA_17_VERSION 61
141
142 #define JAVA_18_VERSION 62
143
144 #define JAVA_19_VERSION 63
145
146 #define JAVA_20_VERSION 64
147
148 #define JAVA_21_VERSION 65
149
150 #define CONSTANT_CLASS_DESCRIPTORS 65
151
152 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
153 assert((bad_constant == JVM_CONSTANT_Module ||
154 bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
155 "Unexpected bad constant pool entry");
156 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
157 }
158
159 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
160 ConstantPool* cp,
161 const int length,
162 TRAPS) {
163 assert(stream != NULL, "invariant");
164 assert(cp != NULL, "invariant");
165
166 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
167 // this function (_current can be allocated in a register, with scalar
168 // replacement of aggregates). The _current pointer is copied back to
169 // stream() when this function returns. DON'T call another method within
170 // this method that uses stream().
171 const ClassFileStream cfs1 = *stream;
172 const ClassFileStream* const cfs = &cfs1;
173
174 debug_only(const u1* const old_current = stream->current();)
175
176 // Used for batching symbol allocations.
177 const char* names[SymbolTable::symbol_alloc_batch_size];
178 int lengths[SymbolTable::symbol_alloc_batch_size];
179 int indices[SymbolTable::symbol_alloc_batch_size];
180 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
181 int names_count = 0;
182
183 // parsing Index 0 is unused
184 for (int index = 1; index < length; index++) {
185 // Each of the following case guarantees one more byte in the stream
186 // for the following tag or the access_flags following constant pool,
187 // so we don't need bounds-check for reading tag.
188 const u1 tag = cfs->get_u1_fast();
189 switch (tag) {
190 case JVM_CONSTANT_Class: {
191 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
192 const u2 name_index = cfs->get_u2_fast();
193 cp->klass_index_at_put(index, name_index);
194 break;
195 }
196 case JVM_CONSTANT_Fieldref: {
197 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
198 const u2 class_index = cfs->get_u2_fast();
199 const u2 name_and_type_index = cfs->get_u2_fast();
200 cp->field_at_put(index, class_index, name_and_type_index);
201 break;
202 }
203 case JVM_CONSTANT_Methodref: {
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->method_at_put(index, class_index, name_and_type_index);
208 break;
209 }
210 case JVM_CONSTANT_InterfaceMethodref: {
491 check_property(valid_symbol_at(name_ref_index),
492 "Invalid constant pool index %u in class file %s",
493 name_ref_index, CHECK);
494 check_property(valid_symbol_at(signature_ref_index),
495 "Invalid constant pool index %u in class file %s",
496 signature_ref_index, CHECK);
497 break;
498 }
499 case JVM_CONSTANT_Utf8:
500 break;
501 case JVM_CONSTANT_UnresolvedClass: // fall-through
502 case JVM_CONSTANT_UnresolvedClassInError: {
503 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
504 break;
505 }
506 case JVM_CONSTANT_ClassIndex: {
507 const int class_index = cp->klass_index_at(index);
508 check_property(valid_symbol_at(class_index),
509 "Invalid constant pool index %u in class file %s",
510 class_index, CHECK);
511
512 Symbol* const name = cp->symbol_at(class_index);
513 const unsigned int name_len = name->utf8_length();
514 if (name->is_Q_signature()) {
515 cp->unresolved_qdescriptor_at_put(index, class_index, num_klasses++);
516 } else {
517 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
518 }
519 break;
520 }
521 case JVM_CONSTANT_StringIndex: {
522 const int string_index = cp->string_index_at(index);
523 check_property(valid_symbol_at(string_index),
524 "Invalid constant pool index %u in class file %s",
525 string_index, CHECK);
526 Symbol* const sym = cp->symbol_at(string_index);
527 cp->unresolved_string_at_put(index, sym);
528 break;
529 }
530 case JVM_CONSTANT_MethodHandle: {
531 const int ref_index = cp->method_handle_index_at(index);
532 check_property(valid_cp_range(ref_index, length),
533 "Invalid constant pool index %u in class file %s",
534 ref_index, CHECK);
535 const constantTag tag = cp->tag_at(ref_index);
536 const int ref_kind = cp->method_handle_ref_kind_at(index);
537
538 switch (ref_kind) {
698 cp->signature_ref_index_at(name_and_type_ref_index);
699 const Symbol* const name = cp->symbol_at(name_ref_index);
700 const Symbol* const signature = cp->symbol_at(signature_ref_index);
701 if (tag == JVM_CONSTANT_Fieldref) {
702 if (_need_verify) {
703 // Field name and signature are verified above, when iterating NameAndType_info.
704 // Need only to be sure signature is non-zero length and the right type.
705 if (Signature::is_method(signature)) {
706 throwIllegalSignature("Field", name, signature, CHECK);
707 }
708 }
709 } else {
710 if (_need_verify) {
711 // Method name and signature are individually verified above, when iterating
712 // NameAndType_info. Need to check here that signature is non-zero length and
713 // the right type.
714 if (!Signature::is_method(signature)) {
715 throwIllegalSignature("Method", name, signature, CHECK);
716 }
717 }
718 // If a class method name begins with '<', it must be "<init>" and have void signature,
719 // or if it is an inline type, <vnew> with return.
720 const unsigned int name_len = name->utf8_length();
721 if (tag == JVM_CONSTANT_Methodref && name_len != 0 &&
722 name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
723 if (name != vmSymbols::object_initializer_name() &&
724 name != vmSymbols::inline_factory_name()) {
725 classfile_parse_error(
726 "Bad method name at constant pool index %u in class file %s",
727 name_ref_index, THREAD);
728 return;
729 } else if (!Signature::is_void_method(signature)) {
730 // if return type is non-void then it must be an inline type
731 if (name == vmSymbols::object_initializer_name() ||
732 !EnableValhalla || !supports_inline_types() ||
733 !signature->ends_with(JVM_SIGNATURE_ENDCLASS)) {
734 throwIllegalSignature("Method", name, signature, CHECK);
735 }
736 }
737 }
738 }
739 break;
740 }
741 case JVM_CONSTANT_MethodHandle: {
742 const int ref_index = cp->method_handle_index_at(index);
743 const int ref_kind = cp->method_handle_ref_kind_at(index);
744 switch (ref_kind) {
745 case JVM_REF_invokeVirtual:
746 case JVM_REF_invokeStatic:
747 case JVM_REF_invokeSpecial:
748 case JVM_REF_newInvokeSpecial: {
749 const int name_and_type_ref_index =
750 cp->name_and_type_ref_index_at(ref_index);
751 const int name_ref_index =
752 cp->name_ref_index_at(name_and_type_ref_index);
753 const Symbol* const name = cp->symbol_at(name_ref_index);
754
755 if (EnableValhalla && supports_inline_types() && name == vmSymbols::inline_factory_name()) { // <vnew>
756 // <vnew> factory methods must be non-void return and invokeStatic.
757 const int signature_ref_index =
758 cp->signature_ref_index_at(name_and_type_ref_index);
759 const Symbol* const signature = cp->symbol_at(signature_ref_index);
760 if (signature->is_void_method_signature() || ref_kind != JVM_REF_invokeStatic) {
761 classfile_parse_error(
762 "Bad factory method name at constant pool index %u in class file %s",
763 name_ref_index, CHECK);
764 }
765 } else if (name != vmSymbols::object_initializer_name()) { // !<init>
766 if (ref_kind == JVM_REF_newInvokeSpecial) {
767 classfile_parse_error(
768 "Bad constructor name at constant pool index %u in class file %s",
769 name_ref_index, THREAD);
770 return;
771 }
772 } else { // <init>
773 // The allowed invocation mode of <init> depends on its signature.
774 // This test corresponds to verify_invoke_instructions in the verifier.
775 const int signature_ref_index =
776 cp->signature_ref_index_at(name_and_type_ref_index);
777 const Symbol* const signature = cp->symbol_at(signature_ref_index);
778 if (signature->is_void_method_signature()
779 && ref_kind == JVM_REF_newInvokeSpecial) {
780 // OK, could be a constructor call
781 } else {
782 classfile_parse_error(
783 "Bad method name at constant pool index %u in class file %s",
784 name_ref_index, THREAD);
785 return;
786 }
787 }
788 break;
789 }
790 // Other ref_kinds are already fully checked in previous pass.
791 } // switch(ref_kind)
792 break;
793 }
794 case JVM_CONSTANT_MethodType: {
795 const Symbol* const no_name = vmSymbols::type_name(); // place holder
796 const Symbol* const signature = cp->method_type_signature_at(index);
797 verify_legal_method_signature(no_name, signature, CHECK);
798 break;
799 }
800 case JVM_CONSTANT_Utf8: {
801 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
838 NameSigHash* entry = table[index];
839 while (entry != NULL) {
840 if (entry->_name == name && entry->_sig == sig) {
841 return false;
842 }
843 entry = entry->_next;
844 }
845
846 // No duplicate is found, allocate a new entry and fill it.
847 entry = new NameSigHash();
848 entry->_name = name;
849 entry->_sig = sig;
850
851 // Insert into hash table
852 entry->_next = table[index];
853 table[index] = entry;
854
855 return true;
856 }
857
858 static void check_identity_and_value_modifiers(ClassFileParser* current, const InstanceKlass* super_type, TRAPS) {
859 assert(super_type != NULL,"Method doesn't support null super type");
860 if (super_type->carries_identity_modifier()) {
861 if (current->carries_value_modifier()) {
862 ResourceMark rm(THREAD);
863 Exceptions::fthrow(
864 THREAD_AND_LOCATION,
865 vmSymbols::java_lang_IncompatibleClassChangeError(),
866 "Value type %s has an identity type as supertype",
867 current->class_name()->as_klass_external_name());
868 return;
869 }
870 current->set_carries_identity_modifier();
871 }
872 if (super_type->carries_value_modifier()) {
873 if (current->carries_identity_modifier()) {
874 ResourceMark rm(THREAD);
875 Exceptions::fthrow(
876 THREAD_AND_LOCATION,
877 vmSymbols::java_lang_IncompatibleClassChangeError(),
878 "Identity type %s has a value type as supertype",
879 current->class_name()->as_klass_external_name());
880 return;
881 }
882 current->set_carries_value_modifier();
883 }
884 }
885
886 void ClassFileParser::parse_interfaces(const ClassFileStream* stream,
887 int itfs_len,
888 ConstantPool* cp,
889 bool* const has_nonstatic_concrete_methods,
890 // FIXME: lots of these functions
891 // declare their parameters as const,
892 // which adds only noise to the code.
893 // Remove the spurious const modifiers.
894 // Many are of the form "const int x"
895 // or "T* const x".
896 bool* const is_declared_atomic,
897 TRAPS) {
898 assert(stream != NULL, "invariant");
899 assert(cp != NULL, "invariant");
900 assert(has_nonstatic_concrete_methods != NULL, "invariant");
901
902 if (itfs_len == 0) {
903 _local_interfaces = Universe::the_empty_instance_klass_array();
904
905 } else {
906 assert(itfs_len > 0, "only called for len>0");
907 _local_interface_indexes = new GrowableArray<u2>(itfs_len);
908 int index = 0;
909 for (index = 0; index < itfs_len; index++) {
910 const u2 interface_index = stream->get_u2(CHECK);
911 check_property(
912 valid_klass_reference_at(interface_index),
913 "Interface name has bad constant pool index %u in class file %s",
914 interface_index, CHECK);
915 _local_interface_indexes->at_put_grow(index, interface_index);
916 }
917
918 if (!_need_verify || itfs_len <= 1) {
919 return;
920 }
921
922 // Check if there's any duplicates in interfaces
923 ResourceMark rm(THREAD);
924 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
925 NameSigHash*,
926 HASH_ROW_SIZE);
927 initialize_hashtable(interface_names);
928 bool dup = false;
929 const Symbol* name = NULL;
930 {
931 debug_only(NoSafepointVerifier nsv;)
932 for (index = 0; index < itfs_len; index++) {
933 name = cp->klass_name_at(_local_interface_indexes->at(index));
934 // If no duplicates, add (name, NULL) in hashtable interface_names.
935 if (!put_after_lookup(name, NULL, interface_names)) {
936 dup = true;
937 break;
938 }
939 }
940 }
941 if (dup) {
942 classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
943 name->as_C_string(), THREAD);
944 }
945 }
946 }
947
948 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp,
949 int constantvalue_index,
950 int signature_index,
951 TRAPS) const {
952 // Make sure the constant pool entry is of a type appropriate to this field
953 guarantee_property(
1399 CHECK);
1400 parsed_annotations->set_field_annotations(a);
1401 a = assemble_annotations(runtime_visible_type_annotations,
1402 runtime_visible_type_annotations_length,
1403 runtime_invisible_type_annotations,
1404 runtime_invisible_type_annotations_length,
1405 CHECK);
1406 parsed_annotations->set_field_type_annotations(a);
1407 return;
1408 }
1409
1410
1411 // Field allocation types. Used for computing field offsets.
1412
1413 enum FieldAllocationType {
1414 STATIC_OOP, // Oops
1415 STATIC_BYTE, // Boolean, Byte, char
1416 STATIC_SHORT, // shorts
1417 STATIC_WORD, // ints
1418 STATIC_DOUBLE, // aligned long or double
1419 STATIC_INLINE, // inline type field
1420 NONSTATIC_OOP,
1421 NONSTATIC_BYTE,
1422 NONSTATIC_SHORT,
1423 NONSTATIC_WORD,
1424 NONSTATIC_DOUBLE,
1425 NONSTATIC_INLINE,
1426 MAX_FIELD_ALLOCATION_TYPE,
1427 BAD_ALLOCATION_TYPE = -1
1428 };
1429
1430 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1431 BAD_ALLOCATION_TYPE, // 0
1432 BAD_ALLOCATION_TYPE, // 1
1433 BAD_ALLOCATION_TYPE, // 2
1434 BAD_ALLOCATION_TYPE, // 3
1435 NONSTATIC_BYTE , // T_BOOLEAN = 4,
1436 NONSTATIC_SHORT, // T_CHAR = 5,
1437 NONSTATIC_WORD, // T_FLOAT = 6,
1438 NONSTATIC_DOUBLE, // T_DOUBLE = 7,
1439 NONSTATIC_BYTE, // T_BYTE = 8,
1440 NONSTATIC_SHORT, // T_SHORT = 9,
1441 NONSTATIC_WORD, // T_INT = 10,
1442 NONSTATIC_DOUBLE, // T_LONG = 11,
1443 NONSTATIC_OOP, // T_OBJECT = 12,
1444 NONSTATIC_OOP, // T_ARRAY = 13,
1445 NONSTATIC_OOP, // T_PRIMITIVE_OBJECT = 14,
1446 BAD_ALLOCATION_TYPE, // T_VOID = 15,
1447 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16,
1448 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17,
1449 BAD_ALLOCATION_TYPE, // T_METADATA = 18,
1450 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1451 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20,
1452 BAD_ALLOCATION_TYPE, // 0
1453 BAD_ALLOCATION_TYPE, // 1
1454 BAD_ALLOCATION_TYPE, // 2
1455 BAD_ALLOCATION_TYPE, // 3
1456 STATIC_BYTE , // T_BOOLEAN = 4,
1457 STATIC_SHORT, // T_CHAR = 5,
1458 STATIC_WORD, // T_FLOAT = 6,
1459 STATIC_DOUBLE, // T_DOUBLE = 7,
1460 STATIC_BYTE, // T_BYTE = 8,
1461 STATIC_SHORT, // T_SHORT = 9,
1462 STATIC_WORD, // T_INT = 10,
1463 STATIC_DOUBLE, // T_LONG = 11,
1464 STATIC_OOP, // T_OBJECT = 12,
1465 STATIC_OOP, // T_ARRAY = 13,
1466 STATIC_OOP, // T_PRIMITIVE_OBJECT = 14,
1467 BAD_ALLOCATION_TYPE, // T_VOID = 15,
1468 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16,
1469 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17,
1470 BAD_ALLOCATION_TYPE, // T_METADATA = 18,
1471 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1472 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20
1473 };
1474
1475 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type, bool is_inline_type) {
1476 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1477 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1478 assert(result != BAD_ALLOCATION_TYPE, "bad type");
1479 if (is_inline_type) {
1480 result = is_static ? STATIC_INLINE : NONSTATIC_INLINE;
1481 }
1482 return result;
1483 }
1484
1485 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1486 public:
1487 u2 count[MAX_FIELD_ALLOCATION_TYPE];
1488
1489 FieldAllocationCount() {
1490 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1491 count[i] = 0;
1492 }
1493 }
1494
1495 void update(bool is_static, BasicType type, bool is_inline_type) {
1496 FieldAllocationType atype = basic_type_to_atype(is_static, type, is_inline_type);
1497 if (atype != BAD_ALLOCATION_TYPE) {
1498 // Make sure there is no overflow with injected fields.
1499 assert(count[atype] < 0xFFFF, "More than 65535 fields");
1500 count[atype]++;
1501 }
1502 }
1503 };
1504
1505 // Side-effects: populates the _fields, _fields_annotations,
1506 // _fields_type_annotations fields
1507 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1508 AccessFlags class_access_flags,
1509 FieldAllocationCount* const fac,
1510 ConstantPool* cp,
1511 const int cp_size,
1512 u2* const java_fields_count_ptr,
1513 TRAPS) {
1514
1515 assert(cfs != NULL, "invariant");
1516 assert(fac != NULL, "invariant");
1517 assert(cp != NULL, "invariant");
1518 assert(java_fields_count_ptr != NULL, "invariant");
1519
1520 assert(NULL == _fields, "invariant");
1521 assert(NULL == _fields_annotations, "invariant");
1522 assert(NULL == _fields_type_annotations, "invariant");
1523
1524 bool is_inline_type = class_access_flags.is_value_class() && !class_access_flags.is_abstract();
1525 cfs->guarantee_more(2, CHECK); // length
1526 const u2 length = cfs->get_u2_fast();
1527 *java_fields_count_ptr = length;
1528
1529 int num_injected = 0;
1530 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1531 &num_injected);
1532
1533 // two more slots are required for inline classes:
1534 // one for the static field with a reference to the pre-allocated default value
1535 // one for the field the JVM injects when detecting an empty inline class
1536 const int total_fields = length + num_injected + (is_inline_type ? 2 : 0);
1537
1538 // The field array starts with tuples of shorts
1539 // [access, name index, sig index, initial value index, byte offset].
1540 // A generic signature slot only exists for field with generic
1541 // signature attribute. And the access flag is set with
1542 // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic
1543 // signature slots are at the end of the field array and after all
1544 // other fields data.
1545 //
1546 // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
1547 // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
1548 // ...
1549 // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
1550 // [generic signature index]
1551 // [generic signature index]
1552 // ...
1553 //
1554 // Allocate a temporary resource array for field data. For each field,
1555 // a slot is reserved in the temporary array for the generic signature
1556 // index. After parsing all fields, the data are copied to a permanent
1557 // array and any unused slots will be discarded.
1558 ResourceMark rm(THREAD);
1559 u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
1560 u2,
1561 total_fields * (FieldInfo::field_slots + 1));
1562
1563 // The generic signature slots start after all other fields' data.
1564 int generic_signature_slot = total_fields * FieldInfo::field_slots;
1565 int num_generic_signature = 0;
1566 int instance_fields_count = 0;
1567 for (int n = 0; n < length; n++) {
1568 // access_flags, name_index, descriptor_index, attributes_count
1569 cfs->guarantee_more(8, CHECK);
1570
1571 jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS;
1572
1573 const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1574 verify_legal_field_modifiers(flags, class_access_flags, CHECK);
1575 AccessFlags access_flags;
1576 access_flags.set_flags(flags);
1577
1578 const u2 name_index = cfs->get_u2_fast();
1579 check_property(valid_symbol_at(name_index),
1580 "Invalid constant pool index %u for field name in class file %s",
1581 name_index, CHECK);
1582 const Symbol* const name = cp->symbol_at(name_index);
1583 verify_legal_field_name(name, CHECK);
1584
1585 const u2 signature_index = cfs->get_u2_fast();
1586 check_property(valid_symbol_at(signature_index),
1587 "Invalid constant pool index %u for field signature in class file %s",
1588 signature_index, CHECK);
1589 const Symbol* const sig = cp->symbol_at(signature_index);
1590 verify_legal_field_signature(name, sig, CHECK);
1591 if (!access_flags.is_static()) instance_fields_count++;
1592
1593 u2 constantvalue_index = 0;
1594 bool is_synthetic = false;
1595 u2 generic_signature_index = 0;
1596 const bool is_static = access_flags.is_static();
1597 FieldAnnotationCollector parsed_annotations(_loader_data);
1598
1599 const u2 attributes_count = cfs->get_u2_fast();
1600 if (attributes_count > 0) {
1601 parse_field_attributes(cfs,
1602 attributes_count,
1603 is_static,
1604 signature_index,
1605 &constantvalue_index,
1606 &is_synthetic,
1607 &generic_signature_index,
1608 &parsed_annotations,
1609 CHECK);
1610
1611 if (parsed_annotations.field_annotations() != NULL) {
1631
1632 if (is_synthetic) {
1633 access_flags.set_is_synthetic();
1634 }
1635 if (generic_signature_index != 0) {
1636 access_flags.set_field_has_generic_signature();
1637 fa[generic_signature_slot] = generic_signature_index;
1638 generic_signature_slot ++;
1639 num_generic_signature ++;
1640 }
1641 }
1642
1643 FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1644 field->initialize(access_flags.as_short(),
1645 name_index,
1646 signature_index,
1647 constantvalue_index);
1648 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1649
1650 // Update FieldAllocationCount for this kind of field
1651 fac->update(is_static, type, type == T_PRIMITIVE_OBJECT);
1652
1653 // After field is initialized with type, we can augment it with aux info
1654 if (parsed_annotations.has_any_annotations()) {
1655 parsed_annotations.apply_to(field);
1656 if (field->is_contended()) {
1657 _has_contended_fields = true;
1658 }
1659 }
1660 }
1661
1662 int index = length;
1663 if (num_injected != 0) {
1664 for (int n = 0; n < num_injected; n++) {
1665 // Check for duplicates
1666 if (injected[n].may_be_java) {
1667 const Symbol* const name = injected[n].name();
1668 const Symbol* const signature = injected[n].signature();
1669 bool duplicate = false;
1670 for (int i = 0; i < length; i++) {
1671 const FieldInfo* const f = FieldInfo::from_field_array(fa, i);
1675 duplicate = true;
1676 break;
1677 }
1678 }
1679 if (duplicate) {
1680 // These will be removed from the field array at the end
1681 continue;
1682 }
1683 }
1684
1685 // Injected field
1686 FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1687 field->initialize((u2)JVM_ACC_FIELD_INTERNAL,
1688 (u2)(injected[n].name_index),
1689 (u2)(injected[n].signature_index),
1690 0);
1691
1692 const BasicType type = Signature::basic_type(injected[n].signature());
1693
1694 // Update FieldAllocationCount for this kind of field
1695 fac->update(false, type, false);
1696 index++;
1697 }
1698 }
1699
1700 if (is_inline_type) {
1701 FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1702 field->initialize(JVM_ACC_FIELD_INTERNAL | JVM_ACC_STATIC,
1703 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(default_value_name)),
1704 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(object_signature)),
1705 0);
1706 const BasicType type = Signature::basic_type(vmSymbols::object_signature());
1707 fac->update(true, type, false);
1708 index++;
1709 }
1710
1711 if (is_inline_type && instance_fields_count == 0) {
1712 _is_empty_inline_type = true;
1713 FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1714 field->initialize(JVM_ACC_FIELD_INTERNAL,
1715 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(empty_marker_name)),
1716 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(byte_signature)),
1717 0);
1718 const BasicType type = Signature::basic_type(vmSymbols::byte_signature());
1719 fac->update(false, type, false);
1720 index++;
1721 }
1722
1723 if (instance_fields_count > 0) {
1724 _has_nonstatic_fields = true;
1725 }
1726
1727 assert(NULL == _fields, "invariant");
1728
1729 _fields =
1730 MetadataFactory::new_array<u2>(_loader_data,
1731 index * FieldInfo::field_slots + num_generic_signature,
1732 CHECK);
1733 // Sometimes injected fields already exist in the Java source so
1734 // the fields array could be too long. In that case the
1735 // fields array is trimmed. Also unused slots that were reserved
1736 // for generic signature indexes are discarded.
1737 {
1738 int i = 0;
1739 for (; i < index * FieldInfo::field_slots; i++) {
1740 _fields->at_put(i, fa[i]);
1741 }
1742 for (int j = total_fields * FieldInfo::field_slots;
1743 j < generic_signature_slot; j++) {
1744 _fields->at_put(i++, fa[j]);
1745 }
1746 assert(_fields->length() == i, "");
2024 "Exception name has bad type at constant pool %u in class file %s",
2025 checked_exception, CHECK_NULL);
2026 }
2027 }
2028 // check exceptions attribute length
2029 if (_need_verify) {
2030 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
2031 sizeof(u2) * size),
2032 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
2033 }
2034 return checked_exceptions_start;
2035 }
2036
2037 void ClassFileParser::throwIllegalSignature(const char* type,
2038 const Symbol* name,
2039 const Symbol* sig,
2040 TRAPS) const {
2041 assert(name != NULL, "invariant");
2042 assert(sig != NULL, "invariant");
2043
2044 const char* class_note = "";
2045 if (is_inline_type() && name == vmSymbols::object_initializer_name()) {
2046 class_note = " (an inline class)";
2047 }
2048
2049 ResourceMark rm(THREAD);
2050 Exceptions::fthrow(THREAD_AND_LOCATION,
2051 vmSymbols::java_lang_ClassFormatError(),
2052 "%s \"%s\" in class %s%s has illegal signature \"%s\"", type,
2053 name->as_C_string(), _class_name->as_C_string(), class_note, sig->as_C_string());
2054 }
2055
2056 AnnotationCollector::ID
2057 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
2058 const Symbol* name,
2059 const bool can_access_vm_annotations) {
2060 const vmSymbolID sid = vmSymbols::find_sid(name);
2061 // Privileged code can use all annotations. Other code silently drops some.
2062 const bool privileged = loader_data->is_boot_class_loader_data() ||
2063 loader_data->is_platform_class_loader_data() ||
2064 can_access_vm_annotations;
2065 switch (sid) {
2066 case VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
2067 if (_location != _in_method) break; // only allow for methods
2068 if (!privileged) break; // only allow in privileged code
2069 return _method_CallerSensitive;
2070 }
2071 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
2072 if (_location != _in_method) break; // only allow for methods
2073 if (!privileged) break; // only allow in privileged code
2336 runtime_visible_type_annotations_length,
2337 runtime_invisible_type_annotations,
2338 runtime_invisible_type_annotations_length,
2339 CHECK);
2340 cm->set_type_annotations(a);
2341 }
2342 }
2343
2344
2345 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2346 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2347 // Method* to save footprint, so we only know the size of the resulting Method* when the
2348 // entire method attribute is parsed.
2349 //
2350 // The promoted_flags parameter is used to pass relevant access_flags
2351 // from the method back up to the containing klass. These flag values
2352 // are added to klass's access_flags.
2353
2354 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2355 bool is_interface,
2356 bool is_value_class,
2357 bool is_abstract_class,
2358 const ConstantPool* cp,
2359 AccessFlags* const promoted_flags,
2360 TRAPS) {
2361 assert(cfs != NULL, "invariant");
2362 assert(cp != NULL, "invariant");
2363 assert(promoted_flags != NULL, "invariant");
2364
2365 ResourceMark rm(THREAD);
2366 // Parse fixed parts:
2367 // access_flags, name_index, descriptor_index, attributes_count
2368 cfs->guarantee_more(8, CHECK_NULL);
2369
2370 int flags = cfs->get_u2_fast();
2371 const u2 name_index = cfs->get_u2_fast();
2372 const int cp_size = cp->length();
2373 check_property(
2374 valid_symbol_at(name_index),
2375 "Illegal constant pool index %u for method name in class file %s",
2376 name_index, CHECK_NULL);
2377 const Symbol* const name = cp->symbol_at(name_index);
2379
2380 const u2 signature_index = cfs->get_u2_fast();
2381 guarantee_property(
2382 valid_symbol_at(signature_index),
2383 "Illegal constant pool index %u for method signature in class file %s",
2384 signature_index, CHECK_NULL);
2385 const Symbol* const signature = cp->symbol_at(signature_index);
2386
2387 if (name == vmSymbols::class_initializer_name()) {
2388 // We ignore the other access flags for a valid class initializer.
2389 // (JVM Spec 2nd ed., chapter 4.6)
2390 if (_major_version < 51) { // backward compatibility
2391 flags = JVM_ACC_STATIC;
2392 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2393 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2394 } else {
2395 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2396 return NULL;
2397 }
2398 } else {
2399 verify_legal_method_modifiers(flags, access_flags() , name, CHECK_NULL);
2400 }
2401
2402 if (EnableValhalla && supports_inline_types() && name == vmSymbols::inline_factory_name()) {
2403 if (is_interface) {
2404 classfile_parse_error("Interface cannot have a method named <vnew>, class file %s", CHECK_NULL);
2405 } else if (!is_value_class) {
2406 classfile_parse_error("Identity class cannot have a method <vnew>, class file %s", CHECK_NULL);
2407 } else if (signature->is_void_method_signature()) {
2408 classfile_parse_error("Factory method <vnew> must have a non-void return type, class file %s", CHECK_NULL);
2409 } else { // also OK, a static factory, as long as the return value is good
2410 bool ok = false;
2411 SignatureStream ss((Symbol*) signature, true);
2412 while (!ss.at_return_type()) ss.next();
2413 if (ss.is_reference()) {
2414 Symbol* ret = ss.as_symbol();
2415 const Symbol* required = class_name();
2416 if (is_hidden()) {
2417 // The original class name for hidden classes changed.
2418 /// So using the original name in the return type is no longer valid.
2419 required = vmSymbols::java_lang_Object();
2420 }
2421 ok = (ret == required);
2422 }
2423 if (!ok) {
2424 throwIllegalSignature("Method", name, signature, CHECK_0);
2425 }
2426 // factory method, with a non-void return. No other
2427 // definition of <vnew> is possible.
2428 //
2429 // The verifier (in verify_invoke_instructions) will inspect the
2430 // signature of any attempt to invoke <vnew>, and ensure that it
2431 // returns non-void.
2432 }
2433 }
2434
2435 if (name == vmSymbols::object_initializer_name()) {
2436 if (is_interface) {
2437 classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL);
2438 } else if ((!is_value_class || is_abstract_class) && signature->is_void_method_signature()) {
2439 // OK, a constructor
2440 } else {
2441 // not OK, so throw the same error as in verify_legal_method_signature.
2442 throwIllegalSignature("Method", name, signature, CHECK_0);
2443 }
2444 // A declared <init> method must always be a non-static
2445 // object constructor, with a void return.
2446 //
2447 // The verifier (in verify_invoke_instructions) will inspect the
2448 // signature of any attempt to invoke <init>, and ensure that it
2449 // returns void.
2450 }
2451
2452 if (EnableValhalla) {
2453 if (((flags & JVM_ACC_SYNCHRONIZED) == JVM_ACC_SYNCHRONIZED)
2454 && ((flags & JVM_ACC_STATIC) == 0 )
2455 && !carries_identity_modifier()) {
2456 classfile_parse_error("Invalid synchronized method in non-identity class %s", THREAD);
2457 return NULL;
2458 }
2459 }
2460
2461 int args_size = -1; // only used when _need_verify is true
2462 if (_need_verify) {
2463 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2464 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2465 verify_legal_method_signature(name, signature, CHECK_NULL);
2466 if (args_size > MAX_ARGS_SIZE) {
2467 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2468 return NULL;
2469 }
2470 }
2471
2472 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2473
2474 // Default values for code and exceptions attribute elements
2475 u2 max_stack = 0;
2476 u2 max_locals = 0;
2477 u4 code_length = 0;
2478 const u1* code_start = 0;
3008 _has_finalizer = true;
3009 }
3010 }
3011 if (name == vmSymbols::object_initializer_name() &&
3012 signature == vmSymbols::void_method_signature() &&
3013 m->is_vanilla_constructor()) {
3014 _has_vanilla_constructor = true;
3015 }
3016
3017 NOT_PRODUCT(m->verify());
3018 return m;
3019 }
3020
3021
3022 // The promoted_flags parameter is used to pass relevant access_flags
3023 // from the methods back up to the containing klass. These flag values
3024 // are added to klass's access_flags.
3025 // Side-effects: populates the _methods field in the parser
3026 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
3027 bool is_interface,
3028 bool is_value_class,
3029 bool is_abstract_type,
3030 AccessFlags* promoted_flags,
3031 bool* has_final_method,
3032 bool* declares_nonstatic_concrete_methods,
3033 TRAPS) {
3034 assert(cfs != NULL, "invariant");
3035 assert(promoted_flags != NULL, "invariant");
3036 assert(has_final_method != NULL, "invariant");
3037 assert(declares_nonstatic_concrete_methods != NULL, "invariant");
3038
3039 assert(NULL == _methods, "invariant");
3040
3041 cfs->guarantee_more(2, CHECK); // length
3042 const u2 length = cfs->get_u2_fast();
3043 if (length == 0) {
3044 _methods = Universe::the_empty_method_array();
3045 } else {
3046 _methods = MetadataFactory::new_array<Method*>(_loader_data,
3047 length,
3048 NULL,
3049 CHECK);
3050
3051 for (int index = 0; index < length; index++) {
3052 Method* method = parse_method(cfs,
3053 is_interface,
3054 is_value_class,
3055 is_abstract_type,
3056 _cp,
3057 promoted_flags,
3058 CHECK);
3059
3060 if (method->is_final()) {
3061 *has_final_method = true;
3062 }
3063 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
3064 // used for interface initialization, and default method inheritance analysis
3065 if (is_interface && !(*declares_nonstatic_concrete_methods)
3066 && !method->is_abstract() && !method->is_static()) {
3067 *declares_nonstatic_concrete_methods = true;
3068 }
3069 _methods->at_put(index, method);
3070 }
3071
3072 if (_need_verify && length > 1) {
3073 // Check duplicated methods
3074 ResourceMark rm(THREAD);
3075 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
3312 valid_klass_reference_at(outer_class_info_index),
3313 "outer_class_info_index %u has bad constant type in class file %s",
3314 outer_class_info_index, CHECK_0);
3315
3316 if (outer_class_info_index != 0) {
3317 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3318 char* bytes = (char*)outer_class_name->bytes();
3319 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3320 "Outer class is an array class in class file %s", CHECK_0);
3321 }
3322 // Inner class name
3323 const u2 inner_name_index = cfs->get_u2_fast();
3324 check_property(
3325 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3326 "inner_name_index %u has bad constant type in class file %s",
3327 inner_name_index, CHECK_0);
3328 if (_need_verify) {
3329 guarantee_property(inner_class_info_index != outer_class_info_index,
3330 "Class is both outer and inner class in class file %s", CHECK_0);
3331 }
3332
3333 jint recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS;
3334 // JVM_ACC_MODULE is defined in JDK-9 and later.
3335 if (_major_version >= JAVA_9_VERSION) {
3336 recognized_modifiers |= JVM_ACC_MODULE;
3337 }
3338 if (supports_inline_types()) {
3339 recognized_modifiers |= JVM_ACC_PRIMITIVE | JVM_ACC_VALUE | JVM_ACC_IDENTITY;
3340 }
3341
3342 // Access flags
3343 jint flags = cfs->get_u2_fast() & recognized_modifiers;
3344
3345 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3346 // Set abstract bit for old class files for backward compatibility
3347 flags |= JVM_ACC_ABSTRACT;
3348 }
3349
3350 if (EnableValhalla) {
3351 if (!supports_inline_types()) {
3352 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
3353 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3354 if (!is_module && !is_interface) {
3355 flags |= JVM_ACC_IDENTITY;
3356 }
3357 }
3358 }
3359
3360 const char* name = inner_name_index == 0 ? "unnamed" : cp->symbol_at(inner_name_index)->as_utf8();
3361 verify_legal_class_modifiers(flags, name, false, CHECK_0);
3362 AccessFlags inner_access_flags(flags);
3363
3364 inner_classes->at_put(index++, inner_class_info_index);
3365 inner_classes->at_put(index++, outer_class_info_index);
3366 inner_classes->at_put(index++, inner_name_index);
3367 inner_classes->at_put(index++, inner_access_flags.as_short());
3368 }
3369
3370 // Check for circular and duplicate entries.
3371 bool has_circularity = false;
3372 if (_need_verify) {
3373 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3374 if (has_circularity) {
3375 // If circularity check failed then ignore InnerClasses attribute.
3376 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3377 index = 0;
3378 if (parsed_enclosingmethod_attribute) {
3379 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3380 _inner_classes = inner_classes;
3381 } else {
3445 if (length > 0) {
3446 int index = 0;
3447 cfs->guarantee_more(2 * length, CHECK_0);
3448 for (int n = 0; n < length; n++) {
3449 const u2 class_info_index = cfs->get_u2_fast();
3450 check_property(
3451 valid_klass_reference_at(class_info_index),
3452 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3453 class_info_index, CHECK_0);
3454 permitted_subclasses->at_put(index++, class_info_index);
3455 }
3456 assert(index == size, "wrong size");
3457 }
3458
3459 // Restore buffer's current position.
3460 cfs->set_current(current_mark);
3461
3462 return length;
3463 }
3464
3465 u2 ClassFileParser::parse_classfile_preload_attribute(const ClassFileStream* const cfs,
3466 const u1* const preload_attribute_start,
3467 TRAPS) {
3468 const u1* const current_mark = cfs->current();
3469 u2 length = 0;
3470 if (preload_attribute_start != NULL) {
3471 cfs->set_current(preload_attribute_start);
3472 cfs->guarantee_more(2, CHECK_0); // length
3473 length = cfs->get_u2_fast();
3474 }
3475 const int size = length;
3476 Array<u2>* const preload_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3477 _preload_classes = preload_classes;
3478 if (length > 0) {
3479 int index = 0;
3480 cfs->guarantee_more(2 * length, CHECK_0);
3481 for (int n = 0; n < length; n++) {
3482 const u2 class_info_index = cfs->get_u2_fast();
3483 check_property(
3484 valid_klass_reference_at(class_info_index),
3485 "Preload class_info_index %u has bad constant type in class file %s",
3486 class_info_index, CHECK_0);
3487 preload_classes->at_put(index++, class_info_index);
3488 }
3489 assert(index == size, "wrong size");
3490 }
3491
3492 // Restore buffer's current position.
3493 cfs->set_current(current_mark);
3494
3495 return length;
3496 }
3497
3498 // Record {
3499 // u2 attribute_name_index;
3500 // u4 attribute_length;
3501 // u2 components_count;
3502 // component_info components[components_count];
3503 // }
3504 // component_info {
3505 // u2 name_index;
3506 // u2 descriptor_index
3507 // u2 attributes_count;
3508 // attribute_info_attributes[attributes_count];
3509 // }
3510 u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3511 const ConstantPool* cp,
3512 const u1* const record_attribute_start,
3513 TRAPS) {
3514 const u1* const current_mark = cfs->current();
3515 int components_count = 0;
3516 unsigned int calculate_attr_size = 0;
3517 if (record_attribute_start != NULL) {
3762 }
3763 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3764 "Bad length on BootstrapMethods in class file %s",
3765 CHECK);
3766 }
3767
3768 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3769 ConstantPool* cp,
3770 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3771 TRAPS) {
3772 assert(cfs != NULL, "invariant");
3773 assert(cp != NULL, "invariant");
3774 assert(parsed_annotations != NULL, "invariant");
3775
3776 // Set inner classes attribute to default sentinel
3777 _inner_classes = Universe::the_empty_short_array();
3778 // Set nest members attribute to default sentinel
3779 _nest_members = Universe::the_empty_short_array();
3780 // Set _permitted_subclasses attribute to default sentinel
3781 _permitted_subclasses = Universe::the_empty_short_array();
3782 // Set _preload_classes attribute to default sentinel
3783 _preload_classes = Universe::the_empty_short_array();
3784 cfs->guarantee_more(2, CHECK); // attributes_count
3785 u2 attributes_count = cfs->get_u2_fast();
3786 bool parsed_sourcefile_attribute = false;
3787 bool parsed_innerclasses_attribute = false;
3788 bool parsed_nest_members_attribute = false;
3789 bool parsed_permitted_subclasses_attribute = false;
3790 bool parsed_preload_attribute = false;
3791 bool parsed_nest_host_attribute = false;
3792 bool parsed_record_attribute = false;
3793 bool parsed_enclosingmethod_attribute = false;
3794 bool parsed_bootstrap_methods_attribute = false;
3795 const u1* runtime_visible_annotations = NULL;
3796 int runtime_visible_annotations_length = 0;
3797 const u1* runtime_invisible_annotations = NULL;
3798 int runtime_invisible_annotations_length = 0;
3799 const u1* runtime_visible_type_annotations = NULL;
3800 int runtime_visible_type_annotations_length = 0;
3801 const u1* runtime_invisible_type_annotations = NULL;
3802 int runtime_invisible_type_annotations_length = 0;
3803 bool runtime_invisible_type_annotations_exists = false;
3804 bool runtime_invisible_annotations_exists = false;
3805 bool parsed_source_debug_ext_annotations_exist = false;
3806 const u1* inner_classes_attribute_start = NULL;
3807 u4 inner_classes_attribute_length = 0;
3808 u2 enclosing_method_class_index = 0;
3809 u2 enclosing_method_method_index = 0;
3810 const u1* nest_members_attribute_start = NULL;
3811 u4 nest_members_attribute_length = 0;
3812 const u1* record_attribute_start = NULL;
3813 u4 record_attribute_length = 0;
3814 const u1* permitted_subclasses_attribute_start = NULL;
3815 u4 permitted_subclasses_attribute_length = 0;
3816 const u1* preload_attribute_start = NULL;
3817 u4 preload_attribute_length = 0;
3818
3819 // Iterate over attributes
3820 while (attributes_count--) {
3821 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3822 const u2 attribute_name_index = cfs->get_u2_fast();
3823 const u4 attribute_length = cfs->get_u4_fast();
3824 check_property(
3825 valid_symbol_at(attribute_name_index),
3826 "Attribute name has bad constant pool index %u in class file %s",
3827 attribute_name_index, CHECK);
3828 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3829 if (tag == vmSymbols::tag_source_file()) {
3830 // Check for SourceFile tag
3831 if (_need_verify) {
3832 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3833 }
3834 if (parsed_sourcefile_attribute) {
3835 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3836 return;
3837 } else {
4024 return;
4025 }
4026 parsed_record_attribute = true;
4027 record_attribute_start = cfs->current();
4028 record_attribute_length = attribute_length;
4029 } else if (_major_version >= JAVA_17_VERSION) {
4030 if (tag == vmSymbols::tag_permitted_subclasses()) {
4031 if (parsed_permitted_subclasses_attribute) {
4032 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
4033 return;
4034 }
4035 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
4036 if (_access_flags.is_final()) {
4037 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
4038 return;
4039 }
4040 parsed_permitted_subclasses_attribute = true;
4041 permitted_subclasses_attribute_start = cfs->current();
4042 permitted_subclasses_attribute_length = attribute_length;
4043 }
4044 if (EnableValhalla && tag == vmSymbols::tag_preload()) {
4045 if (parsed_preload_attribute) {
4046 classfile_parse_error("Multiple Preload attributes in class file %s", CHECK);
4047 return;
4048 }
4049 parsed_preload_attribute = true;
4050 preload_attribute_start = cfs->current();
4051 preload_attribute_length = attribute_length;
4052 }
4053 }
4054 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
4055 cfs->skip_u1(attribute_length, CHECK);
4056 } else {
4057 // Unknown attribute
4058 cfs->skip_u1(attribute_length, CHECK);
4059 }
4060 } else {
4061 // Unknown attribute
4062 cfs->skip_u1(attribute_length, CHECK);
4063 }
4064 } else {
4065 // Unknown attribute
4066 cfs->skip_u1(attribute_length, CHECK);
4067 }
4068 }
4069 _class_annotations = assemble_annotations(runtime_visible_annotations,
4070 runtime_visible_annotations_length,
4071 runtime_invisible_annotations,
4072 runtime_invisible_annotations_length,
4113 CHECK);
4114 if (_need_verify) {
4115 guarantee_property(record_attribute_length == calculated_attr_length,
4116 "Record attribute has wrong length in class file %s",
4117 CHECK);
4118 }
4119 }
4120
4121 if (parsed_permitted_subclasses_attribute) {
4122 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
4123 cfs,
4124 permitted_subclasses_attribute_start,
4125 CHECK);
4126 if (_need_verify) {
4127 guarantee_property(
4128 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
4129 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
4130 }
4131 }
4132
4133 if (parsed_preload_attribute) {
4134 const u2 num_classes = parse_classfile_preload_attribute(
4135 cfs,
4136 preload_attribute_start,
4137 CHECK);
4138 if (_need_verify) {
4139 guarantee_property(
4140 preload_attribute_length == sizeof(num_classes) + sizeof(u2) * num_classes,
4141 "Wrong Preload attribute length in class file %s", CHECK);
4142 }
4143 }
4144
4145 if (_max_bootstrap_specifier_index >= 0) {
4146 guarantee_property(parsed_bootstrap_methods_attribute,
4147 "Missing BootstrapMethods attribute in class file %s", CHECK);
4148 }
4149 }
4150
4151 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
4152 assert(k != NULL, "invariant");
4153
4154 if (_synthetic_flag)
4155 k->set_is_synthetic();
4156 if (_sourcefile_index != 0) {
4157 k->set_source_file_name_index(_sourcefile_index);
4158 }
4159 if (_generic_signature_index != 0) {
4160 k->set_generic_signature_index(_generic_signature_index);
4161 }
4162 if (_sde_buffer != NULL) {
4163 k->set_source_debug_extension(_sde_buffer, _sde_length);
4164 }
4189 // _combined_annotations so these fields can now be cleared.
4190 _class_annotations = NULL;
4191 _class_type_annotations = NULL;
4192 _fields_annotations = NULL;
4193 _fields_type_annotations = NULL;
4194 }
4195
4196 // Transfer ownership of metadata allocated to the InstanceKlass.
4197 void ClassFileParser::apply_parsed_class_metadata(
4198 InstanceKlass* this_klass,
4199 int java_fields_count) {
4200 assert(this_klass != NULL, "invariant");
4201
4202 _cp->set_pool_holder(this_klass);
4203 this_klass->set_constants(_cp);
4204 this_klass->set_fields(_fields, java_fields_count);
4205 this_klass->set_methods(_methods);
4206 this_klass->set_inner_classes(_inner_classes);
4207 this_klass->set_nest_members(_nest_members);
4208 this_klass->set_nest_host_index(_nest_host);
4209 this_klass->set_preload_classes(_preload_classes);
4210 this_klass->set_annotations(_combined_annotations);
4211 this_klass->set_permitted_subclasses(_permitted_subclasses);
4212 this_klass->set_record_components(_record_components);
4213 // Delay the setting of _local_interfaces and _transitive_interfaces until after
4214 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
4215 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
4216 // its _super. If an OOM occurs while loading the current klass, its _super field
4217 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
4218 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
4219 // dereferences to the deallocated _transitive_interfaces will result in a crash.
4220
4221 // Clear out these fields so they don't get deallocated by the destructor
4222 clear_class_metadata();
4223 }
4224
4225 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
4226 int runtime_visible_annotations_length,
4227 const u1* const runtime_invisible_annotations,
4228 int runtime_invisible_annotations_length,
4229 TRAPS) {
4241 }
4242 if (runtime_invisible_annotations != NULL) {
4243 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
4244 int append = runtime_visible_annotations_length+i;
4245 annotations->at_put(append, runtime_invisible_annotations[i]);
4246 }
4247 }
4248 }
4249 return annotations;
4250 }
4251
4252 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
4253 const int super_class_index,
4254 const bool need_verify,
4255 TRAPS) {
4256 assert(cp != NULL, "invariant");
4257 const InstanceKlass* super_klass = NULL;
4258
4259 if (super_class_index == 0) {
4260 check_property(_class_name == vmSymbols::java_lang_Object(),
4261 "Invalid superclass index 0 in class file %s",
4262 CHECK_NULL);
4263 } else {
4264 check_property(valid_klass_reference_at(super_class_index),
4265 "Invalid superclass index %u in class file %s",
4266 super_class_index,
4267 CHECK_NULL);
4268 // The class name should be legal because it is checked when parsing constant pool.
4269 // However, make sure it is not an array type.
4270 if (cp->tag_at(super_class_index).is_klass()) {
4271 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
4272 }
4273 if (need_verify) {
4274 bool is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
4275 guarantee_property(!is_array,
4276 "Bad superclass name in class file %s", CHECK_NULL);
4277 }
4278 }
4279 return super_klass;
4280 }
4281
4282 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
4283 _max_nonstatic_oop_maps = max_blocks;
4284 _nonstatic_oop_map_count = 0;
4285 if (max_blocks == 0) {
4286 _nonstatic_oop_maps = NULL;
4287 } else {
4288 _nonstatic_oop_maps =
4289 NEW_RESOURCE_ARRAY(OopMapBlock, _max_nonstatic_oop_maps);
4290 memset(_nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
4291 }
4292 }
4293
4294 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
4378 }
4379
4380 void OopMapBlocksBuilder::print_on(outputStream* st) const {
4381 st->print_cr(" OopMapBlocks: %3d /%3d", _nonstatic_oop_map_count, _max_nonstatic_oop_maps);
4382 if (_nonstatic_oop_map_count > 0) {
4383 OopMapBlock* map = _nonstatic_oop_maps;
4384 OopMapBlock* last_map = last_oop_map();
4385 assert(map <= last_map, "Last less than first");
4386 while (map <= last_map) {
4387 st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(),
4388 map->offset() + map->offset_span() - heapOopSize, map->count());
4389 map++;
4390 }
4391 }
4392 }
4393
4394 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
4395 print_on(st);
4396 }
4397
4398 void ClassFileParser::throwInlineTypeLimitation(THREAD_AND_LOCATION_DECL,
4399 const char* msg,
4400 const Symbol* name,
4401 const Symbol* sig) const {
4402
4403 ResourceMark rm(THREAD);
4404 if (name == NULL || sig == NULL) {
4405 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4406 vmSymbols::java_lang_ClassFormatError(),
4407 "class: %s - %s", _class_name->as_C_string(), msg);
4408 }
4409 else {
4410 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4411 vmSymbols::java_lang_ClassFormatError(),
4412 "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(),
4413 _class_name->as_C_string(), msg);
4414 }
4415 }
4416
4417 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4418 assert(ik != NULL, "invariant");
4419
4420 const Klass* const super = ik->super();
4421
4422 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4423 // in which case we don't have to register objects as finalizable
4424 if (!_has_empty_finalizer) {
4425 if (_has_finalizer ||
4426 (super != NULL && super->has_finalizer())) {
4427 ik->set_has_finalizer();
4428 }
4429 }
4430
4431 #ifdef ASSERT
4432 bool f = false;
4433 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4434 vmSymbols::void_method_signature());
4435 if (InstanceKlass::is_finalization_enabled() &&
4436 (m != NULL) && !m->is_empty_method()) {
4437 f = true;
4438 }
4439
4440 // Spec doesn't prevent agent from redefinition of empty finalizer.
4441 // Despite the fact that it's generally bad idea and redefined finalizer
4442 // will not work as expected we shouldn't abort vm in this case
4443 if (!ik->has_redefined_this_or_super()) {
4444 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4445 }
4446 #endif
4447
4448 // Check if this klass supports the java.lang.Cloneable interface
4449 if (vmClasses::Cloneable_klass_loaded()) {
4450 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4451 if (ik->is_inline_klass()) {
4452 JavaThread *THREAD = JavaThread::current();
4453 throwInlineTypeLimitation(THREAD_AND_LOCATION, "Inline Types do not support Cloneable");
4454 return;
4455 }
4456 ik->set_is_cloneable();
4457 }
4458 }
4459
4460 // Check if this klass has a vanilla default constructor
4461 if (super == NULL) {
4462 // java.lang.Object has empty default constructor
4463 ik->set_has_vanilla_constructor();
4464 } else {
4465 if (super->has_vanilla_constructor() &&
4466 _has_vanilla_constructor) {
4467 ik->set_has_vanilla_constructor();
4468 }
4469 #ifdef ASSERT
4470 bool v = false;
4471 if (super->has_vanilla_constructor()) {
4472 const Method* const constructor =
4473 ik->find_method(vmSymbols::object_initializer_name(),
4474 vmSymbols::void_method_signature());
4475 if (constructor != NULL && constructor->is_vanilla_constructor()) {
4476 v = true;
4477 }
4478 }
4479 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4480 #endif
4481 }
4482
4483 // If it cannot be fast-path allocated, set a bit in the layout helper.
4484 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4485 assert(ik->size_helper() > 0, "layout_helper is initialized");
4486 if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4487 || ik->is_abstract() || ik->is_interface()
4488 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4489 || ik->size_helper() >= FastAllocateSizeLimit) {
4490 // Forbid fast-path allocation.
4491 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4492 ik->set_layout_helper(lh);
4493 }
4494 }
4495
4496 bool ClassFileParser::supports_inline_types() const {
4497 // Inline types are only supported by class file version 61.65535 and later
4498 return _major_version > JAVA_21_VERSION ||
4499 (_major_version == JAVA_21_VERSION /*&& _minor_version == JAVA_PREVIEW_MINOR_VERSION*/); // JAVA_PREVIEW_MINOR_VERSION not yet implemented by javac, check JVMS draft
4500 }
4501
4502 // utility methods for appending an array with check for duplicates
4503
4504 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4505 const Array<InstanceKlass*>* const ifs) {
4506 // iterate over new interfaces
4507 for (int i = 0; i < ifs->length(); i++) {
4508 InstanceKlass* const e = ifs->at(i);
4509 assert(e->is_klass() && e->is_interface(), "just checking");
4510 // add new interface
4511 result->append_if_missing(e);
4512 }
4513 }
4514
4515 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4516 Array<InstanceKlass*>* local_ifs,
4517 ClassLoaderData* loader_data,
4518 TRAPS) {
4519 assert(local_ifs != NULL, "invariant");
4520 assert(loader_data != NULL, "invariant");
4521
4525 // Add superclass transitive interfaces size
4526 if (super != NULL) {
4527 super_size = super->transitive_interfaces()->length();
4528 max_transitive_size += super_size;
4529 }
4530 // Add local interfaces' super interfaces
4531 const int local_size = local_ifs->length();
4532 for (int i = 0; i < local_size; i++) {
4533 InstanceKlass* const l = local_ifs->at(i);
4534 max_transitive_size += l->transitive_interfaces()->length();
4535 }
4536 // Finally add local interfaces
4537 max_transitive_size += local_size;
4538 // Construct array
4539 if (max_transitive_size == 0) {
4540 // no interfaces, use canonicalized array
4541 return Universe::the_empty_instance_klass_array();
4542 } else if (max_transitive_size == super_size) {
4543 // no new local interfaces added, share superklass' transitive interface array
4544 return super->transitive_interfaces();
4545 // The three lines below are commented to work around bug JDK-8245487
4546 // } else if (max_transitive_size == local_size) {
4547 // // only local interfaces added, share local interface array
4548 // return local_ifs;
4549 } else {
4550 ResourceMark rm;
4551 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size);
4552
4553 // Copy down from superclass
4554 if (super != NULL) {
4555 append_interfaces(result, super->transitive_interfaces());
4556 }
4557
4558 // Copy down from local interfaces' superinterfaces
4559 for (int i = 0; i < local_size; i++) {
4560 InstanceKlass* const l = local_ifs->at(i);
4561 append_interfaces(result, l->transitive_interfaces());
4562 }
4563 // Finally add local interfaces
4564 append_interfaces(result, local_ifs);
4565
4566 // length will be less than the max_transitive_size if duplicates were removed
4567 const int length = result->length();
4568 assert(length <= max_transitive_size, "just checking");
4569
4570 Array<InstanceKlass*>* const new_result =
4571 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL);
4572 for (int i = 0; i < length; i++) {
4573 InstanceKlass* const e = result->at(i);
4574 assert(e != NULL, "just checking");
4575 new_result->at_put(i, e);
4576 }
4577 return new_result;
4578 }
4579 }
4580
4581 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4582 assert(this_klass != NULL, "invariant");
4583 const Klass* const super = this_klass->super();
4584
4585 if (super != NULL) {
4586 const InstanceKlass* super_ik = InstanceKlass::cast(super);
4587
4588 if (super->is_final()) {
4589 classfile_icce_error("class %s cannot inherit from final class %s", super_ik, THREAD);
4590 return;
4591 }
4592
4593 if (super_ik->is_sealed() && !super_ik->has_as_permitted_subclass(this_klass)) {
4594 classfile_icce_error("class %s cannot inherit from sealed class %s", super_ik, THREAD);
4595 return;
4596 }
4597
4598 // The JVMS says that super classes for value types must not have the ACC_IDENTITY
4599 // flag set. But, java.lang.Object must still be allowed to be a direct super class
4600 // for a value classes. So, it is treated as a special case for now.
4601 if (this_klass->access_flags().is_value_class() &&
4602 super_ik->name() != vmSymbols::java_lang_Object() &&
4603 super_ik->is_identity_class()) {
4604 classfile_icce_error("value class %s cannot inherit from class %s", super_ik, THREAD);
4605 return;
4606 }
4607
4608 // If the loader is not the boot loader then throw an exception if its
4609 // superclass is in package jdk.internal.reflect and its loader is not a
4610 // special reflection class loader
4611 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
4612 PackageEntry* super_package = super->package();
4613 if (super_package != NULL &&
4614 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
4615 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
4616 ResourceMark rm(THREAD);
4617 Exceptions::fthrow(
4618 THREAD_AND_LOCATION,
4619 vmSymbols::java_lang_IllegalAccessError(),
4620 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
4621 this_klass->external_name(),
4622 this_klass->class_loader_data()->loader_name_and_id(),
4623 super->external_name());
4624 return;
4625 }
4626 }
4627
4771
4772 for (int index = 0; index < num_methods; index++) {
4773 const Method* const m = methods->at(index);
4774 // if m is static and not the init method, throw a verify error
4775 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4776 ResourceMark rm(THREAD);
4777 Exceptions::fthrow(
4778 THREAD_AND_LOCATION,
4779 vmSymbols::java_lang_VerifyError(),
4780 "Illegal static method %s in interface %s",
4781 m->name()->as_C_string(),
4782 this_klass->external_name()
4783 );
4784 return;
4785 }
4786 }
4787 }
4788
4789 // utility methods for format checking
4790
4791 void ClassFileParser::verify_legal_class_modifiers(jint flags, const char* name, bool is_Object, TRAPS) const {
4792 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4793 const bool is_value_class = (flags & JVM_ACC_VALUE) != 0;
4794 const bool is_primitive_class = (flags & JVM_ACC_PRIMITIVE) != 0;
4795 const bool is_identity_class = (flags & JVM_ACC_IDENTITY) != 0;
4796 const bool is_inner_class = name != NULL;
4797 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4798 if (is_module) {
4799 ResourceMark rm(THREAD);
4800 Exceptions::fthrow(
4801 THREAD_AND_LOCATION,
4802 vmSymbols::java_lang_NoClassDefFoundError(),
4803 "%s is not a class because access_flag ACC_MODULE is set",
4804 _class_name->as_C_string());
4805 return;
4806 }
4807
4808 if (is_value_class && !EnableValhalla) {
4809 ResourceMark rm(THREAD);
4810 Exceptions::fthrow(
4811 THREAD_AND_LOCATION,
4812 vmSymbols::java_lang_ClassFormatError(),
4813 "Class modifier ACC_VALUE in class %s requires option -XX:+EnableValhalla",
4814 _class_name->as_C_string()
4815 );
4816 return;
4817 }
4818
4819 if (is_primitive_class && !EnablePrimitiveClasses) {
4820 ResourceMark rm(THREAD);
4821 Exceptions::fthrow(
4822 THREAD_AND_LOCATION,
4823 vmSymbols::java_lang_ClassFormatError(),
4824 "Class modifier ACC_PRIMITIVE in class %s requires option -XX:+EnablePrimitiveClasses",
4825 _class_name->as_C_string()
4826 );
4827 return;
4828 }
4829
4830 // if (!_need_verify) { return; }
4831
4832 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4833 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4834 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4835 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4836 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4837 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4838 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4839
4840 if ((is_abstract && is_final) ||
4841 (is_interface && !is_abstract) ||
4842 (is_interface && major_gte_1_5 && ((is_super && (!EnableValhalla || !supports_inline_types())) || is_enum)) || // ACC_SUPER (now ACC_IDENTITY) was illegal for interfaces
4843 (!is_interface && major_gte_1_5 && is_annotation) ||
4844 (is_value_class && is_enum) ||
4845 (is_identity_class && is_value_class) ||
4846 (EnableValhalla && supports_inline_types() && !is_module && !is_abstract && !is_Object && !(is_identity_class || is_value_class) && !is_inner_class) ||
4847 (EnablePrimitiveClasses && supports_inline_types() && is_primitive_class && (!is_value_class || !is_final || is_interface || is_abstract))) {
4848 ResourceMark rm(THREAD);
4849 const char* class_note = "";
4850 if (is_value_class) class_note = " (a value class)";
4851 if (is_primitive_class) class_note = " (a primitive class)";
4852 if (is_value_class && is_identity_class) class_note = " (a value and identity class)";
4853 if (name == NULL) { // Not an inner class
4854 Exceptions::fthrow(
4855 THREAD_AND_LOCATION,
4856 vmSymbols::java_lang_ClassFormatError(),
4857 "Illegal class modifiers in class %s%s: 0x%X",
4858 _class_name->as_C_string(), class_note, flags
4859 );
4860 return;
4861 } else {
4862 Exceptions::fthrow(
4863 THREAD_AND_LOCATION,
4864 vmSymbols::java_lang_ClassFormatError(),
4865 "Illegal class modifiers in declaration of inner class %s%s of class %s: 0x%X",
4866 name, class_note, _class_name->as_C_string(), flags
4867 );
4868 return;
4869 }
4870 }
4871 }
4872
4873 static bool has_illegal_visibility(jint flags) {
4874 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4875 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4876 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4877
4878 return ((is_public && is_protected) ||
4879 (is_public && is_private) ||
4880 (is_protected && is_private));
4881 }
4882
4883 // A legal major_version.minor_version must be one of the following:
4884 //
4885 // Major_version >= 45 and major_version < 56, any minor_version.
4886 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4887 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4888 //
4889 void ClassFileParser::verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4915 THREAD_AND_LOCATION,
4916 vmSymbols::java_lang_UnsupportedClassVersionError(),
4917 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4918 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4919 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4920 return;
4921 }
4922
4923 if (!Arguments::enable_preview()) {
4924 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4925 class_name, major, minor, THREAD);
4926 return;
4927 }
4928
4929 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4930 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4931 class_name, major, minor, THREAD);
4932 }
4933 }
4934
4935 void ClassFileParser:: verify_legal_field_modifiers(jint flags,
4936 AccessFlags class_access_flags,
4937 TRAPS) const {
4938 if (!_need_verify) { return; }
4939
4940 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4941 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4942 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4943 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4944 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4945 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4946 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4947 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4948 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4949
4950 const bool is_interface = class_access_flags.is_interface();
4951 const bool is_abstract = class_access_flags.is_abstract();
4952 const bool is_value_class = class_access_flags.is_value_class();
4953 const bool is_identity_class = class_access_flags.is_identity_class();
4954
4955 bool is_illegal = false;
4956
4957 if (is_interface) {
4958 if (!is_public || !is_static || !is_final || is_private ||
4959 is_protected || is_volatile || is_transient ||
4960 (major_gte_1_5 && is_enum)) {
4961 is_illegal = true;
4962 }
4963 } else { // not interface
4964 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4965 is_illegal = true;
4966 } else {
4967 if (is_value_class && !is_abstract && !is_static && !is_final) {
4968 is_illegal = true;
4969 } else if (is_abstract && !is_identity_class && !is_static) {
4970 is_illegal = true;
4971 }
4972 }
4973 }
4974
4975 if (is_illegal) {
4976 ResourceMark rm(THREAD);
4977 Exceptions::fthrow(
4978 THREAD_AND_LOCATION,
4979 vmSymbols::java_lang_ClassFormatError(),
4980 "Illegal field modifiers in class %s: 0x%X",
4981 _class_name->as_C_string(), flags);
4982 return;
4983 }
4984 }
4985
4986 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4987 AccessFlags class_access_flags,
4988 const Symbol* name,
4989 TRAPS) const {
4990 if (!_need_verify) { return; }
4991
4992 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4993 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4994 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4995 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4996 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4997 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4998 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4999 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
5000 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
5001 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
5002 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
5003 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
5004 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
5005 const bool is_initializer = (name == vmSymbols::object_initializer_name());
5006 const bool is_factory = (name == vmSymbols::inline_factory_name() && supports_inline_types());
5007 const bool is_interface = class_access_flags.is_interface();
5008 const bool is_value_class = class_access_flags.is_value_class();
5009 const bool is_identity_class = class_access_flags.is_identity_class();
5010 const bool is_abstract_class = class_access_flags.is_abstract();
5011
5012 bool is_illegal = false;
5013
5014 const char* class_note = "";
5015 if (is_interface) {
5016 if (major_gte_8) {
5017 // Class file version is JAVA_8_VERSION or later Methods of
5018 // interfaces may set any of the flags except ACC_PROTECTED,
5019 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
5020 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
5021 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
5022 (is_native || is_protected || is_final || is_synchronized) ||
5023 // If a specific method of a class or interface has its
5024 // ACC_ABSTRACT flag set, it must not have any of its
5025 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
5026 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
5027 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
5028 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
5029 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
5030 is_illegal = true;
5031 }
5032 } else if (major_gte_1_5) {
5033 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
5034 if (!is_public || is_private || is_protected || is_static || is_final ||
5035 is_synchronized || is_native || !is_abstract || is_strict) {
5036 is_illegal = true;
5037 }
5038 } else {
5039 // Class file version is pre-JAVA_1_5_VERSION
5040 if (!is_public || is_static || is_final || is_native || !is_abstract) {
5041 is_illegal = true;
5042 }
5043 }
5044 } else { // not interface
5045 if (has_illegal_visibility(flags)) {
5046 is_illegal = true;
5047 } else {
5048 if (is_factory) { // <vnew> factory method
5049 if (is_final || is_synchronized || is_native || !is_static ||
5050 is_abstract || is_bridge) {
5051 is_illegal = true;
5052 class_note = (is_value_class ? " (a value class)" : " (not a value class)");
5053 }
5054 } else if (is_initializer) {
5055 if (is_static || is_final || is_synchronized || is_native ||
5056 is_abstract || (major_gte_1_5 && is_bridge)) {
5057 is_illegal = true;
5058 }
5059 } else { // not initializer
5060 if (!is_identity_class && is_synchronized && !is_static) {
5061 is_illegal = true;
5062 class_note = " (not an identity class)";
5063 } else {
5064 if (is_abstract) {
5065 if ((is_final || is_native || is_private || is_static ||
5066 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
5067 is_illegal = true;
5068 }
5069 }
5070 }
5071 }
5072 }
5073 }
5074
5075 if (is_illegal) {
5076 ResourceMark rm(THREAD);
5077 if (is_value_class && is_initializer) {
5078 Exceptions::fthrow(
5079 THREAD_AND_LOCATION,
5080 vmSymbols::java_lang_ClassFormatError(),
5081 "Method <init> is not allowed in value class %s",
5082 _class_name->as_C_string());
5083 } else {
5084 Exceptions::fthrow(
5085 THREAD_AND_LOCATION,
5086 vmSymbols::java_lang_ClassFormatError(),
5087 "Method %s in class %s%s has illegal modifiers: 0x%X",
5088 name->as_C_string(), _class_name->as_C_string(),
5089 class_note, flags);
5090 }
5091 return;
5092 }
5093 }
5094
5095 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
5096 int length,
5097 TRAPS) const {
5098 assert(_need_verify, "only called when _need_verify is true");
5099 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
5100 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
5101 }
5102 }
5103
5104 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
5105 // In class names, '/' separates unqualified names. This is verified in this function also.
5106 // Method names also may not contain the characters '<' or '>', unless <init>
5107 // or <clinit>. Note that method names may not be <init> or <clinit> in this
5108 // method. Because these names have been checked as special cases before
5109 // calling this method in verify_legal_method_name.
5110 //
5228 // be taken as a field signature. Allow "void" if void_ok.
5229 // Return a pointer to just past the signature.
5230 // Return NULL if no legal signature is found.
5231 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5232 bool void_ok,
5233 unsigned int length,
5234 TRAPS) const {
5235 unsigned int array_dim = 0;
5236 while (length > 0) {
5237 switch (signature[0]) {
5238 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
5239 case JVM_SIGNATURE_BOOLEAN:
5240 case JVM_SIGNATURE_BYTE:
5241 case JVM_SIGNATURE_CHAR:
5242 case JVM_SIGNATURE_SHORT:
5243 case JVM_SIGNATURE_INT:
5244 case JVM_SIGNATURE_FLOAT:
5245 case JVM_SIGNATURE_LONG:
5246 case JVM_SIGNATURE_DOUBLE:
5247 return signature + 1;
5248 case JVM_SIGNATURE_PRIMITIVE_OBJECT:
5249 // Can't enable this check fully until JDK upgrades the bytecode generators (TODO: JDK-8270852).
5250 // For now, compare to class file version 51 so old verifier doesn't see Q signatures.
5251 if ( (_major_version < 51 /* CONSTANT_CLASS_DESCRIPTORS */ ) || (!EnablePrimitiveClasses)) {
5252 classfile_parse_error("Class name contains illegal Q-signature "
5253 "in descriptor in class file %s, requires option -XX:+EnablePrimitiveClasses",
5254 CHECK_0);
5255 return NULL;
5256 }
5257 // fall through
5258 case JVM_SIGNATURE_CLASS:
5259 {
5260 if (_major_version < JAVA_1_5_VERSION) {
5261 // Skip over the class name if one is there
5262 const char* const p = skip_over_field_name(signature + 1, true, --length);
5263
5264 // The next character better be a semicolon
5265 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) {
5266 return p + 1;
5267 }
5268 }
5269 else {
5270 // Skip leading 'L' or 'Q' and ignore first appearance of ';'
5271 signature++;
5272 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
5273 // Format check signature
5274 if (c != NULL) {
5275 int newlen = c - (char*) signature;
5276 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5277 if (!legal) {
5278 classfile_parse_error("Class name is empty or contains illegal character "
5279 "in descriptor in class file %s",
5280 THREAD);
5281 return NULL;
5282 }
5283 return signature + newlen + 1;
5284 }
5285 }
5286 return NULL;
5287 }
5288 case JVM_SIGNATURE_ARRAY:
5289 array_dim++;
5290 if (array_dim > 255) {
5306
5307 // Checks if name is a legal class name.
5308 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5309 if (!_need_verify || _relax_verify) { return; }
5310
5311 assert(name->refcount() > 0, "symbol must be kept alive");
5312 char* bytes = (char*)name->bytes();
5313 unsigned int length = name->utf8_length();
5314 bool legal = false;
5315
5316 if (length > 0) {
5317 const char* p;
5318 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5319 p = skip_over_field_signature(bytes, false, length, CHECK);
5320 legal = (p != NULL) && ((p - bytes) == (int)length);
5321 } else if (_major_version < JAVA_1_5_VERSION) {
5322 if (bytes[0] != JVM_SIGNATURE_SPECIAL) {
5323 p = skip_over_field_name(bytes, true, length);
5324 legal = (p != NULL) && ((p - bytes) == (int)length);
5325 }
5326 } else if ((_major_version >= CONSTANT_CLASS_DESCRIPTORS || _class_name->starts_with("jdk/internal/reflect/"))
5327 && bytes[length - 1] == ';' ) {
5328 // Support for L...; and Q...; descriptors
5329 legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
5330 } else {
5331 // 4900761: relax the constraints based on JSR202 spec
5332 // Class names may be drawn from the entire Unicode character set.
5333 // Identifiers between '/' must be unqualified names.
5334 // The utf8 string has been verified when parsing cpool entries.
5335 legal = verify_unqualified_name(bytes, length, LegalClass);
5336 }
5337 }
5338 if (!legal) {
5339 ResourceMark rm(THREAD);
5340 assert(_class_name != NULL, "invariant");
5341 Exceptions::fthrow(
5342 THREAD_AND_LOCATION,
5343 vmSymbols::java_lang_ClassFormatError(),
5344 "Illegal class name \"%.*s\" in class file %s", length, bytes,
5345 _class_name->as_C_string()
5346 );
5347 return;
5348 }
5349 }
5375 THREAD_AND_LOCATION,
5376 vmSymbols::java_lang_ClassFormatError(),
5377 "Illegal field name \"%.*s\" in class %s", length, bytes,
5378 _class_name->as_C_string()
5379 );
5380 return;
5381 }
5382 }
5383
5384 // Checks if name is a legal method name.
5385 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const {
5386 if (!_need_verify || _relax_verify) { return; }
5387
5388 assert(name != NULL, "method name is null");
5389 char* bytes = (char*)name->bytes();
5390 unsigned int length = name->utf8_length();
5391 bool legal = false;
5392
5393 if (length > 0) {
5394 if (bytes[0] == JVM_SIGNATURE_SPECIAL) {
5395 if (name == vmSymbols::object_initializer_name() ||
5396 name == vmSymbols::class_initializer_name() ||
5397 (EnableValhalla && supports_inline_types() &&
5398 name == vmSymbols::inline_factory_name())) {
5399 legal = true;
5400 }
5401 } else if (_major_version < JAVA_1_5_VERSION) {
5402 const char* p;
5403 p = skip_over_field_name(bytes, false, length);
5404 legal = (p != NULL) && ((p - bytes) == (int)length);
5405 } else {
5406 // 4881221: relax the constraints based on JSR202 spec
5407 legal = verify_unqualified_name(bytes, length, LegalMethod);
5408 }
5409 }
5410
5411 if (!legal) {
5412 ResourceMark rm(THREAD);
5413 assert(_class_name != NULL, "invariant");
5414 Exceptions::fthrow(
5415 THREAD_AND_LOCATION,
5416 vmSymbols::java_lang_ClassFormatError(),
5417 "Illegal method name \"%.*s\" in class %s", length, bytes,
5418 _class_name->as_C_string()
5419 );
5420 return;
5421 }
5422 }
5423
5424
5425 // Checks if signature is a legal field signature.
5426 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5427 const Symbol* signature,
5428 TRAPS) const {
5429 if (!_need_verify) { return; }
5430 if ((!supports_inline_types() || !EnablePrimitiveClasses) && (signature->is_Q_signature() || signature->is_Q_array_signature())) {
5431 throwIllegalSignature("Field", name, signature, CHECK);
5432 }
5433
5434 const char* const bytes = (const char* const)signature->bytes();
5435 const unsigned int length = signature->utf8_length();
5436 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
5437
5438 if (p == NULL || (p - bytes) != (int)length) {
5439 throwIllegalSignature("Field", name, signature, CHECK);
5440 }
5441 }
5442
5443 // Check that the signature is compatible with the method name. For example,
5444 // check that <init> has a void signature.
5445 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5446 const Symbol* signature,
5447 TRAPS) const {
5448 if (!_need_verify) {
5449 return;
5450 }
5451
5452 // Class initializers cannot have args for class format version >= 51.
5453 if (name == vmSymbols::class_initializer_name() &&
5454 signature != vmSymbols::void_method_signature() &&
5455 _major_version >= JAVA_7_VERSION) {
5456 throwIllegalSignature("Method", name, signature, THREAD);
5457 return;
5458 }
5459
5460 if (!is_value_class()) {
5461 int sig_length = signature->utf8_length();
5462 if (name->utf8_length() > 0 &&
5463 name->char_at(0) == JVM_SIGNATURE_SPECIAL &&
5464 sig_length > 0 &&
5465 signature->char_at(sig_length - 1) != JVM_SIGNATURE_VOID) {
5466 throwIllegalSignature("Method", name, signature, THREAD);
5467 }
5468 }
5469 }
5470
5471 // Checks if signature is a legal method signature.
5472 // Returns number of parameters
5473 int ClassFileParser::verify_legal_method_signature(const Symbol* name,
5474 const Symbol* signature,
5475 TRAPS) const {
5476 if (!_need_verify) {
5477 // make sure caller's args_size will be less than 0 even for non-static
5478 // method so it will be recomputed in compute_size_of_parameters().
5479 return -2;
5480 }
5481
5482 unsigned int args_size = 0;
5483 const char* p = (const char*)signature->bytes();
5484 unsigned int length = signature->utf8_length();
5485 const char* nextp;
5486
5487 // The first character must be a '('
5623 }
5624 }
5625
5626 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5627 const ClassInstanceInfo& cl_inst_info,
5628 TRAPS) {
5629 if (_klass != NULL) {
5630 return _klass;
5631 }
5632
5633 InstanceKlass* const ik =
5634 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5635
5636 if (is_hidden()) {
5637 mangle_hidden_class_name(ik);
5638 }
5639
5640 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5641
5642 assert(_klass == ik, "invariant");
5643 return ik;
5644 }
5645
5646 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5647 bool changed_by_loadhook,
5648 const ClassInstanceInfo& cl_inst_info,
5649 TRAPS) {
5650 assert(ik != NULL, "invariant");
5651
5652 // Set name and CLD before adding to CLD
5653 ik->set_class_loader_data(_loader_data);
5654 ik->set_name(_class_name);
5655
5656 // Add all classes to our internal class loader list here,
5657 // including classes in the bootstrap (NULL) class loader.
5658 const bool publicize = !is_internal();
5659
5660 _loader_data->add_class(ik, publicize);
5661
5662 set_klass_to_deallocate(ik);
5663
5664 assert(_field_info != NULL, "invariant");
5665 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5666 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5667 "sanity");
5668
5669 assert(ik->is_instance_klass(), "sanity");
5670 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5671
5672 // Fill in information already parsed
5673 ik->set_should_verify_class(_need_verify);
5674
5675 // Not yet: supers are done below to support the new subtype-checking fields
5676 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5677 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5678 if (_field_info->_is_naturally_atomic && ik->is_inline_klass()) {
5679 ik->set_is_naturally_atomic();
5680 }
5681
5682 if (carries_identity_modifier()) {
5683 ik->set_carries_identity_modifier();
5684 } else if (carries_value_modifier()) {
5685 ik->set_carries_value_modifier();
5686 }
5687
5688 assert(_fac != NULL, "invariant");
5689 ik->set_static_oop_field_count(_fac->count[STATIC_OOP] + _fac->count[STATIC_INLINE]);
5690
5691 // this transfers ownership of a lot of arrays from
5692 // the parser onto the InstanceKlass*
5693 apply_parsed_class_metadata(ik, _java_fields_count);
5694
5695 // can only set dynamic nest-host after static nest information is set
5696 if (cl_inst_info.dynamic_nest_host() != NULL) {
5697 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5698 }
5699
5700 // note that is not safe to use the fields in the parser from this point on
5701 assert(NULL == _cp, "invariant");
5702 assert(NULL == _fields, "invariant");
5703 assert(NULL == _methods, "invariant");
5704 assert(NULL == _inner_classes, "invariant");
5705 assert(NULL == _nest_members, "invariant");
5706 assert(NULL == _preload_classes, "invariant");
5707 assert(NULL == _combined_annotations, "invariant");
5708 assert(NULL == _record_components, "invariant");
5709 assert(NULL == _permitted_subclasses, "invariant");
5710
5711 if (_has_final_method) {
5712 ik->set_has_final_method();
5713 }
5714
5715 ik->copy_method_ordering(_method_ordering, CHECK);
5716 // The InstanceKlass::_methods_jmethod_ids cache
5717 // is managed on the assumption that the initial cache
5718 // size is equal to the number of methods in the class. If
5719 // that changes, then InstanceKlass::idnum_can_increment()
5720 // has to be changed accordingly.
5721 ik->set_initial_method_idnum(ik->methods()->length());
5722
5723 ik->set_this_class_index(_this_class_index);
5724
5725 if (_is_hidden) {
5726 // _this_class_index is a CONSTANT_Class entry that refers to this
5727 // hidden class itself. If this class needs to refer to its own methods
5728 // or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5729 // _this_class_index. However, because this class is hidden (it's
5730 // not stored in SystemDictionary), _this_class_index cannot be resolved
5731 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5732 // Therefore, we must eagerly resolve _this_class_index now.
5733 ik->constants()->klass_at_put(_this_class_index, ik);
5734 }
5735
5736 ik->set_minor_version(_minor_version);
5737 ik->set_major_version(_major_version);
5738 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5739 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5740 if (_is_declared_atomic) {
5741 ik->set_is_declared_atomic();
5742 }
5743
5744 if (_is_hidden) {
5745 ik->set_is_hidden();
5746 }
5747
5748 // Set PackageEntry for this_klass
5749 oop cl = ik->class_loader();
5750 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5751 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5752 ik->set_package(cld, NULL, CHECK);
5753
5754 const Array<Method*>* const methods = ik->methods();
5755 assert(methods != NULL, "invariant");
5756 const int methods_len = methods->length();
5757
5758 check_methods_for_intrinsics(ik, methods);
5759
5760 // Fill in field values obtained by parse_classfile_attributes
5761 if (_parsed_annotations->has_any_annotations()) {
5762 _parsed_annotations->apply_to(ik);
5830
5831 assert(_all_mirandas != NULL, "invariant");
5832
5833 // Generate any default methods - default methods are public interface methods
5834 // that have a default implementation. This is new with Java 8.
5835 if (_has_nonstatic_concrete_methods) {
5836 DefaultMethods::generate_default_methods(ik,
5837 _all_mirandas,
5838 CHECK);
5839 }
5840
5841 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5842 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5843 !module_entry->has_default_read_edges()) {
5844 if (!module_entry->set_has_default_read_edges()) {
5845 // We won a potential race
5846 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5847 }
5848 }
5849
5850 bool all_fields_empty = true;
5851 for (AllFieldStream fs(ik->fields(), ik->constants()); !fs.done(); fs.next()) {
5852 if (!fs.access_flags().is_static()) {
5853 if (fs.field_descriptor().is_inline_type()) {
5854 Klass* k = _inline_type_field_klasses->at(fs.index());
5855 ik->set_inline_type_field_klass(fs.index(), k);
5856 if (!InlineKlass::cast(k)->is_empty_inline_type()) { all_fields_empty = false; }
5857 } else {
5858 all_fields_empty = false;
5859 }
5860 } else if (is_inline_type() && (fs.name() == vmSymbols::default_value_name())) {
5861 InlineKlass::cast(ik)->set_default_value_offset(ik->field_offset(fs.index()));
5862 }
5863 }
5864
5865 if (_is_empty_inline_type || (is_inline_type() && all_fields_empty)) {
5866 ik->set_is_empty_inline_type();
5867 }
5868
5869 if (is_inline_type()) {
5870 InlineKlass* vk = InlineKlass::cast(ik);
5871 vk->set_alignment(_alignment);
5872 vk->set_first_field_offset(_first_field_offset);
5873 vk->set_exact_size_in_bytes(_exact_size_in_bytes);
5874 InlineKlass::cast(ik)->initialize_calling_convention(CHECK);
5875 }
5876
5877 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5878
5879 if (!is_internal()) {
5880 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5881
5882 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5883 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5884 log_is_enabled(Info, class, preview)) {
5885 ResourceMark rm;
5886 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5887 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5888 }
5889
5890 if (log_is_enabled(Debug, class, resolve)) {
5891 ResourceMark rm;
5892 // print out the superclass.
5893 const char * from = ik->external_name();
5894 if (ik->java_super() != NULL) {
5895 log_debug(class, resolve)("%s %s (super)",
5896 from,
5947 ClassFileParser::ClassFileParser(ClassFileStream* stream,
5948 Symbol* name,
5949 ClassLoaderData* loader_data,
5950 const ClassLoadInfo* cl_info,
5951 Publicity pub_level,
5952 TRAPS) :
5953 _stream(stream),
5954 _class_name(NULL),
5955 _loader_data(loader_data),
5956 _is_hidden(cl_info->is_hidden()),
5957 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5958 _orig_cp_size(0),
5959 _super_klass(),
5960 _cp(NULL),
5961 _fields(NULL),
5962 _methods(NULL),
5963 _inner_classes(NULL),
5964 _nest_members(NULL),
5965 _nest_host(0),
5966 _permitted_subclasses(NULL),
5967 _preload_classes(NULL),
5968 _record_components(NULL),
5969 _local_interfaces(NULL),
5970 _local_interface_indexes(NULL),
5971 _transitive_interfaces(NULL),
5972 _combined_annotations(NULL),
5973 _class_annotations(NULL),
5974 _class_type_annotations(NULL),
5975 _fields_annotations(NULL),
5976 _fields_type_annotations(NULL),
5977 _klass(NULL),
5978 _klass_to_deallocate(NULL),
5979 _parsed_annotations(NULL),
5980 _fac(NULL),
5981 _field_info(NULL),
5982 _inline_type_field_klasses(NULL),
5983 _method_ordering(NULL),
5984 _all_mirandas(NULL),
5985 _vtable_size(0),
5986 _itable_size(0),
5987 _num_miranda_methods(0),
5988 _protection_domain(cl_info->protection_domain()),
5989 _access_flags(),
5990 _pub_level(pub_level),
5991 _bad_constant_seen(0),
5992 _synthetic_flag(false),
5993 _sde_length(false),
5994 _sde_buffer(NULL),
5995 _sourcefile_index(0),
5996 _generic_signature_index(0),
5997 _major_version(0),
5998 _minor_version(0),
5999 _this_class_index(0),
6000 _super_class_index(0),
6001 _itfs_len(0),
6002 _java_fields_count(0),
6003 _need_verify(false),
6004 _relax_verify(false),
6005 _has_nonstatic_concrete_methods(false),
6006 _declares_nonstatic_concrete_methods(false),
6007 _has_final_method(false),
6008 _has_contended_fields(false),
6009 _has_inline_type_fields(false),
6010 _has_nonstatic_fields(false),
6011 _is_empty_inline_type(false),
6012 _is_naturally_atomic(false),
6013 _is_declared_atomic(false),
6014 _carries_value_modifier(false),
6015 _carries_identity_modifier(false),
6016 _has_finalizer(false),
6017 _has_empty_finalizer(false),
6018 _has_vanilla_constructor(false),
6019 _max_bootstrap_specifier_index(-1) {
6020
6021 _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
6022 _class_name->increment_refcount();
6023
6024 assert(_loader_data != NULL, "invariant");
6025 assert(stream != NULL, "invariant");
6026 assert(_stream != NULL, "invariant");
6027 assert(_stream->buffer() == _stream->current(), "invariant");
6028 assert(_class_name != NULL, "invariant");
6029 assert(0 == _access_flags.as_int(), "invariant");
6030
6031 // Figure out whether we can skip format checking (matching classic VM behavior)
6032 if (DumpSharedSpaces) {
6033 // verify == true means it's a 'remote' class (i.e., non-boot class)
6034 // Verification decision is based on BytecodeVerificationRemote flag
6035 // for those classes.
6045 stream->set_verify(_need_verify);
6046
6047 // Check if verification needs to be relaxed for this class file
6048 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
6049 _relax_verify = relax_format_check_for(_loader_data);
6050
6051 parse_stream(stream, CHECK);
6052
6053 post_process_parsed_stream(stream, _cp, CHECK);
6054 }
6055
6056 void ClassFileParser::clear_class_metadata() {
6057 // metadata created before the instance klass is created. Must be
6058 // deallocated if classfile parsing returns an error.
6059 _cp = NULL;
6060 _fields = NULL;
6061 _methods = NULL;
6062 _inner_classes = NULL;
6063 _nest_members = NULL;
6064 _permitted_subclasses = NULL;
6065 _preload_classes = NULL;
6066 _combined_annotations = NULL;
6067 _class_annotations = _class_type_annotations = NULL;
6068 _fields_annotations = _fields_type_annotations = NULL;
6069 _record_components = NULL;
6070 }
6071
6072 // Destructor to clean up
6073 ClassFileParser::~ClassFileParser() {
6074 _class_name->decrement_refcount();
6075
6076 if (_cp != NULL) {
6077 MetadataFactory::free_metadata(_loader_data, _cp);
6078 }
6079 if (_fields != NULL) {
6080 MetadataFactory::free_array<u2>(_loader_data, _fields);
6081 }
6082
6083 if (_inline_type_field_klasses != NULL) {
6084 MetadataFactory::free_array<InlineKlass*>(_loader_data, _inline_type_field_klasses);
6085 }
6086
6087 if (_methods != NULL) {
6088 // Free methods
6089 InstanceKlass::deallocate_methods(_loader_data, _methods);
6090 }
6091
6092 // beware of the Universe::empty_blah_array!!
6093 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
6094 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
6095 }
6096
6097 if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
6098 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
6099 }
6100
6101 if (_record_components != NULL) {
6102 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
6103 }
6104
6105 if (_permitted_subclasses != NULL && _permitted_subclasses != Universe::the_empty_short_array()) {
6106 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
6107 }
6108
6109 if (_preload_classes != NULL && _preload_classes != Universe::the_empty_short_array()) {
6110 MetadataFactory::free_array<u2>(_loader_data, _preload_classes);
6111 }
6112
6113 // Free interfaces
6114 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
6115 _local_interfaces, _transitive_interfaces);
6116
6117 if (_combined_annotations != NULL) {
6118 // After all annotations arrays have been created, they are installed into the
6119 // Annotations object that will be assigned to the InstanceKlass being created.
6120
6121 // Deallocate the Annotations object and the installed annotations arrays.
6122 _combined_annotations->deallocate_contents(_loader_data);
6123
6124 // If the _combined_annotations pointer is non-NULL,
6125 // then the other annotations fields should have been cleared.
6126 assert(_class_annotations == NULL, "Should have been cleared");
6127 assert(_class_type_annotations == NULL, "Should have been cleared");
6128 assert(_fields_annotations == NULL, "Should have been cleared");
6129 assert(_fields_type_annotations == NULL, "Should have been cleared");
6130 } else {
6131 // If the annotations arrays were not installed into the Annotations object,
6132 // then they have to be deallocated explicitly.
6177 cp_size, CHECK);
6178
6179 _orig_cp_size = cp_size;
6180 if (is_hidden()) { // Add a slot for hidden class name.
6181 cp_size++;
6182 }
6183
6184 _cp = ConstantPool::allocate(_loader_data,
6185 cp_size,
6186 CHECK);
6187
6188 ConstantPool* const cp = _cp;
6189
6190 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6191
6192 assert(cp_size == (const u2)cp->length(), "invariant");
6193
6194 // ACCESS FLAGS
6195 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
6196
6197 jint recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS;
6198 // JVM_ACC_MODULE is defined in JDK-9 and later.
6199 if (_major_version >= JAVA_9_VERSION) {
6200 recognized_modifiers |= JVM_ACC_MODULE;
6201 }
6202 // JVM_ACC_VALUE and JVM_ACC_PRIMITIVE supported version
6203 if (supports_inline_types()) {
6204 recognized_modifiers |= JVM_ACC_PRIMITIVE | JVM_ACC_VALUE;
6205 }
6206
6207 // Access flags
6208 jint flags = stream->get_u2_fast() & recognized_modifiers;
6209
6210 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6211 // Set abstract bit for old class files for backward compatibility
6212 flags |= JVM_ACC_ABSTRACT;
6213 }
6214
6215 // This class and superclass
6216 _this_class_index = stream->get_u2_fast();
6217 check_property(
6218 valid_cp_range(_this_class_index, cp_size) &&
6219 cp->tag_at(_this_class_index).is_unresolved_klass(),
6220 "Invalid this class index %u in constant pool in class file %s",
6221 _this_class_index, CHECK);
6222
6223 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
6224 assert(class_name_in_cp != NULL, "class_name can't be null");
6225
6226 bool is_java_lang_Object = class_name_in_cp == vmSymbols::java_lang_Object();
6227
6228 verify_legal_class_modifiers(flags, NULL, is_java_lang_Object, CHECK);
6229
6230 if (EnableValhalla) {
6231 if(!supports_inline_types()) {
6232 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
6233 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
6234 if (!is_module && !is_interface && !is_java_lang_Object) {
6235 flags |= JVM_ACC_IDENTITY;
6236 }
6237 }
6238 }
6239
6240 _access_flags.set_flags(flags);
6241
6242 if (EnableValhalla) {
6243 if (_access_flags.is_identity_class()) set_carries_identity_modifier();
6244 if (_access_flags.is_value_class()) set_carries_value_modifier();
6245 if (carries_identity_modifier() && carries_value_modifier()) {
6246 classfile_parse_error("Class %s has both ACC_IDENTITY and ACC_VALUE modifiers", THREAD);
6247 }
6248 }
6249
6250 short bad_constant = class_bad_constant_seen();
6251 if (bad_constant != 0) {
6252 // Do not throw CFE until after the access_flags are checked because if
6253 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6254 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
6255 return;
6256 }
6257
6258 // Don't need to check whether this class name is legal or not.
6259 // It has been checked when constant pool is parsed.
6260 // However, make sure it is not an array type.
6261 if (_need_verify) {
6262 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
6263 "Bad class name in class file %s",
6264 CHECK);
6265 }
6266
6267 #ifdef ASSERT
6268 // Basic sanity checks
6269 if (_is_hidden) {
6270 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
6271 }
6272 #endif
6273
6274 // Update the _class_name as needed depending on whether this is a named, un-named, or hidden class.
6275
6276 if (_is_hidden) {
6277 assert(_class_name != NULL, "Unexpected null _class_name");
6315 if (stream->source() != NULL) {
6316 ls.print(" source: %s", stream->source());
6317 }
6318 ls.cr();
6319 }
6320 }
6321
6322 // SUPERKLASS
6323 _super_class_index = stream->get_u2_fast();
6324 _super_klass = parse_super_class(cp,
6325 _super_class_index,
6326 _need_verify,
6327 CHECK);
6328
6329 // Interfaces
6330 _itfs_len = stream->get_u2_fast();
6331 parse_interfaces(stream,
6332 _itfs_len,
6333 cp,
6334 &_has_nonstatic_concrete_methods,
6335 &_is_declared_atomic,
6336 CHECK);
6337
6338 // Fields (offsets are filled in later)
6339 _fac = new FieldAllocationCount();
6340 parse_fields(stream,
6341 _access_flags,
6342 _fac,
6343 cp,
6344 cp_size,
6345 &_java_fields_count,
6346 CHECK);
6347
6348 assert(_fields != NULL, "invariant");
6349
6350 // Methods
6351 AccessFlags promoted_flags;
6352 parse_methods(stream,
6353 is_interface(),
6354 is_value_class(),
6355 is_abstract_class(),
6356 &promoted_flags,
6357 &_has_final_method,
6358 &_declares_nonstatic_concrete_methods,
6359 CHECK);
6360
6361 assert(_methods != NULL, "invariant");
6362
6363 // promote flags from parse_methods() to the klass' flags
6364 _access_flags.add_promoted_flags(promoted_flags.as_int());
6365
6366 if (_declares_nonstatic_concrete_methods) {
6367 _has_nonstatic_concrete_methods = true;
6368 }
6369
6370 // Additional attributes/annotations
6371 _parsed_annotations = new ClassAnnotationCollector();
6372 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6373
6374 assert(_inner_classes != NULL, "invariant");
6375
6418 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
6419 // We have to update the resolved_klass_index and the name_index together
6420 // so extract the existing resolved_klass_index first.
6421 CPKlassSlot cp_klass_slot = _cp->klass_slot_at(_this_class_index);
6422 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
6423 _cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
6424 assert(_cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
6425 "Bad name_index");
6426 }
6427
6428 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream,
6429 ConstantPool* cp,
6430 TRAPS) {
6431 assert(stream != NULL, "invariant");
6432 assert(stream->at_eos(), "invariant");
6433 assert(cp != NULL, "invariant");
6434 assert(_loader_data != NULL, "invariant");
6435
6436 if (_class_name == vmSymbols::java_lang_Object()) {
6437 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(),
6438 "java.lang.Object cannot implement an interface in class file %s",
6439 CHECK);
6440 }
6441 // We check super class after class file is parsed and format is checked
6442 if (_super_class_index > 0 && NULL == _super_klass) {
6443 Symbol* const super_class_name = cp->klass_name_at(_super_class_index);
6444 if (is_interface()) {
6445 // Before attempting to resolve the superclass, check for class format
6446 // errors not checked yet.
6447 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
6448 "Interfaces must have java.lang.Object as superclass in class file %s",
6449 CHECK);
6450 }
6451 Handle loader(THREAD, _loader_data->class_loader());
6452 _super_klass = (const InstanceKlass*)
6453 SystemDictionary::resolve_super_or_fail(_class_name,
6454 super_class_name,
6455 loader,
6456 _protection_domain,
6457 true,
6458 CHECK);
6459 }
6460
6461 if (_super_klass != NULL) {
6462 if (_super_klass->is_interface()) {
6463 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
6464 return;
6465 }
6466
6467 if (EnableValhalla) {
6468 check_identity_and_value_modifiers(this, _super_klass, CHECK);
6469 }
6470
6471 if (_super_klass->has_nonstatic_concrete_methods()) {
6472 _has_nonstatic_concrete_methods = true;
6473 }
6474 if (_super_klass->is_declared_atomic()) {
6475 _is_declared_atomic = true;
6476 }
6477 }
6478
6479 if (*ForceNonTearable != '\0') {
6480 // Allow a command line switch to force the same atomicity property:
6481 const char* class_name_str = _class_name->as_C_string();
6482 if (StringUtils::class_list_match(ForceNonTearable, class_name_str)) {
6483 _is_declared_atomic = true;
6484 }
6485 }
6486
6487 int itfs_len = _local_interface_indexes == NULL ? 0 : _local_interface_indexes->length();
6488 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, NULL, CHECK);
6489 if (_local_interface_indexes != NULL) {
6490 for (int i = 0; i < _local_interface_indexes->length(); i++) {
6491 u2 interface_index = _local_interface_indexes->at(i);
6492 Klass* interf;
6493 if (cp->tag_at(interface_index).is_klass()) {
6494 interf = cp->resolved_klass_at(interface_index);
6495 } else {
6496 Symbol* const unresolved_klass = cp->klass_name_at(interface_index);
6497
6498 // Don't need to check legal name because it's checked when parsing constant pool.
6499 // But need to make sure it's not an array type.
6500 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY,
6501 "Bad interface name in class file %s", CHECK);
6502
6503 // Call resolve_super so class circularity is checked
6504 interf = SystemDictionary::resolve_super_or_fail(
6505 _class_name,
6506 unresolved_klass,
6507 Handle(THREAD, _loader_data->class_loader()),
6508 _protection_domain,
6509 false,
6510 CHECK);
6511 }
6512
6513 if (!interf->is_interface()) {
6514 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6515 err_msg("class %s can not implement %s, because it is not an interface (%s)",
6516 _class_name->as_klass_external_name(),
6517 interf->external_name(),
6518 interf->class_in_module_of_loader()));
6519 }
6520
6521 if (EnableValhalla) {
6522 // Check modifiers and set carries_identity_modifier/carries_value_modifier flags
6523 check_identity_and_value_modifiers(this, InstanceKlass::cast(interf), CHECK);
6524 }
6525
6526 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) {
6527 _has_nonstatic_concrete_methods = true;
6528 }
6529 if (InstanceKlass::cast(interf)->is_declared_atomic()) {
6530 _is_declared_atomic = true;
6531 }
6532 _local_interfaces->at_put(i, InstanceKlass::cast(interf));
6533 }
6534 }
6535 assert(_local_interfaces != NULL, "invariant");
6536
6537 // Compute the transitive list of all unique interfaces implemented by this class
6538 _transitive_interfaces =
6539 compute_transitive_interfaces(_super_klass,
6540 _local_interfaces,
6541 _loader_data,
6542 CHECK);
6543
6544 assert(_transitive_interfaces != NULL, "invariant");
6545
6546 // sort methods
6547 _method_ordering = sort_methods(_methods);
6548
6549 _all_mirandas = new GrowableArray<Method*>(20);
6550
6551 Handle loader(THREAD, _loader_data->class_loader());
6552 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6553 &_num_miranda_methods,
6554 _all_mirandas,
6555 _super_klass,
6556 _methods,
6557 _access_flags,
6558 _major_version,
6559 loader,
6560 _class_name,
6561 _local_interfaces);
6562
6563 // Size of Java itable (in words)
6564 _itable_size = is_interface() ? 0 :
6565 klassItable::compute_itable_size(_transitive_interfaces);
6566
6567 assert(_fac != NULL, "invariant");
6568 assert(_parsed_annotations != NULL, "invariant");
6569
6570
6571 if (EnablePrimitiveClasses) {
6572 _inline_type_field_klasses = MetadataFactory::new_array<InlineKlass*>(_loader_data,
6573 java_fields_count(),
6574 NULL,
6575 CHECK);
6576 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
6577 if (Signature::basic_type(fs.signature()) == T_PRIMITIVE_OBJECT && !fs.access_flags().is_static()) {
6578 // Pre-load inline class
6579 Klass* klass = SystemDictionary::resolve_inline_type_field_or_fail(&fs,
6580 Handle(THREAD, _loader_data->class_loader()),
6581 _protection_domain, true, CHECK);
6582 assert(klass != NULL, "Sanity check");
6583 if (!klass->access_flags().is_value_class()) {
6584 assert(klass->is_instance_klass(), "Sanity check");
6585 ResourceMark rm(THREAD);
6586 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6587 err_msg("Class %s expects class %s to be an inline type, but it is not",
6588 _class_name->as_C_string(),
6589 InstanceKlass::cast(klass)->external_name()));
6590 }
6591 _inline_type_field_klasses->at_put(fs.index(), InlineKlass::cast(klass));
6592 }
6593 }
6594 }
6595
6596 _field_info = new FieldLayoutInfo();
6597 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, _fields,
6598 _parsed_annotations->is_contended(), is_inline_type(),
6599 _field_info, _inline_type_field_klasses);
6600 lb.build_layout(CHECK);
6601 if (is_inline_type()) {
6602 _alignment = lb.get_alignment();
6603 _first_field_offset = lb.get_first_field_offset();
6604 _exact_size_in_bytes = lb.get_exact_size_in_byte();
6605 }
6606 _has_inline_type_fields = _field_info->_has_inline_fields;
6607 }
6608
6609 void ClassFileParser::set_klass(InstanceKlass* klass) {
6610
6611 #ifdef ASSERT
6612 if (klass != NULL) {
6613 assert(NULL == _klass, "leaking?");
6614 }
6615 #endif
6616
6617 _klass = klass;
6618 }
6619
6620 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6621
6622 #ifdef ASSERT
6623 if (klass != NULL) {
6624 assert(NULL == _klass_to_deallocate, "leaking?");
6625 }
6626 #endif
|