/* * Copyright (c) 2025 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 #include #include #include #include #include #include #undef private #include "hpae_source_input_cluster.h" #include "hpae_source_input_node.h" #include "hpae_source_output_node.h" #include "hpae_format_convert.h" #include "audio_engine_log.h" using namespace std; using namespace OHOS::AudioStandard::HPAE; namespace OHOS { namespace AudioStandard { using namespace std; static const uint8_t *RAW_DATA = nullptr; static size_t g_dataSize = 0; static size_t g_pos; const size_t THRESHOLD = 10; const uint32_t DEFAULT_FRAME_LENGTH = 960; const uint32_t DEFAULT_NODE_ID = 1243; typedef void (*TestPtr)(const uint8_t *, size_t); template T GetData() { T object {}; size_t objectSize = sizeof(object); if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) { return object; } errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize); if (ret != EOK) { return {}; } g_pos += objectSize; return object; } template uint32_t GetArrLength(T& arr) { if (arr == nullptr) { AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__); return 0; } return sizeof(arr) / sizeof(arr[0]); } static void GetTestNodeInfo(HpaeNodeInfo &nodeInfo) { nodeInfo.nodeId = DEFAULT_NODE_ID; nodeInfo.frameLen = DEFAULT_FRAME_LENGTH; nodeInfo.samplingRate = SAMPLE_RATE_48000; nodeInfo.channels = STEREO; nodeInfo.format = SAMPLE_S16LE; nodeInfo.sceneType = HPAE_SCENE_RECORD; nodeInfo.sourceBufferType = HPAE_SOURCE_BUFFER_TYPE_MIC; } void DoProcessFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->DoProcess(); } void ResetFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->Reset(); } void ResetAllFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->ResetAll(); } void GetSharedInstanceFuzzTest1() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetSharedInstance(); } void GetSharedInstanceFuzzTest2() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetSharedInstance(nodeInfo); } void GetOutputPortFuzzTest1() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetOutputPort(); } void GetOutputPortFuzzTest2() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetOutputPort(nodeInfo); } void GetCapturerSourceInstanceFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); std::string deviceClass = "file_io"; std::string deviceNetId = "LocalDevice"; SourceType sourceType = SOURCE_TYPE_MIC; std::string sourceName = "mic"; hpaeSourceInputCluster->GetCapturerSourceInstance(deviceClass, deviceNetId, sourceType, sourceName); } void GetOutputPortFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetOutputPort(); } void CapturerSourceDeInitFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->CapturerSourceDeInit(); } void CapturerSourceFlushFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->CapturerSourceFlush(); } void CapturerSourcePauseFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->CapturerSourcePause(); } void CapturerSourceResetFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->CapturerSourceReset(); } void CapturerSourceResumeFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->CapturerSourceResume(); } void CapturerSourceStartFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->CapturerSourceStart(); } void CapturerSourceStopFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->CapturerSourceStop(); } void GetSourceStateFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetSourceState(); } void GetOutputPortNumFuzzTest1() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetOutputPortNum(); } void GetOutputPortNumFuzzTest2() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetOutputPortNum(nodeInfo); } void GetSourceInputNodeTypeFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetSourceInputNodeType(); } void GSetSourceInputNodeTypeFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); HpaeSourceInputNodeType type = HpaeSourceInputNodeType::HPAE_SOURCE_DEFAULT; hpaeSourceInputCluster->SetSourceInputNodeType(type); } void UpdateAppsUidAndSessionIdFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); std::vector appsUid = {GetData()}; std::vector sessionsId = {GetData()}; hpaeSourceInputCluster->UpdateAppsUidAndSessionId(appsUid, sessionsId); } void GetCaptureIdFuzzTest() { HpaeNodeInfo nodeInfo; GetTestNodeInfo(nodeInfo); auto hpaeSourceInputCluster = std::make_shared(nodeInfo); hpaeSourceInputCluster->GetCaptureId(); } typedef void (*TestFuncs[23])(); TestFuncs g_testFuncs = { DoProcessFuzzTest, ResetFuzzTest, ResetAllFuzzTest, GetSharedInstanceFuzzTest1, GetSharedInstanceFuzzTest2, GetOutputPortFuzzTest1, GetOutputPortFuzzTest2, GetCapturerSourceInstanceFuzzTest, GetOutputPortFuzzTest, CapturerSourceDeInitFuzzTest, CapturerSourceFlushFuzzTest, CapturerSourcePauseFuzzTest, CapturerSourceResetFuzzTest, CapturerSourceResumeFuzzTest, CapturerSourceStartFuzzTest, CapturerSourceStopFuzzTest, GetSourceStateFuzzTest, GetOutputPortNumFuzzTest1, GetOutputPortNumFuzzTest2, GetSourceInputNodeTypeFuzzTest, GSetSourceInputNodeTypeFuzzTest, UpdateAppsUidAndSessionIdFuzzTest, GetCaptureIdFuzzTest, }; bool FuzzTest(const uint8_t* rawData, size_t size) { if (rawData == nullptr) { return false; } // initialize data RAW_DATA = rawData; g_dataSize = size; g_pos = 0; uint32_t code = GetData(); uint32_t len = GetArrLength(g_testFuncs); if (len > 0) { g_testFuncs[code % len](); } else { AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__); } return true; } } // namespace AudioStandard } // namesapce OHOS /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { if (size < OHOS::AudioStandard::THRESHOLD) { return 0; } OHOS::AudioStandard::FuzzTest(data, size); return 0; }