1 /*
2  * Copyright (c) 2022-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 "codec_omx_ext.h"
18 #include <cstdlib>
19 
dlclose(void * handle)20 extern "C" __attribute__((visibility("default"))) int dlclose(void* handle)
21 {
22     return 0;
23 }
24 
25 namespace OHOS {
26 namespace Codec {
27     static const int32_t DATA_BUFFERID = 10;
28     static const int32_t DATA_SIZE = 20;
29     static const int32_t DATA_VERSION_NVERSION = 30;
30     static const int32_t DATA_BUFFERTYPE = 40;
31     static const int32_t DATA_BUFFERLEN = 50;
32     static const int32_t DATA_ALLOCLEN = 60;
33     static const int32_t DATA_FILLEDLEN = 70;
34     static const int32_t DATA_OFFSET = 80;
35     static const int32_t DATA_FENCEFD = 90;
36     static const int32_t DATA_TYPE = 100;
37     static const int32_t DATA_PTS = 200;
38     static const int32_t DATA_FLAG = 300;
39     static const int32_t TESTING_APP_DATA = 33;
40 
41     CodecComponentManager *g_manager = nullptr;
42     CodecComponentType *g_component = nullptr;
43     CodecCallbackType *g_callback = nullptr;
44     uint32_t g_componentId = 0;
45     static int32_t g_appData = TESTING_APP_DATA;
46 
Convert2Uint32(const uint8_t * ptr)47     uint32_t Convert2Uint32(const uint8_t* ptr)
48     {
49         if (ptr == nullptr) {
50             return 0;
51         }
52         /*
53          * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
54          * and the third digit no left
55          */
56         return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
57     }
58 
FillDataOmxCodecBuffer(struct OmxCodecBuffer * dataFuzz)59     void FillDataOmxCodecBuffer(struct OmxCodecBuffer *dataFuzz)
60     {
61         dataFuzz->bufferId = DATA_BUFFERID;
62         dataFuzz->size = DATA_SIZE;
63         dataFuzz->version.nVersion = DATA_VERSION_NVERSION;
64         dataFuzz->bufferType = static_cast<enum CodecBufferType>(DATA_BUFFERTYPE);
65         dataFuzz->buffer = reinterpret_cast<uint8_t *>(OsalMemAlloc(DATA_BUFFERLEN));
66         if (dataFuzz->buffer == nullptr) {
67             HDF_LOGE("%{public}s: dataFuzz->buffer is nullptr", __func__);
68             return;
69         }
70         dataFuzz->bufferLen = DATA_BUFFERLEN;
71         dataFuzz->allocLen = DATA_ALLOCLEN;
72         dataFuzz->filledLen = DATA_FILLEDLEN;
73         dataFuzz->offset = DATA_OFFSET;
74         dataFuzz->fenceFd = DATA_FENCEFD;
75         dataFuzz->type = static_cast<enum ShareMemTypes>(DATA_TYPE);
76         dataFuzz->pts = DATA_PTS;
77         dataFuzz->flag = DATA_FLAG;
78     }
79 
Preconditions()80     bool Preconditions()
81     {
82         g_manager = GetCodecComponentManager();
83         if (g_manager == nullptr) {
84             HDF_LOGE("%{public}s: GetCodecComponentManager failed\n", __func__);
85             return false;
86         }
87 
88         g_callback = CodecCallbackTypeStubGetInstance();
89         if (g_callback == nullptr) {
90             HDF_LOGE("%{public}s: CodecCallbackTypeStubGetInstance failed\n", __func__);
91             return false;
92         }
93 
94         int32_t count = g_manager->GetComponentNum();
95         if (count <= 0) {
96             CodecCallbackTypeRelease(g_callback);
97             HDF_LOGE("%{public}s GetComponentNum count = %{public}d", __func__, count);
98             return false;
99         }
100 
101         CodecCompCapability *capList = reinterpret_cast<CodecCompCapability *>(OsalMemAlloc(sizeof(CodecCompCapability)
102             * count));
103         if (capList == nullptr) {
104             CodecCallbackTypeRelease(g_callback);
105             HDF_LOGE("%{public}s: OsalMemAlloc CodecCompCapability failed\n", __func__);
106             return false;
107         }
108 
109         int32_t ret = g_manager->GetComponentCapabilityList(capList, count);
110         if (ret != HDF_SUCCESS) {
111             OsalMemFree(capList);
112             CodecCallbackTypeRelease(g_callback);
113             HDF_LOGI("%{public}s: GetComponentCapabilityList succeed\n", __func__);
114             return false;
115         }
116 
117         ret = g_manager->CreateComponent(&g_component, &g_componentId, capList[0].compName, g_appData, g_callback);
118         if (ret != HDF_SUCCESS) {
119             OsalMemFree(capList);
120             CodecCallbackTypeRelease(g_callback);
121             HDF_LOGE("%{public}s: CreateComponent failed\n", __func__);
122             return false;
123         }
124         OsalMemFree(capList);
125         return true;
126     }
127 
Destroy()128     bool Destroy()
129     {
130         int32_t ret = g_manager->DestroyComponent(g_componentId);
131         if (ret != HDF_SUCCESS) {
132             HDF_LOGE("%{public}s: DestroyComponent failed\n", __func__);
133             return false;
134         }
135         CodecCallbackTypeRelease(g_callback);
136         CodecComponentTypeRelease(g_component);
137         CodecComponentManagerRelease();
138         return true;
139     }
140 } // namespace codec
141 } // namespace OHOS
142