• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/driver/LegacyHalCodeGen.h"
18 
19 #include <fstream>
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 
24 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
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 LegacyHalCodeGen::kInstanceVariableName = "legacyhal_";
35 
GenerateCppBodyFuzzFunction(Formatter & out,const ComponentSpecificationMessage & message,const string & fuzzer_extended_class_name)36 void LegacyHalCodeGen::GenerateCppBodyFuzzFunction(
37     Formatter& out, const ComponentSpecificationMessage& message,
38     const string& fuzzer_extended_class_name) {
39   out << "bool " << fuzzer_extended_class_name << "::Fuzz(" << "\n";
40   out << "    FunctionSpecificationMessage* func_msg," << "\n";
41   out << "    void** result, const string& callback_socket_name) {" << "\n";
42   out.indent();
43   out << "const char* func_name = func_msg->name().c_str();" << "\n";
44   out << "cout << \"Function: \" << func_name << endl;" << "\n";
45 
46   for (auto const& api : message.interface().api()) {
47     std::stringstream ss;
48 
49     out << "if (!strcmp(func_name, \"" << api.name() << "\")) {" << "\n";
50 
51     // args - definition;
52     int arg_count = 0;
53     for (auto const& arg : api.arg()) {
54       out << "    " << GetCppVariableType(arg) << " ";
55       out << "arg" << arg_count << " = ";
56       if (arg_count == 0 && arg.type() == TYPE_PREDEFINED &&
57           !strncmp(arg.predefined_type().c_str(),
58                    message.original_data_structure_name().c_str(),
59                    message.original_data_structure_name().length()) &&
60           message.original_data_structure_name().length() > 0) {
61         out << "reinterpret_cast<" << GetCppVariableType(arg) << ">("
62             << kInstanceVariableName << ")";
63       } else {
64         out << GetCppInstanceType(arg);
65       }
66       out << ";" << "\n";
67       out << "    cout << \"arg" << arg_count << " = \" << arg" << arg_count
68           << " << endl;" << "\n";
69       arg_count++;
70     }
71 
72     out << "    ";
73     out << "typedef void* (*";
74     out << "func_type_" << api.name() << ")(...";
75     out << ");" << "\n";
76 
77     // actual function call
78     if (!api.has_return_type() || api.return_type().type() == TYPE_VOID) {
79       out << "*result = NULL;" << "\n";
80     } else {
81       out << "*result = const_cast<void*>(reinterpret_cast<const void*>(";
82     }
83     out << "    ";
84     out << "((func_type_" << api.name() << ") "
85         << "target_loader_.GetLoaderFunction(\"" << api.name() << "\"))(";
86     // out << "reinterpret_cast<" << message.original_data_structure_name()
87     //    << "*>(" << kInstanceVariableName << ")->" << api.name() << "(";
88 
89     if (arg_count > 0) out << "\n";
90 
91     for (int index = 0; index < arg_count; index++) {
92       out << "      arg" << index;
93       if (index != (arg_count - 1)) {
94         out << "," << "\n";
95       }
96     }
97     if (api.has_return_type() || api.return_type().type() != TYPE_VOID) {
98       out << "))";
99     }
100     out << ");" << "\n";
101     out << "    return true;" << "\n";
102     out << "  }" << "\n";
103   }
104   // TODO: if there were pointers, free them.
105   out << "return false;" << "\n";
106   out.unindent();
107   out << "}" << "\n";
108 }
109 
GenerateCppBodyGetAttributeFunction(Formatter & out,const ComponentSpecificationMessage &,const string & fuzzer_extended_class_name)110 void LegacyHalCodeGen::GenerateCppBodyGetAttributeFunction(
111     Formatter& out,
112     const ComponentSpecificationMessage& /*message*/,
113     const string& fuzzer_extended_class_name) {
114   out << "bool " << fuzzer_extended_class_name << "::GetAttribute(" << "\n";
115   out << "    FunctionSpecificationMessage* func_msg," << "\n";
116   out << "    void** result) {" << "\n";
117   out.indent();
118   out << "const char* func_name = func_msg->name().c_str();" << "\n";
119   out << "cout << \"Function: \" << __func__ << \" '\" << func_name << \"'\" << endl;"
120       << "\n";
121   out << "cerr << \"attribute not supported for legacy hal yet\" << endl;"
122       << "\n";
123   out << "return false;" << "\n";
124   out.unindent();
125   out << "}" << "\n";
126 }
127 
GenerateClassConstructionFunction(Formatter & out,const ComponentSpecificationMessage &,const string & fuzzer_extended_class_name)128 void LegacyHalCodeGen::GenerateClassConstructionFunction(Formatter& out,
129     const ComponentSpecificationMessage& /*message*/,
130     const string& fuzzer_extended_class_name) {
131   out << fuzzer_extended_class_name << "() : FuzzerBase(HAL_LEGACY) {}\n";
132 }
133 
134 }  // namespace vts
135 }  // namespace android
136