blob: 44db276653cddad801ef396a4f7c3c6bd2fbcc6d [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 Yim2ee90792016-04-27 14:53:54 -070082FuzzerBase* SpecificationBuilder::GetFuzzerBaseAndAddAllFunctionsToQueue(
83 const vts::InterfaceSpecificationMessage& iface_spec_msg,
84 const char* dll_file_name) {
85 FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg);
86 if (!fuzzer) {
87 cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl;
88 return NULL;
89 }
90 if (!fuzzer->LoadTargetComponent(dll_file_name)) return NULL;
91
92 for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) {
93 cout << "Add a job " << func_msg.name() << endl;
94 FunctionSpecificationMessage* func_msg_copy = func_msg.New();
95 func_msg_copy->CopyFrom(func_msg);
96 job_queue_.push(make_pair(func_msg_copy, fuzzer));
97 }
98 return fuzzer;
99}
100
101
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700102bool SpecificationBuilder::LoadTargetComponent(
103 const char* dll_file_name,
104 const char* spec_lib_file_path,
105 int target_class,
106 int target_type,
107 float target_version) {
108 if_spec_msg_ = FindInterfaceSpecification(
109 target_class, target_type, target_version);
110 if (!if_spec_msg_) {
111 cerr << __FUNCTION__ <<
112 ": no interface specification file found for "
113 << "class " << target_class
114 << " type " << target_type
115 << " version " << target_version << endl;
116 return false;
117 }
118 cout << "ifspec addr load " << if_spec_msg_ << endl;
119 string output;
120 if_spec_msg_->SerializeToString(&output);
121 cout << "loaded text " << output.length() << endl;
122 cout << "loaded text " << strlen(output.c_str()) << endl;
123 cout << "loaded text " << output << endl;
124 return true;
125}
126
127
Keun Soo Yim08400372016-03-03 13:28:51 -0800128bool SpecificationBuilder::Process(
129 const char* dll_file_name,
130 const char* spec_lib_file_path,
131 int target_class,
132 int target_type,
133 float target_version) {
134 vts::InterfaceSpecificationMessage* interface_specification_message =
135 FindInterfaceSpecification(target_class, target_type, target_version);
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700136 cout << "ifspec addr " << interface_specification_message << endl;
137
Keun Soo Yim08400372016-03-03 13:28:51 -0800138 if (!interface_specification_message) {
139 cerr << __FUNCTION__ <<
140 ": no interface specification file found for "
141 << "class " << target_class
142 << " type " << target_type
143 << " version " << target_version << endl;
144 return false;
145 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800146
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700147 if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) {
Keun Soo Yim08400372016-03-03 13:28:51 -0800148 return false;
149 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700150
151 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
152 *interface_specification_message, dll_file_name)) return false;
153
Keun Soo Yim8103c912016-04-22 20:07:13 -0700154 for (int i = 0; i < epoch_count_; i++) {
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700155 // by default, breath-first-searching is used.
156 if (job_queue_.empty()) {
157 cout << "no more job to process; stopping after epoch " << i << endl;
158 break;
159 }
160
161 pair<vts::FunctionSpecificationMessage*, FuzzerBase*> curr_job =
162 job_queue_.front();
163 job_queue_.pop();
164
165 vts::FunctionSpecificationMessage* func_msg = curr_job.first;
166 FuzzerBase* func_fuzzer = curr_job.second;
167
Keun Soo Yimc5d092a2016-04-28 16:51:56 +0000168 void* result;
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700169 cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl;
170 func_fuzzer->Fuzz(*func_msg, &result);
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700171 if (func_msg->return_type().has_aggregate_type()) {
172 if (result != NULL) {
173 // loads that interface spec and enqueues all functions.
174 cout << __FUNCTION__ << " return type: "
175 << func_msg->return_type().aggregate_type() << endl;
176 string submodule_name = func_msg->return_type().aggregate_type();
177 while (!submodule_name.empty()
178 && (std::isspace(submodule_name.back())
179 || submodule_name.back() == '*' )) {
180 submodule_name.pop_back();
181 }
182 vts::InterfaceSpecificationMessage* iface_spec_msg =
183 FindInterfaceSpecification(
184 target_class, target_type, target_version, submodule_name);
185 if (iface_spec_msg) {
186 cout << __FUNCTION__ << " submodule found - " << submodule_name << endl;
187 if (!GetFuzzerBaseAndAddAllFunctionsToQueue(
188 *iface_spec_msg, dll_file_name)) {
189 return false;
190 }
191 } else {
192 cout << __FUNCTION__ << " submodule not found - " << submodule_name << endl;
193 }
194 } else {
195 cout << __FUNCTION__ << " return value = NULL" << endl;
196 }
197 }
Keun Soo Yim08400372016-03-03 13:28:51 -0800198 }
Keun Soo Yim2ee90792016-04-27 14:53:54 -0700199
Keun Soo Yim08400372016-03-03 13:28:51 -0800200 return true;
201}
202
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700203
204vts::InterfaceSpecificationMessage*
205SpecificationBuilder::GetInterfaceSpecification() const {
206 cout << "ifspec addr get " << if_spec_msg_ << endl;
207 return if_spec_msg_;
208}
209
Keun Soo Yim08400372016-03-03 13:28:51 -0800210} // namespace vts
211} // namespace android