Skip to content

Commit

Permalink
Avoid parsing non-binary XML resource files
Browse files Browse the repository at this point in the history
Summary:
Helper methods associated with resources assume that all XML files found in the APK will be the binary XML format as used by aapt.

This is not the case for "raw" XML files as pointed out in facebook#300 which could be a stand-alone XML file that the app parses manually at runtime. This crashes the resources parsing code here.

A "raw" XML file won't contain any resource references (thus making it uninteresting for many of the `RedexResources.cpp` helper methods), and we'll consider any class name in such a file out of scope for the renamer.

Reviewed By: justinjhendrick

Differential Revision: D7188390

fbshipit-source-id: 46b8c4645e5ccc39d4076b7938ac139c4acd2a5c
  • Loading branch information
wsanville authored and facebook-github-bot committed Mar 8, 2018
1 parent 4e94393 commit 4a2f76f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libredex/RedexResources.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ std::unordered_set<std::string> get_xml_files(
const std::string& directory);
std::unordered_set<uint32_t> get_xml_reference_attributes(
const std::string& filename);
// Checks if the file is in a res/raw folder. Such a file won't be considered
// for resource remapping, class name extraction, etc. These files don't follow
// binary XML format, and thus are out of scope for many optimizations.
bool is_raw_resource(const std::string& filename);
int inline_xml_reference_attributes(
const std::string& filename,
const std::map<uint32_t, android::Res_value>& id_to_inline_value);
Expand Down
12 changes: 12 additions & 0 deletions libresource/RedexResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,17 @@ void ensure_file_contents(
}
}

bool is_raw_resource(const std::string& filename) {
return filename.find("/res/raw/") != std::string::npos
|| filename.find("/res/raw-") != std::string::npos;
}

std::unordered_set<uint32_t> get_xml_reference_attributes(
const std::string& filename) {
if (is_raw_resource(filename)) {
std::unordered_set<uint32_t> empty;
return empty;
}
std::string file_contents = read_entire_file(filename);
ensure_file_contents(file_contents, filename);
return extract_xml_reference_attributes(file_contents, filename);
Expand Down Expand Up @@ -673,6 +682,9 @@ int inline_xml_reference_attributes(
void remap_xml_reference_attributes(
const std::string& filename,
const std::map<uint32_t, uint32_t>& kept_to_remapped_ids) {
if (is_raw_resource(filename)) {
return;
}
std::string file_contents = read_entire_file(filename);
ensure_file_contents(file_contents, filename);
bool made_change = false;
Expand Down
3 changes: 3 additions & 0 deletions opt/renameclasses/RenameClassesV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,9 @@ void RenameClassesPassV2::rename_classes_in_layouts(
size_t num_layout_renamed = 0;
auto xml_files = get_xml_files(m_apk_dir + "/res");
for (const auto& path : xml_files) {
if (is_raw_resource(path)) {
continue;
}
size_t num_renamed;
ssize_t out_delta;
TRACE(RENAME, 5, "Begin rename Views in layout %s\n", path.c_str());
Expand Down

0 comments on commit 4a2f76f

Please sign in to comment.