Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/test/fuzzers/audio_decoder_fuzzer.h" | 11 #include "webrtc/test/fuzzers/audio_decoder_fuzzer.h" |
| 12 | 12 |
| 13 #include <limits> | |
| 14 | |
| 13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/optional.h" | |
| 14 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" | 17 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
| 18 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
| 15 | 19 |
| 16 namespace webrtc { | 20 namespace webrtc { |
| 17 namespace { | 21 namespace { |
| 18 size_t PacketSizeFromTwoBytes(const uint8_t* data, size_t size) { | 22 size_t PacketSizeFromTwoBytes(const uint8_t* data, size_t size) { |
|
pbos-webrtc
2016/01/25 13:53:07
Imo. remove this function and do as below:
hlundin-webrtc
2016/01/25 15:47:09
Done.
| |
| 19 if (size < 2) | 23 return size < 2 ? 0 : static_cast<size_t>( |
| 20 return 0; | 24 ByteReader<uint64_t, 2>::ReadBigEndian(data)); |
| 21 return static_cast<size_t>((data[0] << 8) + data[1]); | 25 } |
| 26 | |
| 27 template <typename T> | |
| 28 T ReadIntCheckedAndIncrement(const uint8_t** data, size_t* size) { | |
|
pbos-webrtc
2016/01/25 13:53:07
bool ParseInt(T* int, size_t size, const uint8_t**
hlundin-webrtc
2016/01/25 15:47:09
Done, except that I omitted your second argument (
hlundin-webrtc
2016/01/26 13:01:55
And when I say "omitted" I mean "moved it to be a
| |
| 29 static_assert(std::numeric_limits<T>::is_integer, "Type must be an integer."); | |
| 30 static_assert(sizeof(T) <= sizeof(uint64_t), | |
| 31 "Cannot read wider than uint64_t."); | |
| 32 const size_t N = sizeof(T); | |
| 33 RTC_DCHECK_LE(N, *size); | |
| 34 uint64_t val = ByteReader<uint64_t, sizeof(T)>::ReadBigEndian(*data); | |
| 35 *data += N; | |
| 36 RTC_DCHECK_GE(*size, N); // Make extra sure we don't wrap *size. | |
| 37 *size -= N; | |
| 38 return static_cast<T>(val); | |
| 22 } | 39 } |
| 23 } // namespace | 40 } // namespace |
| 24 | 41 |
| 25 // This function reads two bytes from the beginning of |data|, interprets them | 42 // This function reads two bytes from the beginning of |data|, interprets them |
| 26 // as the first packet length, and reads this many bytes if available. The | 43 // as the first packet length, and reads this many bytes if available. The |
| 27 // payload is inserted into the decoder, and the process continues until no more | 44 // payload is inserted into the decoder, and the process continues until no more |
| 28 // data is available. | 45 // data is available. |
| 29 void FuzzAudioDecoder(const uint8_t* data, | 46 void FuzzAudioDecoder(const uint8_t* data, |
| 30 size_t size, | 47 size_t size, |
| 31 AudioDecoder* decoder, | 48 AudioDecoder* decoder, |
| 32 int sample_rate_hz, | 49 int sample_rate_hz, |
| 33 size_t max_decoded_bytes, | 50 size_t max_decoded_bytes, |
| 34 int16_t* decoded) { | 51 int16_t* decoded) { |
| 35 const uint8_t* data_ptr = data; | 52 const uint8_t* data_ptr = data; |
| 36 size_t remaining_size = size; | 53 size_t remaining_size = size; |
| 37 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | 54 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); |
| 38 while (packet_len != 0 && packet_len <= remaining_size - 2) { | 55 while (packet_len != 0 && packet_len + 2 <= remaining_size) { |
|
pbos-webrtc
2016/01/25 13:53:07
while (ParseInt(&packet_len, 2, data_ptr, &remaini
hlundin-webrtc
2016/01/25 15:47:09
Done.
| |
| 39 data_ptr += 2; | 56 data_ptr += 2; |
| 40 remaining_size -= 2; | 57 remaining_size -= 2; |
| 41 AudioDecoder::SpeechType speech_type; | 58 AudioDecoder::SpeechType speech_type; |
| 42 decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes, | 59 decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes, |
| 43 decoded, &speech_type); | 60 decoded, &speech_type); |
| 44 data_ptr += packet_len; | 61 data_ptr += packet_len; |
| 45 remaining_size -= packet_len; | 62 remaining_size -= packet_len; |
| 46 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | 63 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); |
| 47 } | 64 } |
| 48 } | 65 } |
| 66 | |
| 67 // This function is identical to FuzzAudioDecoder above, with the distinction | |
| 68 // that it call DecodeRedundant instead of Decode. | |
| 69 void FuzzAudioDecoderRedundant(const uint8_t* data, | |
| 70 size_t size, | |
| 71 AudioDecoder* decoder, | |
| 72 int sample_rate_hz, | |
| 73 size_t max_decoded_bytes, | |
| 74 int16_t* decoded) { | |
| 75 const uint8_t* data_ptr = data; | |
| 76 size_t remaining_size = size; | |
| 77 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | |
| 78 while (packet_len != 0 && packet_len + 2 <= remaining_size) { | |
| 79 data_ptr += 2; | |
| 80 remaining_size -= 2; | |
| 81 AudioDecoder::SpeechType speech_type; | |
| 82 decoder->DecodeRedundant(data_ptr, packet_len, sample_rate_hz, | |
| 83 max_decoded_bytes, decoded, &speech_type); | |
| 84 data_ptr += packet_len; | |
| 85 remaining_size -= packet_len; | |
| 86 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 // This function is similar to FuzzAudioDecoder, but also reads fuzzed data into | |
| 91 // RTP header values. The fuzzed data and values are sent to the decoder's | |
| 92 // IncomingPacket method. | |
| 93 void FuzzAudioDecoderIncomingPacket(const uint8_t* data, | |
| 94 size_t size, | |
| 95 AudioDecoder* decoder) { | |
| 96 const uint8_t* data_ptr = data; | |
| 97 size_t remaining_size = size; | |
| 98 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | |
| 99 // Sum length of rtp_sequence_number, rtp_timestamp, and arrival_timestamp. | |
| 100 const size_t header_len = sizeof(uint16_t) + 2 * sizeof(uint32_t); | |
|
pbos-webrtc
2016/01/25 13:53:07
kHeaderLength
hlundin-webrtc
2016/01/25 15:47:09
Done.
| |
| 101 while (packet_len != 0 && packet_len + 2 + header_len <= remaining_size) { | |
| 102 data_ptr += 2; | |
| 103 remaining_size -= 2; | |
| 104 const uint16_t rtp_sequence_number = | |
| 105 ReadIntCheckedAndIncrement<uint16_t>(&data_ptr, &remaining_size); | |
|
pbos-webrtc
2016/01/25 13:53:07
if (!ParseInt).. (break; or rtp_sequence_number =
hlundin-webrtc
2016/01/25 15:47:09
Done.
| |
| 106 const uint32_t rtp_timestamp = | |
| 107 ReadIntCheckedAndIncrement<uint32_t>(&data_ptr, &remaining_size); | |
| 108 const uint32_t arrival_timestamp = | |
| 109 ReadIntCheckedAndIncrement<uint32_t>(&data_ptr, &remaining_size); | |
| 110 decoder->IncomingPacket(data_ptr, packet_len, rtp_sequence_number, | |
| 111 rtp_timestamp, arrival_timestamp); | |
| 112 data_ptr += packet_len; | |
| 113 remaining_size -= packet_len; | |
| 114 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | |
| 115 } | |
| 116 } | |
| 49 } // namespace webrtc | 117 } // namespace webrtc |
| OLD | NEW |