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/resolvedFieldEntry.hpp"
 36 #include "oops/resolvedIndyEntry.hpp"
 37 #include "prims/methodHandles.hpp"
 38 #include "runtime/arguments.hpp"
 39 #include "runtime/fieldDescriptor.inline.hpp"
 40 #include "runtime/handles.inline.hpp"
 41 #include "utilities/checkedCast.hpp"
 42 
 43 // Computes a CPC map (new_index -> original_index) for constant pool entries
 44 // that are referred to by the interpreter at runtime via the constant pool cache.
 45 // Also computes a CP map (original_index -> new_index).
 46 // Marks entries in CP which require additional processing.
 47 void Rewriter::compute_index_maps() {
 48   const int length  = _pool->length();
 49   init_maps(length);
 50   bool saw_mh_symbol = false;
 51   for (int i = 0; i < length; i++) {
 52     int tag = _pool->tag_at(i).value();
 53     switch (tag) {
 54       case JVM_CONSTANT_Fieldref          :
 55         _cp_map.at_put(i, _field_entry_index);
 56         _field_entry_index++;
 57         _initialized_field_entries.push(ResolvedFieldEntry((u2)i));
 58         break;
 59       case JVM_CONSTANT_InterfaceMethodref: // fall through
 60       case JVM_CONSTANT_Methodref         :
 61         add_cp_cache_entry(i);
 62         break;
 63       case JVM_CONSTANT_Dynamic:
 64         assert(_pool->has_dynamic_constant(), "constant pool's _has_dynamic_constant flag not set");
 65         add_resolved_references_entry(i);
 66         break;
 67       case JVM_CONSTANT_String            : // fall through
 68       case JVM_CONSTANT_MethodHandle      : // fall through
 69       case JVM_CONSTANT_MethodType        : // fall through
 70         add_resolved_references_entry(i);
 71         break;
 72       case JVM_CONSTANT_Utf8:
 73         if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle() ||
 74             _pool->symbol_at(i) == vmSymbols::java_lang_invoke_VarHandle()) {
 75           saw_mh_symbol = true;
 76         }
 77         break;
 78     }
 79   }
 80 
 81   // Record limits of resolved reference map for constant pool cache indices
 82   record_map_limits();
 83 
 84   guarantee((int) _cp_cache_map.length() - 1 <= (int) ((u2)-1),
 85             "all cp cache indexes fit in a u2");
 86 
 87   if (saw_mh_symbol) {
 88     _method_handle_invokers.at_grow(length, 0);
 89   }
 90 }
 91 
 92 // Unrewrite the bytecodes if an error occurs.
 93 void Rewriter::restore_bytecodes(Thread* thread) {
 94   int len = _methods->length();
 95   bool invokespecial_error = false;
 96 
 97   for (int i = len-1; i >= 0; i--) {
 98     Method* method = _methods->at(i);
 99     scan_method(thread, method, true, &invokespecial_error);
100     assert(!invokespecial_error, "reversing should not get an invokespecial error");
101   }
102 }
103 
104 // Creates a constant pool cache given a CPC map
105 void Rewriter::make_constant_pool_cache(TRAPS) {
106   ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
107   ConstantPoolCache* cache =
108       ConstantPoolCache::allocate(loader_data, _cp_cache_map,
109                                   _invokedynamic_references_map, _initialized_indy_entries, _initialized_field_entries, CHECK);
110 
111   // initialize object cache in constant pool
112   _pool->set_cache(cache);
113   cache->set_constant_pool(_pool());
114 
115   // _resolved_references is stored in pool->cache(), so need to be done after
116   // the above lines.
117   _pool->initialize_resolved_references(loader_data, _resolved_references_map,
118                                         _resolved_reference_limit,
119                                         THREAD);
120 #if INCLUDE_CDS
121   if (!HAS_PENDING_EXCEPTION && Arguments::is_dumping_archive()) {
122     if (_pool->pool_holder()->is_shared()) {
123       assert(DynamicDumpSharedSpaces, "must be");
124       // We are linking a shared class from the base archive. This
125       // class won't be written into the dynamic archive, so there's no
126       // need to save its CpCaches.
127     } else {
128       cache->save_for_archive(THREAD);
129     }
130   }
131 #endif
132 
133   // Clean up constant pool cache if initialize_resolved_references() failed.
134   if (HAS_PENDING_EXCEPTION) {
135     MetadataFactory::free_metadata(loader_data, cache);
136     _pool->set_cache(nullptr);  // so the verifier isn't confused
137   }
138 }
139 
140 
141 
142 // The new finalization semantics says that registration of
143 // finalizable objects must be performed on successful return from the
144 // Object.<init> constructor.  We could implement this trivially if
145 // <init> were never rewritten but since JVMTI allows this to occur, a
146 // more complicated solution is required.  A special return bytecode
147 // is used only by Object.<init> to signal the finalization
148 // registration point.  Additionally local 0 must be preserved so it's
149 // available to pass to the registration function.  For simplicity we
150 // require that local 0 is never overwritten so it's available as an
151 // argument for registration.
152 
153 void Rewriter::rewrite_Object_init(const methodHandle& method, TRAPS) {
154   RawBytecodeStream bcs(method);
155   while (!bcs.is_last_bytecode()) {
156     Bytecodes::Code opcode = bcs.raw_next();
157     switch (opcode) {
158       case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
159 
160       case Bytecodes::_istore:
161       case Bytecodes::_lstore:
162       case Bytecodes::_fstore:
163       case Bytecodes::_dstore:
164       case Bytecodes::_astore:
165         if (bcs.get_index() != 0) continue;
166 
167         // fall through
168       case Bytecodes::_istore_0:
169       case Bytecodes::_lstore_0:
170       case Bytecodes::_fstore_0:
171       case Bytecodes::_dstore_0:
172       case Bytecodes::_astore_0:
173         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
174                   "can't overwrite local 0 in Object.<init>");
175         break;
176 
177       default:
178         break;
179     }
180   }
181 }
182 
183 
184 void Rewriter::rewrite_field_reference(address bcp, int offset, bool reverse) {
185   address p = bcp + offset;
186   if (!reverse) {
187     int cp_index = Bytes::get_Java_u2(p);
188     int field_entry_index = _cp_map.at(cp_index);
189     Bytes::put_native_u2(p, checked_cast<u2>(field_entry_index));
190   } else {
191     int field_entry_index = Bytes::get_native_u2(p);
192     int pool_index = _initialized_field_entries.at(field_entry_index).constant_pool_index();
193     Bytes::put_Java_u2(p, checked_cast<u2>(pool_index));
194   }
195 }
196 
197 // Rewrite a classfile-order CP index into a native-order CPC index.
198 void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) {
199   address p = bcp + offset;
200   if (!reverse) {
201     int cp_index    = Bytes::get_Java_u2(p);
202     int  cache_index = cp_entry_to_cp_cache(cp_index);
203     Bytes::put_native_u2(p, (u2)cache_index);
204     if (!_method_handle_invokers.is_empty())
205       maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse);
206   } else {
207     int cache_index = Bytes::get_native_u2(p);
208     int pool_index = cp_cache_entry_pool_index(cache_index);
209     Bytes::put_Java_u2(p, (u2)pool_index);
210     if (!_method_handle_invokers.is_empty())
211       maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse);
212   }
213 }
214 
215 // If the constant pool entry for invokespecial is InterfaceMethodref,
216 // we need to add a separate cpCache entry for its resolution, because it is
217 // different than the resolution for invokeinterface with InterfaceMethodref.
218 // These cannot share cpCache entries.
219 void Rewriter::rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error) {
220   address p = bcp + offset;
221   if (!reverse) {
222     int cp_index = Bytes::get_Java_u2(p);
223     if (_pool->tag_at(cp_index).is_interface_method()) {
224       int cache_index = add_invokespecial_cp_cache_entry(cp_index);
225       if (cache_index != (int)(jushort) cache_index) {
226         *invokespecial_error = true;
227       }
228       Bytes::put_native_u2(p, (u2)cache_index);
229     } else {
230       rewrite_member_reference(bcp, offset, reverse);
231     }
232   } else {
233     rewrite_member_reference(bcp, offset, reverse);
234   }
235 }
236 

