You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015 The WebRTC 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. #include "test/fuzzers/audio_decoder_fuzzer.h"
  11. #include <limits>
  12. #include "absl/types/optional.h"
  13. #include "api/audio_codecs/audio_decoder.h"
  14. #include "modules/rtp_rtcp/source/byte_io.h"
  15. #include "rtc_base/checks.h"
  16. namespace webrtc {
  17. namespace {
  18. template <typename T, unsigned int B = sizeof(T)>
  19. bool ParseInt(const uint8_t** data, size_t* remaining_size, T* value) {
  20. static_assert(std::numeric_limits<T>::is_integer, "Type must be an integer.");
  21. static_assert(sizeof(T) <= sizeof(uint64_t),
  22. "Cannot read wider than uint64_t.");
  23. static_assert(B <= sizeof(T), "T must be at least B bytes wide.");
  24. if (B > *remaining_size)
  25. return false;
  26. uint64_t val = ByteReader<uint64_t, B>::ReadBigEndian(*data);
  27. *data += B;
  28. *remaining_size -= B;
  29. *value = static_cast<T>(val);
  30. return true;
  31. }
  32. } // namespace
  33. // This function reads two bytes from the beginning of |data|, interprets them
  34. // as the first packet length, and reads this many bytes if available. The
  35. // payload is inserted into the decoder, and the process continues until no more
  36. // data is available. Either AudioDecoder::Decode or
  37. // AudioDecoder::DecodeRedundant is used, depending on the value of
  38. // |decode_type|.
  39. void FuzzAudioDecoder(DecoderFunctionType decode_type,
  40. const uint8_t* data,
  41. size_t size,
  42. AudioDecoder* decoder,
  43. int sample_rate_hz,
  44. size_t max_decoded_bytes,
  45. int16_t* decoded) {
  46. const uint8_t* data_ptr = data;
  47. size_t remaining_size = size;
  48. size_t packet_len;
  49. constexpr size_t kMaxNumFuzzedPackets = 200;
  50. for (size_t num_packets = 0; num_packets < kMaxNumFuzzedPackets;
  51. ++num_packets) {
  52. if (!(ParseInt<size_t, 2>(&data_ptr, &remaining_size, &packet_len) &&
  53. packet_len <= remaining_size)) {
  54. break;
  55. }
  56. AudioDecoder::SpeechType speech_type;
  57. switch (decode_type) {
  58. case DecoderFunctionType::kNormalDecode:
  59. decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes,
  60. decoded, &speech_type);
  61. break;
  62. case DecoderFunctionType::kRedundantDecode:
  63. decoder->DecodeRedundant(data_ptr, packet_len, sample_rate_hz,
  64. max_decoded_bytes, decoded, &speech_type);
  65. break;
  66. }
  67. data_ptr += packet_len;
  68. remaining_size -= packet_len;
  69. }
  70. }
  71. } // namespace webrtc