1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/profiling/deobfuscator.h"
18
19 #include <optional>
20 #include "perfetto/base/status.h"
21 #include "perfetto/ext/base/file_utils.h"
22 #include "perfetto/ext/base/scoped_file.h"
23 #include "perfetto/ext/base/string_splitter.h"
24
25 #include "perfetto/protozero/scattered_heap_buffer.h"
26 #include "protos/perfetto/trace/profiling/deobfuscation.pbzero.h"
27 #include "protos/perfetto/trace/trace.pbzero.h"
28 #include "protos/perfetto/trace/trace_packet.pbzero.h"
29
30 namespace perfetto {
31 namespace profiling {
32 namespace {
33
34 struct ProguardClass {
35 std::string obfuscated_name;
36 std::string deobfuscated_name;
37 };
38
ParseClass(std::string line)39 std::optional<ProguardClass> ParseClass(std::string line) {
40 base::StringSplitter ss(std::move(line), ' ');
41
42 if (!ss.Next()) {
43 PERFETTO_ELOG("Missing deobfuscated name.");
44 return std::nullopt;
45 }
46 std::string deobfuscated_name(ss.cur_token(), ss.cur_token_size());
47
48 if (!ss.Next() || ss.cur_token_size() != 2 ||
49 strncmp("->", ss.cur_token(), 2) != 0) {
50 PERFETTO_ELOG("Missing ->");
51 return std::nullopt;
52 }
53
54 if (!ss.Next()) {
55 PERFETTO_ELOG("Missing obfuscated name.");
56 return std::nullopt;
57 }
58 std::string obfuscated_name(ss.cur_token(), ss.cur_token_size());
59 if (obfuscated_name.empty()) {
60 PERFETTO_ELOG("Empty obfuscated name.");
61 return std::nullopt;
62 }
63 if (obfuscated_name.back() != ':') {
64 PERFETTO_ELOG("Expected colon.");
65 return std::nullopt;
66 }
67
68 obfuscated_name.resize(obfuscated_name.size() - 1);
69 if (ss.Next()) {
70 PERFETTO_ELOG("Unexpected data.");
71 return std::nullopt;
72 }
73 return ProguardClass{std::move(obfuscated_name),
74 std::move(deobfuscated_name)};
75 }
76
77 enum class ProguardMemberType {
78 kField,
79 kMethod,
80 };
81
82 struct ProguardMember {
83 ProguardMemberType type;
84 std::string obfuscated_name;
85 std::string deobfuscated_name;
86 };
87
ParseMember(std::string line)88 std::optional<ProguardMember> ParseMember(std::string line) {
89 base::StringSplitter ss(std::move(line), ' ');
90
91 if (!ss.Next()) {
92 PERFETTO_ELOG("Missing type name.");
93 return std::nullopt;
94 }
95 std::string type_name(ss.cur_token(), ss.cur_token_size());
96
97 if (!ss.Next()) {
98 PERFETTO_ELOG("Missing deobfuscated name.");
99 return std::nullopt;
100 }
101 std::string deobfuscated_name(ss.cur_token(), ss.cur_token_size());
102
103 if (!ss.Next() || ss.cur_token_size() != 2 ||
104 strncmp("->", ss.cur_token(), 2) != 0) {
105 PERFETTO_ELOG("Missing ->");
106 return std::nullopt;
107 }
108
109 if (!ss.Next()) {
110 PERFETTO_ELOG("Missing obfuscated name.");
111 return std::nullopt;
112 }
113 std::string obfuscated_name(ss.cur_token(), ss.cur_token_size());
114
115 if (ss.Next()) {
116 PERFETTO_ELOG("Unexpected data.");
117 return std::nullopt;
118 }
119
120 ProguardMemberType member_type;
121 auto paren_idx = deobfuscated_name.find('(');
122 if (paren_idx != std::string::npos) {
123 member_type = ProguardMemberType::kMethod;
124 deobfuscated_name = deobfuscated_name.substr(0, paren_idx);
125 auto colon_idx = type_name.find(':');
126 if (colon_idx != std::string::npos) {
127 type_name = type_name.substr(colon_idx + 1);
128 }
129 } else {
130 member_type = ProguardMemberType::kField;
131 }
132 return ProguardMember{member_type, std::move(obfuscated_name),
133 std::move(deobfuscated_name)};
134 }
135
FlattenMethods(const std::vector<std::string> & v)136 std::string FlattenMethods(const std::vector<std::string>& v) {
137 if (v.size() == 1) {
138 return v[0];
139 }
140 return "[ambiguous]";
141 }
142
143 } // namespace
144
FlattenClasses(const std::map<std::string,std::vector<std::string>> & m)145 std::string FlattenClasses(
146 const std::map<std::string, std::vector<std::string>>& m) {
147 std::string result;
148 bool first = true;
149 for (const auto& p : m) {
150 if (!first) {
151 result += " | ";
152 }
153 result += p.first + "." + FlattenMethods(p.second);
154 first = false;
155 }
156 return result;
157 }
158
159 // See https://www.guardsquare.com/en/products/proguard/manual/retrace for the
160 // file format we are parsing.
AddLine(std::string line)161 base::Status ProguardParser::AddLine(std::string line) {
162 auto first_ch_pos = line.find_first_not_of(" \t");
163 if (first_ch_pos == std::string::npos || line[first_ch_pos] == '#')
164 return base::Status();
165
166 bool is_member = line[0] == ' ';
167 if (is_member && !current_class_) {
168 return base::Status(
169 "Failed to parse proguard map. Saw member before class.");
170 }
171 if (!is_member) {
172 auto opt_cls = ParseClass(std::move(line));
173 if (!opt_cls)
174 return base::Status("Class not found.");
175 auto p = mapping_.emplace(std::move(opt_cls->obfuscated_name),
176 std::move(opt_cls->deobfuscated_name));
177 if (!p.second) {
178 return base::Status("Duplicate class.");
179 }
180 current_class_ = &p.first->second;
181 } else {
182 auto opt_member = ParseMember(std::move(line));
183 if (!opt_member)
184 return base::Status("Failed to parse member.");
185 switch (opt_member->type) {
186 case (ProguardMemberType::kField): {
187 if (!current_class_->AddField(opt_member->obfuscated_name,
188 opt_member->deobfuscated_name)) {
189 return base::Status(std::string("Member redefinition: ") +
190 current_class_->deobfuscated_name().c_str() +
191 "." + opt_member->deobfuscated_name.c_str() +
192 " Proguard map invalid");
193 }
194 break;
195 }
196 case (ProguardMemberType::kMethod): {
197 current_class_->AddMethod(opt_member->obfuscated_name,
198 opt_member->deobfuscated_name);
199 break;
200 }
201 }
202 }
203 return base::Status();
204 }
205
AddLines(std::string contents)206 bool ProguardParser::AddLines(std::string contents) {
207 size_t lineno = 1;
208 for (base::StringSplitter lines(std::move(contents), '\n'); lines.Next();) {
209 auto status = AddLine(lines.cur_token());
210 if (!status.ok()) {
211 PERFETTO_ELOG("Failed to parse proguard map (line %zu): %s", lineno,
212 status.c_message());
213 return false;
214 }
215 lineno++;
216 }
217 return true;
218 }
219
MakeDeobfuscationPackets(const std::string & package_name,const std::map<std::string,profiling::ObfuscatedClass> & mapping,std::function<void (const std::string &)> callback)220 void MakeDeobfuscationPackets(
221 const std::string& package_name,
222 const std::map<std::string, profiling::ObfuscatedClass>& mapping,
223 std::function<void(const std::string&)> callback) {
224 protozero::HeapBuffered<perfetto::protos::pbzero::Trace> trace;
225 auto* packet = trace->add_packet();
226 // TODO(fmayer): Add handling for package name and version code here so we
227 // can support multiple dumps in the same trace.
228 auto* proto_mapping = packet->set_deobfuscation_mapping();
229 proto_mapping->set_package_name(package_name);
230 for (const auto& p : mapping) {
231 const std::string& obfuscated_class_name = p.first;
232 const profiling::ObfuscatedClass& cls = p.second;
233
234 auto* proto_class = proto_mapping->add_obfuscated_classes();
235 proto_class->set_obfuscated_name(obfuscated_class_name);
236 proto_class->set_deobfuscated_name(cls.deobfuscated_name());
237 for (const auto& field_p : cls.deobfuscated_fields()) {
238 const std::string& obfuscated_field_name = field_p.first;
239 const std::string& deobfuscated_field_name = field_p.second;
240 auto* proto_member = proto_class->add_obfuscated_members();
241 proto_member->set_obfuscated_name(obfuscated_field_name);
242 proto_member->set_deobfuscated_name(deobfuscated_field_name);
243 }
244 for (const auto& field_p : cls.deobfuscated_methods()) {
245 const std::string& obfuscated_method_name = field_p.first;
246 const std::string& deobfuscated_method_name = field_p.second;
247 auto* proto_member = proto_class->add_obfuscated_methods();
248 proto_member->set_obfuscated_name(obfuscated_method_name);
249 proto_member->set_deobfuscated_name(deobfuscated_method_name);
250 }
251 }
252 callback(trace.SerializeAsString());
253 }
254
ReadProguardMapsToDeobfuscationPackets(const std::vector<ProguardMap> & maps,std::function<void (std::string)> fn)255 bool ReadProguardMapsToDeobfuscationPackets(
256 const std::vector<ProguardMap>& maps,
257 std::function<void(std::string)> fn) {
258 for (const ProguardMap& map : maps) {
259 const char* filename = map.filename.c_str();
260 base::ScopedFstream f(fopen(filename, "re"));
261 if (!f) {
262 PERFETTO_ELOG("Failed to open %s", filename);
263 return false;
264 }
265 profiling::ProguardParser parser;
266 std::string contents;
267 PERFETTO_CHECK(base::ReadFileStream(*f, &contents));
268 if (!parser.AddLines(std::move(contents))) {
269 PERFETTO_ELOG("Failed to parse %s", filename);
270 return false;
271 }
272 std::map<std::string, profiling::ObfuscatedClass> obfuscation_map =
273 parser.ConsumeMapping();
274
275 // TODO(fmayer): right now, we don't use the profile we are given. We can
276 // filter the output to only contain the classes actually seen in the
277 // profile.
278 MakeDeobfuscationPackets(map.package, obfuscation_map, fn);
279 }
280 return true;
281 }
282
GetPerfettoProguardMapPath()283 std::vector<ProguardMap> GetPerfettoProguardMapPath() {
284 const char* env = getenv("PERFETTO_PROGUARD_MAP");
285 if (env == nullptr)
286 return {};
287 std::vector<ProguardMap> res;
288 for (base::StringSplitter sp(std::string(env), ':'); sp.Next();) {
289 std::string token(sp.cur_token(), sp.cur_token_size());
290 size_t eq = token.find('=');
291 if (eq == std::string::npos) {
292 PERFETTO_ELOG(
293 "Invalid PERFETTO_PROGUARD_MAP. "
294 "Expected format packagename=filename[:packagename=filename...], "
295 "e.g. com.example.package1=foo.txt:com.example.package2=bar.txt.");
296 return {};
297 }
298 res.emplace_back(ProguardMap{token.substr(0, eq), token.substr(eq + 1)});
299 }
300 return res; // for Wreturn-std-move-in-c++11.
301 }
302
303 } // namespace profiling
304 } // namespace perfetto
305