• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "native_avcodec_base.h"
18 #include "native_avformat.h"
19 #include "videodec_sample.h"
20 #include <fuzzer/FuzzedDataProvider.h>
21 #define FUZZ_PROJECT_NAME "swdecodersetparameter_fuzzer"
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 static VDecFuzzSample *vDecSample = nullptr;
26 constexpr uint32_t DEFAULT_WIDTH = 1920;
27 constexpr uint32_t DEFAULT_HEIGHT = 1080;
28 constexpr double DEFAULT_FRAME_RATE = 30.0;
29 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)30 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
31 {
32     if (size < sizeof(int64_t)) {
33         return false;
34     }
35     if (!vDecSample) {
36         vDecSample = new VDecFuzzSample();
37         vDecSample->defaultWidth = DEFAULT_WIDTH;
38         vDecSample->defaultHeight = DEFAULT_HEIGHT;
39         vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
40         vDecSample->isSurfMode = true;
41         vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC");
42         vDecSample->ConfigureVideoDecoder();
43         vDecSample->SetVideoDecoderCallback();
44         vDecSample->Start();
45     }
46     OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("video/avc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
47     FuzzedDataProvider fdp(data, size);
48     int intData0 = fdp.ConsumeIntegral<int32_t>();
49     int intData1 = fdp.ConsumeIntegral<int32_t>();
50     int intData2 = fdp.ConsumeIntegral<int32_t>();
51     int intData3 = fdp.ConsumeIntegral<int32_t>();
52     int intData4 = fdp.ConsumeIntegral<int32_t>();
53     int intData5 = fdp.ConsumeIntegral<int32_t>();
54     int intData6 = fdp.ConsumeIntegral<int32_t>();
55     int intData7 = fdp.ConsumeIntegral<int32_t>();
56     int intData8 = fdp.ConsumeIntegral<int32_t>();
57     int longData = fdp.ConsumeIntegral<int64_t>();
58     double doubleData = fdp.ConsumeFloatingPoint<double>();
59     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData0);
60     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData1);
61     OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData2);
62     OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData3);
63     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData4);
64     OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData5);
65     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData6);
66     OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData7);
67     OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData8);
68     OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
69     OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
70 
71     vDecSample->SetParameter(format);
72 
73     OH_AVFormat_Destroy(format);
74     return true;
75 }
76 } // namespace OHOS
77 
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
80 {
81     /* Run your code on data */
82     OHOS::DoSomethingInterestingWithMyAPI(data, size);
83     return 0;
84 }
85