/
jvmtiRedefineClasses.cpp
4605 lines (4006 loc) · 178 KB
/
jvmtiRedefineClasses.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "cds/metaspaceShared.hpp"
#include "classfile/classFileStream.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/classLoadInfo.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/metadataOnStackMark.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/klassFactory.hpp"
#include "classfile/verifier.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "code/codeCache.hpp"
#include "compiler/compileBroker.hpp"
#include "interpreter/oopMapCache.hpp"
#include "interpreter/rewriter.hpp"
#include "jfr/jfrEvents.hpp"
#include "logging/logStream.hpp"
#include "memory/metadataFactory.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/annotations.hpp"
#include "oops/constantPool.hpp"
#include "oops/fieldStreams.inline.hpp"
#include "oops/klass.inline.hpp"
#include "oops/klassVtable.hpp"
#include "oops/oop.inline.hpp"
#include "oops/recordComponent.hpp"
#include "prims/jvmtiImpl.hpp"
#include "prims/jvmtiRedefineClasses.hpp"
#include "prims/jvmtiThreadState.inline.hpp"
#include "prims/resolvedMethodTable.hpp"
#include "prims/methodComparator.hpp"
#include "runtime/atomic.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/relocator.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "utilities/bitMap.inline.hpp"
#include "utilities/events.hpp"
Array<Method*>* VM_RedefineClasses::_old_methods = NULL;
Array<Method*>* VM_RedefineClasses::_new_methods = NULL;
Method** VM_RedefineClasses::_matching_old_methods = NULL;
Method** VM_RedefineClasses::_matching_new_methods = NULL;
Method** VM_RedefineClasses::_deleted_methods = NULL;
Method** VM_RedefineClasses::_added_methods = NULL;
int VM_RedefineClasses::_matching_methods_length = 0;
int VM_RedefineClasses::_deleted_methods_length = 0;
int VM_RedefineClasses::_added_methods_length = 0;
// This flag is global as the constructor does not reset it:
bool VM_RedefineClasses::_has_redefined_Object = false;
u8 VM_RedefineClasses::_id_counter = 0;
VM_RedefineClasses::VM_RedefineClasses(jint class_count,
const jvmtiClassDefinition *class_defs,
JvmtiClassLoadKind class_load_kind) {
_class_count = class_count;
_class_defs = class_defs;
_class_load_kind = class_load_kind;
_any_class_has_resolved_methods = false;
_res = JVMTI_ERROR_NONE;
_the_class = NULL;
_id = next_id();
}
static inline InstanceKlass* get_ik(jclass def) {
oop mirror = JNIHandles::resolve_non_null(def);
return InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
}
// If any of the classes are being redefined, wait
// Parallel constant pool merging leads to indeterminate constant pools.
void VM_RedefineClasses::lock_classes() {
JvmtiThreadState *state = JvmtiThreadState::state_for(JavaThread::current());
GrowableArray<Klass*>* redef_classes = state->get_classes_being_redefined();
MonitorLocker ml(RedefineClasses_lock);
if (redef_classes == NULL) {
redef_classes = new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(1, mtClass);
state->set_classes_being_redefined(redef_classes);
}
bool has_redefined;
do {
has_redefined = false;
// Go through classes each time until none are being redefined. Skip
// the ones that are being redefined by this thread currently. Class file
// load hook event may trigger new class redefine when we are redefining
// a class (after lock_classes()).
for (int i = 0; i < _class_count; i++) {
InstanceKlass* ik = get_ik(_class_defs[i].klass);
// Check if we are currently redefining the class in this thread already.
if (redef_classes->contains(ik)) {
assert(ik->is_being_redefined(), "sanity");
} else {
if (ik->is_being_redefined()) {
ml.wait();
has_redefined = true;
break; // for loop
}
}
}
} while (has_redefined);
for (int i = 0; i < _class_count; i++) {
InstanceKlass* ik = get_ik(_class_defs[i].klass);
redef_classes->push(ik); // Add to the _classes_being_redefined list
ik->set_is_being_redefined(true);
}
ml.notify_all();
}
void VM_RedefineClasses::unlock_classes() {
JvmtiThreadState *state = JvmtiThreadState::state_for(JavaThread::current());
GrowableArray<Klass*>* redef_classes = state->get_classes_being_redefined();
assert(redef_classes != NULL, "_classes_being_redefined is not allocated");
MonitorLocker ml(RedefineClasses_lock);
for (int i = _class_count - 1; i >= 0; i--) {
InstanceKlass* def_ik = get_ik(_class_defs[i].klass);
if (redef_classes->length() > 0) {
// Remove the class from _classes_being_redefined list
Klass* k = redef_classes->pop();
assert(def_ik == k, "unlocking wrong class");
}
assert(def_ik->is_being_redefined(),
"should be being redefined to get here");
// Unlock after we finish all redefines for this class within
// the thread. Same class can be pushed to the list multiple
// times (not more than once by each recursive redefinition).
if (!redef_classes->contains(def_ik)) {
def_ik->set_is_being_redefined(false);
}
}
ml.notify_all();
}
bool VM_RedefineClasses::doit_prologue() {
if (_class_count == 0) {
_res = JVMTI_ERROR_NONE;
return false;
}
if (_class_defs == NULL) {
_res = JVMTI_ERROR_NULL_POINTER;
return false;
}
for (int i = 0; i < _class_count; i++) {
if (_class_defs[i].klass == NULL) {
_res = JVMTI_ERROR_INVALID_CLASS;
return false;
}
if (_class_defs[i].class_byte_count == 0) {
_res = JVMTI_ERROR_INVALID_CLASS_FORMAT;
return false;
}
if (_class_defs[i].class_bytes == NULL) {
_res = JVMTI_ERROR_NULL_POINTER;
return false;
}
oop mirror = JNIHandles::resolve_non_null(_class_defs[i].klass);
// classes for primitives, arrays, and hidden classes
// cannot be redefined.
if (!is_modifiable_class(mirror)) {
_res = JVMTI_ERROR_UNMODIFIABLE_CLASS;
return false;
}
}
// Start timer after all the sanity checks; not quite accurate, but
// better than adding a bunch of stop() calls.
if (log_is_enabled(Info, redefine, class, timer)) {
_timer_vm_op_prologue.start();
}
lock_classes();
// We first load new class versions in the prologue, because somewhere down the
// call chain it is required that the current thread is a Java thread.
_res = load_new_class_versions();
if (_res != JVMTI_ERROR_NONE) {
// free any successfully created classes, since none are redefined
for (int i = 0; i < _class_count; i++) {
if (_scratch_classes[i] != NULL) {
ClassLoaderData* cld = _scratch_classes[i]->class_loader_data();
// Free the memory for this class at class unloading time. Not before
// because CMS might think this is still live.
InstanceKlass* ik = get_ik(_class_defs[i].klass);
if (ik->get_cached_class_file() == _scratch_classes[i]->get_cached_class_file()) {
// Don't double-free cached_class_file copied from the original class if error.
_scratch_classes[i]->set_cached_class_file(NULL);
}
cld->add_to_deallocate_list(InstanceKlass::cast(_scratch_classes[i]));
}
}
// Free os::malloc allocated memory in load_new_class_version.
os::free(_scratch_classes);
_timer_vm_op_prologue.stop();
unlock_classes();
return false;
}
_timer_vm_op_prologue.stop();
return true;
}
void VM_RedefineClasses::doit() {
Thread* current = Thread::current();
#if INCLUDE_CDS
if (UseSharedSpaces) {
// Sharing is enabled so we remap the shared readonly space to
// shared readwrite, private just in case we need to redefine
// a shared class. We do the remap during the doit() phase of
// the safepoint to be safer.
if (!MetaspaceShared::remap_shared_readonly_as_readwrite()) {
log_info(redefine, class, load)("failed to remap shared readonly space to readwrite, private");
_res = JVMTI_ERROR_INTERNAL;
return;
}
}
#endif
// Mark methods seen on stack and everywhere else so old methods are not
// cleaned up if they're on the stack.
MetadataOnStackMark md_on_stack(/*walk_all_metadata*/true, /*redefinition_walk*/true);
HandleMark hm(current); // make sure any handles created are deleted
// before the stack walk again.
for (int i = 0; i < _class_count; i++) {
redefine_single_class(current, _class_defs[i].klass, _scratch_classes[i]);
}
// Flush all compiled code that depends on the classes redefined.
flush_dependent_code();
// Adjust constantpool caches and vtables for all classes
// that reference methods of the evolved classes.
// Have to do this after all classes are redefined and all methods that
// are redefined are marked as old.
AdjustAndCleanMetadata adjust_and_clean_metadata(current);
ClassLoaderDataGraph::classes_do(&adjust_and_clean_metadata);
// JSR-292 support
if (_any_class_has_resolved_methods) {
bool trace_name_printed = false;
ResolvedMethodTable::adjust_method_entries(&trace_name_printed);
}
// Increment flag indicating that some invariants are no longer true.
// See jvmtiExport.hpp for detailed explanation.
JvmtiExport::increment_redefinition_count();
// check_class() is optionally called for product bits, but is
// always called for non-product bits.
#ifdef PRODUCT
if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) {
#endif
log_trace(redefine, class, obsolete, metadata)("calling check_class");
CheckClass check_class(current);
ClassLoaderDataGraph::classes_do(&check_class);
#ifdef PRODUCT
}
#endif
// Clean up any metadata now unreferenced while MetadataOnStackMark is set.
ClassLoaderDataGraph::clean_deallocate_lists(false);
}
void VM_RedefineClasses::doit_epilogue() {
unlock_classes();
// Free os::malloc allocated memory.
os::free(_scratch_classes);
// Reset the_class to null for error printing.
_the_class = NULL;
if (log_is_enabled(Info, redefine, class, timer)) {
// Used to have separate timers for "doit" and "all", but the timer
// overhead skewed the measurements.
julong doit_time = _timer_rsc_phase1.milliseconds() +
_timer_rsc_phase2.milliseconds();
julong all_time = _timer_vm_op_prologue.milliseconds() + doit_time;
log_info(redefine, class, timer)
("vm_op: all=" JULONG_FORMAT " prologue=" JULONG_FORMAT " doit=" JULONG_FORMAT,
all_time, (julong)_timer_vm_op_prologue.milliseconds(), doit_time);
log_info(redefine, class, timer)
("redefine_single_class: phase1=" JULONG_FORMAT " phase2=" JULONG_FORMAT,
(julong)_timer_rsc_phase1.milliseconds(), (julong)_timer_rsc_phase2.milliseconds());
}
}
bool VM_RedefineClasses::is_modifiable_class(oop klass_mirror) {
// classes for primitives cannot be redefined
if (java_lang_Class::is_primitive(klass_mirror)) {
return false;
}
Klass* k = java_lang_Class::as_Klass(klass_mirror);
// classes for arrays cannot be redefined
if (k == NULL || !k->is_instance_klass()) {
return false;
}
// Cannot redefine or retransform a hidden class.
if (InstanceKlass::cast(k)->is_hidden()) {
return false;
}
return true;
}
// Append the current entry at scratch_i in scratch_cp to *merge_cp_p
// where the end of *merge_cp_p is specified by *merge_cp_length_p. For
// direct CP entries, there is just the current entry to append. For
// indirect and double-indirect CP entries, there are zero or more
// referenced CP entries along with the current entry to append.
// Indirect and double-indirect CP entries are handled by recursive
// calls to append_entry() as needed. The referenced CP entries are
// always appended to *merge_cp_p before the referee CP entry. These
// referenced CP entries may already exist in *merge_cp_p in which case
// there is nothing extra to append and only the current entry is
// appended.
void VM_RedefineClasses::append_entry(const constantPoolHandle& scratch_cp,
int scratch_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
// append is different depending on entry tag type
switch (scratch_cp->tag_at(scratch_i).value()) {
// The old verifier is implemented outside the VM. It loads classes,
// but does not resolve constant pool entries directly so we never
// see Class entries here with the old verifier. Similarly the old
// verifier does not like Class entries in the input constant pool.
// The split-verifier is implemented in the VM so it can optionally
// and directly resolve constant pool entries to load classes. The
// split-verifier can accept either Class entries or UnresolvedClass
// entries in the input constant pool. We revert the appended copy
// back to UnresolvedClass so that either verifier will be happy
// with the constant pool entry.
//
// this is an indirect CP entry so it needs special handling
case JVM_CONSTANT_Class:
case JVM_CONSTANT_UnresolvedClass:
{
int name_i = scratch_cp->klass_name_index_at(scratch_i);
int new_name_i = find_or_append_indirect_entry(scratch_cp, name_i, merge_cp_p,
merge_cp_length_p);
if (new_name_i != name_i) {
log_trace(redefine, class, constantpool)
("Class entry@%d name_index change: %d to %d",
*merge_cp_length_p, name_i, new_name_i);
}
(*merge_cp_p)->temp_unresolved_klass_at_put(*merge_cp_length_p, new_name_i);
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p)++;
} break;
// these are direct CP entries so they can be directly appended,
// but double and long take two constant pool entries
case JVM_CONSTANT_Double: // fall through
case JVM_CONSTANT_Long:
{
ConstantPool::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p);
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p) += 2;
} break;
// these are direct CP entries so they can be directly appended
case JVM_CONSTANT_Float: // fall through
case JVM_CONSTANT_Integer: // fall through
case JVM_CONSTANT_Utf8: // fall through
// This was an indirect CP entry, but it has been changed into
// Symbol*s so this entry can be directly appended.
case JVM_CONSTANT_String: // fall through
{
ConstantPool::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p);
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p)++;
} break;
// this is an indirect CP entry so it needs special handling
case JVM_CONSTANT_NameAndType:
{
int name_ref_i = scratch_cp->name_ref_index_at(scratch_i);
int new_name_ref_i = find_or_append_indirect_entry(scratch_cp, name_ref_i, merge_cp_p,
merge_cp_length_p);
int signature_ref_i = scratch_cp->signature_ref_index_at(scratch_i);
int new_signature_ref_i = find_or_append_indirect_entry(scratch_cp, signature_ref_i,
merge_cp_p, merge_cp_length_p);
// If the referenced entries already exist in *merge_cp_p, then
// both new_name_ref_i and new_signature_ref_i will both be 0.
// In that case, all we are appending is the current entry.
if (new_name_ref_i != name_ref_i) {
log_trace(redefine, class, constantpool)
("NameAndType entry@%d name_ref_index change: %d to %d",
*merge_cp_length_p, name_ref_i, new_name_ref_i);
}
if (new_signature_ref_i != signature_ref_i) {
log_trace(redefine, class, constantpool)
("NameAndType entry@%d signature_ref_index change: %d to %d",
*merge_cp_length_p, signature_ref_i, new_signature_ref_i);
}
(*merge_cp_p)->name_and_type_at_put(*merge_cp_length_p,
new_name_ref_i, new_signature_ref_i);
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p)++;
} break;
// this is a double-indirect CP entry so it needs special handling
case JVM_CONSTANT_Fieldref: // fall through
case JVM_CONSTANT_InterfaceMethodref: // fall through
case JVM_CONSTANT_Methodref:
{
int klass_ref_i = scratch_cp->uncached_klass_ref_index_at(scratch_i);
int new_klass_ref_i = find_or_append_indirect_entry(scratch_cp, klass_ref_i,
merge_cp_p, merge_cp_length_p);
int name_and_type_ref_i = scratch_cp->uncached_name_and_type_ref_index_at(scratch_i);
int new_name_and_type_ref_i = find_or_append_indirect_entry(scratch_cp, name_and_type_ref_i,
merge_cp_p, merge_cp_length_p);
const char *entry_name = NULL;
switch (scratch_cp->tag_at(scratch_i).value()) {
case JVM_CONSTANT_Fieldref:
entry_name = "Fieldref";
(*merge_cp_p)->field_at_put(*merge_cp_length_p, new_klass_ref_i,
new_name_and_type_ref_i);
break;
case JVM_CONSTANT_InterfaceMethodref:
entry_name = "IFMethodref";
(*merge_cp_p)->interface_method_at_put(*merge_cp_length_p,
new_klass_ref_i, new_name_and_type_ref_i);
break;
case JVM_CONSTANT_Methodref:
entry_name = "Methodref";
(*merge_cp_p)->method_at_put(*merge_cp_length_p, new_klass_ref_i,
new_name_and_type_ref_i);
break;
default:
guarantee(false, "bad switch");
break;
}
if (klass_ref_i != new_klass_ref_i) {
log_trace(redefine, class, constantpool)
("%s entry@%d class_index changed: %d to %d", entry_name, *merge_cp_length_p, klass_ref_i, new_klass_ref_i);
}
if (name_and_type_ref_i != new_name_and_type_ref_i) {
log_trace(redefine, class, constantpool)
("%s entry@%d name_and_type_index changed: %d to %d",
entry_name, *merge_cp_length_p, name_and_type_ref_i, new_name_and_type_ref_i);
}
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p)++;
} break;
// this is an indirect CP entry so it needs special handling
case JVM_CONSTANT_MethodType:
{
int ref_i = scratch_cp->method_type_index_at(scratch_i);
int new_ref_i = find_or_append_indirect_entry(scratch_cp, ref_i, merge_cp_p,
merge_cp_length_p);
if (new_ref_i != ref_i) {
log_trace(redefine, class, constantpool)
("MethodType entry@%d ref_index change: %d to %d", *merge_cp_length_p, ref_i, new_ref_i);
}
(*merge_cp_p)->method_type_index_at_put(*merge_cp_length_p, new_ref_i);
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p)++;
} break;
// this is an indirect CP entry so it needs special handling
case JVM_CONSTANT_MethodHandle:
{
int ref_kind = scratch_cp->method_handle_ref_kind_at(scratch_i);
int ref_i = scratch_cp->method_handle_index_at(scratch_i);
int new_ref_i = find_or_append_indirect_entry(scratch_cp, ref_i, merge_cp_p,
merge_cp_length_p);
if (new_ref_i != ref_i) {
log_trace(redefine, class, constantpool)
("MethodHandle entry@%d ref_index change: %d to %d", *merge_cp_length_p, ref_i, new_ref_i);
}
(*merge_cp_p)->method_handle_index_at_put(*merge_cp_length_p, ref_kind, new_ref_i);
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p)++;
} break;
// this is an indirect CP entry so it needs special handling
case JVM_CONSTANT_Dynamic: // fall through
case JVM_CONSTANT_InvokeDynamic:
{
// Index of the bootstrap specifier in the operands array
int old_bs_i = scratch_cp->bootstrap_methods_attribute_index(scratch_i);
int new_bs_i = find_or_append_operand(scratch_cp, old_bs_i, merge_cp_p,
merge_cp_length_p);
// The bootstrap method NameAndType_info index
int old_ref_i = scratch_cp->bootstrap_name_and_type_ref_index_at(scratch_i);
int new_ref_i = find_or_append_indirect_entry(scratch_cp, old_ref_i, merge_cp_p,
merge_cp_length_p);
if (new_bs_i != old_bs_i) {
log_trace(redefine, class, constantpool)
("Dynamic entry@%d bootstrap_method_attr_index change: %d to %d",
*merge_cp_length_p, old_bs_i, new_bs_i);
}
if (new_ref_i != old_ref_i) {
log_trace(redefine, class, constantpool)
("Dynamic entry@%d name_and_type_index change: %d to %d", *merge_cp_length_p, old_ref_i, new_ref_i);
}
if (scratch_cp->tag_at(scratch_i).is_dynamic_constant())
(*merge_cp_p)->dynamic_constant_at_put(*merge_cp_length_p, new_bs_i, new_ref_i);
else
(*merge_cp_p)->invoke_dynamic_at_put(*merge_cp_length_p, new_bs_i, new_ref_i);
if (scratch_i != *merge_cp_length_p) {
// The new entry in *merge_cp_p is at a different index than
// the new entry in scratch_cp so we need to map the index values.
map_index(scratch_cp, scratch_i, *merge_cp_length_p);
}
(*merge_cp_length_p)++;
} break;
// At this stage, Class or UnresolvedClass could be in scratch_cp, but not
// ClassIndex
case JVM_CONSTANT_ClassIndex: // fall through
// Invalid is used as the tag for the second constant pool entry
// occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
// not be seen by itself.
case JVM_CONSTANT_Invalid: // fall through
// At this stage, String could be here, but not StringIndex
case JVM_CONSTANT_StringIndex: // fall through
// At this stage JVM_CONSTANT_UnresolvedClassInError should not be
// here
case JVM_CONSTANT_UnresolvedClassInError: // fall through
default:
{
// leave a breadcrumb
jbyte bad_value = scratch_cp->tag_at(scratch_i).value();
ShouldNotReachHere();
} break;
} // end switch tag value
} // end append_entry()
int VM_RedefineClasses::find_or_append_indirect_entry(const constantPoolHandle& scratch_cp,
int ref_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
int new_ref_i = ref_i;
bool match = (ref_i < *merge_cp_length_p) &&
scratch_cp->compare_entry_to(ref_i, *merge_cp_p, ref_i);
if (!match) {
// forward reference in *merge_cp_p or not a direct match
int found_i = scratch_cp->find_matching_entry(ref_i, *merge_cp_p);
if (found_i != 0) {
guarantee(found_i != ref_i, "compare_entry_to() and find_matching_entry() do not agree");
// Found a matching entry somewhere else in *merge_cp_p so just need a mapping entry.
new_ref_i = found_i;
map_index(scratch_cp, ref_i, found_i);
} else {
// no match found so we have to append this entry to *merge_cp_p
append_entry(scratch_cp, ref_i, merge_cp_p, merge_cp_length_p);
// The above call to append_entry() can only append one entry
// so the post call query of *merge_cp_length_p is only for
// the sake of consistency.
new_ref_i = *merge_cp_length_p - 1;
}
}
return new_ref_i;
} // end find_or_append_indirect_entry()
// Append a bootstrap specifier into the merge_cp operands that is semantically equal
// to the scratch_cp operands bootstrap specifier passed by the old_bs_i index.
// Recursively append new merge_cp entries referenced by the new bootstrap specifier.
void VM_RedefineClasses::append_operand(const constantPoolHandle& scratch_cp, int old_bs_i,
constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
int old_ref_i = scratch_cp->operand_bootstrap_method_ref_index_at(old_bs_i);
int new_ref_i = find_or_append_indirect_entry(scratch_cp, old_ref_i, merge_cp_p,
merge_cp_length_p);
if (new_ref_i != old_ref_i) {
log_trace(redefine, class, constantpool)
("operands entry@%d bootstrap method ref_index change: %d to %d", _operands_cur_length, old_ref_i, new_ref_i);
}
Array<u2>* merge_ops = (*merge_cp_p)->operands();
int new_bs_i = _operands_cur_length;
// We have _operands_cur_length == 0 when the merge_cp operands is empty yet.
// However, the operand_offset_at(0) was set in the extend_operands() call.
int new_base = (new_bs_i == 0) ? (*merge_cp_p)->operand_offset_at(0)
: (*merge_cp_p)->operand_next_offset_at(new_bs_i - 1);
int argc = scratch_cp->operand_argument_count_at(old_bs_i);
ConstantPool::operand_offset_at_put(merge_ops, _operands_cur_length, new_base);
merge_ops->at_put(new_base++, new_ref_i);
merge_ops->at_put(new_base++, argc);
for (int i = 0; i < argc; i++) {
int old_arg_ref_i = scratch_cp->operand_argument_index_at(old_bs_i, i);
int new_arg_ref_i = find_or_append_indirect_entry(scratch_cp, old_arg_ref_i, merge_cp_p,
merge_cp_length_p);
merge_ops->at_put(new_base++, new_arg_ref_i);
if (new_arg_ref_i != old_arg_ref_i) {
log_trace(redefine, class, constantpool)
("operands entry@%d bootstrap method argument ref_index change: %d to %d",
_operands_cur_length, old_arg_ref_i, new_arg_ref_i);
}
}
if (old_bs_i != _operands_cur_length) {
// The bootstrap specifier in *merge_cp_p is at a different index than
// that in scratch_cp so we need to map the index values.
map_operand_index(old_bs_i, new_bs_i);
}
_operands_cur_length++;
} // end append_operand()
int VM_RedefineClasses::find_or_append_operand(const constantPoolHandle& scratch_cp,
int old_bs_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
int new_bs_i = old_bs_i; // bootstrap specifier index
bool match = (old_bs_i < _operands_cur_length) &&
scratch_cp->compare_operand_to(old_bs_i, *merge_cp_p, old_bs_i);
if (!match) {
// forward reference in *merge_cp_p or not a direct match
int found_i = scratch_cp->find_matching_operand(old_bs_i, *merge_cp_p,
_operands_cur_length);
if (found_i != -1) {
guarantee(found_i != old_bs_i, "compare_operand_to() and find_matching_operand() disagree");
// found a matching operand somewhere else in *merge_cp_p so just need a mapping
new_bs_i = found_i;
map_operand_index(old_bs_i, found_i);
} else {
// no match found so we have to append this bootstrap specifier to *merge_cp_p
append_operand(scratch_cp, old_bs_i, merge_cp_p, merge_cp_length_p);
new_bs_i = _operands_cur_length - 1;
}
}
return new_bs_i;
} // end find_or_append_operand()
void VM_RedefineClasses::finalize_operands_merge(const constantPoolHandle& merge_cp, TRAPS) {
if (merge_cp->operands() == NULL) {
return;
}
// Shrink the merge_cp operands
merge_cp->shrink_operands(_operands_cur_length, CHECK);
if (log_is_enabled(Trace, redefine, class, constantpool)) {
// don't want to loop unless we are tracing
int count = 0;
for (int i = 1; i < _operands_index_map_p->length(); i++) {
int value = _operands_index_map_p->at(i);
if (value != -1) {
log_trace(redefine, class, constantpool)("operands_index_map[%d]: old=%d new=%d", count, i, value);
count++;
}
}
}
// Clean-up
_operands_index_map_p = NULL;
_operands_cur_length = 0;
_operands_index_map_count = 0;
} // end finalize_operands_merge()
// Symbol* comparator for qsort
// The caller must have an active ResourceMark.
static int symcmp(const void* a, const void* b) {
char* astr = (*(Symbol**)a)->as_C_string();
char* bstr = (*(Symbol**)b)->as_C_string();
return strcmp(astr, bstr);
}
// The caller must have an active ResourceMark.
static jvmtiError check_attribute_arrays(const char* attr_name,
InstanceKlass* the_class, InstanceKlass* scratch_class,
Array<u2>* the_array, Array<u2>* scr_array) {
bool the_array_exists = the_array != Universe::the_empty_short_array();
bool scr_array_exists = scr_array != Universe::the_empty_short_array();
int array_len = the_array->length();
if (the_array_exists && scr_array_exists) {
if (array_len != scr_array->length()) {
log_trace(redefine, class)
("redefined class %s attribute change error: %s len=%d changed to len=%d",
the_class->external_name(), attr_name, array_len, scr_array->length());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
// The order of entries in the attribute array is not specified so we
// have to explicitly check for the same contents. We do this by copying
// the referenced symbols into their own arrays, sorting them and then
// comparing each element pair.
Symbol** the_syms = NEW_RESOURCE_ARRAY_RETURN_NULL(Symbol*, array_len);
Symbol** scr_syms = NEW_RESOURCE_ARRAY_RETURN_NULL(Symbol*, array_len);
if (the_syms == NULL || scr_syms == NULL) {
return JVMTI_ERROR_OUT_OF_MEMORY;
}
for (int i = 0; i < array_len; i++) {
int the_cp_index = the_array->at(i);
int scr_cp_index = scr_array->at(i);
the_syms[i] = the_class->constants()->klass_name_at(the_cp_index);
scr_syms[i] = scratch_class->constants()->klass_name_at(scr_cp_index);
}
qsort(the_syms, array_len, sizeof(Symbol*), symcmp);
qsort(scr_syms, array_len, sizeof(Symbol*), symcmp);
for (int i = 0; i < array_len; i++) {
if (the_syms[i] != scr_syms[i]) {
log_info(redefine, class)
("redefined class %s attribute change error: %s[%d]: %s changed to %s",
the_class->external_name(), attr_name, i,
the_syms[i]->as_C_string(), scr_syms[i]->as_C_string());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
}
} else if (the_array_exists ^ scr_array_exists) {
const char* action_str = (the_array_exists) ? "removed" : "added";
log_info(redefine, class)
("redefined class %s attribute change error: %s attribute %s",
the_class->external_name(), attr_name, action_str);
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
return JVMTI_ERROR_NONE;
}
static jvmtiError check_nest_attributes(InstanceKlass* the_class,
InstanceKlass* scratch_class) {
// Check whether the class NestHost attribute has been changed.
Thread* thread = Thread::current();
ResourceMark rm(thread);
u2 the_nest_host_idx = the_class->nest_host_index();
u2 scr_nest_host_idx = scratch_class->nest_host_index();
if (the_nest_host_idx != 0 && scr_nest_host_idx != 0) {
Symbol* the_sym = the_class->constants()->klass_name_at(the_nest_host_idx);
Symbol* scr_sym = scratch_class->constants()->klass_name_at(scr_nest_host_idx);
if (the_sym != scr_sym) {
log_info(redefine, class, nestmates)
("redefined class %s attribute change error: NestHost class: %s replaced with: %s",
the_class->external_name(), the_sym->as_C_string(), scr_sym->as_C_string());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
} else if ((the_nest_host_idx == 0) ^ (scr_nest_host_idx == 0)) {
const char* action_str = (the_nest_host_idx != 0) ? "removed" : "added";
log_info(redefine, class, nestmates)
("redefined class %s attribute change error: NestHost attribute %s",
the_class->external_name(), action_str);
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
// Check whether the class NestMembers attribute has been changed.
return check_attribute_arrays("NestMembers",
the_class, scratch_class,
the_class->nest_members(),
scratch_class->nest_members());
}
// Return an error status if the class Record attribute was changed.
static jvmtiError check_record_attribute(InstanceKlass* the_class, InstanceKlass* scratch_class) {
// Get lists of record components.
Array<RecordComponent*>* the_record = the_class->record_components();
Array<RecordComponent*>* scr_record = scratch_class->record_components();
bool the_record_exists = the_record != NULL;
bool scr_record_exists = scr_record != NULL;
if (the_record_exists && scr_record_exists) {
int the_num_components = the_record->length();
int scr_num_components = scr_record->length();
if (the_num_components != scr_num_components) {
log_info(redefine, class, record)
("redefined class %s attribute change error: Record num_components=%d changed to num_components=%d",
the_class->external_name(), the_num_components, scr_num_components);
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
// Compare each field in each record component.
ConstantPool* the_cp = the_class->constants();
ConstantPool* scr_cp = scratch_class->constants();
for (int x = 0; x < the_num_components; x++) {
RecordComponent* the_component = the_record->at(x);
RecordComponent* scr_component = scr_record->at(x);
const Symbol* const the_name = the_cp->symbol_at(the_component->name_index());
const Symbol* const scr_name = scr_cp->symbol_at(scr_component->name_index());
const Symbol* const the_descr = the_cp->symbol_at(the_component->descriptor_index());
const Symbol* const scr_descr = scr_cp->symbol_at(scr_component->descriptor_index());
if (the_name != scr_name || the_descr != scr_descr) {
log_info(redefine, class, record)
("redefined class %s attribute change error: Record name_index, descriptor_index, and/or attributes_count changed",
the_class->external_name());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
int the_gen_sig = the_component->generic_signature_index();
int scr_gen_sig = scr_component->generic_signature_index();
const Symbol* const the_gen_sig_sym = (the_gen_sig == 0 ? NULL :
the_cp->symbol_at(the_component->generic_signature_index()));
const Symbol* const scr_gen_sig_sym = (scr_gen_sig == 0 ? NULL :
scr_cp->symbol_at(scr_component->generic_signature_index()));
if (the_gen_sig_sym != scr_gen_sig_sym) {
log_info(redefine, class, record)
("redefined class %s attribute change error: Record generic_signature attribute changed",
the_class->external_name());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
// It's okay if a record component's annotations were changed.
}
} else if (the_record_exists ^ scr_record_exists) {
const char* action_str = (the_record_exists) ? "removed" : "added";
log_info(redefine, class, record)
("redefined class %s attribute change error: Record attribute %s",
the_class->external_name(), action_str);
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
}
return JVMTI_ERROR_NONE;
}
static jvmtiError check_permitted_subclasses_attribute(InstanceKlass* the_class,
InstanceKlass* scratch_class) {
Thread* thread = Thread::current();
ResourceMark rm(thread);
// Check whether the class PermittedSubclasses attribute has been changed.
return check_attribute_arrays("PermittedSubclasses",
the_class, scratch_class,
the_class->permitted_subclasses(),
scratch_class->permitted_subclasses());
}
static bool can_add_or_delete(Method* m) {
// Compatibility mode
return (AllowRedefinitionToAddDeleteMethods &&
(m->is_private() && (m->is_static() || m->is_final())));
}
jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
InstanceKlass* the_class,
InstanceKlass* scratch_class) {
int i;
// Check superclasses, or rather their names, since superclasses themselves can be
// requested to replace.
// Check for NULL superclass first since this might be java.lang.Object
if (the_class->super() != scratch_class->super() &&
(the_class->super() == NULL || scratch_class->super() == NULL ||
the_class->super()->name() !=
scratch_class->super()->name())) {
log_info(redefine, class, normalize)
("redefined class %s superclass change error: superclass changed from %s to %s.",
the_class->external_name(),
the_class->super() == NULL ? "NULL" : the_class->super()->external_name(),
scratch_class->super() == NULL ? "NULL" : scratch_class->super()->external_name());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
}
// Check if the number, names and order of directly implemented interfaces are the same.
// I think in principle we should just check if the sets of names of directly implemented
// interfaces are the same, i.e. the order of declaration (which, however, if changed in the
// .java file, also changes in .class file) should not matter. However, comparing sets is
// technically a bit more difficult, and, more importantly, I am not sure at present that the
// order of interfaces does not matter on the implementation level, i.e. that the VM does not
// rely on it somewhere.
Array<InstanceKlass*>* k_interfaces = the_class->local_interfaces();
Array<InstanceKlass*>* k_new_interfaces = scratch_class->local_interfaces();
int n_intfs = k_interfaces->length();
if (n_intfs != k_new_interfaces->length()) {
log_info(redefine, class, normalize)
("redefined class %s interfaces change error: number of implemented interfaces changed from %d to %d.",
the_class->external_name(), n_intfs, k_new_interfaces->length());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
}
for (i = 0; i < n_intfs; i++) {
if (k_interfaces->at(i)->name() !=
k_new_interfaces->at(i)->name()) {
log_info(redefine, class, normalize)
("redefined class %s interfaces change error: interface changed from %s to %s.",
the_class->external_name(),
k_interfaces->at(i)->external_name(), k_new_interfaces->at(i)->external_name());
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
}
}
// Check whether class is in the error init state.
if (the_class->is_in_error_state()) {
log_info(redefine, class, normalize)
("redefined class %s is in error init state.", the_class->external_name());
// TBD #5057930: special error code is needed in 1.6
return JVMTI_ERROR_INVALID_CLASS;
}
// Check whether the nest-related attributes have been changed.
jvmtiError err = check_nest_attributes(the_class, scratch_class);
if (err != JVMTI_ERROR_NONE) {
return err;
}
// Check whether the Record attribute has been changed.
err = check_record_attribute(the_class, scratch_class);
if (err != JVMTI_ERROR_NONE) {
return err;
}
// Check whether the PermittedSubclasses attribute has been changed.
err = check_permitted_subclasses_attribute(the_class, scratch_class);
if (err != JVMTI_ERROR_NONE) {
return err;
}
// Check whether class modifiers are the same.
jushort old_flags = (jushort) the_class->access_flags().get_flags();
jushort new_flags = (jushort) scratch_class->access_flags().get_flags();
if (old_flags != new_flags) {
log_info(redefine, class, normalize)
("redefined class %s modifiers change error: modifiers changed from %d to %d.",
the_class->external_name(), old_flags, new_flags);
return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED;
}