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 #ifdef VTS_AGENT_DRIVER_COMM_BINDER // binder 18 19 #include "BinderServer.h" 20 21 #include <stdio.h> 22 #include <stdlib.h> 23 24 #include <iostream> 25 #include <string> 26 27 #include <utils/RefBase.h> 28 #define LOG_TAG "VtsFuzzerBinderServer" 29 #include <utils/Log.h> 30 #include <utils/String8.h> 31 32 #include <binder/IBinder.h> 33 #include <binder/IInterface.h> 34 #include <binder/IPCThreadState.h> 35 #include <binder/IServiceManager.h> 36 #include <binder/ProcessState.h> 37 #include <binder/TextOutput.h> 38 39 #include "binder/VtsFuzzerBinderService.h" 40 #include "specification_parser/SpecificationBuilder.h" 41 42 #include <google/protobuf/text_format.h> 43 #include "test/vts/proto/ComponentSpecificationMessage.pb.h" 44 45 using namespace std; 46 47 namespace android { 48 namespace vts { 49 50 class BnVtsFuzzer : public BnInterface<IVtsFuzzer> { 51 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, 52 uint32_t flags = 0); 53 }; 54 55 status_t BnVtsFuzzer::onTransact(uint32_t code, const Parcel& data, 56 Parcel* reply, uint32_t flags) { 57 ALOGD("BnVtsFuzzer::%s(%i) %i", __FUNCTION__, code, flags); 58 59 data.checkInterface(this); 60 #ifdef VTS_FUZZER_BINDER_DEBUG 61 alog << data << endl; 62 #endif 63 64 switch (code) { 65 case EXIT: 66 Exit(); 67 break; 68 case LOAD_HAL: { 69 const char* path = data.readCString(); 70 const int target_class = data.readInt32(); 71 const int target_type = data.readInt32(); 72 const float target_version = data.readFloat(); 73 const char* module_name = data.readCString(); 74 int32_t result = LoadHal(string(path), target_class, target_type, 75 target_version, string(module_name)); 76 ALOGD("BnVtsFuzzer::%s LoadHal(%s) -> %i", __FUNCTION__, path, result); 77 if (reply == NULL) { 78 ALOGE("reply == NULL"); 79 abort(); 80 } 81 #ifdef VTS_FUZZER_BINDER_DEBUG 82 alog << reply << endl; 83 #endif 84 reply->writeInt32(result); 85 break; 86 } 87 case STATUS: { 88 int32_t type = data.readInt32(); 89 int32_t result = Status(type); 90 91 ALOGD("BnVtsFuzzer::%s status(%i) -> %i", __FUNCTION__, type, result); 92 if (reply == NULL) { 93 ALOGE("reply == NULL"); 94 abort(); 95 } 96 #ifdef VTS_FUZZER_BINDER_DEBUG 97 alog << reply << endl; 98 #endif 99 reply->writeInt32(result); 100 break; 101 } 102 case CALL: { 103 const char* arg = data.readCString(); 104 const char* result = Call(arg); 105 106 ALOGD("BnVtsFuzzer::%s call(%s) = %i", __FUNCTION__, arg, result); 107 if (reply == NULL) { 108 ALOGE("reply == NULL"); 109 abort(); 110 } 111 #ifdef VTS_FUZZER_BINDER_DEBUG 112 alog << reply << endl; 113 #endif 114 reply->writeCString(result); 115 break; 116 } 117 case GET_FUNCTIONS: { 118 const char* result = GetFunctions(); 119 120 if (reply == NULL) { 121 ALOGE("reply == NULL"); 122 abort(); 123 } 124 #ifdef VTS_FUZZER_BINDER_DEBUG 125 alog << reply << endl; 126 #endif 127 reply->writeCString(result); 128 break; 129 } 130 default: 131 return BBinder::onTransact(code, data, reply, flags); 132 } 133 return NO_ERROR; 134 } 135 136 class VtsFuzzerServer : public BnVtsFuzzer { 137 public: 138 VtsFuzzerServer(android::vts::SpecificationBuilder& spec_builder, 139 const char* lib_path) 140 : spec_builder_(spec_builder), lib_path_(lib_path) {} 141 142 void Exit() { printf("VtsFuzzerServer::Exit\n"); } 143 144 int32_t LoadHal(const string& path, int target_class, int target_type, 145 float target_version, const string& module_name) { 146 printf("VtsFuzzerServer::LoadHal(%s)\n", path.c_str()); 147 bool success = spec_builder_.LoadTargetComponent( 148 path.c_str(), lib_path_, target_class, target_type, target_version, 149 module_name.c_str()); 150 cout << "Result: " << success << std::endl; 151 if (success) { 152 return 0; 153 } else { 154 return -1; 155 } 156 } 157 158 int32_t Status(int32_t type) { 159 printf("VtsFuzzerServer::Status(%i)\n", type); 160 return 0; 161 } 162 163 const char* Call(const string& arg) { 164 printf("VtsFuzzerServer::Call(%s)\n", arg.c_str()); 165 FunctionSpecificationMessage* func_msg = new FunctionSpecificationMessage(); 166 google::protobuf::TextFormat::MergeFromString(arg, func_msg); 167 const string& result = spec_builder_.CallFunction(func_msg); 168 return result.c_str(); 169 } 170 171 const char* GetFunctions() { 172 printf("Get functions*"); 173 vts::ComponentSpecificationMessage* spec = 174 spec_builder_.GetComponentSpecification(); 175 if (!spec) { 176 return NULL; 177 } 178 string* output = new string(); 179 printf("getfunctions serial1\n"); 180 if (google::protobuf::TextFormat::PrintToString(*spec, output)) { 181 printf("getfunctions length %d\n", output->length()); 182 return output->c_str(); 183 } else { 184 printf("can't serialize the interface spec message to a string.\n"); 185 return NULL; 186 } 187 } 188 189 private: 190 android::vts::SpecificationBuilder& spec_builder_; 191 const char* lib_path_; 192 }; 193 194 void StartBinderServer(const string& service_name, 195 android::vts::SpecificationBuilder& spec_builder, 196 const char* lib_path) { 197 defaultServiceManager()->addService( 198 String16(service_name.c_str()), 199 new VtsFuzzerServer(spec_builder, lib_path)); 200 android::ProcessState::self()->startThreadPool(); 201 IPCThreadState::self()->joinThreadPool(); 202 } 203 204 } // namespace vts 205 } // namespace android 206 207 #endif 208