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