1 /*
2 * Copyright (c) 2021-2024 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 #ifndef ECMASCRIPT_MEM_BARRIERS_INL_H
17 #define ECMASCRIPT_MEM_BARRIERS_INL_H
18
19 #include "ecmascript/base/config.h"
20 #include "ecmascript/daemon/daemon_thread.h"
21 #include "ecmascript/js_tagged_value.h"
22 #include "ecmascript/js_thread.h"
23 #include "ecmascript/mem/assert_scope.h"
24 #include "ecmascript/mem/barriers.h"
25 #include "ecmascript/mem/region-inl.h"
26 #include "ecmascript/mem/heap.h"
27 #include "ecmascript/ecma_vm.h"
28
29 namespace panda::ecmascript {
30 template<WriteBarrierType writeType = WriteBarrierType::NORMAL>
WriteBarrier(const JSThread * thread,void * obj,size_t offset,JSTaggedType value)31 static ARK_INLINE void WriteBarrier(const JSThread *thread, void *obj, size_t offset, JSTaggedType value)
32 {
33 ASSERT(value != JSTaggedValue::VALUE_UNDEFINED);
34 Region *objectRegion = Region::ObjectAddressToRange(static_cast<TaggedObject *>(obj));
35 Region *valueRegion = Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(value));
36 #if ECMASCRIPT_ENABLE_BARRIER_CHECK
37 if (!thread->GetEcmaVM()->GetHeap()->IsAlive(JSTaggedValue(value).GetHeapObject())) {
38 LOG_FULL(FATAL) << "WriteBarrier checked value:" << value << " is invalid!";
39 }
40 #endif
41 uintptr_t slotAddr = ToUintPtr(obj) + offset;
42 if (objectRegion->InGeneralOldSpace() && valueRegion->InGeneralNewSpace()) {
43 // Should align with '8' in 64 and 32 bit platform
44 ASSERT((slotAddr % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0);
45 objectRegion->InsertOldToNewRSet(slotAddr);
46 } else if (!objectRegion->InSharedHeap() && valueRegion->InSharedSweepableSpace()) {
47 #ifndef NDEBUG
48 if (UNLIKELY(JSTaggedValue(value).IsWeakForHeapObject())) {
49 CHECK_NO_LOCAL_TO_SHARE_WEAK_REF_HANDLE;
50 }
51 #endif
52 objectRegion->InsertLocalToShareRSet(slotAddr);
53 } else if (valueRegion->InEdenSpace() && objectRegion->InYoungSpace()) {
54 objectRegion->InsertNewToEdenRSet(slotAddr);
55 }
56 ASSERT(!objectRegion->InSharedHeap() || valueRegion->InSharedHeap());
57 if (!valueRegion->InSharedHeap() && thread->IsConcurrentMarkingOrFinished()) {
58 Barriers::Update(thread, slotAddr, objectRegion, reinterpret_cast<TaggedObject *>(value),
59 valueRegion, writeType);
60 }
61 if (valueRegion->InSharedSweepableSpace() && thread->IsSharedConcurrentMarkingOrFinished()) {
62 if constexpr (writeType != WriteBarrierType::DESERIALIZE) {
63 Barriers::UpdateShared(thread, reinterpret_cast<TaggedObject *>(value), valueRegion);
64 } else {
65 // In deserialize, will never add references from old object(not allocated by deserialing) to
66 // new object(allocated by deserializing), only two kinds of references(new->old, new->new) will
67 // be added, the old object is considered as serialize_root, and be marked and pushed in
68 // SharedGC::MarkRoots, so just mark all the new object is enough, do not need to push them to
69 // workmanager and recursively visit slots of that.
70 ASSERT(DaemonThread::GetInstance()->IsConcurrentMarkingOrFinished());
71 valueRegion->AtomicMark(JSTaggedValue(value).GetHeapObject());
72 }
73 }
74 }
75
76 template<bool needWriteBarrier>
SetObject(const JSThread * thread,void * obj,size_t offset,JSTaggedType value)77 inline void Barriers::SetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value)
78 {
79 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
80 *reinterpret_cast<JSTaggedType *>(reinterpret_cast<uintptr_t>(obj) + offset) = value;
81 if constexpr (needWriteBarrier) {
82 WriteBarrier(thread, obj, offset, value);
83 }
84 }
85
SynchronizedSetClass(const JSThread * thread,void * obj,JSTaggedType value)86 inline void Barriers::SynchronizedSetClass(const JSThread *thread, void *obj, JSTaggedType value)
87 {
88 reinterpret_cast<volatile std::atomic<JSTaggedType> *>(obj)->store(value, std::memory_order_release);
89 WriteBarrier(thread, obj, 0, value);
90 }
91
SynchronizedSetObject(const JSThread * thread,void * obj,size_t offset,JSTaggedType value,bool isPrimitive)92 inline void Barriers::SynchronizedSetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value,
93 bool isPrimitive)
94 {
95 reinterpret_cast<volatile std::atomic<JSTaggedType> *>(ToUintPtr(obj) + offset)->store(value,
96 std::memory_order_release);
97 if (!isPrimitive) {
98 WriteBarrier(thread, obj, offset, value);
99 }
100 }
101 } // namespace panda::ecmascript
102
103 #endif // ECMASCRIPT_MEM_BARRIERS_INL_H
104