src/share/vm/classfile/classFileParser.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 7086585 Sdiff src/share/vm/classfile

src/share/vm/classfile/classFileParser.cpp

Print this page




  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classFileParser.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/symbolTable.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/verificationType.hpp"
  32 #include "classfile/verifier.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "memory/allocation.hpp"
  35 #include "memory/gcLocker.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/universe.inline.hpp"
  38 #include "oops/constantPoolOop.hpp"

  39 #include "oops/instanceKlass.hpp"
  40 #include "oops/instanceMirrorKlass.hpp"
  41 #include "oops/klass.inline.hpp"
  42 #include "oops/klassOop.hpp"
  43 #include "oops/klassVtable.hpp"
  44 #include "oops/methodOop.hpp"
  45 #include "oops/symbol.hpp"
  46 #include "prims/jvmtiExport.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/perfData.hpp"
  49 #include "runtime/reflection.hpp"
  50 #include "runtime/signature.hpp"
  51 #include "runtime/timer.hpp"
  52 #include "services/classLoadingService.hpp"
  53 #include "services/threadService.hpp"
  54 
  55 // We generally try to create the oops directly when parsing, rather than
  56 // allocating temporary data structures and copying the bytes twice. A
  57 // temporary area is only needed when parsing utf8 entries in the constant
  58 // pool and when parsing line number tables.


 974 
 975   *constantvalue_index_addr = constantvalue_index;
 976   *is_synthetic_addr = is_synthetic;
 977   *generic_signature_index_addr = generic_signature_index;
 978   *field_annotations = assemble_annotations(runtime_visible_annotations,
 979                                             runtime_visible_annotations_length,
 980                                             runtime_invisible_annotations,
 981                                             runtime_invisible_annotations_length,
 982                                             CHECK);
 983   return;
 984 }
 985 
 986 
 987 // Field allocation types. Used for computing field offsets.
 988 
 989 enum FieldAllocationType {
 990   STATIC_OOP,           // Oops
 991   STATIC_BYTE,          // Boolean, Byte, char
 992   STATIC_SHORT,         // shorts
 993   STATIC_WORD,          // ints
 994   STATIC_DOUBLE,        // long or double
 995   STATIC_ALIGNED_DOUBLE,// aligned long or double
 996   NONSTATIC_OOP,
 997   NONSTATIC_BYTE,
 998   NONSTATIC_SHORT,
 999   NONSTATIC_WORD,
1000   NONSTATIC_DOUBLE,
1001   NONSTATIC_ALIGNED_DOUBLE

1002 };
1003 






































