• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "componentloader_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "component_loader.h"
22 #include "constants.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 namespace {
29 const uint32_t DH_TYPE_SIZE = 10;
30 const DHType dhTypeFuzz[DH_TYPE_SIZE] = {
31     DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_AUDIO,
32     DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP
33 };
34 }
35 
ComponentManagerFuzzTest(const uint8_t * data,size_t size)36 void ComponentManagerFuzzTest(const uint8_t* data, size_t size)
37 {
38     if ((data == nullptr) || (size == 0)) {
39         return;
40     }
41 
42     ComponentLoader::GetInstance().Init();
43     DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE];
44 
45     IHardwareHandler *hardwareHandlerPtr = nullptr;
46     ComponentLoader::GetInstance().GetHardwareHandler(dhType, hardwareHandlerPtr);
47     IDistributedHardwareSource *sourcePtr = nullptr;
48     ComponentLoader::GetInstance().GetSource(dhType, sourcePtr);
49     IDistributedHardwareSink *sinkPtr = nullptr;
50     ComponentLoader::GetInstance().GetSink(dhType, sinkPtr);
51 
52     ComponentLoader::GetInstance().ReleaseHardwareHandler(dhType);
53     ComponentLoader::GetInstance().ReleaseSource(dhType);
54     ComponentLoader::GetInstance().ReleaseSink(dhType);
55 
56     ComponentLoader::GetInstance().GetHardwareHandler(dhType);
57     ComponentLoader::GetInstance().GetSource(dhType);
58     ComponentLoader::GetInstance().GetSink(dhType);
59 }
60 }
61 }
62 
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66     /* Run your code on data */
67     OHOS::DistributedHardware::ComponentManagerFuzzTest(data, size);
68     return 0;
69 }
70 
71