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