13#ifndef LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
14#define LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
23#include <initializer_list>
47 template <
typename T> std::vector<T>
ConsumeBytes(
size_t num_bytes);
77 template <
typename T,
size_t size>
82 size_t ConsumeData(
void *destination,
size_t num_bytes);
96 std::vector<T>
ConsumeBytes(
size_t size,
size_t num_bytes);
111 return ConsumeBytes<T>(num_bytes, num_bytes);
122 std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes);
123 result.back() = terminator;
138 static_assert(
sizeof(std::string::value_type) ==
sizeof(uint8_t),
139 "ConsumeBytesAsString cannot convert the data to a string.");
143 reinterpret_cast<const std::string::value_type *
>(
data_ptr_), num_bytes);
165 char next = ConvertUnsignedToSigned<char>(
data_ptr_[0]);
168 next = ConvertUnsignedToSigned<char>(
data_ptr_[0]);
176 result.shrink_to_fit();
197 std::numeric_limits<T>::max());
206 static_assert(std::is_integral<T>::value,
"An integral type is required.");
207 static_assert(
sizeof(
T) <=
sizeof(uint64_t),
"Unsupported integral type.");
213 uint64_t range =
static_cast<uint64_t
>(max) -
static_cast<uint64_t
>(min);
217 while (offset <
sizeof(
T) * CHAR_BIT && (range >> offset) > 0 &&
231 if (range != std::numeric_limits<
decltype(range)>::max())
232 result = result % (range + 1);
234 return static_cast<T>(
static_cast<uint64_t
>(min) + result);
241 return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(),
242 std::numeric_limits<T>::max());
255 constexpr T zero(.0);
256 if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) {
260 range = (max / 2.0) - (min / 2.0);
268 return result + range * ConsumeProbability<T>();
274 static_assert(std::is_floating_point<T>::value,
275 "A floating point type is required.");
280 typename std::conditional<(
sizeof(
T) <=
sizeof(uint32_t)), uint32_t,
283 T result =
static_cast<T>(ConsumeIntegral<IntegralType>());
284 result /=
static_cast<T>(std::numeric_limits<IntegralType>::max());
290 return 1 & ConsumeIntegral<uint8_t>();
297 static_assert(std::is_enum<T>::value,
"|T| must be an enum type.");
298 return static_cast<T>(
299 ConsumeIntegralInRange<uint32_t>(0,
static_cast<uint32_t
>(T::kMaxValue)));
303template <
typename T,
size_t size>
305 static_assert(size > 0,
"The array must be non empty.");
306 return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
309template <
typename T,
size_t size>
311 static_assert(size > 0,
"The array must be non empty.");
312 return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
321 return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1));
340 std::memcpy(destination,
data_ptr_, num_bytes);
354 static_assert(
sizeof(
T) ==
sizeof(uint8_t),
"Incompatible data type.");
362 std::vector<T> result(size);
374 result.shrink_to_fit();
378template <
typename TS,
typename TU>
380 static_assert(
sizeof(TS) ==
sizeof(TU),
"Incompatible data types.");
381 static_assert(!std::numeric_limits<TU>::is_signed,
382 "Source type must be unsigned.");
385 if (std::numeric_limits<TS>::is_modulo)
386 return static_cast<TS
>(value);
390 if (value <= std::numeric_limits<TS>::max()) {
391 return static_cast<TS
>(value);
393 constexpr auto TS_min = std::numeric_limits<TS>::min();
394 return TS_min +
static_cast<TS
>(value - TS_min);
void Advance(size_t num_bytes)
std::vector< T > ConsumeBytesWithTerminator(size_t num_bytes, T terminator=0)
std::string ConsumeBytesAsString(size_t num_bytes)
std::vector< T > ConsumeBytes(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
TS ConvertUnsignedToSigned(TU value)
const uint8_t * data_ptr_
size_t ConsumeData(void *destination, size_t num_bytes)
std::vector< T > ConsumeRemainingBytes()
T PickValueInArray(const T(&array)[size])
T ConsumeFloatingPointInRange(T min, T max)
std::string ConsumeRandomLengthString()
FuzzedDataProvider & operator=(const FuzzedDataProvider &)=delete
void CopyAndAdvance(void *destination, size_t num_bytes)
std::string ConsumeRemainingBytesAsString()
FuzzedDataProvider(const FuzzedDataProvider &)=delete
FuzzedDataProvider(const uint8_t *data, size_t size)
~FuzzedDataProvider()=default
#define T(expected, seed, data)