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