Coverage Report

Created: 2025-09-27 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/decoder_fuzzer.cpp
Line
Count
Source
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
22.3k
{
43
22.3k
  int32_t i;
44
22.3k
  int32_t iBufPos = 0;
45
22.3k
  int32_t iEndOfStreamFlag;
46
22.3k
  int iLevelSetting = (int) WELS_LOG_QUIET; // disable logging while fuzzing
47
22.3k
  int32_t iSliceSize;
48
22.3k
  ISVCDecoder *pDecoder;
49
22.3k
  SDecodingParam sDecParam = {0};
50
22.3k
  SBufferInfo sDstBufInfo;
51
22.3k
  std::unique_ptr<uint8_t[]> pBuf(new uint8_t[size + 4]);
52
22.3k
  uint8_t* pData[3] = {NULL};
53
22.3k
  uint8_t uiStartCode[4] = {0, 0, 0, 1};
54
55
22.3k
  memcpy(pBuf.get(), data, size);
56
22.3k
  memcpy(pBuf.get() + size, &uiStartCode[0], 4);
57
22.3k
  memset(&sDstBufInfo, 0, sizeof(SBufferInfo));
58
59
  // TODO: is this the best/fastest ERROR_CON to use?
60
22.3k
  sDecParam.eEcActiveIdc = ERROR_CON_SLICE_COPY;
61
  // TODO: should we also fuzz VIDEO_BITSTREAM_SVC?
62
22.3k
  sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
63
  
64
22.3k
  WelsCreateDecoder (&pDecoder);
65
22.3k
  pDecoder->Initialize (&sDecParam);
66
22.3k
  pDecoder->SetOption (DECODER_OPTION_TRACE_LEVEL, &iLevelSetting);
67
68
6.18M
  while (1) {
69
6.18M
    if (iBufPos >= size) {
70
22.3k
      iEndOfStreamFlag = 1;
71
22.3k
      if (iEndOfStreamFlag)
72
22.3k
        pDecoder->SetOption (DECODER_OPTION_END_OF_STREAM, (void*)&iEndOfStreamFlag);
73
22.3k
      break;
74
22.3k
    }
75
76
102M
    for (i = 0; i < size; i++) {
77
102M
      if ((pBuf[iBufPos + i] == 0 && pBuf[iBufPos + i + 1] == 0 && pBuf[iBufPos + i + 2] == 0 && pBuf[iBufPos + i + 3] == 1
78
102M
          && i > 0) || (pBuf[iBufPos + i] == 0 && pBuf[iBufPos + i + 1] == 0 && pBuf[iBufPos + i + 2] == 1 && i > 0)) {
79
6.16M
        break;
80
6.16M
      }
81
102M
    }
82
6.16M
    iSliceSize = i;
83
6.16M
    if (iSliceSize < 4) {
84
152k
      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
152k
      iBufPos += iSliceSize;
89
152k
      continue;
90
152k
    }
91
92
6.01M
    pDecoder->DecodeFrameNoDelay (pBuf.get() + iBufPos, iSliceSize, pData, &sDstBufInfo);
93
6.01M
    iBufPos += iSliceSize;
94
6.01M
  }
95
96
22.3k
label_cleanup:
97
22.3k
  pDecoder->Uninitialize ();
98
22.3k
  WelsDestroyDecoder (pDecoder);
99
100
22.3k
  return 0;
101
22.3k
}