1 /**
2 * Copyright (c) 2024-2025 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 <gtest/gtest.h>
17
18 #include "ets_coroutine.h"
19
20 #include "ets_vm.h"
21 #include "types/ets_class.h"
22 #include "types/ets_sync_primitives.h"
23 #include "tests/runtime/types/ets_test_mirror_classes.h"
24
25 namespace ark::ets::test {
26
27 class EtsSyncPrimitivesTest : public testing::Test {
28 public:
EtsSyncPrimitivesTest()29 EtsSyncPrimitivesTest()
30 {
31 RuntimeOptions options;
32 options.SetShouldLoadBootPandaFiles(true);
33 options.SetShouldInitializeIntrinsics(false);
34 options.SetCompilerEnableJit(false);
35 options.SetGcType("epsilon");
36 options.SetLoadRuntimes({"ets"});
37
38 auto stdlib = std::getenv("PANDA_STD_LIB");
39 if (stdlib == nullptr) {
40 std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
41 std::abort();
42 }
43 options.SetBootPandaFiles({stdlib});
44
45 Runtime::Create(options);
46 EtsCoroutine *coroutine = EtsCoroutine::GetCurrent();
47 vm_ = coroutine->GetPandaVM();
48 }
49
~EtsSyncPrimitivesTest()50 ~EtsSyncPrimitivesTest() override
51 {
52 Runtime::Destroy();
53 }
54
55 NO_COPY_SEMANTIC(EtsSyncPrimitivesTest);
56 NO_MOVE_SEMANTIC(EtsSyncPrimitivesTest);
57
GetWaitersListMembers()58 static std::vector<MirrorFieldInfo> GetWaitersListMembers()
59 {
60 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsWaitersList, head_, "head"),
61 MIRROR_FIELD_INFO(EtsWaitersList, tail_, "tail")};
62 }
63
GetMutexMembers()64 static std::vector<MirrorFieldInfo> GetMutexMembers()
65 {
66 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsMutex, waitersList_, "waitersList"),
67 MIRROR_FIELD_INFO(EtsMutex, waiters_, "waiters")};
68 }
69
GetEventMembers()70 static std::vector<MirrorFieldInfo> GetEventMembers()
71 {
72 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsEvent, waitersList_, "waitersList"),
73 MIRROR_FIELD_INFO(EtsEvent, state_, "state")};
74 }
75
GetCondVarMembers()76 static std::vector<MirrorFieldInfo> GetCondVarMembers()
77 {
78 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsCondVar, waitersList_, "waitersList"),
79 MIRROR_FIELD_INFO(EtsCondVar, waiters_, "waiters")};
80 }
81
GetQueueSpinlockMembers()82 static std::vector<MirrorFieldInfo> GetQueueSpinlockMembers()
83 {
84 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsQueueSpinlock, tail_, "tail")};
85 }
86
87 protected:
88 PandaEtsVM *vm_ = nullptr; // NOLINT(misc-non-private-member-variables-in-classes)
89 };
90
91 // Check both EtsMutex and ark::Class<Mutex> has the same number of fields
92 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,WaitersListMemoryLayout)93 TEST_F(EtsSyncPrimitivesTest, WaitersListMemoryLayout)
94 {
95 auto *waitersListClass = PlatformTypes(vm_)->coreWaitersList;
96 MirrorFieldInfo::CompareMemberOffsets(waitersListClass, GetWaitersListMembers());
97 }
98
99 // Check both EtsMutex and ark::Class<Mutex> has the same number of fields
100 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,MutexMemoryLayout)101 TEST_F(EtsSyncPrimitivesTest, MutexMemoryLayout)
102 {
103 auto *mutexClass = PlatformTypes(vm_)->coreMutex;
104 MirrorFieldInfo::CompareMemberOffsets(mutexClass, GetMutexMembers());
105 }
106
107 // Check both EtsEvent and ark::Class<Event> has the same number of fields
108 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,EventMemoryLayout)109 TEST_F(EtsSyncPrimitivesTest, EventMemoryLayout)
110 {
111 auto *eventClass = PlatformTypes(vm_)->coreEvent;
112 MirrorFieldInfo::CompareMemberOffsets(eventClass, GetEventMembers());
113 }
114
115 // Check both EtsCondVar and ark::Class<CondVar> has the same number of fields
116 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,CondVarMemoryLayout)117 TEST_F(EtsSyncPrimitivesTest, CondVarMemoryLayout)
118 {
119 auto *condVarClass = PlatformTypes(vm_)->coreCondVar;
120 MirrorFieldInfo::CompareMemberOffsets(condVarClass, GetCondVarMembers());
121 }
122
123 // Check both EtsQueueSpinlock and ark::Class<QueueSpinlock> has the same number of fields
124 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,QueueSpinlockMemoryLayout)125 TEST_F(EtsSyncPrimitivesTest, QueueSpinlockMemoryLayout)
126 {
127 auto *queueSpinlockClass = PlatformTypes(vm_)->coreQueueSpinlock;
128 MirrorFieldInfo::CompareMemberOffsets(queueSpinlockClass, GetQueueSpinlockMembers());
129 }
130
131 } // namespace ark::ets::test
132