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