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