• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
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 "hook_record_factory_test.h"
17 #include "stack_builder_test.h"
18 #include "native_hook_config.pb.h"
19 #include <string>
20 #include <sys/time.h>
21 #include <vector>
22 
23 using namespace testing::ext;
24 using namespace std;
25 
26 namespace OHOS::Developtools::NativeDaemon {
27 static std::mutex g_mtx;
28 class StackBuilderTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase(void)36 void StackBuilderTest::SetUpTestCase(void) {}
TearDownTestCase(void)37 void StackBuilderTest::TearDownTestCase(void) {}
SetUp()38 void StackBuilderTest::SetUp() {}
TearDown()39 void StackBuilderTest::TearDown() {}
40 
41 /*
42  * @tc.name: FpStackBuilderFillIpTest
43  * @tc.desc: test FpStackBuilder::FillIps normal case.
44  * @tc.type: FUNC
45  */
46 #ifdef __aarch64__
47 HWTEST_F(StackBuilderTest, FpStackBuilderFillIpTest, TestSize.Level1)
48 {
49     NativeHookConfig hookConfig;
50     hookConfig.set_offline_symbolization(true);
51     std::shared_ptr<OHOS::Developtools::NativeDaemon::VirtualRuntime> runtime =
52     std::make_shared<OHOS::Developtools::NativeDaemon::VirtualRuntime>(hookConfig);
53     auto builder = std::make_shared<FpStackBuilder>(&hookConfig, runtime, g_mtx);
54     std::vector<CallFrame> frames;
55     RawStackPtr rawData = std::make_shared<RawStack>();
56     rawData->fpDepth = 1;
57     uint8_t ip = 16;
58     rawData->data = &ip;
59     std::shared_ptr<HookRecord> hookRecord = std::make_shared<HookRecord>(rawData);
60     builder->FillIps(frames, hookRecord);
61     EXPECT_EQ(frames.size(), 1);
62 }
63 
64 /*
65  * @tc.name: FpStackBuilderFillJsSymbolsTest
66  * @tc.desc: test FpStackBuilder::FillJsSymbols normal case.
67  * @tc.type: FUNC
68  */
69 HWTEST_F(StackBuilderTest, FpStackBuilderFillJsSymbolsTest, TestSize.Level1)
70 {
71     NativeHookConfig hookConfig;
72     hookConfig.set_offline_symbolization(true);
73     std::shared_ptr<OHOS::Developtools::NativeDaemon::VirtualRuntime> runtime =
74     std::make_shared<OHOS::Developtools::NativeDaemon::VirtualRuntime>(hookConfig);
75     auto builder = std::make_shared<FpStackBuilder>(&hookConfig, runtime, g_mtx);
76     std::vector<CallFrame> frames;
77     RawStackPtr rawData = std::make_shared<RawStack>();
78     rawData->fpDepth = 1;
79     uint8_t ip = 16;
80     rawData->data = &ip;
81     std::shared_ptr<HookRecord> hookRecord = std::make_shared<HookRecord>(rawData);
82     builder->FillIps(frames, hookRecord);
83     rawData->jsStackData = "funcA|";
84     BaseStackRawData baseData;
85     baseData.jsChainId = 1;
86     rawData->stackContext = &baseData;
87     builder->FillJsSymbols(frames, hookRecord);
88     EXPECT_EQ(frames.size(), 2);
89 }
90 
91 /*
92  * @tc.name: FpStackBuilderFillWrongJsSymbolsTest
93  * @tc.desc: test FpStackBuilder::FillJsSymbols error case.
94  * @tc.type: FUNC
95  */
96 HWTEST_F(StackBuilderTest, FpStackBuilderFillWrongJsSymbolsTest, TestSize.Level1)
97 {
98     NativeHookConfig hookConfig;
99     hookConfig.set_offline_symbolization(true);
100     std::shared_ptr<OHOS::Developtools::NativeDaemon::VirtualRuntime> runtime =
101     std::make_shared<OHOS::Developtools::NativeDaemon::VirtualRuntime>(hookConfig);
102     auto builder = std::make_shared<FpStackBuilder>(&hookConfig, runtime, g_mtx);
103     std::vector<CallFrame> frames;
104     RawStackPtr rawData = std::make_shared<RawStack>();
105     rawData->fpDepth = 1;
106     uint8_t ip = 16;
107     rawData->data = &ip;
108     std::shared_ptr<HookRecord> hookRecord = std::make_shared<HookRecord>(rawData);
109     builder->FillIps(frames, hookRecord);
110     rawData->jsStackData = "funcA";
111     BaseStackRawData baseData;
112     baseData.jsChainId = 1;
113     rawData->stackContext = &baseData;
114     builder->FillJsSymbols(frames, hookRecord);
115     EXPECT_EQ(frames.size(), 1);
116 }
117 #endif
118 
119 /*
120  * @tc.name: DwarfStackBuilderReplaceErrStackTest
121  * @tc.desc: test DwarfStackBuilder::ReplaceErrStack
122  * @tc.type: FUNC
123  */
124 HWTEST_F(StackBuilderTest, DwarfStackBuilderReplaceErrStackTest, TestSize.Level1)
125 {
126     NativeHookConfig hookConfig;
127     hookConfig.set_offline_symbolization(true);
128     std::shared_ptr<OHOS::Developtools::NativeDaemon::VirtualRuntime> runtime =
129     std::make_shared<OHOS::Developtools::NativeDaemon::VirtualRuntime>(hookConfig);
130     auto builder = std::make_shared<DwarfStackBuilder>(&hookConfig, runtime, g_mtx);
131     std::vector<CallFrame> frames;
132     RawStackPtr rawData = std::make_shared<RawStack>();
133     rawData->fpDepth = 1;
134     uint8_t ip = 16;
135     rawData->data = &ip;
136     std::shared_ptr<HookRecord> hookRecord = std::make_shared<HookRecord>(rawData);
137     ASSERT_FALSE(builder->IsRecordInfoValid(hookRecord));
138     builder->ReplaceErrStack(frames);
139     EXPECT_EQ(frames.size(), 1);
140 }
141 
142 /*
143  * @tc.name: DwarfStackBuilderReplaceErrStackTest
144  * @tc.desc: test BuildStackDirector::ConstructCallFrames normal case.
145  * @tc.type: FUNC
146  */
147 #ifdef __aarch64__
148 HWTEST_F(StackBuilderTest, DirectorTest, TestSize.Level1)
149 {
150     NativeHookConfig hookConfig;
151     hookConfig.set_offline_symbolization(true);
152     hookConfig.set_js_stack_report(true);
153     std::shared_ptr<OHOS::Developtools::NativeDaemon::VirtualRuntime> runtime =
154     std::make_shared<OHOS::Developtools::NativeDaemon::VirtualRuntime>(hookConfig);
155     auto builder = std::make_shared<FpStackBuilder>(&hookConfig, runtime, g_mtx);
156     auto director = std::make_shared<BuildStackDirector>(&hookConfig, runtime, g_mtx);
157     director->SetBuilder(builder);
158     RawStackPtr rawData = std::make_shared<RawStack>();
159     rawData->fpDepth = 1;
160     uint8_t ip = 16;
161     rawData->data = &ip;
162     rawData->jsStackData = "funcA";
163     BaseStackRawData baseData;
164     baseData.jsChainId = 1;
165     rawData->stackContext = &baseData;
166     std::shared_ptr<HookRecord> hookRecord = std::make_shared<HookRecord>(rawData);
167     ASSERT_TRUE(director->IsRecordUnwindable(hookRecord));
168     auto& frames = director->ConstructCallFrames(hookRecord);
169     EXPECT_EQ(frames.size(), 1);
170 }
171 #endif
172 
173 /*
174  * @tc.name: DirectorTestFalse
175  * @tc.desc: test BuildStackDirector::IsRecordUnwindable false case.
176  * @tc.type: FUNC
177  */
178 HWTEST_F(StackBuilderTest, DirectorTestFalse, TestSize.Level1)
179 {
180     NativeHookConfig hookConfig;
181     hookConfig.set_offline_symbolization(true);
182     hookConfig.set_js_stack_report(true);
183     std::shared_ptr<OHOS::Developtools::NativeDaemon::VirtualRuntime> runtime =
184     std::make_shared<OHOS::Developtools::NativeDaemon::VirtualRuntime>(hookConfig);
185     auto director = std::make_shared<BuildStackDirector>(&hookConfig, runtime, g_mtx);
186     RawStackPtr rawData = std::make_shared<RawStack>();
187     BaseStackRawData baseData;
188     baseData.type = PR_SET_VMA_MSG;
189     rawData->stackContext = &baseData;
190     std::shared_ptr<HookRecord> hookRecord = std::make_shared<HookRecord>(rawData);
191     ASSERT_FALSE(director->IsRecordUnwindable(hookRecord));
192 }
193 }