Coverage Report

Created: 2025-07-11 07:02

/src/decoder_fuzzer.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
# Copyright 2021 Google LLC
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#      http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
#
16
################################################################################
17
*/
18
// TODO: This should be moved to the openh264 repo.
19
20
#include <stddef.h>
21
#include <stdint.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include <memory>
27
28
#include "codec_def.h"
29
#include "codec_app_def.h"
30
#include "codec_api.h"
31
#include "read_config.h"
32
#include "typedefs.h"
33
#include "measure_time.h"
34
35
/*
36
 * To build locally:
37
 * CC=clang CXX=clang++ CFLAGS="-fsanitize=address,fuzzer-no-link -g" CXXFLAGS="-fsanitize=address,fuzzer-no-link -g" LDFLAGS="-fsanitize=address,fuzzer-no-link" make -j$(nproc) USE_ASM=No BUILDTYPE=Debug libraries
38
 * clang++ -o decoder_fuzzer -fsanitize=address -g -O1 -I./codec/api/wels -I./codec/console/common/inc -I./codec/common/inc -L. -lFuzzer -lstdc++ decoder_fuzzer.cpp libopenh264.a
39
 */
40
41
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
42
23.8k
{
43
23.8k
  int32_t i;
44
23.8k
  int32_t iBufPos = 0;
45
23.8k
  int32_t iEndOfStreamFlag;
46
23.8k
  int iLevelSetting = (int) WELS_LOG_QUIET; // disable logging while fuzzing
47
23.8k
  int32_t iSliceSize;
48
23.8k
  ISVCDecoder *pDecoder;
49
23.8k
  SDecodingParam sDecParam = {0};
50
23.8k
  SBufferInfo sDstBufInfo;
51
23.8k
  std::unique_ptr<uint8_t[]> pBuf(new uint8_t[size + 4]);
52
23.8k
  uint8_t* pData[3] = {NULL};
53
23.8k
  uint8_t uiStartCode[4] = {0, 0, 0, 1};
54
55
23.8k
  memcpy(pBuf.get(), data, size);
56
23.8k
  memcpy(pBuf.get() + size, &uiStartCode[0], 4);
57
23.8k
  memset(&sDstBufInfo, 0, sizeof(SBufferInfo));
58
59
  // TODO: is this the best/fastest ERROR_CON to use?
60
23.8k
  sDecParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
61
  // TODO: should we also fuzz VIDEO_BITSTREAM_SVC?
62
23.8k
  sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
63
  
64
23.8k
  WelsCreateDecoder (&pDecoder);
65
23.8k
  pDecoder->Initialize (&sDecParam);
66
23.8k
  pDecoder->SetOption (DECODER_OPTION_TRACE_LEVEL, &iLevelSetting);
67
68
6.69M
  while (1) {
69
6.69M
    if (iBufPos >= size) {
70
23.8k
      iEndOfStreamFlag = 1;
71
23.8k
      if (iEndOfStreamFlag)
72
23.8k
        pDecoder->SetOption (DECODER_OPTION_END_OF_STREAM, (void*)&iEndOfStreamFlag);
73
23.8k
      break;
74
23.8k
    }
75
76
110M
    for (i = 0; i < size; i++) {
77
110M
      if ((pBuf[iBufPos + i] == 0 && pBuf[iBufPos + i + 1] == 0 && pBuf[iBufPos + i + 2] == 0 && pBuf[iBufPos + i + 3] == 1
78
110M
          && i > 0) || (pBuf[iBufPos + i] == 0 && pBuf[iBufPos + i + 1] == 0 && pBuf[iBufPos + i + 2] == 1 && i > 0)) {
79
6.66M
        break;
80
6.66M
      }
81
110M
    }
82
6.66M
    iSliceSize = i;
83
6.66M
    if (iSliceSize < 4) {
84
179k
      if (iSliceSize == 0) {
85
        // I don't think this should happen but let's just avoid the hang
86
0
        goto label_cleanup;
87
0
      }
88
179k
      iBufPos += iSliceSize;
89
179k
      continue;
90
179k
    }
91
92
6.48M
    pDecoder->DecodeFrameNoDelay (pBuf.get() + iBufPos, iSliceSize, pData, &sDstBufInfo);
93
6.48M
    iBufPos += iSliceSize;
94
6.48M
  }
95
96
23.8k
label_cleanup:
97
23.8k
  pDecoder->Uninitialize ();
98
23.8k
  WelsDestroyDecoder (pDecoder);
99
100
23.8k
  return 0;
101
23.8k
}