1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "precise_hidden_api_finder.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "class_filter.h"
20*795d594fSAndroid Build Coastguard Worker #include "dex/class_accessor-inl.h"
21*795d594fSAndroid Build Coastguard Worker #include "dex/code_item_accessors-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "dex/dex_instruction-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
24*795d594fSAndroid Build Coastguard Worker #include "dex/method_reference.h"
25*795d594fSAndroid Build Coastguard Worker #include "flow_analysis.h"
26*795d594fSAndroid Build Coastguard Worker #include "hidden_api.h"
27*795d594fSAndroid Build Coastguard Worker #include "resolver.h"
28*795d594fSAndroid Build Coastguard Worker #include "veridex.h"
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker #include <iostream>
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker namespace art {
33*795d594fSAndroid Build Coastguard Worker
RunInternal(const std::vector<std::unique_ptr<VeridexResolver>> & resolvers,const ClassFilter & class_filter,const std::function<void (VeridexResolver *,const ClassAccessor::Method &)> & action)34*795d594fSAndroid Build Coastguard Worker void PreciseHiddenApiFinder::RunInternal(
35*795d594fSAndroid Build Coastguard Worker const std::vector<std::unique_ptr<VeridexResolver>>& resolvers,
36*795d594fSAndroid Build Coastguard Worker const ClassFilter& class_filter,
37*795d594fSAndroid Build Coastguard Worker const std::function<void(VeridexResolver*, const ClassAccessor::Method&)>& action) {
38*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<VeridexResolver>& resolver : resolvers) {
39*795d594fSAndroid Build Coastguard Worker for (ClassAccessor accessor : resolver->GetDexFile().GetClasses()) {
40*795d594fSAndroid Build Coastguard Worker if (class_filter.Matches(accessor.GetDescriptor())) {
41*795d594fSAndroid Build Coastguard Worker for (const ClassAccessor::Method& method : accessor.GetMethods()) {
42*795d594fSAndroid Build Coastguard Worker if (method.GetCodeItem() != nullptr) {
43*795d594fSAndroid Build Coastguard Worker action(resolver.get(), method);
44*795d594fSAndroid Build Coastguard Worker }
45*795d594fSAndroid Build Coastguard Worker }
46*795d594fSAndroid Build Coastguard Worker }
47*795d594fSAndroid Build Coastguard Worker }
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker
AddUsesAt(const std::vector<ReflectAccessInfo> & accesses,MethodReference ref)51*795d594fSAndroid Build Coastguard Worker void PreciseHiddenApiFinder::AddUsesAt(const std::vector<ReflectAccessInfo>& accesses,
52*795d594fSAndroid Build Coastguard Worker MethodReference ref) {
53*795d594fSAndroid Build Coastguard Worker for (const ReflectAccessInfo& info : accesses) {
54*795d594fSAndroid Build Coastguard Worker if (info.IsConcrete()) {
55*795d594fSAndroid Build Coastguard Worker concrete_uses_[ref].push_back(info);
56*795d594fSAndroid Build Coastguard Worker } else {
57*795d594fSAndroid Build Coastguard Worker abstract_uses_[ref].push_back(info);
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker
Run(const std::vector<std::unique_ptr<VeridexResolver>> & resolvers,const ClassFilter & class_filter)62*795d594fSAndroid Build Coastguard Worker void PreciseHiddenApiFinder::Run(const std::vector<std::unique_ptr<VeridexResolver>>& resolvers,
63*795d594fSAndroid Build Coastguard Worker const ClassFilter& class_filter) {
64*795d594fSAndroid Build Coastguard Worker // Collect reflection uses.
65*795d594fSAndroid Build Coastguard Worker RunInternal(resolvers,
66*795d594fSAndroid Build Coastguard Worker class_filter,
67*795d594fSAndroid Build Coastguard Worker [this] (VeridexResolver* resolver, const ClassAccessor::Method& method) {
68*795d594fSAndroid Build Coastguard Worker FlowAnalysisCollector collector(resolver, method);
69*795d594fSAndroid Build Coastguard Worker collector.Run();
70*795d594fSAndroid Build Coastguard Worker AddUsesAt(collector.GetUses(), method.GetReference());
71*795d594fSAndroid Build Coastguard Worker });
72*795d594fSAndroid Build Coastguard Worker
73*795d594fSAndroid Build Coastguard Worker // For non-final reflection uses, do a limited fixed point calculation over the code to try
74*795d594fSAndroid Build Coastguard Worker // substituting them with final reflection uses.
75*795d594fSAndroid Build Coastguard Worker // We limit the number of times we iterate over the code as one run can be long.
76*795d594fSAndroid Build Coastguard Worker static const int kMaximumIterations = 10;
77*795d594fSAndroid Build Coastguard Worker uint32_t i = 0;
78*795d594fSAndroid Build Coastguard Worker while (!abstract_uses_.empty() && (i++ < kMaximumIterations)) {
79*795d594fSAndroid Build Coastguard Worker // Fetch and clear the worklist.
80*795d594fSAndroid Build Coastguard Worker std::map<MethodReference, std::vector<ReflectAccessInfo>> current_uses
81*795d594fSAndroid Build Coastguard Worker = std::move(abstract_uses_);
82*795d594fSAndroid Build Coastguard Worker RunInternal(resolvers,
83*795d594fSAndroid Build Coastguard Worker class_filter,
84*795d594fSAndroid Build Coastguard Worker [this, current_uses] (VeridexResolver* resolver,
85*795d594fSAndroid Build Coastguard Worker const ClassAccessor::Method& method) {
86*795d594fSAndroid Build Coastguard Worker FlowAnalysisSubstitutor substitutor(resolver, method, current_uses);
87*795d594fSAndroid Build Coastguard Worker substitutor.Run();
88*795d594fSAndroid Build Coastguard Worker AddUsesAt(substitutor.GetUses(), method.GetReference());
89*795d594fSAndroid Build Coastguard Worker });
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker
Dump(std::ostream & os,HiddenApiStats * stats)93*795d594fSAndroid Build Coastguard Worker void PreciseHiddenApiFinder::Dump(std::ostream& os, HiddenApiStats* stats) {
94*795d594fSAndroid Build Coastguard Worker static const char* kPrefix = " ";
95*795d594fSAndroid Build Coastguard Worker std::map<std::string, std::vector<MethodReference>> named_uses;
96*795d594fSAndroid Build Coastguard Worker for (auto& it : concrete_uses_) {
97*795d594fSAndroid Build Coastguard Worker MethodReference ref = it.first;
98*795d594fSAndroid Build Coastguard Worker for (const ReflectAccessInfo& info : it.second) {
99*795d594fSAndroid Build Coastguard Worker std::string cls(info.cls.ToString());
100*795d594fSAndroid Build Coastguard Worker std::string name(info.name.ToString());
101*795d594fSAndroid Build Coastguard Worker std::string full_name = ART_FORMAT("{}->{}", cls, name);
102*795d594fSAndroid Build Coastguard Worker named_uses[full_name].push_back(ref);
103*795d594fSAndroid Build Coastguard Worker }
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker
106*795d594fSAndroid Build Coastguard Worker for (auto& it : named_uses) {
107*795d594fSAndroid Build Coastguard Worker const std::string& full_name = it.first;
108*795d594fSAndroid Build Coastguard Worker if (hidden_api_.GetSignatureSource(full_name) != SignatureSource::APP &&
109*795d594fSAndroid Build Coastguard Worker hidden_api_.ShouldReport(full_name)) {
110*795d594fSAndroid Build Coastguard Worker stats->reflection_count++;
111*795d594fSAndroid Build Coastguard Worker hiddenapi::ApiList api_list = hidden_api_.GetApiList(full_name);
112*795d594fSAndroid Build Coastguard Worker stats->api_counts[api_list.GetIntValue()]++;
113*795d594fSAndroid Build Coastguard Worker os << "#" << ++stats->count << ": Reflection " << api_list << " " << full_name << " use(s):";
114*795d594fSAndroid Build Coastguard Worker os << std::endl;
115*795d594fSAndroid Build Coastguard Worker for (const MethodReference& ref : it.second) {
116*795d594fSAndroid Build Coastguard Worker os << kPrefix << HiddenApi::GetApiMethodName(ref) << std::endl;
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker os << std::endl;
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker }
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker
123*795d594fSAndroid Build Coastguard Worker } // namespace art
124