1 /*
2 * Copyright (c) 2015, 2022, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
27 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
28
29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
30
31 #include "gc/shared/accessBarrierSupport.inline.hpp"
32 #include "gc/shared/cardTable.hpp"
33 #include "gc/shenandoah/mode/shenandoahMode.hpp"
34 #include "gc/shenandoah/shenandoahAsserts.hpp"
35 #include "gc/shenandoah/shenandoahCardTable.hpp"
36 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
37 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
38 #include "gc/shenandoah/shenandoahGeneration.hpp"
39 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
40 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
41 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
42 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
43 #include "memory/iterator.inline.hpp"
44 #include "oops/oop.inline.hpp"
45
46 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
47 return ShenandoahForwarding::get_forwardee(p);
48 }
49
50 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
51 if (p != nullptr) {
52 return resolve_forwarded_not_null(p);
53 } else {
54 return p;
55 }
56 }
57
58 template <DecoratorSet decorators, class T>
59 inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {
60 assert(ShenandoahLoadRefBarrier, "Should be enabled");
61
62 constexpr bool on_weak = HasDecorator<decorators, ON_WEAK_OOP_REF>::value;
63 constexpr bool on_phantom = HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value;
64
65 // Handle nulls. Strong loads filtered nulls with cset checks.
66 // Weak/phantom loads need to check for nulls here.
67 if (on_weak || on_phantom) {
68 if (obj == nullptr) {
69 return nullptr;
70 }
71 } else {
72 assert(obj != nullptr, "Should have been filtered before");
73 }
74
75 // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
76 if (on_phantom &&
77 _heap->is_concurrent_weak_root_in_progress() &&
78 _heap->is_in_active_generation(obj) &&
79 !_heap->marking_context()->is_marked(obj)) {
80 return nullptr;
81 }
82
83 // Prevent resurrection of unreachable weak references.
84 if (on_weak &&
85 _heap->is_concurrent_weak_root_in_progress() &&
86 _heap->is_in_active_generation(obj) &&
87 !_heap->marking_context()->is_marked_strong(obj)) {
88 return nullptr;
89 }
90
91 // Weak/phantom loads need additional cset check.
92 if (on_phantom || on_weak) {
93 if (!_heap->has_forwarded_objects() || !_heap->in_collection_set(obj)) {
94 return obj;
95 }
96 } else {
97 shenandoah_assert_in_cset(load_addr, obj);
98 }
99
100 oop fwd = ShenandoahForwarding::get_forwardee_mutator(obj);
101 if (obj == fwd) {
102 assert(_heap->is_evacuation_in_progress(), "evac should be in progress");
103 Thread* const t = Thread::current();
104 fwd = _heap->evacuate_object(obj, t);
105 }
106
107 if (load_addr != nullptr && fwd != obj) {
108 // Since we are here and we know the load address, update the reference.
109 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
110 }
111
112 return fwd;
113 }
114
115 inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
116 if (!ShenandoahLoadRefBarrier) {
117 return obj;
118 }
119 if (_heap->has_forwarded_objects() && _heap->in_collection_set(obj)) {
120 // Subsumes null-check
121 assert(obj != nullptr, "cset check must have subsumed null-check");
122 oop fwd = resolve_forwarded_not_null(obj);
123 if (obj == fwd && _heap->is_evacuation_in_progress()) {
124 Thread* t = Thread::current();
125 return _heap->evacuate_object(obj, t);
126 }
127 return fwd;
128 }
129 return obj;
130 }
131
132 template <class T>
133 inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {
134 if (obj == nullptr) {
135 return nullptr;
136 }
137
138 // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
139 if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&
140 _heap->is_concurrent_weak_root_in_progress() &&
141 _heap->is_in_active_generation(obj) &&
142 !_heap->marking_context()->is_marked(obj)) {
143 return nullptr;
144 }
145
146 // Prevent resurrection of unreachable weak references.
147 if ((decorators & ON_WEAK_OOP_REF) != 0 &&
148 _heap->is_concurrent_weak_root_in_progress() &&
149 _heap->is_in_active_generation(obj) &&
150 !_heap->marking_context()->is_marked_strong(obj)) {
151 return nullptr;
152 }
153
154 // Allow runtime to see unreachable objects that are visited during concurrent class-unloading.
155 if ((decorators & AS_NO_KEEPALIVE) != 0 &&
156 _heap->is_concurrent_weak_root_in_progress() &&
157 !_heap->marking_context()->is_marked(obj)) {
158 return obj;
159 }
160
161 oop fwd = load_reference_barrier(obj);
162 if (load_addr != nullptr && fwd != obj) {
163 // Since we are here and we know the load address, update the reference.
164 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
165 }
166
167 return fwd;
168 }
169
170 inline void ShenandoahBarrierSet::enqueue(oop obj, bool filter) {
171 assert(obj != nullptr, "checked by caller");
172 shenandoah_assert_correct(nullptr, obj);
173 assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
174
175 // Filter marked objects before hitting the SATB queues. The same predicate would
176 // be used by SATBMQ::filter to eliminate already marked objects downstream, but
177 // filtering here helps to avoid wasteful SATB queueing work to begin with.
178 if (filter && !_heap->requires_marking(obj)) return;
179
180 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());
181 _satb_mark_queue_set.enqueue_known_active(queue, obj);
182 }
183
184 template <DecoratorSet decorators, typename T>
185 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
186 // Uninitialized and no-keepalive stores do not need barrier.
187 if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
188 HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
189 return;
190 }
191
192 // Stores to weak/phantom require no barrier. The original references would
193 // have been enqueued in the SATB buffer by the load barrier if they were needed.
194 if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value ||
195 HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value) {
196 return;
197 }
198
199 if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
200 T heap_oop = RawAccess<>::oop_load(field);
201 if (!CompressedOops::is_null(heap_oop)) {
202 enqueue(CompressedOops::decode(heap_oop));
203 }
204 }
205 }
206
207 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
208 if (value != nullptr && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
209 enqueue(value);
210 }
211 }
212
213 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
214 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
215 const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
216 const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;
217 if (!peek && !on_strong_oop_ref) {
218 satb_enqueue(value);
219 }
220 }
221
222 template <DecoratorSet decorators, typename T>
223 inline void ShenandoahBarrierSet::write_ref_field_post(T* field) {
224 assert(ShenandoahCardBarrier, "Should have been checked by caller");
225 if (_heap->is_in_young(field)) {
226 // Young field stores do not require card mark.
227 return;
228 }
229 T heap_oop = RawAccess<>::oop_load(field);
230 if (CompressedOops::is_null(heap_oop)) {
231 // Null reference store do not require card mark.
232 return;
233 }
234 oop obj = CompressedOops::decode_not_null(heap_oop);
235 if (!_heap->is_in_young(obj)) {
236 // Not an old->young reference store.
237 return;
238 }
239 volatile CardTable::CardValue* byte = card_table()->byte_for(field);
240 *byte = CardTable::dirty_card_val();
241 }
242
243 template <typename T>
244 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
245 oop value = RawAccess<>::oop_load(addr);
246 value = load_reference_barrier(decorators, value, addr);
247 keep_alive_if_weak(decorators, value);
248 return value;
249 }
250
251 template <typename T>
252 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
253 shenandoah_assert_not_in_cset_except(nullptr, compare_value, (compare_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
254 shenandoah_assert_not_in_cset_except(nullptr, new_value, (new_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
255
256 // Handle the previous value through SATB, as we are about to perform the store.
257 oop prev = RawAccess<>::oop_load(addr);
258 satb_enqueue(prev);
259
260 // Perform LRB on location to fix it up for this and all following accesses.
261 // This guarantees there are no false negatives due to concurrent evacuation,
262 // and the value loaded later by CAS is sanitized by some LRB, or is null.
263 load_reference_barrier(decorators, prev, addr);
264
265 return RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
266 }
267
268 template <typename T>
269 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
270 shenandoah_assert_not_in_cset_except(nullptr, new_value, (new_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
271
272 // Handle the previous value through SATB, as we are about to perform the store.
273 oop prev = RawAccess<>::oop_load(addr);
274 satb_enqueue(prev);
275
276 // Perform LRB on location to fix it up for this and all following accesses.
277 // This is purely opportunistic: we would not have any false negatives here.
278 // This guarantees the value loaded later by XCHG is sanitized by some LRB, or is null.
279 load_reference_barrier(decorators, prev, addr);
280
281 return RawAccess<>::oop_atomic_xchg(addr, new_value);
282 }
283
284 template <DecoratorSet decorators, typename BarrierSetT>
285 template <typename T>
286 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
287 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
288 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
289 return bs->oop_load(decorators, addr);
290 }
291
292 template <DecoratorSet decorators, typename BarrierSetT>
293 template <typename T>
294 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
295 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
296 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
297 return bs->oop_load(decorators, addr);
298 }
299
300 template <DecoratorSet decorators, typename BarrierSetT>
301 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
302 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
303 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
304 return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
305 }
306
307 template <DecoratorSet decorators, typename BarrierSetT>
308 template <typename T>
309 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
310 shenandoah_assert_marked_if(nullptr, value,
311 !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()
312 && !(ShenandoahHeap::heap()->active_generation()->is_young()
313 && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
314 shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
315 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
316 bs->satb_barrier<decorators>(addr);
317 Raw::oop_store(addr, value);
318 }
319
320 template <DecoratorSet decorators, typename BarrierSetT>
321 template <typename T>
322 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
323 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
324 oop_store_common(addr, value);
325 }
326
327 template <DecoratorSet decorators, typename BarrierSetT>
328 template <typename T>
329 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
330 shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
331 shenandoah_assert_not_forwarded_except (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
332
333 oop_store_common(addr, value);
334 if (ShenandoahCardBarrier) {
335 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
336 bs->write_ref_field_post<decorators>(addr);
337 }
338 }
339
340 template <DecoratorSet decorators, typename BarrierSetT>
341 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
342 oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
343 }
344
345 template <DecoratorSet decorators, typename BarrierSetT>
346 template <typename T>
347 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
348 assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
349 assert((decorators & ON_STRONG_OOP_REF) != 0, "CAS only for strong refs");
350 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
351 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
352 }
353
354 template <DecoratorSet decorators, typename BarrierSetT>
355 template <typename T>
356 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
357 assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
358 assert((decorators & ON_STRONG_OOP_REF) != 0, "CAS only for strong refs");
359 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
360 oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
361 if (ShenandoahCardBarrier) {
362 bs->write_ref_field_post<decorators>(addr);
363 }
364 return result;
365 }
366
367 template <DecoratorSet decorators, typename BarrierSetT>
368 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
369 assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
370 assert((decorators & (ON_STRONG_OOP_REF | ON_UNKNOWN_OOP_REF)) != 0, "CAS only for strong refs OR unknown refs (Unsafe)");
371 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
372
373 // Unsafe.compareAndExchange/Set come here with ON_UNKNOWN_OOP_REF set.
374 // These are normally strong refs, but one can use Unsafe on Reference.referent.
375 // We cannot deal with that case. If application does Unsafe operations on
376 // Reference.referent field, this likely breaks weak reference semantics already.
377 // We upgrade the access to strong in (sometimes futile) attempt to maintain heap
378 // integrity, and assert in debug builds for better diagnostics.
379 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
380 assert((resolved_decorators & ON_STRONG_OOP_REF) != 0, "Application error: CAS on weak location");
381 resolved_decorators = (resolved_decorators & ~ON_DECORATOR_MASK) | ON_STRONG_OOP_REF;
382
383 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
384 oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
385 if (ShenandoahCardBarrier) {
386 bs->write_ref_field_post<decorators>(addr);
387 }
388 return result;
389 }
390
391 template <DecoratorSet decorators, typename BarrierSetT>
392 template <typename T>
393 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
394 assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
395 assert((decorators & ON_STRONG_OOP_REF) != 0, "XCHG only for strong refs");
396 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
397 return bs->oop_xchg(decorators, addr, new_value);
398 }
399
400 template <DecoratorSet decorators, typename BarrierSetT>
401 template <typename T>
402 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
403 assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
404 assert((decorators & ON_STRONG_OOP_REF) != 0, "XCHG only for strong refs");
405 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
406 oop result = bs->oop_xchg(decorators, addr, new_value);
407 if (ShenandoahCardBarrier) {
408 bs->write_ref_field_post<decorators>(addr);
409 }
410 return result;
411 }
412
413 template <DecoratorSet decorators, typename BarrierSetT>
414 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
415 assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
416 assert((decorators & (ON_STRONG_OOP_REF | ON_UNKNOWN_OOP_REF)) != 0, "XCHG only for strong refs OR unknown refs (Unsafe)");
417 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
418
419 // Unsafe.getAndSet comes here with ON_UNKNOWN_OOP_REF set.
420 // These are normally strong refs, but one can use Unsafe on Reference.referent.
421 // We cannot deal with that case. If application does Unsafe operations on
422 // Reference.referent field, this likely breaks weak reference semantics already.
423 // We upgrade the access to strong in (sometimes futile) attempt to maintain heap
424 // integrity, and assert in debug builds for better diagnostics.
425 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
426 assert((resolved_decorators & ON_STRONG_OOP_REF) != 0, "Application error: XCHG on weak location");
427 resolved_decorators = (resolved_decorators & ~ON_DECORATOR_MASK) | ON_STRONG_OOP_REF;
428
429 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
430 oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
431 if (ShenandoahCardBarrier) {
432 bs->write_ref_field_post<decorators>(addr);
433 }
434 return result;
435 }
436
437 // Clone barrier support
438 template <bool EVAC>
439 class ShenandoahUpdateEvacForCloneOopClosure : public BasicOopIterateClosure {
440 private:
441 ShenandoahHeap* const _heap;
442 const ShenandoahCollectionSet* const _cset;
443 Thread* const _thread;
444
445 template <class T>
446 inline void do_oop_work(T* p) {
447 T o = RawAccess<>::oop_load(p);
448 if (!CompressedOops::is_null(o)) {
449 oop obj = CompressedOops::decode_not_null(o);
450 if (_cset->is_in(obj)) {
451 oop fwd = ShenandoahForwarding::get_forwardee(obj);
452 if (EVAC && obj == fwd) {
453 fwd = _heap->evacuate_object(obj, _thread);
454 }
455 shenandoah_assert_forwarded_except(p, obj, _heap->cancelled_gc());
456 ShenandoahHeap::atomic_update_oop(fwd, p, o);
457 obj = fwd;
458 }
459 }
460 }
461
462 public:
463 ShenandoahUpdateEvacForCloneOopClosure() :
464 _heap(ShenandoahHeap::heap()),
465 _cset(_heap->collection_set()),
466 _thread(Thread::current()) {}
467
468 virtual void do_oop(oop* p) { do_oop_work(p); }
469 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
470 };
471
472 void ShenandoahBarrierSet::clone_evacuation(oop obj) {
473 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
474 if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
475 ShenandoahUpdateEvacForCloneOopClosure<true> cl;
476 obj->oop_iterate(&cl);
477 }
478 }
479
480 void ShenandoahBarrierSet::clone_update(oop obj) {
481 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
482 if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
483 ShenandoahUpdateEvacForCloneOopClosure<false> cl;
484 obj->oop_iterate(&cl);
485 }
486 }
487
488 template <DecoratorSet decorators, typename BarrierSetT>
489 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
490 // Hot code path, called from compiler/runtime. Make sure fast path is fast.
491
492 // Fix up src before doing the copy, if needed.
493 const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
494 if (gc_state != 0 && ShenandoahCloneBarrier) {
495 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
496 if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
497 bs->clone_evacuation(src);
498 } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
499 bs->clone_update(src);
500 }
501 }
502
503 Raw::clone(src, dst, size);
504
505 // Current allocator never allocates in old, so clone destination is guaranteed to be in young.
506 // Otherwise we need card barriers.
507 shenandoah_assert_in_young_if(nullptr, dst, ShenandoahCardBarrier);
508 }
509
510 template <DecoratorSet decorators, typename BarrierSetT>
511 template <typename T>
512 OopCopyResult ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
513 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
514 size_t length) {
515 T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
516 T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
517
518 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
519 bs->arraycopy_barrier(src, dst, length);
520 OopCopyResult result = Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
521 if (ShenandoahCardBarrier) {
522 bs->write_ref_array((HeapWord*) dst, length);
523 }
524 return result;
525 }
526
527 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
528 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
529 // Young cycles are allowed to run when old marking is in progress. When old marking is in progress,
530 // this barrier will be called with ENQUEUE=true and HAS_FWD=false, even though the young generation
531 // may have forwarded objects.
532 assert(HAS_FWD == _heap->has_forwarded_objects() || _heap->is_concurrent_old_mark_in_progress(), "Forwarded object status is sane");
533 // This function cannot be called to handle marking and evacuation at the same time (they operate on
534 // different sides of the copy).
535 static_assert((HAS_FWD || EVAC) != ENQUEUE, "Cannot evacuate and mark both sides of copy.");
536
537 Thread* thread = Thread::current();
538 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
539 ShenandoahMarkingContext* ctx = _heap->marking_context();
540 const ShenandoahCollectionSet* const cset = _heap->collection_set();
541 T* end = src + count;
542 for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
543 T o = RawAccess<>::oop_load(elem_ptr);
544 if (!CompressedOops::is_null(o)) {
545 oop obj = CompressedOops::decode_not_null(o);
546 if (HAS_FWD && cset->is_in(obj)) {
547 oop fwd = resolve_forwarded_not_null(obj);
548 if (EVAC && obj == fwd) {
549 fwd = _heap->evacuate_object(obj, thread);
550 }
551 shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
552 ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
553 }
554 if (ENQUEUE && !ctx->is_marked_strong(obj)) {
555 _satb_mark_queue_set.enqueue_known_active(queue, obj);
556 }
557 }
558 }
559 }
560
561 template <class T>
562 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
563 if (count == 0) {
564 // No elements to copy, no need for barrier
565 return;
566 }
567
568 const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
569 if ((gc_state & ShenandoahHeap::MARKING) != 0) {
570 // If marking old or young, we must evaluate the SATB barrier. This will be the only
571 // action if we are not marking old. If we are marking old, we must still evaluate the
572 // load reference barrier for a young collection.
573 if (_heap->mode()->is_generational()) {
574 arraycopy_marking<true>(dst, count);
575 } else {
576 arraycopy_marking<false>(dst, count);
577 }
578 }
579
580 if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
581 assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during evacuation");
582 arraycopy_evacuation(src, count);
583 } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
584 assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during update-refs");
585 arraycopy_update(src, count);
586 }
587 }
588
589 template <bool IS_GENERATIONAL, class T>
590 void ShenandoahBarrierSet::arraycopy_marking(T* dst, size_t count) {
591 assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
592 if (ShenandoahSATBBarrier) {
593 if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(dst)) ||
594 (IS_GENERATIONAL && _heap->heap_region_containing(dst)->is_old() && _heap->is_concurrent_young_mark_in_progress())) {
595 arraycopy_work<T, false, false, true>(dst, count);
596 }
597 }
598 }
599
600 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
601 return ary < _heap->heap_region_containing(ary)->get_update_watermark();
602 }
603
604 template <class T>
605 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
606 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
607 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
608 arraycopy_work<T, true, true, false>(src, count);
609 }
610 }
611
612 template <class T>
613 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
614 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
615 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
616 arraycopy_work<T, true, false, false>(src, count);
617 }
618 }
619
620 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP