Add Fuzz and modify Gn

Signed-off-by: htt1997 <hutao105@huawei.com>
This commit is contained in:
htt1997 2022-11-01 14:49:19 +08:00
parent b9271e7a1b
commit 89141e8adc
15 changed files with 502 additions and 31 deletions

View File

@ -32,8 +32,10 @@ group("fuzztest") {
"test/fuzztest/autolaunch_fuzzer:fuzztest",
"test/fuzztest/blob_fuzzer:fuzztest",
"test/fuzztest/devicekvstore_fuzzer:fuzztest",
"test/fuzztest/dfx_fuzzer:fuzztest",
"test/fuzztest/distributedkvdatamanager_fuzzer:fuzztest",
"test/fuzztest/kvstoredisksize_fuzzer:fuzztest",
"test/fuzztest/kvstorescheduler_fuzzer:fuzztest",
"test/fuzztest/singlekvstore_fuzzer:fuzztest",
"test/fuzztest/typesutil_fuzzer:fuzztest",
]

View File

@ -24,14 +24,7 @@ ohos_fuzztest("DeviceKvStoreFuzzTest") {
"//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include",
"//foundation/distributeddatamgr/kv_store/frameworks/common",
"//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include",
"//developtools/profiler/device/base/include",
"//commonlibrary/c_utils/base/include",
"//third_party/sqlite/include",
"//third_party/jsoncpp/include/json",
"//third_party/skia/third_party/externals/spirv-headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/skia/third_party/externals/swiftshader/third_party/SPIRV-Headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/jsoncpp/include/json",
"//third_party/grpc/src/core/lib/json",
]
fuzz_config_file = "//foundation/distributeddatamgr/datamgr_service/test/fuzztest/devicekvstore_fuzzer"
@ -67,9 +60,6 @@ ohos_fuzztest("DeviceKvStoreFuzzTest") {
external_deps = [
"c_utils:utils",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
}

View File

@ -0,0 +1,62 @@
# 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.
#####################hydra-fuzz###################
import("//build/config/features.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("DfxFuzzTest") {
module_out_path = "datamgr_service/adapter"
include_dirs = [
"//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/dfx/src",
"//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/dfx/src/behaviour",
"//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/dfx/src/fault",
"//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/dfx/src/statistic",
"//commonlibrary/c_utils/base/include",
]
fuzz_config_file = "//foundation/distributeddatamgr/datamgr_service/test/fuzztest/dfx_fuzzer"
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [
"dfx_fuzzer.cpp",
]
deps = [
"//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter",
"//third_party/openssl:libcrypto_shared",
]
external_deps = [
"c_utils:utils",
]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":DfxFuzzTest",
]
}
###############################################################################

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,183 @@
/*
* 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.
*/
#include "dfx_fuzzer.h"
#include <any>
#include <map>
#include <string>
#include <unistd.h>
#include "reporter.h"
#include "value_hash.h"
using namespace OHOS::DistributedKv;
using namespace OHOS::DistributedDataDfx;
namespace OHOS {
static std::map<std::string, std::any> fakeCache_;
static const inline int INTERNAL = 1000;
static std::string GetString(const std::string &key)
{
usleep(INTERNAL);
if (fakeCache_.count(key)) {
return std::any_cast<std::string>(fakeCache_[key]);
}
return "";
}
static int GetInt(const std::string &key)
{
usleep(INTERNAL);
if (fakeCache_.count(key)) {
return std::any_cast<int>(fakeCache_[key]);
}
return 0;
}
static void Clear()
{
fakeCache_.clear();
}
void FaultReporterFuzz(FaultReporter *faultReporter, const std::string &strBase, const FaultMsg &faultMsg,
const CommFaultMsg &commFaultMsg, const DBFaultMsg &dbFaultMsg)
{
Clear();
faultReporter->Report(faultMsg);
GetString(strBase);
GetInt(strBase);
Clear();
faultReporter->Report(commFaultMsg);
GetString(strBase);
GetInt(strBase);
Clear();
faultReporter->Report(dbFaultMsg);
GetString(strBase);
GetInt(strBase);
Clear();
}
void DatabaseStatisticFuzz(const std::string &strBase, int intBase)
{
Clear();
auto dbs = Reporter::GetInstance()->DatabaseStatistic();
DbStat ds = { strBase, strBase, strBase, intBase };
dbs->Report(ds);
Clear();
}
void VisitStatisticFuzz(const std::string &strBase)
{
auto vs = Reporter::GetInstance()->VisitStatistic();
struct VisitStat vss = { strBase, strBase };
vs->Report(vss);
Clear();
std::string myuid = strBase;
std::string result;
ValueHash vh;
vh.CalcValueHash(myuid, result);
}
void TrafficStatisticFuzz(const std::string &strBase, int intBase)
{
auto ts = Reporter::GetInstance()->TrafficStatistic();
struct TrafficStat tss = { strBase, strBase, intBase, intBase };
ts->Report(tss);
}
void ApiPerformanceStatisticFuzz(const std::string &strBase, uint64_t uint64Base)
{
Clear();
auto ap = Reporter::GetInstance()->ApiPerformanceStatistic();
struct ApiPerformanceStat aps = { strBase, uint64Base, uint64Base, uint64Base };
ap->Report(aps);
Clear();
}
void BehaviourReporterFuzz(const std::string &strBase)
{
Clear();
auto behaviourReporter = Reporter::GetInstance()->BehaviourReporter();
struct BehaviourMsg behaviourMsg {
.userId = strBase,
.appId = strBase,
.storeId = strBase,
.extensionInfo = strBase,
.behaviourType = BehaviourType::DATABASE_BACKUP
};
;
behaviourReporter->Report(behaviourMsg);
Clear();
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
int fuzzInt = static_cast<int>(size);
int32_t fuzzInt32 = static_cast<int32_t>(size);
uint64_t fuzzUInt64 = static_cast<uint64_t>(size);
std::string fuzzStr(reinterpret_cast<const char *>(data), size);
struct FaultMsg faultMsg {
.faultType = FaultType::SERVICE_FAULT,
.moduleName = fuzzStr,
.interfaceName = fuzzStr,
.errorType = Fault::RF_CLOSE_DB
};
struct CommFaultMsg commFaultMsg {
.userId = fuzzStr,
.appId = fuzzStr,
.storeId = fuzzStr,
.errorCode = { fuzzInt32 }
};
struct DBFaultMsg dbFaultMsg {
.appId = fuzzStr,
.storeId = fuzzStr,
.moduleName = fuzzStr,
.errorType = Fault::DF_DB_DAMAGE
};
auto srvFault = Reporter::GetInstance()->ServiceFault();
OHOS::FaultReporterFuzz(srvFault, fuzzStr, faultMsg, commFaultMsg, dbFaultMsg);
auto rtFault = Reporter::GetInstance()->RuntimeFault();
OHOS::FaultReporterFuzz(rtFault, fuzzStr, faultMsg, commFaultMsg, dbFaultMsg);
auto dbFault = Reporter::GetInstance()->DatabaseFault();
OHOS::FaultReporterFuzz(dbFault, fuzzStr, faultMsg, commFaultMsg, dbFaultMsg);
auto comFault = Reporter::GetInstance()->CommunicationFault();
OHOS::FaultReporterFuzz(comFault, fuzzStr, faultMsg, commFaultMsg, dbFaultMsg);
OHOS::DatabaseStatisticFuzz(fuzzStr, fuzzInt);
OHOS::TrafficStatisticFuzz(fuzzStr, fuzzInt);
OHOS::VisitStatisticFuzz(fuzzStr);
OHOS::ApiPerformanceStatisticFuzz(fuzzStr, fuzzUInt64);
OHOS::BehaviourReporterFuzz(fuzzStr);
return 0;
}

View File

@ -0,0 +1,22 @@
/*
* 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.
*/
#ifndef DFX_FUZZER_H
#define DFX_FUZZER_H
#define FUZZ_PROJECT_NAME "Dfx_fuzzer"
#endif // DFX_FUZZER_H

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>30</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -24,14 +24,7 @@ ohos_fuzztest("DistributedKvDataManagerFuzzTest") {
"//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include",
"//foundation/distributeddatamgr/kv_store/frameworks/common",
"//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include",
"//developtools/profiler/device/base/include",
"//commonlibrary/c_utils/base/include",
"//third_party/sqlite/include",
"//third_party/jsoncpp/include/json",
"//third_party/skia/third_party/externals/spirv-headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/skia/third_party/externals/swiftshader/third_party/SPIRV-Headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/jsoncpp/include/json",
"//third_party/grpc/src/core/lib/json",
]
fuzz_config_file = "//foundation/distributeddatamgr/datamgr_service/test/fuzztest/distributedkvdatamanager_fuzzer"
@ -67,9 +60,6 @@ ohos_fuzztest("DistributedKvDataManagerFuzzTest") {
external_deps = [
"c_utils:utils",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
}

View File

@ -0,0 +1,58 @@
# 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.
#####################hydra-fuzz###################
import("//build/config/features.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("KvStoreSchedulerFuzzTest") {
module_out_path = "datamgr_service/adapter"
include_dirs = [
"//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include/autils",
"//foundation/distributeddatamgr/kv_store/frameworks/common",
]
fuzz_config_file = "//foundation/distributeddatamgr/datamgr_service/test/fuzztest/kvstorescheduler_fuzzer"
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [
"kvstorescheduler_fuzzer.cpp"
]
deps = [
"//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/autils:distributeddata_autils_static"
]
external_deps = [
"c_utils:utils"
]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":KvStoreSchedulerFuzzTest",
]
}
###############################################################################

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,69 @@
/*
* 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.
*/
#include "kvstorescheduler_fuzzer.h"
#include <chrono>
#include <thread>
#include "kv_scheduler.h"
using namespace OHOS::DistributedKv;
namespace OHOS {
} // namespace OHOS
void AtFuzz(size_t time)
{
auto sch = std::make_unique<KvScheduler>();
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now() +
std::chrono::duration<int>(time % 5);
int a = 1;
auto task = sch->At(tp, [&]() { a = 2; });
std::this_thread::sleep_for(std::chrono::seconds(5));
sch->Remove(task);
}
void EveryFUZZ(size_t time)
{
auto sch = std::make_unique<KvScheduler>();
std::chrono::duration<int> delay(time % 5);
std::chrono::duration<int> interval(time % 3);
int a = 1;
sch->Every(delay, interval, [&]() { a++; });
std::this_thread::sleep_for(std::chrono::seconds(1));
sch->Every(0, delay, interval, [&]() { a++; });
sch->Every(1, delay, interval, [&]() { a++; });
std::this_thread::sleep_for(std::chrono::seconds(1));
sch->Every(interval, [&]() { a++; });
sch->Clean();
}
void ResetFuzz(size_t time)
{
auto sch = std::make_unique<KvScheduler>();
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
std::chrono::duration<int> interval(time % 3);
SchedulerTask schedulerTask = {};
sch->Reset(schedulerTask, tp, interval);
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
AtFuzz(size);
EveryFUZZ(size);
ResetFuzz(size);
return 0;
}

View File

@ -0,0 +1,22 @@
/*
* 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.
*/
#ifndef BLOB_FUZZER_H
#define BLOB_FUZZER_H
#define FUZZ_PROJECT_NAME "Blob_fuzzer"
#endif // BLOB_FUZZER_H

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>30</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -24,14 +24,7 @@ ohos_fuzztest("SingleKvStoreFuzzTest") {
"//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include",
"//foundation/distributeddatamgr/kv_store/frameworks/common",
"//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include",
"//developtools/profiler/device/base/include",
"//commonlibrary/c_utils/base/include",
"//third_party/sqlite/include",
"//third_party/jsoncpp/include/json",
"//third_party/skia/third_party/externals/spirv-headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/skia/third_party/externals/swiftshader/third_party/SPIRV-Headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/jsoncpp/include/json",
"//third_party/grpc/src/core/lib/json",
]
fuzz_config_file = "//foundation/distributeddatamgr/datamgr_service/test/fuzztest/singlekvstore_fuzzer"
@ -67,9 +60,6 @@ ohos_fuzztest("SingleKvStoreFuzzTest") {
external_deps = [
"c_utils:utils",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
}

View File

@ -71,7 +71,8 @@ void OptionsFuzz(const std::string &strBase)
.createIfMissing = true,
.encrypt = false,
.autoSync = true,
.kvStoreType = KvStoreType::SINGLE_VERSION };
.kvStoreType = KvStoreType::SINGLE_VERSION
};
optionsIn.area = EL1;
optionsIn.baseDir = strBase;
MessageParcel parcel;