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 "codeccommon_fuzzer.h"
17 #include <cstdlib>
18 
19 using namespace OHOS::HDI::Codec::V3_0;
20 
dlclose(void * handle)21 extern "C" __attribute__((visibility("default"))) int dlclose(void* handle)
22 {
23     return 0;
24 }
25 
26 namespace OHOS {
27 namespace Codec {
28     static const int32_t DATA_BUFFERID = 10;
29     static const int32_t DATA_SIZE = 20;
30     static const int32_t DATA_VERSION_NVERSION = 30;
31     static const int32_t DATA_ALLOCLEN = 60;
32     static const int32_t DATA_FILLEDLEN = 70;
33     static const int32_t DATA_OFFSET = 80;
34     static const int32_t DATA_FENCEFD = 90;
35     static const int32_t DATA_TYPE = 100;
36     static const int32_t DATA_PTS = 200;
37     static const int32_t DATA_FLAG = 300;
38     static const int32_t TESTING_APP_DATA = 33;
39 
40     uint32_t g_componentId = 0;
41     static int32_t g_appData = TESTING_APP_DATA;
42 
Release()43     void Release()
44     {
45         g_component = nullptr;
46         g_callback = nullptr;
47         g_manager = nullptr;
48     }
49 
FillDataOmxCodecBuffer(struct OmxCodecBuffer * dataFuzz)50     void FillDataOmxCodecBuffer(struct OmxCodecBuffer *dataFuzz)
51     {
52         dataFuzz->bufferId = DATA_BUFFERID;
53         dataFuzz->size = DATA_SIZE;
54         dataFuzz->version.nVersion = DATA_VERSION_NVERSION;
55         dataFuzz->bufferType = CODEC_BUFFER_TYPE_DMA_MEM_FD;
56         dataFuzz->bufferhandle = nullptr;
57         dataFuzz->fd = -1;
58         dataFuzz->allocLen = DATA_ALLOCLEN;
59         dataFuzz->filledLen = DATA_FILLEDLEN;
60         dataFuzz->offset = DATA_OFFSET;
61         dataFuzz->fenceFd = DATA_FENCEFD;
62         dataFuzz->type = static_cast<enum ShareMemTypes>(DATA_TYPE);
63         dataFuzz->pts = DATA_PTS;
64         dataFuzz->flag = DATA_FLAG;
65     }
66 
Preconditions()67     bool Preconditions()
68     {
69         g_manager = ICodecComponentManager::Get(true);
70         if (g_manager == nullptr) {
71             HDF_LOGE("%{public}s: ICodecComponentManager failed", __func__);
72             return false;
73         }
74 
75         g_callback = new CodecCallbackFuzz();
76         if (g_callback == nullptr) {
77             HDF_LOGE("%{public}s: codeccallback_fuzzer failed", __func__);
78             Release();
79             return false;
80         }
81 
82         int32_t count = 0;
83         auto err = g_manager->GetComponentNum(count);
84         if (err != HDF_SUCCESS || count <= 0) {
85             HDF_LOGE("%{public}s GetComponentNum return %{public}d, count = %{public}d", __func__, err, count);
86             Release();
87             return false;
88         }
89 
90         std::vector<CodecCompCapability> caps;
91         err = g_manager->GetComponentCapabilityList(caps, count);
92         if (err != HDF_SUCCESS) {
93             HDF_LOGE("%{public}s GetComponentCapabilityList return %{public}d", __func__, err);
94             Release();
95             return false;
96         }
97 
98         int32_t ret = g_manager->CreateComponent(g_component, g_componentId, caps[0].compName, g_appData, g_callback);
99         if (ret != HDF_SUCCESS) {
100             HDF_LOGE("%{public}s: CreateComponent failed\n", __func__);
101             Release();
102             return false;
103         }
104 
105         return true;
106     }
107 
Destroy()108     bool Destroy()
109     {
110         if (g_manager == nullptr) {
111             HDF_LOGE("%{public}s: ICodecComponentManager failed", __func__);
112             return false;
113         }
114 
115         int32_t ret = g_manager->DestroyComponent(g_componentId);
116         if (ret != HDF_SUCCESS) {
117             HDF_LOGE("%{public}s: DestroyComponent failed\n", __func__);
118             Release();
119             return false;
120         }
121         Release();
122         return true;
123     }
124 } // namespace codec
125 } // namespace OHOS
126