• 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 #ifndef VERIFIER_VERIFIER_H
17 #define VERIFIER_VERIFIER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 
23 #include "bytecode_instruction_enum_gen.h"
24 #include "bytecode_instruction-inl.h"
25 #include "code_data_accessor-inl.h"
26 #include "file.h"
27 #include "file_items.h"
28 #include "literal_data_accessor.h"
29 #include "method_data_accessor-inl.h"
30 #include "utils/utf.h"
31 
32 namespace panda::verifier {
33 
34 using Opcode = BytecodeInstruction::Opcode;
35 
36 enum class ActionType {
37     CHECKCONSTPOOLCONTENT,
38     COLLECTINFOS,
39 };
40 
41 class Verifier {
42 public:
43     explicit Verifier(const std::string &filename);
44     ~Verifier() = default;
45 
46     bool Verify();
47     void CollectIdInfos();
48     bool VerifyChecksum();
49     bool VerifyConstantPool();
50     bool VerifyRegisterIndex();
51     bool VerifyConstantPoolIndex();
52     bool VerifyConstantPoolContent();
53 
54     std::vector<uint32_t> literal_ids_;
55     std::unordered_map<uint32_t, uint32_t> inner_literal_map_;
56     std::unordered_map<uint32_t, uint32_t> inner_method_map_;
57 
58 private:
59     void GetLiteralIds();
60     void GetConstantPoolIds();
61     bool CollectIdInInstructions(const panda_file::File::EntityId &method_id);
62     void CollectModuleLiteralId(const panda_file::File::EntityId &field_id);
63     bool CheckConstantPool(const verifier::ActionType type);
64     size_t GetVRegCount(const BytecodeInstruction &bc_ins);
65     bool CheckConstantPoolActions(const verifier::ActionType type, panda_file::File::EntityId method_id);
66     bool VerifyMethodId(const uint32_t &method_id) const;
67     bool VerifyLiteralId(const uint32_t &literal_id) const;
68     bool VerifyStringId(const uint32_t &string_id) const;
69     bool CheckVRegIdx(const BytecodeInstruction &bc_ins, const size_t count, const uint32_t max_reg_idx);
70     std::optional<int64_t> GetFirstImmFromInstruction(const BytecodeInstruction &bc_ins);
71     std::optional<uint64_t> GetSlotNumberFromAnnotation(panda_file::MethodDataAccessor &method_accessor);
72     bool VerifyMethodIdInLiteralArray(const uint32_t &id);
73     bool VerifyStringIdInLiteralArray(const uint32_t &id);
74     bool VerifyLiteralIdInLiteralArray(const uint32_t &id);
75     bool IsModuleLiteralId(const panda_file::File::EntityId &id) const;
76     bool VerifySingleLiteralArray(const panda_file::File::EntityId &literal_id);
77     bool VerifyLiteralArrays();
78     bool IsJumpInstruction(const Opcode &ins_opcode);
79     bool VerifyJumpInstruction(const BytecodeInstruction &bc_ins, const BytecodeInstruction &bc_ins_last,
80                                const BytecodeInstruction &bc_ins_first);
81     bool GetIcSlotFromInstruction(const BytecodeInstruction &bc_ins, uint32_t &first_slot_index, bool &has_slot,
82                                   bool &is_two_slot);
83     bool VerifySlotNumber(panda_file::MethodDataAccessor &method_accessor, const uint32_t &slot_number,
84                           const panda_file::File::EntityId &method_id);
85     bool CheckConstantPoolMethodContent(const panda_file::File::EntityId &method_id);
86     bool CheckConstantPoolIndex() const;
87 
88     std::unique_ptr<const panda_file::File> file_;
89     std::vector<uint32_t> constant_pool_ids_;
90     std::vector<uint32_t> all_method_ids_;
91     std::unordered_set<uint32_t> ins_method_ids_;
92     std::unordered_set<uint32_t> ins_literal_ids_;
93     std::unordered_set<uint32_t> ins_string_ids_;
94     std::unordered_set<uint32_t> module_literals_;
95     static constexpr size_t DEFAULT_ARGUMENT_NUMBER = 3;
96     static constexpr uint32_t FILE_CONTENT_OFFSET = 12U;
97 };
98 } // namespace panda::verifier
99 #endif
100