• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ASSERT_SCOPE_H
17 #define ECMASCRIPT_MEM_ASSERT_SCOPE_H
18 
19 #include <optional>
20 
21 #include "libpandabase/macros.h"
22 #include "libpandabase/utils/bit_field.h"
23 
24 namespace panda::ecmascript {
25 static thread_local size_t currentAssertData(~0);
26 
27 using AssertGarbageCollectBit = panda::BitField<bool, 0, 1>;
28 using AssertHeapAllocBit = AssertGarbageCollectBit::NextFlag;
29 
30 #ifndef NDEBUG
31 constexpr bool IS_ALLOW_CHECK = true;
32 #else
33 constexpr bool IS_ALLOW_CHECK = false;
34 #endif
35 
36 enum class AssertType : uint8_t { GARBAGE_COLLECTION_ASSERT = 0, HEAP_ALLOC_ASSERT, LAST_ASSERT_TYPE };
37 
38 template<AssertType type, bool isAllow, bool IsDebug = IS_ALLOW_CHECK>
39 class AssertScopeT {
40 public:
IsAllowed()41     static bool IsAllowed()
42     {
43         return true;
44     }
45 };
46 
47 template<AssertType type, bool isAllow>
48 class AssertScopeT<type, isAllow, true> {
49 public:
AssertScopeT()50     AssertScopeT() : oldData_(currentAssertData)
51     {
52         switch (type) {
53             case AssertType::GARBAGE_COLLECTION_ASSERT:
54                 currentAssertData = AssertGarbageCollectBit::Update(oldData_.value(), isAllow);
55                 break;
56             case AssertType::HEAP_ALLOC_ASSERT:
57                 currentAssertData = AssertHeapAllocBit::Update(oldData_.value(), isAllow);
58                 break;
59             default:
60                 break;
61         }
62     }
63 
~AssertScopeT()64     ~AssertScopeT()
65     {
66         if (!oldData_.has_value()) {
67             return;
68         }
69 
70         currentAssertData = oldData_.value();
71         oldData_.reset();
72     }
73 
IsAllowed()74     static bool IsAllowed()
75     {
76         switch (type) {
77             case AssertType::GARBAGE_COLLECTION_ASSERT:
78                 return AssertGarbageCollectBit::Decode(currentAssertData);
79             case AssertType::HEAP_ALLOC_ASSERT:
80                 return AssertHeapAllocBit::Decode(currentAssertData);
81             default:
82                 return true;
83         }
84     }
85 
86     NO_COPY_SEMANTIC(AssertScopeT);
87     DEFAULT_NOEXCEPT_MOVE_SEMANTIC(AssertScopeT);
88 
89 private:
90     std::optional<size_t> oldData_;
91 };
92 
93 using DisallowGarbageCollection = AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, false, IS_ALLOW_CHECK>;
94 using AllowGarbageCollection = AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, true, IS_ALLOW_CHECK>;
95 using DisAllowHeapAlloc = AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
96 using AllowHeapAlloc = AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
97 
98 #if (!defined NDEBUG) || (defined RUN_TEST)
99 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
100 #define DISALLOW_GARBAGE_COLLECTION [[maybe_unused]] DisallowGarbageCollection noGc
101 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
102 #define ALLOW_GARBAGE_COLLECTION [[maybe_unused]] AllowGarbageCollection allowGc
103 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
104 #define DISALLOW_HEAP_ALLOC [[maybe_unused]] DisAllowHeapAlloc noHeapAlloc
105 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
106 #define ALLOW_HEAP_ALLOC [[maybe_unused]] AllowHeapAlloc allowHeapAlloc
107 #else
108 #define DISALLOW_GARBAGE_COLLECTION
109 #define ALLOW_GARBAGE_COLLECTION
110 #define DISALLOW_HEAP_ALLOC
111 #define ALLOW_HEAP_ALLOC
112 #endif
113 
114 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
115 #define CHECK_NO_GC ASSERT_PRINT(AllowGarbageCollection::IsAllowed(), "disallow execute garbage collection.");
116 
117 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
118 #define CHECK_NO_HEAP_ALLOC (AllowHeapAlloc::IsAllowed(), "disallow execute heap alloc.");
119 }  // namespace panda::ecmascript
120 
121 #endif  // ECMASCRIPT_MEM_ASSERT_SCOPE_H
122