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