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 template <typename T, unsigned int B = sizeof(T)> |
19 if (size < 2) | 23 bool ParseInt(const uint8_t** data, size_t* remaining_size, T* value) { |
20 return 0; | 24 static_assert(std::numeric_limits<T>::is_integer, "Type must be an integer."); |
21 return static_cast<size_t>((data[0] << 8) + data[1]); | 25 static_assert(sizeof(T) <= sizeof(uint64_t), |
26 "Cannot read wider than uint64_t."); | |
27 static_assert(B <= sizeof(T), "T must be at least B bytes wide."); | |
28 if (B > *remaining_size) | |
29 return false; | |
30 uint64_t val = ByteReader<uint64_t, B>::ReadBigEndian(*data); | |
31 *data += B; | |
32 *remaining_size -= B; | |
33 *value = static_cast<T>(val); | |
34 return true; | |
22 } | 35 } |
23 } // namespace | 36 } // namespace |
24 | 37 |
25 // This function reads two bytes from the beginning of |data|, interprets them | 38 // 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 | 39 // 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 | 40 // payload is inserted into the decoder, and the process continues until no more |
28 // data is available. | 41 // data is available. |
29 void FuzzAudioDecoder(const uint8_t* data, | 42 void FuzzAudioDecoder(const uint8_t* data, |
30 size_t size, | 43 size_t size, |
31 AudioDecoder* decoder, | 44 AudioDecoder* decoder, |
32 int sample_rate_hz, | 45 int sample_rate_hz, |
33 size_t max_decoded_bytes, | 46 size_t max_decoded_bytes, |
34 int16_t* decoded) { | 47 int16_t* decoded) { |
35 const uint8_t* data_ptr = data; | 48 const uint8_t* data_ptr = data; |
36 size_t remaining_size = size; | 49 size_t remaining_size = size; |
37 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | 50 size_t packet_len; |
38 while (packet_len != 0 && packet_len <= remaining_size - 2) { | 51 while (ParseInt<size_t, 2>(&data_ptr, &remaining_size, &packet_len) && |
39 data_ptr += 2; | 52 packet_len <= remaining_size) { |
40 remaining_size -= 2; | |
41 AudioDecoder::SpeechType speech_type; | 53 AudioDecoder::SpeechType speech_type; |
42 decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes, | 54 decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes, |
43 decoded, &speech_type); | 55 decoded, &speech_type); |
44 data_ptr += packet_len; | 56 data_ptr += packet_len; |
45 remaining_size -= packet_len; | 57 remaining_size -= packet_len; |
46 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); | 58 } |
59 } | |
60 | |
61 // This function is identical to FuzzAudioDecoder above, with the distinction | |
62 // that it call DecodeRedundant instead of Decode. | |
pbos-webrtc
2016/01/26 14:18:12
Can you refactor this into one method with an enum
hlundin-webrtc
2016/01/26 22:32:31
Done.
| |
63 void FuzzAudioDecoderRedundant(const uint8_t* data, | |
64 size_t size, | |
65 AudioDecoder* decoder, | |
66 int sample_rate_hz, | |
67 size_t max_decoded_bytes, | |
68 int16_t* decoded) { | |
69 const uint8_t* data_ptr = data; | |
70 size_t remaining_size = size; | |
71 size_t packet_len; | |
72 while (ParseInt<size_t, 2>(&data_ptr, &remaining_size, &packet_len) && | |
73 packet_len <= remaining_size) { | |
74 AudioDecoder::SpeechType speech_type; | |
75 decoder->DecodeRedundant(data_ptr, packet_len, sample_rate_hz, | |
76 max_decoded_bytes, decoded, &speech_type); | |
77 data_ptr += packet_len; | |
78 remaining_size -= packet_len; | |
79 } | |
80 } | |
81 | |
82 // This function is similar to FuzzAudioDecoder, but also reads fuzzed data into | |
pbos-webrtc
2016/01/26 14:18:12
Same as above if possible.
pbos-webrtc
2016/01/26 14:19:08
Not very possible, ignore this. :)
hlundin-webrtc
2016/01/26 22:32:31
Acknowledged.
| |
83 // RTP header values. The fuzzed data and values are sent to the decoder's | |
84 // IncomingPacket method. | |
85 void FuzzAudioDecoderIncomingPacket(const uint8_t* data, | |
86 size_t size, | |
87 AudioDecoder* decoder) { | |
88 const uint8_t* data_ptr = data; | |
89 size_t remaining_size = size; | |
90 size_t packet_len; | |
91 while (ParseInt<size_t, 2>(&data_ptr, &remaining_size, &packet_len)) { | |
92 uint16_t rtp_sequence_number; | |
93 if (!ParseInt(&data_ptr, &remaining_size, &rtp_sequence_number)) | |
94 break; | |
95 uint32_t rtp_timestamp; | |
96 if (!ParseInt(&data_ptr, &remaining_size, &rtp_timestamp)) | |
97 break; | |
98 uint32_t arrival_timestamp; | |
99 if (!ParseInt(&data_ptr, &remaining_size, &arrival_timestamp)) | |
100 break; | |
101 if (remaining_size < packet_len) | |
102 break; | |
103 decoder->IncomingPacket(data_ptr, packet_len, rtp_sequence_number, | |
104 rtp_timestamp, arrival_timestamp); | |
105 data_ptr += packet_len; | |
106 remaining_size -= packet_len; | |
47 } | 107 } |
48 } | 108 } |
49 } // namespace webrtc | 109 } // namespace webrtc |
OLD | NEW |