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