blob: 72ef0c9d1a41260bfd1ab1e3965ea965c8f1aba3 [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
29#include "test/vts/sysfuzzer/common/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 Yim34067de2016-05-17 09:46:37 -070088 cout << __FUNCTION__ << ":" << __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 Yim34067de2016-05-17 09:46:37 -070094 cout << __FUNCTION__ << ":" << __LINE__ << " " << "got fuzzer" << endl;
95 if (!fuzzer->LoadTargetComponent(dll_file_name, module_name_)) {
96 cerr << __FUNCTION__ << ": couldn't load target component file, "
97 << dll_file_name << endl;
Keun Soo Yim072337b2016-05-13 15:58:26 -070098 return NULL;
99 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700100 cout << __FUNCTION__ << ":" << __LINE__ << " " << "loaded target comp" << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700101
102 for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) {
Keun Soo Yim072337b2016-05-13 15:58:26 -0700103 cout << "checking " << func_msg.name() << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700104 if (!strcmp(target_func_name, func_msg.name().c_str())) {
105 return fuzzer;
106 }
107 }
108 return NULL;
109}
110
111
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700112FuzzerBase* SpecificationBuilder::GetFuzzerBaseAndAddAllFunctionsToQueue(
113 const vts::InterfaceSpecificationMessage& iface_spec_msg,
114 const char* dll_file_name) {
115 FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg);
116 if (!fuzzer) {
117 cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl;
118 return NULL;
119 }
120 if (!fuzzer->LoadTargetComponent(dll_file_name)) return NULL;
121
122 for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) {
123 cout << "Add a job " << func_msg.name() << endl;
124 FunctionSpecificationMessage* func_msg_copy = func_msg.New();
125 func_msg_copy->CopyFrom(func_msg);
126 job_queue_.push(make_pair(func_msg_copy, fuzzer));
127 }
128 return fuzzer;
129}
130
131
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700132bool SpecificationBuilder::LoadTargetComponent(
133 const char* dll_file_name,
134 const char* spec_lib_file_path,
135 int target_class,
136 int target_type,
Keun Soo Yim34067de2016-05-17 09:46:37 -0700137 float target_version,
138 const char* module_name) {
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700139 if_spec_msg_ = FindInterfaceSpecification(
140 target_class, target_type, target_version);
141 if (!if_spec_msg_) {
142 cerr << __FUNCTION__ <<
143 ": no interface specification file found for "
144 << "class " << target_class
145 << " type " << target_type
146 << " version " << target_version << endl;
147 return false;
148 }
Keun Soo Yim52f51662016-05-12 17:23:24 -0700149 spec_lib_file_path_ = (char*) malloc(strlen(spec_lib_file_path) + 1);
150 strcpy(spec_lib_file_path_, spec_lib_file_path);
151
152 dll_file_name_ = (char*) malloc(strlen(dll_file_name) + 1);
153 strcpy(dll_file_name_, dll_file_name);
154
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700155 cout << "ifspec addr load " << if_spec_msg_ << endl;
156 string output;
157 if_spec_msg_->SerializeToString(&output);
158 cout << "loaded text " << output.length() << endl;
159 cout << "loaded text " << strlen(output.c_str()) << endl;
160 cout << "loaded text " << output << endl;
Keun Soo Yim34067de2016-05-17 09:46:37 -0700161
162 module_name_ = (char*) malloc(strlen(module_name) + 1);
163 strcpy(module_name_, module_name);
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700164 return true;
165}
166
167
Keun Soo Yim34067de2016-05-17 09:46:37 -0700168const string empty_string = string();
169
Keun Soo Yim04431a92016-05-24 16:26:42 -0700170const string& SpecificationBuilder::CallFunction(
171 FunctionSpecificationMessage* func_msg) {
Keun Soo Yim52f51662016-05-12 17:23:24 -0700172 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path_)) {
Keun Soo Yim34067de2016-05-17 09:46:37 -0700173 return empty_string;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700174 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700175 cout << __FUNCTION__ << ":" << __LINE__ << " " << "loaded if_spec lib" << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700176
177 FuzzerBase* func_fuzzer = GetFuzzerBase(
178 *if_spec_msg_, dll_file_name_, func_msg->name().c_str());
179 if (!func_fuzzer) {
Keun Soo Yim072337b2016-05-13 15:58:26 -0700180 cerr << "can't find FuzzerBase for " << func_msg->name() << " using "
181 << dll_file_name_ << endl;
Keun Soo Yim34067de2016-05-17 09:46:37 -0700182 return empty_string;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700183 }
184
185 void* result;
Keun Soo Yim04431a92016-05-24 16:26:42 -0700186 func_fuzzer->FunctionCallBegin();
Keun Soo Yim52f51662016-05-12 17:23:24 -0700187 cout << "Call Function " << func_msg->name() << endl;
188 func_fuzzer->Fuzz(*func_msg, &result);
Keun Soo Yim04431a92016-05-24 16:26:42 -0700189 cout << __FUNCTION__ << ": called" << endl;
190
191 // set coverage data.
192 vector<unsigned>* coverage = func_fuzzer->FunctionCallEnd();
193 if (coverage && coverage->size() > 0) {
194 for (unsigned int index = 0; index < coverage->size(); index++) {
195 func_msg->mutable_coverage_data()->Add(coverage->at(index));
196 }
197 }
198
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700199 if (func_msg->return_type().aggregate_type().size() > 0) {
200 // TODO: actually handle this case.
Keun Soo Yim52f51662016-05-12 17:23:24 -0700201 if (result != NULL) {
202 // loads that interface spec and enqueues all functions.
203 cout << __FUNCTION__ << " return type: "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700204 << func_msg->return_type().aggregate_type(0) << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700205 } else {
206 cout << __FUNCTION__ << " return value = NULL" << endl;
207 }
Keun Soo Yim04431a92016-05-24 16:26:42 -0700208 cerr << __FUNCTION__ << " todo: support aggregate" << endl;
209 string* output = new string();
210 google::protobuf::TextFormat::PrintToString(*func_msg, output);
211 return *output;
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700212 } else if (func_msg->return_type().primitive_type().size() > 0) {
213 // TODO handle when the size > 1.
214 if (!strcmp(func_msg->return_type().primitive_type(0).c_str(), "int32_t")) {
215 func_msg->mutable_return_type()->mutable_primitive_value()->Add()->set_int32_t(
Keun Soo Yim34067de2016-05-17 09:46:37 -0700216 *((int*)(&result)));
217 cout << "result " << endl;
218 // todo handle more types;
219 string* output = new string();
Keun Soo Yim04431a92016-05-24 16:26:42 -0700220 google::protobuf::TextFormat::PrintToString(*func_msg, output);
Keun Soo Yim34067de2016-05-17 09:46:37 -0700221 return *output;
222 }
Keun Soo Yim52f51662016-05-12 17:23:24 -0700223 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700224 return *(new string("void"));
Keun Soo Yim52f51662016-05-12 17:23:24 -0700225}
226
227
Keun Soo Yim08400372016-03-03 13:28:51 -0800228bool SpecificationBuilder::Process(
229 const char* dll_file_name,
230 const char* spec_lib_file_path,
231 int target_class,
232 int target_type,
233 float target_version) {
234 vts::InterfaceSpecificationMessage* interface_specification_message =
235 FindInterfaceSpecification(target_class, target_type, target_version);
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700236 cout << "ifspec addr " << interface_specification_message << endl;
237
Keun Soo Yim08400372016-03-03 13:28:51 -0800238 if (!interface_specification_message) {
239 cerr << __FUNCTION__ <<
240 ": no interface specification file found for "
241 << "class " << target_class
242 << " type " << target_type
243 << " version " << target_version << endl;
244 return false;
245 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800246
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700247 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) {
Keun Soo Yim08400372016-03-03 13:28:51 -0800248 return false;
249 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700250
251 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
252 *interface_specification_message, dll_file_name)) return false;
253
Keun Soo Yim8103c912016-04-22 20:07:13 -0700254 for (int i = 0; i < epoch_count_; i++) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700255 // by default, breath-first-searching is used.
256 if (job_queue_.empty()) {
257 cout << "no more job to process; stopping after epoch " << i << endl;
258 break;
259 }
260
261 pair<vts::FunctionSpecificationMessage*, FuzzerBase*> curr_job =
262 job_queue_.front();
263 job_queue_.pop();
264
265 vts::FunctionSpecificationMessage* func_msg = curr_job.first;
266 FuzzerBase* func_fuzzer = curr_job.second;
267
Keun Soo Yimc5d092a2016-04-28 16:51:56 +0000268 void* result;
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700269 cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl;
270 func_fuzzer->Fuzz(*func_msg, &result);
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700271 if (func_msg->return_type().aggregate_type().size() > 0) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700272 if (result != NULL) {
273 // loads that interface spec and enqueues all functions.
274 cout << __FUNCTION__ << " return type: "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700275 << func_msg->return_type().aggregate_type(0) << endl;
276 // TODO: handle the case when size > 1
277 string submodule_name = func_msg->return_type().aggregate_type(0);
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700278 while (!submodule_name.empty()
279 && (std::isspace(submodule_name.back())
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700280 || submodule_name.back() == '*')) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700281 submodule_name.pop_back();
282 }
283 vts::InterfaceSpecificationMessage* iface_spec_msg =
284 FindInterfaceSpecification(
285 target_class, target_type, target_version, submodule_name);
286 if (iface_spec_msg) {
287 cout << __FUNCTION__ << " submodule found - " << submodule_name << endl;
288 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
289 *iface_spec_msg, dll_file_name)) {
290 return false;
291 }
292 } else {
293 cout << __FUNCTION__ << " submodule not found - " << submodule_name << endl;
294 }
295 } else {
296 cout << __FUNCTION__ << " return value = NULL" << endl;
297 }
298 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800299 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700300
Keun Soo Yim08400372016-03-03 13:28:51 -0800301 return true;
302}
303
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700304
305vts::InterfaceSpecificationMessage*
306SpecificationBuilder::GetInterfaceSpecification() const {
307 cout << "ifspec addr get " << if_spec_msg_ << endl;
308 return if_spec_msg_;
309}
310
Keun Soo Yim08400372016-03-03 13:28:51 -0800311} // namespace vts
312} // namespace android