1229 }
1230 }
1231 }
1232 if (dup) {
1233 classfile_parse_error("Duplicate field name&signature in class file %s",
1234 CHECK_(nullHandle));
1235 }
1236 }
1237
1238 return fields;
1239 }
1240
1241
1242 static void copy_u2_with_conversion(u2* dest, u2* src, int length) {
1243 while (length-- > 0) {
1244 *dest++ = Bytes::get_Java_u2((u1*) (src++));
1245 }
1246 }
1247
1248
1249 typeArrayHandle ClassFileParser::parse_exception_table(u4 code_length,
1250 u4 exception_table_length,
1251 constantPoolHandle cp,
1252 TRAPS) {
1253 ClassFileStream* cfs = stream();
1254 typeArrayHandle nullHandle;
1255
1256 // 4-tuples of ints [start_pc, end_pc, handler_pc, catch_type index]
1257 typeArrayOop eh = oopFactory::new_permanent_intArray(exception_table_length*4, CHECK_(nullHandle));
1258 typeArrayHandle exception_handlers = typeArrayHandle(THREAD, eh);
1259
1260 int index = 0;
1261 cfs->guarantee_more(8 * exception_table_length, CHECK_(nullHandle)); // start_pc, end_pc, handler_pc, catch_type_index
1262 for (unsigned int i = 0; i < exception_table_length; i++) {
1263 u2 start_pc = cfs->get_u2_fast();
1264 u2 end_pc = cfs->get_u2_fast();
1265 u2 handler_pc = cfs->get_u2_fast();
1266 u2 catch_type_index = cfs->get_u2_fast();
1267 // Will check legal target after parsing code array in verifier.
1268 if (_need_verify) {
1269 guarantee_property((start_pc < end_pc) && (end_pc <= code_length),
1270 "Illegal exception table range in class file %s", CHECK_(nullHandle));
1271 guarantee_property(handler_pc < code_length,
1272 "Illegal exception table handler in class file %s", CHECK_(nullHandle));
1273 if (catch_type_index != 0) {
1274 guarantee_property(valid_cp_range(catch_type_index, cp->length()) &&
1275 is_klass_reference(cp, catch_type_index),
1276 "Catch type in exception table has bad constant type in class file %s", CHECK_(nullHandle));
1277 }
1278 }
1279 exception_handlers->int_at_put(index++, start_pc);
1280 exception_handlers->int_at_put(index++, end_pc);
1281 exception_handlers->int_at_put(index++, handler_pc);
1282 exception_handlers->int_at_put(index++, catch_type_index);
1283 }
1284 return exception_handlers;
1285 }
1286
1287 void ClassFileParser::parse_linenumber_table(
1288 u4 code_attribute_length, u4 code_length,
1289 CompressedLineNumberWriteStream** write_stream, TRAPS) {
1290 ClassFileStream* cfs = stream();
1291 unsigned int num_entries = cfs->get_u2(CHECK);
1292
1293 // Each entry is a u2 start_pc, and a u2 line_number
1294 unsigned int length_in_bytes = num_entries * (sizeof(u2) + sizeof(u2));
1295
1296 // Verify line number attribute and table length
1297 check_property(
1298 code_attribute_length == sizeof(u2) + length_in_bytes,
1299 "LineNumberTable attribute has wrong length in class file %s", CHECK);
1300
1301 cfs->guarantee_more(length_in_bytes, CHECK);
1302
1303 if ((*write_stream) == NULL) {
1304 if (length_in_bytes > fixed_buffer_size) {
1657 verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle));
1658 }
1659
1660 int args_size = -1; // only used when _need_verify is true
1661 if (_need_verify) {
1662 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
1663 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
1664 if (args_size > MAX_ARGS_SIZE) {
1665 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_(nullHandle));
1666 }
1667 }
1668
1669 access_flags.set_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
1670
1671 // Default values for code and exceptions attribute elements
1672 u2 max_stack = 0;
1673 u2 max_locals = 0;
1674 u4 code_length = 0;
1675 u1* code_start = 0;
1676 u2 exception_table_length = 0;
1677 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
1678 u2 checked_exceptions_length = 0;
1679 u2* checked_exceptions_start = NULL;
1680 CompressedLineNumberWriteStream* linenumber_table = NULL;
1681 int linenumber_table_length = 0;
1682 int total_lvt_length = 0;
1683 u2 lvt_cnt = 0;
1684 u2 lvtt_cnt = 0;
1685 bool lvt_allocated = false;
1686 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER;
1687 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER;
1688 u2* localvariable_table_length;
1689 u2** localvariable_table_start;
1690 u2* localvariable_type_table_length;
1691 u2** localvariable_type_table_start;
1692 bool parsed_code_attribute = false;
1693 bool parsed_checked_exceptions_attribute = false;
1694 bool parsed_stackmap_attribute = false;
1695 // stackmap attribute - JDK1.5
1696 typeArrayHandle stackmap_data;
1743 max_locals = cfs->get_u2_fast();
1744 code_length = cfs->get_u4_fast();
1745 }
1746 if (_need_verify) {
1747 guarantee_property(args_size <= max_locals,
1748 "Arguments can't fit into locals in class file %s", CHECK_(nullHandle));
1749 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE,
1750 "Invalid method Code length %u in class file %s",
1751 code_length, CHECK_(nullHandle));
1752 }
1753 // Code pointer
1754 code_start = cfs->get_u1_buffer();
1755 assert(code_start != NULL, "null code start");
1756 cfs->guarantee_more(code_length, CHECK_(nullHandle));
1757 cfs->skip_u1_fast(code_length);
1758
1759 // Exception handler table
1760 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length
1761 exception_table_length = cfs->get_u2_fast();
1762 if (exception_table_length > 0) {
1763 exception_handlers =
1764 parse_exception_table(code_length, exception_table_length, cp, CHECK_(nullHandle));
1765 }
1766
1767 // Parse additional attributes in code attribute
1768 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count
1769 u2 code_attributes_count = cfs->get_u2_fast();
1770
1771 unsigned int calculated_attribute_length = 0;
1772
1773 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) {
1774 calculated_attribute_length =
1775 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length);
1776 } else {
1777 // max_stack, locals and length are smaller in pre-version 45.2 classes
1778 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2);
1779 }
1780 calculated_attribute_length +=
1781 code_length +
1782 sizeof(exception_table_length) +
1783 sizeof(code_attributes_count) +
1947 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1948 }
1949 } else {
1950 // Skip unknown attributes
1951 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1952 }
1953 }
1954
1955 if (linenumber_table != NULL) {
1956 linenumber_table->write_terminator();
1957 linenumber_table_length = linenumber_table->position();
1958 }
1959
1960 // Make sure there's at least one Code attribute in non-native/non-abstract method
1961 if (_need_verify) {
1962 guarantee_property(access_flags.is_native() || access_flags.is_abstract() || parsed_code_attribute,
1963 "Absent Code attribute in method that is not native or abstract in class file %s", CHECK_(nullHandle));
1964 }
1965
1966 // All sizing information for a methodOop is finally available, now create it
1967 methodOop m_oop = oopFactory::new_method(code_length, access_flags, linenumber_table_length,
1968 total_lvt_length, checked_exceptions_length,
1969 oopDesc::IsSafeConc, CHECK_(nullHandle));
1970 methodHandle m (THREAD, m_oop);
1971
1972 ClassLoadingService::add_class_method_size(m_oop->size()*HeapWordSize);
1973
1974 // Fill in information from fixed part (access_flags already set)
1975 m->set_constants(cp());
1976 m->set_name_index(name_index);
1977 m->set_signature_index(signature_index);
1978 m->set_generic_signature_index(generic_signature_index);
1979 #ifdef CC_INTERP
1980 // hmm is there a gc issue here??
1981 ResultTypeFinder rtf(cp->symbol_at(signature_index));
1982 m->set_result_index(rtf.type());
1983 #endif
1984
1985 if (args_size >= 0) {
1986 m->set_size_of_parameters(args_size);
1987 } else {
1988 m->compute_size_of_parameters(THREAD);
1989 }
1990 #ifdef ASSERT
1991 if (args_size >= 0) {
1992 m->compute_size_of_parameters(THREAD);
1993 assert(args_size == m->size_of_parameters(), "");
1994 }
1995 #endif
1996
1997 // Fill in code attribute information
1998 m->set_max_stack(max_stack);
1999 m->set_max_locals(max_locals);
2000 m->constMethod()->set_stackmap_data(stackmap_data());
2001
2002 /**
2003 * The exception_table field is the flag used to indicate
2004 * that the methodOop and it's associated constMethodOop are partially
2005 * initialized and thus are exempt from pre/post GC verification. Once
2006 * the field is set, the oops are considered fully initialized so make
2007 * sure that the oops can pass verification when this field is set.
2008 */
2009 m->set_exception_table(exception_handlers());
2010
2011 // Copy byte codes
2012 m->set_code(code_start);
2013
2014 // Copy line number table
2015 if (linenumber_table != NULL) {
2016 memcpy(m->compressed_linenumber_table(),
2017 linenumber_table->buffer(), linenumber_table_length);
2018 }
2019
2020 // Copy checked exceptions
2021 if (checked_exceptions_length > 0) {
2022 int size = checked_exceptions_length * sizeof(CheckedExceptionElement) / sizeof(u2);
2023 copy_u2_with_conversion((u2*) m->checked_exceptions_start(), checked_exceptions_start, size);
2024 }
2025
2026 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT.
2027 *
2028 * Rules for LVT's and LVTT's are:
2029 * - There can be any number of LVT's and LVTT's.
2030 * - If there are n LVT's, it is the same as if there was just
2031 * one LVT containing all the entries from the n LVT's.
2032 * - There may be no more than one LVT entry per local variable.
2033 * Two LVT entries are 'equal' if these fields are the same:
2034 * start_pc, length, name, slot
2035 * - There may be no more than one LVTT entry per each LVT entry.
2036 * Each LVTT entry has to match some LVT entry.
2037 * - HotSpot internal LVT keeps natural ordering of class file LVT entries.
2038 */
2039 if (total_lvt_length > 0) {
|
1229 }
1230 }
1231 }
1232 if (dup) {
1233 classfile_parse_error("Duplicate field name&signature in class file %s",
1234 CHECK_(nullHandle));
1235 }
1236 }
1237
1238 return fields;
1239 }
1240
1241
1242 static void copy_u2_with_conversion(u2* dest, u2* src, int length) {
1243 while (length-- > 0) {
1244 *dest++ = Bytes::get_Java_u2((u1*) (src++));
1245 }
1246 }
1247
1248
1249 u2* ClassFileParser::parse_exception_table(u4 code_length,
1250 u4 exception_table_length,
1251 constantPoolHandle cp,
1252 TRAPS) {
1253 ClassFileStream* cfs = stream();
1254
1255 u2* exception_table_start = cfs->get_u2_buffer();
1256 assert(exception_table_start != NULL, "null exception table");
1257 cfs->guarantee_more(8 * exception_table_length, CHECK_NULL); // start_pc, end_pc, handler_pc, catch_type_index
1258 // Will check legal target after parsing code array in verifier.
1259 if (_need_verify) {
1260 for (unsigned int i = 0; i < exception_table_length; i++) {
1261 u2 start_pc = cfs->get_u2_fast();
1262 u2 end_pc = cfs->get_u2_fast();
1263 u2 handler_pc = cfs->get_u2_fast();
1264 u2 catch_type_index = cfs->get_u2_fast();
1265 guarantee_property((start_pc < end_pc) && (end_pc <= code_length),
1266 "Illegal exception table range in class file %s",
1267 CHECK_NULL);
1268 guarantee_property(handler_pc < code_length,
1269 "Illegal exception table handler in class file %s",
1270 CHECK_NULL);
1271 if (catch_type_index != 0) {
1272 guarantee_property(valid_cp_range(catch_type_index, cp->length()) &&
1273 is_klass_reference(cp, catch_type_index),
1274 "Catch type in exception table has bad constant type in class file %s", CHECK_NULL);
1275 }
1276 }
1277 } else {
1278 cfs->skip_u2_fast(exception_table_length * 4);
1279 }
1280 return exception_table_start;
1281 }
1282
1283 void ClassFileParser::parse_linenumber_table(
1284 u4 code_attribute_length, u4 code_length,
1285 CompressedLineNumberWriteStream** write_stream, TRAPS) {
1286 ClassFileStream* cfs = stream();
1287 unsigned int num_entries = cfs->get_u2(CHECK);
1288
1289 // Each entry is a u2 start_pc, and a u2 line_number
1290 unsigned int length_in_bytes = num_entries * (sizeof(u2) + sizeof(u2));
1291
1292 // Verify line number attribute and table length
1293 check_property(
1294 code_attribute_length == sizeof(u2) + length_in_bytes,
1295 "LineNumberTable attribute has wrong length in class file %s", CHECK);
1296
1297 cfs->guarantee_more(length_in_bytes, CHECK);
1298
1299 if ((*write_stream) == NULL) {
1300 if (length_in_bytes > fixed_buffer_size) {
1653 verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle));
1654 }
1655
1656 int args_size = -1; // only used when _need_verify is true
1657 if (_need_verify) {
1658 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
1659 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
1660 if (args_size > MAX_ARGS_SIZE) {
1661 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_(nullHandle));
1662 }
1663 }
1664
1665 access_flags.set_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
1666
1667 // Default values for code and exceptions attribute elements
1668 u2 max_stack = 0;
1669 u2 max_locals = 0;
1670 u4 code_length = 0;
1671 u1* code_start = 0;
1672 u2 exception_table_length = 0;
1673 u2* exception_table_start = NULL;
1674 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
1675 u2 checked_exceptions_length = 0;
1676 u2* checked_exceptions_start = NULL;
1677 CompressedLineNumberWriteStream* linenumber_table = NULL;
1678 int linenumber_table_length = 0;
1679 int total_lvt_length = 0;
1680 u2 lvt_cnt = 0;
1681 u2 lvtt_cnt = 0;
1682 bool lvt_allocated = false;
1683 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER;
1684 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER;
1685 u2* localvariable_table_length;
1686 u2** localvariable_table_start;
1687 u2* localvariable_type_table_length;
1688 u2** localvariable_type_table_start;
1689 bool parsed_code_attribute = false;
1690 bool parsed_checked_exceptions_attribute = false;
1691 bool parsed_stackmap_attribute = false;
1692 // stackmap attribute - JDK1.5
1693 typeArrayHandle stackmap_data;
1740 max_locals = cfs->get_u2_fast();
1741 code_length = cfs->get_u4_fast();
1742 }
1743 if (_need_verify) {
1744 guarantee_property(args_size <= max_locals,
1745 "Arguments can't fit into locals in class file %s", CHECK_(nullHandle));
1746 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE,
1747 "Invalid method Code length %u in class file %s",
1748 code_length, CHECK_(nullHandle));
1749 }
1750 // Code pointer
1751 code_start = cfs->get_u1_buffer();
1752 assert(code_start != NULL, "null code start");
1753 cfs->guarantee_more(code_length, CHECK_(nullHandle));
1754 cfs->skip_u1_fast(code_length);
1755
1756 // Exception handler table
1757 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length
1758 exception_table_length = cfs->get_u2_fast();
1759 if (exception_table_length > 0) {
1760 exception_table_start =
1761 parse_exception_table(code_length, exception_table_length, cp, CHECK_(nullHandle));
1762 }
1763
1764 // Parse additional attributes in code attribute
1765 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count
1766 u2 code_attributes_count = cfs->get_u2_fast();
1767
1768 unsigned int calculated_attribute_length = 0;
1769
1770 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) {
1771 calculated_attribute_length =
1772 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length);
1773 } else {
1774 // max_stack, locals and length are smaller in pre-version 45.2 classes
1775 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2);
1776 }
1777 calculated_attribute_length +=
1778 code_length +
1779 sizeof(exception_table_length) +
1780 sizeof(code_attributes_count) +
1944 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1945 }
1946 } else {
1947 // Skip unknown attributes
1948 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1949 }
1950 }
1951
1952 if (linenumber_table != NULL) {
1953 linenumber_table->write_terminator();
1954 linenumber_table_length = linenumber_table->position();
1955 }
1956
1957 // Make sure there's at least one Code attribute in non-native/non-abstract method
1958 if (_need_verify) {
1959 guarantee_property(access_flags.is_native() || access_flags.is_abstract() || parsed_code_attribute,
1960 "Absent Code attribute in method that is not native or abstract in class file %s", CHECK_(nullHandle));
1961 }
1962
1963 // All sizing information for a methodOop is finally available, now create it
1964 methodOop m_oop = oopFactory::new_method(code_length, access_flags,
1965 linenumber_table_length,
1966 total_lvt_length,
1967 exception_table_length,
1968 checked_exceptions_length,
1969 oopDesc::IsSafeConc,
1970 CHECK_(nullHandle));
1971 methodHandle m (THREAD, m_oop);
1972
1973 ClassLoadingService::add_class_method_size(m_oop->size()*HeapWordSize);
1974
1975 // Fill in information from fixed part (access_flags already set)
1976 m->set_constants(cp());
1977 m->set_name_index(name_index);
1978 m->set_signature_index(signature_index);
1979 m->set_generic_signature_index(generic_signature_index);
1980 #ifdef CC_INTERP
1981 // hmm is there a gc issue here??
1982 ResultTypeFinder rtf(cp->symbol_at(signature_index));
1983 m->set_result_index(rtf.type());
1984 #endif
1985
1986 if (args_size >= 0) {
1987 m->set_size_of_parameters(args_size);
1988 } else {
1989 m->compute_size_of_parameters(THREAD);
1990 }
1991 #ifdef ASSERT
1992 if (args_size >= 0) {
1993 m->compute_size_of_parameters(THREAD);
1994 assert(args_size == m->size_of_parameters(), "");
1995 }
1996 #endif
1997
1998 // Fill in code attribute information
1999 m->set_max_stack(max_stack);
2000 m->set_max_locals(max_locals);
2001
2002 /**
2003 * The stackmap_data field is the flag used to indicate
2004 * that the methodOop and it's associated constMethodOop are partially
2005 * initialized and thus are exempt from pre/post GC verification. Once
2006 * the field is set, the oops are considered fully initialized so make
2007 * sure that the oops can pass verification when this field is set.
2008 */
2009 m->constMethod()->set_stackmap_data(stackmap_data());
2010
2011 // Copy byte codes
2012 m->set_code(code_start);
2013
2014 // Copy line number table
2015 if (linenumber_table != NULL) {
2016 memcpy(m->compressed_linenumber_table(),
2017 linenumber_table->buffer(), linenumber_table_length);
2018 }
2019
2020 // Copy exception table
2021 if (exception_table_length > 0) {
2022 int size =
2023 exception_table_length * sizeof(ExceptionTableElement) / sizeof(u2);
2024 copy_u2_with_conversion((u2*) m->exception_table_start(),
2025 exception_table_start, size);
2026 }
2027
2028 // Copy checked exceptions
2029 if (checked_exceptions_length > 0) {
2030 int size = checked_exceptions_length * sizeof(CheckedExceptionElement) / sizeof(u2);
2031 copy_u2_with_conversion((u2*) m->checked_exceptions_start(), checked_exceptions_start, size);
2032 }
2033
2034 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT.
2035 *
2036 * Rules for LVT's and LVTT's are:
2037 * - There can be any number of LVT's and LVTT's.
2038 * - If there are n LVT's, it is the same as if there was just
2039 * one LVT containing all the entries from the n LVT's.
2040 * - There may be no more than one LVT entry per local variable.
2041 * Two LVT entries are 'equal' if these fields are the same:
2042 * start_pc, length, name, slot
2043 * - There may be no more than one LVTT entry per each LVT entry.
2044 * Each LVTT entry has to match some LVT entry.
2045 * - HotSpot internal LVT keeps natural ordering of class file LVT entries.
2046 */
2047 if (total_lvt_length > 0) {
|