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