Coverage Report

Created: 2025-11-11 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/fuzzer/hevc_dec_fuzzer.cpp
Line
Count
Source
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
86.7k
#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;
60
53.2k
void *iv_aligned_malloc(void *ctxt, WORD32 alignment, WORD32 size) {
61
53.2k
  void *buf = NULL;
62
53.2k
  (void)ctxt;
63
53.2k
  if (0 != posix_memalign(&buf, alignment, size)) {
64
0
      return NULL;
65
0
  }
66
53.2k
  return buf;
67
53.2k
}
68
69
49.9k
void iv_aligned_free(void *ctxt, void *buf) {
70
49.9k
  (void)ctxt;
71
49.9k
  free(buf);
72
49.9k
}
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
100
819
Codec::Codec(IV_COLOR_FORMAT_T colorFormat, size_t numCores) {
101
819
  mColorFormat = colorFormat;
102
819
  mNumCores = numCores;
103
819
  mCodec = nullptr;
104
819
  mWidth = 0;
105
819
  mHeight = 0;
106
107
819
  memset(&mOutBufHandle, 0, sizeof(mOutBufHandle));
108
819
}
109
110
7.84k
Codec::~Codec() {}
111
112
819
void Codec::createCodec() {
113
819
  IV_API_CALL_STATUS_T ret;
114
819
  ihevcd_cxa_create_ip_t create_ip{};
115
819
  ihevcd_cxa_create_op_t create_op{};
116
819
  void *fxns = (void *)&ivd_api_function;
117
118
819
  create_ip.s_ivd_create_ip_t.e_cmd = IVD_CMD_CREATE;
119
819
  create_ip.s_ivd_create_ip_t.u4_share_disp_buf = 0;
120
819
  create_ip.s_ivd_create_ip_t.e_output_format = mColorFormat;
121
819
  create_ip.u4_keep_threads_active = 1;
122
819
  create_ip.s_ivd_create_ip_t.pf_aligned_alloc = iv_aligned_malloc;
123
819
  create_ip.s_ivd_create_ip_t.pf_aligned_free = iv_aligned_free;
124
819
  create_ip.s_ivd_create_ip_t.pv_mem_ctxt = NULL;
125
819
  create_ip.s_ivd_create_ip_t.u4_size = sizeof(ihevcd_cxa_create_ip_t);
126
819
  create_op.s_ivd_create_op_t.u4_size = sizeof(ihevcd_cxa_create_op_t);
127
128
819
  ret = ivd_api_function(NULL, (void *)&create_ip, (void *)&create_op);
129
819
  if (ret != IV_SUCCESS) {
130
3
    return;
131
3
  }
132
816
  mCodec = (iv_obj_t *)create_op.s_ivd_create_op_t.pv_handle;
133
816
  mCodec->pv_fxns = fxns;
134
816
  mCodec->u4_size = sizeof(iv_obj_t);
135
816
}
136
137
819
void Codec::deleteCodec() {
138
819
  ivd_delete_ip_t delete_ip{};
139
819
  ivd_delete_op_t delete_op{};
140
141
819
  delete_ip.e_cmd = IVD_CMD_DELETE;
142
819
  delete_ip.u4_size = sizeof(ivd_delete_ip_t);
143
819
  delete_op.u4_size = sizeof(ivd_delete_op_t);
144
145
819
  ivd_api_function(mCodec, (void *)&delete_ip, (void *)&delete_op);
146
819
}
147
148
1.18k
void Codec::resetCodec() {
149
1.18k
  ivd_ctl_reset_ip_t s_ctl_ip{};
150
1.18k
  ivd_ctl_reset_op_t s_ctl_op{};
151
152
1.18k
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
153
1.18k
  s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_RESET;
154
1.18k
  s_ctl_ip.u4_size = sizeof(ivd_ctl_reset_ip_t);
155
1.18k
  s_ctl_op.u4_size = sizeof(ivd_ctl_reset_op_t);
156
157
1.18k
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
158
1.18k
}
159
160
819
void Codec::setCores() {
161
819
  ihevcd_cxa_ctl_set_num_cores_ip_t s_ctl_ip{};
162
819
  ihevcd_cxa_ctl_set_num_cores_op_t s_ctl_op{};
163
164
819
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
165
819
  s_ctl_ip.e_sub_cmd =
166
819
      (IVD_CONTROL_API_COMMAND_TYPE_T)IHEVCD_CXA_CMD_CTL_SET_NUM_CORES;
167
819
  s_ctl_ip.u4_num_cores = mNumCores;
168
819
  s_ctl_ip.u4_size = sizeof(ihevcd_cxa_ctl_set_num_cores_ip_t);
169
819
  s_ctl_op.u4_size = sizeof(ihevcd_cxa_ctl_set_num_cores_op_t);
170
171
819
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
172
819
}
173
174
1.63k
void Codec::setParams(IVD_VIDEO_DECODE_MODE_T mode) {
175
1.63k
  ivd_ctl_set_config_ip_t s_ctl_ip{};
176
1.63k
  ivd_ctl_set_config_op_t s_ctl_op{};
177
178
1.63k
  s_ctl_ip.u4_disp_wd = 0;
179
1.63k
  s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
180
1.63k
  s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
181
1.63k
  s_ctl_ip.e_vid_dec_mode = mode;
182
1.63k
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
183
1.63k
  s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
184
1.63k
  s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
185
1.63k
  s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);
