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 | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 155 | // cout << "ifspec addr load at " << if_spec_msg_ << endl; |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 156 | string output; |
| 157 | if_spec_msg_->SerializeToString(&output); |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 158 | cout << "loaded ifspec length " << 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 | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 164 | cout << __FUNCTION__ << ":" << __LINE__ << " module_name " << module_name_ |
| 165 | << endl; |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
| 169 | |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 170 | const string empty_string = string(); |
| 171 | |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 172 | const string& SpecificationBuilder::CallFunction( |
| 173 | FunctionSpecificationMessage* func_msg) { |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 174 | if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path_)) { |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 175 | return empty_string; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 176 | } |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 177 | cout << __FUNCTION__ << ":" << __LINE__ << " " << "loaded if_spec lib" << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 178 | |
| 179 | FuzzerBase* func_fuzzer = GetFuzzerBase( |
| 180 | *if_spec_msg_, dll_file_name_, func_msg->name().c_str()); |
| 181 | if (!func_fuzzer) { |
Keun Soo Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 182 | cerr << "can't find FuzzerBase for " << func_msg->name() << " using " |
| 183 | << dll_file_name_ << endl; |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 184 | return empty_string; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void* result; |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 188 | func_fuzzer->FunctionCallBegin(); |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 189 | cout << "Call Function " << func_msg->name() << endl; |
| 190 | func_fuzzer->Fuzz(*func_msg, &result); |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 191 | cout << __FUNCTION__ << ": called" << endl; |
| 192 | |
| 193 | // set coverage data. |
| 194 | vector<unsigned>* coverage = func_fuzzer->FunctionCallEnd(); |
| 195 | if (coverage && coverage->size() > 0) { |
| 196 | for (unsigned int index = 0; index < coverage->size(); index++) { |
| 197 | func_msg->mutable_coverage_data()->Add(coverage->at(index)); |
| 198 | } |
| 199 | } |
| 200 | |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 201 | if (func_msg->return_type().aggregate_type().size() > 0) { |
| 202 | // TODO: actually handle this case. |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 203 | if (result != NULL) { |
| 204 | // loads that interface spec and enqueues all functions. |
| 205 | cout << __FUNCTION__ << " return type: " |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 206 | << func_msg->return_type().aggregate_type(0) << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 207 | } else { |
| 208 | cout << __FUNCTION__ << " return value = NULL" << endl; |
| 209 | } |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 210 | cerr << __FUNCTION__ << " todo: support aggregate" << endl; |
| 211 | string* output = new string(); |
| 212 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
| 213 | return *output; |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 214 | } else if (func_msg->return_type().primitive_type().size() > 0) { |
| 215 | // TODO handle when the size > 1. |
| 216 | if (!strcmp(func_msg->return_type().primitive_type(0).c_str(), "int32_t")) { |
| 217 | 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] | 218 | *((int*)(&result))); |
| 219 | cout << "result " << endl; |
| 220 | // todo handle more types; |
| 221 | string* output = new string(); |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 222 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 223 | return *output; |
| 224 | } |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 225 | } |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 226 | return *(new string("void")); |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 230 | bool SpecificationBuilder::Process( |
| 231 | const char* dll_file_name, |
| 232 | const char* spec_lib_file_path, |
| 233 | int target_class, |
| 234 | int target_type, |
| 235 | float target_version) { |
| 236 | vts::InterfaceSpecificationMessage* interface_specification_message = |
| 237 | FindInterfaceSpecification(target_class, target_type, target_version); |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 238 | cout << "ifspec addr " << interface_specification_message << endl; |
| 239 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 240 | if (!interface_specification_message) { |
| 241 | cerr << __FUNCTION__ << |
| 242 | ": no interface specification file found for " |
| 243 | << "class " << target_class |
| 244 | << " type " << target_type |
| 245 | << " version " << target_version << endl; |
| 246 | return false; |
| 247 | } |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 248 | |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 249 | if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) { |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 250 | return false; |
| 251 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 252 | |
| 253 | if (!GetFuzzerBaseAndAddAllFunctionsToQueue( |
| 254 | *interface_specification_message, dll_file_name)) return false; |
| 255 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 256 | for (int i = 0; i < epoch_count_; i++) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 257 | // by default, breath-first-searching is used. |
| 258 | if (job_queue_.empty()) { |
| 259 | cout << "no more job to process; stopping after epoch " << i << endl; |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | pair<vts::FunctionSpecificationMessage*, FuzzerBase*> curr_job = |
| 264 | job_queue_.front(); |
| 265 | job_queue_.pop(); |
| 266 | |
| 267 | vts::FunctionSpecificationMessage* func_msg = curr_job.first; |
| 268 | FuzzerBase* func_fuzzer = curr_job.second; |
| 269 | |
Keun Soo Yim | c5d092a | 2016-04-28 16:51:56 +0000 | [diff] [blame] | 270 | void* result; |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 271 | cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl; |
| 272 | func_fuzzer->Fuzz(*func_msg, &result); |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 273 | if (func_msg->return_type().aggregate_type().size() > 0) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 274 | if (result != NULL) { |
| 275 | // loads that interface spec and enqueues all functions. |
| 276 | cout << __FUNCTION__ << " return type: " |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 277 | << func_msg->return_type().aggregate_type(0) << endl; |
| 278 | // TODO: handle the case when size > 1 |
| 279 | string submodule_name = func_msg->return_type().aggregate_type(0); |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 280 | while (!submodule_name.empty() |
| 281 | && (std::isspace(submodule_name.back()) |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 282 | || submodule_name.back() == '*')) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 283 | submodule_name.pop_back(); |
| 284 | } |
| 285 | vts::InterfaceSpecificationMessage* iface_spec_msg = |
| 286 | FindInterfaceSpecification( |
| 287 | target_class, target_type, target_version, submodule_name); |
| 288 | if (iface_spec_msg) { |
| 289 | cout << __FUNCTION__ << " submodule found - " << submodule_name << endl; |
| 290 | if (!GetFuzzerBaseAndAddAllFunctionsToQueue( |
| 291 | *iface_spec_msg, dll_file_name)) { |
| 292 | return false; |
| 293 | } |
| 294 | } else { |
| 295 | cout << __FUNCTION__ << " submodule not found - " << submodule_name << endl; |
| 296 | } |
| 297 | } else { |
| 298 | cout << __FUNCTION__ << " return value = NULL" << endl; |
| 299 | } |
| 300 | } |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 301 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 302 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 303 | return true; |
| 304 | } |
| 305 | |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 306 | |
| 307 | vts::InterfaceSpecificationMessage* |
| 308 | SpecificationBuilder::GetInterfaceSpecification() const { |
| 309 | cout << "ifspec addr get " << if_spec_msg_ << endl; |
| 310 | return if_spec_msg_; |
| 311 | } |
| 312 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 313 | } // namespace vts |
| 314 | } // namespace android |