diff --git a/test/fuzztest/distributedschedstub_fuzzer/distributedschedstub_fuzzer.cpp b/test/fuzztest/distributedschedstub_fuzzer/distributedschedstub_fuzzer.cpp index bfc77631..72896a30 100644 --- a/test/fuzztest/distributedschedstub_fuzzer/distributedschedstub_fuzzer.cpp +++ b/test/fuzztest/distributedschedstub_fuzzer/distributedschedstub_fuzzer.cpp @@ -402,17 +402,23 @@ void ReleaseRemoteAbilityInnerFuzzTest(const uint8_t* data, size_t size) DistributedSchedService::GetInstance().ProcessDeviceOffline(deviceId); } -void GetDistributedComponentListInnerFuzzTest(const uint8_t* data, size_t size) +int32_t GetDistributedComponentListInnerFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < sizeof(int32_t))) { - return; + return INVALID_PARAMETERS_ERR; } FuzzUtil::MockPermission(); MessageParcel dataParcel; MessageParcel reply; - MessageOption option; + std::vector distributedComponents; + int32_t int32Data = *(reinterpret_cast(data)); + std::string str(reinterpret_cast(data), size); + distributedComponents.push_back(str); + PARCEL_WRITE_HELPER(reply, Int32, int32Data); + PARCEL_WRITE_HELPER(reply, StringVector, distributedComponents); DistributedSchedService::GetInstance().GetDistributedComponentListInner(dataParcel, reply); + return ERR_OK; } void StartRemoteFreeInstallInnerFuzzTest(const uint8_t* data, size_t size) diff --git a/test/fuzztest/distributedwant_fuzzer/distributedwant_fuzzer.cpp b/test/fuzztest/distributedwant_fuzzer/distributedwant_fuzzer.cpp index 88aa4ba5..e56f80f8 100644 --- a/test/fuzztest/distributedwant_fuzzer/distributedwant_fuzzer.cpp +++ b/test/fuzztest/distributedwant_fuzzer/distributedwant_fuzzer.cpp @@ -44,30 +44,38 @@ uint32_t GetU32Data(const char* ptr) 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) +bool DoSomethingInterestingWithMyAPI_DistributedWant_001(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + std::shared_ptr want = std::make_shared(); - unsigned int flags = static_cast(GetU32Data(data)); + unsigned int flags = static_cast(GetU32Data(reinterpret_cast(data))); want->SetFlags(flags); want->RemoveFlags(flags); want->AddFlags(flags); - std::string entity(data, size); + std::string entity(reinterpret_cast(data), size); want->AddEntity(entity); want->HasEntity(entity); want->RemoveEntity(entity); - std::string bundleName(data, size); + std::string bundleName(reinterpret_cast(data), size); want->SetBundle(bundleName); - std::string deviceId(data, size); + std::string deviceId(reinterpret_cast(data), size); want->SetDeviceId(deviceId); want->SetElementName(bundleName, entity); want->SetElementName(deviceId, bundleName, entity); return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWant_002(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWant_002(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + std::shared_ptr want = std::make_shared(); - std::string type(data, size); + std::string type(reinterpret_cast(data), size); want->SetType(type); Uri uri(type); want->SetUri(uri); @@ -78,28 +86,36 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_002(const char* data, size_ return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWant_003(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWant_003(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + std::shared_ptr want = std::make_shared(); want->CountEntities(); want->GetScheme(); DistributedOperation operation; want->SetOperation(operation); - std::string key(data, size); + std::string key(reinterpret_cast(data), size); want->HasParameter(key); - std::string content(data, size); - std::string prop(data, size); - std::string value(data, size); - std::string str(data, size); + std::string content(reinterpret_cast(data), size); + std::string prop(reinterpret_cast(data), size); + std::string value(reinterpret_cast(data), size); + std::string str(reinterpret_cast(data), size); nlohmann::json wantJson; want->ReadFromJson(wantJson); return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWant_004(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWant_004(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + std::shared_ptr want = std::make_shared(); - std::string key(data, size); + std::string key(reinterpret_cast(data), size); sptr remoteObject; want->SetParam(key, remoteObject); std::vector boolValue; @@ -118,10 +134,14 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_004(const char* data, size_ return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWant_005(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWant_005(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + std::shared_ptr want = std::make_shared(); - std::string key(data, size); + std::string key(reinterpret_cast(data), size); std::vector charVector; want->SetParam(key, charVector); want->GetCharArrayParam(key); @@ -143,10 +163,14 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_005(const char* data, size_ return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWant_006(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWant_006(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + std::shared_ptr want = std::make_shared(); - std::string key(data, size); + std::string key(reinterpret_cast(data), size); std::vector floatVector; want->SetParam(key, floatVector); want->GetFloatArrayParam(key); @@ -162,7 +186,7 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_006(const char* data, size_ std::vector shortVector; want->SetParam(key, shortVector); want->GetShortArrayParam(key); - std::string stringValue(data, size); + std::string stringValue(reinterpret_cast(data), size); want->SetParam(key, stringValue); want->GetStringParam(key); std::vector stringVector; @@ -185,38 +209,11 @@ bool DoSomethingInterestingWithMyAPI_DistributedWant_006(const char* data, size_ /* 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(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; + OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_001(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_002(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_003(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_004(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_005(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWant_006(data, size); return 0; } diff --git a/test/fuzztest/distributedwantparams_fuzzer/distributedwantparams_fuzzer.cpp b/test/fuzztest/distributedwantparams_fuzzer/distributedwantparams_fuzzer.cpp index ad602402..225473ff 100644 --- a/test/fuzztest/distributedwantparams_fuzzer/distributedwantparams_fuzzer.cpp +++ b/test/fuzztest/distributedwantparams_fuzzer/distributedwantparams_fuzzer.cpp @@ -54,10 +54,14 @@ uint32_t GetU32Data(const char* ptr) 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) +bool DoSomethingInterestingWithMyAPI_DistributedWantParams_001(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + DistributedWantParams wantOther; - std::string key(data, size); + std::string key(reinterpret_cast(data), size); std::shared_ptr wantParams = std::make_shared(wantOther); sptr array = new (std::nothrow) AAFwk::Array(0, DistributedSchedule::g_IID_IDistributedWantParams); wantParams->SetParam(key, array); @@ -71,14 +75,15 @@ bool DoSomethingInterestingWithMyAPI_DistributedWantParams_001(const char* data, wantParams->HasParam(key); wantParams->IsEmpty(); wantParams->GetParams(); - - Parcel parcel; - return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWantParams_002(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWantParams_002(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + DistributedWantParams wantOther; std::shared_ptr wantParams = std::make_shared(wantOther); Parcel parcel; @@ -92,10 +97,10 @@ bool DoSomethingInterestingWithMyAPI_DistributedWantParams_002(const char* data, wantParams->WriteArrayToParcelLong(parcel, ao); wantParams->WriteArrayToParcelFloat(parcel, ao); wantParams->WriteArrayToParcelDouble(parcel, ao); - long longValue = static_cast(GetU32Data(data)); + long longValue = static_cast(GetU32Data(reinterpret_cast(data))); sptr longIt = Long::Box(longValue); wantParams->WriteToParcelLong(parcel, longIt); - float floatValue = static_cast(GetU32Data(data)); + float floatValue = static_cast(GetU32Data(reinterpret_cast(data))); sptr floatIt = Float::Box(floatValue); wantParams->WriteToParcelFloat(parcel, floatIt); @@ -109,44 +114,48 @@ bool DoSomethingInterestingWithMyAPI_DistributedWantParams_002(const char* data, wantParams->ReadFromParcelArrayLong(parcel, array); wantParams->ReadFromParcelArrayFloat(parcel, array); wantParams->ReadFromParcelArrayDouble(parcel, array); - std::string key(data, size); + std::string key(reinterpret_cast(data), size); wantParams->ReadFromParcelLong(parcel, key); wantParams->ReadFromParcelFloat(parcel, key); - wantParams->ReadArrayToParcel(parcel, GetU32Data(data) % OFFSET_16, array); + wantParams->ReadArrayToParcel(parcel, GetU32Data(reinterpret_cast(data)) % OFFSET_16, array); return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWantParams_003(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWantParams_003(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + DistributedWantParams wantOther; std::shared_ptr wantParams = std::make_shared(wantOther); Parcel parcel; - std::string key(data, size); - int8_t byteValue = static_cast(GetU32Data(data)); + std::string key(reinterpret_cast(data), size); + int8_t byteValue = static_cast(GetU32Data(reinterpret_cast(data))); sptr byteIt = Byte::Box(byteValue); wantParams->WriteToParcelByte(parcel, byteIt); sptr stringIt = String::Box(key); wantParams->WriteToParcelString(parcel, stringIt); - bool boolValue = static_cast(GetU32Data(data) > FOO_MAX_LEN); + bool boolValue = static_cast(GetU32Data(reinterpret_cast(data)) > FOO_MAX_LEN); sptr boolIt = Boolean::Box(boolValue); wantParams->WriteToParcelBool(parcel, boolIt); char charValue = *data; sptr charIt = Char::Box(charValue); wantParams->WriteToParcelChar(parcel, charIt); - short shortValue = static_cast(GetU32Data(data)); + short shortValue = static_cast(GetU32Data(reinterpret_cast(data))); sptr shortIt = Short::Box(shortValue); wantParams->WriteToParcelShort(parcel, shortIt); - double doubleValue = static_cast(GetU32Data(data)); + double doubleValue = static_cast(GetU32Data(reinterpret_cast(data))); sptr doubleIt = Double::Box(doubleValue); wantParams->WriteToParcelDouble(parcel, doubleIt); - int32_t intValue = static_cast(GetU32Data(data)); + int32_t intValue = static_cast(GetU32Data(reinterpret_cast(data))); sptr intIt = Integer::Box(intValue); wantParams->WriteToParcelInt(parcel, intIt); wantParams->WriteToParcelFD(parcel, wantOther); wantParams->WriteToParcelRemoteObject(parcel, wantOther); - int type = static_cast(GetU32Data(data)); + int type = static_cast(GetU32Data(reinterpret_cast(data))); wantParams->ReadFromParcelInt8(parcel, key); wantParams->ReadFromParcelString(parcel, key); wantParams->ReadFromParcelBool(parcel, key); @@ -169,12 +178,16 @@ bool DoSomethingInterestingWithMyAPI_DistributedWantParams_003(const char* data, return true; } -bool DoSomethingInterestingWithMyAPI_DistributedWantParams_004(const char* data, size_t size) +bool DoSomethingInterestingWithMyAPI_DistributedWantParams_004(const uint8_t* data, size_t size) { + if (data == nullptr ||size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { + return false; + } + DistributedWantParams wantOther; std::shared_ptr wantParams = std::make_shared(wantOther); - std::string value(data, size); + std::string value(reinterpret_cast(data), size); sptr stringObj = DistributedWantParams::GetInterfaceByType(DistributedWantParams::VALUE_TYPE_STRING, value); wantParams->CompareInterface(stringObj, stringObj, DistributedWantParams::VALUE_TYPE_STRING); @@ -193,36 +206,9 @@ bool DoSomethingInterestingWithMyAPI_DistributedWantParams_004(const char* data, /* 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); - OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_004(ch, size); - free(ch); - ch = nullptr; + OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_001(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_002(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_003(data, size); + OHOS::DoSomethingInterestingWithMyAPI_DistributedWantParams_004(data, size); return 0; }