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 #include <cstddef>
16 #include <cstdint>
17 #include "serverdec_sample.h"
18 #include <fuzzer/FuzzedDataProvider.h>
19 using namespace std;
20 using namespace OHOS;
21 using namespace OHOS::Media;
22 using namespace OHOS::MediaAVCodec;
23 #define FUZZ_PROJECT_NAME "hwavcdecoderserver_fuzzer"
24
25 namespace OHOS {
HwavcdecoderServerFuzzTest(const uint8_t * data,size_t size)26 bool HwavcdecoderServerFuzzTest(const uint8_t *data, size_t size)
27 {
28 if (size < sizeof(int32_t)) {
29 return false;
30 }
31 FuzzedDataProvider fdp(data, size);
32 VDecServerSample *vDecSample = new VDecServerSample();
33 vDecSample->fuzzData = data;
34 vDecSample->fuzzSize = size;
35 int32_t lengthMin = 176;
36 int32_t lengthMax = 4096;
37 int32_t frameRateMin = 1;
38 int32_t frameRateMax = 1000;
39 vDecSample->defaultWidth = std::clamp(fdp.ConsumeIntegral<int32_t>(), lengthMin, lengthMax);
40 vDecSample->defaultHeight = std::clamp(fdp.ConsumeIntegral<int32_t>(), lengthMin, lengthMax);
41 vDecSample->defaultFrameRate = std::clamp(fdp.ConsumeIntegral<int32_t>(), frameRateMin, frameRateMax);
42 std::vector<int32_t> pixelFormats = { 1, 2, 3, 4, 5 };
43 size_t pfIndex = fdp.ConsumeIntegralInRange<size_t>(0, pixelFormats.size() - 1);
44 vDecSample->defaultPixelFormat = pixelFormats[pfIndex];
45 vDecSample->RunVideoServerDecoder();
46 vDecSample->WaitForEos();
47 delete vDecSample;
48 return false;
49 }
50 } // namespace OHOS
51
52 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)53 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
54 {
55 /* Run your code on data */
56 OHOS::HwavcdecoderServerFuzzTest(data, size);
57 return 0;
58 }
59