1004 
1005 struct FieldAllocationCount {
1006   unsigned int static_oop_count;
1007   unsigned int static_byte_count;
1008   unsigned int static_short_count;
1009   unsigned int static_word_count;
1010   unsigned int static_double_count;
1011   unsigned int nonstatic_oop_count;
1012   unsigned int nonstatic_byte_count;
1013   unsigned int nonstatic_short_count;
1014   unsigned int nonstatic_word_count;
1015   unsigned int nonstatic_double_count;











1016 };
1017 
1018 typeArrayHandle ClassFileParser::parse_fields(constantPoolHandle cp, bool is_interface,
1019                                               struct FieldAllocationCount *fac,
1020                                               objArrayHandle* fields_annotations, TRAPS) {



1021   ClassFileStream* cfs = stream();
1022   typeArrayHandle nullHandle;
1023   cfs->guarantee_more(2, CHECK_(nullHandle));  // length
1024   u2 length = cfs->get_u2_fast();





1025   // Tuples of shorts [access, name index, sig index, initial value index, byte offset, generic signature index]
1026   typeArrayOop new_fields = oopFactory::new_permanent_shortArray(length*instanceKlass::next_offset, CHECK_(nullHandle));
1027   typeArrayHandle fields(THREAD, new_fields);
1028 
1029   int index = 0;
1030   typeArrayHandle field_annotations;
1031   for (int n = 0; n < length; n++) {
1032     cfs->guarantee_more(8, CHECK_(nullHandle));  // access_flags, name_index, descriptor_index, attributes_count
1033 
1034     AccessFlags access_flags;
1035     jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1036     verify_legal_field_modifiers(flags, is_interface, CHECK_(nullHandle));
1037     access_flags.set_flags(flags);
1038 
1039     u2 name_index = cfs->get_u2_fast();
1040     int cp_size = cp->length();
1041     check_property(
1042       valid_cp_range(name_index, cp_size) && cp->tag_at(name_index).is_utf8(),
1043       "Invalid constant pool index %u for field name in class file %s",
1044       name_index, CHECK_(nullHandle));
1045     Symbol*  name = cp->symbol_at(name_index);
1046     verify_legal_field_name(name, CHECK_(nullHandle));
1047 
1048     u2 signature_index = cfs->get_u2_fast();
1049     check_property(


1060     bool is_static = access_flags.is_static();
1061 
1062     u2 attributes_count = cfs->get_u2_fast();
1063     if (attributes_count > 0) {
1064       parse_field_attributes(cp, attributes_count, is_static, signature_index,
1065                              &constantvalue_index, &is_synthetic,
1066                              &generic_signature_index, &field_annotations,
1067                              CHECK_(nullHandle));
1068       if (field_annotations.not_null()) {
1069         if (fields_annotations->is_null()) {
1070           objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1071           *fields_annotations = objArrayHandle(THREAD, md);
1072         }
1073         (*fields_annotations)->obj_at_put(n, field_annotations());
1074       }
1075       if (is_synthetic) {
1076         access_flags.set_is_synthetic();
1077       }
1078     }
1079 
1080     fields->short_at_put(index++, access_flags.as_short());
1081     fields->short_at_put(index++, name_index);
1082     fields->short_at_put(index++, signature_index);
1083     fields->short_at_put(index++, constantvalue_index);



1084 
1085     // Remember how many oops we encountered and compute allocation type
1086     BasicType type = cp->basic_type_for_signature_at(signature_index);
1087     FieldAllocationType atype;
1088     if ( is_static ) {
1089       switch ( type ) {
1090         case  T_BOOLEAN:
1091         case  T_BYTE:
1092           fac->static_byte_count++;
1093           atype = STATIC_BYTE;
1094           break;
1095         case  T_LONG:
1096         case  T_DOUBLE:
1097           if (Universe::field_type_should_be_aligned(type)) {
1098             atype = STATIC_ALIGNED_DOUBLE;
1099           } else {
1100             atype = STATIC_DOUBLE;
1101           }
1102           fac->static_double_count++;














1103           break;
1104         case  T_CHAR:
1105         case  T_SHORT:
1106           fac->static_short_count++;
1107           atype = STATIC_SHORT;
1108           break;
1109         case  T_FLOAT:
1110         case  T_INT:
1111           fac->static_word_count++;
1112           atype = STATIC_WORD;
1113           break;
1114         case  T_ARRAY:
1115         case  T_OBJECT:
1116           fac->static_oop_count++;
1117           atype = STATIC_OOP;
1118           break;
1119         case  T_ADDRESS:
1120         case  T_VOID:
1121         default:
1122           assert(0, "bad field type");
1123       }
1124     } else {
1125       switch ( type ) {
1126         case  T_BOOLEAN:
1127         case  T_BYTE:
1128           fac->nonstatic_byte_count++;
1129           atype = NONSTATIC_BYTE;
1130           break;
1131         case  T_LONG:
1132         case  T_DOUBLE:
1133           if (Universe::field_type_should_be_aligned(type)) {
1134             atype = NONSTATIC_ALIGNED_DOUBLE;
1135           } else {
1136             atype = NONSTATIC_DOUBLE;
1137           }
1138           fac->nonstatic_double_count++;
1139           break;
1140         case  T_CHAR:
1141         case  T_SHORT:
1142           fac->nonstatic_short_count++;
1143           atype = NONSTATIC_SHORT;
1144           break;
1145         case  T_FLOAT:
1146         case  T_INT:
1147           fac->nonstatic_word_count++;
1148           atype = NONSTATIC_WORD;
1149           break;
1150         case  T_ARRAY:
1151         case  T_OBJECT:
1152           fac->nonstatic_oop_count++;
1153           atype = NONSTATIC_OOP;
1154           break;
1155         case  T_ADDRESS:
1156         case  T_VOID:
1157         default:
1158           assert(0, "bad field type");
1159       }
1160     }
1161 














1162     // The correct offset is computed later (all oop fields will be located together)
1163     // We temporarily store the allocation type in the offset field
1164     fields->short_at_put(index++, atype);
1165     fields->short_at_put(index++, 0);  // Clear out high word of byte offset
1166     fields->short_at_put(index++, generic_signature_index);
1167   }
1168 












1169   if (_need_verify && length > 1) {
1170     // Check duplicated fields
1171     ResourceMark rm(THREAD);
1172     NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
1173       THREAD, NameSigHash*, HASH_ROW_SIZE);
1174     initialize_hashtable(names_and_sigs);
1175     bool dup = false;
1176     {
1177       debug_only(No_Safepoint_Verifier nsv;)
1178       for (int i = 0; i < length*instanceKlass::next_offset; i += instanceKlass::next_offset) {
1179         int name_index = fields->ushort_at(i + instanceKlass::name_index_offset);
1180         Symbol* name = cp->symbol_at(name_index);
1181         int sig_index = fields->ushort_at(i + instanceKlass::signature_index_offset);
1182         Symbol* sig = cp->symbol_at(sig_index);
1183         // If no duplicates, add name/signature in hashtable names_and_sigs.
1184         if (!put_after_lookup(name, sig, names_and_sigs)) {
1185           dup = true;
1186           break;
1187         }
1188       }
1189     }
1190     if (dup) {
1191       classfile_parse_error("Duplicate field name&signature in class file %s",
1192                             CHECK_(nullHandle));
1193     }
1194   }
1195 
1196   return fields;
1197 }
1198 
1199 
1200 static void copy_u2_with_conversion(u2* dest, u2* src, int length) {
1201   while (length-- > 0) {
1202     *dest++ = Bytes::get_Java_u2((u1*) (src++));


2575                                                       int runtime_visible_annotations_length,
2576                                                       u1* runtime_invisible_annotations,
2577                                                       int runtime_invisible_annotations_length, TRAPS) {
2578   typeArrayHandle annotations;
2579   if (runtime_visible_annotations != NULL ||
2580       runtime_invisible_annotations != NULL) {
2581     typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length +
2582                                                             runtime_invisible_annotations_length, CHECK_(annotations));
2583     annotations = typeArrayHandle(THREAD, anno);
2584     if (runtime_visible_annotations != NULL) {
2585       memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length);
2586     }
2587     if (runtime_invisible_annotations != NULL) {
2588       memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length);
2589     }
2590   }
2591   return annotations;
2592 }
2593 
2594 
2595 void ClassFileParser::java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
2596   constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS) {
2597   // This code is for compatibility with earlier jdk's that do not
2598   // have the "discovered" field in java.lang.ref.Reference.  For 1.5
2599   // the check for the "discovered" field should issue a warning if
2600   // the field is not found.  For 1.6 this code should be issue a
2601   // fatal error if the "discovered" field is not found.
2602   //
2603   // Increment fac.nonstatic_oop_count so that the start of the
2604   // next type of non-static oops leaves room for the fake oop.
2605   // Do not increment next_nonstatic_oop_offset so that the
2606   // fake oop is place after the java.lang.ref.Reference oop
2607   // fields.
2608   //
2609   // Check the fields in java.lang.ref.Reference for the "discovered"
2610   // field.  If it is not present, artifically create a field for it.
2611   // This allows this VM to run on early JDK where the field is not
2612   // present.
2613   int reference_sig_index = 0;
2614   int reference_name_index = 0;
2615   int reference_index = 0;
2616   int extra = java_lang_ref_Reference::number_of_fake_oop_fields;
2617   const int n = (*fields_ptr)()->length();
2618   for (int i = 0; i < n; i += instanceKlass::next_offset ) {
2619     int name_index =
2620     (*fields_ptr)()->ushort_at(i + instanceKlass::name_index_offset);
2621     int sig_index  =
2622       (*fields_ptr)()->ushort_at(i + instanceKlass::signature_index_offset);
2623     Symbol* f_name = cp->symbol_at(name_index);
2624     Symbol* f_sig  = cp->symbol_at(sig_index);
2625     if (f_sig == vmSymbols::reference_signature() && reference_index == 0) {
2626       // Save the index for reference signature for later use.
2627       // The fake discovered field does not entries in the
2628       // constant pool so the index for its signature cannot
2629       // be extracted from the constant pool.  It will need
2630       // later, however.  It's signature is vmSymbols::reference_signature()
2631       // so same an index for that signature.
2632       reference_sig_index = sig_index;
2633       reference_name_index = name_index;
2634       reference_index = i;
2635     }
2636     if (f_name == vmSymbols::reference_discovered_name() &&
2637       f_sig == vmSymbols::reference_signature()) {
2638       // The values below are fake but will force extra
2639       // non-static oop fields and a corresponding non-static
2640       // oop map block to be allocated.
2641       extra = 0;
2642       break;
2643     }
2644   }
2645   if (extra != 0) {
2646     fac_ptr->nonstatic_oop_count += extra;
2647     // Add the additional entry to "fields" so that the klass
2648     // contains the "discoverd" field and the field will be initialized
2649     // in instances of the object.
2650     int fields_with_fix_length = (*fields_ptr)()->length() +
2651       instanceKlass::next_offset;
2652     typeArrayOop ff = oopFactory::new_permanent_shortArray(
2653                                                 fields_with_fix_length, CHECK);
2654     typeArrayHandle fields_with_fix(THREAD, ff);
2655 
2656     // Take everything from the original but the length.
2657     for (int idx = 0; idx < (*fields_ptr)->length(); idx++) {
2658       fields_with_fix->ushort_at_put(idx, (*fields_ptr)->ushort_at(idx));
2659     }
2660 
2661     // Add the fake field at the end.
2662     int i = (*fields_ptr)->length();
2663     // There is no name index for the fake "discovered" field nor
2664     // signature but a signature is needed so that the field will
2665     // be properly initialized.  Use one found for
2666     // one of the other reference fields. Be sure the index for the
2667     // name is 0.  In fieldDescriptor::initialize() the index of the
2668     // name is checked.  That check is by passed for the last nonstatic
2669     // oop field in a java.lang.ref.Reference which is assumed to be
2670     // this artificial "discovered" field.  An assertion checks that
2671     // the name index is 0.
2672     assert(reference_index != 0, "Missing signature for reference");
2673 
2674     int j;
2675     for (j = 0; j < instanceKlass::next_offset; j++) {
2676       fields_with_fix->ushort_at_put(i + j,
2677         (*fields_ptr)->ushort_at(reference_index +j));
2678     }
2679     // Clear the public access flag and set the private access flag.
2680     short flags;
2681     flags =
2682       fields_with_fix->ushort_at(i + instanceKlass::access_flags_offset);
2683     assert(!(flags & JVM_RECOGNIZED_FIELD_MODIFIERS), "Unexpected access flags set");
2684     flags = flags & (~JVM_ACC_PUBLIC);
2685     flags = flags | JVM_ACC_PRIVATE;
2686     AccessFlags access_flags;
2687     access_flags.set_flags(flags);
2688     assert(!access_flags.is_public(), "Failed to clear public flag");
2689     assert(access_flags.is_private(), "Failed to set private flag");
2690     fields_with_fix->ushort_at_put(i + instanceKlass::access_flags_offset,
2691       flags);
2692 
2693     assert(fields_with_fix->ushort_at(i + instanceKlass::name_index_offset)
2694       == reference_name_index, "The fake reference name is incorrect");
2695     assert(fields_with_fix->ushort_at(i + instanceKlass::signature_index_offset)
2696       == reference_sig_index, "The fake reference signature is incorrect");
2697     // The type of the field is stored in the low_offset entry during
2698     // parsing.
2699     assert(fields_with_fix->ushort_at(i + instanceKlass::low_offset) ==
2700       NONSTATIC_OOP, "The fake reference type is incorrect");
2701 
2702     // "fields" is allocated in the permanent generation.  Disgard
2703     // it and let it be collected.
2704     (*fields_ptr) = fields_with_fix;
2705   }
2706   return;
2707 }
2708 
2709 
2710 void ClassFileParser::java_lang_Class_fix_pre(int* nonstatic_field_size,
2711                                               FieldAllocationCount *fac_ptr) {
2712   // Add fake fields for java.lang.Class instances
2713   //
2714   // This is not particularly nice. We should consider adding a
2715   // private transient object field at the Java level to
2716   // java.lang.Class. Alternatively we could add a subclass of
2717   // instanceKlass which provides an accessor and size computer for
2718   // this field, but that appears to be more code than this hack.
2719   //
2720   // NOTE that we wedge these in at the beginning rather than the
2721   // end of the object because the Class layout changed between JDK
2722   // 1.3 and JDK 1.4 with the new reflection implementation; some
2723   // nonstatic oop fields were added at the Java level. The offsets
2724   // of these fake fields can't change between these two JDK
2725   // versions because when the offsets are computed at bootstrap
2726   // time we don't know yet which version of the JDK we're running in.
2727 
2728   // The values below are fake but will force three non-static oop fields and
2729   // a corresponding non-static oop map block to be allocated.
2730   const int extra = java_lang_Class::number_of_fake_oop_fields;
2731   fac_ptr->nonstatic_oop_count += extra;
2732 
2733   // Reserve some leading space for fake ints
2734   *nonstatic_field_size += align_size_up(java_lang_Class::hc_number_of_fake_int_fields * BytesPerInt, heapOopSize) / heapOopSize;
2735 }
2736 
2737 
2738 void ClassFileParser::java_lang_Class_fix_post(int* next_nonstatic_oop_offset_ptr) {
2739   // Cause the extra fake fields in java.lang.Class to show up before
2740   // the Java fields for layout compatibility between 1.3 and 1.4
2741   // Incrementing next_nonstatic_oop_offset here advances the
2742   // location where the real java fields are placed.
2743   const int extra = java_lang_Class::number_of_fake_oop_fields;
2744   (*next_nonstatic_oop_offset_ptr) += (extra * heapOopSize);
2745 }
2746 
2747 
2748 // Force MethodHandle.vmentry to be an unmanaged pointer.
2749 // There is no way for a classfile to express this, so we must help it.
2750 void ClassFileParser::java_lang_invoke_MethodHandle_fix_pre(constantPoolHandle cp,
2751                                                     typeArrayHandle fields,
2752                                                     FieldAllocationCount *fac_ptr,
2753                                                     TRAPS) {
2754   // Add fake fields for java.lang.invoke.MethodHandle instances
2755   //
2756   // This is not particularly nice, but since there is no way to express
2757   // a native wordSize field in Java, we must do it at this level.
2758 
2759   if (!EnableInvokeDynamic)  return;
2760 
2761   int word_sig_index = 0;
2762   const int cp_size = cp->length();
2763   for (int index = 1; index < cp_size; index++) {
2764     if (cp->tag_at(index).is_utf8() &&
2765         cp->symbol_at(index) == vmSymbols::machine_word_signature()) {
2766       word_sig_index = index;
2767       break;
2768     }
2769   }
2770 
2771   if (word_sig_index == 0)
2772     THROW_MSG(vmSymbols::java_lang_VirtualMachineError(),
2773               "missing I or J signature (for vmentry) in java.lang.invoke.MethodHandle");
2774 
2775   // Find vmentry field and change the signature.
2776   bool found_vmentry = false;
2777   for (int i = 0; i < fields->length(); i += instanceKlass::next_offset) {
2778     int name_index = fields->ushort_at(i + instanceKlass::name_index_offset);
2779     int sig_index  = fields->ushort_at(i + instanceKlass::signature_index_offset);
2780     int acc_flags  = fields->ushort_at(i + instanceKlass::access_flags_offset);
2781     Symbol* f_name = cp->symbol_at(name_index);
2782     Symbol* f_sig  = cp->symbol_at(sig_index);
2783     if (f_name == vmSymbols::vmentry_name() && (acc_flags & JVM_ACC_STATIC) == 0) {
2784       if (f_sig == vmSymbols::machine_word_signature()) {
2785         // If the signature of vmentry is already changed, we're done.
2786         found_vmentry = true;
2787         break;
2788       }
2789       else if (f_sig == vmSymbols::byte_signature()) {
2790         // Adjust the field type from byte to an unmanaged pointer.
2791         assert(fac_ptr->nonstatic_byte_count > 0, "");
2792         fac_ptr->nonstatic_byte_count -= 1;
2793 
2794         fields->ushort_at_put(i + instanceKlass::signature_index_offset, word_sig_index);
2795         assert(wordSize == longSize || wordSize == jintSize, "ILP32 or LP64");
2796         if (wordSize == longSize)  fac_ptr->nonstatic_double_count += 1;
2797         else                       fac_ptr->nonstatic_word_count   += 1;
2798 
2799         FieldAllocationType atype = (FieldAllocationType) fields->ushort_at(i + instanceKlass::low_offset);
2800         assert(atype == NONSTATIC_BYTE, "");
2801         FieldAllocationType new_atype = (wordSize == longSize) ? NONSTATIC_DOUBLE : NONSTATIC_WORD;
2802         fields->ushort_at_put(i + instanceKlass::low_offset, new_atype);
2803 
2804         found_vmentry = true;
2805         break;
2806       }
2807     }
2808   }
2809 
2810   if (!found_vmentry)
2811     THROW_MSG(vmSymbols::java_lang_VirtualMachineError(),
2812               "missing vmentry byte field in java.lang.invoke.MethodHandle");
2813 }
2814 
2815 
2816 instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name,
2817                                                     Handle class_loader,
2818                                                     Handle protection_domain,
2819                                                     KlassHandle host_klass,
2820                                                     GrowableArray<Handle>* cp_patches,
2821                                                     TempNewSymbol& parsed_name,
2822                                                     bool verify,
2823                                                     TRAPS) {
2824   // So that JVMTI can cache class file in the state before retransformable agents
2825   // have modified it
2826   unsigned char *cached_class_file_bytes = NULL;
2827   jint cached_class_file_length;
2828 
2829   ClassFileStream* cfs = stream();
2830   // Timing
2831   assert(THREAD->is_Java_thread(), "must be a JavaThread");
2832   JavaThread* jt = (JavaThread*) THREAD;
2833 
2834   PerfClassTraceTime ctimer(ClassLoader::perf_class_parse_time(),
2835                             ClassLoader::perf_class_parse_selftime(),


3008         if (_need_verify)
3009           is_array = super_klass->oop_is_array();
3010       } else if (_need_verify) {
3011         is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
3012       }
3013       if (_need_verify) {
3014         guarantee_property(!is_array,
3015                           "Bad superclass name in class file %s", CHECK_(nullHandle));
3016       }
3017     }
3018 
3019     // Interfaces
3020     u2 itfs_len = cfs->get_u2_fast();
3021     objArrayHandle local_interfaces;
3022     if (itfs_len == 0) {
3023       local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
3024     } else {
3025       local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, _class_name, CHECK_(nullHandle));
3026     }
3027 

