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());
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);
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);
794 ++attr_count;
795 }
796 if (anno != nullptr) {
797 ++attr_count; // has RuntimeVisibleAnnotations attribute
798 }
799 if (type_anno != nullptr) {
800 ++attr_count; // has RuntimeVisibleTypeAnnotations attribute
801 }
802 if (cpool()->operands() != nullptr) {
803 ++attr_count;
804 }
805 if (ik()->nest_host_index() != 0) {
806 ++attr_count;
807 }
808 if (ik()->nest_members() != Universe::the_empty_short_array()) {
809 ++attr_count;
810 }
811 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
812 ++attr_count;
813 }
814 if (ik()->record_components() != nullptr) {
815 ++attr_count;
816 }
817
818 write_u2(attr_count);
819
820 if (generic_signature != nullptr) {
821 write_signature_attribute(symbol_to_cpool_index(generic_signature));
822 }
823 if (ik()->source_file_name() != nullptr) {
824 write_source_file_attribute();
825 }
826 if (ik()->source_debug_extension() != nullptr) {
827 write_source_debug_extension_attribute();
828 }
829 if (anno != nullptr) {
830 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
831 }
832 if (type_anno != nullptr) {
833 write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
834 }
835 if (ik()->nest_host_index() != 0) {
836 write_nest_host_attribute();
837 }
838 if (ik()->nest_members() != Universe::the_empty_short_array()) {
839 write_nest_members_attribute();
840 }
841 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
842 write_permitted_subclasses_attribute();
843 }
844 if (ik()->record_components() != nullptr) {
845 write_record_attribute();
846 }
847 if (cpool()->operands() != nullptr) {
848 write_bootstrapmethod_attribute();
849 }
850 if (inner_classes_length > 0) {
851 write_inner_classes_attribute(inner_classes_length);
852 }
853 }
854
855 // Write the method information portion of ClassFile structure
856 // JVMSpec| u2 methods_count;
857 // JVMSpec| method_info methods[methods_count];
858 void JvmtiClassFileReconstituter::write_method_infos() {
859 HandleMark hm(thread());
860 Array<Method*>* methods = ik()->methods();
861 int num_methods = methods->length();
862 int num_overpass = 0;
863
902
903 void JvmtiClassFileReconstituter::write_class_file_format() {
904 ReallocMark();
905
906 // JVMSpec| ClassFile {
907 // JVMSpec| u4 magic;
908 write_u4(0xCAFEBABE);
909
910 // JVMSpec| u2 minor_version;
911 // JVMSpec| u2 major_version;
912 write_u2(ik()->minor_version());
913 u2 major = ik()->major_version();
914 write_u2(major);
915
916 // JVMSpec| u2 constant_pool_count;
917 // JVMSpec| cp_info constant_pool[constant_pool_count-1];
918 write_u2(checked_cast<u2>(cpool()->length()));
919 copy_cpool_bytes(writeable_address(cpool_size()));
920
921 // JVMSpec| u2 access_flags;
922 write_u2(ik()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
923
924 // JVMSpec| u2 this_class;
925 // JVMSpec| u2 super_class;
926 write_u2(class_symbol_to_cpool_index(ik()->name()));
927 Klass* super_class = ik()->super();
928 write_u2(super_class == nullptr? 0 : // zero for java.lang.Object
929 class_symbol_to_cpool_index(super_class->name()));
930
931 // JVMSpec| u2 interfaces_count;
932 // JVMSpec| u2 interfaces[interfaces_count];
933 Array<InstanceKlass*>* interfaces = ik()->local_interfaces();
934 int num_interfaces = interfaces->length();
935 write_u2(checked_cast<u2>(num_interfaces));
936 for (int index = 0; index < num_interfaces; index++) {
937 HandleMark hm(thread());
938 InstanceKlass* iik = interfaces->at(index);
939 write_u2(class_symbol_to_cpool_index(iik->name()));
940 }
941
942 // JVMSpec| u2 fields_count;
943 // JVMSpec| field_info fields[fields_count];
1013 // length of bytecode (mnemonic + operands)
1014 address bcp = bs.bcp();
1015 int len = bs.instruction_size();
1016 assert(len > 0, "length must be > 0");
1017
1018 // copy the bytecodes
1019 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
1020 if (len > 1) {
1021 memcpy(p+1, bcp+1, len-1);
1022 }
1023
1024 // During linking the get/put and invoke instructions are rewritten
1025 // with an index into the constant pool cache. The original constant
1026 // pool index must be returned to caller. Rewrite the index.
1027 if (is_rewritten && len > 1) {
1028 bool is_wide = false;
1029 switch (code) {
1030 case Bytecodes::_getstatic : // fall through
1031 case Bytecodes::_putstatic : // fall through
1032 case Bytecodes::_getfield : // fall through
1033 case Bytecodes::_putfield : {
1034 int field_index = Bytes::get_native_u2(bcp+1);
1035 u2 pool_index = mh->constants()->resolved_field_entry_at(field_index)->constant_pool_index();
1036 assert(pool_index < mh->constants()->length(), "sanity check");
1037 Bytes::put_Java_u2((address)(p+1), pool_index); // java byte ordering
1038 break;
1039 }
1040 case Bytecodes::_invokevirtual : // fall through
1041 case Bytecodes::_invokespecial : // fall through
1042 case Bytecodes::_invokestatic : // fall through
1043 case Bytecodes::_invokedynamic : // fall through
1044 case Bytecodes::_invokeinterface : {
1045 assert(len == 3 ||
1046 (code == Bytecodes::_invokeinterface && len == 5) ||
1047 (code == Bytecodes::_invokedynamic && len == 5),
1048 "sanity check");
1049
1050 int cpci = Bytes::get_native_u2(bcp+1);
1051 bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1052 int pool_index;
1053 if (is_invokedynamic) {
|
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 "classfile/vmClasses.hpp"
28 #include "interpreter/bytecodeStream.hpp"
29 #include "memory/universe.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;
47 _cpool = constantPoolHandle(Thread::current(), ik->constants());
69 write_u2(checked_cast<u2>(java_fields));
70 for (JavaFieldStream fs(ik()); !fs.done(); fs.next()) {
71 AccessFlags access_flags = fs.access_flags();
72 u2 name_index = fs.name_index();
73 u2 signature_index = fs.signature_index();
74 u2 initial_value_index = fs.initval_index();
75 guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
76 // int offset = ik()->field_offset( index );
77 u2 generic_signature_index = fs.generic_signature_index();
78 AnnotationArray* anno = fields_anno == nullptr ? nullptr : fields_anno->at(fs.index());
79 AnnotationArray* type_anno = fields_type_anno == nullptr ? nullptr : fields_type_anno->at(fs.index());
80
81 // JVMSpec| field_info {
82 // JVMSpec| u2 access_flags;
83 // JVMSpec| u2 name_index;
84 // JVMSpec| u2 descriptor_index;
85 // JVMSpec| u2 attributes_count;
86 // JVMSpec| attribute_info attributes[attributes_count];
87 // JVMSpec| }
88
89 write_u2(access_flags.get_flags());
90 write_u2(name_index);
91 write_u2(signature_index);
92 u2 attr_count = 0;
93 if (initial_value_index != 0) {
94 ++attr_count;
95 }
96 if (access_flags.is_synthetic()) {
97 // ++attr_count;
98 }
99 if (generic_signature_index != 0) {
100 ++attr_count;
101 }
102 if (anno != nullptr) {
103 ++attr_count; // has RuntimeVisibleAnnotations attribute
104 }
105 if (type_anno != nullptr) {
106 ++attr_count; // has RuntimeVisibleTypeAnnotations attribute
107 }
108
109 write_u2(attr_count);
456 // PermittedSubclasses {
457 // u2 attribute_name_index;
458 // u4 attribute_length;
459 // u2 number_of_classes;
460 // u2 classes[number_of_classes];
461 // }
462 void JvmtiClassFileReconstituter::write_permitted_subclasses_attribute() {
463 Array<u2>* permitted_subclasses = ik()->permitted_subclasses();
464 int number_of_classes = permitted_subclasses->length();
465 int length = sizeof(u2) * (1 + number_of_classes); // '1 +' is for number_of_classes field
466
467 write_attribute_name_index("PermittedSubclasses");
468 write_u4(length);
469 write_u2(checked_cast<u2>(number_of_classes));
470 for (int i = 0; i < number_of_classes; i++) {
471 u2 class_cp_index = permitted_subclasses->at(i);
472 write_u2(class_cp_index);
473 }
474 }
475
476 // LoadableDescriptors {
477 // u2 attribute_name_index;
478 // u4 attribute_length;
479 // u2 number_of_descriptors;
480 // u2 descriptors[number_of_descriptors];
481 // }
482 void JvmtiClassFileReconstituter::write_loadable_descriptors_attribute() {
483 Array<u2>* loadable_descriptors = ik()->loadable_descriptors();
484 int number_of_descriptors = loadable_descriptors->length();
485 int length = sizeof(u2) * (1 + number_of_descriptors); // '1 +' is for number_of_descriptors field
486
487 write_attribute_name_index("LoadableDescriptors");
488 write_u4(length);
489 write_u2(checked_cast<u2>(number_of_descriptors));
490 for (int i = 0; i < number_of_descriptors; i++) {
491 u2 utf8_index = loadable_descriptors->at(i);
492 write_u2(utf8_index);
493 }
494 }
495
496 // Record {
497 // u2 attribute_name_index;
498 // u4 attribute_length;
499 // u2 components_count;
500 // component_info components[components_count];
501 // }
502 // component_info {
503 // u2 name_index;
504 // u2 descriptor_index
505 // u2 attributes_count;
506 // attribute_info_attributes[attributes_count];
507 // }
508 void JvmtiClassFileReconstituter::write_record_attribute() {
509 Array<RecordComponent*>* components = ik()->record_components();
510 int number_of_components = components->length();
511
512 // Each component has a u2 for name, descr, attribute count
513 u4 length = checked_cast<u4>(sizeof(u2) + (sizeof(u2) * 3 * number_of_components));
514 for (int x = 0; x < number_of_components; x++) {
515 RecordComponent* component = components->at(x);
815 ++attr_count;
816 }
817 if (anno != nullptr) {
818 ++attr_count; // has RuntimeVisibleAnnotations attribute
819 }
820 if (type_anno != nullptr) {
821 ++attr_count; // has RuntimeVisibleTypeAnnotations attribute
822 }
823 if (cpool()->operands() != nullptr) {
824 ++attr_count;
825 }
826 if (ik()->nest_host_index() != 0) {
827 ++attr_count;
828 }
829 if (ik()->nest_members() != Universe::the_empty_short_array()) {
830 ++attr_count;
831 }
832 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
833 ++attr_count;
834 }
835 if (ik()->loadable_descriptors() != Universe::the_empty_short_array()) {
836 ++attr_count;
837 }
838 if (ik()->record_components() != nullptr) {
839 ++attr_count;
840 }
841
842 write_u2(attr_count);
843
844 if (generic_signature != nullptr) {
845 write_signature_attribute(symbol_to_cpool_index(generic_signature));
846 }
847 if (ik()->source_file_name() != nullptr) {
848 write_source_file_attribute();
849 }
850 if (ik()->source_debug_extension() != nullptr) {
851 write_source_debug_extension_attribute();
852 }
853 if (anno != nullptr) {
854 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
855 }
856 if (type_anno != nullptr) {
857 write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
858 }
859 if (ik()->nest_host_index() != 0) {
860 write_nest_host_attribute();
861 }
862 if (ik()->nest_members() != Universe::the_empty_short_array()) {
863 write_nest_members_attribute();
864 }
865 if (ik()->permitted_subclasses() != Universe::the_empty_short_array()) {
866 write_permitted_subclasses_attribute();
867 }
868 if (ik()->loadable_descriptors() != Universe::the_empty_short_array()) {
869 write_loadable_descriptors_attribute();
870 }
871 if (ik()->record_components() != nullptr) {
872 write_record_attribute();
873 }
874 if (cpool()->operands() != nullptr) {
875 write_bootstrapmethod_attribute();
876 }
877 if (inner_classes_length > 0) {
878 write_inner_classes_attribute(inner_classes_length);
879 }
880 }
881
882 // Write the method information portion of ClassFile structure
883 // JVMSpec| u2 methods_count;
884 // JVMSpec| method_info methods[methods_count];
885 void JvmtiClassFileReconstituter::write_method_infos() {
886 HandleMark hm(thread());
887 Array<Method*>* methods = ik()->methods();
888 int num_methods = methods->length();
889 int num_overpass = 0;
890
929
930 void JvmtiClassFileReconstituter::write_class_file_format() {
931 ReallocMark();
932
933 // JVMSpec| ClassFile {
934 // JVMSpec| u4 magic;
935 write_u4(0xCAFEBABE);
936
937 // JVMSpec| u2 minor_version;
938 // JVMSpec| u2 major_version;
939 write_u2(ik()->minor_version());
940 u2 major = ik()->major_version();
941 write_u2(major);
942
943 // JVMSpec| u2 constant_pool_count;
944 // JVMSpec| cp_info constant_pool[constant_pool_count-1];
945 write_u2(checked_cast<u2>(cpool()->length()));
946 copy_cpool_bytes(writeable_address(cpool_size()));
947
948 // JVMSpec| u2 access_flags;
949 write_u2(ik()->access_flags().get_flags() & (JVM_RECOGNIZED_CLASS_MODIFIERS));
950 // JVMSpec| u2 this_class;
951 // JVMSpec| u2 super_class;
952 write_u2(class_symbol_to_cpool_index(ik()->name()));
953 Klass* super_class = ik()->super();
954 write_u2(super_class == nullptr? 0 : // zero for java.lang.Object
955 class_symbol_to_cpool_index(super_class->name()));
956
957 // JVMSpec| u2 interfaces_count;
958 // JVMSpec| u2 interfaces[interfaces_count];
959 Array<InstanceKlass*>* interfaces = ik()->local_interfaces();
960 int num_interfaces = interfaces->length();
961 write_u2(checked_cast<u2>(num_interfaces));
962 for (int index = 0; index < num_interfaces; index++) {
963 HandleMark hm(thread());
964 InstanceKlass* iik = interfaces->at(index);
965 write_u2(class_symbol_to_cpool_index(iik->name()));
966 }
967
968 // JVMSpec| u2 fields_count;
969 // JVMSpec| field_info fields[fields_count];
1039 // length of bytecode (mnemonic + operands)
1040 address bcp = bs.bcp();
1041 int len = bs.instruction_size();
1042 assert(len > 0, "length must be > 0");
1043
1044 // copy the bytecodes
1045 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
1046 if (len > 1) {
1047 memcpy(p+1, bcp+1, len-1);
1048 }
1049
1050 // During linking the get/put and invoke instructions are rewritten
1051 // with an index into the constant pool cache. The original constant
1052 // pool index must be returned to caller. Rewrite the index.
1053 if (is_rewritten && len > 1) {
1054 bool is_wide = false;
1055 switch (code) {
1056 case Bytecodes::_getstatic : // fall through
1057 case Bytecodes::_putstatic : // fall through
1058 case Bytecodes::_getfield : // fall through
1059 case Bytecodes::_putfield : {
1060 int field_index = Bytes::get_native_u2(bcp+1);
1061 u2 pool_index = mh->constants()->resolved_field_entry_at(field_index)->constant_pool_index();
1062 assert(pool_index < mh->constants()->length(), "sanity check");
1063 Bytes::put_Java_u2((address)(p+1), pool_index); // java byte ordering
1064 break;
1065 }
1066 case Bytecodes::_invokevirtual : // fall through
1067 case Bytecodes::_invokespecial : // fall through
1068 case Bytecodes::_invokestatic : // fall through
1069 case Bytecodes::_invokedynamic : // fall through
1070 case Bytecodes::_invokeinterface : {
1071 assert(len == 3 ||
1072 (code == Bytecodes::_invokeinterface && len == 5) ||
1073 (code == Bytecodes::_invokedynamic && len == 5),
1074 "sanity check");
1075
1076 int cpci = Bytes::get_native_u2(bcp+1);
1077 bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1078 int pool_index;
1079 if (is_invokedynamic) {
|