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 "usbmgr_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include "usb_service.h"
20 using namespace OHOS::HDI::Usb::V1_0;
21 using namespace OHOS::USB;
22
23 namespace OHOS {
24 constexpr int32_t OFFSET = 4;
25 constexpr size_t THRESHOLD = 10;
26 const std::u16string USB_INTERFACE_TOKEN = u"ohos.usb.IUsbSrv";
27
Convert2Uint32(const uint8_t * ptr)28 uint32_t Convert2Uint32(const uint8_t *ptr)
29 {
30 if (ptr == nullptr) {
31 return 0;
32 }
33 /*
34 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
35 * and the third digit no left
36 */
37 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
38 }
39
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)40 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
41 {
42 if (rawData == nullptr) {
43 return false;
44 }
45 uint32_t code = Convert2Uint32(rawData);
46 rawData = rawData + OFFSET;
47 size = size - OFFSET;
48
49 MessageParcel data;
50 data.WriteInterfaceToken(USB_INTERFACE_TOKEN);
51 data.WriteBuffer(rawData, size);
52 data.RewindRead(0);
53 MessageParcel reply;
54 MessageOption option;
55 DelayedSpSingleton<UsbService>::GetInstance()->OnRemoteRequest(code, data, reply, option);
56 return true;
57 }
58 } // namespace OHOS
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62 {
63 if (size < OHOS::THRESHOLD) {
64 return 0;
65 }
66
67 /* Run your code on data */
68 OHOS::DoSomethingInterestingWithMyAPI(data, size);
69 return 0;
70 }
71