1f857971dSopenharmony_ci/* 2f857971dSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f857971dSopenharmony_ci * you may not use this file except in compliance with the License. 5f857971dSopenharmony_ci * You may obtain a copy of the License at 6f857971dSopenharmony_ci * 7f857971dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f857971dSopenharmony_ci * 9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f857971dSopenharmony_ci * See the License for the specific language governing permissions and 13f857971dSopenharmony_ci * limitations under the License. 14f857971dSopenharmony_ci */ 15f857971dSopenharmony_ci 16f857971dSopenharmony_ci#include "msdpdevicemanager_fuzzer.h" 17f857971dSopenharmony_ci#include "ddm_adapter.h" 18f857971dSopenharmony_ci#include "devicestatus_define.h" 19f857971dSopenharmony_ci 20f857971dSopenharmony_ci#undef LOG_TAG 21f857971dSopenharmony_ci#define LOG_TAG "MsdpDeviceManagerFuzzTest" 22f857971dSopenharmony_cinamespace OHOS { 23f857971dSopenharmony_cinamespace Msdp { 24f857971dSopenharmony_cinamespace DeviceStatus { 25f857971dSopenharmony_cinamespace { 26f857971dSopenharmony_cistruct DeviceStatusEpollEvent { 27f857971dSopenharmony_ci int32_t fd { -1 }; 28f857971dSopenharmony_ci EpollEventType eventType { EPOLL_EVENT_BEGIN }; 29f857971dSopenharmony_ci}; 30f857971dSopenharmony_ci 31f857971dSopenharmony_ciconst uint8_t *g_baseFuzzData = nullptr; 32f857971dSopenharmony_cisize_t g_baseFuzzSize = 0; 33f857971dSopenharmony_cisize_t g_baseFuzzPos = 0; 34f857971dSopenharmony_ciconstexpr size_t STR_LEN = 255; 35f857971dSopenharmony_ciContextService *g_instance = nullptr; 36f857971dSopenharmony_ciconstexpr int32_t DEFAULT_WAIT_TIME_MS { 1000 }; 37f857971dSopenharmony_ciconstexpr int32_t WAIT_FOR_ONCE { 1 }; 38f857971dSopenharmony_ciconstexpr int32_t MAX_N_RETRIES { 100 }; 39f857971dSopenharmony_ci} // namespace 40f857971dSopenharmony_ci 41f857971dSopenharmony_ciContextService::ContextService() 42f857971dSopenharmony_ci{ 43f857971dSopenharmony_ci ddm_ = std::make_unique<DDMAdapter>(); 44f857971dSopenharmony_ci 45f857971dSopenharmony_ci OnStart(); 46f857971dSopenharmony_ci} 47f857971dSopenharmony_ci 48f857971dSopenharmony_ciContextService::~ContextService() 49f857971dSopenharmony_ci{ 50f857971dSopenharmony_ci OnStop(); 51f857971dSopenharmony_ci} 52f857971dSopenharmony_ci 53f857971dSopenharmony_ciIDelegateTasks& ContextService::GetDelegateTasks() 54f857971dSopenharmony_ci{ 55f857971dSopenharmony_ci return delegateTasks_; 56f857971dSopenharmony_ci} 57f857971dSopenharmony_ci 58f857971dSopenharmony_ciIDeviceManager& ContextService::GetDeviceManager() 59f857971dSopenharmony_ci{ 60f857971dSopenharmony_ci return devMgr_; 61f857971dSopenharmony_ci} 62f857971dSopenharmony_ci 63f857971dSopenharmony_ciITimerManager& ContextService::GetTimerManager() 64f857971dSopenharmony_ci{ 65f857971dSopenharmony_ci return timerMgr_; 66f857971dSopenharmony_ci} 67f857971dSopenharmony_ci 68f857971dSopenharmony_ciIDragManager& ContextService::GetDragManager() 69f857971dSopenharmony_ci{ 70f857971dSopenharmony_ci return dragMgr_; 71f857971dSopenharmony_ci} 72f857971dSopenharmony_ci 73f857971dSopenharmony_ciContextService* ContextService::GetInstance() 74f857971dSopenharmony_ci{ 75f857971dSopenharmony_ci static std::once_flag flag; 76f857971dSopenharmony_ci std::call_once(flag, [&]() { 77f857971dSopenharmony_ci ContextService *cooContext = new (std::nothrow) ContextService(); 78f857971dSopenharmony_ci CHKPL(cooContext); 79f857971dSopenharmony_ci g_instance = cooContext; 80f857971dSopenharmony_ci }); 81f857971dSopenharmony_ci return g_instance; 82f857971dSopenharmony_ci} 83f857971dSopenharmony_ci 84f857971dSopenharmony_ciISocketSessionManager& ContextService::GetSocketSessionManager() 85f857971dSopenharmony_ci{ 86f857971dSopenharmony_ci return socketSessionMgr_; 87f857971dSopenharmony_ci} 88f857971dSopenharmony_ci 89f857971dSopenharmony_ciIDDMAdapter& ContextService::GetDDM() 90f857971dSopenharmony_ci{ 91f857971dSopenharmony_ci return *ddm_; 92f857971dSopenharmony_ci} 93f857971dSopenharmony_ci 94f857971dSopenharmony_ciIPluginManager& ContextService::GetPluginManager() 95f857971dSopenharmony_ci{ 96f857971dSopenharmony_ci return *pluginMgr_; 97f857971dSopenharmony_ci} 98f857971dSopenharmony_ci 99f857971dSopenharmony_ciIInputAdapter& ContextService::GetInput() 100f857971dSopenharmony_ci{ 101f857971dSopenharmony_ci return *input_; 102f857971dSopenharmony_ci} 103f857971dSopenharmony_ci 104f857971dSopenharmony_ciIDSoftbusAdapter& ContextService::GetDSoftbus() 105f857971dSopenharmony_ci{ 106f857971dSopenharmony_ci return *dsoftbusAda_; 107f857971dSopenharmony_ci} 108f857971dSopenharmony_ci 109f857971dSopenharmony_cibool ContextService::Init() 110f857971dSopenharmony_ci{ 111f857971dSopenharmony_ci CALL_DEBUG_ENTER; 112f857971dSopenharmony_ci if (EpollCreate() != RET_OK) { 113f857971dSopenharmony_ci FI_HILOGE("Create epoll failed"); 114f857971dSopenharmony_ci return false; 115f857971dSopenharmony_ci } 116f857971dSopenharmony_ci if (InitDelegateTasks() != RET_OK) { 117f857971dSopenharmony_ci FI_HILOGE("Delegate tasks init failed"); 118f857971dSopenharmony_ci goto INIT_FAIL; 119f857971dSopenharmony_ci } 120f857971dSopenharmony_ci if (InitTimerMgr() != RET_OK) { 121f857971dSopenharmony_ci FI_HILOGE("TimerMgr init failed"); 122f857971dSopenharmony_ci goto INIT_FAIL; 123f857971dSopenharmony_ci } 124f857971dSopenharmony_ci if (InitDevMgr() != RET_OK) { 125f857971dSopenharmony_ci FI_HILOGE("DevMgr init failed"); 126f857971dSopenharmony_ci goto INIT_FAIL; 127f857971dSopenharmony_ci } 128f857971dSopenharmony_ci 129f857971dSopenharmony_ci return true; 130f857971dSopenharmony_ci 131f857971dSopenharmony_ciINIT_FAIL: 132f857971dSopenharmony_ci EpollClose(); 133f857971dSopenharmony_ci return false; 134f857971dSopenharmony_ci} 135f857971dSopenharmony_ciint32_t ContextService::InitDevMgr() 136f857971dSopenharmony_ci{ 137f857971dSopenharmony_ci CALL_DEBUG_ENTER; 138f857971dSopenharmony_ci int32_t ret = devMgr_.Init(this); 139f857971dSopenharmony_ci if (ret != RET_OK) { 140f857971dSopenharmony_ci FI_HILOGE("DevMgr init failed"); 141f857971dSopenharmony_ci return ret; 142f857971dSopenharmony_ci } 143f857971dSopenharmony_ci return ret; 144f857971dSopenharmony_ci} 145f857971dSopenharmony_ci 146f857971dSopenharmony_ciint32_t ContextService::InitTimerMgr() 147f857971dSopenharmony_ci{ 148f857971dSopenharmony_ci CALL_DEBUG_ENTER; 149f857971dSopenharmony_ci int32_t ret = timerMgr_.Init(this); 150f857971dSopenharmony_ci if (ret != RET_OK) { 151f857971dSopenharmony_ci FI_HILOGE("TimerMgr init failed"); 152f857971dSopenharmony_ci return ret; 153f857971dSopenharmony_ci } 154f857971dSopenharmony_ci 155f857971dSopenharmony_ci ret = AddEpoll(EPOLL_EVENT_TIMER, timerMgr_.GetTimerFd()); 156f857971dSopenharmony_ci if (ret != RET_OK) { 157f857971dSopenharmony_ci FI_HILOGE("AddEpoll for timer failed"); 158f857971dSopenharmony_ci } 159f857971dSopenharmony_ci return ret; 160f857971dSopenharmony_ci} 161f857971dSopenharmony_ci 162f857971dSopenharmony_ciint32_t ContextService::InitDelegateTasks() 163f857971dSopenharmony_ci{ 164f857971dSopenharmony_ci CALL_DEBUG_ENTER; 165f857971dSopenharmony_ci if (!delegateTasks_.Init()) { 166f857971dSopenharmony_ci FI_HILOGE("The delegate task init failed"); 167f857971dSopenharmony_ci return RET_ERR; 168f857971dSopenharmony_ci } 169f857971dSopenharmony_ci int32_t ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd()); 170f857971dSopenharmony_ci if (ret != RET_OK) { 171f857971dSopenharmony_ci FI_HILOGE("AddEpoll error ret:%{public}d", ret); 172f857971dSopenharmony_ci } 173f857971dSopenharmony_ci FI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", epollFd_, delegateTasks_.GetReadFd()); 174f857971dSopenharmony_ci return ret; 175f857971dSopenharmony_ci} 176f857971dSopenharmony_ci 177f857971dSopenharmony_ciint32_t ContextService::EpollCreate() 178f857971dSopenharmony_ci{ 179f857971dSopenharmony_ci CALL_DEBUG_ENTER; 180f857971dSopenharmony_ci epollFd_ = ::epoll_create1(EPOLL_CLOEXEC); 181f857971dSopenharmony_ci if (epollFd_ < 0) { 182f857971dSopenharmony_ci FI_HILOGE("epoll_create1 failed:%{public}s", ::strerror(errno)); 183f857971dSopenharmony_ci return RET_ERR; 184f857971dSopenharmony_ci } 185f857971dSopenharmony_ci return RET_OK; 186f857971dSopenharmony_ci} 187f857971dSopenharmony_ci 188f857971dSopenharmony_ciint32_t ContextService::AddEpoll(EpollEventType type, int32_t fd) 189f857971dSopenharmony_ci{ 190f857971dSopenharmony_ci CALL_DEBUG_ENTER; 191f857971dSopenharmony_ci if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) { 192f857971dSopenharmony_ci FI_HILOGE("Invalid type:%{public}d", type); 193f857971dSopenharmony_ci return RET_ERR; 194f857971dSopenharmony_ci } 195f857971dSopenharmony_ci if (fd < 0) { 196f857971dSopenharmony_ci FI_HILOGE("Invalid fd:%{public}d", fd); 197f857971dSopenharmony_ci return RET_ERR; 198f857971dSopenharmony_ci } 199f857971dSopenharmony_ci auto eventData = static_cast<DeviceStatusEpollEvent*>(malloc(sizeof(DeviceStatusEpollEvent))); 200f857971dSopenharmony_ci if (!eventData) { 201f857971dSopenharmony_ci FI_HILOGE("Malloc failed"); 202f857971dSopenharmony_ci return RET_ERR; 203f857971dSopenharmony_ci } 204f857971dSopenharmony_ci eventData->fd = fd; 205f857971dSopenharmony_ci eventData->eventType = type; 206f857971dSopenharmony_ci FI_HILOGD("EventData:[fd:%{public}d, type:%{public}d]", eventData->fd, eventData->eventType); 207f857971dSopenharmony_ci 208f857971dSopenharmony_ci struct epoll_event ev {}; 209f857971dSopenharmony_ci ev.events = EPOLLIN; 210f857971dSopenharmony_ci ev.data.ptr = eventData; 211f857971dSopenharmony_ci if (EpollCtl(fd, EPOLL_CTL_ADD, ev) != RET_OK) { 212f857971dSopenharmony_ci free(eventData); 213f857971dSopenharmony_ci eventData = nullptr; 214f857971dSopenharmony_ci ev.data.ptr = nullptr; 215f857971dSopenharmony_ci FI_HILOGE("EpollCtl failed"); 216f857971dSopenharmony_ci return RET_ERR; 217f857971dSopenharmony_ci } 218f857971dSopenharmony_ci return RET_OK; 219f857971dSopenharmony_ci} 220f857971dSopenharmony_ci 221f857971dSopenharmony_ciint32_t ContextService::DelEpoll(EpollEventType type, int32_t fd) 222f857971dSopenharmony_ci{ 223f857971dSopenharmony_ci CALL_DEBUG_ENTER; 224f857971dSopenharmony_ci if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) { 225f857971dSopenharmony_ci FI_HILOGE("Invalid type:%{public}d", type); 226f857971dSopenharmony_ci return RET_ERR; 227f857971dSopenharmony_ci } 228f857971dSopenharmony_ci if (fd < 0) { 229f857971dSopenharmony_ci FI_HILOGE("Invalid fd:%{public}d", fd); 230f857971dSopenharmony_ci return RET_ERR; 231f857971dSopenharmony_ci } 232f857971dSopenharmony_ci struct epoll_event ev {}; 233f857971dSopenharmony_ci if (EpollCtl(fd, EPOLL_CTL_DEL, ev) != RET_OK) { 234f857971dSopenharmony_ci FI_HILOGE("DelEpoll failed"); 235f857971dSopenharmony_ci return RET_ERR; 236f857971dSopenharmony_ci } 237f857971dSopenharmony_ci return RET_OK; 238f857971dSopenharmony_ci} 239f857971dSopenharmony_ci 240f857971dSopenharmony_civoid ContextService::EpollClose() 241f857971dSopenharmony_ci{ 242f857971dSopenharmony_ci CALL_DEBUG_ENTER; 243f857971dSopenharmony_ci if (epollFd_ >= 0) { 244f857971dSopenharmony_ci if (close(epollFd_) < 0) { 245f857971dSopenharmony_ci FI_HILOGE("Close epoll fd failed, error:%{public}s, epollFd_:%{public}d", strerror(errno), epollFd_); 246f857971dSopenharmony_ci } 247f857971dSopenharmony_ci epollFd_ = -1; 248f857971dSopenharmony_ci } 249f857971dSopenharmony_ci} 250f857971dSopenharmony_ci 251f857971dSopenharmony_ciint32_t ContextService::EpollCtl(int32_t fd, int32_t op, struct epoll_event &event) 252f857971dSopenharmony_ci{ 253f857971dSopenharmony_ci CALL_DEBUG_ENTER; 254f857971dSopenharmony_ci if (fd < 0) { 255f857971dSopenharmony_ci FI_HILOGE("Invalid fd:%{public}d", fd); 256f857971dSopenharmony_ci return RET_ERR; 257f857971dSopenharmony_ci } 258f857971dSopenharmony_ci if (epollFd_ < 0) { 259f857971dSopenharmony_ci FI_HILOGE("Invalid epollFd:%{public}d", epollFd_); 260f857971dSopenharmony_ci return RET_ERR; 261f857971dSopenharmony_ci } 262f857971dSopenharmony_ci if (::epoll_ctl(epollFd_, op, fd, &event) != 0) { 263f857971dSopenharmony_ci FI_HILOGE("epoll_ctl(%{public}d,%{public}d,%{public}d) failed:%{public}s", epollFd_, op, fd, ::strerror(errno)); 264f857971dSopenharmony_ci return RET_ERR; 265f857971dSopenharmony_ci } 266f857971dSopenharmony_ci return RET_OK; 267f857971dSopenharmony_ci} 268f857971dSopenharmony_ci 269f857971dSopenharmony_ciint32_t ContextService::EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events) 270f857971dSopenharmony_ci{ 271f857971dSopenharmony_ci if (epollFd_ < 0) { 272f857971dSopenharmony_ci FI_HILOGE("Invalid epollFd:%{public}d", epollFd_); 273f857971dSopenharmony_ci return RET_ERR; 274f857971dSopenharmony_ci } 275f857971dSopenharmony_ci return epoll_wait(epollFd_, &events, maxevents, timeout); 276f857971dSopenharmony_ci} 277f857971dSopenharmony_ci 278f857971dSopenharmony_civoid ContextService::OnTimeout(const struct epoll_event &ev) 279f857971dSopenharmony_ci{ 280f857971dSopenharmony_ci CALL_DEBUG_ENTER; 281f857971dSopenharmony_ci if ((ev.events & EPOLLIN) == EPOLLIN) { 282f857971dSopenharmony_ci uint64_t expiration {}; 283f857971dSopenharmony_ci ssize_t ret = read(timerMgr_.GetTimerFd(), &expiration, sizeof(expiration)); 284f857971dSopenharmony_ci if (ret < 0) { 285f857971dSopenharmony_ci FI_HILOGE("Read expiration failed:%{public}s", strerror(errno)); 286f857971dSopenharmony_ci } 287f857971dSopenharmony_ci timerMgr_.ProcessTimers(); 288f857971dSopenharmony_ci } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) { 289f857971dSopenharmony_ci FI_HILOGE("Epoll hangup:%{public}s", strerror(errno)); 290f857971dSopenharmony_ci } 291f857971dSopenharmony_ci} 292f857971dSopenharmony_ci 293f857971dSopenharmony_civoid ContextService::OnDeviceMgr(const struct epoll_event &ev) 294f857971dSopenharmony_ci{ 295f857971dSopenharmony_ci CALL_DEBUG_ENTER; 296f857971dSopenharmony_ci if ((ev.events & EPOLLIN) == EPOLLIN) { 297f857971dSopenharmony_ci devMgr_.Dispatch(ev); 298f857971dSopenharmony_ci } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) { 299f857971dSopenharmony_ci FI_HILOGE("Epoll hangup:%{public}s", strerror(errno)); 300f857971dSopenharmony_ci } 301f857971dSopenharmony_ci} 302f857971dSopenharmony_ci 303f857971dSopenharmony_ciint32_t ContextService::EnableDevMgr(int32_t nRetries) 304f857971dSopenharmony_ci{ 305f857971dSopenharmony_ci CALL_INFO_TRACE; 306f857971dSopenharmony_ci static int32_t timerId { -1 }; 307f857971dSopenharmony_ci int32_t ret = devMgr_.Enable(); 308f857971dSopenharmony_ci if (ret != RET_OK) { 309f857971dSopenharmony_ci FI_HILOGE("Failed to enable device manager"); 310f857971dSopenharmony_ci if (nRetries > 0) { 311f857971dSopenharmony_ci timerId = timerMgr_.AddTimer(DEFAULT_WAIT_TIME_MS, WAIT_FOR_ONCE, 312f857971dSopenharmony_ci [this, nRetries] { return this->EnableDevMgr(nRetries - 1); }); 313f857971dSopenharmony_ci if (timerId < 0) { 314f857971dSopenharmony_ci FI_HILOGE("AddTimer failed, Failed to enable device manager"); 315f857971dSopenharmony_ci } 316f857971dSopenharmony_ci } else { 317f857971dSopenharmony_ci FI_HILOGE("Maximum number of retries exceeded, Failed to enable device manager"); 318f857971dSopenharmony_ci } 319f857971dSopenharmony_ci return ret; 320f857971dSopenharmony_ci } 321f857971dSopenharmony_ci AddEpoll(EPOLL_EVENT_DEVICE_MGR, devMgr_.GetFd()); 322f857971dSopenharmony_ci if (timerId >= 0) { 323f857971dSopenharmony_ci timerMgr_.RemoveTimer(timerId); 324f857971dSopenharmony_ci timerId = -1; 325f857971dSopenharmony_ci } 326f857971dSopenharmony_ci return RET_OK; 327f857971dSopenharmony_ci} 328f857971dSopenharmony_ci 329f857971dSopenharmony_civoid ContextService::DisableDevMgr() 330f857971dSopenharmony_ci{ 331f857971dSopenharmony_ci DelEpoll(EPOLL_EVENT_DEVICE_MGR, devMgr_.GetFd()); 332f857971dSopenharmony_ci devMgr_.Disable(); 333f857971dSopenharmony_ci} 334f857971dSopenharmony_ci 335f857971dSopenharmony_civoid ContextService::OnStart() 336f857971dSopenharmony_ci{ 337f857971dSopenharmony_ci CALL_DEBUG_ENTER; 338f857971dSopenharmony_ci uint64_t tid = GetThisThreadId(); 339f857971dSopenharmony_ci delegateTasks_.SetWorkerThreadId(tid); 340f857971dSopenharmony_ci 341f857971dSopenharmony_ci if (!Init()) { 342f857971dSopenharmony_ci FI_HILOGE("On start call init failed"); 343f857971dSopenharmony_ci return; 344f857971dSopenharmony_ci } 345f857971dSopenharmony_ci state_ = ServiceRunningState::STATE_RUNNING; 346f857971dSopenharmony_ci ready_ = true; 347f857971dSopenharmony_ci 348f857971dSopenharmony_ci worker_ = std::thread(std::bind(&ContextService::OnThread, this)); 349f857971dSopenharmony_ci} 350f857971dSopenharmony_ci 351f857971dSopenharmony_civoid ContextService::OnStop() 352f857971dSopenharmony_ci{ 353f857971dSopenharmony_ci CALL_DEBUG_ENTER; 354f857971dSopenharmony_ci if (timerMgr_.GetTimerFd() >= 0) { 355f857971dSopenharmony_ci if (close(timerMgr_.GetTimerFd()) < 0) { 356f857971dSopenharmony_ci FI_HILOGE("Close timer fd failed, error:%{public}s", strerror(errno)); 357f857971dSopenharmony_ci } 358f857971dSopenharmony_ci } 359f857971dSopenharmony_ci if (!ready_) { 360f857971dSopenharmony_ci FI_HILOGI("ready state is false"); 361f857971dSopenharmony_ci return; 362f857971dSopenharmony_ci } 363f857971dSopenharmony_ci ready_ = false; 364f857971dSopenharmony_ci state_ = ServiceRunningState::STATE_EXIT; 365f857971dSopenharmony_ci 366f857971dSopenharmony_ci delegateTasks_.PostAsyncTask([]() -> int32_t { 367f857971dSopenharmony_ci FI_HILOGD("No asynchronous operations"); 368f857971dSopenharmony_ci return RET_OK; 369f857971dSopenharmony_ci }); 370f857971dSopenharmony_ci if (worker_.joinable()) { 371f857971dSopenharmony_ci worker_.join(); 372f857971dSopenharmony_ci } 373f857971dSopenharmony_ci DisableDevMgr(); 374f857971dSopenharmony_ci EpollClose(); 375f857971dSopenharmony_ci FI_HILOGI("OnStop leave"); 376f857971dSopenharmony_ci} 377f857971dSopenharmony_ci 378f857971dSopenharmony_civoid ContextService::OnThread() 379f857971dSopenharmony_ci{ 380f857971dSopenharmony_ci CALL_DEBUG_ENTER; 381f857971dSopenharmony_ci SetThreadName(std::string("os_ds_service")); 382f857971dSopenharmony_ci uint64_t tid = GetThisThreadId(); 383f857971dSopenharmony_ci delegateTasks_.SetWorkerThreadId(tid); 384f857971dSopenharmony_ci EnableDevMgr(MAX_N_RETRIES); 385f857971dSopenharmony_ci FI_HILOGD("Main worker thread start, tid:%{public}" PRId64 "", tid); 386f857971dSopenharmony_ci 387f857971dSopenharmony_ci while (state_ == ServiceRunningState::STATE_RUNNING) { 388f857971dSopenharmony_ci struct epoll_event ev[MAX_EVENT_SIZE] {}; 389f857971dSopenharmony_ci int32_t count = EpollWait(MAX_EVENT_SIZE, -1, ev[0]); 390f857971dSopenharmony_ci for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) { 391f857971dSopenharmony_ci auto epollEvent = reinterpret_cast<DeviceStatusEpollEvent*>(ev[i].data.ptr); 392f857971dSopenharmony_ci CHKPC(epollEvent); 393f857971dSopenharmony_ci if (epollEvent->eventType == EPOLL_EVENT_TIMER) { 394f857971dSopenharmony_ci OnTimeout(ev[i]); 395f857971dSopenharmony_ci } else if (epollEvent->eventType == EPOLL_EVENT_ETASK) { 396f857971dSopenharmony_ci OnDelegateTask(ev[i]); 397f857971dSopenharmony_ci } else if (epollEvent->eventType == EPOLL_EVENT_DEVICE_MGR) { 398f857971dSopenharmony_ci OnDeviceMgr(ev[i]); 399f857971dSopenharmony_ci } else { 400f857971dSopenharmony_ci FI_HILOGW("Unknown epoll event type:%{public}d", epollEvent->eventType); 401f857971dSopenharmony_ci } 402f857971dSopenharmony_ci } 403f857971dSopenharmony_ci } 404f857971dSopenharmony_ci FI_HILOGD("Main worker thread stop, tid:%{public}" PRId64 "", tid); 405f857971dSopenharmony_ci} 406f857971dSopenharmony_ci 407f857971dSopenharmony_civoid ContextService::OnDelegateTask(const struct epoll_event &ev) 408f857971dSopenharmony_ci{ 409f857971dSopenharmony_ci if ((ev.events & EPOLLIN) == 0) { 410f857971dSopenharmony_ci FI_HILOGW("Not epollin"); 411f857971dSopenharmony_ci return; 412f857971dSopenharmony_ci } 413f857971dSopenharmony_ci DelegateTasks::TaskData data {}; 414f857971dSopenharmony_ci ssize_t res = read(delegateTasks_.GetReadFd(), &data, sizeof(data)); 415f857971dSopenharmony_ci if (res == -1) { 416f857971dSopenharmony_ci FI_HILOGW("Read failed erron:%{public}d", errno); 417f857971dSopenharmony_ci } 418f857971dSopenharmony_ci FI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ", std:%{public}" PRId64 "" 419f857971dSopenharmony_ci ", taskId:%{public}d", GetThisThreadId(), data.tid, data.taskId); 420f857971dSopenharmony_ci delegateTasks_.ProcessTasks(); 421f857971dSopenharmony_ci} 422f857971dSopenharmony_ci 423f857971dSopenharmony_citemplate <class T> T GetData() 424f857971dSopenharmony_ci{ 425f857971dSopenharmony_ci T objetct{}; 426f857971dSopenharmony_ci size_t objetctSize = sizeof(objetct); 427f857971dSopenharmony_ci if (g_baseFuzzData == nullptr || objetctSize > g_baseFuzzSize - g_baseFuzzPos) { 428f857971dSopenharmony_ci return objetct; 429f857971dSopenharmony_ci } 430f857971dSopenharmony_ci errno_t ret = memcpy_s(&objetct, objetctSize, g_baseFuzzData + g_baseFuzzPos, objetctSize); 431f857971dSopenharmony_ci if (ret != EOK) { 432f857971dSopenharmony_ci return {}; 433f857971dSopenharmony_ci } 434f857971dSopenharmony_ci g_baseFuzzPos += objetctSize; 435f857971dSopenharmony_ci return objetct; 436f857971dSopenharmony_ci} 437f857971dSopenharmony_ci 438f857971dSopenharmony_civoid SetGlobalFuzzData(const uint8_t *data, size_t size) 439f857971dSopenharmony_ci{ 440f857971dSopenharmony_ci g_baseFuzzData = data; 441f857971dSopenharmony_ci g_baseFuzzSize = size; 442f857971dSopenharmony_ci g_baseFuzzPos = 0; 443f857971dSopenharmony_ci} 444f857971dSopenharmony_ci 445f857971dSopenharmony_cistd::string GetStringFromData(int strlen) 446f857971dSopenharmony_ci{ 447f857971dSopenharmony_ci if (strlen < 1) { 448f857971dSopenharmony_ci return ""; 449f857971dSopenharmony_ci } 450f857971dSopenharmony_ci 451f857971dSopenharmony_ci char cstr[strlen]; 452f857971dSopenharmony_ci cstr[strlen - 1] = '\0'; 453f857971dSopenharmony_ci for (int i = 0; i < strlen - 1; i++) { 454f857971dSopenharmony_ci cstr[i] = GetData<char>(); 455f857971dSopenharmony_ci } 456f857971dSopenharmony_ci std::string str(cstr); 457f857971dSopenharmony_ci return str; 458f857971dSopenharmony_ci} 459f857971dSopenharmony_ci 460f857971dSopenharmony_cibool MsdpDeviceManagerFuzzTest(const uint8_t* data, size_t size) 461f857971dSopenharmony_ci{ 462f857971dSopenharmony_ci if ((data == nullptr) || (size < 1)) { 463f857971dSopenharmony_ci return false; 464f857971dSopenharmony_ci } 465f857971dSopenharmony_ci SetGlobalFuzzData(data, size); 466f857971dSopenharmony_ci 467f857971dSopenharmony_ci std::string devStr = GetStringFromData(STR_LEN); 468f857971dSopenharmony_ci int32_t id = GetData<int32_t>(); 469f857971dSopenharmony_ci 470f857971dSopenharmony_ci struct epoll_event ev {}; 471f857971dSopenharmony_ci std::weak_ptr<IDeviceObserver> weakObserver = std::weak_ptr<IDeviceObserver>(); 472f857971dSopenharmony_ci auto env = ContextService::GetInstance(); 473f857971dSopenharmony_ci 474f857971dSopenharmony_ci env->devMgr_.AddDevice(devStr); 475f857971dSopenharmony_ci env->devMgr_.FindDevice(devStr); 476f857971dSopenharmony_ci env->devMgr_.ParseDeviceId(devStr); 477f857971dSopenharmony_ci env->devMgr_.Dispatch(ev); 478f857971dSopenharmony_ci env->GetDeviceManager().GetDevice(id); 479f857971dSopenharmony_ci env->GetDeviceManager().RetriggerHotplug(weakObserver); 480f857971dSopenharmony_ci env->GetDeviceManager().AddDeviceObserver(weakObserver); 481f857971dSopenharmony_ci env->GetDeviceManager().RemoveDeviceObserver(weakObserver); 482f857971dSopenharmony_ci env->devMgr_.HasLocalPointerDevice(); 483f857971dSopenharmony_ci env->devMgr_.HasLocalKeyboardDevice(); 484f857971dSopenharmony_ci env->devMgr_.HasKeyboard(); 485f857971dSopenharmony_ci env->devMgr_.GetKeyboard(); 486f857971dSopenharmony_ci env->devMgr_.RemoveDevice(devStr); 487f857971dSopenharmony_ci return true; 488f857971dSopenharmony_ci} 489f857971dSopenharmony_ci 490f857971dSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 491f857971dSopenharmony_ci{ 492f857971dSopenharmony_ci /* Run your code on data */ 493f857971dSopenharmony_ci if (data == nullptr) { 494f857971dSopenharmony_ci return 0; 495f857971dSopenharmony_ci } 496f857971dSopenharmony_ci 497f857971dSopenharmony_ci OHOS::Msdp::DeviceStatus::MsdpDeviceManagerFuzzTest(data, size); 498f857971dSopenharmony_ci 499f857971dSopenharmony_ci return 0; 500f857971dSopenharmony_ci} 501f857971dSopenharmony_ci} // namespace DeviceStatus 502f857971dSopenharmony_ci} // namespace Msdp 503f857971dSopenharmony_ci} // namespace OHOS