From 49f750ef50baaf30c7cbf5a6fe8d75c0ff183b14 Mon Sep 17 00:00:00 2001 From: c30051536 Date: Sat, 20 Jul 2024 15:21:47 +0800 Subject: [PATCH] add dms fuzz Signed-off-by: c30051536 --- test/fuzztest/BUILD.gn | 1 + .../dschedallconnectmanager_fuzzer/BUILD.gn | 92 +++++++++++++++ .../corpus/init | 16 +++ .../dschedallconnectmanager_fuzzer.cpp | 68 +++++++++++ .../dschedallconnectmanager_fuzzer.h | 21 ++++ .../project.xml | 25 ++++ .../dschedsoftbussession_fuzzer.cpp | 5 + .../BUILD.gn | 2 + .../dschedtransportsoftbusadapter_fuzzer.cpp | 108 ++++++++++++++++++ 9 files changed, 338 insertions(+) create mode 100644 test/fuzztest/dschedallconnectmanager_fuzzer/BUILD.gn create mode 100644 test/fuzztest/dschedallconnectmanager_fuzzer/corpus/init create mode 100644 test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp create mode 100644 test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.h create mode 100644 test/fuzztest/dschedallconnectmanager_fuzzer/project.xml diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index abd98d88..59e832ba 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -22,6 +22,7 @@ group("fuzztest") { "distributedwant_fuzzer:fuzztest", "distributedwantparams_fuzzer:fuzztest", "dmsfreeinstallcallbackstub_fuzzer:fuzztest", + "dschedallconnectmanager_fuzzer:fuzztest", "dschedsoftbussession_fuzzer:fuzztest", "dschedtransportsoftbusadapter_fuzzer:fuzztest", ] diff --git a/test/fuzztest/dschedallconnectmanager_fuzzer/BUILD.gn b/test/fuzztest/dschedallconnectmanager_fuzzer/BUILD.gn new file mode 100644 index 00000000..560455e6 --- /dev/null +++ b/test/fuzztest/dschedallconnectmanager_fuzzer/BUILD.gn @@ -0,0 +1,92 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/dmsfwk/dmsfwk.gni") +ohos_fuzztest("DSchedAllConnectManagerFuzzTest") { + module_out_path = module_output_path + + visibility = [ ":*" ] + include_dirs = [ + "${dms_path}/test/fuzztest/dschedallconnectmanager_fuzzer", + "${dms_path}/services/dtbschedmgr/include/", + "${dms_path}/interfaces/innerkits/common/include/", + "${dms_path}/services/dtbschedmgr/include/collaborate/", + "${dms_path}/common/include/", + "${dms_path}/services/dtbschedmgr/include/softbus_adapter/allconnectmgr", + ] + + fuzz_config_file = "${dms_path}/test/fuzztest/dschedallconnectmanager_fuzzer" + + configs = [ "${dms_path}/services/dtbschedmgr/test/resource:coverage_flags" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + ] + + sources = [ "dschedallconnectmanager_fuzzer.cpp" ] + + deps = [ "${dms_path}/services/dtbschedmgr:distributedschedsvr" ] + + defines = [] + if (dmsfwk_mission_manager) { + defines += [ "SUPPORT_DISTRIBUTED_MISSION_MANAGER" ] + } + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_manager", + "ability_runtime:dataobs_manager", + "ability_runtime:mission_info", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "cJSON:cjson", + "c_utils:utils", + "data_share:datashare_consumer", + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "device_manager:devicemanagersdk", + "device_security_level:dslm_sdk", + "distributed_bundle_framework:dbms_fwk", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", + "init:libbegetutil", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":DSchedAllConnectManagerFuzzTest" ] +} diff --git a/test/fuzztest/dschedallconnectmanager_fuzzer/corpus/init b/test/fuzztest/dschedallconnectmanager_fuzzer/corpus/init new file mode 100644 index 00000000..8eb5a7d6 --- /dev/null +++ b/test/fuzztest/dschedallconnectmanager_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp b/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp new file mode 100644 index 00000000..e762ff4d --- /dev/null +++ b/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dschedallconnectmanager_fuzzer.h" + +#include "dsched_all_connect_manager.h" +#include "service_collaboration_manager_capi.h" + +namespace OHOS { +namespace DistributedSchedule { +void FuzzApplyAdvanceResource(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + const std::string peerNetworkId(reinterpret_cast(data), size); + uint8_t* data1 = const_cast(data); + ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets = + *(reinterpret_cast(data1)); + DSchedAllConnectManager::GetInstance().ApplyAdvanceResource(peerNetworkId, reqInfoSets); +} + +void FuzzGetResourceRequest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + uint8_t* data2 = const_cast(data); + ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets = + *(reinterpret_cast(data2)); + DSchedAllConnectManager::GetInstance().GetResourceRequest(reqInfoSets); +} + +void FuzzPublishServiceState(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + const std::string peerNetworkId(reinterpret_cast(data), size); + const std::string extraInfo(reinterpret_cast(data), size); + uint8_t* temp = const_cast(data); + ServiceCollaborationManagerBussinessStatus state = + *(reinterpret_cast(temp)); + DSchedAllConnectManager::GetInstance().PublishServiceState(peerNetworkId, extraInfo, state); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + OHOS::DistributedSchedule::FuzzApplyAdvanceResource(data, size); + OHOS::DistributedSchedule::FuzzGetResourceRequest(data, size); + OHOS::DistributedSchedule::FuzzPublishServiceState(data, size); + return 0; +} diff --git a/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.h b/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.h new file mode 100644 index 00000000..baa9f4e9 --- /dev/null +++ b/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCHED_ALL_CONNECT_MANAGER_FUZZER_H +#define OHOS_DSCHED_ALL_CONNECT_MANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "dschedallconnectmanager_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/dschedallconnectmanager_fuzzer/project.xml b/test/fuzztest/dschedallconnectmanager_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/test/fuzztest/dschedallconnectmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp b/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp index 5340f7ba..65dd5bd3 100644 --- a/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp +++ b/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp @@ -28,6 +28,11 @@ void FuzzOnBytesReceived(const uint8_t* data, size_t size) std::shared_ptr buffer = std::make_shared(size); DSchedSoftbusSession dschedSoftbusSession; dschedSoftbusSession.OnBytesReceived(buffer); + dschedSoftbusSession.OnConnect(); + dschedSoftbusSession.GetPeerDeviceId(); + int32_t dataType = *(reinterpret_cast(data)); + dschedSoftbusSession.SendData(buffer, dataType); + dschedSoftbusSession.OnDisconnect(); } } } diff --git a/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/BUILD.gn b/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/BUILD.gn index fb5f9845..bb3e34b8 100644 --- a/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/BUILD.gn +++ b/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/BUILD.gn @@ -27,6 +27,8 @@ ohos_fuzztest("DSchedTransportSoftbusAdapterFuzzTest") { "${dms_path}/services/dtbschedmgr/include/collaborate/", "${dms_path}/common/include/", "${dms_path}/services/dtbschedmgr/include/softbus_adapter/transport", + "${dms_path}/services/dtbschedmgr/include/continue", + "${dms_path}/services/dtbschedmgr/include/continue/state", ] fuzz_config_file = diff --git a/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/dschedtransportsoftbusadapter_fuzzer.cpp b/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/dschedtransportsoftbusadapter_fuzzer.cpp index f17a0eec..cc63439a 100644 --- a/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/dschedtransportsoftbusadapter_fuzzer.cpp +++ b/test/fuzztest/dschedtransportsoftbusadapter_fuzzer/dschedtransportsoftbusadapter_fuzzer.cpp @@ -15,10 +15,17 @@ #include "dschedtransportsoftbusadapter_fuzzer.h" +#include "dsched_continue_manager.h" +#include "dsched_data_buffer.h" #include "dsched_transport_softbus_adapter.h" +#include "idata_listener.h" namespace OHOS { namespace DistributedSchedule { +namespace { +constexpr uint32_t MAX_BUFFER_SIZE = 80 * 1024 * 1024; +} + void FuzzOnBind(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < sizeof(size_t))) { @@ -52,6 +59,100 @@ void FuzzOnBytes(const uint8_t* data, size_t size) DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; dschedTransportSoftbusAdapter.OnBytes(sessionId, newdata, dataLen); } + +void FuzzConnectDevice(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t)) || size >= MAX_BUFFER_SIZE) { + return; + } + std::string peerDeviceId(reinterpret_cast(data), size); + int32_t sessionId = *(reinterpret_cast(data)); + + DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; + dschedTransportSoftbusAdapter.ConnectDevice(peerDeviceId, sessionId); + int32_t dataType = *(reinterpret_cast(data)); + std::shared_ptr dataBuffer = std::make_shared(size); + dschedTransportSoftbusAdapter.SendData(sessionId, dataType, dataBuffer); + dschedTransportSoftbusAdapter.SendBytesBySoftbus(sessionId, dataBuffer); + dschedTransportSoftbusAdapter.InitChannel(); + dschedTransportSoftbusAdapter.CreateServerSocket(); + dschedTransportSoftbusAdapter.CreateClientSocket(peerDeviceId); + bool isServer = sessionId % 2; + dschedTransportSoftbusAdapter.CreateSessionRecord(sessionId, peerDeviceId, isServer); + dschedTransportSoftbusAdapter.AddNewPeerSession(peerDeviceId, sessionId); + dschedTransportSoftbusAdapter.ShutdownSession(peerDeviceId, sessionId); + bool isSelfCalled = sessionId % 2; + dschedTransportSoftbusAdapter.NotifyListenersSessionShutdown(sessionId, isSelfCalled); + dschedTransportSoftbusAdapter.ReleaseChannel(); +} + +void FuzzDisconnectDevice(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + std::string peerDeviceId(reinterpret_cast(data), size); + DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; + dschedTransportSoftbusAdapter.DisconnectDevice(peerDeviceId); +} + + +void FuzzOnDataReady(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t)) || size >= MAX_BUFFER_SIZE) { + return; + } + int32_t sessionId = *(reinterpret_cast(data)); + std::shared_ptr dataBuffer = std::make_shared(size); + uint32_t dataType = *(reinterpret_cast(data)); + DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; + dschedTransportSoftbusAdapter.OnDataReady(sessionId, dataBuffer, dataType); +} + +void FuzzRegisterListener(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + int32_t serviceType = *(reinterpret_cast(data)); + std::shared_ptr listener = + std::make_shared(); + DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; + dschedTransportSoftbusAdapter.RegisterListener(serviceType, listener); +} + +void FuzzUnregisterListener(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + int32_t serviceType = *(reinterpret_cast(data)); + std::shared_ptr listener = + std::make_shared(); + DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; + dschedTransportSoftbusAdapter.UnregisterListener(serviceType, listener); +} + +void FuzzSetCallingTokenId(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + int32_t callingTokenId = *(reinterpret_cast(data)); + DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; + dschedTransportSoftbusAdapter.SetCallingTokenId(callingTokenId); +} + +void FuzzGetSessionIdByDeviceId(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(size_t))) { + return; + } + int32_t sessionId = *(reinterpret_cast(data)); + std::string peerDeviceId(reinterpret_cast(data), size); + DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter; + dschedTransportSoftbusAdapter.GetSessionIdByDeviceId(peerDeviceId, sessionId); +} } } @@ -61,5 +162,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedSchedule::FuzzOnBind(data, size); OHOS::DistributedSchedule::FuzzOnShutdown(data, size); OHOS::DistributedSchedule::FuzzOnBytes(data, size); + OHOS::DistributedSchedule::FuzzConnectDevice(data, size); + OHOS::DistributedSchedule::FuzzDisconnectDevice(data, size); + OHOS::DistributedSchedule::FuzzOnDataReady(data, size); + OHOS::DistributedSchedule::FuzzRegisterListener(data, size); + OHOS::DistributedSchedule::FuzzUnregisterListener(data, size); + OHOS::DistributedSchedule::FuzzSetCallingTokenId(data, size); + OHOS::DistributedSchedule::FuzzGetSessionIdByDeviceId(data, size); return 0; }