1 /*
2 * Copyright (c) 2024 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 "DaemonUsbRead_fuzzer.h"
17 #include "daemon_usb.h"
18 #include <uv.h>
19
20 namespace Hdc {
21 class HdcDaemonUsbFuzzer : public HdcDaemonUSB {
22 public:
HdcDaemonUsbFuzzer(void *ptrbase)23 explicit HdcDaemonUsbFuzzer(void *ptrbase) : HdcDaemonUSB(false, ptrbase) {}
24
Instance(void *ptrbase)25 static std::unique_ptr<HdcDaemonUsbFuzzer> Instance(void *ptrbase)
26 {
27 std::unique_ptr<HdcDaemonUsbFuzzer> daemonusb = std::make_unique<HdcDaemonUsbFuzzer>(ptrbase);
28 return daemonusb;
29 }
30 };
31
FuzzDaemonUsbRead(const uint8_t *data, size_t size)32 bool FuzzDaemonUsbRead(const uint8_t *data, size_t size)
33 {
34 auto daemonusb = HdcDaemonUsbFuzzer::Instance(nullptr);
35 if (daemonusb == nullptr) {
36 WRITE_LOG(LOG_FATAL, "FuzzDaemonUsbRead daemonusb is null");
37 return false;
38 }
39 HdcUSB hUSB = {};
40 hUSB.wMaxPacketSizeSend = MAX_PACKET_SIZE_HISPEED;
41 HdcDaemonUSB::CtxUvFileCommonIo ctxRecv = {};
42 ctxRecv.thisClass = &daemonusb;
43 ctxRecv.data = &hUSB;
44 ctxRecv.bufSizeMax = Base::GetUsbffsBulkSize();
45 ctxRecv.buf = const_cast<uint8_t *>(data);
46 ctxRecv.req = {};
47 uv_fs_t *req = &ctxRecv.req;
48 req->result = size;
49 req->data = &ctxRecv;
50 daemonusb->OnUSBRead(req);
51 return true;
52 }
53 } // namespace Hdc
54
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
56 {
57 Hdc::FuzzDaemonUsbRead(data, size);
58 return 0;
59 }
60