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