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