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 EXPECT_TRUE(ver.Verify()); 53 } 54 55 /** 56 * @tc.name: verifier_test_002 57 * @tc.desc: Verify the modified abc file register index function. 58 * @tc.type: FUNC 59 * @tc.require: file path and name 60 */ 61 HWTEST_F(VerifierRegisterTest, verifier_register_002, TestSize.Level1) 62 { 63 const std::string base_file_name = GRAPH_TEST_ABC_DIR "test_register_index.abc"; 64 { 65 panda::verifier::Verifier ver {base_file_name}; 66 ver.CollectIdInfos(); 67 EXPECT_TRUE(ver.VerifyRegisterIndex()); 68 } 69 // the new register index in the abc file 70 const uint8_t new_reg_id = 0x09; 71 72 std::ifstream base_file(base_file_name, std::ios::binary); 73 if (!base_file.is_open()) { 74 LOG(ERROR, VERIFIER) << "Failed to open file " << base_file_name; 75 EXPECT_TRUE(base_file.is_open()); 76 } 77 std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(base_file), {}); 78 // the known instruction which contains register index in the abc file 79 const std::vector<uint8_t> op_code = {0x60, 0x03}; 80 81 for (size_t i = 0; i < buffer.size() - 2; i++) { 82 if (buffer[i] == op_code[0] && buffer[i+1] == op_code[1]) { 83 buffer[i + 1] = static_cast<unsigned char>(new_reg_id); 84 } 85 } 86 87 const std::string tar_file_name = GRAPH_TEST_ABC_DIR "verifier_register_002.abc"; 88 GenerateModifiedAbc(buffer, tar_file_name); 89 base_file.close(); 90 91 { 92 panda::verifier::Verifier ver {tar_file_name}; 93 ver.CollectIdInfos(); 94 EXPECT_FALSE(ver.VerifyRegisterIndex()); 95 } 96 } 97 98 /** 99 * @tc.name: verifier_test_003 100 * @tc.desc: Verify the range register index of the abc file. 101 * @tc.type: FUNC 102 * @tc.require: file path and name 103 */ 104 HWTEST_F(VerifierRegisterTest, verifier_register_003, TestSize.Level1) 105 { 106 const std::string base_file_name = GRAPH_TEST_ABC_DIR "test_register_index.abc"; 107 { 108 panda::verifier::Verifier ver {base_file_name}; 109 ver.CollectIdInfos(); 110 EXPECT_TRUE(ver.VerifyRegisterIndex()); 111 } 112 // the new range register number 113 const uint8_t new_range_reg_num = 0x0c; 114 115 std::ifstream base_file(base_file_name, std::ios::binary); 116 if (!base_file.is_open()) { 117 LOG(ERROR, VERIFIER) << "Failed to open file " << base_file_name; 118 EXPECT_TRUE(base_file.is_open()); 119 } 120 std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(base_file), {}); 121 // the known range instruction in the abc file 122 const std::vector<uint8_t> inst = {0x08, 0x09, 0x03, 0x03}; 123 124 for (size_t i = 0; i < buffer.size() - 4; i++) { 125 if (buffer[i] == inst[0] && buffer[i+1] == inst[1] && 126 buffer[i+2] == inst[2] && buffer[i+3] == inst[3]) { 127 buffer[i + 2] = static_cast<unsigned char>(new_range_reg_num); 128 } 129 } 130 131 const std::string tar_file_name = GRAPH_TEST_ABC_DIR "verifier_register_003.abc"; 132 GenerateModifiedAbc(buffer, tar_file_name); 133 base_file.close(); 134 135 { 136 panda::verifier::Verifier ver {tar_file_name}; 137 ver.CollectIdInfos(); 138 EXPECT_FALSE(ver.VerifyRegisterIndex()); 139 } 140 } 141 } // namespace panda::verifier 142