186
187
1.63k
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
188
1.63k
}
189
190
819
void Codec::setArchitecture(IVD_ARCH_T arch) {
191
819
  ihevcd_cxa_ctl_set_processor_ip_t s_ctl_ip{};
192
819
  ihevcd_cxa_ctl_set_processor_op_t s_ctl_op{};
193
194
819
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
195
819
  s_ctl_ip.e_sub_cmd =
196
819
      (IVD_CONTROL_API_COMMAND_TYPE_T)IHEVCD_CXA_CMD_CTL_SET_PROCESSOR;
197
819
  s_ctl_ip.u4_arch = arch;
198
819
  s_ctl_ip.u4_soc = SOC_GENERIC;
199
819
  s_ctl_ip.u4_size = sizeof(ihevcd_cxa_ctl_set_processor_ip_t);
200
819
  s_ctl_op.u4_size = sizeof(ihevcd_cxa_ctl_set_processor_op_t);
201
202
819
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
203
819
}
204
2.42k
void Codec::freeFrame() {
205
5.77k
  for (int i = 0; i < mOutBufHandle.u4_num_bufs; i++) {
206
3.34k
    if (mOutBufHandle.pu1_bufs[i]) {
207
3.34k
      free(mOutBufHandle.pu1_bufs[i]);
208
3.34k
      mOutBufHandle.pu1_bufs[i] = nullptr;
209
3.34k
    }
210
3.34k
  }
211
2.42k
}
212
213
1.60k
void Codec::allocFrame() {
214
1.60k
  size_t sizes[4] = {0};
215
1.60k
  size_t num_bufs = 0;
216
217
1.60k
  freeFrame();
218
219
1.60k
  memset(&mOutBufHandle, 0, sizeof(mOutBufHandle));
220
221
1.60k
  switch (mColorFormat) {
222
167
    case IV_YUV_420SP_UV:
223
167
      [[fallthrough]];
224
482
    case IV_YUV_420SP_VU:
225
482
      sizes[0] = mWidth * mHeight;
226
482
      sizes[1] = mWidth * mHeight >> 1;
227
482
      num_bufs = 2;
228
482
      break;
229
112
    case IV_YUV_422ILE:
230
112
      sizes[0] = mWidth * mHeight * 2;
231
112
      num_bufs = 1;
232
112
      break;
233
378
    case IV_RGB_565:
234
378
      sizes[0] = mWidth * mHeight * 2;
235
378
      num_bufs = 1;
236
378
      break;
237
3
    case IV_RGBA_8888:
238
3
      sizes[0] = mWidth * mHeight * 4;
239
3
      num_bufs = 1;
240
3
      break;
241
630
    case IV_YUV_420P:
242
630
      [[fallthrough]];
243
630
    default:
244
630
      sizes[0] = mWidth * mHeight;
245
630
      sizes[1] = mWidth * mHeight >> 2;
246
630
      sizes[2] = mWidth * mHeight >> 2;
247
630
      num_bufs = 3;
248
630
      break;
249
1.60k
  }
250
1.60k
  mOutBufHandle.u4_num_bufs = num_bufs;
251
4.95k
  for (int i = 0; i < num_bufs; i++) {
252
3.34k
    mOutBufHandle.u4_min_out_buf_size[i] = sizes[i];
253
3.34k
    mOutBufHandle.pu1_bufs[i] = (UWORD8 *)iv_aligned_malloc(NULL, 16, sizes[i]);
254
3.34k
  }
255
1.60k
}
256
257
819
void Codec::decodeHeader(const uint8_t *data, size_t size) {
258
819
  setParams(IVD_DECODE_HEADER);
259
260
819
  size_t numDecodeCalls = 0;
261
262
30.5k
  while (size > 0 && numDecodeCalls < kMaxNumDecodeCalls) {
263
30.5k
    IV_API_CALL_STATUS_T ret;
264
30.5k
    ivd_video_decode_ip_t dec_ip{};
265
30.5k
    ivd_video_decode_op_t dec_op{};
266
30.5k
    size_t bytes_consumed;
267
268
30.5k
    memset(&dec_ip, 0, sizeof(dec_ip));
269
30.5k
    memset(&dec_op, 0, sizeof(dec_op));
270
271
30.5k
    dec_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
272
30.5k
    dec_ip.u4_ts = 0;
273
30.5k
    dec_ip.pv_stream_buffer = (void *)data;
274
30.5k
    dec_ip.u4_num_Bytes = size;
275
30.5k
    dec_ip.u4_size = sizeof(ivd_video_decode_ip_t);
276
30.5k
    dec_op.u4_size = sizeof(ivd_video_decode_op_t);
277
278
30.5k
    ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
279
280
30.5k
    bytes_consumed = dec_op.u4_num_bytes_consumed;
281
    /* If no bytes are consumed, then consume 4 bytes to ensure fuzzer proceeds
282
     * to feed next data */
283
30.5k
    if (!bytes_consumed) bytes_consumed = 4;
284
285
30.5k
    bytes_consumed = std::min(size, bytes_consumed);
286
287
30.5k
    data += bytes_consumed;
288
30.5k
    size -= bytes_consumed;
289
30.5k
    numDecodeCalls++;
290
291
30.5k
    mWidth = std::min(dec_op.u4_pic_wd, (UWORD32)10240);
292
30.5k
    mHeight = std::min(dec_op.u4_pic_ht, (UWORD32)10240);
293
294
    /* Break after successful header decode */
295
30.5k
    if (mWidth && mHeight) {
296
755
      break;
297
755
    }
298
30.5k
  }
299
  /* if width / height are invalid, set them to defaults */
300
819
  if (!mWidth) mWidth = 1920;
301
819
  if (!mHeight) mHeight = 1088;
302
819
}
303
304
IV_API_CALL_STATUS_T Codec::decodeFrame(const uint8_t *data, size_t size,
305
48.1k
                                        size_t *bytesConsumed) {
306
48.1k
  IV_API_CALL_STATUS_T ret;
307
48.1k
  ivd_video_decode_ip_t dec_ip{};
308
48.1k
  ivd_video_decode_op_t dec_op{};
309
310
48.1k
  memset(&dec_ip, 0, sizeof(dec_ip));
311
48.1k
  memset(&dec_op, 0, sizeof(dec_op));
312
313
48.1k
  dec_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
314
48.1k
  dec_ip.u4_ts = 0;
315
48.1k
  dec_ip.pv_stream_buffer = (void *)data;
316
48.1k
  dec_ip.u4_num_Bytes = size;
317
48.1k
  dec_ip.u4_size = sizeof(ivd_video_decode_ip_t);
318
48.1k
  dec_ip.s_out_buffer = mOutBufHandle;
319
320
48.1k
  dec_op.u4_size = sizeof(ivd_video_decode_op_t);
321
322
48.1k
  ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
323
324
  /* In case of change in resolution, reset codec and feed the same data again
325
   */
326
48.1k
  if (IVD_RES_CHANGED == (dec_op.u4_error_code & 0xFF)) {
327
1.18k
    resetCodec();
328
1.18k
    ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
329
1.18k
  }
330
48.1k
  *bytesConsumed = dec_op.u4_num_bytes_consumed;
331
332
  /* If no bytes are consumed, then consume 4 bytes to ensure fuzzer proceeds
333
   * to feed next data */
334
48.1k
  if (!*bytesConsumed) *bytesConsumed = 4;
335
336
48.1k
  if (dec_op.u4_pic_wd && dec_op.u4_pic_ht &&
337
46.2k
      (mWidth != dec_op.u4_pic_wd || mHeight != dec_op.u4_pic_ht)) {
338
786
    mWidth = std::min(dec_op.u4_pic_wd, (UWORD32)10240);
339
786
    mHeight = std::min(dec_op.u4_pic_ht, (UWORD32)10240);
340
786
    allocFrame();
341
786
  }
342
343
48.1k
  return ret;
344
48.1k
}
345
346
819
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
347
819
  if (size < 1) {
348
0
    return 0;
349
0
  }
