< prev index next >

src/hotspot/share/prims/jvmtiClassFileReconstituter.cpp

Print this page

   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 "classfile/symbolTable.hpp"

  26 #include "interpreter/bytecodeStream.hpp"
  27 #include "memory/universe.hpp"
  28 #include "oops/bsmAttribute.inline.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;

 448 //  PermittedSubclasses {
 449 //    u2 attribute_name_index;
 450 //    u4 attribute_length;
 451 //    u2 number_of_classes;
 452 //    u2 classes[number_of_classes];
 453 //  }
 454 void JvmtiClassFileReconstituter::write_permitted_subclasses_attribute() {
 455   Array<u2>* permitted_subclasses = ik()->permitted_subclasses();
 456   int number_of_classes = permitted_subclasses->length();
 457   int length = sizeof(u2) * (1 + number_of_classes); // '1 +' is for number_of_classes field
 458 
 459   write_attribute_name_index("PermittedSubclasses");
 460   write_u4(length);
 461   write_u2(checked_cast<u2>(number_of_classes));
 462   for (int i = 0; i < number_of_classes; i++) {
 463     u2 class_cp_index = permitted_subclasses->at(i);
 464     write_u2(class_cp_index);
 465   }
 466 }
 467 




















 468 //  Record {
 469 //    u2 attribute_name_index;
 470 //    u4 attribute_length;
 471 //    u2 components_count;
 472 //    component_info components[components_count];
 473 //  }
 474 //  component_info {
 475 //    u2 name_index;
 476 //    u2 descriptor_index
 477 //    u2 attributes_count;
 478 //    attribute_info_attributes[attributes_count];
 479 //  }
 480 void JvmtiClassFileReconstituter::write_record_attribute() {
 481   Array<RecordComponent*>* components = ik()->record_components();
 482   int number_of_components = components->length();
 483 
 484   // Each component has a u2 for name, descr, attribute count
 485   u4 length = checked_cast<u4>(sizeof(u2) + (sizeof(u2) * 3 * number_of_components));
 486   for (int x = 0; x < number_of_components; x++) {
 487     RecordComponent* component = components->at(x);

 528 // JVMSpec|     {  u2 inner_class_info_index;
 529 // JVMSpec|        u2 outer_class_info_index;
 530 // JVMSpec|        u2 inner_name_index;
 531 // JVMSpec|        u2 inner_class_access_flags;
 532 // JVMSpec|     } classes[number_of_classes];
 533 // JVMSpec|   }
 534 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
 535   InnerClassesIterator iter(ik());
 536   guarantee(iter.length() != 0 && iter.length() == length,
 537             "caller must check");
 538   u2 entry_count = checked_cast<u2>(length / InstanceKlass::inner_class_next_offset);
 539   u4 size = 2 + entry_count * (2+2+2+2);
 540 
 541   write_attribute_name_index("InnerClasses");
 542   write_u4(size);
 543   write_u2(entry_count);
 544   for (; !iter.done(); iter.next()) {
 545     write_u2(iter.inner_class_info_index());
 546     write_u2(iter.outer_class_info_index());
 547     write_u2(iter.inner_name_index());
 548     write_u2(iter.inner_access_flags());





 549   }
 550 }
 551 
 552 // Write Synthetic attribute
 553 // JVMSpec|   Synthetic_attribute {
 554 // JVMSpec|     u2 attribute_name_index;
 555 // JVMSpec|     u4 attribute_length;
 556 // JVMSpec|   }
 557 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
 558   write_attribute_name_index("Synthetic");
 559   write_u4(0); //length always zero
 560 }
 561 
 562 // Compute size of LineNumberTable
 563 u2 JvmtiClassFileReconstituter::line_number_table_entries(const methodHandle& method) {
 564   // The line number table is compressed so we don't know how big it is until decompressed.
 565   // Decompression is really fast so we just do it twice.
 566   u2 num_entries = 0;
 567   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
 568   while (stream.read_pair()) {

 787     ++attr_count;
 788   }
 789   if (anno != nullptr) {
 790     ++attr_count;     // has RuntimeVisibleAnnotations attribute
 791   }
 792   if (type_anno != nullptr) {
 793     ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
 794   }
 795   if (!cpool()->bsm_entries().is_empty()) {
 796     ++attr_count;
 797   }
 798   if (ik()->nest_host_index() != 0) {
 799     ++attr_count;
 800   }
 801   if (ik()->nest_members() != Universe::the_empty_short_array()) {
 802     ++attr_count;
 803   }
 804   if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
 805     ++attr_count;
 806   }



 807   if (ik()->record_components() != nullptr) {
 808     ++attr_count;
 809   }
 810 
 811   write_u2(attr_count);
 812 
 813   if (generic_signature != nullptr) {
 814     write_signature_attribute(symbol_to_cpool_index(generic_signature));
 815   }
 816   if (ik()->source_file_name() != nullptr) {
 817     write_source_file_attribute();
 818   }
 819   if (ik()->source_debug_extension() != nullptr) {
 820     write_source_debug_extension_attribute();
 821   }
 822   if (anno != nullptr) {
 823     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
 824   }
 825   if (type_anno != nullptr) {
 826     write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
 827   }
 828   if (ik()->nest_host_index() != 0) {
 829     write_nest_host_attribute();
 830   }
 831   if (ik()->nest_members() != Universe::the_empty_short_array()) {
 832     write_nest_members_attribute();
 833   }
 834   if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
 835     write_permitted_subclasses_attribute();
 836   }



 837   if (ik()->record_components() != nullptr) {
 838     write_record_attribute();
 839   }
 840   if (!cpool()->bsm_entries().is_empty()) {
 841     write_bootstrapmethod_attribute();
 842   }
 843   if (inner_classes_length > 0) {
 844     write_inner_classes_attribute(inner_classes_length);
 845   }
 846 }
 847 
 848 // Write the method information portion of ClassFile structure
 849 // JVMSpec|     u2 methods_count;
 850 // JVMSpec|     method_info methods[methods_count];
 851 void JvmtiClassFileReconstituter::write_method_infos() {
 852   HandleMark hm(thread());
 853   Array<Method*>* methods = ik()->methods();
 854   int num_methods = methods->length();
 855   int num_overpass = 0;
 856 

1011     // length of bytecode (mnemonic + operands)
1012     address bcp = bs.bcp();
1013     int     len = bs.instruction_size();
1014     assert(len > 0, "length must be > 0");
1015 
1016     // copy the bytecodes
1017     *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
1018     if (len > 1) {
1019       memcpy(p+1, bcp+1, len-1);
1020     }
1021 
1022     // During linking the get/put and invoke instructions are rewritten
1023     // with an index into the constant pool cache. The original constant
1024     // pool index must be returned to caller.  Rewrite the index.
1025     if (is_rewritten && len > 1) {
1026       bool is_wide = false;
1027       switch (code) {
1028       case Bytecodes::_getstatic       :  // fall through
1029       case Bytecodes::_putstatic       :  // fall through
1030       case Bytecodes::_getfield        :  // fall through
1031       case Bytecodes::_putfield        :  {
1032         int field_index = Bytes::get_native_u2(bcp+1);
1033         u2 pool_index = mh->constants()->resolved_field_entry_at(field_index)->constant_pool_index();
1034         assert(pool_index < mh->constants()->length(), "sanity check");
1035         Bytes::put_Java_u2((address)(p+1), pool_index);     // java byte ordering
1036         break;
1037       }
1038       case Bytecodes::_invokevirtual   :  // fall through
1039       case Bytecodes::_invokespecial   :  // fall through
1040       case Bytecodes::_invokestatic    :  // fall through
1041       case Bytecodes::_invokedynamic   :  // fall through
1042       case Bytecodes::_invokeinterface : {
1043         assert(len == 3 ||
1044                (code == Bytecodes::_invokeinterface && len == 5) ||
1045                (code == Bytecodes::_invokedynamic   && len == 5),
1046                "sanity check");
1047 
1048         int cpci = Bytes::get_native_u2(bcp+1);
1049         bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1050         int pool_index;
1051         if (is_invokedynamic) {

   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 "classfile/symbolTable.hpp"
  26 #include "classfile/vmClasses.hpp"
  27 #include "interpreter/bytecodeStream.hpp"
  28 #include "memory/universe.hpp"
  29 #include "oops/bsmAttribute.inline.hpp"
  30 #include "oops/constantPool.inline.hpp"
  31 #include "oops/fieldStreams.inline.hpp"
  32 #include "oops/instanceKlass.inline.hpp"
  33 #include "oops/recordComponent.hpp"
  34 #include "prims/jvmtiClassFileReconstituter.hpp"
  35 #include "runtime/handles.inline.hpp"
  36 #include "runtime/signature.hpp"
  37 #include "utilities/bytes.hpp"
  38 #include "utilities/checkedCast.hpp"
  39 
  40 // FIXME: add Deprecated attribute
  41 // FIXME: fix Synthetic attribute
  42 // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
  43 
  44 JvmtiConstantPoolReconstituter::JvmtiConstantPoolReconstituter(InstanceKlass* ik) {
  45   set_error(JVMTI_ERROR_NONE);
  46   _ik = ik;

 449 //  PermittedSubclasses {
 450 //    u2 attribute_name_index;
 451 //    u4 attribute_length;
 452 //    u2 number_of_classes;
 453 //    u2 classes[number_of_classes];
 454 //  }
 455 void JvmtiClassFileReconstituter::write_permitted_subclasses_attribute() {
 456   Array<u2>* permitted_subclasses = ik()->permitted_subclasses();
 457   int number_of_classes = permitted_subclasses->length();
 458   int length = sizeof(u2) * (1 + number_of_classes); // '1 +' is for number_of_classes field
 459 
 460   write_attribute_name_index("PermittedSubclasses");
 461   write_u4(length);
 462   write_u2(checked_cast<u2>(number_of_classes));
 463   for (int i = 0; i < number_of_classes; i++) {
 464     u2 class_cp_index = permitted_subclasses->at(i);
 465     write_u2(class_cp_index);
 466   }
 467 }
 468 
 469 // LoadableDescriptors {
 470 //   u2 attribute_name_index;
 471 //   u4 attribute_length;
 472 //   u2 number_of_descriptors;
 473 //   u2 descriptors[number_of_descriptors];
 474 // }
 475 void JvmtiClassFileReconstituter::write_loadable_descriptors_attribute() {
 476   Array<u2>* loadable_descriptors = ik()->loadable_descriptors();
 477   int number_of_descriptors = loadable_descriptors->length();
 478   int length = sizeof(u2) * (1 + number_of_descriptors); // '1 +' is for number_of_descriptors field
 479 
 480   write_attribute_name_index("LoadableDescriptors");
 481   write_u4(length);
 482   write_u2(checked_cast<u2>(number_of_descriptors));
 483   for (int i = 0; i < number_of_descriptors; i++) {
 484     u2 utf8_index = loadable_descriptors->at(i);
 485     write_u2(utf8_index);
 486   }
 487 }
 488 
 489 //  Record {
 490 //    u2 attribute_name_index;
 491 //    u4 attribute_length;
 492 //    u2 components_count;
 493 //    component_info components[components_count];
 494 //  }
 495 //  component_info {
 496 //    u2 name_index;
 497 //    u2 descriptor_index
 498 //    u2 attributes_count;
 499 //    attribute_info_attributes[attributes_count];
 500 //  }
 501 void JvmtiClassFileReconstituter::write_record_attribute() {
 502   Array<RecordComponent*>* components = ik()->record_components();
 503   int number_of_components = components->length();
 504 
 505   // Each component has a u2 for name, descr, attribute count
 506   u4 length = checked_cast<u4>(sizeof(u2) + (sizeof(u2) * 3 * number_of_components));
 507   for (int x = 0; x < number_of_components; x++) {
 508     RecordComponent* component = components->at(x);

 549 // JVMSpec|     {  u2 inner_class_info_index;
 550 // JVMSpec|        u2 outer_class_info_index;
 551 // JVMSpec|        u2 inner_name_index;
 552 // JVMSpec|        u2 inner_class_access_flags;
 553 // JVMSpec|     } classes[number_of_classes];
 554 // JVMSpec|   }
 555 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
 556   InnerClassesIterator iter(ik());
 557   guarantee(iter.length() != 0 && iter.length() == length,
 558             "caller must check");
 559   u2 entry_count = checked_cast<u2>(length / InstanceKlass::inner_class_next_offset);
 560   u4 size = 2 + entry_count * (2+2+2+2);
 561 
 562   write_attribute_name_index("InnerClasses");
 563   write_u4(size);
 564   write_u2(entry_count);
 565   for (; !iter.done(); iter.next()) {
 566     write_u2(iter.inner_class_info_index());
 567     write_u2(iter.outer_class_info_index());
 568     write_u2(iter.inner_name_index());
 569     u2 flags = iter.inner_access_flags();
 570     // ClassFileParser may add identity to inner class attributes, so remove it.
 571     if (!ik()->supports_inline_types()) {
 572       flags &= ~JVM_ACC_IDENTITY;;
 573     }
 574     write_u2(flags);
 575   }
 576 }
 577 
 578 // Write Synthetic attribute
 579 // JVMSpec|   Synthetic_attribute {
 580 // JVMSpec|     u2 attribute_name_index;
 581 // JVMSpec|     u4 attribute_length;
 582 // JVMSpec|   }
 583 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
 584   write_attribute_name_index("Synthetic");
 585   write_u4(0); //length always zero
 586 }
 587 
 588 // Compute size of LineNumberTable
 589 u2 JvmtiClassFileReconstituter::line_number_table_entries(const methodHandle& method) {
 590   // The line number table is compressed so we don't know how big it is until decompressed.
 591   // Decompression is really fast so we just do it twice.
 592   u2 num_entries = 0;
 593   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
 594   while (stream.read_pair()) {

 813     ++attr_count;
 814   }
 815   if (anno != nullptr) {
 816     ++attr_count;     // has RuntimeVisibleAnnotations attribute
 817   }
 818   if (type_anno != nullptr) {
 819     ++attr_count;     // has RuntimeVisibleTypeAnnotations attribute
 820   }
 821   if (!cpool()->bsm_entries().is_empty()) {
 822     ++attr_count;
 823   }
 824   if (ik()->nest_host_index() != 0) {
 825     ++attr_count;
 826   }
 827   if (ik()->nest_members() != Universe::the_empty_short_array()) {
 828     ++attr_count;
 829   }
 830   if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
 831     ++attr_count;
 832   }
 833   if (ik()->loadable_descriptors() != Universe::the_empty_short_array()) {
 834     ++attr_count;
 835   }
 836   if (ik()->record_components() != nullptr) {
 837     ++attr_count;
 838   }
 839 
 840   write_u2(attr_count);
 841 
 842   if (generic_signature != nullptr) {
 843     write_signature_attribute(symbol_to_cpool_index(generic_signature));
 844   }
 845   if (ik()->source_file_name() != nullptr) {
 846     write_source_file_attribute();
 847   }
 848   if (ik()->source_debug_extension() != nullptr) {
 849     write_source_debug_extension_attribute();
 850   }
 851   if (anno != nullptr) {
 852     write_annotations_attribute("RuntimeVisibleAnnotations", anno);
 853   }
 854   if (type_anno != nullptr) {
 855     write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
 856   }
 857   if (ik()->nest_host_index() != 0) {
 858     write_nest_host_attribute();
 859   }
 860   if (ik()->nest_members() != Universe::the_empty_short_array()) {
 861     write_nest_members_attribute();
 862   }
 863   if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
 864     write_permitted_subclasses_attribute();
 865   }
 866   if (ik()->loadable_descriptors() != Universe::the_empty_short_array()) {
 867     write_loadable_descriptors_attribute();
 868   }
 869   if (ik()->record_components() != nullptr) {
 870     write_record_attribute();
 871   }
 872   if (!cpool()->bsm_entries().is_empty()) {
 873     write_bootstrapmethod_attribute();
 874   }
 875   if (inner_classes_length > 0) {
 876     write_inner_classes_attribute(inner_classes_length);
 877   }
 878 }
 879 
 880 // Write the method information portion of ClassFile structure
 881 // JVMSpec|     u2 methods_count;
 882 // JVMSpec|     method_info methods[methods_count];
 883 void JvmtiClassFileReconstituter::write_method_infos() {
 884   HandleMark hm(thread());
 885   Array<Method*>* methods = ik()->methods();
 886   int num_methods = methods->length();
 887   int num_overpass = 0;
 888 

1043     // length of bytecode (mnemonic + operands)
1044     address bcp = bs.bcp();
1045     int     len = bs.instruction_size();
1046     assert(len > 0, "length must be > 0");
1047 
1048     // copy the bytecodes
1049     *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
1050     if (len > 1) {
1051       memcpy(p+1, bcp+1, len-1);
1052     }
1053 
1054     // During linking the get/put and invoke instructions are rewritten
1055     // with an index into the constant pool cache. The original constant
1056     // pool index must be returned to caller.  Rewrite the index.
1057     if (is_rewritten && len > 1) {
1058       bool is_wide = false;
1059       switch (code) {
1060       case Bytecodes::_getstatic       :  // fall through
1061       case Bytecodes::_putstatic       :  // fall through
1062       case Bytecodes::_getfield        :  // fall through
1063       case Bytecodes::_putfield        : {
1064         int field_index = Bytes::get_native_u2(bcp+1);
1065         u2 pool_index = mh->constants()->resolved_field_entry_at(field_index)->constant_pool_index();
1066         assert(pool_index < mh->constants()->length(), "sanity check");
1067         Bytes::put_Java_u2((address)(p+1), pool_index);     // java byte ordering
1068         break;
1069       }
1070       case Bytecodes::_invokevirtual   :  // fall through
1071       case Bytecodes::_invokespecial   :  // fall through
1072       case Bytecodes::_invokestatic    :  // fall through
1073       case Bytecodes::_invokedynamic   :  // fall through
1074       case Bytecodes::_invokeinterface : {
1075         assert(len == 3 ||
1076                (code == Bytecodes::_invokeinterface && len == 5) ||
1077                (code == Bytecodes::_invokedynamic   && len == 5),
1078                "sanity check");
1079 
1080         int cpci = Bytes::get_native_u2(bcp+1);
1081         bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1082         int pool_index;
1083         if (is_invokedynamic) {
< prev index next >