3028     // Fields (offsets are filled in later)
3029     struct FieldAllocationCount fac = {0,0,0,0,0,0,0,0,0,0};
3030     objArrayHandle fields_annotations;
3031     typeArrayHandle fields = parse_fields(cp, access_flags.is_interface(), &fac, &fields_annotations, CHECK_(nullHandle));


3032     // Methods
3033     bool has_final_method = false;
3034     AccessFlags promoted_flags;
3035     promoted_flags.set_flags(0);
3036     // These need to be oop pointers because they are allocated lazily
3037     // inside parse_methods inside a nested HandleMark
3038     objArrayOop methods_annotations_oop = NULL;
3039     objArrayOop methods_parameter_annotations_oop = NULL;
3040     objArrayOop methods_default_annotations_oop = NULL;
3041     objArrayHandle methods = parse_methods(cp, access_flags.is_interface(),
3042                                            &promoted_flags,
3043                                            &has_final_method,
3044                                            &methods_annotations_oop,
3045                                            &methods_parameter_annotations_oop,
3046                                            &methods_default_annotations_oop,
3047                                            CHECK_(nullHandle));
3048 
3049     objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
3050     objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
3051     objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);


3129     int static_field_size = 0;
3130     int next_static_oop_offset;
3131     int next_static_double_offset;
3132     int next_static_word_offset;
3133     int next_static_short_offset;
3134     int next_static_byte_offset;
3135     int next_static_type_offset;
3136     int next_nonstatic_oop_offset;
3137     int next_nonstatic_double_offset;
3138     int next_nonstatic_word_offset;
3139     int next_nonstatic_short_offset;
3140     int next_nonstatic_byte_offset;
3141     int next_nonstatic_type_offset;
3142     int first_nonstatic_oop_offset;
3143     int first_nonstatic_field_offset;
3144     int next_nonstatic_field_offset;
3145 
3146     // Calculate the starting byte offsets
3147     next_static_oop_offset      = instanceMirrorKlass::offset_of_static_fields();
3148     next_static_double_offset   = next_static_oop_offset +
3149                                   (fac.static_oop_count * heapOopSize);
3150     if ( fac.static_double_count &&
3151          (Universe::field_type_should_be_aligned(T_DOUBLE) ||
3152           Universe::field_type_should_be_aligned(T_LONG)) ) {
3153       next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
3154     }
3155 
3156     next_static_word_offset     = next_static_double_offset +
3157                                   (fac.static_double_count * BytesPerLong);
3158     next_static_short_offset    = next_static_word_offset +
3159                                   (fac.static_word_count * BytesPerInt);
3160     next_static_byte_offset     = next_static_short_offset +
3161                                   (fac.static_short_count * BytesPerShort);
3162     next_static_type_offset     = align_size_up((next_static_byte_offset +
3163                                   fac.static_byte_count ), wordSize );
3164     static_field_size           = (next_static_type_offset -
3165                                   next_static_oop_offset) / wordSize;
3166 
3167     // Add fake fields for java.lang.Class instances (also see below)
3168     if (class_name == vmSymbols::java_lang_Class() && class_loader.is_null()) {
3169       java_lang_Class_fix_pre(&nonstatic_field_size, &fac);
3170     }
3171 
3172     first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
3173                                    nonstatic_field_size * heapOopSize;
3174     next_nonstatic_field_offset = first_nonstatic_field_offset;
3175 
3176     // adjust the vmentry field declaration in java.lang.invoke.MethodHandle
3177     if (EnableInvokeDynamic && class_name == vmSymbols::java_lang_invoke_MethodHandle() && class_loader.is_null()) {
3178       java_lang_invoke_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3179     }

