14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_ASSERT_SCOPE_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_ASSERT_SCOPE_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <optional>
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci#include "ecmascript/common.h"
224514f5e3Sopenharmony_ci#include "libpandabase/utils/bit_field.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::ecmascript {
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci#ifndef NDEBUG
274514f5e3Sopenharmony_ciconstexpr bool IS_ALLOW_CHECK = true;
284514f5e3Sopenharmony_ci#else
294514f5e3Sopenharmony_ciconstexpr bool IS_ALLOW_CHECK = false;
304514f5e3Sopenharmony_ci#endif
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_cienum class AssertType : uint8_t {
334514f5e3Sopenharmony_ci    GARBAGE_COLLECTION_ASSERT = 0,
344514f5e3Sopenharmony_ci    HEAP_ALLOC_ASSERT,
354514f5e3Sopenharmony_ci    HANDLE_ALLOC_ASSERT,
364514f5e3Sopenharmony_ci    DEREF_HANDLE_ASSERT,
374514f5e3Sopenharmony_ci    LOCAL_TO_SHARE_WEAK_REF_ASSERT,
384514f5e3Sopenharmony_ci    LAST_ASSERT_TYPE
394514f5e3Sopenharmony_ci};
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_citemplate<AssertType type, bool isAllow, bool IsDebug = IS_ALLOW_CHECK>
424514f5e3Sopenharmony_ciclass PUBLIC_API AssertScopeT {
434514f5e3Sopenharmony_cipublic:
444514f5e3Sopenharmony_ci    static bool IsAllowed();
454514f5e3Sopenharmony_ci};
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_citemplate<AssertType type, bool isAllow>
484514f5e3Sopenharmony_ciclass PUBLIC_API AssertScopeT<type, isAllow, true> {
494514f5e3Sopenharmony_cipublic:
504514f5e3Sopenharmony_ci    AssertScopeT();
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    ~AssertScopeT();
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci    static bool IsAllowed();
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ciprivate:
574514f5e3Sopenharmony_ci    std::optional<size_t> oldData_;
584514f5e3Sopenharmony_ci};
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ciusing DisallowGarbageCollection = AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, false, IS_ALLOW_CHECK>;
614514f5e3Sopenharmony_ciusing AllowGarbageCollection = AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, true, IS_ALLOW_CHECK>;
624514f5e3Sopenharmony_ciusing DisAllowHeapAlloc = AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
634514f5e3Sopenharmony_ciusing AllowHeapAlloc = AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
644514f5e3Sopenharmony_ciusing DisAllowHandleAllocation = AssertScopeT<AssertType::HANDLE_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
654514f5e3Sopenharmony_ciusing AllowHandleAllocation = AssertScopeT<AssertType::HANDLE_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
664514f5e3Sopenharmony_ciusing DisAllowDeRefHandle = AssertScopeT<AssertType::DEREF_HANDLE_ASSERT, false, IS_ALLOW_CHECK>;
674514f5e3Sopenharmony_ciusing AllowDeRefHandle = AssertScopeT<AssertType::DEREF_HANDLE_ASSERT, true, IS_ALLOW_CHECK>;
684514f5e3Sopenharmony_ciusing DisAllowLocalToShareWeakRefHandle = AssertScopeT<AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT,
694514f5e3Sopenharmony_ci                                                       false, IS_ALLOW_CHECK>;
704514f5e3Sopenharmony_ciusing AllowLocalToShareWeakRefHandle = AssertScopeT<AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT,
714514f5e3Sopenharmony_ci                                                    true, IS_ALLOW_CHECK>;
724514f5e3Sopenharmony_ci#if (!defined NDEBUG) || (defined RUN_TEST)
734514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
744514f5e3Sopenharmony_ci#define DISALLOW_GARBAGE_COLLECTION [[maybe_unused]] DisallowGarbageCollection noGc
754514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
764514f5e3Sopenharmony_ci#define ALLOW_GARBAGE_COLLECTION [[maybe_unused]] AllowGarbageCollection allowGc
774514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
784514f5e3Sopenharmony_ci#define DISALLOW_HEAP_ALLOC [[maybe_unused]] DisAllowHeapAlloc noHeapAlloc
794514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
804514f5e3Sopenharmony_ci#define ALLOW_HEAP_ALLOC [[maybe_unused]] AllowHeapAlloc allowHeapAlloc
814514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
824514f5e3Sopenharmony_ci#define DISALLOW_HANDLE_ALLOC [[maybe_unused]] DisAllowHandleAllocation disAllowHandleAlloc
834514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
844514f5e3Sopenharmony_ci#define ALLOW_HANDLE_ALLOC [[maybe_unused]] AllowHandleAllocation allowHandleAlloc
854514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
864514f5e3Sopenharmony_ci#define DISALLOW_DEREF_HANDLE [[maybe_unused]] DisAllowDeRefHandle disAllowDeRefHandle
874514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
884514f5e3Sopenharmony_ci#define ALLOW_DEREF_HANDLE [[maybe_unused]] AllowDeRefHandle allowDeRefHandle
894514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
904514f5e3Sopenharmony_ci#define DISALLOW_LOCAL_TO_SHARE_WEAK_REF_HANDLE             \
914514f5e3Sopenharmony_ci    [[maybe_unused]] DisAllowLocalToShareWeakRefHandle disAllowLocalToShareWeakRefHandle
924514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
934514f5e3Sopenharmony_ci#define ALLOW_LOCAL_TO_SHARE_WEAK_REF_HANDLE                \
944514f5e3Sopenharmony_ci    [[maybe_unused]] AllowLocalToShareWeakRefHandle allowLocalToShareRefHandle
954514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
964514f5e3Sopenharmony_ci#define DISALLOW_HEAP_ACCESS \
974514f5e3Sopenharmony_ci    DISALLOW_HEAP_ALLOC;     \
984514f5e3Sopenharmony_ci    DISALLOW_HANDLE_ALLOC;   \
994514f5e3Sopenharmony_ci    DISALLOW_DEREF_HANDLE;
1004514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1014514f5e3Sopenharmony_ci#define ALLOW_HEAP_ACCESS \
1024514f5e3Sopenharmony_ci    ALLOW_HEAP_ALLOC;     \
1034514f5e3Sopenharmony_ci    ALLOW_HANDLE_ALLOC;   \
1044514f5e3Sopenharmony_ci    ALLOW_DEREF_HANDLE;
1054514f5e3Sopenharmony_ci#else
1064514f5e3Sopenharmony_ci#define DISALLOW_GARBAGE_COLLECTION
1074514f5e3Sopenharmony_ci#define ALLOW_GARBAGE_COLLECTION
1084514f5e3Sopenharmony_ci#define DISALLOW_HEAP_ALLOC
1094514f5e3Sopenharmony_ci#define ALLOW_HEAP_ALLOC
1104514f5e3Sopenharmony_ci#define DISALLOW_HANDLE_ALLOC
1114514f5e3Sopenharmony_ci#define ALLOW_HANDLE_ALLOC
1124514f5e3Sopenharmony_ci#define DISALLOW_HEAP_ACCESS
1134514f5e3Sopenharmony_ci#define ALLOW_HEAP_ACCESS
1144514f5e3Sopenharmony_ci#define DISALLOW_DEREF_HANDLE
1154514f5e3Sopenharmony_ci#define ALLOW_DEREF_HANDLE
1164514f5e3Sopenharmony_ci#define DISALLOW_LOCAL_TO_SHARE_WEAK_REF_HANDLE
1174514f5e3Sopenharmony_ci#define ALLOW_LOCAL_TO_SHARE_WEAK_REF_HANDLE
1184514f5e3Sopenharmony_ci#endif
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1214514f5e3Sopenharmony_ci#define CHECK_NO_GC ASSERT_PRINT(AllowGarbageCollection::IsAllowed(), "disallow execute garbage collection.")
1224514f5e3Sopenharmony_ci// Some checks failed, need to check and fix
1234514f5e3Sopenharmony_ci#undef CHECK_NO_GC
1244514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1254514f5e3Sopenharmony_ci#define CHECK_NO_GC static_cast<void>(0)
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1284514f5e3Sopenharmony_ci#define CHECK_NO_HEAP_ALLOC ASSERT_PRINT(AllowHeapAlloc::IsAllowed(), "disallow execute heap alloc.")
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1314514f5e3Sopenharmony_ci#define CHECK_NO_HANDLE_ALLOC ASSERT_PRINT(AllowHandleAllocation::IsAllowed(), "disallow execute handle alloc.")
1324514f5e3Sopenharmony_ci
1334514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1344514f5e3Sopenharmony_ci#define CHECK_NO_DEREF_HANDLE ASSERT_PRINT(AllowDeRefHandle::IsAllowed(), "disallow execute deref handle.")
1354514f5e3Sopenharmony_ci
1364514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1374514f5e3Sopenharmony_ci#define CHECK_NO_LOCAL_TO_SHARE_WEAK_REF_HANDLE         \
1384514f5e3Sopenharmony_ci    ASSERT_PRINT(AllowLocalToShareWeakRefHandle::IsAllowed(), "disallow local to share weak ref handle.")
1394514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_ASSERT_SCOPE_H
142