DFX: add fuzz for distributed want

Signed-off-by: z00838083 <zhuhuixuan@huawei.com>
This commit is contained in:
z00838083 2024-07-19 17:31:20 +08:00
parent c7bc7393b0
commit 563e1e7e8a
11 changed files with 599 additions and 0 deletions

View File

@ -17,5 +17,7 @@ group("fuzztest") {
deps = [
"continuationmanager_fuzzer:fuzztest",
"distributedschedstub_fuzzer:fuzztest",
"distributedwant_fuzzer:fuzztest",
"distributedwantparams_fuzzer:fuzztest",
]
}

View File

@ -0,0 +1,49 @@
# 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/test.gni")
import("//foundation/ability/dmsfwk/dmsfwk.gni")
ohos_fuzztest("DistributedWantFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "${dms_path}/test/fuzztest/distributedwant_fuzzer"
include_dirs = [
"${dms_path}/services/dtbschedmgr/include",
"${dms_path}/services/dtbschedmgr/include/distributedWant",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
"-Dprivate=public",
]
sources = [ "distributedwant_fuzzer.cpp" ]
configs = [ "${dms_path}/services/dtbschedmgr/test/resource:coverage_flags" ]
deps = [ "${dms_path}/services/dtbschedmgr:distributedschedsvr" ]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"c_utils:utils",
]
}
group("fuzztest") {
testonly = true
deps = [ ":DistributedWantFuzzTest" ]
}

View File

@ -0,0 +1,16 @@
/*
* 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.
*/
FUZZ

View File

