1 /*
2 * Copyright (c) 2023 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 <cstddef>
17 #include <cstdint>
18 #include "hdf_log.h"
19 #include "usbddk_fuzzer.h"
20 #include "v1_0/usb_ddk_stub.h"
21 #include "v1_0/iusb_ddk.h"
22
23 using namespace OHOS::HDI::Usb::Ddk::V1_0;
24
25 namespace OHOS {
26 constexpr size_t THRESHOLD = 10;
27 constexpr int32_t OFFSET = 4;
28 constexpr int32_t ZERO_BIT = 0;
29 constexpr int32_t FIRST_BIT = 1;
30 constexpr int32_t SECOND_BIT = 2;
31 constexpr int32_t THIRD_BIT = 3;
32 constexpr int32_t ZERO_MOVE_LEN = 24;
33 constexpr int32_t FIRST_MOVE_LEN = 16;
34 constexpr int32_t SECOND_MOVE_LEN = 8;
35 const std::u16string USB_INTERFACE_TOKEN = u"ohos.hdi.usb.ddk.v1_0.IUsbDdk";
36
Convert2Uint32(const uint8_t * ptr)37 uint32_t Convert2Uint32(const uint8_t *ptr)
38 {
39 if (ptr == nullptr) {
40 HDF_LOGE("%{public}s: ptr is null", __func__);
41 return 0;
42 }
43 /*
44 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
45 * and the third digit no left
46 */
47 return (ptr[ZERO_BIT] << ZERO_MOVE_LEN) | (ptr[FIRST_BIT] << FIRST_MOVE_LEN) | (ptr[SECOND_BIT] <<
48 SECOND_MOVE_LEN) | (ptr[THIRD_BIT]);
49 }
50
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)51 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
52 {
53 if (rawData == nullptr) {
54 HDF_LOGE("%{public}s: rawData is null", __func__);
55 return false;
56 }
57 uint32_t code = Convert2Uint32(rawData);
58 rawData = rawData + OFFSET;
59 size = size - OFFSET;
60
61 MessageParcel data;
62 data.WriteInterfaceToken(USB_INTERFACE_TOKEN);
63 data.WriteBuffer(rawData, size);
64 data.RewindRead(0);
65 MessageParcel reply;
66 MessageOption option;
67 sptr<IUsbDdk> usbDdkInterface = IUsbDdk::Get(false);
68 if (usbDdkInterface == nullptr) {
69 HDF_LOGE("%{public}s: get usbDdkInterface failed", __func__);
70 return false;
71 }
72 sptr<UsbDdkStub> usbDdk = new UsbDdkStub(usbDdkInterface);
73 if (usbDdk == nullptr) {
74 HDF_LOGE("%{public}s: new usbDdk failed", __func__);
75 return false;
76 }
77 usbDdk->OnRemoteRequest(code, data, reply, option);
78
79 return true;
80 }
81 } // namespace OHOS
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
85 {
86 if (size < OHOS::THRESHOLD) {
87 return 0;
88 }
89
90 /* Run your code on data */
91 OHOS::DoSomethingInterestingWithMyAPI(data, size);
92 return 0;
93 }
94