• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #include "runtime/include/runtime.h"
18 #include "libpandabase/test_utilities.h"
19 
20 namespace ark::test {
21 
22 class ThreadTest : public testing::Test {
23 public:
24     // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
25     MTManagedThread *thread {};
ThreadTest()26     ThreadTest()
27     {
28         RuntimeOptions options;
29         options.SetShouldLoadBootPandaFiles(false);
30         options.SetShouldInitializeIntrinsics(false);
31         /*
32          * gtest ASSERT_DEATH doesn't work with multiple threads:
33          * "In particular, death tests don't like having multiple threads in the parent process"
34          * turn off gc-thread & compiler-thread here, because we use a lot of ASSERT_DEATH and test can hang.
35          */
36         options.SetCompilerEnableJit(false);
37         options.SetGcType("epsilon");
38         Logger::InitializeStdLogging(Logger::Level::ERROR, 0);
39         Runtime::Create(options);
40         thread = MTManagedThread::GetCurrent();
41     }
42 
~ThreadTest()43     ~ThreadTest() override
44     {
45         Runtime::Destroy();
46     }
47 
48     NO_COPY_SEMANTIC(ThreadTest);
49     NO_MOVE_SEMANTIC(ThreadTest);
50 
AssertNative() const51     void AssertNative() const
52     {
53         ASSERT_TRUE(thread->IsInNativeCode());
54         ASSERT_FALSE(thread->IsManagedCode());
55     }
56 
AssertManaged() const57     void AssertManaged() const
58     {
59         ASSERT_FALSE(thread->IsInNativeCode());
60         ASSERT_TRUE(thread->IsManagedCode());
61     }
62 
BeginToStateAndEnd(MTManagedThread::ThreadState state) const63     void BeginToStateAndEnd(MTManagedThread::ThreadState state) const
64     {
65         if (state == MTManagedThread::ThreadState::NATIVE_CODE) {
66             thread->NativeCodeBegin();
67             AssertNative();
68             thread->NativeCodeEnd();
69         } else if (state == MTManagedThread::ThreadState::MANAGED_CODE) {
70             thread->ManagedCodeBegin();
71             AssertManaged();
72             thread->ManagedCodeEnd();
73         } else {
74             UNREACHABLE();
75         }
76     }
77 };
78 
79 /**
80  * call stack:
81  *
82  * native #0
83  *   managed #1
84  *      native #2
85  *          access #3
86  *   access #4
87  *
88  */
TEST_F(ThreadTest,LegalThreadStatesTest)89 TEST_F(ThreadTest, LegalThreadStatesTest)
90 {
91     AssertNative();
92     thread->ManagedCodeBegin();  // #1
93     AssertManaged();
94     thread->NativeCodeBegin();  // #2
95     AssertNative();
96 
97     thread->NativeCodeEnd();  // #2
98     AssertManaged();
99     thread->ManagedCodeEnd();  // #1
100     AssertNative();
101 }
102 
DEATH_TEST_F(ThreadTest,BeginForbiddenStatesFromNativeFrame)103 DEATH_TEST_F(ThreadTest, BeginForbiddenStatesFromNativeFrame)
104 {
105     testing::FLAGS_gtest_death_test_style = "threadsafe";
106 
107     AssertNative();
108 #ifndef NDEBUG
109     ASSERT_DEATH(thread->NativeCodeBegin(), "last frame is: NATIVE_CODE");
110 #endif
111     AssertNative();
112 }
113 
DEATH_TEST_F(ThreadTest,BeginForbiddenStatesFromManagedFrame)114 DEATH_TEST_F(ThreadTest, BeginForbiddenStatesFromManagedFrame)
115 {
116     testing::FLAGS_gtest_death_test_style = "threadsafe";
117 
118     AssertNative();
119     thread->ManagedCodeBegin();
120     AssertManaged();
121 #ifndef NDEBUG
122     ASSERT_DEATH(thread->ManagedCodeBegin(), "last frame is: MANAGED_CODE");
123 #endif
124     AssertManaged();
125     thread->ManagedCodeEnd();
126     AssertNative();
127 }
128 
DEATH_TEST_F(ThreadTest,EndNativeStateByOtherStates)129 DEATH_TEST_F(ThreadTest, EndNativeStateByOtherStates)
130 {
131     testing::FLAGS_gtest_death_test_style = "threadsafe";
132 
133     AssertNative();
134 
135 #ifndef NDEBUG
136     ASSERT_DEATH(thread->ManagedCodeEnd(), "last frame is: NATIVE_CODE");
137     ASSERT_DEATH(thread->ManagedCodeEnd(), "last frame is: NATIVE_CODE");
138 #endif
139 }
140 
DEATH_TEST_F(ThreadTest,EndManagedStateByOtherStates)141 DEATH_TEST_F(ThreadTest, EndManagedStateByOtherStates)
142 {
143     testing::FLAGS_gtest_death_test_style = "threadsafe";
144 
145     AssertNative();
146     thread->ManagedCodeBegin();
147 
148 #ifndef NDEBUG
149     ASSERT_DEATH(thread->NativeCodeEnd(), "last frame is: MANAGED_CODE");
150 #endif
151     thread->ManagedCodeEnd();
152 }
153 
DEATH_TEST_F(ThreadTest,TestAllConversions)154 DEATH_TEST_F(ThreadTest, TestAllConversions)
155 {
156     testing::FLAGS_gtest_death_test_style = "threadsafe";
157 
158     // from NATIVE_CODE
159     AssertNative();
160 #ifndef NDEBUG
161     ASSERT_DEATH(BeginToStateAndEnd(MTManagedThread::ThreadState::NATIVE_CODE), "last frame is: NATIVE_CODE");
162 #endif
163     BeginToStateAndEnd(MTManagedThread::ThreadState::MANAGED_CODE);
164 
165     // from MANAGED_CODE
166     thread->ManagedCodeBegin();
167     AssertManaged();
168 
169     BeginToStateAndEnd(MTManagedThread::ThreadState::NATIVE_CODE);
170 #ifndef NDEBUG
171     ASSERT_DEATH(BeginToStateAndEnd(MTManagedThread::ThreadState::MANAGED_CODE), "last frame is: MANAGED_CODE");
172 #endif
173     thread->ManagedCodeEnd();
174     AssertNative();
175 }
176 }  // namespace ark::test
177