573 verify_legal_field_signature(name, signature, CHECK_(nullHandle));
574 }
575 } else {
576 verify_legal_method_name(name, CHECK_(nullHandle));
577 if (_need_verify && _major_version >= JAVA_7_VERSION) {
578 // Signature is verified above, when iterating NameAndType_info.
579 // Need only to be sure it's the right type.
580 if (signature->byte_at(0) != JVM_SIGNATURE_FUNC) {
581 throwIllegalSignature(
582 "Method", name, signature, CHECK_(nullHandle));
583 }
584 } else {
585 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
586 }
587 if (tag == JVM_CONSTANT_Methodref) {
588 // 4509014: If a class method name begins with '<', it must be "<init>".
589 assert(name != NULL, "method name in constant pool is null");
590 unsigned int name_len = name->utf8_length();
591 assert(name_len > 0, "bad method name"); // already verified as legal name
592 if (name->byte_at(0) == '<') {
593 if (name != vmSymbols::object_initializer_name()) {
594 classfile_parse_error(
595 "Bad method name at constant pool index %u in class file %s",
596 name_ref_index, CHECK_(nullHandle));
597 }
598 }
599 }
600 }
601 break;
602 }
603 case JVM_CONSTANT_MethodHandle: {
604 int ref_index = cp->method_handle_index_at(index);
605 int ref_kind = cp->method_handle_ref_kind_at(index);
606 switch (ref_kind) {
607 case JVM_REF_invokeVirtual:
608 case JVM_REF_invokeStatic:
609 case JVM_REF_invokeSpecial:
610 case JVM_REF_newInvokeSpecial:
611 {
612 int name_and_type_ref_index = cp->name_and_type_ref_index_at(ref_index);
613 int name_ref_index = cp->name_ref_index_at(name_and_type_ref_index);
614 Symbol* name = cp->symbol_at(name_ref_index);
615 if (ref_kind == JVM_REF_newInvokeSpecial) {
616 if (name != vmSymbols::object_initializer_name()) {
617 classfile_parse_error(
618 "Bad constructor name at constant pool index %u in class file %s",
619 name_ref_index, CHECK_(nullHandle));
620 }
621 } else {
622 if (name == vmSymbols::object_initializer_name()) {
623 classfile_parse_error(
624 "Bad method name at constant pool index %u in class file %s",
625 name_ref_index, CHECK_(nullHandle));
626 }
627 }
628 }
629 break;
630 // Other ref_kinds are already fully checked in previous pass.
631 }
632 break;
633 }
634 case JVM_CONSTANT_MethodType: {
635 Symbol* no_name = vmSymbols::type_name(); // place holder
636 Symbol* signature = cp->method_type_signature_at(index);
637 verify_legal_method_signature(no_name, signature, CHECK_(nullHandle));
638 break;
639 }
640 case JVM_CONSTANT_Utf8: {
641 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
642 }
726 return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE;
727 }
728
729
730 void initialize_hashtable(NameSigHash** table) {
731 memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE);
732 }
733
734 // Return false if the name/sig combination is found in table.
735 // Return true if no duplicate is found. And name/sig is added as a new entry in table.
736 // The old format checker uses heap sort to find duplicates.
737 // NOTE: caller should guarantee that GC doesn't happen during the life cycle
738 // of table since we don't expect Symbol*'s to move.
739 bool put_after_lookup(Symbol* name, Symbol* sig, NameSigHash** table) {
740 assert(name != NULL, "name in constant pool is NULL");
741
742 // First lookup for duplicates
743 int index = hash(name, sig);
744 NameSigHash* entry = table[index];
745 while (entry != NULL) {
746 if (entry->_name == name && entry->_sig == sig) {
747 return false;
748 }
749 entry = entry->_next;
750 }
751
752 // No duplicate is found, allocate a new entry and fill it.
753 entry = new NameSigHash();
754 entry->_name = name;
755 entry->_sig = sig;
756
757 // Insert into hash table
758 entry->_next = table[index];
759 table[index] = entry;
760
761 return true;
762 }
763
764
765 Array<Klass*>* ClassFileParser::parse_interfaces(int length,
766 Handle protection_domain,
888 bool is_synthetic = false;
889 u1* runtime_visible_annotations = NULL;
890 int runtime_visible_annotations_length = 0;
891 u1* runtime_invisible_annotations = NULL;
892 int runtime_invisible_annotations_length = 0;
893 u1* runtime_visible_type_annotations = NULL;
894 int runtime_visible_type_annotations_length = 0;
895 u1* runtime_invisible_type_annotations = NULL;
896 int runtime_invisible_type_annotations_length = 0;
897 bool runtime_invisible_annotations_exists = false;
898 bool runtime_invisible_type_annotations_exists = false;
899 while (attributes_count--) {
900 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
901 u2 attribute_name_index = cfs->get_u2_fast();
902 u4 attribute_length = cfs->get_u4_fast();
903 check_property(valid_symbol_at(attribute_name_index),
904 "Invalid field attribute index %u in class file %s",
905 attribute_name_index,
906 CHECK);
907 Symbol* attribute_name = _cp->symbol_at(attribute_name_index);
908 if (is_static && attribute_name == vmSymbols::tag_constant_value()) {
909 // ignore if non-static
910 if (constantvalue_index != 0) {
911 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK);
912 }
913 check_property(
914 attribute_length == 2,
915 "Invalid ConstantValue field attribute length %u in class file %s",
916 attribute_length, CHECK);
917 constantvalue_index = cfs->get_u2(CHECK);
918 if (_need_verify) {
919 verify_constantvalue(constantvalue_index, signature_index, CHECK);
920 }
921 } else if (attribute_name == vmSymbols::tag_synthetic()) {
922 if (attribute_length != 0) {
923 classfile_parse_error(
924 "Invalid Synthetic field attribute length %u in class file %s",
925 attribute_length, CHECK);
926 }
927 is_synthetic = true;
928 } else if (attribute_name == vmSymbols::tag_deprecated()) { // 4276120
929 if (attribute_length != 0) {
930 classfile_parse_error(
931 "Invalid Deprecated field attribute length %u in class file %s",
932 attribute_length, CHECK);
933 }
934 } else if (_major_version >= JAVA_1_5_VERSION) {
935 if (attribute_name == vmSymbols::tag_signature()) {
936 if (attribute_length != 2) {
937 classfile_parse_error(
938 "Wrong size %u for field's Signature attribute in class file %s",
939 attribute_length, CHECK);
940 }
941 generic_signature_index = parse_generic_signature_attribute(CHECK);
942 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
943 if (runtime_visible_annotations != NULL) {
944 classfile_parse_error(
945 "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK);
946 }
947 runtime_visible_annotations_length = attribute_length;
948 runtime_visible_annotations = cfs->get_u1_buffer();
949 assert(runtime_visible_annotations != NULL, "null visible annotations");
950 parse_annotations(runtime_visible_annotations,
951 runtime_visible_annotations_length,
952 parsed_annotations,
953 CHECK);
954 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
955 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
956 if (runtime_invisible_annotations_exists) {
957 classfile_parse_error(
958 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK);
959 }
960 runtime_invisible_annotations_exists = true;
961 if (PreserveAllAnnotations) {
962 runtime_invisible_annotations_length = attribute_length;
963 runtime_invisible_annotations = cfs->get_u1_buffer();
964 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
965 }
966 cfs->skip_u1(attribute_length, CHECK);
967 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {
968 if (runtime_visible_type_annotations != NULL) {
969 classfile_parse_error(
970 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK);
971 }
972 runtime_visible_type_annotations_length = attribute_length;
973 runtime_visible_type_annotations = cfs->get_u1_buffer();
974 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
975 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
976 } else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) {
977 if (runtime_invisible_type_annotations_exists) {
978 classfile_parse_error(
979 "Multiple RuntimeInvisibleTypeAnnotations attributes for field in class file %s", CHECK);
980 } else {
981 runtime_invisible_type_annotations_exists = true;
982 }
983 if (PreserveAllAnnotations) {
984 runtime_invisible_type_annotations_length = attribute_length;
985 runtime_invisible_type_annotations = cfs->get_u1_buffer();
986 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
987 }
988 cfs->skip_u1(attribute_length, CHECK);
989 } else {
990 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes
991 }
992 } else {
993 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes
994 }
995 }
996
1214
1215 // Remember how many oops we encountered and compute allocation type
1216 FieldAllocationType atype = fac->update(is_static, type);
1217 field->set_allocation_type(atype);
1218
1219 // After field is initialized with type, we can augment it with aux info
1220 if (parsed_annotations.has_any_annotations())
1221 parsed_annotations.apply_to(field);
1222 }
1223
1224 int index = length;
1225 if (num_injected != 0) {
1226 for (int n = 0; n < num_injected; n++) {
1227 // Check for duplicates
1228 if (injected[n].may_be_java) {
1229 Symbol* name = injected[n].name();
1230 Symbol* signature = injected[n].signature();
1231 bool duplicate = false;
1232 for (int i = 0; i < length; i++) {
1233 FieldInfo* f = FieldInfo::from_field_array(fa, i);
1234 if (name == _cp->symbol_at(f->name_index()) &&
1235 signature == _cp->symbol_at(f->signature_index())) {
1236 // Symbol is desclared in Java so skip this one
1237 duplicate = true;
1238 break;
1239 }
1240 }
1241 if (duplicate) {
1242 // These will be removed from the field array at the end
1243 continue;
1244 }
1245 }
1246
1247 // Injected field
1248 FieldInfo* field = FieldInfo::from_field_array(fa, index);
1249 field->initialize(JVM_ACC_FIELD_INTERNAL,
1250 injected[n].name_index,
1251 injected[n].signature_index,
1252 0);
1253
1254 BasicType type = FieldType::basic_type(injected[n].signature());
1255
1471 classfile_parse_error(
1472 "Invalid length %u in %s in class file %s",
1473 length, tbl_name, CHECK_NULL);
1474 }
1475 int cp_size = _cp->length();
1476 guarantee_property(valid_symbol_at(name_index),
1477 "Name index %u in %s has bad constant type in class file %s",
1478 name_index, tbl_name, CHECK_NULL);
1479 guarantee_property(valid_symbol_at(descriptor_index),
1480 "Signature index %u in %s has bad constant type in class file %s",
1481 descriptor_index, tbl_name, CHECK_NULL);
1482
1483 Symbol* name = _cp->symbol_at(name_index);
1484 Symbol* sig = _cp->symbol_at(descriptor_index);
1485 verify_legal_field_name(name, CHECK_NULL);
1486 u2 extra_slot = 0;
1487 if (!isLVTT) {
1488 verify_legal_field_signature(name, sig, CHECK_NULL);
1489
1490 // 4894874: check special cases for double and long local variables
1491 if (sig == vmSymbols::type_signature(T_DOUBLE) ||
1492 sig == vmSymbols::type_signature(T_LONG)) {
1493 extra_slot = 1;
1494 }
1495 }
1496 guarantee_property((index + extra_slot) < max_locals,
1497 "Invalid index %u in %s in class file %s",
1498 index, tbl_name, CHECK_NULL);
1499 }
1500 }
1501 return localvariable_table_start;
1502 }
1503
1504
1505 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
1506 u1* u1_array, u2* u2_array, TRAPS) {
1507 ClassFileStream* cfs = stream();
1508 u2 index = 0; // index in the array with long/double occupying two slots
1509 u4 i1 = *u1_index;
1510 u4 i2 = *u2_index + 1;
1511 for(int i = 0; i < array_length; i++) {
1512 u1 tag = u1_array[i1++] = cfs->get_u1(CHECK);
1693 AnnotationCollector::ID id = coll->annotation_index(_loader_data, aname);
1694 if (id == AnnotationCollector::_unknown) continue;
1695 coll->set_annotation(id);
1696
1697 if (id == AnnotationCollector::_sun_misc_Contended) {
1698 // @Contended can optionally specify the contention group.
1699 //
1700 // Contended group defines the equivalence class over the fields:
1701 // the fields within the same contended group are not treated distinct.
1702 // The only exception is default group, which does not incur the
1703 // equivalence. Naturally, contention group for classes is meaningless.
1704 //
1705 // While the contention group is specified as String, annotation
1706 // values are already interned, and we might as well use the constant
1707 // pool index as the group tag.
1708 //
1709 u2 group_index = 0; // default contended group
1710 if (count == 1
1711 && s_size == (index - index0) // match size
1712 && s_tag_val == *(abase + tag_off)
1713 && member == vmSymbols::value_name()) {
1714 group_index = Bytes::get_Java_u2(abase + s_con_off);
1715 if (_cp->symbol_at(group_index)->utf8_length() == 0) {
1716 group_index = 0; // default contended group
1717 }
1718 }
1719 coll->set_contended_group(group_index);
1720 }
1721 }
1722 }
1723
1724 ClassFileParser::AnnotationCollector::ID
1725 ClassFileParser::AnnotationCollector::annotation_index(ClassLoaderData* loader_data,
1726 Symbol* name) {
1727 vmSymbols::SID sid = vmSymbols::find_sid(name);
1728 // Privileged code can use all annotations. Other code silently drops some.
1729 const bool privileged = loader_data->is_the_null_class_loader_data() ||
1730 loader_data->is_ext_class_loader_data() ||
1731 loader_data->is_anonymous();
1732 switch (sid) {
1733 case vmSymbols::VM_SYMBOL_ENUM_NAME(sun_reflect_CallerSensitive_signature):
1956 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count
1957
1958 int flags = cfs->get_u2_fast();
1959 u2 name_index = cfs->get_u2_fast();
1960 int cp_size = _cp->length();
1961 check_property(
1962 valid_symbol_at(name_index),
1963 "Illegal constant pool index %u for method name in class file %s",
1964 name_index, CHECK_(nullHandle));
1965 Symbol* name = _cp->symbol_at(name_index);
1966 verify_legal_method_name(name, CHECK_(nullHandle));
1967
1968 u2 signature_index = cfs->get_u2_fast();
1969 guarantee_property(
1970 valid_symbol_at(signature_index),
1971 "Illegal constant pool index %u for method signature in class file %s",
1972 signature_index, CHECK_(nullHandle));
1973 Symbol* signature = _cp->symbol_at(signature_index);
1974
1975 AccessFlags access_flags;
1976 if (name == vmSymbols::class_initializer_name()) {
1977 // We ignore the other access flags for a valid class initializer.
1978 // (JVM Spec 2nd ed., chapter 4.6)
1979 if (_major_version < 51) { // backward compatibility
1980 flags = JVM_ACC_STATIC;
1981 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
1982 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT;
1983 }
1984 } else {
1985 verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle));
1986 }
1987
1988 int args_size = -1; // only used when _need_verify is true
1989 if (_need_verify) {
1990 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
1991 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
1992 if (args_size > MAX_ARGS_SIZE) {
1993 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_(nullHandle));
1994 }
1995 }
1996
2042 u1* runtime_invisible_type_annotations = NULL;
2043 int runtime_invisible_type_annotations_length = 0;
2044 bool runtime_invisible_annotations_exists = false;
2045 bool runtime_invisible_type_annotations_exists = false;
2046 bool runtime_invisible_parameter_annotations_exists = false;
2047 u1* annotation_default = NULL;
2048 int annotation_default_length = 0;
2049
2050 // Parse code and exceptions attribute
2051 u2 method_attributes_count = cfs->get_u2_fast();
2052 while (method_attributes_count--) {
2053 cfs->guarantee_more(6, CHECK_(nullHandle)); // method_attribute_name_index, method_attribute_length
2054 u2 method_attribute_name_index = cfs->get_u2_fast();
2055 u4 method_attribute_length = cfs->get_u4_fast();
2056 check_property(
2057 valid_symbol_at(method_attribute_name_index),
2058 "Invalid method attribute name index %u in class file %s",
2059 method_attribute_name_index, CHECK_(nullHandle));
2060
2061 Symbol* method_attribute_name = _cp->symbol_at(method_attribute_name_index);
2062 if (method_attribute_name == vmSymbols::tag_code()) {
2063 // Parse Code attribute
2064 if (_need_verify) {
2065 guarantee_property(
2066 !access_flags.is_native() && !access_flags.is_abstract(),
2067 "Code attribute in native or abstract methods in class file %s",
2068 CHECK_(nullHandle));
2069 }
2070 if (parsed_code_attribute) {
2071 classfile_parse_error("Multiple Code attributes in class file %s", CHECK_(nullHandle));
2072 }
2073 parsed_code_attribute = true;
2074
2075 // Stack size, locals size, and code size
2076 if (_major_version == 45 && _minor_version <= 2) {
2077 cfs->guarantee_more(4, CHECK_(nullHandle));
2078 max_stack = cfs->get_u1_fast();
2079 max_locals = cfs->get_u1_fast();
2080 code_length = cfs->get_u2_fast();
2081 } else {
2082 cfs->guarantee_more(8, CHECK_(nullHandle));
2123 sizeof(exception_table_length) +
2124 sizeof(code_attributes_count) +
2125 exception_table_length *
2126 ( sizeof(u2) + // start_pc
2127 sizeof(u2) + // end_pc
2128 sizeof(u2) + // handler_pc
2129 sizeof(u2) ); // catch_type_index
2130
2131 while (code_attributes_count--) {
2132 cfs->guarantee_more(6, CHECK_(nullHandle)); // code_attribute_name_index, code_attribute_length
2133 u2 code_attribute_name_index = cfs->get_u2_fast();
2134 u4 code_attribute_length = cfs->get_u4_fast();
2135 calculated_attribute_length += code_attribute_length +
2136 sizeof(code_attribute_name_index) +
2137 sizeof(code_attribute_length);
2138 check_property(valid_symbol_at(code_attribute_name_index),
2139 "Invalid code attribute name index %u in class file %s",
2140 code_attribute_name_index,
2141 CHECK_(nullHandle));
2142 if (LoadLineNumberTables &&
2143 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) {
2144 // Parse and compress line number table
2145 parse_linenumber_table(code_attribute_length, code_length,
2146 &linenumber_table, CHECK_(nullHandle));
2147
2148 } else if (LoadLocalVariableTables &&
2149 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) {
2150 // Parse local variable table
2151 if (!lvt_allocated) {
2152 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2153 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2154 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2155 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2156 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2157 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2158 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2159 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2160 lvt_allocated = true;
2161 }
2162 if (lvt_cnt == max_lvt_cnt) {
2163 max_lvt_cnt <<= 1;
2164 localvariable_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt);
2165 localvariable_table_start = REALLOC_RESOURCE_ARRAY(u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt);
2166 }
2167 localvariable_table_start[lvt_cnt] =
2168 parse_localvariable_table(code_length,
2169 max_locals,
2170 code_attribute_length,
2171 &localvariable_table_length[lvt_cnt],
2172 false, // is not LVTT
2173 CHECK_(nullHandle));
2174 total_lvt_length += localvariable_table_length[lvt_cnt];
2175 lvt_cnt++;
2176 } else if (LoadLocalVariableTypeTables &&
2177 _major_version >= JAVA_1_5_VERSION &&
2178 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) {
2179 if (!lvt_allocated) {
2180 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2181 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2182 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2183 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2184 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2185 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2186 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2187 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2188 lvt_allocated = true;
2189 }
2190 // Parse local variable type table
2191 if (lvtt_cnt == max_lvtt_cnt) {
2192 max_lvtt_cnt <<= 1;
2193 localvariable_type_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt);
2194 localvariable_type_table_start = REALLOC_RESOURCE_ARRAY(u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt);
2195 }
2196 localvariable_type_table_start[lvtt_cnt] =
2197 parse_localvariable_table(code_length,
2198 max_locals,
2199 code_attribute_length,
2200 &localvariable_type_table_length[lvtt_cnt],
2201 true, // is LVTT
2202 CHECK_(nullHandle));
2203 lvtt_cnt++;
2204 } else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION &&
2205 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) {
2206 // Stack map is only needed by the new verifier in JDK1.5.
2207 if (parsed_stackmap_attribute) {
2208 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_(nullHandle));
2209 }
2210 stackmap_data = parse_stackmap_table(code_attribute_length, CHECK_(nullHandle));
2211 stackmap_data_length = code_attribute_length;
2212 parsed_stackmap_attribute = true;
2213 } else {
2214 // Skip unknown attributes
2215 cfs->skip_u1(code_attribute_length, CHECK_(nullHandle));
2216 }
2217 }
2218 // check method attribute length
2219 if (_need_verify) {
2220 guarantee_property(method_attribute_length == calculated_attribute_length,
2221 "Code segment has wrong length in class file %s", CHECK_(nullHandle));
2222 }
2223 } else if (method_attribute_name == vmSymbols::tag_exceptions()) {
2224 // Parse Exceptions attribute
2225 if (parsed_checked_exceptions_attribute) {
2226 classfile_parse_error("Multiple Exceptions attributes in class file %s", CHECK_(nullHandle));
2227 }
2228 parsed_checked_exceptions_attribute = true;
2229 checked_exceptions_start =
2230 parse_checked_exceptions(&checked_exceptions_length,
2231 method_attribute_length,
2232 CHECK_(nullHandle));
2233 } else if (method_attribute_name == vmSymbols::tag_method_parameters()) {
2234 // reject multiple method parameters
2235 if (method_parameters_seen) {
2236 classfile_parse_error("Multiple MethodParameters attributes in class file %s", CHECK_(nullHandle));
2237 }
2238 method_parameters_seen = true;
2239 method_parameters_length = cfs->get_u1_fast();
2240 const u2 real_length = (method_parameters_length * 4u) + 1u;
2241 if (method_attribute_length != real_length) {
2242 classfile_parse_error(
2243 "Invalid MethodParameters method attribute length %u in class file",
2244 method_attribute_length, CHECK_(nullHandle));
2245 }
2246 method_parameters_data = cfs->get_u1_buffer();
2247 cfs->skip_u2_fast(method_parameters_length);
2248 cfs->skip_u2_fast(method_parameters_length);
2249 // ignore this attribute if it cannot be reflected
2250 if (!SystemDictionary::Parameter_klass_loaded())
2251 method_parameters_length = -1;
2252 } else if (method_attribute_name == vmSymbols::tag_synthetic()) {
2253 if (method_attribute_length != 0) {
2254 classfile_parse_error(
2255 "Invalid Synthetic method attribute length %u in class file %s",
2256 method_attribute_length, CHECK_(nullHandle));
2257 }
2258 // Should we check that there hasn't already been a synthetic attribute?
2259 access_flags.set_is_synthetic();
2260 } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120
2261 if (method_attribute_length != 0) {
2262 classfile_parse_error(
2263 "Invalid Deprecated method attribute length %u in class file %s",
2264 method_attribute_length, CHECK_(nullHandle));
2265 }
2266 } else if (_major_version >= JAVA_1_5_VERSION) {
2267 if (method_attribute_name == vmSymbols::tag_signature()) {
2268 if (method_attribute_length != 2) {
2269 classfile_parse_error(
2270 "Invalid Signature attribute length %u in class file %s",
2271 method_attribute_length, CHECK_(nullHandle));
2272 }
2273 generic_signature_index = parse_generic_signature_attribute(CHECK_(nullHandle));
2274 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
2275 if (runtime_visible_annotations != NULL) {
2276 classfile_parse_error(
2277 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2278 }
2279 runtime_visible_annotations_length = method_attribute_length;
2280 runtime_visible_annotations = cfs->get_u1_buffer();
2281 assert(runtime_visible_annotations != NULL, "null visible annotations");
2282 parse_annotations(runtime_visible_annotations,
2283 runtime_visible_annotations_length, &parsed_annotations,
2284 CHECK_(nullHandle));
2285 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle));
2286 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
2287 if (runtime_invisible_annotations_exists) {
2288 classfile_parse_error(
2289 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2290 }
2291 runtime_invisible_annotations_exists = true;
2292 if (PreserveAllAnnotations) {
2293 runtime_invisible_annotations_length = method_attribute_length;
2294 runtime_invisible_annotations = cfs->get_u1_buffer();
2295 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2296 }
2297 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2298 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {
2299 if (runtime_visible_parameter_annotations != NULL) {
2300 classfile_parse_error(
2301 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2302 }
2303 runtime_visible_parameter_annotations_length = method_attribute_length;
2304 runtime_visible_parameter_annotations = cfs->get_u1_buffer();
2305 assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations");
2306 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_(nullHandle));
2307 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) {
2308 if (runtime_invisible_parameter_annotations_exists) {
2309 classfile_parse_error(
2310 "Multiple RuntimeInvisibleParameterAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2311 }
2312 runtime_invisible_parameter_annotations_exists = true;
2313 if (PreserveAllAnnotations) {
2314 runtime_invisible_parameter_annotations_length = method_attribute_length;
2315 runtime_invisible_parameter_annotations = cfs->get_u1_buffer();
2316 assert(runtime_invisible_parameter_annotations != NULL, "null invisible parameter annotations");
2317 }
2318 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2319 } else if (method_attribute_name == vmSymbols::tag_annotation_default()) {
2320 if (annotation_default != NULL) {
2321 classfile_parse_error(
2322 "Multiple AnnotationDefault attributes for method in class file %s",
2323 CHECK_(nullHandle));
2324 }
2325 annotation_default_length = method_attribute_length;
2326 annotation_default = cfs->get_u1_buffer();
2327 assert(annotation_default != NULL, "null annotation default");
2328 cfs->skip_u1(annotation_default_length, CHECK_(nullHandle));
2329 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {
2330 if (runtime_visible_type_annotations != NULL) {
2331 classfile_parse_error(
2332 "Multiple RuntimeVisibleTypeAnnotations attributes for method in class file %s",
2333 CHECK_(nullHandle));
2334 }
2335 runtime_visible_type_annotations_length = method_attribute_length;
2336 runtime_visible_type_annotations = cfs->get_u1_buffer();
2337 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
2338 // No need for the VM to parse Type annotations
2339 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_(nullHandle));
2340 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) {
2341 if (runtime_invisible_type_annotations_exists) {
2342 classfile_parse_error(
2343 "Multiple RuntimeInvisibleTypeAnnotations attributes for method in class file %s",
2344 CHECK_(nullHandle));
2345 } else {
2346 runtime_invisible_type_annotations_exists = true;
2347 }
2348 if (PreserveAllAnnotations) {
2349 runtime_invisible_type_annotations_length = method_attribute_length;
2350 runtime_invisible_type_annotations = cfs->get_u1_buffer();
2351 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
2352 }
2353 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2354 } else {
2355 // Skip unknown attributes
2356 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2357 }
2358 } else {
2359 // Skip unknown attributes
2360 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2474 parsed_annotations.apply_to(m);
2475
2476 // Copy annotations
2477 copy_method_annotations(m->constMethod(),
2478 runtime_visible_annotations,
2479 runtime_visible_annotations_length,
2480 runtime_invisible_annotations,
2481 runtime_invisible_annotations_length,
2482 runtime_visible_parameter_annotations,
2483 runtime_visible_parameter_annotations_length,
2484 runtime_invisible_parameter_annotations,
2485 runtime_invisible_parameter_annotations_length,
2486 runtime_visible_type_annotations,
2487 runtime_visible_type_annotations_length,
2488 runtime_invisible_type_annotations,
2489 runtime_invisible_type_annotations_length,
2490 annotation_default,
2491 annotation_default_length,
2492 CHECK_NULL);
2493
2494 if (name == vmSymbols::finalize_method_name() &&
2495 signature == vmSymbols::void_method_signature()) {
2496 if (m->is_empty_method()) {
2497 _has_empty_finalizer = true;
2498 } else {
2499 _has_finalizer = true;
2500 }
2501 }
2502 if (name == vmSymbols::object_initializer_name() &&
2503 signature == vmSymbols::void_method_signature() &&
2504 m->is_vanilla_constructor()) {
2505 _has_vanilla_constructor = true;
2506 }
2507
2508 NOT_PRODUCT(m->verify());
2509 return m;
2510 }
2511
2512
2513 // The promoted_flags parameter is used to pass relevant access_flags
2514 // from the methods back up to the containing klass. These flag values
2515 // are added to klass's access_flags.
2516
2517 Array<Method*>* ClassFileParser::parse_methods(bool is_interface,
2518 AccessFlags* promoted_flags,
2519 bool* has_final_method,
2520 bool* declares_default_methods,
2521 TRAPS) {
2522 ClassFileStream* cfs = stream();
2523 cfs->guarantee_more(2, CHECK_NULL); // length
2865 int runtime_visible_type_annotations_length = 0;
2866 u1* runtime_invisible_type_annotations = NULL;
2867 int runtime_invisible_type_annotations_length = 0;
2868 bool runtime_invisible_type_annotations_exists = false;
2869 bool runtime_invisible_annotations_exists = false;
2870 bool parsed_source_debug_ext_annotations_exist = false;
2871 u1* inner_classes_attribute_start = NULL;
2872 u4 inner_classes_attribute_length = 0;
2873 u2 enclosing_method_class_index = 0;
2874 u2 enclosing_method_method_index = 0;
2875 // Iterate over attributes
2876 while (attributes_count--) {
2877 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
2878 u2 attribute_name_index = cfs->get_u2_fast();
2879 u4 attribute_length = cfs->get_u4_fast();
2880 check_property(
2881 valid_symbol_at(attribute_name_index),
2882 "Attribute name has bad constant pool index %u in class file %s",
2883 attribute_name_index, CHECK);
2884 Symbol* tag = _cp->symbol_at(attribute_name_index);
2885 if (tag == vmSymbols::tag_source_file()) {
2886 // Check for SourceFile tag
2887 if (_need_verify) {
2888 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
2889 }
2890 if (parsed_sourcefile_attribute) {
2891 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
2892 } else {
2893 parsed_sourcefile_attribute = true;
2894 }
2895 parse_classfile_sourcefile_attribute(CHECK);
2896 } else if (tag == vmSymbols::tag_source_debug_extension()) {
2897 // Check for SourceDebugExtension tag
2898 if (parsed_source_debug_ext_annotations_exist) {
2899 classfile_parse_error(
2900 "Multiple SourceDebugExtension attributes in class file %s", CHECK);
2901 }
2902 parsed_source_debug_ext_annotations_exist = true;
2903 parse_classfile_source_debug_extension_attribute((int)attribute_length, CHECK);
2904 } else if (tag == vmSymbols::tag_inner_classes()) {
2905 // Check for InnerClasses tag
2906 if (parsed_innerclasses_attribute) {
2907 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK);
2908 } else {
2909 parsed_innerclasses_attribute = true;
2910 }
2911 inner_classes_attribute_start = cfs->get_u1_buffer();
2912 inner_classes_attribute_length = attribute_length;
2913 cfs->skip_u1(inner_classes_attribute_length, CHECK);
2914 } else if (tag == vmSymbols::tag_synthetic()) {
2915 // Check for Synthetic tag
2916 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec
2917 if (attribute_length != 0) {
2918 classfile_parse_error(
2919 "Invalid Synthetic classfile attribute length %u in class file %s",
2920 attribute_length, CHECK);
2921 }
2922 parse_classfile_synthetic_attribute(CHECK);
2923 } else if (tag == vmSymbols::tag_deprecated()) {
2924 // Check for Deprecatd tag - 4276120
2925 if (attribute_length != 0) {
2926 classfile_parse_error(
2927 "Invalid Deprecated classfile attribute length %u in class file %s",
2928 attribute_length, CHECK);
2929 }
2930 } else if (_major_version >= JAVA_1_5_VERSION) {
2931 if (tag == vmSymbols::tag_signature()) {
2932 if (attribute_length != 2) {
2933 classfile_parse_error(
2934 "Wrong Signature attribute length %u in class file %s",
2935 attribute_length, CHECK);
2936 }
2937 parse_classfile_signature_attribute(CHECK);
2938 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
2939 if (runtime_visible_annotations != NULL) {
2940 classfile_parse_error(
2941 "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK);
2942 }
2943 runtime_visible_annotations_length = attribute_length;
2944 runtime_visible_annotations = cfs->get_u1_buffer();
2945 assert(runtime_visible_annotations != NULL, "null visible annotations");
2946 parse_annotations(runtime_visible_annotations,
2947 runtime_visible_annotations_length,
2948 parsed_annotations,
2949 CHECK);
2950 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2951 } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) {
2952 if (runtime_invisible_annotations_exists) {
2953 classfile_parse_error(
2954 "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK);
2955 }
2956 runtime_invisible_annotations_exists = true;
2957 if (PreserveAllAnnotations) {
2958 runtime_invisible_annotations_length = attribute_length;
2959 runtime_invisible_annotations = cfs->get_u1_buffer();
2960 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2961 }
2962 cfs->skip_u1(attribute_length, CHECK);
2963 } else if (tag == vmSymbols::tag_enclosing_method()) {
2964 if (parsed_enclosingmethod_attribute) {
2965 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
2966 } else {
2967 parsed_enclosingmethod_attribute = true;
2968 }
2969 guarantee_property(attribute_length == 4,
2970 "Wrong EnclosingMethod attribute length %u in class file %s",
2971 attribute_length, CHECK);
2972 cfs->guarantee_more(4, CHECK); // class_index, method_index
2973 enclosing_method_class_index = cfs->get_u2_fast();
2974 enclosing_method_method_index = cfs->get_u2_fast();
2975 if (enclosing_method_class_index == 0) {
2976 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
2977 }
2978 // Validate the constant pool indices and types
2979 check_property(valid_klass_reference_at(enclosing_method_class_index),
2980 "Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
2981 if (enclosing_method_method_index != 0 &&
2982 (!_cp->is_within_bounds(enclosing_method_method_index) ||
2983 !_cp->tag_at(enclosing_method_method_index).is_name_and_type())) {
2984 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2985 }
2986 } else if (tag == vmSymbols::tag_bootstrap_methods() &&
2987 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2988 if (parsed_bootstrap_methods_attribute)
2989 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK);
2990 parsed_bootstrap_methods_attribute = true;
2991 parse_classfile_bootstrap_methods_attribute(attribute_length, CHECK);
2992 } else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) {
2993 if (runtime_visible_type_annotations != NULL) {
2994 classfile_parse_error(
2995 "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK);
2996 }
2997 runtime_visible_type_annotations_length = attribute_length;
2998 runtime_visible_type_annotations = cfs->get_u1_buffer();
2999 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
3000 // No need for the VM to parse Type annotations
3001 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
3002 } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) {
3003 if (runtime_invisible_type_annotations_exists) {
3004 classfile_parse_error(
3005 "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK);
3006 } else {
3007 runtime_invisible_type_annotations_exists = true;
3008 }
3009 if (PreserveAllAnnotations) {
3010 runtime_invisible_type_annotations_length = attribute_length;
3011 runtime_invisible_type_annotations = cfs->get_u1_buffer();
3012 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
3013 }
3014 cfs->skip_u1(attribute_length, CHECK);
3015 } else {
3016 // Unknown attribute
3017 cfs->skip_u1(attribute_length, CHECK);
3018 }
3019 } else {
3020 // Unknown attribute
3021 cfs->skip_u1(attribute_length, CHECK);
3022 }
3125 CHECK_(annotations));
3126 if (runtime_visible_annotations != NULL) {
3127 for (int i = 0; i < runtime_visible_annotations_length; i++) {
3128 annotations->at_put(i, runtime_visible_annotations[i]);
3129 }
3130 }
3131 if (runtime_invisible_annotations != NULL) {
3132 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3133 int append = runtime_visible_annotations_length+i;
3134 annotations->at_put(append, runtime_invisible_annotations[i]);
3135 }
3136 }
3137 }
3138 return annotations;
3139 }
3140
3141 instanceKlassHandle ClassFileParser::parse_super_class(int super_class_index,
3142 TRAPS) {
3143 instanceKlassHandle super_klass;
3144 if (super_class_index == 0) {
3145 check_property(_class_name == vmSymbols::java_lang_Object(),
3146 "Invalid superclass index %u in class file %s",
3147 super_class_index,
3148 CHECK_NULL);
3149 } else {
3150 check_property(valid_klass_reference_at(super_class_index),
3151 "Invalid superclass index %u in class file %s",
3152 super_class_index,
3153 CHECK_NULL);
3154 // The class name should be legal because it is checked when parsing constant pool.
3155 // However, make sure it is not an array type.
3156 bool is_array = false;
3157 if (_cp->tag_at(super_class_index).is_klass()) {
3158 super_klass = instanceKlassHandle(THREAD, _cp->resolved_klass_at(super_class_index));
3159 if (_need_verify)
3160 is_array = super_klass->oop_is_array();
3161 } else if (_need_verify) {
3162 is_array = (_cp->klass_name_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
3163 }
3164 if (_need_verify) {
3165 guarantee_property(!is_array,
3288 unsigned int max_nonstatic_oop_maps = fac->count[NONSTATIC_OOP] + 1;
3289
3290 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3291 THREAD, int, max_nonstatic_oop_maps);
3292 nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
3293 THREAD, unsigned int, max_nonstatic_oop_maps);
3294
3295 first_nonstatic_oop_offset = 0; // will be set for first oop field
3296
3297 bool compact_fields = CompactFields;
3298 int allocation_style = FieldsAllocationStyle;
3299 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3300 assert(false, "0 <= FieldsAllocationStyle <= 2");
3301 allocation_style = 1; // Optimistic
3302 }
3303
3304 // The next classes have predefined hard-coded fields offsets
3305 // (see in JavaClasses::compute_hard_coded_offsets()).
3306 // Use default fields allocation order for them.
3307 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
3308 (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
3309 _class_name == vmSymbols::java_lang_Class() ||
3310 _class_name == vmSymbols::java_lang_ClassLoader() ||
3311 _class_name == vmSymbols::java_lang_ref_Reference() ||
3312 _class_name == vmSymbols::java_lang_ref_SoftReference() ||
3313 _class_name == vmSymbols::java_lang_StackTraceElement() ||
3314 _class_name == vmSymbols::java_lang_String() ||
3315 _class_name == vmSymbols::java_lang_Throwable() ||
3316 _class_name == vmSymbols::java_lang_Boolean() ||
3317 _class_name == vmSymbols::java_lang_Character() ||
3318 _class_name == vmSymbols::java_lang_Float() ||
3319 _class_name == vmSymbols::java_lang_Double() ||
3320 _class_name == vmSymbols::java_lang_Byte() ||
3321 _class_name == vmSymbols::java_lang_Short() ||
3322 _class_name == vmSymbols::java_lang_Integer() ||
3323 _class_name == vmSymbols::java_lang_Long())) {
3324 allocation_style = 0; // Allocate oops first
3325 compact_fields = false; // Don't compact fields
3326 }
3327
3328 // Rearrange fields for a given allocation style
3329 if( allocation_style == 0 ) {
3330 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
3331 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3332 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3333 (nonstatic_oop_count * heapOopSize);
3334 } else if( allocation_style == 1 ) {
3335 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
3336 next_nonstatic_double_offset = next_nonstatic_field_offset;
3337 } else if( allocation_style == 2 ) {
3338 // Fields allocation: oops fields in super and sub classes are together.
3339 if( nonstatic_field_size > 0 && _super_klass() != NULL &&
3340 _super_klass->nonstatic_oop_map_size() > 0 ) {
3341 unsigned int map_count = _super_klass->nonstatic_oop_map_count();
3342 OopMapBlock* first_map = _super_klass->start_of_nonstatic_oop_maps();
3343 OopMapBlock* last_map = first_map + map_count - 1;
3897 parsed_name->increment_refcount();
3898
3899 // Update _class_name which could be null previously to be class_name
3900 _class_name = class_name;
3901
3902 // Don't need to check whether this class name is legal or not.
3903 // It has been checked when constant pool is parsed.
3904 // However, make sure it is not an array type.
3905 if (_need_verify) {
3906 guarantee_property(class_name->byte_at(0) != JVM_SIGNATURE_ARRAY,
3907 "Bad class name in class file %s",
3908 CHECK_(nullHandle));
3909 }
3910
3911 Klass* preserve_this_klass; // for storing result across HandleMark
3912
3913 // release all handles when parsing is done
3914 { HandleMark hm(THREAD);
3915
3916 // Checks if name in class file matches requested name
3917 if (name != NULL && class_name != name) {
3918 ResourceMark rm(THREAD);
3919 Exceptions::fthrow(
3920 THREAD_AND_LOCATION,
3921 vmSymbols::java_lang_NoClassDefFoundError(),
3922 "%s (wrong name: %s)",
3923 name->as_C_string(),
3924 class_name->as_C_string()
3925 );
3926 return nullHandle;
3927 }
3928
3929 if (TraceClassLoadingPreorder) {
3930 tty->print("[Loading %s", (name != NULL) ? name->as_klass_external_name() : "NoName");
3931 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
3932 tty->print_cr("]");
3933 }
3934 #if INCLUDE_CDS
3935 if (DumpLoadedClassList != NULL && cfs->source() != NULL && classlist_file->is_open()) {
3936 // Only dump the classes that can be stored into CDS archive
3937 if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
3975 has_default_methods = true;
3976 }
3977
3978 // Additional attributes
3979 ClassAnnotationCollector parsed_annotations;
3980 parse_classfile_attributes(&parsed_annotations, CHECK_(nullHandle));
3981
3982 // Finalize the Annotations metadata object,
3983 // now that all annotation arrays have been created.
3984 create_combined_annotations(CHECK_(nullHandle));
3985
3986 // Make sure this is the end of class file stream
3987 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3988
3989 // We check super class after class file is parsed and format is checked
3990 if (super_class_index > 0 && super_klass.is_null()) {
3991 Symbol* sk = cp->klass_name_at(super_class_index);
3992 if (access_flags.is_interface()) {
3993 // Before attempting to resolve the superclass, check for class format
3994 // errors not checked yet.
3995 guarantee_property(sk == vmSymbols::java_lang_Object(),
3996 "Interfaces must have java.lang.Object as superclass in class file %s",
3997 CHECK_(nullHandle));
3998 }
3999 Klass* k = SystemDictionary::resolve_super_or_fail(class_name, sk,
4000 class_loader,
4001 protection_domain,
4002 true,
4003 CHECK_(nullHandle));
4004
4005 KlassHandle kh (THREAD, k);
4006 super_klass = instanceKlassHandle(THREAD, kh());
4007 }
4008 if (super_klass.not_null()) {
4009
4010 if (super_klass->has_default_methods()) {
4011 has_default_methods = true;
4012 }
4013
4014 if (super_klass->is_interface()) {
4015 ResourceMark rm(THREAD);
4464 k->set_has_vanilla_constructor();
4465 }
4466 #ifdef ASSERT
4467 bool v = false;
4468 if (super->has_vanilla_constructor()) {
4469 Method* constructor = k->find_method(vmSymbols::object_initializer_name(
4470 ), vmSymbols::void_method_signature());
4471 if (constructor != NULL && constructor->is_vanilla_constructor()) {
4472 v = true;
4473 }
4474 }
4475 assert(v == k->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4476 #endif
4477 }
4478
4479 // If it cannot be fast-path allocated, set a bit in the layout helper.
4480 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4481 assert(k->size_helper() > 0, "layout_helper is initialized");
4482 if ((!RegisterFinalizersAtInit && k->has_finalizer())
4483 || k->is_abstract() || k->is_interface()
4484 || (k->name() == vmSymbols::java_lang_Class() && k->class_loader() == NULL)
4485 || k->size_helper() >= FastAllocateSizeLimit) {
4486 // Forbid fast-path allocation.
4487 jint lh = Klass::instance_layout_helper(k->size_helper(), true);
4488 k->set_layout_helper(lh);
4489 }
4490 }
4491
4492 // Attach super classes and interface classes to class loader data
4493 void ClassFileParser::record_defined_class_dependencies(instanceKlassHandle defined_klass, TRAPS) {
4494 ClassLoaderData * defining_loader_data = defined_klass->class_loader_data();
4495 if (defining_loader_data->is_the_null_class_loader_data()) {
4496 // Dependencies to null class loader data are implicit.
4497 return;
4498 } else {
4499 // add super class dependency
4500 Klass* super = defined_klass->super();
4501 if (super != NULL) {
4502 defining_loader_data->record_dependency(super, CHECK);
4503 }
4504
4615 "class %s cannot access its superinterface %s",
4616 this_klass->external_name(),
4617 InstanceKlass::cast(k)->external_name()
4618 );
4619 return;
4620 }
4621 }
4622 }
4623
4624
4625 void ClassFileParser::check_final_method_override(instanceKlassHandle this_klass, TRAPS) {
4626 Array<Method*>* methods = this_klass->methods();
4627 int num_methods = methods->length();
4628
4629 // go thru each method and check if it overrides a final method
4630 for (int index = 0; index < num_methods; index++) {
4631 Method* m = methods->at(index);
4632
4633 // skip private, static, and <init> methods
4634 if ((!m->is_private() && !m->is_static()) &&
4635 (m->name() != vmSymbols::object_initializer_name())) {
4636
4637 Symbol* name = m->name();
4638 Symbol* signature = m->signature();
4639 Klass* k = this_klass->super();
4640 Method* super_m = NULL;
4641 while (k != NULL) {
4642 // skip supers that don't have final methods.
4643 if (k->has_final_method()) {
4644 // lookup a matching method in the super class hierarchy
4645 super_m = InstanceKlass::cast(k)->lookup_method(name, signature);
4646 if (super_m == NULL) {
4647 break; // didn't find any match; get out
4648 }
4649
4650 if (super_m->is_final() && !super_m->is_static() &&
4651 // matching method in super is final, and not static
4652 (Reflection::verify_field_access(this_klass(),
4653 super_m->method_holder(),
4654 super_m->method_holder(),
4655 super_m->access_flags(), false))
4672 k = super_m->method_holder()->super();
4673 continue;
4674 }
4675
4676 k = k->super();
4677 }
4678 }
4679 }
4680 }
4681
4682
4683 // assumes that this_klass is an interface
4684 void ClassFileParser::check_illegal_static_method(instanceKlassHandle this_klass, TRAPS) {
4685 assert(this_klass->is_interface(), "not an interface");
4686 Array<Method*>* methods = this_klass->methods();
4687 int num_methods = methods->length();
4688
4689 for (int index = 0; index < num_methods; index++) {
4690 Method* m = methods->at(index);
4691 // if m is static and not the init method, throw a verify error
4692 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4693 ResourceMark rm(THREAD);
4694 Exceptions::fthrow(
4695 THREAD_AND_LOCATION,
4696 vmSymbols::java_lang_VerifyError(),
4697 "Illegal static method %s in interface %s",
4698 m->name()->as_C_string(),
4699 this_klass->external_name()
4700 );
4701 return;
4702 }
4703 }
4704 }
4705
4706 // utility methods for format checking
4707
4708 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) {
4709 if (!_need_verify) { return; }
4710
4711 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4712 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4787 return;
4788 }
4789 }
4790
4791 void ClassFileParser::verify_legal_method_modifiers(
4792 jint flags, bool is_interface, Symbol* name, TRAPS) {
4793 if (!_need_verify) { return; }
4794
4795 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4796 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4797 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4798 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4799 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4800 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4801 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4802 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4803 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4804 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4805 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4806 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4807 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4808
4809 bool is_illegal = false;
4810
4811 if (is_interface) {
4812 if (major_gte_8) {
4813 // Class file version is JAVA_8_VERSION or later Methods of
4814 // interfaces may set any of the flags except ACC_PROTECTED,
4815 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4816 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4817 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4818 (is_native || is_protected || is_final || is_synchronized) ||
4819 // If a specific method of a class or interface has its
4820 // ACC_ABSTRACT flag set, it must not have any of its
4821 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4822 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4823 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4824 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4825 (is_abstract && (is_private || is_static || is_strict))) {
4826 is_illegal = true;
4827 }
4998 vmSymbols::java_lang_ClassFormatError(),
4999 "Illegal field name \"%s\" in class %s", bytes,
5000 _class_name->as_C_string()
5001 );
5002 return;
5003 }
5004 }
5005
5006 // Checks if name is a legal method name.
5007 void ClassFileParser::verify_legal_method_name(Symbol* name, TRAPS) {
5008 if (!_need_verify || _relax_verify) { return; }
5009
5010 assert(name != NULL, "method name is null");
5011 char buf[fixed_buffer_size];
5012 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
5013 unsigned int length = name->utf8_length();
5014 bool legal = false;
5015
5016 if (length > 0) {
5017 if (bytes[0] == '<') {
5018 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
5019 legal = true;
5020 }
5021 } else if (_major_version < JAVA_1_5_VERSION) {
5022 char* p;
5023 p = skip_over_field_name(bytes, false, length);
5024 legal = (p != NULL) && ((p - bytes) == (int)length);
5025 } else {
5026 // 4881221: relax the constraints based on JSR202 spec
5027 legal = verify_unqualified_name(bytes, length, LegalMethod);
5028 }
5029 }
5030
5031 if (!legal) {
5032 ResourceMark rm(THREAD);
5033 Exceptions::fthrow(
5034 THREAD_AND_LOCATION,
5035 vmSymbols::java_lang_ClassFormatError(),
5036 "Illegal method name \"%s\" in class %s", bytes,
5037 _class_name->as_C_string()
5038 );
|
573 verify_legal_field_signature(name, signature, CHECK_(nullHandle));
574 }
575 } else {
576 verify_legal_method_name(name, CHECK_(nullHandle));
577 if (_need_verify && _major_version >= JAVA_7_VERSION) {
578 // Signature is verified above, when iterating NameAndType_info.
579 // Need only to be sure it's the right type.
580 if (signature->byte_at(0) != JVM_SIGNATURE_FUNC) {
581 throwIllegalSignature(
582 "Method", name, signature, CHECK_(nullHandle));
583 }
584 } else {
585 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
586 }
587 if (tag == JVM_CONSTANT_Methodref) {
588 // 4509014: If a class method name begins with '<', it must be "<init>".
589 assert(name != NULL, "method name in constant pool is null");
590 unsigned int name_len = name->utf8_length();
591 assert(name_len > 0, "bad method name"); // already verified as legal name
592 if (name->byte_at(0) == '<') {
593 if (name->not_equals(vmSymbols::object_initializer_name())) {
594 classfile_parse_error(
595 "Bad method name at constant pool index %u in class file %s",
596 name_ref_index, CHECK_(nullHandle));
597 }
598 }
599 }
600 }
601 break;
602 }
603 case JVM_CONSTANT_MethodHandle: {
604 int ref_index = cp->method_handle_index_at(index);
605 int ref_kind = cp->method_handle_ref_kind_at(index);
606 switch (ref_kind) {
607 case JVM_REF_invokeVirtual:
608 case JVM_REF_invokeStatic:
609 case JVM_REF_invokeSpecial:
610 case JVM_REF_newInvokeSpecial:
611 {
612 int name_and_type_ref_index = cp->name_and_type_ref_index_at(ref_index);
613 int name_ref_index = cp->name_ref_index_at(name_and_type_ref_index);
614 Symbol* name = cp->symbol_at(name_ref_index);
615 if (ref_kind == JVM_REF_newInvokeSpecial) {
616 if (name->not_equals(vmSymbols::object_initializer_name())) {
617 classfile_parse_error(
618 "Bad constructor name at constant pool index %u in class file %s",
619 name_ref_index, CHECK_(nullHandle));
620 }
621 } else {
622 if (name->equals(vmSymbols::object_initializer_name())) {
623 classfile_parse_error(
624 "Bad method name at constant pool index %u in class file %s",
625 name_ref_index, CHECK_(nullHandle));
626 }
627 }
628 }
629 break;
630 // Other ref_kinds are already fully checked in previous pass.
631 }
632 break;
633 }
634 case JVM_CONSTANT_MethodType: {
635 Symbol* no_name = vmSymbols::type_name(); // place holder
636 Symbol* signature = cp->method_type_signature_at(index);
637 verify_legal_method_signature(no_name, signature, CHECK_(nullHandle));
638 break;
639 }
640 case JVM_CONSTANT_Utf8: {
641 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
642 }
726 return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE;
727 }
728
729
730 void initialize_hashtable(NameSigHash** table) {
731 memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE);
732 }
733
734 // Return false if the name/sig combination is found in table.
735 // Return true if no duplicate is found. And name/sig is added as a new entry in table.
736 // The old format checker uses heap sort to find duplicates.
737 // NOTE: caller should guarantee that GC doesn't happen during the life cycle
738 // of table since we don't expect Symbol*'s to move.
739 bool put_after_lookup(Symbol* name, Symbol* sig, NameSigHash** table) {
740 assert(name != NULL, "name in constant pool is NULL");
741
742 // First lookup for duplicates
743 int index = hash(name, sig);
744 NameSigHash* entry = table[index];
745 while (entry != NULL) {
746 if (entry->_name->equals(name) && entry->_sig->equals(sig)) {
747 return false;
748 }
749 entry = entry->_next;
750 }
751
752 // No duplicate is found, allocate a new entry and fill it.
753 entry = new NameSigHash();
754 entry->_name = name;
755 entry->_sig = sig;
756
757 // Insert into hash table
758 entry->_next = table[index];
759 table[index] = entry;
760
761 return true;
762 }
763
764
765 Array<Klass*>* ClassFileParser::parse_interfaces(int length,
766 Handle protection_domain,
888 bool is_synthetic = false;
889 u1* runtime_visible_annotations = NULL;
890 int runtime_visible_annotations_length = 0;
891 u1* runtime_invisible_annotations = NULL;
892 int runtime_invisible_annotations_length = 0;
893 u1* runtime_visible_type_annotations = NULL;
894 int runtime_visible_type_annotations_length = 0;
895 u1* runtime_invisible_type_annotations = NULL;
896 int runtime_invisible_type_annotations_length = 0;
897 bool runtime_invisible_annotations_exists = false;
898 bool runtime_invisible_type_annotations_exists = false;
899 while (attributes_count--) {
900 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
901 u2 attribute_name_index = cfs->get_u2_fast();
902 u4 attribute_length = cfs->get_u4_fast();
903 check_property(valid_symbol_at(attribute_name_index),
904 "Invalid field attribute index %u in class file %s",
905 attribute_name_index,
906 CHECK);
907 Symbol* attribute_name = _cp->symbol_at(attribute_name_index);
908 if (is_static && attribute_name->equals(vmSymbols::tag_constant_value())) {
909 // ignore if non-static
910 if (constantvalue_index != 0) {
911 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK);
912 }
913 check_property(
914 attribute_length == 2,
915 "Invalid ConstantValue field attribute length %u in class file %s",
916 attribute_length, CHECK);
917 constantvalue_index = cfs->get_u2(CHECK);
918 if (_need_verify) {
919 verify_constantvalue(constantvalue_index, signature_index, CHECK);
920 }
921 } else if (attribute_name->equals(vmSymbols::tag_synthetic())) {
922 if (attribute_length != 0) {
923 classfile_parse_error(
924 "Invalid Synthetic field attribute length %u in class file %s",
925 attribute_length, CHECK);
926 }
927 is_synthetic = true;
928 } else if (attribute_name->equals(vmSymbols::tag_deprecated())) { // 4276120
929 if (attribute_length != 0) {
930 classfile_parse_error(
931 "Invalid Deprecated field attribute length %u in class file %s",
932 attribute_length, CHECK);
933 }
934 } else if (_major_version >= JAVA_1_5_VERSION) {
935 if (attribute_name->equals(vmSymbols::tag_signature())) {
936 if (attribute_length != 2) {
937 classfile_parse_error(
938 "Wrong size %u for field's Signature attribute in class file %s",
939 attribute_length, CHECK);
940 }
941 generic_signature_index = parse_generic_signature_attribute(CHECK);
942 } else if (attribute_name->equals(vmSymbols::tag_runtime_visible_annotations())) {
943 if (runtime_visible_annotations != NULL) {
944 classfile_parse_error(
945 "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK);
946 }
947 runtime_visible_annotations_length = attribute_length;
948 runtime_visible_annotations = cfs->get_u1_buffer();
949 assert(runtime_visible_annotations != NULL, "null visible annotations");
950 parse_annotations(runtime_visible_annotations,
951 runtime_visible_annotations_length,
952 parsed_annotations,
953 CHECK);
954 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
955 } else if (attribute_name->equals(vmSymbols::tag_runtime_invisible_annotations())) {
956 if (runtime_invisible_annotations_exists) {
957 classfile_parse_error(
958 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK);
959 }
960 runtime_invisible_annotations_exists = true;
961 if (PreserveAllAnnotations) {
962 runtime_invisible_annotations_length = attribute_length;
963 runtime_invisible_annotations = cfs->get_u1_buffer();
964 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
965 }
966 cfs->skip_u1(attribute_length, CHECK);
967 } else if (attribute_name->equals(vmSymbols::tag_runtime_visible_type_annotations())) {
968 if (runtime_visible_type_annotations != NULL) {
969 classfile_parse_error(
970 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK);
971 }
972 runtime_visible_type_annotations_length = attribute_length;
973 runtime_visible_type_annotations = cfs->get_u1_buffer();
974 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
975 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
976 } else if (attribute_name->equals(vmSymbols::tag_runtime_invisible_type_annotations())) {
977 if (runtime_invisible_type_annotations_exists) {
978 classfile_parse_error(
979 "Multiple RuntimeInvisibleTypeAnnotations attributes for field in class file %s", CHECK);
980 } else {
981 runtime_invisible_type_annotations_exists = true;
982 }
983 if (PreserveAllAnnotations) {
984 runtime_invisible_type_annotations_length = attribute_length;
985 runtime_invisible_type_annotations = cfs->get_u1_buffer();
986 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
987 }
988 cfs->skip_u1(attribute_length, CHECK);
989 } else {
990 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes
991 }
992 } else {
993 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes
994 }
995 }
996
1214
1215 // Remember how many oops we encountered and compute allocation type
1216 FieldAllocationType atype = fac->update(is_static, type);
1217 field->set_allocation_type(atype);
1218
1219 // After field is initialized with type, we can augment it with aux info
1220 if (parsed_annotations.has_any_annotations())
1221 parsed_annotations.apply_to(field);
1222 }
1223
1224 int index = length;
1225 if (num_injected != 0) {
1226 for (int n = 0; n < num_injected; n++) {
1227 // Check for duplicates
1228 if (injected[n].may_be_java) {
1229 Symbol* name = injected[n].name();
1230 Symbol* signature = injected[n].signature();
1231 bool duplicate = false;
1232 for (int i = 0; i < length; i++) {
1233 FieldInfo* f = FieldInfo::from_field_array(fa, i);
1234 if (name->equals(_cp->symbol_at(f->name_index())) &&
1235 signature->equals(_cp->symbol_at(f->signature_index()))) {
1236 // Symbol is desclared in Java so skip this one
1237 duplicate = true;
1238 break;
1239 }
1240 }
1241 if (duplicate) {
1242 // These will be removed from the field array at the end
1243 continue;
1244 }
1245 }
1246
1247 // Injected field
1248 FieldInfo* field = FieldInfo::from_field_array(fa, index);
1249 field->initialize(JVM_ACC_FIELD_INTERNAL,
1250 injected[n].name_index,
1251 injected[n].signature_index,
1252 0);
1253
1254 BasicType type = FieldType::basic_type(injected[n].signature());
1255
1471 classfile_parse_error(
1472 "Invalid length %u in %s in class file %s",
1473 length, tbl_name, CHECK_NULL);
1474 }
1475 int cp_size = _cp->length();
1476 guarantee_property(valid_symbol_at(name_index),
1477 "Name index %u in %s has bad constant type in class file %s",
1478 name_index, tbl_name, CHECK_NULL);
1479 guarantee_property(valid_symbol_at(descriptor_index),
1480 "Signature index %u in %s has bad constant type in class file %s",
1481 descriptor_index, tbl_name, CHECK_NULL);
1482
1483 Symbol* name = _cp->symbol_at(name_index);
1484 Symbol* sig = _cp->symbol_at(descriptor_index);
1485 verify_legal_field_name(name, CHECK_NULL);
1486 u2 extra_slot = 0;
1487 if (!isLVTT) {
1488 verify_legal_field_signature(name, sig, CHECK_NULL);
1489
1490 // 4894874: check special cases for double and long local variables
1491 if (sig->equals(vmSymbols::type_signature(T_DOUBLE)) ||
1492 sig->equals(vmSymbols::type_signature(T_LONG))) {
1493 extra_slot = 1;
1494 }
1495 }
1496 guarantee_property((index + extra_slot) < max_locals,
1497 "Invalid index %u in %s in class file %s",
1498 index, tbl_name, CHECK_NULL);
1499 }
1500 }
1501 return localvariable_table_start;
1502 }
1503
1504
1505 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
1506 u1* u1_array, u2* u2_array, TRAPS) {
1507 ClassFileStream* cfs = stream();
1508 u2 index = 0; // index in the array with long/double occupying two slots
1509 u4 i1 = *u1_index;
1510 u4 i2 = *u2_index + 1;
1511 for(int i = 0; i < array_length; i++) {
1512 u1 tag = u1_array[i1++] = cfs->get_u1(CHECK);
1693 AnnotationCollector::ID id = coll->annotation_index(_loader_data, aname);
1694 if (id == AnnotationCollector::_unknown) continue;
1695 coll->set_annotation(id);
1696
1697 if (id == AnnotationCollector::_sun_misc_Contended) {
1698 // @Contended can optionally specify the contention group.
1699 //
1700 // Contended group defines the equivalence class over the fields:
1701 // the fields within the same contended group are not treated distinct.
1702 // The only exception is default group, which does not incur the
1703 // equivalence. Naturally, contention group for classes is meaningless.
1704 //
1705 // While the contention group is specified as String, annotation
1706 // values are already interned, and we might as well use the constant
1707 // pool index as the group tag.
1708 //
1709 u2 group_index = 0; // default contended group
1710 if (count == 1
1711 && s_size == (index - index0) // match size
1712 && s_tag_val == *(abase + tag_off)
1713 && member->equals(vmSymbols::value_name())) {
1714 group_index = Bytes::get_Java_u2(abase + s_con_off);
1715 if (_cp->symbol_at(group_index)->utf8_length() == 0) {
1716 group_index = 0; // default contended group
1717 }
1718 }
1719 coll->set_contended_group(group_index);
1720 }
1721 }
1722 }
1723
1724 ClassFileParser::AnnotationCollector::ID
1725 ClassFileParser::AnnotationCollector::annotation_index(ClassLoaderData* loader_data,
1726 Symbol* name) {
1727 vmSymbols::SID sid = vmSymbols::find_sid(name);
1728 // Privileged code can use all annotations. Other code silently drops some.
1729 const bool privileged = loader_data->is_the_null_class_loader_data() ||
1730 loader_data->is_ext_class_loader_data() ||
1731 loader_data->is_anonymous();
1732 switch (sid) {
1733 case vmSymbols::VM_SYMBOL_ENUM_NAME(sun_reflect_CallerSensitive_signature):
1956 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count
1957
1958 int flags = cfs->get_u2_fast();
1959 u2 name_index = cfs->get_u2_fast();
1960 int cp_size = _cp->length();
1961 check_property(
1962 valid_symbol_at(name_index),
1963 "Illegal constant pool index %u for method name in class file %s",
1964 name_index, CHECK_(nullHandle));
1965 Symbol* name = _cp->symbol_at(name_index);
1966 verify_legal_method_name(name, CHECK_(nullHandle));
1967
1968 u2 signature_index = cfs->get_u2_fast();
1969 guarantee_property(
1970 valid_symbol_at(signature_index),
1971 "Illegal constant pool index %u for method signature in class file %s",
1972 signature_index, CHECK_(nullHandle));
1973 Symbol* signature = _cp->symbol_at(signature_index);
1974
1975 AccessFlags access_flags;
1976 if (name->equals(vmSymbols::class_initializer_name())) {
1977 // We ignore the other access flags for a valid class initializer.
1978 // (JVM Spec 2nd ed., chapter 4.6)
1979 if (_major_version < 51) { // backward compatibility
1980 flags = JVM_ACC_STATIC;
1981 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
1982 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT;
1983 }
1984 } else {
1985 verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle));
1986 }
1987
1988 int args_size = -1; // only used when _need_verify is true
1989 if (_need_verify) {
1990 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
1991 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
1992 if (args_size > MAX_ARGS_SIZE) {
1993 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_(nullHandle));
1994 }
1995 }
1996
2042 u1* runtime_invisible_type_annotations = NULL;
2043 int runtime_invisible_type_annotations_length = 0;
2044 bool runtime_invisible_annotations_exists = false;
2045 bool runtime_invisible_type_annotations_exists = false;
2046 bool runtime_invisible_parameter_annotations_exists = false;
2047 u1* annotation_default = NULL;
2048 int annotation_default_length = 0;
2049
2050 // Parse code and exceptions attribute
2051 u2 method_attributes_count = cfs->get_u2_fast();
2052 while (method_attributes_count--) {
2053 cfs->guarantee_more(6, CHECK_(nullHandle)); // method_attribute_name_index, method_attribute_length
2054 u2 method_attribute_name_index = cfs->get_u2_fast();
2055 u4 method_attribute_length = cfs->get_u4_fast();
2056 check_property(
2057 valid_symbol_at(method_attribute_name_index),
2058 "Invalid method attribute name index %u in class file %s",
2059 method_attribute_name_index, CHECK_(nullHandle));
2060
2061 Symbol* method_attribute_name = _cp->symbol_at(method_attribute_name_index);
2062 if (method_attribute_name->equals(vmSymbols::tag_code())) {
2063 // Parse Code attribute
2064 if (_need_verify) {
2065 guarantee_property(
2066 !access_flags.is_native() && !access_flags.is_abstract(),
2067 "Code attribute in native or abstract methods in class file %s",
2068 CHECK_(nullHandle));
2069 }
2070 if (parsed_code_attribute) {
2071 classfile_parse_error("Multiple Code attributes in class file %s", CHECK_(nullHandle));
2072 }
2073 parsed_code_attribute = true;
2074
2075 // Stack size, locals size, and code size
2076 if (_major_version == 45 && _minor_version <= 2) {
2077 cfs->guarantee_more(4, CHECK_(nullHandle));
2078 max_stack = cfs->get_u1_fast();
2079 max_locals = cfs->get_u1_fast();
2080 code_length = cfs->get_u2_fast();
2081 } else {
2082 cfs->guarantee_more(8, CHECK_(nullHandle));
2123 sizeof(exception_table_length) +
2124 sizeof(code_attributes_count) +
2125 exception_table_length *
2126 ( sizeof(u2) + // start_pc
2127 sizeof(u2) + // end_pc
2128 sizeof(u2) + // handler_pc
2129 sizeof(u2) ); // catch_type_index
2130
2131 while (code_attributes_count--) {
2132 cfs->guarantee_more(6, CHECK_(nullHandle)); // code_attribute_name_index, code_attribute_length
2133 u2 code_attribute_name_index = cfs->get_u2_fast();
2134 u4 code_attribute_length = cfs->get_u4_fast();
2135 calculated_attribute_length += code_attribute_length +
2136 sizeof(code_attribute_name_index) +
2137 sizeof(code_attribute_length);
2138 check_property(valid_symbol_at(code_attribute_name_index),
2139 "Invalid code attribute name index %u in class file %s",
2140 code_attribute_name_index,
2141 CHECK_(nullHandle));
2142 if (LoadLineNumberTables &&
2143 _cp->symbol_at(code_attribute_name_index)->equals(vmSymbols::tag_line_number_table())) {
2144 // Parse and compress line number table
2145 parse_linenumber_table(code_attribute_length, code_length,
2146 &linenumber_table, CHECK_(nullHandle));
2147
2148 } else if (LoadLocalVariableTables &&
2149 _cp->symbol_at(code_attribute_name_index)->equals(vmSymbols::tag_local_variable_table())) {
2150 // Parse local variable table
2151 if (!lvt_allocated) {
2152 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2153 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2154 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2155 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2156 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2157 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2158 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2159 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2160 lvt_allocated = true;
2161 }
2162 if (lvt_cnt == max_lvt_cnt) {
2163 max_lvt_cnt <<= 1;
2164 localvariable_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt);
2165 localvariable_table_start = REALLOC_RESOURCE_ARRAY(u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt);
2166 }
2167 localvariable_table_start[lvt_cnt] =
2168 parse_localvariable_table(code_length,
2169 max_locals,
2170 code_attribute_length,
2171 &localvariable_table_length[lvt_cnt],
2172 false, // is not LVTT
2173 CHECK_(nullHandle));
2174 total_lvt_length += localvariable_table_length[lvt_cnt];
2175 lvt_cnt++;
2176 } else if (LoadLocalVariableTypeTables &&
2177 _major_version >= JAVA_1_5_VERSION &&
2178 _cp->symbol_at(code_attribute_name_index)->equals(vmSymbols::tag_local_variable_type_table())) {
2179 if (!lvt_allocated) {
2180 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2181 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2182 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2183 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2184 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2185 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2186 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2187 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2188 lvt_allocated = true;
2189 }
2190 // Parse local variable type table
2191 if (lvtt_cnt == max_lvtt_cnt) {
2192 max_lvtt_cnt <<= 1;
2193 localvariable_type_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt);
2194 localvariable_type_table_start = REALLOC_RESOURCE_ARRAY(u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt);
2195 }
2196 localvariable_type_table_start[lvtt_cnt] =
2197 parse_localvariable_table(code_length,
2198 max_locals,
2199 code_attribute_length,
2200 &localvariable_type_table_length[lvtt_cnt],
2201 true, // is LVTT
2202 CHECK_(nullHandle));
2203 lvtt_cnt++;
2204 } else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION &&
2205 _cp->symbol_at(code_attribute_name_index)->equals(vmSymbols::tag_stack_map_table())) {
2206 // Stack map is only needed by the new verifier in JDK1.5.
2207 if (parsed_stackmap_attribute) {
2208 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_(nullHandle));
2209 }
2210 stackmap_data = parse_stackmap_table(code_attribute_length, CHECK_(nullHandle));
2211 stackmap_data_length = code_attribute_length;
2212 parsed_stackmap_attribute = true;
2213 } else {
2214 // Skip unknown attributes
2215 cfs->skip_u1(code_attribute_length, CHECK_(nullHandle));
2216 }
2217 }
2218 // check method attribute length
2219 if (_need_verify) {
2220 guarantee_property(method_attribute_length == calculated_attribute_length,
2221 "Code segment has wrong length in class file %s", CHECK_(nullHandle));
2222 }
2223 } else if (method_attribute_name->equals(vmSymbols::tag_exceptions())) {
2224 // Parse Exceptions attribute
2225 if (parsed_checked_exceptions_attribute) {
2226 classfile_parse_error("Multiple Exceptions attributes in class file %s", CHECK_(nullHandle));
2227 }
2228 parsed_checked_exceptions_attribute = true;
2229 checked_exceptions_start =
2230 parse_checked_exceptions(&checked_exceptions_length,
2231 method_attribute_length,
2232 CHECK_(nullHandle));
2233 } else if (method_attribute_name->equals(vmSymbols::tag_method_parameters())) {
2234 // reject multiple method parameters
2235 if (method_parameters_seen) {
2236 classfile_parse_error("Multiple MethodParameters attributes in class file %s", CHECK_(nullHandle));
2237 }
2238 method_parameters_seen = true;
2239 method_parameters_length = cfs->get_u1_fast();
2240 const u2 real_length = (method_parameters_length * 4u) + 1u;
2241 if (method_attribute_length != real_length) {
2242 classfile_parse_error(
2243 "Invalid MethodParameters method attribute length %u in class file",
2244 method_attribute_length, CHECK_(nullHandle));
2245 }
2246 method_parameters_data = cfs->get_u1_buffer();
2247 cfs->skip_u2_fast(method_parameters_length);
2248 cfs->skip_u2_fast(method_parameters_length);
2249 // ignore this attribute if it cannot be reflected
2250 if (!SystemDictionary::Parameter_klass_loaded())
2251 method_parameters_length = -1;
2252 } else if (method_attribute_name->equals(vmSymbols::tag_synthetic())) {
2253 if (method_attribute_length != 0) {
2254 classfile_parse_error(
2255 "Invalid Synthetic method attribute length %u in class file %s",
2256 method_attribute_length, CHECK_(nullHandle));
2257 }
2258 // Should we check that there hasn't already been a synthetic attribute?
2259 access_flags.set_is_synthetic();
2260 } else if (method_attribute_name->equals(vmSymbols::tag_deprecated())) { // 4276120
2261 if (method_attribute_length != 0) {
2262 classfile_parse_error(
2263 "Invalid Deprecated method attribute length %u in class file %s",
2264 method_attribute_length, CHECK_(nullHandle));
2265 }
2266 } else if (_major_version >= JAVA_1_5_VERSION) {
2267 if (method_attribute_name->equals(vmSymbols::tag_signature())) {
2268 if (method_attribute_length != 2) {
2269 classfile_parse_error(
2270 "Invalid Signature attribute length %u in class file %s",
2271 method_attribute_length, CHECK_(nullHandle));
2272 }
2273 generic_signature_index = parse_generic_signature_attribute(CHECK_(nullHandle));
2274 } else if (method_attribute_name->equals(vmSymbols::tag_runtime_visible_annotations())) {
2275 if (runtime_visible_annotations != NULL) {
2276 classfile_parse_error(
2277 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2278 }
2279 runtime_visible_annotations_length = method_attribute_length;
2280 runtime_visible_annotations = cfs->get_u1_buffer();
2281 assert(runtime_visible_annotations != NULL, "null visible annotations");
2282 parse_annotations(runtime_visible_annotations,
2283 runtime_visible_annotations_length, &parsed_annotations,
2284 CHECK_(nullHandle));
2285 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle));
2286 } else if (method_attribute_name->equals(vmSymbols::tag_runtime_invisible_annotations())) {
2287 if (runtime_invisible_annotations_exists) {
2288 classfile_parse_error(
2289 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2290 }
2291 runtime_invisible_annotations_exists = true;
2292 if (PreserveAllAnnotations) {
2293 runtime_invisible_annotations_length = method_attribute_length;
2294 runtime_invisible_annotations = cfs->get_u1_buffer();
2295 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2296 }
2297 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2298 } else if (method_attribute_name->equals(vmSymbols::tag_runtime_visible_parameter_annotations())) {
2299 if (runtime_visible_parameter_annotations != NULL) {
2300 classfile_parse_error(
2301 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2302 }
2303 runtime_visible_parameter_annotations_length = method_attribute_length;
2304 runtime_visible_parameter_annotations = cfs->get_u1_buffer();
2305 assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations");
2306 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_(nullHandle));
2307 } else if (method_attribute_name->equals(vmSymbols::tag_runtime_invisible_parameter_annotations())) {
2308 if (runtime_invisible_parameter_annotations_exists) {
2309 classfile_parse_error(
2310 "Multiple RuntimeInvisibleParameterAnnotations attributes for method in class file %s", CHECK_(nullHandle));
2311 }
2312 runtime_invisible_parameter_annotations_exists = true;
2313 if (PreserveAllAnnotations) {
2314 runtime_invisible_parameter_annotations_length = method_attribute_length;
2315 runtime_invisible_parameter_annotations = cfs->get_u1_buffer();
2316 assert(runtime_invisible_parameter_annotations != NULL, "null invisible parameter annotations");
2317 }
2318 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2319 } else if (method_attribute_name->equals(vmSymbols::tag_annotation_default())) {
2320 if (annotation_default != NULL) {
2321 classfile_parse_error(
2322 "Multiple AnnotationDefault attributes for method in class file %s",
2323 CHECK_(nullHandle));
2324 }
2325 annotation_default_length = method_attribute_length;
2326 annotation_default = cfs->get_u1_buffer();
2327 assert(annotation_default != NULL, "null annotation default");
2328 cfs->skip_u1(annotation_default_length, CHECK_(nullHandle));
2329 } else if (method_attribute_name->equals(vmSymbols::tag_runtime_visible_type_annotations())) {
2330 if (runtime_visible_type_annotations != NULL) {
2331 classfile_parse_error(
2332 "Multiple RuntimeVisibleTypeAnnotations attributes for method in class file %s",
2333 CHECK_(nullHandle));
2334 }
2335 runtime_visible_type_annotations_length = method_attribute_length;
2336 runtime_visible_type_annotations = cfs->get_u1_buffer();
2337 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
2338 // No need for the VM to parse Type annotations
2339 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_(nullHandle));
2340 } else if (method_attribute_name->equals(vmSymbols::tag_runtime_invisible_type_annotations())) {
2341 if (runtime_invisible_type_annotations_exists) {
2342 classfile_parse_error(
2343 "Multiple RuntimeInvisibleTypeAnnotations attributes for method in class file %s",
2344 CHECK_(nullHandle));
2345 } else {
2346 runtime_invisible_type_annotations_exists = true;
2347 }
2348 if (PreserveAllAnnotations) {
2349 runtime_invisible_type_annotations_length = method_attribute_length;
2350 runtime_invisible_type_annotations = cfs->get_u1_buffer();
2351 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
2352 }
2353 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2354 } else {
2355 // Skip unknown attributes
2356 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2357 }
2358 } else {
2359 // Skip unknown attributes
2360 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
2474 parsed_annotations.apply_to(m);
2475
2476 // Copy annotations
2477 copy_method_annotations(m->constMethod(),
2478 runtime_visible_annotations,
2479 runtime_visible_annotations_length,
2480 runtime_invisible_annotations,
2481 runtime_invisible_annotations_length,
2482 runtime_visible_parameter_annotations,
2483 runtime_visible_parameter_annotations_length,
2484 runtime_invisible_parameter_annotations,
2485 runtime_invisible_parameter_annotations_length,
2486 runtime_visible_type_annotations,
2487 runtime_visible_type_annotations_length,
2488 runtime_invisible_type_annotations,
2489 runtime_invisible_type_annotations_length,
2490 annotation_default,
2491 annotation_default_length,
2492 CHECK_NULL);
2493
2494 if (name->equals(vmSymbols::finalize_method_name()) &&
2495 signature->equals(vmSymbols::void_method_signature())) {
2496 if (m->is_empty_method()) {
2497 _has_empty_finalizer = true;
2498 } else {
2499 _has_finalizer = true;
2500 }
2501 }
2502 if (name->equals(vmSymbols::object_initializer_name()) &&
2503 signature->equals(vmSymbols::void_method_signature()) &&
2504 m->is_vanilla_constructor()) {
2505 _has_vanilla_constructor = true;
2506 }
2507
2508 NOT_PRODUCT(m->verify());
2509 return m;
2510 }
2511
2512
2513 // The promoted_flags parameter is used to pass relevant access_flags
2514 // from the methods back up to the containing klass. These flag values
2515 // are added to klass's access_flags.
2516
2517 Array<Method*>* ClassFileParser::parse_methods(bool is_interface,
2518 AccessFlags* promoted_flags,
2519 bool* has_final_method,
2520 bool* declares_default_methods,
2521 TRAPS) {
2522 ClassFileStream* cfs = stream();
2523 cfs->guarantee_more(2, CHECK_NULL); // length
2865 int runtime_visible_type_annotations_length = 0;
2866 u1* runtime_invisible_type_annotations = NULL;
2867 int runtime_invisible_type_annotations_length = 0;
2868 bool runtime_invisible_type_annotations_exists = false;
2869 bool runtime_invisible_annotations_exists = false;
2870 bool parsed_source_debug_ext_annotations_exist = false;
2871 u1* inner_classes_attribute_start = NULL;
2872 u4 inner_classes_attribute_length = 0;
2873 u2 enclosing_method_class_index = 0;
2874 u2 enclosing_method_method_index = 0;
2875 // Iterate over attributes
2876 while (attributes_count--) {
2877 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
2878 u2 attribute_name_index = cfs->get_u2_fast();
2879 u4 attribute_length = cfs->get_u4_fast();
2880 check_property(
2881 valid_symbol_at(attribute_name_index),
2882 "Attribute name has bad constant pool index %u in class file %s",
2883 attribute_name_index, CHECK);
2884 Symbol* tag = _cp->symbol_at(attribute_name_index);
2885 if (tag->equals(vmSymbols::tag_source_file())) {
2886 // Check for SourceFile tag
2887 if (_need_verify) {
2888 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
2889 }
2890 if (parsed_sourcefile_attribute) {
2891 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
2892 } else {
2893 parsed_sourcefile_attribute = true;
2894 }
2895 parse_classfile_sourcefile_attribute(CHECK);
2896 } else if (tag->equals(vmSymbols::tag_source_debug_extension())) {
2897 // Check for SourceDebugExtension tag
2898 if (parsed_source_debug_ext_annotations_exist) {
2899 classfile_parse_error(
2900 "Multiple SourceDebugExtension attributes in class file %s", CHECK);
2901 }
2902 parsed_source_debug_ext_annotations_exist = true;
2903 parse_classfile_source_debug_extension_attribute((int)attribute_length, CHECK);
2904 } else if (tag->equals(vmSymbols::tag_inner_classes())) {
2905 // Check for InnerClasses tag
2906 if (parsed_innerclasses_attribute) {
2907 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK);
2908 } else {
2909 parsed_innerclasses_attribute = true;
2910 }
2911 inner_classes_attribute_start = cfs->get_u1_buffer();
2912 inner_classes_attribute_length = attribute_length;
2913 cfs->skip_u1(inner_classes_attribute_length, CHECK);
2914 } else if (tag->equals(vmSymbols::tag_synthetic())) {
2915 // Check for Synthetic tag
2916 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec
2917 if (attribute_length != 0) {
2918 classfile_parse_error(
2919 "Invalid Synthetic classfile attribute length %u in class file %s",
2920 attribute_length, CHECK);
2921 }
2922 parse_classfile_synthetic_attribute(CHECK);
2923 } else if (tag->equals(vmSymbols::tag_deprecated())) {
2924 // Check for Deprecatd tag - 4276120
2925 if (attribute_length != 0) {
2926 classfile_parse_error(
2927 "Invalid Deprecated classfile attribute length %u in class file %s",
2928 attribute_length, CHECK);
2929 }
2930 } else if (_major_version >= JAVA_1_5_VERSION) {
2931 if (tag->equals(vmSymbols::tag_signature())) {
2932 if (attribute_length != 2) {
2933 classfile_parse_error(
2934 "Wrong Signature attribute length %u in class file %s",
2935 attribute_length, CHECK);
2936 }
2937 parse_classfile_signature_attribute(CHECK);
2938 } else if (tag->equals(vmSymbols::tag_runtime_visible_annotations())) {
2939 if (runtime_visible_annotations != NULL) {
2940 classfile_parse_error(
2941 "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK);
2942 }
2943 runtime_visible_annotations_length = attribute_length;
2944 runtime_visible_annotations = cfs->get_u1_buffer();
2945 assert(runtime_visible_annotations != NULL, "null visible annotations");
2946 parse_annotations(runtime_visible_annotations,
2947 runtime_visible_annotations_length,
2948 parsed_annotations,
2949 CHECK);
2950 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2951 } else if (tag->equals(vmSymbols::tag_runtime_invisible_annotations())) {
2952 if (runtime_invisible_annotations_exists) {
2953 classfile_parse_error(
2954 "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK);
2955 }
2956 runtime_invisible_annotations_exists = true;
2957 if (PreserveAllAnnotations) {
2958 runtime_invisible_annotations_length = attribute_length;
2959 runtime_invisible_annotations = cfs->get_u1_buffer();
2960 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2961 }
2962 cfs->skip_u1(attribute_length, CHECK);
2963 } else if (tag->equals(vmSymbols::tag_enclosing_method())) {
2964 if (parsed_enclosingmethod_attribute) {
2965 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
2966 } else {
2967 parsed_enclosingmethod_attribute = true;
2968 }
2969 guarantee_property(attribute_length == 4,
2970 "Wrong EnclosingMethod attribute length %u in class file %s",
2971 attribute_length, CHECK);
2972 cfs->guarantee_more(4, CHECK); // class_index, method_index
2973 enclosing_method_class_index = cfs->get_u2_fast();
2974 enclosing_method_method_index = cfs->get_u2_fast();
2975 if (enclosing_method_class_index == 0) {
2976 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
2977 }
2978 // Validate the constant pool indices and types
2979 check_property(valid_klass_reference_at(enclosing_method_class_index),
2980 "Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
2981 if (enclosing_method_method_index != 0 &&
2982 (!_cp->is_within_bounds(enclosing_method_method_index) ||
2983 !_cp->tag_at(enclosing_method_method_index).is_name_and_type())) {
2984 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2985 }
2986 } else if (tag->equals(vmSymbols::tag_bootstrap_methods()) &&
2987 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2988 if (parsed_bootstrap_methods_attribute)
2989 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK);
2990 parsed_bootstrap_methods_attribute = true;
2991 parse_classfile_bootstrap_methods_attribute(attribute_length, CHECK);
2992 } else if (tag->equals(vmSymbols::tag_runtime_visible_type_annotations())) {
2993 if (runtime_visible_type_annotations != NULL) {
2994 classfile_parse_error(
2995 "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK);
2996 }
2997 runtime_visible_type_annotations_length = attribute_length;
2998 runtime_visible_type_annotations = cfs->get_u1_buffer();
2999 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
3000 // No need for the VM to parse Type annotations
3001 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
3002 } else if (tag->equals(vmSymbols::tag_runtime_invisible_type_annotations())) {
3003 if (runtime_invisible_type_annotations_exists) {
3004 classfile_parse_error(
3005 "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK);
3006 } else {
3007 runtime_invisible_type_annotations_exists = true;
3008 }
3009 if (PreserveAllAnnotations) {
3010 runtime_invisible_type_annotations_length = attribute_length;
3011 runtime_invisible_type_annotations = cfs->get_u1_buffer();
3012 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
3013 }
3014 cfs->skip_u1(attribute_length, CHECK);
3015 } else {
3016 // Unknown attribute
3017 cfs->skip_u1(attribute_length, CHECK);
3018 }
3019 } else {
3020 // Unknown attribute
3021 cfs->skip_u1(attribute_length, CHECK);
3022 }
3125 CHECK_(annotations));
3126 if (runtime_visible_annotations != NULL) {
3127 for (int i = 0; i < runtime_visible_annotations_length; i++) {
3128 annotations->at_put(i, runtime_visible_annotations[i]);
3129 }
3130 }
3131 if (runtime_invisible_annotations != NULL) {
3132 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3133 int append = runtime_visible_annotations_length+i;
3134 annotations->at_put(append, runtime_invisible_annotations[i]);
3135 }
3136 }
3137 }
3138 return annotations;
3139 }
3140
3141 instanceKlassHandle ClassFileParser::parse_super_class(int super_class_index,
3142 TRAPS) {
3143 instanceKlassHandle super_klass;
3144 if (super_class_index == 0) {
3145 check_property(_class_name->equals(vmSymbols::java_lang_Object()),
3146 "Invalid superclass index %u in class file %s",
3147 super_class_index,
3148 CHECK_NULL);
3149 } else {
3150 check_property(valid_klass_reference_at(super_class_index),
3151 "Invalid superclass index %u in class file %s",
3152 super_class_index,
3153 CHECK_NULL);
3154 // The class name should be legal because it is checked when parsing constant pool.
3155 // However, make sure it is not an array type.
3156 bool is_array = false;
3157 if (_cp->tag_at(super_class_index).is_klass()) {
3158 super_klass = instanceKlassHandle(THREAD, _cp->resolved_klass_at(super_class_index));
3159 if (_need_verify)
3160 is_array = super_klass->oop_is_array();
3161 } else if (_need_verify) {
3162 is_array = (_cp->klass_name_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
3163 }
3164 if (_need_verify) {
3165 guarantee_property(!is_array,
3288 unsigned int max_nonstatic_oop_maps = fac->count[NONSTATIC_OOP] + 1;
3289
3290 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3291 THREAD, int, max_nonstatic_oop_maps);
3292 nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
3293 THREAD, unsigned int, max_nonstatic_oop_maps);
3294
3295 first_nonstatic_oop_offset = 0; // will be set for first oop field
3296
3297 bool compact_fields = CompactFields;
3298 int allocation_style = FieldsAllocationStyle;
3299 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3300 assert(false, "0 <= FieldsAllocationStyle <= 2");
3301 allocation_style = 1; // Optimistic
3302 }
3303
3304 // The next classes have predefined hard-coded fields offsets
3305 // (see in JavaClasses::compute_hard_coded_offsets()).
3306 // Use default fields allocation order for them.
3307 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
3308 (_class_name->equals(vmSymbols::java_lang_AssertionStatusDirectives()) ||
3309 _class_name->equals(vmSymbols::java_lang_Class()) ||
3310 _class_name->equals(vmSymbols::java_lang_ClassLoader()) ||
3311 _class_name->equals(vmSymbols::java_lang_ref_Reference()) ||
3312 _class_name->equals(vmSymbols::java_lang_ref_SoftReference()) ||
3313 _class_name->equals(vmSymbols::java_lang_StackTraceElement()) ||
3314 _class_name->equals(vmSymbols::java_lang_String()) ||
3315 _class_name->equals(vmSymbols::java_lang_Throwable()) ||
3316 _class_name->equals(vmSymbols::java_lang_Boolean()) ||
3317 _class_name->equals(vmSymbols::java_lang_Character()) ||
3318 _class_name->equals(vmSymbols::java_lang_Float()) ||
3319 _class_name->equals(vmSymbols::java_lang_Double()) ||
3320 _class_name->equals(vmSymbols::java_lang_Byte()) ||
3321 _class_name->equals(vmSymbols::java_lang_Short()) ||
3322 _class_name->equals(vmSymbols::java_lang_Integer()) ||
3323 _class_name->equals(vmSymbols::java_lang_Long()))) {
3324 allocation_style = 0; // Allocate oops first
3325 compact_fields = false; // Don't compact fields
3326 }
3327
3328 // Rearrange fields for a given allocation style
3329 if( allocation_style == 0 ) {
3330 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
3331 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3332 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3333 (nonstatic_oop_count * heapOopSize);
3334 } else if( allocation_style == 1 ) {
3335 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
3336 next_nonstatic_double_offset = next_nonstatic_field_offset;
3337 } else if( allocation_style == 2 ) {
3338 // Fields allocation: oops fields in super and sub classes are together.
3339 if( nonstatic_field_size > 0 && _super_klass() != NULL &&
3340 _super_klass->nonstatic_oop_map_size() > 0 ) {
3341 unsigned int map_count = _super_klass->nonstatic_oop_map_count();
3342 OopMapBlock* first_map = _super_klass->start_of_nonstatic_oop_maps();
3343 OopMapBlock* last_map = first_map + map_count - 1;
3897 parsed_name->increment_refcount();
3898
3899 // Update _class_name which could be null previously to be class_name
3900 _class_name = class_name;
3901
3902 // Don't need to check whether this class name is legal or not.
3903 // It has been checked when constant pool is parsed.
3904 // However, make sure it is not an array type.
3905 if (_need_verify) {
3906 guarantee_property(class_name->byte_at(0) != JVM_SIGNATURE_ARRAY,
3907 "Bad class name in class file %s",
3908 CHECK_(nullHandle));
3909 }
3910
3911 Klass* preserve_this_klass; // for storing result across HandleMark
3912
3913 // release all handles when parsing is done
3914 { HandleMark hm(THREAD);
3915
3916 // Checks if name in class file matches requested name
3917 if (name->not_equals(NULL) && class_name->not_equals(name)) {
3918 ResourceMark rm(THREAD);
3919 Exceptions::fthrow(
3920 THREAD_AND_LOCATION,
3921 vmSymbols::java_lang_NoClassDefFoundError(),
3922 "%s (wrong name: %s)",
3923 name->as_C_string(),
3924 class_name->as_C_string()
3925 );
3926 return nullHandle;
3927 }
3928
3929 if (TraceClassLoadingPreorder) {
3930 tty->print("[Loading %s", (name != NULL) ? name->as_klass_external_name() : "NoName");
3931 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
3932 tty->print_cr("]");
3933 }
3934 #if INCLUDE_CDS
3935 if (DumpLoadedClassList != NULL && cfs->source() != NULL && classlist_file->is_open()) {
3936 // Only dump the classes that can be stored into CDS archive
3937 if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
3975 has_default_methods = true;
3976 }
3977
3978 // Additional attributes
3979 ClassAnnotationCollector parsed_annotations;
3980 parse_classfile_attributes(&parsed_annotations, CHECK_(nullHandle));
3981
3982 // Finalize the Annotations metadata object,
3983 // now that all annotation arrays have been created.
3984 create_combined_annotations(CHECK_(nullHandle));
3985
3986 // Make sure this is the end of class file stream
3987 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3988
3989 // We check super class after class file is parsed and format is checked
3990 if (super_class_index > 0 && super_klass.is_null()) {
3991 Symbol* sk = cp->klass_name_at(super_class_index);
3992 if (access_flags.is_interface()) {
3993 // Before attempting to resolve the superclass, check for class format
3994 // errors not checked yet.
3995 guarantee_property(sk->equals(vmSymbols::java_lang_Object()),
3996 "Interfaces must have java.lang.Object as superclass in class file %s",
3997 CHECK_(nullHandle));
3998 }
3999 Klass* k = SystemDictionary::resolve_super_or_fail(class_name, sk,
4000 class_loader,
4001 protection_domain,
4002 true,
4003 CHECK_(nullHandle));
4004
4005 KlassHandle kh (THREAD, k);
4006 super_klass = instanceKlassHandle(THREAD, kh());
4007 }
4008 if (super_klass.not_null()) {
4009
4010 if (super_klass->has_default_methods()) {
4011 has_default_methods = true;
4012 }
4013
4014 if (super_klass->is_interface()) {
4015 ResourceMark rm(THREAD);
4464 k->set_has_vanilla_constructor();
4465 }
4466 #ifdef ASSERT
4467 bool v = false;
4468 if (super->has_vanilla_constructor()) {
4469 Method* constructor = k->find_method(vmSymbols::object_initializer_name(
4470 ), vmSymbols::void_method_signature());
4471 if (constructor != NULL && constructor->is_vanilla_constructor()) {
4472 v = true;
4473 }
4474 }
4475 assert(v == k->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4476 #endif
4477 }
4478
4479 // If it cannot be fast-path allocated, set a bit in the layout helper.
4480 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4481 assert(k->size_helper() > 0, "layout_helper is initialized");
4482 if ((!RegisterFinalizersAtInit && k->has_finalizer())
4483 || k->is_abstract() || k->is_interface()
4484 || (k->name()->equals(vmSymbols::java_lang_Class()) && k->class_loader() == NULL)
4485 || k->size_helper() >= FastAllocateSizeLimit) {
4486 // Forbid fast-path allocation.
4487 jint lh = Klass::instance_layout_helper(k->size_helper(), true);
4488 k->set_layout_helper(lh);
4489 }
4490 }
4491
4492 // Attach super classes and interface classes to class loader data
4493 void ClassFileParser::record_defined_class_dependencies(instanceKlassHandle defined_klass, TRAPS) {
4494 ClassLoaderData * defining_loader_data = defined_klass->class_loader_data();
4495 if (defining_loader_data->is_the_null_class_loader_data()) {
4496 // Dependencies to null class loader data are implicit.
4497 return;
4498 } else {
4499 // add super class dependency
4500 Klass* super = defined_klass->super();
4501 if (super != NULL) {
4502 defining_loader_data->record_dependency(super, CHECK);
4503 }
4504
4615 "class %s cannot access its superinterface %s",
4616 this_klass->external_name(),
4617 InstanceKlass::cast(k)->external_name()
4618 );
4619 return;
4620 }
4621 }
4622 }
4623
4624
4625 void ClassFileParser::check_final_method_override(instanceKlassHandle this_klass, TRAPS) {
4626 Array<Method*>* methods = this_klass->methods();
4627 int num_methods = methods->length();
4628
4629 // go thru each method and check if it overrides a final method
4630 for (int index = 0; index < num_methods; index++) {
4631 Method* m = methods->at(index);
4632
4633 // skip private, static, and <init> methods
4634 if ((!m->is_private() && !m->is_static()) &&
4635 (m->name()->not_equals(vmSymbols::object_initializer_name()))) {
4636
4637 Symbol* name = m->name();
4638 Symbol* signature = m->signature();
4639 Klass* k = this_klass->super();
4640 Method* super_m = NULL;
4641 while (k != NULL) {
4642 // skip supers that don't have final methods.
4643 if (k->has_final_method()) {
4644 // lookup a matching method in the super class hierarchy
4645 super_m = InstanceKlass::cast(k)->lookup_method(name, signature);
4646 if (super_m == NULL) {
4647 break; // didn't find any match; get out
4648 }
4649
4650 if (super_m->is_final() && !super_m->is_static() &&
4651 // matching method in super is final, and not static
4652 (Reflection::verify_field_access(this_klass(),
4653 super_m->method_holder(),
4654 super_m->method_holder(),
4655 super_m->access_flags(), false))
4672 k = super_m->method_holder()->super();
4673 continue;
4674 }
4675
4676 k = k->super();
4677 }
4678 }
4679 }
4680 }
4681
4682
4683 // assumes that this_klass is an interface
4684 void ClassFileParser::check_illegal_static_method(instanceKlassHandle this_klass, TRAPS) {
4685 assert(this_klass->is_interface(), "not an interface");
4686 Array<Method*>* methods = this_klass->methods();
4687 int num_methods = methods->length();
4688
4689 for (int index = 0; index < num_methods; index++) {
4690 Method* m = methods->at(index);
4691 // if m is static and not the init method, throw a verify error
4692 if ((m->is_static()) && (m->name()->not_equals(vmSymbols::class_initializer_name()))) {
4693 ResourceMark rm(THREAD);
4694 Exceptions::fthrow(
4695 THREAD_AND_LOCATION,
4696 vmSymbols::java_lang_VerifyError(),
4697 "Illegal static method %s in interface %s",
4698 m->name()->as_C_string(),
4699 this_klass->external_name()
4700 );
4701 return;
4702 }
4703 }
4704 }
4705
4706 // utility methods for format checking
4707
4708 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) {
4709 if (!_need_verify) { return; }
4710
4711 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4712 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4787 return;
4788 }
4789 }
4790
4791 void ClassFileParser::verify_legal_method_modifiers(
4792 jint flags, bool is_interface, Symbol* name, TRAPS) {
4793 if (!_need_verify) { return; }
4794
4795 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4796 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4797 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4798 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4799 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4800 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4801 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4802 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4803 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4804 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4805 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4806 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4807 const bool is_initializer = (name->equals(vmSymbols::object_initializer_name()));
4808
4809 bool is_illegal = false;
4810
4811 if (is_interface) {
4812 if (major_gte_8) {
4813 // Class file version is JAVA_8_VERSION or later Methods of
4814 // interfaces may set any of the flags except ACC_PROTECTED,
4815 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must
4816 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set.
4817 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */
4818 (is_native || is_protected || is_final || is_synchronized) ||
4819 // If a specific method of a class or interface has its
4820 // ACC_ABSTRACT flag set, it must not have any of its
4821 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC,
4822 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to
4823 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as
4824 // those flags are illegal irrespective of ACC_ABSTRACT being set or not.
4825 (is_abstract && (is_private || is_static || is_strict))) {
4826 is_illegal = true;
4827 }
4998 vmSymbols::java_lang_ClassFormatError(),
4999 "Illegal field name \"%s\" in class %s", bytes,
5000 _class_name->as_C_string()
5001 );
5002 return;
5003 }
5004 }
5005
5006 // Checks if name is a legal method name.
5007 void ClassFileParser::verify_legal_method_name(Symbol* name, TRAPS) {
5008 if (!_need_verify || _relax_verify) { return; }
5009
5010 assert(name != NULL, "method name is null");
5011 char buf[fixed_buffer_size];
5012 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
5013 unsigned int length = name->utf8_length();
5014 bool legal = false;
5015
5016 if (length > 0) {
5017 if (bytes[0] == '<') {
5018 if (name->equals(vmSymbols::object_initializer_name()) || name->equals(vmSymbols::class_initializer_name())) {
5019 legal = true;
5020 }
5021 } else if (_major_version < JAVA_1_5_VERSION) {
5022 char* p;
5023 p = skip_over_field_name(bytes, false, length);
5024 legal = (p != NULL) && ((p - bytes) == (int)length);
5025 } else {
5026 // 4881221: relax the constraints based on JSR202 spec
5027 legal = verify_unqualified_name(bytes, length, LegalMethod);
5028 }
5029 }
5030
5031 if (!legal) {
5032 ResourceMark rm(THREAD);
5033 Exceptions::fthrow(
5034 THREAD_AND_LOCATION,
5035 vmSymbols::java_lang_ClassFormatError(),
5036 "Illegal method name \"%s\" in class %s", bytes,
5037 _class_name->as_C_string()
5038 );
|