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, oop new_value) {
212   assert(ShenandoahCardBarrier, "Should have been checked by caller");
213 
214   if (new_value == nullptr) {
215     // Null reference stores do not require card mark.
216     return;
217   }
218 
219   if (_heap->is_in_young(field)) {
220     // Young field stores do not require card mark.
221     return;
222   }
223 
224   if (!_heap->is_in_young(new_value)) {
225     // Not an old->young reference store.
226     return;
227   }
228 
229   volatile CardTable::CardValue* byte = card_table()->byte_for(field);
230   if (UseCondCardMark && (*byte == CardTable::dirty_card_val())) {
231     return;
232   }
233   *byte = CardTable::dirty_card_val();
234 }
235 
236 template <typename T>
237 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
238   oop value = RawAccess<>::oop_load(addr);
239   value = load_reference_barrier(decorators, value, addr);
240   keep_alive_if_weak(decorators, value);
241   return value;
242 }
243 
244 template <typename T>
245 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
246   shenandoah_assert_not_in_cset_except(nullptr, compare_value, (compare_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
247   shenandoah_assert_not_in_cset_except(nullptr, new_value, (new_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
248 
249   // Handle the previous value through SATB, as we are about to perform the store.
250   oop prev = RawAccess<>::oop_load(addr);
251   satb_enqueue(prev);
252 
253   // Perform LRB on location to fix it up for this and all following accesses.
254   // This guarantees there are no false negatives due to concurrent evacuation,
255   // and the value loaded later by CAS is sanitized by some LRB, or is null.
256   load_reference_barrier(decorators, prev, addr);
257 
258   return RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
259 }
260 
261 template <typename T>
262 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
263   shenandoah_assert_not_in_cset_except(nullptr, new_value, (new_value == nullptr || ShenandoahHeap::heap()->cancelled_gc()));
264 
265   // Handle the previous value through SATB, as we are about to perform the store.
266   oop prev = RawAccess<>::oop_load(addr);
267   satb_enqueue(prev);
268 
269   // Perform LRB on location to fix it up for this and all following accesses.
270   // This is purely opportunistic: we would not have any false negatives here.
271   // This guarantees the value loaded later by XCHG is sanitized by some LRB, or is null.
272   load_reference_barrier(decorators, prev, addr);
273 
274   return RawAccess<>::oop_atomic_xchg(addr, new_value);
275 }
276 
277 template <DecoratorSet decorators, typename BarrierSetT>
278 template <typename T>
279 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
280   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
281   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
282   return bs->oop_load(decorators, addr);
283 }
284 
285 template <DecoratorSet decorators, typename BarrierSetT>
286 template <typename T>
287 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
288   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
289   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
290   return bs->oop_load(decorators, addr);
291 }
292 
293 template <DecoratorSet decorators, typename BarrierSetT>
294 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
295   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
296   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
297   return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
298 }
299 
300 template <DecoratorSet decorators, typename BarrierSetT>
301 template <typename T>
302 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
303   shenandoah_assert_marked_if(nullptr, value,
304                               !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()
305                               && !(ShenandoahHeap::heap()->active_generation()->is_young()
306                                    && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
307   shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
308   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
309   bs->satb_barrier<decorators>(addr);
310   Raw::oop_store(addr, value);
311 }
312 
313 template <DecoratorSet decorators, typename BarrierSetT>
314 template <typename T>
315 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
316   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
317   oop_store_common(addr, value);
318 }
319 
320 template <DecoratorSet decorators, typename BarrierSetT>
321 template <typename T>
322 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
323   shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
324   shenandoah_assert_not_forwarded_except  (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
325 
326   oop_store_common(addr, value);
327   if (ShenandoahCardBarrier) {
328     ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
329     bs->write_ref_field_post<decorators>(addr, value);
330   }
331 }
332 
333 template <DecoratorSet decorators, typename BarrierSetT>
334 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
335   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
336 }
337 
338 template <DecoratorSet decorators, typename BarrierSetT>
339 template <typename T>
340 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
341   assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
342   assert((decorators & ON_STRONG_OOP_REF) != 0, "CAS only for strong refs");
343   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
344   return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
345 }
346 
347 template <DecoratorSet decorators, typename BarrierSetT>
348 template <typename T>
349 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
350   assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
351   assert((decorators & ON_STRONG_OOP_REF) != 0, "CAS only for strong refs");
352   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
353   oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
354   if (ShenandoahCardBarrier) {
355     bs->write_ref_field_post<decorators>(addr, new_value);
356   }
357   return result;
358 }
359 
360 template <DecoratorSet decorators, typename BarrierSetT>
361 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
362   assert((decorators & AS_NO_KEEPALIVE) == 0, "CAS only with keep-alive");
363   assert((decorators & (ON_STRONG_OOP_REF | ON_UNKNOWN_OOP_REF)) != 0, "CAS only for strong refs OR unknown refs (Unsafe)");
364   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
365 
366   // Unsafe.compareAndExchange/Set come here with ON_UNKNOWN_OOP_REF set.
367   // These are normally strong refs, but one can use Unsafe on Reference.referent.
368   // We cannot deal with that case. If application does Unsafe operations on
369   // Reference.referent field, this likely breaks weak reference semantics already.
370   // We upgrade the access to strong in (sometimes futile) attempt to maintain heap
371   // integrity, and assert in debug builds for better diagnostics.
372   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
373   assert((resolved_decorators & ON_STRONG_OOP_REF) != 0, "Application error: CAS on weak location");
374   resolved_decorators = (resolved_decorators & ~ON_DECORATOR_MASK) | ON_STRONG_OOP_REF;
375 
376   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
377   oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
378   if (ShenandoahCardBarrier) {
379     bs->write_ref_field_post<decorators>(addr, new_value);
380   }
381   return result;
382 }
383 
384 template <DecoratorSet decorators, typename BarrierSetT>
385 template <typename T>
386 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
387   assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
388   assert((decorators & ON_STRONG_OOP_REF) != 0, "XCHG only for strong refs");
389   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
390   return bs->oop_xchg(decorators, addr, new_value);
391 }
392 
393 template <DecoratorSet decorators, typename BarrierSetT>
394 template <typename T>
395 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
396   assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
397   assert((decorators & ON_STRONG_OOP_REF) != 0, "XCHG only for strong refs");
398   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
399   oop result = bs->oop_xchg(decorators, addr, new_value);
400   if (ShenandoahCardBarrier) {
401     bs->write_ref_field_post<decorators>(addr, new_value);
402   }
403   return result;
404 }
405 
406 template <DecoratorSet decorators, typename BarrierSetT>
407 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
408   assert((decorators & AS_NO_KEEPALIVE) == 0, "XCHG only with keep-alive");
409   assert((decorators & (ON_STRONG_OOP_REF | ON_UNKNOWN_OOP_REF)) != 0, "XCHG only for strong refs OR unknown refs (Unsafe)");
410   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
411 
412   // Unsafe.getAndSet comes here with ON_UNKNOWN_OOP_REF set.
413   // These are normally strong refs, but one can use Unsafe on Reference.referent.
414   // We cannot deal with that case. If application does Unsafe operations on
415   // Reference.referent field, this likely breaks weak reference semantics already.
416   // We upgrade the access to strong in (sometimes futile) attempt to maintain heap
417   // integrity, and assert in debug builds for better diagnostics.
418   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
419   assert((resolved_decorators & ON_STRONG_OOP_REF) != 0, "Application error: XCHG on weak location");
420   resolved_decorators = (resolved_decorators & ~ON_DECORATOR_MASK) | ON_STRONG_OOP_REF;
421 
422   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
423   oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
424   if (ShenandoahCardBarrier) {
425     bs->write_ref_field_post<decorators>(addr, new_value);
426   }
427   return result;
428 }
429 
430 // Clone barrier support
431 template <bool EVAC>
432 class ShenandoahUpdateEvacForCloneOopClosure : public BasicOopIterateClosure {
433 private:
434   ShenandoahHeap* const _heap;
435   const ShenandoahCollectionSet* const _cset;
436   Thread* const _thread;
437 
438   template <class T>
439   inline void do_oop_work(T* p) {
440     T o = RawAccess<>::oop_load(p);
441     if (!CompressedOops::is_null(o)) {
442       oop obj = CompressedOops::decode_not_null(o);
443       if (_cset->is_in(obj)) {
444         oop fwd = ShenandoahForwarding::get_forwardee(obj);
445         if (EVAC && obj == fwd) {
446           fwd = _heap->evacuate_object(obj, _thread);
447         }
448         shenandoah_assert_forwarded_except(p, obj, _heap->cancelled_gc());
449         ShenandoahHeap::atomic_update_oop(fwd, p, o);
450         obj = fwd;
451       }
452     }
453   }
454 
455 public:
456   ShenandoahUpdateEvacForCloneOopClosure() :
457           _heap(ShenandoahHeap::heap()),
458           _cset(_heap->collection_set()),
459           _thread(Thread::current()) {}
460 
461   virtual void do_oop(oop* p)       { do_oop_work(p); }
462   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
463 };
464 
465 template <bool EVAC>
466 void ShenandoahBarrierSet::clone_work(oop obj) {
467   if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
468     ShenandoahUpdateEvacForCloneOopClosure<EVAC> cl;
469     obj->oop_iterate(&cl);
470   }
471 }
472 
473 template <DecoratorSet decorators, typename BarrierSetT>
474 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
475   // Hot code path, called from compiler/runtime. Make sure fast path is fast.
476 
477   // Fix up src before doing the copy, if needed.
478   const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
479   if (gc_state != 0 && ShenandoahCloneBarrier) {
480     ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
481     if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
482       bs->clone_work<true>(src);
483     } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
484       bs->clone_work<false>(src);
485     }
486   }
487 
488   Raw::clone(src, dst, size);
489 
490   // Current allocator never allocates in old, so clone destination is guaranteed to be in young.
491   // Otherwise we need card barriers.
492   shenandoah_assert_in_young_if(nullptr, dst, ShenandoahCardBarrier);
493 }
494 
495 template <DecoratorSet decorators, typename BarrierSetT>
496 template <typename T>
497 OopCopyResult ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
498                                                                                                   arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
499                                                                                                   size_t length) {
500   T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
501   T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
502 
503   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
504   bs->arraycopy_barrier(src, dst, length);
505   OopCopyResult result = Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
506   if (ShenandoahCardBarrier) {
507     bs->write_ref_array((HeapWord*) dst, length);
508   }
509   return result;
510 }
511 
512 template <class T>
513 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
514   if (count == 0) {
515     // No elements to copy, no need for barrier
516     return;
517   }
518 
519   const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
520   if ((gc_state & ShenandoahHeap::MARKING) != 0) {
521     // If marking old or young, we must evaluate the SATB barrier. This will be the only
522     // action if we are not marking old. If we are marking old, we must still evaluate the
523     // load reference barrier for a young collection.
524     if (_heap->mode()->is_generational()) {
525       arraycopy_marking<true>(dst, count);
526     } else {
527       arraycopy_marking<false>(dst, count);
528     }
529   }
530 
531   if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
532     assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during evacuation");
533     arraycopy_evacuation(src, count);
534   } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
535     assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during update-refs");
536     arraycopy_update(src, count);
537   }
538 }
539 
540 template <bool IS_GENERATIONAL, class T>
541 void ShenandoahBarrierSet::arraycopy_marking(T* dst, size_t count) {
542   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
543   if (!ShenandoahSATBBarrier) {
544     return;
545   }
546 
547   const ShenandoahMarkingContext* ctx = _heap->marking_context();
548   // Everything allocated above TAMS is alive and doesn't need the barrier to keep it that way
549   if (is_above_tams<IS_GENERATIONAL>(ctx, dst)) {
550     return;
551   }
552 
553   assert(!_heap->has_forwarded_objects() || _heap->is_concurrent_old_mark_in_progress(), "Forwarded object status is sane");
554   Thread* thread = Thread::current();
555   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
556   T* end = dst + count;
557   for (T* elem_ptr = dst; elem_ptr < end; ++elem_ptr) {
558     T o = RawAccess<>::oop_load(elem_ptr);
559     if (!CompressedOops::is_null(o)) {
560       oop obj = CompressedOops::decode_not_null(o);
561       if (!ctx->is_marked_strong(obj)) {
562         _satb_mark_queue_set.enqueue_known_active(queue, obj);
563       }
564     }
565   }
566 }
567 
568 template <bool IS_GENERATIONAL, class T>
569 bool ShenandoahBarrierSet::is_above_tams(const ShenandoahMarkingContext* ctx, T* dst) const {
570   // TAMS for an old region is unreliable during a young-only mark, so overwritten pointers in old dst arrays must
571   // be enqueued to preserve old->young referents copied in and overwritten after init mark. See JDK-8373116.
572   return ctx->allocated_after_mark_start(reinterpret_cast<HeapWord*>(dst))
573          && !(IS_GENERATIONAL
574               && _heap->heap_region_containing(dst)->is_old()
575               && _heap->is_concurrent_young_mark_in_progress());
576 }
577 
578 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) const {
579   return ary < _heap->heap_region_containing(ary)->get_update_watermark();
580 }
581 
582 template <class T>
583 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
584   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
585   if (!need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
586     return;
587   }
588 
589   assert(_heap->has_forwarded_objects(), "Forwarded object status is sane");
590   Thread* thread = Thread::current();
591   const ShenandoahCollectionSet* const cset = _heap->collection_set();
592   T* end = src + count;
593   for (T* elem_ptr = src; elem_ptr < end; ++elem_ptr) {
594     T o = RawAccess<>::oop_load(elem_ptr);
595     if (!CompressedOops::is_null(o)) {
596       oop obj = CompressedOops::decode_not_null(o);
597       if (cset->is_in(obj)) {
598         oop fwd = ShenandoahForwarding::get_forwardee(obj);
599         if (obj == fwd) {
600           fwd = _heap->evacuate_object(obj, thread);
601         }
602         shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
603         ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
604       }
605     }
606   }
607 }
608 
609 template <class T>
610 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
611   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
612   if (!need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
613     return;
614   }
615 
616   assert(_heap->has_forwarded_objects(), "Forwarded object status is sane");
617   const ShenandoahCollectionSet* const cset = _heap->collection_set();
618   T* end = src + count;
619   for (T* elem_ptr = src; elem_ptr < end; ++elem_ptr) {
620     T o = RawAccess<>::oop_load(elem_ptr);
621     if (!CompressedOops::is_null(o)) {
622       oop obj = CompressedOops::decode_not_null(o);
623       if (cset->is_in(obj)) {
624         oop fwd = ShenandoahForwarding::get_forwardee(obj);
625         shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
626         ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
627       }
628     }
629   }
630 }
631 
632 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP