xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/deobfuscation_mapping_table.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2022 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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_DEOBFUSCATION_MAPPING_TABLE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_DEOBFUSCATION_MAPPING_TABLE_H_
19 
20 #include <cstdint>
21 #include <optional>
22 #include <string>
23 #include <vector>
24 
25 #include "perfetto/ext/base/flat_hash_map.h"
26 #include "perfetto/ext/base/hash.h"
27 #include "perfetto/ext/base/string_view.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 
30 namespace perfetto {
31 namespace trace_processor {
32 
33 // Constains deobfuscation for Java class names and its members per |PackageId|.
34 class DeobfuscationMappingTable {
35  public:
36   struct PackageId {
37     std::string package_name;
38     int64_t version_code;
39 
40     bool operator==(const PackageId& other) const {
41       return package_name == other.package_name &&
42              version_code == other.version_code;
43     }
44   };
45 
46   // For the given |package| and |obfuscated_class_name| adds translations of
47   // the class and its members.
48   //
49   // Returns `true` if the translation for the given class
50   // was inserted, `false` if there is already a translation for the given
51   // class.
52   bool AddClassTranslation(
53       const PackageId& package,
54       StringId obfuscated_class_name,
55       StringId deobfuscated_class_name,
56       base::FlatHashMap<StringId, StringId> obfuscated_to_deobfuscated_members);
57 
58   // These functions return the deobfuscated class/member name from an
59   // obfuscated class/member name.
60   // If a package is not provided, the |default_package_id_| is used.
61   // If translation is not found, returns std::nullopt.
62 
63   std::optional<StringId> TranslateClass(StringId obfuscated_class_name) const;
64 
65   std::optional<StringId> TranslateClass(const PackageId& package,
66                                          StringId obfuscated_class_name) const;
67 
68   std::optional<StringId> TranslateMember(const PackageId& package,
69                                           StringId obfuscated_class_name,
70                                           StringId obfuscated_member) const;
71 
72  private:
73   struct PackageIdHash {
operatorPackageIdHash74     std::size_t operator()(PackageId const& p) const noexcept {
75       return static_cast<std::size_t>(
76           base::Hasher::Combine(p.package_name, p.version_code));
77     }
78   };
79 
80   using ObfuscatedToDeobfuscatedMembers = base::FlatHashMap<StringId, StringId>;
81   struct ClassTranslation {
82     StringId deobfuscated_class_name;
83     ObfuscatedToDeobfuscatedMembers members;
84   };
85 
86   using ObfuscatedClassesToMembers =
87       base::FlatHashMap<StringId, ClassTranslation>;
88   base::FlatHashMap<PackageId, ObfuscatedClassesToMembers, PackageIdHash>
89       class_per_package_translation_;
90 
91   // To translate entities which don't have a package id, we will use
92   // |default_package_id_|. |default_package_id_| is a package id of the first
93   // inserted entity with a package id;
94   // We need this because curently TraceProcessor doesn't use the package
95   // version of the arguments.
96   // TODO(b/244700870): start use the package version of arguments.
97   std::optional<PackageId> default_package_id_;
98 };
99 
100 }  // namespace trace_processor
101 }  // namespace perfetto
102 
103 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_DEOBFUSCATION_MAPPING_TABLE_H_
104