• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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 
82 protected:
83     PandaEtsVM *vm_ = nullptr;  // NOLINT(misc-non-private-member-variables-in-classes)
84 };
85 
86 // Check both EtsMutex and ark::Class<Mutex> has the same number of fields
87 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,WaitersListMemoryLayout)88 TEST_F(EtsSyncPrimitivesTest, WaitersListMemoryLayout)
89 {
90     auto *waitersListClass = vm_->GetClassLinker()->GetWaitersListClass();
91     MirrorFieldInfo::CompareMemberOffsets(waitersListClass, GetWaitersListMembers());
92 }
93 
94 // Check both EtsMutex and ark::Class<Mutex> has the same number of fields
95 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,MutexMemoryLayout)96 TEST_F(EtsSyncPrimitivesTest, MutexMemoryLayout)
97 {
98     auto *mutexClass = vm_->GetClassLinker()->GetMutexClass();
99     MirrorFieldInfo::CompareMemberOffsets(mutexClass, GetMutexMembers());
100 }
101 
102 // Check both EtsEvent and ark::Class<Event> has the same number of fields
103 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,EventMemoryLayout)104 TEST_F(EtsSyncPrimitivesTest, EventMemoryLayout)
105 {
106     auto *eventClass = vm_->GetClassLinker()->GetEventClass();
107     MirrorFieldInfo::CompareMemberOffsets(eventClass, GetEventMembers());
108 }
109 
110 // Check both EtsCondVar and ark::Class<CondVar> has the same number of fields
111 // and at the same offsets
TEST_F(EtsSyncPrimitivesTest,CondVarMemoryLayout)112 TEST_F(EtsSyncPrimitivesTest, CondVarMemoryLayout)
113 {
114     auto *condVarClass = vm_->GetClassLinker()->GetCondVarClass();
115     MirrorFieldInfo::CompareMemberOffsets(condVarClass, GetCondVarMembers());
116 }
117 
118 }  // namespace ark::ets::test
119