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