1 /*
2  * Copyright 2016 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_SYSFUZZER_COMMON_FUZZER_BASE_H__
18 #define __VTS_SYSFUZZER_COMMON_FUZZER_BASE_H__
19 
20 #include "component_loader/DllLoader.h"
21 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
22 
23 // HACK: NAN is #defined by math.h which gets included by
24 // ComponentSpecificationMessage.pb.h, but some HALs use
25 // enums called NAN.  Undefine NAN to work around it.
26 #undef NAN
27 
28 using namespace std;
29 
30 namespace android {
31 namespace vts {
32 
33 class DriverBase {
34  public:
35   explicit DriverBase(int target_class);
36   virtual ~DriverBase();
37 
38   // Loads a target component where the argument is the file path.
39   // Returns true iff successful.
40   bool LoadTargetComponent(const char* target_dll_path);
41 
42   // Gets the HIDL service.
43   // Returns true iff successful.
GetService(bool,const char *)44   virtual bool GetService(bool /*get_stub*/, const char* /*service_name*/) {
45     return false;
46   };
47 
48   // Fuzz tests the loaded component using the provided interface specification.
49   // Returns true iff the testing is conducted completely.
50   bool Fuzz(vts::ComponentSpecificationMessage* message, void** result);
51 
52   // Actual implementation of routines to test a specific function using the
53   // provided function interface specification message.
54   // Returns true iff the testing is conducted completely.
Fuzz(vts::FunctionSpecificationMessage *,void **,const string &)55   virtual bool Fuzz(vts::FunctionSpecificationMessage* /*func_msg*/,
56                     void** /*result*/, const string& /*callback_socket_name*/) {
57     return false;
58   };
59 
CallFunction(const vts::FunctionSpecificationMessage &,const string &,vts::FunctionSpecificationMessage *)60   virtual bool CallFunction(
61       const vts::FunctionSpecificationMessage& /*func_msg*/,
62       const string& /*callback_socket_name*/,
63       vts::FunctionSpecificationMessage* /*result_msg*/) {
64     return false;
65   };
66 
VerifyResults(const vts::FunctionSpecificationMessage &,const vts::FunctionSpecificationMessage &)67   virtual bool VerifyResults(
68       const vts::FunctionSpecificationMessage& /*expected_result_msg*/,
69       const vts::FunctionSpecificationMessage& /*actual_result_msg*/) {
70     return false;
71   };
72 
GetAttribute(vts::FunctionSpecificationMessage *,void **)73   virtual bool GetAttribute(vts::FunctionSpecificationMessage* /*func_msg*/,
74                             void** /*result*/) {
75     return false;
76   }
77 
78   // Called before calling a target function.
79   void FunctionCallBegin();
80 
81   // Called after calling a target function. Fills in the code coverage info.
82   bool FunctionCallEnd(FunctionSpecificationMessage* msg);
83 
84   // Scans all GCDA files under a given dir and adds to the message.
85   bool ScanAllGcdaFiles(const string& basepath,
86                         FunctionSpecificationMessage* msg);
87 
88  protected:
89   bool ReadGcdaFile(const string& basepath, const string& filename,
90                     FunctionSpecificationMessage* msg);
91 
92   // a pointer to a HAL data structure of the loaded component.
93   struct hw_device_t* device_;
94 
95   // DLL Loader class.
96   DllLoader target_loader_;
97 
98   // a pointer to the HAL_MODULE_INFO_SYM data structure of the loaded
99   // component.
100   struct hw_module_t* hmi_;
101 
102  private:
103   // a pointer to the string which contains the loaded component.
104   char* target_dll_path_;
105 
106   // function name prefix.
107   const char* function_name_prefix_;
108 
109   // target class
110   const int target_class_;
111 
112   // target component file name (without extension)
113   char* component_filename_;
114 
115   // path to store the gcov output files.
116   char* gcov_output_basepath_;
117 };
118 
119 }  // namespace vts
120 }  // namespace android
121 
122 #endif  // __VTS_SYSFUZZER_COMMON_FUZZER_BASE_H__
123