13 #ifndef LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
14 #define LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
21 #include <initializer_list>
23 #include <type_traits>
44 template <
typename T> std::vector<T>
ConsumeBytes(
size_t num_bytes);
73 template <
typename T,
size_t size> T
PickValueInArray(
const T (&array)[size]);
74 template <
typename T> T
PickValueInArray(std::initializer_list<const T> list);
77 size_t ConsumeData(
void *destination,
size_t num_bytes);
91 std::vector<T>
ConsumeBytes(
size_t size,
size_t num_bytes);
103 template <
typename T>
106 return ConsumeBytes<T>(num_bytes, num_bytes);
113 template <
typename T>
117 std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes);
118 result.back() = terminator;
123 template <
typename T>
134 "ConsumeBytesAsString cannot convert the data to a string.");
138 reinterpret_cast<const std::string::value_type *
>(
data_ptr_), num_bytes);
160 char next = ConvertUnsignedToSigned<char>(
data_ptr_[0]);
163 next = ConvertUnsignedToSigned<char>(
data_ptr_[0]);
171 result.shrink_to_fit();
199 template <
typename T>
201 static_assert(std::is_integral<T>::value,
"An integral type is required.");
212 while (offset <
sizeof(T) *
CHAR_BIT && (range >> offset) > 0 &&
226 if (range != std::numeric_limits<decltype(range)>
::max())
227 result = result % (range + 1);
229 return static_cast<T
>(
min + result);
236 return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(),
243 template <
typename T>
250 constexpr T zero(.0);
255 range = (
max / 2.0) - (
min / 2.0);
263 return result + range * ConsumeProbability<T>();
270 "A floating point type is required.");
278 T result =
static_cast<T
>(ConsumeIntegral<IntegralType>());
285 return 1 & ConsumeIntegral<uint8_t>();
292 static_assert(std::is_enum<T>::value,
"|T| must be an enum type.");
293 return static_cast<T
>(
294 ConsumeIntegralInRange<uint32_t>(0,
static_cast<uint32_t>(T::kMaxValue)));
298 template <
typename T,
size_t size>
301 return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
304 template <
typename T>
310 return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1));
341 template <
typename T>
351 std::vector<T> result(size);
363 result.shrink_to_fit();
367 template <
typename TS,
typename TU>
371 "Source type must be unsigned.");
374 if (std::numeric_limits<TS>::is_modulo)
375 return static_cast<TS>(value);
380 return static_cast<TS>(value);
383 return TS_min +
static_cast<char>(value - TS_min);
#define static_assert(x, y)
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
__DEVICE__ int min(int __a, int __b)
__DEVICE__ int max(int __a, int __b)
static __inline__ uint32_t
static __inline__ uint64_t
static __inline__ uint8_t
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()
void CopyAndAdvance(void *destination, size_t num_bytes)
std::string ConsumeRemainingBytesAsString()
FuzzedDataProvider & operator=(const FuzzedDataProvider &)=delete
FuzzedDataProvider(const FuzzedDataProvider &)=delete
FuzzedDataProvider(const uint8_t *data, size_t size)
~FuzzedDataProvider()=default