• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "common_components/heap/ark_collector/preforward_barrier.h"
16 
17 #include "common_components/heap/allocator/region_space.h"
18 #include "common_components/base/sys_call.h"
19 #include "common_components/common/scoped_object_lock.h"
20 #include "common_components/mutator/mutator.h"
21 #include "heap/collector/collector_proxy.h"
22 #if defined(COMMON_TSAN_SUPPORT)
23 #include "common_components/sanitizer/sanitizer_interface.h"
24 #endif
25 
26 namespace common {
ReadRefField(BaseObject * obj,RefField<false> & field) const27 BaseObject* PreforwardBarrier::ReadRefField(BaseObject* obj, RefField<false>& field) const
28 {
29     do {
30         RefField<> tmpField(field);
31         BaseObject* oldRef = reinterpret_cast<BaseObject *>(tmpField.GetAddress());
32         if (LIKELY_CC(!static_cast<CollectorProxy *>(&theCollector)->IsFromObject(oldRef))) {
33             return oldRef;
34         }
35 
36         auto weakMask = reinterpret_cast<MAddress>(oldRef) & TAG_WEAK;
37         oldRef = reinterpret_cast<BaseObject *>(reinterpret_cast<MAddress>(oldRef) & (~TAG_WEAK));
38         BaseObject* toObj = nullptr;
39         if (static_cast<CollectorProxy *>(&theCollector)->TryForwardRefField(obj, field, toObj)) {
40             return (BaseObject*)((uintptr_t)toObj | weakMask);
41         }
42     } while (true);
43     // unreachable path.
44     return nullptr;
45 }
46 
ReadStaticRef(RefField<false> & field) const47 BaseObject* PreforwardBarrier::ReadStaticRef(RefField<false>& field) const { return ReadRefField(nullptr, field); }
48 
49 // If the object is still alive, return its toSpace object; if not, return nullptr
ReadStringTableStaticRef(RefField<false> & field) const50 BaseObject* PreforwardBarrier::ReadStringTableStaticRef(RefField<false>& field) const
51 {
52     // Note: CMC GC assumes all objects in string table are not in young space. Based on the assumption, CMC GC skip
53     // read barrier in young GC
54     if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) {
55         return reinterpret_cast<BaseObject*>(field.GetFieldValue());
56     }
57 
58     auto isSurvivor = [](BaseObject* obj) {
59         auto gcReason = Heap::GetHeap().GetGCReason();
60         RegionDesc *regionInfo = RegionDesc::GetAliveRegionDescAt(reinterpret_cast<HeapAddress>(obj));
61         return (regionInfo->IsNewObjectSinceMarking(obj) ||
62             regionInfo->IsToRegion() || regionInfo->IsMarkedObject(obj));
63     };
64 
65     RefField<> tmpField(field);
66     BaseObject* obj = tmpField.GetTargetObject();
67     if (obj != nullptr && isSurvivor(obj)) {
68         return ReadRefField(nullptr, field);
69     } else {
70         return nullptr;
71     }
72 }
73 
74 
ReadStruct(HeapAddress dst,BaseObject * obj,HeapAddress src,size_t size) const75 void PreforwardBarrier::ReadStruct(HeapAddress dst, BaseObject* obj, HeapAddress src, size_t size) const
76 {
77     if (obj != nullptr) {
78         // note fix/untag dst would be better.
79         obj->ForEachRefInStruct(
80             [this, obj](RefField<false>& field) {
81                 RefField<> oldField(field);
82                 BaseObject* target = ReadRefField(obj, field);
83                 (void)target;
84             },
85             src, src + size);
86     }
87 
88     LOGF_CHECK(memcpy_s(reinterpret_cast<void*>(dst), size, reinterpret_cast<void*>(src), size) == EOK) <<
89         "read struct memcpy_s failed";
90 }
91 
AtomicReadRefField(BaseObject * obj,RefField<true> & field,MemoryOrder order) const92 BaseObject* PreforwardBarrier::AtomicReadRefField(BaseObject* obj, RefField<true>& field, MemoryOrder order) const
93 {
94     RefField<false> tmpField(field.GetFieldValue(order));
95     BaseObject* target = ReadRefField(nullptr, tmpField);
96     DLOG(PBARRIER, "atomic read obj %p ref@%p: %#zx -> %p", obj, &field, tmpField.GetFieldValue(), target);
97     return target;
98 }
99 
AtomicWriteRefField(BaseObject * obj,RefField<true> & field,BaseObject * newRef,MemoryOrder order) const100 void PreforwardBarrier::AtomicWriteRefField(BaseObject* obj, RefField<true>& field, BaseObject* newRef,
101                                             MemoryOrder order) const
102 {
103     RefField<> newField(newRef);
104     field.SetFieldValue(newField.GetFieldValue(), order);
105     if (obj != nullptr) {
106         DLOG(PBARRIER, "atomic write obj %p<%p>(%zu) ref@%p: %#zx", obj, obj->GetTypeInfo(), obj->GetSize(), &field,
107              newField.GetFieldValue());
108     } else {
109         DLOG(PBARRIER, "atomic write static ref@%p: %#zx", &field, newField.GetFieldValue());
110     }
111 }
112 
AtomicSwapRefField(BaseObject * obj,RefField<true> & field,BaseObject * newRef,MemoryOrder order) const113 BaseObject* PreforwardBarrier::AtomicSwapRefField(BaseObject* obj, RefField<true>& field, BaseObject* newRef,
114                                                   MemoryOrder order) const
115 {
116     HeapAddress oldValue = field.Exchange(newRef, order);
117     RefField<> oldField(oldValue);
118     BaseObject* oldRef = ReadRefField(nullptr, oldField);
119     DLOG(BARRIER, "atomic swap obj %p<%p>(%zu) ref@%p: old %#zx(%p), new %#zx(%p)", obj, obj->GetTypeInfo(),
120          obj->GetSize(), &field, oldValue, oldRef, field.GetFieldValue(), newRef);
121     return oldRef;
122 }
123 
CompareAndSwapRefField(BaseObject * obj,RefField<true> & field,BaseObject * oldRef,BaseObject * newRef,MemoryOrder succOrder,MemoryOrder failOrder) const124 bool PreforwardBarrier::CompareAndSwapRefField(BaseObject* obj, RefField<true>& field, BaseObject* oldRef,
125                                                BaseObject* newRef, MemoryOrder succOrder, MemoryOrder failOrder) const
126 {
127     HeapAddress oldFieldValue = field.GetFieldValue(std::memory_order_seq_cst);
128     RefField<false> oldField(oldFieldValue);
129     BaseObject* oldVersion = ReadRefField(nullptr, oldField);
130     while (oldVersion == oldRef) {
131         RefField<> newField(newRef);
132         if (field.CompareExchange(oldFieldValue, newField.GetFieldValue(), succOrder, failOrder)) {
133             return true;
134         }
135         oldFieldValue = field.GetFieldValue(std::memory_order_seq_cst);
136         RefField<false> tmp(oldFieldValue);
137         oldVersion = ReadRefField(nullptr, tmp);
138     }
139 
140     return false;
141 }
142 
CopyStructArray(BaseObject * dstObj,HeapAddress dstField,MIndex dstSize,BaseObject * srcObj,HeapAddress srcField,MIndex srcSize) const143 void PreforwardBarrier::CopyStructArray(BaseObject* dstObj, HeapAddress dstField, MIndex dstSize, BaseObject* srcObj,
144                                         HeapAddress srcField, MIndex srcSize) const
145 {
146     LOG_COMMON(FATAL) << "Unresolved fatal";
147     UNREACHABLE_CC();
148 }
149 } // namespace common
150