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 "softbusadapter_fuzzer.h"
17
18 #include <cstddef>
19 #include "securec.h"
20 #include <cstdint>
21 #include <unistd.h>
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 #include "distributed_mission_broadcast_listener.h"
25 #include "softbus_adapter/softbus_adapter.h"
26
27 namespace OHOS {
28 namespace DistributedSchedule {
29 #ifdef DMSFWK_INTERACTIVE_ADAPTER
30 namespace {
31 constexpr size_t CAPACITY = 10;
32 const int32_t WAITTIME = 10000;
33 }
34 #endif
35
FuzzSendSoftbusEvent(const uint8_t * data,size_t size)36 void FuzzSendSoftbusEvent(const uint8_t* data, size_t size)
37 {
38 if ((data == nullptr) || (size < sizeof(uint32_t))) {
39 return;
40 }
41 std::shared_ptr<DSchedDataBuffer> buffer = std::make_shared<DSchedDataBuffer>(size);
42 if (memcpy_s(buffer->Data(), buffer->Capacity(), data, size) != ERR_OK) {
43 return;
44 }
45 SoftbusAdapter::GetInstance().SendSoftbusEvent(buffer);
46 SoftbusAdapter::GetInstance().StopSoftbusEvent();
47 }
48
FuzzOnBroadCastRecv(const uint8_t * data,size_t size)49 void FuzzOnBroadCastRecv(const uint8_t* data, size_t size)
50 {
51 if ((data == nullptr) || (size < sizeof(uint32_t))) {
52 return;
53 }
54 FuzzedDataProvider fdp(data, size);
55 int32_t dataLen = fdp.ConsumeIntegral<uint32_t>();
56 std::string networkId = fdp.ConsumeRandomLengthString();
57 uint8_t* newdata = const_cast<uint8_t*>(data);
58 SoftbusAdapter::GetInstance().OnBroadCastRecv(networkId, newdata, dataLen);
59 std::shared_ptr<SoftbusAdapterListener> listener = std::make_shared<DistributedMissionBroadcastListener>();
60 SoftbusAdapter::GetInstance().RegisterSoftbusEventListener(listener);
61 SoftbusAdapter::GetInstance().UnregisterSoftbusEventListener(listener);
62 }
63
64 #ifdef DMSFWK_INTERACTIVE_ADAPTER
FuzzDealSendSoftbusEvent(const uint8_t * data,size_t size)65 void FuzzDealSendSoftbusEvent(const uint8_t* data, size_t size)
66 {
67 if ((data == nullptr) || (size < sizeof(uint32_t))) {
68 return;
69 }
70 FuzzedDataProvider fdp(data, size);
71 std::shared_ptr<DSchedDataBuffer> buffer = nullptr;
72 int32_t retry = fdp.ConsumeIntegral<int32_t>();
73 SoftbusAdapter::GetInstance().Init();
74 SoftbusAdapter::GetInstance().DealSendSoftbusEvent(buffer, retry);
75 SoftbusAdapter::GetInstance().RetrySendSoftbusEvent(buffer, retry);
76
77 buffer = std::make_shared<DSchedDataBuffer>(CAPACITY);
78 SoftbusAdapter::GetInstance().DealSendSoftbusEvent(buffer, retry);
79 SoftbusAdapter::GetInstance().RetrySendSoftbusEvent(buffer, retry);
80 usleep(WAITTIME);
81 SoftbusAdapter::GetInstance().UnInit();
82 }
83 #endif
84 }
85 }
86
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90 OHOS::DistributedSchedule::FuzzSendSoftbusEvent(data, size);
91 OHOS::DistributedSchedule::FuzzOnBroadCastRecv(data, size);
92 #ifdef DMSFWK_INTERACTIVE_ADAPTER
93 OHOS::DistributedSchedule::FuzzDealSendSoftbusEvent(data, size);
94 #endif
95 return 0;
96 }
97