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