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