blob: b7ea74c0590d14782b4a657bb2014f89bc16da31 [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"
30
31namespace android {
32namespace vts {
33
Keun Soo Yim8103c912016-04-22 20:07:13 -070034SpecificationBuilder::SpecificationBuilder(
35 const string dir_path, int epoch_count)
36 : dir_path_(dir_path),
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -070037 epoch_count_(epoch_count),
38 if_spec_msg_(NULL) {}
Keun Soo Yim08400372016-03-03 13:28:51 -080039
40
41vts::InterfaceSpecificationMessage*
42SpecificationBuilder::FindInterfaceSpecification(
43 const int target_class,
44 const int target_type,
Keun Soo Yim2ee90792016-04-27 14:53:54 -070045 const float target_version,
46 const string submodule_name) {
Keun Soo Yim08400372016-03-03 13:28:51 -080047 DIR* dir;
48 struct dirent* ent;
49
50 if (!(dir = opendir(dir_path_.c_str()))) {
51 cerr << __FUNCTION__ << ": Can't opendir " << dir_path_ << endl;
52 return NULL;
53 }
54
55 while ((ent = readdir(dir))) {
56 if (string(ent->d_name).find(SPEC_FILE_EXT) != std::string::npos) {
57 cout << __FUNCTION__ << ": Checking a file " << ent->d_name << endl;
58 const string file_path = string(dir_path_) + "/" + string(ent->d_name);
59 vts::InterfaceSpecificationMessage* message =
60 new vts::InterfaceSpecificationMessage();
61 if (InterfaceSpecificationParser::parse(file_path.c_str(), message)) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -070062 if (message->component_type() == target_type
Keun Soo Yim08400372016-03-03 13:28:51 -080063 && message->component_type_version() == target_version) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -070064 if (submodule_name.length() > 0) {
65 if (message->component_class() != HAL_SUBMODULE
66 || message->original_data_structure_name() != submodule_name) {
67 continue;
68 }
69 } else if (message->component_class() != target_class) continue;
Keun Soo Yim08400372016-03-03 13:28:51 -080070 closedir(dir);
71 return message;
72 }
73 }
74 delete message;
75 }
76 }
77 closedir(dir);
78 return NULL;
79}
80
81
Keun Soo Yim52f51662016-05-12 17:23:24 -070082FuzzerBase* SpecificationBuilder::GetFuzzerBase(
83 const vts::InterfaceSpecificationMessage& iface_spec_msg,
84 const char* dll_file_name,
85 const char* target_func_name) {
86 FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg);
87 if (!fuzzer) {
88 cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl;
89 return NULL;
90 }
91 if (!fuzzer->LoadTargetComponent(dll_file_name)) return NULL;
92
93 for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) {
94 if (!strcmp(target_func_name, func_msg.name().c_str())) {
95 return fuzzer;
96 }
97 }
98 return NULL;
99}
100
101
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700102FuzzerBase* SpecificationBuilder::GetFuzzerBaseAndAddAllFunctionsToQueue(
103 const vts::InterfaceSpecificationMessage& iface_spec_msg,
104 const char* dll_file_name) {
105 FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg);
106 if (!fuzzer) {
107 cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl;
108 return NULL;
109 }
110 if (!fuzzer->LoadTargetComponent(dll_file_name)) return NULL;
111
112 for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) {
113 cout << "Add a job " << func_msg.name() << endl;
114 FunctionSpecificationMessage* func_msg_copy = func_msg.New();
115 func_msg_copy->CopyFrom(func_msg);
116 job_queue_.push(make_pair(func_msg_copy, fuzzer));
117 }
118 return fuzzer;
119}
120
121
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700122bool SpecificationBuilder::LoadTargetComponent(
123 const char* dll_file_name,
124 const char* spec_lib_file_path,
125 int target_class,
126 int target_type,
127 float target_version) {
128 if_spec_msg_ = FindInterfaceSpecification(
129 target_class, target_type, target_version);
130 if (!if_spec_msg_) {
131 cerr << __FUNCTION__ <<
132 ": no interface specification file found for "
133 << "class " << target_class
134 << " type " << target_type
135 << " version " << target_version << endl;
136 return false;
137 }
Keun Soo Yim52f51662016-05-12 17:23:24 -0700138 spec_lib_file_path_ = (char*) malloc(strlen(spec_lib_file_path) + 1);
139 strcpy(spec_lib_file_path_, spec_lib_file_path);
140
141 dll_file_name_ = (char*) malloc(strlen(dll_file_name) + 1);
142 strcpy(dll_file_name_, dll_file_name);
143
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700144 cout << "ifspec addr load " << if_spec_msg_ << endl;
145 string output;
146 if_spec_msg_->SerializeToString(&output);
147 cout << "loaded text " << output.length() << endl;
148 cout << "loaded text " << strlen(output.c_str()) << endl;
149 cout << "loaded text " << output << endl;
150 return true;
151}
152
153
Keun Soo Yim52f51662016-05-12 17:23:24 -0700154bool SpecificationBuilder::CallFunction(FunctionSpecificationMessage* func_msg) {
155 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path_)) {
156 return false;
157 }
158
159 FuzzerBase* func_fuzzer = GetFuzzerBase(
160 *if_spec_msg_, dll_file_name_, func_msg->name().c_str());
161 if (!func_fuzzer) {
162 cerr << "can't find FuzzerBase" << endl;
163 return false;
164 }
165
166 void* result;
167 cout << "Call Function " << func_msg->name() << endl;
168 func_fuzzer->Fuzz(*func_msg, &result);
169 if (func_msg->return_type().has_aggregate_type()) {
170 if (result != NULL) {
171 // loads that interface spec and enqueues all functions.
172 cout << __FUNCTION__ << " return type: "
173 << func_msg->return_type().aggregate_type() << endl;
174 } else {
175 cout << __FUNCTION__ << " return value = NULL" << endl;
176 }
177 }
178
179 return true;
180}
181
182
Keun Soo Yim08400372016-03-03 13:28:51 -0800183bool SpecificationBuilder::Process(
184 const char* dll_file_name,
185 const char* spec_lib_file_path,
186 int target_class,
187 int target_type,
188 float target_version) {
189 vts::InterfaceSpecificationMessage* interface_specification_message =
190 FindInterfaceSpecification(target_class, target_type, target_version);
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700191 cout << "ifspec addr " << interface_specification_message << endl;
192
Keun Soo Yim08400372016-03-03 13:28:51 -0800193 if (!interface_specification_message) {
194 cerr << __FUNCTION__ <<
195 ": no interface specification file found for "
196 << "class " << target_class
197 << " type " << target_type
198 << " version " << target_version << endl;
199 return false;
200 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800201
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700202 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) {
Keun Soo Yim08400372016-03-03 13:28:51 -0800203 return false;
204 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700205
206 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
207 *interface_specification_message, dll_file_name)) return false;
208
Keun Soo Yim8103c912016-04-22 20:07:13 -0700209 for (int i = 0; i < epoch_count_; i++) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700210 // by default, breath-first-searching is used.
211 if (job_queue_.empty()) {
212 cout << "no more job to process; stopping after epoch " << i << endl;
213 break;
214 }
215
216 pair<vts::FunctionSpecificationMessage*, FuzzerBase*> curr_job =
217 job_queue_.front();
218 job_queue_.pop();
219
220 vts::FunctionSpecificationMessage* func_msg = curr_job.first;
221 FuzzerBase* func_fuzzer = curr_job.second;
222
Keun Soo Yimc5d092a2016-04-28 16:51:56 +0000223 void* result;
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700224 cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl;
225 func_fuzzer->Fuzz(*func_msg, &result);
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700226 if (func_msg->return_type().has_aggregate_type()) {
227 if (result != NULL) {
228 // loads that interface spec and enqueues all functions.
229 cout << __FUNCTION__ << " return type: "
230 << func_msg->return_type().aggregate_type() << endl;
231 string submodule_name = func_msg->return_type().aggregate_type();
232 while (!submodule_name.empty()
233 && (std::isspace(submodule_name.back())
234 || submodule_name.back() == '*' )) {
235 submodule_name.pop_back();
236 }
237 vts::InterfaceSpecificationMessage* iface_spec_msg =
238 FindInterfaceSpecification(
239 target_class, target_type, target_version, submodule_name);
240 if (iface_spec_msg) {
241 cout << __FUNCTION__ << " submodule found - " << submodule_name << endl;
242 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
243 *iface_spec_msg, dll_file_name)) {
244 return false;
245 }
246 } else {
247 cout << __FUNCTION__ << " submodule not found - " << submodule_name << endl;
248 }
249 } else {
250 cout << __FUNCTION__ << " return value = NULL" << endl;
251 }
252 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800253 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700254
Keun Soo Yim08400372016-03-03 13:28:51 -0800255 return true;
256}
257
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700258
259vts::InterfaceSpecificationMessage*
260SpecificationBuilder::GetInterfaceSpecification() const {
261 cout << "ifspec addr get " << if_spec_msg_ << endl;
262 return if_spec_msg_;
263}
264
Keun Soo Yim08400372016-03-03 13:28:51 -0800265} // namespace vts
266} // namespace android