Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 22 | #include <queue> |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 23 | #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 Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 30 | #include <google/protobuf/text_format.h> |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace vts { |
| 34 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 35 | SpecificationBuilder::SpecificationBuilder( |
| 36 | const string dir_path, int epoch_count) |
| 37 | : dir_path_(dir_path), |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 38 | epoch_count_(epoch_count), |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 39 | if_spec_msg_(NULL), |
| 40 | module_name_(NULL) {} |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 41 | |
| 42 | |
| 43 | vts::InterfaceSpecificationMessage* |
| 44 | SpecificationBuilder::FindInterfaceSpecification( |
| 45 | const int target_class, |
| 46 | const int target_type, |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 47 | const float target_version, |
| 48 | const string submodule_name) { |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 49 | 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 Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 64 | if (message->component_type() == target_type |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 65 | && message->component_type_version() == target_version) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 66 | 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 Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 72 | closedir(dir); |
| 73 | return message; |
| 74 | } |
| 75 | } |
| 76 | delete message; |
| 77 | } |
| 78 | } |
| 79 | closedir(dir); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 84 | FuzzerBase* SpecificationBuilder::GetFuzzerBase( |
| 85 | const vts::InterfaceSpecificationMessage& iface_spec_msg, |
| 86 | const char* dll_file_name, |
| 87 | const char* target_func_name) { |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 88 | cout << __FUNCTION__ << ":" << __LINE__ << " " << "entry" << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 89 | 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 Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 94 | 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 Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 98 | return NULL; |
| 99 | } |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 100 | cout << __FUNCTION__ << ":" << __LINE__ << " " << "loaded target comp" << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 101 | |
| 102 | for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) { |
Keun Soo Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 103 | cout << "checking " << func_msg.name() << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 104 | if (!strcmp(target_func_name, func_msg.name().c_str())) { |
| 105 | return fuzzer; |
| 106 | } |
| 107 | } |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 112 | FuzzerBase* 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 Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 132 | bool 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 Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 137 | float target_version, |
| 138 | const char* module_name) { |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 139 | 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 Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 149 | 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 Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 155 | 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 Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 161 | |
| 162 | module_name_ = (char*) malloc(strlen(module_name) + 1); |
| 163 | strcpy(module_name_, module_name); |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 168 | const string empty_string = string(); |
| 169 | |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame^] | 170 | const string& SpecificationBuilder::CallFunction( |
| 171 | FunctionSpecificationMessage* func_msg) { |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 172 | if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path_)) { |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 173 | return empty_string; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 174 | } |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 175 | cout << __FUNCTION__ << ":" << __LINE__ << " " << "loaded if_spec lib" << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 176 | |
| 177 | FuzzerBase* func_fuzzer = GetFuzzerBase( |
| 178 | *if_spec_msg_, dll_file_name_, func_msg->name().c_str()); |
| 179 | if (!func_fuzzer) { |
Keun Soo Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 180 | cerr << "can't find FuzzerBase for " << func_msg->name() << " using " |
| 181 | << dll_file_name_ << endl; |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 182 | return empty_string; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void* result; |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame^] | 186 | func_fuzzer->FunctionCallBegin(); |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 187 | cout << "Call Function " << func_msg->name() << endl; |
| 188 | func_fuzzer->Fuzz(*func_msg, &result); |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame^] | 189 | 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 Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 199 | if (func_msg->return_type().aggregate_type().size() > 0) { |
| 200 | // TODO: actually handle this case. |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 201 | if (result != NULL) { |
| 202 | // loads that interface spec and enqueues all functions. |
| 203 | cout << __FUNCTION__ << " return type: " |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 204 | << func_msg->return_type().aggregate_type(0) << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 205 | } else { |
| 206 | cout << __FUNCTION__ << " return value = NULL" << endl; |
| 207 | } |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame^] | 208 | cerr << __FUNCTION__ << " todo: support aggregate" << endl; |
| 209 | string* output = new string(); |
| 210 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
| 211 | return *output; |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 212 | } 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 Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 216 | *((int*)(&result))); |
| 217 | cout << "result " << endl; |
| 218 | // todo handle more types; |
| 219 | string* output = new string(); |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame^] | 220 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 221 | return *output; |
| 222 | } |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 223 | } |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 224 | return *(new string("void")); |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 228 | bool 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 Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 236 | cout << "ifspec addr " << interface_specification_message << endl; |
| 237 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 238 | 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 Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 246 | |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 247 | if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) { |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 248 | return false; |
| 249 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 250 | |
| 251 | if (!GetFuzzerBaseAndAddAllFunctionsToQueue( |
| 252 | *interface_specification_message, dll_file_name)) return false; |
| 253 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 254 | for (int i = 0; i < epoch_count_; i++) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 255 | // 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 Yim | c5d092a | 2016-04-28 16:51:56 +0000 | [diff] [blame] | 268 | void* result; |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 269 | cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl; |
| 270 | func_fuzzer->Fuzz(*func_msg, &result); |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 271 | if (func_msg->return_type().aggregate_type().size() > 0) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 272 | if (result != NULL) { |
| 273 | // loads that interface spec and enqueues all functions. |
| 274 | cout << __FUNCTION__ << " return type: " |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 275 | << 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 Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 278 | while (!submodule_name.empty() |
| 279 | && (std::isspace(submodule_name.back()) |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 280 | || submodule_name.back() == '*')) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 281 | 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 Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 299 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 300 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 304 | |
| 305 | vts::InterfaceSpecificationMessage* |
| 306 | SpecificationBuilder::GetInterfaceSpecification() const { |
| 307 | cout << "ifspec addr get " << if_spec_msg_ << endl; |
| 308 | return if_spec_msg_; |
| 309 | } |
| 310 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 311 | } // namespace vts |
| 312 | } // namespace android |