Skip to content

Commit 2ae9fb1

Browse files
Nikolai Tillmannfacebook-github-bot
authored andcommitted
Some conversions to UnorderedSet
Summary: This diff converts around 30% of the `std::unordered_set` references in the codebase. Reviewed By: agampe Differential Revision: D72061757 fbshipit-source-id: d2b06719b98645b9a882832b2c408464a80ce463
1 parent cec43d2 commit 2ae9fb1

File tree

118 files changed

+843
-883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+843
-883
lines changed

libredex/AnnoUtils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
#pragma once
99

10+
#include "DeterministicContainers.h"
1011
#include "DexAnnotation.h"
1112
#include "DexClass.h"
1213
#include "DexUtil.h"
1314

14-
#include <unordered_set>
15-
1615
/**
1716
* Parses the default value of an annotation given the annotation and the
1817
* element name.
@@ -88,7 +87,7 @@ DexAnnotation* get_annotation(const DexMember* member, DexType* anno_type) {
8887

8988
template <class DexMember>
9089
bool has_any_annotation(DexMember* member,
91-
const std::unordered_set<DexType*>& anno_types) {
90+
const UnorderedSet<DexType*>& anno_types) {
9291
const auto& annos = member->get_anno_set();
9392
if (annos == nullptr) {
9493
return false;

libredex/ApkResources.cpp

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ std::string read_attribute_name_at_idx(const android::ResXMLTree& parser,
924924
void extract_classes_from_layout(
925925
const char* data,
926926
size_t size,
927-
const std::unordered_set<std::string>& attributes_to_read,
927+
const UnorderedSet<std::string>& attributes_to_read,
928928
resources::StringOrReferenceSet* out_classes,
929929
std::unordered_multimap<std::string, resources::StringOrReference>*
930930
out_attributes) {
@@ -1004,7 +1004,7 @@ void extract_classes_from_layout(
10041004

10051005
void ApkResources::collect_layout_classes_and_attributes_for_file(
10061006
const std::string& file_path,
1007-
const std::unordered_set<std::string>& attributes_to_read,
1007+
const UnorderedSet<std::string>& attributes_to_read,
10081008
resources::StringOrReferenceSet* out_classes,
10091009
std::unordered_multimap<std::string, resources::StringOrReference>*
10101010
out_attributes) {
@@ -1031,7 +1031,7 @@ class XmlStringAttributeCollector : public arsc::SimpleXmlParser {
10311031
return true;
10321032
}
10331033

1034-
std::unordered_set<std::string> m_utf8s_values;
1034+
UnorderedSet<std::string> m_utf8s_values;
10351035
};
10361036

10371037
class XmlElementCollector : public arsc::SimpleXmlParser {
@@ -1063,7 +1063,7 @@ class XmlElementCollector : public arsc::SimpleXmlParser {
10631063
return arsc::SimpleXmlParser::visit_start_tag(node, extension);
10641064
}
10651065

1066-
std::unordered_set<std::string> m_element_names;
1066+
UnorderedSet<std::string> m_element_names;
10671067
};
10681068

10691069
// Rewrites node, extension, and attribute list to transform encountered
@@ -1176,13 +1176,12 @@ class NodeAttributeTransformer : public XmlElementCollector {
11761176
} // namespace
11771177

11781178
void ApkResources::collect_xml_attribute_string_values_for_file(
1179-
const std::string& file_path, std::unordered_set<std::string>* out) {
1179+
const std::string& file_path, UnorderedSet<std::string>* out) {
11801180
redex::read_file_with_contents(file_path, [&](const char* data, size_t size) {
11811181
if (arsc::is_binary_xml(data, size)) {
11821182
XmlStringAttributeCollector collector;
11831183
if (collector.visit((void*)data, size)) {
1184-
out->insert(collector.m_utf8s_values.begin(),
1185-
collector.m_utf8s_values.end());
1184+
insert_unordered_iterable(*out, collector.m_utf8s_values);
11861185
}
11871186
}
11881187
});
@@ -1346,13 +1345,13 @@ boost::optional<std::string> ApkResources::get_manifest_package_name() {
13461345
});
13471346
}
13481347

1349-
std::unordered_set<std::string> ApkResources::get_service_loader_classes() {
1348+
UnorderedSet<std::string> ApkResources::get_service_loader_classes() {
13501349
const std::string meta_inf =
13511350
(boost::filesystem::path(m_directory) / "META-INF/services/").string();
13521351
return get_service_loader_classes_helper(meta_inf);
13531352
}
13541353

1355-
std::unordered_set<uint32_t> ApkResources::get_xml_reference_attributes(
1354+
UnorderedSet<uint32_t> ApkResources::get_xml_reference_attributes(
13561355
const std::string& filename) {
13571356
if (is_raw_resource(filename)) {
13581357
return {};
@@ -1421,13 +1420,11 @@ bool ApkResources::rename_classes_in_layout(
14211420
return true;
14221421
}
14231422

1424-
std::unordered_set<std::string> ApkResources::find_all_xml_files() {
1423+
UnorderedSet<std::string> ApkResources::find_all_xml_files() {
14251424
std::string manifest_path = m_directory + "/AndroidManifest.xml";
1426-
std::unordered_set<std::string> all_xml_files;
1425+
UnorderedSet<std::string> all_xml_files;
14271426
all_xml_files.emplace(manifest_path);
1428-
for (const std::string& path : get_xml_files(m_directory + "/res")) {
1429-
all_xml_files.emplace(path);
1430-
}
1427+
insert_unordered_iterable(all_xml_files, get_xml_files(m_directory + "/res"));
14311428
return all_xml_files;
14321429
}
14331430

@@ -1459,7 +1456,7 @@ namespace {
14591456

14601457
void obfuscate_xml_attributes(
14611458
const std::string& filename,
1462-
const std::unordered_set<std::string>& do_not_obfuscate_elements) {
1459+
const UnorderedSet<std::string>& do_not_obfuscate_elements) {
14631460
auto file = RedexMappedFile::open(filename, false);
14641461
apk::XmlFileEditor editor;
14651462
always_assert_log(editor.visit((void*)file.data(), file.size()),
@@ -1498,8 +1495,8 @@ void obfuscate_xml_attributes(
14981495
} // namespace
14991496

15001497
void ApkResources::obfuscate_xml_files(
1501-
const std::unordered_set<std::string>& allowed_types,
1502-
const std::unordered_set<std::string>& do_not_obfuscate_elements) {
1498+
const UnorderedSet<std::string>& allowed_types,
1499+
const UnorderedSet<std::string>& do_not_obfuscate_elements) {
15031500
using path_t = boost::filesystem::path;
15041501
using dir_iterator = boost::filesystem::directory_iterator;
15051502

@@ -1513,9 +1510,7 @@ void ApkResources::obfuscate_xml_files(
15131510
// TODO(T126661220): support obfuscated input.
15141511
if (is_directory(entry_path) &&
15151512
can_obfuscate_xml_file(allowed_types, entry_string)) {
1516-
for (const std::string& layout : get_xml_files(entry_string)) {
1517-
xml_paths.emplace(layout);
1518-
}
1513+
insert_unordered_iterable(xml_paths, get_xml_files(entry_string));
15191514
}
15201515
}
15211516
}
@@ -1886,13 +1881,14 @@ void rebuild_string_pool(
18861881

18871882
// Given the kept strings, build the mapping from old -> new in the projected
18881883
// new string pool.
1889-
void project_string_mapping(const std::unordered_set<uint32_t>& used_strings,
1884+
void project_string_mapping(const UnorderedSet<uint32_t>& used_strings,
18901885
const android::ResStringPool& string_pool,
18911886
UnorderedMap<uint32_t, uint32_t>* kept_old_to_new,
18921887
bool sort_by_string_value = false) {
18931888
always_assert(kept_old_to_new->empty());
18941889

1895-
std::vector<uint32_t> used_indices(used_strings.begin(), used_strings.end());
1890+
std::vector<uint32_t> used_indices;
1891+
insert_unordered_iterable(used_indices, used_indices.end(), used_strings);
18961892
if (!sort_by_string_value) {
18971893
std::sort(used_indices.begin(), used_indices.end());
18981894
} else {
@@ -1962,7 +1958,7 @@ void ResourcesArscFile::finalize_resource_table(const ResourceConfig& config) {
19621958
// 1) Collect all referenced global string indicies and key string indicies.
19631959
PackageStringRefCollector collector;
19641960
collector.visit(m_f.data(), m_arsc_len);
1965-
std::unordered_set<uint32_t> used_global_strings;
1961+
UnorderedSet<uint32_t> used_global_strings;
19661962
for (const auto& value : collector.m_values) {
19671963
used_global_strings.emplace(dtohl(value->data));
19681964
}
@@ -1990,7 +1986,7 @@ void ResourcesArscFile::finalize_resource_table(const ResourceConfig& config) {
19901986
// 4) Actually build the new global ResStringPool. While doing this, remap all
19911987
// span refs encountered (in case ResStringPool has copied its underlying
19921988
// data).
1993-
std::unordered_set<android::ResStringPool_span*> remapped_spans;
1989+
UnorderedSet<android::ResStringPool_span*> remapped_spans;
19941990
auto remap_spans = [&global_old_to_new,
19951991
&remapped_spans](android::ResStringPool_span* span) {
19961992
// Guard against span offsets that have been "canonicalized"
@@ -2023,7 +2019,7 @@ void ResourcesArscFile::finalize_resource_table(const ResourceConfig& config) {
20232019
refs.insert(package_type_entries.begin(), package_type_entries.end());
20242020
}
20252021
auto key_string_pool = collector.m_package_key_strings.at(package);
2026-
std::unordered_set<uint32_t> used_key_strings;
2022+
UnorderedSet<uint32_t> used_key_strings;
20272023
for (auto& ref : refs) {
20282024
used_key_strings.emplace(dtohl(ref->index));
20292025
}
@@ -2159,9 +2155,9 @@ void rebuild_string_pool_with_addition(
21592155
size_t ResourcesArscFile::obfuscate_resource_and_serialize(
21602156
const std::vector<std::string>& /* unused */,
21612157
const std::map<std::string, std::string>& filepath_old_to_new,
2162-
const std::unordered_set<uint32_t>& allowed_types,
2163-
const std::unordered_set<std::string>& keep_resource_prefixes,
2164-
const std::unordered_set<std::string>& keep_resource_specific) {
2158+
const UnorderedSet<uint32_t>& allowed_types,
2159+
const UnorderedSet<std::string>& keep_resource_prefixes,
2160+
const UnorderedSet<std::string>& keep_resource_specific) {
21652161
arsc::ResTableBuilder table_builder;
21662162

21672163
// Find the global string pool and read its settings.
@@ -2261,11 +2257,9 @@ size_t ResourcesArscFile::obfuscate_resource_and_serialize(
22612257
std::string old_string =
22622258
arsc::get_string_from_pool(*key_string_pool, old);
22632259
if (keep_resource_specific.count(old_string) > 0 ||
2264-
std::find_if(keep_resource_prefixes.begin(),
2265-
keep_resource_prefixes.end(),
2266-
[&](const std::string& v) {
2267-
return old_string.find(v) == 0;
2268-
}) != keep_resource_prefixes.end()) {
2260+
unordered_any_of(keep_resource_prefixes, [&](const std::string& v) {
2261+
return old_string.find(v) == 0;
2262+
})) {
22692263
// Resource name matches block criteria; don't change the name.
22702264
continue;
22712265
}
@@ -2350,8 +2344,8 @@ uint64_t ResourcesArscFile::resource_value_count(uint32_t res_id) {
23502344
void ResourcesArscFile::walk_references_for_resource(
23512345
uint32_t resID,
23522346
ResourcePathType path_type,
2353-
std::unordered_set<uint32_t>* nodes_visited,
2354-
std::unordered_set<std::string>* potential_file_paths) {
2347+
UnorderedSet<uint32_t>* nodes_visited,
2348+
UnorderedSet<std::string>* potential_file_paths) {
23552349
if (nodes_visited->find(resID) != nodes_visited->end()) {
23562350
return;
23572351
}
@@ -2508,7 +2502,8 @@ size_t ResourcesArscFile::serialize() {
25082502
for (auto& type_info : type_infos) {
25092503
auto type_builder = std::make_shared<arsc::ResTableTypeProjector>(
25102504
package_id, type_info.spec, type_info.configs);
2511-
type_builder->remove_ids(m_ids_to_remove, m_nullify_removed);
2505+
type_builder->remove_ids(unordered_unsafe_unwrap(m_ids_to_remove),
2506+
m_nullify_removed);
25122507
package_builder->add_type(type_builder);
25132508
}
25142509
// Append any new types
@@ -2654,8 +2649,8 @@ class EntryRemapper : public arsc::ResourceTableVisitor {
26542649
const std::map<uint32_t, uint32_t>& m_old_to_new;
26552650
// Tolerate a "canonicalized" version of a type, to make sure we don't double
26562651
// remap.
2657-
std::unordered_set<android::Res_value*> m_seen_values;
2658-
std::unordered_set<android::ResTable_ref*> m_seen_refs;
2652+
UnorderedSet<android::Res_value*> m_seen_values;
2653+
UnorderedSet<android::ResTable_ref*> m_seen_refs;
26592654
};
26602655
} // namespace
26612656

@@ -2672,13 +2667,13 @@ void ResourcesArscFile::get_type_names(std::vector<std::string>* type_names) {
26722667
table_snapshot.get_type_names(APPLICATION_PACKAGE, type_names);
26732668
}
26742669

2675-
std::unordered_set<uint32_t> ResourcesArscFile::get_types_by_name(
2676-
const std::unordered_set<std::string>& type_names) {
2670+
UnorderedSet<uint32_t> ResourcesArscFile::get_types_by_name(
2671+
const UnorderedSet<std::string>& type_names) {
26772672
auto& table_snapshot = get_table_snapshot();
26782673
std::vector<std::string> all_types;
26792674
table_snapshot.get_type_names(APPLICATION_PACKAGE, &all_types);
26802675

2681-
std::unordered_set<uint32_t> type_ids;
2676+
UnorderedSet<uint32_t> type_ids;
26822677
for (size_t i = 0; i < all_types.size(); ++i) {
26832678
if (type_names.count(all_types.at(i)) == 1) {
26842679
type_ids.emplace((i + 1) << TYPE_INDEX_BIT_SHIFT);
@@ -2687,19 +2682,18 @@ std::unordered_set<uint32_t> ResourcesArscFile::get_types_by_name(
26872682
return type_ids;
26882683
}
26892684

2690-
std::unordered_set<uint32_t> ResourcesArscFile::get_types_by_name_prefixes(
2691-
const std::unordered_set<std::string>& type_name_prefixes) {
2685+
UnorderedSet<uint32_t> ResourcesArscFile::get_types_by_name_prefixes(
2686+
const UnorderedSet<std::string>& type_name_prefixes) {
26922687
auto& table_snapshot = get_table_snapshot();
26932688
std::vector<std::string> all_types;
26942689
table_snapshot.get_type_names(APPLICATION_PACKAGE, &all_types);
26952690

2696-
std::unordered_set<uint32_t> type_ids;
2691+
UnorderedSet<uint32_t> type_ids;
26972692
for (size_t i = 0; i < all_types.size(); ++i) {
26982693
const auto& type_name = all_types.at(i);
2699-
if (std::find_if(type_name_prefixes.begin(), type_name_prefixes.end(),
2700-
[&](const std::string& prefix) {
2701-
return type_name.find(prefix) == 0;
2702-
}) != type_name_prefixes.end()) {
2694+
if (unordered_any_of(type_name_prefixes, [&](const std::string& prefix) {
2695+
return type_name.find(prefix) == 0;
2696+
})) {
27032697
type_ids.emplace((i + 1) << TYPE_INDEX_BIT_SHIFT);
27042698
}
27052699
}
@@ -2712,7 +2706,7 @@ namespace {
27122706
// references encountered will be resolved and handled recursively.
27132707
void resolve_string_index_for_id(apk::TableSnapshot& table_snapshot,
27142708
uint32_t id,
2715-
std::unordered_set<uint32_t>* seen,
2709+
UnorderedSet<uint32_t>* seen,
27162710
std::set<uint32_t>* out_idx) {
27172711
// Annoyingly, Android build tools allow references to have cycles in them
27182712
// without failing at build time. At runtime, such a situation would just loop
@@ -2742,7 +2736,7 @@ std::vector<std::string> ResourcesArscFile::get_resource_strings_by_name(
27422736
auto& table_snapshot = get_table_snapshot();
27432737
auto it = name_to_ids.find(res_name);
27442738
if (it != name_to_ids.end()) {
2745-
std::unordered_set<uint32_t> seen;
2739+
UnorderedSet<uint32_t> seen;
27462740
std::set<uint32_t> string_idx;
27472741
for (uint32_t id : it->second) {
27482742
resolve_string_index_for_id(table_snapshot, id, &seen, &string_idx);
@@ -2760,7 +2754,7 @@ std::vector<std::string> ResourcesArscFile::get_resource_strings_by_name(
27602754
void ResourcesArscFile::resolve_string_values_for_resource_reference(
27612755
uint32_t ref, std::vector<std::string>* values) {
27622756
auto& table_snapshot = get_table_snapshot();
2763-
std::unordered_set<uint32_t> seen;
2757+
UnorderedSet<uint32_t> seen;
27642758
std::set<uint32_t> string_idx;
27652759
resolve_string_index_for_id(table_snapshot, ref, &seen, &string_idx);
27662760
for (const auto& i : string_idx) {
@@ -2868,7 +2862,7 @@ ResourcesArscFile::get_inlinable_resource_values() {
28682862
return inlinable_resources;
28692863
}
28702864

2871-
std::unordered_set<uint32_t> ResourcesArscFile::get_overlayable_id_roots() {
2865+
UnorderedSet<uint32_t> ResourcesArscFile::get_overlayable_id_roots() {
28722866
auto& table_snapshot = get_table_snapshot();
28732867
auto& parsed_table = table_snapshot.get_parsed_table();
28742868
return parsed_table.m_overlayable_ids;

0 commit comments

Comments
 (0)