blob: 0088c14ba993d5c5b125aea87012ec8588eaf8df [file] [log] [blame]
Tri Vo9f5fd852017-05-20 12:32:26 -07001/*
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#include <sstream>
21
22#include "utils/InterfaceSpecUtil.h"
23#include "vintf/HalManifest.h"
Zhuoyao Zhang51bdaee2017-10-29 18:37:27 -070024#include "vintf/Version.h"
Tri Vo9f5fd852017-05-20 12:32:26 -070025#include "vintf/VintfObject.h"
26
27using android::vintf::HalManifest;
Zhuoyao Zhang51bdaee2017-10-29 18:37:27 -070028using android::vintf::Version;
Tri Vo9f5fd852017-05-20 12:32:26 -070029
Tri Vo9f5fd852017-05-20 12:32:26 -070030using std::cerr;
Tri Voa0433862017-07-26 16:13:02 -070031using std::cout;
Tri Vo9f5fd852017-05-20 12:32:26 -070032using std::string;
Tri Vo9f5fd852017-05-20 12:32:26 -070033using std::unordered_map;
Tri Voa0433862017-07-26 16:13:02 -070034using std::vector;
Tri Vo9f5fd852017-05-20 12:32:26 -070035
36namespace android {
37namespace vts {
38namespace fuzzer {
39
40static string GetDriverName(const CompSpec &comp_spec) {
41 stringstream version;
42 version.precision(1);
43 version << fixed << comp_spec.component_type_version();
44 string driver_name =
45 comp_spec.package() + "@" + version.str() + "-vts.driver.so";
46 return driver_name;
47}
48
49static string GetServiceName(const CompSpec &comp_spec) {
Tri Vo9f5fd852017-05-20 12:32:26 -070050 string hal_name = comp_spec.package();
Zhuoyao Zhang51bdaee2017-10-29 18:37:27 -070051 string hal_version = GetVersionString(comp_spec.component_type_version());
Tri Vo9f5fd852017-05-20 12:32:26 -070052 string iface_name = comp_spec.component_name();
53
Zhuoyao Zhang51bdaee2017-10-29 18:37:27 -070054 size_t major_version =
55 std::stoul(hal_version.substr(0, hal_version.find(".")));
56 size_t minor_version =
57 std::stoul(hal_version.substr(hal_version.find(".") + 1));
58
Yifan Hongd18f06f2017-08-29 18:27:37 -070059 auto instance_names =
60 ::android::vintf::VintfObject::GetDeviceHalManifest()->getInstances(
Zhuoyao Zhang51bdaee2017-10-29 18:37:27 -070061 hal_name, Version(major_version, minor_version), iface_name);
Tri Vo9f5fd852017-05-20 12:32:26 -070062 if (instance_names.empty()) {
63 cerr << "HAL service name not available in VINTF." << endl;
Tri Vod1528682017-07-20 15:36:48 -070064 std::abort();
Tri Vo9f5fd852017-05-20 12:32:26 -070065 }
66
67 // For fuzzing we don't care which instance of the HAL is targeted.
68 string service_name = *instance_names.begin();
69 cout << "Available HAL instances: " << endl;
70 for (const string &instance_name : instance_names) {
71 cout << instance_name << endl;
72 }
73 cout << "Using HAL instance: " << service_name << endl;
74
75 return service_name;
76}
77
Tri Voa0c29852017-05-22 15:37:40 -070078static void *Dlopen(string lib_name) {
Tri Vo9f5fd852017-05-20 12:32:26 -070079 // Clear dlerror().
80 dlerror();
Tri Voa0c29852017-05-22 15:37:40 -070081 void *handle = dlopen(lib_name.c_str(), RTLD_LAZY);
82 if (!handle) {
83 cerr << __func__ << ": " << dlerror() << endl;
84 cerr << __func__ << ": Can't load shared library: " << lib_name << endl;
Tri Vod1528682017-07-20 15:36:48 -070085 std::abort();
Tri Voa0c29852017-05-22 15:37:40 -070086 }
87 return handle;
88}
89
90static void *Dlsym(void *handle, string function_name) {
91 const char *error;
92 // Clear dlerror().
93 dlerror();
94 void *function = dlsym(handle, function_name.c_str());
Tri Vo9f5fd852017-05-20 12:32:26 -070095 if ((error = dlerror()) != NULL) {
96 cerr << __func__ << ": Can't find: " << function_name << endl;
97 cerr << error << endl;
Tri Vod1528682017-07-20 15:36:48 -070098 std::abort();
Tri Vo9f5fd852017-05-20 12:32:26 -070099 }
Tri Voa0c29852017-05-22 15:37:40 -0700100 return function;
101}
Tri Vo9f5fd852017-05-20 12:32:26 -0700102
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700103static void GetService(DriverBase *hal, string service_name, bool binder_mode) {
Tri Vo9f5fd852017-05-20 12:32:26 -0700104 // For fuzzing, only passthrough mode provides coverage.
105 // If binder mode is not requested, attempt to open HAL in passthrough mode.
106 // If the attempt fails, fall back to binder mode.
107 if (!binder_mode) {
108 if (!hal->GetService(true, service_name.c_str())) {
109 cerr << __func__ << ": Failed to open HAL in passthrough mode. "
110 << "Falling back to binder mode." << endl;
111 } else {
112 cerr << "HAL opened in passthrough mode." << endl;
Tri Voa0c29852017-05-22 15:37:40 -0700113 return;
Tri Vo9f5fd852017-05-20 12:32:26 -0700114 }
115 }
116
117 if (!hal->GetService(false, service_name.c_str())) {
118 cerr << __func__ << ": Failed to open HAL in binder mode." << endl;
Tri Vod1528682017-07-20 15:36:48 -0700119 std::abort();
Tri Vo9f5fd852017-05-20 12:32:26 -0700120 } else {
121 cerr << "HAL opened in binder mode." << endl;
Tri Voa0c29852017-05-22 15:37:40 -0700122 return;
123 }
124}
125
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700126DriverBase *ProtoFuzzerRunner::LoadInterface(const CompSpec &comp_spec,
Tri Voa0c29852017-05-22 15:37:40 -0700127 uint64_t hidl_service = 0) {
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700128 DriverBase *hal;
Tri Voa0c29852017-05-22 15:37:40 -0700129 // Clear dlerror().
130 dlerror();
131
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700132 // DriverBase can be constructed with or without an argument.
133 // Using different DriverBase constructors requires dlsym'ing different
Tri Voa0c29852017-05-22 15:37:40 -0700134 // symbols from the driver library.
135 string function_name = GetFunctionNamePrefix(comp_spec);
136 if (hidl_service) {
137 function_name += "with_arg";
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700138 using loader_func = DriverBase *(*)(uint64_t);
Tri Voa0c29852017-05-22 15:37:40 -0700139 auto hal_loader = (loader_func)Dlsym(driver_handle_, function_name.c_str());
140 hal = hal_loader(hidl_service);
141 } else {
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700142 using loader_func = DriverBase *(*)();
Tri Voa0c29852017-05-22 15:37:40 -0700143 auto hal_loader = (loader_func)Dlsym(driver_handle_, function_name.c_str());
144 hal = hal_loader();
Tri Vo9f5fd852017-05-20 12:32:26 -0700145 }
146 return hal;
147}
148
149ProtoFuzzerRunner::ProtoFuzzerRunner(const vector<CompSpec> &comp_specs) {
150 for (const auto &comp_spec : comp_specs) {
151 if (comp_spec.has_interface()) {
152 string name = comp_spec.component_name();
153 comp_specs_[name] = comp_spec;
154 }
155 }
156}
157
158void ProtoFuzzerRunner::Init(const string &iface_name, bool binder_mode) {
159 const CompSpec *comp_spec = FindCompSpec(iface_name);
Tri Voa0c29852017-05-22 15:37:40 -0700160 // dlopen VTS driver library.
161 string driver_name = GetDriverName(*comp_spec);
162 driver_handle_ = Dlopen(driver_name);
163
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700164 std::shared_ptr<DriverBase> hal{LoadInterface(*comp_spec)};
Tri Voa0c29852017-05-22 15:37:40 -0700165 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);
Tri Vo9f5fd852017-05-20 12:32:26 -0700172
173 // Register this interface as opened by the runner.
174 opened_ifaces_[iface_name] = {
175 .comp_spec_ = comp_spec, .hal_ = hal,
176 };
177}
178
179void ProtoFuzzerRunner::Execute(const ExecSpec &exec_spec) {
Tri Voa0c29852017-05-22 15:37:40 -0700180 for (const auto &func_call : exec_spec.function_call()) {
Tri Vo9cfc72e2017-06-07 17:50:05 -0700181 cout << func_call.DebugString() << endl;
Tri Voa0c29852017-05-22 15:37:40 -0700182 Execute(func_call);
183 }
184}
Tri Vo9f5fd852017-05-20 12:32:26 -0700185
Tri Voa0c29852017-05-22 15:37:40 -0700186void ProtoFuzzerRunner::Execute(const FuncCall &func_call) {
187 string iface_name = func_call.hidl_interface_name();
188 const FuncSpec &func_spec = func_call.api();
189
190 auto iface_desc = opened_ifaces_.find(iface_name);
191 if (iface_desc == opened_ifaces_.end()) {
192 cerr << "Interface is not open: " << iface_name << endl;
Tri Vod1528682017-07-20 15:36:48 -0700193 std::abort();
Tri Voa0c29852017-05-22 15:37:40 -0700194 }
Tri Voa0c29852017-05-22 15:37:40 -0700195
196 FuncSpec result{};
197 iface_desc->second.hal_->CallFunction(func_spec, "", &result);
198
Tri Voa0433862017-07-26 16:13:02 -0700199 stats_.RegisterTouch(iface_name, func_spec.name());
Tri Voa0c29852017-05-22 15:37:40 -0700200 ProcessReturnValue(result);
201}
202
203static string StripNamespace(const string &type) {
204 size_t idx = type.find_last_of(':');
205 if (idx == string::npos) {
206 return "";
207 }
208 return type.substr(idx + 1);
209}
210
211void ProtoFuzzerRunner::ProcessReturnValue(const FuncSpec &result) {
212 for (const auto &var : result.return_type_hidl()) {
Tri Vo2407ef22017-07-23 15:45:09 -0700213 // If result contains a pointer to an interface, register it in
214 // opened_ifaces_ table. That pointer must not be a nullptr.
215 if (var.has_hidl_interface_pointer() && var.hidl_interface_pointer() &&
216 var.has_predefined_type()) {
Tri Voa0c29852017-05-22 15:37:40 -0700217 uint64_t hidl_service = var.hidl_interface_pointer();
218 string type = var.predefined_type();
219 string iface_name = StripNamespace(type);
220
221 const CompSpec *comp_spec = FindCompSpec(iface_name);
Zhuoyao Zhang6af0bde2017-06-23 15:10:42 -0700222 std::shared_ptr<DriverBase> hal{LoadInterface(*comp_spec, hidl_service)};
Tri Voa0c29852017-05-22 15:37:40 -0700223
Tri Vo8bc46c52017-08-10 17:27:49 -0700224 // If this interface has not been seen before, record the fact.
225 if (opened_ifaces_.find(iface_name) == opened_ifaces_.end()) {
226 cerr << "Discovered new interface: " << iface_name << endl;
227 }
228
Tri Voa0c29852017-05-22 15:37:40 -0700229 // Register this interface as opened by the runner.
230 opened_ifaces_[iface_name] = {
231 .comp_spec_ = comp_spec, .hal_ = hal,
232 };
Tri Vo9f5fd852017-05-20 12:32:26 -0700233 }
Tri Vo9f5fd852017-05-20 12:32:26 -0700234 }
235}
236
237const CompSpec *ProtoFuzzerRunner::FindCompSpec(std::string name) {
238 auto comp_spec = comp_specs_.find(name);
239 if (comp_spec == comp_specs_.end()) {
240 cerr << "VTS spec not found: " << name << endl;
Tri Vod1528682017-07-20 15:36:48 -0700241 std::abort();
Tri Vo9f5fd852017-05-20 12:32:26 -0700242 }
243 return &comp_spec->second;
244}
245
246} // namespace fuzzer
247} // namespace vts
248} // namespace android