1 #include "android/hardware/tests/memory/1.0/MemoryTest.vts.h"
2 #include "vts_measurement.h"
3 #include <android-base/logging.h>
4 #include <android/hidl/allocator/1.0/IAllocator.h>
5 #include <fcntl.h>
6 #include <fmq/MessageQueue.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 
10 
11 using namespace android::hardware::tests::memory::V1_0;
12 namespace android {
13 namespace vts {
GetService(bool get_stub,const char * service_name)14 bool FuzzerExtended_android_hardware_tests_memory_V1_0_IMemoryTest::GetService(bool get_stub, const char* service_name) {
15     static bool initialized = false;
16     if (!initialized) {
17         LOG(INFO) << "HIDL getService";
18         if (service_name) {
19           LOG(INFO) << "  - service name: " << service_name;
20         }
21         hw_binder_proxy_ = ::android::hardware::tests::memory::V1_0::IMemoryTest::getService(service_name, get_stub);
22         if (hw_binder_proxy_ == nullptr) {
23             LOG(ERROR) << "getService() returned a null pointer.";
24             return false;
25         }
26         LOG(DEBUG) << "hw_binder_proxy_ = " << hw_binder_proxy_.get();
27         initialized = true;
28     }
29     return true;
30 }
31 
32 
haveSomeMemory(const::android::hardware::hidl_memory & arg0,std::function<void (const::android::hardware::hidl_memory & arg0)> cb)33 ::android::hardware::Return<void> Vts_android_hardware_tests_memory_V1_0_IMemoryTest::haveSomeMemory(
34     const ::android::hardware::hidl_memory& arg0 __attribute__((__unused__)), std::function<void(const ::android::hardware::hidl_memory& arg0)> cb) {
35     LOG(INFO) << "haveSomeMemory called";
36     AndroidSystemCallbackRequestMessage callback_message;
37     callback_message.set_id(GetCallbackID("haveSomeMemory"));
38     callback_message.set_name("Vts_android_hardware_tests_memory_V1_0_IMemoryTest::haveSomeMemory");
39     VariableSpecificationMessage* var_msg0 = callback_message.add_arg();
40     var_msg0->set_type(TYPE_HIDL_MEMORY);
41     var_msg0->mutable_hidl_memory_value()->set_hidl_mem_address(reinterpret_cast<size_t>(new android::hardware::hidl_memory(arg0)));
42     RpcCallToAgent(callback_message, callback_socket_name_);
43     cb(::android::hardware::hidl_memory());
44     return ::android::hardware::Void();
45 }
46 
fillMemory(const::android::hardware::hidl_memory & arg0,uint8_t arg1)47 ::android::hardware::Return<void> Vts_android_hardware_tests_memory_V1_0_IMemoryTest::fillMemory(
48     const ::android::hardware::hidl_memory& arg0 __attribute__((__unused__)),
49     uint8_t arg1 __attribute__((__unused__))) {
50     LOG(INFO) << "fillMemory called";
51     AndroidSystemCallbackRequestMessage callback_message;
52     callback_message.set_id(GetCallbackID("fillMemory"));
53     callback_message.set_name("Vts_android_hardware_tests_memory_V1_0_IMemoryTest::fillMemory");
54     VariableSpecificationMessage* var_msg0 = callback_message.add_arg();
55     var_msg0->set_type(TYPE_HIDL_MEMORY);
56     var_msg0->mutable_hidl_memory_value()->set_hidl_mem_address(reinterpret_cast<size_t>(new android::hardware::hidl_memory(arg0)));
57     VariableSpecificationMessage* var_msg1 = callback_message.add_arg();
58     var_msg1->set_type(TYPE_SCALAR);
59     var_msg1->set_scalar_type("uint8_t");
60     var_msg1->mutable_scalar_value()->set_uint8_t(arg1);
61     RpcCallToAgent(callback_message, callback_socket_name_);
62     return ::android::hardware::Void();
63 }
64 
haveSomeMemoryBlock(const::android::hidl::memory::block::V1_0::MemoryBlock & arg0,std::function<void (const::android::hidl::memory::block::V1_0::MemoryBlock & arg0)> cb)65 ::android::hardware::Return<void> Vts_android_hardware_tests_memory_V1_0_IMemoryTest::haveSomeMemoryBlock(
66     const ::android::hidl::memory::block::V1_0::MemoryBlock& arg0 __attribute__((__unused__)), std::function<void(const ::android::hidl::memory::block::V1_0::MemoryBlock& arg0)> cb) {
67     LOG(INFO) << "haveSomeMemoryBlock called";
68     AndroidSystemCallbackRequestMessage callback_message;
69     callback_message.set_id(GetCallbackID("haveSomeMemoryBlock"));
70     callback_message.set_name("Vts_android_hardware_tests_memory_V1_0_IMemoryTest::haveSomeMemoryBlock");
71     VariableSpecificationMessage* var_msg0 = callback_message.add_arg();
72     var_msg0->set_type(TYPE_STRUCT);
73     SetResult__android__hidl__memory__block__V1_0__MemoryBlock(var_msg0, arg0);
74     RpcCallToAgent(callback_message, callback_socket_name_);
75     cb(::android::hidl::memory::block::V1_0::MemoryBlock());
76     return ::android::hardware::Void();
77 }
78 
set(const::android::hardware::hidl_memory & arg0)79 ::android::hardware::Return<void> Vts_android_hardware_tests_memory_V1_0_IMemoryTest::set(
80     const ::android::hardware::hidl_memory& arg0 __attribute__((__unused__))) {
81     LOG(INFO) << "set called";
82     AndroidSystemCallbackRequestMessage callback_message;
83     callback_message.set_id(GetCallbackID("set"));
84     callback_message.set_name("Vts_android_hardware_tests_memory_V1_0_IMemoryTest::set");
85     VariableSpecificationMessage* var_msg0 = callback_message.add_arg();
86     var_msg0->set_type(TYPE_HIDL_MEMORY);
87     var_msg0->mutable_hidl_memory_value()->set_hidl_mem_address(reinterpret_cast<size_t>(new android::hardware::hidl_memory(arg0)));
88     RpcCallToAgent(callback_message, callback_socket_name_);
89     return ::android::hardware::Void();
90 }
91 
get()92 ::android::hardware::Return<sp<::android::hidl::memory::token::V1_0::IMemoryToken>> Vts_android_hardware_tests_memory_V1_0_IMemoryTest::get(
93     ) {
94     LOG(INFO) << "get called";
95     AndroidSystemCallbackRequestMessage callback_message;
96     callback_message.set_id(GetCallbackID("get"));
97     callback_message.set_name("Vts_android_hardware_tests_memory_V1_0_IMemoryTest::get");
98     RpcCallToAgent(callback_message, callback_socket_name_);
99     return nullptr;
100 }
101 
VtsFuzzerCreateVts_android_hardware_tests_memory_V1_0_IMemoryTest(const string & callback_socket_name)102 sp<::android::hardware::tests::memory::V1_0::IMemoryTest> VtsFuzzerCreateVts_android_hardware_tests_memory_V1_0_IMemoryTest(const string& callback_socket_name) {
103     static sp<::android::hardware::tests::memory::V1_0::IMemoryTest> result;
104     result = new Vts_android_hardware_tests_memory_V1_0_IMemoryTest(callback_socket_name);
105     return result;
106 }
107 
Fuzz(FunctionSpecificationMessage *,void **,const string &)108 bool FuzzerExtended_android_hardware_tests_memory_V1_0_IMemoryTest::Fuzz(
109     FunctionSpecificationMessage* /*func_msg*/,
110     void** /*result*/, const string& /*callback_socket_name*/) {
111     return true;
112 }
GetAttribute(FunctionSpecificationMessage *,void **)113 bool FuzzerExtended_android_hardware_tests_memory_V1_0_IMemoryTest::GetAttribute(
114     FunctionSpecificationMessage* /*func_msg*/,
115     void** /*result*/) {
116     LOG(ERROR) << "attribute not found.";
117     return false;
118 }
CallFunction(const FunctionSpecificationMessage & func_msg,const string & callback_socket_name,FunctionSpecificationMessage * result_msg)119 bool FuzzerExtended_android_hardware_tests_memory_V1_0_IMemoryTest::CallFunction(
120     const FunctionSpecificationMessage& func_msg,
121     const string& callback_socket_name __attribute__((__unused__)),
122     FunctionSpecificationMessage* result_msg) {
123     const char* func_name = func_msg.name().c_str();
124     if (hw_binder_proxy_ == nullptr) {
125         LOG(ERROR) << "hw_binder_proxy_ is null. ";
126         return false;
127     }
128     if (!strcmp(func_name, "haveSomeMemory")) {
129         ::android::hardware::hidl_memory arg0;
130         if (func_msg.arg(0).hidl_memory_value().has_hidl_mem_address()) {
131             arg0 = *(reinterpret_cast<android::hardware::hidl_memory*>(func_msg.arg(0).hidl_memory_value().hidl_mem_address()));
132         } else {
133             sp<::android::hidl::allocator::V1_0::IAllocator> ashmemAllocator = ::android::hidl::allocator::V1_0::IAllocator::getService("ashmem");
134             if (ashmemAllocator == nullptr) {
135                 LOG(ERROR) << "Failed to get ashmemAllocator! ";
136                 exit(-1);
137             }
138             auto res = ashmemAllocator->allocate(func_msg.arg(0).hidl_memory_value().size(), [&](bool success, const hardware::hidl_memory& memory) {
139                 if (!success) {
140                     LOG(ERROR) << "Failed to allocate memory! ";
141                     arg0 = ::android::hardware::hidl_memory();
142                     return;
143                 }
144                 arg0 = memory;
145             });
146         }
147         LOG(DEBUG) << "local_device = " << hw_binder_proxy_.get();
148         hw_binder_proxy_->haveSomeMemory(arg0, [&](const ::android::hardware::hidl_memory& arg0 __attribute__((__unused__))){
149             LOG(INFO) << "callback haveSomeMemory called";
150             result_msg->set_name("haveSomeMemory");
151             VariableSpecificationMessage* result_val_0 = result_msg->add_return_type_hidl();
152             result_val_0->set_type(TYPE_HIDL_MEMORY);
153             result_val_0->mutable_hidl_memory_value()->set_hidl_mem_address(reinterpret_cast<size_t>(new android::hardware::hidl_memory(arg0)));
154         });
155         return true;
156     }
157     if (!strcmp(func_name, "fillMemory")) {
158         ::android::hardware::hidl_memory arg0;
159         if (func_msg.arg(0).hidl_memory_value().has_hidl_mem_address()) {
160             arg0 = *(reinterpret_cast<android::hardware::hidl_memory*>(func_msg.arg(0).hidl_memory_value().hidl_mem_address()));
161         } else {
162             sp<::android::hidl::allocator::V1_0::IAllocator> ashmemAllocator = ::android::hidl::allocator::V1_0::IAllocator::getService("ashmem");
163             if (ashmemAllocator == nullptr) {
164                 LOG(ERROR) << "Failed to get ashmemAllocator! ";
165                 exit(-1);
166             }
167             auto res = ashmemAllocator->allocate(func_msg.arg(0).hidl_memory_value().size(), [&](bool success, const hardware::hidl_memory& memory) {
168                 if (!success) {
169                     LOG(ERROR) << "Failed to allocate memory! ";
170                     arg0 = ::android::hardware::hidl_memory();
171                     return;
172                 }
173                 arg0 = memory;
174             });
175         }
176         uint8_t arg1 = 0;
177         arg1 = func_msg.arg(1).scalar_value().uint8_t();
178         LOG(DEBUG) << "local_device = " << hw_binder_proxy_.get();
179         hw_binder_proxy_->fillMemory(arg0, arg1);
180         result_msg->set_name("fillMemory");
181         return true;
182     }
183     if (!strcmp(func_name, "haveSomeMemoryBlock")) {
184         ::android::hidl::memory::block::V1_0::MemoryBlock arg0;
185         MessageTo__android__hidl__memory__block__V1_0__MemoryBlock(func_msg.arg(0), &(arg0), callback_socket_name);
186         LOG(DEBUG) << "local_device = " << hw_binder_proxy_.get();
187         hw_binder_proxy_->haveSomeMemoryBlock(arg0, [&](const ::android::hidl::memory::block::V1_0::MemoryBlock& arg0 __attribute__((__unused__))){
188             LOG(INFO) << "callback haveSomeMemoryBlock called";
189             result_msg->set_name("haveSomeMemoryBlock");
190             VariableSpecificationMessage* result_val_0 = result_msg->add_return_type_hidl();
191             result_val_0->set_type(TYPE_STRUCT);
192             SetResult__android__hidl__memory__block__V1_0__MemoryBlock(result_val_0, arg0);
193         });
194         return true;
195     }
196     if (!strcmp(func_name, "set")) {
197         ::android::hardware::hidl_memory arg0;
198         if (func_msg.arg(0).hidl_memory_value().has_hidl_mem_address()) {
199             arg0 = *(reinterpret_cast<android::hardware::hidl_memory*>(func_msg.arg(0).hidl_memory_value().hidl_mem_address()));
200         } else {
201             sp<::android::hidl::allocator::V1_0::IAllocator> ashmemAllocator = ::android::hidl::allocator::V1_0::IAllocator::getService("ashmem");
202             if (ashmemAllocator == nullptr) {
203                 LOG(ERROR) << "Failed to get ashmemAllocator! ";
204                 exit(-1);
205             }
206             auto res = ashmemAllocator->allocate(func_msg.arg(0).hidl_memory_value().size(), [&](bool success, const hardware::hidl_memory& memory) {
207                 if (!success) {
208                     LOG(ERROR) << "Failed to allocate memory! ";
209                     arg0 = ::android::hardware::hidl_memory();
210                     return;
211                 }
212                 arg0 = memory;
213             });
214         }
215         LOG(DEBUG) << "local_device = " << hw_binder_proxy_.get();
216         hw_binder_proxy_->set(arg0);
217         result_msg->set_name("set");
218         return true;
219     }
220     if (!strcmp(func_name, "get")) {
221         LOG(DEBUG) << "local_device = " << hw_binder_proxy_.get();
222         sp<::android::hidl::memory::token::V1_0::IMemoryToken> result0 = hw_binder_proxy_->get();
223         result_msg->set_name("get");
224         VariableSpecificationMessage* result_val_0 = result_msg->add_return_type_hidl();
225         result_val_0->set_type(TYPE_HIDL_INTERFACE);
226         result_val_0->set_predefined_type("::android::hidl::memory::token::V1_0::IMemoryToken");
227         if (result0 != nullptr) {
228             result0->incStrong(result0.get());
229             result_val_0->set_hidl_interface_pointer(reinterpret_cast<uintptr_t>(result0.get()));
230         } else {
231             result_val_0->set_hidl_interface_pointer(0);
232         }
233         return true;
234     }
235     if (!strcmp(func_name, "notifySyspropsChanged")) {
236         LOG(INFO) << "Call notifySyspropsChanged";
237         hw_binder_proxy_->notifySyspropsChanged();
238         result_msg->set_name("notifySyspropsChanged");
239         return true;
240     }
241     return false;
242 }
243 
VerifyResults(const FunctionSpecificationMessage & expected_result,const FunctionSpecificationMessage & actual_result)244 bool FuzzerExtended_android_hardware_tests_memory_V1_0_IMemoryTest::VerifyResults(const FunctionSpecificationMessage& expected_result __attribute__((__unused__)),
245     const FunctionSpecificationMessage& actual_result __attribute__((__unused__))) {
246     if (!strcmp(actual_result.name().c_str(), "haveSomeMemory")) {
247         if (actual_result.return_type_hidl_size() != expected_result.return_type_hidl_size() ) { return false; }
248         LOG(ERROR) << "TYPE_HIDL_MEMORY is not supported yet. ";
249         return true;
250     }
251     if (!strcmp(actual_result.name().c_str(), "fillMemory")) {
252         if (actual_result.return_type_hidl_size() != expected_result.return_type_hidl_size() ) { return false; }
253         return true;
254     }
255     if (!strcmp(actual_result.name().c_str(), "haveSomeMemoryBlock")) {
256         if (actual_result.return_type_hidl_size() != expected_result.return_type_hidl_size() ) { return false; }
257         if (!Verify__android__hidl__memory__block__V1_0__MemoryBlock(expected_result.return_type_hidl(0), actual_result.return_type_hidl(0))) { return false; }
258         return true;
259     }
260     if (!strcmp(actual_result.name().c_str(), "set")) {
261         if (actual_result.return_type_hidl_size() != expected_result.return_type_hidl_size() ) { return false; }
262         return true;
263     }
264     if (!strcmp(actual_result.name().c_str(), "get")) {
265         if (actual_result.return_type_hidl_size() != expected_result.return_type_hidl_size() ) { return false; }
266         LOG(ERROR) << "TYPE_HIDL_INTERFACE is not supported yet. ";
267         return true;
268     }
269     return false;
270 }
271 
272 extern "C" {
vts_func_4_android_hardware_tests_memory_V1_0_IMemoryTest_()273 android::vts::DriverBase* vts_func_4_android_hardware_tests_memory_V1_0_IMemoryTest_() {
274     return (android::vts::DriverBase*) new android::vts::FuzzerExtended_android_hardware_tests_memory_V1_0_IMemoryTest();
275 }
276 
vts_func_4_android_hardware_tests_memory_V1_0_IMemoryTest_with_arg(uint64_t hw_binder_proxy)277 android::vts::DriverBase* vts_func_4_android_hardware_tests_memory_V1_0_IMemoryTest_with_arg(uint64_t hw_binder_proxy) {
278     ::android::hardware::tests::memory::V1_0::IMemoryTest* arg = nullptr;
279     if (hw_binder_proxy) {
280         arg = reinterpret_cast<::android::hardware::tests::memory::V1_0::IMemoryTest*>(hw_binder_proxy);
281     } else {
282         LOG(INFO) << " Creating DriverBase with null proxy.";
283     }
284     android::vts::DriverBase* result =
285         new android::vts::FuzzerExtended_android_hardware_tests_memory_V1_0_IMemoryTest(
286             arg);
287     if (arg != nullptr) {
288         arg->decStrong(arg);
289     }
290     return result;
291 }
292 
293 }
294 }  // namespace vts
295 }  // namespace android
296