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
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_ndk_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 VDecNdkSample *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 (!vDecSample) {
38 vDecSample = new VDecNdkSample();
39 vDecSample->DEFAULT_WIDTH = DEFAULT_WIDTH;
40 vDecSample->DEFAULT_HEIGHT = DEFAULT_HEIGHT;
41 vDecSample->DEFAULT_FRAME_RATE = DEFAULT_FRAME_RATE;
42 vDecSample->SURFACE_OUTPUT = false;
43 vDecSample->AFTER_EOS_DESTORY_CODEC = false;
44 vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC");
45 vDecSample->ConfigureVideoDecoder();
46 vDecSample->SetVideoDecoderCallback();
47 vDecSample->Start();
48 }
49 OH_AVErrCode ret = vDecSample->InputFunc_FUZZ(data, size);
50 if (ret != AV_ERR_OK) {
51 vDecSample->Release();
52 delete vDecSample;
53 vDecSample = nullptr;
54 return false;
55 }
56 return true;
57 }
58 } // namespace OHOS
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62 {
63 /* Run your code on data */
64 OHOS::DoSomethingInterestingWithMyAPI(data, size);
65 return 0;
66 }
67