3180 
3181     // Add a fake "discovered" field if it is not present
3182     // for compatibility with earlier jdk's.
3183     if (class_name == vmSymbols::java_lang_ref_Reference()
3184       && class_loader.is_null()) {
3185       java_lang_ref_Reference_fix_pre(&fields, cp, &fac, CHECK_(nullHandle));
3186     }
3187     // end of "discovered" field compactibility fix
3188 
3189     unsigned int nonstatic_double_count = fac.nonstatic_double_count;
3190     unsigned int nonstatic_word_count   = fac.nonstatic_word_count;
3191     unsigned int nonstatic_short_count  = fac.nonstatic_short_count;
3192     unsigned int nonstatic_byte_count   = fac.nonstatic_byte_count;
3193     unsigned int nonstatic_oop_count    = fac.nonstatic_oop_count;
3194 
3195     bool super_has_nonstatic_fields =
3196             (super_klass() != NULL && super_klass->has_nonstatic_fields());
3197     bool has_nonstatic_fields  =  super_has_nonstatic_fields ||
3198             ((nonstatic_double_count + nonstatic_word_count +
3199               nonstatic_short_count + nonstatic_byte_count +
3200               nonstatic_oop_count) != 0);
3201 
3202 
3203     // Prepare list of oops for oop map generation.
3204     int* nonstatic_oop_offsets;
3205     unsigned int* nonstatic_oop_counts;
3206     unsigned int nonstatic_oop_map_count = 0;
3207 
3208     nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3209               THREAD, int, nonstatic_oop_count + 1);
3210     nonstatic_oop_counts  = NEW_RESOURCE_ARRAY_IN_THREAD(
3211               THREAD, unsigned int, nonstatic_oop_count + 1);
3212 
3213     // Add fake fields for java.lang.Class instances (also see above).
3214     // FieldsAllocationStyle and CompactFields values will be reset to default.
3215     if(class_name == vmSymbols::java_lang_Class() && class_loader.is_null()) {
3216       java_lang_Class_fix_post(&next_nonstatic_field_offset);
3217       nonstatic_oop_offsets[0] = first_nonstatic_field_offset;
3218       const uint fake_oop_count = (next_nonstatic_field_offset -
3219                                    first_nonstatic_field_offset) / heapOopSize;
3220       nonstatic_oop_counts[0] = fake_oop_count;
3221       nonstatic_oop_map_count = 1;
3222       nonstatic_oop_count -= fake_oop_count;
3223       first_nonstatic_oop_offset = first_nonstatic_field_offset;
3224     } else {
3225       first_nonstatic_oop_offset = 0; // will be set for first oop field
3226     }
3227 
3228 #ifndef PRODUCT
3229     if( PrintCompactFieldsSavings ) {
3230       next_nonstatic_double_offset = next_nonstatic_field_offset +
3231                                      (nonstatic_oop_count * heapOopSize);
3232       if ( nonstatic_double_count > 0 ) {
3233         next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
3234       }
3235       next_nonstatic_word_offset  = next_nonstatic_double_offset +
3236                                     (nonstatic_double_count * BytesPerLong);
3237       next_nonstatic_short_offset = next_nonstatic_word_offset +
3238                                     (nonstatic_word_count * BytesPerInt);
3239       next_nonstatic_byte_offset  = next_nonstatic_short_offset +
3240                                     (nonstatic_short_count * BytesPerShort);
3241       next_nonstatic_type_offset  = align_size_up((next_nonstatic_byte_offset +
3242                                     nonstatic_byte_count ), heapOopSize );
3243       orig_nonstatic_field_size   = nonstatic_field_size +
3244       ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
3245     }
3246 #endif


