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 | |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame] | 29 | #include "test/vts/runners/host/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( |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 36 | const string dir_path, int epoch_count, int agent_port) |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 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), |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 40 | module_name_(NULL), |
| 41 | agent_port_(agent_port) {} |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 42 | |
| 43 | |
| 44 | vts::InterfaceSpecificationMessage* |
| 45 | SpecificationBuilder::FindInterfaceSpecification( |
| 46 | const int target_class, |
| 47 | const int target_type, |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 48 | const float target_version, |
| 49 | const string submodule_name) { |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 50 | DIR* dir; |
| 51 | struct dirent* ent; |
| 52 | |
| 53 | if (!(dir = opendir(dir_path_.c_str()))) { |
| 54 | cerr << __FUNCTION__ << ": Can't opendir " << dir_path_ << endl; |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | while ((ent = readdir(dir))) { |
| 59 | if (string(ent->d_name).find(SPEC_FILE_EXT) != std::string::npos) { |
| 60 | cout << __FUNCTION__ << ": Checking a file " << ent->d_name << endl; |
| 61 | const string file_path = string(dir_path_) + "/" + string(ent->d_name); |
| 62 | vts::InterfaceSpecificationMessage* message = |
| 63 | new vts::InterfaceSpecificationMessage(); |
| 64 | if (InterfaceSpecificationParser::parse(file_path.c_str(), message)) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 65 | if (message->component_type() == target_type |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 66 | && message->component_type_version() == target_version) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 67 | if (submodule_name.length() > 0) { |
| 68 | if (message->component_class() != HAL_SUBMODULE |
| 69 | || message->original_data_structure_name() != submodule_name) { |
| 70 | continue; |
| 71 | } |
| 72 | } else if (message->component_class() != target_class) continue; |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 73 | closedir(dir); |
| 74 | return message; |
| 75 | } |
| 76 | } |
| 77 | delete message; |
| 78 | } |
| 79 | } |
| 80 | closedir(dir); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 85 | FuzzerBase* SpecificationBuilder::GetFuzzerBase( |
| 86 | const vts::InterfaceSpecificationMessage& iface_spec_msg, |
| 87 | const char* dll_file_name, |
| 88 | const char* target_func_name) { |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 89 | cout << __func__ << ":" << __LINE__ << " " << "entry" << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 90 | FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg); |
| 91 | if (!fuzzer) { |
| 92 | cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl; |
| 93 | return NULL; |
| 94 | } |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 95 | |
| 96 | // TODO: don't load multiple times. reuse FuzzerBase*. |
| 97 | cout << __func__ << ":" << __LINE__ << " " << "got fuzzer" << endl; |
| 98 | if (!fuzzer->LoadTargetComponent(dll_file_name)) { |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 99 | cerr << __FUNCTION__ << ": couldn't load target component file, " |
| 100 | << dll_file_name << endl; |
Keun Soo Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 103 | cout << __func__ << ":" << __LINE__ << " " << "loaded target comp" << endl; |
| 104 | |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 105 | return fuzzer; |
| 106 | /* |
| 107 | * TODO: now always return the fuzzer. this change is due to the difficulty |
| 108 | * in checking nested apis although that's possible. need to check whether |
| 109 | * Fuzz() found the function, while still distinguishing the difference |
| 110 | * between that and defined but non-set api. |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 111 | if (!strcmp(target_func_name, "#Open")) return fuzzer; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 112 | |
| 113 | for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) { |
Keun Soo Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 114 | cout << "checking " << func_msg.name() << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 115 | if (!strcmp(target_func_name, func_msg.name().c_str())) { |
| 116 | return fuzzer; |
| 117 | } |
| 118 | } |
| 119 | return NULL; |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 120 | */ |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 124 | FuzzerBase* SpecificationBuilder::GetFuzzerBaseAndAddAllFunctionsToQueue( |
| 125 | const vts::InterfaceSpecificationMessage& iface_spec_msg, |
| 126 | const char* dll_file_name) { |
| 127 | FuzzerBase* fuzzer = wrapper_.GetFuzzer(iface_spec_msg); |
| 128 | if (!fuzzer) { |
| 129 | cerr << __FUNCTION__ << ": couldn't get a fuzzer base class" << endl; |
| 130 | return NULL; |
| 131 | } |
| 132 | if (!fuzzer->LoadTargetComponent(dll_file_name)) return NULL; |
| 133 | |
| 134 | for (const vts::FunctionSpecificationMessage& func_msg : iface_spec_msg.api()) { |
| 135 | cout << "Add a job " << func_msg.name() << endl; |
| 136 | FunctionSpecificationMessage* func_msg_copy = func_msg.New(); |
| 137 | func_msg_copy->CopyFrom(func_msg); |
| 138 | job_queue_.push(make_pair(func_msg_copy, fuzzer)); |
| 139 | } |
| 140 | return fuzzer; |
| 141 | } |
| 142 | |
| 143 | |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 144 | bool SpecificationBuilder::LoadTargetComponent( |
| 145 | const char* dll_file_name, |
| 146 | const char* spec_lib_file_path, |
| 147 | int target_class, |
| 148 | int target_type, |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 149 | float target_version, |
| 150 | const char* module_name) { |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 151 | if_spec_msg_ = FindInterfaceSpecification( |
| 152 | target_class, target_type, target_version); |
| 153 | if (!if_spec_msg_) { |
| 154 | cerr << __FUNCTION__ << |
| 155 | ": no interface specification file found for " |
| 156 | << "class " << target_class |
| 157 | << " type " << target_type |
| 158 | << " version " << target_version << endl; |
| 159 | return false; |
| 160 | } |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 161 | spec_lib_file_path_ = (char*) malloc(strlen(spec_lib_file_path) + 1); |
| 162 | strcpy(spec_lib_file_path_, spec_lib_file_path); |
| 163 | |
| 164 | dll_file_name_ = (char*) malloc(strlen(dll_file_name) + 1); |
| 165 | strcpy(dll_file_name_, dll_file_name); |
| 166 | |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame] | 167 | // cout << "ifspec addr load at " << if_spec_msg_ << endl; |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 168 | string output; |
| 169 | if_spec_msg_->SerializeToString(&output); |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame] | 170 | cout << "loaded ifspec length " << output.length() << endl; |
| 171 | // cout << "loaded text " << strlen(output.c_str()) << endl; |
| 172 | // cout << "loaded text " << output << endl; |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 173 | |
| 174 | module_name_ = (char*) malloc(strlen(module_name) + 1); |
| 175 | strcpy(module_name_, module_name); |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame] | 176 | cout << __FUNCTION__ << ":" << __LINE__ << " module_name " << module_name_ |
| 177 | << endl; |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 182 | const string empty_string = string(); |
| 183 | |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 184 | const string& SpecificationBuilder::CallFunction( |
| 185 | FunctionSpecificationMessage* func_msg) { |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 186 | if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path_)) { |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 187 | return empty_string; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 188 | } |
Keun Soo Yim | 7ad6492 | 2016-06-07 18:14:40 -0700 | [diff] [blame] | 189 | cout << __func__ << " " << "loaded if_spec lib" << endl; |
| 190 | cout << __func__ << " " << dll_file_name_ << " " << func_msg->name() << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 191 | |
| 192 | FuzzerBase* func_fuzzer = GetFuzzerBase( |
| 193 | *if_spec_msg_, dll_file_name_, func_msg->name().c_str()); |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 194 | cout << __func__ << ":" << __LINE__ << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 195 | if (!func_fuzzer) { |
Keun Soo Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 196 | cerr << "can't find FuzzerBase for " << func_msg->name() << " using " |
| 197 | << dll_file_name_ << endl; |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 198 | return empty_string; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 201 | if (func_msg->name() == "#Open") { |
| 202 | cout << __func__ << ":" << __LINE__ << endl; |
| 203 | if (func_msg->arg().size() > 0) { |
| 204 | cout << __func__ << " " << func_msg->arg(0).primitive_value(0).bytes().c_str() << endl; |
| 205 | func_fuzzer->OpenConventionalHal( |
| 206 | func_msg->arg(0).primitive_value(0).bytes().c_str()); |
| 207 | } else { |
| 208 | cout << __func__ << " no arg" << endl; |
| 209 | func_fuzzer->OpenConventionalHal(); |
| 210 | } |
| 211 | cout << __func__ << " opened" << endl; |
| 212 | // return the return value from open; |
| 213 | if (func_msg->return_type().primitive_type().size() > 0) { |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 214 | cout << __func__ << " return_type exists" << endl; |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 215 | // TODO handle when the size > 1. |
| 216 | if (!strcmp(func_msg->return_type().primitive_type(0).c_str(), "int32_t")) { |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 217 | cout << __func__ << " return_type is int32_t" << endl; |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 218 | func_msg->mutable_return_type()->mutable_primitive_value()->Add()->set_int32_t(0); |
| 219 | cout << "result " << endl; |
| 220 | // todo handle more types; |
| 221 | string* output = new string(); |
| 222 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
| 223 | return *output; |
| 224 | } |
| 225 | } |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 226 | cerr << __func__ << " return_type unknown" << endl; |
| 227 | string* output = new string(); |
| 228 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
| 229 | return *output; |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 230 | } |
| 231 | cout << __func__ << ":" << __LINE__ << endl; |
| 232 | |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 233 | void* result; |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 234 | func_fuzzer->FunctionCallBegin(); |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 235 | cout << __func__ << " Call Function " << func_msg->name() << endl; |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 236 | if (!func_fuzzer->Fuzz(func_msg, &result, agent_port_)) { |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 237 | cout << __func__ << " function not found - todo handle more explicitly" << endl; |
| 238 | return *(new string("error")); |
| 239 | } |
| 240 | cout << __func__ << ": called" << endl; |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 241 | |
| 242 | // set coverage data. |
| 243 | vector<unsigned>* coverage = func_fuzzer->FunctionCallEnd(); |
| 244 | if (coverage && coverage->size() > 0) { |
| 245 | for (unsigned int index = 0; index < coverage->size(); index++) { |
| 246 | func_msg->mutable_coverage_data()->Add(coverage->at(index)); |
| 247 | } |
| 248 | } |
| 249 | |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 250 | if (func_msg->return_type().aggregate_type().size() > 0) { |
| 251 | // TODO: actually handle this case. |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 252 | if (result != NULL) { |
| 253 | // loads that interface spec and enqueues all functions. |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 254 | cout << __func__ << " return type: " |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 255 | << func_msg->return_type().aggregate_type(0) << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 256 | } else { |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 257 | cout << __func__ << " return value = NULL" << endl; |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 258 | } |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 259 | cerr << __func__ << " todo: support aggregate" << endl; |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 260 | string* output = new string(); |
| 261 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
| 262 | return *output; |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 263 | } else if (func_msg->return_type().primitive_type().size() > 0) { |
| 264 | // TODO handle when the size > 1. |
| 265 | if (!strcmp(func_msg->return_type().primitive_type(0).c_str(), "int32_t")) { |
| 266 | 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] | 267 | *((int*)(&result))); |
| 268 | cout << "result " << endl; |
| 269 | // todo handle more types; |
| 270 | string* output = new string(); |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 271 | google::protobuf::TextFormat::PrintToString(*func_msg, output); |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 272 | return *output; |
| 273 | } |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 274 | } |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 275 | return *(new string("void")); |
Keun Soo Yim | 52f5166 | 2016-05-12 17:23:24 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 279 | bool SpecificationBuilder::Process( |
| 280 | const char* dll_file_name, |
| 281 | const char* spec_lib_file_path, |
| 282 | int target_class, |
| 283 | int target_type, |
| 284 | float target_version) { |
| 285 | vts::InterfaceSpecificationMessage* interface_specification_message = |
| 286 | FindInterfaceSpecification(target_class, target_type, target_version); |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 287 | cout << "ifspec addr " << interface_specification_message << endl; |
| 288 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 289 | if (!interface_specification_message) { |
| 290 | cerr << __FUNCTION__ << |
| 291 | ": no interface specification file found for " |
| 292 | << "class " << target_class |
| 293 | << " type " << target_type |
| 294 | << " version " << target_version << endl; |
| 295 | return false; |
| 296 | } |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 297 | |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 298 | if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) { |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 299 | return false; |
| 300 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 301 | |
| 302 | if (!GetFuzzerBaseAndAddAllFunctionsToQueue( |
| 303 | *interface_specification_message, dll_file_name)) return false; |
| 304 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 305 | for (int i = 0; i < epoch_count_; i++) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 306 | // by default, breath-first-searching is used. |
| 307 | if (job_queue_.empty()) { |
| 308 | cout << "no more job to process; stopping after epoch " << i << endl; |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | pair<vts::FunctionSpecificationMessage*, FuzzerBase*> curr_job = |
| 313 | job_queue_.front(); |
| 314 | job_queue_.pop(); |
| 315 | |
| 316 | vts::FunctionSpecificationMessage* func_msg = curr_job.first; |
| 317 | FuzzerBase* func_fuzzer = curr_job.second; |
| 318 | |
Keun Soo Yim | c5d092a | 2016-04-28 16:51:56 +0000 | [diff] [blame] | 319 | void* result; |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 320 | cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl; |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 321 | func_fuzzer->Fuzz(func_msg, &result, agent_port_); |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 322 | if (func_msg->return_type().aggregate_type().size() > 0) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 323 | if (result != NULL) { |
| 324 | // loads that interface spec and enqueues all functions. |
| 325 | cout << __FUNCTION__ << " return type: " |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 326 | << func_msg->return_type().aggregate_type(0) << endl; |
| 327 | // TODO: handle the case when size > 1 |
| 328 | string submodule_name = func_msg->return_type().aggregate_type(0); |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 329 | while (!submodule_name.empty() |
| 330 | && (std::isspace(submodule_name.back()) |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 331 | || submodule_name.back() == '*')) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 332 | submodule_name.pop_back(); |
| 333 | } |
| 334 | vts::InterfaceSpecificationMessage* iface_spec_msg = |
| 335 | FindInterfaceSpecification( |
| 336 | target_class, target_type, target_version, submodule_name); |
| 337 | if (iface_spec_msg) { |
| 338 | cout << __FUNCTION__ << " submodule found - " << submodule_name << endl; |
| 339 | if (!GetFuzzerBaseAndAddAllFunctionsToQueue( |
| 340 | *iface_spec_msg, dll_file_name)) { |
| 341 | return false; |
| 342 | } |
| 343 | } else { |
| 344 | cout << __FUNCTION__ << " submodule not found - " << submodule_name << endl; |
| 345 | } |
| 346 | } else { |
| 347 | cout << __FUNCTION__ << " return value = NULL" << endl; |
| 348 | } |
| 349 | } |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 350 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 351 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 355 | |
| 356 | vts::InterfaceSpecificationMessage* |
| 357 | SpecificationBuilder::GetInterfaceSpecification() const { |
| 358 | cout << "ifspec addr get " << if_spec_msg_ << endl; |
| 359 | return if_spec_msg_; |
| 360 | } |
| 361 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 362 | } // namespace vts |
| 363 | } // namespace android |