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