Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: webrtc/test/fuzzers/audio_decoder_fuzzer.cc

Issue 1607173003: Fuzzer tests for AudioDecoder's DecodeRedundant and IncomingPacket (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Updates after review Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
15 18
16 namespace webrtc { 19 namespace webrtc {
17 namespace { 20 namespace {
21 rtc::Optional<uint64_t> ParseInt(const uint8_t* data, size_t size, size_t N) {
22 RTC_CHECK_LE(N, sizeof(uint64_t));
23 RTC_CHECK_GT(N, static_cast<size_t>(0));
24 if (size < N)
25 return rtc::Optional<uint64_t>();
26 uint64_t val = 0;
pbos-webrtc 2016/01/21 15:31:24 Actually can you use webrtc::ByteReader instead? J
hlundin-webrtc 2016/01/22 13:58:56 That works. Except there is a bug in ByteReader. F
27 for (size_t i = 0; i < N; ++i) {
28 val = (val << 8) + data[i];
29 }
30 return rtc::Optional<uint64_t>(val);
31 }
32
18 size_t PacketSizeFromTwoBytes(const uint8_t* data, size_t size) { 33 size_t PacketSizeFromTwoBytes(const uint8_t* data, size_t size) {
19 if (size < 2) 34 rtc::Optional<uint64_t> val = ParseInt(data, size, 2);
20 return 0; 35 return val ? static_cast<size_t>(*val) : 0;
21 return static_cast<size_t>((data[0] << 8) + data[1]); 36 }
37
38 template <typename T>
39 T ReadIntCheckedAndIncrement(const uint8_t** data, size_t* size) {
40 static_assert(std::numeric_limits<T>::is_integer, "Type must be an integer.");
41 static_assert(sizeof(T) <= sizeof(uint64_t),
42 "Cannot read wider than uint64_t.");
43 const size_t N = sizeof(T);
44 rtc::Optional<uint64_t> val = ParseInt(*data, *size, N);
45 RTC_CHECK(val);
46 *data += N;
47 RTC_DCHECK_GE(*size, N); // Make extra sure we don't wrap *size.
48 *size -= N;
49 return static_cast<T>(*val);
22 } 50 }
23 } // namespace 51 } // namespace
24 52
25 // This function reads two bytes from the beginning of |data|, interprets them 53 // 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 54 // 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 55 // payload is inserted into the decoder, and the process continues until no more
28 // data is available. 56 // data is available.
29 void FuzzAudioDecoder(const uint8_t* data, 57 void FuzzAudioDecoder(const uint8_t* data,
30 size_t size, 58 size_t size,
31 AudioDecoder* decoder, 59 AudioDecoder* decoder,
32 int sample_rate_hz, 60 int sample_rate_hz,
33 size_t max_decoded_bytes, 61 size_t max_decoded_bytes,
34 int16_t* decoded) { 62 int16_t* decoded) {
35 const uint8_t* data_ptr = data; 63 const uint8_t* data_ptr = data;
36 size_t remaining_size = size; 64 size_t remaining_size = size;
37 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); 65 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size);
38 while (packet_len != 0 && packet_len <= remaining_size - 2) { 66 while (packet_len != 0 && packet_len + 2 <= remaining_size) {
39 data_ptr += 2; 67 data_ptr += 2;
40 remaining_size -= 2; 68 remaining_size -= 2;
41 AudioDecoder::SpeechType speech_type; 69 AudioDecoder::SpeechType speech_type;
42 decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes, 70 decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes,
43 decoded, &speech_type); 71 decoded, &speech_type);
44 data_ptr += packet_len; 72 data_ptr += packet_len;
45 remaining_size -= packet_len; 73 remaining_size -= packet_len;
46 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); 74 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size);
47 } 75 }
48 } 76 }
77
78 // This function is identical to FuzzAudioDecoder above, with the distinction
79 // that it call DecodeRedundant instead of Decode.
80 void FuzzAudioDecoderRedundant(const uint8_t* data,
81 size_t size,
82 AudioDecoder* decoder,
83 int sample_rate_hz,
84 size_t max_decoded_bytes,
85 int16_t* decoded) {
86 const uint8_t* data_ptr = data;
87 size_t remaining_size = size;
88 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size);
89 while (packet_len != 0 && packet_len + 2 <= remaining_size) {
90 data_ptr += 2;
91 remaining_size -= 2;
92 AudioDecoder::SpeechType speech_type;
93 decoder->DecodeRedundant(data_ptr, packet_len, sample_rate_hz,
94 max_decoded_bytes, decoded, &speech_type);
95 data_ptr += packet_len;
96 remaining_size -= packet_len;
97 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size);
98 }
99 }
100
101 // This function is similar to FuzzAudioDecoder, but also reads fuzzed data into
102 // RTP header values. The fuzzed data and values are sent to the decoder's
103 // IncomingPacket method.
104 void FuzzAudioDecoderIncomingPacket(const uint8_t* data,
105 size_t size,
106 AudioDecoder* decoder) {
107 const uint8_t* data_ptr = data;
108 size_t remaining_size = size;
109 size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size);
110 // Sum length of rtp_sequence_number, rtp_timestamp, and arrival_timestamp.
111 const size_t header_len = sizeof(uint16_t) + 2 * sizeof(uint32_t);
112 while (packet_len != 0 && packet_len + 2 + header_len <= remaining_size) {
113 data_ptr += 2;
114 remaining_size -= 2;
115 const uint16_t rtp_sequence_number =
116 ReadIntCheckedAndIncrement<uint16_t>(&data_ptr, &remaining_size);
117 const uint32_t rtp_timestamp =
118 ReadIntCheckedAndIncrement<uint32_t>(&data_ptr, &remaining_size);
119 const uint32_t arrival_timestamp =
120 ReadIntCheckedAndIncrement<uint32_t>(&data_ptr, &remaining_size);
121 decoder->IncomingPacket(data_ptr, packet_len, rtp_sequence_number,
122 rtp_timestamp, arrival_timestamp);
123 data_ptr += packet_len;
124 remaining_size -= packet_len;
125 packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size);
126 }
127 }
49 } // namespace webrtc 128 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/test/fuzzers/audio_decoder_fuzzer.h ('k') | webrtc/test/fuzzers/audio_decoder_isac_incoming_packet_fuzzer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698