vpx_dec_fuzzer.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2018 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. /*
  11. * Fuzzer for libvpx decoders
  12. * ==========================
  13. * Requirements
  14. * --------------
  15. * Requires Clang 6.0 or above as -fsanitize=fuzzer is used as a linker
  16. * option.
  17. * Steps to build
  18. * --------------
  19. * Clone libvpx repository
  20. $git clone https://chromium.googlesource.com/webm/libvpx
  21. * Create a directory in parallel to libvpx and change directory
  22. $mkdir vpx_dec_fuzzer
  23. $cd vpx_dec_fuzzer/
  24. * Enable sanitizers (Supported: address integer memory thread undefined)
  25. $source ../libvpx/tools/set_analyzer_env.sh address
  26. * Configure libvpx.
  27. * Note --size-limit and VPX_MAX_ALLOCABLE_MEMORY are defined to avoid
  28. * Out of memory errors when running generated fuzzer binary
  29. $../libvpx/configure --disable-unit-tests --size-limit=12288x12288 \
  30. --extra-cflags="-fsanitize=fuzzer-no-link \
  31. -DVPX_MAX_ALLOCABLE_MEMORY=1073741824" \
  32. --disable-webm-io --enable-debug --disable-vp8-encoder \
  33. --disable-vp9-encoder --disable-examples
  34. * Build libvpx
  35. $make -j32
  36. * Build vp9 fuzzer
  37. $ $CXX $CXXFLAGS -std=c++11 -DDECODER=vp9 \
  38. -fsanitize=fuzzer -I../libvpx -I. -Wl,--start-group \
  39. ../libvpx/examples/vpx_dec_fuzzer.cc -o ./vpx_dec_fuzzer_vp9 \
  40. ./libvpx.a -Wl,--end-group
  41. * DECODER should be defined as vp9 or vp8 to enable vp9/vp8
  42. *
  43. * create a corpus directory and copy some ivf files there.
  44. * Based on which codec (vp8/vp9) is being tested, it is recommended to
  45. * have corresponding ivf files in corpus directory
  46. * Empty corpus directoy also is acceptable, though not recommended
  47. $mkdir CORPUS && cp some-files CORPUS
  48. * Run fuzzing:
  49. $./vpx_dec_fuzzer_vp9 CORPUS
  50. * References:
  51. * http://llvm.org/docs/LibFuzzer.html
  52. * https://github.com/google/oss-fuzz
  53. */
  54. #include <stddef.h>
  55. #include <stdint.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <algorithm>
  59. #include <memory>
  60. #include "vpx/vp8dx.h"
  61. #include "vpx/vpx_decoder.h"
  62. #include "vpx_ports/mem_ops.h"
  63. #define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
  64. #define IVF_FILE_HDR_SZ 32
  65. #define VPXD_INTERFACE(name) VPXD_INTERFACE_(name)
  66. #define VPXD_INTERFACE_(name) vpx_codec_##name##_dx()
  67. extern "C" void usage_exit(void) { exit(EXIT_FAILURE); }
  68. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  69. if (size <= IVF_FILE_HDR_SZ) {
  70. return 0;
  71. }
  72. vpx_codec_ctx_t codec;
  73. // Set thread count in the range [1, 64].
  74. const unsigned int threads = (data[IVF_FILE_HDR_SZ] & 0x3f) + 1;
  75. vpx_codec_dec_cfg_t cfg = { threads, 0, 0 };
  76. if (vpx_codec_dec_init(&codec, VPXD_INTERFACE(DECODER), &cfg, 0)) {
  77. return 0;
  78. }
  79. data += IVF_FILE_HDR_SZ;
  80. size -= IVF_FILE_HDR_SZ;
  81. while (size > IVF_FRAME_HDR_SZ) {
  82. size_t frame_size = mem_get_le32(data);
  83. size -= IVF_FRAME_HDR_SZ;
  84. data += IVF_FRAME_HDR_SZ;
  85. frame_size = std::min(size, frame_size);
  86. const vpx_codec_err_t err =
  87. vpx_codec_decode(&codec, data, frame_size, nullptr, 0);
  88. static_cast<void>(err);
  89. vpx_codec_iter_t iter = nullptr;
  90. vpx_image_t *img = nullptr;
  91. while ((img = vpx_codec_get_frame(&codec, &iter)) != nullptr) {
  92. }
  93. data += frame_size;
  94. size -= frame_size;
  95. }
  96. vpx_codec_destroy(&codec);
  97. return 0;
  98. }