3361     next_nonstatic_byte_offset  = next_nonstatic_short_offset +
3362                                   (nonstatic_short_count * BytesPerShort);
3363 
3364     int notaligned_offset;
3365     if( allocation_style == 0 ) {
3366       notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3367     } else { // allocation_style == 1
3368       next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3369       if( nonstatic_oop_count > 0 ) {
3370         next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
3371       }
3372       notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
3373     }
3374     next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
3375     nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
3376                                    - first_nonstatic_field_offset)/heapOopSize);
3377 
3378     // Iterate over fields again and compute correct offsets.
3379     // The field allocation type was temporarily stored in the offset slot.
3380     // oop fields are located before non-oop fields (static and non-static).
3381     int len = fields->length();
3382     for (int i = 0; i < len; i += instanceKlass::next_offset) {
3383       int real_offset;
3384       FieldAllocationType atype = (FieldAllocationType) fields->ushort_at(i + instanceKlass::low_offset);
3385       switch (atype) {
3386         case STATIC_OOP:
3387           real_offset = next_static_oop_offset;
3388           next_static_oop_offset += heapOopSize;
3389           break;
3390         case STATIC_BYTE:
3391           real_offset = next_static_byte_offset;
3392           next_static_byte_offset += 1;
3393           break;
3394         case STATIC_SHORT:
3395           real_offset = next_static_short_offset;
3396           next_static_short_offset += BytesPerShort;
3397           break;
3398         case STATIC_WORD:
3399           real_offset = next_static_word_offset;
3400           next_static_word_offset += BytesPerInt;
3401           break;
3402         case STATIC_ALIGNED_DOUBLE:
3403         case STATIC_DOUBLE:
3404           real_offset = next_static_double_offset;
3405           next_static_double_offset += BytesPerLong;
3406           break;
3407         case NONSTATIC_OOP:
3408           if( nonstatic_oop_space_count > 0 ) {
3409             real_offset = nonstatic_oop_space_offset;
3410             nonstatic_oop_space_offset += heapOopSize;
3411             nonstatic_oop_space_count  -= 1;
3412           } else {
3413             real_offset = next_nonstatic_oop_offset;
3414             next_nonstatic_oop_offset += heapOopSize;
3415           }
3416           // Update oop maps
3417           if( nonstatic_oop_map_count > 0 &&
3418               nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3419               real_offset -
3420               int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
3421               heapOopSize ) {
3422             // Extend current oop map


3444         case NONSTATIC_SHORT:
3445           if( nonstatic_short_space_count > 0 ) {
3446             real_offset = nonstatic_short_space_offset;
3447             nonstatic_short_space_offset += BytesPerShort;
3448             nonstatic_short_space_count  -= 1;
3449           } else {
3450             real_offset = next_nonstatic_short_offset;
3451             next_nonstatic_short_offset += BytesPerShort;
3452           }
3453           break;
3454         case NONSTATIC_WORD:
3455           if( nonstatic_word_space_count > 0 ) {
3456             real_offset = nonstatic_word_space_offset;
3457             nonstatic_word_space_offset += BytesPerInt;
3458             nonstatic_word_space_count  -= 1;
3459           } else {
3460             real_offset = next_nonstatic_word_offset;
3461             next_nonstatic_word_offset += BytesPerInt;
3462           }
3463           break;
3464         case NONSTATIC_ALIGNED_DOUBLE:
3465         case NONSTATIC_DOUBLE:
3466           real_offset = next_nonstatic_double_offset;
3467           next_nonstatic_double_offset += BytesPerLong;
3468           break;
3469         default:
3470           ShouldNotReachHere();
3471       }
3472       fields->short_at_put(i + instanceKlass::low_offset,  extract_low_short_from_int(real_offset));
3473       fields->short_at_put(i + instanceKlass::high_offset, extract_high_short_from_int(real_offset));
3474     }
3475 
3476     // Size of instances
3477     int instance_size;
3478 
3479     next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3480     instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3481 
3482     assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3483 
3484     // Number of non-static oop map blocks allocated at end of klass.
3485     const unsigned int total_oop_map_count =
3486       compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3487                             first_nonstatic_oop_offset);
3488 
3489     // Compute reference type
3490     ReferenceType rt;
3491     if (super_klass() == NULL) {
3492       rt = REF_NONE;
3493     } else {


3500                                                 total_oop_map_count,
3501                                                 rt, CHECK_(nullHandle));
3502     instanceKlassHandle this_klass (THREAD, ik);
3503 
3504     assert(this_klass->static_field_size() == static_field_size, "sanity");
3505     assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
3506            "sanity");
3507 
3508     // Fill in information already parsed
3509     this_klass->set_access_flags(access_flags);
3510     this_klass->set_should_verify_class(verify);
3511     jint lh = Klass::instance_layout_helper(instance_size, false);
3512     this_klass->set_layout_helper(lh);
3513     assert(this_klass->oop_is_instance(), "layout is correct");
3514     assert(this_klass->size_helper() == instance_size, "correct size_helper");
3515     // Not yet: supers are done below to support the new subtype-checking fields
3516     //this_klass->set_super(super_klass());
3517     this_klass->set_class_loader(class_loader());
3518     this_klass->set_nonstatic_field_size(nonstatic_field_size);
3519     this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3520     this_klass->set_static_oop_field_count(fac.static_oop_count);
3521     cp->set_pool_holder(this_klass());
3522     error_handler.set_in_error(false);   // turn off error handler for cp
3523     this_klass->set_constants(cp());
3524     this_klass->set_local_interfaces(local_interfaces());
3525     this_klass->set_fields(fields());
3526     this_klass->set_methods(methods());
3527     if (has_final_method) {
3528       this_klass->set_has_final_method();
3529     }
3530     this_klass->set_method_ordering(method_ordering());
3531     // The instanceKlass::_methods_jmethod_ids cache and the
3532     // instanceKlass::_methods_cached_itable_indices cache are
3533     // both managed on the assumption that the initial cache
3534     // size is equal to the number of methods in the class. If
3535     // that changes, then instanceKlass::idnum_can_increment()
3536     // has to be changed accordingly.
3537     this_klass->set_initial_method_idnum(methods->length());
3538     this_klass->set_name(cp->klass_name_at(this_class_index));
3539     if (LinkWellKnownClasses || is_anonymous())  // I am well known to myself
3540       cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3541     this_klass->set_protection_domain(protection_domain());
3542     this_klass->set_fields_annotations(fields_annotations());
3543     this_klass->set_methods_annotations(methods_annotations());
3544     this_klass->set_methods_parameter_annotations(methods_parameter_annotations());
3545     this_klass->set_methods_default_annotations(methods_default_annotations());




  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classFileParser.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/symbolTable.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/verificationType.hpp"
  32 #include "classfile/verifier.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "memory/allocation.hpp"
  35 #include "memory/gcLocker.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/universe.inline.hpp"
  38 #include "oops/constantPoolOop.hpp"
  39 #include "oops/fieldStreams.hpp"
  40 #include "oops/instanceKlass.hpp"
  41 #include "oops/instanceMirrorKlass.hpp"
  42 #include "oops/klass.inline.hpp"
  43 #include "oops/klassOop.hpp"
  44 #include "oops/klassVtable.hpp"
  45 #include "oops/methodOop.hpp"
  46 #include "oops/symbol.hpp"
  47 #include "prims/jvmtiExport.hpp"
  48 #include "runtime/javaCalls.hpp"
  49 #include "runtime/perfData.hpp"
  50 #include "runtime/reflection.hpp"
  51 #include "runtime/signature.hpp"
  52 #include "runtime/timer.hpp"
  53 #include "services/classLoadingService.hpp"
  54 #include "services/threadService.hpp"
  55 
  56 // We generally try to create the oops directly when parsing, rather than
  57 // allocating temporary data structures and copying the bytes twice. A
  58 // temporary area is only needed when parsing utf8 entries in the constant
  59 // pool and when parsing line number tables.


 975 
 976   *constantvalue_index_addr = constantvalue_index;
 977   *is_synthetic_addr = is_synthetic;
 978   *generic_signature_index_addr = generic_signature_index;
 979   *field_annotations = assemble_annotations(runtime_visible_annotations,
 980                                             runtime_visible_annotations_length,
 981                                             runtime_invisible_annotations,
 982                                             runtime_invisible_annotations_length,
 983                                             CHECK);
 984   return;
 985 }
 986 
 987 
 988 // Field allocation types. Used for computing field offsets.
 989 
 990 enum FieldAllocationType {
 991   STATIC_OOP,           // Oops
 992   STATIC_BYTE,          // Boolean, Byte, char
 993   STATIC_SHORT,         // shorts
 994   STATIC_WORD,          // ints
 995   STATIC_DOUBLE,        // aligned long or double

 996   NONSTATIC_OOP,
 997   NONSTATIC_BYTE,
 998   NONSTATIC_SHORT,
 999   NONSTATIC_WORD,
1000   NONSTATIC_DOUBLE,
1001   MAX_FIELD_ALLOCATION_TYPE,
1002   BAD_ALLOCATION_TYPE = -1
1003 };
1004 
1005 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1006   BAD_ALLOCATION_TYPE, // 0
1007   BAD_ALLOCATION_TYPE, // 1
1008   BAD_ALLOCATION_TYPE, // 2
1009   BAD_ALLOCATION_TYPE, // 3
1010   NONSTATIC_BYTE ,     // T_BOOLEAN  =  4,
1011   NONSTATIC_SHORT,     // T_CHAR     =  5,
1012   NONSTATIC_WORD,      // T_FLOAT    =  6,
1013   NONSTATIC_DOUBLE,    // T_DOUBLE   =  7,
1014   NONSTATIC_BYTE,      // T_BYTE     =  8,
1015   NONSTATIC_SHORT,     // T_SHORT    =  9,
1016   NONSTATIC_WORD,      // T_INT      = 10,
1017   NONSTATIC_DOUBLE,    // T_LONG     = 11,
1018   NONSTATIC_OOP,       // T_OBJECT   = 12,
1019   NONSTATIC_OOP,       // T_ARRAY    = 13,
1020   BAD_ALLOCATION_TYPE, // T_VOID     = 14,
1021   BAD_ALLOCATION_TYPE, // T_ADDRESS  = 15,
1022   BAD_ALLOCATION_TYPE, // T_NARROWOOP= 16,
1023   BAD_ALLOCATION_TYPE, // T_CONFLICT = 17,
1024   BAD_ALLOCATION_TYPE, // 0
1025   BAD_ALLOCATION_TYPE, // 1
1026   BAD_ALLOCATION_TYPE, // 2
1027   BAD_ALLOCATION_TYPE, // 3
1028   STATIC_BYTE ,        // T_BOOLEAN  =  4,
1029   STATIC_SHORT,        // T_CHAR     =  5,
1030   STATIC_WORD,          // T_FLOAT    =  6,
1031   STATIC_DOUBLE,       // T_DOUBLE   =  7,
1032   STATIC_BYTE,         // T_BYTE     =  8,
1033   STATIC_SHORT,        // T_SHORT    =  9,
1034   STATIC_WORD,         // T_INT      = 10,
1035   STATIC_DOUBLE,       // T_LONG     = 11,
1036   STATIC_OOP,          // T_OBJECT   = 12,
1037   STATIC_OOP,          // T_ARRAY    = 13,
1038   BAD_ALLOCATION_TYPE, // T_VOID     = 14,
1039   BAD_ALLOCATION_TYPE, // T_ADDRESS  = 15,
1040   BAD_ALLOCATION_TYPE, // T_NARROWOOP= 16,
1041   BAD_ALLOCATION_TYPE, // T_CONFLICT = 17,
1042 };
1043 
1044 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {
1045   assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1046   FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1047   assert(result != BAD_ALLOCATION_TYPE, "bad type");
1048   return result;
1049 }
1050 
1051 class FieldAllocationCount: public ResourceObj {
1052  public:
1053   unsigned int count[MAX_FIELD_ALLOCATION_TYPE];
1054 
1055   FieldAllocationCount() {
1056     for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1057       count[i] = 0;
1058     }
1059   }
1060   
1061   FieldAllocationType update(bool is_static, BasicType type) {
1062     FieldAllocationType atype = basic_type_to_atype(is_static, type);
1063     count[atype]++;
1064     return atype;
1065   }
1066 };
1067 
1068 
1069 typeArrayHandle ClassFileParser::parse_fields(Symbol* class_name,
1070                                               constantPoolHandle cp, bool is_interface,
1071                                               FieldAllocationCount *fac,
1072                                               objArrayHandle* fields_annotations,
1073                                               int* java_fields_count_ptr, TRAPS) {
1074   ClassFileStream* cfs = stream();
1075   typeArrayHandle nullHandle;
1076   cfs->guarantee_more(2, CHECK_(nullHandle));  // length
1077   u2 length = cfs->get_u2_fast();
1078   *java_fields_count_ptr = length;
1079 
1080   int num_injected = 0;
1081   InjectedField* injected = JavaClasses::get_injected(class_name, &num_injected);
1082 
1083   // Tuples of shorts [access, name index, sig index, initial value index, byte offset, generic signature index]
1084   typeArrayOop new_fields = oopFactory::new_permanent_shortArray((length + num_injected) * FieldInfo::field_slots, CHECK_(nullHandle));
1085   typeArrayHandle fields(THREAD, new_fields);
1086 

1087   typeArrayHandle field_annotations;
1088   for (int n = 0; n < length; n++) {
1089     cfs->guarantee_more(8, CHECK_(nullHandle));  // access_flags, name_index, descriptor_index, attributes_count
1090 
1091     AccessFlags access_flags;
1092     jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1093     verify_legal_field_modifiers(flags, is_interface, CHECK_(nullHandle));
1094     access_flags.set_flags(flags);
1095 
1096     u2 name_index = cfs->get_u2_fast();
1097     int cp_size = cp->length();
1098     check_property(
1099       valid_cp_range(name_index, cp_size) && cp->tag_at(name_index).is_utf8(),
1100       "Invalid constant pool index %u for field name in class file %s",
1101       name_index, CHECK_(nullHandle));
1102     Symbol*  name = cp->symbol_at(name_index);
1103     verify_legal_field_name(name, CHECK_(nullHandle));
1104 
1105     u2 signature_index = cfs->get_u2_fast();
1106     check_property(


1117     bool is_static = access_flags.is_static();
1118 
1119     u2 attributes_count = cfs->get_u2_fast();
1120     if (attributes_count > 0) {
1121       parse_field_attributes(cp, attributes_count, is_static, signature_index,
1122                              &constantvalue_index, &is_synthetic,
1123                              &generic_signature_index, &field_annotations,
1124                              CHECK_(nullHandle));
1125       if (field_annotations.not_null()) {
1126         if (fields_annotations->is_null()) {
1127           objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1128           *fields_annotations = objArrayHandle(THREAD, md);
1129         }
1130         (*fields_annotations)->obj_at_put(n, field_annotations());
1131       }
1132       if (is_synthetic) {
1133         access_flags.set_is_synthetic();
1134       }
1135     }
1136 
1137     FieldInfo* field = FieldInfo::from_field_array(fields(), n);
1138     field->initialize(access_flags.as_short(),
1139                       name_index,
1140                       signature_index,
1141                       constantvalue_index,
1142                       generic_signature_index,
1143                       0);
1144 

1145     BasicType type = cp->basic_type_for_signature_at(signature_index);
1146     
1147     // Remember how many oops we encountered and compute allocation type
1148     FieldAllocationType atype = fac->update(is_static, type);
1149 
1150     // The correct offset is computed later (all oop fields will be located together)
1151     // We temporarily store the allocation type in the offset field
1152     field->set_offset(atype);







1153   }
1154 
1155   if (num_injected != 0) {
1156     int index = length;
1157     for (int n = 0; n < num_injected; n++) {
1158       // Check for duplicates
1159       if (injected[n].may_be_java) {
1160         Symbol* name      = injected[n].name();
1161         Symbol* signature = injected[n].signature();
1162         bool duplicate = false;
1163         for (int i = 0; i < length; i++) {
1164           FieldInfo* f = FieldInfo::from_field_array(fields(), i);
1165           if (name      == cp->symbol_at(f->name_index()) &&
1166               signature == cp->symbol_at(f->signature_index())) {
1167             // Symbol is desclared in Java so skip this one
1168             duplicate = true;
1169             break;



















1170           }













1171         }
1172         if (duplicate) {
1173           // These will be removed from the field array at the end
1174           continue;


















1175         }
1176       }
1177 
1178       // Injected field
1179       FieldInfo* field = FieldInfo::from_field_array(fields(), index);
1180       field->initialize(JVM_ACC_FIELD_INTERNAL,
1181                         injected[n].name_index,
1182                         injected[n].signature_index,
1183                         0,
1184                         0,
1185                         0);
1186 
1187       BasicType type = FieldType::basic_type(injected[n].signature());
1188       
1189       // Remember how many oops we encountered and compute allocation type
1190       FieldAllocationType atype = fac->update(false, type);
1191       
1192       // The correct offset is computed later (all oop fields will be located together)
1193       // We temporarily store the allocation type in the offset field
1194       field->set_offset(atype);
1195       index++;

1196     }
1197 
1198     if (index < length + num_injected) {
1199       // sometimes injected fields already exist in the Java source so
1200       // the fields array could be too long.  In that case trim the
1201       // fields array.
1202       new_fields = oopFactory::new_permanent_shortArray(index * FieldInfo::field_slots, CHECK_(nullHandle));
1203       for (int i = 0; i < index * FieldInfo::field_slots; i++) {
1204         new_fields->short_at_put(i, fields->short_at(i));
1205       }
1206       fields = new_fields;
1207     }
1208   }
1209 
1210   if (_need_verify && length > 1) {
1211     // Check duplicated fields
1212     ResourceMark rm(THREAD);
1213     NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
1214       THREAD, NameSigHash*, HASH_ROW_SIZE);
1215     initialize_hashtable(names_and_sigs);
1216     bool dup = false;
1217     {
1218       debug_only(No_Safepoint_Verifier nsv;)
1219       for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
1220         Symbol* name = fs.name();
1221         Symbol* sig = fs.signature();


1222         // If no duplicates, add name/signature in hashtable names_and_sigs.
1223         if (!put_after_lookup(name, sig, names_and_sigs)) {
1224           dup = true;
1225           break;
1226         }
1227       }
1228     }
1229     if (dup) {
1230       classfile_parse_error("Duplicate field name&signature in class file %s",
1231                             CHECK_(nullHandle));
1232     }
1233   }
1234 
1235   return fields;
1236 }
1237 
1238 
1239 static void copy_u2_with_conversion(u2* dest, u2* src, int length) {
1240   while (length-- > 0) {
1241     *dest++ = Bytes::get_Java_u2((u1*) (src++));


2614                                                       int runtime_visible_annotations_length,
2615                                                       u1* runtime_invisible_annotations,
2616                                                       int runtime_invisible_annotations_length, TRAPS) {
2617   typeArrayHandle annotations;
2618   if (runtime_visible_annotations != NULL ||
2619       runtime_invisible_annotations != NULL) {
2620     typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length +
2621                                                             runtime_invisible_annotations_length, CHECK_(annotations));
2622     annotations = typeArrayHandle(THREAD, anno);
2623     if (runtime_visible_annotations != NULL) {
2624       memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length);
2625     }
2626     if (runtime_invisible_annotations != NULL) {
2627       memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length);
2628     }
2629   }
2630   return annotations;
2631 }
2632 
2633 





























































































































































































































