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 #include "ProtoFuzzerRunner.h"
18 
19 #include <dlfcn.h>
20 
21 #include <sstream>
22 
23 #include <hidl/ServiceManagement.h>
24 
25 #include "utils/InterfaceSpecUtil.h"
26 
27 using android::hardware::getAllHalInstanceNames;
28 using std::cerr;
29 using std::cout;
30 using std::string;
31 using std::unordered_map;
32 using std::vector;
33 
34 namespace android {
35 namespace vts {
36 namespace fuzzer {
37 
GetVersionString(const CompSpec & comp_spec)38 static string GetVersionString(const CompSpec &comp_spec) {
39   stringstream version_major, version_minor;
40   version_major << comp_spec.component_type_version_major();
41   version_minor << comp_spec.component_type_version_minor();
42   return version_major.str() + "." + version_minor.str();
43 }
44 
GetDriverName(const CompSpec & comp_spec)45 static string GetDriverName(const CompSpec &comp_spec) {
46   string version_string = GetVersionString(comp_spec);
47   string driver_name =
48       comp_spec.package() + "@" + version_string + "-vts.driver.so";
49   return driver_name;
50 }
51 
GetServiceName(const CompSpec & comp_spec)52 static string GetServiceName(const CompSpec &comp_spec) {
53   string hal_name = comp_spec.package();
54   string iface_name = comp_spec.component_name();
55 
56   auto descriptor =
57       hal_name + "@" + GetVersionString(comp_spec) + "::" + iface_name;
58   auto instance_names = getAllHalInstanceNames(descriptor);
59 
60   if (instance_names.empty()) {
61     cerr << "HAL service name not available in VINTF." << endl;
62     std::abort();
63   }
64 
65   // For fuzzing we don't care which instance of the HAL is targeted.
66   string service_name = *instance_names.begin();
67   cout << "Available HAL instances: " << endl;
68   for (const string &instance_name : instance_names) {
69     cout << instance_name << endl;
70   }
71   cout << "Using HAL instance: " << service_name << endl;
72 
73   return service_name;
74 }
75 
Dlopen(string lib_name)76 static void *Dlopen(string lib_name) {
77   // Clear dlerror().
78   dlerror();
79   void *handle = dlopen(lib_name.c_str(), RTLD_LAZY);
80   if (!handle) {
81     cerr << __func__ << ": " << dlerror() << endl;
82     cerr << __func__ << ": Can't load shared library: " << lib_name << endl;
83     std::abort();
84   }
85   return handle;
86 }
87 
Dlsym(void * handle,string function_name)88 static void *Dlsym(void *handle, string function_name) {
89   const char *error;
90   // Clear dlerror().
91   dlerror();
92   void *function = dlsym(handle, function_name.c_str());
93   if ((error = dlerror()) != NULL) {
94     cerr << __func__ << ": Can't find: " << function_name << endl;
95     cerr << error << endl;
96     std::abort();
97   }
98   return function;
99 }
100 
GetService(DriverBase * hal,string service_name,bool binder_mode)101 static void GetService(DriverBase *hal, string service_name, bool binder_mode) {
102   // For fuzzing, only passthrough mode provides coverage.
103   // If binder mode is not requested, attempt to open HAL in passthrough mode.
104   // If the attempt fails, fall back to binder mode.
105   if (!binder_mode) {
106     if (!hal->GetService(true, service_name.c_str())) {
107       cerr << __func__ << ": Failed to open HAL in passthrough mode. "
108            << "Falling back to binder mode." << endl;
109     } else {
110       cerr << "HAL opened in passthrough mode." << endl;
111       return;
112     }
113   }
114 
115   if (!hal->GetService(false, service_name.c_str())) {
116     cerr << __func__ << ": Failed to open HAL in binder mode." << endl;
117     std::abort();
118   } else {
119     cerr << "HAL opened in binder mode." << endl;
120     return;
121   }
122 }
123 
LoadInterface(const CompSpec & comp_spec,uint64_t hidl_service=0)124 DriverBase *ProtoFuzzerRunner::LoadInterface(const CompSpec &comp_spec,
125                                              uint64_t hidl_service = 0) {
126   DriverBase *hal;
127   // Clear dlerror().
128   dlerror();
129 
130   // DriverBase can be constructed with or without an argument.
131   // Using different DriverBase constructors requires dlsym'ing different
132   // symbols from the driver library.
133   string function_name = GetFunctionNamePrefix(comp_spec);
134   if (hidl_service) {
135     function_name += "with_arg";
136     using loader_func = DriverBase *(*)(uint64_t);
137     auto hal_loader = (loader_func)Dlsym(driver_handle_, function_name.c_str());
138     hal = hal_loader(hidl_service);
139   } else {
140     using loader_func = DriverBase *(*)();
141     auto hal_loader = (loader_func)Dlsym(driver_handle_, function_name.c_str());
142     hal = hal_loader();
143   }
144   return hal;
145 }
146 
ProtoFuzzerRunner(const vector<CompSpec> & comp_specs,const string version_iface)147 ProtoFuzzerRunner::ProtoFuzzerRunner(const vector<CompSpec> &comp_specs,
148                                      const string version_iface) {
149   for (const auto &comp_spec : comp_specs) {
150     string target_version = GetVersionString(comp_spec);
151     if (comp_spec.has_interface() && target_version == version_iface) {
152       string name = comp_spec.component_name();
153       comp_specs_[name] = comp_spec;
154     }
155   }
156 }
157 
Init(const string & iface_name,bool binder_mode)158 void ProtoFuzzerRunner::Init(const string &iface_name, bool binder_mode) {
159   const CompSpec *comp_spec = FindCompSpec(iface_name);
160   // dlopen VTS driver library.
161   string driver_name = GetDriverName(*comp_spec);
162   driver_handle_ = Dlopen(driver_name);
163 
164   std::shared_ptr<DriverBase> hal{LoadInterface(*comp_spec)};
165   string service_name = GetServiceName(*comp_spec);
166   cerr << "HAL name: " << comp_spec->package() << endl
167        << "Interface name: " << comp_spec->component_name() << endl
168        << "Service name: " << service_name << endl;
169 
170   // This should only be done for top-level interfaces.
171   GetService(hal.get(), service_name, binder_mode);
172 
173   // Register this interface as opened by the runner.
174   opened_ifaces_[iface_name] = {
175       .comp_spec_ = comp_spec, .hal_ = hal,
176   };
177 }
178 
Execute(const ExecSpec & exec_spec)179 void ProtoFuzzerRunner::Execute(const ExecSpec &exec_spec) {
180   for (const auto &func_call : exec_spec.function_call()) {
181     Execute(func_call);
182   }
183 }
184 
Execute(const FuncCall & func_call)185 void ProtoFuzzerRunner::Execute(const FuncCall &func_call) {
186   string iface_name = func_call.hidl_interface_name();
187   const FuncSpec &func_spec = func_call.api();
188 
189   auto iface_desc = opened_ifaces_.find(iface_name);
190   if (iface_desc == opened_ifaces_.end()) {
191     cerr << "Interface is not open: " << iface_name << endl;
192     std::abort();
193   }
194 
195   FuncSpec result{};
196   iface_desc->second.hal_->CallFunction(func_spec, "", &result);
197 
198   stats_.RegisterTouch(iface_name, func_spec.name());
199   ProcessReturnValue(result);
200 }
201 
StripNamespace(const string & type)202 static string StripNamespace(const string &type) {
203   size_t idx = type.find_last_of(':');
204   if (idx == string::npos) {
205     return "";
206   }
207   return type.substr(idx + 1);
208 }
209 
ProcessReturnValue(const FuncSpec & result)210 void ProtoFuzzerRunner::ProcessReturnValue(const FuncSpec &result) {
211   for (const auto &var : result.return_type_hidl()) {
212     // If result contains a pointer to an interface, register it in
213     // opened_ifaces_ table. That pointer must not be a nullptr.
214     if (var.has_hidl_interface_pointer() && var.hidl_interface_pointer() &&
215         var.has_predefined_type()) {
216       uint64_t hidl_service = var.hidl_interface_pointer();
217       string type = var.predefined_type();
218       string iface_name = StripNamespace(type);
219 
220       const CompSpec *comp_spec = FindCompSpec(iface_name);
221       std::shared_ptr<DriverBase> hal{LoadInterface(*comp_spec, hidl_service)};
222 
223       // If this interface has not been seen before, record the fact.
224       if (opened_ifaces_.find(iface_name) == opened_ifaces_.end()) {
225         cerr << "Discovered new interface: " << iface_name << endl;
226       }
227 
228       // Register this interface as opened by the runner.
229       opened_ifaces_[iface_name] = {
230           .comp_spec_ = comp_spec, .hal_ = hal,
231       };
232     }
233   }
234 }
235 
FindCompSpec(std::string name)236 const CompSpec *ProtoFuzzerRunner::FindCompSpec(std::string name) {
237   auto comp_spec = comp_specs_.find(name);
238   if (comp_spec == comp_specs_.end()) {
239     cerr << "VTS spec not found: " << name << endl;
240     std::abort();
241   }
242   return &comp_spec->second;
243 }
244 
245 }  // namespace fuzzer
246 }  // namespace vts
247 }  // namespace android
248