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 "ipcsocket_fuzzer.h"
17
18 #include "singleton.h"
19
20 #include "devicestatus_define.h"
21 #include "socket_client.h"
22 #include "socket_session_manager.h"
23 #include "socket_session.h"
24 #include "socket_connection.h"
25 #include "stream_client.h"
26
27 #include "message_parcel.h"
28
29 #undef LOG_TAG
30 #define LOG_TAG "IpcSocketFuzzTest"
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 const std::u16string FORMMGR_INTERFACE_TOKEN { u"ohos.msdp.Idevicestatus" };
35 inline constexpr int32_t MAX_EVENT_SIZE { 100 };
36 const uint8_t *g_baseFuzzData = nullptr;
37 size_t g_baseFuzzSize = 0;
38 size_t g_baseFuzzPos = 0;
39
40 namespace OHOS {
41
GetData()42 template <class T> T GetData()
43 {
44 T objetct{};
45 size_t objetctSize = sizeof(objetct);
46 if (g_baseFuzzData == nullptr || objetctSize > g_baseFuzzSize - g_baseFuzzPos) {
47 return objetct;
48 }
49 errno_t ret = memcpy_s(&objetct, objetctSize, g_baseFuzzData + g_baseFuzzPos, objetctSize);
50 if (ret != EOK) {
51 return {};
52 }
53 g_baseFuzzPos += objetctSize;
54 return objetct;
55 }
56
SocketConnectionFuzzTest(const uint8_t * data,size_t size)57 bool SocketConnectionFuzzTest(const uint8_t* data, size_t size)
58 {
59 if ((data == nullptr) || (size < 1)) {
60 return false;
61 }
62
63 auto recv = [](const NetPacket &pkt) {
64 return;
65 };
66 auto onDisconnected = []() {
67 return;
68 };
69 g_baseFuzzData = data;
70 g_baseFuzzSize = size;
71 g_baseFuzzPos = 0;
72 int32_t fd = GetData<int32_t>();
73 SocketConnection socketConnection(1, recv, onDisconnected);
74
75 auto socket = []() {
76 return 0;
77 };
78 NetPacket packet(MessageId::COORDINATION_ADD_LISTENER);
79 struct epoll_event ev{};
80 auto client = std::make_unique<SocketClient>();
81 client->Connect();
82 client->Socket();
83 MessageId msgId { MessageId::INVALID };
84 NetPacket pkt(msgId);
85 client->OnPacket(pkt);
86 client->Reconnect();
87 client->Stop();
88 client->OnDisconnected();
89 SocketSession socketSession("testProgramName", 1, 1, 1, 1, 1);
90 socketSession.SendMsg(packet);
91 socketSession.ToString();
92 socketSession.Dispatch(ev);
93 socketConnection.OnReadable(fd);
94 socketConnection.OnShutdown(fd);
95 socketConnection.OnException(fd);
96 Msdp::DeviceStatus::SocketConnection::Connect(socket, recv, onDisconnected);
97 client = nullptr;
98 return true;
99 }
100
101 } // namespace OHOS
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
103 {
104 /* Run your code on data */
105 if (data == nullptr) {
106 return 0;
107 }
108
109 OHOS::SocketConnectionFuzzTest(data, size);
110 return 0;
111 }
112 } // namespace DeviceStatus
113 } // namespace Msdp
114 } // namespace OHOS