1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 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 "hdf_log.h" 19094332d3Sopenharmony_ci#include "usb_fuzzer.h" 20094332d3Sopenharmony_ci#include "v1_0/usb_interface_stub.h" 21094332d3Sopenharmony_ci 22094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0; 23094332d3Sopenharmony_ci 24094332d3Sopenharmony_cinamespace OHOS { 25094332d3Sopenharmony_ciconstexpr size_t THRESHOLD = 10; 26094332d3Sopenharmony_ciconstexpr int32_t OFFSET = 4; 27094332d3Sopenharmony_ciconstexpr int32_t ZERO_BIT = 0; 28094332d3Sopenharmony_ciconstexpr int32_t FIRST_BIT = 1; 29094332d3Sopenharmony_ciconstexpr int32_t SECOND_BIT = 2; 30094332d3Sopenharmony_ciconstexpr int32_t THIRD_BIT = 3; 31094332d3Sopenharmony_ciconstexpr int32_t ZERO_MOVE_LEN = 24; 32094332d3Sopenharmony_ciconstexpr int32_t FIRST_MOVE_LEN = 16; 33094332d3Sopenharmony_ciconstexpr int32_t SECOND_MOVE_LEN = 8; 34094332d3Sopenharmony_ciconst std::u16string USB_INTERFACE_TOKEN = u"ohos.hdi.usb.v1_0.IUsbInterface"; 35094332d3Sopenharmony_ci 36094332d3Sopenharmony_ciuint32_t Convert2Uint32(const uint8_t *ptr) 37094332d3Sopenharmony_ci{ 38094332d3Sopenharmony_ci if (ptr == nullptr) { 39094332d3Sopenharmony_ci HDF_LOGE("%{public}s: ptr is null", __func__); 40094332d3Sopenharmony_ci return 0; 41094332d3Sopenharmony_ci } 42094332d3Sopenharmony_ci /* 43094332d3Sopenharmony_ci * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left, 44094332d3Sopenharmony_ci * and the third digit no left 45094332d3Sopenharmony_ci */ 46094332d3Sopenharmony_ci return (ptr[ZERO_BIT] << ZERO_MOVE_LEN) | (ptr[FIRST_BIT] << FIRST_MOVE_LEN) | (ptr[SECOND_BIT] << 47094332d3Sopenharmony_ci SECOND_MOVE_LEN) | (ptr[THIRD_BIT]); 48094332d3Sopenharmony_ci} 49094332d3Sopenharmony_ci 50094332d3Sopenharmony_cibool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size) 51094332d3Sopenharmony_ci{ 52094332d3Sopenharmony_ci if (rawData == nullptr) { 53094332d3Sopenharmony_ci HDF_LOGE("%{public}s: rawData is null", __func__); 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 sptr<IUsbInterface> usbInterface = IUsbInterface::Get(false); 67094332d3Sopenharmony_ci if (usbInterface == nullptr) { 68094332d3Sopenharmony_ci HDF_LOGE("%{public}s: get usbInterface failed", __func__); 69094332d3Sopenharmony_ci return false; 70094332d3Sopenharmony_ci } 71094332d3Sopenharmony_ci sptr<UsbInterfaceStub> interfaceStub = new UsbInterfaceStub(usbInterface); 72094332d3Sopenharmony_ci if (interfaceStub == nullptr) { 73094332d3Sopenharmony_ci HDF_LOGE("%{public}s: new interfaceStub failed", __func__); 74094332d3Sopenharmony_ci return false; 75094332d3Sopenharmony_ci } 76094332d3Sopenharmony_ci interfaceStub->OnRemoteRequest(code, data, reply, option); 77094332d3Sopenharmony_ci 78094332d3Sopenharmony_ci return true; 79094332d3Sopenharmony_ci} 80094332d3Sopenharmony_ci} // namespace OHOS 81094332d3Sopenharmony_ci 82094332d3Sopenharmony_ci/* Fuzzer entry point */ 83094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 84094332d3Sopenharmony_ci{ 85094332d3Sopenharmony_ci if (size < OHOS::THRESHOLD) { 86094332d3Sopenharmony_ci return 0; 87094332d3Sopenharmony_ci } 88094332d3Sopenharmony_ci 89094332d3Sopenharmony_ci /* Run your code on data */ 90094332d3Sopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(data, size); 91094332d3Sopenharmony_ci return 0; 92094332d3Sopenharmony_ci} 93