• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "usbmgrbulkcancel_fuzzer.h"
17 
18 #include <array>
19 #include <optional>
20 
21 #include "ashmem.h"
22 #include "iremote_object.h"
23 #include "usb_callback_test.h"
24 #include "usb_common_fuzz.h"
25 #include "usb_errors.h"
26 
27 namespace {
28 const uint32_t ASHMEM_MAX_SIZE = 1024;
29 const uint32_t MEM_DATA = 1024 * 1024;
30 }
31 namespace OHOS {
32 namespace USB {
GetSharedMem()33     sptr<Ashmem> GetSharedMem()
34     {
35         sptr<Ashmem> asmptr = Ashmem::CreateAshmem("ttashmem001", MEM_DATA);
36         if (asmptr == nullptr) {
37             USB_HILOGE(MODULE_USB_SERVICE, "InitAshmemOne CreateAshmem failed");
38             return nullptr;
39         }
40 
41         asmptr->MapReadAndWriteAshmem();
42 
43         std::array<uint8_t, ASHMEM_MAX_SIZE> tdata;
44         tdata.fill('Y');
45         uint32_t offset = 0;
46         while (offset < MEM_DATA) {
47             uint32_t tlen = (MEM_DATA - offset) < ASHMEM_MAX_SIZE ? (MEM_DATA - offset) : ASHMEM_MAX_SIZE;
48             asmptr->WriteToAshmem(tdata.data(), tlen, offset);
49             offset += tlen;
50         }
51         return asmptr;
52     }
53 
UsbMgrBulkCancelFuzzTest(const uint8_t * data,size_t)54     bool UsbMgrBulkCancelFuzzTest(const uint8_t* data, size_t /* size */)
55     {
56         auto[res, pipe, interface] = UsbMgrPrepareFuzzEnv();
57         if (!res) {
58             USB_HILOGE(MODULE_USB_SERVICE, "prepare error");
59             return false;
60         }
61 
62         auto& usbSrvClient = UsbSrvClient::GetInstance();
63         sptr<UsbCallbackTest> cb = new UsbCallbackTest();
64         USBEndpoint point = const_cast<UsbInterface &>(interface).GetEndpoints().at(1);
65         auto ret = usbSrvClient.RegBulkCallback(const_cast<USBDevicePipe &>(pipe), point, cb);
66         if (ret != UEC_OK) {
67             USB_HILOGE(MODULE_USB_SERVICE, "RegBulkCallback failed ret=%{public}d", ret);
68             return false;
69         }
70 
71         auto sharedMem = GetSharedMem();
72         if (sharedMem == nullptr) {
73             USB_HILOGE(MODULE_USB_SERVICE, "GetSharedMem failed ret=%{public}d", ret);
74             return false;
75         }
76 
77         ret = usbSrvClient.BulkWrite(const_cast<USBDevicePipe &>(pipe), point, sharedMem);
78         if (ret != UEC_OK) {
79             USB_HILOGE(MODULE_USB_SERVICE, "BulkWrite failed ret=%{public}d", ret);
80             return false;
81         }
82 
83         if (usbSrvClient.BulkCancel(reinterpret_cast<USBDevicePipe &>(data),
84             reinterpret_cast<const USBEndpoint&>(data)) == UEC_OK) {
85             return false;
86         }
87         return true;
88     }
89 } // USB
90 } // OHOS
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95     /* Run your code on data */
96     OHOS::USB::UsbMgrBulkCancelFuzzTest(data, size);
97     return 0;
98 }
99