src/share/vm/prims/jvmtiClassFileReconstituter.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 1
2d8a650513c2
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 /*
    26  * This file has been modified by Loongson Technology in 2015. These
    27  * modifications are Copyright (c) 2015 Loongson Technology, and are made
    28  * available on the same license terms set forth above.
    29  */
    31 #include "precompiled.hpp"
    32 #include "classfile/symbolTable.hpp"
    33 #include "interpreter/bytecodeStream.hpp"
    34 #include "oops/fieldStreams.hpp"
    35 #include "prims/jvmtiClassFileReconstituter.hpp"
    36 #include "runtime/signature.hpp"
    37 #ifdef TARGET_ARCH_x86
    38 # include "bytes_x86.hpp"
    39 #endif
    40 #ifdef TARGET_ARCH_sparc
    41 # include "bytes_sparc.hpp"
    42 #endif
    43 #ifdef TARGET_ARCH_zero
    44 # include "bytes_zero.hpp"
    45 #endif
    46 #ifdef TARGET_ARCH_arm
    47 # include "bytes_arm.hpp"
    48 #endif
    49 #ifdef TARGET_ARCH_ppc
    50 # include "bytes_ppc.hpp"
    51 #endif
    52 #ifdef TARGET_ARCH_mips
    53 # include "bytes_mips.hpp"
    54 #endif
    55 // FIXME: add Deprecated attribute
    56 // FIXME: fix Synthetic attribute
    57 // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
    60 // Write the field information portion of ClassFile structure
    61 // JVMSpec|     u2 fields_count;
    62 // JVMSpec|     field_info fields[fields_count];
    63 void JvmtiClassFileReconstituter::write_field_infos() {
    64   HandleMark hm(thread());
    65   Array<AnnotationArray*>* fields_anno = ikh()->fields_annotations();
    67   // Compute the real number of Java fields
    68   int java_fields = ikh()->java_fields_count();
    70   write_u2(java_fields);
    71   for (JavaFieldStream fs(ikh()); !fs.done(); fs.next()) {
    72     AccessFlags access_flags = fs.access_flags();
    73     int name_index = fs.name_index();
    74     int signature_index = fs.signature_index();
    75     int initial_value_index = fs.initval_index();
    76     guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
    77     // int offset = ikh()->field_offset( index );
    78     int generic_signature_index = fs.generic_signature_index();
    79     AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index());
    81     // JVMSpec|   field_info {
    82     // JVMSpec|         u2 access_flags;
    83     // JVMSpec|         u2 name_index;
    84     // JVMSpec|         u2 descriptor_index;
    85     // JVMSpec|         u2 attributes_count;
    86     // JVMSpec|         attribute_info attributes[attributes_count];
    87     // JVMSpec|   }
    89     write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
    90     write_u2(name_index);
    91     write_u2(signature_index);
    92     int attr_count = 0;
    93     if (initial_value_index != 0) {
    94       ++attr_count;
    95     }
    96     if (access_flags.is_synthetic()) {
    97       // ++attr_count;
    98     }
    99     if (generic_signature_index != 0) {
   100       ++attr_count;
   101     }
   102     if (anno != NULL) {
   103       ++attr_count;     // has RuntimeVisibleAnnotations attribute
   104     }
   106     write_u2(attr_count);
   108     if (initial_value_index != 0) {
   109       write_attribute_name_index("ConstantValue");
   110       write_u4(2); //length always 2
   111       write_u2(initial_value_index);
   112     }
   113     if (access_flags.is_synthetic()) {
   114       // write_synthetic_attribute();
   115     }
   116     if (generic_signature_index != 0) {
   117       write_signature_attribute(generic_signature_index);
   118     }
   119     if (anno != NULL) {
   120       write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   121     }
   122   }
   123 }
   125 // Write Code attribute
   126 // JVMSpec|   Code_attribute {
   127 // JVMSpec|     u2 attribute_name_index;
   128 // JVMSpec|     u4 attribute_length;
   129 // JVMSpec|     u2 max_stack;
   130 // JVMSpec|     u2 max_locals;
   131 // JVMSpec|     u4 code_length;
   132 // JVMSpec|     u1 code[code_length];
   133 // JVMSpec|     u2 exception_table_length;
   134 // JVMSpec|     {       u2 start_pc;
   135 // JVMSpec|             u2 end_pc;
   136 // JVMSpec|             u2  handler_pc;
   137 // JVMSpec|             u2  catch_type;
   138 // JVMSpec|     }       exception_table[exception_table_length];
   139 // JVMSpec|     u2 attributes_count;
   140 // JVMSpec|     attribute_info attributes[attributes_count];
   141 // JVMSpec|   }
   142 void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) {
   143   ConstMethod* const_method = method->constMethod();
   144   u2 line_num_cnt = 0;
   145   int stackmap_len = 0;
   146   int local_variable_table_length = 0;
   147   int local_variable_type_table_length = 0;
   149   // compute number and length of attributes
   150   int attr_count = 0;
   151   int attr_size = 0;
   152   if (const_method->has_linenumber_table()) {
   153     line_num_cnt = line_number_table_entries(method);
   154     if (line_num_cnt != 0) {
   155       ++attr_count;
   156       // Compute the complete size of the line number table attribute:
   157       //      LineNumberTable_attribute {
   158       //        u2 attribute_name_index;
   159       //        u4 attribute_length;
   160       //        u2 line_number_table_length;
   161       //        {  u2 start_pc;
   162       //           u2 line_number;
   163       //        } line_number_table[line_number_table_length];
   164       //      }
   165       attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2);
   166     }
   167   }
   168   if (method->has_stackmap_table()) {
   169     stackmap_len = method->stackmap_data()->length();
   170     if (stackmap_len != 0) {
   171       ++attr_count;
   172       // Compute the  size of the stack map table attribute (VM stores raw):
   173       //      StackMapTable_attribute {
   174       //        u2 attribute_name_index;
   175       //        u4 attribute_length;
   176       //        u2 number_of_entries;
   177       //        stack_map_frame_entries[number_of_entries];
   178       //      }
   179       attr_size += 2 + 4 + stackmap_len;
   180     }
   181   }
   182   if (method->has_localvariable_table()) {
   183     local_variable_table_length = method->localvariable_table_length();
   184     if (local_variable_table_length != 0) {
   185       ++attr_count;
   186       // Compute the size of the local variable table attribute (VM stores raw):
   187       // LocalVariableTable_attribute {
   188       //   u2 attribute_name_index;
   189       //   u4 attribute_length;
   190       //   u2 local_variable_table_length;
   191       //   {
   192       //     u2 start_pc;
   193       //     u2 length;
   194       //     u2 name_index;
   195       //     u2 descriptor_index;
   196       //     u2 index;
   197       //   }
   198       attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2);
   200       // Local variables with generic signatures must have LVTT entries
   201       LocalVariableTableElement *elem = method->localvariable_table_start();
   202       for (int idx = 0; idx < local_variable_table_length; idx++) {
   203         if (elem[idx].signature_cp_index != 0) {
   204           local_variable_type_table_length++;
   205         }
   206       }
   208       if (local_variable_type_table_length != 0) {
   209         ++attr_count;
   210         // Compute the size of the local variable type table attribute (VM stores raw):
   211         // LocalVariableTypeTable_attribute {
   212         //   u2 attribute_name_index;
   213         //   u4 attribute_length;
   214         //   u2 local_variable_type_table_length;
   215         //   {
   216         //     u2 start_pc;
   217         //     u2 length;
   218         //     u2 name_index;
   219         //     u2 signature_index;
   220         //     u2 index;
   221         //   }
   222         attr_size += 2 + 4 + 2 + local_variable_type_table_length * (2 + 2 + 2 + 2 + 2);
   223       }
   224     }
   225   }
   227   ExceptionTable exception_table(method());
   228   int exception_table_length = exception_table.length();
   229   int code_size = const_method->code_size();
   230   int size =
   231     2+2+4 +                                // max_stack, max_locals, code_length
   232     code_size +                            // code
   233     2 +                                    // exception_table_length
   234     (2+2+2+2) * exception_table_length +   // exception_table
   235     2 +                                    // attributes_count
   236     attr_size;                             // attributes
   238   write_attribute_name_index("Code");
   239   write_u4(size);
   240   write_u2(method->verifier_max_stack());
   241   write_u2(method->max_locals());
   242   write_u4(code_size);
   243   copy_bytecodes(method, (unsigned char*)writeable_address(code_size));
   244   write_u2(exception_table_length);
   245   for (int index = 0; index < exception_table_length; index++) {
   246     write_u2(exception_table.start_pc(index));
   247     write_u2(exception_table.end_pc(index));
   248     write_u2(exception_table.handler_pc(index));
   249     write_u2(exception_table.catch_type_index(index));
   250   }
   251   write_u2(attr_count);
   252   if (line_num_cnt != 0) {
   253     write_line_number_table_attribute(method, line_num_cnt);
   254   }
   255   if (stackmap_len != 0) {
   256     write_stackmap_table_attribute(method, stackmap_len);
   257   }
   258   if (local_variable_table_length != 0) {
   259     write_local_variable_table_attribute(method, local_variable_table_length);
   260   }
   261   if (local_variable_type_table_length != 0) {
   262     write_local_variable_type_table_attribute(method, local_variable_type_table_length);
   263   }
   264 }
   266 // Write Exceptions attribute
   267 // JVMSpec|   Exceptions_attribute {
   268 // JVMSpec|     u2 attribute_name_index;
   269 // JVMSpec|     u4 attribute_length;
   270 // JVMSpec|     u2 number_of_exceptions;
   271 // JVMSpec|     u2 exception_index_table[number_of_exceptions];
   272 // JVMSpec|   }
   273 void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) {
   274   CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start();
   275   int checked_exceptions_length = const_method->checked_exceptions_length();
   276   int size =
   277     2 +                                    // number_of_exceptions
   278     2 * checked_exceptions_length;         // exception_index_table
   280   write_attribute_name_index("Exceptions");
   281   write_u4(size);
   282   write_u2(checked_exceptions_length);
   283   for (int index = 0; index < checked_exceptions_length; index++) {
   284     write_u2(checked_exceptions[index].class_cp_index);
   285   }
   286 }
   288 // Write SourceFile attribute
   289 // JVMSpec|   SourceFile_attribute {
   290 // JVMSpec|     u2 attribute_name_index;
   291 // JVMSpec|     u4 attribute_length;
   292 // JVMSpec|     u2 sourcefile_index;
   293 // JVMSpec|   }
   294 void JvmtiClassFileReconstituter::write_source_file_attribute() {
   295   assert(ikh()->source_file_name() != NULL, "caller must check");
   297   write_attribute_name_index("SourceFile");
   298   write_u4(2);  // always length 2
   299   write_u2(symbol_to_cpool_index(ikh()->source_file_name()));
   300 }
   302 // Write SourceDebugExtension attribute
   303 // JSR45|   SourceDebugExtension_attribute {
   304 // JSR45|       u2 attribute_name_index;
   305 // JSR45|       u4 attribute_length;
   306 // JSR45|       u1 debug_extension[attribute_length];
   307 // JSR45|   }
   308 void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() {
   309   assert(ikh()->source_debug_extension() != NULL, "caller must check");
   311   write_attribute_name_index("SourceDebugExtension");
   312   int len = (int)strlen(ikh()->source_debug_extension());
   313   write_u4(len);
   314   u1* ext = (u1*)ikh()->source_debug_extension();
   315   for (int i=0; i<len; i++) {
   316     write_u1(ext[i]);
   317   }
   318 }
   320 // Write (generic) Signature attribute
   321 // JVMSpec|   Signature_attribute {
   322 // JVMSpec|     u2 attribute_name_index;
   323 // JVMSpec|     u4 attribute_length;
   324 // JVMSpec|     u2 signature_index;
   325 // JVMSpec|   }
   326 void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) {
   327   write_attribute_name_index("Signature");
   328   write_u4(2);  // always length 2
   329   write_u2(generic_signature_index);
   330 }
   332 // Compute the number of entries in the InnerClasses attribute
   333 u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() {
   334   InnerClassesIterator iter(ikh());
   335   return iter.length();
   336 }
   338 // Write an annotation attribute.  The VM stores them in raw form, so all we need
   339 // to do is add the attrubute name and fill in the length.
   340 // JSR202|   *Annotations_attribute {
   341 // JSR202|     u2 attribute_name_index;
   342 // JSR202|     u4 attribute_length;
   343 // JSR202|     ...
   344 // JSR202|   }
   345 void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
   346                                                               AnnotationArray* annos) {
   347   u4 length = annos->length();
   348   write_attribute_name_index(attr_name);
   349   write_u4(length);
   350   memcpy(writeable_address(length), annos->adr_at(0), length);
   351 }
   353 //  BootstrapMethods_attribute {
   354 //    u2 attribute_name_index;
   355 //    u4 attribute_length;
   356 //    u2 num_bootstrap_methods;
   357 //    {   u2 bootstrap_method_ref;
   358 //        u2 num_bootstrap_arguments;
   359 //        u2 bootstrap_arguments[num_bootstrap_arguments];
   360 //    } bootstrap_methods[num_bootstrap_methods];
   361 //  }
   362 void JvmtiClassFileReconstituter::write_bootstrapmethod_attribute() {
   363   Array<u2>* operands = cpool()->operands();
   364   write_attribute_name_index("BootstrapMethods");
   365   int num_bootstrap_methods = ConstantPool::operand_array_length(operands);
   367   // calculate length of attribute
   368   int length = sizeof(u2); // num_bootstrap_methods
   369   for (int n = 0; n < num_bootstrap_methods; n++) {
   370     u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
   371     length += sizeof(u2); // bootstrap_method_ref
   372     length += sizeof(u2); // num_bootstrap_arguments
   373     length += sizeof(u2) * num_bootstrap_arguments; // bootstrap_arguments[num_bootstrap_arguments]
   374   }
   375   write_u4(length);
   377   // write attribute
   378   write_u2(num_bootstrap_methods);
   379   for (int n = 0; n < num_bootstrap_methods; n++) {
   380     u2 bootstrap_method_ref = cpool()->operand_bootstrap_method_ref_index_at(n);
   381     u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
   382     write_u2(bootstrap_method_ref);
   383     write_u2(num_bootstrap_arguments);
   384     for (int arg = 0; arg < num_bootstrap_arguments; arg++) {
   385       u2 bootstrap_argument = cpool()->operand_argument_index_at(n, arg);
   386       write_u2(bootstrap_argument);
   387     }
   388   }
   389 }
   392 // Write InnerClasses attribute
   393 // JVMSpec|   InnerClasses_attribute {
   394 // JVMSpec|     u2 attribute_name_index;
   395 // JVMSpec|     u4 attribute_length;
   396 // JVMSpec|     u2 number_of_classes;
   397 // JVMSpec|     {  u2 inner_class_info_index;
   398 // JVMSpec|        u2 outer_class_info_index;
   399 // JVMSpec|        u2 inner_name_index;
   400 // JVMSpec|        u2 inner_class_access_flags;
   401 // JVMSpec|     } classes[number_of_classes];
   402 // JVMSpec|   }
   403 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
   404   InnerClassesIterator iter(ikh());
   405   guarantee(iter.length() != 0 && iter.length() == length,
   406             "caller must check");
   407   u2 entry_count = length / InstanceKlass::inner_class_next_offset;
   408   u4 size = 2 + entry_count * (2+2+2+2);
   410   write_attribute_name_index("InnerClasses");
   411   write_u4(size);
   412   write_u2(entry_count);
   413   for (; !iter.done(); iter.next()) {
   414     write_u2(iter.inner_class_info_index());
   415     write_u2(iter.outer_class_info_index());
   416     write_u2(iter.inner_name_index());
   417     write_u2(iter.inner_access_flags());
   418   }
   419 }
   421 // Write Synthetic attribute
   422 // JVMSpec|   Synthetic_attribute {
   423 // JVMSpec|     u2 attribute_name_index;
   424 // JVMSpec|     u4 attribute_length;
   425 // JVMSpec|   }
   426 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
   427   write_attribute_name_index("Synthetic");
   428   write_u4(0); //length always zero
   429 }
   431 // Compute size of LineNumberTable
   432 u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) {
   433   // The line number table is compressed so we don't know how big it is until decompressed.
   434   // Decompression is really fast so we just do it twice.
   435   u2 num_entries = 0;
   436   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
   437   while (stream.read_pair()) {
   438     num_entries++;
   439   }
   440   return num_entries;
   441 }
   443 // Write LineNumberTable attribute
   444 // JVMSpec|   LineNumberTable_attribute {
   445 // JVMSpec|     u2 attribute_name_index;
   446 // JVMSpec|     u4 attribute_length;
   447 // JVMSpec|     u2 line_number_table_length;
   448 // JVMSpec|     {  u2 start_pc;
   449 // JVMSpec|        u2 line_number;
   450 // JVMSpec|     } line_number_table[line_number_table_length];
   451 // JVMSpec|   }
   452 void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method,
   453                                                                     u2 num_entries) {
   455   write_attribute_name_index("LineNumberTable");
   456   write_u4(2 + num_entries * (2 + 2));
   457   write_u2(num_entries);
   459   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
   460   while (stream.read_pair()) {
   461     write_u2(stream.bci());
   462     write_u2(stream.line());
   463   }
   464 }
   466 // Write LocalVariableTable attribute
   467 // JVMSpec|   LocalVariableTable_attribute {
   468 // JVMSpec|     u2 attribute_name_index;
   469 // JVMSpec|     u4 attribute_length;
   470 // JVMSpec|     u2 local_variable_table_length;
   471 // JVMSpec|     {  u2 start_pc;
   472 // JVMSpec|       u2 length;
   473 // JVMSpec|       u2 name_index;
   474 // JVMSpec|       u2 descriptor_index;
   475 // JVMSpec|       u2 index;
   476 // JVMSpec|     } local_variable_table[local_variable_table_length];
   477 // JVMSpec|   }
   478 void JvmtiClassFileReconstituter::write_local_variable_table_attribute(methodHandle method, u2 num_entries) {
   479     write_attribute_name_index("LocalVariableTable");
   480     write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
   481     write_u2(num_entries);
   483     assert(method->localvariable_table_length() == num_entries, "just checking");
   485     LocalVariableTableElement *elem = method->localvariable_table_start();
   486     for (int j=0; j<method->localvariable_table_length(); j++) {
   487       write_u2(elem->start_bci);
   488       write_u2(elem->length);
   489       write_u2(elem->name_cp_index);
   490       write_u2(elem->descriptor_cp_index);
   491       write_u2(elem->slot);
   492       elem++;
   493     }
   494 }
   496 // Write LocalVariableTypeTable attribute
   497 // JVMSpec|   LocalVariableTypeTable_attribute {
   498 // JVMSpec|     u2 attribute_name_index;
   499 // JVMSpec|     u4 attribute_length;
   500 // JVMSpec|     u2 local_variable_type_table_length;
   501 // JVMSpec|     { u2 start_pc;
   502 // JVMSpec|       u2 length;
   503 // JVMSpec|       u2 name_index;
   504 // JVMSpec|       u2 signature_index;
   505 // JVMSpec|       u2 index;
   506 // JVMSpec|     } local_variable_type_table[local_variable_type_table_length];
   507 // JVMSpec|   }
   508 void JvmtiClassFileReconstituter::write_local_variable_type_table_attribute(methodHandle method, u2 num_entries) {
   509     write_attribute_name_index("LocalVariableTypeTable");
   510     write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
   511     write_u2(num_entries);
   513     LocalVariableTableElement *elem = method->localvariable_table_start();
   514     for (int j=0; j<method->localvariable_table_length(); j++) {
   515       if (elem->signature_cp_index > 0) {
   516         // Local variable has a generic signature - write LVTT attribute entry
   517         write_u2(elem->start_bci);
   518         write_u2(elem->length);
   519         write_u2(elem->name_cp_index);
   520         write_u2(elem->signature_cp_index);
   521         write_u2(elem->slot);
   522         num_entries--;
   523       }
   524       elem++;
   525     }
   526     assert(num_entries == 0, "just checking");
   527 }
   529 // Write stack map table attribute
   530 // JSR-202|   StackMapTable_attribute {
   531 // JSR-202|     u2 attribute_name_index;
   532 // JSR-202|     u4 attribute_length;
   533 // JSR-202|     u2 number_of_entries;
   534 // JSR-202|     stack_map_frame_entries[number_of_entries];
   535 // JSR-202|   }
   536 void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method,
   537                                                                  int stackmap_len) {
   539   write_attribute_name_index("StackMapTable");
   540   write_u4(stackmap_len);
   541   memcpy(
   542     writeable_address(stackmap_len),
   543     (void*)(method->stackmap_data()->adr_at(0)),
   544     stackmap_len);
   545 }
   547 // Write one method_info structure
   548 // JVMSpec|   method_info {
   549 // JVMSpec|     u2 access_flags;
   550 // JVMSpec|     u2 name_index;
   551 // JVMSpec|     u2 descriptor_index;
   552 // JVMSpec|     u2 attributes_count;
   553 // JVMSpec|     attribute_info attributes[attributes_count];
   554 // JVMSpec|   }
   555 void JvmtiClassFileReconstituter::write_method_info(methodHandle method) {
   556   AccessFlags access_flags = method->access_flags();
   557   ConstMethod* const_method = method->constMethod();
   558   u2 generic_signature_index = const_method->generic_signature_index();
   559   AnnotationArray* anno = method->annotations();
   560   AnnotationArray* param_anno = method->parameter_annotations();
   561   AnnotationArray* default_anno = method->annotation_default();
   563   // skip generated default interface methods
   564   if (method->is_overpass()) {
   565     return;
   566   }
   568   write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS);
   569   write_u2(const_method->name_index());
   570   write_u2(const_method->signature_index());
   572   // write attributes in the same order javac does, so we can test with byte for
   573   // byte comparison
   574   int attr_count = 0;
   575   if (const_method->code_size() != 0) {
   576     ++attr_count;     // has Code attribute
   577   }
   578   if (const_method->has_checked_exceptions()) {
   579     ++attr_count;     // has Exceptions attribute
   580   }
   581   if (default_anno != NULL) {
   582     ++attr_count;     // has AnnotationDefault attribute
   583   }
   584   // Deprecated attribute would go here
   585   if (access_flags.is_synthetic()) { // FIXME
   586     // ++attr_count;
   587   }
   588   if (generic_signature_index != 0) {
   589     ++attr_count;
   590   }
   591   if (anno != NULL) {
   592     ++attr_count;     // has RuntimeVisibleAnnotations attribute
   593   }
   594   if (param_anno != NULL) {
   595     ++attr_count;     // has RuntimeVisibleParameterAnnotations attribute
   596   }
   598   write_u2(attr_count);
   599   if (const_method->code_size() > 0) {
   600     write_code_attribute(method);
   601   }
   602   if (const_method->has_checked_exceptions()) {
   603     write_exceptions_attribute(const_method);
   604   }
   605   if (default_anno != NULL) {
   606     write_annotations_attribute("AnnotationDefault", default_anno);
   607   }
   608   // Deprecated attribute would go here
   609   if (access_flags.is_synthetic()) {
   610     // write_synthetic_attribute();
   611   }
   612   if (generic_signature_index != 0) {
   613     write_signature_attribute(generic_signature_index);
   614   }
   615   if (anno != NULL) {
   616     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   617   }
   618   if (param_anno != NULL) {
   619     write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
   620   }
   621 }
   623 // Write the class attributes portion of ClassFile structure
   624 // JVMSpec|     u2 attributes_count;
   625 // JVMSpec|     attribute_info attributes[attributes_count];
   626 void JvmtiClassFileReconstituter::write_class_attributes() {
   627   u2 inner_classes_length = inner_classes_attribute_length();
   628   Symbol* generic_signature = ikh()->generic_signature();
   629   AnnotationArray* anno = ikh()->class_annotations();
   631   int attr_count = 0;
   632   if (generic_signature != NULL) {
   633     ++attr_count;
   634   }
   635   if (ikh()->source_file_name() != NULL) {
   636     ++attr_count;
   637   }
   638   if (ikh()->source_debug_extension() != NULL) {
   639     ++attr_count;
   640   }
   641   if (inner_classes_length > 0) {
   642     ++attr_count;
   643   }
   644   if (anno != NULL) {
   645     ++attr_count;     // has RuntimeVisibleAnnotations attribute
   646   }
   647   if (cpool()->operands() != NULL) {
   648     ++attr_count;
   649   }
   651   write_u2(attr_count);
   653   if (generic_signature != NULL) {
   654     write_signature_attribute(symbol_to_cpool_index(generic_signature));
   655   }
   656   if (ikh()->source_file_name() != NULL) {
   657     write_source_file_attribute();
   658   }
   659   if (ikh()->source_debug_extension() != NULL) {
   660     write_source_debug_extension_attribute();
   661   }
   662   if (inner_classes_length > 0) {
   663     write_inner_classes_attribute(inner_classes_length);
   664   }
   665   if (anno != NULL) {
   666     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   667   }
   668   if (cpool()->operands() != NULL) {
   669     write_bootstrapmethod_attribute();
   670   }
   671 }
   673 // Write the method information portion of ClassFile structure
   674 // JVMSpec|     u2 methods_count;
   675 // JVMSpec|     method_info methods[methods_count];
   676 void JvmtiClassFileReconstituter::write_method_infos() {
   677   HandleMark hm(thread());
   678   Array<Method*>* methods = ikh()->methods();
   679   int num_methods = methods->length();
   680   int num_overpass = 0;
   682   // count the generated default interface methods
   683   // these will not be re-created by write_method_info
   684   // and should not be included in the total count
   685   for (int index = 0; index < num_methods; index++) {
   686     Method* method = methods->at(index);
   687     if (method->is_overpass()) {
   688       num_overpass++;
   689     }
   690   }
   692   write_u2(num_methods - num_overpass);
   693   if (JvmtiExport::can_maintain_original_method_order()) {
   694     int index;
   695     int original_index;
   696     intArray method_order(num_methods, 0);
   698     // invert the method order mapping
   699     for (index = 0; index < num_methods; index++) {
   700       original_index = ikh()->method_ordering()->at(index);
   701       assert(original_index >= 0 && original_index < num_methods,
   702              "invalid original method index");
   703       method_order.at_put(original_index, index);
   704     }
   706     // write in original order
   707     for (original_index = 0; original_index < num_methods; original_index++) {
   708       index = method_order.at(original_index);
   709       methodHandle method(thread(), methods->at(index));
   710       write_method_info(method);
   711     }
   712   } else {
   713     // method order not preserved just dump the method infos
   714     for (int index = 0; index < num_methods; index++) {
   715       methodHandle method(thread(), methods->at(index));
   716       write_method_info(method);
   717     }
   718   }
   719 }
   721 void JvmtiClassFileReconstituter::write_class_file_format() {
   722   ReallocMark();
   724   // JVMSpec|   ClassFile {
   725   // JVMSpec|           u4 magic;
   726   write_u4(0xCAFEBABE);
   728   // JVMSpec|           u2 minor_version;
   729   // JVMSpec|           u2 major_version;
   730   write_u2(ikh()->minor_version());
   731   u2 major = ikh()->major_version();
   732   write_u2(major);
   734   // JVMSpec|           u2 constant_pool_count;
   735   // JVMSpec|           cp_info constant_pool[constant_pool_count-1];
   736   write_u2(cpool()->length());
   737   copy_cpool_bytes(writeable_address(cpool_size()));
   739   // JVMSpec|           u2 access_flags;
   740   write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
   742   // JVMSpec|           u2 this_class;
   743   // JVMSpec|           u2 super_class;
   744   write_u2(class_symbol_to_cpool_index(ikh()->name()));
   745   Klass* super_class = ikh()->super();
   746   write_u2(super_class == NULL? 0 :  // zero for java.lang.Object
   747                 class_symbol_to_cpool_index(super_class->name()));
   749   // JVMSpec|           u2 interfaces_count;
   750   // JVMSpec|           u2 interfaces[interfaces_count];
   751   Array<Klass*>* interfaces =  ikh()->local_interfaces();
   752   int num_interfaces = interfaces->length();
   753   write_u2(num_interfaces);
   754   for (int index = 0; index < num_interfaces; index++) {
   755     HandleMark hm(thread());
   756     instanceKlassHandle iikh(thread(), interfaces->at(index));
   757     write_u2(class_symbol_to_cpool_index(iikh->name()));
   758   }
   760   // JVMSpec|           u2 fields_count;
   761   // JVMSpec|           field_info fields[fields_count];
   762   write_field_infos();
   764   // JVMSpec|           u2 methods_count;
   765   // JVMSpec|           method_info methods[methods_count];
   766   write_method_infos();
   768   // JVMSpec|           u2 attributes_count;
   769   // JVMSpec|           attribute_info attributes[attributes_count];
   770   // JVMSpec|   } /* end ClassFile 8?
   771   write_class_attributes();
   772 }
   774 address JvmtiClassFileReconstituter::writeable_address(size_t size) {
   775   size_t used_size = _buffer_ptr - _buffer;
   776   if (size + used_size >= _buffer_size) {
   777     // compute the new buffer size: must be at least twice as big as before
   778     // plus whatever new is being used; then convert to nice clean block boundary
   779     size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size
   780                                                          * initial_buffer_size;
   782     // VM goes belly-up if the memory isn't available, so cannot do OOM processing
   783     _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size);
   784     _buffer_size = new_buffer_size;
   785     _buffer_ptr = _buffer + used_size;
   786   }
   787   u1* ret_ptr = _buffer_ptr;
   788   _buffer_ptr += size;
   789   return ret_ptr;
   790 }
   792 void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) {
   793   TempNewSymbol sym = SymbolTable::probe(name, (int)strlen(name));
   794   assert(sym != NULL, "attribute name symbol not found");
   795   u2 attr_name_index = symbol_to_cpool_index(sym);
   796   assert(attr_name_index != 0, "attribute name symbol not in constant pool");
   797   write_u2(attr_name_index);
   798 }
   800 void JvmtiClassFileReconstituter::write_u1(u1 x) {
   801   *writeable_address(1) = x;
   802 }
   804 void JvmtiClassFileReconstituter::write_u2(u2 x) {
   805   Bytes::put_Java_u2(writeable_address(2), x);
   806 }
   808 void JvmtiClassFileReconstituter::write_u4(u4 x) {
   809   Bytes::put_Java_u4(writeable_address(4), x);
   810 }
   812 void JvmtiClassFileReconstituter::write_u8(u8 x) {
   813   Bytes::put_Java_u8(writeable_address(8), x);
   814 }
   816 void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh,
   817                                                  unsigned char* bytecodes) {
   818   // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes
   819   // and the breakpoint bytecode are converted to their original bytecodes.
   821   BytecodeStream bs(mh);
   823   unsigned char* p = bytecodes;
   824   Bytecodes::Code code;
   825   bool is_rewritten = mh->method_holder()->is_rewritten();
   827   while ((code = bs.next()) >= 0) {
   828     assert(Bytecodes::is_java_code(code), "sanity check");
   829     assert(code != Bytecodes::_breakpoint, "sanity check");
   831     // length of bytecode (mnemonic + operands)
   832     address bcp = bs.bcp();
   833     int     len = bs.instruction_size();
   834     assert(len > 0, "length must be > 0");
   836     // copy the bytecodes
   837     *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
   838     if (len > 1) {
   839       memcpy(p+1, bcp+1, len-1);
   840     }
   842     // During linking the get/put and invoke instructions are rewritten
   843     // with an index into the constant pool cache. The original constant
   844     // pool index must be returned to caller.  Rewrite the index.
   845     if (is_rewritten && len > 1) {
   846       bool is_wide = false;
   847       switch (code) {
   848       case Bytecodes::_getstatic       :  // fall through
   849       case Bytecodes::_putstatic       :  // fall through
   850       case Bytecodes::_getfield        :  // fall through
   851       case Bytecodes::_putfield        :  // fall through
   852       case Bytecodes::_invokevirtual   :  // fall through
   853       case Bytecodes::_invokespecial   :  // fall through
   854       case Bytecodes::_invokestatic    :  // fall through
   855       case Bytecodes::_invokedynamic   :  // fall through
   856       case Bytecodes::_invokeinterface : {
   857         assert(len == 3 ||
   858                (code == Bytecodes::_invokeinterface && len == 5) ||
   859                (code == Bytecodes::_invokedynamic   && len == 5),
   860                "sanity check");
   862         int cpci = Bytes::get_native_u2(bcp+1);
   863         bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic);
   864         ConstantPoolCacheEntry* entry;
   865         if (is_invokedynamic) {
   866           cpci = Bytes::get_native_u4(bcp+1);
   867           entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci);
   868         } else {
   869         // cache cannot be pre-fetched since some classes won't have it yet
   870           entry = mh->constants()->cache()->entry_at(cpci);
   871         }
   872         int i = entry->constant_pool_index();
   873         assert(i < mh->constants()->length(), "sanity check");
   874         Bytes::put_Java_u2((address)(p+1), (u2)i);     // java byte ordering
   875         if (is_invokedynamic)  *(p+3) = *(p+4) = 0;
   876         break;
   877       }
   878       case Bytecodes::_ldc_w:
   879         is_wide = true; // fall through
   880       case Bytecodes::_ldc: {
   881         if (bs.raw_code() == Bytecodes::_fast_aldc || bs.raw_code() == Bytecodes::_fast_aldc_w) {
   882           int cpci = is_wide ? Bytes::get_native_u2(bcp+1) : (u1)(*(bcp+1));
   883           int i = mh->constants()->object_to_cp_index(cpci);
   884           assert(i < mh->constants()->length(), "sanity check");
   885           if (is_wide) {
   886             Bytes::put_Java_u2((address)(p+1), (u2)i);     // java byte ordering
   887           } else {
   888             *(p+1) = (u1)i;
   889           }
   890         }
   891         break;
   892         }
   893       }
   894     }
   896     p += len;
   897   }
   898 }

mercurial