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