2634 instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name,
2635                                                     Handle class_loader,
2636                                                     Handle protection_domain,
2637                                                     KlassHandle host_klass,
2638                                                     GrowableArray<Handle>* cp_patches,
2639                                                     TempNewSymbol& parsed_name,
2640                                                     bool verify,
2641                                                     TRAPS) {
2642   // So that JVMTI can cache class file in the state before retransformable agents
2643   // have modified it
2644   unsigned char *cached_class_file_bytes = NULL;
2645   jint cached_class_file_length;
2646 
2647   ClassFileStream* cfs = stream();
2648   // Timing
2649   assert(THREAD->is_Java_thread(), "must be a JavaThread");
2650   JavaThread* jt = (JavaThread*) THREAD;
2651 
2652   PerfClassTraceTime ctimer(ClassLoader::perf_class_parse_time(),
2653                             ClassLoader::perf_class_parse_selftime(),


2826         if (_need_verify)
2827           is_array = super_klass->oop_is_array();
2828       } else if (_need_verify) {
2829         is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
2830       }
2831       if (_need_verify) {
2832         guarantee_property(!is_array,
2833                           "Bad superclass name in class file %s", CHECK_(nullHandle));
2834       }
2835     }
2836 
2837     // Interfaces
2838     u2 itfs_len = cfs->get_u2_fast();
2839     objArrayHandle local_interfaces;
2840     if (itfs_len == 0) {
2841       local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
2842     } else {
2843       local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, _class_name, CHECK_(nullHandle));
2844     }
2845 
2846     int java_fields_count = 0;
2847     // Fields (offsets are filled in later)
2848     FieldAllocationCount fac;
2849     objArrayHandle fields_annotations;
2850     typeArrayHandle fields = parse_fields(class_name, cp, access_flags.is_interface(), &fac, &fields_annotations,
2851                                           &java_fields_count,
2852                                           CHECK_(nullHandle));
2853     // Methods
2854     bool has_final_method = false;
2855     AccessFlags promoted_flags;
2856     promoted_flags.set_flags(0);
2857     // These need to be oop pointers because they are allocated lazily
2858     // inside parse_methods inside a nested HandleMark
2859     objArrayOop methods_annotations_oop = NULL;
2860     objArrayOop methods_parameter_annotations_oop = NULL;
2861     objArrayOop methods_default_annotations_oop = NULL;
2862     objArrayHandle methods = parse_methods(cp, access_flags.is_interface(),
2863                                            &promoted_flags,
2864                                            &has_final_method,
2865                                            &methods_annotations_oop,
2866                                            &methods_parameter_annotations_oop,
2867                                            &methods_default_annotations_oop,
2868                                            CHECK_(nullHandle));
2869 
2870     objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
2871     objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
2872     objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);


