src/share/vm/interpreter/rewriter.cpp

Wed, 02 Jan 2013 20:28:09 -0500

author
coleenp
date
Wed, 02 Jan 2013 20:28:09 -0500
changeset 4395
cc6a617fffd2
parent 4133
f6b0eb4e44cf
child 4643
f16e75e0cf11
permissions
-rw-r--r--

8005494: SIGSEGV in Rewriter::relocate_and_link() when testing Weblogic with CompressedOops and KlassPtrs
Summary: Relocate functions with jsr's when rewriting so not repeated after reading shared archive
Reviewed-by: twisti, jrose

     1 /*
     2  * Copyright (c) 1998, 2012, 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  */
    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"
    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);
    49         break;
    50       case JVM_CONSTANT_String:
    51       case JVM_CONSTANT_Object:
    52       case JVM_CONSTANT_MethodHandle      : // fall through
    53       case JVM_CONSTANT_MethodType        : // fall through
    54         add_resolved_references_entry(i);
    55         break;
    56       case JVM_CONSTANT_Utf8:
    57         if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle())
    58           saw_mh_symbol = true;
    59         break;
    60     }
    61   }
    63   // Record limits of resolved reference map for constant pool cache indices
    64   record_map_limits();
    66   guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
    67             "all cp cache indexes fit in a u2");
    69   if (saw_mh_symbol)
    70     _method_handle_invokers.initialize(length, (int)0);
    71 }
    73 // Unrewrite the bytecodes if an error occurs.
    74 void Rewriter::restore_bytecodes() {
    75   int len = _methods->length();
    77   for (int i = len-1; i >= 0; i--) {
    78     Method* method = _methods->at(i);
    79     scan_method(method, true);
    80   }
    81 }
    83 // Creates a constant pool cache given a CPC map
    84 void Rewriter::make_constant_pool_cache(TRAPS) {
    85   const int length = _cp_cache_map.length();
    86   ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
    87   ConstantPoolCache* cache =
    88       ConstantPoolCache::allocate(loader_data, length, CHECK);
    90   // initialize object cache in constant pool
    91   _pool->initialize_resolved_references(loader_data, _resolved_references_map,
    92                                         _resolved_reference_limit,
    93                                         CHECK);
    95   No_Safepoint_Verifier nsv;
    96   cache->initialize(_cp_cache_map, _invokedynamic_references_map);
    97   _pool->set_cache(cache);
    98   cache->set_constant_pool(_pool());
    99 }
   103 // The new finalization semantics says that registration of
   104 // finalizable objects must be performed on successful return from the
   105 // Object.<init> constructor.  We could implement this trivially if
   106 // <init> were never rewritten but since JVMTI allows this to occur, a
   107 // more complicated solution is required.  A special return bytecode
   108 // is used only by Object.<init> to signal the finalization
   109 // registration point.  Additionally local 0 must be preserved so it's
   110 // available to pass to the registration function.  For simplicty we
   111 // require that local 0 is never overwritten so it's available as an
   112 // argument for registration.
   114 void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) {
   115   RawBytecodeStream bcs(method);
   116   while (!bcs.is_last_bytecode()) {
   117     Bytecodes::Code opcode = bcs.raw_next();
   118     switch (opcode) {
   119       case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
   121       case Bytecodes::_istore:
   122       case Bytecodes::_lstore:
   123       case Bytecodes::_fstore:
   124       case Bytecodes::_dstore:
   125       case Bytecodes::_astore:
   126         if (bcs.get_index() != 0) continue;
   128         // fall through
   129       case Bytecodes::_istore_0:
   130       case Bytecodes::_lstore_0:
   131       case Bytecodes::_fstore_0:
   132       case Bytecodes::_dstore_0:
   133       case Bytecodes::_astore_0:
   134         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
   135                   "can't overwrite local 0 in Object.<init>");
   136         break;
   137     }
   138   }
   139 }
   142 // Rewrite a classfile-order CP index into a native-order CPC index.
   143 void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) {
   144   address p = bcp + offset;
   145   if (!reverse) {
   146     int  cp_index    = Bytes::get_Java_u2(p);
   147     int  cache_index = cp_entry_to_cp_cache(cp_index);
   148     Bytes::put_native_u2(p, cache_index);
   149     if (!_method_handle_invokers.is_empty())
   150       maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse);
   151   } else {
   152     int cache_index = Bytes::get_native_u2(p);
   153     int pool_index = cp_cache_entry_pool_index(cache_index);
   154     Bytes::put_Java_u2(p, pool_index);
   155     if (!_method_handle_invokers.is_empty())
   156       maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse);
   157   }
   158 }
   161 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
   162 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
   163   if (!reverse) {
   164     if ((*opc) == (u1)Bytecodes::_invokevirtual ||
   165         // allow invokespecial as an alias, although it would be very odd:
   166         (*opc) == (u1)Bytecodes::_invokespecial) {
   167       assert(_pool->tag_at(cp_index).is_method(), "wrong index");
   168       // Determine whether this is a signature-polymorphic method.
   169       if (cp_index >= _method_handle_invokers.length())  return;
   170       int status = _method_handle_invokers[cp_index];
   171       assert(status >= -1 && status <= 1, "oob tri-state");
   172       if (status == 0) {
   173         if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
   174             MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
   175                                                          _pool->name_ref_at(cp_index))) {
   176           // we may need a resolved_refs entry for the appendix
   177           add_invokedynamic_resolved_references_entries(cp_index, cache_index);
   178           status = +1;
   179         } else {
   180           status = -1;
   181         }
   182         _method_handle_invokers[cp_index] = status;
   183       }
   184       // We use a special internal bytecode for such methods (if non-static).
   185       // The basic reason for this is that such methods need an extra "appendix" argument
   186       // to transmit the call site's intended call type.
   187       if (status > 0) {
   188         (*opc) = (u1)Bytecodes::_invokehandle;
   189       }
   190     }
   191   } else {
   192     // Do not need to look at cp_index.
   193     if ((*opc) == (u1)Bytecodes::_invokehandle) {
   194       (*opc) = (u1)Bytecodes::_invokevirtual;
   195       // Ignore corner case of original _invokespecial instruction.
   196       // This is safe because (a) the signature polymorphic method was final, and
   197       // (b) the implementation of MethodHandle will not call invokespecial on it.
   198     }
   199   }
   200 }
   203 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) {
   204   address p = bcp + offset;
   205   assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode");
   206   if (!reverse) {
   207     int cp_index = Bytes::get_Java_u2(p);
   208     int cache_index = add_invokedynamic_cp_cache_entry(cp_index);
   209     add_invokedynamic_resolved_references_entries(cp_index, cache_index);
   210     // Replace the trailing four bytes with a CPC index for the dynamic
   211     // call site.  Unlike other CPC entries, there is one per bytecode,
   212     // not just one per distinct CP entry.  In other words, the
   213     // CPC-to-CP relation is many-to-one for invokedynamic entries.
   214     // This means we must use a larger index size than u2 to address
   215     // all these entries.  That is the main reason invokedynamic
   216     // must have a five-byte instruction format.  (Of course, other JVM
   217     // implementations can use the bytes for other purposes.)
   218     Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index));
   219     // Note: We use native_u4 format exclusively for 4-byte indexes.
   220   } else {
   221     // callsite index
   222     int cache_index = ConstantPool::decode_invokedynamic_index(
   223                         Bytes::get_native_u4(p));
   224     int cp_index = cp_cache_entry_pool_index(cache_index);
   225     assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index");
   226     // zero out 4 bytes
   227     Bytes::put_Java_u4(p, 0);
   228     Bytes::put_Java_u2(p, cp_index);
   229   }
   230 }
   233 // Rewrite some ldc bytecodes to _fast_aldc
   234 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide,
   235                                  bool reverse) {
   236   if (!reverse) {
   237     assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode");
   238     address p = bcp + offset;
   239     int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p);
   240     constantTag tag = _pool->tag_at(cp_index).value();
   241     if (tag.is_method_handle() || tag.is_method_type() || tag.is_string() || tag.is_object()) {
   242       int ref_index = cp_entry_to_resolved_references(cp_index);
   243       if (is_wide) {
   244         (*bcp) = Bytecodes::_fast_aldc_w;
   245         assert(ref_index == (u2)ref_index, "index overflow");
   246         Bytes::put_native_u2(p, ref_index);
   247       } else {
   248         (*bcp) = Bytecodes::_fast_aldc;
   249         assert(ref_index == (u1)ref_index, "index overflow");
   250         (*p) = (u1)ref_index;
   251       }
   252     }
   253   } else {
   254     Bytecodes::Code rewritten_bc =
   255               (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc);
   256     if ((*bcp) == rewritten_bc) {
   257       address p = bcp + offset;
   258       int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p);
   259       int pool_index = resolved_references_entry_to_pool_index(ref_index);
   260       if (is_wide) {
   261         (*bcp) = Bytecodes::_ldc_w;
   262         assert(pool_index == (u2)pool_index, "index overflow");
   263         Bytes::put_Java_u2(p, pool_index);
   264       } else {
   265         (*bcp) = Bytecodes::_ldc;
   266         assert(pool_index == (u1)pool_index, "index overflow");
   267         (*p) = (u1)pool_index;
   268       }
   269     }
   270   }
   271 }
   274 // Rewrites a method given the index_map information
   275 void Rewriter::scan_method(Method* method, bool reverse) {
   277   int nof_jsrs = 0;
   278   bool has_monitor_bytecodes = false;
   280   {
   281     // We cannot tolerate a GC in this block, because we've
   282     // cached the bytecodes in 'code_base'. If the Method*
   283     // moves, the bytecodes will also move.
   284     No_Safepoint_Verifier nsv;
   285     Bytecodes::Code c;
   287     // Bytecodes and their length
   288     const address code_base = method->code_base();
   289     const int code_length = method->code_size();
   291     int bc_length;
   292     for (int bci = 0; bci < code_length; bci += bc_length) {
   293       address bcp = code_base + bci;
   294       int prefix_length = 0;
   295       c = (Bytecodes::Code)(*bcp);
   297       // Since we have the code, see if we can get the length
   298       // directly. Some more complicated bytecodes will report
   299       // a length of zero, meaning we need to make another method
   300       // call to calculate the length.
   301       bc_length = Bytecodes::length_for(c);
   302       if (bc_length == 0) {
   303         bc_length = Bytecodes::length_at(method, bcp);
   305         // length_at will put us at the bytecode after the one modified
   306         // by 'wide'. We don't currently examine any of the bytecodes
   307         // modified by wide, but in case we do in the future...
   308         if (c == Bytecodes::_wide) {
   309           prefix_length = 1;
   310           c = (Bytecodes::Code)bcp[1];
   311         }
   312       }
   314       assert(bc_length != 0, "impossible bytecode length");
   316       switch (c) {
   317         case Bytecodes::_lookupswitch   : {
   318 #ifndef CC_INTERP
   319           Bytecode_lookupswitch bc(method, bcp);
   320           (*bcp) = (
   321             bc.number_of_pairs() < BinarySwitchThreshold
   322             ? Bytecodes::_fast_linearswitch
   323             : Bytecodes::_fast_binaryswitch
   324           );
   325 #endif
   326           break;
   327         }
   328         case Bytecodes::_fast_linearswitch:
   329         case Bytecodes::_fast_binaryswitch: {
   330 #ifndef CC_INTERP
   331           (*bcp) = Bytecodes::_lookupswitch;
   332 #endif
   333           break;
   334         }
   335         case Bytecodes::_getstatic      : // fall through
   336         case Bytecodes::_putstatic      : // fall through
   337         case Bytecodes::_getfield       : // fall through
   338         case Bytecodes::_putfield       : // fall through
   339         case Bytecodes::_invokevirtual  : // fall through
   340         case Bytecodes::_invokespecial  : // fall through
   341         case Bytecodes::_invokestatic   :
   342         case Bytecodes::_invokeinterface:
   343         case Bytecodes::_invokehandle   : // if reverse=true
   344           rewrite_member_reference(bcp, prefix_length+1, reverse);
   345           break;
   346         case Bytecodes::_invokedynamic:
   347           rewrite_invokedynamic(bcp, prefix_length+1, reverse);
   348           break;
   349         case Bytecodes::_ldc:
   350         case Bytecodes::_fast_aldc:  // if reverse=true
   351           maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse);
   352           break;
   353         case Bytecodes::_ldc_w:
   354         case Bytecodes::_fast_aldc_w:  // if reverse=true
   355           maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse);
   356           break;
   357         case Bytecodes::_jsr            : // fall through
   358         case Bytecodes::_jsr_w          : nof_jsrs++;                   break;
   359         case Bytecodes::_monitorenter   : // fall through
   360         case Bytecodes::_monitorexit    : has_monitor_bytecodes = true; break;
   361       }
   362     }
   363   }
   365   // Update access flags
   366   if (has_monitor_bytecodes) {
   367     method->set_has_monitor_bytecodes();
   368   }
   370   // The present of a jsr bytecode implies that the method might potentially
   371   // have to be rewritten, so we run the oopMapGenerator on the method
   372   if (nof_jsrs > 0) {
   373     method->set_has_jsrs();
   374     // Second pass will revisit this method.
   375     assert(method->has_jsrs(), "didn't we just set this?");
   376   }
   377 }
   379 // After constant pool is created, revisit methods containing jsrs.
   380 methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) {
   381   ResourceMark rm(THREAD);
   382   ResolveOopMapConflicts romc(method);
   383   methodHandle original_method = method;
   384   method = romc.do_potential_rewrite(CHECK_(methodHandle()));
   385   // Update monitor matching info.
   386   if (romc.monitor_safe()) {
   387     method->set_guaranteed_monitor_matching();
   388   }
   390   return method;
   391 }
   393 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
   394   ResourceMark rm(THREAD);
   395   Rewriter     rw(klass, klass->constants(), klass->methods(), CHECK);
   396   // (That's all, folks.)
   397 }
   400 Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS)
   401   : _klass(klass),
   402     _pool(cpool),
   403     _methods(methods)
   404 {
   405   assert(_pool->cache() == NULL, "constant pool cache must not be set yet");
   407   // determine index maps for Method* rewriting
   408   compute_index_maps();
   410   if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
   411     bool did_rewrite = false;
   412     int i = _methods->length();
   413     while (i-- > 0) {
   414       Method* method = _methods->at(i);
   415       if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
   416         // rewrite the return bytecodes of Object.<init> to register the
   417         // object for finalization if needed.
   418         methodHandle m(THREAD, method);
   419         rewrite_Object_init(m, CHECK);
   420         did_rewrite = true;
   421         break;
   422       }
   423     }
   424     assert(did_rewrite, "must find Object::<init> to rewrite it");
   425   }
   427   // rewrite methods, in two passes
   428   int len = _methods->length();
   430   for (int i = len-1; i >= 0; i--) {
   431     Method* method = _methods->at(i);
   432     scan_method(method);
   433   }
   435   // allocate constant pool cache, now that we've seen all the bytecodes
   436   make_constant_pool_cache(THREAD);
   438   // Restore bytecodes to their unrewritten state if there are exceptions
   439   // rewriting bytecodes or allocating the cpCache
   440   if (HAS_PENDING_EXCEPTION) {
   441     restore_bytecodes();
   442     return;
   443   }
   445   // Relocate after everything, but still do this under the is_rewritten flag,
   446   // so methods with jsrs in custom class lists in aren't attempted to be
   447   // rewritten in the RO section of the shared archive.
   448   // Relocated bytecodes don't have to be restored, only the cp cache entries
   449   for (int i = len-1; i >= 0; i--) {
   450     methodHandle m(THREAD, _methods->at(i));
   452     if (m->has_jsrs()) {
   453       m = rewrite_jsrs(m, THREAD);
   454       // Restore bytecodes to their unrewritten state if there are exceptions
   455       // relocating bytecodes.  If some are relocated, that is ok because that
   456       // doesn't affect constant pool to cpCache rewriting.
   457       if (HAS_PENDING_EXCEPTION) {
   458         restore_bytecodes();
   459         return;
   460       }
   461       // Method might have gotten rewritten.
   462       methods->at_put(i, m());
   463     }
   464   }
   465 }

mercurial