src/hotspot/share/interpreter/rewriter.cpp
changeset 47216 71c04702a3d5
parent 47103 a993ec29ec75
child 48794 ea0d0781c63c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1998, 2017, 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 "gc/shared/gcLocker.hpp"
       
    27 #include "interpreter/bytecodes.hpp"
       
    28 #include "interpreter/interpreter.hpp"
       
    29 #include "interpreter/rewriter.hpp"
       
    30 #include "memory/metadataFactory.hpp"
       
    31 #include "memory/metaspaceShared.hpp"
       
    32 #include "memory/resourceArea.hpp"
       
    33 #include "oops/generateOopMap.hpp"
       
    34 #include "prims/methodHandles.hpp"
       
    35 
       
    36 // Computes a CPC map (new_index -> original_index) for constant pool entries
       
    37 // that are referred to by the interpreter at runtime via the constant pool cache.
       
    38 // Also computes a CP map (original_index -> new_index).
       
    39 // Marks entries in CP which require additional processing.
       
    40 void Rewriter::compute_index_maps() {
       
    41   const int length  = _pool->length();
       
    42   init_maps(length);
       
    43   bool saw_mh_symbol = false;
       
    44   for (int i = 0; i < length; i++) {
       
    45     int tag = _pool->tag_at(i).value();
       
    46     switch (tag) {
       
    47       case JVM_CONSTANT_InterfaceMethodref:
       
    48       case JVM_CONSTANT_Fieldref          : // fall through
       
    49       case JVM_CONSTANT_Methodref         : // fall through
       
    50         add_cp_cache_entry(i);
       
    51         break;
       
    52       case JVM_CONSTANT_String:
       
    53       case JVM_CONSTANT_MethodHandle      : // fall through
       
    54       case JVM_CONSTANT_MethodType        : // fall through
       
    55         add_resolved_references_entry(i);
       
    56         break;
       
    57       case JVM_CONSTANT_Utf8:
       
    58         if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle() ||
       
    59             _pool->symbol_at(i) == vmSymbols::java_lang_invoke_VarHandle()) {
       
    60           saw_mh_symbol = true;
       
    61         }
       
    62         break;
       
    63     }
       
    64   }
       
    65 
       
    66   // Record limits of resolved reference map for constant pool cache indices
       
    67   record_map_limits();
       
    68 
       
    69   guarantee((int) _cp_cache_map.length() - 1 <= (int) ((u2)-1),
       
    70             "all cp cache indexes fit in a u2");
       
    71 
       
    72   if (saw_mh_symbol) {
       
    73     _method_handle_invokers.at_grow(length, 0);
       
    74   }
       
    75 }
       
    76 
       
    77 // Unrewrite the bytecodes if an error occurs.
       
    78 void Rewriter::restore_bytecodes() {
       
    79   int len = _methods->length();
       
    80   bool invokespecial_error = false;
       
    81 
       
    82   for (int i = len-1; i >= 0; i--) {
       
    83     Method* method = _methods->at(i);
       
    84     scan_method(method, true, &invokespecial_error);
       
    85     assert(!invokespecial_error, "reversing should not get an invokespecial error");
       
    86   }
       
    87 }
       
    88 
       
    89 // Creates a constant pool cache given a CPC map
       
    90 void Rewriter::make_constant_pool_cache(TRAPS) {
       
    91   ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
       
    92   ConstantPoolCache* cache =
       
    93       ConstantPoolCache::allocate(loader_data, _cp_cache_map,
       
    94                                   _invokedynamic_cp_cache_map,
       
    95                                   _invokedynamic_references_map, CHECK);
       
    96 
       
    97   // initialize object cache in constant pool
       
    98   _pool->set_cache(cache);
       
    99   cache->set_constant_pool(_pool());
       
   100 
       
   101   // _resolved_references is stored in pool->cache(), so need to be done after
       
   102   // the above lines.
       
   103   _pool->initialize_resolved_references(loader_data, _resolved_references_map,
       
   104                                         _resolved_reference_limit,
       
   105                                         THREAD);
       
   106 
       
   107   // Clean up constant pool cache if initialize_resolved_references() failed.
       
   108   if (HAS_PENDING_EXCEPTION) {
       
   109     MetadataFactory::free_metadata(loader_data, cache);
       
   110     _pool->set_cache(NULL);  // so the verifier isn't confused
       
   111   }
       
   112 
       
   113   DEBUG_ONLY(
       
   114   if (DumpSharedSpaces) {
       
   115     cache->verify_just_initialized();
       
   116   })
       
   117 }
       
   118 
       
   119 
       
   120 
       
   121 // The new finalization semantics says that registration of
       
   122 // finalizable objects must be performed on successful return from the
       
   123 // Object.<init> constructor.  We could implement this trivially if
       
   124 // <init> were never rewritten but since JVMTI allows this to occur, a
       
   125 // more complicated solution is required.  A special return bytecode
       
   126 // is used only by Object.<init> to signal the finalization
       
   127 // registration point.  Additionally local 0 must be preserved so it's
       
   128 // available to pass to the registration function.  For simplicity we
       
   129 // require that local 0 is never overwritten so it's available as an
       
   130 // argument for registration.
       
   131 
       
   132 void Rewriter::rewrite_Object_init(const methodHandle& method, TRAPS) {
       
   133   RawBytecodeStream bcs(method);
       
   134   while (!bcs.is_last_bytecode()) {
       
   135     Bytecodes::Code opcode = bcs.raw_next();
       
   136     switch (opcode) {
       
   137       case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
       
   138 
       
   139       case Bytecodes::_istore:
       
   140       case Bytecodes::_lstore:
       
   141       case Bytecodes::_fstore:
       
   142       case Bytecodes::_dstore:
       
   143       case Bytecodes::_astore:
       
   144         if (bcs.get_index() != 0) continue;
       
   145 
       
   146         // fall through
       
   147       case Bytecodes::_istore_0:
       
   148       case Bytecodes::_lstore_0:
       
   149       case Bytecodes::_fstore_0:
       
   150       case Bytecodes::_dstore_0:
       
   151       case Bytecodes::_astore_0:
       
   152         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
       
   153                   "can't overwrite local 0 in Object.<init>");
       
   154         break;
       
   155 
       
   156       default:
       
   157         break;
       
   158     }
       
   159   }
       
   160 }
       
   161 
       
   162 
       
   163 // Rewrite a classfile-order CP index into a native-order CPC index.
       
   164 void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) {
       
   165   address p = bcp + offset;
       
   166   if (!reverse) {
       
   167     int  cp_index    = Bytes::get_Java_u2(p);
       
   168     int  cache_index = cp_entry_to_cp_cache(cp_index);
       
   169     Bytes::put_native_u2(p, cache_index);
       
   170     if (!_method_handle_invokers.is_empty())
       
   171       maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse);
       
   172   } else {
       
   173     int cache_index = Bytes::get_native_u2(p);
       
   174     int pool_index = cp_cache_entry_pool_index(cache_index);
       
   175     Bytes::put_Java_u2(p, pool_index);
       
   176     if (!_method_handle_invokers.is_empty())
       
   177       maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse);
       
   178   }
       
   179 }
       
   180 
       
   181 // If the constant pool entry for invokespecial is InterfaceMethodref,
       
   182 // we need to add a separate cpCache entry for its resolution, because it is
       
   183 // different than the resolution for invokeinterface with InterfaceMethodref.
       
   184 // These cannot share cpCache entries.
       
   185 void Rewriter::rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error) {
       
   186   address p = bcp + offset;
       
   187   if (!reverse) {
       
   188     int cp_index = Bytes::get_Java_u2(p);
       
   189     if (_pool->tag_at(cp_index).is_interface_method()) {
       
   190       int cache_index = add_invokespecial_cp_cache_entry(cp_index);
       
   191       if (cache_index != (int)(jushort) cache_index) {
       
   192         *invokespecial_error = true;
       
   193       }
       
   194       Bytes::put_native_u2(p, cache_index);
       
   195     } else {
       
   196       rewrite_member_reference(bcp, offset, reverse);
       
   197     }
       
   198   } else {
       
   199     rewrite_member_reference(bcp, offset, reverse);
       
   200   }
       
   201 }
       
   202 
       
   203 
       
   204 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
       
   205 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
       
   206   if (!reverse) {
       
   207     if ((*opc) == (u1)Bytecodes::_invokevirtual ||
       
   208         // allow invokespecial as an alias, although it would be very odd:
       
   209         (*opc) == (u1)Bytecodes::_invokespecial) {
       
   210       assert(_pool->tag_at(cp_index).is_method(), "wrong index");
       
   211       // Determine whether this is a signature-polymorphic method.
       
   212       if (cp_index >= _method_handle_invokers.length())  return;
       
   213       int status = _method_handle_invokers.at(cp_index);
       
   214       assert(status >= -1 && status <= 1, "oob tri-state");
       
   215       if (status == 0) {
       
   216         if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
       
   217             MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
       
   218                                                          _pool->name_ref_at(cp_index))) {
       
   219           // we may need a resolved_refs entry for the appendix
       
   220           add_invokedynamic_resolved_references_entries(cp_index, cache_index);
       
   221           status = +1;
       
   222         } else if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_VarHandle() &&
       
   223                    MethodHandles::is_signature_polymorphic_name(SystemDictionary::VarHandle_klass(),
       
   224                                                                 _pool->name_ref_at(cp_index))) {
       
   225           // we may need a resolved_refs entry for the appendix
       
   226           add_invokedynamic_resolved_references_entries(cp_index, cache_index);
       
   227           status = +1;
       
   228         } else {
       
   229           status = -1;
       
   230         }
       
   231         _method_handle_invokers.at(cp_index) = status;
       
   232       }
       
   233       // We use a special internal bytecode for such methods (if non-static).
       
   234       // The basic reason for this is that such methods need an extra "appendix" argument
       
   235       // to transmit the call site's intended call type.
       
   236       if (status > 0) {
       
   237         (*opc) = (u1)Bytecodes::_invokehandle;
       
   238       }
       
   239     }
       
   240   } else {
       
   241     // Do not need to look at cp_index.
       
   242     if ((*opc) == (u1)Bytecodes::_invokehandle) {
       
   243       (*opc) = (u1)Bytecodes::_invokevirtual;
       
   244       // Ignore corner case of original _invokespecial instruction.
       
   245       // This is safe because (a) the signature polymorphic method was final, and
       
   246       // (b) the implementation of MethodHandle will not call invokespecial on it.
       
   247     }
       
   248   }
       
   249 }
       
   250 
       
   251 
       
   252 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) {
       
   253   address p = bcp + offset;
       
   254   assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode");
       
   255   if (!reverse) {
       
   256     int cp_index = Bytes::get_Java_u2(p);
       
   257     int cache_index = add_invokedynamic_cp_cache_entry(cp_index);
       
   258     int resolved_index = add_invokedynamic_resolved_references_entries(cp_index, cache_index);
       
   259     // Replace the trailing four bytes with a CPC index for the dynamic
       
   260     // call site.  Unlike other CPC entries, there is one per bytecode,
       
   261     // not just one per distinct CP entry.  In other words, the
       
   262     // CPC-to-CP relation is many-to-one for invokedynamic entries.
       
   263     // This means we must use a larger index size than u2 to address
       
   264     // all these entries.  That is the main reason invokedynamic
       
   265     // must have a five-byte instruction format.  (Of course, other JVM
       
   266     // implementations can use the bytes for other purposes.)
       
   267     // Note: We use native_u4 format exclusively for 4-byte indexes.
       
   268     Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index));
       
   269     // add the bcp in case we need to patch this bytecode if we also find a
       
   270     // invokespecial/InterfaceMethodref in the bytecode stream
       
   271     _patch_invokedynamic_bcps->push(p);
       
   272     _patch_invokedynamic_refs->push(resolved_index);
       
   273   } else {
       
   274     int cache_index = ConstantPool::decode_invokedynamic_index(
       
   275                         Bytes::get_native_u4(p));
       
   276     // We will reverse the bytecode rewriting _after_ adjusting them.
       
   277     // Adjust the cache index by offset to the invokedynamic entries in the
       
   278     // cpCache plus the delta if the invokedynamic bytecodes were adjusted.
       
   279     int adjustment = cp_cache_delta() + _first_iteration_cp_cache_limit;
       
   280     int cp_index = invokedynamic_cp_cache_entry_pool_index(cache_index - adjustment);
       
   281     assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index");
       
   282     // zero out 4 bytes
       
   283     Bytes::put_Java_u4(p, 0);
       
   284     Bytes::put_Java_u2(p, cp_index);
       
   285   }
       
   286 }
       
   287 
       
   288 void Rewriter::patch_invokedynamic_bytecodes() {
       
   289   // If the end of the cp_cache is the same as after initializing with the
       
   290   // cpool, nothing needs to be done.  Invokedynamic bytecodes are at the
       
   291   // correct offsets. ie. no invokespecials added
       
   292   int delta = cp_cache_delta();
       
   293   if (delta > 0) {
       
   294     int length = _patch_invokedynamic_bcps->length();
       
   295     assert(length == _patch_invokedynamic_refs->length(),
       
   296            "lengths should match");
       
   297     for (int i = 0; i < length; i++) {
       
   298       address p = _patch_invokedynamic_bcps->at(i);
       
   299       int cache_index = ConstantPool::decode_invokedynamic_index(
       
   300                           Bytes::get_native_u4(p));
       
   301       Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta));
       
   302 
       
   303       // invokedynamic resolved references map also points to cp cache and must
       
   304       // add delta to each.
       
   305       int resolved_index = _patch_invokedynamic_refs->at(i);
       
   306       for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
       
   307         assert(_invokedynamic_references_map.at(resolved_index + entry) == cache_index,
       
   308              "should be the same index");
       
   309         _invokedynamic_references_map.at_put(resolved_index+entry,
       
   310                                              cache_index + delta);
       
   311       }
       
   312     }
       
   313   }
       
   314 }
       
   315 
       
   316 
       
   317 // Rewrite some ldc bytecodes to _fast_aldc
       
   318 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide,
       
   319                                  bool reverse) {
       
   320   if (!reverse) {
       
   321     assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode");
       
   322     address p = bcp + offset;
       
   323     int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p);
       
   324     constantTag tag = _pool->tag_at(cp_index).value();
       
   325     if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) {
       
   326       int ref_index = cp_entry_to_resolved_references(cp_index);
       
   327       if (is_wide) {
       
   328         (*bcp) = Bytecodes::_fast_aldc_w;
       
   329         assert(ref_index == (u2)ref_index, "index overflow");
       
   330         Bytes::put_native_u2(p, ref_index);
       
   331       } else {
       
   332         (*bcp) = Bytecodes::_fast_aldc;
       
   333         assert(ref_index == (u1)ref_index, "index overflow");
       
   334         (*p) = (u1)ref_index;
       
   335       }
       
   336     }
       
   337   } else {
       
   338     Bytecodes::Code rewritten_bc =
       
   339               (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc);
       
   340     if ((*bcp) == rewritten_bc) {
       
   341       address p = bcp + offset;
       
   342       int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p);
       
   343       int pool_index = resolved_references_entry_to_pool_index(ref_index);
       
   344       if (is_wide) {
       
   345         (*bcp) = Bytecodes::_ldc_w;
       
   346         assert(pool_index == (u2)pool_index, "index overflow");
       
   347         Bytes::put_Java_u2(p, pool_index);
       
   348       } else {
       
   349         (*bcp) = Bytecodes::_ldc;
       
   350         assert(pool_index == (u1)pool_index, "index overflow");
       
   351         (*p) = (u1)pool_index;
       
   352       }
       
   353     }
       
   354   }
       
   355 }
       
   356 
       
   357 
       
   358 // Rewrites a method given the index_map information
       
   359 void Rewriter::scan_method(Method* method, bool reverse, bool* invokespecial_error) {
       
   360 
       
   361   int nof_jsrs = 0;
       
   362   bool has_monitor_bytecodes = false;
       
   363   Bytecodes::Code c;
       
   364 
       
   365   // Bytecodes and their length
       
   366   const address code_base = method->code_base();
       
   367   const int code_length = method->code_size();
       
   368 
       
   369   int bc_length;
       
   370   for (int bci = 0; bci < code_length; bci += bc_length) {
       
   371     address bcp = code_base + bci;
       
   372     int prefix_length = 0;
       
   373     c = (Bytecodes::Code)(*bcp);
       
   374 
       
   375     // Since we have the code, see if we can get the length
       
   376     // directly. Some more complicated bytecodes will report
       
   377     // a length of zero, meaning we need to make another method
       
   378     // call to calculate the length.
       
   379     bc_length = Bytecodes::length_for(c);
       
   380     if (bc_length == 0) {
       
   381       bc_length = Bytecodes::length_at(method, bcp);
       
   382 
       
   383       // length_at will put us at the bytecode after the one modified
       
   384       // by 'wide'. We don't currently examine any of the bytecodes
       
   385       // modified by wide, but in case we do in the future...
       
   386       if (c == Bytecodes::_wide) {
       
   387         prefix_length = 1;
       
   388         c = (Bytecodes::Code)bcp[1];
       
   389       }
       
   390     }
       
   391 
       
   392     assert(bc_length != 0, "impossible bytecode length");
       
   393 
       
   394     switch (c) {
       
   395       case Bytecodes::_lookupswitch   : {
       
   396 #ifndef CC_INTERP
       
   397         Bytecode_lookupswitch bc(method, bcp);
       
   398         (*bcp) = (
       
   399           bc.number_of_pairs() < BinarySwitchThreshold
       
   400           ? Bytecodes::_fast_linearswitch
       
   401           : Bytecodes::_fast_binaryswitch
       
   402         );
       
   403 #endif
       
   404         break;
       
   405       }
       
   406       case Bytecodes::_fast_linearswitch:
       
   407       case Bytecodes::_fast_binaryswitch: {
       
   408 #ifndef CC_INTERP
       
   409         (*bcp) = Bytecodes::_lookupswitch;
       
   410 #endif
       
   411         break;
       
   412       }
       
   413 
       
   414       case Bytecodes::_invokespecial  : {
       
   415         rewrite_invokespecial(bcp, prefix_length+1, reverse, invokespecial_error);
       
   416         break;
       
   417       }
       
   418 
       
   419       case Bytecodes::_putstatic      :
       
   420       case Bytecodes::_putfield       : {
       
   421         if (!reverse) {
       
   422           // Check if any final field of the class given as parameter is modified
       
   423           // outside of initializer methods of the class. Fields that are modified
       
   424           // are marked with a flag. For marked fields, the compilers do not perform
       
   425           // constant folding (as the field can be changed after initialization).
       
   426           //
       
   427           // The check is performed after verification and only if verification has
       
   428           // succeeded. Therefore, the class is guaranteed to be well-formed.
       
   429           InstanceKlass* klass = method->method_holder();
       
   430           u2 bc_index = Bytes::get_Java_u2(bcp + prefix_length + 1);
       
   431           constantPoolHandle cp(method->constants());
       
   432           Symbol* ref_class_name = cp->klass_name_at(cp->klass_ref_index_at(bc_index));
       
   433 
       
   434           if (klass->name() == ref_class_name) {
       
   435             Symbol* field_name = cp->name_ref_at(bc_index);
       
   436             Symbol* field_sig = cp->signature_ref_at(bc_index);
       
   437 
       
   438             fieldDescriptor fd;
       
   439             if (klass->find_field(field_name, field_sig, &fd) != NULL) {
       
   440               if (fd.access_flags().is_final()) {
       
   441                 if (fd.access_flags().is_static()) {
       
   442                   if (!method->is_static_initializer()) {
       
   443                     fd.set_has_initialized_final_update(true);
       
   444                   }
       
   445                 } else {
       
   446                   if (!method->is_object_initializer()) {
       
   447                     fd.set_has_initialized_final_update(true);
       
   448                   }
       
   449                 }
       
   450               }
       
   451             }
       
   452           }
       
   453         }
       
   454       }
       
   455       // fall through
       
   456       case Bytecodes::_getstatic      : // fall through
       
   457       case Bytecodes::_getfield       : // fall through
       
   458       case Bytecodes::_invokevirtual  : // fall through
       
   459       case Bytecodes::_invokestatic   :
       
   460       case Bytecodes::_invokeinterface:
       
   461       case Bytecodes::_invokehandle   : // if reverse=true
       
   462         rewrite_member_reference(bcp, prefix_length+1, reverse);
       
   463         break;
       
   464       case Bytecodes::_invokedynamic:
       
   465         rewrite_invokedynamic(bcp, prefix_length+1, reverse);
       
   466         break;
       
   467       case Bytecodes::_ldc:
       
   468       case Bytecodes::_fast_aldc:  // if reverse=true
       
   469         maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse);
       
   470         break;
       
   471       case Bytecodes::_ldc_w:
       
   472       case Bytecodes::_fast_aldc_w:  // if reverse=true
       
   473         maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse);
       
   474         break;
       
   475       case Bytecodes::_jsr            : // fall through
       
   476       case Bytecodes::_jsr_w          : nof_jsrs++;                   break;
       
   477       case Bytecodes::_monitorenter   : // fall through
       
   478       case Bytecodes::_monitorexit    : has_monitor_bytecodes = true; break;
       
   479 
       
   480       default: break;
       
   481     }
       
   482   }
       
   483 
       
   484   // Update access flags
       
   485   if (has_monitor_bytecodes) {
       
   486     method->set_has_monitor_bytecodes();
       
   487   }
       
   488 
       
   489   // The present of a jsr bytecode implies that the method might potentially
       
   490   // have to be rewritten, so we run the oopMapGenerator on the method
       
   491   if (nof_jsrs > 0) {
       
   492     method->set_has_jsrs();
       
   493     // Second pass will revisit this method.
       
   494     assert(method->has_jsrs(), "didn't we just set this?");
       
   495   }
       
   496 }
       
   497 
       
   498 // After constant pool is created, revisit methods containing jsrs.
       
   499 methodHandle Rewriter::rewrite_jsrs(const methodHandle& method, TRAPS) {
       
   500   ResourceMark rm(THREAD);
       
   501   ResolveOopMapConflicts romc(method);
       
   502   methodHandle new_method = romc.do_potential_rewrite(CHECK_(methodHandle()));
       
   503   // Update monitor matching info.
       
   504   if (romc.monitor_safe()) {
       
   505     new_method->set_guaranteed_monitor_matching();
       
   506   }
       
   507 
       
   508   return new_method;
       
   509 }
       
   510 
       
   511 void Rewriter::rewrite_bytecodes(TRAPS) {
       
   512   assert(_pool->cache() == NULL, "constant pool cache must not be set yet");
       
   513 
       
   514   // determine index maps for Method* rewriting
       
   515   compute_index_maps();
       
   516 
       
   517   if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
       
   518     bool did_rewrite = false;
       
   519     int i = _methods->length();
       
   520     while (i-- > 0) {
       
   521       Method* method = _methods->at(i);
       
   522       if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
       
   523         // rewrite the return bytecodes of Object.<init> to register the
       
   524         // object for finalization if needed.
       
   525         methodHandle m(THREAD, method);
       
   526         rewrite_Object_init(m, CHECK);
       
   527         did_rewrite = true;
       
   528         break;
       
   529       }
       
   530     }
       
   531     assert(did_rewrite, "must find Object::<init> to rewrite it");
       
   532   }
       
   533 
       
   534   // rewrite methods, in two passes
       
   535   int len = _methods->length();
       
   536   bool invokespecial_error = false;
       
   537 
       
   538   for (int i = len-1; i >= 0; i--) {
       
   539     Method* method = _methods->at(i);
       
   540     scan_method(method, false, &invokespecial_error);
       
   541     if (invokespecial_error) {
       
   542       // If you get an error here, there is no reversing bytecodes
       
   543       // This exception is stored for this class and no further attempt is
       
   544       // made at verifying or rewriting.
       
   545       THROW_MSG(vmSymbols::java_lang_InternalError(),
       
   546                 "This classfile overflows invokespecial for interfaces "
       
   547                 "and cannot be loaded");
       
   548       return;
       
   549      }
       
   550   }
       
   551 
       
   552   // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref
       
   553   // entries had to be added.
       
   554   patch_invokedynamic_bytecodes();
       
   555 }
       
   556 
       
   557 void Rewriter::rewrite(InstanceKlass* klass, TRAPS) {
       
   558   if (!DumpSharedSpaces) {
       
   559     assert(!MetaspaceShared::is_in_shared_space(klass), "archive methods must not be rewritten at run time");
       
   560   }
       
   561   ResourceMark rm(THREAD);
       
   562   Rewriter     rw(klass, klass->constants(), klass->methods(), CHECK);
       
   563   // (That's all, folks.)
       
   564 }
       
   565 
       
   566 Rewriter::Rewriter(InstanceKlass* klass, const constantPoolHandle& cpool, Array<Method*>* methods, TRAPS)
       
   567   : _klass(klass),
       
   568     _pool(cpool),
       
   569     _methods(methods),
       
   570     _cp_map(cpool->length()),
       
   571     _cp_cache_map(cpool->length() / 2),
       
   572     _reference_map(cpool->length()),
       
   573     _resolved_references_map(cpool->length() / 2),
       
   574     _invokedynamic_references_map(cpool->length() / 2),
       
   575     _method_handle_invokers(cpool->length()),
       
   576     _invokedynamic_cp_cache_map(cpool->length() / 4)
       
   577 {
       
   578 
       
   579   // Rewrite bytecodes - exception here exits.
       
   580   rewrite_bytecodes(CHECK);
       
   581 
       
   582   // Stress restoring bytecodes
       
   583   if (StressRewriter) {
       
   584     restore_bytecodes();
       
   585     rewrite_bytecodes(CHECK);
       
   586   }
       
   587 
       
   588   // allocate constant pool cache, now that we've seen all the bytecodes
       
   589   make_constant_pool_cache(THREAD);
       
   590 
       
   591   // Restore bytecodes to their unrewritten state if there are exceptions
       
   592   // rewriting bytecodes or allocating the cpCache
       
   593   if (HAS_PENDING_EXCEPTION) {
       
   594     restore_bytecodes();
       
   595     return;
       
   596   }
       
   597 
       
   598   // Relocate after everything, but still do this under the is_rewritten flag,
       
   599   // so methods with jsrs in custom class lists in aren't attempted to be
       
   600   // rewritten in the RO section of the shared archive.
       
   601   // Relocated bytecodes don't have to be restored, only the cp cache entries
       
   602   int len = _methods->length();
       
   603   for (int i = len-1; i >= 0; i--) {
       
   604     methodHandle m(THREAD, _methods->at(i));
       
   605 
       
   606     if (m->has_jsrs()) {
       
   607       m = rewrite_jsrs(m, THREAD);
       
   608       // Restore bytecodes to their unrewritten state if there are exceptions
       
   609       // relocating bytecodes.  If some are relocated, that is ok because that
       
   610       // doesn't affect constant pool to cpCache rewriting.
       
   611       if (HAS_PENDING_EXCEPTION) {
       
   612         restore_bytecodes();
       
   613         return;
       
   614       }
       
   615       // Method might have gotten rewritten.
       
   616       methods->at_put(i, m());
       
   617     }
       
   618   }
       
   619 }