1 /*
2 * Copyright (c) 2022-2023 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 <array>
17 #include <iostream>
18
19 #include <gtest/gtest.h>
20 #include "defect_scan_aux_api.h"
21
22 namespace panda::defect_scan_aux::test {
23
24 class DefectScanAuxTest : public testing::Test {
25 public:
SetUpTestCase(void)26 static void SetUpTestCase(void) {};
TearDownTestCase(void)27 static void TearDownTestCase(void) {};
SetUp()28 void SetUp() {};
TearDown()29 void TearDown() {};
30 };
31
CheckFunction(std::unique_ptr<const AbcFile> & abc_file,std::string_view func_name)32 static const Function *CheckFunction(std::unique_ptr<const AbcFile> &abc_file, std::string_view func_name)
33 {
34 if (abc_file == nullptr) {
35 EXPECT_NE(abc_file, nullptr);
36 return nullptr;
37 }
38 auto func0 = abc_file->GetFunctionByName(func_name);
39 if (func0 == nullptr) {
40 EXPECT_NE(func0, nullptr);
41 return nullptr;
42 }
43 EXPECT_EQ(func0->GetFunctionName(), func_name);
44 return func0;
45 }
46
ContainDefinedFunction(std::unique_ptr<const AbcFile> & abc_file,const Function * par_func,std::string_view func_name)47 static bool ContainDefinedFunction(std::unique_ptr<const AbcFile> &abc_file,
48 const Function *par_func, std::string_view func_name)
49 {
50 if (abc_file == nullptr) {
51 EXPECT_NE(abc_file, nullptr);
52 return false;
53 }
54 size_t df_cnt0 = par_func->GetDefinedFunctionCount();
55 for (size_t i = 0; i < df_cnt0; ++i) {
56 auto df = par_func->GetDefinedFunctionByIndex(i);
57 if (df->GetFunctionName() == func_name) {
58 EXPECT_EQ(df->GetParentFunction(), par_func);
59 return true;
60 }
61 }
62 return false;
63 }
64
ContainMemberFunction(std::unique_ptr<const AbcFile> & abc_file,const Class * class0,std::string_view func_name)65 static bool ContainMemberFunction(std::unique_ptr<const AbcFile> &abc_file,
66 const Class *class0, std::string_view func_name)
67 {
68 if (abc_file == nullptr) {
69 EXPECT_NE(abc_file, nullptr);
70 return false;
71 }
72 size_t mf_func_count = class0->GetMemberFunctionCount();
73 for (size_t i = 0; i < mf_func_count; ++i) {
74 auto mf = class0->GetMemberFunctionByIndex(i);
75 if (mf->GetFunctionName() == func_name) {
76 EXPECT_EQ(class0->GetMemberFunctionByName(func_name), mf);
77 return true;
78 }
79 }
80 auto par_class = class0->GetParentClass();
81 if (par_class != nullptr) {
82 return ContainMemberFunction(abc_file, par_class, func_name);
83 }
84 return false;
85 }
86
CheckClass(std::unique_ptr<const AbcFile> & abc_file,std::string_view class_name)87 static const Class *CheckClass(std::unique_ptr<const AbcFile> &abc_file, std::string_view class_name)
88 {
89 if (abc_file == nullptr) {
90 EXPECT_NE(abc_file, nullptr);
91 return nullptr;
92 }
93 auto *class0 = abc_file->GetClassByName(class_name);
94 if (class0 == nullptr) {
95 EXPECT_NE(class0, nullptr);
96 return nullptr;
97 }
98 EXPECT_EQ(class0->GetClassName(), class_name);
99 [[maybe_unused]] size_t mf_func_count = class0->GetMemberFunctionCount();
100 ASSERT(mf_func_count >= 1);
101 [[maybe_unused]] auto mf_func0 = class0->GetMemberFunctionByIndex(0);
102 if (mf_func0 == nullptr) {
103 EXPECT_NE(mf_func0, nullptr);
104 return nullptr;
105 }
106 EXPECT_EQ(abc_file->GetFunctionByName(class_name), mf_func0);
107 EXPECT_EQ(class0->GetMemberFunctionByName(class_name), mf_func0);
108 EXPECT_EQ(mf_func0->GetClass(), class0);
109 CheckFunction(abc_file, class_name);
110 return class0;
111 }
112
CheckAbcFile(std::unique_ptr<const AbcFile> & abc_file,size_t test_def_func_cnt,size_t test_def_class_cnt)113 void CheckAbcFile(std::unique_ptr<const AbcFile> &abc_file, size_t test_def_func_cnt, size_t test_def_class_cnt)
114 {
115 ASSERT_NE(abc_file, nullptr);
116 size_t def_func_cnt = abc_file->GetDefinedFunctionCount();
117 EXPECT_EQ(def_func_cnt, test_def_func_cnt);
118 size_t def_class_cnt = abc_file->GetDefinedClassCount();
119 EXPECT_EQ(def_class_cnt, test_def_class_cnt);
120 }
121
122 HWTEST(DefectScanAuxTest, DefineInfoFuncTest, testing::ext::TestSize.Level0)
123 {
124 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "define_info_test.abc";
125 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
126 CheckAbcFile(abc_file, 30U, 10U);
127
128 // check each defined func
129 // func_main_0
130 auto f0 = CheckFunction(abc_file, "func_main_0");
131 ASSERT_TRUE(f0->GetClass() == nullptr);
132 ASSERT_TRUE(f0->GetParentFunction() == nullptr);
133 size_t dc_cnt0 = f0->GetDefinedClassCount();
134 EXPECT_EQ(dc_cnt0, 2U);
135 EXPECT_EQ(f0->GetDefinedClassByIndex(0)->GetClassName(), "#~@1=#Bar");
136 EXPECT_EQ(f0->GetDefinedClassByIndex(1)->GetClassName(), "#~@4=#ExampleClass1");
137 size_t df_cnt0 = f0->GetDefinedFunctionCount();
138 EXPECT_EQ(df_cnt0, 12U);
139 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#*#func1"));
140 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#*#func2"));
141 ASSERT_FALSE(ContainDefinedFunction(abc_file, f0, "func1"));
142 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#~@1>#func6"));
143 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#~@1>#getName"));
144 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#~@1>#setName"));
145 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#~@1>#func9"));
146 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#*#func10"));
147 ASSERT_TRUE(ContainDefinedFunction(abc_file, f0, "#~@4>#func17"));
148 // func2
149 auto f1 = CheckFunction(abc_file, "#*#func2");
150 ASSERT_NE(f1, nullptr);
151 EXPECT_EQ(f1->GetArgCount(), 5U);
152 size_t df_cnt1 = f1->GetDefinedFunctionCount();
153 EXPECT_EQ(df_cnt1, 2U);
154 ASSERT_TRUE(ContainDefinedFunction(abc_file, f1, "#*@0*#func4"));
155 // func10
156 auto f2 = CheckFunction(abc_file, "#*#func10");
157 EXPECT_EQ(f2->GetArgCount(), 3U);
158 size_t dc_cnt2 = f2->GetDefinedClassCount();
159 EXPECT_EQ(dc_cnt2, 2U);
160 EXPECT_EQ(f2->GetDefinedClassByIndex(0)->GetClassName(), "#*@2~@1=#Bar");
161 EXPECT_EQ(f2->GetDefinedClassByIndex(1)->GetClassName(), "#*@2~@3=#Bar2");
162 size_t df_cnt2 = f2->GetDefinedFunctionCount();
163 EXPECT_EQ(df_cnt2, 7U);
164 EXPECT_TRUE(ContainDefinedFunction(abc_file, f2, "#*@2~@1>#baseFoo1"));
165 EXPECT_TRUE(ContainDefinedFunction(abc_file, f2, "#*@2~@3>#func12"));
166 EXPECT_TRUE(ContainDefinedFunction(abc_file, f2, "#*@2~@3>#a"));
167 EXPECT_TRUE(ContainDefinedFunction(abc_file, f2, "#*@2~@3>#symbol"));
168 EXPECT_TRUE(ContainDefinedFunction(abc_file, f2, "#*@2~@3>#func15"));
169 }
170
171 HWTEST(DefectScanAuxTest, DefineInfoClassTest, testing::ext::TestSize.Level0)
172 {
173 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "define_info_test.abc";
174 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
175 CheckAbcFile(abc_file, 30U, 10U);
176
177 // check each defined class
178 // #1#Bar
179 auto f0 = CheckFunction(abc_file, "func_main_0");
180 auto class0 = CheckClass(abc_file, "#~@1=#Bar");
181 ASSERT_TRUE(class0->GetParentClass() == nullptr);
182 ASSERT_TRUE(class0->GetDefiningFunction() == f0);
183 size_t mf_count0 = class0->GetMemberFunctionCount();
184 EXPECT_EQ(mf_count0, 5U);
185 EXPECT_TRUE(ContainMemberFunction(abc_file, class0, "#~@1>#func6"));
186 EXPECT_TRUE(ContainMemberFunction(abc_file, class0, "#~@1>#getName"));
187 EXPECT_TRUE(ContainMemberFunction(abc_file, class0, "#~@1>#setName"));
188 EXPECT_TRUE(ContainMemberFunction(abc_file, class0, "#~@1>#func9"));
189 // #3#Bar2
190 auto class1 = CheckClass(abc_file, "#*@2~@3=#Bar2");
191 ASSERT_TRUE(class1->GetParentClass() != nullptr);
192 ASSERT_TRUE(class1->GetDefiningFunction() == abc_file->GetFunctionByName("#*#func10"));
193 size_t mf_count1 = class1->GetMemberFunctionCount();
194 EXPECT_EQ(mf_count1, 5U);
195 EXPECT_TRUE(ContainMemberFunction(abc_file, class1, "#*@2~@1>#baseFoo1"));
196 EXPECT_TRUE(ContainMemberFunction(abc_file, class1, "#*@2~@3>#func12"));
197 EXPECT_TRUE(ContainMemberFunction(abc_file, class1, "#*@2~@3>#func15"));
198 // #8#ExampleClass2
199 auto class2 = CheckClass(abc_file, "#*@5*@7~@6=#ExampleClass2");
200 EXPECT_EQ(class2->GetParentClassName(), "#*@5~@4=#ExampleClass1");
201 EXPECT_FALSE(ContainMemberFunction(abc_file, class2, "#~@4=#func17"));
202 EXPECT_TRUE(ContainMemberFunction(abc_file, class2, "#*@5~@4>#func19"));
203 // #9#ExtendService
204 auto class3 = CheckClass(abc_file, "#*@9~@8=#ExtendService");
205 ASSERT_TRUE(class3->GetParentClass() == nullptr);
206 EXPECT_EQ(class3->GetParentClassName(), "BaseService");
207 EXPECT_EQ(class3->GetParClassExternalModuleName(), "../base/service");
208 EXPECT_TRUE(class3->GetParClassGlobalVarName().empty());
209 // #10#ExtendPhoneService
210 auto class4 = CheckClass(abc_file, "#*@9~@a=#ExtendPhoneService");
211 ASSERT_TRUE(class4->GetParentClass() == nullptr);
212 EXPECT_EQ(class4->GetParentClassName(), "PhoneService");
213 EXPECT_EQ(class4->GetParClassExternalModuleName(), "../mod1");
214 EXPECT_TRUE(class4->GetParClassGlobalVarName().empty());
215 // #11#ExtendDataSource
216 auto class5 = CheckClass(abc_file, "#*@9~@b=#ExtendDataSource");
217 ASSERT_TRUE(class5->GetParentClass() == nullptr);
218 EXPECT_EQ(class5->GetParentClassName(), "BasicDataSource");
219 EXPECT_TRUE(class5->GetParClassExternalModuleName().empty());
220 EXPECT_EQ(class5->GetParClassGlobalVarName(), "globalvar");
221 // #12#ExtendDataItem
222 auto class6 = CheckClass(abc_file, "#*@9~@c=#ExtendDataItem");
223 ASSERT_TRUE(class6->GetParentClass() == nullptr);
224 EXPECT_EQ(class6->GetParentClassName(), "DataItem");
225 EXPECT_TRUE(class6->GetParClassExternalModuleName().empty());
226 EXPECT_EQ(class6->GetParClassGlobalVarName(), "globalvar2.Data");
227 }
228
229 HWTEST(DefectScanAuxTest, DebugInfoTest, testing::ext::TestSize.Level0)
230 {
231 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "debug_info_test.abc";
232 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
233 ASSERT_NE(abc_file, nullptr);
234
235 // check debug info, whether the line number obtained from call inst is correct
236 auto f0 = abc_file->GetFunctionByName("#*#foo");
237 ASSERT_NE(f0, nullptr);
238 EXPECT_EQ(f0->GetCalleeInfoCount(), 3U);
239 auto ci0_0 = f0->GetCalleeInfoByIndex(0);
240 ASSERT_NE(ci0_0, nullptr);
241 // callarg0
242 EXPECT_EQ(abc_file->GetLineNumberByInst(f0, ci0_0->GetCallInst()), 34);
243 auto ci0_1 = f0->GetCalleeInfoByIndex(1);
244 ASSERT_NE(ci0_1, nullptr);
245 // callspread
246 EXPECT_EQ(abc_file->GetLineNumberByInst(f0, ci0_1->GetCallInst()), 38);
247 auto ci0_2 = f0->GetCalleeInfoByIndex(2);
248 ASSERT_NE(ci0_2, nullptr);
249 // callirange
250 EXPECT_EQ(abc_file->GetLineNumberByInst(f0, ci0_2->GetCallInst()), 40);
251 // ctor of Data
252 auto f1 = abc_file->GetFunctionByName("#*@2~@1=#Data");
253 ASSERT_NE(f1, nullptr);
254 EXPECT_EQ(f1->GetCalleeInfoCount(), 1U);
255 auto ci1_0 = f1->GetCalleeInfoByIndex(0);
256 // supercall
257 EXPECT_EQ(abc_file->GetLineNumberByInst(f1, ci1_0->GetCallInst()), 60);
258 // bar
259 auto f2 = abc_file->GetFunctionByName("#*#bar");
260 ASSERT_NE(f2, nullptr);
261 EXPECT_EQ(f2->GetCalleeInfoCount(), 2U);
262 auto ci2_0 = f2->GetCalleeInfoByIndex(0);
263 ASSERT_NE(ci2_0, nullptr);
264 // callithisrange
265 EXPECT_EQ(abc_file->GetLineNumberByInst(f2, ci2_0->GetCallInst()), 70);
266 auto ci2_1 = f2->GetCalleeInfoByIndex(1);
267 ASSERT_NE(ci2_1, nullptr);
268 // callithisrange
269 EXPECT_EQ(abc_file->GetLineNumberByInst(f2, ci2_1->GetCallInst()), 75);
270 }
271
272 HWTEST(DefectScanAuxTest, CalleeInfoTest1, testing::ext::TestSize.Level0)
273 {
274 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "callee_info_test.abc";
275 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
276 CheckAbcFile(abc_file, 19U, 2U);
277
278 // check callee info of each func
279 // foo
280 auto f0 = abc_file->GetFunctionByName("#*#foo");
281 ASSERT_NE(abc_file, nullptr);
282 size_t ci_cnt0 = f0->GetCalleeInfoCount();
283 EXPECT_EQ(ci_cnt0, 6U);
284 auto ci0_0 = f0->GetCalleeInfoByIndex(0);
285 ASSERT_NE(abc_file, nullptr);
286 ASSERT_TRUE(f0->GetCalleeInfoByCallInst(ci0_0->GetCallInst()) == ci0_0);
287 ASSERT_TRUE(ci0_0->IsCalleeDefinite());
288 EXPECT_EQ(ci0_0->GetCalleeArgCount(), 1);
289 ASSERT_TRUE(ci0_0->GetCaller() == f0);
290 ASSERT_TRUE(ci0_0->GetCallee() == abc_file->GetFunctionByName("#*@0*#func2"));
291 auto ci0_1 = f0->GetCalleeInfoByIndex(1);
292 ASSERT_NE(ci0_1, nullptr);
293 EXPECT_EQ(f0->GetCalleeInfoByCallInst(ci0_1->GetCallInst()), ci0_1);
294 ASSERT_FALSE(ci0_1->IsCalleeDefinite());
295 EXPECT_EQ(ci0_1->GetCalleeArgCount(), 1);
296 ASSERT_TRUE(ci0_1->GetCallee() == nullptr);
297 EXPECT_EQ(ci0_1->GetFunctionName(), "log");
298 EXPECT_EQ(ci0_1->GetGlobalVarName(), "console");
299 auto ci0_2 = f0->GetCalleeInfoByIndex(2);
300 ASSERT_NE(ci0_2, nullptr);
301 ASSERT_FALSE(ci0_2->IsCalleeDefinite());
302 EXPECT_EQ(ci0_2->GetCalleeArgCount(), 1);
303 ASSERT_TRUE(ci0_2->GetCallee() == nullptr);
304 EXPECT_EQ(ci0_2->GetFunctionName(), "logd");
305 EXPECT_EQ(ci0_2->GetGlobalVarName(), "globalvar.hilog");
306 auto ci0_3 = f0->GetCalleeInfoByIndex(3);
307 ASSERT_NE(ci0_3, nullptr);
308 ASSERT_TRUE(ci0_3->IsCalleeDefinite());
309 ASSERT_TRUE(ci0_3->GetCallee() == abc_file->GetFunctionByName("#*@0*#func2"));
310 auto ci0_4 = f0->GetCalleeInfoByIndex(4);
311 ASSERT_NE(ci0_4, nullptr);
312 ASSERT_TRUE(ci0_4->IsCalleeDefinite());
313 EXPECT_EQ(ci0_4->GetCalleeArgCount(), 2);
314 ASSERT_TRUE(ci0_4->GetCallee() == abc_file->GetFunctionByName("#*#func1"));
315 auto ci0_5 = f0->GetCalleeInfoByIndex(5);
316 ASSERT_NE(ci0_5, nullptr);
317 ASSERT_FALSE(ci0_5->IsCalleeDefinite());
318 EXPECT_EQ(ci0_5->GetFunctionName(), "bar");
319 }
320
321 HWTEST(DefectScanAuxTest, CalleeInfoTest2, testing::ext::TestSize.Level0)
322 {
323 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "callee_info_test.abc";
324 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
325 CheckAbcFile(abc_file, 19U, 2U);
326
327 // check callee info of each func
328 // foo1
329 auto f1 = abc_file->GetFunctionByName("#*#foo1");
330 ASSERT_NE(f1, nullptr);
331 size_t ci_cnt1 = f1->GetCalleeInfoCount();
332 EXPECT_EQ(ci_cnt1, 3U);
333 auto ci1_0 = f1->GetCalleeInfoByIndex(0);
334 ASSERT_NE(ci1_0, nullptr);
335 ASSERT_TRUE(ci1_0->IsCalleeDefinite());
336 ASSERT_TRUE(ci1_0->GetCallee() == abc_file->GetFunctionByName("#*@1*#fn"));
337 auto ci1_1 = f1->GetCalleeInfoByIndex(1);
338 ASSERT_NE(ci1_1, nullptr);
339 ASSERT_TRUE(ci1_1->IsCalleeDefinite());
340 ASSERT_TRUE(ci1_1->GetCallee() == abc_file->GetFunctionByName("#*@1*#fn"));
341 auto ci1_2 = f1->GetCalleeInfoByIndex(2);
342 ASSERT_NE(ci1_2, nullptr);
343 ASSERT_FALSE(ci1_2->IsCalleeDefinite());
344 EXPECT_EQ(ci1_2->GetFunctionName(), "bind");
345 // #2#ColorPoint
346 auto f2 = abc_file->GetFunctionByName("#*@4~@3=#ColorPoint");
347 ASSERT_NE(f2, nullptr);
348 size_t ci_cnt2 = f2->GetCalleeInfoCount();
349 EXPECT_EQ(ci_cnt2, 1U);
350 auto ci2_0 = f2->GetCalleeInfoByIndex(0);
351 ASSERT_NE(ci2_0, nullptr);
352 ASSERT_TRUE(ci2_0->IsCalleeDefinite());
353 ASSERT_TRUE(ci2_0->GetClass() == abc_file->GetClassByName("#~@2=#Point"));
354 ASSERT_TRUE(ci2_0->GetCallee() == abc_file->GetFunctionByName("#~@2=#Point"));
355 // func6
356 auto f3 = abc_file->GetFunctionByName("#*#func6");
357 ASSERT_NE(f3, nullptr);
358 size_t ci_cnt3 = f3->GetCalleeInfoCount();
359 EXPECT_EQ(ci_cnt3, 1U);
360 auto ci3_0 = f3->GetCalleeInfoByIndex(0);
361 EXPECT_FALSE(ci3_0->IsCalleeDefinite());
362 EXPECT_EQ(ci3_0->GetFunctionName(), "bar");
363 EXPECT_EQ(ci3_0->GetExternalModuleName(), "./mod2");
364 }
365
366 HWTEST(DefectScanAuxTest, CalleeInfoTest3, testing::ext::TestSize.Level0)
367 {
368 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "callee_info_test.abc";
369 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
370 CheckAbcFile(abc_file, 19U, 2U);
371
372 // check callee info of each func
373 // func7
374 auto f4 = abc_file->GetFunctionByName("#*@5*#func7");
375 ASSERT_NE(f4, nullptr);
376 size_t ci_cnt4 = f4->GetCalleeInfoCount();
377 EXPECT_EQ(ci_cnt4, 2U);
378 auto ci4_0 = f4->GetCalleeInfoByIndex(0);
379 ASSERT_NE(ci4_0, nullptr);
380 ASSERT_FALSE(ci4_0->IsCalleeDefinite());
381 EXPECT_EQ(ci4_0->GetCalleeArgCount(), 2);
382 EXPECT_EQ(ci4_0->GetFunctionName(), "sum");
383 EXPECT_EQ(ci4_0->GetExternalModuleName(), "../../mod1");
384 auto ci4_1 = f4->GetCalleeInfoByIndex(1);
385 ASSERT_NE(ci4_1, nullptr);
386 EXPECT_FALSE(ci4_1->IsCalleeDefinite());
387 EXPECT_EQ(ci4_1->GetCalleeArgCount(), 2);
388 EXPECT_EQ(ci4_1->GetFunctionName(), "sub");
389 EXPECT_EQ(ci4_1->GetExternalModuleName(), "../../mod1");
390 // callMemberFunc1
391 auto f5 = abc_file->GetFunctionByName("#*@6*#callMemberFunc1");
392 ASSERT_NE(f5, nullptr);
393 size_t ci_cnt5 = f5->GetCalleeInfoCount();
394 EXPECT_EQ(ci_cnt5, 2U);
395 auto ci5_0 = f5->GetCalleeInfoByIndex(0);
396 ASSERT_NE(ci5_0, nullptr);
397 EXPECT_TRUE(ci5_0->IsCalleeDefinite());
398 EXPECT_TRUE(ci5_0->GetClass() != nullptr);
399 EXPECT_EQ(ci5_0->GetClass(), abc_file->GetClassByName("#~@2=#Point"));
400 EXPECT_TRUE(ci5_0->GetCallee() != nullptr);
401 EXPECT_EQ(ci5_0->GetCallee(), abc_file->GetFunctionByName("#~@2>#getCoordinateX"));
402 auto ci5_1 = f5->GetCalleeInfoByIndex(1);
403 ASSERT_NE(ci5_1, nullptr);
404 EXPECT_TRUE(ci5_1->IsCalleeDefinite());
405 EXPECT_TRUE(ci5_1->GetClass() != nullptr);
406 EXPECT_EQ(ci5_1->GetClass(), abc_file->GetClassByName("#~@2=#Point"));
407 EXPECT_TRUE(ci5_1->GetCallee() != nullptr);
408 EXPECT_EQ(ci5_1->GetCallee(), abc_file->GetFunctionByName("#~@2>#setCoordinateX"));
409 }
410
411 HWTEST(DefectScanAuxTest, CalleeInfoTest4, testing::ext::TestSize.Level0)
412 {
413 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "callee_info_test.abc";
414 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
415 CheckAbcFile(abc_file, 19U, 2U);
416
417 // check callee info of each func
418 // callMemberFunc2
419 auto f6 = abc_file->GetFunctionByName("#*@6*@7*#callMemberFunc2");
420 ASSERT_NE(f6, nullptr);
421 size_t ci_cnt6 = f6->GetCalleeInfoCount();
422 EXPECT_EQ(ci_cnt6, 2U);
423 auto ci6_0 = f6->GetCalleeInfoByIndex(0);
424 ASSERT_NE(ci6_0, nullptr);
425 EXPECT_TRUE(ci6_0->IsCalleeDefinite());
426 EXPECT_TRUE(ci6_0->GetClass() != nullptr);
427 EXPECT_EQ(ci6_0->GetClass(), abc_file->GetClassByName("#~@2=#Point"));
428 EXPECT_TRUE(ci6_0->GetCallee() != nullptr);
429 EXPECT_EQ(ci6_0->GetCallee(), abc_file->GetFunctionByName("#~@2>#plus"));
430 auto ci6_1 = f6->GetCalleeInfoByIndex(1);
431 ASSERT_NE(ci6_1, nullptr);
432 EXPECT_FALSE(ci6_1->IsCalleeDefinite());
433 EXPECT_EQ(ci6_1->GetFunctionName(), "sub");
434 EXPECT_EQ(ci6_1->GetExternalModuleName(), "");
435 EXPECT_EQ(ci6_1->GetGlobalVarName(), "");
436 // callExClassMemberFunc1
437 auto f7 = abc_file->GetFunctionByName("#*@8*#callExClassMemberFunc1");
438 ASSERT_NE(f7, nullptr);
439 size_t ci_cnt7 = f7->GetCalleeInfoCount();
440 EXPECT_EQ(ci_cnt7, 1U);
441 auto ci7_0 = f7->GetCalleeInfoByIndex(0);
442 ASSERT_NE(ci7_0, nullptr);
443 EXPECT_FALSE(ci7_0->IsCalleeDefinite());
444 EXPECT_TRUE(ci7_0->GetClass() == nullptr);
445 EXPECT_EQ(ci7_0->GetFunctionName(), "makePhoneCall");
446 EXPECT_EQ(ci7_0->GetExternalModuleName(), "../../mod1");
447 EXPECT_EQ(ci7_0->GetGlobalVarName(), "");
448 }
449
450 HWTEST(DefectScanAuxTest, GraphTest, testing::ext::TestSize.Level0)
451 {
452 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "graph_test.abc";
453 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
454 ASSERT_NE(abc_file, nullptr);
455 auto f0 = CheckFunction(abc_file, "#*#foo");
456 ASSERT_NE(f0, nullptr);
457 // check api of graph
458 auto &graph = f0->GetGraph();
459 auto bb_list = graph.GetBasicBlockList();
460 EXPECT_EQ(bb_list.size(), 8u);
461 EXPECT_EQ(bb_list.front(), graph.GetStartBasicBlock());
462 EXPECT_EQ(bb_list.back(), graph.GetEndBasicBlock());
463 std::unordered_map<InstType, uint32_t> inst_cnt_table;
__anon4380e86a0102(const Inst &inst) 464 graph.VisitAllInstructions([&inst_cnt_table](const Inst &inst) {
465 auto type = inst.GetType();
466 inst_cnt_table.insert_or_assign(type, inst_cnt_table[type] + 1);
467 });
468 uint32_t newobj_cnt = inst_cnt_table[InstType::NEWOBJRANGE_IMM8_IMM8_V8] +
469 inst_cnt_table[InstType::NEWOBJRANGE_IMM16_IMM8_V8] +
470 inst_cnt_table[InstType::WIDE_NEWOBJRANGE_PREF_IMM16_V8];
471 EXPECT_EQ(newobj_cnt, 2U);
472 uint32_t ldlex_cnt = inst_cnt_table[InstType::LDLEXVAR_IMM4_IMM4] +
473 inst_cnt_table[InstType::LDLEXVAR_IMM8_IMM8] +
474 inst_cnt_table[InstType::WIDE_LDLEXVAR_PREF_IMM16_IMM16];
475 EXPECT_EQ(ldlex_cnt, 7U);
476 EXPECT_EQ(
477 inst_cnt_table[InstType::LDOBJBYNAME_IMM8_ID16] + inst_cnt_table[InstType::LDOBJBYNAME_IMM16_ID16], 4U);
478 EXPECT_EQ(inst_cnt_table[InstType::CALLTHIS1_IMM8_V8_V8], 3U);
479 EXPECT_EQ(inst_cnt_table[InstType::CALLTHIS2_IMM8_V8_V8_V8], 1U);
480 EXPECT_EQ(inst_cnt_table[InstType::DEFINEFUNC_IMM8_ID16_IMM8] +
481 inst_cnt_table[InstType::DEFINEFUNC_IMM16_ID16_IMM8],
482 1U);
483 EXPECT_EQ(inst_cnt_table[InstType::CALLARG0_IMM8], 1U);
484 EXPECT_EQ(inst_cnt_table[InstType::CALLARG1_IMM8_V8], 1U);
485 EXPECT_EQ(inst_cnt_table[InstType::LDEXTERNALMODULEVAR_IMM8] +
486 inst_cnt_table[InstType::WIDE_LDEXTERNALMODULEVAR_PREF_IMM16],
487 1U);
488 EXPECT_EQ(inst_cnt_table[InstType::GETMODULENAMESPACE_IMM8] +
489 inst_cnt_table[InstType::WIDE_GETMODULENAMESPACE_PREF_IMM16],
490 0U);
491 EXPECT_EQ(inst_cnt_table[InstType::OPCODE_PARAMETER], 5U);
492 }
493
494 HWTEST(DefectScanAuxTest, BasicBlockTest, testing::ext::TestSize.Level0)
495 {
496 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "graph_test.abc";
497 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
498 ASSERT_NE(abc_file, nullptr);
499 auto f0 = CheckFunction(abc_file, "#*#foo");
500 ASSERT_NE(f0, nullptr);
501 // check api of basic block
502 auto &graph = f0->GetGraph();
503 auto bb0 = graph.GetStartBasicBlock();
504 EXPECT_EQ(bb0.GetPredBlocks().size(), 0U);
505 EXPECT_EQ(bb0.GetSuccBlocks().size(), 1U);
506 auto bb1 = bb0.GetSuccBlocks()[0];
507 EXPECT_EQ(bb1.GetPredBlocks().size(), 1U);
508 EXPECT_EQ(bb1.GetPredBlocks()[0], bb0);
509 auto bb1_succ_bb = bb1.GetSuccBlocks();
510 EXPECT_EQ(bb1_succ_bb.size(), 2U);
511 ASSERT_TRUE(
512 (bb1_succ_bb[0].GetSuccBlocks().size() == 2 && bb1_succ_bb[1].GetSuccBlocks().size() == 1) ||
513 (bb1_succ_bb[0].GetSuccBlocks().size() == 1 && bb1_succ_bb[1].GetSuccBlocks().size() == 2));
514 auto bb2 = graph.GetEndBasicBlock();
515 auto bb2_pred_bb = bb2.GetPredBlocks();
516 EXPECT_EQ(bb2_pred_bb.size(), 2U);
517 auto bb3 = bb2_pred_bb[0];
518 auto bb4 = bb2_pred_bb[1];
519 if (bb3.GetPredBlocks().size() < bb4.GetPredBlocks().size()) {
520 std::swap(bb3, bb4);
521 }
522 EXPECT_EQ(bb3.GetPredBlocks().size(), 2U);
523 EXPECT_EQ(bb4.GetPredBlocks().size(), 1U);
524 EXPECT_EQ(bb4.GetPredBlocks()[0], bb1);
525 }
526
527 HWTEST(DefectScanAuxTest, InstTest1, testing::ext::TestSize.Level0)
528 {
529 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "graph_test.abc";
530 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
531 ASSERT_NE(abc_file, nullptr);
532 auto f0 = CheckFunction(abc_file, "#*#foo");
533 ASSERT_NE(f0, nullptr);
534 // Test 1 : check api of inst
535 size_t ci_cnt = f0->GetCalleeInfoCount();
536 EXPECT_EQ(ci_cnt, 6U);
537 auto ci0 = f0->GetCalleeInfoByIndex(ci_cnt - 1);
538 auto call_inst0 = ci0->GetCallInst();
539 auto call_inst0_ins = call_inst0.GetInputInsts();
540 EXPECT_EQ(call_inst0_ins.size(), 4U);
541 auto call_inst0_in1_type = call_inst0_ins[1].GetType();
542 EXPECT_EQ(call_inst0_in1_type, InstType::CALLARG0_IMM8);
543 EXPECT_EQ(call_inst0_ins[1].GetUserInsts().size(), 1U);
544 EXPECT_EQ(call_inst0_ins[1].GetUserInsts()[0], call_inst0);
545 auto call_inst0_in2_type = call_inst0_ins[2].GetType();
546 EXPECT_EQ(call_inst0_in2_type, InstType::OPCODE_PARAMETER);
547 EXPECT_EQ(call_inst0_ins[2].GetArgIndex(), 4U);
548 auto param1_usrs = call_inst0_ins[2].GetUserInsts();
549 ASSERT_TRUE(std::find(param1_usrs.begin(), param1_usrs.end(), call_inst0) != param1_usrs.end());
550 auto call_inst0_in3_type = call_inst0_ins[3].GetType();
551 ASSERT_TRUE(call_inst0_in3_type == InstType::LDOBJBYNAME_IMM8_ID16 ||
552 call_inst0_in3_type == InstType::LDOBJBYNAME_IMM16_ID16);
553 EXPECT_EQ(call_inst0_ins[3].GetUserInsts().size(), 1U);
554 EXPECT_EQ(call_inst0_ins[3].GetUserInsts()[0], call_inst0);
555 }
556
557 HWTEST(DefectScanAuxTest, InstTest2, testing::ext::TestSize.Level0)
558 {
559 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "graph_test.abc";
560 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
561 ASSERT_NE(abc_file, nullptr);
562 auto f0 = CheckFunction(abc_file, "#*#foo");
563 ASSERT_NE(f0, nullptr);
564 // Test 2 : check api of inst
565 size_t ci_cnt = f0->GetCalleeInfoCount();
566 auto ci1 = f0->GetCalleeInfoByIndex(ci_cnt - 2);
567 auto call_inst1 = ci1->GetCallInst();
568 auto call_inst1_ins = call_inst1.GetInputInsts();
569 EXPECT_EQ(call_inst1_ins.size(), 2U);
570 auto call_inst1_in0_type = call_inst1_ins[1].GetType();
571 ASSERT_TRUE(call_inst1_in0_type == InstType::LDEXTERNALMODULEVAR_IMM8 ||
572 call_inst1_in0_type == InstType::WIDE_LDEXTERNALMODULEVAR_PREF_IMM16);
573 EXPECT_EQ(call_inst1_ins[1].GetUserInsts().size(), 2U);
574 ASSERT_TRUE((call_inst1_ins[1].GetUserInsts()[0] == call_inst1) ||
575 (call_inst1_ins[1].GetUserInsts()[1] == call_inst1));
576 auto phi_inst = call_inst1_ins[0];
577 EXPECT_EQ(phi_inst.GetType(), InstType::OPCODE_PHI);
578 auto phi_inst_ins = phi_inst.GetInputInsts();
579 EXPECT_EQ(phi_inst_ins.size(), 2U);
580 auto phi_inst_in0 = phi_inst_ins[0];
581 auto phi_inst_in1 = phi_inst_ins[1];
582 if (phi_inst_in0.GetType() != InstType::OPCODE_PARAMETER) {
583 std::swap(phi_inst_in0, phi_inst_in1);
584 }
585 EXPECT_EQ(phi_inst_in0.GetType(), InstType::OPCODE_PARAMETER);
586 EXPECT_EQ(phi_inst_in0.GetArgIndex(), 3U);
587 auto param0_usrs = phi_inst_in0.GetUserInsts();
588 ASSERT_TRUE(std::find(param0_usrs.begin(), param0_usrs.end(), phi_inst) != param0_usrs.end());
589 EXPECT_EQ(phi_inst_in1.GetType(), InstType::ADD2_IMM8_V8);
590 auto add2_inst_ins = phi_inst_in1.GetInputInsts();
591 EXPECT_EQ(add2_inst_ins.size(), 2U);
592 EXPECT_EQ(add2_inst_ins[0].GetType(), InstType::OPCODE_PARAMETER);
593 EXPECT_EQ(add2_inst_ins[1].GetType(), InstType::OPCODE_PARAMETER);
594 }
595
596 HWTEST(DefectScanAuxTest, ModuleInfoClassTest, testing::ext::TestSize.Level0)
597 {
598 const std::string USER_INPUT = "#~@1=#UserInput";
599 const std::string UI_INPUT = "UInput";
600 const std::string GET_TEXT = "#~@1>#getText";
601 const std::string INNER_USER_INPUT = "#~@0=#InnerUserInput";
602 const std::string GET_TEXT_BASE = "#~@0>#getTextBase";
603 const std::string FUNC1 = "#*#func1";
604 const std::string FUNC2 = "#*#func2";
605 const std::string FUNC3 = "#*#func3";
606
607 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "module_info_test.abc";
608 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
609 CheckAbcFile(abc_file, 8U, 2U);
610
611 // check exported class
612 std::string inter_name0 = abc_file->GetLocalNameByExportName(UI_INPUT);
613 EXPECT_EQ(inter_name0, "UserInput");
614 auto ex_class = abc_file->GetExportClassByExportName(UI_INPUT);
615 EXPECT_EQ(ex_class->GetClassName(), USER_INPUT);
616 CheckClass(abc_file, USER_INPUT);
617 size_t mf_func_count0 = ex_class->GetMemberFunctionCount();
618 EXPECT_EQ(mf_func_count0, 2U);
619 auto mf_func0_1 = ex_class->GetMemberFunctionByIndex(1);
620 EXPECT_EQ(mf_func0_1->GetFunctionName(), GET_TEXT);
621 ASSERT_TRUE(mf_func0_1->GetClass() == ex_class);
622 CheckFunction(abc_file, GET_TEXT);
623 auto par_class = ex_class->GetParentClass();
624 ASSERT_TRUE(par_class != nullptr);
625 EXPECT_EQ(par_class->GetClassName(), INNER_USER_INPUT);
626 CheckClass(abc_file, INNER_USER_INPUT);
627 size_t mf_func_count1 = par_class->GetMemberFunctionCount();
628 EXPECT_EQ(mf_func_count1, 2U);
629 auto mf_func1_1 = par_class->GetMemberFunctionByIndex(1);
630 EXPECT_EQ(mf_func1_1->GetFunctionName(), GET_TEXT_BASE);
631 ASSERT_TRUE(mf_func1_1->GetClass() == par_class);
632 CheckFunction(abc_file, GET_TEXT_BASE);
633 }
634
635 HWTEST(DefectScanAuxTest, ModuleInfoTest, testing::ext::TestSize.Level0)
636 {
637 const std::string USER_INPUT = "#~@1=#UserInput";
638 const std::string UI_INPUT = "UInput";
639 const std::string GET_TEXT = "#~@1>#getText";
640 const std::string INNER_USER_INPUT = "#~@0=#InnerUserInput";
641 const std::string GET_TEXT_BASE = "#~@0>#getTextBase";
642 const std::string FUNC1 = "#*#func1";
643 const std::string FUNC2 = "#*#func2";
644 const std::string FUNC3 = "#*#func3";
645
646 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "module_info_test.abc";
647 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
648 CheckAbcFile(abc_file, 8U, 2U);
649
650 // check exported func
651 // func3
652 std::string inter_name1 = abc_file->GetLocalNameByExportName("exFunc3");
653 EXPECT_EQ(inter_name1, "func3");
654 auto ex_func1 = abc_file->GetExportFunctionByExportName("exFunc3");
655 EXPECT_EQ(ex_func1->GetFunctionName(), FUNC3);
656 CheckFunction(abc_file, FUNC3);
657 // func1
658 std::string inter_name2 = abc_file->GetLocalNameByExportName("default");
659 EXPECT_EQ(inter_name2, "func1");
660 auto ex_func2 = abc_file->GetExportFunctionByExportName("default");
661 EXPECT_EQ(ex_func2->GetFunctionName(), FUNC1);
662 CheckFunction(abc_file, FUNC1);
663 // func2
664 std::string inter_name3 = abc_file->GetLocalNameByExportName("func2");
665 EXPECT_EQ(inter_name3, "func2");
666 auto ex_func3 = abc_file->GetExportFunctionByExportName("func2");
667 EXPECT_EQ(ex_func3->GetFunctionName(), FUNC2);
668 CheckFunction(abc_file, FUNC2);
669
670 // GetModuleNameByLocalName
671 std::string mod_name0 = abc_file->GetModuleNameByLocalName("var1");
672 EXPECT_EQ(mod_name0, "./mod1");
673 std::string mod_name1 = abc_file->GetModuleNameByLocalName("ns");
674 EXPECT_EQ(mod_name1, "./mod2");
675 std::string mod_name2 = abc_file->GetModuleNameByLocalName("var3");
676 EXPECT_EQ(mod_name2, "../mod3");
677 std::string mod_name3 = abc_file->GetModuleNameByLocalName("localVar4");
678 EXPECT_EQ(mod_name3, "../../mod4");
679
680 // GetImportNameByLocalName
681 std::string im_name0 = abc_file->GetImportNameByLocalName("var1");
682 EXPECT_EQ(im_name0, "default");
683 std::string im_name1 = abc_file->GetImportNameByLocalName("var3");
684 EXPECT_EQ(im_name1, "var3");
685 std::string im_name2 = abc_file->GetImportNameByLocalName("localVar4");
686 EXPECT_EQ(im_name2, "var4");
687 // GetImportNameByExportName
688 std::string ind_im_name0 = abc_file->GetImportNameByExportName("v");
689 EXPECT_EQ(ind_im_name0, "v5");
690 std::string ind_im_name1 = abc_file->GetImportNameByExportName("foo");
691 EXPECT_EQ(ind_im_name1, "foo");
692 // GetModuleNameByExportName
693 std::string ind_mod_name0 = abc_file->GetModuleNameByExportName("v");
694 EXPECT_EQ(ind_mod_name0, "./mod5");
695 std::string ind_mod_name1 = abc_file->GetModuleNameByExportName("foo");
696 EXPECT_EQ(ind_mod_name1, "../../mod7");
697 }
698
699 HWTEST(DefectScanAuxTest, AsyncFunctionTest, testing::ext::TestSize.Level0)
700 {
701 std::string test_name = DEFECT_SCAN_AUX_TEST_ABC_DIR "async_function_test.abc";
702 auto abc_file = panda::defect_scan_aux::AbcFile::Open(test_name);
703 ASSERT(abc_file != nullptr);
704
705 auto func_main = abc_file->GetFunctionByName("func_main_0");
706 auto func_count = func_main->GetDefinedFunctionCount();
707
708 std::unordered_map<InstType, uint32_t> inst_cnt_table;
709
710 for (size_t i = 0; i < func_count; ++i) {
711 auto func = func_main->GetDefinedFunctionByIndex(i);
712 auto graph = func->GetGraph();
__anon4380e86a0202(const Inst &inst) 713 graph.VisitAllInstructions([&inst_cnt_table](const Inst &inst) {
714 auto type = inst.GetType();
715 inst_cnt_table.insert_or_assign(type, inst_cnt_table[type] + 1);
716 });
717 }
718 EXPECT_EQ(inst_cnt_table[InstType::CREATEASYNCGENERATOROBJ_V8], 1U);
719 EXPECT_EQ(inst_cnt_table[InstType::ASYNCGENERATORRESOLVE_V8_V8_V8], 3U);
720 EXPECT_EQ(inst_cnt_table[InstType::ASYNCGENERATORREJECT_V8], 1U);
721 EXPECT_EQ(inst_cnt_table[InstType::GETASYNCITERATOR_IMM8], 1U);
722 EXPECT_EQ(inst_cnt_table[InstType::SETGENERATORSTATE_IMM8], 4U);
723 EXPECT_EQ(inst_cnt_table[InstType::CALLRUNTIME_NOTIFYCONCURRENTRESULT_PREF_NONE], 6U);
724 }
725 } // namespace panda::defect_scan_aux::test
726