blob: 146d4de91076c3ecbe39ca454dde179ea0ea84c6 [file] [log] [blame]
Keun Soo Yimd4559882016-05-13 20:03:12 -07001/*
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/LegacyHalCodeGen.h"
18
19#include <fstream>
20#include <iostream>
21#include <sstream>
22#include <string>
23
24#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
25
26#include "VtsCompilerUtils.h"
27
28using namespace std;
29using namespace android;
30
31namespace android {
32namespace vts {
33
34const char* const LegacyHalCodeGen::kInstanceVariableName = "legacyhal_";
35
36
37void LegacyHalCodeGen::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;
42 cpp_ss << " const FunctionSpecificationMessage& func_msg," << endl;
43 cpp_ss << " void** result) {" << endl;
44 cpp_ss << " const char* func_name = func_msg.name().c_str();" << endl;
45 cpp_ss << " cout << \"Function: \" << func_name << endl;" << endl;
46
47 for (auto const& api : message.api()) {
48 std::stringstream ss;
49
50 cpp_ss << " if (!strcmp(func_name, \"" << api.name() << "\")) {" << endl;
51
52 // args - definition;
53 int arg_count = 0;
54 for (auto const& arg : api.arg()) {
55 cpp_ss << " " << GetCppVariableType(arg) << " ";
56 cpp_ss << "arg" << arg_count << " = ";
57 if (arg_count == 0
Keun Soo Yima8cf69a2016-05-18 18:11:37 -070058 && arg.aggregate_type().size() == 1
59 && !strncmp(arg.aggregate_type(0).c_str(),
Keun Soo Yimd4559882016-05-13 20:03:12 -070060 message.original_data_structure_name().c_str(),
61 message.original_data_structure_name().length())
62 && message.original_data_structure_name().length() > 0) {
63 cpp_ss << "reinterpret_cast<" << GetCppVariableType(arg) << ">("
64 << kInstanceVariableName << ")";
65 } else {
66 cpp_ss << GetCppInstanceType(arg);
67 }
68 cpp_ss << ";" << endl;
69 cpp_ss << " cout << \"arg" << arg_count << " = \" << arg" << arg_count
70 << " << endl;" << endl;
71 arg_count++;
72 }
73
74 cpp_ss << " ";
75 cpp_ss << "typedef void* (*";
76 cpp_ss << "func_type_" << api.name() << ")(...";
77 cpp_ss << ");" << endl;
78
79 // actual function call
Keun Soo Yima8cf69a2016-05-18 18:11:37 -070080 if (api.return_type().primitive_type().size() == 1
81 && !strcmp(api.return_type().primitive_type(0).c_str(), "void")) {
Keun Soo Yimd4559882016-05-13 20:03:12 -070082 cpp_ss << "*result = NULL;" << endl;
83 } else {
84 cpp_ss << "*result = const_cast<void*>(reinterpret_cast<const void*>(";
85 }
86 cpp_ss << " ";
87 cpp_ss << "((func_type_" << api.name() << ") "
88 << "target_loader_.GetLoaderFunction(\"" << api.name() << "\"))(";
89 //cpp_ss << "reinterpret_cast<" << message.original_data_structure_name()
90 // << "*>(" << kInstanceVariableName << ")->" << api.name() << "(";
91
92 if (arg_count > 0) cpp_ss << endl;
93
94 for (int index = 0; index < arg_count; index++) {
95 cpp_ss << " arg" << index;
96 if (index != (arg_count - 1)) {
97 cpp_ss << "," << endl;
98 }
99 }
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700100 if (api.return_type().primitive_type().size() == 0
101 || (api.return_type().primitive_type().size() == 1
102 && strcmp(api.return_type().primitive_type(0).c_str(), "void"))) {
Keun Soo Yimd4559882016-05-13 20:03:12 -0700103 cpp_ss << "))";
104 }
105 cpp_ss << ");" << endl;
106 cpp_ss << " return true;" << endl;
107 cpp_ss << " }" << endl;
108 }
109 // TODO: if there were pointers, free them.
110 cpp_ss << " return false;" << endl;
111 cpp_ss << "}" << endl;
112}
113
114
115void LegacyHalCodeGen::GenerateHeaderGlobalFunctionDeclarations(
116 std::stringstream& /*h_ss*/,
117 const string& /*function_prototype*/) {}
118
119} // namespace vts
120} // namespace android