1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <cstddef> 17094332d3Sopenharmony_ci#include <cstdint> 18094332d3Sopenharmony_ci#include <unistd.h> 19094332d3Sopenharmony_ci 20094332d3Sopenharmony_ci#include "UsbSubscriberTest.h" 21094332d3Sopenharmony_ci#include "hdf_log.h" 22094332d3Sopenharmony_ci#include "message_parcel.h" 23094332d3Sopenharmony_ci#include "usb_impl.h" 24094332d3Sopenharmony_ci#include "usbhost_fuzzer.h" 25094332d3Sopenharmony_ci#include "v1_0/usb_interface_stub.h" 26094332d3Sopenharmony_ci 27094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0; 28094332d3Sopenharmony_ciusing OHOS::HDI::Usb::V1_0::UsbDev; 29094332d3Sopenharmony_ciusing namespace OHOS::USB; 30094332d3Sopenharmony_ci 31094332d3Sopenharmony_cinamespace OHOS { 32094332d3Sopenharmony_ciconstexpr size_t THRESHOLD = 10; 33094332d3Sopenharmony_ciconstexpr int32_t OFFSET = 4; 34094332d3Sopenharmony_ciconst std::u16string USB_INTERFACE_TOKEN = u"ohos.hdi.usb.v1_0.IUsbInterface"; 35094332d3Sopenharmony_ciconst int32_t SLEEP_TIME = 3; 36094332d3Sopenharmony_ciconst int32_t DEFAULT_PORT_ID = 1; 37094332d3Sopenharmony_ciconst int32_t DEFAULT_ROLE_HOST = 1; 38094332d3Sopenharmony_ci 39094332d3Sopenharmony_ciuint32_t Convert2Uint32(const uint8_t *ptr) 40094332d3Sopenharmony_ci{ 41094332d3Sopenharmony_ci if (ptr == nullptr) { 42094332d3Sopenharmony_ci return 0; 43094332d3Sopenharmony_ci } 44094332d3Sopenharmony_ci /* 45094332d3Sopenharmony_ci * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left, 46094332d3Sopenharmony_ci * and the third digit no left 47094332d3Sopenharmony_ci */ 48094332d3Sopenharmony_ci return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); 49094332d3Sopenharmony_ci} 50094332d3Sopenharmony_ci 51094332d3Sopenharmony_cibool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size) 52094332d3Sopenharmony_ci{ 53094332d3Sopenharmony_ci if (rawData == nullptr) { 54094332d3Sopenharmony_ci return false; 55094332d3Sopenharmony_ci } 56094332d3Sopenharmony_ci uint32_t code = Convert2Uint32(rawData); 57094332d3Sopenharmony_ci rawData = rawData + OFFSET; 58094332d3Sopenharmony_ci size = size - OFFSET; 59094332d3Sopenharmony_ci 60094332d3Sopenharmony_ci MessageParcel data; 61094332d3Sopenharmony_ci data.WriteInterfaceToken(USB_INTERFACE_TOKEN); 62094332d3Sopenharmony_ci data.WriteBuffer(rawData, size); 63094332d3Sopenharmony_ci data.RewindRead(0); 64094332d3Sopenharmony_ci MessageParcel reply; 65094332d3Sopenharmony_ci MessageOption option; 66094332d3Sopenharmony_ci 67094332d3Sopenharmony_ci sptr<IUsbInterface> usbInterface = IUsbInterface::Get(false); 68094332d3Sopenharmony_ci if (usbInterface == nullptr) { 69094332d3Sopenharmony_ci HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__); 70094332d3Sopenharmony_ci return false; 71094332d3Sopenharmony_ci } 72094332d3Sopenharmony_ci 73094332d3Sopenharmony_ci int32_t ret = usbInterface->SetPortRole(DEFAULT_PORT_ID, DEFAULT_ROLE_HOST, DEFAULT_ROLE_HOST); 74094332d3Sopenharmony_ci sleep(SLEEP_TIME); 75094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 76094332d3Sopenharmony_ci HDF_LOGE("%{public}s: set port role as host failed", __func__); 77094332d3Sopenharmony_ci return ret; 78094332d3Sopenharmony_ci } 79094332d3Sopenharmony_ci 80094332d3Sopenharmony_ci sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest(); 81094332d3Sopenharmony_ci ret = usbInterface->BindUsbdSubscriber(subscriber); 82094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 83094332d3Sopenharmony_ci HDF_LOGE("%{public}s: bind usbd subscriber failed", __func__); 84094332d3Sopenharmony_ci return ret; 85094332d3Sopenharmony_ci } 86094332d3Sopenharmony_ci 87094332d3Sopenharmony_ci sleep(1); 88094332d3Sopenharmony_ci UsbDev dev; 89094332d3Sopenharmony_ci dev.busNum = subscriber->busNum_; 90094332d3Sopenharmony_ci dev.devAddr = subscriber->devAddr_; 91094332d3Sopenharmony_ci ret = usbInterface->OpenDevice(dev); 92094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 93094332d3Sopenharmony_ci HDF_LOGE("%{public}s: open device failed", __func__); 94094332d3Sopenharmony_ci return ret; 95094332d3Sopenharmony_ci } 96094332d3Sopenharmony_ci 97094332d3Sopenharmony_ci sptr<UsbInterfaceStub> impl = new UsbInterfaceStub(usbInterface); 98094332d3Sopenharmony_ci if (impl == nullptr) { 99094332d3Sopenharmony_ci HDF_LOGE("%{public}s:new UsbInterfaceStub failed.", __func__); 100094332d3Sopenharmony_ci return false; 101094332d3Sopenharmony_ci } 102094332d3Sopenharmony_ci impl->OnRemoteRequest(code, data, reply, option); 103094332d3Sopenharmony_ci 104094332d3Sopenharmony_ci return true; 105094332d3Sopenharmony_ci} 106094332d3Sopenharmony_ci} // namespace OHOS 107094332d3Sopenharmony_ci 108094332d3Sopenharmony_ci/* Fuzzer entry point */ 109094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 110094332d3Sopenharmony_ci{ 111094332d3Sopenharmony_ci if (size < OHOS::THRESHOLD) { 112094332d3Sopenharmony_ci return 0; 113094332d3Sopenharmony_ci } 114094332d3Sopenharmony_ci 115094332d3Sopenharmony_ci /* Run your code on data */ 116094332d3Sopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(data, size); 117094332d3Sopenharmony_ci return 0; 118094332d3Sopenharmony_ci} 119