diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 59e832ba..fd49f214 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -25,5 +25,6 @@ group("fuzztest") { "dschedallconnectmanager_fuzzer:fuzztest", "dschedsoftbussession_fuzzer:fuzztest", "dschedtransportsoftbusadapter_fuzzer:fuzztest", + "softbusadapter_fuzzer:fuzztest", ] } diff --git a/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp b/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp index e762ff4d..a1548b33 100644 --- a/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp +++ b/test/fuzztest/dschedallconnectmanager_fuzzer/dschedallconnectmanager_fuzzer.cpp @@ -22,37 +22,36 @@ namespace OHOS { namespace DistributedSchedule { void FuzzApplyAdvanceResource(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < sizeof(size_t))) { + if ((data == nullptr) || (size < sizeof(uint32_t))) { return; } const std::string peerNetworkId(reinterpret_cast(data), size); - uint8_t* data1 = const_cast(data); - ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets = - *(reinterpret_cast(data1)); + ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets; + reqInfoSets.remoteHardwareListSize = *(reinterpret_cast(data)); + reqInfoSets.localHardwareListSize = *(reinterpret_cast(data)); DSchedAllConnectManager::GetInstance().ApplyAdvanceResource(peerNetworkId, reqInfoSets); } void FuzzGetResourceRequest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < sizeof(size_t))) { + if ((data == nullptr) || (size < sizeof(uint32_t))) { return; } - uint8_t* data2 = const_cast(data); - ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets = - *(reinterpret_cast(data2)); + ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets; + reqInfoSets.remoteHardwareListSize = *(reinterpret_cast(data)); + reqInfoSets.localHardwareListSize = *(reinterpret_cast(data)); DSchedAllConnectManager::GetInstance().GetResourceRequest(reqInfoSets); } void FuzzPublishServiceState(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < sizeof(size_t))) { + if ((data == nullptr) || (size < sizeof(uint32_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)); + *(reinterpret_cast(data)); DSchedAllConnectManager::GetInstance().PublishServiceState(peerNetworkId, extraInfo, state); } } diff --git a/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp b/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp index 65dd5bd3..3267a694 100644 --- a/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp +++ b/test/fuzztest/dschedsoftbussession_fuzzer/dschedsoftbussession_fuzzer.cpp @@ -25,6 +25,9 @@ namespace OHOS { namespace DistributedSchedule { void FuzzOnBytesReceived(const uint8_t* data, size_t size) { + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } std::shared_ptr buffer = std::make_shared(size); DSchedSoftbusSession dschedSoftbusSession; dschedSoftbusSession.OnBytesReceived(buffer); diff --git a/test/fuzztest/softbusadapter_fuzzer/BUILD.gn b/test/fuzztest/softbusadapter_fuzzer/BUILD.gn new file mode 100644 index 00000000..e3e68a81 --- /dev/null +++ b/test/fuzztest/softbusadapter_fuzzer/BUILD.gn @@ -0,0 +1,94 @@ +# 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("SoftbusAdapterFuzzTest") { + module_out_path = module_output_path + + visibility = [ ":*" ] + include_dirs = [ + "${dms_path}/common/include/", + "${dms_path}/interfaces/innerkits/common/include/", + "${dms_path}/services/dtbschedmgr/include/", + "${dms_path}/services/dtbschedmgr/include/collaborate/", + "${dms_path}/services/dtbschedmgr/include/mission", + "${dms_path}/services/dtbschedmgr/include/softbus_adapter", + "${dms_path}/services/dtbschedmgr/include/softbus_adapter/transport", + "${dms_path}/test/fuzztest/softbusadapter_fuzzer", + ] + + fuzz_config_file = "${dms_path}/test/fuzztest/softbusadapter_fuzzer" + + configs = [ "${dms_path}/services/dtbschedmgr/test/resource:coverage_flags" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + ] + + sources = [ "softbusadapter_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 = [ ":SoftbusAdapterFuzzTest" ] +} diff --git a/test/fuzztest/softbusadapter_fuzzer/corpus/init b/test/fuzztest/softbusadapter_fuzzer/corpus/init new file mode 100644 index 00000000..8eb5a7d6 --- /dev/null +++ b/test/fuzztest/softbusadapter_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/softbusadapter_fuzzer/project.xml b/test/fuzztest/softbusadapter_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/test/fuzztest/softbusadapter_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp b/test/fuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp new file mode 100644 index 00000000..9fff54a0 --- /dev/null +++ b/test/fuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp @@ -0,0 +1,59 @@ +/* + * 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 "softbusadapter_fuzzer.h" + +#include +#include + +#include "distributed_mission_broadcast_listener.h" +#include "softbus_adapter/softbus_adapter.h" + +namespace OHOS { +namespace DistributedSchedule { +void FuzzSendSoftbusEvent(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(uint32_t))) { + return; + } + uint8_t* sendData = const_cast(data); + uint32_t sendDataLen = *(reinterpret_cast(data)); + SoftbusAdapter::GetInstance().SendSoftbusEvent(sendData, sendDataLen); + SoftbusAdapter::GetInstance().StopSoftbusEvent(); +} + +void FuzzOnBroadCastRecv(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(uint32_t))) { + return; + } + std::string networkId(reinterpret_cast(data), size); + uint8_t* newdata = const_cast(data); + uint32_t dataLen = *(reinterpret_cast(data)); + SoftbusAdapter::GetInstance().OnBroadCastRecv(networkId, newdata, dataLen); + std::shared_ptr listener = std::make_shared(); + SoftbusAdapter::GetInstance().RegisterSoftbusEventListener(listener); + SoftbusAdapter::GetInstance().UnregisterSoftbusEventListener(listener); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + OHOS::DistributedSchedule::FuzzSendSoftbusEvent(data, size); + OHOS::DistributedSchedule::FuzzOnBroadCastRecv(data, size); + return 0; +} diff --git a/test/fuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.h b/test/fuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.h new file mode 100644 index 00000000..3e54b318 --- /dev/null +++ b/test/fuzztest/softbusadapter_fuzzer/softbusadapter_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_SOFTBUSS_ADAPTER_FUZZER_H +#define OHOS_SOFTBUSS_ADAPTER_FUZZER_H + +#define FUZZ_PROJECT_NAME "softbusadapter_fuzzer" + +#endif \ No newline at end of file