1140 |
1148 |
1141 u2 constantvalue_index = 0; |
1149 u2 constantvalue_index = 0; |
1142 bool is_synthetic = false; |
1150 bool is_synthetic = false; |
1143 u2 generic_signature_index = 0; |
1151 u2 generic_signature_index = 0; |
1144 bool is_static = access_flags.is_static(); |
1152 bool is_static = access_flags.is_static(); |
|
1153 FieldAnnotationCollector parsed_annotations; |
1145 |
1154 |
1146 u2 attributes_count = cfs->get_u2_fast(); |
1155 u2 attributes_count = cfs->get_u2_fast(); |
1147 if (attributes_count > 0) { |
1156 if (attributes_count > 0) { |
1148 parse_field_attributes(cp, attributes_count, is_static, signature_index, |
1157 parse_field_attributes(cp, attributes_count, is_static, signature_index, |
1149 &constantvalue_index, &is_synthetic, |
1158 &constantvalue_index, &is_synthetic, |
1150 &generic_signature_index, &field_annotations, |
1159 &generic_signature_index, &field_annotations, |
|
1160 &parsed_annotations, |
1151 CHECK_(nullHandle)); |
1161 CHECK_(nullHandle)); |
1152 if (field_annotations.not_null()) { |
1162 if (field_annotations.not_null()) { |
1153 if (fields_annotations->is_null()) { |
1163 if (fields_annotations->is_null()) { |
1154 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); |
1164 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); |
1155 *fields_annotations = objArrayHandle(THREAD, md); |
1165 *fields_annotations = objArrayHandle(THREAD, md); |
1636 vmSymbols::java_lang_ClassFormatError(), |
1648 vmSymbols::java_lang_ClassFormatError(), |
1637 "%s \"%s\" in class %s has illegal signature \"%s\"", type, |
1649 "%s \"%s\" in class %s has illegal signature \"%s\"", type, |
1638 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); |
1650 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); |
1639 } |
1651 } |
1640 |
1652 |
|
1653 // Skip an annotation. Return >=limit if there is any problem. |
|
1654 int ClassFileParser::skip_annotation(u1* buffer, int limit, int index) { |
|
1655 // annotation := atype:u2 do(nmem:u2) {member:u2 value} |
|
1656 // value := switch (tag:u1) { ... } |
|
1657 index += 2; // skip atype |
|
1658 if ((index += 2) >= limit) return limit; // read nmem |
|
1659 int nmem = Bytes::get_Java_u2(buffer+index-2); |
|
1660 while (--nmem >= 0 && index < limit) { |
|
1661 index += 2; // skip member |
|
1662 index = skip_annotation_value(buffer, limit, index); |
|
1663 } |
|
1664 return index; |
|
1665 } |
|
1666 |
|
1667 // Skip an annotation value. Return >=limit if there is any problem. |
|
1668 int ClassFileParser::skip_annotation_value(u1* buffer, int limit, int index) { |
|
1669 // value := switch (tag:u1) { |
|
1670 // case B, C, I, S, Z, D, F, J, c: con:u2; |
|
1671 // case e: e_class:u2 e_name:u2; |
|
1672 // case s: s_con:u2; |
|
1673 // case [: do(nval:u2) {value}; |
|
1674 // case @: annotation; |
|
1675 // case s: s_con:u2; |
|
1676 // } |
|
1677 if ((index += 1) >= limit) return limit; // read tag |
|
1678 u1 tag = buffer[index-1]; |
|
1679 switch (tag) { |
|
1680 case 'B': case 'C': case 'I': case 'S': case 'Z': |
|
1681 case 'D': case 'F': case 'J': case 'c': case 's': |
|
1682 index += 2; // skip con or s_con |
|
1683 break; |
|
1684 case 'e': |
|
1685 index += 4; // skip e_class, e_name |
|
1686 break; |
|
1687 case '[': |
|
1688 { |
|
1689 if ((index += 2) >= limit) return limit; // read nval |
|
1690 int nval = Bytes::get_Java_u2(buffer+index-2); |
|
1691 while (--nval >= 0 && index < limit) { |
|
1692 index = skip_annotation_value(buffer, limit, index); |
|
1693 } |
|
1694 } |
|
1695 break; |
|
1696 case '@': |
|
1697 index = skip_annotation(buffer, limit, index); |
|
1698 break; |
|
1699 default: |
|
1700 assert(false, "annotation tag"); |
|
1701 return limit; // bad tag byte |
|
1702 } |
|
1703 return index; |
|
1704 } |
|
1705 |
|
1706 // Sift through annotations, looking for those significant to the VM: |
|
1707 void ClassFileParser::parse_annotations(u1* buffer, int limit, |
|
1708 constantPoolHandle cp, |
|
1709 ClassFileParser::AnnotationCollector* coll, |
|
1710 TRAPS) { |
|
1711 // annotations := do(nann:u2) {annotation} |
|
1712 int index = 0; |
|
1713 if ((index += 2) >= limit) return; // read nann |
|
1714 int nann = Bytes::get_Java_u2(buffer+index-2); |
|
1715 enum { // initial annotation layout |
|
1716 atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;' |
|
1717 count_off = 2, // u2 such as 1 (one value) |
|
1718 member_off = 4, // utf8 such as 'value' |
|
1719 tag_off = 6, // u1 such as 'c' (type) or 'e' (enum) |
|
1720 e_tag_val = 'e', |
|
1721 e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;' |
|
1722 e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME' |
|
1723 e_size = 11, // end of 'e' annotation |
|
1724 c_tag_val = 'c', |
|
1725 c_con_off = 7, // utf8 payload, such as 'I' or 'Ljava/lang/String;' |
|
1726 c_size = 9, // end of 'c' annotation |
|
1727 min_size = 6 // smallest possible size (zero members) |
|
1728 }; |
|
1729 while ((--nann) >= 0 && (index-2 + min_size <= limit)) { |
|
1730 int index0 = index; |
|
1731 index = skip_annotation(buffer, limit, index); |
|
1732 u1* abase = buffer + index0; |
|
1733 int atype = Bytes::get_Java_u2(abase + atype_off); |
|
1734 int count = Bytes::get_Java_u2(abase + count_off); |
|
1735 Symbol* aname = check_symbol_at(cp, atype); |
|
1736 if (aname == NULL) break; // invalid annotation name |
|
1737 Symbol* member = NULL; |
|
1738 if (count >= 1) { |
|
1739 int member_index = Bytes::get_Java_u2(abase + member_off); |
|
1740 member = check_symbol_at(cp, member_index); |
|
1741 if (member == NULL) break; // invalid member name |
|
1742 } |
|
1743 |
|
1744 // Here is where parsing particular annotations will take place. |
|
1745 AnnotationCollector::ID id = coll->annotation_index(aname); |
|
1746 if (id == AnnotationCollector::_unknown) continue; |
|
1747 coll->set_annotation(id); |
|
1748 // If there are no values, just set the bit and move on: |
|
1749 if (count == 0) continue; |
|
1750 |
|
1751 // For the record, here is how annotation payloads can be collected. |
|
1752 // Suppose we want to capture @Retention.value. Here is how: |
|
1753 //if (id == AnnotationCollector::_class_Retention) { |
|
1754 // Symbol* payload = NULL; |
|
1755 // if (count == 1 |
|
1756 // && e_size == (index0 - index) // match size |
|
1757 // && e_tag_val == *(abase + tag_off) |
|
1758 // && (check_symbol_at(cp, Bytes::get_Java_u2(abase + e_type_off)) |
|
1759 // == vmSymbols::RetentionPolicy_signature()) |
|
1760 // && member == vmSymbols::value_name()) { |
|
1761 // payload = check_symbol_at(cp, Bytes::get_Java_u2(abase + e_con_off)); |
|
1762 // } |
|
1763 // check_property(payload != NULL, |
|
1764 // "Invalid @Retention annotation at offset %u in class file %s", |
|
1765 // index0, CHECK); |
|
1766 // if (payload != NULL) { |
|
1767 // payload->increment_refcount(); |
|
1768 // coll->_class_RetentionPolicy = payload; |
|
1769 // } |
|
1770 //} |
|
1771 } |
|
1772 } |
|
1773 |
|
1774 ClassFileParser::AnnotationCollector::ID ClassFileParser::AnnotationCollector::annotation_index(Symbol* name) { |
|
1775 vmSymbols::SID sid = vmSymbols::find_sid(name); |
|
1776 switch (sid) { |
|
1777 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_ForceInline_signature): |
|
1778 if (_location != _in_method) break; // only allow for methods |
|
1779 return _method_ForceInline; |
|
1780 default: break; |
|
1781 } |
|
1782 return AnnotationCollector::_unknown; |
|
1783 } |
|
1784 |
|
1785 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) { |
|
1786 fatal("no field annotations yet"); |
|
1787 } |
|
1788 |
|
1789 void ClassFileParser::MethodAnnotationCollector::apply_to(methodHandle m) { |
|
1790 if (has_annotation(_method_ForceInline)) |
|
1791 m->set_force_inline(true); |
|
1792 } |
|
1793 |
|
1794 void ClassFileParser::ClassAnnotationCollector::apply_to(instanceKlassHandle k) { |
|
1795 fatal("no class annotations yet"); |
|
1796 } |
|
1797 |
|
1798 |
1641 #define MAX_ARGS_SIZE 255 |
1799 #define MAX_ARGS_SIZE 255 |
1642 #define MAX_CODE_SIZE 65535 |
1800 #define MAX_CODE_SIZE 65535 |
1643 #define INITIAL_MAX_LVT_NUMBER 256 |
1801 #define INITIAL_MAX_LVT_NUMBER 256 |
1644 |
1802 |
1645 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions |
1803 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions |
1646 // attribute is inlined. This is curbersome to avoid since we inline most of the parts in the |
1804 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the |
1647 // methodOop to save footprint, so we only know the size of the resulting methodOop when the |
1805 // methodOop to save footprint, so we only know the size of the resulting methodOop when the |
1648 // entire method attribute is parsed. |
1806 // entire method attribute is parsed. |
1649 // |
1807 // |
1650 // The promoted_flags parameter is used to pass relevant access_flags |
1808 // The promoted_flags parameter is used to pass relevant access_flags |
1651 // from the method back up to the containing klass. These flag values |
1809 // from the method back up to the containing klass. These flag values |
2312 return typeArrayHandle(THREAD, Universe::the_empty_int_array()); |
2474 return typeArrayHandle(THREAD, Universe::the_empty_int_array()); |
2313 } |
2475 } |
2314 } |
2476 } |
2315 |
2477 |
2316 |
2478 |
2317 void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { |
2479 void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, TRAPS) { |
2318 ClassFileStream* cfs = stream(); |
2480 ClassFileStream* cfs = stream(); |
2319 cfs->guarantee_more(2, CHECK); // sourcefile_index |
2481 cfs->guarantee_more(2, CHECK); // sourcefile_index |
2320 u2 sourcefile_index = cfs->get_u2_fast(); |
2482 u2 sourcefile_index = cfs->get_u2_fast(); |
2321 check_property( |
2483 check_property( |
2322 valid_cp_range(sourcefile_index, cp->length()) && |
2484 valid_cp_range(sourcefile_index, cp->length()) && |
2323 cp->tag_at(sourcefile_index).is_utf8(), |
2485 cp->tag_at(sourcefile_index).is_utf8(), |
2324 "Invalid SourceFile attribute at constant pool index %u in class file %s", |
2486 "Invalid SourceFile attribute at constant pool index %u in class file %s", |
2325 sourcefile_index, CHECK); |
2487 sourcefile_index, CHECK); |
2326 k->set_source_file_name(cp->symbol_at(sourcefile_index)); |
2488 set_class_sourcefile(cp->symbol_at(sourcefile_index)); |
2327 } |
2489 } |
2328 |
2490 |
2329 |
2491 |
2330 |
2492 |
2331 void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, |
2493 void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, |
2332 instanceKlassHandle k, |
|
2333 int length, TRAPS) { |
2494 int length, TRAPS) { |
2334 ClassFileStream* cfs = stream(); |
2495 ClassFileStream* cfs = stream(); |
2335 u1* sde_buffer = cfs->get_u1_buffer(); |
2496 u1* sde_buffer = cfs->get_u1_buffer(); |
2336 assert(sde_buffer != NULL, "null sde buffer"); |
2497 assert(sde_buffer != NULL, "null sde buffer"); |
2337 |
2498 |
2338 // Don't bother storing it if there is no way to retrieve it |
2499 // Don't bother storing it if there is no way to retrieve it |
2339 if (JvmtiExport::can_get_source_debug_extension()) { |
2500 if (JvmtiExport::can_get_source_debug_extension()) { |
2340 k->set_source_debug_extension((char*)sde_buffer, length); |
2501 assert((length+1) > length, "Overflow checking"); |
|
2502 u1* sde = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, u1, length+1); |
|
2503 for (int i = 0; i < length; i++) { |
|
2504 sde[i] = sde_buffer[i]; |
|
2505 } |
|
2506 sde[length] = '\0'; |
|
2507 set_class_sde_buffer((char*)sde, length); |
2341 } |
2508 } |
2342 // Got utf8 string, set stream position forward |
2509 // Got utf8 string, set stream position forward |
2343 cfs->skip_u1(length, CHECK); |
2510 cfs->skip_u1(length, CHECK); |
2344 } |
2511 } |
2345 |
2512 |
2442 inner_classes->short_at_put(index++, enclosing_method_method_index); |
2609 inner_classes->short_at_put(index++, enclosing_method_method_index); |
2443 } |
2610 } |
2444 assert(index == size, "wrong size"); |
2611 assert(index == size, "wrong size"); |
2445 |
2612 |
2446 // Update instanceKlass with inner class info. |
2613 // Update instanceKlass with inner class info. |
2447 k->set_inner_classes(inner_classes()); |
2614 set_class_inner_classes(inner_classes); |
2448 |
2615 |
2449 // Restore buffer's current position. |
2616 // Restore buffer's current position. |
2450 cfs->set_current(current_mark); |
2617 cfs->set_current(current_mark); |
2451 |
2618 |
2452 return length; |
2619 return length; |
2453 } |
2620 } |
2454 |
2621 |
2455 void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { |
2622 void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, TRAPS) { |
2456 k->set_is_synthetic(); |
2623 set_class_synthetic_flag(true); |
2457 } |
2624 } |
2458 |
2625 |
2459 void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { |
2626 void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, TRAPS) { |
2460 ClassFileStream* cfs = stream(); |
2627 ClassFileStream* cfs = stream(); |
2461 u2 signature_index = cfs->get_u2(CHECK); |
2628 u2 signature_index = cfs->get_u2(CHECK); |
2462 check_property( |
2629 check_property( |
2463 valid_cp_range(signature_index, cp->length()) && |
2630 valid_cp_range(signature_index, cp->length()) && |
2464 cp->tag_at(signature_index).is_utf8(), |
2631 cp->tag_at(signature_index).is_utf8(), |
2465 "Invalid constant pool index %u in Signature attribute in class file %s", |
2632 "Invalid constant pool index %u in Signature attribute in class file %s", |
2466 signature_index, CHECK); |
2633 signature_index, CHECK); |
2467 k->set_generic_signature(cp->symbol_at(signature_index)); |
2634 set_class_generic_signature(cp->symbol_at(signature_index)); |
2468 } |
2635 } |
2469 |
2636 |
2470 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, instanceKlassHandle k, |
2637 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, |
2471 u4 attribute_byte_length, TRAPS) { |
2638 u4 attribute_byte_length, TRAPS) { |
2472 ClassFileStream* cfs = stream(); |
2639 ClassFileStream* cfs = stream(); |
2473 u1* current_start = cfs->current(); |
2640 u1* current_start = cfs->current(); |
2474 |
2641 |
2475 cfs->guarantee_more(2, CHECK); // length |
2642 cfs->guarantee_more(2, CHECK); // length |
2613 if (attribute_length != 2) { |
2782 if (attribute_length != 2) { |
2614 classfile_parse_error( |
2783 classfile_parse_error( |
2615 "Wrong Signature attribute length %u in class file %s", |
2784 "Wrong Signature attribute length %u in class file %s", |
2616 attribute_length, CHECK); |
2785 attribute_length, CHECK); |
2617 } |
2786 } |
2618 parse_classfile_signature_attribute(cp, k, CHECK); |
2787 parse_classfile_signature_attribute(cp, CHECK); |
2619 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { |
2788 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { |
2620 runtime_visible_annotations_length = attribute_length; |
2789 runtime_visible_annotations_length = attribute_length; |
2621 runtime_visible_annotations = cfs->get_u1_buffer(); |
2790 runtime_visible_annotations = cfs->get_u1_buffer(); |
2622 assert(runtime_visible_annotations != NULL, "null visible annotations"); |
2791 assert(runtime_visible_annotations != NULL, "null visible annotations"); |
|
2792 parse_annotations(runtime_visible_annotations, |
|
2793 runtime_visible_annotations_length, |
|
2794 cp, |
|
2795 parsed_annotations, |
|
2796 CHECK); |
2623 cfs->skip_u1(runtime_visible_annotations_length, CHECK); |
2797 cfs->skip_u1(runtime_visible_annotations_length, CHECK); |
2624 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) { |
2798 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) { |
2625 runtime_invisible_annotations_length = attribute_length; |
2799 runtime_invisible_annotations_length = attribute_length; |
2626 runtime_invisible_annotations = cfs->get_u1_buffer(); |
2800 runtime_invisible_annotations = cfs->get_u1_buffer(); |
2627 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); |
2801 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); |
2666 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations, |
2840 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations, |
2667 runtime_visible_annotations_length, |
2841 runtime_visible_annotations_length, |
2668 runtime_invisible_annotations, |
2842 runtime_invisible_annotations, |
2669 runtime_invisible_annotations_length, |
2843 runtime_invisible_annotations_length, |
2670 CHECK); |
2844 CHECK); |
2671 k->set_class_annotations(annotations()); |
2845 set_class_annotations(annotations); |
2672 |
2846 |
2673 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { |
2847 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { |
2674 u2 num_of_classes = parse_classfile_inner_classes_attribute( |
2848 u2 num_of_classes = parse_classfile_inner_classes_attribute( |
2675 inner_classes_attribute_start, |
2849 inner_classes_attribute_start, |
2676 parsed_innerclasses_attribute, |
2850 parsed_innerclasses_attribute, |
2677 enclosing_method_class_index, |
2851 enclosing_method_class_index, |
2678 enclosing_method_method_index, |
2852 enclosing_method_method_index, |
2679 cp, k, CHECK); |
2853 cp, CHECK); |
2680 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) { |
2854 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) { |
2681 guarantee_property( |
2855 guarantee_property( |
2682 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, |
2856 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, |
2683 "Wrong InnerClasses attribute length in class file %s", CHECK); |
2857 "Wrong InnerClasses attribute length in class file %s", CHECK); |
2684 } |
2858 } |
2688 guarantee_property(parsed_bootstrap_methods_attribute, |
2862 guarantee_property(parsed_bootstrap_methods_attribute, |
2689 "Missing BootstrapMethods attribute in class file %s", CHECK); |
2863 "Missing BootstrapMethods attribute in class file %s", CHECK); |
2690 } |
2864 } |
2691 } |
2865 } |
2692 |
2866 |
|
2867 void ClassFileParser::apply_parsed_class_attributes(instanceKlassHandle k) { |
|
2868 if (_synthetic_flag) |
|
2869 k->set_is_synthetic(); |
|
2870 if (_sourcefile != NULL) { |
|
2871 _sourcefile->increment_refcount(); |
|
2872 k->set_source_file_name(_sourcefile); |
|
2873 } |
|
2874 if (_generic_signature != NULL) { |
|
2875 _generic_signature->increment_refcount(); |
|
2876 k->set_generic_signature(_generic_signature); |
|
2877 } |
|
2878 if (_sde_buffer != NULL) { |
|
2879 k->set_source_debug_extension(_sde_buffer, _sde_length); |
|
2880 } |
|
2881 k->set_inner_classes(_inner_classes()); |
|
2882 k->set_class_annotations(_annotations()); |
|
2883 } |
2693 |
2884 |
2694 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations, |
2885 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations, |
2695 int runtime_visible_annotations_length, |
2886 int runtime_visible_annotations_length, |
2696 u1* runtime_invisible_annotations, |
2887 u1* runtime_invisible_annotations, |
2697 int runtime_invisible_annotations_length, TRAPS) { |
2888 int runtime_invisible_annotations_length, TRAPS) { |