4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "cds/cdsConfig.hpp"
25 #include "classfile/classFileParser.hpp"
26 #include "classfile/classFileStream.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/classLoaderData.inline.hpp"
29 #include "classfile/classLoadInfo.hpp"
30 #include "classfile/defaultMethods.hpp"
31 #include "classfile/fieldLayoutBuilder.hpp"
32 #include "classfile/javaClasses.inline.hpp"
33 #include "classfile/moduleEntry.hpp"
34 #include "classfile/packageEntry.hpp"
35 #include "classfile/symbolTable.hpp"
36 #include "classfile/systemDictionary.hpp"
37 #include "classfile/systemDictionaryShared.hpp"
38 #include "classfile/verificationType.hpp"
39 #include "classfile/verifier.hpp"
40 #include "classfile/vmClasses.hpp"
41 #include "classfile/vmSymbols.hpp"
42 #include "jvm.h"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "memory/allocation.hpp"
46 #include "memory/metadataFactory.hpp"
47 #include "memory/oopFactory.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "memory/universe.hpp"
50 #include "oops/annotations.hpp"
51 #include "oops/bsmAttribute.inline.hpp"
52 #include "oops/constantPool.inline.hpp"
53 #include "oops/fieldInfo.hpp"
54 #include "oops/fieldStreams.inline.hpp"
55 #include "oops/instanceKlass.inline.hpp"
56 #include "oops/instanceMirrorKlass.hpp"
57 #include "oops/klass.inline.hpp"
58 #include "oops/klassVtable.hpp"
59 #include "oops/metadata.hpp"
60 #include "oops/method.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/recordComponent.hpp"
63 #include "oops/symbol.hpp"
64 #include "prims/jvmtiExport.hpp"
65 #include "prims/jvmtiThreadState.hpp"
66 #include "runtime/arguments.hpp"
67 #include "runtime/fieldDescriptor.inline.hpp"
68 #include "runtime/handles.inline.hpp"
69 #include "runtime/javaCalls.hpp"
70 #include "runtime/os.hpp"
71 #include "runtime/perfData.hpp"
72 #include "runtime/reflection.hpp"
73 #include "runtime/safepointVerifiers.hpp"
74 #include "runtime/signature.hpp"
75 #include "runtime/timer.hpp"
76 #include "services/classLoadingService.hpp"
77 #include "services/threadService.hpp"
78 #include "utilities/align.hpp"
79 #include "utilities/bitMap.inline.hpp"
80 #include "utilities/checkedCast.hpp"
81 #include "utilities/copy.hpp"
82 #include "utilities/exceptions.hpp"
83 #include "utilities/formatBuffer.hpp"
84 #include "utilities/globalDefinitions.hpp"
85 #include "utilities/growableArray.hpp"
86 #include "utilities/hashTable.hpp"
87 #include "utilities/macros.hpp"
88 #include "utilities/ostream.hpp"
89 #include "utilities/utf8.hpp"
90
91 // We generally try to create the oops directly when parsing, rather than
92 // allocating temporary data structures and copying the bytes twice. A
93 // temporary area is only needed when parsing utf8 entries in the constant
94 // pool and when parsing line number tables.
95
96 // We add assert in debug mode when class format is not checked.
97
98 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
99 #define JAVA_MIN_SUPPORTED_VERSION 45
100 #define JAVA_PREVIEW_MINOR_VERSION 65535
101
102 // Used for two backward compatibility reasons:
103 // - to check for new additions to the class file format in JDK1.5
104 // - to check for bug fixes in the format checker in JDK1.5
105 #define JAVA_1_5_VERSION 49
106
107 // Used for backward compatibility reasons:
108 // - to check for javac bug fixes that happened after 1.5
481 guarantee_property(valid_symbol_at(name_ref_index),
482 "Invalid constant pool index %u in class file %s",
483 name_ref_index, CHECK);
484 guarantee_property(valid_symbol_at(signature_ref_index),
485 "Invalid constant pool index %u in class file %s",
486 signature_ref_index, CHECK);
487 break;
488 }
489 case JVM_CONSTANT_Utf8:
490 break;
491 case JVM_CONSTANT_UnresolvedClass: // fall-through
492 case JVM_CONSTANT_UnresolvedClassInError: {
493 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
494 break;
495 }
496 case JVM_CONSTANT_ClassIndex: {
497 const int class_index = cp->klass_index_at(index);
498 guarantee_property(valid_symbol_at(class_index),
499 "Invalid constant pool index %u in class file %s",
500 class_index, CHECK);
501 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
502 break;
503 }
504 case JVM_CONSTANT_StringIndex: {
505 const int string_index = cp->string_index_at(index);
506 guarantee_property(valid_symbol_at(string_index),
507 "Invalid constant pool index %u in class file %s",
508 string_index, CHECK);
509 Symbol* const sym = cp->symbol_at(string_index);
510 cp->unresolved_string_at_put(index, sym);
511 break;
512 }
513 case JVM_CONSTANT_MethodHandle: {
514 const int ref_index = cp->method_handle_index_at(index);
515 guarantee_property(valid_cp_range(ref_index, length),
516 "Invalid constant pool index %u in class file %s",
517 ref_index, CHECK);
518 const constantTag tag = cp->tag_at(ref_index);
519 const int ref_kind = cp->method_handle_ref_kind_at(index);
520
922 class AnnotationCollector : public ResourceObj{
923 public:
924 enum Location { _in_field, _in_method, _in_class };
925 enum ID {
926 _unknown = 0,
927 _method_CallerSensitive,
928 _method_ForceInline,
929 _method_DontInline,
930 _method_ChangesCurrentThread,
931 _method_JvmtiHideEvents,
932 _method_JvmtiMountTransition,
933 _method_InjectedProfile,
934 _method_LambdaForm_Compiled,
935 _method_Hidden,
936 _method_Scoped,
937 _method_IntrinsicCandidate,
938 _jdk_internal_vm_annotation_Contended,
939 _field_Stable,
940 _jdk_internal_vm_annotation_ReservedStackAccess,
941 _jdk_internal_ValueBased,
942 _java_lang_Deprecated,
943 _java_lang_Deprecated_for_removal,
944 _jdk_internal_vm_annotation_AOTSafeClassInitializer,
945 _method_AOTRuntimeSetup,
946 _jdk_internal_vm_annotation_TrustFinalFields,
947 _annotation_LIMIT
948 };
949 const Location _location;
950 int _annotations_present;
951 u2 _contended_group;
952
953 AnnotationCollector(Location location)
954 : _location(location), _annotations_present(0), _contended_group(0)
955 {
956 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
957 }
958 // If this annotation name has an ID, report it (or _none).
959 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
960 // Set the annotation name:
961 void set_annotation(ID id) {
1356 }
1357
1358 *constantvalue_index_addr = constantvalue_index;
1359 *is_synthetic_addr = is_synthetic;
1360 *generic_signature_index_addr = generic_signature_index;
1361 AnnotationArray* a = allocate_annotations(runtime_visible_annotations,
1362 runtime_visible_annotations_length,
1363 CHECK);
1364 parsed_annotations->set_field_annotations(a);
1365 a = allocate_annotations(runtime_visible_type_annotations,
1366 runtime_visible_type_annotations_length,
1367 CHECK);
1368 parsed_annotations->set_field_type_annotations(a);
1369 return;
1370 }
1371
1372
1373 // Side-effects: populates the _fields, _fields_annotations,
1374 // _fields_type_annotations fields
1375 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1376 bool is_interface,
1377 ConstantPool* cp,
1378 const int cp_size,
1379 u2* const java_fields_count_ptr,
1380 TRAPS) {
1381
1382 assert(cfs != nullptr, "invariant");
1383 assert(cp != nullptr, "invariant");
1384 assert(java_fields_count_ptr != nullptr, "invariant");
1385
1386 assert(nullptr == _fields_annotations, "invariant");
1387 assert(nullptr == _fields_type_annotations, "invariant");
1388
1389 cfs->guarantee_more(2, CHECK); // length
1390 const u2 length = cfs->get_u2_fast();
1391 *java_fields_count_ptr = length;
1392
1393 int num_injected = 0;
1394 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1395 &num_injected);
1396 const int total_fields = length + num_injected;
1397
1398 // Allocate a temporary resource array to collect field data.
1399 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1400 _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1401
1402 ResourceMark rm(THREAD);
1403 for (int n = 0; n < length; n++) {
1404 // access_flags, name_index, descriptor_index, attributes_count
1405 cfs->guarantee_more(8, CHECK);
1406
1407 AccessFlags access_flags;
1408 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1409 verify_legal_field_modifiers(flags, is_interface, CHECK);
1410 access_flags.set_flags(flags);
1411 FieldInfo::FieldFlags fieldFlags(0);
1412
1413 const u2 name_index = cfs->get_u2_fast();
1414 guarantee_property(valid_symbol_at(name_index),
1415 "Invalid constant pool index %u for field name in class file %s",
1416 name_index, CHECK);
1417 const Symbol* const name = cp->symbol_at(name_index);
1418 verify_legal_field_name(name, CHECK);
1419
1420 const u2 signature_index = cfs->get_u2_fast();
1421 guarantee_property(valid_symbol_at(signature_index),
1422 "Invalid constant pool index %u for field signature in class file %s",
1423 signature_index, CHECK);
1424 const Symbol* const sig = cp->symbol_at(signature_index);
1425 verify_legal_field_signature(name, sig, CHECK);
1426
1427 u2 constantvalue_index = 0;
1428 bool is_synthetic = false;
1429 u2 generic_signature_index = 0;
1430 const bool is_static = access_flags.is_static();
1431 FieldAnnotationCollector parsed_annotations(_loader_data);
1432
1433 const u2 attributes_count = cfs->get_u2_fast();
1434 if (attributes_count > 0) {
1435 parse_field_attributes(cfs,
1436 attributes_count,
1437 is_static,
1438 signature_index,
1439 &constantvalue_index,
1440 &is_synthetic,
1441 &generic_signature_index,
1442 &parsed_annotations,
1443 CHECK);
1444
1445 if (parsed_annotations.field_annotations() != nullptr) {
1446 if (_fields_annotations == nullptr) {
1447 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1448 _loader_data, length, nullptr,
1449 CHECK);
1450 }
1451 _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1452 parsed_annotations.set_field_annotations(nullptr);
1453 }
1454 if (parsed_annotations.field_type_annotations() != nullptr) {
1455 if (_fields_type_annotations == nullptr) {
1456 _fields_type_annotations =
1457 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1458 length,
1459 nullptr,
1460 CHECK);
1461 }
1462 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1463 parsed_annotations.set_field_type_annotations(nullptr);
1464 }
1465
1466 if (is_synthetic) {
1467 access_flags.set_is_synthetic();
1468 }
1469 if (generic_signature_index != 0) {
1470 fieldFlags.update_generic(true);
1471 }
1472 }
1473
1474 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1475
1476 // Update number of static oop fields.
1477 if (is_static && is_reference_type(type)) {
1478 _static_oop_count++;
1479 }
1480
1481 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1482 fi.set_index(n);
1483 if (fieldFlags.is_generic()) {
1484 fi.set_generic_signature_index(generic_signature_index);
1485 }
1486 parsed_annotations.apply_to(&fi);
1487 if (fi.field_flags().is_contended()) {
1488 _has_contended_fields = true;
1489 }
1490 _temp_field_info->append(fi);
1491 }
1492 assert(_temp_field_info->length() == length, "Must be");
1493
1494 int index = length;
1495 if (num_injected != 0) {
1496 for (int n = 0; n < num_injected; n++) {
1497 // Check for duplicates
1498 if (injected[n].may_be_java) {
1499 const Symbol* const name = injected[n].name();
1500 const Symbol* const signature = injected[n].signature();
1501 bool duplicate = false;
1502 for (int i = 0; i < length; i++) {
1503 const FieldInfo* const f = _temp_field_info->adr_at(i);
1504 if (name == cp->symbol_at(f->name_index()) &&
1505 signature == cp->symbol_at(f->signature_index())) {
1506 // Symbol is desclared in Java so skip this one
1507 duplicate = true;
1508 break;
1509 }
1510 }
1511 if (duplicate) {
1512 // These will be removed from the field array at the end
1513 continue;
1514 }
1515 }
1516
1517 // Injected field
1518 FieldInfo::FieldFlags fflags(0);
1519 fflags.update_injected(true);
1520 AccessFlags aflags;
1521 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1522 fi.set_index(index);
1523 _temp_field_info->append(fi);
1524 index++;
1525 }
1526 }
1527
1528 assert(_temp_field_info->length() == index, "Must be");
1529
1530 if (_need_verify && length > 1) {
1531 // Check duplicated fields
1532 ResourceMark rm(THREAD);
1533 // Set containing name-signature pairs
1534 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1535 for (int i = 0; i < _temp_field_info->length(); i++) {
1536 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1537 _temp_field_info->adr_at(i)->signature(_cp));
1538 // If no duplicates, add name/signature in hashtable names_and_sigs.
1539 if(!names_and_sigs->put(name_and_sig, 0)) {
1540 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1541 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1542 return;
1543 }
1544 }
1545 }
1546 }
1547
1548
1893 }
1894 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
1895 if (_location != _in_field && _location != _in_class) {
1896 break; // only allow for fields and classes
1897 }
1898 if (!EnableContended || (RestrictContended && !privileged)) {
1899 break; // honor privileges
1900 }
1901 return _jdk_internal_vm_annotation_Contended;
1902 }
1903 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
1904 if (_location != _in_method) break; // only allow for methods
1905 if (RestrictReservedStack && !privileged) break; // honor privileges
1906 return _jdk_internal_vm_annotation_ReservedStackAccess;
1907 }
1908 case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
1909 if (_location != _in_class) break; // only allow for classes
1910 if (!privileged) break; // only allow in privileged code
1911 return _jdk_internal_ValueBased;
1912 }
1913 case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
1914 return _java_lang_Deprecated;
1915 }
1916 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_AOTSafeClassInitializer_signature): {
1917 if (_location != _in_class) break; // only allow for classes
1918 if (!privileged) break; // only allow in privileged code
1919 return _jdk_internal_vm_annotation_AOTSafeClassInitializer;
1920 }
1921 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_AOTRuntimeSetup_signature): {
1922 if (_location != _in_method) break; // only allow for methods
1923 if (!privileged) break; // only allow in privileged code
1924 return _method_AOTRuntimeSetup;
1925 }
1926 default: {
1927 break;
1928 }
1929 }
1930 return AnnotationCollector::_unknown;
1931 }
1932
2132 }
2133
2134 if (runtime_visible_type_annotations_length > 0) {
2135 a = allocate_annotations(runtime_visible_type_annotations,
2136 runtime_visible_type_annotations_length,
2137 CHECK);
2138 cm->set_type_annotations(a);
2139 }
2140 }
2141
2142
2143 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2144 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2145 // Method* to save footprint, so we only know the size of the resulting Method* when the
2146 // entire method attribute is parsed.
2147 //
2148 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2149
2150 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2151 bool is_interface,
2152 const ConstantPool* cp,
2153 bool* const has_localvariable_table,
2154 TRAPS) {
2155 assert(cfs != nullptr, "invariant");
2156 assert(cp != nullptr, "invariant");
2157 assert(has_localvariable_table != nullptr, "invariant");
2158
2159 ResourceMark rm(THREAD);
2160 // Parse fixed parts:
2161 // access_flags, name_index, descriptor_index, attributes_count
2162 cfs->guarantee_more(8, CHECK_NULL);
2163
2164 u2 flags = cfs->get_u2_fast();
2165 const u2 name_index = cfs->get_u2_fast();
2166 const int cp_size = cp->length();
2167 guarantee_property(
2168 valid_symbol_at(name_index),
2169 "Illegal constant pool index %u for method name in class file %s",
2170 name_index, CHECK_NULL);
2171 const Symbol* const name = cp->symbol_at(name_index);
2173
2174 const u2 signature_index = cfs->get_u2_fast();
2175 guarantee_property(
2176 valid_symbol_at(signature_index),
2177 "Illegal constant pool index %u for method signature in class file %s",
2178 signature_index, CHECK_NULL);
2179 const Symbol* const signature = cp->symbol_at(signature_index);
2180
2181 if (name == vmSymbols::class_initializer_name()) {
2182 // We ignore the other access flags for a valid class initializer.
2183 // (JVM Spec 2nd ed., chapter 4.6)
2184 if (_major_version < 51) { // backward compatibility
2185 flags = JVM_ACC_STATIC;
2186 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2187 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2188 } else {
2189 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2190 return nullptr;
2191 }
2192 } else {
2193 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2194 }
2195
2196 if (name == vmSymbols::object_initializer_name() && is_interface) {
2197 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2198 return nullptr;
2199 }
2200
2201 int args_size = -1; // only used when _need_verify is true
2202 if (_need_verify) {
2203 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2204 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2205 verify_legal_method_signature(name, signature, CHECK_NULL);
2206 if (args_size > MAX_ARGS_SIZE) {
2207 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2208 return nullptr;
2209 }
2210 }
2211
2212 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2213
2214 // Default values for code and exceptions attribute elements
2215 u2 max_stack = 0;
2216 u2 max_locals = 0;
2217 u4 code_length = 0;
2218 const u1* code_start = nullptr;
2219 u2 exception_table_length = 0;
2220 const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements
2699 !access_flags.is_private() || !access_flags.is_static()) {
2700 classfile_parse_error("@AOTRuntimeSetup method must be declared private static void runtimeSetup() for class %s", CHECK_NULL);
2701 }
2702 _has_aot_runtime_setup_method = true;
2703 }
2704
2705 // Copy annotations
2706 copy_method_annotations(m->constMethod(),
2707 runtime_visible_annotations,
2708 runtime_visible_annotations_length,
2709 runtime_visible_parameter_annotations,
2710 runtime_visible_parameter_annotations_length,
2711 runtime_visible_type_annotations,
2712 runtime_visible_type_annotations_length,
2713 annotation_default,
2714 annotation_default_length,
2715 CHECK_NULL);
2716
2717 if (InstanceKlass::is_finalization_enabled() &&
2718 name == vmSymbols::finalize_method_name() &&
2719 signature == vmSymbols::void_method_signature()) {
2720 if (m->is_empty_method()) {
2721 _has_empty_finalizer = true;
2722 } else {
2723 _has_finalizer = true;
2724 }
2725 }
2726
2727 NOT_PRODUCT(m->verify());
2728 return m;
2729 }
2730
2731
2732 // Side-effects: populates the _methods field in the parser
2733 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2734 bool is_interface,
2735 bool* const has_localvariable_table,
2736 bool* has_final_method,
2737 bool* declares_nonstatic_concrete_methods,
2738 TRAPS) {
2739 assert(cfs != nullptr, "invariant");
2740 assert(has_localvariable_table != nullptr, "invariant");
2741 assert(has_final_method != nullptr, "invariant");
2742 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2743
2744 assert(nullptr == _methods, "invariant");
2745
2746 cfs->guarantee_more(2, CHECK); // length
2747 const u2 length = cfs->get_u2_fast();
2748 if (length == 0) {
2749 _methods = Universe::the_empty_method_array();
2750 } else {
2751 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2752 length,
2753 nullptr,
2754 CHECK);
2755
2756 for (int index = 0; index < length; index++) {
2757 Method* method = parse_method(cfs,
2758 is_interface,
2759 _cp,
2760 has_localvariable_table,
2761 CHECK);
2762
2763 if (method->is_final()) {
2764 *has_final_method = true;
2765 }
2766 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2767 // used for interface initialization, and default method inheritance analysis
2768 if (is_interface && !(*declares_nonstatic_concrete_methods)
2769 && !method->is_abstract() && !method->is_static()) {
2770 *declares_nonstatic_concrete_methods = true;
2771 }
2772 _methods->at_put(index, method);
2773 }
2774
2775 if (_need_verify && length > 1) {
2776 // Check duplicated methods
2777 ResourceMark rm(THREAD);
2778 // Set containing name-signature pairs
3004 valid_klass_reference_at(outer_class_info_index),
3005 "outer_class_info_index %u has bad constant type in class file %s",
3006 outer_class_info_index, CHECK_0);
3007
3008 if (outer_class_info_index != 0) {
3009 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3010 char* bytes = (char*)outer_class_name->bytes();
3011 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3012 "Outer class is an array class in class file %s", CHECK_0);
3013 }
3014 // Inner class name
3015 const u2 inner_name_index = cfs->get_u2_fast();
3016 guarantee_property(
3017 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3018 "inner_name_index %u has bad constant type in class file %s",
3019 inner_name_index, CHECK_0);
3020 if (_need_verify) {
3021 guarantee_property(inner_class_info_index != outer_class_info_index,
3022 "Class is both outer and inner class in class file %s", CHECK_0);
3023 }
3024 // Access flags
3025 u2 flags;
3026 // JVM_ACC_MODULE is defined in JDK-9 and later.
3027 if (_major_version >= JAVA_9_VERSION) {
3028 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3029 } else {
3030 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3031 }
3032 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3033 // Set abstract bit for old class files for backward compatibility
3034 flags |= JVM_ACC_ABSTRACT;
3035 }
3036
3037 Symbol* inner_name_symbol = inner_name_index == 0 ? nullptr : cp->symbol_at(inner_name_index);
3038 verify_legal_class_modifiers(flags, inner_name_symbol, inner_name_index == 0, CHECK_0);
3039 AccessFlags inner_access_flags(flags);
3040
3041 inner_classes->at_put(index++, inner_class_info_index);
3042 inner_classes->at_put(index++, outer_class_info_index);
3043 inner_classes->at_put(index++, inner_name_index);
3044 inner_classes->at_put(index++, inner_access_flags.as_unsigned_short());
3045 }
3046
3047 // Check for circular and duplicate entries.
3048 bool has_circularity = false;
3049 if (_need_verify) {
3050 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3051 if (has_circularity) {
3052 // If circularity check failed then ignore InnerClasses attribute.
3053 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3054 index = 0;
3055 if (parsed_enclosingmethod_attribute) {
3056 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3122 if (length > 0) {
3123 int index = 0;
3124 cfs->guarantee_more(2 * length, CHECK_0);
3125 for (int n = 0; n < length; n++) {
3126 const u2 class_info_index = cfs->get_u2_fast();
3127 guarantee_property(
3128 valid_klass_reference_at(class_info_index),
3129 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3130 class_info_index, CHECK_0);
3131 permitted_subclasses->at_put(index++, class_info_index);
3132 }
3133 assert(index == size, "wrong size");
3134 }
3135
3136 // Restore buffer's current position.
3137 cfs->set_current(current_mark);
3138
3139 return length;
3140 }
3141
3142 // Record {
3143 // u2 attribute_name_index;
3144 // u4 attribute_length;
3145 // u2 components_count;
3146 // component_info components[components_count];
3147 // }
3148 // component_info {
3149 // u2 name_index;
3150 // u2 descriptor_index
3151 // u2 attributes_count;
3152 // attribute_info_attributes[attributes_count];
3153 // }
3154 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3155 const ConstantPool* cp,
3156 const u1* const record_attribute_start,
3157 TRAPS) {
3158 const u1* const current_mark = cfs->current();
3159 int components_count = 0;
3160 unsigned int calculate_attr_size = 0;
3161 if (record_attribute_start != nullptr) {
3372 cp->bsm_entries().end_extension(iter, _loader_data, CHECK);
3373 guarantee_property(current_before_parsing + attribute_byte_length == cfs->current(),
3374 "Bad length on BootstrapMethods in class file %s",
3375 CHECK);
3376 }
3377
3378 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3379 ConstantPool* cp,
3380 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3381 TRAPS) {
3382 assert(cfs != nullptr, "invariant");
3383 assert(cp != nullptr, "invariant");
3384 assert(parsed_annotations != nullptr, "invariant");
3385
3386 // Set inner classes attribute to default sentinel
3387 _inner_classes = Universe::the_empty_short_array();
3388 // Set nest members attribute to default sentinel
3389 _nest_members = Universe::the_empty_short_array();
3390 // Set _permitted_subclasses attribute to default sentinel
3391 _permitted_subclasses = Universe::the_empty_short_array();
3392 cfs->guarantee_more(2, CHECK); // attributes_count
3393 u2 attributes_count = cfs->get_u2_fast();
3394 bool parsed_sourcefile_attribute = false;
3395 bool parsed_innerclasses_attribute = false;
3396 bool parsed_nest_members_attribute = false;
3397 bool parsed_permitted_subclasses_attribute = false;
3398 bool parsed_nest_host_attribute = false;
3399 bool parsed_record_attribute = false;
3400 bool parsed_enclosingmethod_attribute = false;
3401 bool parsed_bootstrap_methods_attribute = false;
3402 const u1* runtime_visible_annotations = nullptr;
3403 int runtime_visible_annotations_length = 0;
3404 const u1* runtime_visible_type_annotations = nullptr;
3405 int runtime_visible_type_annotations_length = 0;
3406 bool runtime_invisible_type_annotations_exists = false;
3407 bool runtime_invisible_annotations_exists = false;
3408 bool parsed_source_debug_ext_annotations_exist = false;
3409 const u1* inner_classes_attribute_start = nullptr;
3410 u4 inner_classes_attribute_length = 0;
3411 u2 enclosing_method_class_index = 0;
3412 u2 enclosing_method_method_index = 0;
3413 const u1* nest_members_attribute_start = nullptr;
3414 u4 nest_members_attribute_length = 0;
3415 const u1* record_attribute_start = nullptr;
3416 u4 record_attribute_length = 0;
3417 const u1* permitted_subclasses_attribute_start = nullptr;
3418 u4 permitted_subclasses_attribute_length = 0;
3419
3420 // Iterate over attributes
3421 while (attributes_count--) {
3422 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3423 const u2 attribute_name_index = cfs->get_u2_fast();
3424 const u4 attribute_length = cfs->get_u4_fast();
3425 guarantee_property(
3426 valid_symbol_at(attribute_name_index),
3427 "Attribute name has bad constant pool index %u in class file %s",
3428 attribute_name_index, CHECK);
3429 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3430 if (tag == vmSymbols::tag_source_file()) {
3431 // Check for SourceFile tag
3432 if (_need_verify) {
3433 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3434 }
3435 if (parsed_sourcefile_attribute) {
3436 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3437 return;
3438 } else {
3614 return;
3615 }
3616 parsed_record_attribute = true;
3617 record_attribute_start = cfs->current();
3618 record_attribute_length = attribute_length;
3619 } else if (_major_version >= JAVA_17_VERSION) {
3620 if (tag == vmSymbols::tag_permitted_subclasses()) {
3621 if (parsed_permitted_subclasses_attribute) {
3622 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3623 return;
3624 }
3625 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3626 if (_access_flags.is_final()) {
3627 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3628 return;
3629 }
3630 parsed_permitted_subclasses_attribute = true;
3631 permitted_subclasses_attribute_start = cfs->current();
3632 permitted_subclasses_attribute_length = attribute_length;
3633 }
3634 }
3635 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3636 cfs->skip_u1(attribute_length, CHECK);
3637 } else {
3638 // Unknown attribute
3639 cfs->skip_u1(attribute_length, CHECK);
3640 }
3641 } else {
3642 // Unknown attribute
3643 cfs->skip_u1(attribute_length, CHECK);
3644 }
3645 } else {
3646 // Unknown attribute
3647 cfs->skip_u1(attribute_length, CHECK);
3648 }
3649 }
3650 _class_annotations = allocate_annotations(runtime_visible_annotations,
3651 runtime_visible_annotations_length,
3652 CHECK);
3653 _class_type_annotations = allocate_annotations(runtime_visible_type_annotations,
3690 CHECK);
3691 if (_need_verify) {
3692 guarantee_property(record_attribute_length == calculated_attr_length,
3693 "Record attribute has wrong length in class file %s",
3694 CHECK);
3695 }
3696 }
3697
3698 if (parsed_permitted_subclasses_attribute) {
3699 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3700 cfs,
3701 permitted_subclasses_attribute_start,
3702 CHECK);
3703 if (_need_verify) {
3704 guarantee_property(
3705 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3706 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3707 }
3708 }
3709
3710 if (_max_bootstrap_specifier_index >= 0) {
3711 guarantee_property(parsed_bootstrap_methods_attribute,
3712 "Missing BootstrapMethods attribute in class file %s", CHECK);
3713 }
3714 }
3715
3716 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3717 assert(k != nullptr, "invariant");
3718
3719 if (_synthetic_flag)
3720 k->set_is_synthetic();
3721 if (_sourcefile_index != 0) {
3722 k->set_source_file_name_index(_sourcefile_index);
3723 }
3724 if (_generic_signature_index != 0) {
3725 k->set_generic_signature_index(_generic_signature_index);
3726 }
3727 if (_sde_buffer != nullptr) {
3728 k->set_source_debug_extension(_sde_buffer, _sde_length);
3729 }
3756 _class_type_annotations = nullptr;
3757 _fields_annotations = nullptr;
3758 _fields_type_annotations = nullptr;
3759 }
3760
3761 // Transfer ownership of metadata allocated to the InstanceKlass.
3762 void ClassFileParser::apply_parsed_class_metadata(
3763 InstanceKlass* this_klass,
3764 int java_fields_count) {
3765 assert(this_klass != nullptr, "invariant");
3766
3767 _cp->set_pool_holder(this_klass);
3768 this_klass->set_constants(_cp);
3769 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3770 this_klass->set_fieldinfo_search_table(_fieldinfo_search_table);
3771 this_klass->set_fields_status(_fields_status);
3772 this_klass->set_methods(_methods);
3773 this_klass->set_inner_classes(_inner_classes);
3774 this_klass->set_nest_members(_nest_members);
3775 this_klass->set_nest_host_index(_nest_host);
3776 this_klass->set_annotations(_combined_annotations);
3777 this_klass->set_permitted_subclasses(_permitted_subclasses);
3778 this_klass->set_record_components(_record_components);
3779
3780 DEBUG_ONLY(FieldInfoStream::validate_search_table(_cp, _fieldinfo_stream, _fieldinfo_search_table));
3781
3782 // Delay the setting of _local_interfaces and _transitive_interfaces until after
3783 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3784 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3785 // its _super. If an OOM occurs while loading the current klass, its _super field
3786 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3787 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3788 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3789
3790 // Clear out these fields so they don't get deallocated by the destructor
3791 clear_class_metadata();
3792 }
3793
3794 AnnotationArray* ClassFileParser::allocate_annotations(const u1* const anno,
3795 int anno_length,
3796 TRAPS) {
3797 AnnotationArray* annotations = nullptr;
3798 if (anno != nullptr) {
3799 annotations = MetadataFactory::new_array<u1>(_loader_data,
3800 anno_length,
3801 CHECK_(annotations));
3802 for (int i = 0; i < anno_length; i++) {
3803 annotations->at_put(i, anno[i]);
3804 }
3805 }
3806 return annotations;
3807 }
3808
3809 void ClassFileParser::check_super_class(ConstantPool* const cp,
3810 const int super_class_index,
3811 const bool need_verify,
3812 TRAPS) {
3813 assert(cp != nullptr, "invariant");
3814
3815 if (super_class_index == 0) {
3816 guarantee_property(_class_name == vmSymbols::java_lang_Object(),
3817 "Invalid superclass index %u in class file %s",
3818 super_class_index,
3819 CHECK);
3820 } else {
3821 guarantee_property(valid_klass_reference_at(super_class_index),
3822 "Invalid superclass index %u in class file %s",
3823 super_class_index,
3824 CHECK);
3825
3826 // The class name should be legal because it is checked when parsing constant pool.
3827 // However, make sure it is not an array type.
3828 if (need_verify) {
3829 guarantee_property(cp->klass_name_at(super_class_index)->char_at(0) != JVM_SIGNATURE_ARRAY,
3830 "Bad superclass name in class file %s", CHECK);
3831 }
3832 }
3833 }
3834
3835 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
3836 _max_nonstatic_oop_maps = max_blocks;
3837 _nonstatic_oop_map_count = 0;
3838 if (max_blocks == 0) {
3950
3951 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
3952 assert(ik != nullptr, "invariant");
3953
3954 const InstanceKlass* const super = ik->super();
3955
3956 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
3957 // in which case we don't have to register objects as finalizable
3958 if (!_has_empty_finalizer) {
3959 if (_has_finalizer ||
3960 (super != nullptr && super->has_finalizer())) {
3961 ik->set_has_finalizer();
3962 }
3963 }
3964
3965 #ifdef ASSERT
3966 bool f = false;
3967 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
3968 vmSymbols::void_method_signature());
3969 if (InstanceKlass::is_finalization_enabled() &&
3970 (m != nullptr) && !m->is_empty_method()) {
3971 f = true;
3972 }
3973
3974 // Spec doesn't prevent agent from redefinition of empty finalizer.
3975 // Despite the fact that it's generally bad idea and redefined finalizer
3976 // will not work as expected we shouldn't abort vm in this case
3977 if (!ik->has_redefined_this_or_super()) {
3978 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
3979 }
3980 #endif
3981
3982 // Check if this klass supports the java.lang.Cloneable interface
3983 if (vmClasses::Cloneable_klass_is_loaded()) {
3984 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
3985 ik->set_is_cloneable();
3986 }
3987 }
3988
3989 // If it cannot be fast-path allocated, set a bit in the layout helper.
3990 // See documentation of InstanceKlass::can_be_fastpath_allocated().
3991 assert(ik->size_helper() > 0, "layout_helper is initialized");
3992 if (ik->is_abstract() || ik->is_interface()
3993 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
3994 || ik->size_helper() >= FastAllocateSizeLimit) {
3995 // Forbid fast-path allocation.
3996 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
3997 ik->set_layout_helper(lh);
3998 }
3999
4000 // Propagate the AOT runtimeSetup method discovery
4001 if (_has_aot_runtime_setup_method) {
4002 ik->set_is_runtime_setup_required();
4003 if (log_is_enabled(Info, aot, init)) {
4004 ResourceMark rm;
4005 log_info(aot, init)("Found @AOTRuntimeSetup class %s", ik->external_name());
4006 }
4007 }
4008 }
4009
4010 // utility methods for appending an array with check for duplicates
4011
4012 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4013 const Array<InstanceKlass*>* const ifs) {
4014 // iterate over new interfaces
4015 for (int i = 0; i < ifs->length(); i++) {
4016 InstanceKlass* const e = ifs->at(i);
4017 assert(e->is_klass() && e->is_interface(), "just checking");
4018 // add new interface
4019 result->append_if_missing(e);
4020 }
4021 }
4022
4023 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4024 Array<InstanceKlass*>* local_ifs,
4025 ClassLoaderData* loader_data,
4026 TRAPS) {
4027 assert(local_ifs != nullptr, "invariant");
4028 assert(loader_data != nullptr, "invariant");
4029
4086
4087 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4088 assert(this_klass != nullptr, "invariant");
4089 const InstanceKlass* const super = this_klass->super();
4090
4091 if (super != nullptr) {
4092 if (super->is_final()) {
4093 classfile_icce_error("class %s cannot inherit from final class %s", super, THREAD);
4094 return;
4095 }
4096
4097 if (super->is_sealed()) {
4098 stringStream ss;
4099 ResourceMark rm(THREAD);
4100 if (!super->has_as_permitted_subclass(this_klass, ss)) {
4101 classfile_icce_error(ss.as_string(), THREAD);
4102 return;
4103 }
4104 }
4105
4106 Reflection::VerifyClassAccessResults vca_result =
4107 Reflection::verify_class_access(this_klass, super, false);
4108 if (vca_result != Reflection::ACCESS_OK) {
4109 ResourceMark rm(THREAD);
4110 char* msg = Reflection::verify_class_access_msg(this_klass,
4111 super,
4112 vca_result);
4113
4114 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4115 if (msg == nullptr) {
4116 bool same_module = (this_klass->module() == super->module());
4117 Exceptions::fthrow(
4118 THREAD_AND_LOCATION,
4119 vmSymbols::java_lang_IllegalAccessError(),
4120 "class %s cannot access its %ssuperclass %s (%s%s%s)",
4121 this_klass->external_name(),
4122 super->is_abstract() ? "abstract " : "",
4123 super->external_name(),
4124 (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),
4125 (same_module) ? "" : "; ",
4278 // Verify the class modifiers for the current class, or an inner class if inner_name is non-null.
4279 void ClassFileParser::verify_legal_class_modifiers(jint flags, Symbol* inner_name, bool is_anonymous_inner_class, TRAPS) const {
4280 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4281 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4282 if (is_module) {
4283 ResourceMark rm(THREAD);
4284 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4285 Exceptions::fthrow(
4286 THREAD_AND_LOCATION,
4287 vmSymbols::java_lang_NoClassDefFoundError(),
4288 "%s is not a class because access_flag ACC_MODULE is set",
4289 _class_name->as_C_string());
4290 return;
4291 }
4292
4293 if (!_need_verify) { return; }
4294
4295 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4296 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4297 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4298 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4299 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4300 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4301 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4302
4303 if ((is_abstract && is_final) ||
4304 (is_interface && !is_abstract) ||
4305 (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4306 (!is_interface && major_gte_1_5 && is_annotation)) {
4307 ResourceMark rm(THREAD);
4308 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4309 if (inner_name == nullptr && !is_anonymous_inner_class) {
4310 Exceptions::fthrow(
4311 THREAD_AND_LOCATION,
4312 vmSymbols::java_lang_ClassFormatError(),
4313 "Illegal class modifiers in class %s: 0x%X",
4314 _class_name->as_C_string(), flags
4315 );
4316 } else {
4317 if (is_anonymous_inner_class) {
4318 Exceptions::fthrow(
4319 THREAD_AND_LOCATION,
4320 vmSymbols::java_lang_ClassFormatError(),
4321 "Illegal class modifiers in anonymous inner class of class %s: 0x%X",
4322 _class_name->as_C_string(), flags
4323 );
4324 } else {
4325 Exceptions::fthrow(
4326 THREAD_AND_LOCATION,
4381 THREAD_AND_LOCATION,
4382 vmSymbols::java_lang_UnsupportedClassVersionError(),
4383 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4384 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4385 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4386 return;
4387 }
4388
4389 if (!Arguments::enable_preview()) {
4390 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4391 class_name, major, minor, THREAD);
4392 return;
4393 }
4394
4395 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4396 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4397 class_name, major, minor, THREAD);
4398 }
4399 }
4400
4401 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4402 bool is_interface,
4403 TRAPS) const {
4404 if (!_need_verify) { return; }
4405
4406 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4407 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4408 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4409 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4410 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4411 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4412 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4413 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4414 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4415
4416 bool is_illegal = false;
4417
4418 if (is_interface) {
4419 if (!is_public || !is_static || !is_final || is_private ||
4420 is_protected || is_volatile || is_transient ||
4421 (major_gte_1_5 && is_enum)) {
4422 is_illegal = true;
4423 }
4424 } else { // not interface
4425 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4426 is_illegal = true;
4427 }
4428 }
4429
4430 if (is_illegal) {
4431 ResourceMark rm(THREAD);
4432 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4433 Exceptions::fthrow(
4434 THREAD_AND_LOCATION,
4435 vmSymbols::java_lang_ClassFormatError(),
4436 "Illegal field modifiers in class %s: 0x%X",
4437 _class_name->as_C_string(), flags);
4438 return;
4439 }
4440 }
4441
4442 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4443 bool is_interface,
4444 const Symbol* name,
4445 TRAPS) const {
4446 if (!_need_verify) { return; }
4447
4448 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4449 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4450 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4451 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4452 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4453 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4454 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4455 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4456 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4457 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4458 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4459 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4460 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4461 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4462
4463 bool is_illegal = false;
4464
4465 if (is_interface) {
4466 if (major_gte_8) {
4467 // Class file version is JAVA_8_VERSION or later Methods of
4468 // interfaces may set any of the flags except ACC_PROTECTED,
4469 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4470 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4471 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4472 (is_native || is_protected || is_final || is_synchronized) ||
4473 // If a specific method of a class or interface has its
4474 // ACC_ABSTRACT flag set, it must not have any of its
4475 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4476 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4477 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4478 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4479 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4480 is_illegal = true;
4481 }
4482 } else if (major_gte_1_5) {
4483 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4484 if (!is_public || is_private || is_protected || is_static || is_final ||
4485 is_synchronized || is_native || !is_abstract || is_strict) {
4486 is_illegal = true;
4487 }
4488 } else {
4489 // Class file version is pre-JAVA_1_5_VERSION
4490 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4491 is_illegal = true;
4492 }
4493 }
4494 } else { // not interface
4495 if (has_illegal_visibility(flags)) {
4496 is_illegal = true;
4497 } else {
4498 if (is_initializer) {
4499 if (is_static || is_final || is_synchronized || is_native ||
4500 is_abstract || (major_gte_1_5 && is_bridge)) {
4501 is_illegal = true;
4502 }
4503 } else { // not initializer
4504 if (is_abstract) {
4505 if ((is_final || is_native || is_private || is_static ||
4506 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4507 is_illegal = true;
4508 }
4509 }
4510 }
4511 }
4512 }
4513
4514 if (is_illegal) {
4515 ResourceMark rm(THREAD);
4516 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4517 Exceptions::fthrow(
4518 THREAD_AND_LOCATION,
4519 vmSymbols::java_lang_ClassFormatError(),
4520 "Method %s in class %s has illegal modifiers: 0x%X",
4521 name->as_C_string(), _class_name->as_C_string(), flags);
4522 return;
4523 }
4524 }
4525
4526 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4527 int length,
4528 TRAPS) const {
4529 assert(_need_verify, "only called when _need_verify is true");
4530 // Note: 0 <= length < 64K, as it comes from a u2 entry in the CP.
4531 if (!UTF8::is_legal_utf8(buffer, static_cast<size_t>(length), _major_version <= 47)) {
4532 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4533 }
4534 }
4535
4536 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4537 // In class names, '/' separates unqualified names. This is verified in this function also.
4538 // Method names also may not contain the characters '<' or '>', unless <init>
4539 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4540 // method. Because these names have been checked as special cases before
4541 // calling this method in verify_legal_method_name.
4559 if (type == ClassFileParser::LegalClass) {
4560 if (p == name || p+1 >= name+length ||
4561 *(p+1) == JVM_SIGNATURE_SLASH) {
4562 return false;
4563 }
4564 } else {
4565 return false; // do not permit '/' unless it's class name
4566 }
4567 break;
4568 case JVM_SIGNATURE_SPECIAL:
4569 case JVM_SIGNATURE_ENDSPECIAL:
4570 // do not permit '<' or '>' in method names
4571 if (type == ClassFileParser::LegalMethod) {
4572 return false;
4573 }
4574 }
4575 }
4576 return true;
4577 }
4578
4579 // Take pointer to a UTF8 byte string (not NUL-terminated).
4580 // Skip over the longest part of the string that could
4581 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
4582 // Return a pointer to just past the fieldname.
4583 // Return null if no fieldname at all was found, or in the case of slash_ok
4584 // being true, we saw consecutive slashes (meaning we were looking for a
4585 // qualified path but found something that was badly-formed).
4586 static const char* skip_over_field_name(const char* const name,
4587 bool slash_ok,
4588 unsigned int length) {
4589 const char* p;
4590 jboolean last_is_slash = false;
4591 jboolean not_first_ch = false;
4592
4593 for (p = name; p != name + length; not_first_ch = true) {
4594 const char* old_p = p;
4595 jchar ch = *p;
4596 if (ch < 128) {
4597 p++;
4598 // quick check for ascii
4825 } else {
4826 // 4881221: relax the constraints based on JSR202 spec
4827 legal = verify_unqualified_name(bytes, length, LegalMethod);
4828 }
4829 }
4830
4831 if (!legal) {
4832 ResourceMark rm(THREAD);
4833 assert(_class_name != nullptr, "invariant");
4834 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4835 Exceptions::fthrow(
4836 THREAD_AND_LOCATION,
4837 vmSymbols::java_lang_ClassFormatError(),
4838 "Illegal method name \"%.*s\" in class %s", length, bytes,
4839 _class_name->as_C_string()
4840 );
4841 return;
4842 }
4843 }
4844
4845
4846 // Checks if signature is a legal field signature.
4847 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
4848 const Symbol* signature,
4849 TRAPS) const {
4850 if (!_need_verify) { return; }
4851
4852 const char* const bytes = (const char*)signature->bytes();
4853 const unsigned int length = signature->utf8_length();
4854 const char* const p = skip_over_field_signature(bytes, false, length, CHECK);
4855
4856 if (p == nullptr || (p - bytes) != (int)length) {
4857 throwIllegalSignature("Field", name, signature, CHECK);
4858 }
4859 }
4860
4861 // Check that the signature is compatible with the method name. For example,
4862 // check that <init> has a void signature.
4863 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
4864 const Symbol* signature,
4865 TRAPS) const {
4866 if (!_need_verify) {
4867 return;
4868 }
4869
4870 // Class initializers cannot have args for class format version >= 51.
4871 if (name == vmSymbols::class_initializer_name() &&
4872 signature != vmSymbols::void_method_signature() &&
4873 _major_version >= JAVA_7_VERSION) {
4874 throwIllegalSignature("Method", name, signature, THREAD);
4875 return;
4876 }
4913 length -= pointer_delta_as_int(nextp, p);
4914 p = nextp;
4915 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4916 }
4917 // The first non-signature thing better be a ')'
4918 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
4919 length--;
4920 // Now we better just have a return value
4921 nextp = skip_over_field_signature(p, true, length, CHECK_0);
4922 if (nextp && ((int)length == (nextp - p))) {
4923 return args_size;
4924 }
4925 }
4926 }
4927 // Report error
4928 throwIllegalSignature("Method", name, signature, THREAD);
4929 return 0;
4930 }
4931
4932 int ClassFileParser::static_field_size() const {
4933 assert(_field_info != nullptr, "invariant");
4934 return _field_info->_static_field_size;
4935 }
4936
4937 int ClassFileParser::total_oop_map_count() const {
4938 assert(_field_info != nullptr, "invariant");
4939 return _field_info->oop_map_blocks->_nonstatic_oop_map_count;
4940 }
4941
4942 jint ClassFileParser::layout_size() const {
4943 assert(_field_info != nullptr, "invariant");
4944 return _field_info->_instance_size;
4945 }
4946
4947 static void check_methods_for_intrinsics(const InstanceKlass* ik,
4948 const Array<Method*>* methods) {
4949 assert(ik != nullptr, "invariant");
4950 assert(methods != nullptr, "invariant");
4951
4952 // Set up Method*::intrinsic_id as soon as we know the names of methods.
4953 // (We used to do this lazily, but now we query it in Rewriter,
4954 // which is eagerly done for every method, so we might as well do it now,
4955 // when everything is fresh in memory.)
4956 const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);
4957
4958 if (klass_id != vmSymbolID::NO_SID) {
4959 for (int j = 0; j < methods->length(); ++j) {
4960 Method* method = methods->at(j);
4961 method->init_intrinsic_id(klass_id);
4962
4963 if (CheckIntrinsics) {
4964 // Check if an intrinsic is defined for method 'method',
5039 }
5040 }
5041
5042 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5043 const ClassInstanceInfo& cl_inst_info,
5044 TRAPS) {
5045 if (_klass != nullptr) {
5046 return _klass;
5047 }
5048
5049 InstanceKlass* const ik =
5050 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5051
5052 if (is_hidden()) {
5053 mangle_hidden_class_name(ik);
5054 }
5055
5056 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5057
5058 assert(_klass == ik, "invariant");
5059
5060 return ik;
5061 }
5062
5063 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5064 bool changed_by_loadhook,
5065 const ClassInstanceInfo& cl_inst_info,
5066 TRAPS) {
5067 assert(ik != nullptr, "invariant");
5068
5069 // Set name and CLD before adding to CLD
5070 ik->set_class_loader_data(_loader_data);
5071 ik->set_class_loader_type();
5072 ik->set_name(_class_name);
5073
5074 // Add all classes to our internal class loader list here,
5075 // including classes in the bootstrap (null) class loader.
5076 const bool publicize = !is_internal();
5077
5078 _loader_data->add_class(ik, publicize);
5079
5080 set_klass_to_deallocate(ik);
5081
5082 assert(_field_info != nullptr, "invariant");
5083 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5084 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5085 "sanity");
5086
5087 assert(ik->is_instance_klass(), "sanity");
5088 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5089
5090 // Fill in information already parsed
5091 ik->set_should_verify_class(_need_verify);
5092
5093 // Not yet: supers are done below to support the new subtype-checking fields
5094 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5095 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5096 ik->set_static_oop_field_count(_static_oop_count);
5097
5098 // this transfers ownership of a lot of arrays from
5099 // the parser onto the InstanceKlass*
5100 apply_parsed_class_metadata(ik, _java_fields_count);
5101
5102 // can only set dynamic nest-host after static nest information is set
5103 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5104 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5105 }
5106
5107 // note that is not safe to use the fields in the parser from this point on
5108 assert(nullptr == _cp, "invariant");
5109 assert(nullptr == _fieldinfo_stream, "invariant");
5110 assert(nullptr == _fieldinfo_search_table, "invariant");
5111 assert(nullptr == _fields_status, "invariant");
5112 assert(nullptr == _methods, "invariant");
5113 assert(nullptr == _inner_classes, "invariant");
5114 assert(nullptr == _nest_members, "invariant");
5115 assert(nullptr == _combined_annotations, "invariant");
5116 assert(nullptr == _record_components, "invariant");
5117 assert(nullptr == _permitted_subclasses, "invariant");
5118
5119 if (_has_localvariable_table) {
5120 ik->set_has_localvariable_table(true);
5121 }
5122
5123 if (_has_final_method) {
5124 ik->set_has_final_method();
5125 }
5126
5127 ik->copy_method_ordering(_method_ordering, CHECK);
5128 // The InstanceKlass::_methods_jmethod_ids cache
5129 // is managed on the assumption that the initial cache
5130 // size is equal to the number of methods in the class. If
5131 // that changes, then InstanceKlass::idnum_can_increment()
5132 // has to be changed accordingly.
5133 ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5134
5135 ik->set_this_class_index(_this_class_index);
5136
5137 if (_is_hidden) {
5174 if ((_num_miranda_methods > 0) ||
5175 // if this class introduced new miranda methods or
5176 (_super_klass != nullptr && _super_klass->has_miranda_methods())
5177 // super class exists and this class inherited miranda methods
5178 ) {
5179 ik->set_has_miranda_methods(); // then set a flag
5180 }
5181
5182 // Fill in information needed to compute superclasses.
5183 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5184 ik->set_transitive_interfaces(_transitive_interfaces);
5185 ik->set_local_interfaces(_local_interfaces);
5186 _transitive_interfaces = nullptr;
5187 _local_interfaces = nullptr;
5188
5189 // Initialize itable offset tables
5190 klassItable::setup_itable_offset_table(ik);
5191
5192 // Compute transitive closure of interfaces this class implements
5193 // Do final class setup
5194 OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks;
5195 if (oop_map_blocks->_nonstatic_oop_map_count > 0) {
5196 oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5197 }
5198
5199 if (_has_contended_fields || _parsed_annotations->is_contended() ||
5200 ( _super_klass != nullptr && _super_klass->has_contended_annotations())) {
5201 ik->set_has_contended_annotations(true);
5202 }
5203
5204 // Fill in has_finalizer and layout_helper
5205 set_precomputed_flags(ik);
5206
5207 // check if this class can access its super class
5208 check_super_class_access(ik, CHECK);
5209
5210 // check if this class can access its superinterfaces
5211 check_super_interface_access(ik, CHECK);
5212
5213 // check if this class overrides any final method
5214 check_final_method_override(ik, CHECK);
5235
5236 assert(_all_mirandas != nullptr, "invariant");
5237
5238 // Generate any default methods - default methods are public interface methods
5239 // that have a default implementation. This is new with Java 8.
5240 if (_has_nonstatic_concrete_methods) {
5241 DefaultMethods::generate_default_methods(ik,
5242 _all_mirandas,
5243 CHECK);
5244 }
5245
5246 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5247 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5248 !module_entry->has_default_read_edges()) {
5249 if (!module_entry->set_has_default_read_edges()) {
5250 // We won a potential race
5251 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5252 }
5253 }
5254
5255 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5256
5257 if (!is_internal()) {
5258 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5259 if (CDSConfig::is_dumping_archive()) {
5260 SystemDictionaryShared::check_code_source(ik, _stream);
5261 }
5262
5263 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5264 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5265 log_is_enabled(Info, class, preview)) {
5266 ResourceMark rm;
5267 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5268 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5269 }
5270
5271 if (log_is_enabled(Debug, class, resolve)) {
5272 ResourceMark rm;
5273 // print out the superclass.
5274 const char * from = ik->external_name();
5318 const ClassLoadInfo* cl_info,
5319 Publicity pub_level,
5320 TRAPS) :
5321 _stream(stream),
5322 _class_name(nullptr),
5323 _loader_data(loader_data),
5324 _is_hidden(cl_info->is_hidden()),
5325 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5326 _orig_cp_size(0),
5327 _static_oop_count(0),
5328 _super_klass(),
5329 _cp(nullptr),
5330 _fieldinfo_stream(nullptr),
5331 _fieldinfo_search_table(nullptr),
5332 _fields_status(nullptr),
5333 _methods(nullptr),
5334 _inner_classes(nullptr),
5335 _nest_members(nullptr),
5336 _nest_host(0),
5337 _permitted_subclasses(nullptr),
5338 _record_components(nullptr),
5339 _local_interfaces(nullptr),
5340 _transitive_interfaces(nullptr),
5341 _combined_annotations(nullptr),
5342 _class_annotations(nullptr),
5343 _class_type_annotations(nullptr),
5344 _fields_annotations(nullptr),
5345 _fields_type_annotations(nullptr),
5346 _klass(nullptr),
5347 _klass_to_deallocate(nullptr),
5348 _parsed_annotations(nullptr),
5349 _field_info(nullptr),
5350 _temp_field_info(nullptr),
5351 _method_ordering(nullptr),
5352 _all_mirandas(nullptr),
5353 _vtable_size(0),
5354 _itable_size(0),
5355 _num_miranda_methods(0),
5356 _protection_domain(cl_info->protection_domain()),
5357 _access_flags(),
5358 _pub_level(pub_level),
5359 _bad_constant_seen(0),
5360 _synthetic_flag(false),
5361 _sde_length(false),
5362 _sde_buffer(nullptr),
5363 _sourcefile_index(0),
5364 _generic_signature_index(0),
5365 _major_version(0),
5366 _minor_version(0),
5367 _this_class_index(0),
5368 _super_class_index(0),
5369 _itfs_len(0),
5370 _java_fields_count(0),
5371 _need_verify(false),
5372 _has_nonstatic_concrete_methods(false),
5373 _declares_nonstatic_concrete_methods(false),
5374 _has_localvariable_table(false),
5375 _has_final_method(false),
5376 _has_contended_fields(false),
5377 _has_aot_runtime_setup_method(false),
5378 _has_finalizer(false),
5379 _has_empty_finalizer(false),
5380 _max_bootstrap_specifier_index(-1) {
5381
5382 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5383 _class_name->increment_refcount();
5384
5385 assert(_loader_data != nullptr, "invariant");
5386 assert(stream != nullptr, "invariant");
5387 assert(_stream != nullptr, "invariant");
5388 assert(_stream->buffer() == _stream->current(), "invariant");
5389 assert(_class_name != nullptr, "invariant");
5390 assert(0 == _access_flags.as_unsigned_short(), "invariant");
5391
5392 // Figure out whether we can skip format checking (matching classic VM behavior)
5393 // Always verify CFLH bytes from the user agents.
5394 _need_verify = stream->from_class_file_load_hook() ? true : Verifier::should_verify_for(_loader_data->class_loader());
5395
5396 // synch back verification state to stream to check for truncation.
5397 stream->set_need_verify(_need_verify);
5398
5399 parse_stream(stream, CHECK);
5400
5401 post_process_parsed_stream(stream, _cp, CHECK);
5402 }
5403
5404 void ClassFileParser::clear_class_metadata() {
5405 // metadata created before the instance klass is created. Must be
5406 // deallocated if classfile parsing returns an error.
5407 _cp = nullptr;
5408 _fieldinfo_stream = nullptr;
5409 _fieldinfo_search_table = nullptr;
5410 _fields_status = nullptr;
5411 _methods = nullptr;
5412 _inner_classes = nullptr;
5413 _nest_members = nullptr;
5414 _permitted_subclasses = nullptr;
5415 _combined_annotations = nullptr;
5416 _class_annotations = _class_type_annotations = nullptr;
5417 _fields_annotations = _fields_type_annotations = nullptr;
5418 _record_components = nullptr;
5419 }
5420
5421 // Destructor to clean up
5422 ClassFileParser::~ClassFileParser() {
5423 _class_name->decrement_refcount();
5424
5425 if (_cp != nullptr) {
5426 MetadataFactory::free_metadata(_loader_data, _cp);
5427 }
5428
5429 if (_fieldinfo_stream != nullptr) {
5430 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5431 }
5432 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_search_table);
5433
5434 if (_fields_status != nullptr) {
5435 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5436 }
5437
5438 if (_methods != nullptr) {
5439 // Free methods
5440 InstanceKlass::deallocate_methods(_loader_data, _methods);
5441 }
5442
5443 // beware of the Universe::empty_blah_array!!
5444 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5445 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5446 }
5447
5448 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5449 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5450 }
5451
5452 if (_record_components != nullptr) {
5453 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5454 }
5455
5456 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5457 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5458 }
5459
5460 // Free interfaces
5461 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5462 _local_interfaces, _transitive_interfaces);
5463
5464 if (_combined_annotations != nullptr) {
5465 // After all annotations arrays have been created, they are installed into the
5466 // Annotations object that will be assigned to the InstanceKlass being created.
5467
5468 // Deallocate the Annotations object and the installed annotations arrays.
5469 _combined_annotations->deallocate_contents(_loader_data);
5470
5471 // If the _combined_annotations pointer is non-null,
5472 // then the other annotations fields should have been cleared.
5473 assert(_class_annotations == nullptr, "Should have been cleared");
5474 assert(_class_type_annotations == nullptr, "Should have been cleared");
5475 assert(_fields_annotations == nullptr, "Should have been cleared");
5476 assert(_fields_type_annotations == nullptr, "Should have been cleared");
5477 } else {
5478 // If the annotations arrays were not installed into the Annotations object,
5479 // then they have to be deallocated explicitly.
5539
5540 assert(cp_size == (u2)cp->length(), "invariant");
5541
5542 // ACCESS FLAGS
5543 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5544
5545 // Access flags
5546 u2 flags;
5547 // JVM_ACC_MODULE is defined in JDK-9 and later.
5548 if (_major_version >= JAVA_9_VERSION) {
5549 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5550 } else {
5551 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5552 }
5553
5554 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5555 // Set abstract bit for old class files for backward compatibility
5556 flags |= JVM_ACC_ABSTRACT;
5557 }
5558
5559 verify_legal_class_modifiers(flags, nullptr, false, CHECK);
5560
5561 short bad_constant = class_bad_constant_seen();
5562 if (bad_constant != 0) {
5563 // Do not throw CFE until after the access_flags are checked because if
5564 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
5565 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
5566 return;
5567 }
5568
5569 _access_flags.set_flags(flags);
5570
5571 // This class and superclass
5572 _this_class_index = stream->get_u2_fast();
5573 guarantee_property(
5574 valid_cp_range(_this_class_index, cp_size) &&
5575 cp->tag_at(_this_class_index).is_unresolved_klass(),
5576 "Invalid this class index %u in constant pool in class file %s",
5577 _this_class_index, CHECK);
5578
5646
5647 // SUPERKLASS
5648 _super_class_index = stream->get_u2_fast();
5649 check_super_class(cp,
5650 _super_class_index,
5651 _need_verify,
5652 CHECK);
5653
5654 // Interfaces
5655 _itfs_len = stream->get_u2_fast();
5656 parse_interfaces(stream,
5657 _itfs_len,
5658 cp,
5659 &_has_nonstatic_concrete_methods,
5660 CHECK);
5661
5662 assert(_local_interfaces != nullptr, "invariant");
5663
5664 // Fields (offsets are filled in later)
5665 parse_fields(stream,
5666 _access_flags.is_interface(),
5667 cp,
5668 cp_size,
5669 &_java_fields_count,
5670 CHECK);
5671
5672 assert(_temp_field_info != nullptr, "invariant");
5673
5674 // Methods
5675 parse_methods(stream,
5676 _access_flags.is_interface(),
5677 &_has_localvariable_table,
5678 &_has_final_method,
5679 &_declares_nonstatic_concrete_methods,
5680 CHECK);
5681
5682 assert(_methods != nullptr, "invariant");
5683
5684 if (_declares_nonstatic_concrete_methods) {
5685 _has_nonstatic_concrete_methods = true;
5686 }
5766 // errors not checked yet.
5767 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
5768 "Interfaces must have java.lang.Object as superclass in class file %s",
5769 CHECK);
5770 }
5771 Handle loader(THREAD, _loader_data->class_loader());
5772 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
5773 // fast path to avoid lookup
5774 _super_klass = vmClasses::Object_klass();
5775 } else {
5776 _super_klass = (const InstanceKlass*)
5777 SystemDictionary::resolve_super_or_fail(_class_name,
5778 super_class_name,
5779 loader,
5780 true,
5781 CHECK);
5782 }
5783 }
5784
5785 if (_super_klass != nullptr) {
5786 if (_super_klass->has_nonstatic_concrete_methods()) {
5787 _has_nonstatic_concrete_methods = true;
5788 }
5789
5790 if (_super_klass->is_interface()) {
5791 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
5792 return;
5793 }
5794 }
5795
5796 // Compute the transitive list of all unique interfaces implemented by this class
5797 _transitive_interfaces =
5798 compute_transitive_interfaces(_super_klass,
5799 _local_interfaces,
5800 _loader_data,
5801 CHECK);
5802
5803 assert(_transitive_interfaces != nullptr, "invariant");
5804
5805 // sort methods
5806 _method_ordering = sort_methods(_methods);
5807
5808 _all_mirandas = new GrowableArray<Method*>(20);
5809
5810 Handle loader(THREAD, _loader_data->class_loader());
5811 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
5812 &_num_miranda_methods,
5813 _all_mirandas,
5814 _super_klass,
5815 _methods,
5816 _access_flags,
5817 _major_version,
5818 loader,
5819 _class_name,
5820 _local_interfaces);
5821
5822 // Size of Java itable (in words)
5823 _itable_size = _access_flags.is_interface() ? 0 :
5824 klassItable::compute_itable_size(_transitive_interfaces);
5825
5826 assert(_parsed_annotations != nullptr, "invariant");
5827
5828 _field_info = new FieldLayoutInfo();
5829 FieldLayoutBuilder lb(class_name(), super_klass(), _cp, /*_fields*/ _temp_field_info,
5830 _parsed_annotations->is_contended(), _field_info);
5831 lb.build_layout();
5832
5833 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
5834 _fieldinfo_stream =
5835 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
5836 injected_fields_count, loader_data(), CHECK);
5837 _fieldinfo_search_table = FieldInfoStream::create_search_table(_cp, _fieldinfo_stream, _loader_data, CHECK);
5838 _fields_status =
5839 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
5840 FieldStatus(0), CHECK);
5841 }
5842
5843 void ClassFileParser::set_klass(InstanceKlass* klass) {
5844
5845 #ifdef ASSERT
5846 if (klass != nullptr) {
5847 assert(nullptr == _klass, "leaking?");
5848 }
5849 #endif
5850
5851 _klass = klass;
5852 }
5853
5854 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
5855
5856 #ifdef ASSERT
5857 if (klass != nullptr) {
5858 assert(nullptr == _klass_to_deallocate, "leaking?");
5859 }
5860 #endif
5861
5862 _klass_to_deallocate = klass;
5863 }
5864
5865 // Caller responsible for ResourceMark
5866 // clone stream with rewound position
5867 const ClassFileStream* ClassFileParser::clone_stream() const {
5868 assert(_stream != nullptr, "invariant");
5869
5870 return _stream->clone();
5871 }
5872
5873 ReferenceType ClassFileParser::super_reference_type() const {
|
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/cdsConfig.hpp"
26 #include "classfile/classFileParser.hpp"
27 #include "classfile/classFileStream.hpp"
28 #include "classfile/classLoader.hpp"
29 #include "classfile/classLoaderData.inline.hpp"
30 #include "classfile/classLoadInfo.hpp"
31 #include "classfile/defaultMethods.hpp"
32 #include "classfile/fieldLayoutBuilder.hpp"
33 #include "classfile/javaClasses.inline.hpp"
34 #include "classfile/moduleEntry.hpp"
35 #include "classfile/packageEntry.hpp"
36 #include "classfile/symbolTable.hpp"
37 #include "classfile/systemDictionary.hpp"
38 #include "classfile/systemDictionaryShared.hpp"
39 #include "classfile/verificationType.hpp"
40 #include "classfile/verifier.hpp"
41 #include "classfile/vmClasses.hpp"
42 #include "classfile/vmSymbols.hpp"
43 #include "jvm.h"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "memory/allocation.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/oopFactory.hpp"
49 #include "memory/resourceArea.hpp"
50 #include "memory/universe.hpp"
51 #include "oops/annotations.hpp"
52 #include "oops/bsmAttribute.inline.hpp"
53 #include "oops/constantPool.inline.hpp"
54 #include "oops/fieldInfo.hpp"
55 #include "oops/fieldStreams.inline.hpp"
56 #include "oops/inlineKlass.inline.hpp"
57 #include "oops/instanceKlass.inline.hpp"
58 #include "oops/instanceMirrorKlass.hpp"
59 #include "oops/klass.inline.hpp"
60 #include "oops/klassVtable.hpp"
61 #include "oops/metadata.hpp"
62 #include "oops/method.inline.hpp"
63 #include "oops/oop.inline.hpp"
64 #include "oops/recordComponent.hpp"
65 #include "oops/symbol.hpp"
66 #include "prims/jvmtiExport.hpp"
67 #include "prims/jvmtiThreadState.hpp"
68 #include "runtime/arguments.hpp"
69 #include "runtime/fieldDescriptor.inline.hpp"
70 #include "runtime/handles.inline.hpp"
71 #include "runtime/javaCalls.hpp"
72 #include "runtime/os.hpp"
73 #include "runtime/perfData.hpp"
74 #include "runtime/reflection.hpp"
75 #include "runtime/safepointVerifiers.hpp"
76 #include "runtime/signature.hpp"
77 #include "runtime/timer.hpp"
78 #include "services/classLoadingService.hpp"
79 #include "services/threadService.hpp"
80 #include "utilities/align.hpp"
81 #include "utilities/bitMap.inline.hpp"
82 #include "utilities/checkedCast.hpp"
83 #include "utilities/copy.hpp"
84 #include "utilities/exceptions.hpp"
85 #include "utilities/formatBuffer.hpp"
86 #include "utilities/globalDefinitions.hpp"
87 #include "utilities/growableArray.hpp"
88 #include "utilities/hashTable.hpp"
89 #include "utilities/macros.hpp"
90 #include "utilities/ostream.hpp"
91 #include "utilities/stringUtils.hpp"
92 #include "utilities/utf8.hpp"
93
94 // We generally try to create the oops directly when parsing, rather than
95 // allocating temporary data structures and copying the bytes twice. A
96 // temporary area is only needed when parsing utf8 entries in the constant
97 // pool and when parsing line number tables.
98
99 // We add assert in debug mode when class format is not checked.
100
101 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
102 #define JAVA_MIN_SUPPORTED_VERSION 45
103 #define JAVA_PREVIEW_MINOR_VERSION 65535
104
105 // Used for two backward compatibility reasons:
106 // - to check for new additions to the class file format in JDK1.5
107 // - to check for bug fixes in the format checker in JDK1.5
108 #define JAVA_1_5_VERSION 49
109
110 // Used for backward compatibility reasons:
111 // - to check for javac bug fixes that happened after 1.5
484 guarantee_property(valid_symbol_at(name_ref_index),
485 "Invalid constant pool index %u in class file %s",
486 name_ref_index, CHECK);
487 guarantee_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 guarantee_property(valid_symbol_at(class_index),
502 "Invalid constant pool index %u in class file %s",
503 class_index, CHECK);
504
505 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
506 break;
507 }
508 case JVM_CONSTANT_StringIndex: {
509 const int string_index = cp->string_index_at(index);
510 guarantee_property(valid_symbol_at(string_index),
511 "Invalid constant pool index %u in class file %s",
512 string_index, CHECK);
513 Symbol* const sym = cp->symbol_at(string_index);
514 cp->unresolved_string_at_put(index, sym);
515 break;
516 }
517 case JVM_CONSTANT_MethodHandle: {
518 const int ref_index = cp->method_handle_index_at(index);
519 guarantee_property(valid_cp_range(ref_index, length),
520 "Invalid constant pool index %u in class file %s",
521 ref_index, CHECK);
522 const constantTag tag = cp->tag_at(ref_index);
523 const int ref_kind = cp->method_handle_ref_kind_at(index);
524
926 class AnnotationCollector : public ResourceObj{
927 public:
928 enum Location { _in_field, _in_method, _in_class };
929 enum ID {
930 _unknown = 0,
931 _method_CallerSensitive,
932 _method_ForceInline,
933 _method_DontInline,
934 _method_ChangesCurrentThread,
935 _method_JvmtiHideEvents,
936 _method_JvmtiMountTransition,
937 _method_InjectedProfile,
938 _method_LambdaForm_Compiled,
939 _method_Hidden,
940 _method_Scoped,
941 _method_IntrinsicCandidate,
942 _jdk_internal_vm_annotation_Contended,
943 _field_Stable,
944 _jdk_internal_vm_annotation_ReservedStackAccess,
945 _jdk_internal_ValueBased,
946 _jdk_internal_LooselyConsistentValue,
947 _jdk_internal_NullRestricted,
948 _java_lang_Deprecated,
949 _java_lang_Deprecated_for_removal,
950 _jdk_internal_vm_annotation_AOTSafeClassInitializer,
951 _method_AOTRuntimeSetup,
952 _jdk_internal_vm_annotation_TrustFinalFields,
953 _annotation_LIMIT
954 };
955 const Location _location;
956 int _annotations_present;
957 u2 _contended_group;
958
959 AnnotationCollector(Location location)
960 : _location(location), _annotations_present(0), _contended_group(0)
961 {
962 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
963 }
964 // If this annotation name has an ID, report it (or _none).
965 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, bool can_access_vm_annotations);
966 // Set the annotation name:
967 void set_annotation(ID id) {
1362 }
1363
1364 *constantvalue_index_addr = constantvalue_index;
1365 *is_synthetic_addr = is_synthetic;
1366 *generic_signature_index_addr = generic_signature_index;
1367 AnnotationArray* a = allocate_annotations(runtime_visible_annotations,
1368 runtime_visible_annotations_length,
1369 CHECK);
1370 parsed_annotations->set_field_annotations(a);
1371 a = allocate_annotations(runtime_visible_type_annotations,
1372 runtime_visible_type_annotations_length,
1373 CHECK);
1374 parsed_annotations->set_field_type_annotations(a);
1375 return;
1376 }
1377
1378
1379 // Side-effects: populates the _fields, _fields_annotations,
1380 // _fields_type_annotations fields
1381 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1382 AccessFlags class_access_flags,
1383 ConstantPool* cp,
1384 const int cp_size,
1385 u2* const java_fields_count_ptr,
1386 TRAPS) {
1387
1388 assert(cfs != nullptr, "invariant");
1389 assert(cp != nullptr, "invariant");
1390 assert(java_fields_count_ptr != nullptr, "invariant");
1391
1392 assert(nullptr == _fields_annotations, "invariant");
1393 assert(nullptr == _fields_type_annotations, "invariant");
1394
1395 // "inline type" means concrete value class
1396 bool is_inline_type = !class_access_flags.is_identity_class() && !class_access_flags.is_abstract();
1397 // "value class" can be either abstract or concrete value class
1398 bool is_value_class = !class_access_flags.is_identity_class() && !class_access_flags.is_interface();
1399 cfs->guarantee_more(2, CHECK); // length
1400 const u2 length = cfs->get_u2_fast();
1401 *java_fields_count_ptr = length;
1402
1403 int num_injected = 0;
1404 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1405 &num_injected);
1406
1407 // Two more slots are required for inline classes:
1408 // - The static field ".null_reset" which carries the nullable flat layout
1409 // representation of null, added below
1410 // - The nonstatic field ".empty" the JVM injects when detecting an empty
1411 // inline class, added in FieldLayoutBuilder::compute_inline_class_layout
1412 // One more slot is required for both abstract value class and inline classes:
1413 // - The static field ".acmp_maps" for acmp and identity hash, tracks
1414 // nonstatic fields both inherited or declared, added below
1415 const int total_fields = length + num_injected + (is_inline_type ? 2 : 0)
1416 + (is_value_class ? 1 : 0);
1417
1418 // Allocate a temporary resource array to collect field data.
1419 // After parsing all fields, data are stored in a UNSIGNED5 compressed stream.
1420 _temp_field_info = new GrowableArray<FieldInfo>(total_fields);
1421
1422 ResourceMark rm(THREAD);
1423 for (int n = 0; n < length; n++) {
1424 // access_flags, name_index, descriptor_index, attributes_count
1425 cfs->guarantee_more(8, CHECK);
1426
1427 jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS;
1428 if (!supports_inline_types()) {
1429 recognized_modifiers &= ~JVM_ACC_STRICT_INIT;
1430 }
1431
1432 const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1433 verify_legal_field_modifiers(flags, class_access_flags, CHECK);
1434 AccessFlags access_flags;
1435 access_flags.set_flags(flags);
1436 FieldInfo::FieldFlags fieldFlags(0);
1437
1438 const u2 name_index = cfs->get_u2_fast();
1439 guarantee_property(valid_symbol_at(name_index),
1440 "Invalid constant pool index %u for field name in class file %s",
1441 name_index, CHECK);
1442 const Symbol* const name = cp->symbol_at(name_index);
1443 verify_legal_field_name(name, CHECK);
1444
1445 const u2 signature_index = cfs->get_u2_fast();
1446 guarantee_property(valid_symbol_at(signature_index),
1447 "Invalid constant pool index %u for field signature in class file %s",
1448 signature_index, CHECK);
1449 const Symbol* const sig = cp->symbol_at(signature_index);
1450 verify_legal_field_signature(name, sig, CHECK);
1451
1452 u2 constantvalue_index = 0;
1453 bool is_synthetic = false;
1454 u2 generic_signature_index = 0;
1455 const bool is_static = access_flags.is_static();
1456 FieldAnnotationCollector parsed_annotations(_loader_data);
1457
1458 bool is_null_restricted = false;
1459
1460 const u2 attributes_count = cfs->get_u2_fast();
1461 if (attributes_count > 0) {
1462 parse_field_attributes(cfs,
1463 attributes_count,
1464 is_static,
1465 signature_index,
1466 &constantvalue_index,
1467 &is_synthetic,
1468 &generic_signature_index,
1469 &parsed_annotations,
1470 CHECK);
1471
1472 if (parsed_annotations.field_annotations() != nullptr) {
1473 if (_fields_annotations == nullptr) {
1474 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1475 _loader_data, length, nullptr,
1476 CHECK);
1477 }
1478 _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1479 parsed_annotations.set_field_annotations(nullptr);
1480 if (parsed_annotations.has_annotation(AnnotationCollector::_jdk_internal_NullRestricted)) {
1481 if (!Signature::has_envelope(sig)) {
1482 Exceptions::fthrow(
1483 THREAD_AND_LOCATION,
1484 vmSymbols::java_lang_ClassFormatError(),
1485 "Illegal use of @jdk.internal.vm.annotation.NullRestricted annotation on field %s.%s with signature %s (primitive types can never be null)",
1486 class_name()->as_C_string(), name->as_C_string(), sig->as_C_string());
1487 return;
1488 }
1489 if (!supports_inline_types()) {
1490 Exceptions::fthrow(
1491 THREAD_AND_LOCATION,
1492 vmSymbols::java_lang_ClassFormatError(),
1493 "Illegal use of @jdk.internal.vm.annotation.NullRestricted annotation on field %s.%s in non-preview class file",
1494 class_name()->as_C_string(), name->as_C_string());
1495 return;
1496 }
1497
1498 if ((flags & JVM_ACC_STRICT_INIT) == 0) {
1499 // Inject STRICT_INIT and validate in context
1500 const jint patched_flags = flags | JVM_ACC_STRICT_INIT;
1501 verify_legal_field_modifiers(patched_flags, class_access_flags, CHECK);
1502 access_flags.set_flags(patched_flags);
1503 }
1504 is_null_restricted = true;
1505 }
1506 }
1507 if (parsed_annotations.field_type_annotations() != nullptr) {
1508 if (_fields_type_annotations == nullptr) {
1509 _fields_type_annotations =
1510 MetadataFactory::new_array<AnnotationArray*>(_loader_data,
1511 length,
1512 nullptr,
1513 CHECK);
1514 }
1515 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1516 parsed_annotations.set_field_type_annotations(nullptr);
1517 }
1518
1519 if (is_synthetic) {
1520 access_flags.set_is_synthetic();
1521 }
1522 if (generic_signature_index != 0) {
1523 fieldFlags.update_generic(true);
1524 }
1525 }
1526
1527 if (is_null_restricted) {
1528 fieldFlags.update_null_free_inline_type(true);
1529 if (is_static) {
1530 _has_null_restricted_static_fields = true;
1531 }
1532 }
1533
1534 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1535
1536 // Update number of static oop fields.
1537 if (is_static && is_reference_type(type)) {
1538 _static_oop_count++;
1539 }
1540
1541 FieldInfo fi(access_flags, name_index, signature_index, constantvalue_index, fieldFlags);
1542 fi.set_index(n);
1543 if (fieldFlags.is_generic()) {
1544 fi.set_generic_signature_index(generic_signature_index);
1545 }
1546 parsed_annotations.apply_to(&fi);
1547 if (fi.field_flags().is_contended()) {
1548 _has_contended_fields = true;
1549 }
1550 if (access_flags.is_strict() && access_flags.is_static()) {
1551 _has_strict_static_fields = true;
1552 }
1553 _temp_field_info->append(fi);
1554 }
1555 assert(_temp_field_info->length() == length, "Must be");
1556
1557 if (num_injected != 0) {
1558 for (int n = 0; n < num_injected; n++) {
1559 // Check for duplicates
1560 if (injected[n].may_be_java) {
1561 const Symbol* const name = injected[n].name();
1562 const Symbol* const signature = injected[n].signature();
1563 bool duplicate = false;
1564 for (int i = 0; i < length; i++) {
1565 const FieldInfo* const f = _temp_field_info->adr_at(i);
1566 if (name == cp->symbol_at(f->name_index()) &&
1567 signature == cp->symbol_at(f->signature_index())) {
1568 // Symbol is desclared in Java so skip this one
1569 duplicate = true;
1570 break;
1571 }
1572 }
1573 if (duplicate) {
1574 // These will be removed from the field array at the end
1575 continue;
1576 }
1577 }
1578
1579 // Injected field
1580 FieldInfo::FieldFlags fflags(0);
1581 fflags.update_injected(true);
1582 AccessFlags aflags;
1583 FieldInfo fi(aflags, (u2)(injected[n].name_index), (u2)(injected[n].signature_index), 0, fflags);
1584 int idx = _temp_field_info->append(fi);
1585 _temp_field_info->adr_at(idx)->set_index(idx);
1586 }
1587 }
1588
1589 if (is_inline_type) {
1590 // Inject static ".null_reset" field. This is an all-zero value with its null-channel set to zero.
1591 // It should never be seen by user code, it is used when writing "null" to a nullable flat field
1592 // The all-zero value ensure that any embedded oop will be set to null, to avoid keeping dead objects
1593 // alive.
1594 FieldInfo::FieldFlags fflags2(0);
1595 fflags2.update_injected(true);
1596 AccessFlags aflags2(JVM_ACC_STATIC);
1597 FieldInfo fi2(aflags2,
1598 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(null_reset_value_name)),
1599 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(object_signature)),
1600 0,
1601 fflags2);
1602 int idx2 = _temp_field_info->append(fi2);
1603 _temp_field_info->adr_at(idx2)->set_index(idx2);
1604 _static_oop_count++;
1605 }
1606 if (!access_flags().is_identity_class() && !access_flags().is_interface()
1607 && _class_name != vmSymbols::java_lang_Object()) {
1608 // Acmp map ".acmp_maps" required for abstract and concrete value classes
1609 FieldInfo::FieldFlags fflags2(0);
1610 fflags2.update_injected(true);
1611 fflags2.update_stable(true);
1612 AccessFlags aflags2(JVM_ACC_STATIC | JVM_ACC_FINAL);
1613 FieldInfo fi3(aflags2,
1614 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(acmp_maps_name)),
1615 (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(int_array_signature)),
1616 0,
1617 fflags2);
1618 int idx2 = _temp_field_info->append(fi3);
1619 _temp_field_info->adr_at(idx2)->set_index(idx2);
1620 _static_oop_count++;
1621 }
1622
1623 if (_need_verify && length > 1) {
1624 // Check duplicated fields
1625 ResourceMark rm(THREAD);
1626 // Set containing name-signature pairs
1627 NameSigHashtable* names_and_sigs = new NameSigHashtable();
1628 for (int i = 0; i < _temp_field_info->length(); i++) {
1629 NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
1630 _temp_field_info->adr_at(i)->signature(_cp));
1631 // If no duplicates, add name/signature in hashtable names_and_sigs.
1632 if(!names_and_sigs->put(name_and_sig, 0)) {
1633 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
1634 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
1635 return;
1636 }
1637 }
1638 }
1639 }
1640
1641
1986 }
1987 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
1988 if (_location != _in_field && _location != _in_class) {
1989 break; // only allow for fields and classes
1990 }
1991 if (!EnableContended || (RestrictContended && !privileged)) {
1992 break; // honor privileges
1993 }
1994 return _jdk_internal_vm_annotation_Contended;
1995 }
1996 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): {
1997 if (_location != _in_method) break; // only allow for methods
1998 if (RestrictReservedStack && !privileged) break; // honor privileges
1999 return _jdk_internal_vm_annotation_ReservedStackAccess;
2000 }
2001 case VM_SYMBOL_ENUM_NAME(jdk_internal_ValueBased_signature): {
2002 if (_location != _in_class) break; // only allow for classes
2003 if (!privileged) break; // only allow in privileged code
2004 return _jdk_internal_ValueBased;
2005 }
2006 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_LooselyConsistentValue_signature): {
2007 if (_location != _in_class) break; // only allow for classes
2008 return _jdk_internal_LooselyConsistentValue;
2009 }
2010 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_NullRestricted_signature): {
2011 if (_location != _in_field) break; // only allow for fields
2012 return _jdk_internal_NullRestricted;
2013 }
2014 case VM_SYMBOL_ENUM_NAME(java_lang_Deprecated): {
2015 return _java_lang_Deprecated;
2016 }
2017 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_AOTSafeClassInitializer_signature): {
2018 if (_location != _in_class) break; // only allow for classes
2019 if (!privileged) break; // only allow in privileged code
2020 return _jdk_internal_vm_annotation_AOTSafeClassInitializer;
2021 }
2022 case VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_AOTRuntimeSetup_signature): {
2023 if (_location != _in_method) break; // only allow for methods
2024 if (!privileged) break; // only allow in privileged code
2025 return _method_AOTRuntimeSetup;
2026 }
2027 default: {
2028 break;
2029 }
2030 }
2031 return AnnotationCollector::_unknown;
2032 }
2033
2233 }
2234
2235 if (runtime_visible_type_annotations_length > 0) {
2236 a = allocate_annotations(runtime_visible_type_annotations,
2237 runtime_visible_type_annotations_length,
2238 CHECK);
2239 cm->set_type_annotations(a);
2240 }
2241 }
2242
2243
2244 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2245 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2246 // Method* to save footprint, so we only know the size of the resulting Method* when the
2247 // entire method attribute is parsed.
2248 //
2249 // The has_localvariable_table parameter is used to pass up the value to InstanceKlass.
2250
2251 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2252 bool is_interface,
2253 bool is_identity_class,
2254 const ConstantPool* cp,
2255 bool* const has_localvariable_table,
2256 TRAPS) {
2257 assert(cfs != nullptr, "invariant");
2258 assert(cp != nullptr, "invariant");
2259 assert(has_localvariable_table != nullptr, "invariant");
2260
2261 ResourceMark rm(THREAD);
2262 // Parse fixed parts:
2263 // access_flags, name_index, descriptor_index, attributes_count
2264 cfs->guarantee_more(8, CHECK_NULL);
2265
2266 u2 flags = cfs->get_u2_fast();
2267 const u2 name_index = cfs->get_u2_fast();
2268 const int cp_size = cp->length();
2269 guarantee_property(
2270 valid_symbol_at(name_index),
2271 "Illegal constant pool index %u for method name in class file %s",
2272 name_index, CHECK_NULL);
2273 const Symbol* const name = cp->symbol_at(name_index);
2275
2276 const u2 signature_index = cfs->get_u2_fast();
2277 guarantee_property(
2278 valid_symbol_at(signature_index),
2279 "Illegal constant pool index %u for method signature in class file %s",
2280 signature_index, CHECK_NULL);
2281 const Symbol* const signature = cp->symbol_at(signature_index);
2282
2283 if (name == vmSymbols::class_initializer_name()) {
2284 // We ignore the other access flags for a valid class initializer.
2285 // (JVM Spec 2nd ed., chapter 4.6)
2286 if (_major_version < 51) { // backward compatibility
2287 flags = JVM_ACC_STATIC;
2288 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2289 flags &= JVM_ACC_STATIC | (_major_version <= JAVA_16_VERSION ? JVM_ACC_STRICT : 0);
2290 } else {
2291 classfile_parse_error("Method <clinit> is not static in class file %s", THREAD);
2292 return nullptr;
2293 }
2294 } else {
2295 verify_legal_method_modifiers(flags, access_flags(), name, CHECK_NULL);
2296 }
2297
2298 if (name == vmSymbols::object_initializer_name() && is_interface) {
2299 classfile_parse_error("Interface cannot have a method named <init>, class file %s", THREAD);
2300 return nullptr;
2301 }
2302
2303 if (Arguments::is_valhalla_enabled()) {
2304 if (((flags & JVM_ACC_SYNCHRONIZED) == JVM_ACC_SYNCHRONIZED)
2305 && ((flags & JVM_ACC_STATIC) == 0 )
2306 && !_access_flags.is_identity_class()) {
2307 classfile_parse_error("Invalid synchronized method in non-identity class %s", THREAD);
2308 return nullptr;
2309 }
2310 }
2311
2312 int args_size = -1; // only used when _need_verify is true
2313 if (_need_verify) {
2314 verify_legal_name_with_signature(name, signature, CHECK_NULL);
2315 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2316 verify_legal_method_signature(name, signature, CHECK_NULL);
2317 if (args_size > MAX_ARGS_SIZE) {
2318 classfile_parse_error("Too many arguments in method signature in class file %s", THREAD);
2319 return nullptr;
2320 }
2321 }
2322
2323 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2324
2325 // Default values for code and exceptions attribute elements
2326 u2 max_stack = 0;
2327 u2 max_locals = 0;
2328 u4 code_length = 0;
2329 const u1* code_start = nullptr;
2330 u2 exception_table_length = 0;
2331 const unsafe_u2* exception_table_start = nullptr; // (potentially unaligned) pointer to array of u2 elements
2810 !access_flags.is_private() || !access_flags.is_static()) {
2811 classfile_parse_error("@AOTRuntimeSetup method must be declared private static void runtimeSetup() for class %s", CHECK_NULL);
2812 }
2813 _has_aot_runtime_setup_method = true;
2814 }
2815
2816 // Copy annotations
2817 copy_method_annotations(m->constMethod(),
2818 runtime_visible_annotations,
2819 runtime_visible_annotations_length,
2820 runtime_visible_parameter_annotations,
2821 runtime_visible_parameter_annotations_length,
2822 runtime_visible_type_annotations,
2823 runtime_visible_type_annotations_length,
2824 annotation_default,
2825 annotation_default_length,
2826 CHECK_NULL);
2827
2828 if (InstanceKlass::is_finalization_enabled() &&
2829 name == vmSymbols::finalize_method_name() &&
2830 signature == vmSymbols::void_method_signature() &&
2831 is_identity_class) {
2832 if (m->is_empty_method()) {
2833 _has_empty_finalizer = true;
2834 } else {
2835 _has_finalizer = true;
2836 }
2837 }
2838
2839 NOT_PRODUCT(m->verify());
2840 return m;
2841 }
2842
2843
2844 // Side-effects: populates the _methods field in the parser
2845 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2846 bool is_interface,
2847 bool* const has_localvariable_table,
2848 bool* has_final_method,
2849 bool* declares_nonstatic_concrete_methods,
2850 TRAPS) {
2851 assert(cfs != nullptr, "invariant");
2852 assert(has_localvariable_table != nullptr, "invariant");
2853 assert(has_final_method != nullptr, "invariant");
2854 assert(declares_nonstatic_concrete_methods != nullptr, "invariant");
2855
2856 assert(nullptr == _methods, "invariant");
2857
2858 cfs->guarantee_more(2, CHECK); // length
2859 const u2 length = cfs->get_u2_fast();
2860 if (length == 0) {
2861 _methods = Universe::the_empty_method_array();
2862 } else {
2863 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2864 length,
2865 nullptr,
2866 CHECK);
2867
2868 for (int index = 0; index < length; index++) {
2869 Method* method = parse_method(cfs,
2870 is_interface,
2871 is_identity_class(),
2872 _cp,
2873 has_localvariable_table,
2874 CHECK);
2875
2876 if (method->is_final()) {
2877 *has_final_method = true;
2878 }
2879 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2880 // used for interface initialization, and default method inheritance analysis
2881 if (is_interface && !(*declares_nonstatic_concrete_methods)
2882 && !method->is_abstract() && !method->is_static()) {
2883 *declares_nonstatic_concrete_methods = true;
2884 }
2885 _methods->at_put(index, method);
2886 }
2887
2888 if (_need_verify && length > 1) {
2889 // Check duplicated methods
2890 ResourceMark rm(THREAD);
2891 // Set containing name-signature pairs
3117 valid_klass_reference_at(outer_class_info_index),
3118 "outer_class_info_index %u has bad constant type in class file %s",
3119 outer_class_info_index, CHECK_0);
3120
3121 if (outer_class_info_index != 0) {
3122 const Symbol* const outer_class_name = cp->klass_name_at(outer_class_info_index);
3123 char* bytes = (char*)outer_class_name->bytes();
3124 guarantee_property(bytes[0] != JVM_SIGNATURE_ARRAY,
3125 "Outer class is an array class in class file %s", CHECK_0);
3126 }
3127 // Inner class name
3128 const u2 inner_name_index = cfs->get_u2_fast();
3129 guarantee_property(
3130 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3131 "inner_name_index %u has bad constant type in class file %s",
3132 inner_name_index, CHECK_0);
3133 if (_need_verify) {
3134 guarantee_property(inner_class_info_index != outer_class_info_index,
3135 "Class is both outer and inner class in class file %s", CHECK_0);
3136 }
3137
3138 // Access flags
3139 u2 flags;
3140 // JVM_ACC_MODULE is defined in JDK-9 and later.
3141 if (_major_version >= JAVA_9_VERSION) {
3142 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3143 } else {
3144 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3145 }
3146
3147 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3148 // Set abstract bit for old class files for backward compatibility
3149 flags |= JVM_ACC_ABSTRACT;
3150 }
3151
3152 if (!supports_inline_types()) {
3153 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
3154 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3155 if (!is_module && !is_interface) {
3156 flags |= JVM_ACC_IDENTITY;
3157 }
3158 }
3159
3160 Symbol* inner_name_symbol = inner_name_index == 0 ? nullptr : cp->symbol_at(inner_name_index);
3161 verify_legal_class_modifiers(flags, inner_name_symbol, inner_name_index == 0, CHECK_0);
3162 AccessFlags inner_access_flags(flags);
3163
3164 inner_classes->at_put(index++, inner_class_info_index);
3165 inner_classes->at_put(index++, outer_class_info_index);
3166 inner_classes->at_put(index++, inner_name_index);
3167 inner_classes->at_put(index++, inner_access_flags.as_unsigned_short());
3168 }
3169
3170 // Check for circular and duplicate entries.
3171 bool has_circularity = false;
3172 if (_need_verify) {
3173 has_circularity = check_inner_classes_circularity(cp, length * 4, CHECK_0);
3174 if (has_circularity) {
3175 // If circularity check failed then ignore InnerClasses attribute.
3176 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
3177 index = 0;
3178 if (parsed_enclosingmethod_attribute) {
3179 inner_classes = MetadataFactory::new_array<u2>(_loader_data, 2, CHECK_0);
3245 if (length > 0) {
3246 int index = 0;
3247 cfs->guarantee_more(2 * length, CHECK_0);
3248 for (int n = 0; n < length; n++) {
3249 const u2 class_info_index = cfs->get_u2_fast();
3250 guarantee_property(
3251 valid_klass_reference_at(class_info_index),
3252 "Permitted subclass class_info_index %u has bad constant type in class file %s",
3253 class_info_index, CHECK_0);
3254 permitted_subclasses->at_put(index++, class_info_index);
3255 }
3256 assert(index == size, "wrong size");
3257 }
3258
3259 // Restore buffer's current position.
3260 cfs->set_current(current_mark);
3261
3262 return length;
3263 }
3264
3265 u2 ClassFileParser::parse_classfile_loadable_descriptors_attribute(const ClassFileStream* const cfs,
3266 const u1* const loadable_descriptors_attribute_start,
3267 TRAPS) {
3268 const u1* const current_mark = cfs->current();
3269 u2 length = 0;
3270 if (loadable_descriptors_attribute_start != nullptr) {
3271 cfs->set_current(loadable_descriptors_attribute_start);
3272 cfs->guarantee_more(2, CHECK_0); // length
3273 length = cfs->get_u2_fast();
3274 }
3275 const int size = length;
3276 Array<u2>* const loadable_descriptors = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3277 _loadable_descriptors = loadable_descriptors;
3278 if (length > 0) {
3279 int index = 0;
3280 cfs->guarantee_more(2 * length, CHECK_0);
3281 for (int n = 0; n < length; n++) {
3282 const u2 descriptor_index = cfs->get_u2_fast();
3283 guarantee_property(
3284 valid_symbol_at(descriptor_index),
3285 "LoadableDescriptors descriptor_index %u has bad constant type in class file %s",
3286 descriptor_index, CHECK_0);
3287 Symbol* descriptor = _cp->symbol_at(descriptor_index);
3288 bool valid = legal_field_signature(descriptor, CHECK_0);
3289 if (!valid) {
3290 ResourceMark rm(THREAD);
3291 Exceptions::fthrow(THREAD_AND_LOCATION,
3292 vmSymbols::java_lang_ClassFormatError(),
3293 "Descriptor from LoadableDescriptors attribute at index \"%d\" in class %s has illegal signature \"%s\"",
3294 descriptor_index, _class_name->as_C_string(), descriptor->as_C_string());
3295 return 0;
3296 }
3297 loadable_descriptors->at_put(index++, descriptor_index);
3298 }
3299 assert(index == size, "wrong size");
3300 }
3301
3302 // Restore buffer's current position.
3303 cfs->set_current(current_mark);
3304
3305 return length;
3306 }
3307
3308 // Record {
3309 // u2 attribute_name_index;
3310 // u4 attribute_length;
3311 // u2 components_count;
3312 // component_info components[components_count];
3313 // }
3314 // component_info {
3315 // u2 name_index;
3316 // u2 descriptor_index
3317 // u2 attributes_count;
3318 // attribute_info_attributes[attributes_count];
3319 // }
3320 u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3321 const ConstantPool* cp,
3322 const u1* const record_attribute_start,
3323 TRAPS) {
3324 const u1* const current_mark = cfs->current();
3325 int components_count = 0;
3326 unsigned int calculate_attr_size = 0;
3327 if (record_attribute_start != nullptr) {
3538 cp->bsm_entries().end_extension(iter, _loader_data, CHECK);
3539 guarantee_property(current_before_parsing + attribute_byte_length == cfs->current(),
3540 "Bad length on BootstrapMethods in class file %s",
3541 CHECK);
3542 }
3543
3544 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3545 ConstantPool* cp,
3546 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3547 TRAPS) {
3548 assert(cfs != nullptr, "invariant");
3549 assert(cp != nullptr, "invariant");
3550 assert(parsed_annotations != nullptr, "invariant");
3551
3552 // Set inner classes attribute to default sentinel
3553 _inner_classes = Universe::the_empty_short_array();
3554 // Set nest members attribute to default sentinel
3555 _nest_members = Universe::the_empty_short_array();
3556 // Set _permitted_subclasses attribute to default sentinel
3557 _permitted_subclasses = Universe::the_empty_short_array();
3558 // Set _loadable_descriptors attribute to default sentinel
3559 _loadable_descriptors = Universe::the_empty_short_array();
3560 cfs->guarantee_more(2, CHECK); // attributes_count
3561 u2 attributes_count = cfs->get_u2_fast();
3562 bool parsed_sourcefile_attribute = false;
3563 bool parsed_innerclasses_attribute = false;
3564 bool parsed_nest_members_attribute = false;
3565 bool parsed_permitted_subclasses_attribute = false;
3566 bool parsed_loadable_descriptors_attribute = false;
3567 bool parsed_nest_host_attribute = false;
3568 bool parsed_record_attribute = false;
3569 bool parsed_enclosingmethod_attribute = false;
3570 bool parsed_bootstrap_methods_attribute = false;
3571 const u1* runtime_visible_annotations = nullptr;
3572 int runtime_visible_annotations_length = 0;
3573 const u1* runtime_visible_type_annotations = nullptr;
3574 int runtime_visible_type_annotations_length = 0;
3575 bool runtime_invisible_type_annotations_exists = false;
3576 bool runtime_invisible_annotations_exists = false;
3577 bool parsed_source_debug_ext_annotations_exist = false;
3578 const u1* inner_classes_attribute_start = nullptr;
3579 u4 inner_classes_attribute_length = 0;
3580 u2 enclosing_method_class_index = 0;
3581 u2 enclosing_method_method_index = 0;
3582 const u1* nest_members_attribute_start = nullptr;
3583 u4 nest_members_attribute_length = 0;
3584 const u1* record_attribute_start = nullptr;
3585 u4 record_attribute_length = 0;
3586 const u1* permitted_subclasses_attribute_start = nullptr;
3587 u4 permitted_subclasses_attribute_length = 0;
3588 const u1* loadable_descriptors_attribute_start = nullptr;
3589 u4 loadable_descriptors_attribute_length = 0;
3590
3591 // Iterate over attributes
3592 while (attributes_count--) {
3593 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3594 const u2 attribute_name_index = cfs->get_u2_fast();
3595 const u4 attribute_length = cfs->get_u4_fast();
3596 guarantee_property(
3597 valid_symbol_at(attribute_name_index),
3598 "Attribute name has bad constant pool index %u in class file %s",
3599 attribute_name_index, CHECK);
3600 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3601 if (tag == vmSymbols::tag_source_file()) {
3602 // Check for SourceFile tag
3603 if (_need_verify) {
3604 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3605 }
3606 if (parsed_sourcefile_attribute) {
3607 classfile_parse_error("Multiple SourceFile attributes in class file %s", THREAD);
3608 return;
3609 } else {
3785 return;
3786 }
3787 parsed_record_attribute = true;
3788 record_attribute_start = cfs->current();
3789 record_attribute_length = attribute_length;
3790 } else if (_major_version >= JAVA_17_VERSION) {
3791 if (tag == vmSymbols::tag_permitted_subclasses()) {
3792 if (parsed_permitted_subclasses_attribute) {
3793 classfile_parse_error("Multiple PermittedSubclasses attributes in class file %s", CHECK);
3794 return;
3795 }
3796 // Classes marked ACC_FINAL cannot have a PermittedSubclasses attribute.
3797 if (_access_flags.is_final()) {
3798 classfile_parse_error("PermittedSubclasses attribute in final class file %s", CHECK);
3799 return;
3800 }
3801 parsed_permitted_subclasses_attribute = true;
3802 permitted_subclasses_attribute_start = cfs->current();
3803 permitted_subclasses_attribute_length = attribute_length;
3804 }
3805 if (Arguments::is_valhalla_enabled() && tag == vmSymbols::tag_loadable_descriptors()) {
3806 if (parsed_loadable_descriptors_attribute) {
3807 classfile_parse_error("Multiple LoadableDescriptors attributes in class file %s", CHECK);
3808 return;
3809 }
3810 parsed_loadable_descriptors_attribute = true;
3811 loadable_descriptors_attribute_start = cfs->current();
3812 loadable_descriptors_attribute_length = attribute_length;
3813 }
3814 }
3815 // Skip attribute_length for any attribute where major_verson >= JAVA_17_VERSION
3816 cfs->skip_u1(attribute_length, CHECK);
3817 } else {
3818 // Unknown attribute
3819 cfs->skip_u1(attribute_length, CHECK);
3820 }
3821 } else {
3822 // Unknown attribute
3823 cfs->skip_u1(attribute_length, CHECK);
3824 }
3825 } else {
3826 // Unknown attribute
3827 cfs->skip_u1(attribute_length, CHECK);
3828 }
3829 }
3830 _class_annotations = allocate_annotations(runtime_visible_annotations,
3831 runtime_visible_annotations_length,
3832 CHECK);
3833 _class_type_annotations = allocate_annotations(runtime_visible_type_annotations,
3870 CHECK);
3871 if (_need_verify) {
3872 guarantee_property(record_attribute_length == calculated_attr_length,
3873 "Record attribute has wrong length in class file %s",
3874 CHECK);
3875 }
3876 }
3877
3878 if (parsed_permitted_subclasses_attribute) {
3879 const u2 num_subclasses = parse_classfile_permitted_subclasses_attribute(
3880 cfs,
3881 permitted_subclasses_attribute_start,
3882 CHECK);
3883 if (_need_verify) {
3884 guarantee_property(
3885 permitted_subclasses_attribute_length == sizeof(num_subclasses) + sizeof(u2) * num_subclasses,
3886 "Wrong PermittedSubclasses attribute length in class file %s", CHECK);
3887 }
3888 }
3889
3890 if (parsed_loadable_descriptors_attribute) {
3891 const u2 num_classes = parse_classfile_loadable_descriptors_attribute(
3892 cfs,
3893 loadable_descriptors_attribute_start,
3894 CHECK);
3895 if (_need_verify) {
3896 guarantee_property(
3897 loadable_descriptors_attribute_length == sizeof(num_classes) + sizeof(u2) * num_classes,
3898 "Wrong LoadableDescriptors attribute length in class file %s", CHECK);
3899 }
3900 }
3901
3902 if (_max_bootstrap_specifier_index >= 0) {
3903 guarantee_property(parsed_bootstrap_methods_attribute,
3904 "Missing BootstrapMethods attribute in class file %s", CHECK);
3905 }
3906 }
3907
3908 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3909 assert(k != nullptr, "invariant");
3910
3911 if (_synthetic_flag)
3912 k->set_is_synthetic();
3913 if (_sourcefile_index != 0) {
3914 k->set_source_file_name_index(_sourcefile_index);
3915 }
3916 if (_generic_signature_index != 0) {
3917 k->set_generic_signature_index(_generic_signature_index);
3918 }
3919 if (_sde_buffer != nullptr) {
3920 k->set_source_debug_extension(_sde_buffer, _sde_length);
3921 }
3948 _class_type_annotations = nullptr;
3949 _fields_annotations = nullptr;
3950 _fields_type_annotations = nullptr;
3951 }
3952
3953 // Transfer ownership of metadata allocated to the InstanceKlass.
3954 void ClassFileParser::apply_parsed_class_metadata(
3955 InstanceKlass* this_klass,
3956 int java_fields_count) {
3957 assert(this_klass != nullptr, "invariant");
3958
3959 _cp->set_pool_holder(this_klass);
3960 this_klass->set_constants(_cp);
3961 this_klass->set_fieldinfo_stream(_fieldinfo_stream);
3962 this_klass->set_fieldinfo_search_table(_fieldinfo_search_table);
3963 this_klass->set_fields_status(_fields_status);
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_loadable_descriptors(_loadable_descriptors);
3969 this_klass->set_annotations(_combined_annotations);
3970 this_klass->set_permitted_subclasses(_permitted_subclasses);
3971 this_klass->set_record_components(_record_components);
3972 this_klass->set_inline_layout_info_array(_inline_layout_info_array);
3973
3974 DEBUG_ONLY(FieldInfoStream::validate_search_table(_cp, _fieldinfo_stream, _fieldinfo_search_table));
3975
3976 // Delay the setting of _local_interfaces and _transitive_interfaces until after
3977 // initialize_supers() in fill_instance_klass(). It is because the _local_interfaces could
3978 // be shared with _transitive_interfaces and _transitive_interfaces may be shared with
3979 // its _super. If an OOM occurs while loading the current klass, its _super field
3980 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3981 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3982 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3983
3984 // Clear out these fields so they don't get deallocated by the destructor
3985 clear_class_metadata();
3986 }
3987
3988 AnnotationArray* ClassFileParser::allocate_annotations(const u1* const anno,
3989 int anno_length,
3990 TRAPS) {
3991 AnnotationArray* annotations = nullptr;
3992 if (anno != nullptr) {
3993 annotations = MetadataFactory::new_array<u1>(_loader_data,
3994 anno_length,
3995 CHECK_(annotations));
3996 for (int i = 0; i < anno_length; i++) {
3997 annotations->at_put(i, anno[i]);
3998 }
3999 }
4000 return annotations;
4001 }
4002
4003 void ClassFileParser::check_super_class(ConstantPool* const cp,
4004 const int super_class_index,
4005 const bool need_verify,
4006 TRAPS) {
4007 assert(cp != nullptr, "invariant");
4008
4009 if (super_class_index == 0) {
4010 guarantee_property(_class_name == vmSymbols::java_lang_Object(),
4011 "Invalid superclass index 0 in class file %s",
4012 CHECK);
4013 } else {
4014 guarantee_property(valid_klass_reference_at(super_class_index),
4015 "Invalid superclass index %u in class file %s",
4016 super_class_index,
4017 CHECK);
4018
4019 // The class name should be legal because it is checked when parsing constant pool.
4020 // However, make sure it is not an array type.
4021 if (need_verify) {
4022 guarantee_property(cp->klass_name_at(super_class_index)->char_at(0) != JVM_SIGNATURE_ARRAY,
4023 "Bad superclass name in class file %s", CHECK);
4024 }
4025 }
4026 }
4027
4028 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int max_blocks) {
4029 _max_nonstatic_oop_maps = max_blocks;
4030 _nonstatic_oop_map_count = 0;
4031 if (max_blocks == 0) {
4143
4144 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4145 assert(ik != nullptr, "invariant");
4146
4147 const InstanceKlass* const super = ik->super();
4148
4149 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4150 // in which case we don't have to register objects as finalizable
4151 if (!_has_empty_finalizer) {
4152 if (_has_finalizer ||
4153 (super != nullptr && super->has_finalizer())) {
4154 ik->set_has_finalizer();
4155 }
4156 }
4157
4158 #ifdef ASSERT
4159 bool f = false;
4160 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4161 vmSymbols::void_method_signature());
4162 if (InstanceKlass::is_finalization_enabled() &&
4163 (m != nullptr) && !m->is_empty_method() &&
4164 m->method_holder()->access_flags().is_identity_class()) {
4165 f = true;
4166 }
4167
4168 // Spec doesn't prevent agent from redefinition of empty finalizer.
4169 // Despite the fact that it's generally bad idea and redefined finalizer
4170 // will not work as expected we shouldn't abort vm in this case
4171 if (!ik->has_redefined_this_or_super()) {
4172 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4173 }
4174 #endif
4175
4176 // Check if this klass supports the java.lang.Cloneable interface
4177 if (vmClasses::Cloneable_klass_is_loaded()) {
4178 if (ik->is_subtype_of(vmClasses::Cloneable_klass())) {
4179 ik->set_is_cloneable();
4180 }
4181 }
4182
4183 // If it cannot be fast-path allocated, set a bit in the layout helper.
4184 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4185 assert(ik->size_helper() > 0, "layout_helper is initialized");
4186 if (ik->is_abstract() || ik->is_interface()
4187 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == nullptr)
4188 || ik->size_helper() >= FastAllocateSizeLimit) {
4189 // Forbid fast-path allocation.
4190 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4191 ik->set_layout_helper(lh);
4192 }
4193
4194 // Propagate the AOT runtimeSetup method discovery
4195 if (_has_aot_runtime_setup_method) {
4196 ik->set_is_runtime_setup_required();
4197 if (log_is_enabled(Info, aot, init)) {
4198 ResourceMark rm;
4199 log_info(aot, init)("Found @AOTRuntimeSetup class %s", ik->external_name());
4200 }
4201 }
4202 }
4203
4204 bool ClassFileParser::supports_inline_types() const {
4205 // Inline types are only supported by class file version 71.65535 and later
4206 return _major_version > JAVA_28_VERSION ||
4207 (_major_version == JAVA_28_VERSION && _minor_version == JAVA_PREVIEW_MINOR_VERSION);
4208 }
4209
4210 // utility methods for appending an array with check for duplicates
4211
4212 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4213 const Array<InstanceKlass*>* const ifs) {
4214 // iterate over new interfaces
4215 for (int i = 0; i < ifs->length(); i++) {
4216 InstanceKlass* const e = ifs->at(i);
4217 assert(e->is_klass() && e->is_interface(), "just checking");
4218 // add new interface
4219 result->append_if_missing(e);
4220 }
4221 }
4222
4223 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4224 Array<InstanceKlass*>* local_ifs,
4225 ClassLoaderData* loader_data,
4226 TRAPS) {
4227 assert(local_ifs != nullptr, "invariant");
4228 assert(loader_data != nullptr, "invariant");
4229
4286
4287 void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, TRAPS) {
4288 assert(this_klass != nullptr, "invariant");
4289 const InstanceKlass* const super = this_klass->super();
4290
4291 if (super != nullptr) {
4292 if (super->is_final()) {
4293 classfile_icce_error("class %s cannot inherit from final class %s", super, THREAD);
4294 return;
4295 }
4296
4297 if (super->is_sealed()) {
4298 stringStream ss;
4299 ResourceMark rm(THREAD);
4300 if (!super->has_as_permitted_subclass(this_klass, ss)) {
4301 classfile_icce_error(ss.as_string(), THREAD);
4302 return;
4303 }
4304 }
4305
4306 // The JVMS says that super classes for value types must not have the ACC_IDENTITY
4307 // flag set. But, java.lang.Object must still be allowed to be a direct super class
4308 // for value classes. So, it is treated as a special case for now.
4309 if (!this_klass->access_flags().is_identity_class() &&
4310 super->name() != vmSymbols::java_lang_Object() &&
4311 super->is_identity_class()) {
4312 classfile_icce_error("value class %s cannot inherit from class %s", super, THREAD);
4313 return;
4314 }
4315
4316 Reflection::VerifyClassAccessResults vca_result =
4317 Reflection::verify_class_access(this_klass, super, false);
4318 if (vca_result != Reflection::ACCESS_OK) {
4319 ResourceMark rm(THREAD);
4320 char* msg = Reflection::verify_class_access_msg(this_klass,
4321 super,
4322 vca_result);
4323
4324 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4325 if (msg == nullptr) {
4326 bool same_module = (this_klass->module() == super->module());
4327 Exceptions::fthrow(
4328 THREAD_AND_LOCATION,
4329 vmSymbols::java_lang_IllegalAccessError(),
4330 "class %s cannot access its %ssuperclass %s (%s%s%s)",
4331 this_klass->external_name(),
4332 super->is_abstract() ? "abstract " : "",
4333 super->external_name(),
4334 (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(),
4335 (same_module) ? "" : "; ",
4488 // Verify the class modifiers for the current class, or an inner class if inner_name is non-null.
4489 void ClassFileParser::verify_legal_class_modifiers(jint flags, Symbol* inner_name, bool is_anonymous_inner_class, TRAPS) const {
4490 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4491 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4492 if (is_module) {
4493 ResourceMark rm(THREAD);
4494 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4495 Exceptions::fthrow(
4496 THREAD_AND_LOCATION,
4497 vmSymbols::java_lang_NoClassDefFoundError(),
4498 "%s is not a class because access_flag ACC_MODULE is set",
4499 _class_name->as_C_string());
4500 return;
4501 }
4502
4503 if (!_need_verify) { return; }
4504
4505 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4506 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4507 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4508 const bool is_identity = (flags & JVM_ACC_IDENTITY) != 0;
4509 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4510 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4511 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4512 const bool valid_value_class = is_identity || is_interface || (supports_inline_types() && (is_abstract || is_final));
4513
4514 if ((is_abstract && is_final) ||
4515 (is_interface && !is_abstract) ||
4516 (is_interface && major_gte_1_5 && (is_identity || is_enum)) || // ACC_SUPER (now ACC_IDENTITY) was illegal for interfaces
4517 (!is_interface && major_gte_1_5 && is_annotation) ||
4518 (!valid_value_class)) {
4519 ResourceMark rm(THREAD);
4520 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4521 if (inner_name == nullptr && !is_anonymous_inner_class) {
4522 Exceptions::fthrow(
4523 THREAD_AND_LOCATION,
4524 vmSymbols::java_lang_ClassFormatError(),
4525 "Illegal class modifiers in class %s: 0x%X",
4526 _class_name->as_C_string(), flags
4527 );
4528 } else {
4529 if (is_anonymous_inner_class) {
4530 Exceptions::fthrow(
4531 THREAD_AND_LOCATION,
4532 vmSymbols::java_lang_ClassFormatError(),
4533 "Illegal class modifiers in anonymous inner class of class %s: 0x%X",
4534 _class_name->as_C_string(), flags
4535 );
4536 } else {
4537 Exceptions::fthrow(
4538 THREAD_AND_LOCATION,
4593 THREAD_AND_LOCATION,
4594 vmSymbols::java_lang_UnsupportedClassVersionError(),
4595 "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4596 "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4597 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4598 return;
4599 }
4600
4601 if (!Arguments::enable_preview()) {
4602 classfile_ucve_error("Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4603 class_name, major, minor, THREAD);
4604 return;
4605 }
4606
4607 } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4608 classfile_ucve_error("%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4609 class_name, major, minor, THREAD);
4610 }
4611 }
4612
4613 void ClassFileParser:: verify_legal_field_modifiers(jint flags,
4614 AccessFlags class_access_flags,
4615 TRAPS) const {
4616 if (!_need_verify) { return; }
4617
4618 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4619 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4620 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4621 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4622 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4623 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4624 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4625 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4626 const bool is_strict = (flags & JVM_ACC_STRICT_INIT) != 0;
4627 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4628
4629 const bool is_interface = class_access_flags.is_interface();
4630 const bool is_identity_class = class_access_flags.is_identity_class();
4631
4632 bool is_illegal = false;
4633 const char* error_msg = "";
4634
4635 // There is some overlap in the checks that apply, for example interface fields
4636 // must be static, static fields can't be strict, and therefore interfaces can't
4637 // have strict fields. So we don't have to check every possible invalid combination
4638 // individually as long as all are covered. Once we have found an illegal combination
4639 // we can stop checking.
4640
4641 if (!is_illegal) {
4642 if (is_interface) {
4643 if (!is_public || !is_static || !is_final || is_private ||
4644 is_protected || is_volatile || is_transient ||
4645 (major_gte_1_5 && is_enum)) {
4646 is_illegal = true;
4647 error_msg = "interface fields must be public, static and final, and may be synthetic";
4648 }
4649 } else { // not interface
4650 if (has_illegal_visibility(flags)) {
4651 is_illegal = true;
4652 error_msg = "invalid visibility flags for class field";
4653 } else if (is_final && is_volatile) {
4654 is_illegal = true;
4655 error_msg = "fields cannot be final and volatile";
4656 } else if (supports_inline_types()) {
4657 if (!is_identity_class && !is_static && (!is_strict || !is_final)) {
4658 is_illegal = true;
4659 error_msg = "value class fields must be either non-static final and strict, or static";
4660 }
4661 }
4662 }
4663 }
4664
4665 if (is_illegal) {
4666 ResourceMark rm(THREAD);
4667 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4668 Exceptions::fthrow(
4669 THREAD_AND_LOCATION,
4670 vmSymbols::java_lang_ClassFormatError(),
4671 "Illegal field modifiers (%s) in class %s: 0x%X",
4672 error_msg, _class_name->as_C_string(), flags);
4673 return;
4674 }
4675 }
4676
4677 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4678 AccessFlags class_access_flags,
4679 const Symbol* name,
4680 TRAPS) const {
4681 if (!_need_verify) { return; }
4682
4683 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4684 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4685 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4686 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4687 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4688 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4689 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4690 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4691 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4692 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4693 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4694 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4695 const bool major_gte_17 = _major_version >= JAVA_17_VERSION;
4696 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4697 const bool is_interface = class_access_flags.is_interface();
4698 const bool is_identity_class = class_access_flags.is_identity_class();
4699
4700 bool is_illegal = false;
4701 const char* class_note = "";
4702
4703 if (is_interface) {
4704 if (major_gte_8) {
4705 // Class file version is JAVA_8_VERSION or later Methods of
4706 // interfaces may set any of the flags except ACC_PROTECTED,
4707 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4708 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4709 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4710 (is_native || is_protected || is_final || is_synchronized) ||
4711 // If a specific method of a class or interface has its
4712 // ACC_ABSTRACT flag set, it must not have any of its
4713 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4714 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4715 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4716 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4717 (is_abstract && (is_private || is_static || (!major_gte_17 && is_strict)))) {
4718 is_illegal = true;
4719 }
4720 } else if (major_gte_1_5) {
4721 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION)
4722 if (!is_public || is_private || is_protected || is_static || is_final ||
4723 is_synchronized || is_native || !is_abstract || is_strict) {
4724 is_illegal = true;
4725 }
4726 } else {
4727 // Class file version is pre-JAVA_1_5_VERSION
4728 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4729 is_illegal = true;
4730 }
4731 }
4732 } else { // not interface
4733 if (has_illegal_visibility(flags)) {
4734 is_illegal = true;
4735 } else {
4736 if (is_initializer) {
4737 if (is_static || is_final || is_synchronized || is_native ||
4738 is_abstract || (major_gte_1_5 && is_bridge)) {
4739 is_illegal = true;
4740 }
4741 } else { // not initializer
4742 if (!is_identity_class && is_synchronized && !is_static) {
4743 is_illegal = true;
4744 class_note = " (not an identity class)";
4745 } else {
4746 if (is_abstract) {
4747 if ((is_final || is_native || is_private || is_static ||
4748 (major_gte_1_5 && (is_synchronized || (!major_gte_17 && is_strict))))) {
4749 is_illegal = true;
4750 }
4751 }
4752 }
4753 }
4754 }
4755 }
4756
4757 if (is_illegal) {
4758 ResourceMark rm(THREAD);
4759 // Names are all known to be < 64k so we know this formatted message is not excessively large.
4760 Exceptions::fthrow(
4761 THREAD_AND_LOCATION,
4762 vmSymbols::java_lang_ClassFormatError(),
4763 "Method %s in class %s%s has illegal modifiers: 0x%X",
4764 name->as_C_string(), _class_name->as_C_string(), class_note, flags);
4765 return;
4766 }
4767 }
4768
4769 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4770 int length,
4771 TRAPS) const {
4772 assert(_need_verify, "only called when _need_verify is true");
4773 // Note: 0 <= length < 64K, as it comes from a u2 entry in the CP.
4774 if (!UTF8::is_legal_utf8(buffer, static_cast<size_t>(length), _major_version <= 47)) {
4775 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", THREAD);
4776 }
4777 }
4778
4779 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4780 // In class names, '/' separates unqualified names. This is verified in this function also.
4781 // Method names also may not contain the characters '<' or '>', unless <init>
4782 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4783 // method. Because these names have been checked as special cases before
4784 // calling this method in verify_legal_method_name.
4802 if (type == ClassFileParser::LegalClass) {
4803 if (p == name || p+1 >= name+length ||
4804 *(p+1) == JVM_SIGNATURE_SLASH) {
4805 return false;
4806 }
4807 } else {
4808 return false; // do not permit '/' unless it's class name
4809 }
4810 break;
4811 case JVM_SIGNATURE_SPECIAL:
4812 case JVM_SIGNATURE_ENDSPECIAL:
4813 // do not permit '<' or '>' in method names
4814 if (type == ClassFileParser::LegalMethod) {
4815 return false;
4816 }
4817 }
4818 }
4819 return true;
4820 }
4821
4822 bool ClassFileParser::is_class_in_loadable_descriptors_attribute(Symbol *klass) {
4823 if (_loadable_descriptors == nullptr) return false;
4824 for (int i = 0; i < _loadable_descriptors->length(); i++) {
4825 Symbol* class_name = _cp->symbol_at(_loadable_descriptors->at(i));
4826 if (class_name == klass) return true;
4827 }
4828 return false;
4829 }
4830
4831 // Take pointer to a UTF8 byte string (not NUL-terminated).
4832 // Skip over the longest part of the string that could
4833 // be taken as a fieldname. Allow non-trailing '/'s if slash_ok is true.
4834 // Return a pointer to just past the fieldname.
4835 // Return null if no fieldname at all was found, or in the case of slash_ok
4836 // being true, we saw consecutive slashes (meaning we were looking for a
4837 // qualified path but found something that was badly-formed).
4838 static const char* skip_over_field_name(const char* const name,
4839 bool slash_ok,
4840 unsigned int length) {
4841 const char* p;
4842 jboolean last_is_slash = false;
4843 jboolean not_first_ch = false;
4844
4845 for (p = name; p != name + length; not_first_ch = true) {
4846 const char* old_p = p;
4847 jchar ch = *p;
4848 if (ch < 128) {
4849 p++;
4850 // quick check for ascii
5077 } else {
5078 // 4881221: relax the constraints based on JSR202 spec
5079 legal = verify_unqualified_name(bytes, length, LegalMethod);
5080 }
5081 }
5082
5083 if (!legal) {
5084 ResourceMark rm(THREAD);
5085 assert(_class_name != nullptr, "invariant");
5086 // Names are all known to be < 64k so we know this formatted message is not excessively large.
5087 Exceptions::fthrow(
5088 THREAD_AND_LOCATION,
5089 vmSymbols::java_lang_ClassFormatError(),
5090 "Illegal method name \"%.*s\" in class %s", length, bytes,
5091 _class_name->as_C_string()
5092 );
5093 return;
5094 }
5095 }
5096
5097 bool ClassFileParser::legal_field_signature(const Symbol* signature, TRAPS) const {
5098 const char* const bytes = (const char*)signature->bytes();
5099 const unsigned int length = signature->utf8_length();
5100 const char* const p = skip_over_field_signature(bytes, false, length, CHECK_false);
5101
5102 if (p == nullptr || (p - bytes) != (int)length) {
5103 return false;
5104 }
5105 return true;
5106 }
5107
5108 // Checks if signature is a legal field signature.
5109 void ClassFileParser::verify_legal_field_signature(const Symbol* name,
5110 const Symbol* signature,
5111 TRAPS) const {
5112 if (!_need_verify) { return; }
5113
5114 if (!legal_field_signature(signature, THREAD)) {
5115 CLEAR_PENDING_EXCEPTION; // throw this exception instead
5116 throwIllegalSignature("Field", name, signature, CHECK);
5117 }
5118 }
5119
5120 // Check that the signature is compatible with the method name. For example,
5121 // check that <init> has a void signature.
5122 void ClassFileParser::verify_legal_name_with_signature(const Symbol* name,
5123 const Symbol* signature,
5124 TRAPS) const {
5125 if (!_need_verify) {
5126 return;
5127 }
5128
5129 // Class initializers cannot have args for class format version >= 51.
5130 if (name == vmSymbols::class_initializer_name() &&
5131 signature != vmSymbols::void_method_signature() &&
5132 _major_version >= JAVA_7_VERSION) {
5133 throwIllegalSignature("Method", name, signature, THREAD);
5134 return;
5135 }
5172 length -= pointer_delta_as_int(nextp, p);
5173 p = nextp;
5174 nextp = skip_over_field_signature(p, false, length, CHECK_0);
5175 }
5176 // The first non-signature thing better be a ')'
5177 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
5178 length--;
5179 // Now we better just have a return value
5180 nextp = skip_over_field_signature(p, true, length, CHECK_0);
5181 if (nextp && ((int)length == (nextp - p))) {
5182 return args_size;
5183 }
5184 }
5185 }
5186 // Report error
5187 throwIllegalSignature("Method", name, signature, THREAD);
5188 return 0;
5189 }
5190
5191 int ClassFileParser::static_field_size() const {
5192 assert(_layout_info != nullptr, "invariant");
5193 return _layout_info->_static_field_size;
5194 }
5195
5196 int ClassFileParser::total_oop_map_count() const {
5197 assert(_layout_info != nullptr, "invariant");
5198 return _layout_info->oop_map_blocks->_nonstatic_oop_map_count;
5199 }
5200
5201 jint ClassFileParser::layout_size() const {
5202 assert(_layout_info != nullptr, "invariant");
5203 return _layout_info->_instance_size;
5204 }
5205
5206 static void check_methods_for_intrinsics(const InstanceKlass* ik,
5207 const Array<Method*>* methods) {
5208 assert(ik != nullptr, "invariant");
5209 assert(methods != nullptr, "invariant");
5210
5211 // Set up Method*::intrinsic_id as soon as we know the names of methods.
5212 // (We used to do this lazily, but now we query it in Rewriter,
5213 // which is eagerly done for every method, so we might as well do it now,
5214 // when everything is fresh in memory.)
5215 const vmSymbolID klass_id = Method::klass_id_for_intrinsics(ik);
5216
5217 if (klass_id != vmSymbolID::NO_SID) {
5218 for (int j = 0; j < methods->length(); ++j) {
5219 Method* method = methods->at(j);
5220 method->init_intrinsic_id(klass_id);
5221
5222 if (CheckIntrinsics) {
5223 // Check if an intrinsic is defined for method 'method',
5298 }
5299 }
5300
5301 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5302 const ClassInstanceInfo& cl_inst_info,
5303 TRAPS) {
5304 if (_klass != nullptr) {
5305 return _klass;
5306 }
5307
5308 InstanceKlass* const ik =
5309 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5310
5311 if (is_hidden()) {
5312 mangle_hidden_class_name(ik);
5313 }
5314
5315 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5316
5317 assert(_klass == ik, "invariant");
5318 return ik;
5319 }
5320
5321 void ClassFileParser::create_acmp_maps(InstanceKlass* ik, TRAPS) {
5322 ik->set_acmp_maps_offset(_layout_info->_acmp_maps_offset);
5323 // Current format of acmp maps:
5324 // All maps are stored contiguously in a single int array because it might
5325 // be too early to instantiate an Object array (to be investigated)
5326 // Format is:
5327 // [number_of_nonoop_entries][offset0][size0][offset1][size1]...[oop_offset0][oop_offset1]...
5328 // ^ ^
5329 // | |
5330 // --------------------- Pair of integer describing a segment of
5331 // contiguous non-oop fields
5332 // First element is the number of segment of contiguous non-oop fields
5333 // Then, each segment of contiguous non-oop fields is described by two consecutive elements:
5334 // the offset then the size.
5335 // After the last segment of contiguous non-oop fields, oop fields are described, one element
5336 // per oop field, containing the offset of the field.
5337 int nonoop_acmp_map_size = _layout_info->_nonoop_acmp_map->length() * 2;
5338 int oop_acmp_map_size = _layout_info->_oop_acmp_map->length();
5339 int acmp_map_size = nonoop_acmp_map_size + oop_acmp_map_size + 1;
5340
5341 typeArrayOop map = oopFactory::new_intArray(acmp_map_size, CHECK);
5342 typeArrayHandle map_h(THREAD, map);
5343 Array<int>* acmp_maps_array = MetadataFactory::new_array<int>(loader_data(), acmp_map_size, CHECK);
5344
5345 map_h->int_at_put(0, _layout_info->_nonoop_acmp_map->length());
5346 acmp_maps_array->at_put(0, _layout_info->_nonoop_acmp_map->length());
5347 for (int i = 0; i < _layout_info->_nonoop_acmp_map->length(); i++) {
5348 map_h->int_at_put(i * 2 + 1, _layout_info->_nonoop_acmp_map->at(i)._offset);
5349 map_h->int_at_put(i * 2 + 2, _layout_info->_nonoop_acmp_map->at(i)._size);
5350
5351 // Also store acmp maps as metadata for regeneration when using dynamic archive or AOT training data.
5352 acmp_maps_array->at_put(i * 2 + 1, _layout_info->_nonoop_acmp_map->at(i)._offset);
5353 acmp_maps_array->at_put(i * 2 + 2, _layout_info->_nonoop_acmp_map->at(i)._size);
5354 }
5355 int oop_map_start = nonoop_acmp_map_size + 1;
5356 for (int i = 0; i < _layout_info->_oop_acmp_map->length(); i++) {
5357 map_h->int_at_put(oop_map_start + i, _layout_info->_oop_acmp_map->at(i));
5358 acmp_maps_array->at_put(oop_map_start + i, _layout_info->_oop_acmp_map->at(i));
5359 }
5360 assert(acmp_maps_array->length() == map_h->length(), "sanity");
5361 ik->java_mirror()->obj_field_put(ik->acmp_maps_offset(), map_h());
5362 ik->set_acmp_maps_array(acmp_maps_array);
5363 }
5364
5365 // See the declarations of _fast_acmp_offset and _fast_acmp_mask in InlineKlass::Members
5366 // for details about the fast path logic, and the meaning of these values.
5367 void ClassFileParser::set_fast_acmp_members(InlineKlass* vk) const {
5368 if (_layout_info->_oop_acmp_map->length() > 0) { // Oops are not allowed in the fast path
5369 return;
5370 }
5371
5372 int64_t mask = 0;
5373 #ifndef VM_LITTLE_ENDIAN
5374 // Leaving the mask and offset to default values (that is just "return;") is a correct and easy way to implement
5375 // this for other endianness, but it will have a runtime cost in do_acmp, without the benefit. It is better, and
5376 // probably easy with access to such an architecture, to adapt the logic below
5377 // for big-endian architectures, but filling the mask from the other end. I prefer not to do it blindly.
5378 vk->set_fast_acmp_offset(0);
5379 vk->set_fast_acmp_mask(mask);
5380
5381 #else
5382
5383 // Compute the mask for a memory zone with offset "start" and of size "size", both in bytes,
5384 // assuming it won't overflow the long. In little endian, the byte with smallest address/smallest offset/closest from header
5385 // will be the least significant byte.
5386 // Value Memory layout
5387 // make_mask_piece(0, 1) = 0x0000 0000 0000 00ff => ff | 00 | 00 | 00 | 00 | 00 | 00 | 00
5388 // make_mask_piece(0, 1) = 0x0000 0000 0000 ffff => ff | ff | 00 | 00 | 00 | 00 | 00 | 00
5389 // make_mask_piece(1, 1) = 0x0000 0000 0000 ff00 => 00 | ff | 00 | 00 | 00 | 00 | 00 | 00
5390 // make_mask_piece(1, 3) = 0x0000 0000 ffff ff00 => 00 | ff | ff | ff | 00 | 00 | 00 | 00
5391 auto make_mask_piece = [](int start, int size) -> int64_t { return right_n_bits<int64_t>(size * BitsPerByte) << (start * BitsPerByte); };
5392
5393 // We build the mask for a 64-bit load from the start of the payload. For each contiguous piece of memory
5394 // we build the mask containing 1's where it would be in the loaded long: it must be as
5395 // long as the memory that is being accessed, but the placement depends on the endianness.
5396 for (int i = 0; i < _layout_info->_nonoop_acmp_map->length(); i++) {
5397 int piece_start = _layout_info->_nonoop_acmp_map->at(i)._offset - _layout_info->_payload_offset;
5398 int piece_size = _layout_info->_nonoop_acmp_map->at(i)._size;
5399 int piece_end = piece_start + piece_size - 1;
5400 if (piece_end >= BytesPerLong) { // Too far! Can't fit in an 8-byte load, fast path will not be taken
5401 return;
5402 }
5403 int64_t mask_piece = make_mask_piece(piece_start, piece_size);
5404 mask |= mask_piece;
5405 }
5406
5407 // If the payload is smaller than 64 bits, reading a long after the header can lead to an over-read. To avoid that, we can move the
5408 // load to a lower offset (toward the beginning of the object), and adjust the mask accordingly, even if it means reading (part of)
5409 // the header: the mask will filter that out.
5410 // Since an object cannot be less than 8 bytes, it's surely safe.
5411 if (mask == 0) {
5412 // Special case: empty object. There is nothing to compare, and no payload. We can just read from the start
5413 // of the header to ensure we load within the object. We can't use the general case: count_leading_zeros/count_trailing_zeros doesn't
5414 // accept null argument. This case is endianness-agnostic.
5415 vk->set_fast_acmp_offset(0);
5416 vk->set_fast_acmp_mask(mask);
5417 } else {
5418 // In little endian, if the payload is not a full 64 bits, the mask will have leading 0s (most significant bytes are bytes with
5419 // higher addresses/higher offset/further from the header). We can shift the mask so that there aren't leading 0s anymore, and
5420 // decrease the offset by the same amount.
5421 // For instance, let's say _payload_offset is 16, and the mask is 0x0000 ffff ffff 00ff, we have 16 leading 0s
5422 // (the bubble at offset 1 is fine: it might be due to padding). Reading from the start of the payload might read 2 bytes
5423 // after the end of the payload. To avoid that, we are going to shift the payload by 16 bits, and remove 2 bytes
5424 // from the offset we should load from. At the end, fast_acmp_offset will be 14 and the mask 0xffff ffff 00ff 0000.
5425 // The lowest 16 0s introduced by the shift will filter out the 2 bytes we read from the header.
5426 // Big endian architectures probably need to look at count_trailing_zeros, and shift right until the last bit is 1.
5427 // The offset still needs to be decreased.
5428 int leading_zeroes = static_cast<int>(count_leading_zeros(mask));
5429 assert(leading_zeroes % BitsPerByte == 0, "we should mask full bytes");
5430 mask <<= leading_zeroes;
5431 assert(count_leading_zeros(mask) == 0, "fast acmp mask can be moved further!");
5432 int offset = _layout_info->_payload_offset - leading_zeroes / BitsPerByte;
5433 assert(offset >= 0, "fast acmp path shouldn't load before the object");
5434 vk->set_fast_acmp_offset(offset);
5435 vk->set_fast_acmp_mask(mask);
5436 }
5437 #endif // VM_LITTLE_ENDIAN
5438 }
5439
5440 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5441 bool changed_by_loadhook,
5442 const ClassInstanceInfo& cl_inst_info,
5443 TRAPS) {
5444 assert(ik != nullptr, "invariant");
5445
5446 // Set name and CLD before adding to CLD
5447 ik->set_class_loader_data(_loader_data);
5448 ik->set_class_loader_type();
5449 ik->set_name(_class_name);
5450
5451 // Add all classes to our internal class loader list here,
5452 // including classes in the bootstrap (null) class loader.
5453 const bool publicize = !is_internal();
5454
5455 _loader_data->add_class(ik, publicize);
5456
5457 set_klass_to_deallocate(ik);
5458
5459 assert(_layout_info != nullptr, "invariant");
5460 assert(ik->static_field_size() == _layout_info->_static_field_size, "sanity");
5461 assert(ik->nonstatic_oop_map_count() == _layout_info->oop_map_blocks->_nonstatic_oop_map_count,
5462 "sanity");
5463
5464 assert(ik->is_instance_klass(), "sanity");
5465 assert(ik->size_helper() == _layout_info->_instance_size, "sanity");
5466
5467 // Fill in information already parsed
5468 ik->set_should_verify_class(_need_verify);
5469
5470 // Not yet: supers are done below to support the new subtype-checking fields
5471 ik->set_nonstatic_field_size(_layout_info->_nonstatic_field_size);
5472 ik->set_has_nonstatic_fields(_layout_info->_has_nonstatic_fields);
5473 ik->set_has_strict_static_fields(_has_strict_static_fields);
5474
5475 if (_layout_info->_is_naturally_atomic) {
5476 ik->set_is_naturally_atomic();
5477 }
5478
5479 if (_layout_info->_must_be_atomic) {
5480 ik->set_must_be_atomic();
5481 }
5482
5483 ik->set_static_oop_field_count(_static_oop_count);
5484
5485 if (_has_null_restricted_static_fields) {
5486 ik->set_has_null_restricted_static_fields();
5487 for (int i = 0; i < _temp_field_info->length(); i++) {
5488 FieldInfo& fieldinfo = _temp_field_info->at(i);
5489 if (fieldinfo.access_flags().is_static() && fieldinfo.field_flags().is_null_free_inline_type()) {
5490 Symbol* sig = fieldinfo.signature(_cp);
5491 assert(Signature::has_envelope(sig), "Must already have been checked");
5492 TempNewSymbol name = Signature::strip_envelope(sig);
5493 if (name == _class_name) {
5494 // Replace the nullptr previously stored now that we have the InstanceKlass for this klass.
5495 _inline_layout_info_array->adr_at(fieldinfo.index())->set_klass(InlineKlass::cast(ik));
5496 }
5497 assert(_inline_layout_info_array->adr_at(fieldinfo.index())->klass()->is_inline_klass(), "Must be");
5498 }
5499 }
5500 }
5501
5502 // this transfers ownership of a lot of arrays from
5503 // the parser onto the InstanceKlass*
5504 apply_parsed_class_metadata(ik, _java_fields_count);
5505
5506 // can only set dynamic nest-host after static nest information is set
5507 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5508 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5509 }
5510
5511 // note that is not safe to use the fields in the parser from this point on
5512 assert(nullptr == _cp, "invariant");
5513 assert(nullptr == _fieldinfo_stream, "invariant");
5514 assert(nullptr == _fieldinfo_search_table, "invariant");
5515 assert(nullptr == _fields_status, "invariant");
5516 assert(nullptr == _methods, "invariant");
5517 assert(nullptr == _inner_classes, "invariant");
5518 assert(nullptr == _nest_members, "invariant");
5519 assert(nullptr == _loadable_descriptors, "invariant");
5520 assert(nullptr == _combined_annotations, "invariant");
5521 assert(nullptr == _record_components, "invariant");
5522 assert(nullptr == _permitted_subclasses, "invariant");
5523 assert(nullptr == _inline_layout_info_array, "invariant");
5524
5525 if (_has_localvariable_table) {
5526 ik->set_has_localvariable_table(true);
5527 }
5528
5529 if (_has_final_method) {
5530 ik->set_has_final_method();
5531 }
5532
5533 ik->copy_method_ordering(_method_ordering, CHECK);
5534 // The InstanceKlass::_methods_jmethod_ids cache
5535 // is managed on the assumption that the initial cache
5536 // size is equal to the number of methods in the class. If
5537 // that changes, then InstanceKlass::idnum_can_increment()
5538 // has to be changed accordingly.
5539 ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));
5540
5541 ik->set_this_class_index(_this_class_index);
5542
5543 if (_is_hidden) {
5580 if ((_num_miranda_methods > 0) ||
5581 // if this class introduced new miranda methods or
5582 (_super_klass != nullptr && _super_klass->has_miranda_methods())
5583 // super class exists and this class inherited miranda methods
5584 ) {
5585 ik->set_has_miranda_methods(); // then set a flag
5586 }
5587
5588 // Fill in information needed to compute superclasses.
5589 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5590 ik->set_transitive_interfaces(_transitive_interfaces);
5591 ik->set_local_interfaces(_local_interfaces);
5592 _transitive_interfaces = nullptr;
5593 _local_interfaces = nullptr;
5594
5595 // Initialize itable offset tables
5596 klassItable::setup_itable_offset_table(ik);
5597
5598 // Compute transitive closure of interfaces this class implements
5599 // Do final class setup
5600 OopMapBlocksBuilder* oop_map_blocks = _layout_info->oop_map_blocks;
5601 if (oop_map_blocks->_nonstatic_oop_map_count > 0) {
5602 oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5603 }
5604
5605 if (_has_contended_fields || _parsed_annotations->is_contended() ||
5606 ( _super_klass != nullptr && _super_klass->has_contended_annotations())) {
5607 ik->set_has_contended_annotations(true);
5608 }
5609
5610 // Fill in has_finalizer and layout_helper
5611 set_precomputed_flags(ik);
5612
5613 // check if this class can access its super class
5614 check_super_class_access(ik, CHECK);
5615
5616 // check if this class can access its superinterfaces
5617 check_super_interface_access(ik, CHECK);
5618
5619 // check if this class overrides any final method
5620 check_final_method_override(ik, CHECK);
5641
5642 assert(_all_mirandas != nullptr, "invariant");
5643
5644 // Generate any default methods - default methods are public interface methods
5645 // that have a default implementation. This is new with Java 8.
5646 if (_has_nonstatic_concrete_methods) {
5647 DefaultMethods::generate_default_methods(ik,
5648 _all_mirandas,
5649 CHECK);
5650 }
5651
5652 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5653 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5654 !module_entry->has_default_read_edges()) {
5655 if (!module_entry->set_has_default_read_edges()) {
5656 // We won a potential race
5657 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5658 }
5659 }
5660
5661 if (is_inline_type()) {
5662 InlineKlass* vk = InlineKlass::cast(ik);
5663 vk->set_payload_alignment(_layout_info->_payload_alignment);
5664 vk->set_payload_offset(_layout_info->_payload_offset);
5665 vk->set_payload_size_in_bytes(_layout_info->_payload_size_in_bytes);
5666 vk->set_null_free_non_atomic_size_in_bytes(_layout_info->_null_free_non_atomic_size_in_bytes);
5667 vk->set_null_free_non_atomic_alignment(_layout_info->_null_free_non_atomic_alignment);
5668 vk->set_null_free_atomic_size_in_bytes(_layout_info->_null_free_atomic_layout_size_in_bytes);
5669 vk->set_nullable_atomic_size_in_bytes(_layout_info->_nullable_atomic_layout_size_in_bytes);
5670 vk->set_nullable_non_atomic_size_in_bytes(_layout_info->_nullable_non_atomic_layout_size_in_bytes);
5671 vk->set_null_marker_offset(_layout_info->_null_marker_offset);
5672 vk->set_null_reset_value_offset(_layout_info->_null_reset_value_offset);
5673 if (_layout_info->_is_empty_inline_klass) vk->set_is_empty_inline_type();
5674
5675 if (UseAcmpFastPath) {
5676 set_fast_acmp_members(vk);
5677 }
5678
5679 vk->initialize_calling_convention(CHECK);
5680 }
5681
5682 if (Arguments::is_valhalla_enabled() && !access_flags().is_identity_class() && !access_flags().is_interface()
5683 && _class_name != vmSymbols::java_lang_Object()) {
5684 // Both abstract and concrete value classes need a field map for acmp
5685 create_acmp_maps(ik, CHECK);
5686 }
5687
5688 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5689
5690 if (!is_internal()) {
5691 ik->print_class_load_logging(_loader_data, module_entry, _stream);
5692 if (CDSConfig::is_dumping_archive()) {
5693 SystemDictionaryShared::check_code_source(ik, _stream);
5694 }
5695
5696 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5697 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5698 log_is_enabled(Info, class, preview)) {
5699 ResourceMark rm;
5700 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5701 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5702 }
5703
5704 if (log_is_enabled(Debug, class, resolve)) {
5705 ResourceMark rm;
5706 // print out the superclass.
5707 const char * from = ik->external_name();
5751 const ClassLoadInfo* cl_info,
5752 Publicity pub_level,
5753 TRAPS) :
5754 _stream(stream),
5755 _class_name(nullptr),
5756 _loader_data(loader_data),
5757 _is_hidden(cl_info->is_hidden()),
5758 _can_access_vm_annotations(cl_info->can_access_vm_annotations()),
5759 _orig_cp_size(0),
5760 _static_oop_count(0),
5761 _super_klass(),
5762 _cp(nullptr),
5763 _fieldinfo_stream(nullptr),
5764 _fieldinfo_search_table(nullptr),
5765 _fields_status(nullptr),
5766 _methods(nullptr),
5767 _inner_classes(nullptr),
5768 _nest_members(nullptr),
5769 _nest_host(0),
5770 _permitted_subclasses(nullptr),
5771 _loadable_descriptors(nullptr),
5772 _record_components(nullptr),
5773 _local_interfaces(nullptr),
5774 _transitive_interfaces(nullptr),
5775 _combined_annotations(nullptr),
5776 _class_annotations(nullptr),
5777 _class_type_annotations(nullptr),
5778 _fields_annotations(nullptr),
5779 _fields_type_annotations(nullptr),
5780 _klass(nullptr),
5781 _klass_to_deallocate(nullptr),
5782 _parsed_annotations(nullptr),
5783 _layout_info(nullptr),
5784 _inline_layout_info_array(nullptr),
5785 _temp_field_info(nullptr),
5786 _method_ordering(nullptr),
5787 _all_mirandas(nullptr),
5788 _vtable_size(0),
5789 _itable_size(0),
5790 _num_miranda_methods(0),
5791 _protection_domain(cl_info->protection_domain()),
5792 _access_flags(),
5793 _pub_level(pub_level),
5794 _bad_constant_seen(0),
5795 _synthetic_flag(false),
5796 _sde_length(false),
5797 _sde_buffer(nullptr),
5798 _sourcefile_index(0),
5799 _generic_signature_index(0),
5800 _major_version(0),
5801 _minor_version(0),
5802 _this_class_index(0),
5803 _super_class_index(0),
5804 _itfs_len(0),
5805 _java_fields_count(0),
5806 _need_verify(false),
5807 _has_nonstatic_concrete_methods(false),
5808 _declares_nonstatic_concrete_methods(false),
5809 _has_localvariable_table(false),
5810 _has_final_method(false),
5811 _has_contended_fields(false),
5812 _has_aot_runtime_setup_method(false),
5813 _has_strict_static_fields(false),
5814 _has_null_restricted_static_fields(false),
5815 _must_be_atomic(true),
5816 _has_finalizer(false),
5817 _has_empty_finalizer(false),
5818 _max_bootstrap_specifier_index(-1) {
5819
5820 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5821 _class_name->increment_refcount();
5822
5823 assert(_loader_data != nullptr, "invariant");
5824 assert(stream != nullptr, "invariant");
5825 assert(_stream != nullptr, "invariant");
5826 assert(_stream->buffer() == _stream->current(), "invariant");
5827 assert(_class_name != nullptr, "invariant");
5828 assert(0 == _access_flags.as_unsigned_short(), "invariant");
5829
5830 // Figure out whether we can skip format checking (matching classic VM behavior)
5831 // Always verify CFLH bytes from the user agents.
5832 _need_verify = stream->from_class_file_load_hook() ? true : Verifier::should_verify_for(_loader_data->class_loader());
5833
5834 // synch back verification state to stream to check for truncation.
5835 stream->set_need_verify(_need_verify);
5836
5837 parse_stream(stream, CHECK);
5838
5839 post_process_parsed_stream(stream, _cp, CHECK);
5840 }
5841
5842 void ClassFileParser::clear_class_metadata() {
5843 // metadata created before the instance klass is created. Must be
5844 // deallocated if classfile parsing returns an error.
5845 _cp = nullptr;
5846 _fieldinfo_stream = nullptr;
5847 _fieldinfo_search_table = nullptr;
5848 _fields_status = nullptr;
5849 _methods = nullptr;
5850 _inner_classes = nullptr;
5851 _nest_members = nullptr;
5852 _permitted_subclasses = nullptr;
5853 _loadable_descriptors = nullptr;
5854 _combined_annotations = nullptr;
5855 _class_annotations = _class_type_annotations = nullptr;
5856 _fields_annotations = _fields_type_annotations = nullptr;
5857 _record_components = nullptr;
5858 _inline_layout_info_array = nullptr;
5859 }
5860
5861 // Destructor to clean up
5862 ClassFileParser::~ClassFileParser() {
5863 _class_name->decrement_refcount();
5864
5865 if (_cp != nullptr) {
5866 MetadataFactory::free_metadata(_loader_data, _cp);
5867 }
5868
5869 if (_fieldinfo_stream != nullptr) {
5870 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_stream);
5871 }
5872 MetadataFactory::free_array<u1>(_loader_data, _fieldinfo_search_table);
5873
5874 if (_fields_status != nullptr) {
5875 MetadataFactory::free_array<FieldStatus>(_loader_data, _fields_status);
5876 }
5877
5878 if (_inline_layout_info_array != nullptr) {
5879 MetadataFactory::free_array<InlineLayoutInfo>(_loader_data, _inline_layout_info_array);
5880 }
5881
5882 if (_methods != nullptr) {
5883 // Free methods
5884 InstanceKlass::deallocate_methods(_loader_data, _methods);
5885 }
5886
5887 // beware of the Universe::empty_blah_array!!
5888 if (_inner_classes != nullptr && _inner_classes != Universe::the_empty_short_array()) {
5889 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5890 }
5891
5892 if (_nest_members != nullptr && _nest_members != Universe::the_empty_short_array()) {
5893 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5894 }
5895
5896 if (_record_components != nullptr) {
5897 InstanceKlass::deallocate_record_components(_loader_data, _record_components);
5898 }
5899
5900 if (_permitted_subclasses != nullptr && _permitted_subclasses != Universe::the_empty_short_array()) {
5901 MetadataFactory::free_array<u2>(_loader_data, _permitted_subclasses);
5902 }
5903
5904 if (_loadable_descriptors != nullptr && _loadable_descriptors != Universe::the_empty_short_array()) {
5905 MetadataFactory::free_array<u2>(_loader_data, _loadable_descriptors);
5906 }
5907
5908 // Free interfaces
5909 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5910 _local_interfaces, _transitive_interfaces);
5911
5912 if (_combined_annotations != nullptr) {
5913 // After all annotations arrays have been created, they are installed into the
5914 // Annotations object that will be assigned to the InstanceKlass being created.
5915
5916 // Deallocate the Annotations object and the installed annotations arrays.
5917 _combined_annotations->deallocate_contents(_loader_data);
5918
5919 // If the _combined_annotations pointer is non-null,
5920 // then the other annotations fields should have been cleared.
5921 assert(_class_annotations == nullptr, "Should have been cleared");
5922 assert(_class_type_annotations == nullptr, "Should have been cleared");
5923 assert(_fields_annotations == nullptr, "Should have been cleared");
5924 assert(_fields_type_annotations == nullptr, "Should have been cleared");
5925 } else {
5926 // If the annotations arrays were not installed into the Annotations object,
5927 // then they have to be deallocated explicitly.
5987
5988 assert(cp_size == (u2)cp->length(), "invariant");
5989
5990 // ACCESS FLAGS
5991 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5992
5993 // Access flags
5994 u2 flags;
5995 // JVM_ACC_MODULE is defined in JDK-9 and later.
5996 if (_major_version >= JAVA_9_VERSION) {
5997 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5998 } else {
5999 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
6000 }
6001
6002 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6003 // Set abstract bit for old class files for backward compatibility
6004 flags |= JVM_ACC_ABSTRACT;
6005 }
6006
6007 // Fixing ACC_SUPER/ACC_IDENTITY for old class files
6008 if (!supports_inline_types()) {
6009 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
6010 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
6011 if (!is_module && !is_interface) {
6012 flags |= JVM_ACC_IDENTITY;
6013 }
6014 }
6015
6016 verify_legal_class_modifiers(flags, nullptr, false, CHECK);
6017
6018 short bad_constant = class_bad_constant_seen();
6019 if (bad_constant != 0) {
6020 // Do not throw CFE until after the access_flags are checked because if
6021 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6022 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, THREAD);
6023 return;
6024 }
6025
6026 _access_flags.set_flags(flags);
6027
6028 // This class and superclass
6029 _this_class_index = stream->get_u2_fast();
6030 guarantee_property(
6031 valid_cp_range(_this_class_index, cp_size) &&
6032 cp->tag_at(_this_class_index).is_unresolved_klass(),
6033 "Invalid this class index %u in constant pool in class file %s",
6034 _this_class_index, CHECK);
6035
6103
6104 // SUPERKLASS
6105 _super_class_index = stream->get_u2_fast();
6106 check_super_class(cp,
6107 _super_class_index,
6108 _need_verify,
6109 CHECK);
6110
6111 // Interfaces
6112 _itfs_len = stream->get_u2_fast();
6113 parse_interfaces(stream,
6114 _itfs_len,
6115 cp,
6116 &_has_nonstatic_concrete_methods,
6117 CHECK);
6118
6119 assert(_local_interfaces != nullptr, "invariant");
6120
6121 // Fields (offsets are filled in later)
6122 parse_fields(stream,
6123 _access_flags,
6124 cp,
6125 cp_size,
6126 &_java_fields_count,
6127 CHECK);
6128
6129 assert(_temp_field_info != nullptr, "invariant");
6130
6131 // Methods
6132 parse_methods(stream,
6133 _access_flags.is_interface(),
6134 &_has_localvariable_table,
6135 &_has_final_method,
6136 &_declares_nonstatic_concrete_methods,
6137 CHECK);
6138
6139 assert(_methods != nullptr, "invariant");
6140
6141 if (_declares_nonstatic_concrete_methods) {
6142 _has_nonstatic_concrete_methods = true;
6143 }
6223 // errors not checked yet.
6224 guarantee_property(super_class_name == vmSymbols::java_lang_Object(),
6225 "Interfaces must have java.lang.Object as superclass in class file %s",
6226 CHECK);
6227 }
6228 Handle loader(THREAD, _loader_data->class_loader());
6229 if (loader.is_null() && super_class_name == vmSymbols::java_lang_Object()) {
6230 // fast path to avoid lookup
6231 _super_klass = vmClasses::Object_klass();
6232 } else {
6233 _super_klass = (const InstanceKlass*)
6234 SystemDictionary::resolve_super_or_fail(_class_name,
6235 super_class_name,
6236 loader,
6237 true,
6238 CHECK);
6239 }
6240 }
6241
6242 if (_super_klass != nullptr) {
6243 if (_super_klass->is_interface()) {
6244 classfile_icce_error("class %s has interface %s as super class", _super_klass, THREAD);
6245 return;
6246 }
6247
6248 if (_super_klass->is_final()) {
6249 classfile_icce_error("class %s cannot inherit from final class %s", _super_klass, THREAD);
6250 return;
6251 }
6252
6253 if (Arguments::is_valhalla_enabled()) {
6254 // If this class is a value class, its super class cannot be an identity class unless it is java.lang.Object.
6255 if (_super_klass->access_flags().is_identity_class() && !access_flags().is_identity_class() &&
6256 _super_klass != vmClasses::Object_klass()) {
6257 ResourceMark rm(THREAD);
6258 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
6259 err_msg("Value type %s has an identity type as supertype", _class_name->as_C_string()));
6260 }
6261 }
6262
6263 if (_super_klass->has_nonstatic_concrete_methods()) {
6264 _has_nonstatic_concrete_methods = true;
6265 }
6266 }
6267
6268 if (_parsed_annotations->has_annotation(AnnotationCollector::_jdk_internal_LooselyConsistentValue) && _access_flags.is_identity_class()) {
6269 THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
6270 err_msg("class %s cannot have annotation jdk.internal.vm.annotation.LooselyConsistentValue, because it is not a value class",
6271 _class_name->as_klass_external_name()));
6272 }
6273
6274 // Determining if the class allows tearing or not (default is not)
6275 if (Arguments::is_valhalla_enabled() && !_access_flags.is_identity_class()) {
6276 if (_parsed_annotations->has_annotation(ClassAnnotationCollector::_jdk_internal_LooselyConsistentValue)
6277 && (_super_klass == vmClasses::Object_klass() || !_super_klass->must_be_atomic())) {
6278 // Conditions above are not sufficient to determine atomicity requirements.
6279 // Fields with atomicity requirements could force the current class to have atomicity requirements too.
6280 // Mark as not needing atomicity for now - can be updated when computing the fields layout.
6281 // The InstanceKlass must be filled with the value from the FieldLayoutInfo returned by
6282 // the FieldLayoutBuilder, not with this _must_be_atomic field.
6283 _must_be_atomic = false;
6284 }
6285 // Apply VM options override
6286 if (*ForceNonTearable != '\0') {
6287 // Allow a command line switch to force the same atomicity property:
6288 ResourceMark rm(THREAD);
6289 const char* class_name_str = _class_name->as_C_string();
6290 if (StringUtils::class_list_match(ForceNonTearable, class_name_str)) {
6291 _must_be_atomic = true;
6292 }
6293 }
6294 }
6295
6296 // Compute the transitive list of all unique interfaces implemented by this class
6297 _transitive_interfaces =
6298 compute_transitive_interfaces(_super_klass,
6299 _local_interfaces,
6300 _loader_data,
6301 CHECK);
6302
6303 assert(_transitive_interfaces != nullptr, "invariant");
6304
6305 // sort methods
6306 _method_ordering = sort_methods(_methods);
6307
6308 _all_mirandas = new GrowableArray<Method*>(20);
6309
6310 Handle loader(THREAD, _loader_data->class_loader());
6311 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6312 &_num_miranda_methods,
6313 _all_mirandas,
6314 _super_klass,
6315 _methods,
6316 _access_flags,
6317 _major_version,
6318 loader,
6319 _class_name,
6320 _local_interfaces);
6321
6322 // Size of Java itable (in words)
6323 _itable_size = is_interface() ? 0 :
6324 klassItable::compute_itable_size(_transitive_interfaces);
6325
6326 assert(_parsed_annotations != nullptr, "invariant");
6327
6328 if (Arguments::is_valhalla_enabled()) {
6329 fetch_field_classes(cp, CHECK);
6330 }
6331
6332 _layout_info = new FieldLayoutInfo();
6333 FieldLayoutBuilder lb(class_name(), loader_data(), super_klass(), _cp, /*_fields*/ _temp_field_info,
6334 _parsed_annotations->is_contended(), is_inline_type(),
6335 access_flags().is_abstract() && !access_flags().is_identity_class() && !access_flags().is_interface(),
6336 _must_be_atomic, _layout_info, _inline_layout_info_array);
6337 lb.build_layout();
6338
6339 // If it turned out that we didn't inline any of the fields, we deallocate
6340 // the array of InlineLayoutInfo since it isn't needed, and so it isn't
6341 // transferred to the allocated InstanceKlass.
6342 if (_inline_layout_info_array != nullptr && !(_layout_info->_has_inlined_fields || _has_null_restricted_static_fields)) {
6343 MetadataFactory::free_array<InlineLayoutInfo>(_loader_data, _inline_layout_info_array);
6344 _inline_layout_info_array = nullptr;
6345 }
6346
6347 int injected_fields_count = _temp_field_info->length() - _java_fields_count;
6348 _fieldinfo_stream =
6349 FieldInfoStream::create_FieldInfoStream(_temp_field_info, _java_fields_count,
6350 injected_fields_count, loader_data(), CHECK);
6351 _fieldinfo_search_table = FieldInfoStream::create_search_table(_cp, _fieldinfo_stream, _loader_data, CHECK);
6352 _fields_status =
6353 MetadataFactory::new_array<FieldStatus>(_loader_data, _temp_field_info->length(),
6354 FieldStatus(0), CHECK);
6355
6356 // Strict static fields track initialization status from the beginning of time.
6357 // After this class runs <clinit>, they will be verified as being "not unset".
6358 // See Step 8 of InstanceKlass::initialize_impl.
6359 if (_has_strict_static_fields) {
6360 bool found_one = false;
6361 for (int i = 0; i < _temp_field_info->length(); i++) {
6362 FieldInfo& fi = *_temp_field_info->adr_at(i);
6363 if (fi.access_flags().is_strict() && fi.access_flags().is_static()) {
6364 found_one = true;
6365 if (fi.initializer_index() != 0) {
6366 // skip strict static fields with ConstantValue attributes
6367 } else {
6368 _fields_status->adr_at(fi.index())->update_strict_static_unset(true);
6369 _fields_status->adr_at(fi.index())->update_strict_static_unread(true);
6370 }
6371 }
6372 }
6373 assert(found_one == _has_strict_static_fields,
6374 "correct prediction = %d", (int)_has_strict_static_fields);
6375 }
6376 }
6377
6378 // In order to be able to optimize field layouts by applying heap flattening,
6379 // the JVM needs to know the layout of the class of the fields, which implies
6380 // having these classes loaded. The strategy has two folds:
6381 // 1 - if the current class has a LoadableDescriptors attribute containing the
6382 // name of the class of the field and PreloadClasses is true, the JVM will
6383 // try to speculatively load this class. Failures to load the class are
6384 // silently discarded, and loads resulting in a non-optimizable class
6385 // are ignored
6386 // 2 - if step 1 cannot be applied, the JVM will simply check if the class
6387 // loader of the current class already knows these classes (no class
6388 // loading triggered in this case). Note that the migrated value classes
6389 // of the JDK are automatically registered to all class loaders, and
6390 // are guaranteed to be found in this step even if the current class
6391 // has not been recompiled with JEP 401 features enabled
6392 void ClassFileParser::fetch_field_classes(ConstantPool* cp, TRAPS) {
6393 for (int i = 0; i < _temp_field_info->length(); i++) {
6394 FieldInfo& fieldinfo = _temp_field_info->at(i);
6395 if (fieldinfo.access_flags().is_static() && !fieldinfo.field_flags().is_null_free_inline_type()) continue;
6396 Symbol* sig = fieldinfo.signature(cp);
6397 if (Signature::has_envelope(sig)) {
6398 TempNewSymbol name = Signature::strip_envelope(sig);
6399 if (name == _class_name) {
6400 if (fieldinfo.field_flags().is_null_free_inline_type() && !is_inline_type()) {
6401 fieldinfo.field_flags_addr()->update_null_free_inline_type(false);
6402 } else {
6403 // Dummy setting to trigger the allocation of the inline_layout_info array -
6404 // the real pointer will be set later in ::fill_instance_klass, once the InlineKlass has been allocated.
6405 set_inline_layout_info_klass(fieldinfo.index(), nullptr, CHECK);
6406 }
6407 continue;
6408 }
6409 if (PreloadClasses && is_class_in_loadable_descriptors_attribute(sig)) {
6410 ResourceMark rm(THREAD);
6411 log_info(class, preload)("Preloading of class %s during loading of class %s. "
6412 "Cause: field type in LoadableDescriptors attribute",
6413 name->as_C_string(), _class_name->as_C_string());
6414 oop loader = loader_data()->class_loader();
6415 InstanceKlass* klass = SystemDictionary::resolve_super_or_fail(_class_name, name,
6416 Handle(THREAD, loader),
6417 false, THREAD);
6418
6419 assert((klass == nullptr) == HAS_PENDING_EXCEPTION, "Must be the same");
6420
6421 if (klass != nullptr) {
6422 if (klass->is_inline_klass()) {
6423 set_inline_layout_info_klass(fieldinfo.index(), InlineKlass::cast(klass), CHECK);
6424 log_info(class, preload)("Preloading of class %s during loading of class %s "
6425 "(cause: field type in LoadableDescriptors attribute) succeeded",
6426 name->as_C_string(), _class_name->as_C_string());
6427 } else {
6428 // Non value classes are allowed by the current spec, but it could be an indication of an issue so let's log this
6429 log_info(class, preload)("Preloading of class %s during loading of class %s "
6430 "(cause: field type in LoadableDescriptors attribute) but loaded class is not a value class",
6431 name->as_C_string(), _class_name->as_C_string());
6432 if (fieldinfo.field_flags().is_null_free_inline_type()) {
6433 log_warning(class, preload)("After preloading of class %s during loading of class %s "
6434 "field was annotated with @NullRestricted but loaded class is not a value class, "
6435 "the annotation is ignored",
6436 name->as_C_string(), _class_name->as_C_string());
6437 fieldinfo.field_flags_addr()->update_null_free_inline_type(false);
6438 }
6439 }
6440 } else {
6441 log_info(class, preload)("Preloading of class %s during loading of class %s "
6442 "(cause: field type in LoadableDescriptors attribute) failed : %s",
6443 name->as_C_string(), _class_name->as_C_string(),
6444 PENDING_EXCEPTION->klass()->name()->as_C_string());
6445 if (fieldinfo.field_flags().is_null_free_inline_type()) {
6446 log_warning(class, preload)("After preloading of class %s during loading of class %s failed,"
6447 "field was annotated with @NullRestricted but class is unknown, "
6448 "the annotation is ignored",
6449 name->as_C_string(), _class_name->as_C_string());
6450 fieldinfo.field_flags_addr()->update_null_free_inline_type(false);
6451 }
6452
6453 // Loads triggered by the LoadableDescriptors attribute are speculative, failures must not
6454 // impact loading of current class.
6455 CLEAR_PENDING_EXCEPTION;
6456 }
6457 } else {
6458 oop loader = loader_data()->class_loader();
6459 InstanceKlass* klass = SystemDictionary::find_instance_klass(THREAD, name, Handle(THREAD, loader));
6460 if (klass != nullptr && klass->is_inline_klass()) {
6461 set_inline_layout_info_klass(fieldinfo.index(), InlineKlass::cast(klass), CHECK);
6462 ResourceMark rm(THREAD);
6463 log_info(class, preload)("During loading of class %s , class %s found in local system dictionary"
6464 "(field type not in LoadableDescriptors attribute)",
6465 _class_name->as_C_string(), name->as_C_string());
6466 } else if (fieldinfo.field_flags().is_null_free_inline_type()) {
6467 ResourceMark rm(THREAD);
6468 if (klass == nullptr) {
6469 log_warning(class, preload)("During loading of class %s, class %s is unknown, "
6470 "but a field of this type was annotated with @NullRestricted, "
6471 "the annotation is ignored",
6472 _class_name->as_C_string(), name->as_C_string());
6473 } else {
6474 log_warning(class, preload)("During loading of class %s, class %s was found in the local system dictionary "
6475 "and is not a concrete value class, but a field of this type was annotated with "
6476 "@NullRestricted, the annotation is ignored",
6477 _class_name->as_C_string(), name->as_C_string());
6478 }
6479 fieldinfo.field_flags_addr()->update_null_free_inline_type(false);
6480 }
6481 }
6482 }
6483 }
6484 }
6485
6486 void ClassFileParser::set_klass(InstanceKlass* klass) {
6487
6488 #ifdef ASSERT
6489 if (klass != nullptr) {
6490 assert(nullptr == _klass, "leaking?");
6491 }
6492 #endif
6493
6494 _klass = klass;
6495 }
6496
6497 void ClassFileParser::set_inline_layout_info_klass(int field_index, InlineKlass* ik, TRAPS) {
6498 assert(field_index >= 0 && field_index < java_fields_count(), "IOOB: 0 <= %d < %d", field_index, (int)java_fields_count());
6499
6500 // The array of InlineLayoutInfo is allocated on demand. This way the array is
6501 // never allocated for an InstanceKlass which has no need for this information.
6502 if (_inline_layout_info_array == nullptr) {
6503 _inline_layout_info_array = MetadataFactory::new_array<InlineLayoutInfo>(_loader_data,
6504 java_fields_count(),
6505 CHECK);
6506 }
6507
6508 // Set the Klass for the field's index
6509 _inline_layout_info_array->adr_at(field_index)->set_klass(ik);
6510 }
6511
6512 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6513
6514 #ifdef ASSERT
6515 if (klass != nullptr) {
6516 assert(nullptr == _klass_to_deallocate, "leaking?");
6517 }
6518 #endif
6519
6520 _klass_to_deallocate = klass;
6521 }
6522
6523 // Caller responsible for ResourceMark
6524 // clone stream with rewound position
6525 const ClassFileStream* ClassFileParser::clone_stream() const {
6526 assert(_stream != nullptr, "invariant");
6527
6528 return _stream->clone();
6529 }
6530
6531 ReferenceType ClassFileParser::super_reference_type() const {
|