1686862fbSopenharmony_ci/* 2686862fbSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4686862fbSopenharmony_ci * you may not use this file except in compliance with the License. 5686862fbSopenharmony_ci * You may obtain a copy of the License at 6686862fbSopenharmony_ci * 7686862fbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8686862fbSopenharmony_ci * 9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12686862fbSopenharmony_ci * See the License for the specific language governing permissions and 13686862fbSopenharmony_ci * limitations under the License. 14686862fbSopenharmony_ci */ 15686862fbSopenharmony_ci 16686862fbSopenharmony_ci#include "dschedsoftbussession_fuzzer.h" 17686862fbSopenharmony_ci 18686862fbSopenharmony_ci#include <cstddef> 19686862fbSopenharmony_ci#include <cstdint> 20686862fbSopenharmony_ci#include <securec.h> 21686862fbSopenharmony_ci 22686862fbSopenharmony_ci#include "dsched_data_buffer.h" 23686862fbSopenharmony_ci#include "dsched_softbus_session.h" 24686862fbSopenharmony_ci 25686862fbSopenharmony_cinamespace OHOS { 26686862fbSopenharmony_cinamespace DistributedSchedule { 27686862fbSopenharmony_cinamespace { 28686862fbSopenharmony_cistatic const uint32_t DSCHED_MAX_BUFFER_SIZE = 80 * 1024 * 1024; 29686862fbSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 30686862fbSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 31686862fbSopenharmony_ciconstexpr int32_t POS_0 = 0; 32686862fbSopenharmony_ciconstexpr int32_t POS_1 = 1; 33686862fbSopenharmony_ciconstexpr int32_t POS_2 = 2; 34686862fbSopenharmony_ciconstexpr int32_t POS_3 = 3; 35686862fbSopenharmony_ciconstexpr int32_t OFFSET_24 = 24; 36686862fbSopenharmony_ciconstexpr int32_t OFFSET_16 = 16; 37686862fbSopenharmony_ciconstexpr int32_t OFFSET_8 = 8; 38686862fbSopenharmony_ci} 39686862fbSopenharmony_ci 40686862fbSopenharmony_ciint32_t Get32Data(const uint8_t* ptr, size_t size) 41686862fbSopenharmony_ci{ 42686862fbSopenharmony_ci if (size > FOO_MAX_LEN || size < U32_AT_SIZE) { 43686862fbSopenharmony_ci return 0; 44686862fbSopenharmony_ci } 45686862fbSopenharmony_ci char *ch = static_cast<char*>(malloc(size + 1)); 46686862fbSopenharmony_ci if (ch == nullptr) { 47686862fbSopenharmony_ci return 0; 48686862fbSopenharmony_ci } 49686862fbSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 50686862fbSopenharmony_ci if (memcpy_s(ch, size + 1, ptr, size) != EOK) { 51686862fbSopenharmony_ci free(ch); 52686862fbSopenharmony_ci ch = nullptr; 53686862fbSopenharmony_ci return 0; 54686862fbSopenharmony_ci } 55686862fbSopenharmony_ci int32_t data = (ch[POS_0] << OFFSET_24) | (ch[POS_1] << OFFSET_16) | (ch[POS_2] << OFFSET_8) | ch[POS_3]; 56686862fbSopenharmony_ci free(ch); 57686862fbSopenharmony_ci ch = nullptr; 58686862fbSopenharmony_ci return data; 59686862fbSopenharmony_ci} 60686862fbSopenharmony_ci 61686862fbSopenharmony_civoid FuzzOnBytesReceived(const uint8_t* data, size_t size) 62686862fbSopenharmony_ci{ 63686862fbSopenharmony_ci if ((data == nullptr) || (size < U32_AT_SIZE)) { 64686862fbSopenharmony_ci return; 65686862fbSopenharmony_ci } 66686862fbSopenharmony_ci size_t intParam = static_cast<size_t>(Get32Data(data, size)); 67686862fbSopenharmony_ci if (intParam >= DSCHED_MAX_BUFFER_SIZE) { 68686862fbSopenharmony_ci return; 69686862fbSopenharmony_ci } 70686862fbSopenharmony_ci std::shared_ptr<DSchedDataBuffer> buffer = std::make_shared<DSchedDataBuffer>(intParam); 71686862fbSopenharmony_ci DSchedSoftbusSession dschedSoftbusSession; 72686862fbSopenharmony_ci dschedSoftbusSession.OnBytesReceived(buffer); 73686862fbSopenharmony_ci dschedSoftbusSession.OnConnect(); 74686862fbSopenharmony_ci dschedSoftbusSession.GetPeerDeviceId(); 75686862fbSopenharmony_ci int32_t dataType = Get32Data(data, size); 76686862fbSopenharmony_ci dschedSoftbusSession.SendData(buffer, dataType); 77686862fbSopenharmony_ci dschedSoftbusSession.OnDisconnect(); 78686862fbSopenharmony_ci} 79686862fbSopenharmony_ci} 80686862fbSopenharmony_ci} 81686862fbSopenharmony_ci 82686862fbSopenharmony_ci/* Fuzzer entry point */ 83686862fbSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 84686862fbSopenharmony_ci{ 85686862fbSopenharmony_ci OHOS::DistributedSchedule::FuzzOnBytesReceived(data, size); 86686862fbSopenharmony_ci return 0; 87686862fbSopenharmony_ci} 88