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 "instruction_info.h" 20 21 namespace panda::guard { 22 23 enum Scope { 24 NONE, 25 TOP_LEVEL, 26 FUNCTION, 27 }; 28 29 enum ParamIndex { 30 INDEX_0, 31 INDEX_1, 32 INDEX_2, 33 INDEX_3, 34 }; 35 36 class IExtractNames { 37 public: 38 virtual ~IExtractNames() = default; 39 40 /** 41 * extract all export names 42 * @param strings strings set 43 */ 44 virtual void ExtractNames(std::set<std::string> &strings) const = 0; 45 }; 46 47 using FunctionTraver = void(Function &function); 48 49 class IEntity { 50 public: 51 virtual ~IEntity() = default; 52 53 /** 54 * Create Entity 55 */ 56 virtual void Create() = 0; 57 58 /** 59 * Obfuscate Entity 60 */ 61 virtual void Obfuscate() = 0; 62 }; 63 64 class Program; 65 66 class Entity : public IEntity { 67 public: Entity(Program * program)68 explicit Entity(Program *program) : program_(program) {} 69 Entity(Program * program,const std::string & name)70 Entity(Program *program, const std::string &name) : program_(program), name_(name), obfName_(name) {} 71 72 /** 73 * set entity and it's members export 74 * @param isExport entity is export 75 */ 76 virtual void SetExportAndRefreshNeedUpdate(bool isExport); 77 78 /** 79 * get Entity is export 80 * @return isExport 81 */ 82 [[nodiscard]] bool IsExport() const; 83 84 /** 85 * Create Entity 86 * 1. Build Entity 87 * 2. RefreshNeedUpdate 88 */ 89 void Create() final; 90 91 /** 92 * Obfuscate Entity 93 * 1. needUpdate true->2 false->return 94 * 2、Update name 95 * @return result code 96 */ 97 void Obfuscate() final; 98 99 /** 100 * Write NameCacheInfo to NameCacheEntity 101 * 1. Write NameCache to FileCache 102 * 2. Write NameCache to PropertyCache 103 * @param filePath File name before obfuscation 104 */ 105 virtual void WriteNameCache(const std::string &filePath); 106 107 /** 108 * Implement this method when obfuscated is not required but writing nameCache is needed 109 */ 110 virtual void WriteNameCache(); 111 112 /** 113 * origin name of Entity 114 */ 115 [[nodiscard]] virtual std::string GetName() const; 116 117 /** 118 * obf name of Entity 119 */ 120 [[nodiscard]] virtual std::string GetObfName() const; 121 122 protected: 123 /** 124 * Build Entity 125 */ 126 virtual void Build(); 127 128 /** 129 * Refresh needUpdate by configs 130 */ 131 virtual void RefreshNeedUpdate(); 132 133 /** 134 * Write obfuscation mapping to the IdentifierCache and MemberMethodCache nodes of the obfuscated files in the 135 * NameCache file 136 * @param filePath File name before obfuscation 137 */ 138 virtual void WriteFileCache(const std::string &filePath); 139 140 /** 141 * Write obfuscation mapping to the PropertyCache nodes of the obfuscated files in the NameCache 142 */ 143 virtual void WritePropertyCache(); 144 145 /** 146 * Update Entity name 147 */ 148 virtual void Update(); 149 150 /** 151 * set nameCacheScope 152 * 1. SCOPE=TOP_LEVEL nameCacheScope: name 153 * 2. SCOPE=FUNCTION nameCacheScope: functionScope#name 154 * @param name nameCache name 155 */ 156 void SetNameCacheScope(const std::string &name); 157 158 [[nodiscard]] std::string GetNameCacheScope() const; 159 160 void UpdateLiteralArrayTableIdx(const std::string &originIdx, const std::string &updatedIdx) const; 161 162 public: 163 Program *program_ = nullptr; 164 Scope scope_ = NONE; 165 std::string nameCacheScope_; 166 std::string name_; 167 std::string obfName_; 168 bool export_ = false; 169 bool needUpdate_ = true; 170 bool obfuscated_ = false; 171 // define instruction list, when entity define in finally block, define ins maybe occurs multiple times 172 std::vector<InstructionInfo> defineInsList_ {}; 173 }; 174 175 class TopLevelOptionEntity : public Entity { 176 public: 177 static bool NeedUpdate(const Entity &entity); 178 179 static void WritePropertyCache(const Entity &entity); 180 TopLevelOptionEntity(Program * program)181 explicit TopLevelOptionEntity(Program *program) : Entity(program) {} 182 183 void WritePropertyCache() override; 184 185 protected: 186 void RefreshNeedUpdate() override; 187 }; 188 189 class PropertyOptionEntity : public Entity { 190 public: 191 static bool NeedUpdate(const Entity &entity); 192 193 static void WritePropertyCache(const Entity &entity); 194 PropertyOptionEntity(Program * program)195 explicit PropertyOptionEntity(Program *program) : Entity(program) {} 196 197 void WritePropertyCache() override; 198 199 protected: 200 void RefreshNeedUpdate() override; 201 }; 202 203 } // namespace panda::guard 204 205 #endif // PANDA_GUARD_OBFUSCATE_ENTITY_H 206