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_PRECISE_HIDDEN_API_FINDER_H_
18 #define ART_TOOLS_VERIDEX_PRECISE_HIDDEN_API_FINDER_H_
19 
20 #include "dex/method_reference.h"
21 #include "flow_analysis.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 known uses of hidden APIs from reflection.
36  */
37 class PreciseHiddenApiFinder {
38  public:
PreciseHiddenApiFinder(const HiddenApi & hidden_api)39   explicit PreciseHiddenApiFinder(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 
45   void Dump(std::ostream& os, HiddenApiStats* stats);
46 
47  private:
48   // Run over all methods of all dex files, and call `action` on each.
49   void RunInternal(
50       const std::vector<std::unique_ptr<VeridexResolver>>& resolvers,
51       const std::function<void(VeridexResolver*, const ClassAccessor::Method&)>& action);
52 
53   // Add uses found in method `ref`.
54   void AddUsesAt(const std::vector<ReflectAccessInfo>& accesses, MethodReference ref);
55 
56   const HiddenApi& hidden_api_;
57 
58   std::map<MethodReference, std::vector<ReflectAccessInfo>> concrete_uses_;
59   std::map<MethodReference, std::vector<ReflectAccessInfo>> abstract_uses_;
60 };
61 
62 }  // namespace art
63 
64 #endif  // ART_TOOLS_VERIDEX_PRECISE_HIDDEN_API_FINDER_H_
65