1 /*
2 * Copyright (c) 2024 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 "dschedtransportsoftbusadapter_fuzzer.h"
17
18 #include <fuzzer/FuzzedDataProvider.h>
19
20 #include "dsched_continue_manager.h"
21 #include "dsched_data_buffer.h"
22 #include "dsched_transport_softbus_adapter.h"
23 #include "idata_listener.h"
24
25 namespace OHOS {
26 namespace DistributedSchedule {
27 namespace {
28 constexpr uint32_t MAX_BUFFER_SIZE = 80 * 1024 * 1024;
29 }
30
FuzzOnBind(const uint8_t * data,size_t size)31 void FuzzOnBind(const uint8_t* data, size_t size)
32 {
33 if ((data == nullptr) || (size < sizeof(size_t))) {
34 return;
35 }
36 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
37 std::string peerDeviceId(reinterpret_cast<const char*>(data), size);
38 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
39 dschedTransportSoftbusAdapter.OnBind(sessionId, peerDeviceId);
40 }
41
FuzzOnShutdown(const uint8_t * data,size_t size)42 void FuzzOnShutdown(const uint8_t* data, size_t size)
43 {
44 if ((data == nullptr) || (size < sizeof(size_t))) {
45 return;
46 }
47 FuzzedDataProvider fdp(data, size);
48 bool isSelfcalled = fdp.ConsumeBool();
49 int32_t sessionId = fdp.ConsumeIntegral<int32_t>();
50 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
51 dschedTransportSoftbusAdapter.OnShutdown(sessionId, isSelfcalled);
52 }
53
FuzzOnBytes(const uint8_t * data,size_t size)54 void FuzzOnBytes(const uint8_t* data, size_t size)
55 {
56 if ((data == nullptr) || (size < sizeof(size_t))) {
57 return;
58 }
59 const void* newdata = reinterpret_cast<const void*>(data);
60 FuzzedDataProvider fdp(data, size);
61 int32_t sessionId = fdp.ConsumeIntegral<int32_t>();
62 int32_t dataLen = fdp.ConsumeIntegral<int32_t>();
63 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
64 dschedTransportSoftbusAdapter.OnBytes(sessionId, newdata, dataLen);
65 }
66
FuzzConnectDevice(const uint8_t * data,size_t size)67 void FuzzConnectDevice(const uint8_t* data, size_t size)
68 {
69 if ((data == nullptr) || (size < sizeof(size_t)) || size >= MAX_BUFFER_SIZE) {
70 return;
71 }
72 FuzzedDataProvider fdp(data, size);
73 int32_t sessionId = fdp.ConsumeIntegral<int32_t>();
74 int32_t dataType = fdp.ConsumeIntegral<int32_t>();
75 std::string peerDeviceId = fdp.ConsumeRandomLengthString();
76
77 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
78 dschedTransportSoftbusAdapter.ConnectDevice(peerDeviceId, sessionId);
79 std::shared_ptr<DSchedDataBuffer> dataBuffer = std::make_shared<DSchedDataBuffer>(size);
80 dschedTransportSoftbusAdapter.SendData(sessionId, dataType, dataBuffer);
81 dschedTransportSoftbusAdapter.SendBytesBySoftbus(sessionId, dataBuffer);
82 dschedTransportSoftbusAdapter.InitChannel();
83 dschedTransportSoftbusAdapter.CreateServerSocket();
84 dschedTransportSoftbusAdapter.CreateClientSocket(peerDeviceId);
85 bool isServer = sessionId % 2;
86 dschedTransportSoftbusAdapter.CreateSessionRecord(sessionId, peerDeviceId, isServer, SERVICE_TYPE_CONTINUE);
87 dschedTransportSoftbusAdapter.AddNewPeerSession(peerDeviceId, sessionId, SERVICE_TYPE_CONTINUE);
88 dschedTransportSoftbusAdapter.ShutdownSession(peerDeviceId, sessionId);
89 bool isSelfCalled = sessionId % 2;
90 dschedTransportSoftbusAdapter.NotifyListenersSessionShutdown(sessionId, isSelfCalled);
91 dschedTransportSoftbusAdapter.ReleaseChannel();
92 }
93
FuzzDisconnectDevice(const uint8_t * data,size_t size)94 void FuzzDisconnectDevice(const uint8_t* data, size_t size)
95 {
96 if ((data == nullptr) || (size < sizeof(size_t))) {
97 return;
98 }
99 std::string peerDeviceId(reinterpret_cast<const char*>(data), size);
100 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
101 dschedTransportSoftbusAdapter.DisconnectDevice(peerDeviceId);
102 }
103
104
FuzzOnDataReady(const uint8_t * data,size_t size)105 void FuzzOnDataReady(const uint8_t* data, size_t size)
106 {
107 if ((data == nullptr) || (size < sizeof(size_t)) || size >= MAX_BUFFER_SIZE) {
108 return;
109 }
110 FuzzedDataProvider fdp(data, size);
111 int32_t sessionId = fdp.ConsumeIntegral<int32_t>();
112 uint32_t dataType = fdp.ConsumeIntegral<uint32_t>();
113 std::shared_ptr<DSchedDataBuffer> dataBuffer = std::make_shared<DSchedDataBuffer>(size);
114 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
115 dschedTransportSoftbusAdapter.OnDataReady(sessionId, dataBuffer, dataType);
116 }
117
FuzzRegisterListener(const uint8_t * data,size_t size)118 void FuzzRegisterListener(const uint8_t* data, size_t size)
119 {
120 if ((data == nullptr) || (size < sizeof(size_t))) {
121 return;
122 }
123 int32_t serviceType = *(reinterpret_cast<const int32_t*>(data));
124 std::shared_ptr<DSchedContinueManager::SoftbusListener> listener =
125 std::make_shared<DSchedContinueManager::SoftbusListener>();
126 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
127 dschedTransportSoftbusAdapter.RegisterListener(serviceType, listener);
128 }
129
FuzzUnregisterListener(const uint8_t * data,size_t size)130 void FuzzUnregisterListener(const uint8_t* data, size_t size)
131 {
132 if ((data == nullptr) || (size < sizeof(size_t))) {
133 return;
134 }
135 int32_t serviceType = *(reinterpret_cast<const int32_t*>(data));
136 std::shared_ptr<DSchedContinueManager::SoftbusListener> listener =
137 std::make_shared<DSchedContinueManager::SoftbusListener>();
138 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
139 dschedTransportSoftbusAdapter.UnregisterListener(serviceType, listener);
140 }
141
FuzzSetCallingTokenId(const uint8_t * data,size_t size)142 void FuzzSetCallingTokenId(const uint8_t* data, size_t size)
143 {
144 if ((data == nullptr) || (size < sizeof(size_t))) {
145 return;
146 }
147 int32_t callingTokenId = *(reinterpret_cast<const int32_t*>(data));
148 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
149 dschedTransportSoftbusAdapter.SetCallingTokenId(callingTokenId);
150 }
151
FuzzGetSessionIdByDeviceId(const uint8_t * data,size_t size)152 void FuzzGetSessionIdByDeviceId(const uint8_t* data, size_t size)
153 {
154 if ((data == nullptr) || (size < sizeof(size_t))) {
155 return;
156 }
157 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
158 std::string peerDeviceId(reinterpret_cast<const char*>(data), size);
159 DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
160 dschedTransportSoftbusAdapter.GetSessionIdByDeviceId(peerDeviceId, sessionId);
161 }
162 }
163 }
164
165 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)166 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
167 {
168 OHOS::DistributedSchedule::FuzzOnBind(data, size);
169 OHOS::DistributedSchedule::FuzzOnShutdown(data, size);
170 OHOS::DistributedSchedule::FuzzOnBytes(data, size);
171 OHOS::DistributedSchedule::FuzzConnectDevice(data, size);
172 OHOS::DistributedSchedule::FuzzDisconnectDevice(data, size);
173 OHOS::DistributedSchedule::FuzzOnDataReady(data, size);
174 OHOS::DistributedSchedule::FuzzRegisterListener(data, size);
175 OHOS::DistributedSchedule::FuzzUnregisterListener(data, size);
176 OHOS::DistributedSchedule::FuzzSetCallingTokenId(data, size);
177 OHOS::DistributedSchedule::FuzzGetSessionIdByDeviceId(data, size);
178 return 0;
179 }
180