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 <stdlib.h>
18 
19 #include <iostream>
20 #include <string>
21 
22 #define LOG_TAG "VtsFuzzerBinderService"
23 #include <log/log.h>
24 #include <utils/RefBase.h>
25 
26 #include <binder/IBinder.h>
27 #include <binder/IInterface.h>
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/ProcessState.h>
31 #include <binder/TextOutput.h>
32 
33 #include "binder/VtsFuzzerBinderService.h"
34 #include "utils/InterfaceSpecUtil.h"
35 
36 using namespace std;
37 
38 namespace android {
39 namespace vts {
40 
41 IMPLEMENT_META_INTERFACE(VtsFuzzer, VTS_FUZZER_BINDER_SERVICE_NAME);
42 
Exit()43 void BpVtsFuzzer::Exit() {
44   Parcel data;
45   Parcel reply;
46   data.writeInterfaceToken(IVtsFuzzer::getInterfaceDescriptor());
47   data.writeString16(String16("Exit code"));
48   remote()->transact(EXIT, data, &reply, IBinder::FLAG_ONEWAY);
49 }
50 
LoadHal(const string & path,int target_class,int target_type,int target_version_major,int target_version_minor,const string & module_name)51 int32_t BpVtsFuzzer::LoadHal(const string& path, int target_class,
52                              int target_type, int target_version_major,
53                              int target_version_minor,
54                              const string& module_name) {
55   Parcel data;
56   Parcel reply;
57 
58   printf("agent->driver: LoadHal(%s, %d, %d, %s, %s)\n", path.c_str(),
59          target_class, target_type,
60          GetVersionString(target_version_major, target_version_minor).c_str(),
61          module_name.c_str());
62   data.writeInterfaceToken(IVtsFuzzer::getInterfaceDescriptor());
63   data.writeCString(path.c_str());
64   data.writeInt32(target_class);
65   data.writeInt32(target_type);
66   data.writeInt32(target_version_major);
67   data.writeInt32(target_version_minor);
68   data.writeCString(module_name.c_str());
69 
70 #ifdef VTS_FUZZER_BINDER_DEBUG
71   alog << "BpVtsFuzzer::Status request parcel:\n"
72        << data
73        << endl;
74 #endif
75 
76   remote()->transact(LOAD_HAL, data, &reply);
77 
78 #ifdef VTS_FUZZER_BINDER_DEBUG
79   alog << "BpVtsFuzzer::Status response parcel:\n"
80        << reply
81        << endl;
82 #endif
83 
84   int32_t res;
85   status_t status = reply.readInt32(&res);
86 
87   printf("driver->agent: LoadHal returns %d\n", status);
88   return res;
89 }
90 
Status(int32_t type)91 int32_t BpVtsFuzzer::Status(int32_t type) {
92   Parcel data;
93   Parcel reply;
94 
95   data.writeInterfaceToken(IVtsFuzzer::getInterfaceDescriptor());
96   data.writeInt32(type);
97 
98 #ifdef VTS_FUZZER_BINDER_DEBUG
99   alog << "BpVtsFuzzer::Status request parcel:\n"
100        << data
101        << endl;
102 #endif
103 
104   remote()->transact(STATUS, data, &reply);
105 
106 #ifdef VTS_FUZZER_BINDER_DEBUG
107   alog << "BpVtsFuzzer::Status response parcel:\n"
108        << reply
109        << endl;
110 #endif
111 
112   int32_t res;
113   /* status_t */ reply.readInt32(&res);
114   return res;
115 }
116 
Call(const string & call_payload)117 string BpVtsFuzzer::Call(const string& call_payload) {
118   Parcel data, reply;
119   data.writeInterfaceToken(IVtsFuzzer::getInterfaceDescriptor());
120   data.writeCString(call_payload.c_str());
121 #ifdef VTS_FUZZER_BINDER_DEBUG
122   alog << data << endl;
123 #endif
124 
125   remote()->transact(CALL, data, &reply);
126 #ifdef VTS_FUZZER_BINDER_DEBUG
127   alog << reply << endl;
128 #endif
129 
130   const char* res = reply.readCString();
131   if (res == NULL) {
132     printf("reply == NULL\n");
133     return res;
134   }
135 
136   printf("len(reply) = %zu\n", strlen(res));
137   return {res};
138 }
139 
GetFunctions()140 const char* BpVtsFuzzer::GetFunctions() {
141   Parcel data, reply;
142   data.writeInterfaceToken(IVtsFuzzer::getInterfaceDescriptor());
143 #ifdef VTS_FUZZER_BINDER_DEBUG
144   alog << data << endl;
145 #endif
146 
147   remote()->transact(GET_FUNCTIONS, data, &reply);
148 #ifdef VTS_FUZZER_BINDER_DEBUG
149   alog << reply << endl;
150 #endif
151 
152   const char* res = reply.readCString();
153   if (res == NULL) {
154     printf("reply == NULL\n");
155     return res;
156   }
157 
158   printf("len(reply) = %zu\n", strlen(res));
159   return res;
160 }
161 
162 }  // namespace vts
163 }  // namespace android
164