1 /*
2  * Copyright 2017 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 __VTS_PROTO_FUZZER_RUNNER_H_
18 #define __VTS_PROTO_FUZZER_RUNNER_H_
19 
20 #include "ProtoFuzzerStats.h"
21 #include "ProtoFuzzerUtils.h"
22 
23 #include <memory>
24 
25 namespace android {
26 namespace vts {
27 namespace fuzzer {
28 
29 // Describes a HIDL HAL interface.
30 struct IfaceDesc {
31   // VTS spec of the interface.
32   const CompSpec *comp_spec_;
33   // Handle to an interface instance.
34   std::shared_ptr<DriverBase> hal_;
35 };
36 
37 using IfaceDescTbl = std::unordered_map<std::string, IfaceDesc>;
38 
39 // Responsible for issuing function calls to the HAL and keeps track of
40 // HAL-related information, e.g. which interfaces has been opened so far.
41 class ProtoFuzzerRunner {
42  public:
43   explicit ProtoFuzzerRunner(const std::vector<CompSpec> &comp_specs,
44                              const std::string version_iface);
45 
46   // Initializes interface descriptor table by opening the root interface.
47   void Init(const std::string &, bool);
48   // Call every API from call sequence specified by the ExecSpec.
49   void Execute(const ExecSpec &);
50   // Execute the specified interface function call.
51   void Execute(const FuncCall &);
52   // Accessor to interface descriptor table containing currently opened
53   // interfaces.
GetOpenedIfaces()54   const IfaceDescTbl &GetOpenedIfaces() const { return opened_ifaces_; }
55   // Accessor to stats object.
GetStats()56   const ProtoFuzzerStats &GetStats() const { return stats_; }
57   // Returns true iff there are opened interfaces that are untouched.
UntouchedIfaces()58   bool UntouchedIfaces() const {
59     return opened_ifaces_.size() > stats_.TouchedIfaces().size();
60   }
61 
62  private:
63   // Looks up interface spec by name.
64   const CompSpec *FindCompSpec(std::string);
65   // Processes return value from a function call.
66   void ProcessReturnValue(const FuncSpec &result);
67   // Loads the interface corresponding to the given VTS spec. Interface is
68   // constructed with the given argument.
69   DriverBase *LoadInterface(const CompSpec &, uint64_t);
70 
71   // Keeps track of opened interfaces.
72   IfaceDescTbl opened_ifaces_;
73   // All loaded VTS specs indexed by name.
74   std::unordered_map<std::string, CompSpec> comp_specs_;
75   // Handle to the driver library.
76   void *driver_handle_;
77 
78   // Collects statistical information.
79   ProtoFuzzerStats stats_;
80 };
81 
82 }  // namespace fuzzer
83 }  // namespace vts
84 }  // namespace android
85 
86 #endif  // __VTS_PROTO_FUZZER_RUNNER_H__
87