2950     int static_field_size = 0;
2951     int next_static_oop_offset;
2952     int next_static_double_offset;
2953     int next_static_word_offset;
2954     int next_static_short_offset;
2955     int next_static_byte_offset;
2956     int next_static_type_offset;
2957     int next_nonstatic_oop_offset;
2958     int next_nonstatic_double_offset;
2959     int next_nonstatic_word_offset;
2960     int next_nonstatic_short_offset;
2961     int next_nonstatic_byte_offset;
2962     int next_nonstatic_type_offset;
2963     int first_nonstatic_oop_offset;
2964     int first_nonstatic_field_offset;
2965     int next_nonstatic_field_offset;
2966 
2967     // Calculate the starting byte offsets
2968     next_static_oop_offset      = instanceMirrorKlass::offset_of_static_fields();
2969     next_static_double_offset   = next_static_oop_offset +
2970                                   (fac.count[STATIC_OOP] * heapOopSize);
2971     if ( fac.count[STATIC_DOUBLE] &&
2972          (Universe::field_type_should_be_aligned(T_DOUBLE) ||
2973           Universe::field_type_should_be_aligned(T_LONG)) ) {
2974       next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
2975     }
2976 
2977     next_static_word_offset     = next_static_double_offset +
2978                                   (fac.count[STATIC_DOUBLE] * BytesPerLong);
2979     next_static_short_offset    = next_static_word_offset +
2980                                   (fac.count[STATIC_WORD] * BytesPerInt);
2981     next_static_byte_offset     = next_static_short_offset +
2982                                   (fac.count[STATIC_SHORT] * BytesPerShort);
2983     next_static_type_offset     = align_size_up((next_static_byte_offset +
2984                                   fac.count[STATIC_BYTE] ), wordSize );
2985     static_field_size           = (next_static_type_offset -
2986                                   next_static_oop_offset) / wordSize;
2987 





2988     first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
2989                                    nonstatic_field_size * heapOopSize;
2990     next_nonstatic_field_offset = first_nonstatic_field_offset;
2991 
2992     unsigned int nonstatic_double_count = fac.count[NONSTATIC_DOUBLE];
2993     unsigned int nonstatic_word_count   = fac.count[NONSTATIC_WORD];
2994     unsigned int nonstatic_short_count  = fac.count[NONSTATIC_SHORT];
2995     unsigned int nonstatic_byte_count   = fac.count[NONSTATIC_BYTE];
2996     unsigned int nonstatic_oop_count    = fac.count[NONSTATIC_OOP];
2997 














