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