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 "dschedallconnectmanager_fuzzer.h"
17 
18 #include "dsched_all_connect_manager.h"
19 #include "service_collaboration_manager_capi.h"
20 
21 namespace OHOS {
22 namespace DistributedSchedule {
23 namespace {
24 constexpr uint32_t MIN_SIZE = 38;
25 constexpr uint32_t UINT32_T_OFFSET = 4;
26 constexpr uint32_t ENUM_OFFSET = 4;
27 constexpr uint32_t BOOL_OFFSET = 1;
28 constexpr uint32_t INT32_T_OFFSET = 4;
29 constexpr uint32_t CHAR_PONTER_OFFSET = 4;
30 }
31 
FuzzApplyAdvanceResource(const uint8_t * data,size_t size)32 void FuzzApplyAdvanceResource(const uint8_t* data, size_t size)
33 {
34     if ((data == nullptr) || (size < MIN_SIZE)) {
35         return;
36     }
37     ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets;
38 
39     reqInfoSets.remoteHardwareListSize = *(reinterpret_cast<const uint32_t*>(data));
40 
41     ServiceCollaborationManager_HardwareRequestInfo remoteHardwareListTemp;
42     uint32_t offset = UINT32_T_OFFSET;
43     remoteHardwareListTemp.hardWareType =
44         *(reinterpret_cast<const ServiceCollaborationManagerHardwareType*>(data + offset));
45     offset += ENUM_OFFSET;
46     remoteHardwareListTemp.canShare = *(reinterpret_cast<const bool*>(data + offset));
47     offset += BOOL_OFFSET;
48     reqInfoSets.remoteHardwareList = &remoteHardwareListTemp;
49 
50     reqInfoSets.localHardwareListSize = *(reinterpret_cast<const uint32_t*>(data + offset));
51     offset += UINT32_T_OFFSET;
52 
53     ServiceCollaborationManager_HardwareRequestInfo localHardwareListTemp;
54     localHardwareListTemp.hardWareType =
55         *(reinterpret_cast<const ServiceCollaborationManagerHardwareType*>(data + offset));
56     offset += ENUM_OFFSET;
57     localHardwareListTemp.canShare = *(reinterpret_cast<const bool*>(data + offset));
58     offset += BOOL_OFFSET;
59     reqInfoSets.localHardwareList = &localHardwareListTemp;
60 
61     ServiceCollaborationManager_CommunicationRequestInfo communicationRequestTemp;
62     communicationRequestTemp.minBandwidth = *(reinterpret_cast<const int32_t*>(data + offset));
63     offset += INT32_T_OFFSET;
64     communicationRequestTemp.maxLatency = *(reinterpret_cast<const int32_t*>(data + offset));
65     offset += INT32_T_OFFSET;
66     communicationRequestTemp.minLatency = *(reinterpret_cast<const int32_t*>(data + offset));
67     offset += INT32_T_OFFSET;
68     communicationRequestTemp.maxWaitTime = *(reinterpret_cast<const int32_t*>(data + offset));
69     offset += INT32_T_OFFSET;
70     communicationRequestTemp.dataType = reinterpret_cast<const char*>(data + offset);
71     offset += CHAR_PONTER_OFFSET;
72     reqInfoSets.communicationRequest = &communicationRequestTemp;
73 
74     const std::string peerNetworkId(reinterpret_cast<const char*>(data + offset), size - MIN_SIZE);
75     DSchedAllConnectManager::GetInstance().ApplyAdvanceResource(peerNetworkId, reqInfoSets);
76 }
77 
FuzzGetResourceRequest(const uint8_t * data,size_t size)78 void FuzzGetResourceRequest(const uint8_t* data, size_t size)
79 {
80     if ((data == nullptr) || (size < MIN_SIZE)) {
81         return;
82     }
83     ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets;
84 
85     reqInfoSets.remoteHardwareListSize = *(reinterpret_cast<const uint32_t*>(data));
86 
87     ServiceCollaborationManager_HardwareRequestInfo remoteHardwareListTemp;
88     uint32_t offset = UINT32_T_OFFSET;
89     remoteHardwareListTemp.hardWareType =
90         *(reinterpret_cast<const ServiceCollaborationManagerHardwareType*>(data + offset));
91     offset += ENUM_OFFSET;
92     remoteHardwareListTemp.canShare = *(reinterpret_cast<const bool*>(data + offset));
93     offset += BOOL_OFFSET;
94     reqInfoSets.remoteHardwareList = &remoteHardwareListTemp;
95 
96     reqInfoSets.localHardwareListSize = *(reinterpret_cast<const uint32_t*>(data + offset));
97     offset += UINT32_T_OFFSET;
98 
99     ServiceCollaborationManager_HardwareRequestInfo localHardwareListTemp;
100     localHardwareListTemp.hardWareType =
101         *(reinterpret_cast<const ServiceCollaborationManagerHardwareType*>(data + offset));
102     offset += ENUM_OFFSET;
103     localHardwareListTemp.canShare = *(reinterpret_cast<const bool*>(data + offset));
104     offset += BOOL_OFFSET;
105     reqInfoSets.localHardwareList = &localHardwareListTemp;
106 
107     ServiceCollaborationManager_CommunicationRequestInfo communicationRequestTemp;
108     communicationRequestTemp.minBandwidth = *(reinterpret_cast<const int32_t*>(data + offset));
109     offset += INT32_T_OFFSET;
110     communicationRequestTemp.maxLatency = *(reinterpret_cast<const int32_t*>(data + offset));
111     offset += INT32_T_OFFSET;
112     communicationRequestTemp.minLatency = *(reinterpret_cast<const int32_t*>(data + offset));
113     offset += INT32_T_OFFSET;
114     communicationRequestTemp.maxWaitTime = *(reinterpret_cast<const int32_t*>(data + offset));
115     offset += INT32_T_OFFSET;
116     communicationRequestTemp.dataType = reinterpret_cast<const char*>(data + offset);
117     reqInfoSets.communicationRequest = &communicationRequestTemp;
118     DSchedAllConnectManager::GetInstance().GetResourceRequest(reqInfoSets);
119 }
120 
FuzzPublishServiceState(const uint8_t * data,size_t size)121 void FuzzPublishServiceState(const uint8_t* data, size_t size)
122 {
123     if ((data == nullptr) || (size < sizeof(size_t))) {
124         return;
125     }
126     const std::string peerNetworkId(reinterpret_cast<const char*>(data), size);
127     const std::string extraInfo(reinterpret_cast<const char*>(data), size);
128     uint8_t* temp = const_cast<uint8_t*>(data);
129     ServiceCollaborationManagerBussinessStatus state =
130         *(reinterpret_cast<ServiceCollaborationManagerBussinessStatus*>(temp));
131     DSchedAllConnectManager::GetInstance().PublishServiceState(peerNetworkId, extraInfo, state);
132 }
133 }
134 }
135 
136 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
138 {
139     OHOS::DistributedSchedule::FuzzApplyAdvanceResource(data, size);
140     OHOS::DistributedSchedule::FuzzGetResourceRequest(data, size);
141     OHOS::DistributedSchedule::FuzzPublishServiceState(data, size);
142     return 0;
143 }
144