/
shenandoahVerifier.cpp
1009 lines (873 loc) · 37 KB
/
shenandoahVerifier.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) 2017, 2020, Red Hat, Inc. 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 "gc/shenandoah/shenandoahAsserts.hpp"
#include "gc/shenandoah/shenandoahConcurrentRoots.hpp"
#include "gc/shenandoah/shenandoahForwarding.inline.hpp"
#include "gc/shenandoah/shenandoahPhaseTimings.hpp"
#include "gc/shenandoah/shenandoahHeap.inline.hpp"
#include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
#include "gc/shenandoah/shenandoahRootProcessor.hpp"
#include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
#include "gc/shenandoah/shenandoahUtils.hpp"
#include "gc/shenandoah/shenandoahVerifier.hpp"
#include "memory/allocation.hpp"
#include "memory/iterator.inline.hpp"
#include "memory/resourceArea.hpp"
#include "oops/compressedOops.inline.hpp"
#include "runtime/atomic.hpp"
#include "runtime/orderAccess.hpp"
#include "utilities/align.hpp"
// Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
#ifdef verify_oop
#undef verify_oop
#endif
class ShenandoahVerifyOopClosure : public BasicOopIterateClosure {
private:
const char* _phase;
ShenandoahVerifier::VerifyOptions _options;
ShenandoahVerifierStack* _stack;
ShenandoahHeap* _heap;
MarkBitMap* _map;
ShenandoahLivenessData* _ld;
void* _interior_loc;
oop _loc;
public:
ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld,
const char* phase, ShenandoahVerifier::VerifyOptions options) :
_phase(phase),
_options(options),
_stack(stack),
_heap(ShenandoahHeap::heap()),
_map(map),
_ld(ld),
_interior_loc(NULL),
_loc(NULL) { }
private:
void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
if (!test) {
ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
}
}
template <class T>
void do_oop_work(T* p) {
T o = RawAccess<>::oop_load(p);
if (!CompressedOops::is_null(o)) {
oop obj = CompressedOops::decode_not_null(o);
// Single threaded verification can use faster non-atomic stack and bitmap
// methods.
//
// For performance reasons, only fully verify non-marked field values.
// We are here when the host object for *p is already marked.
if (_map->par_mark(obj)) {
verify_oop_at(p, obj);
_stack->push(ShenandoahVerifierTask(obj));
}
}
}
void verify_oop(oop obj) {
// Perform consistency checks with gradually decreasing safety level. This guarantees
// that failure report would not try to touch something that was not yet verified to be
// safe to process.
check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj),
"oop must be in heap");
check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
"oop must be aligned");
ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj);
Klass* obj_klass = obj->klass_or_null();
// Verify that obj is not in dead space:
{
// Do this before touching obj->size()
check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL,
"Object klass pointer should not be NULL");
check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
"Object klass pointer must go to metaspace");
HeapWord *obj_addr = cast_from_oop<HeapWord*>(obj);
check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(),
"Object start should be within the region");
if (!obj_reg->is_humongous()) {
check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + obj->size()) <= obj_reg->top(),
"Object end should be within the region");
} else {
size_t humongous_start = obj_reg->index();
size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift());
for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
"Humongous object is in continuation that fits it");
}
}
// ------------ obj is safe at this point --------------
check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
"Object should be in active region");
switch (_options._verify_liveness) {
case ShenandoahVerifier::_verify_liveness_disable:
// skip
break;
case ShenandoahVerifier::_verify_liveness_complete:
Atomic::add(&_ld[obj_reg->index()], (uint) obj->size());
// fallthrough for fast failure for un-live regions:
case ShenandoahVerifier::_verify_liveness_conservative:
check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(),
"Object must belong to region with live data");
break;
default:
assert(false, "Unhandled liveness verification");
}
}
oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
ShenandoahHeapRegion* fwd_reg = NULL;
if (obj != fwd) {
check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd),
"Forwardee must be in heap");
check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
"Forwardee is set");
check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
"Forwardee must be aligned");
// Do this before touching fwd->size()
Klass* fwd_klass = fwd->klass_or_null();
check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL,
"Forwardee klass pointer should not be NULL");
check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass),
"Forwardee klass pointer must go to metaspace");
check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass,
"Forwardee klass pointer must go to metaspace");
fwd_reg = _heap->heap_region_containing(fwd);
// Verify that forwardee is not in the dead space:
check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
"Should have no humongous forwardees");
HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd);
check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
"Forwardee start should be within the region");
check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(),
"Forwardee end should be within the region");
oop fwd2 = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
"Double forwarding");
} else {
fwd_reg = obj_reg;
}
// ------------ obj and fwd are safe at this point --------------
switch (_options._verify_marked) {
case ShenandoahVerifier::_verify_marked_disable:
// skip
break;
case ShenandoahVerifier::_verify_marked_incomplete:
check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj),
"Must be marked in incomplete bitmap");
break;
case ShenandoahVerifier::_verify_marked_complete:
check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj),
"Must be marked in complete bitmap");
break;
default:
assert(false, "Unhandled mark verification");
}
switch (_options._verify_forwarded) {
case ShenandoahVerifier::_verify_forwarded_disable:
// skip
break;
case ShenandoahVerifier::_verify_forwarded_none: {
check(ShenandoahAsserts::_safe_all, obj, (obj == fwd),
"Should not be forwarded");
break;
}
case ShenandoahVerifier::_verify_forwarded_allow: {
if (obj != fwd) {
check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg,
"Forwardee should be in another region");
}
break;
}
default:
assert(false, "Unhandled forwarding verification");
}
switch (_options._verify_cset) {
case ShenandoahVerifier::_verify_cset_disable:
// skip
break;
case ShenandoahVerifier::_verify_cset_none:
check(ShenandoahAsserts::_safe_all, obj, !_heap->in_collection_set(obj),
"Should not have references to collection set");
break;
case ShenandoahVerifier::_verify_cset_forwarded:
if (_heap->in_collection_set(obj)) {
check(ShenandoahAsserts::_safe_all, obj, (obj != fwd),
"Object in collection set, should have forwardee");
}
break;
default:
assert(false, "Unhandled cset verification");
}
}
public:
/**
* Verify object with known interior reference.
* @param p interior reference where the object is referenced from; can be off-heap
* @param obj verified object
*/
template <class T>
void verify_oop_at(T* p, oop obj) {
_interior_loc = p;
verify_oop(obj);
_interior_loc = NULL;
}
/**
* Verify object without known interior reference.
* Useful when picking up the object at known offset in heap,
* but without knowing what objects reference it.
* @param obj verified object
*/
void verify_oop_standalone(oop obj) {
_interior_loc = NULL;
verify_oop(obj);
_interior_loc = NULL;
}
/**
* Verify oop fields from this object.
* @param obj host object for verified fields
*/
void verify_oops_from(oop obj) {
_loc = obj;
obj->oop_iterate(this);
_loc = NULL;
}
virtual void do_oop(oop* p) { do_oop_work(p); }
virtual void do_oop(narrowOop* p) { do_oop_work(p); }
};
class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
private:
size_t _used, _committed, _garbage;
public:
ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {};
void heap_region_do(ShenandoahHeapRegion* r) {
_used += r->used();
_garbage += r->garbage();
_committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
}
size_t used() { return _used; }
size_t committed() { return _committed; }
size_t garbage() { return _garbage; }
};
class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure {
private:
ShenandoahHeap* _heap;
const char* _phase;
ShenandoahVerifier::VerifyRegions _regions;
public:
ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) :
_heap(ShenandoahHeap::heap()),
_phase(phase),
_regions(regions) {};
void print_failure(ShenandoahHeapRegion* r, const char* label) {
ResourceMark rm;
ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label);
stringStream ss;
r->print_on(&ss);
msg.append("%s", ss.as_string());
report_vm_error(__FILE__, __LINE__, msg.buffer());
}
void verify(ShenandoahHeapRegion* r, bool test, const char* msg) {
if (!test) {
print_failure(r, msg);
}
}
void heap_region_do(ShenandoahHeapRegion* r) {
switch (_regions) {
case ShenandoahVerifier::_verify_regions_disable:
break;
case ShenandoahVerifier::_verify_regions_notrash:
verify(r, !r->is_trash(),
"Should not have trash regions");
break;
case ShenandoahVerifier::_verify_regions_nocset:
verify(r, !r->is_cset(),
"Should not have cset regions");
break;
case ShenandoahVerifier::_verify_regions_notrash_nocset:
verify(r, !r->is_trash(),
"Should not have trash regions");
verify(r, !r->is_cset(),
"Should not have cset regions");
break;
default:
ShouldNotReachHere();
}
verify(r, r->capacity() == ShenandoahHeapRegion::region_size_bytes(),
"Capacity should match region size");
verify(r, r->bottom() <= r->top(),
"Region top should not be less than bottom");
verify(r, r->bottom() <= _heap->marking_context()->top_at_mark_start(r),
"Region TAMS should not be less than bottom");
verify(r, _heap->marking_context()->top_at_mark_start(r) <= r->top(),
"Complete TAMS should not be larger than top");
verify(r, r->get_live_data_bytes() <= r->capacity(),
"Live data cannot be larger than capacity");
verify(r, r->garbage() <= r->capacity(),
"Garbage cannot be larger than capacity");
verify(r, r->used() <= r->capacity(),
"Used cannot be larger than capacity");
verify(r, r->get_shared_allocs() <= r->capacity(),
"Shared alloc count should not be larger than capacity");
verify(r, r->get_tlab_allocs() <= r->capacity(),
"TLAB alloc count should not be larger than capacity");
verify(r, r->get_gclab_allocs() <= r->capacity(),
"GCLAB alloc count should not be larger than capacity");
verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() == r->used(),
"Accurate accounting: shared + TLAB + GCLAB = used");
verify(r, !r->is_empty() || !r->has_live(),
"Empty regions should not have live data");
verify(r, r->is_cset() == _heap->collection_set()->is_in(r),
"Transitional: region flags and collection set agree");
}
};
class ShenandoahVerifierReachableTask : public AbstractGangTask {
private:
const char* _label;
ShenandoahRootVerifier* _verifier;
ShenandoahVerifier::VerifyOptions _options;
ShenandoahHeap* _heap;
ShenandoahLivenessData* _ld;
MarkBitMap* _bitmap;
volatile size_t _processed;
public:
ShenandoahVerifierReachableTask(MarkBitMap* bitmap,
ShenandoahLivenessData* ld,
ShenandoahRootVerifier* verifier,
const char* label,
ShenandoahVerifier::VerifyOptions options) :
AbstractGangTask("Shenandoah Parallel Verifier Reachable Task"),
_label(label),
_verifier(verifier),
_options(options),
_heap(ShenandoahHeap::heap()),
_ld(ld),
_bitmap(bitmap),
_processed(0) {};
size_t processed() {
return _processed;
}
virtual void work(uint worker_id) {
ResourceMark rm;
ShenandoahVerifierStack stack;
// On level 2, we need to only check the roots once.
// On level 3, we want to check the roots, and seed the local stack.
// It is a lesser evil to accept multiple root scans at level 3, because
// extended parallelism would buy us out.
if (((ShenandoahVerifyLevel == 2) && (worker_id == 0))
|| (ShenandoahVerifyLevel >= 3)) {
ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
ShenandoahMessageBuffer("%s, Roots", _label),
_options);
if (_heap->unload_classes()) {
_verifier->strong_roots_do(&cl);
} else {
_verifier->roots_do(&cl);
}
}
size_t processed = 0;
if (ShenandoahVerifyLevel >= 3) {
ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
ShenandoahMessageBuffer("%s, Reachable", _label),
_options);
while (!stack.is_empty()) {
processed++;
ShenandoahVerifierTask task = stack.pop();
cl.verify_oops_from(task.obj());
}
}
Atomic::add(&_processed, processed);
}
};
class ShenandoahVerifierMarkedRegionTask : public AbstractGangTask {
private:
const char* _label;
ShenandoahVerifier::VerifyOptions _options;
ShenandoahHeap *_heap;
MarkBitMap* _bitmap;
ShenandoahLivenessData* _ld;
volatile size_t _claimed;
volatile size_t _processed;
public:
ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap,
ShenandoahLivenessData* ld,
const char* label,
ShenandoahVerifier::VerifyOptions options) :
AbstractGangTask("Shenandoah Parallel Verifier Marked Region"),
_label(label),
_options(options),
_heap(ShenandoahHeap::heap()),
_bitmap(bitmap),
_ld(ld),
_claimed(0),
_processed(0) {};
size_t processed() {
return _processed;
}
virtual void work(uint worker_id) {
ShenandoahVerifierStack stack;
ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
ShenandoahMessageBuffer("%s, Marked", _label),
_options);
while (true) {
size_t v = Atomic::fetch_and_add(&_claimed, 1u);
if (v < _heap->num_regions()) {
ShenandoahHeapRegion* r = _heap->get_region(v);
if (!r->is_humongous() && !r->is_trash()) {
work_regular(r, stack, cl);
} else if (r->is_humongous_start()) {
work_humongous(r, stack, cl);
}
} else {
break;
}
}
}
virtual void work_humongous(ShenandoahHeapRegion *r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) {
size_t processed = 0;
HeapWord* obj = r->bottom();
if (_heap->complete_marking_context()->is_marked((oop)obj)) {
verify_and_follow(obj, stack, cl, &processed);
}
Atomic::add(&_processed, processed);
}
virtual void work_regular(ShenandoahHeapRegion *r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) {
size_t processed = 0;
MarkBitMap* mark_bit_map = _heap->complete_marking_context()->mark_bit_map();
HeapWord* tams = _heap->complete_marking_context()->top_at_mark_start(r);
// Bitmaps, before TAMS
if (tams > r->bottom()) {
HeapWord* start = r->bottom();
HeapWord* addr = mark_bit_map->get_next_marked_addr(start, tams);
while (addr < tams) {
verify_and_follow(addr, stack, cl, &processed);
addr += 1;
if (addr < tams) {
addr = mark_bit_map->get_next_marked_addr(addr, tams);
}
}
}
// Size-based, after TAMS
{
HeapWord* limit = r->top();
HeapWord* addr = tams;
while (addr < limit) {
verify_and_follow(addr, stack, cl, &processed);
addr += oop(addr)->size();
}
}
Atomic::add(&_processed, processed);
}
void verify_and_follow(HeapWord *addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, size_t *processed) {
if (!_bitmap->par_mark(addr)) return;
// Verify the object itself:
oop obj = oop(addr);
cl.verify_oop_standalone(obj);
// Verify everything reachable from that object too, hopefully realizing
// everything was already marked, and never touching further:
cl.verify_oops_from(obj);
(*processed)++;
while (!stack.is_empty()) {
ShenandoahVerifierTask task = stack.pop();
cl.verify_oops_from(task.obj());
(*processed)++;
}
}
};
class VerifyThreadGCState : public ThreadClosure {
private:
const char* _label;
char _expected;
public:
VerifyThreadGCState(const char* label, char expected) : _expected(expected) {}
void do_thread(Thread* t) {
char actual = ShenandoahThreadLocalData::gc_state(t);
if (actual != _expected) {
fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual);
}
}
};
class ShenandoahGCStateResetter : public StackObj {
private:
ShenandoahHeap* const _heap;
char _gc_state;
public:
ShenandoahGCStateResetter() : _heap(ShenandoahHeap::heap()) {
_gc_state = _heap->gc_state();
_heap->_gc_state.clear();
}
~ShenandoahGCStateResetter() {
_heap->_gc_state.set(_gc_state);
assert(_heap->gc_state() == _gc_state, "Should be restored");
}
};
void ShenandoahVerifier::verify_at_safepoint(const char *label,
VerifyForwarded forwarded, VerifyMarked marked,
VerifyCollectionSet cset,
VerifyLiveness liveness, VerifyRegions regions,
VerifyGCState gcstate,
VerifyWeakRoots weak_roots) {
guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens");
guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize");
// Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check
ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/);
log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel);
// GC state checks
{
char expected = -1;
bool enabled;
switch (gcstate) {
case _verify_gcstate_disable:
enabled = false;
break;
case _verify_gcstate_forwarded:
enabled = true;
expected = ShenandoahHeap::HAS_FORWARDED;
break;
case _verify_gcstate_evacuation:
enabled = true;
expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION;
break;
case _verify_gcstate_stable:
enabled = true;
expected = ShenandoahHeap::STABLE;
break;
default:
enabled = false;
assert(false, "Unhandled gc-state verification");
}
if (enabled) {
char actual = _heap->gc_state();
if (actual != expected) {
fatal("%s: Global gc-state: expected %d, actual %d", label, expected, actual);
}
VerifyThreadGCState vtgcs(label, expected);
Threads::java_threads_do(&vtgcs);
}
}
// Deactivate barriers temporarily: Verifier wants plain heap accesses
ShenandoahGCStateResetter resetter;
// Heap size checks
{
ShenandoahHeapLocker lock(_heap->lock());
ShenandoahCalculateRegionStatsClosure cl;
_heap->heap_region_iterate(&cl);
size_t heap_used = _heap->used();
guarantee(cl.used() == heap_used,
"%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
label,
byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used),
byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used()));
size_t heap_committed = _heap->committed();
guarantee(cl.committed() == heap_committed,
"%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s",
label,
byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed),
byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed()));
}
// Internal heap region checks
if (ShenandoahVerifyLevel >= 1) {
ShenandoahVerifyHeapRegionClosure cl(label, regions);
_heap->heap_region_iterate(&cl);
}
OrderAccess::fence();
_heap->make_parsable(false);
// Allocate temporary bitmap for storing marking wavefront:
_verification_bit_map->clear();
// Allocate temporary array for storing liveness data
ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC);
Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0);
const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate);
// Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap.
// This verifies what application can see, since it only cares about reachable objects.
size_t count_reachable = 0;
if (ShenandoahVerifyLevel >= 2) {
ShenandoahRootVerifier verifier;
switch (weak_roots) {
case _verify_serial_weak_roots:
verifier.excludes(ShenandoahRootVerifier::ConcurrentWeakRoots);
break;
case _verify_concurrent_weak_roots:
verifier.excludes(ShenandoahRootVerifier::SerialWeakRoots);
break;
case _verify_all_weak_roots:
break;
default:
ShouldNotReachHere();
}
ShenandoahVerifierReachableTask task(_verification_bit_map, ld, &verifier, label, options);
_heap->workers()->run_task(&task);
count_reachable = task.processed();
}
// Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector,
// not the application, can see during the region scans. There is no reason to process the objects
// that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS:
// before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics
// what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect)
// version
size_t count_marked = 0;
if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete) {
guarantee(_heap->marking_context()->is_complete(), "Marking context should be complete");
ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options);
_heap->workers()->run_task(&task);
count_marked = task.processed();
} else {
guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be");
}
// Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes
// marked objects.
if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) {
for (size_t i = 0; i < _heap->num_regions(); i++) {
ShenandoahHeapRegion* r = _heap->get_region(i);
juint verf_live = 0;
if (r->is_humongous()) {
// For humongous objects, test if start region is marked live, and if so,
// all humongous regions in that chain have live data equal to their "used".
juint start_live = Atomic::load_acquire(&ld[r->humongous_start_region()->index()]);
if (start_live > 0) {
verf_live = (juint)(r->used() / HeapWordSize);
}
} else {
verf_live = Atomic::load_acquire(&ld[r->index()]);
}
size_t reg_live = r->get_live_data_words();
if (reg_live != verf_live) {
ResourceMark rm;
stringStream ss;
r->print_on(&ss);
fatal("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " UINT32_FORMAT "\n%s",
label, reg_live, verf_live, ss.as_string());
}
}
}
log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)",
label, ShenandoahVerifyLevel, count_reachable, count_marked);
FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld);
}
void ShenandoahVerifier::verify_generic(VerifyOption vo) {
verify_at_safepoint(
"Generic Verification",
_verify_forwarded_allow, // conservatively allow forwarded
_verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
_verify_cset_disable, // cset may be inconsistent
_verify_liveness_disable, // no reliable liveness data
_verify_regions_disable, // no reliable region data
_verify_gcstate_disable, // no data about gcstate
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_before_concmark() {
verify_at_safepoint(
"Before Mark",
_verify_forwarded_none, // UR should have fixed up
_verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
_verify_cset_none, // UR should have fixed this
_verify_liveness_disable, // no reliable liveness data
_verify_regions_notrash, // no trash regions
_verify_gcstate_stable, // there are no forwarded objects
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_after_concmark() {
verify_at_safepoint(
"After Mark",
_verify_forwarded_none, // no forwarded references
_verify_marked_complete, // bitmaps as precise as we can get
_verify_cset_none, // no references to cset anymore
_verify_liveness_complete, // liveness data must be complete here
_verify_regions_disable, // trash regions not yet recycled
_verify_gcstate_stable, // mark should have stabilized the heap
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_before_evacuation() {
// Concurrent weak roots are evacuated during concurrent phase
VerifyWeakRoots verify_weak_roots = ShenandoahConcurrentRoots::should_do_concurrent_class_unloading() ?
_verify_serial_weak_roots :
_verify_all_weak_roots;
verify_at_safepoint(
"Before Evacuation",
_verify_forwarded_none, // no forwarded references
_verify_marked_complete, // walk over marked objects too
_verify_cset_disable, // non-forwarded references to cset expected
_verify_liveness_complete, // liveness data must be complete here
_verify_regions_disable, // trash regions not yet recycled
_verify_gcstate_stable, // mark should have stabilized the heap
verify_weak_roots
);
}
void ShenandoahVerifier::verify_during_evacuation() {
// Concurrent weak roots are evacuated during concurrent phase
VerifyWeakRoots verify_weak_roots = ShenandoahConcurrentRoots::should_do_concurrent_class_unloading() ?
_verify_serial_weak_roots :
_verify_all_weak_roots;
verify_at_safepoint(
"During Evacuation",
_verify_forwarded_allow, // some forwarded references are allowed
_verify_marked_disable, // walk only roots
_verify_cset_disable, // some cset references are not forwarded yet
_verify_liveness_disable, // liveness data might be already stale after pre-evacs
_verify_regions_disable, // trash regions not yet recycled
_verify_gcstate_evacuation, // evacuation is in progress
verify_weak_roots
);
}
void ShenandoahVerifier::verify_after_evacuation() {
verify_at_safepoint(
"After Evacuation",
_verify_forwarded_allow, // objects are still forwarded
_verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
_verify_cset_forwarded, // all cset refs are fully forwarded
_verify_liveness_disable, // no reliable liveness data anymore
_verify_regions_notrash, // trash regions have been recycled already
_verify_gcstate_forwarded, // evacuation produced some forwarded objects
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_before_updaterefs() {
verify_at_safepoint(
"Before Updating References",
_verify_forwarded_allow, // forwarded references allowed
_verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
_verify_cset_forwarded, // all cset refs are fully forwarded
_verify_liveness_disable, // no reliable liveness data anymore
_verify_regions_notrash, // trash regions have been recycled already
_verify_gcstate_forwarded, // evacuation should have produced some forwarded objects
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_after_updaterefs() {
verify_at_safepoint(
"After Updating References",
_verify_forwarded_none, // no forwarded references
_verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
_verify_cset_none, // no cset references, all updated
_verify_liveness_disable, // no reliable liveness data anymore
_verify_regions_nocset, // no cset regions, trash regions have appeared
_verify_gcstate_stable, // update refs had cleaned up forwarded objects
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_after_degenerated() {
verify_at_safepoint(
"After Degenerated GC",
_verify_forwarded_none, // all objects are non-forwarded
_verify_marked_complete, // all objects are marked in complete bitmap
_verify_cset_none, // no cset references
_verify_liveness_disable, // no reliable liveness data anymore
_verify_regions_notrash_nocset, // no trash, no cset
_verify_gcstate_stable, // degenerated refs had cleaned up forwarded objects
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_before_fullgc() {
verify_at_safepoint(
"Before Full GC",
_verify_forwarded_allow, // can have forwarded objects
_verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
_verify_cset_disable, // cset might be foobared
_verify_liveness_disable, // no reliable liveness data anymore
_verify_regions_disable, // no reliable region data here
_verify_gcstate_disable, // no reliable gcstate data
_verify_all_weak_roots
);
}
void ShenandoahVerifier::verify_after_fullgc() {
verify_at_safepoint(
"After Full GC",
_verify_forwarded_none, // all objects are non-forwarded
_verify_marked_complete, // all objects are marked in complete bitmap
_verify_cset_none, // no cset references
_verify_liveness_disable, // no reliable liveness data anymore
_verify_regions_notrash_nocset, // no trash, no cset
_verify_gcstate_stable, // full gc cleaned up everything
_verify_all_weak_roots
);
}
class ShenandoahVerifyNoForwared : public OopClosure {
private:
template <class T>
void do_oop_work(T* p) {
T o = RawAccess<>::oop_load(p);
if (!CompressedOops::is_null(o)) {
oop obj = CompressedOops::decode_not_null(o);
oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
if (obj != fwd) {
ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
"Verify Roots", "Should not be forwarded", __FILE__, __LINE__);
}
}
}
public:
void do_oop(narrowOop* p) { do_oop_work(p); }
void do_oop(oop* p) { do_oop_work(p); }
};
class ShenandoahVerifyInToSpaceClosure : public OopClosure {
private:
template <class T>
void do_oop_work(T* p) {
T o = RawAccess<>::oop_load(p);
if (!CompressedOops::is_null(o)) {
oop obj = CompressedOops::decode_not_null(o);
ShenandoahHeap* heap = ShenandoahHeap::heap();
if (!heap->marking_context()->is_marked(obj)) {
ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
"Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__);
}
if (heap->in_collection_set(obj)) {
ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
"Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__);
}
oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
if (obj != fwd) {
ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
"Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__);
}
}
}
public:
void do_oop(narrowOop* p) { do_oop_work(p); }
void do_oop(oop* p) { do_oop_work(p); }
};
void ShenandoahVerifier::verify_roots_in_to_space() {
ShenandoahRootVerifier verifier;
ShenandoahVerifyInToSpaceClosure cl;
verifier.oops_do(&cl);
}
void ShenandoahVerifier::verify_roots_in_to_space_except(ShenandoahRootVerifier::RootTypes types) {
ShenandoahRootVerifier verifier;
verifier.excludes(types);
ShenandoahVerifyInToSpaceClosure cl;
verifier.oops_do(&cl);
}
void ShenandoahVerifier::verify_roots_no_forwarded() {
ShenandoahRootVerifier verifier;
ShenandoahVerifyNoForwared cl;