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_CLASS_H 17 #define PANDA_GUARD_OBFUSCATE_CLASS_H 18 19 #include "entity.h" 20 #include "method.h" 21 #include "module_record.h" 22 23 namespace panda::guard { 24 25 class Class final : public Entity, public IExtractNames { 26 public: Class(Program * program,const std::string & constructorIdx)27 Class(Program *program, const std::string &constructorIdx) : Entity(program), constructor_(program, constructorIdx) 28 { 29 } 30 31 void Build() override; 32 33 void WriteNameCache(const std::string &filePath) override; 34 35 /** 36 * Traverse all method instructions 37 * @param callback instruction callback 38 */ 39 void EnumerateMethodIns(const std::function<InsTraver> &callback); 40 41 /** 42 * For Each Function In Class 43 * 1. Class.constructor 44 * 2. Class.methods(LiteralArray) 45 * 3. Class.outerMethods(defined by definemethod) 46 */ 47 void EnumerateFunctions(const std::function<FunctionTraver> &callback); 48 49 void ExtractNames(std::set<std::string> &strings) const override; 50 51 protected: 52 void Update() override; 53 54 void WriteFileCache(const std::string &filePath) override; 55 56 void WritePropertyCache() override; 57 58 private: 59 void CreateMethods(const pandasm::LiteralArray &literalArray); 60 61 void CreateMethod(const pandasm::LiteralArray &literalArray, size_t index, bool isStatic); 62 63 void UpdateLiteralArrayIdx(); 64 65 public: 66 ModuleRecord *moduleRecord_ = nullptr; 67 std::optional<Node*> node_ = std::nullopt; 68 Function constructor_; 69 std::string literalArrayIdx_; 70 /* 71 * Method and OuterMethod Example Explanation: 72 * class A { 73 * foo() {} 74 * get v() {} 75 * } 76 * bytecode: 77 * literalArray: main_738 {...[tag_value:5, string:"foo",...]} 78 * 79 * defineclasswithbuffer 0x1 main.#~A=#A, main_738 80 * lda.str v 81 * definemethod main.#~A>#v 82 * definegettersetterbyvalue 83 * 84 * foo: Method, in literalArray 85 * get v: OuterMethod, not in literalArray 86 */ 87 // The defineclasswithbuffer instruction associates methods in literalArray 88 std::vector<std::shared_ptr<Method>> methods_ {}; 89 // The external method defined by the definemethod instruction 90 std::vector<std::shared_ptr<OuterMethod>> outerMethods_ {}; 91 /* if is callruntime instruction, special processing is required when parsing the literalArray because the end of 92 * literalArray of callruntime instruction is not a static method but running information */ 93 bool callRunTimeInst_ = false; 94 bool component_ = false; // is UI component class 95 }; 96 97 } // namespace panda::guard 98 99 #endif // PANDA_GUARD_OBFUSCATE_CLASS_H 100