• 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_FUNCTION_H
17 #define PANDA_GUARD_OBFUSCATE_FUNCTION_H
18 
19 #include <functional>
20 
21 #include "compiler/optimizer/ir/graph.h"
22 #include "mem/arena_allocator.h"
23 
24 #include "entity.h"
25 #include "property.h"
26 
27 namespace panda::guard {
28 
29 class Node;
30 
31 using InsTraver = void(InstructionInfo &functionInfo);
32 
33 enum class FunctionType {
34     NONE,
35     INSTANCE_FUNCTION,     // '>'
36     STATIC_FUNCTION,       // '<'
37     CONSTRUCTOR_FUNCTION,  // '='
38     NORMAL_FUNCTION,       // '*'
39     ENUM_FUNCTION,         // '%'
40     NAMESPACE_FUNCTION,    // '&'
41 };
42 
43 class Function : public Entity, public IExtractNames {
44 public:
45     Function(Program *program, std::string idx, bool useScope = true)
Entity(program)46         : Entity(program), idx_(std::move(idx)), useScope_(useScope)
47     {
48         this->obfIdx_ = this->idx_;  // obfIdx default equal to idx
49     }
50 
51     /**
52      * Function Init
53      * 1. split idx get record, scope, name
54      * 2. InitBaseInfo from origin Function
55      */
56     void Init();
57 
58     void Build() override;
59 
60     void WriteNameCache(const std::string &filePath) override;
61 
62     void ExtractNames(std::set<std::string> &strings) const override;
63 
64     /**
65      * Traverse all instructions of the function
66      * @param callback instruction callback
67      */
68     void ForEachIns(const std::function<InsTraver> &callback);
69 
70     /**
71      * Update all reference instruction
72      */
73     void UpdateReference();
74 
75     /**
76      * To delete the ConsoleLog log, call it after updating other instruction (deleting the log will change the relative
77      * order of instruction)
78      */
79     void RemoveConsoleLog();
80 
81     /**
82      * Fill instruction with specified sequence number
83      * @param index instruction number
84      * @param instInfo instruction to be filled in
85      */
86     void FillInstInfo(size_t index, InstructionInfo &instInfo);
87 
88     /**
89      * Retrieve the Graph structure corresponding to the function, which is used to associate instruction calculations
90      */
91     void GetGraph(compiler::Graph *&outGraph);
92 
93     /**
94      * Get origin function struct
95      * @return origin function struct
96      */
97     pandasm::Function &GetOriginFunction();
98 
99 protected:
100     void RefreshNeedUpdate() override;
101 
102     void Update() override;
103 
104     void WriteFileCache(const std::string &filePath) override;
105 
106     void WritePropertyCache() override;
107 
108     /**
109      * Modify the function definition to default to the instruction where the function is defined (definefunc)
110      */
111     virtual void UpdateDefine() const;
112 
113     virtual void InitNameCacheScope();
114 
115     [[nodiscard]] std::string GetLines() const;
116 
117     virtual bool IsWhiteListOrAnonymousFunction(const std::string &functionIdx) const;
118 
119     virtual bool IsNameObfuscated() const;
120 
121 private:
122     void InitBaseInfo();
123 
124     void SetFunctionType(char functionTypeCode);
125 
126     /**
127      * e.g. class A {
128      *  constructor {
129      *    this.v1 = 1;
130      *
131      *    let obj = {};
132      *    obj.v2 = 2;
133      *  }
134      * }
135      * v1: property, bind function
136      * v2: variable property, bind object
137      */
138     void CreateProperty(const InstructionInfo &info);
139 
140     void UpdateName(const Node &node);
141 
142     void UpdateFunctionTable(Node &node);
143 
144     void BuildPcInsMap(const compiler::Graph *graph);
145 
146 public:
147     std::string idx_;
148     std::string obfIdx_;
149     std::string recordName_;
150     std::string rawName_;
151     std::string scopeTypeStr_;
152     FunctionType type_ = FunctionType::NONE;
153     size_t regsNum = 0;  // The number of registers requested within the function
154     size_t startLine_ = 0;
155     size_t endLine_ = 0;
156     uint32_t methodPtr_ = 0;                           // this field is used for graph analysis
157     std::vector<Property> properties_ {};              // own property, Attributes associated with constructor
158     std::vector<Property> variableProperties_ {};      // Traverse the properties present in the function or parameters
159     std::unordered_map<size_t, size_t> pcInstMap_ {};  // Mapping table between instruction PC and instruction index
160     bool useScope_ = true;
161     bool nameNeedUpdate_ = true;     // Function name needs to be updated
162     bool contentNeedUpdate_ = true;  // Function content (attributes, etc.) needs to be updated
163 
164 private:
165     bool anonymous = false;  // is anonymous function
166     compiler::Graph *graph_ = nullptr;
167     std::shared_ptr<ArenaAllocator> allocator_ = nullptr;
168     std::shared_ptr<ArenaAllocator> localAllocator_ = nullptr;
169     std::shared_ptr<compiler::RuntimeInterface> runtimeInterface_ = nullptr;
170 };
171 
172 }  // namespace panda::guard
173 
174 #endif  // PANDA_GUARD_OBFUSCATE_FUNCTION_H
175