Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 "code_gen/CodeGenBase.h" |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <iostream> |
| 21 | #include <sstream> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "utils/InterfaceSpecUtil.h" |
| 25 | |
| 26 | #include "VtsCompilerUtils.h" |
| 27 | |
| 28 | using namespace std; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace vts { |
| 32 | |
| 33 | CodeGenBase::CodeGenBase(const char* input_vts_file_path, |
| 34 | const char* vts_name) |
| 35 | : input_vts_file_path_(input_vts_file_path), |
| 36 | vts_name_(vts_name) {} |
| 37 | |
| 38 | |
| 39 | CodeGenBase::~CodeGenBase() {} |
| 40 | |
| 41 | |
| 42 | void CodeGenBase::GenerateAll(std::stringstream& cpp_ss, |
| 43 | std::stringstream& h_ss, |
| 44 | const InterfaceSpecificationMessage& message) { |
| 45 | cpp_ss << "#include \"" << string(input_vts_file_path_) << ".h\"" << endl; |
| 46 | |
| 47 | cpp_ss << "#include <iostream>" << endl; |
| 48 | cpp_ss << "#include \"vts_datatype.h\"" << endl; |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 49 | cpp_ss << "#include \"vts_measurement.h\"" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 50 | for (auto const& header : message.header()) { |
| 51 | cpp_ss << "#include " << header << endl; |
| 52 | } |
| 53 | GenerateOpenNameSpaces(cpp_ss); |
| 54 | |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 55 | string component_name = GetComponentName(message); |
Keun Soo Yim | 110664c | 2016-07-06 17:49:03 -0700 | [diff] [blame^] | 56 | if (component_name.empty()) { |
| 57 | cerr << __func__ << ":" << __LINE__ << " error component_name is empty" << endl; |
| 58 | exit(-1); |
| 59 | } |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 60 | string fuzzer_extended_class_name; |
Keun Soo Yim | 7844641 | 2016-06-16 14:30:13 -0700 | [diff] [blame] | 61 | if (message.component_class() == HAL_CONVENTIONAL |
Keun Soo Yim | dc55fb8 | 2016-06-25 10:13:58 -0700 | [diff] [blame] | 62 | || message.component_class() == HAL_CONVENTIONAL_SUBMODULE |
Keun Soo Yim | 110664c | 2016-07-06 17:49:03 -0700 | [diff] [blame^] | 63 | || message.component_class() == HAL_LEGACY |
| 64 | || message.component_class() == LIB_SHARED) { |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 65 | fuzzer_extended_class_name = "FuzzerExtended_" + component_name; |
| 66 | } |
| 67 | |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 68 | GenerateAllHeader(fuzzer_extended_class_name, h_ss, message); |
| 69 | cpp_ss << endl << endl; |
Keun Soo Yim | dc55fb8 | 2016-06-25 10:13:58 -0700 | [diff] [blame] | 70 | if (message.component_class() == HAL_CONVENTIONAL |
| 71 | || message.component_class() == HAL_CONVENTIONAL_SUBMODULE) { |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 72 | GenerateCppBodyCallbackFunction(cpp_ss, message, fuzzer_extended_class_name); |
| 73 | } |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 74 | |
| 75 | cpp_ss << endl; |
| 76 | GenerateCppBodyFuzzFunction(cpp_ss, message, fuzzer_extended_class_name); |
| 77 | |
| 78 | std::stringstream ss; |
| 79 | // return type |
| 80 | ss << "android::vts::FuzzerBase* " << endl; |
| 81 | // function name |
| 82 | string function_name_prefix = GetFunctionNamePrefix(message); |
| 83 | ss << function_name_prefix << "(" << endl; |
| 84 | ss << ")"; |
| 85 | |
| 86 | GenerateCppBodyGlobalFunctions(cpp_ss, ss.str(), fuzzer_extended_class_name); |
| 87 | |
| 88 | GenerateCloseNameSpaces(cpp_ss); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void CodeGenBase::GenerateAllHeader( |
| 93 | const string& fuzzer_extended_class_name, |
| 94 | std::stringstream& h_ss, const InterfaceSpecificationMessage& message) { |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 95 | h_ss << "#ifndef __VTS_SPEC_" << vts_name_ << "__" << endl; |
| 96 | h_ss << "#define __VTS_SPEC_" << vts_name_ << "__" << endl; |
| 97 | h_ss << endl; |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 98 | h_ss << "#include <stdio.h>" << endl; |
| 99 | h_ss << "#include <stdarg.h>" << endl; |
| 100 | h_ss << "#include <stdlib.h>" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 101 | h_ss << "#define LOG_TAG \"" << fuzzer_extended_class_name << "\"" << endl; |
| 102 | h_ss << "#include <utils/Log.h>" << endl; |
| 103 | h_ss << "#include \"common/fuzz_tester/FuzzerBase.h\"" << endl; |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 104 | h_ss << "#include \"common/fuzz_tester/FuzzerCallbackBase.h\"" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 105 | for (auto const& header : message.header()) { |
| 106 | h_ss << "#include " << header << endl; |
| 107 | } |
| 108 | h_ss << "\n\n" << endl; |
| 109 | GenerateOpenNameSpaces(h_ss); |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 110 | |
| 111 | GenerateClassHeader(fuzzer_extended_class_name, h_ss, message); |
| 112 | |
| 113 | string function_name_prefix = GetFunctionNamePrefix(message); |
| 114 | |
| 115 | std::stringstream ss; |
| 116 | // return type |
| 117 | h_ss << endl; |
| 118 | ss << "android::vts::FuzzerBase* " << endl; |
| 119 | // function name |
| 120 | ss << function_name_prefix << "(" << endl; |
| 121 | ss << ")"; |
| 122 | |
| 123 | GenerateHeaderGlobalFunctionDeclarations(h_ss, ss.str()); |
| 124 | |
| 125 | GenerateCloseNameSpaces(h_ss); |
| 126 | h_ss << "#endif" << endl; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | void CodeGenBase::GenerateClassHeader( |
| 131 | const string& fuzzer_extended_class_name, |
| 132 | std::stringstream& h_ss, |
| 133 | const InterfaceSpecificationMessage& message) { |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 134 | h_ss << "class " << fuzzer_extended_class_name << " : public FuzzerBase {" |
| 135 | << endl; |
Keun Soo Yim | d455988 | 2016-05-13 20:03:12 -0700 | [diff] [blame] | 136 | h_ss << " public:" << endl; |
| 137 | h_ss << " " << fuzzer_extended_class_name << "() : FuzzerBase("; |
| 138 | |
Keun Soo Yim | 7844641 | 2016-06-16 14:30:13 -0700 | [diff] [blame] | 139 | if (message.component_class() == HAL_CONVENTIONAL) h_ss << "HAL_CONVENTIONAL"; |
Keun Soo Yim | 110664c | 2016-07-06 17:49:03 -0700 | [diff] [blame^] | 140 | else if (message.component_class() == HAL_CONVENTIONAL_SUBMODULE) { |
Keun Soo Yim | dc55fb8 | 2016-06-25 10:13:58 -0700 | [diff] [blame] | 141 | h_ss << "HAL_CONVENTIONAL_SUBMODULE"; |
| 142 | } |
Keun Soo Yim | 110664c | 2016-07-06 17:49:03 -0700 | [diff] [blame^] | 143 | else if (message.component_class() == HAL_LEGACY) h_ss << "HAL_LEGACY"; |
| 144 | else if (message.component_class() == LIB_SHARED) h_ss << "LIB_SHARED"; |
Keun Soo Yim | d455988 | 2016-05-13 20:03:12 -0700 | [diff] [blame] | 145 | |
| 146 | h_ss << ") { }" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 147 | h_ss << " protected:" << endl; |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame] | 148 | h_ss << " bool Fuzz(FunctionSpecificationMessage* func_msg," << endl; |
Keun Soo Yim | 6d8a16b | 2016-06-30 19:29:02 -0700 | [diff] [blame] | 149 | h_ss << " void** result, const string& callback_socket_name);" |
| 150 | << endl; |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 151 | |
| 152 | // produce Fuzz method(s) for sub_struct(s). |
| 153 | for (auto const& sub_struct : message.sub_struct()) { |
| 154 | GenerateFuzzFunctionForSubStruct(h_ss, sub_struct, "_"); |
| 155 | } |
| 156 | |
Keun Soo Yim | dc55fb8 | 2016-06-25 10:13:58 -0700 | [diff] [blame] | 157 | if (message.component_class() == HAL_CONVENTIONAL_SUBMODULE) { |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 158 | string component_name = GetComponentName(message); |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 159 | h_ss << " void SetSubModule(" << component_name << "* submodule) {" << endl; |
| 160 | h_ss << " submodule_ = submodule;" << endl; |
| 161 | h_ss << " }" << endl; |
| 162 | h_ss << endl; |
| 163 | h_ss << " private:" << endl; |
| 164 | h_ss << " " << message.original_data_structure_name() << "* submodule_;" |
| 165 | << endl; |
| 166 | } |
| 167 | h_ss << "};" << endl; |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 168 | } |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 169 | |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 170 | |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 171 | void CodeGenBase::GenerateFuzzFunctionForSubStruct( |
| 172 | std::stringstream& h_ss, |
| 173 | const StructSpecificationMessage& message, const string& parent_path) { |
| 174 | h_ss << " bool Fuzz_" << parent_path << message.name() |
| 175 | << "(FunctionSpecificationMessage* func_msg," << endl; |
Keun Soo Yim | 6d8a16b | 2016-06-30 19:29:02 -0700 | [diff] [blame] | 176 | h_ss << " void** result, const string& callback_socket_name);" |
| 177 | << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 178 | |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 179 | for (auto const& sub_struct : message.sub_struct()) { |
| 180 | GenerateFuzzFunctionForSubStruct( |
| 181 | h_ss, sub_struct, parent_path + message.name() + "_"); |
| 182 | } |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
| 186 | void CodeGenBase::GenerateOpenNameSpaces(std::stringstream& ss) { |
| 187 | ss << "namespace android {" << endl; |
| 188 | ss << "namespace vts {" << endl; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | void CodeGenBase::GenerateCloseNameSpaces(std::stringstream& ss) { |
| 193 | ss << "} // namespace vts" << endl; |
| 194 | ss << "} // namespace android" << endl; |
| 195 | } |
| 196 | |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 197 | |
| 198 | void CodeGenBase::GenerateCodeToStartMeasurement(std::stringstream& ss) { |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 199 | ss << " VtsMeasurement vts_measurement;" << endl; |
| 200 | ss << " vts_measurement.Start();" << endl; |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | void CodeGenBase::GenerateCodeToStopMeasurement(std::stringstream& ss) { |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 205 | ss << " vector<float>* measured = vts_measurement.Stop();" << endl; |
| 206 | ss << " cout << \"time \" << (*measured)[0] << endl;" << endl; |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 209 | |
| 210 | string CodeGenBase::GetComponentName( |
| 211 | const InterfaceSpecificationMessage& message) { |
Keun Soo Yim | 110664c | 2016-07-06 17:49:03 -0700 | [diff] [blame^] | 212 | if (!message.component_name().empty()) { |
| 213 | return message.component_name(); |
| 214 | } |
| 215 | |
Keun Soo Yim | 91d634a | 2016-06-02 11:38:00 -0700 | [diff] [blame] | 216 | string component_name = message.original_data_structure_name(); |
| 217 | while (!component_name.empty() |
| 218 | && (std::isspace(component_name.back()) |
| 219 | || component_name.back() == '*' )) { |
| 220 | component_name.pop_back(); |
| 221 | } |
| 222 | const auto pos = component_name.find_last_of(" "); |
| 223 | if (pos != std::string::npos) { |
| 224 | component_name = component_name.substr(pos + 1); |
| 225 | } |
| 226 | return component_name; |
| 227 | } |
Keun Soo Yim | a4a6d53 | 2016-06-08 09:11:40 -0700 | [diff] [blame] | 228 | |
| 229 | |
| 230 | void CodeGenBase::GenerateCppBodyCallbackFunction( |
| 231 | std::stringstream& /*cpp_ss*/, |
| 232 | const InterfaceSpecificationMessage& /*message*/, |
| 233 | const string& /*fuzzer_extended_class_name*/) {} |
| 234 | |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 235 | } // namespace vts |
| 236 | } // namespace android |