1 /*
2 * Copyright (c) 2021 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/js_tagged_value.h"
21 #include "ecmascript/mem/barriers.h"
22 #include "ecmascript/mem/region-inl.h"
23 #include "ecmascript/mem/heap.h"
24
25 namespace panda::ecmascript {
WriteBarrier(void * obj,size_t offset,JSTaggedType value)26 static ARK_INLINE void WriteBarrier(void *obj, size_t offset, JSTaggedType value)
27 {
28 ASSERT(value != JSTaggedValue::VALUE_UNDEFINED);
29 Region *objectRegion = Region::ObjectAddressToRange(static_cast<TaggedObject *>(obj));
30 Region *valueRegion = Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(value));
31 #if ECMASCRIPT_ENABLE_BARRIER_CHECK
32 if (!valueRegion->GetJSThread()->GetEcmaVM()->GetHeap()->IsAlive(JSTaggedValue(value).GetHeapObject())) {
33 LOG_FULL(FATAL) << "WriteBarrier checked value:" << value << " is invalid!";
34 }
35 #endif
36 uintptr_t slotAddr = ToUintPtr(obj) + offset;
37 if (!objectRegion->InYoungSpace() && valueRegion->InYoungSpace()) {
38 // Should align with '8' in 64 and 32 bit platform
39 ASSERT((slotAddr % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0);
40 objectRegion->InsertOldToNewRSet(slotAddr);
41 }
42
43 if (valueRegion->IsMarking()) {
44 Barriers::Update(slotAddr, objectRegion, reinterpret_cast<TaggedObject *>(value), valueRegion);
45 }
46 }
47
48 /* static */
49 // CODECHECK-NOLINTNEXTLINE(C_RULE_ID_COMMENT_LOCATION)
50 // default value for need_write_barrier is true
51 template<bool need_write_barrier>
SetObject(const JSThread * thread,void * obj,size_t offset,JSTaggedType value)52 inline void Barriers::SetObject([[maybe_unused]] const JSThread *thread, void *obj, size_t offset, JSTaggedType value)
53 {
54 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
55 *reinterpret_cast<JSTaggedType *>(reinterpret_cast<uintptr_t>(obj) + offset) = value;
56 WriteBarrier(obj, offset, value);
57 }
58
SynchronizedSetClass(void * obj,JSTaggedType value)59 inline void Barriers::SynchronizedSetClass(void *obj, JSTaggedType value)
60 {
61 reinterpret_cast<volatile std::atomic<JSTaggedType> *>(obj)->store(value, std::memory_order_release);
62 WriteBarrier(obj, 0, value);
63 }
64 } // namespace panda::ecmascript
65
66 #endif // ECMASCRIPT_MEM_BARRIERS_INL_H
67