• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 PANDA_GUARD_OBFUSCATE_ENTITY_H
17 #define PANDA_GUARD_OBFUSCATE_ENTITY_H
18 
19 #include "assembly-program.h"
20 
21 namespace panda::guard {
22 
23 enum Scope {
24     NONE,
25     TOP_LEVEL,
26     FUNCTION,
27 };
28 
29 class IExtractNames {
30 public:
31     virtual ~IExtractNames() = default;
32 
33     /**
34      * extract all export names
35      * @param strings strings set
36      */
37     virtual void ExtractNames(std::set<std::string> &strings) const = 0;
38 };
39 
40 class Function;
41 
42 enum class FunctionType;
43 
44 using FunctionTraver = void(Function &function);
45 
46 /**
47  * Instruction information
48  * Due to STL's memory reallocation mechanism, internal pointers are only accessible during traversal
49  */
50 class InstructionInfo {
51 public:
52     explicit InstructionInfo() = default;
53 
InstructionInfo(Function * func,pandasm::Ins * ins,size_t index)54     InstructionInfo(Function *func, pandasm::Ins *ins, size_t index) : function_(func), ins_(ins), index_(index) {}
55 
56     /**
57      * Determine whether the instruction uses registers within the function
58      */
59     [[nodiscard]] bool IsInnerReg() const;
60 
61     /**
62      * Determine whether it is valid instruction information
63      */
64     [[nodiscard]] bool IsValid() const;
65 
66     void UpdateInsName(bool generateNewName = true);
67 
68     void UpdateInsFileName();
69 
70     void WriteNameCache();
71 
72     Function *function_ = nullptr;
73     pandasm::Ins *ins_ = nullptr;
74     size_t index_ = 0;
75 
76 private:
77     std::string origin_ {};
78     std::string obfName_ {};
79 };
80 
81 class Program;
82 
83 class IEntity {
84 public:
85     virtual ~IEntity() = default;
86 
87     /**
88      * Create Entity
89      */
90     virtual void Create() = 0;
91 
92     /**
93      * Obfuscate Entity
94      */
95     virtual void Obfuscate() = 0;
96 };
97 
98 class Entity : public IEntity {
99 public:
Entity(Program * program)100     explicit Entity(Program *program) : program_(program) {}
101 
Entity(Program * program,const std::string & name)102     Entity(Program *program, const std::string &name) : program_(program), name_(name), obfName_(name) {}
103 
104     /**
105      * get Entity is export
106      * @return isExport
107      */
108     [[nodiscard]] bool IsExport() const;
109 
110     /**
111      * Create Entity
112      * 1. Build Entity
113      * 2. RefreshNeedUpdate
114      */
115     void Create() final;
116 
117     /**
118      * Obfuscate Entity
119      * 1. needUpdate true->2 false->return
120      * 2、Update name
121      * @return result code
122      */
123     void Obfuscate() final;
124 
125     /**
126      * Write NameCacheInfo to NameCacheEntity
127      * 1. Write NameCache to FileCache
128      * 2. Write NameCache to PropertyCache
129      * @param filePath File name before obfuscation
130      */
131     virtual void WriteNameCache(const std::string &filePath);
132 
133     /**
134      * origin name of Entity
135      */
136     [[nodiscard]] virtual std::string GetName() const;
137 
138     /**
139      * obf name of Entity
140      */
141     [[nodiscard]] virtual std::string GetObfName() const;
142 
143 protected:
144     /**
145      * Build Entity
146      */
147     virtual void Build();
148 
149     /**
150      * Refresh needUpdate by configs
151      */
152     virtual void RefreshNeedUpdate();
153 
154     /**
155      * Write obfuscation mapping to the IdentifierCache and MemberMethodCache nodes of the obfuscated files in the
156      * NameCache file
157      * @param filePath File name before obfuscation
158      */
159     virtual void WriteFileCache(const std::string &filePath);
160 
161     /**
162      * Write obfuscation mapping to the PropertyCache nodes of the obfuscated files in the NameCache
163      */
164     virtual void WritePropertyCache();
165 
166     /**
167      * Update Entity name
168      */
169     virtual void Update();
170 
171     /**
172      * set nameCacheScope
173      * 1. SCOPE=TOP_LEVEL nameCacheScope: name
174      * 2. SCOPE=FUNCTION nameCacheScope: functionScope#name
175      * @param name nameCache name
176      */
177     void SetNameCacheScope(const std::string &name);
178 
179     [[nodiscard]] std::string GetNameCacheScope() const;
180 
181 public:
182     Program *program_ = nullptr;
183     Scope scope_ = NONE;
184     std::string nameCacheScope_;
185     std::string name_;
186     std::string obfName_;
187     bool export_ = false;
188     bool needUpdate = true;
189     bool obfuscated = false;
190     // define instruction list, when entity define in finally block, define ins maybe occurs multiple times
191     std::vector<InstructionInfo> defineInsList_ {};
192 };
193 
194 class TopLevelOptionEntity : public Entity {
195 public:
196     static bool NeedUpdate(const Entity &entity);
197 
198     static void WritePropertyCache(const Entity &entity);
199 
TopLevelOptionEntity(Program * program)200     explicit TopLevelOptionEntity(Program *program) : Entity(program) {}
201 
202     void WritePropertyCache() override;
203 
204 protected:
205     void RefreshNeedUpdate() override;
206 };
207 
208 class PropertyOptionEntity : public Entity {
209 public:
210     static bool NeedUpdate(const Entity &entity);
211 
212     static void WritePropertyCache(const Entity &entity);
213 
PropertyOptionEntity(Program * program)214     explicit PropertyOptionEntity(Program *program) : Entity(program) {}
215 
216     void WritePropertyCache() override;
217 
218 protected:
219     void RefreshNeedUpdate() override;
220 };
221 
222 }  // namespace panda::guard
223 
224 #endif  // PANDA_GUARD_OBFUSCATE_ENTITY_H
225