237 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
238 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
239   if (!reverse) {
240     if ((*opc) == (u1)Bytecodes::_invokevirtual ||
241         // allow invokespecial as an alias, although it would be very odd:
242         (*opc) == (u1)Bytecodes::_invokespecial) {
243       assert(_pool->tag_at(cp_index).is_method(), "wrong index");
244       // Determine whether this is a signature-polymorphic method.
245       if (cp_index >= _method_handle_invokers.length())  return;
246       int status = _method_handle_invokers.at(cp_index);
247       assert(status >= -1 && status <= 1, "oob tri-state");
248       if (status == 0) {
249         if (_pool->uncached_klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
250             MethodHandles::is_signature_polymorphic_name(vmClasses::MethodHandle_klass(),
251                                                          _pool->uncached_name_ref_at(cp_index))) {
252           // we may need a resolved_refs entry for the appendix
253           add_invokedynamic_resolved_references_entry(cp_index, cache_index);
254           status = +1;
255         } else if (_pool->uncached_klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_VarHandle() &&
256                    MethodHandles::is_signature_polymorphic_name(vmClasses::VarHandle_klass(),
257                                                                 _pool->uncached_name_ref_at(cp_index))) {
258           // we may need a resolved_refs entry for the appendix
259           add_invokedynamic_resolved_references_entry(cp_index, cache_index);
260           status = +1;
261         } else {
262           status = -1;
263         }
264         _method_handle_invokers.at(cp_index) = status;
265       }
266       // We use a special internal bytecode for such methods (if non-static).
267       // The basic reason for this is that such methods need an extra "appendix" argument
268       // to transmit the call site's intended call type.
269       if (status > 0) {
270         (*opc) = (u1)Bytecodes::_invokehandle;
271       }
272     }
273   } else {
274     // Do not need to look at cp_index.
275     if ((*opc) == (u1)Bytecodes::_invokehandle) {
276       (*opc) = (u1)Bytecodes::_invokevirtual;
277       // Ignore corner case of original _invokespecial instruction.
278       // This is safe because (a) the signature polymorphic method was final, and
279       // (b) the implementation of MethodHandle will not call invokespecial on it.
280     }
281   }
282 }
283 
284 
285 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) {
286   address p = bcp + offset;
287   assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode");
288   if (!reverse) {
289     int cp_index = Bytes::get_Java_u2(p);
290     int resolved_index = add_invokedynamic_resolved_references_entry(cp_index, -1); // Indy no longer has a CPCE
291     // Replace the trailing four bytes with an index to the array of
292     // indy resolution information in the CPC. There is one entry for
293     // each bytecode, even if they make the same call. In other words,
294     // the CPC-to-CP relation is many-to-one for invokedynamic entries.
295     // This means we must use a larger index size than u2 to address
296     // all these entries.  That is the main reason invokedynamic
297     // must have a five-byte instruction format.  (Of course, other JVM
298     // implementations can use the bytes for other purposes.)
299     // Note: We use native_u4 format exclusively for 4-byte indexes.
300     Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(_invokedynamic_index));
301     _invokedynamic_index++;
302 
303     // Collect invokedynamic information before creating ResolvedInvokeDynamicInfo array
304     _initialized_indy_entries.push(ResolvedIndyEntry((u2)resolved_index, (u2)cp_index));
305   } else {
306     // Should do nothing since we are not patching this bytecode
307     int cache_index = ConstantPool::decode_invokedynamic_index(
308                         Bytes::get_native_u4(p));
309     int cp_index = _initialized_indy_entries.at(cache_index).constant_pool_index();
310     assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index");
311     // zero out 4 bytes
312     Bytes::put_Java_u4(p, 0);
313     Bytes::put_Java_u2(p, (u2)cp_index);
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 
326     if (tag.is_method_handle() ||
327         tag.is_method_type() ||
328         tag.is_string() ||
329         (tag.is_dynamic_constant() &&
330          // keep regular ldc interpreter logic for condy primitives
331          is_reference_type(Signature::basic_type(_pool->uncached_signature_ref_at(cp_index))))
332         ) {
333       int ref_index = cp_entry_to_resolved_references(cp_index);
334       if (is_wide) {
335         (*bcp) = Bytecodes::_fast_aldc_w;
336         assert(ref_index == (u2)ref_index, "index overflow");
337         Bytes::put_native_u2(p, (u2)ref_index);
338       } else {
339         (*bcp) = Bytecodes::_fast_aldc;
340         assert(ref_index == (u1)ref_index, "index overflow");
341         (*p) = (u1)ref_index;
342       }
343     }
344   } else {
345     Bytecodes::Code rewritten_bc =
346               (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc);
347     if ((*bcp) == rewritten_bc) {
348       address p = bcp + offset;
349       int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p);
350       int pool_index = resolved_references_entry_to_pool_index(ref_index);
351       if (is_wide) {
352         (*bcp) = Bytecodes::_ldc_w;
353         assert(pool_index == (u2)pool_index, "index overflow");
354         Bytes::put_Java_u2(p, (u2)pool_index);
355       } else {
356         (*bcp) = Bytecodes::_ldc;
357         assert(pool_index == (u1)pool_index, "index overflow");
358         (*p) = (u1)pool_index;
359       }
360     }
361   }
362 }
363 
364 
365 // Rewrites a method given the index_map information
366 void Rewriter::scan_method(Thread* thread, Method* method, bool reverse, bool* invokespecial_error) {
367 
368   int nof_jsrs = 0;
369   bool has_monitor_bytecodes = false;
370   Bytecodes::Code c;
371 
372   // Bytecodes and their length
373   const address code_base = method->code_base();
374   const int code_length = method->code_size();
375 
376   int bc_length;
377   for (int bci = 0; bci < code_length; bci += bc_length) {
378     address bcp = code_base + bci;
379     int prefix_length = 0;
380     c = (Bytecodes::Code)(*bcp);
381 
382     // Since we have the code, see if we can get the length
383     // directly. Some more complicated bytecodes will report
384     // a length of zero, meaning we need to make another method
385     // call to calculate the length.
386     bc_length = Bytecodes::length_for(c);
387     if (bc_length == 0) {
388       bc_length = Bytecodes::length_at(method, bcp);
389 
390       // length_at will put us at the bytecode after the one modified
391       // by 'wide'. We don't currently examine any of the bytecodes
392       // modified by wide, but in case we do in the future...
393       if (c == Bytecodes::_wide) {
394         prefix_length = 1;
395         c = (Bytecodes::Code)bcp[1];
396       }
397     }
398 
399     // Continuing with an invalid bytecode will fail in the loop below.
400     // So guarantee here.
401     guarantee(bc_length > 0, "Verifier should have caught this invalid bytecode");
402 
403     switch (c) {
404       case Bytecodes::_lookupswitch   : {
405 #ifndef ZERO
406         Bytecode_lookupswitch bc(method, bcp);
407         (*bcp) = (
408           bc.number_of_pairs() < BinarySwitchThreshold
409           ? Bytecodes::_fast_linearswitch
410           : Bytecodes::_fast_binaryswitch
411         );
412 #endif
413         break;
414       }
415       case Bytecodes::_fast_linearswitch:
416       case Bytecodes::_fast_binaryswitch: {
417 #ifndef ZERO
418         (*bcp) = Bytecodes::_lookupswitch;
419 #endif
420         break;
421       }
422 
423       case Bytecodes::_invokespecial  : {
424         rewrite_invokespecial(bcp, prefix_length+1, reverse, invokespecial_error);
425         break;
426       }
427 
428       case Bytecodes::_putstatic      :
429       case Bytecodes::_putfield       : {
430         if (!reverse) {
431           // Check if any final field of the class given as parameter is modified
432           // outside of initializer methods of the class. Fields that are modified
433           // are marked with a flag. For marked fields, the compilers do not perform
434           // constant folding (as the field can be changed after initialization).
435           //
436           // The check is performed after verification and only if verification has
437           // succeeded. Therefore, the class is guaranteed to be well-formed.
438           InstanceKlass* klass = method->method_holder();
439           u2 bc_index = Bytes::get_Java_u2(bcp + prefix_length + 1);
440           constantPoolHandle cp(thread, method->constants());
441           Symbol* ref_class_name = cp->klass_name_at(cp->uncached_klass_ref_index_at(bc_index));
442 
443           if (klass->name() == ref_class_name) {
444             Symbol* field_name = cp->uncached_name_ref_at(bc_index);
445             Symbol* field_sig = cp->uncached_signature_ref_at(bc_index);
446 
447             fieldDescriptor fd;
448             if (klass->find_field(field_name, field_sig, &fd) != nullptr) {
449               if (fd.access_flags().is_final()) {
450                 if (fd.access_flags().is_static()) {
451                   if (!method->is_class_initializer()) {
452                     fd.set_has_initialized_final_update(true);
453                   }
454                 } else {
455                   if (!method->is_object_constructor()) {
456                     fd.set_has_initialized_final_update(true);
457                   }
458                 }
459               }
460             }
461           }
462         }
463       }
464       // fall through
465       case Bytecodes::_getstatic      : // fall through
466       case Bytecodes::_getfield       : // fall through
467       case Bytecodes::_withfield      : // fall through but may require more checks for correctness
468         rewrite_field_reference(bcp, prefix_length+1, reverse);
469         break;
470       case Bytecodes::_invokevirtual  : // fall through
471       case Bytecodes::_invokestatic   :
472       case Bytecodes::_invokeinterface:
473       case Bytecodes::_invokehandle   : // if reverse=true
474         rewrite_member_reference(bcp, prefix_length+1, reverse);
475         break;
476       case Bytecodes::_invokedynamic:
477         rewrite_invokedynamic(bcp, prefix_length+1, reverse);
478         break;
479       case Bytecodes::_ldc:
480       case Bytecodes::_fast_aldc:  // if reverse=true
481         maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse);
482         break;
483       case Bytecodes::_ldc_w:
484       case Bytecodes::_fast_aldc_w:  // if reverse=true
485         maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse);
486         break;
487       case Bytecodes::_jsr            : // fall through
488       case Bytecodes::_jsr_w          : nof_jsrs++;                   break;
489       case Bytecodes::_monitorenter   : // fall through
490       case Bytecodes::_monitorexit    : has_monitor_bytecodes = true; break;
491 
492       default: break;
493     }
494   }
495 
496   // Update flags
497   if (has_monitor_bytecodes) {
498     method->set_has_monitor_bytecodes();
499   }
500 
501   // The present of a jsr bytecode implies that the method might potentially
502   // have to be rewritten, so we run the oopMapGenerator on the method
503   if (nof_jsrs > 0) {
504     method->set_has_jsrs();
505   }
506 }
507 
508 // After constant pool is created, revisit methods containing jsrs.
509 methodHandle Rewriter::rewrite_jsrs(const methodHandle& method, TRAPS) {
510   ResourceMark rm(THREAD);
511   ResolveOopMapConflicts romc(method);
512   methodHandle new_method = romc.do_potential_rewrite(CHECK_(methodHandle()));
513   // Update monitor matching info.
514   if (romc.monitor_safe()) {
515     new_method->set_guaranteed_monitor_matching();
516   }
517 
518   return new_method;
519 }
520 
521 void Rewriter::rewrite_bytecodes(TRAPS) {
522   assert(_pool->cache() == nullptr, "constant pool cache must not be set yet");
523 
524   // determine index maps for Method* rewriting
525   compute_index_maps();
526 
527   if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
528     bool did_rewrite = false;
529     int i = _methods->length();
530     while (i-- > 0) {
531       Method* method = _methods->at(i);
532       if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
533         // rewrite the return bytecodes of Object.<init> to register the
534         // object for finalization if needed.
535         methodHandle m(THREAD, method);
536         rewrite_Object_init(m, CHECK);
537         did_rewrite = true;
538         break;
539       }
540     }
541     assert(did_rewrite, "must find Object::<init> to rewrite it");
542   }
543 
544   // rewrite methods, in two passes
545   int len = _methods->length();
546   bool invokespecial_error = false;
547 
548   for (int i = len-1; i >= 0; i--) {
549     Method* method = _methods->at(i);
550     scan_method(THREAD, method, false, &invokespecial_error);
551     if (invokespecial_error) {
552       // If you get an error here, there is no reversing bytecodes
553       // This exception is stored for this class and no further attempt is
554       // made at verifying or rewriting.
555       THROW_MSG(vmSymbols::java_lang_InternalError(),
556                 "This classfile overflows invokespecial for interfaces "
557                 "and cannot be loaded");
558       return;
559      }
560   }
561 }
562 
563 void Rewriter::rewrite(InstanceKlass* klass, TRAPS) {
564 #if INCLUDE_CDS
565   if (klass->is_shared()) {
566     assert(!klass->is_rewritten(), "rewritten shared classes cannot be rewritten again");
567   }
568 #endif // INCLUDE_CDS
569   ResourceMark rm(THREAD);
570   constantPoolHandle cpool(THREAD, klass->constants());
571   Rewriter     rw(klass, cpool, klass->methods(), CHECK);
572   // (That's all, folks.)
573 }
574 
575 Rewriter::Rewriter(InstanceKlass* klass, const constantPoolHandle& cpool, Array<Method*>* methods, TRAPS)
576   : _klass(klass),
577     _pool(cpool),
578     _methods(methods),
579     _cp_map(cpool->length()),
580     _cp_cache_map(cpool->length() / 2),
581     _reference_map(cpool->length()),
582     _resolved_references_map(cpool->length() / 2),
583     _invokedynamic_references_map(cpool->length() / 2),
584     _method_handle_invokers(cpool->length()),
585     _invokedynamic_index(0),
586     _field_entry_index(0)
587 {
588 
589   // Rewrite bytecodes - exception here exits.
590   rewrite_bytecodes(CHECK);
591 
592   // Stress restoring bytecodes
593   if (StressRewriter) {
594     restore_bytecodes(THREAD);
595     rewrite_bytecodes(CHECK);
596   }
597 
598   // allocate constant pool cache, now that we've seen all the bytecodes
599   make_constant_pool_cache(THREAD);
600 
601   // Restore bytecodes to their unrewritten state if there are exceptions
602   // rewriting bytecodes or allocating the cpCache
603   if (HAS_PENDING_EXCEPTION) {
604     restore_bytecodes(THREAD);
605     return;
606   }
607 
608   // Relocate after everything, but still do this under the is_rewritten flag,
609   // so methods with jsrs in custom class lists in aren't attempted to be
610   // rewritten in the RO section of the shared archive.
611   // Relocated bytecodes don't have to be restored, only the cp cache entries
612   int len = _methods->length();
613   for (int i = len-1; i >= 0; i--) {
614     methodHandle m(THREAD, _methods->at(i));
615 
616     if (m->has_jsrs()) {
617       m = rewrite_jsrs(m, THREAD);
618       // Restore bytecodes to their unrewritten state if there are exceptions
619       // relocating bytecodes.  If some are relocated, that is ok because that
620       // doesn't affect constant pool to cpCache rewriting.
621       if (HAS_PENDING_EXCEPTION) {
622         restore_bytecodes(THREAD);
623         return;
624       }
625       // Method might have gotten rewritten.
626       methods->at_put(i, m());
627     }
628   }
629 }
--- EOF ---