!1000 add dms fuzz

Merge pull request !1000 from chenbin/master
This commit is contained in:
openharmony_ci 2024-07-23 14:39:04 +00:00 committed by Gitee
commit ac04a26d54
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 338 additions and 0 deletions

View File

@ -22,6 +22,7 @@ group("fuzztest") {
"distributedwant_fuzzer:fuzztest",
"distributedwantparams_fuzzer:fuzztest",
"dmsfreeinstallcallbackstub_fuzzer:fuzztest",
"dschedallconnectmanager_fuzzer:fuzztest",
"dschedsoftbussession_fuzzer:fuzztest",
"dschedtransportsoftbusadapter_fuzzer:fuzztest",
]

View File

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

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,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<const char*>(data), size);
uint8_t* data1 = const_cast<uint8_t*>(data);
ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets =
*(reinterpret_cast<ServiceCollaborationManager_ResourceRequestInfoSets*>(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<uint8_t*>(data);
ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets =
*(reinterpret_cast<ServiceCollaborationManager_ResourceRequestInfoSets*>(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<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));
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;
}

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_DSCHED_ALL_CONNECT_MANAGER_FUZZER_H
#define OHOS_DSCHED_ALL_CONNECT_MANAGER_FUZZER_H
#define FUZZ_PROJECT_NAME "dschedallconnectmanager_fuzzer"
#endif

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

@ -28,6 +28,11 @@ void FuzzOnBytesReceived(const uint8_t* data, size_t size)
std::shared_ptr<DSchedDataBuffer> buffer = std::make_shared<DSchedDataBuffer>(size);
DSchedSoftbusSession dschedSoftbusSession;
dschedSoftbusSession.OnBytesReceived(buffer);
dschedSoftbusSession.OnConnect();
dschedSoftbusSession.GetPeerDeviceId();
int32_t dataType = *(reinterpret_cast<const int32_t*>(data));
dschedSoftbusSession.SendData(buffer, dataType);
dschedSoftbusSession.OnDisconnect();
}
}
}

View File

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

View File

@ -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<const char*>(data), size);
int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
DSchedTransportSoftbusAdapter dschedTransportSoftbusAdapter;
dschedTransportSoftbusAdapter.ConnectDevice(peerDeviceId, sessionId);
int32_t dataType = *(reinterpret_cast<const int32_t*>(data));
std::shared_ptr<DSchedDataBuffer> dataBuffer = std::make_shared<DSchedDataBuffer>(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<const char*>(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<const int32_t*>(data));
std::shared_ptr<DSchedDataBuffer> dataBuffer = std::make_shared<DSchedDataBuffer>(size);
uint32_t dataType = *(reinterpret_cast<const uint32_t*>(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<const int32_t*>(data));
std::shared_ptr<DSchedContinueManager::SoftbusListener> listener =
std::make_shared<DSchedContinueManager::SoftbusListener>();
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<const int32_t*>(data));
std::shared_ptr<DSchedContinueManager::SoftbusListener> listener =
std::make_shared<DSchedContinueManager::SoftbusListener>();
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<const int32_t*>(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<const int32_t*>(data));
std::string peerDeviceId(reinterpret_cast<const char*>(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;
}