blob: 9c7697629009d0372ecb56082db44aa6ff188c1b [file] [log] [blame]
Mark Brand0c720502017-10-20 16:23:23 +02001/*
2 * Copyright (c) 2017 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
11#include <algorithm>
12
Mark Brand0c720502017-10-20 16:23:23 +020013#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Yves Gerey665174f2018-06-19 15:03:05 +020014#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
Mark Brand0c720502017-10-20 16:23:23 +020015#include "modules/rtp_rtcp/source/byte_io.h"
16#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Danil Chapovalov04fd2152019-09-20 11:40:12 +020017#include "test/fuzzers/fuzz_data_helper.h"
Mark Brand0c720502017-10-20 16:23:23 +020018
19namespace webrtc {
20
21namespace {
22class DummyCallback : public RecoveredPacketReceiver {
23 void OnRecoveredPacket(const uint8_t* packet, size_t length) override {}
24};
25} // namespace
26
27void FuzzOneInput(const uint8_t* data, size_t size) {
28 constexpr size_t kMinDataNeeded = 12;
Sam Zackrisson26204702018-10-25 13:46:26 +020029 if (size < kMinDataNeeded || size > 2000) {
Mark Brand0c720502017-10-20 16:23:23 +020030 return;
31 }
32
33 uint32_t ulpfec_ssrc = ByteReader<uint32_t>::ReadLittleEndian(data + 0);
34 uint16_t ulpfec_seq_num = ByteReader<uint16_t>::ReadLittleEndian(data + 4);
35 uint32_t media_ssrc = ByteReader<uint32_t>::ReadLittleEndian(data + 6);
36 uint16_t media_seq_num = ByteReader<uint16_t>::ReadLittleEndian(data + 10);
37
38 DummyCallback callback;
39 std::unique_ptr<UlpfecReceiver> receiver(
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020040 UlpfecReceiver::Create(ulpfec_ssrc, &callback, {}));
Mark Brand0c720502017-10-20 16:23:23 +020041
Danil Chapovalov04fd2152019-09-20 11:40:12 +020042 test::FuzzDataHelper fuzz_data(rtc::MakeArrayView(data, size));
43 while (fuzz_data.CanReadBytes(kMinDataNeeded)) {
44 size_t packet_length = kRtpHeaderSize + fuzz_data.Read<uint8_t>();
45 auto raw_packet = fuzz_data.ReadByteArray(packet_length);
46
47 RtpPacket parsed_packet;
48 if (!parsed_packet.Parse(raw_packet))
49 continue;
50
51 // Overwrite the fields for the sequence number and SSRC with
Mark Brand0c720502017-10-20 16:23:23 +020052 // consistent values for either a received UlpFEC packet or received media
53 // packet. (We're still relying on libfuzzer to manage to generate packet
54 // headers that interact together; this just ensures that we have two
55 // consistent streams).
Danil Chapovalov04fd2152019-09-20 11:40:12 +020056 if (fuzz_data.ReadOrDefaultValue<uint8_t>(0) % 2 == 0) {
Mark Brand0c720502017-10-20 16:23:23 +020057 // Simulate UlpFEC packet.
Danil Chapovalov04fd2152019-09-20 11:40:12 +020058 parsed_packet.SetSequenceNumber(ulpfec_seq_num++);
59 parsed_packet.SetSsrc(ulpfec_ssrc);
Mark Brand0c720502017-10-20 16:23:23 +020060 } else {
61 // Simulate media packet.
Danil Chapovalov04fd2152019-09-20 11:40:12 +020062 parsed_packet.SetSequenceNumber(media_seq_num++);
63 parsed_packet.SetSsrc(media_ssrc);
Mark Brand0c720502017-10-20 16:23:23 +020064 }
Danil Chapovalov04fd2152019-09-20 11:40:12 +020065
66 receiver->AddReceivedRedPacket(parsed_packet, 0);
Mark Brand0c720502017-10-20 16:23:23 +020067 }
68
69 receiver->ProcessReceivedFec();
70}
71
72} // namespace webrtc