Mark Brand | 0c72050 | 2017-10-20 16:23:23 +0200 | [diff] [blame] | 1 | /* |
| 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 Brand | 0c72050 | 2017-10-20 16:23:23 +0200 | [diff] [blame] | 13 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 14 | #include "modules/rtp_rtcp/include/ulpfec_receiver.h" |
Mark Brand | 0c72050 | 2017-10-20 16:23:23 +0200 | [diff] [blame] | 15 | #include "modules/rtp_rtcp/source/byte_io.h" |
| 16 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Mark Brand | 0c72050 | 2017-10-20 16:23:23 +0200 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | class DummyCallback : public RecoveredPacketReceiver { |
| 22 | void OnRecoveredPacket(const uint8_t* packet, size_t length) override {} |
| 23 | }; |
| 24 | } // namespace |
| 25 | |
| 26 | void FuzzOneInput(const uint8_t* data, size_t size) { |
| 27 | constexpr size_t kMinDataNeeded = 12; |
Sam Zackrisson | 2620470 | 2018-10-25 13:46:26 +0200 | [diff] [blame] | 28 | if (size < kMinDataNeeded || size > 2000) { |
Mark Brand | 0c72050 | 2017-10-20 16:23:23 +0200 | [diff] [blame] | 29 | return; |
| 30 | } |
| 31 | |
| 32 | uint32_t ulpfec_ssrc = ByteReader<uint32_t>::ReadLittleEndian(data + 0); |
| 33 | uint16_t ulpfec_seq_num = ByteReader<uint16_t>::ReadLittleEndian(data + 4); |
| 34 | uint32_t media_ssrc = ByteReader<uint32_t>::ReadLittleEndian(data + 6); |
| 35 | uint16_t media_seq_num = ByteReader<uint16_t>::ReadLittleEndian(data + 10); |
| 36 | |
| 37 | DummyCallback callback; |
| 38 | std::unique_ptr<UlpfecReceiver> receiver( |
| 39 | UlpfecReceiver::Create(ulpfec_ssrc, &callback)); |
| 40 | |
| 41 | std::unique_ptr<uint8_t[]> packet; |
| 42 | size_t packet_length; |
| 43 | size_t i = kMinDataNeeded; |
| 44 | while (i < size) { |
| 45 | packet_length = kRtpHeaderSize + data[i++]; |
| 46 | packet = std::unique_ptr<uint8_t[]>(new uint8_t[packet_length]); |
| 47 | if (i + packet_length >= size) { |
| 48 | break; |
| 49 | } |
| 50 | memcpy(packet.get(), data + i, packet_length); |
| 51 | i += packet_length; |
| 52 | // Overwrite the RTPHeader fields for the sequence number and SSRC with |
| 53 | // consistent values for either a received UlpFEC packet or received media |
| 54 | // packet. (We're still relying on libfuzzer to manage to generate packet |
| 55 | // headers that interact together; this just ensures that we have two |
| 56 | // consistent streams). |
| 57 | if (i < size && data[i++] % 2 == 0) { |
| 58 | // Simulate UlpFEC packet. |
| 59 | ByteWriter<uint16_t>::WriteBigEndian(packet.get() + 2, ulpfec_seq_num++); |
| 60 | ByteWriter<uint32_t>::WriteBigEndian(packet.get() + 8, ulpfec_ssrc); |
| 61 | } else { |
| 62 | // Simulate media packet. |
| 63 | ByteWriter<uint16_t>::WriteBigEndian(packet.get() + 2, media_seq_num++); |
| 64 | ByteWriter<uint32_t>::WriteBigEndian(packet.get() + 8, media_ssrc); |
| 65 | } |
| 66 | RtpPacketReceived parsed_packet; |
| 67 | RTPHeader parsed_header; |
| 68 | if (parsed_packet.Parse(packet.get(), packet_length)) { |
| 69 | parsed_packet.GetHeader(&parsed_header); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | receiver->AddReceivedRedPacket(parsed_header, packet.get(), packet_length, |
| 71 | 0); |
Mark Brand | 0c72050 | 2017-10-20 16:23:23 +0200 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | receiver->ProcessReceivedFec(); |
| 76 | } |
| 77 | |
| 78 | } // namespace webrtc |