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