blob: b2161150ab0a243eeae8bade8b0f08b541304424 [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
170const string& SpecificationBuilder::CallFunction(FunctionSpecificationMessage* func_msg) {
Keun Soo Yim52f51662016-05-12 17:23:24 -0700171 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path_)) {
Keun Soo Yim34067de2016-05-17 09:46:37 -0700172 return empty_string;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700173 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700174 cout << __FUNCTION__ << ":" << __LINE__ << " " << "loaded if_spec lib" << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700175
176 FuzzerBase* func_fuzzer = GetFuzzerBase(
177 *if_spec_msg_, dll_file_name_, func_msg->name().c_str());
178 if (!func_fuzzer) {
Keun Soo Yim072337b2016-05-13 15:58:26 -0700179 cerr << "can't find FuzzerBase for " << func_msg->name() << " using "
180 << dll_file_name_ << endl;
Keun Soo Yim34067de2016-05-17 09:46:37 -0700181 return empty_string;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700182 }
183
184 void* result;
185 cout << "Call Function " << func_msg->name() << endl;
186 func_fuzzer->Fuzz(*func_msg, &result);
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700187 if (func_msg->return_type().aggregate_type().size() > 0) {
188 // TODO: actually handle this case.
Keun Soo Yim52f51662016-05-12 17:23:24 -0700189 if (result != NULL) {
190 // loads that interface spec and enqueues all functions.
191 cout << __FUNCTION__ << " return type: "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700192 << func_msg->return_type().aggregate_type(0) << endl;
Keun Soo Yim52f51662016-05-12 17:23:24 -0700193 } else {
194 cout << __FUNCTION__ << " return value = NULL" << endl;
195 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700196 return *(new string("todo: support aggregate"));
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700197 } else if (func_msg->return_type().primitive_type().size() > 0) {
198 // TODO handle when the size > 1.
199 if (!strcmp(func_msg->return_type().primitive_type(0).c_str(), "int32_t")) {
200 func_msg->mutable_return_type()->mutable_primitive_value()->Add()->set_int32_t(
Keun Soo Yim34067de2016-05-17 09:46:37 -0700201 *((int*)(&result)));
202 cout << "result " << endl;
203 // todo handle more types;
204 string* output = new string();
205 google::protobuf::TextFormat::PrintToString(func_msg->return_type(),
206 output);
207 return *output;
208 }
Keun Soo Yim52f51662016-05-12 17:23:24 -0700209 }
Keun Soo Yim34067de2016-05-17 09:46:37 -0700210 return *(new string("void"));
Keun Soo Yim52f51662016-05-12 17:23:24 -0700211}
212
213
Keun Soo Yim08400372016-03-03 13:28:51 -0800214bool SpecificationBuilder::Process(
215 const char* dll_file_name,
216 const char* spec_lib_file_path,
217 int target_class,
218 int target_type,
219 float target_version) {
220 vts::InterfaceSpecificationMessage* interface_specification_message =
221 FindInterfaceSpecification(target_class, target_type, target_version);
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700222 cout << "ifspec addr " << interface_specification_message << endl;
223
Keun Soo Yim08400372016-03-03 13:28:51 -0800224 if (!interface_specification_message) {
225 cerr << __FUNCTION__ <<
226 ": no interface specification file found for "
227 << "class " << target_class
228 << " type " << target_type
229 << " version " << target_version << endl;
230 return false;
231 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800232
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700233 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) {
Keun Soo Yim08400372016-03-03 13:28:51 -0800234 return false;
235 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700236
237 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
238 *interface_specification_message, dll_file_name)) return false;
239
Keun Soo Yim8103c912016-04-22 20:07:13 -0700240 for (int i = 0; i < epoch_count_; i++) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700241 // by default, breath-first-searching is used.
242 if (job_queue_.empty()) {
243 cout << "no more job to process; stopping after epoch " << i << endl;
244 break;
245 }
246
247 pair<vts::FunctionSpecificationMessage*, FuzzerBase*> curr_job =
248 job_queue_.front();
249 job_queue_.pop();
250
251 vts::FunctionSpecificationMessage* func_msg = curr_job.first;
252 FuzzerBase* func_fuzzer = curr_job.second;
253
Keun Soo Yimc5d092a2016-04-28 16:51:56 +0000254 void* result;
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700255 cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl;
256 func_fuzzer->Fuzz(*func_msg, &result);
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700257 if (func_msg->return_type().aggregate_type().size() > 0) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700258 if (result != NULL) {
259 // loads that interface spec and enqueues all functions.
260 cout << __FUNCTION__ << " return type: "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700261 << func_msg->return_type().aggregate_type(0) << endl;
262 // TODO: handle the case when size > 1
263 string submodule_name = func_msg->return_type().aggregate_type(0);
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700264 while (!submodule_name.empty()
265 && (std::isspace(submodule_name.back())
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700266 || submodule_name.back() == '*')) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700267 submodule_name.pop_back();
268 }
269 vts::InterfaceSpecificationMessage* iface_spec_msg =
270 FindInterfaceSpecification(
271 target_class, target_type, target_version, submodule_name);
272 if (iface_spec_msg) {
273 cout << __FUNCTION__ << " submodule found - " << submodule_name << endl;
274 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
275 *iface_spec_msg, dll_file_name)) {
276 return false;
277 }
278 } else {
279 cout << __FUNCTION__ << " submodule not found - " << submodule_name << endl;
280 }
281 } else {
282 cout << __FUNCTION__ << " return value = NULL" << endl;
283 }
284 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800285 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700286
Keun Soo Yim08400372016-03-03 13:28:51 -0800287 return true;
288}
289
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700290
291vts::InterfaceSpecificationMessage*
292SpecificationBuilder::GetInterfaceSpecification() const {
293 cout << "ifspec addr get " << if_spec_msg_ << endl;
294 return if_spec_msg_;
295}
296
Keun Soo Yim08400372016-03-03 13:28:51 -0800297} // namespace vts
298} // namespace android