1 /*
2  * Copyright (C) 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_SPECPARSER_SPECBUILDER_H__
18 #define __VTS_SYSFUZZER_COMMON_SPECPARSER_SPECBUILDER_H__
19 
20 #include <queue>
21 #include <string>
22 
23 #include "component_loader/DllLoader.h"
24 #include "driver_base/DriverBase.h"
25 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
26 
27 using namespace std;
28 
29 namespace android {
30 namespace vts {
31 // Builder of an interface specification.
32 class HalDriverLoader {
33  public:
34   // Constructor where the first argument is the path of a dir which contains
35   // all available interface specification files.
36   HalDriverLoader(const string dir_path, int epoch_count,
37                   const string& callback_socket_name);
38 
39   // Scans the dir and returns an component specification for a requested
40   // component.
41   // Args:
42   //   version_major: int, hal major version, e.g. 1.0 -> 1
43   //   version_minor: int, hal minor version, e.g. 1.0 -> 0
44   bool FindComponentSpecification(const int component_class,
45                                   const string& package_name,
46                                   const int version_major,
47                                   const int version_minor,
48                                   const string& component_name,
49                                   const int component_type,
50                                   ComponentSpecificationMessage* spec_msg);
51 
52   // Create driver for given component.
53   DriverBase* GetDriver(const string& driver_lib_path,
54                         const ComponentSpecificationMessage& spec_msg,
55                         const string& hw_binder_service_name,
56                         const uint64_t interface_pt,
57                         bool with_interface_pointer,
58                         const string& dll_file_name);
59 
60   // Returns FuzzBase for a given interface specification, and adds all the
61   // found functions to the fuzzing job queue.
62   // TODO (zhuoyao): consider to deprecate this method.
63   DriverBase* GetFuzzerBaseAndAddAllFunctionsToQueue(
64       const char* driver_lib_path,
65       const ComponentSpecificationMessage& iface_spec_msg,
66       const char* dll_file_name, const char* hw_service_name);
67 
68   // Main function for the VTS system fuzzer where dll_file_name is the path of
69   // a target component, spec_lib_file_path is the path of a specification
70   // library file, and the rest three arguments are the basic information of
71   // the target component.
72   // Args:
73   //   target_version_major: hal major version, e.g. 1.0 -> 1.
74   //   target_version_minor: hal minor version, e.g. 1.0 -> 0.
75   // TODO (zhuoyao): consider to deprecate this method.
76   bool Process(const char* dll_file_name, const char* spec_lib_file_path,
77                int target_class, int target_type, int target_version_major,
78                int target_version_minor, const char* target_package,
79                const char* target_component_name, const char* hal_service_name);
80 
81  private:
82   // Internal method to create driver for library.
83   DriverBase* GetLibDriver(const string& driver_lib_path,
84                            const ComponentSpecificationMessage& spec_msg,
85                            const string& dll_file_name);
86 
87   // Internal method to create driver for HIDL HAL.
88   DriverBase* GetHidlHalDriver(const string& driver_lib_path,
89                                const ComponentSpecificationMessage& spec_msg,
90                                const string& hal_service_name,
91                                const uint64_t interface_pt,
92                                bool with_interface_pt);
93 
94   // Internal method to create driver for HIDL HAL with interface pointer.
95   DriverBase* LoadDriverWithInterfacePointer(
96       const string& driver_lib_path,
97       const ComponentSpecificationMessage& spec_msg,
98       const uint64_t interface_pt);
99 
100   // Helper method to load a driver library with the given path.
101   DriverBase* LoadDriver(const string& driver_lib_path,
102                          const ComponentSpecificationMessage& spec_msg);
103 
104   // A DLL Loader instance used to load the driver library.
105   DllLoader dll_loader_;
106   // the path of a dir which contains interface specification ASCII proto files.
107   const string dir_path_;
108   // the total number of epochs
109   const int epoch_count_;
110   // the server socket port # of the agent.
111   const string callback_socket_name_;
112   // fuzzing job queue. Used by Process method.
113   queue<pair<FunctionSpecificationMessage*, DriverBase*>> job_queue_;
114 };
115 
116 }  // namespace vts
117 }  // namespace android
118 
119 #endif  // __VTS_SYSFUZZER_COMMON_SPECPARSER_SPECBUILDER_H__
120