• 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     void SetExportAndRefreshNeedUpdate(bool isExport) override;
65 
66     /**
67      * Traverse all instructions of the function
68      * @param callback instruction callback
69      */
70     void EnumerateIns(const std::function<InsTraver> &callback);
71 
72     /**
73      * Update all reference instruction
74      */
75     void UpdateReference();
76 
77     /**
78      * update function annotation reference
79      */
80     void UpdateAnnotationReference();
81 
82     /**
83      * To delete the ConsoleLog log, call it after updating other instruction (deleting the log will change the relative
84      * order of instruction)
85      */
86     void RemoveConsoleLog();
87 
88     /**
89      * when the compact obfuscation option is enabled, uniformly change the line number to 1
90      */
91     void RemoveLineNumber();
92 
93     /**
94      * Fill instruction with specified sequence number
95      * @param index instruction number
96      * @param instInfo instruction to be filled in
97      */
98     void FillInstInfo(size_t index, InstructionInfo &instInfo);
99 
100     /**
101      * Retrieve the Graph structure corresponding to the function, which is used to associate instruction calculations
102      */
103     void GetGraph(compiler::Graph *&outGraph);
104 
105     /**
106      * Get origin function struct
107      * @return origin function struct
108      */
109     pandasm::Function &GetOriginFunction();
110 
111 protected:
112     void RefreshNeedUpdate() override;
113 
114     void Update() override;
115 
116     void WriteFileCache(const std::string &filePath) override;
117 
118     void WritePropertyCache() override;
119 
120     /**
121      * Modify the function definition to default to the instruction where the function is defined (definefunc)
122      */
123     virtual void UpdateDefine() const;
124 
125     virtual void InitNameCacheScope();
126 
127     [[nodiscard]] std::string GetLines() const;
128 
129     [[nodiscard]] virtual bool IsWhiteListOrAnonymousFunction(const std::string &functionIdx) const;
130 
131     [[nodiscard]] virtual bool IsNameObfuscated() const;
132 
133 private:
134     void InitBaseInfo();
135 
136     void SetFunctionType(char functionTypeCode);
137 
138     void UpdateName(const std::shared_ptr<Node> &node);
139 
140     void UpdateFunctionTable(const std::shared_ptr<Node> &node) const;
141 
142     void BuildPcInsMap(const compiler::Graph *graph);
143 
144     void FreeGraph();
145 
146 public:
147     std::optional<Node *> node_ = std::nullopt;
148     std::string idx_;
149     std::string obfIdx_;
150     std::string recordName_;
151     std::string rawName_;
152     std::string scopeTypeStr_;
153     FunctionType type_ = FunctionType::NONE;
154     size_t regsNum_ = 0;  // The number of registers requested within the function
155     size_t startLine_ = 0;
156     size_t endLine_ = 0;
157     uint32_t methodPtr_ = 0;  // this field is used for graph analysis
158     // own property, Attributes associated with function key:property name
159     std::unordered_map<std::string, std::shared_ptr<Property>> propertyTable_ {};
160     // Traverse the properties present in the function or parameters
161     std::vector<std::shared_ptr<Property>> variableProperties_ {};
162     // Object.defineProperty, only when enable ui decorator scene is assigned values
163     std::vector<std::shared_ptr<Property>> objectDecoratorProperties_ {};
164     std::unordered_map<size_t, size_t> pcInstMap_ {};  // Mapping table between instruction PC and instruction index
165     bool useScope_ = true;
166     bool nameNeedUpdate_ = true;     // Function name needs to be updated
167     bool contentNeedUpdate_ = true;  // Function content (attributes, etc.) needs to be updated
168     bool component_ = false;
169 
170 private:
171     bool anonymous_ = false;  // is anonymous function
172     compiler::Graph *graph_ = nullptr;
173     std::unique_ptr<ArenaAllocator> allocator_ = nullptr;
174     std::unique_ptr<ArenaAllocator> localAllocator_ = nullptr;
175     std::unique_ptr<compiler::RuntimeInterface> runtimeInterface_ = nullptr;
176 };
177 
178 }  // namespace panda::guard
179 
180 #endif  // PANDA_GUARD_OBFUSCATE_FUNCTION_H
181