2998     bool super_has_nonstatic_fields =
2999             (super_klass() != NULL && super_klass->has_nonstatic_fields());
3000     bool has_nonstatic_fields  =  super_has_nonstatic_fields ||
3001             ((nonstatic_double_count + nonstatic_word_count +
3002               nonstatic_short_count + nonstatic_byte_count +
3003               nonstatic_oop_count) != 0);
3004 
3005 
3006     // Prepare list of oops for oop map generation.
3007     int* nonstatic_oop_offsets;
3008     unsigned int* nonstatic_oop_counts;
3009     unsigned int nonstatic_oop_map_count = 0;
3010 
3011     nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3012               THREAD, int, nonstatic_oop_count + 1);
3013     nonstatic_oop_counts  = NEW_RESOURCE_ARRAY_IN_THREAD(
3014               THREAD, unsigned int, nonstatic_oop_count + 1);
3015 












3016     first_nonstatic_oop_offset = 0; // will be set for first oop field

3017 
3018 #ifndef PRODUCT
3019     if( PrintCompactFieldsSavings ) {
3020       next_nonstatic_double_offset = next_nonstatic_field_offset +
3021                                      (nonstatic_oop_count * heapOopSize);
3022       if ( nonstatic_double_count > 0 ) {
3023         next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
3024       }
3025       next_nonstatic_word_offset  = next_nonstatic_double_offset +
3026                                     (nonstatic_double_count * BytesPerLong);
3027       next_nonstatic_short_offset = next_nonstatic_word_offset +
3028                                     (nonstatic_word_count * BytesPerInt);
3029       next_nonstatic_byte_offset  = next_nonstatic_short_offset +
3030                                     (nonstatic_short_count * BytesPerShort);
3031       next_nonstatic_type_offset  = align_size_up((next_nonstatic_byte_offset +
3032                                     nonstatic_byte_count ), heapOopSize );
3033       orig_nonstatic_field_size   = nonstatic_field_size +
3034       ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
3035     }
3036 #endif


3151     next_nonstatic_byte_offset  = next_nonstatic_short_offset +
3152                                   (nonstatic_short_count * BytesPerShort);
3153 
3154     int notaligned_offset;
3155     if( allocation_style == 0 ) {
3156       notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3157     } else { // allocation_style == 1
3158       next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3159       if( nonstatic_oop_count > 0 ) {
3160         next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
3161       }
3162       notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
3163     }
3164     next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
3165     nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
3166                                    - first_nonstatic_field_offset)/heapOopSize);
3167 
3168     // Iterate over fields again and compute correct offsets.
3169     // The field allocation type was temporarily stored in the offset slot.
3170     // oop fields are located before non-oop fields (static and non-static).
3171     for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {

3172       int real_offset;
3173       FieldAllocationType atype = (FieldAllocationType) fs.offset();
3174       switch (atype) {
3175         case STATIC_OOP:
3176           real_offset = next_static_oop_offset;
3177           next_static_oop_offset += heapOopSize;
3178           break;
3179         case STATIC_BYTE:
3180           real_offset = next_static_byte_offset;
3181           next_static_byte_offset += 1;
3182           break;
3183         case STATIC_SHORT:
3184           real_offset = next_static_short_offset;
3185           next_static_short_offset += BytesPerShort;
3186           break;
3187         case STATIC_WORD:
3188           real_offset = next_static_word_offset;
3189           next_static_word_offset += BytesPerInt;
3190           break;

3191         case STATIC_DOUBLE:
3192           real_offset = next_static_double_offset;
3193           next_static_double_offset += BytesPerLong;
3194           break;
3195         case NONSTATIC_OOP:
3196           if( nonstatic_oop_space_count > 0 ) {
3197             real_offset = nonstatic_oop_space_offset;
3198             nonstatic_oop_space_offset += heapOopSize;
3199             nonstatic_oop_space_count  -= 1;
3200           } else {
3201             real_offset = next_nonstatic_oop_offset;
3202             next_nonstatic_oop_offset += heapOopSize;
3203           }
3204           // Update oop maps
3205           if( nonstatic_oop_map_count > 0 &&
3206               nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3207               real_offset -
3208               int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
3209               heapOopSize ) {
3210             // Extend current oop map


3232         case NONSTATIC_SHORT:
3233           if( nonstatic_short_space_count > 0 ) {
3234             real_offset = nonstatic_short_space_offset;
3235             nonstatic_short_space_offset += BytesPerShort;
3236             nonstatic_short_space_count  -= 1;
3237           } else {
3238             real_offset = next_nonstatic_short_offset;
3239             next_nonstatic_short_offset += BytesPerShort;
3240           }
3241           break;
3242         case NONSTATIC_WORD:
3243           if( nonstatic_word_space_count > 0 ) {
3244             real_offset = nonstatic_word_space_offset;
3245             nonstatic_word_space_offset += BytesPerInt;
3246             nonstatic_word_space_count  -= 1;
3247           } else {
3248             real_offset = next_nonstatic_word_offset;
3249             next_nonstatic_word_offset += BytesPerInt;
3250           }
3251           break;

3252         case NONSTATIC_DOUBLE:
3253           real_offset = next_nonstatic_double_offset;
3254           next_nonstatic_double_offset += BytesPerLong;
3255           break;
3256         default:
3257           ShouldNotReachHere();
3258       }
3259       fs.set_offset(real_offset);

3260     }
3261 
3262     // Size of instances
3263     int instance_size;
3264 
3265     next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3266     instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3267 
3268     assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3269 
3270     // Number of non-static oop map blocks allocated at end of klass.
3271     const unsigned int total_oop_map_count =
3272       compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3273                             first_nonstatic_oop_offset);
3274 
3275     // Compute reference type
3276     ReferenceType rt;
3277     if (super_klass() == NULL) {
3278       rt = REF_NONE;
3279     } else {


3286                                                 total_oop_map_count,
3287                                                 rt, CHECK_(nullHandle));
3288     instanceKlassHandle this_klass (THREAD, ik);
3289 
3290     assert(this_klass->static_field_size() == static_field_size, "sanity");
3291     assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
3292            "sanity");
3293 
3294     // Fill in information already parsed
3295     this_klass->set_access_flags(access_flags);
3296     this_klass->set_should_verify_class(verify);
3297     jint lh = Klass::instance_layout_helper(instance_size, false);
3298     this_klass->set_layout_helper(lh);
3299     assert(this_klass->oop_is_instance(), "layout is correct");
3300     assert(this_klass->size_helper() == instance_size, "correct size_helper");
3301     // Not yet: supers are done below to support the new subtype-checking fields
3302     //this_klass->set_super(super_klass());
3303     this_klass->set_class_loader(class_loader());
3304     this_klass->set_nonstatic_field_size(nonstatic_field_size);
3305     this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3306     this_klass->set_static_oop_field_count(fac.count[STATIC_OOP]);
3307     cp->set_pool_holder(this_klass());
3308     error_handler.set_in_error(false);   // turn off error handler for cp
3309     this_klass->set_constants(cp());
3310     this_klass->set_local_interfaces(local_interfaces());
3311     this_klass->set_fields(fields(), java_fields_count);
3312     this_klass->set_methods(methods());
3313     if (has_final_method) {
3314       this_klass->set_has_final_method();
3315     }
3316     this_klass->set_method_ordering(method_ordering());
3317     // The instanceKlass::_methods_jmethod_ids cache and the
3318     // instanceKlass::_methods_cached_itable_indices cache are
3319     // both managed on the assumption that the initial cache
3320     // size is equal to the number of methods in the class. If
3321     // that changes, then instanceKlass::idnum_can_increment()
3322     // has to be changed accordingly.
3323     this_klass->set_initial_method_idnum(methods->length());
3324     this_klass->set_name(cp->klass_name_at(this_class_index));
3325     if (LinkWellKnownClasses || is_anonymous())  // I am well known to myself
3326       cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3327     this_klass->set_protection_domain(protection_domain());
3328     this_klass->set_fields_annotations(fields_annotations());
3329     this_klass->set_methods_annotations(methods_annotations());
3330     this_klass->set_methods_parameter_annotations(methods_parameter_annotations());
3331     this_klass->set_methods_default_annotations(methods_default_annotations());


src/share/vm/classfile/classFileParser.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File