@ -0,0 +1,218 @@
/*
* 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 "distributedwant_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include <iostream>
#include "distributed_want.h"
#include "securec.h"
using namespace OHOS::AAFwk;
using namespace OHOS::DistributedSchedule;
namespace OHOS {
namespace {
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
constexpr int32_t POS_0 = 0;
constexpr int32_t POS_1 = 1;
constexpr int32_t POS_2 = 2;
constexpr int32_t POS_3 = 3;
constexpr int32_t OFFSET_24 = 24;
constexpr int32_t OFFSET_16 = 16;
constexpr int32_t OFFSET_8 = 8;
}
uint32_t GetU32Data(const char* ptr)
{
// convert fuzz input data to an integer
return (ptr[POS_0] << OFFSET_24) | (ptr[POS_1] << OFFSET_16) | (ptr[POS_2] << OFFSET_8) | ptr[POS_3];
}
bool DoSomethingInterestingWithMyAPI_DistributedWant_001(const char* data, size_t size)
{
std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
unsigned int flags = static_cast<unsigned int>(GetU32Data(data));
want->SetFlags(flags);
want->RemoveFlags(flags);
std::string entity(data, size);
want->AddEntity(entity);
want->HasEntity(entity);
want->RemoveEntity(entity);
std::string bundleName(data, size);
want->SetBundle(bundleName);
std::string deviceId(data, size);
want->SetDeviceId(deviceId);
return true;
}
bool DoSomethingInterestingWithMyAPI_DistributedWant_002(const char* data, size_t size)
{
std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
std::string type(data, size);
want->SetType(type);
want->FormatType(type);
Uri uri(type);
want->SetUri(uri);
want->SetUriAndType(uri, type);
want->FormatUri(uri);
want->FormatUri(type);
char* charUri;
want->WantParseUri(charUri);
want->GetLowerCaseScheme(uri);
want->FormatUriAndType(uri, type);
want->FormatMimeType(type);
return true;
}
bool DoSomethingInterestingWithMyAPI_DistributedWant_003(const char* data, size_t size)
{
std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
want->CountEntities();
DistributedWant *wantptr = nullptr;
want->GetScheme();
AppExecFwk::ElementName elementName;
wantptr = want->MakeMainAbility(elementName);
if (wantptr) {
delete wantptr;
wantptr = nullptr;
}
DistributedOperation operation;
want->SetOperation(operation);
wantptr = want->CloneOperation();
if (wantptr) {
delete wantptr;
wantptr = nullptr;
}
std::string key(data, size);
want->HasParameter(key);
std::string content(data, size);
std::string prop(data, size);
std::string value(data, size);
want->ParseContent(content, prop, value);
std::string str(data, size);
want->Decode(str);
want->Encode(str);
nlohmann::json wantJson;
want->ReadFromJson(wantJson);
return true;
}
bool DoSomethingInterestingWithMyAPI_DistributedWant_004(const char* data, size_t size)
{
std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
std::string key(data, size);
sptr<IRemoteObject> remoteObject;
want->SetParam(key, remoteObject);
want->GetRemoteObject(key);
std::vector<bool> boolValue;
want->SetParam(key, boolValue);
want->GetBoolArrayParam(key);
byte byteValue = '\0';
want->SetParam(key, byteValue);
want->GetByteParam(key, byteValue);
std::vector<byte> byteVector;
want->SetParam(key, byteVector);
want->GetByteArrayParam(key);
zchar charValue = U'\0';
want->SetParam(key, charValue);
want->GetCharParam(key, charValue);
return true;
}
bool DoSomethingInterestingWithMyAPI_DistributedWant_005(const char* data, size_t size)
{
std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
std::string key(data, size);
std::vector<zchar> charVector;
want->SetParam(key, charVector);
want->GetCharArrayParam(key);
std::vector<int> intVector;
want->SetParam(key, intVector);
want->GetIntArrayParam(key);
double doubleValue = 0.0;
want->SetParam(key, doubleValue);
want->GetDoubleParam(key, doubleValue);
std::vector<double> doubleVector;
want->SetParam(key, doubleVector);
want->GetDoubleArrayParam(key);
float floatValue = 0.0;
want->SetParam(key, floatValue);
want->GetFloatParam(key, floatValue);
return true;
}
bool DoSomethingInterestingWithMyAPI_DistributedWant_006(const char* data, size_t size)
{
std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
std::string key(data, size);
std::vector<float> floatVector;
want->SetParam(key, floatVector);
want->GetFloatArrayParam(key);
std::vector<long> longVector;
want->SetParam(key, longVector);
want->GetLongArrayParam(key);
short shortValue = 0;
want->SetParam(key, shortValue);
want->GetShortParam(key, shortValue);
std::vector<short> shortVector;
want->SetParam(key, shortVector);
want->GetShortArrayParam(key);
std::vector<std::string> stringVector;
want->SetParam(key, stringVector);
return true;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
/* Run your code on data */
if (data == nullptr) {
std::cout << "invalid data" << std::endl;
return 0;
}
/* Validate the length of size */
if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
return 0;
}
char* ch = reinterpret_cast<char *>(malloc(size + 1));
if (ch == nullptr) {
std::cout << "malloc failed." << std::endl;
return 0;
}
(void)memset_s(ch, size + 1, 0x00, size + 1);
if (memcpy_s(ch, size + 1, data, size) != EOK) {
std::cout << "copy failed." << std::endl;
free(ch);
ch = nullptr;
return 0;
}
OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_001(ch, size);
OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_002(ch, size);
OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_003(ch, size);
OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_004(ch, size);
OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_005(ch, size);
OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_006(ch, size);
free(ch);
ch = nullptr;
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_DISTRIBUTEDWANT_FUZZER_H
#define OHOS_DISTRIBUTEDWANT_FUZZER_H
#define FUZZ_PROJECT_NAME "distributedwant_fuzzer"
#endif // OHOS_DISTRIBUTEDWANT_FUZZER_H

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<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,49 @@
# 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/test.gni")
import("//foundation/ability/dmsfwk/dmsfwk.gni")
ohos_fuzztest("DistributedWantParamsFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "${dms_path}/test/fuzztest/distributedwantparams_fuzzer"
include_dirs = [
"${dms_path}/services/dtbschedmgr/include",
"${dms_path}/services/dtbschedmgr/include/distributedWant",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
"-Dprivate=public",
]
sources = [ "distributedwantparams_fuzzer.cpp" ]
configs = [ "${dms_path}/services/dtbschedmgr/test/resource:coverage_flags" ]
deps = [ "${dms_path}/services/dtbschedmgr:distributedschedsvr" ]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"c_utils:utils",
]
}
group("fuzztest") {
testonly = true
deps = [ ":DistributedWantParamsFuzzTest" ]
}

View File

@ -0,0 +1,16 @@
/*
* 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.
*/
FUZZ

View File

