1 /*
2 * Copyright (c) 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/mem/assert_scope.h"
17
18 namespace panda::ecmascript {
19
20 using AssertGarbageCollectBit = panda::BitField<bool, 0, 1>; // default enable
21 using AssertHeapAllocBit = AssertGarbageCollectBit::NextFlag; // default enable
22 using AssertHandleAllocBit = AssertHeapAllocBit::NextFlag; // default enable
23 using AssertDeRefHandleBit = AssertHandleAllocBit::NextFlag; // default enable
24 using AssertLocalToShareWeakRefBit = AssertDeRefHandleBit::NextFlag; // default disable
25
26 constexpr size_t INITIAL_ASSERT_DATA = AssertGarbageCollectBit::Mask() |
27 AssertHeapAllocBit::Mask() |
28 AssertHandleAllocBit::Mask() |
29 AssertDeRefHandleBit::Mask();
30
31 thread_local size_t currentAssertData = INITIAL_ASSERT_DATA;
32
ForceResetAssertData()33 void ForceResetAssertData()
34 {
35 currentAssertData = INITIAL_ASSERT_DATA;
36 }
37
38 template<AssertType type, bool isAllow, bool IsDebug>
IsAllowed()39 bool AssertScopeT<type, isAllow, IsDebug>::IsAllowed()
40 {
41 return true;
42 };
43
44 template<AssertType type, bool isAllow>
AssertScopeT()45 AssertScopeT<type, isAllow, true>::AssertScopeT() : oldData_(currentAssertData)
46 {
47 switch (type) {
48 case AssertType::GARBAGE_COLLECTION_ASSERT:
49 currentAssertData = AssertGarbageCollectBit::Update(oldData_.value(), isAllow);
50 break;
51 case AssertType::HEAP_ALLOC_ASSERT:
52 currentAssertData = AssertHeapAllocBit::Update(oldData_.value(), isAllow);
53 break;
54 case AssertType::HANDLE_ALLOC_ASSERT:
55 currentAssertData = AssertHandleAllocBit::Update(oldData_.value(), isAllow);
56 break;
57 case AssertType::DEREF_HANDLE_ASSERT:
58 currentAssertData = AssertDeRefHandleBit::Update(oldData_.value(), isAllow);
59 break;
60 case AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT:
61 currentAssertData = AssertLocalToShareWeakRefBit::Update(oldData_.value(), isAllow);
62 break;
63 default:
64 break;
65 }
66 }
67
68 template<AssertType type, bool isAllow>
~AssertScopeT()69 AssertScopeT<type, isAllow, true>::~AssertScopeT()
70 {
71 if (!oldData_.has_value()) {
72 return;
73 }
74 currentAssertData = oldData_.value();
75 oldData_.reset();
76 }
77
78 template<AssertType type, bool isAllow>
IsAllowed()79 bool AssertScopeT<type, isAllow, true>::IsAllowed()
80 {
81 switch (type) {
82 case AssertType::GARBAGE_COLLECTION_ASSERT:
83 return AssertGarbageCollectBit::Decode(currentAssertData);
84 case AssertType::HEAP_ALLOC_ASSERT:
85 return AssertHeapAllocBit::Decode(currentAssertData);
86 case AssertType::HANDLE_ALLOC_ASSERT:
87 return AssertHandleAllocBit::Decode(currentAssertData);
88 case AssertType::DEREF_HANDLE_ASSERT:
89 return AssertDeRefHandleBit::Decode(currentAssertData);
90 case AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT:
91 return AssertLocalToShareWeakRefBit::Decode(currentAssertData);
92 default:
93 return true;
94 }
95 }
96
97 template class PUBLIC_API AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, false, IS_ALLOW_CHECK>;
98 template class PUBLIC_API AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, true, IS_ALLOW_CHECK>;
99 template class PUBLIC_API AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
100 template class PUBLIC_API AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
101 template class PUBLIC_API AssertScopeT<AssertType::HANDLE_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
102 template class PUBLIC_API AssertScopeT<AssertType::HANDLE_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
103 template class PUBLIC_API AssertScopeT<AssertType::DEREF_HANDLE_ASSERT, false, IS_ALLOW_CHECK>;
104 template class PUBLIC_API AssertScopeT<AssertType::DEREF_HANDLE_ASSERT, true, IS_ALLOW_CHECK>;
105 template class PUBLIC_API AssertScopeT<AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT, false, IS_ALLOW_CHECK>;
106 template class PUBLIC_API AssertScopeT<AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT, true, IS_ALLOW_CHECK>;
107
108 } // namespace panda::ecmascript