src/hotspot/share/prims/jvmtiClassFileReconstituter.cpp
changeset 47216 71c04702a3d5
parent 46630 75aa3e39d02c
child 49449 ef5d5d343e2a
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2005, 2017, 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  */
       
    24 
       
    25 #include "precompiled.hpp"
       
    26 #include "classfile/symbolTable.hpp"
       
    27 #include "interpreter/bytecodeStream.hpp"
       
    28 #include "oops/fieldStreams.hpp"
       
    29 #include "prims/jvmtiClassFileReconstituter.hpp"
       
    30 #include "runtime/signature.hpp"
       
    31 #include "utilities/bytes.hpp"
       
    32 
       
    33 // FIXME: add Deprecated attribute
       
    34 // FIXME: fix Synthetic attribute
       
    35 // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
       
    36 
       
    37 
       
    38 // Write the field information portion of ClassFile structure
       
    39 // JVMSpec|     u2 fields_count;
       
    40 // JVMSpec|     field_info fields[fields_count];
       
    41 void JvmtiClassFileReconstituter::write_field_infos() {
       
    42   HandleMark hm(thread());
       
    43   Array<AnnotationArray*>* fields_anno = ik()->fields_annotations();
       
    44   Array<AnnotationArray*>* fields_type_anno = ik()->fields_type_annotations();
       
    45 
       
    46   // Compute the real number of Java fields
       
    47   int java_fields = ik()->java_fields_count();
       
    48 
       
    49   write_u2(java_fields);
       
    50   for (JavaFieldStream fs(ik()); !fs.done(); fs.next()) {
       
    51     AccessFlags access_flags = fs.access_flags();
       
    52     int name_index = fs.name_index();
       
    53     int signature_index = fs.signature_index();
       
    54     int initial_value_index = fs.initval_index();
       
    55     guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
       
    56     // int offset = ik()->field_offset( index );
       
    57     int generic_signature_index = fs.generic_signature_index();
       
    58     AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index());
       
    59     AnnotationArray* type_anno = fields_type_anno == NULL ? NULL : fields_type_anno->at(fs.index());
       
    60 
       
    61     // JVMSpec|   field_info {
       
    62     // JVMSpec|         u2 access_flags;
       
    63     // JVMSpec|         u2 name_index;
       
    64     // JVMSpec|         u2 descriptor_index;
       
    65     // JVMSpec|         u2 attributes_count;
       
    66     // JVMSpec|         attribute_info attributes[attributes_count];
       
    67     // JVMSpec|   }
       
    68 
       
    69     write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
       
    70     write_u2(name_index);
       
    71     write_u2(signature_index);
       
    72     int attr_count = 0;
       
    73     if (initial_value_index != 0) {
       
    74       ++attr_count;
       
    75     }
       
    76     if (access_flags.is_synthetic()) {
       
    77       // ++attr_count;
       
    78     }
       
    79     if (generic_signature_index != 0) {
       
    80       ++attr_count;
       
    81     }
       
    82     if (anno != NULL) {
       
    83       ++attr_count;     // has RuntimeVisibleAnnotations attribute
       
    84     }
       
    85     if (type_anno != NULL) {
       
    86       ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
       
    87     }
       
    88 
       
    89     write_u2(attr_count);
       
    90 
       
    91     if (initial_value_index != 0) {
       
    92       write_attribute_name_index("ConstantValue");
       
    93       write_u4(2); //length always 2
       
    94       write_u2(initial_value_index);
       
    95     }
       
    96     if (access_flags.is_synthetic()) {
       
    97       // write_synthetic_attribute();
       
    98     }
       
    99     if (generic_signature_index != 0) {
       
   100       write_signature_attribute(generic_signature_index);
       
   101     }
       
   102     if (anno != NULL) {
       
   103       write_annotations_attribute("RuntimeVisibleAnnotations", anno);
       
   104     }
       
   105     if (type_anno != NULL) {
       
   106       write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
       
   107     }
       
   108   }
       
   109 }
       
   110 
       
   111 // Write Code attribute
       
   112 // JVMSpec|   Code_attribute {
       
   113 // JVMSpec|     u2 attribute_name_index;
       
   114 // JVMSpec|     u4 attribute_length;
       
   115 // JVMSpec|     u2 max_stack;
       
   116 // JVMSpec|     u2 max_locals;
       
   117 // JVMSpec|     u4 code_length;
       
   118 // JVMSpec|     u1 code[code_length];
       
   119 // JVMSpec|     u2 exception_table_length;
       
   120 // JVMSpec|     {       u2 start_pc;
       
   121 // JVMSpec|             u2 end_pc;
       
   122 // JVMSpec|             u2  handler_pc;
       
   123 // JVMSpec|             u2  catch_type;
       
   124 // JVMSpec|     }       exception_table[exception_table_length];
       
   125 // JVMSpec|     u2 attributes_count;
       
   126 // JVMSpec|     attribute_info attributes[attributes_count];
       
   127 // JVMSpec|   }
       
   128 void JvmtiClassFileReconstituter::write_code_attribute(const methodHandle& method) {
       
   129   ConstMethod* const_method = method->constMethod();
       
   130   u2 line_num_cnt = 0;
       
   131   int stackmap_len = 0;
       
   132   int local_variable_table_length = 0;
       
   133   int local_variable_type_table_length = 0;
       
   134 
       
   135   // compute number and length of attributes
       
   136   int attr_count = 0;
       
   137   int attr_size = 0;
       
   138   if (const_method->has_linenumber_table()) {
       
   139     line_num_cnt = line_number_table_entries(method);
       
   140     if (line_num_cnt != 0) {
       
   141       ++attr_count;
       
   142       // Compute the complete size of the line number table attribute:
       
   143       //      LineNumberTable_attribute {
       
   144       //        u2 attribute_name_index;
       
   145       //        u4 attribute_length;
       
   146       //        u2 line_number_table_length;
       
   147       //        {  u2 start_pc;
       
   148       //           u2 line_number;
       
   149       //        } line_number_table[line_number_table_length];
       
   150       //      }
       
   151       attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2);
       
   152     }
       
   153   }
       
   154   if (method->has_stackmap_table()) {
       
   155     stackmap_len = method->stackmap_data()->length();
       
   156     if (stackmap_len != 0) {
       
   157       ++attr_count;
       
   158       // Compute the  size of the stack map table attribute (VM stores raw):
       
   159       //      StackMapTable_attribute {
       
   160       //        u2 attribute_name_index;
       
   161       //        u4 attribute_length;
       
   162       //        u2 number_of_entries;
       
   163       //        stack_map_frame_entries[number_of_entries];
       
   164       //      }
       
   165       attr_size += 2 + 4 + stackmap_len;
       
   166     }
       
   167   }
       
   168   if (method->has_localvariable_table()) {
       
   169     local_variable_table_length = method->localvariable_table_length();
       
   170     if (local_variable_table_length != 0) {
       
   171       ++attr_count;
       
   172       // Compute the size of the local variable table attribute (VM stores raw):
       
   173       // LocalVariableTable_attribute {
       
   174       //   u2 attribute_name_index;
       
   175       //   u4 attribute_length;
       
   176       //   u2 local_variable_table_length;
       
   177       //   {
       
   178       //     u2 start_pc;
       
   179       //     u2 length;
       
   180       //     u2 name_index;
       
   181       //     u2 descriptor_index;
       
   182       //     u2 index;
       
   183       //   }
       
   184       attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2);
       
   185 
       
   186       // Local variables with generic signatures must have LVTT entries
       
   187       LocalVariableTableElement *elem = method->localvariable_table_start();
       
   188       for (int idx = 0; idx < local_variable_table_length; idx++) {
       
   189         if (elem[idx].signature_cp_index != 0) {
       
   190           local_variable_type_table_length++;
       
   191         }
       
   192       }
       
   193 
       
   194       if (local_variable_type_table_length != 0) {
       
   195         ++attr_count;
       
   196         // Compute the size of the local variable type table attribute (VM stores raw):
       
   197         // LocalVariableTypeTable_attribute {
       
   198         //   u2 attribute_name_index;
       
   199         //   u4 attribute_length;
       
   200         //   u2 local_variable_type_table_length;
       
   201         //   {
       
   202         //     u2 start_pc;
       
   203         //     u2 length;
       
   204         //     u2 name_index;
       
   205         //     u2 signature_index;
       
   206         //     u2 index;
       
   207         //   }
       
   208         attr_size += 2 + 4 + 2 + local_variable_type_table_length * (2 + 2 + 2 + 2 + 2);
       
   209       }
       
   210     }
       
   211   }
       
   212 
       
   213   ExceptionTable exception_table(method());
       
   214   int exception_table_length = exception_table.length();
       
   215   int code_size = const_method->code_size();
       
   216   int size =
       
   217     2+2+4 +                                // max_stack, max_locals, code_length
       
   218     code_size +                            // code
       
   219     2 +                                    // exception_table_length
       
   220     (2+2+2+2) * exception_table_length +   // exception_table
       
   221     2 +                                    // attributes_count
       
   222     attr_size;                             // attributes
       
   223 
       
   224   write_attribute_name_index("Code");
       
   225   write_u4(size);
       
   226   write_u2(method->verifier_max_stack());
       
   227   write_u2(method->max_locals());
       
   228   write_u4(code_size);
       
   229   copy_bytecodes(method, (unsigned char*)writeable_address(code_size));
       
   230   write_u2(exception_table_length);
       
   231   for (int index = 0; index < exception_table_length; index++) {
       
   232     write_u2(exception_table.start_pc(index));
       
   233     write_u2(exception_table.end_pc(index));
       
   234     write_u2(exception_table.handler_pc(index));
       
   235     write_u2(exception_table.catch_type_index(index));
       
   236   }
       
   237   write_u2(attr_count);
       
   238   if (line_num_cnt != 0) {
       
   239     write_line_number_table_attribute(method, line_num_cnt);
       
   240   }
       
   241   if (stackmap_len != 0) {
       
   242     write_stackmap_table_attribute(method, stackmap_len);
       
   243   }
       
   244   if (local_variable_table_length != 0) {
       
   245     write_local_variable_table_attribute(method, local_variable_table_length);
       
   246   }
       
   247   if (local_variable_type_table_length != 0) {
       
   248     write_local_variable_type_table_attribute(method, local_variable_type_table_length);
       
   249   }
       
   250 }
       
   251 
       
   252 // Write Exceptions attribute
       
   253 // JVMSpec|   Exceptions_attribute {
       
   254 // JVMSpec|     u2 attribute_name_index;
       
   255 // JVMSpec|     u4 attribute_length;
       
   256 // JVMSpec|     u2 number_of_exceptions;
       
   257 // JVMSpec|     u2 exception_index_table[number_of_exceptions];
       
   258 // JVMSpec|   }
       
   259 void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) {
       
   260   CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start();
       
   261   int checked_exceptions_length = const_method->checked_exceptions_length();
       
   262   int size =
       
   263     2 +                                    // number_of_exceptions
       
   264     2 * checked_exceptions_length;         // exception_index_table
       
   265 
       
   266   write_attribute_name_index("Exceptions");
       
   267   write_u4(size);
       
   268   write_u2(checked_exceptions_length);
       
   269   for (int index = 0; index < checked_exceptions_length; index++) {
       
   270     write_u2(checked_exceptions[index].class_cp_index);
       
   271   }
       
   272 }
       
   273 
       
   274 // Write SourceFile attribute
       
   275 // JVMSpec|   SourceFile_attribute {
       
   276 // JVMSpec|     u2 attribute_name_index;
       
   277 // JVMSpec|     u4 attribute_length;
       
   278 // JVMSpec|     u2 sourcefile_index;
       
   279 // JVMSpec|   }
       
   280 void JvmtiClassFileReconstituter::write_source_file_attribute() {
       
   281   assert(ik()->source_file_name() != NULL, "caller must check");
       
   282 
       
   283   write_attribute_name_index("SourceFile");
       
   284   write_u4(2);  // always length 2
       
   285   write_u2(symbol_to_cpool_index(ik()->source_file_name()));
       
   286 }
       
   287 
       
   288 // Write SourceDebugExtension attribute
       
   289 // JSR45|   SourceDebugExtension_attribute {
       
   290 // JSR45|       u2 attribute_name_index;
       
   291 // JSR45|       u4 attribute_length;
       
   292 // JSR45|       u1 debug_extension[attribute_length];
       
   293 // JSR45|   }
       
   294 void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() {
       
   295   assert(ik()->source_debug_extension() != NULL, "caller must check");
       
   296 
       
   297   write_attribute_name_index("SourceDebugExtension");
       
   298   int len = (int)strlen(ik()->source_debug_extension());
       
   299   write_u4(len);
       
   300   u1* ext = (u1*)ik()->source_debug_extension();
       
   301   for (int i=0; i<len; i++) {
       
   302     write_u1(ext[i]);
       
   303   }
       
   304 }
       
   305 
       
   306 // Write (generic) Signature attribute
       
   307 // JVMSpec|   Signature_attribute {
       
   308 // JVMSpec|     u2 attribute_name_index;
       
   309 // JVMSpec|     u4 attribute_length;
       
   310 // JVMSpec|     u2 signature_index;
       
   311 // JVMSpec|   }
       
   312 void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) {
       
   313   write_attribute_name_index("Signature");
       
   314   write_u4(2);  // always length 2
       
   315   write_u2(generic_signature_index);
       
   316 }
       
   317 
       
   318 // Compute the number of entries in the InnerClasses attribute
       
   319 u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() {
       
   320   InnerClassesIterator iter(ik());
       
   321   return iter.length();
       
   322 }
       
   323 
       
   324 // Write an annotation attribute.  The VM stores them in raw form, so all we need
       
   325 // to do is add the attrubute name and fill in the length.
       
   326 // JSR202|   *Annotations_attribute {
       
   327 // JSR202|     u2 attribute_name_index;
       
   328 // JSR202|     u4 attribute_length;
       
   329 // JSR202|     ...
       
   330 // JSR202|   }
       
   331 void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
       
   332                                                               AnnotationArray* annos) {
       
   333   u4 length = annos->length();
       
   334   write_attribute_name_index(attr_name);
       
   335   write_u4(length);
       
   336   memcpy(writeable_address(length), annos->adr_at(0), length);
       
   337 }
       
   338 
       
   339 //  BootstrapMethods_attribute {
       
   340 //    u2 attribute_name_index;
       
   341 //    u4 attribute_length;
       
   342 //    u2 num_bootstrap_methods;
       
   343 //    {   u2 bootstrap_method_ref;
       
   344 //        u2 num_bootstrap_arguments;
       
   345 //        u2 bootstrap_arguments[num_bootstrap_arguments];
       
   346 //    } bootstrap_methods[num_bootstrap_methods];
       
   347 //  }
       
   348 void JvmtiClassFileReconstituter::write_bootstrapmethod_attribute() {
       
   349   Array<u2>* operands = cpool()->operands();
       
   350   write_attribute_name_index("BootstrapMethods");
       
   351   int num_bootstrap_methods = ConstantPool::operand_array_length(operands);
       
   352 
       
   353   // calculate length of attribute
       
   354   int length = sizeof(u2); // num_bootstrap_methods
       
   355   for (int n = 0; n < num_bootstrap_methods; n++) {
       
   356     u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
       
   357     length += sizeof(u2); // bootstrap_method_ref
       
   358     length += sizeof(u2); // num_bootstrap_arguments
       
   359     length += sizeof(u2) * num_bootstrap_arguments; // bootstrap_arguments[num_bootstrap_arguments]
       
   360   }
       
   361   write_u4(length);
       
   362 
       
   363   // write attribute
       
   364   write_u2(num_bootstrap_methods);
       
   365   for (int n = 0; n < num_bootstrap_methods; n++) {
       
   366     u2 bootstrap_method_ref = cpool()->operand_bootstrap_method_ref_index_at(n);
       
   367     u2 num_bootstrap_arguments = cpool()->operand_argument_count_at(n);
       
   368     write_u2(bootstrap_method_ref);
       
   369     write_u2(num_bootstrap_arguments);
       
   370     for (int arg = 0; arg < num_bootstrap_arguments; arg++) {
       
   371       u2 bootstrap_argument = cpool()->operand_argument_index_at(n, arg);
       
   372       write_u2(bootstrap_argument);
       
   373     }
       
   374   }
       
   375 }
       
   376 
       
   377 
       
   378 // Write InnerClasses attribute
       
   379 // JVMSpec|   InnerClasses_attribute {
       
   380 // JVMSpec|     u2 attribute_name_index;
       
   381 // JVMSpec|     u4 attribute_length;
       
   382 // JVMSpec|     u2 number_of_classes;
       
   383 // JVMSpec|     {  u2 inner_class_info_index;
       
   384 // JVMSpec|        u2 outer_class_info_index;
       
   385 // JVMSpec|        u2 inner_name_index;
       
   386 // JVMSpec|        u2 inner_class_access_flags;
       
   387 // JVMSpec|     } classes[number_of_classes];
       
   388 // JVMSpec|   }
       
   389 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
       
   390   InnerClassesIterator iter(ik());
       
   391   guarantee(iter.length() != 0 && iter.length() == length,
       
   392             "caller must check");
       
   393   u2 entry_count = length / InstanceKlass::inner_class_next_offset;
       
   394   u4 size = 2 + entry_count * (2+2+2+2);
       
   395 
       
   396   write_attribute_name_index("InnerClasses");
       
   397   write_u4(size);
       
   398   write_u2(entry_count);
       
   399   for (; !iter.done(); iter.next()) {
       
   400     write_u2(iter.inner_class_info_index());
       
   401     write_u2(iter.outer_class_info_index());
       
   402     write_u2(iter.inner_name_index());
       
   403     write_u2(iter.inner_access_flags());
       
   404   }
       
   405 }
       
   406 
       
   407 // Write Synthetic attribute
       
   408 // JVMSpec|   Synthetic_attribute {
       
   409 // JVMSpec|     u2 attribute_name_index;
       
   410 // JVMSpec|     u4 attribute_length;
       
   411 // JVMSpec|   }
       
   412 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
       
   413   write_attribute_name_index("Synthetic");
       
   414   write_u4(0); //length always zero
       
   415 }
       
   416 
       
   417 // Compute size of LineNumberTable
       
   418 u2 JvmtiClassFileReconstituter::line_number_table_entries(const methodHandle& method) {
       
   419   // The line number table is compressed so we don't know how big it is until decompressed.
       
   420   // Decompression is really fast so we just do it twice.
       
   421   u2 num_entries = 0;
       
   422   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
       
   423   while (stream.read_pair()) {
       
   424     num_entries++;
       
   425   }
       
   426   return num_entries;
       
   427 }
       
   428 
       
   429 // Write LineNumberTable attribute
       
   430 // JVMSpec|   LineNumberTable_attribute {
       
   431 // JVMSpec|     u2 attribute_name_index;
       
   432 // JVMSpec|     u4 attribute_length;
       
   433 // JVMSpec|     u2 line_number_table_length;
       
   434 // JVMSpec|     {  u2 start_pc;
       
   435 // JVMSpec|        u2 line_number;
       
   436 // JVMSpec|     } line_number_table[line_number_table_length];
       
   437 // JVMSpec|   }
       
   438 void JvmtiClassFileReconstituter::write_line_number_table_attribute(const methodHandle& method,
       
   439                                                                     u2 num_entries) {
       
   440 
       
   441   write_attribute_name_index("LineNumberTable");
       
   442   write_u4(2 + num_entries * (2 + 2));
       
   443   write_u2(num_entries);
       
   444 
       
   445   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
       
   446   while (stream.read_pair()) {
       
   447     write_u2(stream.bci());
       
   448     write_u2(stream.line());
       
   449   }
       
   450 }
       
   451 
       
   452 // Write LocalVariableTable attribute
       
   453 // JVMSpec|   LocalVariableTable_attribute {
       
   454 // JVMSpec|     u2 attribute_name_index;
       
   455 // JVMSpec|     u4 attribute_length;
       
   456 // JVMSpec|     u2 local_variable_table_length;
       
   457 // JVMSpec|     {  u2 start_pc;
       
   458 // JVMSpec|       u2 length;
       
   459 // JVMSpec|       u2 name_index;
       
   460 // JVMSpec|       u2 descriptor_index;
       
   461 // JVMSpec|       u2 index;
       
   462 // JVMSpec|     } local_variable_table[local_variable_table_length];
       
   463 // JVMSpec|   }
       
   464 void JvmtiClassFileReconstituter::write_local_variable_table_attribute(const methodHandle& method, u2 num_entries) {
       
   465     write_attribute_name_index("LocalVariableTable");
       
   466     write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
       
   467     write_u2(num_entries);
       
   468 
       
   469     assert(method->localvariable_table_length() == num_entries, "just checking");
       
   470 
       
   471     LocalVariableTableElement *elem = method->localvariable_table_start();
       
   472     for (int j=0; j<method->localvariable_table_length(); j++) {
       
   473       write_u2(elem->start_bci);
       
   474       write_u2(elem->length);
       
   475       write_u2(elem->name_cp_index);
       
   476       write_u2(elem->descriptor_cp_index);
       
   477       write_u2(elem->slot);
       
   478       elem++;
       
   479     }
       
   480 }
       
   481 
       
   482 // Write LocalVariableTypeTable attribute
       
   483 // JVMSpec|   LocalVariableTypeTable_attribute {
       
   484 // JVMSpec|     u2 attribute_name_index;
       
   485 // JVMSpec|     u4 attribute_length;
       
   486 // JVMSpec|     u2 local_variable_type_table_length;
       
   487 // JVMSpec|     { u2 start_pc;
       
   488 // JVMSpec|       u2 length;
       
   489 // JVMSpec|       u2 name_index;
       
   490 // JVMSpec|       u2 signature_index;
       
   491 // JVMSpec|       u2 index;
       
   492 // JVMSpec|     } local_variable_type_table[local_variable_type_table_length];
       
   493 // JVMSpec|   }
       
   494 void JvmtiClassFileReconstituter::write_local_variable_type_table_attribute(const methodHandle& method, u2 num_entries) {
       
   495     write_attribute_name_index("LocalVariableTypeTable");
       
   496     write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
       
   497     write_u2(num_entries);
       
   498 
       
   499     LocalVariableTableElement *elem = method->localvariable_table_start();
       
   500     for (int j=0; j<method->localvariable_table_length(); j++) {
       
   501       if (elem->signature_cp_index > 0) {
       
   502         // Local variable has a generic signature - write LVTT attribute entry
       
   503         write_u2(elem->start_bci);
       
   504         write_u2(elem->length);
       
   505         write_u2(elem->name_cp_index);
       
   506         write_u2(elem->signature_cp_index);
       
   507         write_u2(elem->slot);
       
   508         num_entries--;
       
   509       }
       
   510       elem++;
       
   511     }
       
   512     assert(num_entries == 0, "just checking");
       
   513 }
       
   514 
       
   515 // Write stack map table attribute
       
   516 // JSR-202|   StackMapTable_attribute {
       
   517 // JSR-202|     u2 attribute_name_index;
       
   518 // JSR-202|     u4 attribute_length;
       
   519 // JSR-202|     u2 number_of_entries;
       
   520 // JSR-202|     stack_map_frame_entries[number_of_entries];
       
   521 // JSR-202|   }
       
   522 void JvmtiClassFileReconstituter::write_stackmap_table_attribute(const methodHandle& method,
       
   523                                                                  int stackmap_len) {
       
   524 
       
   525   write_attribute_name_index("StackMapTable");
       
   526   write_u4(stackmap_len);
       
   527   memcpy(
       
   528     writeable_address(stackmap_len),
       
   529     (void*)(method->stackmap_data()->adr_at(0)),
       
   530     stackmap_len);
       
   531 }
       
   532 
       
   533 // Write one method_info structure
       
   534 // JVMSpec|   method_info {
       
   535 // JVMSpec|     u2 access_flags;
       
   536 // JVMSpec|     u2 name_index;
       
   537 // JVMSpec|     u2 descriptor_index;
       
   538 // JVMSpec|     u2 attributes_count;
       
   539 // JVMSpec|     attribute_info attributes[attributes_count];
       
   540 // JVMSpec|   }
       
   541 void JvmtiClassFileReconstituter::write_method_info(const methodHandle& method) {
       
   542   AccessFlags access_flags = method->access_flags();
       
   543   ConstMethod* const_method = method->constMethod();
       
   544   u2 generic_signature_index = const_method->generic_signature_index();
       
   545   AnnotationArray* anno = method->annotations();
       
   546   AnnotationArray* param_anno = method->parameter_annotations();
       
   547   AnnotationArray* default_anno = method->annotation_default();
       
   548   AnnotationArray* type_anno = method->type_annotations();
       
   549 
       
   550   // skip generated default interface methods
       
   551   if (method->is_overpass()) {
       
   552     return;
       
   553   }
       
   554 
       
   555   write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS);
       
   556   write_u2(const_method->name_index());
       
   557   write_u2(const_method->signature_index());
       
   558 
       
   559   // write attributes in the same order javac does, so we can test with byte for
       
   560   // byte comparison
       
   561   int attr_count = 0;
       
   562   if (const_method->code_size() != 0) {
       
   563     ++attr_count;     // has Code attribute
       
   564   }
       
   565   if (const_method->has_checked_exceptions()) {
       
   566     ++attr_count;     // has Exceptions attribute
       
   567   }
       
   568   if (default_anno != NULL) {
       
   569     ++attr_count;     // has AnnotationDefault attribute
       
   570   }
       
   571   // Deprecated attribute would go here
       
   572   if (access_flags.is_synthetic()) { // FIXME
       
   573     // ++attr_count;
       
   574   }
       
   575   if (generic_signature_index != 0) {
       
   576     ++attr_count;
       
   577   }
       
   578   if (anno != NULL) {
       
   579     ++attr_count;     // has RuntimeVisibleAnnotations attribute
       
   580   }
       
   581   if (param_anno != NULL) {
       
   582     ++attr_count;     // has RuntimeVisibleParameterAnnotations attribute
       
   583   }
       
   584   if (type_anno != NULL) {
       
   585     ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
       
   586   }
       
   587 
       
   588   write_u2(attr_count);
       
   589   if (const_method->code_size() > 0) {
       
   590     write_code_attribute(method);
       
   591   }
       
   592   if (const_method->has_checked_exceptions()) {
       
   593     write_exceptions_attribute(const_method);
       
   594   }
       
   595   if (default_anno != NULL) {
       
   596     write_annotations_attribute("AnnotationDefault", default_anno);
       
   597   }
       
   598   // Deprecated attribute would go here
       
   599   if (access_flags.is_synthetic()) {
       
   600     // write_synthetic_attribute();
       
   601   }
       
   602   if (generic_signature_index != 0) {
       
   603     write_signature_attribute(generic_signature_index);
       
   604   }
       
   605   if (anno != NULL) {
       
   606     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
       
   607   }
       
   608   if (param_anno != NULL) {
       
   609     write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
       
   610   }
       
   611   if (type_anno != NULL) {
       
   612     write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
       
   613   }
       
   614 }
       
   615 
       
   616 // Write the class attributes portion of ClassFile structure
       
   617 // JVMSpec|     u2 attributes_count;
       
   618 // JVMSpec|     attribute_info attributes[attributes_count];
       
   619 void JvmtiClassFileReconstituter::write_class_attributes() {
       
   620   u2 inner_classes_length = inner_classes_attribute_length();
       
   621   Symbol* generic_signature = ik()->generic_signature();
       
   622   AnnotationArray* anno = ik()->class_annotations();
       
   623   AnnotationArray* type_anno = ik()->class_type_annotations();
       
   624 
       
   625   int attr_count = 0;
       
   626   if (generic_signature != NULL) {
       
   627     ++attr_count;
       
   628   }
       
   629   if (ik()->source_file_name() != NULL) {
       
   630     ++attr_count;
       
   631   }
       
   632   if (ik()->source_debug_extension() != NULL) {
       
   633     ++attr_count;
       
   634   }
       
   635   if (inner_classes_length > 0) {
       
   636     ++attr_count;
       
   637   }
       
   638   if (anno != NULL) {
       
   639     ++attr_count;     // has RuntimeVisibleAnnotations attribute
       
   640   }
       
   641   if (type_anno != NULL) {
       
   642     ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
       
   643   }
       
   644   if (cpool()->operands() != NULL) {
       
   645     ++attr_count;
       
   646   }
       
   647 
       
   648   write_u2(attr_count);
       
   649 
       
   650   if (generic_signature != NULL) {
       
   651     write_signature_attribute(symbol_to_cpool_index(generic_signature));
       
   652   }
       
   653   if (ik()->source_file_name() != NULL) {
       
   654     write_source_file_attribute();
       
   655   }
       
   656   if (ik()->source_debug_extension() != NULL) {
       
   657     write_source_debug_extension_attribute();
       
   658   }
       
   659   if (inner_classes_length > 0) {
       
   660     write_inner_classes_attribute(inner_classes_length);
       
   661   }
       
   662   if (anno != NULL) {
       
   663     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
       
   664   }
       
   665   if (type_anno != NULL) {
       
   666     write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
       
   667   }
       
   668   if (cpool()->operands() != NULL) {
       
   669     write_bootstrapmethod_attribute();
       
   670   }
       
   671 }
       
   672 
       
   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 = ik()->methods();
       
   679   int num_methods = methods->length();
       
   680   int num_overpass = 0;
       
   681 
       
   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   }
       
   691 
       
   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, num_methods, 0);
       
   697 
       
   698     // invert the method order mapping
       
   699     for (index = 0; index < num_methods; index++) {
       
   700       original_index = ik()->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     }
       
   705 
       
   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 }
       
   720 
       
   721 void JvmtiClassFileReconstituter::write_class_file_format() {
       
   722   ReallocMark();
       
   723 
       
   724   // JVMSpec|   ClassFile {
       
   725   // JVMSpec|           u4 magic;
       
   726   write_u4(0xCAFEBABE);
       
   727 
       
   728   // JVMSpec|           u2 minor_version;
       
   729   // JVMSpec|           u2 major_version;
       
   730   write_u2(ik()->minor_version());
       
   731   u2 major = ik()->major_version();
       
   732   write_u2(major);
       
   733 
       
   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()));
       
   738 
       
   739   // JVMSpec|           u2 access_flags;
       
   740   write_u2(ik()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
       
   741 
       
   742   // JVMSpec|           u2 this_class;
       
   743   // JVMSpec|           u2 super_class;
       
   744   write_u2(class_symbol_to_cpool_index(ik()->name()));
       
   745   Klass* super_class = ik()->super();
       
   746   write_u2(super_class == NULL? 0 :  // zero for java.lang.Object
       
   747                 class_symbol_to_cpool_index(super_class->name()));
       
   748 
       
   749   // JVMSpec|           u2 interfaces_count;
       
   750   // JVMSpec|           u2 interfaces[interfaces_count];
       
   751   Array<Klass*>* interfaces =  ik()->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     InstanceKlass* iik = InstanceKlass::cast(interfaces->at(index));
       
   757     write_u2(class_symbol_to_cpool_index(iik->name()));
       
   758   }
       
   759 
       
   760   // JVMSpec|           u2 fields_count;
       
   761   // JVMSpec|           field_info fields[fields_count];
       
   762   write_field_infos();
       
   763 
       
   764   // JVMSpec|           u2 methods_count;
       
   765   // JVMSpec|           method_info methods[methods_count];
       
   766   write_method_infos();
       
   767 
       
   768   // JVMSpec|           u2 attributes_count;
       
   769   // JVMSpec|           attribute_info attributes[attributes_count];
       
   770   // JVMSpec|   } /* end ClassFile 8?
       
   771   write_class_attributes();
       
   772 }
       
   773 
       
   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;
       
   781 
       
   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 }
       
   791 
       
   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 }
       
   799 
       
   800 void JvmtiClassFileReconstituter::write_u1(u1 x) {
       
   801   *writeable_address(1) = x;
       
   802 }
       
   803 
       
   804 void JvmtiClassFileReconstituter::write_u2(u2 x) {
       
   805   Bytes::put_Java_u2(writeable_address(2), x);
       
   806 }
       
   807 
       
   808 void JvmtiClassFileReconstituter::write_u4(u4 x) {
       
   809   Bytes::put_Java_u4(writeable_address(4), x);
       
   810 }
       
   811 
       
   812 void JvmtiClassFileReconstituter::write_u8(u8 x) {
       
   813   Bytes::put_Java_u8(writeable_address(8), x);
       
   814 }
       
   815 
       
   816 void JvmtiClassFileReconstituter::copy_bytecodes(const 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.
       
   820 
       
   821   BytecodeStream bs(mh);
       
   822 
       
   823   unsigned char* p = bytecodes;
       
   824   Bytecodes::Code code;
       
   825   bool is_rewritten = mh->method_holder()->is_rewritten();
       
   826 
       
   827   while ((code = bs.next()) >= 0) {
       
   828     assert(Bytecodes::is_java_code(code), "sanity check");
       
   829     assert(code != Bytecodes::_breakpoint, "sanity check");
       
   830 
       
   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");
       
   835 
       
   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     }
       
   841 
       
   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");
       
   861 
       
   862         int cpci = Bytes::get_native_u2(bcp+1);
       
   863         bool is_invokedynamic = (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       default:
       
   894         break;
       
   895       }
       
   896     }
       
   897 
       
   898     p += len;
       
   899   }
       
   900 }