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 |
11 #include <algorithm> | 11 #include <algorithm> |
12 | 12 |
13 #include "webrtc/base/array_view.h" | 13 #include "webrtc/base/array_view.h" |
14 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 14 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
15 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" | 15 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
16 #include "webrtc/system_wrappers/include/clock.h" | |
16 #include "webrtc/voice_engine/transport_feedback_packet_loss_tracker.h" | 17 #include "webrtc/voice_engine/transport_feedback_packet_loss_tracker.h" |
17 | 18 |
18 namespace webrtc { | 19 namespace webrtc { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 template <typename T> | 23 template <typename T> |
23 T FuzzInput(const uint8_t** data, size_t* size) { | 24 T FuzzInput(const uint8_t** data, size_t* size) { |
24 RTC_CHECK_GE(*size, sizeof(T)); | 25 RTC_CHECK_GE(*size, sizeof(T)); |
25 T rc = ByteReader<T>::ReadBigEndian(*data); | 26 T rc = ByteReader<T>::ReadBigEndian(*data); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 | 103 |
103 bool Setup(const uint8_t** data, | 104 bool Setup(const uint8_t** data, |
104 size_t* size, | 105 size_t* size, |
105 std::unique_ptr<TransportFeedbackPacketLossTracker>* tracker) { | 106 std::unique_ptr<TransportFeedbackPacketLossTracker>* tracker) { |
106 if (*size < 3 * sizeof(uint16_t)) { | 107 if (*size < 3 * sizeof(uint16_t)) { |
107 return false; | 108 return false; |
108 } | 109 } |
109 | 110 |
110 constexpr size_t kSeqNumHalf = 0x8000u; | 111 constexpr size_t kSeqNumHalf = 0x8000u; |
111 | 112 |
112 // 0x8000 >= max_window_size >= plr_min_num_packets > rplr_min_num_pairs >= 1 | 113 const int64_t max_window_size_ms = FuzzInRange(data, size, 1, 1 << 16); |
113 // (The distribution isn't uniform, but it's enough; more would be overkill.) | 114 const size_t plr_min_num_packets = FuzzInRange(data, size, 1, kSeqNumHalf); |
114 const size_t max_window_size = FuzzInRange(data, size, 2, kSeqNumHalf); | 115 const size_t rplr_min_num_pairs = FuzzInRange(data, size, 1, kSeqNumHalf - 1); |
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 | 116 |
120 tracker->reset(new TransportFeedbackPacketLossTracker( | 117 tracker->reset(new TransportFeedbackPacketLossTracker( |
121 max_window_size, plr_min_num_packets, rplr_min_num_pairs)); | 118 max_window_size_ms, plr_min_num_packets, rplr_min_num_pairs)); |
122 | 119 |
123 return true; | 120 return true; |
124 } | 121 } |
125 | 122 |
126 bool FuzzSequenceNumberDelta(const uint8_t** data, | 123 bool FuzzSequenceNumberDelta(const uint8_t** data, |
127 size_t* size, | 124 size_t* size, |
128 uint16_t* delta) { | 125 uint16_t* delta) { |
129 // Fuzz with a higher likelihood for immediately consecutive pairs | 126 // Fuzz with a higher likelihood for immediately consecutive pairs |
130 // than you would by just fuzzing 1-256. | 127 // than you would by just fuzzing 1-256. |
131 // Note: Values higher than 256 still possible, but that would be in a new | 128 // Note: Values higher than 256 still possible, but that would be in a new |
132 // packet-sending block. | 129 // packet-sending block. |
133 // * Fuzzed value in [0 : 127] (50% chance) -> delta is 1. | 130 // * Fuzzed value in [0 : 127] (50% chance) -> delta is 1. |
134 // * Fuzzed value in [128 : 255] (50% chance) -> delta in range [2 : 129]. | 131 // * Fuzzed value in [128 : 255] (50% chance) -> delta in range [2 : 129]. |
135 if (*size < sizeof(uint8_t)) { | 132 if (*size < sizeof(uint8_t)) { |
136 return false; | 133 return false; |
137 } | 134 } |
138 uint8_t fuzzed = FuzzInput<uint8_t>(data, size); | 135 uint8_t fuzzed = FuzzInput<uint8_t>(data, size); |
139 *delta = (fuzzed < 128) ? 1 : (fuzzed - 128 + 2); | 136 *delta = (fuzzed < 128) ? 1 : (fuzzed - 128 + 2); |
140 return true; | 137 return true; |
141 } | 138 } |
142 | 139 |
140 bool FuzzClockAdvancement(const uint8_t** data, | |
141 size_t* size, | |
142 int64_t* time_ms) { | |
143 // Fuzzing 64-bit worth of delta would be extreme overkill, as 32-bit is | |
144 // already ~49 days long. We'll fuzz deltas up to a smaller value, and this | |
145 // way also guarantee that wrap-around is impossible, as in real life. | |
146 | |
147 // Higher likelihood for more likely cases: | |
148 // 5% chance of delta = 0. | |
149 // 20% chance of delta in range [1 : 10] (uniformly distributed) | |
150 // 55% chance of delta in range [11 : 500] (uniformly distributed) | |
151 // 20% chance of delta in range [501 : 10000] (uniformly distributed) | |
152 struct probability_distribution { | |
153 float probability; | |
154 size_t lower; | |
155 size_t higher; | |
156 }; | |
157 constexpr probability_distribution clock_probability_distribution[] = { | |
158 {0.05, 0, 0}, {0.20, 1, 10}, {0.55, 11, 500}, {0.20, 501, 10000} | |
159 }; | |
160 | |
161 if (*size < sizeof(uint8_t)) { | |
162 return false; | |
163 } | |
164 const float fuzzed = FuzzInput<uint8_t>(data, size) / 256.0f; | |
165 | |
166 float cumulative_probability = 0; | |
167 for (const auto& dist : clock_probability_distribution) { | |
168 cumulative_probability += dist.probability; | |
169 if (fuzzed < cumulative_probability) { | |
170 if (dist.lower == dist.higher) { | |
171 *time_ms += dist.lower; | |
172 return true; | |
173 } else if (*size < sizeof(uint16_t)) { | |
174 return false; | |
175 } else { | |
176 *time_ms += FuzzInRange(data, size, dist.lower, dist.higher); | |
177 return true; | |
178 } | |
179 } | |
180 } | |
181 | |
182 RTC_NOTREACHED(); | |
183 return false; | |
184 } | |
185 | |
143 bool FuzzPacketSendBlock( | 186 bool FuzzPacketSendBlock( |
144 std::unique_ptr<TransportFeedbackPacketLossTracker>& tracker, | 187 std::unique_ptr<TransportFeedbackPacketLossTracker>& tracker, |
145 const uint8_t** data, | 188 const uint8_t** data, |
146 size_t* size) { | 189 size_t* size, |
190 int64_t* time_ms) { | |
147 // We want to test with block lengths between 1 and 2^16, inclusive. | 191 // We want to test with block lengths between 1 and 2^16, inclusive. |
148 if (*size < sizeof(uint8_t)) { | 192 if (*size < sizeof(uint8_t)) { |
149 return false; | 193 return false; |
150 } | 194 } |
151 size_t packet_block_len = 1 + FuzzInput<uint8_t>(data, size); | 195 size_t packet_block_len = 1 + FuzzInput<uint8_t>(data, size); |
152 | 196 |
153 // First sent sequence number uniformly selected. | 197 // First sent sequence number uniformly selected. |
154 if (*size < sizeof(uint16_t)) { | 198 if (*size < sizeof(uint16_t)) { |
155 return false; | 199 return false; |
156 } | 200 } |
157 uint16_t seq_num = FuzzInput<uint16_t>(data, size); | 201 uint16_t seq_num = FuzzInput<uint16_t>(data, size); |
158 tracker->OnPacketAdded(seq_num); | 202 tracker->OnPacketAdded(seq_num, *time_ms); |
159 tracker->Validate(); | 203 tracker->Validate(); |
160 | 204 |
205 bool may_continue = FuzzClockAdvancement(data, size, time_ms); | |
206 if (!may_continue) { | |
207 return false; | |
208 } | |
209 | |
161 for (size_t i = 1; i < packet_block_len; i++) { | 210 for (size_t i = 1; i < packet_block_len; i++) { |
162 uint16_t delta; | 211 uint16_t delta; |
163 bool may_continue = FuzzSequenceNumberDelta(data, size, &delta); | 212 may_continue = FuzzSequenceNumberDelta(data, size, &delta); |
213 if (!may_continue) | |
214 return false; | |
215 may_continue = FuzzClockAdvancement(data, size, time_ms); | |
164 if (!may_continue) | 216 if (!may_continue) |
165 return false; | 217 return false; |
166 seq_num += delta; | 218 seq_num += delta; |
167 tracker->OnPacketAdded(seq_num); | 219 tracker->OnPacketAdded(seq_num, *time_ms); |
168 tracker->Validate(); | 220 tracker->Validate(); |
169 } | 221 } |
170 | 222 |
171 return true; | 223 return true; |
172 } | 224 } |
173 | 225 |
174 bool FuzzTransportFeedbackBlock( | 226 bool FuzzTransportFeedbackBlock( |
175 std::unique_ptr<TransportFeedbackPacketLossTracker>& tracker, | 227 std::unique_ptr<TransportFeedbackPacketLossTracker>& tracker, |
176 const uint8_t** data, | 228 const uint8_t** data, |
177 size_t* size) { | 229 size_t* size) { |
(...skipping 21 matching lines...) Expand all Loading... | |
199 } | 251 } |
200 | 252 |
201 } // namespace | 253 } // namespace |
202 | 254 |
203 void FuzzOneInput(const uint8_t* data, size_t size) { | 255 void FuzzOneInput(const uint8_t* data, size_t size) { |
204 std::unique_ptr<TransportFeedbackPacketLossTracker> tracker; | 256 std::unique_ptr<TransportFeedbackPacketLossTracker> tracker; |
205 bool may_continue; | 257 bool may_continue; |
206 | 258 |
207 may_continue = Setup(&data, &size, &tracker); | 259 may_continue = Setup(&data, &size, &tracker); |
208 | 260 |
261 // We never expect this to wrap around, so it makes sense to just start with | |
262 // a sane value, and keep on incrementing by a fuzzed delta. | |
263 int64_t time_ms = Clock::GetRealTimeClock()->TimeInMilliseconds(); | |
pbos-webrtc
2017/03/03 18:37:34
Read a 32bit value from the fuzzed data instead, i
elad.alon_webrtc.org
2017/03/03 18:38:47
Will do.
| |
264 | |
209 while (may_continue) { | 265 while (may_continue) { |
210 may_continue = FuzzPacketSendBlock(tracker, &data, &size); | 266 may_continue = FuzzPacketSendBlock(tracker, &data, &size, &time_ms); |
211 if (!may_continue) { | 267 if (!may_continue) { |
212 return; | 268 return; |
213 } | 269 } |
214 may_continue = FuzzTransportFeedbackBlock(tracker, &data, &size); | 270 may_continue = FuzzTransportFeedbackBlock(tracker, &data, &size); |
215 } | 271 } |
216 } | 272 } |
217 | 273 |
218 } // namespace webrtc | 274 } // namespace webrtc |
OLD | NEW |