Skip to content

Commit b9d49dc

Browse files
committed
8337981: ShenandoahHeap::is_in should check for alive regions
Reviewed-by: rkennke, wkemper
1 parent 9775d57 commit b9d49dc

12 files changed

+111
-60
lines changed

src/hotspot/share/gc/shenandoah/shenandoahAsserts.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void print_raw_memory(ShenandoahMessageBuffer &msg, void* loc) {
3737
// should be in heap, in known committed region, within that region.
3838

3939
ShenandoahHeap* heap = ShenandoahHeap::heap();
40-
if (!heap->is_in(loc)) return;
40+
if (!heap->is_in_reserved(loc)) return;
4141

4242
ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
4343
if (r != nullptr && r->is_committed()) {
@@ -77,7 +77,7 @@ void ShenandoahAsserts::print_obj(ShenandoahMessageBuffer& msg, oop obj) {
7777

7878
void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) {
7979
ShenandoahHeap* heap = ShenandoahHeap::heap();
80-
if (heap->is_in(loc)) {
80+
if (heap->is_in_reserved(loc)) {
8181
msg.append(" inside Java heap\n");
8282
ShenandoahHeapRegion *r = heap->heap_region_containing(loc);
8383
stringStream ss;
@@ -96,7 +96,7 @@ void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) {
9696
void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) {
9797
ShenandoahHeap* heap = ShenandoahHeap::heap();
9898
msg.append(" " PTR_FORMAT " - safe print, no details\n", p2i(loc));
99-
if (heap->is_in(loc)) {
99+
if (heap->is_in_reserved(loc)) {
100100
ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
101101
if (r != nullptr) {
102102
stringStream ss;
@@ -113,7 +113,7 @@ void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_l
113113
ShenandoahHeap* heap = ShenandoahHeap::heap();
114114
ResourceMark rm;
115115

116-
bool loc_in_heap = (loc != nullptr && heap->is_in(loc));
116+
bool loc_in_heap = (loc != nullptr && heap->is_in_reserved(loc));
117117

118118
ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label);
119119

@@ -166,22 +166,22 @@ void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_l
166166
report_vm_error(file, line, msg.buffer());
167167
}
168168

169-
void ShenandoahAsserts::assert_in_heap(void* interior_loc, oop obj, const char *file, int line) {
169+
void ShenandoahAsserts::assert_in_heap_bounds(void* interior_loc, oop obj, const char *file, int line) {
170170
ShenandoahHeap* heap = ShenandoahHeap::heap();
171171

172-
if (!heap->is_in(obj)) {
173-
print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap failed",
174-
"oop must point to a heap address",
172+
if (!heap->is_in_reserved(obj)) {
173+
print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds failed",
174+
"oop must be in heap bounds",
175175
file, line);
176176
}
177177
}
178178

179-
void ShenandoahAsserts::assert_in_heap_or_null(void* interior_loc, oop obj, const char *file, int line) {
179+
void ShenandoahAsserts::assert_in_heap_bounds_or_null(void* interior_loc, oop obj, const char *file, int line) {
180180
ShenandoahHeap* heap = ShenandoahHeap::heap();
181181

182-
if (obj != nullptr && !heap->is_in(obj)) {
183-
print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_or_null failed",
184-
"oop must point to a heap address",
182+
if (obj != nullptr && !heap->is_in_reserved(obj)) {
183+
print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds_or_null failed",
184+
"oop must be in heap bounds",
185185
file, line);
186186
}
187187
}
@@ -191,9 +191,9 @@ void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char*
191191

192192
// Step 1. Check that obj is correct.
193193
// After this step, it is safe to call heap_region_containing().
194-
if (!heap->is_in(obj)) {
194+
if (!heap->is_in_reserved(obj)) {
195195
print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
196-
"oop must point to a heap address",
196+
"oop must be in heap bounds",
197197
file, line);
198198
}
199199

@@ -210,6 +210,12 @@ void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char*
210210
file,line);
211211
}
212212

213+
if (!heap->is_in(obj)) {
214+
print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
215+
"Object should be in active region area",
216+
file, line);
217+
}
218+
213219
oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
214220

215221
if (obj != fwd) {
@@ -223,9 +229,9 @@ void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char*
223229
}
224230

225231
// Step 2. Check that forwardee is correct
226-
if (!heap->is_in(fwd)) {
232+
if (!heap->is_in_reserved(fwd)) {
227233
print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
228-
"Forwardee must point to a heap address",
234+
"Forwardee must be in heap bounds",
229235
file, line);
230236
}
231237

@@ -236,9 +242,15 @@ void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char*
236242
}
237243

238244
// Step 3. Check that forwardee points to correct region
245+
if (!heap->is_in(fwd)) {
246+
print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
247+
"Forwardee should be in active region area",
248+
file, line);
249+
}
250+
239251
if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) {
240252
print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
241-
"Non-trivial forwardee should in another region",
253+
"Non-trivial forwardee should be in another region",
242254
file, line);
243255
}
244256

src/hotspot/share/gc/shenandoah/shenandoahAsserts.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class ShenandoahAsserts {
5353
static void print_rp_failure(const char *label, BoolObjectClosure* actual,
5454
const char *file, int line);
5555

56-
static void assert_in_heap(void* interior_loc, oop obj, const char* file, int line);
57-
static void assert_in_heap_or_null(void* interior_loc, oop obj, const char* file, int line);
56+
static void assert_in_heap_bounds(void* interior_loc, oop obj, const char* file, int line);
57+
static void assert_in_heap_bounds_or_null(void* interior_loc, oop obj, const char* file, int line);
5858
static void assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line);
5959

6060
static void assert_correct(void* interior_loc, oop obj, const char* file, int line);
@@ -74,10 +74,10 @@ class ShenandoahAsserts {
7474
static void assert_heaplocked_or_safepoint(const char* file, int line);
7575

7676
#ifdef ASSERT
77-
#define shenandoah_assert_in_heap(interior_loc, obj) \
78-
ShenandoahAsserts::assert_in_heap(interior_loc, obj, __FILE__, __LINE__)
79-
#define shenandoah_assert_in_heap_or_null(interior_loc, obj) \
80-
ShenandoahAsserts::assert_in_heap_or_null(interior_loc, obj, __FILE__, __LINE__)
77+
#define shenandoah_assert_in_heap_bounds(interior_loc, obj) \
78+
ShenandoahAsserts::assert_in_heap_bounds(interior_loc, obj, __FILE__, __LINE__)
79+
#define shenandoah_assert_in_heap_bounds_or_null(interior_loc, obj) \
80+
ShenandoahAsserts::assert_in_heap_bounds_or_null(interior_loc, obj, __FILE__, __LINE__)
8181
#define shenandoah_assert_in_correct_region(interior_loc, obj) \
8282
ShenandoahAsserts::assert_in_correct_region(interior_loc, obj, __FILE__, __LINE__)
8383

@@ -164,8 +164,8 @@ class ShenandoahAsserts {
164164
#define shenandoah_assert_heaplocked_or_safepoint() \
165165
ShenandoahAsserts::assert_heaplocked_or_safepoint(__FILE__, __LINE__)
166166
#else
167-
#define shenandoah_assert_in_heap(interior_loc, obj)
168-
#define shenandoah_assert_in_heap_or_null(interior_loc, obj)
167+
#define shenandoah_assert_in_heap_bounds(interior_loc, obj)
168+
#define shenandoah_assert_in_heap_bounds_or_null(interior_loc, obj)
169169
#define shenandoah_assert_in_correct_region(interior_loc, obj)
170170

171171
#define shenandoah_assert_correct_if(interior_loc, obj, condition)

src/hotspot/share/gc/shenandoah/shenandoahCollectionSet.inline.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ bool ShenandoahCollectionSet::is_in(ShenandoahHeapRegion* r) const {
4141
}
4242

4343
bool ShenandoahCollectionSet::is_in(oop p) const {
44-
shenandoah_assert_in_heap_or_null(nullptr, p);
44+
shenandoah_assert_in_heap_bounds_or_null(nullptr, p);
4545
return is_in_loc(cast_from_oop<void*>(p));
4646
}
4747

4848
bool ShenandoahCollectionSet::is_in_loc(void* p) const {
49-
assert(p == nullptr || _heap->is_in(p), "Must be in the heap");
49+
assert(p == nullptr || _heap->is_in_reserved(p), "Must be in the heap");
5050
uintx index = ((uintx) p) >> _region_size_bytes_shift;
5151
// no need to subtract the bottom of the heap from p,
5252
// _biased_cset_map is biased

src/hotspot/share/gc/shenandoah/shenandoahConcurrentGC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ void ShenandoahEvacUpdateCleanupOopStorageRootsClosure::do_oop(oop* p) {
718718
const oop obj = RawAccess<>::oop_load(p);
719719
if (!CompressedOops::is_null(obj)) {
720720
if (!_mark_context->is_marked(obj)) {
721-
shenandoah_assert_correct(p, obj);
721+
// Note: The obj is dead here. Do not touch it, just clear.
722722
ShenandoahHeap::atomic_clear_oop(p, obj);
723723
} else if (_evac_in_progress && _heap->in_collection_set(obj)) {
724724
oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);

src/hotspot/share/gc/shenandoah/shenandoahForwarding.inline.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "runtime/javaThread.hpp"
3333

3434
inline oop ShenandoahForwarding::get_forwardee_raw(oop obj) {
35-
shenandoah_assert_in_heap(nullptr, obj);
35+
shenandoah_assert_in_heap_bounds(nullptr, obj);
3636
return get_forwardee_raw_unchecked(obj);
3737
}
3838

src/hotspot/share/gc/shenandoah/shenandoahHeap.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,18 @@ size_t ShenandoahHeap::initial_capacity() const {
730730
}
731731

732732
bool ShenandoahHeap::is_in(const void* p) const {
733-
HeapWord* heap_base = (HeapWord*) base();
734-
HeapWord* last_region_end = heap_base + ShenandoahHeapRegion::region_size_words() * num_regions();
735-
return p >= heap_base && p < last_region_end;
733+
if (is_in_reserved(p)) {
734+
if (is_full_gc_move_in_progress()) {
735+
// Full GC move is running, we do not have a consistent region
736+
// information yet. But we know the pointer is in heap.
737+
return true;
738+
}
739+
// Now check if we point to a live section in active region.
740+
ShenandoahHeapRegion* r = heap_region_containing(p);
741+
return (r->is_active() && p < r->top());
742+
} else {
743+
return false;
744+
}
736745
}
737746

738747
void ShenandoahHeap::maybe_uncommit(double shrink_before, size_t shrink_until) {

src/hotspot/share/gc/shenandoah/shenandoahHeap.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ class ShenandoahHeap : public CollectedHeap, public ShenandoahSpaceInfo {
493493
public:
494494
bool is_maximal_no_gc() const override shenandoah_not_implemented_return(false);
495495

496+
// Check the pointer is in active part of Java heap.
497+
// Use is_in_reserved to check if object is within heap bounds.
496498
bool is_in(const void* p) const override;
497499

498500
bool requires_barriers(stackChunkOop obj) const override;

src/hotspot/share/gc/shenandoah/shenandoahMarkBitMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void ShenandoahMarkBitMap::clear_range_large(MemRegion mr) {
122122

123123
#ifdef ASSERT
124124
void ShenandoahMarkBitMap::check_mark(HeapWord* addr) const {
125-
assert(ShenandoahHeap::heap()->is_in(addr),
125+
assert(ShenandoahHeap::heap()->is_in_reserved(addr),
126126
"Trying to access bitmap " PTR_FORMAT " for address " PTR_FORMAT " not in the heap.",
127127
p2i(this), p2i(addr));
128128
}

src/hotspot/share/gc/shenandoah/shenandoahMarkingContext.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ class ShenandoahMarkingContext : public CHeapObj<mtGC> {
6363
inline bool mark_weak(oop obj);
6464

6565
// Simple versions of marking accessors, to be used outside of marking (e.g. no possible concurrent updates)
66-
inline bool is_marked(oop) const;
66+
inline bool is_marked(oop obj) const;
67+
inline bool is_marked(HeapWord* raw_obj) const;
6768
inline bool is_marked_strong(oop obj) const;
69+
inline bool is_marked_strong(HeapWord* raw_obj) const;
6870
inline bool is_marked_weak(oop obj) const;
6971

7072
inline HeapWord* get_next_marked_addr(HeapWord* addr, HeapWord* limit) const;

src/hotspot/share/gc/shenandoah/shenandoahMarkingContext.inline.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,19 @@ inline bool ShenandoahMarkingContext::mark_weak(oop obj) {
3838
}
3939

4040
inline bool ShenandoahMarkingContext::is_marked(oop obj) const {
41-
return allocated_after_mark_start(obj) || _mark_bit_map.is_marked(cast_from_oop<HeapWord *>(obj));
41+
return is_marked(cast_from_oop<HeapWord*>(obj));
42+
}
43+
44+
inline bool ShenandoahMarkingContext::is_marked(HeapWord* raw_obj) const {
45+
return allocated_after_mark_start(raw_obj) || _mark_bit_map.is_marked(raw_obj);
4246
}
4347

4448
inline bool ShenandoahMarkingContext::is_marked_strong(oop obj) const {
45-
return allocated_after_mark_start(obj) || _mark_bit_map.is_marked_strong(cast_from_oop<HeapWord*>(obj));
49+
return is_marked_strong(cast_from_oop<HeapWord*>(obj));
50+
}
51+
52+
inline bool ShenandoahMarkingContext::is_marked_strong(HeapWord* raw_obj) const {
53+
return allocated_after_mark_start(raw_obj) || _mark_bit_map.is_marked_strong(raw_obj);
4654
}
4755

4856
inline bool ShenandoahMarkingContext::is_marked_weak(oop obj) const {

0 commit comments

Comments
 (0)