< prev index next >

src/hotspot/share/prims/jvmtiClassFileReconstituter.cpp

Print this page

   1 /*
   2  * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"

  27 #include "interpreter/bytecodeStream.hpp"
  28 #include "memory/universe.hpp"
  29 #include "oops/fieldStreams.inline.hpp"
  30 #include "oops/recordComponent.hpp"
  31 #include "prims/jvmtiClassFileReconstituter.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/signature.hpp"
  34 #include "utilities/bytes.hpp"
  35 
  36 // FIXME: add Deprecated attribute
  37 // FIXME: fix Synthetic attribute
  38 // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
  39 
  40 JvmtiConstantPoolReconstituter::JvmtiConstantPoolReconstituter(InstanceKlass* ik) {
  41   set_error(JVMTI_ERROR_NONE);
  42   _ik = ik;
  43   _cpool = constantPoolHandle(Thread::current(), ik->constants());
  44   _symmap = new SymbolHashMap();
  45   _classmap = new SymbolHashMap();
  46   _cpool_size = _cpool->hash_entries_to(_symmap, _classmap);

 865 
 866 void JvmtiClassFileReconstituter::write_class_file_format() {
 867   ReallocMark();
 868 
 869   // JVMSpec|   ClassFile {
 870   // JVMSpec|           u4 magic;
 871   write_u4(0xCAFEBABE);
 872 
 873   // JVMSpec|           u2 minor_version;
 874   // JVMSpec|           u2 major_version;
 875   write_u2(ik()->minor_version());
 876   u2 major = ik()->major_version();
 877   write_u2(major);
 878 
 879   // JVMSpec|           u2 constant_pool_count;
 880   // JVMSpec|           cp_info constant_pool[constant_pool_count-1];
 881   write_u2(cpool()->length());
 882   copy_cpool_bytes(writeable_address(cpool_size()));
 883 
 884   // JVMSpec|           u2 access_flags;
 885   write_u2(ik()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
 886 
 887   // JVMSpec|           u2 this_class;
 888   // JVMSpec|           u2 super_class;
 889   write_u2(class_symbol_to_cpool_index(ik()->name()));
 890   Klass* super_class = ik()->super();
 891   write_u2(super_class == NULL? 0 :  // zero for java.lang.Object
 892                 class_symbol_to_cpool_index(super_class->name()));
 893 
 894   // JVMSpec|           u2 interfaces_count;
 895   // JVMSpec|           u2 interfaces[interfaces_count];
 896   Array<InstanceKlass*>* interfaces =  ik()->local_interfaces();
 897   int num_interfaces = interfaces->length();
 898   write_u2(num_interfaces);


 899   for (int index = 0; index < num_interfaces; index++) {
 900     HandleMark hm(thread());
 901     InstanceKlass* iik = interfaces->at(index);
 902     write_u2(class_symbol_to_cpool_index(iik->name()));



 903   }
 904 
 905   // JVMSpec|           u2 fields_count;
 906   // JVMSpec|           field_info fields[fields_count];
 907   write_field_infos();
 908 
 909   // JVMSpec|           u2 methods_count;
 910   // JVMSpec|           method_info methods[methods_count];
 911   write_method_infos();
 912 
 913   // JVMSpec|           u2 attributes_count;
 914   // JVMSpec|           attribute_info attributes[attributes_count];
 915   // JVMSpec|   } /* end ClassFile 8?
 916   write_class_attributes();
 917 }
 918 
 919 address JvmtiClassFileReconstituter::writeable_address(size_t size) {
 920   size_t used_size = _buffer_ptr - _buffer;
 921   if (size + used_size >= _buffer_size) {
 922     // compute the new buffer size: must be at least twice as big as before

 977     address bcp = bs.bcp();
 978     int     len = bs.instruction_size();
 979     assert(len > 0, "length must be > 0");
 980 
 981     // copy the bytecodes
 982     *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
 983     if (len > 1) {
 984       memcpy(p+1, bcp+1, len-1);
 985     }
 986 
 987     // During linking the get/put and invoke instructions are rewritten
 988     // with an index into the constant pool cache. The original constant
 989     // pool index must be returned to caller.  Rewrite the index.
 990     if (is_rewritten && len > 1) {
 991       bool is_wide = false;
 992       switch (code) {
 993       case Bytecodes::_getstatic       :  // fall through
 994       case Bytecodes::_putstatic       :  // fall through
 995       case Bytecodes::_getfield        :  // fall through
 996       case Bytecodes::_putfield        :  // fall through

 997       case Bytecodes::_invokevirtual   :  // fall through
 998       case Bytecodes::_invokespecial   :  // fall through
 999       case Bytecodes::_invokestatic    :  // fall through
1000       case Bytecodes::_invokedynamic   :  // fall through
1001       case Bytecodes::_invokeinterface : {
1002         assert(len == 3 ||
1003                (code == Bytecodes::_invokeinterface && len == 5) ||
1004                (code == Bytecodes::_invokedynamic   && len == 5),
1005                "sanity check");
1006 
1007         int cpci = Bytes::get_native_u2(bcp+1);
1008         bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1009         ConstantPoolCacheEntry* entry;
1010         if (is_invokedynamic) {
1011           cpci = Bytes::get_native_u4(bcp+1);
1012           entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci);
1013         } else {
1014         // cache cannot be pre-fetched since some classes won't have it yet
1015           entry = mh->constants()->cache()->entry_at(cpci);
1016         }

   1 /*
   2  * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 "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/fieldStreams.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 
  37 // FIXME: add Deprecated attribute
  38 // FIXME: fix Synthetic attribute
  39 // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
  40 
  41 JvmtiConstantPoolReconstituter::JvmtiConstantPoolReconstituter(InstanceKlass* ik) {
  42   set_error(JVMTI_ERROR_NONE);
  43   _ik = ik;
  44   _cpool = constantPoolHandle(Thread::current(), ik->constants());
  45   _symmap = new SymbolHashMap();
  46   _classmap = new SymbolHashMap();
  47   _cpool_size = _cpool->hash_entries_to(_symmap, _classmap);

 866 
 867 void JvmtiClassFileReconstituter::write_class_file_format() {
 868   ReallocMark();
 869 
 870   // JVMSpec|   ClassFile {
 871   // JVMSpec|           u4 magic;
 872   write_u4(0xCAFEBABE);
 873 
 874   // JVMSpec|           u2 minor_version;
 875   // JVMSpec|           u2 major_version;
 876   write_u2(ik()->minor_version());
 877   u2 major = ik()->major_version();
 878   write_u2(major);
 879 
 880   // JVMSpec|           u2 constant_pool_count;
 881   // JVMSpec|           cp_info constant_pool[constant_pool_count-1];
 882   write_u2(cpool()->length());
 883   copy_cpool_bytes(writeable_address(cpool_size()));
 884 
 885   // JVMSpec|           u2 access_flags;
 886   write_u2(ik()->access_flags().get_flags() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_INLINE | JVM_ACC_VALUE));
 887 
 888   // JVMSpec|           u2 this_class;
 889   // JVMSpec|           u2 super_class;
 890   write_u2(class_symbol_to_cpool_index(ik()->name()));
 891   Klass* super_class = ik()->super();
 892   write_u2(super_class == NULL? 0 :  // zero for java.lang.Object
 893                 class_symbol_to_cpool_index(super_class->name()));
 894 
 895   // JVMSpec|           u2 interfaces_count;
 896   // JVMSpec|           u2 interfaces[interfaces_count];
 897   Array<InstanceKlass*>* interfaces =  ik()->local_interfaces();
 898   int num_interfaces = interfaces->length();
 899   write_u2(num_interfaces -
 900            (ik()->has_injected_identityObject() || ik()->has_injected_valueObject() ? 1 : 0));
 901 
 902   for (int index = 0; index < num_interfaces; index++) {
 903     HandleMark hm(thread());
 904     InstanceKlass* iik = interfaces->at(index);
 905     if ( (!ik()->has_injected_identityObject() || iik != vmClasses::IdentityObject_klass()) &&
 906          (!ik()->has_injected_valueObject() || iik != vmClasses::ValueObject_klass())) {
 907       write_u2(class_symbol_to_cpool_index(iik->name()));
 908     }
 909   }
 910 
 911   // JVMSpec|           u2 fields_count;
 912   // JVMSpec|           field_info fields[fields_count];
 913   write_field_infos();
 914 
 915   // JVMSpec|           u2 methods_count;
 916   // JVMSpec|           method_info methods[methods_count];
 917   write_method_infos();
 918 
 919   // JVMSpec|           u2 attributes_count;
 920   // JVMSpec|           attribute_info attributes[attributes_count];
 921   // JVMSpec|   } /* end ClassFile 8?
 922   write_class_attributes();
 923 }
 924 
 925 address JvmtiClassFileReconstituter::writeable_address(size_t size) {
 926   size_t used_size = _buffer_ptr - _buffer;
 927   if (size + used_size >= _buffer_size) {
 928     // compute the new buffer size: must be at least twice as big as before

 983     address bcp = bs.bcp();
 984     int     len = bs.instruction_size();
 985     assert(len > 0, "length must be > 0");
 986 
 987     // copy the bytecodes
 988     *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
 989     if (len > 1) {
 990       memcpy(p+1, bcp+1, len-1);
 991     }
 992 
 993     // During linking the get/put and invoke instructions are rewritten
 994     // with an index into the constant pool cache. The original constant
 995     // pool index must be returned to caller.  Rewrite the index.
 996     if (is_rewritten && len > 1) {
 997       bool is_wide = false;
 998       switch (code) {
 999       case Bytecodes::_getstatic       :  // fall through
1000       case Bytecodes::_putstatic       :  // fall through
1001       case Bytecodes::_getfield        :  // fall through
1002       case Bytecodes::_putfield        :  // fall through
1003       case Bytecodes::_withfield       :  // fall through
1004       case Bytecodes::_invokevirtual   :  // fall through
1005       case Bytecodes::_invokespecial   :  // fall through
1006       case Bytecodes::_invokestatic    :  // fall through
1007       case Bytecodes::_invokedynamic   :  // fall through
1008       case Bytecodes::_invokeinterface : {
1009         assert(len == 3 ||
1010                (code == Bytecodes::_invokeinterface && len == 5) ||
1011                (code == Bytecodes::_invokedynamic   && len == 5),
1012                "sanity check");
1013 
1014         int cpci = Bytes::get_native_u2(bcp+1);
1015         bool is_invokedynamic = (code == Bytecodes::_invokedynamic);
1016         ConstantPoolCacheEntry* entry;
1017         if (is_invokedynamic) {
1018           cpci = Bytes::get_native_u4(bcp+1);
1019           entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci);
1020         } else {
1021         // cache cannot be pre-fetched since some classes won't have it yet
1022           entry = mh->constants()->cache()->entry_at(cpci);
1023         }
< prev index next >