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