• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #include "ecmascript/mem/barriers_get-inl.h"
17 #include "ecmascript/mem/work_manager-inl.h"
18 #include "common_components/heap/allocator/region_desc.h"
19 #include "common_components/mutator/mutator.h"
20 #include "ecmascript/runtime.h"
21 
22 namespace panda::ecmascript {
Update(const JSThread * thread,uintptr_t slotAddr,Region * objectRegion,TaggedObject * value,Region * valueRegion,WriteBarrierType writeType)23 void Barriers::Update(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion, TaggedObject *value,
24                       Region *valueRegion, WriteBarrierType writeType)
25 {
26     ASSERT(!g_isEnableCMCGC);
27     if (valueRegion->InSharedHeap()) {
28         return;
29     }
30     auto heap = thread->GetEcmaVM()->GetHeap();
31     if (heap->IsConcurrentFullMark()) {
32         if (valueRegion->InCollectSet() && !objectRegion->InYoungSpaceOrCSet()) {
33             objectRegion->AtomicInsertCrossRegionRSet(slotAddr);
34         }
35     } else {
36         if (!valueRegion->InYoungSpace()) {
37             return;
38         }
39     }
40 
41     // Weak ref record and concurrent mark record maybe conflict.
42     // This conflict is solved by keeping alive weak reference. A small amount of floating garbage may be added.
43     TaggedObject *heapValue = JSTaggedValue(value).GetHeapObject();
44     if (valueRegion->IsFreshRegion()) {
45         valueRegion->NonAtomicMark(heapValue);
46     } else if (writeType != WriteBarrierType::DESERIALIZE && valueRegion->AtomicMark(heapValue)) {
47         heap->GetWorkManager()->GetWorkNodeHolder(MAIN_THREAD_INDEX)->Push(heapValue);
48     }
49 }
50 
UpdateShared(const JSThread * thread,uintptr_t slotAddr,Region * objectRegion,TaggedObject * value,Region * valueRegion)51 void Barriers::UpdateShared(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion, TaggedObject *value,
52                             Region *valueRegion)
53 {
54     ASSERT(!g_isEnableCMCGC);
55 
56     ASSERT(DaemonThread::GetInstance()->IsConcurrentMarkingOrFinished());
57     ASSERT(valueRegion->InSharedSweepableSpace());
58     if (valueRegion->InSCollectSet() && objectRegion->InSharedHeap()) {
59         objectRegion->AtomicInsertCrossRegionRSet(slotAddr);
60     }
61     // Weak ref record and concurrent mark record maybe conflict.
62     // This conflict is solved by keeping alive weak reference. A small amount of floating garbage may be added.
63     TaggedObject *heapValue = JSTaggedValue(value).GetHeapObject();
64     if (valueRegion->AtomicMark(heapValue)) {
65         Heap *heap = const_cast<Heap*>(thread->GetEcmaVM()->GetHeap());
66         WorkNode *&localBuffer = heap->GetMarkingObjectLocalBuffer();
67         SharedHeap::GetInstance()->GetWorkManager()->PushToLocalMarkingBuffer(localBuffer, heapValue);
68     }
69 }
70 
71 template <Region::RegionSpaceKind kind>
BatchBitSet(const JSThread * thread,Region * objectRegion,JSTaggedValue * dst,size_t count)72 ARK_NOINLINE bool BatchBitSet([[maybe_unused]] const JSThread* thread, Region* objectRegion, JSTaggedValue* dst,
73                               size_t count)
74 {
75     bool allValueNotHeap = true;
76     Region::Updater updater = objectRegion->GetBatchRSetUpdater<kind>(ToUintPtr(dst));
77     for (size_t i = 0; i < count; i++, updater.Next()) {
78         JSTaggedValue taggedValue = dst[i];
79         if (!taggedValue.IsHeapObject()) {
80             continue;
81         }
82         allValueNotHeap = false;
83         const Region* valueRegion = Region::ObjectAddressToRange(taggedValue.GetTaggedObject());
84 #if ECMASCRIPT_ENABLE_BARRIER_CHECK
85         ASSERT(taggedValue.GetRawData() != JSTaggedValue::VALUE_UNDEFINED);
86         if (!thread->GetEcmaVM()->GetHeap()->IsAlive(taggedValue.GetHeapObject())) {
87             LOG_FULL(FATAL) << "WriteBarrier checked value:" << taggedValue.GetRawData() << " is invalid!";
88         }
89 #endif
90         if (valueRegion->InSharedSweepableSpace()) {
91 #ifndef NDEBUG
92             if (UNLIKELY(taggedValue.IsWeakForHeapObject())) {
93                 CHECK_NO_LOCAL_TO_SHARE_WEAK_REF_HANDLE;
94             }
95 #endif
96             updater.UpdateLocalToShare();
97             continue;
98         }
99         if constexpr (kind == Region::InGeneralOld) {
100             if (valueRegion->InYoungSpace()) {
101                 updater.UpdateOldToNew();
102                 continue;
103             }
104         }
105     }
106     return allValueNotHeap;
107 }
108 
CMCWriteBarrier(const JSThread * thread,void * obj,size_t offset,JSTaggedType value)109 void Barriers::CMCWriteBarrier(const JSThread *thread, void *obj, size_t offset, JSTaggedType value)
110 {
111     ASSERT(g_isEnableCMCGC);
112     common::BaseRuntime::WriteBarrier(obj, (void *)((uintptr_t)obj + offset), (void*)value);
113     return;
114 }
115 
116 #ifdef ARK_USE_SATB_BARRIER
CMCArrayCopyWriteBarrier(const JSThread * thread,const TaggedObject * dstObj,void * src,void * dst,size_t count)117 void Barriers::CMCArrayCopyWriteBarrier(const JSThread *thread, const TaggedObject *dstObj, void* src, void* dst,
118                                         size_t count)
119 {
120     // need opt
121     ASSERT(g_isEnableCMCGC);
122     JSTaggedType *dstPtr = reinterpret_cast<JSTaggedType *>(dst);
123     JSTaggedType *srcPtr = reinterpret_cast<JSTaggedType *>(src);
124     for (size_t i = 0; i < count; i++) {
125         JSTaggedType offset = i * sizeof(JSTaggedType);
126         JSTaggedType value = *reinterpret_cast<JSTaggedType *>(static_cast<JSTaggedType>(srcPtr) + offset);
127         void* obj = reinterpret_cast<void*>(const_cast<JSTaggedType *>(dstObj));
128         void* field = reinterpret_cast<void*>(static_cast<JSTaggedType>(dst) + offset);
129         common::BaseRuntime::WriteBarrier(obj, field, (void*)value);
130     }
131     return;
132 }
133 #else
ShouldProcessSATB(common::GCPhase gcPhase)134 bool Barriers::ShouldProcessSATB(common::GCPhase gcPhase)
135 {
136     switch (gcPhase) {
137         case common::GCPhase::GC_PHASE_ENUM:
138         case common::GCPhase::GC_PHASE_MARK:
139         case common::GCPhase::GC_PHASE_FINAL_MARK:
140         case common::GCPhase::GC_PHASE_REMARK_SATB:
141             return true;
142         default:
143             return false;
144     }
145 }
146 
ShouldGetGCReason(common::GCPhase gcPhase)147 bool Barriers::ShouldGetGCReason(common::GCPhase gcPhase)
148 {
149     switch (gcPhase) {
150         case common::GCPhase::GC_PHASE_ENUM:
151         case common::GCPhase::GC_PHASE_MARK:
152         case common::GCPhase::GC_PHASE_POST_MARK:
153             return true;
154         default:
155             return false;
156     }
157 }
158 
ShouldUpdateRememberSet(common::GCPhase gcPhase)159 bool Barriers::ShouldUpdateRememberSet(common::GCPhase gcPhase)
160 {
161     if (common::Heap::GetHeap().GetGCReason() == common::GC_REASON_YOUNG || !ShouldGetGCReason(gcPhase)) {
162         return true;
163     }
164     return false;
165 }
166 
CMCArrayCopyWriteBarrier(const JSThread * thread,const TaggedObject * dstObj,void * src,void * dst,size_t count)167 void Barriers::CMCArrayCopyWriteBarrier(const JSThread *thread, const TaggedObject *dstObj, void* src, void* dst,
168                                         size_t count)
169 {
170     ASSERT(g_isEnableCMCGC);
171     ASSERT(dstObj != nullptr);
172 
173     common::BaseObject* object = reinterpret_cast<BaseObject*>(const_cast<TaggedObject*>(dstObj));
174     common::RegionDesc::InlinedRegionMetaData *objMetaRegion =
175         common::RegionDesc::InlinedRegionMetaData::GetInlinedRegionMetaData(reinterpret_cast<uintptr_t>(object));
176     JSTaggedType *srcPtr = reinterpret_cast<JSTaggedType *>(src);
177     common::GCPhase gcPhase = thread->GetCMCGCPhase();
178     // 1. update Rememberset
179     if (ShouldUpdateRememberSet(gcPhase)) {
180         auto checkReference = [&](BaseObject *ref) {
181             common::RegionDesc::InlinedRegionMetaData *refMetaRegion =
182                 common::RegionDesc::InlinedRegionMetaData::GetInlinedRegionMetaData(reinterpret_cast<uintptr_t>(ref));
183             return (!objMetaRegion->IsInYoungSpaceForWB() && refMetaRegion->IsInYoungSpaceForWB());
184         };
185 
186         for (size_t i = 0; i < count; i++) {
187             JSTaggedType ref = *reinterpret_cast<JSTaggedType *>(ToUintPtr(srcPtr) + i * sizeof(JSTaggedType));
188             if (!common::Heap::IsTaggedObject(reinterpret_cast<common::HeapAddress>(ref))) {
189                 continue;
190             }
191             ASSERT(common::Heap::IsHeapAddress(ref));
192             if (checkReference(reinterpret_cast<BaseObject *>(ref))) {
193                 objMetaRegion->MarkRSetCardTable(object);
194                 break;
195             }
196         }
197     }
198 
199     // 2. SATB buffer proccess
200     if (ShouldProcessSATB(gcPhase)) {
201         common::Mutator* mutator = common::Mutator::GetMutator();
202         for (size_t i = 0; i < count; i++) {
203             JSTaggedType ref = *reinterpret_cast<JSTaggedType *>(ToUintPtr(srcPtr) + i * sizeof(JSTaggedType));
204             if (!common::Heap::IsTaggedObject(reinterpret_cast<common::HeapAddress>(ref))) {
205                 continue;
206             }
207             ref = ref & ~(common::Barrier::TAG_WEAK);
208             mutator->RememberObjectInSatbBuffer(reinterpret_cast<BaseObject *>(ref));
209         }
210     }
211 }
212 #endif
213 
CMCArrayCopyReadBarrierForward(const JSThread * thread,JSTaggedValue * dst,const JSTaggedValue * src,size_t count)214 void Barriers::CMCArrayCopyReadBarrierForward(const JSThread *thread, JSTaggedValue* dst, const JSTaggedValue* src,
215                                               size_t count)
216 {
217     for (size_t i = 0; i < count; i++) {
218         JSTaggedType valueToRef = Barriers::GetTaggedValue(thread, src, i * sizeof(JSTaggedType));
219         Barriers::SetObject<false>(thread, dst, i * sizeof(JSTaggedType), valueToRef);
220     }
221 }
222 
CMCArrayCopyReadBarrierBackward(const JSThread * thread,JSTaggedValue * dst,const JSTaggedValue * src,size_t count)223 void Barriers::CMCArrayCopyReadBarrierBackward(const JSThread *thread, JSTaggedValue* dst, const JSTaggedValue* src,
224                                                size_t count)
225 {
226     for (size_t i = count; i > 0; i--) {
227         JSTaggedType valueToRef = Barriers::GetTaggedValue(thread, src, (i - 1) * sizeof(JSTaggedType));
228         Barriers::SetObject<false>(thread, dst, (i - 1) * sizeof(JSTaggedType), valueToRef);
229     }
230 }
231 
232 template bool BatchBitSet<Region::InYoung>(const JSThread*, Region*, JSTaggedValue*, size_t);
233 template bool BatchBitSet<Region::InGeneralOld>(const JSThread*, Region*, JSTaggedValue*, size_t);
234 template bool BatchBitSet<Region::Other>(const JSThread*, Region*, JSTaggedValue*, size_t);
235 
236 }  // namespace panda::ecmascript
237