@ -0,0 +1,157 @@
/*
* 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 "distributedwantparams_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include <iostream>
#include "byte_wrapper.h"
#include "distributed_want_params.h"
#include "distributed_want_params_wrapper.h"
#include "double_wrapper.h"
#include "float_wrapper.h"
#include "securec.h"
#include "short_wrapper.h"
#include "string_wrapper.h"
#include "zchar_wrapper.h"
using namespace OHOS::AAFwk;
using namespace OHOS::DistributedSchedule;
namespace OHOS {
namespace {
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
constexpr int32_t POS_0 = 0;
constexpr int32_t POS_1 = 1;
constexpr int32_t POS_2 = 2;
constexpr int32_t POS_3 = 3;
constexpr int32_t OFFSET_24 = 24;
constexpr int32_t OFFSET_16 = 16;
constexpr int32_t OFFSET_8 = 8;
}
uint32_t GetU32Data(const char* ptr)
{
// convert fuzz input data to an integer
return (ptr[POS_0] << OFFSET_24) | (ptr[POS_1] << OFFSET_16) | (ptr[POS_2] << OFFSET_8) | ptr[POS_3];
}
bool DoSomethingInterestingWithMyAPI_DistributedWantParams_001(const char* data, size_t size)
{
UnsupportedData other;
std::shared_ptr<UnsupportedData> unsupportedData = std::make_shared<UnsupportedData>(other);
DistributedWantParams wantOther;
std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
std::string key(data, size);
int8_t byteValue = static_cast<int8_t>(GetU32Data(data));
sptr<IInterface> stringIt = String::Box(key);
wantParams->SetParam(key, stringIt);
wantParams->KeySet();
wantParams->HasParam(key);
wantParams->IsEmpty();
Parcel parcel;
sptr<IInterface> byteIt = Byte::Box(byteValue);
wantParams->WriteToParcelByte(parcel, byteIt);
return true;
}
bool DoSomethingInterestingWithMyAPI_DistributedWantParams_002(const char* data, size_t size)
{
DistributedWantParams wantOther;
std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
Parcel parcel;
IArray* ao = nullptr;
wantParams->WriteArrayToParcelChar(parcel, ao);
wantParams->WriteArrayToParcelShort(parcel, ao);
wantParams->WriteToParcelFD(parcel, wantOther);
wantParams->WriteToParcelRemoteObject(parcel, wantOther);
float floatValue = 0.0;
sptr<IInterface> floatIt = Float::Box(floatValue);
wantParams->WriteToParcelFloat(parcel, floatIt);
sptr<IArray> array;
wantParams->ReadFromParcelArrayChar(parcel, array);
wantParams->ReadFromParcelArrayShort(parcel, array);
std::string key(data, size);
int type = static_cast<int>(GetU32Data(data));
wantParams->ReadFromParcelWantParamWrapper(parcel, key, type);
wantParams->ReadFromParcelFD(parcel, key);
wantParams->ReadFromParcelRemoteObject(parcel, key);
wantParams->ReadFromParcelFloat(parcel, key);
return true;
}
bool DoSomethingInterestingWithMyAPI_DistributedWantParams_003(const char* data, size_t size)
{
DistributedWantParams wantOther;
std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
Parcel parcel;
char charValue = *data;
sptr<IInterface> charIt = Char::Box(charValue);
wantParams->WriteToParcelChar(parcel, charIt);
short shortValue = 0;
sptr<IInterface> shortIt = Short::Box(shortValue);
wantParams->WriteToParcelShort(parcel, shortIt);
double doubleValue = 0.0;
sptr<IInterface> doubleIt = Double::Box(doubleValue);
wantParams->WriteToParcelDouble(parcel, doubleIt);
IArray* ao = nullptr;
wantParams->WriteArrayToParcelString(parcel, ao);
wantParams->WriteArrayToParcelBool(parcel, ao);
wantParams->WriteArrayToParcelByte(parcel, ao);
wantParams->WriteArrayToParcelInt(parcel, ao);
wantParams->WriteArrayToParcelLong(parcel, ao);
wantParams->WriteArrayToParcelFloat(parcel, ao);
wantParams->WriteArrayToParcelDouble(parcel, ao);
return true;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
/* Run your code on data */
if (data == nullptr) {
std::cout << "invalid data" << std::endl;
return 0;
}
/* Validate the length of size */
if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
return 0;
}
char* ch = (char *)malloc(size + 1);
if (ch == nullptr) {
std::cout << "malloc failed." << std::endl;
return 0;
}
(void)memset_s(ch, size + 1, 0x00, size + 1);
if (memcpy_s(ch, size + 1, data, size) != EOK) {
std::cout << "copy failed." << std::endl;
free(ch);
ch = nullptr;
return 0;
}
OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_001(ch, size);
OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_002(ch, size);
OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_003(ch, size);
free(ch);
ch = nullptr;
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_DISTRIBUTEDWANTPARAMS_FUZZER_H
#define OHOS_DISTRIBUTEDWANTPARAMS_FUZZER_H
#define FUZZ_PROJECT_NAME "distributedwantparams_fuzzer"
#endif // OHOS_DISTRIBUTEDWANTPARAMS_FUZZER_H

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<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>