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/constantPool.inline.hpp"
29 #include "oops/fieldStreams.inline.hpp"
30 #include "oops/instanceKlass.inline.hpp"
31 #include "oops/recordComponent.hpp"
32 #include "prims/jvmtiClassFileReconstituter.hpp"
33 #include "runtime/handles.inline.hpp"
34 #include "runtime/signature.hpp"
35 #include "utilities/bytes.hpp"
36 #include "utilities/checkedCast.hpp"
37
38 // FIXME: add Deprecated attribute
39 // FIXME: fix Synthetic attribute
40 // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
41
42 JvmtiConstantPoolReconstituter::JvmtiConstantPoolReconstituter(InstanceKlass* ik) {
43 set_error(JVMTI_ERROR_NONE);
44 _ik = ik;
45 _cpool = constantPoolHandle(Thread::current(), ik->constants());
454 // PermittedSubclasses {
455 // u2 attribute_name_index;
456 // u4 attribute_length;
457 // u2 number_of_classes;
458 // u2 classes[number_of_classes];
459 // }
460 void JvmtiClassFileReconstituter::write_permitted_subclasses_attribute() {
461 Array<u2>* permitted_subclasses = ik()->permitted_subclasses();
462 int number_of_classes = permitted_subclasses->length();
463 int length = sizeof(u2) * (1 + number_of_classes); // '1 +' is for number_of_classes field
464
465 write_attribute_name_index("PermittedSubclasses");
466 write_u4(length);
467 write_u2(checked_cast<u2>(number_of_classes));
468 for (int i = 0; i < number_of_classes; i++) {
469 u2 class_cp_index = permitted_subclasses->at(i);
470 write_u2(class_cp_index);
471 }
472 }
473
474 // Record {
475 // u2 attribute_name_index;
476 // u4 attribute_length;
477 // u2 components_count;
478 // component_info components[components_count];
479 // }
480 // component_info {
481 // u2 name_index;
482 // u2 descriptor_index
483 // u2 attributes_count;
484 // attribute_info_attributes[attributes_count];
485 // }
486 void JvmtiClassFileReconstituter::write_record_attribute() {
487 Array<RecordComponent*>* components = ik()->record_components();
488 int number_of_components = components->length();
489
490 // Each component has a u2 for name, descr, attribute count
491 u4 length = checked_cast<u4>(sizeof(u2) + (sizeof(u2) * 3 * number_of_components));
492 for (int x = 0; x < number_of_components; x++) {
493 RecordComponent* component = components->at(x);
534 // JVMSpec| { u2 inner_class_info_index;
535 // JVMSpec| u2 outer_class_info_index;
536 // JVMSpec| u2 inner_name_index;
537 // JVMSpec| u2 inner_class_access_flags;
538 // JVMSpec| } classes[number_of_classes];
539 // JVMSpec| }
540 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
541 InnerClassesIterator iter(ik());
542 guarantee(iter.length() != 0 && iter.length() == length,
543 "caller must check");
544 u2 entry_count = checked_cast<u2>(length / InstanceKlass::inner_class_next_offset);
545 u4 size = 2 + entry_count * (2+2+2+2);
546
547 write_attribute_name_index("InnerClasses");
548 write_u4(size);
549 write_u2(entry_count);
550 for (; !iter.done(); iter.next()) {
551 write_u2(iter.inner_class_info_index());
552 write_u2(iter.outer_class_info_index());
553 write_u2(iter.inner_name_index());
554 write_u2(iter.inner_access_flags());
555 }
556 }
557
558 // Write Synthetic attribute
559 // JVMSpec| Synthetic_attribute {
560 // JVMSpec| u2 attribute_name_index;
561 // JVMSpec| u4 attribute_length;
562 // JVMSpec| }
563 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
564 write_attribute_name_index("Synthetic");
565 write_u4(0); //length always zero
566 }
567
568 // Compute size of LineNumberTable
569 u2 JvmtiClassFileReconstituter::line_number_table_entries(const methodHandle& method) {
570 // The line number table is compressed so we don't know how big it is until decompressed.
571 // Decompression is really fast so we just do it twice.
572 u2 num_entries = 0;
573 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
574 while (stream.read_pair()) {
793 ++attr_count;
794 }
795 if (anno != nullptr) {
796 ++attr_count; // has RuntimeVisibleAnnotations attribute
797 }
798 if (type_anno != nullptr) {
799 ++attr_count; // has RuntimeVisibleTypeAnnotations attribute
800 }
801 if (cpool()->operands() != nullptr) {
802 ++attr_count;
803 }
804 if (ik()->nest_host_index() != 0) {
805 ++attr_count;
806 }
807 if (ik()->nest_members() != Universe::the_empty_short_array()) {
808 ++attr_count;
809 }
810 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
811 ++attr_count;
812 }
813 if (ik()->record_components() != nullptr) {
814 ++attr_count;
815 }
816
817 write_u2(attr_count);
818
819 if (generic_signature != nullptr) {
820 write_signature_attribute(symbol_to_cpool_index(generic_signature));
821 }
822 if (ik()->source_file_name() != nullptr) {
823 write_source_file_attribute();
824 }
825 if (ik()->source_debug_extension() != nullptr) {
826 write_source_debug_extension_attribute();
827 }
828 if (anno != nullptr) {
829 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
830 }
831 if (type_anno != nullptr) {
832 write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
833 }
834 if (ik()->nest_host_index() != 0) {
835 write_nest_host_attribute();
836 }
837 if (ik()->nest_members() != Universe::the_empty_short_array()) {
838 write_nest_members_attribute();
839 }
840 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
841 write_permitted_subclasses_attribute();
842 }
843 if (ik()->record_components() != nullptr) {
844 write_record_attribute();
845 }
846 if (cpool()->operands() != nullptr) {
847 write_bootstrapmethod_attribute();
848 }
849 if (inner_classes_length > 0) {
850 write_inner_classes_attribute(inner_classes_length);
851 }
852 }
853
854 // Write the method information portion of ClassFile structure
855 // JVMSpec| u2 methods_count;
856 // JVMSpec| method_info methods[methods_count];
857 void JvmtiClassFileReconstituter::write_method_infos() {
858 HandleMark hm(thread());
859 Array<Method*>* methods = ik()->methods();
860 int num_methods = methods->length();
861 int num_overpass = 0;
862
1012 // length of bytecode (mnemonic + operands)
1013 address bcp = bs.bcp();
1014 int len = bs.instruction_size();
1015 assert(len > 0, "length must be > 0");
1016
1017 // copy the bytecodes
1018 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
1019 if (len > 1) {
1020 memcpy(p+1, bcp+1, len-1);
1021 }
1022
1023 // During linking the get/put and invoke instructions are rewritten
1024 // with an index into the constant pool cache. The original constant
1025 // pool index must be returned to caller. Rewrite the index.
1026 if (is_rewritten && len > 1) {
1027 bool is_wide = false;
1028 switch (code) {
1029 case Bytecodes::_getstatic : // fall through
1030 case Bytecodes::_putstatic : // fall through
1031 case Bytecodes::_getfield : // fall through
1032 case Bytecodes::_putfield : {
1033 int field_index = Bytes::get_native_u2(bcp+1);
1034 u2 pool_index = mh->constants()->resolved_field_entry_at(field_index)->constant_pool_index();
1035 assert(pool_index < mh->constants()->length(), "sanity check");
1036 Bytes::put_Java_u2((address)(p+1), pool_index); // java byte ordering
1037 break;
1038 }
1039 case Bytecodes::_invokevirtual : // fall through
1040 case Bytecodes::_invokespecial : // fall through
1041 case Bytecodes::_invokestatic : // fall through
1042 case Bytecodes::_invokedynamic : // fall through
1043 case Bytecodes::_invokeinterface : {
1044 assert(len == 3 ||
1045 (code == Bytecodes::_invokeinterface && len == 5) ||
1046 (code == Bytecodes::_invokedynamic && len == 5),
1047 "sanity check");
1048
1049 int cpci = Bytes::get_native_u2(bcp+1);
1050 bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1051 int pool_index;
1052 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/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());
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 // LoadableDescriptors {
476 // u2 attribute_name_index;
477 // u4 attribute_length;
478 // u2 number_of_descriptors;
479 // u2 descriptors[number_of_descriptors];
480 // }
481 void JvmtiClassFileReconstituter::write_loadable_descriptors_attribute() {
482 Array<u2>* loadable_descriptors = ik()->loadable_descriptors();
483 int number_of_descriptors = loadable_descriptors->length();
484 int length = sizeof(u2) * (1 + number_of_descriptors); // '1 +' is for number_of_descriptors field
485
486 write_attribute_name_index("LoadableDescriptors");
487 write_u4(length);
488 write_u2(checked_cast<u2>(number_of_descriptors));
489 for (int i = 0; i < number_of_descriptors; i++) {
490 u2 utf8_index = loadable_descriptors->at(i);
491 write_u2(utf8_index);
492 }
493 }
494
495 // Record {
496 // u2 attribute_name_index;
497 // u4 attribute_length;
498 // u2 components_count;
499 // component_info components[components_count];
500 // }
501 // component_info {
502 // u2 name_index;
503 // u2 descriptor_index
504 // u2 attributes_count;
505 // attribute_info_attributes[attributes_count];
506 // }
507 void JvmtiClassFileReconstituter::write_record_attribute() {
508 Array<RecordComponent*>* components = ik()->record_components();
509 int number_of_components = components->length();
510
511 // Each component has a u2 for name, descr, attribute count
512 u4 length = checked_cast<u4>(sizeof(u2) + (sizeof(u2) * 3 * number_of_components));
513 for (int x = 0; x < number_of_components; x++) {
514 RecordComponent* component = components->at(x);
555 // JVMSpec| { u2 inner_class_info_index;
556 // JVMSpec| u2 outer_class_info_index;
557 // JVMSpec| u2 inner_name_index;
558 // JVMSpec| u2 inner_class_access_flags;
559 // JVMSpec| } classes[number_of_classes];
560 // JVMSpec| }
561 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
562 InnerClassesIterator iter(ik());
563 guarantee(iter.length() != 0 && iter.length() == length,
564 "caller must check");
565 u2 entry_count = checked_cast<u2>(length / InstanceKlass::inner_class_next_offset);
566 u4 size = 2 + entry_count * (2+2+2+2);
567
568 write_attribute_name_index("InnerClasses");
569 write_u4(size);
570 write_u2(entry_count);
571 for (; !iter.done(); iter.next()) {
572 write_u2(iter.inner_class_info_index());
573 write_u2(iter.outer_class_info_index());
574 write_u2(iter.inner_name_index());
575 u2 flags = iter.inner_access_flags();
576 // ClassFileParser may add identity to inner class attributes, so remove it.
577 if (!ik()->supports_inline_types()) {
578 flags &= ~JVM_ACC_IDENTITY;;
579 }
580 write_u2(flags);
581 }
582 }
583
584 // Write Synthetic attribute
585 // JVMSpec| Synthetic_attribute {
586 // JVMSpec| u2 attribute_name_index;
587 // JVMSpec| u4 attribute_length;
588 // JVMSpec| }
589 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
590 write_attribute_name_index("Synthetic");
591 write_u4(0); //length always zero
592 }
593
594 // Compute size of LineNumberTable
595 u2 JvmtiClassFileReconstituter::line_number_table_entries(const methodHandle& method) {
596 // The line number table is compressed so we don't know how big it is until decompressed.
597 // Decompression is really fast so we just do it twice.
598 u2 num_entries = 0;
599 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
600 while (stream.read_pair()) {
819 ++attr_count;
820 }
821 if (anno != nullptr) {
822 ++attr_count; // has RuntimeVisibleAnnotations attribute
823 }
824 if (type_anno != nullptr) {
825 ++attr_count; // has RuntimeVisibleTypeAnnotations attribute
826 }
827 if (cpool()->operands() != nullptr) {
828 ++attr_count;
829 }
830 if (ik()->nest_host_index() != 0) {
831 ++attr_count;
832 }
833 if (ik()->nest_members() != Universe::the_empty_short_array()) {
834 ++attr_count;
835 }
836 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
837 ++attr_count;
838 }
839 if (ik()->loadable_descriptors() != Universe::the_empty_short_array()) {
840 ++attr_count;
841 }
842 if (ik()->record_components() != nullptr) {
843 ++attr_count;
844 }
845
846 write_u2(attr_count);
847
848 if (generic_signature != nullptr) {
849 write_signature_attribute(symbol_to_cpool_index(generic_signature));
850 }
851 if (ik()->source_file_name() != nullptr) {
852 write_source_file_attribute();
853 }
854 if (ik()->source_debug_extension() != nullptr) {
855 write_source_debug_extension_attribute();
856 }
857 if (anno != nullptr) {
858 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
859 }
860 if (type_anno != nullptr) {
861 write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
862 }
863 if (ik()->nest_host_index() != 0) {
864 write_nest_host_attribute();
865 }
866 if (ik()->nest_members() != Universe::the_empty_short_array()) {
867 write_nest_members_attribute();
868 }
869 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
870 write_permitted_subclasses_attribute();
871 }
872 if (ik()->loadable_descriptors() != Universe::the_empty_short_array()) {
873 write_loadable_descriptors_attribute();
874 }
875 if (ik()->record_components() != nullptr) {
876 write_record_attribute();
877 }
878 if (cpool()->operands() != nullptr) {
879 write_bootstrapmethod_attribute();
880 }
881 if (inner_classes_length > 0) {
882 write_inner_classes_attribute(inner_classes_length);
883 }
884 }
885
886 // Write the method information portion of ClassFile structure
887 // JVMSpec| u2 methods_count;
888 // JVMSpec| method_info methods[methods_count];
889 void JvmtiClassFileReconstituter::write_method_infos() {
890 HandleMark hm(thread());
891 Array<Method*>* methods = ik()->methods();
892 int num_methods = methods->length();
893 int num_overpass = 0;
894
1044 // length of bytecode (mnemonic + operands)
1045 address bcp = bs.bcp();
1046 int len = bs.instruction_size();
1047 assert(len > 0, "length must be > 0");
1048
1049 // copy the bytecodes
1050 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
1051 if (len > 1) {
1052 memcpy(p+1, bcp+1, len-1);
1053 }
1054
1055 // During linking the get/put and invoke instructions are rewritten
1056 // with an index into the constant pool cache. The original constant
1057 // pool index must be returned to caller. Rewrite the index.
1058 if (is_rewritten && len > 1) {
1059 bool is_wide = false;
1060 switch (code) {
1061 case Bytecodes::_getstatic : // fall through
1062 case Bytecodes::_putstatic : // fall through
1063 case Bytecodes::_getfield : // fall through
1064 case Bytecodes::_putfield : {
1065 int field_index = Bytes::get_native_u2(bcp+1);
1066 u2 pool_index = mh->constants()->resolved_field_entry_at(field_index)->constant_pool_index();
1067 assert(pool_index < mh->constants()->length(), "sanity check");
1068 Bytes::put_Java_u2((address)(p+1), pool_index); // java byte ordering
1069 break;
1070 }
1071 case Bytecodes::_invokevirtual : // fall through
1072 case Bytecodes::_invokespecial : // fall through
1073 case Bytecodes::_invokestatic : // fall through
1074 case Bytecodes::_invokedynamic : // fall through
1075 case Bytecodes::_invokeinterface : {
1076 assert(len == 3 ||
1077 (code == Bytecodes::_invokeinterface && len == 5) ||
1078 (code == Bytecodes::_invokedynamic && len == 5),
1079 "sanity check");
1080
1081 int cpci = Bytes::get_native_u2(bcp+1);
1082 bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1083 int pool_index;
1084 if (is_invokedynamic) {
|