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