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 "usbserialsetparams_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
30 constexpr uint32_t DEFAULT_BAUD_RATE = 9600;
31 constexpr uint8_t DEFAULT_DATA_BITS = 8;
32 constexpr uint8_t DEFAULT_STOP_BITS = 1;
33
UpdateParamsFromData(UsbSerialParams & params,const uint8_t * data,size_t size)34 static void UpdateParamsFromData(UsbSerialParams& params, const uint8_t* data, size_t size)
35 {
36 if (data == nullptr) {
37 HDF_LOGE("Invalid input data.");
38 return;
39 }
40
41 size_t offset = 0;
42 if (size >= sizeof(uint32_t)) {
43 params.baudRate = static_cast<uint32_t>(data[0]) | (static_cast<uint32_t>(data[1]) << SECOND_MOVE_LEN) |
44 (static_cast<uint32_t>(data[SECOND_BIT]) << FIRST_MOVE_LEN) |
45 (static_cast<uint32_t>(data[THIRD_BIT]) << ZERO_MOVE_LEN);
46 offset += sizeof(uint32_t);
47 }
48
49 if (size >= offset + sizeof(uint8_t)) {
50 params.nDataBits = data[offset++];
51 }
52
53 if (size >= offset + sizeof(uint8_t)) {
54 params.nStopBits = data[offset++];
55 }
56
57 if (size >= offset + sizeof(uint8_t)) {
58 enum UsbSerialParity parityValue = static_cast<enum UsbSerialParity>(data[offset]);
59 params.parity = parityValue;
60 }
61 }
62
UsbSerialSetParamsFuzzTest(const uint8_t * data,size_t size)63 bool UsbSerialSetParamsFuzzTest(const uint8_t *data, size_t size)
64 {
65 sptr<IUsbSerialDdk> usbSerialInterface = IUsbSerialDdk::Get();
66 int32_t ret = usbSerialInterface->Init();
67 if (ret != HDF_SUCCESS) {
68 HDF_LOGE("%s: init failed, ret = %d", __func__, ret);
69 return false;
70 }
71
72 UsbSerialParams params = {
73 .baudRate = DEFAULT_BAUD_RATE,
74 .nDataBits = DEFAULT_DATA_BITS,
75 .nStopBits = DEFAULT_STOP_BITS,
76 .parity = USB_SERIAL_PARITY_NONE,
77 };
78
79 UsbSerialDeviceHandle* device = nullptr;
80 ret = UsbSerialFuzzTestHostModeInit(usbSerialInterface, device);
81 if (ret != HDF_SUCCESS) {
82 HDF_LOGE("%s: UsbSerial open device failed, ret = %d", __func__, ret);
83 return false;
84 }
85
86 UpdateParamsFromData(params, data, size);
87 ret = usbSerialInterface->SetParams(*device, params);
88 if (ret != HDF_SUCCESS) {
89 usbSerialInterface->Release();
90 HDF_LOGE("%{public}s: SetBaudRate failed", __func__);
91 return false;
92 }
93
94 ret = usbSerialInterface->Close(*device);
95 if (ret != HDF_SUCCESS) {
96 HDF_LOGE("%{public}s: close device failed", __func__);
97 return false;
98 }
99
100 ret = usbSerialInterface->Release();
101 if (ret != HDF_SUCCESS) {
102 HDF_LOGE("%{public}s: release failed", __func__);
103 return false;
104 }
105 return true;
106 }
107 } // namespace USBSerial
108 } // namespace OHOS
109
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
111 {
112 if (size < OHOS::THRESHOLD) {
113 return 0;
114 }
115 OHOS::USBSerial::UsbSerialSetParamsFuzzTest(data, size);
116 return 0;
117 }