• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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_UI_DECORATOR_H
17 #define PANDA_GUARD_OBFUSCATE_UI_DECORATOR_H
18 
19 #include "entity.h"
20 #include "object.h"
21 #include "property.h"
22 
23 namespace panda::guard {
24 
25 enum class UiDecoratorType {
26     NONE = 0,
27     // create by new
28     LINK,
29     PROP,
30     OBJECT_LINK,
31     PROVIDE,  // @State @Watch class object name same with @Provide, so it's all handled by @Provide
32     // create by member method
33     LOCAL_STORAGE_LINK,
34     LOCAL_STORAGE_PROP,
35     STORAGE_LINK,
36     STORAGE_PROP,
37     CONSUME,
38     // create by field in params. @Extend will add prefix __TEXT__, inst is definefunc, function obf will handle it
39     LOCAL,
40     EVENT,  // @BuilderParam is the same as @Event, so it's all handled by @Event
41     ONCE,   // @Once must be used together with @Param, so in func_main0 is @Once, constructor is handled by @Param
42     PARAM,
43     MONITOR,
44     ANIMATABLE_EXTEND,
45     TRACK,
46     TRACE,
47     COMPUTED,
48     BUILDER,
49     CONSUMER,
50     PROVIDER,
51     EXTERNAL_API,  // @Provide @Watch external api, the inst of this type will attach to @Provide
52     REUSABLE_V2
53 };
54 
55 class UiDecorator final : public PropertyOptionEntity, public IExtractNames {
56 public:
UiDecorator(Program * program,const std::unordered_map<std::string,std::shared_ptr<Object>> & objectTable)57     explicit UiDecorator(Program *program, const std::unordered_map<std::string, std::shared_ptr<Object>> &objectTable)
58         : PropertyOptionEntity(program), objectTableRef_(objectTable)
59     {
60     }
61 
62     static bool IsUiDecoratorIns(const InstructionInfo &info, Scope scope);
63 
64     void ExtractNames(std::set<std::string> &strings) const override;
65 
66     void Build() override;
67 
68     void AddDefineInsList(const std::vector<InstructionInfo> &instLIst);
69 
70     [[nodiscard]] bool IsValidUiDecoratorType() const;
71 
72 protected:
73     void Update() override;
74 
75 private:
76     // handled ins in func_main0
77     void HandleInstInFuncMain0();
78 
79     // handled ins in function
80     void HandleInstInFunction();
81 
82     void HandleStObjByNameIns();
83 
84     void HandleNewObjRangeIns(const InstructionInfo &info);
85 
86     /*
87      * e.g.
88      * @Monitor("age", "name") --> need obfuscated age and name
89      * @Monitor("info.name") --> need obfuscated info and name
90      */
91     void BuildMonitorDecorator();
92 
93     void BuildCreatedByMemberMethodDecorator(const InstructionInfo &info, uint32_t paramIndex);
94 
95     void BuildCreatedByMemberFieldDecorator();
96 
97     void BuildEventDecorator();
98 
99     void UpdateMonitorDecorator();
100 
101     [[nodiscard]] bool IsMonitorUiDecoratorType() const;
102 
103     void AddDefineInsList(const InstructionInfo &ins);
104 
105     /*
106      * e.g.
107      * @ReusableV2
108      * @ComponentV2
109      * struct XXX {}
110      *
111      * idx_001 { 4 [tag_value:5, string:"getReuseId", tag_value:255, null_value:0] }
112      * function getReuseId { lda.str XXX }
113      * function foo {
114      *   createobjectwithbuffer idx_001
115      *   sta v2
116      *   definefunc getReuseId
117      *   definepropertybyname getReuseId, v2
118      *   callthis1 v2
119      * }
120      *
121      * 1. find callthi1, then find v2(object idx_001)
122      * 2. check object idx_001 contains getReuseId
123      * 3. find the function associated with the property getReuseId, and then iterate through its instructions to obtain
124      * the decorator
125      */
126     void BuildReusableV2Decorator();
127 
128     [[nodiscard]] bool IsReusableV2UiDecoratorType() const;
129 
130     std::shared_ptr<Property> GetObjectOuterProperty(const std::string &literalArrayIdx) const;
131 
132     void EnumerateIns(const InstructionInfo &inst);
133 
134 public:
135     InstructionInfo baseInst_ {};  // obtain ui decorator information by this inst
136 
137 private:
138     UiDecoratorType type_ = UiDecoratorType::NONE;
139 
140     const std::unordered_map<std::string, std::shared_ptr<Object>> &objectTableRef_;
141 };
142 
143 }  // namespace panda::guard
144 
145 #endif  // PANDA_GUARD_OBFUSCATE_UI_DECORATOR_H
146