1 /*
2 * Copyright (c) 2024 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 <cstddef>
17 #include <cstdint>
18 #include "native_avcodec_base.h"
19 #include "native_avcodec_videodecoder.h"
20 #include "native_averrors.h"
21 #include "videodec_api11_sample.h"
22
23 #define FUZZ_PROJECT_NAME "swdecoderresource_fuzzer"
24
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::Media;
28
29 static VDecFuzzSample *g_vDecSample = nullptr;
30 constexpr uint32_t DEFAULT_WIDTH = 1920;
31 constexpr uint32_t DEFAULT_HEIGHT = 1080;
32 constexpr double DEFAULT_FRAME_RATE = 30.0;
33
34 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)35 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
36 {
37 if (!g_vDecSample) {
38 g_vDecSample = new VDecFuzzSample();
39 g_vDecSample->defaultWidth = DEFAULT_WIDTH;
40 g_vDecSample->defaultHeight = DEFAULT_HEIGHT;
41 g_vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
42 g_vDecSample->CreateVideoDecoder();
43 g_vDecSample->ConfigureVideoDecoder();
44 g_vDecSample->SetVideoDecoderCallback();
45 g_vDecSample->Start();
46 }
47 OH_AVErrCode ret = g_vDecSample->InputFuncFUZZ(data, size);
48 if (ret != AV_ERR_OK) {
49 g_vDecSample->Flush();
50 g_vDecSample->Stop();
51 g_vDecSample->Reset();
52 g_vDecSample->Release();
53 delete g_vDecSample;
54 g_vDecSample = nullptr;
55 return false;
56 }
57 return true;
58 }
59 } // namespace OHOS
60
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
63 {
64 /* Run your code on data */
65 OHOS::DoSomethingInterestingWithMyAPI(data, size);
66 return 0;
67 }
68