webrtc/test/fuzzers/audio_decoder_fuzzer.cc
Sam Zackrisson 35c773dad6 Cap the number of fuzzed decoder packets to 200
The fuzzer figured out that 3 bytes is enough to fuzz a package.
2 bytes for packet length, and 1 byte of actual packet. A 20K test case
can generate > 6000 packets. It does not seem like efficient fuzzing.

This CL simply stops execution when 200 packets have been generated.
That corresponds to 4 seconds of 20 ms packets.

Bug: chromium:840115
Change-Id: Id2742a6f8021134bacd8a6e8c71b32f20c7f1086
Reviewed-on: https://webrtc-review.googlesource.com/88566
Reviewed-by: Alex Loiko <aleloi@webrtc.org>
Commit-Queue: Sam Zackrisson <saza@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24000}
2018-07-17 09:14:45 +00:00

104 lines
3.9 KiB
C++

/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "test/fuzzers/audio_decoder_fuzzer.h"
#include <limits>
#include "absl/types/optional.h"
#include "api/audio_codecs/audio_decoder.h"
#include "modules/rtp_rtcp/source/byte_io.h"
#include "rtc_base/checks.h"
namespace webrtc {
namespace {
template <typename T, unsigned int B = sizeof(T)>
bool ParseInt(const uint8_t** data, size_t* remaining_size, T* value) {
static_assert(std::numeric_limits<T>::is_integer, "Type must be an integer.");
static_assert(sizeof(T) <= sizeof(uint64_t),
"Cannot read wider than uint64_t.");
static_assert(B <= sizeof(T), "T must be at least B bytes wide.");
if (B > *remaining_size)
return false;
uint64_t val = ByteReader<uint64_t, B>::ReadBigEndian(*data);
*data += B;
*remaining_size -= B;
*value = static_cast<T>(val);
return true;
}
} // namespace
// This function reads two bytes from the beginning of |data|, interprets them
// as the first packet length, and reads this many bytes if available. The
// payload is inserted into the decoder, and the process continues until no more
// data is available. Either AudioDecoder::Decode or
// AudioDecoder::DecodeRedundant is used, depending on the value of
// |decode_type|.
void FuzzAudioDecoder(DecoderFunctionType decode_type,
const uint8_t* data,
size_t size,
AudioDecoder* decoder,
int sample_rate_hz,
size_t max_decoded_bytes,
int16_t* decoded) {
const uint8_t* data_ptr = data;
size_t remaining_size = size;
size_t packet_len;
constexpr size_t kMaxNumFuzzedPackets = 200;
for (size_t num_packets = 0; num_packets < kMaxNumFuzzedPackets;
++num_packets) {
if (!(ParseInt<size_t, 2>(&data_ptr, &remaining_size, &packet_len) &&
packet_len <= remaining_size)) {
break;
}
AudioDecoder::SpeechType speech_type;
switch (decode_type) {
case DecoderFunctionType::kNormalDecode:
decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes,
decoded, &speech_type);
break;
case DecoderFunctionType::kRedundantDecode:
decoder->DecodeRedundant(data_ptr, packet_len, sample_rate_hz,
max_decoded_bytes, decoded, &speech_type);
break;
}
data_ptr += packet_len;
remaining_size -= packet_len;
}
}
// This function is similar to FuzzAudioDecoder, but also reads fuzzed data into
// RTP header values. The fuzzed data and values are sent to the decoder's
// IncomingPacket method.
void FuzzAudioDecoderIncomingPacket(const uint8_t* data,
size_t size,
AudioDecoder* decoder) {
const uint8_t* data_ptr = data;
size_t remaining_size = size;
size_t packet_len;
while (ParseInt<size_t, 2>(&data_ptr, &remaining_size, &packet_len)) {
uint16_t rtp_sequence_number;
if (!ParseInt(&data_ptr, &remaining_size, &rtp_sequence_number))
break;
uint32_t rtp_timestamp;
if (!ParseInt(&data_ptr, &remaining_size, &rtp_timestamp))
break;
uint32_t arrival_timestamp;
if (!ParseInt(&data_ptr, &remaining_size, &arrival_timestamp))
break;
if (remaining_size < packet_len)
break;
decoder->IncomingPacket(data_ptr, packet_len, rtp_sequence_number,
rtp_timestamp, arrival_timestamp);
data_ptr += packet_len;
remaining_size -= packet_len;
}
}
} // namespace webrtc