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 #include "ecmascript/free_object.h"
17 
18 #include "ecmascript/mem/heap.h"
19 #include "ecmascript/mem/tagged_object-inl.h"
20 #include "ecmascript/global_env_constants-inl.h"
21 
22 namespace panda::ecmascript {
FillFreeObject(BaseHeap *heap, uintptr_t address, size_t size)23 FreeObject *FreeObject::FillFreeObject(BaseHeap *heap, uintptr_t address, size_t size)
24 {
25     ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void *>(address), size);
26     auto globalConst = heap->GetGlobalConst();
27     FreeObject *object = nullptr;
28     if (size >= FreeObject::SIZE_OFFSET && size < FreeObject::SIZE) {
29         object = reinterpret_cast<FreeObject *>(address);
30         object->SetClassWithoutBarrier(
31             JSHClass::Cast(globalConst->GetFreeObjectWithOneFieldClass().GetTaggedObject()));
32         object->SetNext(INVALID_OBJECT);
33     } else if (size >= FreeObject::SIZE) {
34         object = reinterpret_cast<FreeObject *>(address);
35         object->SetClassWithoutBarrier(
36             JSHClass::Cast(globalConst->GetFreeObjectWithTwoFieldClass().GetTaggedObject()));
37         object->SetAvailable(size);
38         object->SetNext(INVALID_OBJECT);
39     } else if (size == FreeObject::NEXT_OFFSET) {
40         object = reinterpret_cast<FreeObject *>(address);
41         object->SetClassWithoutBarrier(
42             JSHClass::Cast(globalConst->GetFreeObjectWithNoneFieldClass().GetTaggedObject()));
43     } else {
44         LOG_ECMA(DEBUG) << "Fill free object size is smaller";
45     }
46 #ifdef ARK_ASAN_ON
47     ASAN_POISON_MEMORY_REGION(reinterpret_cast<void *>(address), size);
48 #endif
49     return object;
50 }
51 
Available() const52 uint32_t FreeObject::Available() const
53 {
54     auto hclass = GetClass();
55     if (hclass->IsFreeObjectWithShortField()) {
56         return hclass->GetObjectSize();
57     }
58     ASSERT(GetSize().IsInt());
59     return GetSize().GetInt();
60 }
61 
IsFreeObject() const62 bool FreeObject::IsFreeObject() const
63 {
64     return GetClass()->IsFreeObject();
65 }
66 
67 // Before operating any freeobject, need to mark unpoison when is_asan is true.
AsanUnPoisonFreeObject() const68 void FreeObject::AsanUnPoisonFreeObject() const
69 {
70 #ifdef ARK_ASAN_ON
71     ASAN_UNPOISON_MEMORY_REGION(this, NEXT_OFFSET);
72     if (GetClass()->IsFreeObjectWithOneField()) {
73         ASAN_UNPOISON_MEMORY_REGION(this, SIZE_OFFSET);
74     } else if (GetClass()->IsFreeObjectWithTwoField()) {
75         ASAN_UNPOISON_MEMORY_REGION(this, SIZE);
76     }
77 #endif
78 }
79 
80 // After operating any freeobject, need to marked poison again when is_asan is true
AsanPoisonFreeObject() const81 void FreeObject::AsanPoisonFreeObject() const
82 {
83 #ifdef ARK_ASAN_ON
84     if (GetClass()->IsFreeObjectWithNoneField()) {
85         ASAN_POISON_MEMORY_REGION(this, NEXT_OFFSET);
86     } else if (GetClass()->IsFreeObjectWithOneField()) {
87         ASAN_POISON_MEMORY_REGION(this, SIZE_OFFSET);
88     } else if (GetClass()->IsFreeObjectWithTwoField()) {
89         ASAN_POISON_MEMORY_REGION(this, SIZE);
90     }
91 #endif
92 }
93 
94 }  // namespace panda::ecmascript
95