1 /*
  2  * Copyright (c) 2015, 2021, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 29 
 30 #include "gc/shared/accessBarrierSupport.inline.hpp"
 31 #include "gc/shared/cardTable.hpp"
 32 #include "gc/shenandoah/shenandoahAsserts.hpp"
 33 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 34 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
 35 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
 36 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 37 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
 38 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 39 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 40 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 41 #include "memory/iterator.inline.hpp"
 42 #include "oops/oop.inline.hpp"
 43 
 44 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
 45   return ShenandoahForwarding::get_forwardee(p);
 46 }
 47 
 48 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
 49   if (p != NULL) {
 50     return resolve_forwarded_not_null(p);
 51   } else {
 52     return p;
 53   }
 54 }
 55 
 56 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) {
 57   return ShenandoahForwarding::get_forwardee_mutator(p);
 58 }
 59 
 60 template <class T>
 61 inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {
 62   assert(ShenandoahLoadRefBarrier, "should be enabled");
 63   shenandoah_assert_in_cset(load_addr, obj);
 64 
 65   oop fwd = resolve_forwarded_not_null_mutator(obj);
 66   if (obj == fwd) {
 67     assert(_heap->is_evacuation_in_progress(),
 68            "evac should be in progress");
 69     Thread* const t = Thread::current();
 70     ShenandoahEvacOOMScope scope(t);
 71     fwd = _heap->evacuate_object(obj, t);
 72   }
 73 
 74   if (load_addr != NULL && fwd != obj) {
 75     // Since we are here and we know the load address, update the reference.
 76     ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
 77   }
 78 
 79   return fwd;
 80 }
 81 
 82 inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
 83   if (!ShenandoahLoadRefBarrier) {
 84     return obj;
 85   }
 86   if (_heap->has_forwarded_objects() &&
 87       _heap->in_collection_set(obj)) { // Subsumes NULL-check
 88     assert(obj != NULL, "cset check must have subsumed NULL-check");
 89     oop fwd = resolve_forwarded_not_null(obj);
 90     // TODO: It should not be necessary to check evac-in-progress here.
 91     // We do it for mark-compact, which may have forwarded objects,
 92     // and objects in cset and gets here via runtime barriers.
 93     // We can probably fix this as soon as mark-compact has its own
 94     // marking phase.
 95     if (obj == fwd && _heap->is_evacuation_in_progress()) {
 96        Thread* t = Thread::current();
 97       ShenandoahEvacOOMScope oom_evac_scope(t);
 98       return _heap->evacuate_object(obj, t);
 99     }
100     return fwd;
101   }
102   return obj;
103 }
104 
105 template <class T>
106 inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {
107   if (obj == NULL) {
108     return NULL;
109   }
110 
111   // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
112   if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&
113       _heap->is_concurrent_weak_root_in_progress() &&
114       _heap->is_in_active_generation(obj) &&
115       !_heap->marking_context()->is_marked(obj)) {
116     return NULL;
117   }
118 
119   // Prevent resurrection of unreachable weak references.
120   if ((decorators & ON_WEAK_OOP_REF) != 0 &&
121       _heap->is_concurrent_weak_root_in_progress() &&
122       _heap->is_in_active_generation(obj) &&
123       !_heap->marking_context()->is_marked_strong(obj)) {
124     return NULL;
125   }
126 
127   // Prevent resurrection of unreachable objects that are visited during
128   // concurrent class-unloading.
129   if ((decorators & AS_NO_KEEPALIVE) != 0 &&
130       _heap->is_evacuation_in_progress() &&
131       !_heap->marking_context()->is_marked(obj)) {
132     return obj;
133   }
134 
135   oop fwd = load_reference_barrier(obj);
136   if (ShenandoahSelfFixing && load_addr != NULL && fwd != obj) {
137     // Since we are here and we know the load address, update the reference.
138     ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
139   }
140 
141   return fwd;
142 }
143 
144 inline void ShenandoahBarrierSet::enqueue(oop obj) {
145   assert(obj != NULL, "checked by caller");
146   assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
147 
148   // Filter marked objects before hitting the SATB queues. The same predicate would
149   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
150   // filtering here helps to avoid wasteful SATB queueing work to begin with.
151   if (!_heap->requires_marking(obj)) return;
152 
153   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());
154   _satb_mark_queue_set.enqueue_known_active(queue, obj);
155 }
156 
157 template <DecoratorSet decorators, typename T>
158 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
159   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
160       HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
161     return;
162   }
163   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
164     T heap_oop = RawAccess<>::oop_load(field);
165     if (!CompressedOops::is_null(heap_oop)) {
166       enqueue(CompressedOops::decode(heap_oop));
167     }
168   }
169 }
170 
171 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
172   if (value != NULL && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
173     enqueue(value);
174   }
175 }
176 
177 inline void ShenandoahBarrierSet::iu_barrier(oop obj) {
178   if (ShenandoahIUBarrier && obj != NULL && _heap->is_concurrent_mark_in_progress()) {
179     enqueue(obj);
180   }
181 }
182 
183 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
184   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
185   const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
186   const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
187   if (!peek && !on_strong_oop_ref) {
188     satb_enqueue(value);
189   }
190 }
191 
192 template <DecoratorSet decorators, typename T>
193 inline void ShenandoahBarrierSet::write_ref_field_post(T* field, oop newVal) {
194   if (ShenandoahHeap::heap()->mode()->is_generational()) {
195     volatile CardTable::CardValue* byte = card_table()->byte_for(field);
196     *byte = CardTable::dirty_card_val();
197   }
198 }
199 
200 template <typename T>
201 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
202   oop value = RawAccess<>::oop_load(addr);
203   value = load_reference_barrier(decorators, value, addr);
204   keep_alive_if_weak(decorators, value);
205   return value;
206 }
207 
208 template <typename T>
209 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
210   iu_barrier(new_value);
211   oop res;
212   oop expected = compare_value;
213   do {
214     compare_value = expected;
215     res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
216     expected = res;
217   } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
218 
219   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
220   // because it must be the previous value.
221   res = load_reference_barrier(decorators, res, reinterpret_cast<T*>(NULL));
222   satb_enqueue(res);
223   return res;
224 }
225 
226 template <typename T>
227 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
228   iu_barrier(new_value);
229   oop previous = RawAccess<>::oop_atomic_xchg(addr, new_value);
230   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
231   // because it must be the previous value.
232   previous = load_reference_barrier<T>(decorators, previous, reinterpret_cast<T*>(NULL));
233   satb_enqueue(previous);
234   return previous;
235 }
236 
237 template <DecoratorSet decorators, typename BarrierSetT>
238 template <typename T>
239 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
240   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
241   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
242   return bs->oop_load(decorators, addr);
243 }
244 
245 template <DecoratorSet decorators, typename BarrierSetT>
246 template <typename T>
247 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
248   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
249   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
250   return bs->oop_load(decorators, addr);
251 }
252 
253 template <DecoratorSet decorators, typename BarrierSetT>
254 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
255   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
256   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
257   return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
258 }
259 
260 template <DecoratorSet decorators, typename BarrierSetT>
261 template <typename T>
262 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
263   shenandoah_assert_marked_if(NULL, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress() &&
264                               !(ShenandoahHeap::heap()->is_gc_generation_young() && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
265   shenandoah_assert_not_in_cset_if(addr, value, value != NULL && !ShenandoahHeap::heap()->cancelled_gc());
266   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
267   bs->iu_barrier(value);
268   bs->satb_barrier<decorators>(addr);
269   Raw::oop_store(addr, value);
270 }
271 
272 template <DecoratorSet decorators, typename BarrierSetT>
273 template <typename T>
274 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
275   shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
276   shenandoah_assert_not_forwarded_except  (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
277   shenandoah_assert_not_in_cset_except    (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
278 
279   oop_store_not_in_heap(addr, value);
280   ShenandoahBarrierSet::barrier_set()->write_ref_field_post<decorators>(addr, value);
281 }
282 
283 template <DecoratorSet decorators, typename BarrierSetT>
284 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
285   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
286 }
287 
288 template <DecoratorSet decorators, typename BarrierSetT>
289 template <typename T>
290 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
291   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
292   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
293   return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
294 }
295 
296 template <DecoratorSet decorators, typename BarrierSetT>
297 template <typename T>
298 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
299   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
300   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
301   oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
302   bs->write_ref_field_post<decorators>(addr, new_value);
303   return result;
304 }
305 
306 template <DecoratorSet decorators, typename BarrierSetT>
307 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
308   assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
309   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
310   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
311   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
312   oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
313   bs->write_ref_field_post<decorators>(addr, new_value);
314   return result;
315 }
316 
317 template <DecoratorSet decorators, typename BarrierSetT>
318 template <typename T>
319 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
320   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
321   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
322   return bs->oop_xchg(decorators, addr, new_value);
323 }
324 
325 template <DecoratorSet decorators, typename BarrierSetT>
326 template <typename T>
327 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
328   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
329   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
330   oop result = bs->oop_xchg(decorators, addr, new_value);
331   bs->write_ref_field_post<decorators>(addr, new_value);
332   return result;
333 }
334 
335 template <DecoratorSet decorators, typename BarrierSetT>
336 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
337   assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
338   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
339   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
340   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
341   oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
342   bs->write_ref_field_post<decorators>(addr, new_value);
343   return result;
344 }
345 
346 // Clone barrier support
347 template <DecoratorSet decorators, typename BarrierSetT>
348 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
349   if (ShenandoahCloneBarrier) {
350     ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
351   }
352   Raw::clone(src, dst, size);
353 }
354 
355 template <DecoratorSet decorators, typename BarrierSetT>
356 template <typename T>
357 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
358                                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
359                                                                                          size_t length) {
360   T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
361   T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
362 
363   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
364   bs->arraycopy_barrier(src, dst, length);
365   bool result = Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
366   bs->write_ref_array((HeapWord*) dst, length);
367   return result;
368 }
369 
370 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
371 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
372   // We allow forwarding in young generation and marking in old generation
373   // to happen simultaneously.
374   assert(_heap->mode()->is_generational() || HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane");
375 
376   Thread* thread = Thread::current();
377   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
378   ShenandoahMarkingContext* ctx = _heap->marking_context();
379   const ShenandoahCollectionSet* const cset = _heap->collection_set();
380   T* end = src + count;
381   for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
382     T o = RawAccess<>::oop_load(elem_ptr);
383     if (!CompressedOops::is_null(o)) {
384       oop obj = CompressedOops::decode_not_null(o);
385       if (HAS_FWD && cset->is_in(obj)) {
386         oop fwd = resolve_forwarded_not_null(obj);
387         if (EVAC && obj == fwd) {
388           fwd = _heap->evacuate_object(obj, thread);
389         }
390         assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
391         ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
392         obj = fwd;
393       }
394       if (ENQUEUE && !ctx->is_marked_strong_or_old(obj)) {
395         _satb_mark_queue_set.enqueue_known_active(queue, obj);
396       }
397     }
398   }
399 }
400 
401 template <class T>
402 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
403   if (count == 0) {
404     return;
405   }
406   int gc_state = _heap->gc_state();
407   if ((gc_state & ShenandoahHeap::YOUNG_MARKING) != 0) {
408     arraycopy_marking(src, dst, count);
409     return;
410   }
411 
412   if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
413     arraycopy_evacuation(src, count);
414   } else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {
415     arraycopy_update(src, count);
416   }
417 
418   if (_heap->mode()->is_generational()) {
419     assert(ShenandoahSATBBarrier, "Generational mode assumes SATB mode");
420     // TODO: Could we optimize here by checking that dst is in an old region?
421     if ((gc_state & ShenandoahHeap::OLD_MARKING) != 0) {
422       // Note that we can't do the arraycopy marking using the 'src' array when
423       // SATB mode is enabled (so we can't do this as part of the iteration for
424       // evacuation or update references).
425       arraycopy_marking(src, dst, count);
426     }
427   }
428 }
429 
430 template <class T>
431 void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count) {
432   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
433   T* array = ShenandoahSATBBarrier ? dst : src;
434   if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(array))) {
435     arraycopy_work<T, false, false, true>(array, count);
436   }
437 }
438 
439 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
440   return ary < _heap->heap_region_containing(ary)->get_update_watermark();
441 }
442 
443 template <class T>
444 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
445   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
446   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
447     ShenandoahEvacOOMScope oom_evac;
448     arraycopy_work<T, true, true, false>(src, count);
449   }
450 }
451 
452 template <class T>
453 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
454   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
455   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
456     arraycopy_work<T, true, false, false>(src, count);
457   }
458 }
459 
460 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP