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 template <DecoratorSet decorators, class T>
 47 inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {
 48   assert(ShenandoahLoadRefBarrier, "Should be enabled");
 49 
 50   constexpr bool on_weak    = HasDecorator<decorators, ON_WEAK_OOP_REF>::value;
 51   constexpr bool on_phantom = HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value;
 52 
 53   // Handle nulls. Strong loads filtered nulls with cset checks.
 54   // Weak/phantom loads need to check for nulls here.
 55   if (on_weak || on_phantom) {
 56     if (obj == nullptr) {
 57       return nullptr;
 58     }
 59   } else {
 60     assert(obj != nullptr, "Should have been filtered before");
 61   }
 62 
 63   // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
 64   if (on_phantom &&
 65       _heap->is_concurrent_weak_root_in_progress() &&
 66       _heap->is_in_active_generation(obj) &&
 67       !_heap->marking_context()->is_marked(obj)) {
 68     return nullptr;
 69   }
 70 
 71   // Prevent resurrection of unreachable weak references.
 72   if (on_weak &&
 73       _heap->is_concurrent_weak_root_in_progress() &&
 74       _heap->is_in_active_generation(obj) &&
 75       !_heap->marking_context()->is_marked_strong(obj)) {
 76     return nullptr;
 77   }
 78 
 79   // Weak/phantom loads need additional cset check.
 80   if (on_phantom || on_weak) {
 81     if (!_heap->has_forwarded_objects() || !_heap->in_collection_set(obj)) {
 82       return obj;
 83     }
 84   } else {
 85     shenandoah_assert_in_cset(load_addr, obj);
 86   }
 87 
 88   oop fwd = ShenandoahForwarding::get_forwardee_mutator(obj);
 89   if (obj == fwd) {
 90     assert(_heap->is_evacuation_in_progress(), "evac should be in progress");
 91     Thread* const t = Thread::current();
 92     fwd = _heap->evacuate_object(obj, t);
 93   }
 94 
 95   if (load_addr != nullptr && fwd != obj) {
 96     // Since we are here and we know the load address, update the reference.
 97     ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
 98   }
 99 
100   return fwd;
101 }
102 
103 inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
104   if (!ShenandoahLoadRefBarrier) {
105     return obj;
106   }
107   if (_heap->has_forwarded_objects() && _heap->in_collection_set(obj)) {
108     // Subsumes null-check
109     assert(obj != nullptr, "cset check must have subsumed null-check");
110     oop fwd = ShenandoahForwarding::get_forwardee(obj);
111     if (obj == fwd && _heap->is_evacuation_in_progress()) {
112       Thread* t = Thread::current();
113       return _heap->evacuate_object(obj, t);
114     }
115     return fwd;
116   }
117   return obj;
118 }
119 
120 template <class T>
121 inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {
122   if (obj == nullptr) {
123     return nullptr;
124   }
125 
126   // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
127   if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&
128       _heap->is_concurrent_weak_root_in_progress() &&
129       _heap->is_in_active_generation(obj) &&
130       !_heap->marking_context()->is_marked(obj)) {
131     return nullptr;
132   }
133 
134   // Prevent resurrection of unreachable weak references.
135   if ((decorators & ON_WEAK_OOP_REF) != 0 &&
136       _heap->is_concurrent_weak_root_in_progress() &&
137       _heap->is_in_active_generation(obj) &&
138       !_heap->marking_context()->is_marked_strong(obj)) {
139     return nullptr;
140   }
141 
142   // Allow runtime to see unreachable objects that are visited during concurrent class-unloading.
143   if ((decorators & AS_NO_KEEPALIVE) != 0 &&
144       _heap->is_concurrent_weak_root_in_progress() &&
145       !_heap->marking_context()->is_marked(obj)) {
146     return obj;
147   }
148 
149   oop fwd = load_reference_barrier(obj);
150   if (load_addr != nullptr && fwd != obj) {
151     // Since we are here and we know the load address, update the reference.
152     ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
153   }
154 
155   return fwd;
156 }
157 
158 inline void ShenandoahBarrierSet::enqueue(oop obj, bool filter) {
159   assert(obj != nullptr, "checked by caller");
160   shenandoah_assert_correct(nullptr, obj);
161   assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
162 
163   // Filter marked objects before hitting the SATB queues. The same predicate would
164   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
165   // filtering here helps to avoid wasteful SATB queueing work to begin with.
166   if (filter && !_heap->requires_marking(obj)) return;
167 
168   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());
169   _satb_mark_queue_set.enqueue_known_active(queue, obj);
170 }
171 
172 template <DecoratorSet decorators, typename T>
173 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
174   // Uninitialized and no-keepalive stores do not need barrier.
175   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
176       HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
177     return;
178   }
179 
180   // Stores to weak/phantom require no barrier. The original references would
181   // have been enqueued in the SATB buffer by the load barrier if they were needed.
182   if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value ||
183       HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value) {
184     return;
185   }
186 
187   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
188     T heap_oop = RawAccess<>::oop_load(field);
189     if (!CompressedOops::is_null(heap_oop)) {
190       enqueue(CompressedOops::decode(heap_oop));
191     }
192   }
193 }
194 
195 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
196   if (value != nullptr && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
197     enqueue(value);
198   }
199 }
200 
201 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
202   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
203   const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
204   const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
205   if (!peek && !on_strong_oop_ref) {
206     satb_enqueue(value);
207   }
208 }
209 
210 template <DecoratorSet decorators, typename T>
211 inline void ShenandoahBarrierSet::write_ref_field_post(T* field) {
212   assert(ShenandoahCardBarrier, "Should have been checked by caller");
213   if (_heap->is_in_young(field)) {
214     // Young field stores do not require card mark.
215     return;
216   }
217   T heap_oop = RawAccess<>::oop_load(field);
218   if (CompressedOops::is_null(heap_oop)) {
219     // Null reference store do not require card mark.
220     return;
221   }
222   oop obj = CompressedOops::decode_not_null(heap_oop);
223   if (!_heap->is_in_young(obj)) {
224     // Not an old->young reference store.
225     return;
226   }
227   volatile CardTable::CardValue* byte = card_table()->byte_for(field);
228   *byte = CardTable::dirty_card_val();
229 }
230 
231 template <typename T>
232 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
233   oop value = RawAccess<>::oop_load(addr);
234   value = load_reference_barrier(decorators, value, addr);
235   keep_alive_if_weak(decorators, value);
236   return value;
237 }
238 
239 template <typename T>
240 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
241   shenandoah_assert_not_in_cset_except(nullptr, compare_value, (compare_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
242   shenandoah_assert_not_in_cset_except(nullptr, new_value, (new_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
243 
244   // Handle the previous value through SATB, as we are about to perform the store.
245   oop prev = RawAccess<>::oop_load(addr);
246   satb_enqueue(prev);
247 
248   // Perform LRB on location to fix it up for this and all following accesses.
249   // This guarantees there are no false negatives due to concurrent evacuation,
250   // and the value loaded later by CAS is sanitized by some LRB, or is null.
251   load_reference_barrier(decorators, prev, addr);
252 
253   return RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
254 }
255 
256 template <typename T>
257 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
258   shenandoah_assert_not_in_cset_except(nullptr, new_value, (new_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
259 
260   // Handle the previous value through SATB, as we are about to perform the store.
261   oop prev = RawAccess<>::oop_load(addr);
262   satb_enqueue(prev);
263 
264   // Perform LRB on location to fix it up for this and all following accesses.
265   // This is purely opportunistic: we would not have any false negatives here.
266   // This guarantees the value loaded later by XCHG is sanitized by some LRB, or is null.
267   load_reference_barrier(decorators, prev, addr);
268 
269   return RawAccess<>::oop_atomic_xchg(addr, new_value);
270 }
271 
272 template <DecoratorSet decorators, typename BarrierSetT>
273 template <typename T>
274 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
275   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
276   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
277   return bs->oop_load(decorators, addr);
278 }
279 
280 template <DecoratorSet decorators, typename BarrierSetT>
281 template <typename T>
282 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
283   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
284   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
285   return bs->oop_load(decorators, addr);
286 }
287 
288 template <DecoratorSet decorators, typename BarrierSetT>
289 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
290   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
291   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
292   return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
293 }
294 
295 template <DecoratorSet decorators, typename BarrierSetT>
296 template <typename T>
297 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
298   shenandoah_assert_marked_if(nullptr, value,
299                               !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()
300                               && !(ShenandoahHeap::heap()->active_generation()->is_young()
301                                    && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
302   shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
303   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
304   bs->satb_barrier<decorators>(addr);
305   Raw::oop_store(addr, value);
306 }
307 
308 template <DecoratorSet decorators, typename BarrierSetT>
309 template <typename T>
310 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
311   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
312   oop_store_common(addr, value);
313 }
314 
315 template <DecoratorSet decorators, typename BarrierSetT>
316 template <typename T>
317 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
318   shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
319   shenandoah_assert_not_forwarded_except  (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
320 
321   oop_store_common(addr, value);
322   if (ShenandoahCardBarrier) {
323     ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
324     bs->write_ref_field_post<decorators>(addr);
325   }
326 }
327 
328 template <DecoratorSet decorators, typename BarrierSetT>
329 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
330   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
331 }
332 
333 template <DecoratorSet decorators, typename BarrierSetT>
334 template <typename T>
335 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
336   assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
337   assert((decorators & ON_STRONG_OOP_REF) != 0, "CAS only for strong refs");
338   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
339   return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
340 }
341 
342 template <DecoratorSet decorators, typename BarrierSetT>
343 template <typename T>
344 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
345   assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
346   assert((decorators & ON_STRONG_OOP_REF) != 0, "CAS only for strong refs");
347   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
348   oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
349   if (ShenandoahCardBarrier) {
350     bs->write_ref_field_post<decorators>(addr);
351   }
352   return result;
353 }
354 
355 template <DecoratorSet decorators, typename BarrierSetT>
356 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, 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 | ON_UNKNOWN_OOP_REF)) != 0, "CAS only for strong refs OR unknown refs (Unsafe)");
359   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
360 
361   // Unsafe.compareAndExchange/Set come here with ON_UNKNOWN_OOP_REF set.
362   // These are normally strong refs, but one can use Unsafe on Reference.referent.
363   // We cannot deal with that case. If application does Unsafe operations on
364   // Reference.referent field, this likely breaks weak reference semantics already.
365   // We upgrade the access to strong in (sometimes futile) attempt to maintain heap
366   // integrity, and assert in debug builds for better diagnostics.
367   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
368   assert((resolved_decorators & ON_STRONG_OOP_REF) != 0, "Application error: CAS on weak location");
369   resolved_decorators = (resolved_decorators & ~ON_DECORATOR_MASK) | ON_STRONG_OOP_REF;
370 
371   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
372   oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
373   if (ShenandoahCardBarrier) {
374     bs->write_ref_field_post<decorators>(addr);
375   }
376   return result;
377 }
378 
379 template <DecoratorSet decorators, typename BarrierSetT>
380 template <typename T>
381 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
382   assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
383   assert((decorators & ON_STRONG_OOP_REF) != 0, "XCHG only for strong refs");
384   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
385   return bs->oop_xchg(decorators, addr, new_value);
386 }
387 
388 template <DecoratorSet decorators, typename BarrierSetT>
389 template <typename T>
390 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
391   assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
392   assert((decorators & ON_STRONG_OOP_REF) != 0, "XCHG only for strong refs");
393   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
394   oop result = bs->oop_xchg(decorators, addr, new_value);
395   if (ShenandoahCardBarrier) {
396     bs->write_ref_field_post<decorators>(addr);
397   }
398   return result;
399 }
400 
401 template <DecoratorSet decorators, typename BarrierSetT>
402 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
403   assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
404   assert((decorators & (ON_STRONG_OOP_REF | ON_UNKNOWN_OOP_REF)) != 0, "XCHG only for strong refs OR unknown refs (Unsafe)");
405   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
406 
407   // Unsafe.getAndSet comes here with ON_UNKNOWN_OOP_REF set.
408   // These are normally strong refs, but one can use Unsafe on Reference.referent.
409   // We cannot deal with that case. If application does Unsafe operations on
410   // Reference.referent field, this likely breaks weak reference semantics already.
411   // We upgrade the access to strong in (sometimes futile) attempt to maintain heap
412   // integrity, and assert in debug builds for better diagnostics.
413   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
414   assert((resolved_decorators & ON_STRONG_OOP_REF) != 0, "Application error: XCHG on weak location");
415   resolved_decorators = (resolved_decorators & ~ON_DECORATOR_MASK) | ON_STRONG_OOP_REF;
416 
417   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
418   oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
419   if (ShenandoahCardBarrier) {
420     bs->write_ref_field_post<decorators>(addr);
421   }
422   return result;
423 }
424 
425 // Clone barrier support
426 template <bool EVAC>
427 class ShenandoahUpdateEvacForCloneOopClosure : public BasicOopIterateClosure {
428 private:
429   ShenandoahHeap* const _heap;
430   const ShenandoahCollectionSet* const _cset;
431   Thread* const _thread;
432 
433   template <class T>
434   inline void do_oop_work(T* p) {
435     T o = RawAccess<>::oop_load(p);
436     if (!CompressedOops::is_null(o)) {
437       oop obj = CompressedOops::decode_not_null(o);
438       if (_cset->is_in(obj)) {
439         oop fwd = ShenandoahForwarding::get_forwardee(obj);
440         if (EVAC && obj == fwd) {
441           fwd = _heap->evacuate_object(obj, _thread);
442         }
443         shenandoah_assert_forwarded_except(p, obj, _heap->cancelled_gc());
444         ShenandoahHeap::atomic_update_oop(fwd, p, o);
445         obj = fwd;
446       }
447     }
448   }
449 
450 public:
451   ShenandoahUpdateEvacForCloneOopClosure() :
452           _heap(ShenandoahHeap::heap()),
453           _cset(_heap->collection_set()),
454           _thread(Thread::current()) {}
455 
456   virtual void do_oop(oop* p)       { do_oop_work(p); }
457   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
458 };
459 
460 template <bool EVAC>
461 void ShenandoahBarrierSet::clone_work(oop obj) {
462   if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
463     ShenandoahUpdateEvacForCloneOopClosure<EVAC> cl;
464     obj->oop_iterate(&cl);
465   }
466 }
467 
468 template <DecoratorSet decorators, typename BarrierSetT>
469 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
470   // Hot code path, called from compiler/runtime. Make sure fast path is fast.
471 
472   // Fix up src before doing the copy, if needed.
473   const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
474   if (gc_state != 0 && ShenandoahCloneBarrier) {
475     ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
476     if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
477       bs->clone_work<true>(src);
478     } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
479       bs->clone_work<false>(src);
480     }
481   }
482 
483   Raw::clone(src, dst, size);
484 
485   // Current allocator never allocates in old, so clone destination is guaranteed to be in young.
486   // Otherwise we need card barriers.
487   shenandoah_assert_in_young_if(nullptr, dst, ShenandoahCardBarrier);
488 }
489 
490 template <DecoratorSet decorators, typename BarrierSetT>
491 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::value_copy_in_heap(const ValuePayload& src, const ValuePayload& dst) {
492   precond(src.klass() == dst.klass());
493 
494   const InlineKlass* md = src.klass();
495   if (!md->contains_oops()) {
496     // If we do not have oops in the flat array, we can just do a raw copy.
497     Raw::value_copy(src, dst);
498   } else {
499     ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
500     // addr() points at the payload start, the oop map offset are relative to
501     // the object header, adjust address to account for this discrepancy.
502     const address oop_map_adjusted_src_addr = src.addr() - md->payload_offset();
503     const address oop_map_adjusted_dst_addr = dst.addr() - md->payload_offset();
504     typedef typename ValueOopType<decorators>::type OopType;
505 
506     // Oop maps tell us where the array-like structures are in the value payload.
507     // For simplicity, apply arraycopy barriers over them.
508     bool dest_uninit = HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value;
509     {
510       OopMapBlock* map = md->start_of_nonstatic_oop_maps();
511       OopMapBlock* const end = map + md->nonstatic_oop_map_count();
512 
513       while (map != end) {
514         address src_oop_address = oop_map_adjusted_src_addr + map->offset();
515         address dst_oop_address = oop_map_adjusted_dst_addr + map->offset();
516         bs->arraycopy_barrier((OopType*) src_oop_address, (OopType*) dst_oop_address, map->count(), dest_uninit);
517         map++;
518       }
519     }
520 
521     Raw::value_copy(src, dst);
522 
523     // Similarly, we can ask for ref array store helper for post-barriers.
524     if (ShenandoahCardBarrier) {
525       OopMapBlock* map = md->start_of_nonstatic_oop_maps();
526       OopMapBlock* const end = map + md->nonstatic_oop_map_count();
527       while (map != end) {
528         address dst_oop_address = oop_map_adjusted_dst_addr + map->offset();
529         bs->write_ref_array((HeapWord*) dst_oop_address, map->count());
530         map++;
531       }
532     }
533   }
534 }
535 
536 template <DecoratorSet decorators, typename BarrierSetT>
537 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::value_store_null_in_heap(const ValuePayload& dst) {
538   const InlineKlass* md = dst.klass();
539   if (!md->contains_oops()) {
540     // If we do not have oops in the flat array, we can just do a raw clear.
541     Raw::value_store_null(dst);
542   } else {
543     ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
544     // addr() points at the payload start, the oop map offset are relative to
545     // the object header, adjust address to account for this discrepancy.
546     const address oop_map_adjusted_dst_addr = dst.addr() - md->payload_offset();
547     typedef typename ValueOopType<decorators>::type OopType;
548 
549     // Oop maps tell us where the array-like structures are in the value payload.
550     // For simplicity, apply arraycopy barriers over them.
551     bool dest_uninit = HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value;
552     if (!dest_uninit) {
553       OopMapBlock* map = md->start_of_nonstatic_oop_maps();
554       OopMapBlock* const end = map + md->nonstatic_oop_map_count();
555       while (map != end) {
556         address dst_oop_address = oop_map_adjusted_dst_addr + map->offset();
557         bs->arraycopy_barrier((OopType*) dst_oop_address, (OopType*) dst_oop_address, map->count(), dest_uninit);
558         map++;
559       }
560     }
561 
562     Raw::value_store_null(dst);
563 
564     // Storing null does not require post-barriers
565   }
566 }
567 
568 template <DecoratorSet decorators, typename BarrierSetT>
569 template <typename T>
570 OopCopyResult ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
571                                                                                                   arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
572                                                                                                   size_t length) {
573   T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
574   T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
575   bool dest_uninit = HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value;
576 
577   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
578   bs->arraycopy_barrier(src, dst, length, dest_uninit);
579   OopCopyResult result = Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
580   if (ShenandoahCardBarrier) {
581     bs->write_ref_array((HeapWord*) dst, length);
582   }
583   return result;
584 }
585 
586 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
587 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
588   // Young cycles are allowed to run when old marking is in progress. When old marking is in progress,
589   // this barrier will be called with ENQUEUE=true and HAS_FWD=false, even though the young generation
590   // may have forwarded objects.
591   assert(HAS_FWD == _heap->has_forwarded_objects() || _heap->is_concurrent_old_mark_in_progress(), "Forwarded object status is sane");
592   // This function cannot be called to handle marking and evacuation at the same time (they operate on
593   // different sides of the copy).
594   static_assert((HAS_FWD || EVAC) != ENQUEUE, "Cannot evacuate and mark both sides of copy.");
595 
596   Thread* thread = Thread::current();
597   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
598   ShenandoahMarkingContext* ctx = _heap->marking_context();
599   const ShenandoahCollectionSet* const cset = _heap->collection_set();
600   T* end = src + count;
601   for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
602     T o = RawAccess<>::oop_load(elem_ptr);
603     if (!CompressedOops::is_null(o)) {
604       oop obj = CompressedOops::decode_not_null(o);
605       if (HAS_FWD && cset->is_in(obj)) {
606         oop fwd = ShenandoahForwarding::get_forwardee(obj);
607         if (EVAC && obj == fwd) {
608           fwd = _heap->evacuate_object(obj, thread);
609         }
610         shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
611         ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
612       }
613       if (ENQUEUE && !ctx->is_marked_strong(obj)) {
614         _satb_mark_queue_set.enqueue_known_active(queue, obj);
615       }
616     }
617   }
618 }
619 
620 template <class T>
621 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count, bool dest_uninit) {
622   if (count == 0) {
623     // No elements to copy, no need for barrier
624     return;
625   }
626 
627   const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
628   if (!dest_uninit && (gc_state & ShenandoahHeap::MARKING) != 0) {
629     // If marking old or young, we must evaluate the SATB barrier. This will be the only
630     // action if we are not marking old. If we are marking old, we must still evaluate the
631     // load reference barrier for a young collection.
632     if (_heap->mode()->is_generational()) {
633       arraycopy_marking<true>(dst, count);
634     } else {
635       arraycopy_marking<false>(dst, count);
636     }
637   }
638 
639   if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
640     assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during evacuation");
641     arraycopy_evacuation(src, count);
642   } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
643     assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during update-refs");
644     arraycopy_update(src, count);
645   }
646 }
647 
648 template <bool IS_GENERATIONAL, class T>
649 void ShenandoahBarrierSet::arraycopy_marking(T* dst, size_t count) {
650   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
651   if (ShenandoahSATBBarrier) {
652     if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(dst)) ||
653         (IS_GENERATIONAL && _heap->heap_region_containing(dst)->is_old() && _heap->is_concurrent_young_mark_in_progress())) {
654       arraycopy_work<T, false, false, true>(dst, count);
655     }
656   }
657 }
658 
659 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
660   return ary < _heap->heap_region_containing(ary)->get_update_watermark();
661 }
662 
663 template <class T>
664 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
665   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
666   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
667     arraycopy_work<T, true, true, false>(src, count);
668   }
669 }
670 
671 template <class T>
672 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
673   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
674   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
675     arraycopy_work<T, true, false, false>(src, count);
676   }
677 }
678 
679 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP