-
Notifications
You must be signed in to change notification settings - Fork 852
Support parsing mergedClasses metadata to better de-obfuscate heap dumps. #6911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MichaelFQuigley
wants to merge
5
commits into
main
Choose a base branch
from
dev/michaelquigley/dev/michaelquigley/deobfuscator-merged-classes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e765cd1
tp: Make simple_json_parser header-only
MichaelFQuigley daec53f
tp: Add support for R8 merged classes in deobfuscator
MichaelFQuigley 19dc68e
Merge branch 'main' into dev/michaelquigley/dev/michaelquigley/deobfu…
MichaelFQuigley 2603c68
Update simple_json_parser to be a perfetto_component to address ODR i…
MichaelFQuigley 639bdcd
Update Android.bp
MichaelFQuigley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
|
|
||
| #include "src/trace_processor/util/deobfuscation/deobfuscator.h" | ||
| #include "src/trace_processor/util/simple_json_parser.h" | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
|
|
@@ -25,6 +26,7 @@ | |
| #include "perfetto/ext/base/file_utils.h" | ||
| #include "perfetto/ext/base/scoped_file.h" | ||
| #include "perfetto/ext/base/string_splitter.h" | ||
| #include "perfetto/ext/base/string_utils.h" | ||
|
|
||
| #include "perfetto/protozero/scattered_heap_buffer.h" | ||
| #include "protos/perfetto/trace/profiling/deobfuscation.pbzero.h" | ||
|
|
@@ -35,6 +37,18 @@ namespace perfetto { | |
| namespace profiling { | ||
| namespace { | ||
|
|
||
| using SimpleJsonParser = perfetto::trace_processor::json::SimpleJsonParser; | ||
| using FieldResult = perfetto::trace_processor::json::FieldResult; | ||
|
|
||
| // Json keys used to parse merged classes. | ||
| constexpr std::string_view kMergedClassesId = | ||
| "com.android.tools.r8.mergedClasses"; | ||
| constexpr std::string_view kIdKey = "id"; | ||
| constexpr std::string_view kClassNameKey = "name"; | ||
| constexpr std::string_view kClassIdKey = "class_id"; | ||
| constexpr std::string_view kClassIdFieldKey = "class_id_field"; | ||
| constexpr std::string_view kMergedClassesKey = "merged_classes"; | ||
|
|
||
| struct ProguardClass { | ||
| std::string obfuscated_name; | ||
| std::string deobfuscated_name; | ||
|
|
@@ -78,6 +92,107 @@ std::optional<ProguardClass> ParseClass(std::string line) { | |
| std::move(deobfuscated_name)}; | ||
| } | ||
|
|
||
| // Forward declaration. | ||
| base::Status ParseMergedClass(SimpleJsonParser& parser, | ||
| ObfuscatedClass::MergedClass& out); | ||
|
|
||
| // Parses common fields for MergedClasses (class_id_field and merged_classes). | ||
| FieldResult ParseMergedClassesField(SimpleJsonParser& parser, | ||
| std::string_view key, | ||
| ObfuscatedClass::MergedClasses& mcs) { | ||
| if (key == kClassIdFieldKey) { | ||
| if (auto val = parser.GetString()) { | ||
| mcs.class_id_field_name = std::string(*val); | ||
| } else { | ||
| return base::Status("Expected class id field name."); | ||
| } | ||
| return FieldResult::Handled{}; | ||
| } | ||
| if (key == kMergedClassesKey) { | ||
| if (!parser.IsArray()) { | ||
| return base::Status("Expected array for merged_classes."); | ||
| } | ||
| base::Status array_status = | ||
| parser.ForEachArrayElement([&]() -> base::Status { | ||
| ObfuscatedClass::MergedClass mc; | ||
| RETURN_IF_ERROR(ParseMergedClass(parser, mc)); | ||
| mcs.merged_classes.push_back(std::move(mc)); | ||
| return base::OkStatus(); | ||
| }); | ||
| return FieldResult(array_status); | ||
| } | ||
| return FieldResult::Skip{}; | ||
| } | ||
|
|
||
| // Parses a single MergedClass entry from R8 mergedClasses JSON. | ||
| base::Status ParseMergedClass(SimpleJsonParser& parser, | ||
| ObfuscatedClass::MergedClass& out) { | ||
| return parser.ForEachField([&](std::string_view key) -> FieldResult { | ||
| if (key == kClassNameKey) { | ||
| if (auto val = parser.GetString()) { | ||
| out.name = std::string(*val); | ||
| } else { | ||
| return base::Status("Expected class name."); | ||
| } | ||
| return FieldResult::Handled{}; | ||
| } | ||
| if (key == kClassIdKey) { | ||
| if (auto val_str = parser.GetString()) { | ||
| if (auto parsed_val = base::StringViewToInt32(*val_str)) { | ||
| out.class_id = *parsed_val; | ||
| } else { | ||
| return base::Status("Invalid class id format in string."); | ||
| } | ||
| } else if (auto val_int = parser.GetInt64(); | ||
| val_int.has_value() && | ||
| val_int.value() >= std::numeric_limits<int32_t>::min() && | ||
| val_int.value() <= std::numeric_limits<int32_t>::max()) { | ||
| out.class_id = static_cast<int32_t>(*val_int); | ||
| } else { | ||
| return base::Status("Expected class id."); | ||
| } | ||
| return FieldResult::Handled{}; | ||
| } | ||
| FieldResult res = | ||
| ParseMergedClassesField(parser, key, out.nested_merged_classes); | ||
| if (!res.handled) { | ||
| PERFETTO_DLOG("Unknown field in merged class JSON: %.*s", | ||
| static_cast<int>(key.size()), key.data()); | ||
| } | ||
| return res; | ||
| }); | ||
| } | ||
|
|
||
| // Parses R8 `com.android.tools.r8.mergedClasses` JSON comment string in | ||
| // Proguard mapping. | ||
| base::Status ParseMergedClassesComment(std::string_view json_str, | ||
| ObfuscatedClass& target_class) { | ||
| SimpleJsonParser parser(json_str); | ||
| RETURN_IF_ERROR(parser.Parse()); | ||
|
|
||
| bool is_merged_classes_id = false; | ||
| ObfuscatedClass::MergedClasses mcs; | ||
|
|
||
| base::Status s = | ||
| parser.ForEachField([&](std::string_view key) -> FieldResult { | ||
| if (key == kIdKey) { | ||
| if (auto val = parser.GetString()) { | ||
| if (*val == kMergedClassesId) { | ||
| is_merged_classes_id = true; | ||
| } | ||
| } | ||
| return FieldResult::Handled{}; | ||
| } | ||
| return ParseMergedClassesField(parser, key, mcs); | ||
| }); | ||
| RETURN_IF_ERROR(s); | ||
|
|
||
| if (is_merged_classes_id) { | ||
| *target_class.mutable_merged_classes() = std::move(mcs); | ||
| } | ||
| return base::OkStatus(); | ||
| } | ||
|
|
||
| enum class ProguardMemberType { | ||
| kField, | ||
| kMethod, | ||
|
|
@@ -332,9 +447,25 @@ std::map<std::string, std::string> ObfuscatedClass::deobfuscated_methods() | |
| // file format we are parsing. | ||
| base::Status ProguardParser::AddLine(std::string line) { | ||
| auto first_ch_pos = line.find_first_not_of(" \t"); | ||
| if (first_ch_pos == std::string::npos || line[first_ch_pos] == '#') | ||
| if (first_ch_pos == std::string::npos) | ||
| return base::Status(); | ||
|
|
||
| if (line[first_ch_pos] == '#') { | ||
| if (current_class_ == nullptr) { | ||
| return base::Status(); | ||
| } | ||
| size_t json_start = line.find('{'); | ||
| if (json_start != std::string::npos) { | ||
| std::string_view json_sv = std::string_view(line).substr(json_start); | ||
| base::Status s = ParseMergedClassesComment(json_sv, *current_class_); | ||
| if (!s.ok()) { | ||
| PERFETTO_DLOG("Failed to parse merged classes comment: %s\non line %s", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, push a |
||
| s.message().c_str(), line.c_str()); | ||
| } | ||
| } | ||
| return base::Status(); | ||
| } | ||
|
|
||
| bool is_member = line[0] == ' '; | ||
| if (is_member && !current_class_) { | ||
| return base::Status( | ||
|
|
@@ -405,6 +536,35 @@ bool ProguardParser::AddLines(std::string contents) { | |
| return true; | ||
| } | ||
|
|
||
| static void SerializeMergedClasses( | ||
| const profiling::ObfuscatedClass::MergedClasses& src, | ||
| perfetto::protos::pbzero::ObfuscatedClass::MergedClasses* dest) { | ||
| if (!src.class_id_field_name.empty()) { | ||
| dest->set_class_id_field_name(src.class_id_field_name); | ||
| } | ||
| for (const auto& mc : src.merged_classes) { | ||
| auto* dest_mc = dest->add_merged_classes(); | ||
| if (!mc.name.empty()) { | ||
| dest_mc->set_name(mc.name); | ||
| } | ||
| if (mc.class_id.has_value()) { | ||
| dest_mc->set_class_id(*mc.class_id); | ||
| } | ||
| if (!mc.nested_merged_classes.merged_classes.empty()) { | ||
| SerializeMergedClasses(mc.nested_merged_classes, | ||
| dest_mc->set_merged_classes()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void SerializeTopLevelMergedClasses( | ||
| const profiling::ObfuscatedClass::MergedClasses& src, | ||
| perfetto::protos::pbzero::ObfuscatedClass* dest) { | ||
| if (!src.merged_classes.empty()) { | ||
| SerializeMergedClasses(src, dest->set_merged_classes()); | ||
| } | ||
| } | ||
|
|
||
| void MakeDeobfuscationPackets( | ||
| const std::string& package_name, | ||
| const std::map<std::string, profiling::ObfuscatedClass>& mapping, | ||
|
|
@@ -447,6 +607,7 @@ void MakeDeobfuscationPackets( | |
| proto_member->set_source_line_end(*method.source_line_end); | ||
| } | ||
| } | ||
| SerializeTopLevelMergedClasses(cls.merged_classes(), proto_class); | ||
| } | ||
| callback(trace.SerializeAsString()); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There can be other metadata with this prefix
# {.So, trying to parse this json can be costly. What about a early substring check for the
kMergedClassesId?