src/share/vm/interpreter/rewriter.cpp

Print this page




   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 "interpreter/bytecodes.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "interpreter/rewriter.hpp"

  29 #include "memory/gcLocker.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/generateOopMap.hpp"
  32 #include "prims/methodHandles.hpp"
  33 
  34 // Computes a CPC map (new_index -> original_index) for constant pool entries
  35 // that are referred to by the interpreter at runtime via the constant pool cache.
  36 // Also computes a CP map (original_index -> new_index).
  37 // Marks entries in CP which require additional processing.
  38 void Rewriter::compute_index_maps() {
  39   const int length  = _pool->length();
  40   init_maps(length);
  41   bool saw_mh_symbol = false;
  42   for (int i = 0; i < length; i++) {
  43     int tag = _pool->tag_at(i).value();
  44     switch (tag) {
  45       case JVM_CONSTANT_InterfaceMethodref:
  46       case JVM_CONSTANT_Fieldref          : // fall through
  47       case JVM_CONSTANT_Methodref         : // fall through
  48         add_cp_cache_entry(i);


 483   for (int i = len-1; i >= 0; i--) {
 484     Method* method = _methods->at(i);
 485     scan_method(method, false, &invokespecial_error);
 486     if (invokespecial_error) {
 487       // If you get an error here, there is no reversing bytecodes
 488       // This exception is stored for this class and no further attempt is
 489       // made at verifying or rewriting.
 490       THROW_MSG(vmSymbols::java_lang_InternalError(),
 491                 "This classfile overflows invokespecial for interfaces "
 492                 "and cannot be loaded");
 493       return;
 494      }
 495   }
 496 
 497   // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref
 498   // entries had to be added.
 499   patch_invokedynamic_bytecodes();
 500 }
 501 
 502 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {



 503   ResourceMark rm(THREAD);
 504   Rewriter     rw(klass, klass->constants(), klass->methods(), CHECK);
 505   // (That's all, folks.)
 506 }
 507 
 508 
 509 Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS)
 510   : _klass(klass),
 511     _pool(cpool),
 512     _methods(methods)
 513 {
 514 
 515   // Rewrite bytecodes - exception here exits.
 516   rewrite_bytecodes(CHECK);
 517 
 518   // Stress restoring bytecodes
 519   if (StressRewriter) {
 520     restore_bytecodes();
 521     rewrite_bytecodes(CHECK);
 522   }
 523 
 524   // allocate constant pool cache, now that we've seen all the bytecodes
 525   make_constant_pool_cache(THREAD);
 526 
 527   // Restore bytecodes to their unrewritten state if there are exceptions
 528   // rewriting bytecodes or allocating the cpCache




   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 "interpreter/bytecodes.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "interpreter/rewriter.hpp"
  29 #include "memory/metaspaceShared.hpp"
  30 #include "memory/gcLocker.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "oops/generateOopMap.hpp"
  33 #include "prims/methodHandles.hpp"
  34 
  35 // Computes a CPC map (new_index -> original_index) for constant pool entries
  36 // that are referred to by the interpreter at runtime via the constant pool cache.
  37 // Also computes a CP map (original_index -> new_index).
  38 // Marks entries in CP which require additional processing.
  39 void Rewriter::compute_index_maps() {
  40   const int length  = _pool->length();
  41   init_maps(length);
  42   bool saw_mh_symbol = false;
  43   for (int i = 0; i < length; i++) {
  44     int tag = _pool->tag_at(i).value();
  45     switch (tag) {
  46       case JVM_CONSTANT_InterfaceMethodref:
  47       case JVM_CONSTANT_Fieldref          : // fall through
  48       case JVM_CONSTANT_Methodref         : // fall through
  49         add_cp_cache_entry(i);


 484   for (int i = len-1; i >= 0; i--) {
 485     Method* method = _methods->at(i);
 486     scan_method(method, false, &invokespecial_error);
 487     if (invokespecial_error) {
 488       // If you get an error here, there is no reversing bytecodes
 489       // This exception is stored for this class and no further attempt is
 490       // made at verifying or rewriting.
 491       THROW_MSG(vmSymbols::java_lang_InternalError(),
 492                 "This classfile overflows invokespecial for interfaces "
 493                 "and cannot be loaded");
 494       return;
 495      }
 496   }
 497 
 498   // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref
 499   // entries had to be added.
 500   patch_invokedynamic_bytecodes();
 501 }
 502 
 503 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
 504   if (!DumpSharedSpaces) {
 505     assert(!MetaspaceShared::is_in_shared_space(klass()), "archive methods must not be rewritten at run time");
 506   }
 507   ResourceMark rm(THREAD);
 508   Rewriter     rw(klass, klass->constants(), klass->methods(), CHECK);
 509   // (That's all, folks.)
 510 }
 511 

 512 Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS)
 513   : _klass(klass),
 514     _pool(cpool),
 515     _methods(methods)
 516 {
 517 
 518   // Rewrite bytecodes - exception here exits.
 519   rewrite_bytecodes(CHECK);
 520 
 521   // Stress restoring bytecodes
 522   if (StressRewriter) {
 523     restore_bytecodes();
 524     rewrite_bytecodes(CHECK);
 525   }
 526 
 527   // allocate constant pool cache, now that we've seen all the bytecodes
 528   make_constant_pool_cache(THREAD);
 529 
 530   // Restore bytecodes to their unrewritten state if there are exceptions
 531   // rewriting bytecodes or allocating the cpCache