1 /*
2 * Copyright (C) 2018 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 "precise_hidden_api_finder.h"
18
19 #include "dex/code_item_accessors-inl.h"
20 #include "dex/dex_instruction-inl.h"
21 #include "dex/dex_file.h"
22 #include "dex/method_reference.h"
23 #include "flow_analysis.h"
24 #include "hidden_api.h"
25 #include "resolver.h"
26 #include "veridex.h"
27
28 #include <iostream>
29
30 namespace art {
31
RunInternal(const std::vector<std::unique_ptr<VeridexResolver>> & resolvers,const std::function<void (VeridexResolver *,const ClassDataItemIterator &)> & action)32 void PreciseHiddenApiFinder::RunInternal(
33 const std::vector<std::unique_ptr<VeridexResolver>>& resolvers,
34 const std::function<void(VeridexResolver*, const ClassDataItemIterator&)>& action) {
35 for (const std::unique_ptr<VeridexResolver>& resolver : resolvers) {
36 const DexFile& dex_file = resolver->GetDexFile();
37 size_t class_def_count = dex_file.NumClassDefs();
38 for (size_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
39 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
40 const uint8_t* class_data = dex_file.GetClassData(class_def);
41 if (class_data == nullptr) {
42 // Empty class.
43 continue;
44 }
45 ClassDataItemIterator it(dex_file, class_data);
46 it.SkipAllFields();
47 for (; it.HasNextMethod(); it.Next()) {
48 const DexFile::CodeItem* code_item = it.GetMethodCodeItem();
49 if (code_item == nullptr) {
50 continue;
51 }
52 action(resolver.get(), it);
53 }
54 }
55 }
56 }
57
AddUsesAt(const std::vector<ReflectAccessInfo> & accesses,MethodReference ref)58 void PreciseHiddenApiFinder::AddUsesAt(const std::vector<ReflectAccessInfo>& accesses,
59 MethodReference ref) {
60 for (const ReflectAccessInfo& info : accesses) {
61 if (info.IsConcrete()) {
62 concrete_uses_[ref].push_back(info);
63 } else {
64 abstract_uses_[ref].push_back(info);
65 }
66 }
67 }
68
Run(const std::vector<std::unique_ptr<VeridexResolver>> & resolvers)69 void PreciseHiddenApiFinder::Run(const std::vector<std::unique_ptr<VeridexResolver>>& resolvers) {
70 // Collect reflection uses.
71 RunInternal(resolvers, [this] (VeridexResolver* resolver, const ClassDataItemIterator& it) {
72 FlowAnalysisCollector collector(resolver, it);
73 collector.Run();
74 AddUsesAt(collector.GetUses(), MethodReference(&resolver->GetDexFile(), it.GetMemberIndex()));
75 });
76
77 // For non-final reflection uses, do a limited fixed point calculation over the code to try
78 // substituting them with final reflection uses.
79 // We limit the number of times we iterate over the code as one run can be long.
80 static const int kMaximumIterations = 10;
81 uint32_t i = 0;
82 while (!abstract_uses_.empty() && (i++ < kMaximumIterations)) {
83 // Fetch and clear the worklist.
84 std::map<MethodReference, std::vector<ReflectAccessInfo>> current_uses
85 = std::move(abstract_uses_);
86 RunInternal(resolvers,
87 [this, current_uses] (VeridexResolver* resolver, const ClassDataItemIterator& it) {
88 FlowAnalysisSubstitutor substitutor(resolver, it, current_uses);
89 substitutor.Run();
90 AddUsesAt(substitutor.GetUses(),
91 MethodReference(&resolver->GetDexFile(), it.GetMemberIndex()));
92 });
93 }
94 }
95
Dump(std::ostream & os,HiddenApiStats * stats)96 void PreciseHiddenApiFinder::Dump(std::ostream& os, HiddenApiStats* stats) {
97 static const char* kPrefix = " ";
98 std::map<std::string, std::vector<MethodReference>> named_uses;
99 for (auto it : concrete_uses_) {
100 MethodReference ref = it.first;
101 for (const ReflectAccessInfo& info : it.second) {
102 std::string cls(info.cls.ToString());
103 std::string name(info.name.ToString());
104 std::string full_name = cls + "->" + name;
105 HiddenApiAccessFlags::ApiList api_list = hidden_api_.GetApiList(full_name);
106 if (api_list != HiddenApiAccessFlags::kWhitelist) {
107 named_uses[full_name].push_back(ref);
108 }
109 }
110 }
111
112 for (auto it : named_uses) {
113 ++stats->reflection_count;
114 const std::string& full_name = it.first;
115 HiddenApiAccessFlags::ApiList api_list = hidden_api_.GetApiList(full_name);
116 stats->api_counts[api_list]++;
117 os << "#" << ++stats->count << ": Reflection " << api_list << " " << full_name << " use(s):";
118 os << std::endl;
119 for (const MethodReference& ref : it.second) {
120 os << kPrefix << HiddenApi::GetApiMethodName(ref) << std::endl;
121 }
122 os << std::endl;
123 }
124 }
125
126 } // namespace art
127