1 /******************************************************************************
2  *
3  * Copyright (C) 2019 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <algorithm>
28 #include <memory>
29 
30 #include "ihevc_typedefs.h"
31 #include "ihevcd_cxa.h"
32 #include "iv.h"
33 #include "ivd.h"
34 
35 #define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
36 #define ivd_api_function ihevcd_cxa_api_function
37 const IV_COLOR_FORMAT_T supportedColorFormats[] = {
38     IV_YUV_420P,   IV_YUV_420SP_UV, IV_YUV_420SP_VU,
39     IV_YUV_422ILE, IV_RGB_565,      IV_RGBA_8888};
40 
41 /* Decoder ignores invalid arch, i.e. for arm build, if SSSE3 is requested,
42  * decoder defaults to a supported configuration. So same set of supported
43  * architectures can be used in arm/arm64/x86 builds */
44 const IVD_ARCH_T supportedArchitectures[] = {
45     ARCH_ARM_NONEON,  ARCH_ARM_A9Q,   ARCH_ARM_NEONINTR, ARCH_ARMV8_GENERIC,
46     ARCH_X86_GENERIC, ARCH_X86_SSSE3, ARCH_X86_SSE42};
47 
48 enum {
49   OFFSET_COLOR_FORMAT = 6,
50   OFFSET_NUM_CORES,
51   OFFSET_ARCH,
52   /* Should be the last entry */
53   OFFSET_MAX,
54 };
55 
56 const static int kMaxNumDecodeCalls = 100;
57 const static int kSupportedColorFormats = NELEMENTS(supportedColorFormats);
58 const static int kSupportedArchitectures = NELEMENTS(supportedArchitectures);
59 const static int kMaxCores = 4;
iv_aligned_malloc(void * ctxt,WORD32 alignment,WORD32 size)60 void *iv_aligned_malloc(void *ctxt, WORD32 alignment, WORD32 size) {
61   void *buf = NULL;
62   (void)ctxt;
63   if (0 != posix_memalign(&buf, alignment, size)) {
64       return NULL;
65   }
66   return buf;
67 }
68 
iv_aligned_free(void * ctxt,void * buf)69 void iv_aligned_free(void *ctxt, void *buf) {
70   (void)ctxt;
71   free(buf);
72 }
73 
74 class Codec {
75  public:
76   Codec(IV_COLOR_FORMAT_T colorFormat, size_t numCores);
77   ~Codec();
78 
79   void createCodec();
80   void deleteCodec();
81   void resetCodec();
82   void setCores();
83   void allocFrame();
84   void freeFrame();
85   void decodeHeader(const uint8_t *data, size_t size);
86   IV_API_CALL_STATUS_T decodeFrame(const uint8_t *data, size_t size,
87                                    size_t *bytesConsumed);
88   void setParams(IVD_VIDEO_DECODE_MODE_T mode);
89   void setArchitecture(IVD_ARCH_T arch);
90 
91  private:
92   IV_COLOR_FORMAT_T mColorFormat;
93   size_t mNumCores;
94   iv_obj_t *mCodec;
95   ivd_out_bufdesc_t mOutBufHandle;
96   uint32_t mWidth;
97   uint32_t mHeight;
98 };
99 
Codec(IV_COLOR_FORMAT_T colorFormat,size_t numCores)100 Codec::Codec(IV_COLOR_FORMAT_T colorFormat, size_t numCores) {
101   mColorFormat = colorFormat;
102   mNumCores = numCores;
103   mCodec = nullptr;
104   mWidth = 0;
105   mHeight = 0;
106 
107   memset(&mOutBufHandle, 0, sizeof(mOutBufHandle));
108 }
109 
~Codec()110 Codec::~Codec() {}
111 
createCodec()112 void Codec::createCodec() {
113   IV_API_CALL_STATUS_T ret;
114   ihevcd_cxa_create_ip_t create_ip;
115   ihevcd_cxa_create_op_t create_op;
116   void *fxns = (void *)&ivd_api_function;
117 
118   create_ip.s_ivd_create_ip_t.e_cmd = IVD_CMD_CREATE;
119   create_ip.s_ivd_create_ip_t.u4_share_disp_buf = 0;
120   create_ip.s_ivd_create_ip_t.e_output_format = mColorFormat;
121   create_ip.s_ivd_create_ip_t.pf_aligned_alloc = iv_aligned_malloc;
122   create_ip.s_ivd_create_ip_t.pf_aligned_free = iv_aligned_free;
123   create_ip.s_ivd_create_ip_t.pv_mem_ctxt = NULL;
124   create_ip.s_ivd_create_ip_t.u4_size = sizeof(ihevcd_cxa_create_ip_t);
125   create_op.s_ivd_create_op_t.u4_size = sizeof(ihevcd_cxa_create_op_t);
126 
127   ret = ivd_api_function(NULL, (void *)&create_ip, (void *)&create_op);
128   if (ret != IV_SUCCESS) {
129     return;
130   }
131   mCodec = (iv_obj_t *)create_op.s_ivd_create_op_t.pv_handle;
132   mCodec->pv_fxns = fxns;
133   mCodec->u4_size = sizeof(iv_obj_t);
134 }
135 
deleteCodec()136 void Codec::deleteCodec() {
137   ivd_delete_ip_t delete_ip;
138   ivd_delete_op_t delete_op;
139 
140   delete_ip.e_cmd = IVD_CMD_DELETE;
141   delete_ip.u4_size = sizeof(ivd_delete_ip_t);
142   delete_op.u4_size = sizeof(ivd_delete_op_t);
143 
144   ivd_api_function(mCodec, (void *)&delete_ip, (void *)&delete_op);
145 }
146 
resetCodec()147 void Codec::resetCodec() {
148   ivd_ctl_reset_ip_t s_ctl_ip;
149   ivd_ctl_reset_op_t s_ctl_op;
150 
151   s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
152   s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_RESET;
153   s_ctl_ip.u4_size = sizeof(ivd_ctl_reset_ip_t);
154   s_ctl_op.u4_size = sizeof(ivd_ctl_reset_op_t);
155 
156   ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
157 }
158 
setCores()159 void Codec::setCores() {
160   ihevcd_cxa_ctl_set_num_cores_ip_t s_ctl_ip;
161   ihevcd_cxa_ctl_set_num_cores_op_t s_ctl_op;
162 
163   s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
164   s_ctl_ip.e_sub_cmd =
165       (IVD_CONTROL_API_COMMAND_TYPE_T)IHEVCD_CXA_CMD_CTL_SET_NUM_CORES;
166   s_ctl_ip.u4_num_cores = mNumCores;
167   s_ctl_ip.u4_size = sizeof(ihevcd_cxa_ctl_set_num_cores_ip_t);
168   s_ctl_op.u4_size = sizeof(ihevcd_cxa_ctl_set_num_cores_op_t);
169 
170   ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
171 }
172 
setParams(IVD_VIDEO_DECODE_MODE_T mode)173 void Codec::setParams(IVD_VIDEO_DECODE_MODE_T mode) {
174   ivd_ctl_set_config_ip_t s_ctl_ip;
175   ivd_ctl_set_config_op_t s_ctl_op;
176 
177   s_ctl_ip.u4_disp_wd = 0;
178   s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
179   s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
180   s_ctl_ip.e_vid_dec_mode = mode;
181   s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
182   s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
183   s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
184   s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);
185 
186   ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
187 }
188 
setArchitecture(IVD_ARCH_T arch)189 void Codec::setArchitecture(IVD_ARCH_T arch) {
190   ihevcd_cxa_ctl_set_processor_ip_t s_ctl_ip;
191   ihevcd_cxa_ctl_set_processor_op_t s_ctl_op;
192 
193   s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
194   s_ctl_ip.e_sub_cmd =
195       (IVD_CONTROL_API_COMMAND_TYPE_T)IHEVCD_CXA_CMD_CTL_SET_PROCESSOR;
196   s_ctl_ip.u4_arch = arch;
197   s_ctl_ip.u4_soc = SOC_GENERIC;
198   s_ctl_ip.u4_size = sizeof(ihevcd_cxa_ctl_set_processor_ip_t);
199   s_ctl_op.u4_size = sizeof(ihevcd_cxa_ctl_set_processor_op_t);
200 
201   ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
202 }
freeFrame()203 void Codec::freeFrame() {
204   for (int i = 0; i < mOutBufHandle.u4_num_bufs; i++) {
205     if (mOutBufHandle.pu1_bufs[i]) {
206       free(mOutBufHandle.pu1_bufs[i]);
207       mOutBufHandle.pu1_bufs[i] = nullptr;
208     }
209   }
210 }
211 
allocFrame()212 void Codec::allocFrame() {
213   size_t sizes[4] = {0};
214   size_t num_bufs = 0;
215 
216   freeFrame();
217 
218   memset(&mOutBufHandle, 0, sizeof(mOutBufHandle));
219 
220   switch (mColorFormat) {
221     case IV_YUV_420SP_UV:
222       [[fallthrough]];
223     case IV_YUV_420SP_VU:
224       sizes[0] = mWidth * mHeight;
225       sizes[1] = mWidth * mHeight >> 1;
226       num_bufs = 2;
227       break;
228     case IV_YUV_422ILE:
229       sizes[0] = mWidth * mHeight * 2;
230       num_bufs = 1;
231       break;
232     case IV_RGB_565:
233       sizes[0] = mWidth * mHeight * 2;
234       num_bufs = 1;
235       break;
236     case IV_RGBA_8888:
237       sizes[0] = mWidth * mHeight * 4;
238       num_bufs = 1;
239       break;
240     case IV_YUV_420P:
241       [[fallthrough]];
242     default:
243       sizes[0] = mWidth * mHeight;
244       sizes[1] = mWidth * mHeight >> 2;
245       sizes[2] = mWidth * mHeight >> 2;
246       num_bufs = 3;
247       break;
248   }
249   mOutBufHandle.u4_num_bufs = num_bufs;
250   for (int i = 0; i < num_bufs; i++) {
251     mOutBufHandle.u4_min_out_buf_size[i] = sizes[i];
252     mOutBufHandle.pu1_bufs[i] = (UWORD8 *)iv_aligned_malloc(NULL, 16, sizes[i]);
253   }
254 }
255 
decodeHeader(const uint8_t * data,size_t size)256 void Codec::decodeHeader(const uint8_t *data, size_t size) {
257   setParams(IVD_DECODE_HEADER);
258 
259   while (size > 0) {
260     IV_API_CALL_STATUS_T ret;
261     ivd_video_decode_ip_t dec_ip;
262     ivd_video_decode_op_t dec_op;
263     size_t bytes_consumed;
264 
265     memset(&dec_ip, 0, sizeof(dec_ip));
266     memset(&dec_op, 0, sizeof(dec_op));
267 
268     dec_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
269     dec_ip.u4_ts = 0;
270     dec_ip.pv_stream_buffer = (void *)data;
271     dec_ip.u4_num_Bytes = size;
272     dec_ip.u4_size = sizeof(ivd_video_decode_ip_t);
273     dec_op.u4_size = sizeof(ivd_video_decode_op_t);
274 
275     ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
276 
277     bytes_consumed = dec_op.u4_num_bytes_consumed;
278     /* If no bytes are consumed, then consume 4 bytes to ensure fuzzer proceeds
279      * to feed next data */
280     if (!bytes_consumed) bytes_consumed = 4;
281 
282     bytes_consumed = std::min(size, bytes_consumed);
283 
284     data += bytes_consumed;
285     size -= bytes_consumed;
286 
287     mWidth = std::min(dec_op.u4_pic_wd, (UWORD32)10240);
288     mHeight = std::min(dec_op.u4_pic_ht, (UWORD32)10240);
289 
290     /* Break after successful header decode */
291     if (mWidth && mHeight) {
292       break;
293     }
294   }
295   /* if width / height are invalid, set them to defaults */
296   if (!mWidth) mWidth = 1920;
297   if (!mHeight) mHeight = 1088;
298 }
299 
decodeFrame(const uint8_t * data,size_t size,size_t * bytesConsumed)300 IV_API_CALL_STATUS_T Codec::decodeFrame(const uint8_t *data, size_t size,
301                                         size_t *bytesConsumed) {
302   IV_API_CALL_STATUS_T ret;
303   ivd_video_decode_ip_t dec_ip;
304   ivd_video_decode_op_t dec_op;
305 
306   memset(&dec_ip, 0, sizeof(dec_ip));
307   memset(&dec_op, 0, sizeof(dec_op));
308 
309   dec_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
310   dec_ip.u4_ts = 0;
311   dec_ip.pv_stream_buffer = (void *)data;
312   dec_ip.u4_num_Bytes = size;
313   dec_ip.u4_size = sizeof(ivd_video_decode_ip_t);
314   dec_ip.s_out_buffer = mOutBufHandle;
315 
316   dec_op.u4_size = sizeof(ivd_video_decode_op_t);
317 
318   ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
319 
320   /* In case of change in resolution, reset codec and feed the same data again
321    */
322   if (IVD_RES_CHANGED == (dec_op.u4_error_code & 0xFF)) {
323     resetCodec();
324     ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
325   }
326   *bytesConsumed = dec_op.u4_num_bytes_consumed;
327 
328   /* If no bytes are consumed, then consume 4 bytes to ensure fuzzer proceeds
329    * to feed next data */
330   if (!*bytesConsumed) *bytesConsumed = 4;
331 
332   if (dec_op.u4_pic_wd && dec_op.u4_pic_ht &&
333       (mWidth != dec_op.u4_pic_wd || mHeight != dec_op.u4_pic_ht)) {
334     mWidth = std::min(dec_op.u4_pic_wd, (UWORD32)10240);
335     mHeight = std::min(dec_op.u4_pic_ht, (UWORD32)10240);
336     allocFrame();
337   }
338 
339   return ret;
340 }
341 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)342 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
343   if (size < 1) {
344     return 0;
345   }
346   size_t colorFormatOfst = std::min((size_t)OFFSET_COLOR_FORMAT, size - 1);
347   size_t numCoresOfst = std::min((size_t)OFFSET_NUM_CORES, size - 1);
348   size_t architectureOfst = std::min((size_t)OFFSET_ARCH, size - 1);
349   size_t architectureIdx = data[architectureOfst] % kSupportedArchitectures;
350   IVD_ARCH_T arch = (IVD_ARCH_T)supportedArchitectures[architectureIdx];
351   size_t colorFormatIdx = data[colorFormatOfst] % kSupportedColorFormats;
352   IV_COLOR_FORMAT_T colorFormat =
353       (IV_COLOR_FORMAT_T)(supportedColorFormats[colorFormatIdx]);
354   uint32_t numCores = (data[numCoresOfst] % kMaxCores) + 1;
355   size_t numDecodeCalls = 0;
356   Codec *codec = new Codec(colorFormat, numCores);
357   codec->createCodec();
358   codec->setArchitecture(arch);
359   codec->setCores();
360   codec->decodeHeader(data, size);
361   codec->setParams(IVD_DECODE_FRAME);
362   codec->allocFrame();
363 
364   while (size > 0 && numDecodeCalls < kMaxNumDecodeCalls) {
365     IV_API_CALL_STATUS_T ret;
366     size_t bytesConsumed;
367     ret = codec->decodeFrame(data, size, &bytesConsumed);
368 
369     bytesConsumed = std::min(size, bytesConsumed);
370     data += bytesConsumed;
371     size -= bytesConsumed;
372     numDecodeCalls++;
373   }
374 
375   codec->freeFrame();
376   codec->deleteCodec();
377   delete codec;
378   return 0;
379 }
380