blob: f8067b78ea5898e2c9fc1ffcde7aaf1a08aaa6d1 [file] [log] [blame]
philipelea142f82017-01-11 02:01:56 -08001/*
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
Jonathan Metzman9f80b972018-10-05 10:38:13 -070011#include "modules/video_coding/frame_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "modules/video_coding/packet_buffer.h"
13#include "system_wrappers/include/clock.h"
philipel0c87e292018-05-17 16:44:47 +020014#include "test/fuzzers/fuzz_data_helper.h"
philipelea142f82017-01-11 02:01:56 -080015
16namespace webrtc {
philipelea142f82017-01-11 02:01:56 -080017namespace {
Elad Alonb4643ad2019-02-22 11:19:50 +010018class NullCallback : public video_coding::OnAssembledFrameCallback {
19 void OnAssembledFrame(std::unique_ptr<video_coding::RtpFrameObject> frame) {}
philipelea142f82017-01-11 02:01:56 -080020};
21} // namespace
22
23void FuzzOneInput(const uint8_t* data, size_t size) {
Sam Zackrisson26204702018-10-25 13:46:26 +020024 if (size > 200000) {
25 return;
26 }
philipelea142f82017-01-11 02:01:56 -080027 VCMPacket packet;
28 NullCallback callback;
29 SimulatedClock clock(0);
30 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer(
31 video_coding::PacketBuffer::Create(&clock, 8, 1024, &callback));
philipel0c87e292018-05-17 16:44:47 +020032 test::FuzzDataHelper helper(rtc::ArrayView<const uint8_t>(data, size));
philipelea142f82017-01-11 02:01:56 -080033
philipel32134472018-07-10 11:31:29 +020034 while (helper.BytesLeft()) {
Chen Xing9aa870a2019-06-21 10:48:59 +020035 // Complex types (e.g. non-POD-like types) can't be bit-wise fuzzed with
36 // random data or it will put them in an invalid state. We therefore backup
37 // their byte-patterns before the fuzzing and restore them after.
philipel32134472018-07-10 11:31:29 +020038 uint8_t video_header_backup[sizeof(packet.video_header)];
39 memcpy(&video_header_backup, &packet.video_header,
40 sizeof(packet.video_header));
philipelc71cd6c2018-10-03 15:22:51 +020041 uint8_t generic_descriptor_backup[sizeof(packet.generic_descriptor)];
42 memcpy(&generic_descriptor_backup, &packet.generic_descriptor,
43 sizeof(packet.generic_descriptor));
Chen Xing9aa870a2019-06-21 10:48:59 +020044 uint8_t packet_info_backup[sizeof(packet.packet_info)];
45 memcpy(&packet_info_backup, &packet.packet_info,
46 sizeof(packet.packet_info));
philipelc71cd6c2018-10-03 15:22:51 +020047
philipel0c87e292018-05-17 16:44:47 +020048 helper.CopyTo(&packet);
philipel32134472018-07-10 11:31:29 +020049
50 memcpy(&packet.video_header, &video_header_backup,
51 sizeof(packet.video_header));
philipelc71cd6c2018-10-03 15:22:51 +020052 memcpy(&packet.generic_descriptor, &generic_descriptor_backup,
53 sizeof(packet.generic_descriptor));
Chen Xing9aa870a2019-06-21 10:48:59 +020054 memcpy(&packet.packet_info, &packet_info_backup,
55 sizeof(packet.packet_info));
philipelc71cd6c2018-10-03 15:22:51 +020056
philipel32134472018-07-10 11:31:29 +020057 // The packet buffer owns the payload of the packet.
58 uint8_t payload_size;
59 helper.CopyTo(&payload_size);
60 packet.sizeBytes = payload_size;
61 packet.dataPtr = new uint8_t[payload_size];
62
63 packet_buffer->InsertPacket(&packet);
64 }
philipelea142f82017-01-11 02:01:56 -080065}
66
67} // namespace webrtc