From a64980fd0078dc30f4274b243c03e0c53b2d5090 Mon Sep 17 00:00:00 2001 From: zhonglufu Date: Wed, 12 Jun 2024 16:42:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=9B=B8?= =?UTF-8?q?=E6=9C=BA=E6=96=B0=E5=A2=9EFUZZ=E7=94=A8=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhonglufu --- .../callbacksinkonremoterequest_fuzzer.cpp | 24 +++- .../native_cpp/test/sourcefuzztest/BUILD.gn | 1 + .../softbusadapter_fuzzer/BUILD.gn | 77 ++++++++++ .../softbusadapter_fuzzer/corpus/init | 14 ++ .../softbusadapter_fuzzer/project.xml | 25 ++++ .../softbusadapter_fuzzer.cpp | 132 ++++++++++++++++++ .../softbusadapter_fuzzer.h | 21 +++ 7 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn create mode 100644 interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/corpus/init create mode 100644 interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/project.xml create mode 100644 interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp create mode 100644 interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.h diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonremoterequest_fuzzer/callbacksinkonremoterequest_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonremoterequest_fuzzer/callbacksinkonremoterequest_fuzzer.cpp index 759b9442..8b4f0bf2 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonremoterequest_fuzzer/callbacksinkonremoterequest_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonremoterequest_fuzzer/callbacksinkonremoterequest_fuzzer.cpp @@ -56,6 +56,28 @@ void CallbackSinkOnRemoteRequestFuzzTest(const uint8_t* data, size_t size) dcameraSinkCallback->memberFuncMap_[code] = &DCameraSinkCallbackStub::OnNotifyResourceInfoInner; dcameraSinkCallback->OnRemoteRequest(code, pdata, reply, option); } + +void CallbackSinkOnNotifyResourceInfoInnerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + MessageParcel pdata; + MessageParcel reply; + int32_t resType = static_cast(resourceEventType[data[0] % DC_RESOURCE_SIZE]); + std::string subtype(reinterpret_cast(data), size); + std::string networkId(reinterpret_cast(data), size); + bool isSensitive = data[0] % DC_RESOURCE_VALUE; + bool isSameAccout = data[0] % DC_RESOURCE_VALUE; + pdata.WriteInt32(resType); + pdata.WriteString(subtype); + pdata.WriteString(networkId); + pdata.ReadBool(isSensitive); + pdata.ReadBool(isSameAccout); + + sptr dcameraSinkCallback(new (std::nothrow) DCameraSinkCallback()); + dcameraSinkCallback->OnNotifyResourceInfoInner(pdata, reply); +} } } @@ -64,6 +86,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ OHOS::DistributedHardware::CallbackSinkOnRemoteRequestFuzzTest(data, size); + OHOS::DistributedHardware::CallbackSinkOnNotifyResourceInfoInnerFuzzTest(data, size); return 0; } - diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/BUILD.gn index 5c4a9160..9f8b5cd8 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/BUILD.gn @@ -21,6 +21,7 @@ group("fuzztest") { "decodeoninputbufferavailable_fuzzer:fuzztest", "decodeonoutputbufferavailable_fuzzer:fuzztest", "onsourcelocalcamsrvdied_fuzzer:fuzztest", + "softbusadapter_fuzzer:fuzztest", "softbusonsourcebytesreceived_fuzzer:fuzztest", "softbusonsourcemessagereceived_fuzzer:fuzztest", "softbusonsourcesessionclosed_fuzzer:fuzztest", diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn new file mode 100644 index 00000000..ce3831f3 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn @@ -0,0 +1,77 @@ +# Copyright (c) 2023-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/test.gni") +import( + "//foundation/distributedhardware/distributed_camera/distributedcamera.gni") + +##############################fuzztest########################################## +ohos_fuzztest("SoftbusAdapterFuzzTest") { + module_out_path = "${fuzz_test_output_path}/softbusadapter" + + fuzz_config_file = "${innerkits_path}/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer" + + include_dirs = [ + "${fwk_common_path}/utils/include", + "${services_path}/cameraservice/base/include", + ] + + include_dirs += [ + "${services_path}/channel/include", + "${common_path}/include/constants", + "${common_path}/include/utils", + "//foundation//communication//dsoftbus//core//common//include", + "${fwk_innerkits_path}/include", + "${feeding_smoother_path}/base", + "${feeding_smoother_path}/derived", + "${feeding_smoother_path}/utils", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + "-Dprotected=public", + ] + + sources = [ "softbusadapter_fuzzer.cpp" ] + + deps = [ + "${common_path}:distributed_camera_utils", + "${services_path}/channel:distributed_camera_channel", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"SoftbusAdapterFuzzTest\"", + "LOG_DOMAIN=0xD004150", + ] + + external_deps = [ + "c_utils:utils", + "distributed_hardware_fwk:libdhfwk_sdk", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":SoftbusAdapterFuzzTest" ] +} +############################################################################### diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/corpus/init b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/corpus/init new file mode 100644 index 00000000..e4ceac1b --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/project.xml b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/project.xml new file mode 100644 index 00000000..4fdbc407 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp new file mode 100644 index 00000000..34488dde --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2023 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 "dcamera_softbus_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +void SoftbusCreatSoftBusSinkSocketServerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int64_t))) { + return; + } + + std::string sessionName = "sourcetest02"; + std::string peerSessionName = "dh_control_0"; + std::string peerDevId = "abcd"; + DCAMERA_CHANNEL_ROLE role = DCAMERA_CHANNLE_ROLE_SOURCE; + DCameraSessionMode sessionMode = DCameraSessionMode::DCAMERA_SESSION_MODE_CTRL; + DCameraSoftbusAdapter::GetInstance().CreatSoftBusSinkSocketServer(sessionName, role, + sessionMode, peerDevId, peerSessionName); +} + +void SoftbusCreateSoftBusSourceSocketClientFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int64_t))) { + return; + } + + std::string sessionName = "sourcetest013"; + DCAMERA_CHANNEL_ROLE role = DCAMERA_CHANNLE_ROLE_SOURCE; + std::string mySessName = "sourcetest013"; + std::string peerSessName = "sinktest012"; + DCameraSessionMode sessionMode = DCameraSessionMode::DCAMERA_SESSION_MODE_CTRL; + std::string peerDevId = "bb536a637105409e904d4da83790a4a7"; + std::string myDevId = "abcde"; + DCameraSoftbusAdapter::GetInstance().CreateSoftBusSourceSocketClient(myDevId, peerSessName, peerDevId, + sessionMode, role); +} + +void SoftbusDestroySoftbusSessionServerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int64_t))) { + return; + } + + std::string sessionName = "sourcetest02"; + std::string peerSessionName = "dh_control_0"; + std::string peerDevId = "abcd"; + DCAMERA_CHANNEL_ROLE role = DCAMERA_CHANNLE_ROLE_SOURCE; + DCameraSessionMode sessionMode = DCameraSessionMode::DCAMERA_SESSION_MODE_CTRL; + DCameraSoftbusAdapter::GetInstance().CreatSoftBusSinkSocketServer(sessionName, role, + sessionMode, peerDevId, peerSessionName); + DCameraSoftbusAdapter::GetInstance().DestroySoftbusSessionServer(sessionName); +} + +void SoftbusCloseSoftbusSessionFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int64_t))) { + return; + } + + std::string sessionName = "sourcetest04"; + DCAMERA_CHANNEL_ROLE role = DCAMERA_CHANNLE_ROLE_SOURCE; + std::string mySessName = "sourcetest04"; + std::string peerSessName = "sinktest02"; + DCameraSessionMode sessionMode = DCameraSessionMode::DCAMERA_SESSION_MODE_CTRL; + std::string peerDevId = "bb536a637105409e904d4da83790a4a7"; + DCameraSoftbusAdapter::GetInstance().CreatSoftBusSinkSocketServer(mySessName, role, + sessionMode, peerDevId, peerSessName); + int32_t sessionId = 1; + DCameraSoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId); +} + +void SoftbusSendSofbusStreamFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int64_t))) { + return; + } + + std::string sessionName = "sourcetest03"; + DCAMERA_CHANNEL_ROLE role = DCAMERA_CHANNLE_ROLE_SOURCE; + std::string mySessName = "sourcetest03"; + std::string peerSessName = "sinktest02"; + DCameraSessionMode sessionMode = DCameraSessionMode::DCAMERA_SESSION_MODE_VIDEO; + std::string peerDevId = "bb536a637105409e904d4da83790a4a7"; + std::string myDevId = "abcde"; + DCameraSoftbusAdapter::GetInstance().CreateSoftBusSourceSocketClient(myDevId, peerSessName, peerDevId, + sessionMode, role); + size_t capacity = 1; + std::shared_ptr dataBuffer = std::make_shared(capacity); + int32_t sessionId = 2; + DCameraSoftbusAdapter::GetInstance().SendSofbusStream(sessionId, dataBuffer); +} + +void SoftbusGetLocalNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int64_t))) { + return; + } + + std::string devId = "bb536a637105409e904d4da83790a4a7"; + DCameraSoftbusAdapter::GetInstance().GetLocalNetworkId(devId); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::SoftbusCreatSoftBusSinkSocketServerFuzzTest(data, size); + OHOS::DistributedHardware::SoftbusCreateSoftBusSourceSocketClientFuzzTest(data, size); + OHOS::DistributedHardware::SoftbusDestroySoftbusSessionServerFuzzTest(data, size); + OHOS::DistributedHardware::SoftbusCloseSoftbusSessionFuzzTest(data, size); + OHOS::DistributedHardware::SoftbusSendSofbusStreamFuzzTest(data, size); + OHOS::DistributedHardware::SoftbusGetLocalNetworkIdFuzzTest(data, size); + return 0; +} diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.h b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.h new file mode 100644 index 00000000..412ef031 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 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 SOFTBUSADAPTER_FUZZER_H +#define SOFTBUSADAPTER_FUZZER_H + +#define FUZZ_PROJECT_NAME "softbusadapter_fuzzer" + +#endif \ No newline at end of file -- Gitee From 382d335663909d16da09b3bba643a4ae8cabdab8 Mon Sep 17 00:00:00 2001 From: zhonglufu Date: Wed, 12 Jun 2024 17:17:50 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=9B=B8?= =?UTF-8?q?=E6=9C=BA=E6=96=B0=E5=A2=9EFUZZ=E7=94=A8=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhonglufu --- .../test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn index ce3831f3..9246b751 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn @@ -15,13 +15,14 @@ import("//build/config/features.gni") import("//build/test.gni") import( - "//foundation/distributedhardware/distributed_camera/distributedcamera.gni") + "$base_root/foundation/distributedhardware/distributed_camera/distributedcamera.gni") ##############################fuzztest########################################## ohos_fuzztest("SoftbusAdapterFuzzTest") { module_out_path = "${fuzz_test_output_path}/softbusadapter" - fuzz_config_file = "${innerkits_path}/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer" + fuzz_config_file = + "${innerkits_path}/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer" include_dirs = [ "${fwk_common_path}/utils/include", -- Gitee From 142b49210d3a5ce091ebd2f054870e346888d287 Mon Sep 17 00:00:00 2001 From: zhonglufu Date: Wed, 12 Jun 2024 17:30:22 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=9B=B8?= =?UTF-8?q?=E6=9C=BA=E6=96=B0=E5=A2=9EFUZZ=E7=94=A8=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhonglufu --- .../test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn index 9246b751..5973d5db 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. #####################hydra-fuzz################### +base_root = "../../../../../../../../../" import("//build/config/features.gni") import("//build/test.gni") import( -- Gitee