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/HalCodeGen.h" |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <iostream> |
| 21 | #include <sstream> |
| 22 | #include <string> |
| 23 | |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame^] | 24 | #include "test/vts/runners/host/proto/InterfaceSpecificationMessage.pb.h" |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 25 | |
| 26 | #include "VtsCompilerUtils.h" |
| 27 | |
| 28 | using namespace std; |
| 29 | using namespace android; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace vts { |
| 33 | |
| 34 | const char* const HalCodeGen::kInstanceVariableName = "device_"; |
| 35 | |
| 36 | |
| 37 | void HalCodeGen::GenerateCppBodyFuzzFunction( |
| 38 | std::stringstream& cpp_ss, |
| 39 | const InterfaceSpecificationMessage& message, |
| 40 | const string& fuzzer_extended_class_name) { |
| 41 | cpp_ss << "bool " << fuzzer_extended_class_name << "::Fuzz(" << endl; |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame^] | 42 | cpp_ss << " FunctionSpecificationMessage* func_msg," << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 43 | cpp_ss << " void** result) {" << endl; |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame^] | 44 | cpp_ss << " const char* func_name = func_msg->name().c_str();" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 45 | cpp_ss << " cout << \"Function: \" << func_name << endl;" << endl; |
| 46 | |
| 47 | for (auto const& api : message.api()) { |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 48 | cpp_ss << " if (!strcmp(func_name, \"" << api.name() << "\")) {" << endl; |
| 49 | |
| 50 | // args - definition; |
| 51 | int arg_count = 0; |
| 52 | for (auto const& arg : api.arg()) { |
| 53 | cpp_ss << " " << GetCppVariableType(arg) << " "; |
| 54 | cpp_ss << "arg" << arg_count << " = "; |
| 55 | if (arg_count == 0 |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 56 | && arg.aggregate_type().size() == 1 |
| 57 | && !strncmp(arg.aggregate_type(0).c_str(), |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 58 | message.original_data_structure_name().c_str(), |
| 59 | message.original_data_structure_name().length())) { |
| 60 | cpp_ss << "reinterpret_cast<" << GetCppVariableType(arg) << ">(" |
| 61 | << kInstanceVariableName << ")"; |
| 62 | } else { |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 63 | std::stringstream msg_ss; |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame^] | 64 | msg_ss << "func_msg->arg(" << arg_count << ")"; |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 65 | string msg = msg_ss.str(); |
| 66 | |
Keun Soo Yim | 0ae2f74 | 2016-06-01 14:36:01 -0700 | [diff] [blame] | 67 | if (arg.primitive_type().size() > 0) { |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame^] | 68 | cpp_ss << "(" << msg << ".aggregate_type().size() == 0 && " |
| 69 | << msg << ".primitive_type().size() == 1)? "; |
Keun Soo Yim | 0ae2f74 | 2016-06-01 14:36:01 -0700 | [diff] [blame] | 70 | if (!strcmp(arg.primitive_type(0).c_str(), "pointer") |
| 71 | || !strcmp(arg.primitive_type(0).c_str(), "char_pointer") |
| 72 | || !strcmp(arg.primitive_type(0).c_str(), "function_pointer")) { |
| 73 | cpp_ss << "reinterpret_cast<" << GetCppVariableType(arg) << ">"; |
| 74 | } |
| 75 | cpp_ss << "(" << msg << ".primitive_value(0)"; |
| 76 | |
| 77 | if (arg.primitive_type(0) == "int32_t" |
| 78 | || arg.primitive_type(0) == "uint32_t" |
| 79 | || arg.primitive_type(0) == "int64_t" |
| 80 | || arg.primitive_type(0) == "uint64_t" |
| 81 | || arg.primitive_type(0) == "int16_t" |
| 82 | || arg.primitive_type(0) == "uint16_t" |
| 83 | || arg.primitive_type(0) == "int8_t" |
| 84 | || arg.primitive_type(0) == "uint8_t" |
| 85 | || arg.primitive_type(0) == "float_t" |
| 86 | || arg.primitive_type(0) == "double_t") { |
| 87 | cpp_ss << "." << arg.primitive_type(0) << "() "; |
| 88 | } else if (!strcmp(arg.primitive_type(0).c_str(), "pointer")) { |
| 89 | cpp_ss << ".pointer() "; |
| 90 | } else if (!strcmp(arg.primitive_type(0).c_str(), "char_pointer")) { |
| 91 | cpp_ss << ".pointer() "; |
| 92 | } else if (!strcmp(arg.primitive_type(0).c_str(), "function_pointer")) { |
| 93 | cpp_ss << ".pointer() "; |
| 94 | } else { |
| 95 | cerr << __func__ << " ERROR unsupported type " << arg.primitive_type(0) << endl; |
| 96 | exit(-1); |
| 97 | } |
| 98 | cpp_ss << ") : "; |
| 99 | } |
| 100 | |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame^] | 101 | cpp_ss << "( (" << msg << ".aggregate_value_size() > 0 || " |
| 102 | << msg << ".primitive_value_size() > 0)? "; |
Keun Soo Yim | 34067de | 2016-05-17 09:46:37 -0700 | [diff] [blame] | 103 | cpp_ss << GetCppInstanceType(arg, msg); |
Keun Soo Yim | 0ae2f74 | 2016-06-01 14:36:01 -0700 | [diff] [blame] | 104 | cpp_ss << " : " << GetCppInstanceType(arg) << " )"; |
| 105 | // TODO: use the given message and call a lib function which converts |
| 106 | // a message to a C/C++ struct. |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 107 | } |
| 108 | cpp_ss << ";" << endl; |
| 109 | cpp_ss << " cout << \"arg" << arg_count << " = \" << arg" << arg_count |
| 110 | << " << endl;" << endl; |
| 111 | arg_count++; |
| 112 | } |
| 113 | |
| 114 | // actual function call |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 115 | GenerateCodeToStartMeasurement(cpp_ss); |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 116 | cpp_ss << " cout << \"hit2.\" << device_ << endl;" << endl; |
| 117 | |
| 118 | cpp_ss << " " << message.original_data_structure_name() |
| 119 | << "* local_device = "; |
| 120 | cpp_ss << "reinterpret_cast<" << message.original_data_structure_name() |
| 121 | << "*>(" << kInstanceVariableName << ");" << endl; |
| 122 | |
| 123 | cpp_ss << " if (local_device == NULL) {" << endl; |
| 124 | cpp_ss << " cout << \"use hmi\" << endl;" << endl; |
| 125 | cpp_ss << " local_device = reinterpret_cast<" << message.original_data_structure_name() |
| 126 | << "*>(hmi_);" << endl; |
| 127 | cpp_ss << " }" << endl; |
| 128 | cpp_ss << " if (local_device == NULL) {" << endl; |
| 129 | cpp_ss << " cerr << \"both device_ and hmi_ are NULL.\" << endl;" << endl; |
| 130 | cpp_ss << " return false;" << endl; |
| 131 | cpp_ss << " }" << endl; |
| 132 | cpp_ss << " if (reinterpret_cast<" << message.original_data_structure_name() |
| 133 | << "*>(local_device)->" << api.name() << " == NULL"; |
| 134 | cpp_ss << ") {" << endl; |
| 135 | cpp_ss << " cerr << \"api not set.\" << endl;" << endl; |
| 136 | // todo: consider throwing an exception at least a way to tell more |
| 137 | // specifically to the caller. |
| 138 | cpp_ss << " return false;" << endl; |
| 139 | cpp_ss << " }" << endl; |
| 140 | |
| 141 | cpp_ss << " cout << \"ok. let's call.\" << endl;" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 142 | cpp_ss << " "; |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 143 | if (api.return_type().primitive_type().size() == 1 |
| 144 | && !strcmp(api.return_type().primitive_type(0).c_str(), "void")) { |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 145 | cpp_ss << "*result = NULL;" << endl; |
| 146 | } else { |
| 147 | cpp_ss << "*result = const_cast<void*>(reinterpret_cast<const void*>("; |
| 148 | } |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 149 | cpp_ss << "local_device->" << api.name() << "("; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 150 | if (arg_count > 0) cpp_ss << endl; |
| 151 | |
| 152 | for (int index = 0; index < arg_count; index++) { |
| 153 | cpp_ss << " arg" << index; |
| 154 | if (index != (arg_count - 1)) { |
| 155 | cpp_ss << "," << endl; |
| 156 | } |
| 157 | } |
Keun Soo Yim | a8cf69a | 2016-05-18 18:11:37 -0700 | [diff] [blame] | 158 | if (api.return_type().primitive_type().size() == 0 |
| 159 | || (api.return_type().primitive_type().size() == 1 |
| 160 | && strcmp(api.return_type().primitive_type(0).c_str(), "void"))) { |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 161 | cpp_ss << "))"; |
| 162 | } |
| 163 | cpp_ss << ");" << endl; |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 164 | GenerateCodeToStopMeasurement(cpp_ss); |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 165 | cpp_ss << " cout << \"called\" << endl;" << endl; |
Keun Soo Yim | 0ae2f74 | 2016-06-01 14:36:01 -0700 | [diff] [blame] | 166 | |
| 167 | // Copy the output (call by pointer or reference cases). |
| 168 | arg_count = 0; |
| 169 | for (auto const& arg : api.arg()) { |
| 170 | if (arg.is_output()) { |
| 171 | // TODO check the return value |
| 172 | cpp_ss << " " << GetConversionToProtobufFunctionName(arg) |
| 173 | << "(arg" << arg_count << ", " |
Keun Soo Yim | f518585 | 2016-06-01 19:37:10 -0700 | [diff] [blame^] | 174 | << "func_msg->mutable_arg(" << arg_count << "));" << endl; |
Keun Soo Yim | 0ae2f74 | 2016-06-01 14:36:01 -0700 | [diff] [blame] | 175 | } |
| 176 | arg_count++; |
| 177 | } |
| 178 | |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 179 | cpp_ss << " return true;" << endl; |
| 180 | cpp_ss << " }" << endl; |
| 181 | } |
| 182 | // TODO: if there were pointers, free them. |
| 183 | cpp_ss << " return false;" << endl; |
| 184 | cpp_ss << "}" << endl; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | void HalCodeGen::GenerateHeaderGlobalFunctionDeclarations( |
| 189 | std::stringstream& h_ss, |
| 190 | const string& function_prototype) { |
| 191 | h_ss << "extern \"C\" {" << endl; |
| 192 | h_ss << "extern " << function_prototype << ";" << endl; |
| 193 | h_ss << "}" << endl; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void HalCodeGen::GenerateCppBodyGlobalFunctions( |
| 198 | std::stringstream& cpp_ss, |
| 199 | const string& function_prototype, |
| 200 | const string& fuzzer_extended_class_name) { |
| 201 | cpp_ss << "extern \"C\" {" << endl; |
| 202 | cpp_ss << function_prototype << " {" << endl; |
| 203 | cpp_ss << " return (android::vts::FuzzerBase*) " |
| 204 | << "new android::vts::" << fuzzer_extended_class_name << "();" << endl; |
| 205 | cpp_ss << "}" << endl << endl; |
| 206 | cpp_ss << "}" << endl; |
| 207 | } |
| 208 | |
| 209 | } // namespace vts |
| 210 | } // namespace android |