• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "verifier.h"
17 #include "utils.h"
18 
19 #include <gtest/gtest.h>
20 #include <string>
21 #include <cstdlib>
22 #include <fstream>
23 
24 #include "file.h"
25 #include "utils/logger.h"
26 #include "code_data_accessor-inl.h"
27 #include "method_data_accessor-inl.h"
28 
29 using namespace testing::ext;
30 
31 namespace panda::verifier {
32 class VerifierRegisterTest : public testing::Test {
33 public:
SetUpTestCase(void)34     static void SetUpTestCase(void) {};
TearDownTestCase(void)35     static void TearDownTestCase(void) {};
SetUp()36     void SetUp() {};
TearDown()37     void TearDown() {};
38 };
39 
40 /**
41 * @tc.name: verifier_test_001
42 * @tc.desc: Verify the abc file register index function.
43 * @tc.type: FUNC
44 * @tc.require: file path and name
45 */
46 HWTEST_F(VerifierRegisterTest, verifier_register_001, TestSize.Level1)
47 {
48     const std::string file_name = GRAPH_TEST_ABC_DIR "test_register_index.abc";
49     panda::verifier::Verifier ver {file_name};
50     ver.CollectIdInfos();
51     EXPECT_TRUE(ver.VerifyRegisterIndex());
52 }
53 
54 /**
55 * @tc.name: verifier_test_002
56 * @tc.desc: Verify the modified abc file register index function.
57 * @tc.type: FUNC
58 * @tc.require: file path and name
59 */
60 HWTEST_F(VerifierRegisterTest, verifier_register_002, TestSize.Level1)
61 {
62     const std::string base_file_name = GRAPH_TEST_ABC_DIR "test_register_index.abc";
63     {
64         panda::verifier::Verifier ver {base_file_name};
65         ver.CollectIdInfos();
66         EXPECT_TRUE(ver.VerifyRegisterIndex());
67     }
68     // the new register index in the abc file
69     const uint8_t new_reg_id = 0x09;
70 
71     std::ifstream base_file(base_file_name, std::ios::binary);
72     if (!base_file.is_open()) {
73         LOG(ERROR, VERIFIER) << "Failed to open file " << base_file_name;
74         EXPECT_TRUE(base_file.is_open());
75     }
76     std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(base_file), {});
77     // the known instruction which contains register index in the abc file
78     const std::vector<uint8_t> op_code = {0x60, 0x03};
79 
80     for (size_t i = 0; i < buffer.size() - 2; i++) {
81         if (buffer[i] == op_code[0] && buffer[i+1] == op_code[1]) {
82             buffer[i + 1] = static_cast<unsigned char>(new_reg_id);
83         }
84     }
85 
86     const std::string tar_file_name = GRAPH_TEST_ABC_DIR "verifier_register_002.abc";
87     GenerateModifiedAbc(buffer, tar_file_name);
88     base_file.close();
89 
90     {
91         panda::verifier::Verifier ver {tar_file_name};
92         ver.CollectIdInfos();
93         EXPECT_FALSE(ver.VerifyRegisterIndex());
94     }
95 }
96 } // namespace panda::verifier
97