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 "usbserialsettimeout_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include "hdf_log.h"
21 #include "usbserialcommonfunction_fuzzer.h"
22
23 using namespace OHOS::HDI::Usb::UsbSerialDdk::V1_0;
24
25 namespace OHOS {
26 constexpr size_t THRESHOLD = 10;
27
28 namespace USBSerial {
29 constexpr int32_t DEFAULT_TIMEOUT = 3000;
30
GetTimeoutFromData(const uint8_t * data,size_t size)31 static int32_t GetTimeoutFromData(const uint8_t* data, size_t size)
32 {
33 if (data == nullptr || size < sizeof(int32_t)) {
34 HDF_LOGE("Invalid input data or insufficient size.");
35 return DEFAULT_TIMEOUT;
36 }
37
38 int32_t timeout = static_cast<int32_t>(static_cast<uint32_t>(data[0]) |
39 (static_cast<uint32_t>(data[1]) << SECOND_MOVE_LEN) |
40 (static_cast<uint32_t>(data[SECOND_BIT]) << FIRST_MOVE_LEN) |
41 (static_cast<uint32_t>(data[THIRD_BIT]) << ZERO_MOVE_LEN));
42
43 return timeout;
44 }
45
UsbSerialSetBaudRateFuzzTest(const uint8_t * data,size_t size)46 bool UsbSerialSetBaudRateFuzzTest(const uint8_t *data, size_t size)
47 {
48 sptr<IUsbSerialDdk> usbSerialInterface = IUsbSerialDdk::Get();
49 int32_t ret = usbSerialInterface->Init();
50 if (ret != HDF_SUCCESS) {
51 HDF_LOGE("%s: init failed, ret = %d", __func__, ret);
52 return false;
53 }
54
55 UsbSerialDeviceHandle* device = nullptr;
56 ret = UsbSerialFuzzTestHostModeInit(usbSerialInterface, device);
57 if (ret != HDF_SUCCESS) {
58 HDF_LOGE("%s: UsbSerial open device failed, ret = %d", __func__, ret);
59 return false;
60 }
61
62 int32_t timeout = GetTimeoutFromData(data, size);
63 ret = usbSerialInterface->SetTimeout(*device, timeout);
64 if (ret != HDF_SUCCESS) {
65 HDF_LOGE("%{public}s: SetBaudRate failed", __func__);
66 return false;
67 }
68
69 ret = usbSerialInterface->Close(*device);
70 if (ret != HDF_SUCCESS) {
71 HDF_LOGE("%{public}s: close device failed", __func__);
72 usbSerialInterface->Release();
73 return false;
74 }
75
76 ret = usbSerialInterface->Release();
77 if (ret != HDF_SUCCESS) {
78 HDF_LOGE("%{public}s: release failed", __func__);
79 return false;
80 }
81 return true;
82 }
83 } // namespace USBSerial
84 } // namespace OHOS
85
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
87 {
88 if (size < OHOS::THRESHOLD) {
89 return 0;
90 }
91 OHOS::USBSerial::UsbSerialSetBaudRateFuzzTest(data, size);
92 return 0;
93 }