blob: bbc469921aa657745031f89e989445605ad652f9 [file] [log] [blame]
Keun Soo Yim08400372016-03-03 13:28:51 -08001/*
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#include "specification_parser/SpecificationBuilder.h"
18
19#include <dirent.h>
20
21#include <iostream>
Keun Soo Yim2ee90792016-04-27 14:53:54 -070022#include <queue>
Keun Soo Yim08400372016-03-03 13:28:51 -080023#include <string>
24
25#include "fuzz_tester/FuzzerBase.h"
26#include "fuzz_tester/FuzzerWrapper.h"
27#include "specification_parser/InterfaceSpecificationParser.h"
28
Keun Soo Yimf5185852016-06-01 19:37:10 -070029#include "test/vts/runners/host/proto/InterfaceSpecificationMessage.pb.h"
Keun Soo Yim34067de2016-05-17 09:46:37 -070030#include <google/protobuf/text_format.h>
Keun Soo Yim08400372016-03-03 13:28:51 -080031
32namespace android {
33namespace vts {
34
Keun Soo Yim8103c912016-04-22 20:07:13 -070035SpecificationBuilder::SpecificationBuilder(
36 const string dir_path, int epoch_count)
37 : dir_path_(dir_path),
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -070038 epoch_count_(epoch_count),
Keun Soo Yim34067de2016-05-17 09:46:37 -070039 if_spec_msg_(NULL),
40 module_name_(NULL) {}
Keun Soo Yim08400372016-03-03 13:28:51 -080041
42
43vts::InterfaceSpecificationMessage*
44SpecificationBuilder::FindInterfaceSpecification(
45 const int target_class,
46 const int target_type,
Keun Soo Yim2ee90792016-04-27 14:53:54 -070047 const float target_version,
48 const string submodule_name) {
Keun Soo Yim08400372016-03-03 13:28:51 -080049 DIR* dir;
50 struct dirent* ent;
51
52 if (!(dir = opendir(dir_path_.c_str()))) {
53 cerr << __FUNCTION__ << ": Can't opendir " << dir_path_ << endl;
54 return NULL;
55 }
56
57 while ((ent = readdir(dir))) {
58 if (string(ent->d_name).find(SPEC_FILE_EXT) != std::string::npos) {
59 cout << __FUNCTION__ << ": Checking a file " << ent->d_name << endl;
60 const string file_path = string(dir_path_) + "/" + string(ent->d_name);
61 vts::InterfaceSpecificationMessage* message =
62 new vts::InterfaceSpecificationMessage();
63 if (InterfaceSpecificationParser::parse(file_path.c_str(), message)) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -070064 if (message->component_type() == target_type
Keun Soo Yim08400372016-03-03 13:28:51 -080065 && message->component_type_version() == target_version) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -070066 if (submodule_name.length() > 0) {
67 if (message->component_class() != HAL_SUBMODULE
68 || message->original_data_structure_name() != submodule_name) {
69 continue;
70 }
71 } else if (message->component_class() != target_class) continue;
Keun Soo Yim08400372016-03-03 13:28:51 -080072 closedir(dir);
73 return message;
74 }
75 }
76 delete message;
77 }
78 }
79 closedir(dir);
80 return NULL;
81}
82
83
Keun Soo Yim52f51662016-05-12 17:23:24 -070084FuzzerBase* SpecificationBuilder::GetFuzzerBase(
85 const vts::InterfaceSpecificationMessage& iface_spec_msg,
86 const char* dll_file_name,
87 const char* target_func_name) {
Keun Soo Yim6d944952016-05-31 16:30:56 -070088 cout << __func__ << ":" << __LINE__ << " " << "entry" << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -070089 FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg);
90 if (!fuzzer) {
91 cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl;
92 return NULL;
93 }
Keun Soo Yim6d944952016-05-31 16:30:56 -070094
95 // TODO: don't load multiple times. reuse FuzzerBase*.
96 cout << __func__ << ":" << __LINE__ << " " << "got fuzzer" << endl;
97 if (!fuzzer->LoadTargetComponent(dll_file_name)) {
Keun Soo Yim34067de2016-05-17 09:46:37 -070098 cerr << __FUNCTION__ << ": couldn't load target component file, "
99 << dll_file_name << endl;
Keun Soo Yim072337b2016-05-13 15:58:26 -0700100 return NULL;
101 }
Keun Soo Yim6d944952016-05-31 16:30:56 -0700102 cout << __func__ << ":" << __LINE__ << " " << "loaded target comp" << endl;
103
104 if (!strcmp(target_func_name, "#Open")) return fuzzer;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700105
106 for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) {
Keun Soo Yim072337b2016-05-13 15:58:26 -0700107 cout << "checking " << func_msg.name() << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700108 if (!strcmp(target_func_name, func_msg.name().c_str())) {
109 return fuzzer;
110 }
111 }
112 return NULL;
113}
114
115
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700116FuzzerBase* SpecificationBuilder::GetFuzzerBaseAndAddAllFunctionsToQueue(
117 const vts::InterfaceSpecificationMessage& iface_spec_msg,
118 const char* dll_file_name) {
119 FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg);
120 if (!fuzzer) {
121 cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl;
122 return NULL;
123 }
124 if (!fuzzer->LoadTargetComponent(dll_file_name)) return NULL;
125
126 for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) {
127 cout << "Add a job " << func_msg.name() << endl;
128 FunctionSpecificationMessage* func_msg_copy = func_msg.New();
129 func_msg_copy->CopyFrom(func_msg);
130 job_queue_.push(make_pair(func_msg_copy, fuzzer));
131 }
132 return fuzzer;
133}
134
135
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700136bool SpecificationBuilder::LoadTargetComponent(
137 const char* dll_file_name,
138 const char* spec_lib_file_path,
139 int target_class,
140 int target_type,
Keun Soo Yim34067de2016-05-17 09:46:37 -0700141 float target_version,
142 const char* module_name) {
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700143 if_spec_msg_ = FindInterfaceSpecification(
144 target_class, target_type, target_version);
145 if (!if_spec_msg_) {
146 cerr << __FUNCTION__ <<
147 ": no interface specification file found for "
148 << "class " << target_class
149 << " type " << target_type
150 << " version " << target_version << endl;
151 return false;
152 }
Keun Soo Yim52f51662016-05-12 17:23:24 -0700153 spec_lib_file_path_ = (char*) malloc(strlen(spec_lib_file_path) + 1);
154 strcpy(spec_lib_file_path_, spec_lib_file_path);
155
156 dll_file_name_ = (char*) malloc(strlen(dll_file_name) + 1);
157 strcpy(dll_file_name_, dll_file_name);
158
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700159 // cout << "ifspec addr load at " << if_spec_msg_ << endl;
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700160 string output;
161 if_spec_msg_->SerializeToString(&output);
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700162 cout << "loaded ifspec length " << output.length() << endl;
163 // cout << "loaded text " << strlen(output.c_str()) << endl;
164 // cout << "loaded text " << output << endl;
Keun Soo Yim34067de2016-05-17 09:46:37 -0700165
166 module_name_ = (char*) malloc(strlen(module_name) + 1);
167 strcpy(module_name_, module_name);
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700168 cout << __FUNCTION__ << ":" << __LINE__ << " module_name " << module_name_
169 << endl;
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700170 return true;
171}
172
173
Keun Soo Yim34067de2016-05-17 09:46:37 -0700174const string empty_string = string();
175
Keun Soo Yim04431a92016-05-24 16:26:42 -0700176const string& SpecificationBuilder::CallFunction(
177 FunctionSpecificationMessage* func_msg) {
Keun Soo Yim52f51662016-05-12 17:23:24 -0700178 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path_)) {
Keun Soo Yim34067de2016-05-17 09:46:37 -0700179 return empty_string;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700180 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700181 cout << __FUNCTION__ << ":" << __LINE__ << " " << "loaded if_spec lib" << endl;
Keun Soo Yim6d944952016-05-31 16:30:56 -0700182 cout << __func__ << " " << func_msg->name() << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700183
184 FuzzerBase* func_fuzzer = GetFuzzerBase(
185 *if_spec_msg_, dll_file_name_, func_msg->name().c_str());
Keun Soo Yim6d944952016-05-31 16:30:56 -0700186 cout << __func__ << ":" << __LINE__ << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700187 if (!func_fuzzer) {
Keun Soo Yim072337b2016-05-13 15:58:26 -0700188 cerr << "can't find FuzzerBase for " << func_msg->name() << " using "
189 << dll_file_name_ << endl;
Keun Soo Yim34067de2016-05-17 09:46:37 -0700190 return empty_string;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700191 }
192
Keun Soo Yim6d944952016-05-31 16:30:56 -0700193 if (func_msg->name() == "#Open") {
194 cout << __func__ << ":" << __LINE__ << endl;
195 if (func_msg->arg().size() > 0) {
196 cout << __func__ << " " << func_msg->arg(0).primitive_value(0).bytes().c_str() << endl;
197 func_fuzzer->OpenConventionalHal(
198 func_msg->arg(0).primitive_value(0).bytes().c_str());
199 } else {
200 cout << __func__ << " no arg" << endl;
201 func_fuzzer->OpenConventionalHal();
202 }
203 cout << __func__ << " opened" << endl;
204 // return the return value from open;
205 if (func_msg->return_type().primitive_type().size() > 0) {
206 // TODO handle when the size > 1.
207 if (!strcmp(func_msg->return_type().primitive_type(0).c_str(), "int32_t")) {
208 func_msg->mutable_return_type()->mutable_primitive_value()->Add()->set_int32_t(0);
209 cout << "result " << endl;
210 // todo handle more types;
211 string* output = new string();
212 google::protobuf::TextFormat::PrintToString(*func_msg, output);
213 return *output;
214 }
215 }
216 return empty_string;
217 }
218 cout << __func__ << ":" << __LINE__ << endl;
219
Keun Soo Yim52f51662016-05-12 17:23:24 -0700220 void* result;
Keun Soo Yim04431a92016-05-24 16:26:42 -0700221 func_fuzzer->FunctionCallBegin();
Keun Soo Yim6d944952016-05-31 16:30:56 -0700222 cout << __func__ << " Call Function " << func_msg->name() << endl;
Keun Soo Yimf5185852016-06-01 19:37:10 -0700223 if (!func_fuzzer->Fuzz(func_msg, &result)) {
Keun Soo Yim6d944952016-05-31 16:30:56 -0700224 cout << __func__ << " function not found - todo handle more explicitly" << endl;
225 return *(new string("error"));
226 }
227 cout << __func__ << ": called" << endl;
Keun Soo Yim04431a92016-05-24 16:26:42 -0700228
229 // set coverage data.
230 vector<unsigned>* coverage = func_fuzzer->FunctionCallEnd();
231 if (coverage && coverage->size() > 0) {
232 for (unsigned int index = 0; index < coverage->size(); index++) {
233 func_msg->mutable_coverage_data()->Add(coverage->at(index));
234 }
235 }
236
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700237 if (func_msg->return_type().aggregate_type().size() > 0) {
238 // TODO: actually handle this case.
Keun Soo Yim52f51662016-05-12 17:23:24 -0700239 if (result != NULL) {
240 // loads that interface spec and enqueues all functions.
Keun Soo Yim6d944952016-05-31 16:30:56 -0700241 cout << __func__ << " return type: "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700242 << func_msg->return_type().aggregate_type(0) << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700243 } else {
Keun Soo Yim6d944952016-05-31 16:30:56 -0700244 cout << __func__ << " return value = NULL" << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700245 }
Keun Soo Yim6d944952016-05-31 16:30:56 -0700246 cerr << __func__ << " todo: support aggregate" << endl;
Keun Soo Yim04431a92016-05-24 16:26:42 -0700247 string* output = new string();
248 google::protobuf::TextFormat::PrintToString(*func_msg, output);
249 return *output;
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700250 } else if (func_msg->return_type().primitive_type().size() > 0) {
251 // TODO handle when the size > 1.
252 if (!strcmp(func_msg->return_type().primitive_type(0).c_str(), "int32_t")) {
253 func_msg->mutable_return_type()->mutable_primitive_value()->Add()->set_int32_t(
Keun Soo Yim34067de2016-05-17 09:46:37 -0700254 *((int*)(&result)));
255 cout << "result " << endl;
256 // todo handle more types;
257 string* output = new string();
Keun Soo Yim04431a92016-05-24 16:26:42 -0700258 google::protobuf::TextFormat::PrintToString(*func_msg, output);
Keun Soo Yim34067de2016-05-17 09:46:37 -0700259 return *output;
260 }
Keun Soo Yim52f51662016-05-12 17:23:24 -0700261 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700262 return *(new string("void"));
Keun Soo Yim52f51662016-05-12 17:23:24 -0700263}
264
265
Keun Soo Yim08400372016-03-03 13:28:51 -0800266bool SpecificationBuilder::Process(
267 const char* dll_file_name,
268 const char* spec_lib_file_path,
269 int target_class,
270 int target_type,
271 float target_version) {
272 vts::InterfaceSpecificationMessage* interface_specification_message =
273 FindInterfaceSpecification(target_class, target_type, target_version);
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700274 cout << "ifspec addr " << interface_specification_message << endl;
275
Keun Soo Yim08400372016-03-03 13:28:51 -0800276 if (!interface_specification_message) {
277 cerr << __FUNCTION__ <<
278 ": no interface specification file found for "
279 << "class " << target_class
280 << " type " << target_type
281 << " version " << target_version << endl;
282 return false;
283 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800284
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700285 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) {
Keun Soo Yim08400372016-03-03 13:28:51 -0800286 return false;
287 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700288
289 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
290 *interface_specification_message, dll_file_name)) return false;
291
Keun Soo Yim8103c912016-04-22 20:07:13 -0700292 for (int i = 0; i < epoch_count_; i++) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700293 // by default, breath-first-searching is used.
294 if (job_queue_.empty()) {
295 cout << "no more job to process; stopping after epoch " << i << endl;
296 break;
297 }
298
299 pair<vts::FunctionSpecificationMessage*, FuzzerBase*> curr_job =
300 job_queue_.front();
301 job_queue_.pop();
302
303 vts::FunctionSpecificationMessage* func_msg = curr_job.first;
304 FuzzerBase* func_fuzzer = curr_job.second;
305
Keun Soo Yimc5d092a2016-04-28 16:51:56 +0000306 void* result;
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700307 cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl;
Keun Soo Yimf5185852016-06-01 19:37:10 -0700308 func_fuzzer->Fuzz(func_msg, &result);
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700309 if (func_msg->return_type().aggregate_type().size() > 0) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700310 if (result != NULL) {
311 // loads that interface spec and enqueues all functions.
312 cout << __FUNCTION__ << " return type: "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700313 << func_msg->return_type().aggregate_type(0) << endl;
314 // TODO: handle the case when size > 1
315 string submodule_name = func_msg->return_type().aggregate_type(0);
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700316 while (!submodule_name.empty()
317 && (std::isspace(submodule_name.back())
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700318 || submodule_name.back() == '*')) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700319 submodule_name.pop_back();
320 }
321 vts::InterfaceSpecificationMessage* iface_spec_msg =
322 FindInterfaceSpecification(
323 target_class, target_type, target_version, submodule_name);
324 if (iface_spec_msg) {
325 cout << __FUNCTION__ << " submodule found - " << submodule_name << endl;
326 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
327 *iface_spec_msg, dll_file_name)) {
328 return false;
329 }
330 } else {
331 cout << __FUNCTION__ << " submodule not found - " << submodule_name << endl;
332 }
333 } else {
334 cout << __FUNCTION__ << " return value = NULL" << endl;
335 }
336 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800337 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700338
Keun Soo Yim08400372016-03-03 13:28:51 -0800339 return true;
340}
341
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700342
343vts::InterfaceSpecificationMessage*
344SpecificationBuilder::GetInterfaceSpecification() const {
345 cout << "ifspec addr get " << if_spec_msg_ << endl;
346 return if_spec_msg_;
347}
348
Keun Soo Yim08400372016-03-03 13:28:51 -0800349} // namespace vts
350} // namespace android