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" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace vts { |
| 33 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 34 | SpecificationBuilder::SpecificationBuilder( |
| 35 | const string dir_path, int epoch_count) |
| 36 | : dir_path_(dir_path), |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame^] | 37 | epoch_count_(epoch_count), |
| 38 | if_spec_msg_(NULL) {} |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | vts::InterfaceSpecificationMessage* |
| 42 | SpecificationBuilder::FindInterfaceSpecification( |
| 43 | const int target_class, |
| 44 | const int target_type, |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 45 | const float target_version, |
| 46 | const string submodule_name) { |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 47 | 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 Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 62 | if (message->component_type() == target_type |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 63 | && message->component_type_version() == target_version) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 64 | 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 Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 70 | closedir(dir); |
| 71 | return message; |
| 72 | } |
| 73 | } |
| 74 | delete message; |
| 75 | } |
| 76 | } |
| 77 | closedir(dir); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 82 | FuzzerBase* 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 Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame^] | 102 | bool 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 Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 128 | bool 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 Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame^] | 136 | cout << "ifspec addr " << interface_specification_message << endl; |
| 137 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 138 | 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 Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 146 | |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 147 | if (!wrapper_.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) { |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 148 | return false; |
| 149 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 150 | |
| 151 | if (!GetFuzzerBaseAndAddAllFunctionsToQueue( |
| 152 | *interface_specification_message, dll_file_name)) return false; |
| 153 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 154 | for (int i = 0; i < epoch_count_; i++) { |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 155 | // 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 Yim | c5d092a | 2016-04-28 16:51:56 +0000 | [diff] [blame] | 168 | void* result; |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 169 | cout << "Iteration " << (i + 1) << " Function " << func_msg->name() << endl; |
| 170 | func_fuzzer->Fuzz(*func_msg, &result); |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 171 | 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 Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 198 | } |
Keun Soo Yim | 2ee9079 | 2016-04-27 14:53:54 -0700 | [diff] [blame] | 199 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame^] | 203 | |
| 204 | vts::InterfaceSpecificationMessage* |
| 205 | SpecificationBuilder::GetInterfaceSpecification() const { |
| 206 | cout << "ifspec addr get " << if_spec_msg_ << endl; |
| 207 | return if_spec_msg_; |
| 208 | } |
| 209 | |
Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame] | 210 | } // namespace vts |
| 211 | } // namespace android |