350
819
  size_t colorFormatOfst = std::min((size_t)OFFSET_COLOR_FORMAT, size - 1);
351
819
  size_t numCoresOfst = std::min((size_t)OFFSET_NUM_CORES, size - 1);
352
819
  size_t architectureOfst = std::min((size_t)OFFSET_ARCH, size - 1);
353
819
  size_t architectureIdx = data[architectureOfst] % kSupportedArchitectures;
354
819
  IVD_ARCH_T arch = (IVD_ARCH_T)supportedArchitectures[architectureIdx];
355
819
  size_t colorFormatIdx = data[colorFormatOfst] % kSupportedColorFormats;
356
819
  IV_COLOR_FORMAT_T colorFormat =
357
819
      (IV_COLOR_FORMAT_T)(supportedColorFormats[colorFormatIdx]);
358
819
  uint32_t numCores = (data[numCoresOfst] % kMaxCores) + 1;
359
819
  size_t numDecodeCalls = 0;
360
819
  Codec *codec = new Codec(colorFormat, numCores);
361
819
  codec->createCodec();
362
819
  codec->setArchitecture(arch);
363
819
  codec->setCores();
364
819
  codec->decodeHeader(data, size);
365
819
  codec->setParams(IVD_DECODE_FRAME);
366
819
  codec->allocFrame();
367
368
49.0k
  while (size > 0 && numDecodeCalls < kMaxNumDecodeCalls) {
369
48.1k
    IV_API_CALL_STATUS_T ret;
370
48.1k
    size_t bytesConsumed;
371
48.1k
    ret = codec->decodeFrame(data, size, &bytesConsumed);
372
373
48.1k
    bytesConsumed = std::min(size, bytesConsumed);
374
48.1k
    data += bytesConsumed;
375
48.1k
    size -= bytesConsumed;
376
48.1k
    numDecodeCalls++;
377
48.1k
  }
378
379
819
  codec->freeFrame();
380
819
  codec->deleteCodec();
381
819
  delete codec;
382
819
  return 0;
383
819
}