• 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_NODE_H
17 #define PANDA_GUARD_OBFUSCATE_NODE_H
18 
19 #include "class.h"
20 #include "entity.h"
21 #include "file_path.h"
22 #include "function.h"
23 #include "module_record.h"
24 #include "object.h"
25 
26 namespace panda::guard {
27 
28 class Node final : public Entity {
29 public:
Node(Program * program,const std::string & name,std::string pkgName)30     Node(Program *program, const std::string &name, std::string pkgName)
31         : Entity(program, name),
32           moduleRecord_(program, name),
33           pkgName_(std::move(pkgName)),
34           sourceName_(name),
35           obfSourceName_(name)
36     {
37     }
38 
39     void Build() override;
40 
41     /**
42      * For Each Function In Node
43      * 1. Functions
44      * 2. For Each Function In Classes
45      */
46     void ForEachFunction(const std::function<FunctionTraver> &callback);
47 
48     /**
49      * Update file name references in this node
50      */
51     void UpdateFileNameReferences();
52 
53     /**
54      * Update SourceFile with obf file name
55      * SourceFile: file path in "abc record's source_file"
56      */
57     void UpdateSourceFile(const std::string &file);
58 
59     /**
60      * update record scopeNames
61      */
62     void UpdateScopeNames();
63 
64     /**
65      * Find PkgName field in pandasm::Record
66      */
67     static bool FindPkgName(const pandasm::Record &record, std::string &pkgName);
68 
69     /**
70      * record is json file
71      * @param record record
72      * @return true, false
73      */
74     static bool IsJsonFile(const pandasm::Record &record);
75 
76 protected:
77     void RefreshNeedUpdate() override;
78 
79     void Update() override;
80 
81 private:
82     void ForEachIns(const InstructionInfo &info, Scope scope);
83 
84     void CreateFunction(const InstructionInfo &info, Scope scope);
85 
86     void CreateClass(const InstructionInfo &info, Scope scope);
87 
88     /**
89      * e.g. method in class
90      * class A { get b() {return 1;}}
91      * bytecode:
92      * defineclasswithbuffer
93      * definemethod
94      *
95      * e.g. method in object
96      * let obj = { get b() {return 1;}}
97      * bytecode:
98      * createobjectwithbuffer
99      * definemethod
100      */
101     void CreateOuterMethod(const InstructionInfo &info);
102 
103     void CreateObject(const InstructionInfo &info, Scope scope);
104 
105     /**
106      * when obj field is array, outer ins definepropertybyname will add an property for object
107      * let obj = { arr:[] }
108      * bytecode:
109      * createobjectwithbuffer
110      * sta v0
111      * definepropertybyname arr v0
112      * @param info instruction info
113      */
114     void CreateObjectOuterProperty(const InstructionInfo &info);
115 
116     /**
117      * let obj = new ClassA();
118      * obj.field = 1; // field not defined in ClassA, field is a outer property
119      * @param info instruction info
120      */
121     void CreateOuterProperty(const InstructionInfo &info);
122 
123     /*
124      * this instruction crosses functions and cannot be analyzed by graph, therefore it has been added to the whitelist
125      * e.g.
126      * class A { ['field'] = 1; }
127      * bytecode:
128      *  func1: stlexvar
129      *  func2: defineclasswithbuffer
130      */
131     static void FindStLexVarName(const InstructionInfo &info);
132 
133     void CreateFilePath();
134 
135     void CreateFilePathForDefaultMode();
136 
137     void CreateFilePathForNormalizedMode();
138 
139     void ExtractNames();
140 
141     void WriteFileCache(const std::string &filePath) override;
142 
143     void UpdateRecordTable();
144 
145     void UpdateFileNameDefine();
146 
147     static void GetMethodNameInfo(const InstructionInfo &info, InstructionInfo &nameInfo);
148 
149 public:
150     ModuleRecord moduleRecord_;
151     FilePath filepath_;
152     std::unordered_map<std::string, Function> functionTable_ {};  // key: Function idx
153     std::unordered_map<std::string, Class> classTable_ {};        // key: class literalArray idx
154     std::unordered_map<std::string, Object> objectTable_ {};      // key: object literalArray idx
155     std::vector<Property> outerProperties_ {};
156     std::set<std::string> strings_ {};
157     std::string pkgName_;
158     bool fileNameNeedUpdate_ = true;
159     bool contentNeedUpdate_ = true;
160     bool isNormalizedOhmUrl_ = false;
161     std::string sourceName_;  // file path in "nameCache key" format
162     std::string obfSourceName_;
163     std::string sourceFile_;  // file path in "abc record's source_file"
164     std::string obfSourceFile_;
165     bool sourceFileUpdated_ = false;  // is sourceFile updated
166 };
167 
168 }  // namespace panda::guard
169 
170 #endif  // PANDA_GUARD_OBFUSCATE_NODE_H
171