OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 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 |
(...skipping 26 matching lines...) Expand all Loading... |
37 RTC_CHECK_LT(upper - lower, 1 << (8 * sizeof(uint16_t))); | 37 RTC_CHECK_LT(upper - lower, 1 << (8 * sizeof(uint16_t))); |
38 const size_t range = upper - lower; | 38 const size_t range = upper - lower; |
39 const uint16_t fuzzed = FuzzInput<uint16_t>(data, size); | 39 const uint16_t fuzzed = FuzzInput<uint16_t>(data, size); |
40 const size_t offset = (static_cast<float>(fuzzed) / 0x10000) * (range + 1); | 40 const size_t offset = (static_cast<float>(fuzzed) / 0x10000) * (range + 1); |
41 RTC_CHECK_LE(offset, range); // (fuzzed <= 0xffff) -> (offset < range + 1) | 41 RTC_CHECK_LE(offset, range); // (fuzzed <= 0xffff) -> (offset < range + 1) |
42 return lower + offset; | 42 return lower + offset; |
43 } | 43 } |
44 | 44 |
45 class TransportFeedbackGenerator { | 45 class TransportFeedbackGenerator { |
46 public: | 46 public: |
47 explicit TransportFeedbackGenerator(rtc::ArrayView<const uint8_t> data) | 47 explicit TransportFeedbackGenerator(const uint8_t** data, size_t* size) |
48 : data_(data), ended_(false), data_idx_(0) {} | 48 : data_(data), size_(size) {} |
49 | 49 |
50 void GetNextTransportFeedback(rtcp::TransportFeedback* feedback) { | 50 bool GetNextTransportFeedback(rtcp::TransportFeedback* feedback) { |
51 uint16_t base_seq_num = 0; | 51 uint16_t base_seq_num = 0; |
52 if (!ReadData<uint16_t>(&base_seq_num)) { | 52 if (!ReadData<uint16_t>(&base_seq_num)) { |
53 return; | 53 return false; |
| 54 } |
| 55 constexpr int64_t kBaseTimeUs = 1234; // Irrelevant to this test. |
| 56 feedback->SetBase(base_seq_num, kBaseTimeUs); |
| 57 |
| 58 uint16_t remaining_packets = 0; |
| 59 if (!ReadData<uint16_t>(&remaining_packets)) |
| 60 return false; |
| 61 // Range is [0x00001 : 0x10000], but we keep it 0x0000 to 0xffff for now, |
| 62 // and add the last status as RECEIVED. That is because of a limitation |
| 63 // that says that the last status cannot be LOST. |
| 64 |
| 65 uint16_t seq_num = base_seq_num; |
| 66 while (remaining_packets > 0) { |
| 67 uint8_t status_byte = 0; |
| 68 if (!ReadData<uint8_t>(&status_byte)) { |
| 69 return false; |
| 70 } |
| 71 // Each status byte contains 8 statuses. |
| 72 for (size_t i = 0; i < 8 && remaining_packets > 0; ++i) { |
| 73 const bool received = (status_byte & (0x01 << i)); |
| 74 if (received) { |
| 75 feedback->AddReceivedPacket(seq_num, kBaseTimeUs); |
| 76 } |
| 77 ++seq_num; |
| 78 --remaining_packets; |
| 79 } |
54 } | 80 } |
55 | 81 |
56 const int64_t kBaseTimeUs = 1234; // Irrelevant to this test. | 82 // As mentioned above, all feedbacks must report with a received packet. |
57 feedback->SetBase(base_seq_num, kBaseTimeUs); | 83 feedback->AddReceivedPacket(seq_num, kBaseTimeUs); |
58 | 84 |
59 uint16_t num_statuses = 0; | 85 return true; |
60 if (!ReadData<uint16_t>(&num_statuses)) | |
61 return; | |
62 num_statuses = std::max<uint16_t>(num_statuses, 1); | |
63 | |
64 uint16_t seq_num = base_seq_num; | |
65 while (true) { | |
66 uint8_t status_byte = 0; | |
67 if (!ReadData<uint8_t>(&status_byte)) | |
68 return; | |
69 // Each status byte contains 8 statuses. | |
70 for (size_t j = 0; j < 8; ++j) { | |
71 if (status_byte & 0x01) { | |
72 feedback->AddReceivedPacket(seq_num, kBaseTimeUs); | |
73 } | |
74 seq_num++; | |
75 if (seq_num >= base_seq_num + num_statuses) { | |
76 feedback->AddReceivedPacket(seq_num, kBaseTimeUs); | |
77 return; | |
78 } | |
79 status_byte >>= 1; | |
80 } | |
81 } | |
82 } | 86 } |
83 | 87 |
84 bool ended() const { return ended_; } | |
85 | |
86 private: | 88 private: |
87 template <typename T> | 89 template <typename T> |
88 bool ReadData(T* value) { | 90 bool ReadData(T* value) { |
89 RTC_CHECK(!ended_); | 91 if (*size_ < sizeof(T)) { |
90 if (data_idx_ + sizeof(T) > data_.size()) { | |
91 ended_ = true; | |
92 return false; | 92 return false; |
| 93 } else { |
| 94 *value = FuzzInput<T>(data_, size_); |
| 95 return true; |
93 } | 96 } |
94 *value = ByteReader<T>::ReadBigEndian(&data_[data_idx_]); | 97 } |
95 data_idx_ += sizeof(T); | 98 |
| 99 const uint8_t** data_; |
| 100 size_t* size_; |
| 101 }; |
| 102 |
| 103 bool Setup(const uint8_t** data, |
| 104 size_t* size, |
| 105 std::unique_ptr<TransportFeedbackPacketLossTracker>* tracker) { |
| 106 if (*size < 3 * sizeof(uint16_t)) { |
| 107 return false; |
| 108 } |
| 109 |
| 110 constexpr size_t kSeqNumHalf = 0x8000u; |
| 111 |
| 112 // 0x8000 >= max_window_size >= plr_min_num_packets > rplr_min_num_pairs >= 1 |
| 113 // (The distribution isn't uniform, but it's enough; more would be overkill.) |
| 114 const size_t max_window_size = FuzzInRange(data, size, 2, kSeqNumHalf); |
| 115 const size_t plr_min_num_packets = |
| 116 FuzzInRange(data, size, 2, max_window_size); |
| 117 const size_t rplr_min_num_pairs = |
| 118 FuzzInRange(data, size, 1, plr_min_num_packets - 1); |
| 119 |
| 120 tracker->reset(new TransportFeedbackPacketLossTracker( |
| 121 max_window_size, plr_min_num_packets, rplr_min_num_pairs)); |
| 122 |
| 123 return true; |
| 124 } |
| 125 |
| 126 bool FuzzSequenceNumberDelta(const uint8_t** data, |
| 127 size_t* size, |
| 128 uint16_t* delta) { |
| 129 // Fuzz with a higher likelihood for immediately consecutive pairs |
| 130 // than you would by just fuzzing 1-256. |
| 131 // Note: Values higher than 256 still possible, but that would be in a new |
| 132 // packet-sending block. |
| 133 // * First seed in range [0 : 127] (50% chance) -> delta is 1. |
| 134 // * First seed in range [128 : 255] (50% chance) -> delta in range [2 : 129]. |
| 135 if (*size < sizeof(uint8_t)) { |
| 136 return false; |
| 137 } |
| 138 uint8_t fuzzed = FuzzInput<uint8_t>(data, size); |
| 139 *delta = (fuzzed < 128) ? 1 : (fuzzed - 128 + 2); |
| 140 return true; |
| 141 } |
| 142 |
| 143 bool FuzzPacketSendBlock( |
| 144 std::unique_ptr<TransportFeedbackPacketLossTracker>& tracker, |
| 145 const uint8_t** data, |
| 146 size_t* size) { |
| 147 // We want to test with block lengths between 1 and 2^16, inclusive. |
| 148 if (*size < sizeof(uint16_t)) { |
| 149 return false; |
| 150 } |
| 151 size_t packet_block_len = 1 + FuzzInput<uint16_t>(data, size); |
| 152 |
| 153 if (packet_block_len == 0) { |
96 return true; | 154 return true; |
97 } | 155 } |
98 | 156 |
99 const rtc::ArrayView<const uint8_t> data_; | 157 // First sent sequence number uniformly selected. |
100 bool ended_; | 158 if (*size < sizeof(uint16_t)) { |
101 size_t data_idx_; | 159 return false; |
102 }; | 160 } |
| 161 uint16_t seq_num = FuzzInput<uint16_t>(data, size); |
| 162 tracker->OnPacketAdded(seq_num); |
| 163 tracker->Validate(); |
| 164 |
| 165 // Fuzz subsequent sequence numbers according to a non-uniformly |
| 166 // distributed delta (to make sure the fuzzer-test does not end up |
| 167 // spending 99.9% of its time working on a mostly empty window). |
| 168 for (size_t i = 1; i < packet_block_len; i++) { |
| 169 uint16_t delta; |
| 170 bool may_continue = FuzzSequenceNumberDelta(data, size, &delta); |
| 171 if (!may_continue) |
| 172 return false; |
| 173 seq_num += delta; |
| 174 tracker->OnPacketAdded(seq_num); |
| 175 tracker->Validate(); |
| 176 } |
| 177 |
| 178 return true; |
| 179 } |
| 180 |
| 181 bool FuzzTransportFeedbackBlock( |
| 182 std::unique_ptr<TransportFeedbackPacketLossTracker>& tracker, |
| 183 const uint8_t** data, |
| 184 size_t* size) { |
| 185 // Fuzz the number of back-to-back feedbacks. At least one, or this would |
| 186 // be meaningless - we'd go straight back to fuzzing another packet |
| 187 // transmission block. |
| 188 if (*size < sizeof(uint8_t)) { |
| 189 return false; |
| 190 } |
| 191 |
| 192 size_t feedbacks_num = 1 + (FuzzInput<uint8_t>(data, size) & 0x3f); |
| 193 TransportFeedbackGenerator feedback_generator(data, size); |
| 194 |
| 195 for (size_t i = 0; i < feedbacks_num; i++) { |
| 196 rtcp::TransportFeedback feedback; |
| 197 bool may_continue = feedback_generator.GetNextTransportFeedback(&feedback); |
| 198 if (!may_continue) { |
| 199 return false; |
| 200 } |
| 201 tracker->OnReceivedTransportFeedback(feedback); |
| 202 tracker->Validate(); |
| 203 } |
| 204 |
| 205 return true; |
| 206 } |
103 | 207 |
104 } // namespace | 208 } // namespace |
105 | 209 |
106 void FuzzOneInput(const uint8_t* data, size_t size) { | 210 void FuzzOneInput(const uint8_t* data, size_t size) { |
107 if (size < 3 * sizeof(uint16_t)) { | 211 std::unique_ptr<TransportFeedbackPacketLossTracker> tracker; |
108 return; | 212 bool may_continue; |
109 } | |
110 constexpr size_t kSeqNumHalf = 0x8000u; | |
111 | 213 |
112 // 0x8000 >= max_window_size >= plr_min_num_packets > rplr_min_num_pairs >= 1 | 214 may_continue = Setup(&data, &size, &tracker); |
113 // (The distribution isn't uniform, but it's enough; more would be overkill.) | |
114 const size_t max_window_size = FuzzInRange(&data, &size, 2, kSeqNumHalf); | |
115 const size_t plr_min_num_packets = | |
116 FuzzInRange(&data, &size, 2, max_window_size); | |
117 const size_t rplr_min_num_pairs = | |
118 FuzzInRange(&data, &size, 1, plr_min_num_packets - 1); | |
119 | 215 |
120 TransportFeedbackPacketLossTracker tracker( | 216 while (may_continue) { |
121 max_window_size, plr_min_num_packets, rplr_min_num_pairs); | 217 may_continue = FuzzPacketSendBlock(tracker, &data, &size); |
122 | 218 if (!may_continue) { |
123 TransportFeedbackGenerator feedback_generator( | 219 return; |
124 rtc::ArrayView<const uint8_t>(data, size)); | 220 } |
125 | 221 may_continue = FuzzTransportFeedbackBlock(tracker, &data, &size); |
126 while (!feedback_generator.ended()) { | |
127 rtcp::TransportFeedback feedback; | |
128 feedback_generator.GetNextTransportFeedback(&feedback); | |
129 tracker.OnReceivedTransportFeedback(feedback); | |
130 tracker.Validate(); | |
131 } | 222 } |
132 } | 223 } |
133 | 224 |
134 } // namespace webrtc | 225 } // namespace webrtc |
OLD | NEW |