src/share/vm/prims/jvmtiClassFileReconstituter.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 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();
    66   Array<AnnotationArray*>* fields_type_anno = ikh()->fields_type_annotations();
    68   // Compute the real number of Java fields
    69   int java_fields = ikh()->java_fields_count();
    71   write_u2(java_fields);
    72   for (JavaFieldStream fs(ikh()); !fs.done(); fs.next()) {
    73     AccessFlags access_flags = fs.access_flags();
    74     int name_index = fs.name_index();
    75     int signature_index = fs.signature_index();
    76     int initial_value_index = fs.initval_index();
    77     guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
    78     // int offset = ikh()->field_offset( index );
    79     int generic_signature_index = fs.generic_signature_index();
    80     AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index());
    81     AnnotationArray* type_anno = fields_type_anno == NULL ? NULL : fields_type_anno->at(fs.index());
    83     // JVMSpec|   field_info {
    84     // JVMSpec|         u2 access_flags;
    85     // JVMSpec|         u2 name_index;
    86     // JVMSpec|         u2 descriptor_index;
    87     // JVMSpec|         u2 attributes_count;
    88     // JVMSpec|         attribute_info attributes[attributes_count];
    89     // JVMSpec|   }
    91     write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
    92     write_u2(name_index);
    93     write_u2(signature_index);
    94     int attr_count = 0;
    95     if (initial_value_index != 0) {
    96       ++attr_count;
    97     }
    98     if (access_flags.is_synthetic()) {
    99       // ++attr_count;
   100     }
   101     if (generic_signature_index != 0) {
   102       ++attr_count;
   103     }
   104     if (anno != NULL) {
   105       ++attr_count;     // has RuntimeVisibleAnnotations attribute
   106     }
   107     if (type_anno != NULL) {
   108       ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
   109     }
   111     write_u2(attr_count);
   113     if (initial_value_index != 0) {
   114       write_attribute_name_index("ConstantValue");
   115       write_u4(2); //length always 2
   116       write_u2(initial_value_index);
   117     }
   118     if (access_flags.is_synthetic()) {
   119       // write_synthetic_attribute();
   120     }
   121     if (generic_signature_index != 0) {
   122       write_signature_attribute(generic_signature_index);
   123     }
   124     if (anno != NULL) {
   125       write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   126     }
   127     if (type_anno != NULL) {
   128       write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
   129     }
   130   }
   131 }
   133 // Write Code attribute
   134 // JVMSpec|   Code_attribute {
   135 // JVMSpec|     u2 attribute_name_index;
   136 // JVMSpec|     u4 attribute_length;
   137 // JVMSpec|     u2 max_stack;
   138 // JVMSpec|     u2 max_locals;
   139 // JVMSpec|     u4 code_length;
   140 // JVMSpec|     u1 code[code_length];
   141 // JVMSpec|     u2 exception_table_length;
   142 // JVMSpec|     {       u2 start_pc;
   143 // JVMSpec|             u2 end_pc;
   144 // JVMSpec|             u2  handler_pc;
   145 // JVMSpec|             u2  catch_type;
   146 // JVMSpec|     }       exception_table[exception_table_length];
   147 // JVMSpec|     u2 attributes_count;
   148 // JVMSpec|     attribute_info attributes[attributes_count];
   149 // JVMSpec|   }
   150 void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) {
   151   ConstMethod* const_method = method->constMethod();
   152   u2 line_num_cnt = 0;
   153   int stackmap_len = 0;
   154   int local_variable_table_length = 0;
   155   int local_variable_type_table_length = 0;
   157   // compute number and length of attributes
   158   int attr_count = 0;
   159   int attr_size = 0;
   160   if (const_method->has_linenumber_table()) {
   161     line_num_cnt = line_number_table_entries(method);
   162     if (line_num_cnt != 0) {
   163       ++attr_count;
   164       // Compute the complete size of the line number table attribute:
   165       //      LineNumberTable_attribute {
   166       //        u2 attribute_name_index;
   167       //        u4 attribute_length;
   168       //        u2 line_number_table_length;
   169       //        {  u2 start_pc;
   170       //           u2 line_number;
   171       //        } line_number_table[line_number_table_length];
   172       //      }
   173       attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2);
   174     }
   175   }
   176   if (method->has_stackmap_table()) {
   177     stackmap_len = method->stackmap_data()->length();
   178     if (stackmap_len != 0) {
   179       ++attr_count;
   180       // Compute the  size of the stack map table attribute (VM stores raw):
   181       //      StackMapTable_attribute {
   182       //        u2 attribute_name_index;
   183       //        u4 attribute_length;
   184       //        u2 number_of_entries;
   185       //        stack_map_frame_entries[number_of_entries];
   186       //      }
   187       attr_size += 2 + 4 + stackmap_len;
   188     }
   189   }
   190   if (method->has_localvariable_table()) {
   191     local_variable_table_length = method->localvariable_table_length();
   192     if (local_variable_table_length != 0) {
   193       ++attr_count;
   194       // Compute the size of the local variable table attribute (VM stores raw):
   195       // LocalVariableTable_attribute {
   196       //   u2 attribute_name_index;
   197       //   u4 attribute_length;
   198       //   u2 local_variable_table_length;
   199       //   {
   200       //     u2 start_pc;
   201       //     u2 length;
   202       //     u2 name_index;
   203       //     u2 descriptor_index;
   204       //     u2 index;
   205       //   }
   206       attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2);
   208       // Local variables with generic signatures must have LVTT entries
   209       LocalVariableTableElement *elem = method->localvariable_table_start();
   210       for (int idx = 0; idx < local_variable_table_length; idx++) {
   211         if (elem[idx].signature_cp_index != 0) {
   212           local_variable_type_table_length++;
   213         }
   214       }
   216       if (local_variable_type_table_length != 0) {
   217         ++attr_count;
   218         // Compute the size of the local variable type table attribute (VM stores raw):
   219         // LocalVariableTypeTable_attribute {
   220         //   u2 attribute_name_index;
   221         //   u4 attribute_length;
   222         //   u2 local_variable_type_table_length;
   223         //   {
   224         //     u2 start_pc;
   225         //     u2 length;
   226         //     u2 name_index;
   227         //     u2 signature_index;
   228         //     u2 index;
   229         //   }
   230         attr_size += 2 + 4 + 2 + local_variable_type_table_length * (2 + 2 + 2 + 2 + 2);
   231       }
   232     }
   233   }
   235   ExceptionTable exception_table(method());
   236   int exception_table_length = exception_table.length();
   237   int code_size = const_method->code_size();
   238   int size =
   239     2+2+4 +                                // max_stack, max_locals, code_length
   240     code_size +                            // code
   241     2 +                                    // exception_table_length
   242     (2+2+2+2) * exception_table_length +   // exception_table
   243     2 +                                    // attributes_count
   244     attr_size;                             // attributes
   246   write_attribute_name_index("Code");
   247   write_u4(size);
   248   write_u2(method->verifier_max_stack());
   249   write_u2(method->max_locals());
   250   write_u4(code_size);
   251   copy_bytecodes(method, (unsigned char*)writeable_address(code_size));
   252   write_u2(exception_table_length);
   253   for (int index = 0; index < exception_table_length; index++) {
   254     write_u2(exception_table.start_pc(index));
   255     write_u2(exception_table.end_pc(index));
   256     write_u2(exception_table.handler_pc(index));
   257     write_u2(exception_table.catch_type_index(index));
   258   }
   259   write_u2(attr_count);
   260   if (line_num_cnt != 0) {
   261     write_line_number_table_attribute(method, line_num_cnt);
   262   }
   263   if (stackmap_len != 0) {
   264     write_stackmap_table_attribute(method, stackmap_len);
   265   }
   266   if (local_variable_table_length != 0) {
   267     write_local_variable_table_attribute(method, local_variable_table_length);
   268   }
   269   if (local_variable_type_table_length != 0) {
   270     write_local_variable_type_table_attribute(method, local_variable_type_table_length);
   271   }
   272 }
   274 // Write Exceptions attribute
   275 // JVMSpec|   Exceptions_attribute {
   276 // JVMSpec|     u2 attribute_name_index;
   277 // JVMSpec|     u4 attribute_length;
   278 // JVMSpec|     u2 number_of_exceptions;
   279 // JVMSpec|     u2 exception_index_table[number_of_exceptions];
   280 // JVMSpec|   }
   281 void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) {
   282   CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start();
   283   int checked_exceptions_length = const_method->checked_exceptions_length();
   284   int size =
   285     2 +                                    // number_of_exceptions
   286     2 * checked_exceptions_length;         // exception_index_table
   288   write_attribute_name_index("Exceptions");
   289   write_u4(size);
   290   write_u2(checked_exceptions_length);
   291   for (int index = 0; index < checked_exceptions_length; index++) {
   292     write_u2(checked_exceptions[index].class_cp_index);
   293   }
   294 }
   296 // Write SourceFile attribute
   297 // JVMSpec|   SourceFile_attribute {
   298 // JVMSpec|     u2 attribute_name_index;
   299 // JVMSpec|     u4 attribute_length;
   300 // JVMSpec|     u2 sourcefile_index;
   301 // JVMSpec|   }
   302 void JvmtiClassFileReconstituter::write_source_file_attribute() {
   303   assert(ikh()->source_file_name() != NULL, "caller must check");
   305   write_attribute_name_index("SourceFile");
   306   write_u4(2);  // always length 2
   307   write_u2(symbol_to_cpool_index(ikh()->source_file_name()));
   308 }
   310 // Write SourceDebugExtension attribute
   311 // JSR45|   SourceDebugExtension_attribute {
   312 // JSR45|       u2 attribute_name_index;
   313 // JSR45|       u4 attribute_length;
   314 // JSR45|       u1 debug_extension[attribute_length];
   315 // JSR45|   }
   316 void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() {
   317   assert(ikh()->source_debug_extension() != NULL, "caller must check");
   319   write_attribute_name_index("SourceDebugExtension");
   320   int len = (int)strlen(ikh()->source_debug_extension());
   321   write_u4(len);
   322   u1* ext = (u1*)ikh()->source_debug_extension();
   323   for (int i=0; i<len; i++) {
   324     write_u1(ext[i]);
   325   }
   326 }
   328 // Write (generic) Signature attribute
   329 // JVMSpec|   Signature_attribute {
   330 // JVMSpec|     u2 attribute_name_index;
   331 // JVMSpec|     u4 attribute_length;
   332 // JVMSpec|     u2 signature_index;
   333 // JVMSpec|   }
   334 void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) {
   335   write_attribute_name_index("Signature");
   336   write_u4(2);  // always length 2
   337   write_u2(generic_signature_index);
   338 }
   340 // Compute the number of entries in the InnerClasses attribute
   341 u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() {
   342   InnerClassesIterator iter(ikh());
   343   return iter.length();
   344 }
   346 // Write an annotation attribute.  The VM stores them in raw form, so all we need
   347 // to do is add the attrubute name and fill in the length.
   348 // JSR202|   *Annotations_attribute {
   349 // JSR202|     u2 attribute_name_index;
   350 // JSR202|     u4 attribute_length;
   351 // JSR202|     ...
   352 // JSR202|   }
   353 void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
   354                                                               AnnotationArray* annos) {
   355   u4 length = annos->length();
   356   write_attribute_name_index(attr_name);
   357   write_u4(length);
   358   memcpy(writeable_address(length), annos->adr_at(0), length);
   359 }
   361 //  BootstrapMethods_attribute {
   362 //    u2 attribute_name_index;
   363 //    u4 attribute_length;
   364 //    u2 num_bootstrap_methods;
   365 //    {   u2 bootstrap_method_ref;
   366 //        u2 num_bootstrap_arguments;
   367 //        u2 bootstrap_arguments[num_bootstrap_arguments];
   368 //    } bootstrap_methods[num_bootstrap_methods];
   369 //  }
   370 void JvmtiClassFileReconstituter::write_bootstrapmethod_attribute() {
   371   Array<u2>* operands = cpool()->operands();
   372   write_attribute_name_index("BootstrapMethods");
   373   int num_bootstrap_methods = ConstantPool::operand_array_length(operands);
   375   // calculate length of attribute
   376   int length = sizeof(u2); // num_bootstrap_methods
   377   for (int n = 0; n < num_bootstrap_methods; n++) {
   378     u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
   379     length += sizeof(u2); // bootstrap_method_ref
   380     length += sizeof(u2); // num_bootstrap_arguments
   381     length += sizeof(u2) * num_bootstrap_arguments; // bootstrap_arguments[num_bootstrap_arguments]
   382   }
   383   write_u4(length);
   385   // write attribute
   386   write_u2(num_bootstrap_methods);
   387   for (int n = 0; n < num_bootstrap_methods; n++) {
   388     u2 bootstrap_method_ref = cpool()->operand_bootstrap_method_ref_index_at(n);
   389     u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
   390     write_u2(bootstrap_method_ref);
   391     write_u2(num_bootstrap_arguments);
   392     for (int arg = 0; arg < num_bootstrap_arguments; arg++) {
   393       u2 bootstrap_argument = cpool()->operand_argument_index_at(n, arg);
   394       write_u2(bootstrap_argument);
   395     }
   396   }
   397 }
   400 // Write InnerClasses attribute
   401 // JVMSpec|   InnerClasses_attribute {
   402 // JVMSpec|     u2 attribute_name_index;
   403 // JVMSpec|     u4 attribute_length;
   404 // JVMSpec|     u2 number_of_classes;
   405 // JVMSpec|     {  u2 inner_class_info_index;
   406 // JVMSpec|        u2 outer_class_info_index;
   407 // JVMSpec|        u2 inner_name_index;
   408 // JVMSpec|        u2 inner_class_access_flags;
   409 // JVMSpec|     } classes[number_of_classes];
   410 // JVMSpec|   }
   411 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
   412   InnerClassesIterator iter(ikh());
   413   guarantee(iter.length() != 0 && iter.length() == length,
   414             "caller must check");
   415   u2 entry_count = length / InstanceKlass::inner_class_next_offset;
   416   u4 size = 2 + entry_count * (2+2+2+2);
   418   write_attribute_name_index("InnerClasses");
   419   write_u4(size);
   420   write_u2(entry_count);
   421   for (; !iter.done(); iter.next()) {
   422     write_u2(iter.inner_class_info_index());
   423     write_u2(iter.outer_class_info_index());
   424     write_u2(iter.inner_name_index());
   425     write_u2(iter.inner_access_flags());
   426   }
   427 }
   429 // Write Synthetic attribute
   430 // JVMSpec|   Synthetic_attribute {
   431 // JVMSpec|     u2 attribute_name_index;
   432 // JVMSpec|     u4 attribute_length;
   433 // JVMSpec|   }
   434 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
   435   write_attribute_name_index("Synthetic");
   436   write_u4(0); //length always zero
   437 }
   439 // Compute size of LineNumberTable
   440 u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) {
   441   // The line number table is compressed so we don't know how big it is until decompressed.
   442   // Decompression is really fast so we just do it twice.
   443   u2 num_entries = 0;
   444   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
   445   while (stream.read_pair()) {
   446     num_entries++;
   447   }
   448   return num_entries;
   449 }
   451 // Write LineNumberTable attribute
   452 // JVMSpec|   LineNumberTable_attribute {
   453 // JVMSpec|     u2 attribute_name_index;
   454 // JVMSpec|     u4 attribute_length;
   455 // JVMSpec|     u2 line_number_table_length;
   456 // JVMSpec|     {  u2 start_pc;
   457 // JVMSpec|        u2 line_number;
   458 // JVMSpec|     } line_number_table[line_number_table_length];
   459 // JVMSpec|   }
   460 void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method,
   461                                                                     u2 num_entries) {
   463   write_attribute_name_index("LineNumberTable");
   464   write_u4(2 + num_entries * (2 + 2));
   465   write_u2(num_entries);
   467   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
   468   while (stream.read_pair()) {
   469     write_u2(stream.bci());
   470     write_u2(stream.line());
   471   }
   472 }
   474 // Write LocalVariableTable attribute
   475 // JVMSpec|   LocalVariableTable_attribute {
   476 // JVMSpec|     u2 attribute_name_index;
   477 // JVMSpec|     u4 attribute_length;
   478 // JVMSpec|     u2 local_variable_table_length;
   479 // JVMSpec|     {  u2 start_pc;
   480 // JVMSpec|       u2 length;
   481 // JVMSpec|       u2 name_index;
   482 // JVMSpec|       u2 descriptor_index;
   483 // JVMSpec|       u2 index;
   484 // JVMSpec|     } local_variable_table[local_variable_table_length];
   485 // JVMSpec|   }
   486 void JvmtiClassFileReconstituter::write_local_variable_table_attribute(methodHandle method, u2 num_entries) {
   487     write_attribute_name_index("LocalVariableTable");
   488     write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
   489     write_u2(num_entries);
   491     assert(method->localvariable_table_length() == num_entries, "just checking");
   493     LocalVariableTableElement *elem = method->localvariable_table_start();
   494     for (int j=0; j<method->localvariable_table_length(); j++) {
   495       write_u2(elem->start_bci);
   496       write_u2(elem->length);
   497       write_u2(elem->name_cp_index);
   498       write_u2(elem->descriptor_cp_index);
   499       write_u2(elem->slot);
   500       elem++;
   501     }
   502 }
   504 // Write LocalVariableTypeTable attribute
   505 // JVMSpec|   LocalVariableTypeTable_attribute {
   506 // JVMSpec|     u2 attribute_name_index;
   507 // JVMSpec|     u4 attribute_length;
   508 // JVMSpec|     u2 local_variable_type_table_length;
   509 // JVMSpec|     { u2 start_pc;
   510 // JVMSpec|       u2 length;
   511 // JVMSpec|       u2 name_index;
   512 // JVMSpec|       u2 signature_index;
   513 // JVMSpec|       u2 index;
   514 // JVMSpec|     } local_variable_type_table[local_variable_type_table_length];
   515 // JVMSpec|   }
   516 void JvmtiClassFileReconstituter::write_local_variable_type_table_attribute(methodHandle method, u2 num_entries) {
   517     write_attribute_name_index("LocalVariableTypeTable");
   518     write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
   519     write_u2(num_entries);
   521     LocalVariableTableElement *elem = method->localvariable_table_start();
   522     for (int j=0; j<method->localvariable_table_length(); j++) {
   523       if (elem->signature_cp_index > 0) {
   524         // Local variable has a generic signature - write LVTT attribute entry
   525         write_u2(elem->start_bci);
   526         write_u2(elem->length);
   527         write_u2(elem->name_cp_index);
   528         write_u2(elem->signature_cp_index);
   529         write_u2(elem->slot);
   530         num_entries--;
   531       }
   532       elem++;
   533     }
   534     assert(num_entries == 0, "just checking");
   535 }
   537 // Write stack map table attribute
   538 // JSR-202|   StackMapTable_attribute {
   539 // JSR-202|     u2 attribute_name_index;
   540 // JSR-202|     u4 attribute_length;
   541 // JSR-202|     u2 number_of_entries;
   542 // JSR-202|     stack_map_frame_entries[number_of_entries];
   543 // JSR-202|   }
   544 void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method,
   545                                                                  int stackmap_len) {
   547   write_attribute_name_index("StackMapTable");
   548   write_u4(stackmap_len);
   549   memcpy(
   550     writeable_address(stackmap_len),
   551     (void*)(method->stackmap_data()->adr_at(0)),
   552     stackmap_len);
   553 }
   555 // Write one method_info structure
   556 // JVMSpec|   method_info {
   557 // JVMSpec|     u2 access_flags;
   558 // JVMSpec|     u2 name_index;
   559 // JVMSpec|     u2 descriptor_index;
   560 // JVMSpec|     u2 attributes_count;
   561 // JVMSpec|     attribute_info attributes[attributes_count];
   562 // JVMSpec|   }
   563 void JvmtiClassFileReconstituter::write_method_info(methodHandle method) {
   564   AccessFlags access_flags = method->access_flags();
   565   ConstMethod* const_method = method->constMethod();
   566   u2 generic_signature_index = const_method->generic_signature_index();
   567   AnnotationArray* anno = method->annotations();
   568   AnnotationArray* param_anno = method->parameter_annotations();
   569   AnnotationArray* default_anno = method->annotation_default();
   570   AnnotationArray* type_anno = method->type_annotations();
   572   // skip generated default interface methods
   573   if (method->is_overpass()) {
   574     return;
   575   }
   577   write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS);
   578   write_u2(const_method->name_index());
   579   write_u2(const_method->signature_index());
   581   // write attributes in the same order javac does, so we can test with byte for
   582   // byte comparison
   583   int attr_count = 0;
   584   if (const_method->code_size() != 0) {
   585     ++attr_count;     // has Code attribute
   586   }
   587   if (const_method->has_checked_exceptions()) {
   588     ++attr_count;     // has Exceptions attribute
   589   }
   590   if (default_anno != NULL) {
   591     ++attr_count;     // has AnnotationDefault attribute
   592   }
   593   // Deprecated attribute would go here
   594   if (access_flags.is_synthetic()) { // FIXME
   595     // ++attr_count;
   596   }
   597   if (generic_signature_index != 0) {
   598     ++attr_count;
   599   }
   600   if (anno != NULL) {
   601     ++attr_count;     // has RuntimeVisibleAnnotations attribute
   602   }
   603   if (param_anno != NULL) {
   604     ++attr_count;     // has RuntimeVisibleParameterAnnotations attribute
   605   }
   606   if (type_anno != NULL) {
   607     ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
   608   }
   610   write_u2(attr_count);
   611   if (const_method->code_size() > 0) {
   612     write_code_attribute(method);
   613   }
   614   if (const_method->has_checked_exceptions()) {
   615     write_exceptions_attribute(const_method);
   616   }
   617   if (default_anno != NULL) {
   618     write_annotations_attribute("AnnotationDefault", default_anno);
   619   }
   620   // Deprecated attribute would go here
   621   if (access_flags.is_synthetic()) {
   622     // write_synthetic_attribute();
   623   }
   624   if (generic_signature_index != 0) {
   625     write_signature_attribute(generic_signature_index);
   626   }
   627   if (anno != NULL) {
   628     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   629   }
   630   if (param_anno != NULL) {
   631     write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
   632   }
   633   if (type_anno != NULL) {
   634     write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
   635   }
   636 }
   638 // Write the class attributes portion of ClassFile structure
   639 // JVMSpec|     u2 attributes_count;
   640 // JVMSpec|     attribute_info attributes[attributes_count];
   641 void JvmtiClassFileReconstituter::write_class_attributes() {
   642   u2 inner_classes_length = inner_classes_attribute_length();
   643   Symbol* generic_signature = ikh()->generic_signature();
   644   AnnotationArray* anno = ikh()->class_annotations();
   645   AnnotationArray* type_anno = ikh()->class_type_annotations();
   647   int attr_count = 0;
   648   if (generic_signature != NULL) {
   649     ++attr_count;
   650   }
   651   if (ikh()->source_file_name() != NULL) {
   652     ++attr_count;
   653   }
   654   if (ikh()->source_debug_extension() != NULL) {
   655     ++attr_count;
   656   }
   657   if (inner_classes_length > 0) {
   658     ++attr_count;
   659   }
   660   if (anno != NULL) {
   661     ++attr_count;     // has RuntimeVisibleAnnotations attribute
   662   }
   663   if (type_anno != NULL) {
   664     ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
   665   }
   666   if (cpool()->operands() != NULL) {
   667     ++attr_count;
   668   }
   670   write_u2(attr_count);
   672   if (generic_signature != NULL) {
   673     write_signature_attribute(symbol_to_cpool_index(generic_signature));
   674   }
   675   if (ikh()->source_file_name() != NULL) {
   676     write_source_file_attribute();
   677   }
   678   if (ikh()->source_debug_extension() != NULL) {
   679     write_source_debug_extension_attribute();
   680   }
   681   if (inner_classes_length > 0) {
   682     write_inner_classes_attribute(inner_classes_length);
   683   }
   684   if (anno != NULL) {
   685     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
   686   }
   687   if (type_anno != NULL) {
   688     write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
   689   }
   690   if (cpool()->operands() != NULL) {
   691     write_bootstrapmethod_attribute();
   692   }
   693 }
   695 // Write the method information portion of ClassFile structure
   696 // JVMSpec|     u2 methods_count;
   697 // JVMSpec|     method_info methods[methods_count];
   698 void JvmtiClassFileReconstituter::write_method_infos() {
   699   HandleMark hm(thread());
   700   Array<Method*>* methods = ikh()->methods();
   701   int num_methods = methods->length();
   702   int num_overpass = 0;
   704   // count the generated default interface methods
   705   // these will not be re-created by write_method_info
   706   // and should not be included in the total count
   707   for (int index = 0; index < num_methods; index++) {
   708     Method* method = methods->at(index);
   709     if (method->is_overpass()) {
   710       num_overpass++;
   711     }
   712   }
   714   write_u2(num_methods - num_overpass);
   715   if (JvmtiExport::can_maintain_original_method_order()) {
   716     int index;
   717     int original_index;
   718     intArray method_order(num_methods, 0);
   720     // invert the method order mapping
   721     for (index = 0; index < num_methods; index++) {
   722       original_index = ikh()->method_ordering()->at(index);
   723       assert(original_index >= 0 && original_index < num_methods,
   724              "invalid original method index");
   725       method_order.at_put(original_index, index);
   726     }
   728     // write in original order
   729     for (original_index = 0; original_index < num_methods; original_index++) {
   730       index = method_order.at(original_index);
   731       methodHandle method(thread(), methods->at(index));
   732       write_method_info(method);
   733     }
   734   } else {
   735     // method order not preserved just dump the method infos
   736     for (int index = 0; index < num_methods; index++) {
   737       methodHandle method(thread(), methods->at(index));
   738       write_method_info(method);
   739     }
   740   }
   741 }
   743 void JvmtiClassFileReconstituter::write_class_file_format() {
   744   ReallocMark();
   746   // JVMSpec|   ClassFile {
   747   // JVMSpec|           u4 magic;
   748   write_u4(0xCAFEBABE);
   750   // JVMSpec|           u2 minor_version;
   751   // JVMSpec|           u2 major_version;
   752   write_u2(ikh()->minor_version());
   753   u2 major = ikh()->major_version();
   754   write_u2(major);
   756   // JVMSpec|           u2 constant_pool_count;
   757   // JVMSpec|           cp_info constant_pool[constant_pool_count-1];
   758   write_u2(cpool()->length());
   759   copy_cpool_bytes(writeable_address(cpool_size()));
   761   // JVMSpec|           u2 access_flags;
   762   write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
   764   // JVMSpec|           u2 this_class;
   765   // JVMSpec|           u2 super_class;
   766   write_u2(class_symbol_to_cpool_index(ikh()->name()));
   767   Klass* super_class = ikh()->super();
   768   write_u2(super_class == NULL? 0 :  // zero for java.lang.Object
   769                 class_symbol_to_cpool_index(super_class->name()));
   771   // JVMSpec|           u2 interfaces_count;
   772   // JVMSpec|           u2 interfaces[interfaces_count];
   773   Array<Klass*>* interfaces =  ikh()->local_interfaces();
   774   int num_interfaces = interfaces->length();
   775   write_u2(num_interfaces);
   776   for (int index = 0; index < num_interfaces; index++) {
   777     HandleMark hm(thread());
   778     instanceKlassHandle iikh(thread(), interfaces->at(index));
   779     write_u2(class_symbol_to_cpool_index(iikh->name()));
   780   }
   782   // JVMSpec|           u2 fields_count;
   783   // JVMSpec|           field_info fields[fields_count];
   784   write_field_infos();
   786   // JVMSpec|           u2 methods_count;
   787   // JVMSpec|           method_info methods[methods_count];
   788   write_method_infos();
   790   // JVMSpec|           u2 attributes_count;
   791   // JVMSpec|           attribute_info attributes[attributes_count];
   792   // JVMSpec|   } /* end ClassFile 8?
   793   write_class_attributes();
   794 }
   796 address JvmtiClassFileReconstituter::writeable_address(size_t size) {
   797   size_t used_size = _buffer_ptr - _buffer;
   798   if (size + used_size >= _buffer_size) {
   799     // compute the new buffer size: must be at least twice as big as before
   800     // plus whatever new is being used; then convert to nice clean block boundary
   801     size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size
   802                                                          * initial_buffer_size;
   804     // VM goes belly-up if the memory isn't available, so cannot do OOM processing
   805     _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size);
   806     _buffer_size = new_buffer_size;
   807     _buffer_ptr = _buffer + used_size;
   808   }
   809   u1* ret_ptr = _buffer_ptr;
   810   _buffer_ptr += size;
   811   return ret_ptr;
   812 }
   814 void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) {
   815   TempNewSymbol sym = SymbolTable::probe(name, (int)strlen(name));
   816   assert(sym != NULL, "attribute name symbol not found");
   817   u2 attr_name_index = symbol_to_cpool_index(sym);
   818   assert(attr_name_index != 0, "attribute name symbol not in constant pool");
   819   write_u2(attr_name_index);
   820 }
   822 void JvmtiClassFileReconstituter::write_u1(u1 x) {
   823   *writeable_address(1) = x;
   824 }
   826 void JvmtiClassFileReconstituter::write_u2(u2 x) {
   827   Bytes::put_Java_u2(writeable_address(2), x);
   828 }
   830 void JvmtiClassFileReconstituter::write_u4(u4 x) {
   831   Bytes::put_Java_u4(writeable_address(4), x);
   832 }
   834 void JvmtiClassFileReconstituter::write_u8(u8 x) {
   835   Bytes::put_Java_u8(writeable_address(8), x);
   836 }
   838 void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh,
   839                                                  unsigned char* bytecodes) {
   840   // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes
   841   // and the breakpoint bytecode are converted to their original bytecodes.
   843   BytecodeStream bs(mh);
   845   unsigned char* p = bytecodes;
   846   Bytecodes::Code code;
   847   bool is_rewritten = mh->method_holder()->is_rewritten();
   849   while ((code = bs.next()) >= 0) {
   850     assert(Bytecodes::is_java_code(code), "sanity check");
   851     assert(code != Bytecodes::_breakpoint, "sanity check");
   853     // length of bytecode (mnemonic + operands)
   854     address bcp = bs.bcp();
   855     int     len = bs.instruction_size();
   856     assert(len > 0, "length must be > 0");
   858     // copy the bytecodes
   859     *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
   860     if (len > 1) {
   861       memcpy(p+1, bcp+1, len-1);
   862     }
   864     // During linking the get/put and invoke instructions are rewritten
   865     // with an index into the constant pool cache. The original constant
   866     // pool index must be returned to caller.  Rewrite the index.
   867     if (is_rewritten && len > 1) {
   868       bool is_wide = false;
   869       switch (code) {
   870       case Bytecodes::_getstatic       :  // fall through
   871       case Bytecodes::_putstatic       :  // fall through
   872       case Bytecodes::_getfield        :  // fall through
   873       case Bytecodes::_putfield        :  // fall through
   874       case Bytecodes::_invokevirtual   :  // fall through
   875       case Bytecodes::_invokespecial   :  // fall through
   876       case Bytecodes::_invokestatic    :  // fall through
   877       case Bytecodes::_invokedynamic   :  // fall through
   878       case Bytecodes::_invokeinterface : {
   879         assert(len == 3 ||
   880                (code == Bytecodes::_invokeinterface && len == 5) ||
   881                (code == Bytecodes::_invokedynamic   && len == 5),
   882                "sanity check");
   884         int cpci = Bytes::get_native_u2(bcp+1);
   885         bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic);
   886         ConstantPoolCacheEntry* entry;
   887         if (is_invokedynamic) {
   888           cpci = Bytes::get_native_u4(bcp+1);
   889           entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci);
   890         } else {
   891         // cache cannot be pre-fetched since some classes won't have it yet
   892           entry = mh->constants()->cache()->entry_at(cpci);
   893         }
   894         int i = entry->constant_pool_index();
   895         assert(i < mh->constants()->length(), "sanity check");
   896         Bytes::put_Java_u2((address)(p+1), (u2)i);     // java byte ordering
   897         if (is_invokedynamic)  *(p+3) = *(p+4) = 0;
   898         break;
   899       }
   900       case Bytecodes::_ldc_w:
   901         is_wide = true; // fall through
   902       case Bytecodes::_ldc: {
   903         if (bs.raw_code() == Bytecodes::_fast_aldc || bs.raw_code() == Bytecodes::_fast_aldc_w) {
   904           int cpci = is_wide ? Bytes::get_native_u2(bcp+1) : (u1)(*(bcp+1));
   905           int i = mh->constants()->object_to_cp_index(cpci);
   906           assert(i < mh->constants()->length(), "sanity check");
   907           if (is_wide) {
   908             Bytes::put_Java_u2((address)(p+1), (u2)i);     // java byte ordering
   909           } else {
   910             *(p+1) = (u1)i;
   911           }
   912         }
   913         break;
   914         }
   915       }
   916     }
   918     p += len;
   919   }
   920 }

mercurial