add adapter fuzz

Signed-off-by: c30051536 <chenbin179@huawei.com>
This commit is contained in:
c30051536 2024-07-27 15:09:37 +08:00
parent b8e37c1073
commit 2e7530a738
8 changed files with 229 additions and 11 deletions

View File

@ -25,5 +25,6 @@ group("fuzztest") {
"dschedallconnectmanager_fuzzer:fuzztest",
"dschedsoftbussession_fuzzer:fuzztest",
"dschedtransportsoftbusadapter_fuzzer:fuzztest",
"softbusadapter_fuzzer:fuzztest",
]
}

View File

@ -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<const char*>(data), size);
uint8_t* data1 = const_cast<uint8_t*>(data);
ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets =
*(reinterpret_cast<ServiceCollaborationManager_ResourceRequestInfoSets*>(data1));
ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets;
reqInfoSets.remoteHardwareListSize = *(reinterpret_cast<const uint32_t*>(data));
reqInfoSets.localHardwareListSize = *(reinterpret_cast<const uint32_t*>(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<uint8_t*>(data);
ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets =
*(reinterpret_cast<ServiceCollaborationManager_ResourceRequestInfoSets*>(data2));
ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets;
reqInfoSets.remoteHardwareListSize = *(reinterpret_cast<const uint32_t*>(data));
reqInfoSets.localHardwareListSize = *(reinterpret_cast<const uint32_t*>(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<const char*>(data), size);
const std::string extraInfo(reinterpret_cast<const char*>(data), size);
uint8_t* temp = const_cast<uint8_t*>(data);
ServiceCollaborationManagerBussinessStatus state =
*(reinterpret_cast<ServiceCollaborationManagerBussinessStatus*>(temp));
*(reinterpret_cast<const ServiceCollaborationManagerBussinessStatus*>(data));
DSchedAllConnectManager::GetInstance().PublishServiceState(peerNetworkId, extraInfo, state);
}
}

View File

@ -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<DSchedDataBuffer> buffer = std::make_shared<DSchedDataBuffer>(size);
DSchedSoftbusSession dschedSoftbusSession;
dschedSoftbusSession.OnBytesReceived(buffer);

View File

@ -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" ]
}

View File

@ -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

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -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 <cstddef>
#include <cstdint>
#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<uint8_t*>(data);
uint32_t sendDataLen = *(reinterpret_cast<const uint32_t*>(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<const char*>(data), size);
uint8_t* newdata = const_cast<uint8_t*>(data);
uint32_t dataLen = *(reinterpret_cast<const uint32_t*>(data));
SoftbusAdapter::GetInstance().OnBroadCastRecv(networkId, newdata, dataLen);
std::shared_ptr<SoftbusAdapterListener> listener = std::make_shared<DistributedMissionBroadcastListener>();
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;
}

View File

@ -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