• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "securec.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include "native_avcapability.h"
20 #include "native_avcodec_base.h"
21 #include <fuzzer/FuzzedDataProvider.h>
22 using namespace std;
23 #define FUZZ_PROJECT_NAME "avcapability_fuzzer"
24 
25 OH_AVCapability *cap = nullptr;
26 const int32_t *g_profiles = nullptr;
27 uint32_t g_profileNum = 0;
28 const int32_t *g_levels = nullptr;
29 uint32_t g_levelNum = 0;
30 const int32_t *g_pixFormat = nullptr;
31 uint32_t g_pixFormatNum = 0;
32 const int32_t *g_sampleRates = nullptr;
33 uint32_t g_sampleRateNum = 0;
34 int32_t g_widthAlignment = 0;
35 int32_t g_heightAlignment = 0;
36 OH_AVRange *g_rangeList = nullptr;
37 uint32_t g_rangeListNum = 0;
38 
AvcapabilityFuzzTest(const uint8_t * data,size_t size)39 bool AvcapabilityFuzzTest(const uint8_t *data, size_t size)
40 {
41     FuzzedDataProvider fdp(data, size);
42     size_t maxSize = std::numeric_limits<size_t>::max();
43     string mimeStr = fdp.ConsumeRandomLengthString(maxSize);
44     bool isEncoder = fdp.ConsumeBool();
45     int32_t profile = fdp.ConsumeIntegral<int32_t>();
46     int32_t level = fdp.ConsumeIntegral<int32_t>();
47     int bitrateValue = fdp.ConsumeIntegralInRange(0, 3);
48     OH_BitrateMode mode = static_cast<OH_BitrateMode>(bitrateValue);
49     int32_t height = fdp.ConsumeIntegral<int32_t>();
50     int32_t width = fdp.ConsumeIntegral<int32_t>();
51     int32_t frameRate = fdp.ConsumeIntegral<int32_t>();
52     int featureValue = fdp.ConsumeIntegralInRange(0, 2);
53     OH_AVCapabilityFeature feature = static_cast<OH_AVCapabilityFeature>(featureValue);
54     OH_AVCodecCategory category = static_cast<OH_AVCodecCategory>(fdp.ConsumeIntegral<uint8_t>());
55 
56     OH_AVCodec_GetCapability(mimeStr.c_str(), isEncoder);
57     cap = OH_AVCodec_GetCapabilityByCategory(mimeStr.c_str(), isEncoder, category);
58     OH_AVRange range;
59     memset_s(&range, sizeof(OH_AVRange), 0, sizeof(OH_AVRange));
60     OH_AVCapability_GetName(cap);
61     OH_AVCapability_IsHardware(cap);
62     OH_AVCapability_GetMaxSupportedInstances(cap);
63     OH_AVCapability_GetSupportedProfiles(cap, &g_profiles, &g_profileNum);
64     OH_AVCapability_GetSupportedLevelsForProfile(cap, profile, &g_levels, &g_levelNum);
65     OH_AVCapability_AreProfileAndLevelSupported(cap, profile, level);
66     OH_AVCapability_GetEncoderBitrateRange(cap, &range);
67     OH_AVCapability_GetEncoderQualityRange(cap, &range);
68     OH_AVCapability_GetEncoderComplexityRange(cap, &range);
69     OH_AVCapability_IsEncoderBitrateModeSupported(cap, mode);
70     OH_AVCapability_GetVideoSupportedPixelFormats(cap, &g_pixFormat, &g_pixFormatNum);
71     OH_AVCapability_GetVideoWidthAlignment(cap, &g_widthAlignment);
72     OH_AVCapability_GetVideoHeightAlignment(cap, &g_heightAlignment);
73     OH_AVCapability_GetVideoWidthRangeForHeight(cap, height, &range);
74     OH_AVCapability_GetVideoHeightRangeForWidth(cap, width, &range);
75     OH_AVCapability_GetVideoWidthRange(cap, &range);
76     OH_AVCapability_GetVideoHeightRange(cap, &range);
77     OH_AVCapability_IsVideoSizeSupported(cap, width, height);
78     OH_AVCapability_GetVideoFrameRateRange(cap, &range);
79     OH_AVCapability_GetVideoFrameRateRangeForSize(cap, width, height, &range);
80     OH_AVCapability_AreVideoSizeAndFrameRateSupported(cap, width, height, frameRate);
81     OH_AVCapability_IsFeatureSupported(cap, feature);
82     OH_AVCapability_GetFeatureProperties(cap, feature);
83     OH_AVCapability_GetAudioChannelCountRange(cap, &range);
84     OH_AVCapability_GetAudioSupportedSampleRates(cap, &g_sampleRates, &g_sampleRateNum);
85     OH_AVCapability_GetAudioSupportedSampleRateRanges(cap, &g_rangeList, &g_rangeListNum);
86     return true;
87 }
88 
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
91 {
92     /* Run your code on data */
93     AvcapabilityFuzzTest(data, size);
94     return 0;
95 }
96