< prev index next >

src/hotspot/share/prims/jvmtiClassFileReconstituter.cpp

Print this page

   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);

 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];

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) {

   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);

 900 
 901 void JvmtiClassFileReconstituter::write_class_file_format() {
 902   ReallocMark();
 903 
 904   // JVMSpec|   ClassFile {
 905   // JVMSpec|           u4 magic;
 906   write_u4(0xCAFEBABE);
 907 
 908   // JVMSpec|           u2 minor_version;
 909   // JVMSpec|           u2 major_version;
 910   write_u2(ik()->minor_version());
 911   u2 major = ik()->major_version();
 912   write_u2(major);
 913 
 914   // JVMSpec|           u2 constant_pool_count;
 915   // JVMSpec|           cp_info constant_pool[constant_pool_count-1];
 916   write_u2(checked_cast<u2>(cpool()->length()));
 917   copy_cpool_bytes(writeable_address(cpool_size()));
 918 
 919   // JVMSpec|           u2 access_flags;
 920   write_u2(ik()->access_flags().get_flags() & (JVM_RECOGNIZED_CLASS_MODIFIERS));

 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];

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) {
< prev index next >