1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
/*!
|===- FuzzedDataProvider.h - Utility header for fuzz targets ---*- C++ -* ===//
|
| Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
| See https://llvm.org/LICENSE.txt for license information.
| SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|===----------------------------------------------------------------------===//
| A single header library providing an utility class to break up an array of
| bytes. Whenever run on the same input, provides the same output, as long as
| its methods are called in the same order, with the same arguments.
|===----------------------------------------------------------------------===//
*/
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/test/fuzz/FuzzedDataProvider.h]
/**
| In addition to the comments below, the
| API is also briefly documented at
| https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#fuzzed-data-provider
|
*/
pub struct FuzzedDataProvider {
data_ptr: *const u8,
remaining_bytes: usize,
}
impl FuzzedDataProvider {
/**
| |data| is an array of length |size| that the
| FuzzedDataProvider wraps to provide more
| granular access. |data| must outlive the
| FuzzedDataProvider.
*/
pub fn new(
data: *const u8,
size: usize) -> Self {
todo!();
/*
: data_ptr(data),
: remaining_bytes(size),
*/
}
/*
| See the implementation below (after
| the class definition) for more verbose
| comments for each of the methods.
|
*/
/**
| Reports the remaining bytes available
| for fuzzed input.
|
*/
pub fn remaining_bytes(&mut self) -> usize {
todo!();
/*
return remaining_bytes_;
*/
}
/**
| Methods returning std::vector of bytes. These
| are the most popular choice when splitting
| fuzzing input into pieces, as every piece is
| put into a separate buffer (i.e. ASan would
| catch any under-/overflow) and the memory
| will be released automatically.
|
=============================
|
| Returns a std::vector containing |num_bytes| of
| input data. If fewer than |num_bytes| of data
| remain, returns a shorter std::vector
| containing all of the data that's left. Can be
| used with any byte sized type, such as char,
| unsigned char, uint8_t, etc.
*/
pub fn consume_bytes<T>(&mut self, num_bytes: usize) -> Vec<T> {
todo!();
/*
num_bytes = std::min(num_bytes, remaining_bytes_);
return ConsumeBytes<T>(num_bytes, num_bytes);
*/
}
/**
| Similar to |ConsumeBytes|, but also appends the
| terminator value at the end of the resulting
| vector. Useful, when a mutable null-terminated
| C-string is needed, for example. But that is
| a rare case. Better avoid it, if possible, and
| prefer using |ConsumeBytes| or
| |ConsumeBytesAsString| methods.
*/
pub fn consume_bytes_with_terminator<T: num::Zero>(&mut self,
num_bytes: usize,
terminator: Option<T>) -> Vec<T> {
let terminator: T = terminator.unwrap_or(T::zero());
todo!();
/*
num_bytes = std::min(num_bytes, remaining_bytes_);
std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes);
result.back() = terminator;
return result;
*/
}
/**
| Returns a std::vector containing all
| remaining bytes of the input data.
|
*/
pub fn consume_remaining_bytes<T>(&mut self) -> Vec<T> {
todo!();
/*
return ConsumeBytes<T>(remaining_bytes_);
*/
}
/**
| Methods returning strings. Use only when you
| need a std::string or a null terminated
| C-string. Otherwise, prefer the methods
| returning std::vector.
|
==================================
|
| Returns a std::string containing |num_bytes| of
| input data. Using this and |.c_str()| on the
| resulting string is the best way to get an
| immutable null-terminated C string. If fewer
| than |num_bytes| of data remain, returns
| a shorter std::string containing all of the
| data that's left.
*/
#[inline] pub fn consume_bytes_as_string(&mut self, num_bytes: usize) -> String {
todo!();
/*
const_assert(sizeof(std::string::value_type) == sizeof(uint8_t),
"ConsumeBytesAsString cannot convert the data to a string.");
num_bytes = std::min(num_bytes, remaining_bytes_);
std::string result(
reinterpret_cast<const std::string::value_type *>(data_ptr_), num_bytes);
Advance(num_bytes);
return result;
*/
}
/**
| Returns a std::string of length from 0 to
| |max_length|. When it runs out of input data,
| returns what remains of the input. Designed to
| be more stable with respect to a fuzzer
| inserting characters than just picking a random
| length and then consuming that many bytes with
| |ConsumeBytes|.
*/
#[inline] pub fn consume_random_length_string_with_maxlen(&mut self, max_length: usize) -> String {
todo!();
/*
// Reads bytes from the start of |data_ptr_|. Maps "\\" to "\", and maps "\"
// followed by anything else to the end of the string. As a result of this
// logic, a fuzzer can insert characters into the string, and the string
// will be lengthened to include those new characters, resulting in a more
// stable fuzzer than picking the length of a string independently from
// picking its contents.
std::string result;
// Reserve the anticipated capaticity to prevent several reallocations.
result.reserve(std::min(max_length, remaining_bytes_));
for (size_t i = 0; i < max_length && remaining_bytes_ != 0; ++i) {
char next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
Advance(1);
if (next == '\\' && remaining_bytes_ != 0) {
next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
Advance(1);
if (next != '\\')
break;
}
result += next;
}
result.shrink_to_fit();
return result;
*/
}
/**
| Returns a std::string of length from
| 0 to remaining_bytes_|.
|
*/
#[inline] pub fn consume_random_length_string(&mut self) -> String {
todo!();
/*
return ConsumeRandomLengthString(remaining_bytes_);
*/
}
/**
| Returns a std::string containing all remaining
| bytes of the input data.
|
| Prefer using |ConsumeRemainingBytes| unless you
| actually need a std::string object.
*/
#[inline] pub fn consume_remaining_bytes_as_string(&mut self) -> String {
todo!();
/*
return ConsumeBytesAsString(remaining_bytes_);
*/
}
/**
| Methods returning integer values.
|
=============================
| Returns a number in the range [Type's min,
| Type's max]. The value might not be uniformly
| distributed in the given range. If there's no
| input data left, always returns |min|.
*/
pub fn consume_integral<T>(&mut self) -> T {
todo!();
/*
return ConsumeIntegralInRange(std::numeric_limits<T>::min(),
std::numeric_limits<T>::max());
*/
}
/**
| Returns a number in the range [min, max] by
| consuming bytes from the input data. The value
| might not be uniformly distributed in the given
| range. If there's no input data left, always
| returns |min|. |min| must be less than or equal
| to |max|.
*/
pub fn consume_integral_in_range<T>(&mut self, min: T, max: T) -> T {
todo!();
/*
const_assert(std::is_integral<T>::value, "An integral type is required.");
const_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
if (min > max)
abort();
// Use the biggest type possible to hold the range and the result.
uint64_t range = static_cast<uint64_t>(max) - min;
uint64_t result = 0;
size_t offset = 0;
while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
remaining_bytes_ != 0) {
// Pull bytes off the end of the seed data. Experimentally, this seems to
// allow the fuzzer to more easily explore the input space. This makes
// sense, since it works by modifying inputs that caused new code to run,
// and this data is often used to encode length of data read by
// |ConsumeBytes|. Separating out read lengths makes it easier modify the
// contents of the data that is actually read.
--remaining_bytes_;
result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
offset += CHAR_BIT;
}
// Avoid division by 0, in case |range + 1| results in overflow.
if (range != std::numeric_limits<decltype(range)>::max())
result = result % (range + 1);
return static_cast<T>(min + result);
*/
}
/**
| Methods returning floating point values.
|
=============================
| Returns a floating point value in the range
| [Type's lowest, Type's max] by consuming bytes
| from the input data. If there's no input data
| left, always returns approximately 0.
*/
pub fn consume_floating_point<T>(&mut self) -> T {
todo!();
/*
return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(),
std::numeric_limits<T>::max());
*/
}
/**
| Returns a floating point value in the given
| range by consuming bytes from the input
| data. If there's no input data left, returns
| |min|. Note that |min| must be less than or
| equal to |max|.
*/
pub fn consume_floating_point_in_range<T>(&mut self, min: T, max: T) -> T {
todo!();
/*
if (min > max)
abort();
T range = .0;
T result = min;
constexpr T zero(.0);
if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) {
// The diff |max - min| would overflow the given floating point type. Use
// the half of the diff as the range and consume a bool to decide whether
// the result is in the first of the second part of the diff.
range = (max / 2.0) - (min / 2.0);
if (ConsumeBool()) {
result += range;
}
} else {
range = max - min;
}
return result + range * ConsumeProbability<T>();
*/
}
/**
| Returns a floating point number in the
| range [0.0, 1.0]. If there's no input
| data left, always returns 0.
|
| 0 <= return value <= 1.
|
*/
pub fn consume_probability<T>(&mut self) -> T {
todo!();
/*
const_assert(std::is_floating_point<T>::value,
"A floating point type is required.");
// Use different integral types for different floating point types in order
// to provide better density of the resulting values.
using IntegralType =
typename std::conditional<(sizeof(T) <= sizeof(uint32_t)), uint32_t,
uint64_t>::type;
T result = static_cast<T>(ConsumeIntegral<IntegralType>());
result /= static_cast<T>(std::numeric_limits<IntegralType>::max());
return result;
*/
}
/**
| Reads one byte and returns a bool, or
| false when no data remains.
|
*/
#[inline] pub fn consume_bool(&mut self) -> bool {
todo!();
/*
return 1 & ConsumeIntegral<uint8_t>();
*/
}
/**
| Returns a value chosen from the given
| enum.
|
=====================================
| Returns an enum value. The enum must start at
| 0 and be contiguous. It must also contain
| |kMaxValue| aliased to its largest (inclusive)
| value. Such as: enum class Foo { SomeValue,
| OtherValue, kMaxValue = OtherValue };
*/
pub fn consume_enum<T>(&mut self) -> T {
todo!();
/*
const_assert(std::is_enum<T>::value, "|T| must be an enum type.");
return static_cast<T>(
ConsumeIntegralInRange<uint32_t>(0, static_cast<uint32_t>(T::kMaxValue)));
*/
}
/**
| Returns a copy of the value selected
| from the given fixed-size |array|.
|
*/
pub fn pick_value_in_array<T, const size: usize>(&mut self, array: &[T; size]) -> T {
todo!();
/*
const_assert(size > 0, "The array must be non empty.");
return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
*/
}
pub fn pick_value_in_array_with_initlist<T>(&mut self, list: InitializerList<T>) -> T {
todo!();
/*
// TODO(Dor1s): switch to const_assert once C++14 is allowed.
if (!list.size())
abort();
return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1));
*/
}
/**
| Writes data to the given destination
| and returns number of bytes written.
|
=======================================
| Writes |num_bytes| of input data to the given
| destination pointer. If there is not enough
| data left, writes all remaining bytes. Return
| value is the number of bytes written.
|
| In general, it's better to avoid using this
| function, but it may be useful in cases when
| it's necessary to fill a certain buffer or
| object with fuzzing data.
*/
#[inline] pub fn consume_data(&mut self,
destination: *mut c_void,
num_bytes: usize) -> usize {
todo!();
/*
num_bytes = std::min(num_bytes, remaining_bytes_);
CopyAndAdvance(destination, num_bytes);
return num_bytes;
*/
}
/**
| Private methods.
|
*/
#[inline] pub fn copy_and_advance(&mut self,
destination: *mut c_void,
num_bytes: usize) {
todo!();
/*
std::memcpy(destination, data_ptr_, num_bytes);
Advance(num_bytes);
*/
}
#[inline] pub fn advance(&mut self, num_bytes: usize) {
todo!();
/*
if (num_bytes > remaining_bytes_)
abort();
data_ptr_ += num_bytes;
remaining_bytes_ -= num_bytes;
*/
}
pub fn consume_bytes_with_size<T>(&mut self,
size: usize,
num_bytes: usize) -> Vec<T> {
todo!();
/*
const_assert(sizeof(T) == sizeof(uint8_t), "Incompatible data type.");
// The point of using the size-based constructor below is to increase the
// odds of having a vector object with capacity being equal to the length.
// That part is always implementation specific, but at least both libc++ and
// libstdc++ allocate the requested number of bytes in that constructor,
// which seems to be a natural choice for other implementations as well.
// To increase the odds even more, we also call |shrink_to_fit| below.
std::vector<T> result(size);
if (size == 0) {
if (num_bytes != 0)
abort();
return result;
}
CopyAndAdvance(result.data(), num_bytes);
// Even though |shrink_to_fit| is also implementation specific, we expect it
// to provide an additional assurance in case vector's constructor allocated
// a buffer which is larger than the actual amount of data we put inside it.
result.shrink_to_fit();
return result;
*/
}
pub fn convert_unsigned_to_signed<TS, TU>(&mut self, value: TU) -> TS {
todo!();
/*
const_assert(sizeof(TS) == sizeof(TU), "Incompatible data types.");
const_assert(!std::numeric_limits<TU>::is_signed,
"Source type must be unsigned.");
// TODO(Dor1s): change to `if constexpr` once C++17 becomes mainstream.
if (std::numeric_limits<TS>::is_modulo)
return static_cast<TS>(value);
// Avoid using implementation-defined unsigned to signed conversions.
// To learn more, see https://stackoverflow.com/questions/13150449.
if (value <= std::numeric_limits<TS>::max()) {
return static_cast<TS>(value);
} else {
constexpr auto TS_min = std::numeric_limits<TS>::min();
return TS_min + static_cast<char>(value - TS_min);
}
*/
}
}