Home | History | Annotate | Download | only in include
      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   ProtoFuzzerRunner(const std::vector<CompSpec> &comp_specs);
     44 
     45   // Initializes interface descriptor table by opening the root interface.
     46   void Init(const std::string &, bool);
     47   // Call every API from call sequence specified by the ExecSpec.
     48   void Execute(const ExecSpec &);
     49   // Execute the specified interface function call.
     50   void Execute(const FuncCall &);
     51   // Accessor to interface descriptor table containing currently opened
     52   // interfaces.
     53   const IfaceDescTbl &GetOpenedIfaces() const { return opened_ifaces_; }
     54   // Accessor to stats object.
     55   const ProtoFuzzerStats &GetStats() const { return stats_; }
     56   // Returns true iff there are opened interfaces that are untouched.
     57   bool UntouchedIfaces() const {
     58     return opened_ifaces_.size() > stats_.TouchedIfaces().size();
     59   }
     60 
     61  private:
     62   // Looks up interface spec by name.
     63   const CompSpec *FindCompSpec(std::string);
     64   // Processes return value from a function call.
     65   void ProcessReturnValue(const FuncSpec &result);
     66   // Loads the interface corresponding to the given VTS spec. Interface is
     67   // constructed with the given argument.
     68   DriverBase *LoadInterface(const CompSpec &, uint64_t);
     69 
     70   // Keeps track of opened interfaces.
     71   IfaceDescTbl opened_ifaces_;
     72   // All loaded VTS specs indexed by name.
     73   std::unordered_map<std::string, CompSpec> comp_specs_;
     74   // Handle to the driver library.
     75   void *driver_handle_;
     76 
     77   // Collects statistical information.
     78   ProtoFuzzerStats stats_;
     79 };
     80 
     81 }  // namespace fuzzer
     82 }  // namespace vts
     83 }  // namespace android
     84 
     85 #endif  // __VTS_PROTO_FUZZER_RUNNER_H__
     86