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 #ifndef ART_TOOLS_VERIDEX_HIDDEN_API_FINDER_H_
18 #define ART_TOOLS_VERIDEX_HIDDEN_API_FINDER_H_
19 
20 #include "class_filter.h"
21 #include "dex/method_reference.h"
22 
23 #include <iostream>
24 #include <map>
25 #include <set>
26 #include <string>
27 
28 namespace art {
29 
30 class HiddenApi;
31 struct HiddenApiStats;
32 class VeridexResolver;
33 
34 /**
35  * Reports potential uses of hidden APIs from static linking and reflection.
36  */
37 class HiddenApiFinder {
38  public:
HiddenApiFinder(const HiddenApi & hidden_api)39   explicit HiddenApiFinder(const HiddenApi& hidden_api) : hidden_api_(hidden_api) {}
40 
41   // Iterate over the dex files associated with the passed resolvers to report
42   // hidden API uses.
43   void Run(const std::vector<std::unique_ptr<VeridexResolver>>& app_resolvers,
44            const ClassFilter& app_class_filter);
45 
46   void Dump(std::ostream& os, HiddenApiStats* stats, bool dump_reflection);
47 
48  private:
49   void CollectAccesses(VeridexResolver* resolver, const ClassFilter& class_filter);
50   void CheckMethod(uint32_t method_idx, VeridexResolver* resolver, MethodReference ref);
51   void CheckField(uint32_t field_idx, VeridexResolver* resolver, MethodReference ref);
52   void DumpReferences(std::ostream& os, const std::vector<MethodReference>& references);
53 
54   const HiddenApi& hidden_api_;
55   std::set<std::string> classes_;
56   std::set<std::string> strings_;
57   std::map<std::string, std::vector<MethodReference>> reflection_locations_;
58   std::map<std::string, std::vector<MethodReference>> method_locations_;
59   std::map<std::string, std::vector<MethodReference>> field_locations_;
60 };
61 
62 }  // namespace art
63 
64 #endif  // ART_TOOLS_VERIDEX_HIDDEN_API_FINDER_H_
65