13cab2bb3Spatrick //===- FuzzedDataProvider.h - Utility header for fuzz targets ---*- C++ -* ===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick // A single header library providing an utility class to break up an array of
93cab2bb3Spatrick // bytes. Whenever run on the same input, provides the same output, as long as
103cab2bb3Spatrick // its methods are called in the same order, with the same arguments.
113cab2bb3Spatrick //===----------------------------------------------------------------------===//
123cab2bb3Spatrick
133cab2bb3Spatrick #ifndef LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
143cab2bb3Spatrick #define LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
153cab2bb3Spatrick
163cab2bb3Spatrick #include <algorithm>
17*d89ec533Spatrick #include <array>
183cab2bb3Spatrick #include <climits>
193cab2bb3Spatrick #include <cstddef>
203cab2bb3Spatrick #include <cstdint>
213cab2bb3Spatrick #include <cstring>
223cab2bb3Spatrick #include <initializer_list>
23*d89ec533Spatrick #include <limits>
243cab2bb3Spatrick #include <string>
253cab2bb3Spatrick #include <type_traits>
263cab2bb3Spatrick #include <utility>
273cab2bb3Spatrick #include <vector>
283cab2bb3Spatrick
293cab2bb3Spatrick // In addition to the comments below, the API is also briefly documented at
303cab2bb3Spatrick // https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#fuzzed-data-provider
313cab2bb3Spatrick class FuzzedDataProvider {
323cab2bb3Spatrick public:
333cab2bb3Spatrick // |data| is an array of length |size| that the FuzzedDataProvider wraps to
343cab2bb3Spatrick // provide more granular access. |data| must outlive the FuzzedDataProvider.
FuzzedDataProvider(const uint8_t * data,size_t size)353cab2bb3Spatrick FuzzedDataProvider(const uint8_t *data, size_t size)
363cab2bb3Spatrick : data_ptr_(data), remaining_bytes_(size) {}
373cab2bb3Spatrick ~FuzzedDataProvider() = default;
383cab2bb3Spatrick
391f9cb04fSpatrick // See the implementation below (after the class definition) for more verbose
401f9cb04fSpatrick // comments for each of the methods.
411f9cb04fSpatrick
421f9cb04fSpatrick // Methods returning std::vector of bytes. These are the most popular choice
431f9cb04fSpatrick // when splitting fuzzing input into pieces, as every piece is put into a
441f9cb04fSpatrick // separate buffer (i.e. ASan would catch any under-/overflow) and the memory
451f9cb04fSpatrick // will be released automatically.
461f9cb04fSpatrick template <typename T> std::vector<T> ConsumeBytes(size_t num_bytes);
471f9cb04fSpatrick template <typename T>
481f9cb04fSpatrick std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes, T terminator = 0);
491f9cb04fSpatrick template <typename T> std::vector<T> ConsumeRemainingBytes();
501f9cb04fSpatrick
511f9cb04fSpatrick // Methods returning strings. Use only when you need a std::string or a null
521f9cb04fSpatrick // terminated C-string. Otherwise, prefer the methods returning std::vector.
531f9cb04fSpatrick std::string ConsumeBytesAsString(size_t num_bytes);
541f9cb04fSpatrick std::string ConsumeRandomLengthString(size_t max_length);
551f9cb04fSpatrick std::string ConsumeRandomLengthString();
561f9cb04fSpatrick std::string ConsumeRemainingBytesAsString();
571f9cb04fSpatrick
581f9cb04fSpatrick // Methods returning integer values.
591f9cb04fSpatrick template <typename T> T ConsumeIntegral();
601f9cb04fSpatrick template <typename T> T ConsumeIntegralInRange(T min, T max);
611f9cb04fSpatrick
621f9cb04fSpatrick // Methods returning floating point values.
631f9cb04fSpatrick template <typename T> T ConsumeFloatingPoint();
641f9cb04fSpatrick template <typename T> T ConsumeFloatingPointInRange(T min, T max);
651f9cb04fSpatrick
661f9cb04fSpatrick // 0 <= return value <= 1.
671f9cb04fSpatrick template <typename T> T ConsumeProbability();
681f9cb04fSpatrick
691f9cb04fSpatrick bool ConsumeBool();
701f9cb04fSpatrick
711f9cb04fSpatrick // Returns a value chosen from the given enum.
721f9cb04fSpatrick template <typename T> T ConsumeEnum();
731f9cb04fSpatrick
741f9cb04fSpatrick // Returns a value from the given array.
751f9cb04fSpatrick template <typename T, size_t size> T PickValueInArray(const T (&array)[size]);
76*d89ec533Spatrick template <typename T, size_t size>
77*d89ec533Spatrick T PickValueInArray(const std::array<T, size> &array);
781f9cb04fSpatrick template <typename T> T PickValueInArray(std::initializer_list<const T> list);
791f9cb04fSpatrick
801f9cb04fSpatrick // Writes data to the given destination and returns number of bytes written.
811f9cb04fSpatrick size_t ConsumeData(void *destination, size_t num_bytes);
821f9cb04fSpatrick
831f9cb04fSpatrick // Reports the remaining bytes available for fuzzed input.
remaining_bytes()841f9cb04fSpatrick size_t remaining_bytes() { return remaining_bytes_; }
851f9cb04fSpatrick
861f9cb04fSpatrick private:
871f9cb04fSpatrick FuzzedDataProvider(const FuzzedDataProvider &) = delete;
881f9cb04fSpatrick FuzzedDataProvider &operator=(const FuzzedDataProvider &) = delete;
891f9cb04fSpatrick
901f9cb04fSpatrick void CopyAndAdvance(void *destination, size_t num_bytes);
911f9cb04fSpatrick
921f9cb04fSpatrick void Advance(size_t num_bytes);
931f9cb04fSpatrick
941f9cb04fSpatrick template <typename T>
951f9cb04fSpatrick std::vector<T> ConsumeBytes(size_t size, size_t num_bytes);
961f9cb04fSpatrick
971f9cb04fSpatrick template <typename TS, typename TU> TS ConvertUnsignedToSigned(TU value);
981f9cb04fSpatrick
991f9cb04fSpatrick const uint8_t *data_ptr_;
1001f9cb04fSpatrick size_t remaining_bytes_;
1011f9cb04fSpatrick };
1021f9cb04fSpatrick
1033cab2bb3Spatrick // Returns a std::vector containing |num_bytes| of input data. If fewer than
1043cab2bb3Spatrick // |num_bytes| of data remain, returns a shorter std::vector containing all
1053cab2bb3Spatrick // of the data that's left. Can be used with any byte sized type, such as
1063cab2bb3Spatrick // char, unsigned char, uint8_t, etc.
1071f9cb04fSpatrick template <typename T>
ConsumeBytes(size_t num_bytes)1081f9cb04fSpatrick std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t num_bytes) {
1093cab2bb3Spatrick num_bytes = std::min(num_bytes, remaining_bytes_);
1103cab2bb3Spatrick return ConsumeBytes<T>(num_bytes, num_bytes);
1113cab2bb3Spatrick }
1123cab2bb3Spatrick
1133cab2bb3Spatrick // Similar to |ConsumeBytes|, but also appends the terminator value at the end
1143cab2bb3Spatrick // of the resulting vector. Useful, when a mutable null-terminated C-string is
1153cab2bb3Spatrick // needed, for example. But that is a rare case. Better avoid it, if possible,
1163cab2bb3Spatrick // and prefer using |ConsumeBytes| or |ConsumeBytesAsString| methods.
1173cab2bb3Spatrick template <typename T>
ConsumeBytesWithTerminator(size_t num_bytes,T terminator)1181f9cb04fSpatrick std::vector<T> FuzzedDataProvider::ConsumeBytesWithTerminator(size_t num_bytes,
1191f9cb04fSpatrick T terminator) {
1203cab2bb3Spatrick num_bytes = std::min(num_bytes, remaining_bytes_);
1213cab2bb3Spatrick std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes);
1223cab2bb3Spatrick result.back() = terminator;
1233cab2bb3Spatrick return result;
1243cab2bb3Spatrick }
1253cab2bb3Spatrick
1261f9cb04fSpatrick // Returns a std::vector containing all remaining bytes of the input data.
1271f9cb04fSpatrick template <typename T>
ConsumeRemainingBytes()1281f9cb04fSpatrick std::vector<T> FuzzedDataProvider::ConsumeRemainingBytes() {
1291f9cb04fSpatrick return ConsumeBytes<T>(remaining_bytes_);
1301f9cb04fSpatrick }
1311f9cb04fSpatrick
1323cab2bb3Spatrick // Returns a std::string containing |num_bytes| of input data. Using this and
1333cab2bb3Spatrick // |.c_str()| on the resulting string is the best way to get an immutable
1343cab2bb3Spatrick // null-terminated C string. If fewer than |num_bytes| of data remain, returns
1353cab2bb3Spatrick // a shorter std::string containing all of the data that's left.
ConsumeBytesAsString(size_t num_bytes)1361f9cb04fSpatrick inline std::string FuzzedDataProvider::ConsumeBytesAsString(size_t num_bytes) {
1373cab2bb3Spatrick static_assert(sizeof(std::string::value_type) == sizeof(uint8_t),
1383cab2bb3Spatrick "ConsumeBytesAsString cannot convert the data to a string.");
1393cab2bb3Spatrick
1403cab2bb3Spatrick num_bytes = std::min(num_bytes, remaining_bytes_);
1413cab2bb3Spatrick std::string result(
1421f9cb04fSpatrick reinterpret_cast<const std::string::value_type *>(data_ptr_), num_bytes);
1433cab2bb3Spatrick Advance(num_bytes);
1443cab2bb3Spatrick return result;
1453cab2bb3Spatrick }
1463cab2bb3Spatrick
1471f9cb04fSpatrick // Returns a std::string of length from 0 to |max_length|. When it runs out of
1481f9cb04fSpatrick // input data, returns what remains of the input. Designed to be more stable
1491f9cb04fSpatrick // with respect to a fuzzer inserting characters than just picking a random
1501f9cb04fSpatrick // length and then consuming that many bytes with |ConsumeBytes|.
1511f9cb04fSpatrick inline std::string
ConsumeRandomLengthString(size_t max_length)1521f9cb04fSpatrick FuzzedDataProvider::ConsumeRandomLengthString(size_t max_length) {
1531f9cb04fSpatrick // Reads bytes from the start of |data_ptr_|. Maps "\\" to "\", and maps "\"
1541f9cb04fSpatrick // followed by anything else to the end of the string. As a result of this
1551f9cb04fSpatrick // logic, a fuzzer can insert characters into the string, and the string
1561f9cb04fSpatrick // will be lengthened to include those new characters, resulting in a more
1571f9cb04fSpatrick // stable fuzzer than picking the length of a string independently from
1581f9cb04fSpatrick // picking its contents.
1591f9cb04fSpatrick std::string result;
1601f9cb04fSpatrick
1611f9cb04fSpatrick // Reserve the anticipated capaticity to prevent several reallocations.
1621f9cb04fSpatrick result.reserve(std::min(max_length, remaining_bytes_));
1631f9cb04fSpatrick for (size_t i = 0; i < max_length && remaining_bytes_ != 0; ++i) {
1641f9cb04fSpatrick char next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
1651f9cb04fSpatrick Advance(1);
1661f9cb04fSpatrick if (next == '\\' && remaining_bytes_ != 0) {
1671f9cb04fSpatrick next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
1681f9cb04fSpatrick Advance(1);
1691f9cb04fSpatrick if (next != '\\')
1701f9cb04fSpatrick break;
1711f9cb04fSpatrick }
1721f9cb04fSpatrick result += next;
1731f9cb04fSpatrick }
1741f9cb04fSpatrick
1751f9cb04fSpatrick result.shrink_to_fit();
1761f9cb04fSpatrick return result;
1771f9cb04fSpatrick }
1781f9cb04fSpatrick
1791f9cb04fSpatrick // Returns a std::string of length from 0 to |remaining_bytes_|.
ConsumeRandomLengthString()1801f9cb04fSpatrick inline std::string FuzzedDataProvider::ConsumeRandomLengthString() {
1811f9cb04fSpatrick return ConsumeRandomLengthString(remaining_bytes_);
1821f9cb04fSpatrick }
1831f9cb04fSpatrick
1841f9cb04fSpatrick // Returns a std::string containing all remaining bytes of the input data.
1851f9cb04fSpatrick // Prefer using |ConsumeRemainingBytes| unless you actually need a std::string
1861f9cb04fSpatrick // object.
ConsumeRemainingBytesAsString()1871f9cb04fSpatrick inline std::string FuzzedDataProvider::ConsumeRemainingBytesAsString() {
1881f9cb04fSpatrick return ConsumeBytesAsString(remaining_bytes_);
1891f9cb04fSpatrick }
1901f9cb04fSpatrick
1911f9cb04fSpatrick // Returns a number in the range [Type's min, Type's max]. The value might
1921f9cb04fSpatrick // not be uniformly distributed in the given range. If there's no input data
1931f9cb04fSpatrick // left, always returns |min|.
ConsumeIntegral()1941f9cb04fSpatrick template <typename T> T FuzzedDataProvider::ConsumeIntegral() {
1951f9cb04fSpatrick return ConsumeIntegralInRange(std::numeric_limits<T>::min(),
1961f9cb04fSpatrick std::numeric_limits<T>::max());
1971f9cb04fSpatrick }
1981f9cb04fSpatrick
1993cab2bb3Spatrick // Returns a number in the range [min, max] by consuming bytes from the
2003cab2bb3Spatrick // input data. The value might not be uniformly distributed in the given
2013cab2bb3Spatrick // range. If there's no input data left, always returns |min|. |min| must
2023cab2bb3Spatrick // be less than or equal to |max|.
2031f9cb04fSpatrick template <typename T>
ConsumeIntegralInRange(T min,T max)2041f9cb04fSpatrick T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
2053cab2bb3Spatrick static_assert(std::is_integral<T>::value, "An integral type is required.");
2063cab2bb3Spatrick static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
2073cab2bb3Spatrick
2083cab2bb3Spatrick if (min > max)
2093cab2bb3Spatrick abort();
2103cab2bb3Spatrick
2113cab2bb3Spatrick // Use the biggest type possible to hold the range and the result.
2123cab2bb3Spatrick uint64_t range = static_cast<uint64_t>(max) - min;
2133cab2bb3Spatrick uint64_t result = 0;
2143cab2bb3Spatrick size_t offset = 0;
2153cab2bb3Spatrick
2163cab2bb3Spatrick while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
2173cab2bb3Spatrick remaining_bytes_ != 0) {
2183cab2bb3Spatrick // Pull bytes off the end of the seed data. Experimentally, this seems to
2193cab2bb3Spatrick // allow the fuzzer to more easily explore the input space. This makes
2203cab2bb3Spatrick // sense, since it works by modifying inputs that caused new code to run,
2213cab2bb3Spatrick // and this data is often used to encode length of data read by
2223cab2bb3Spatrick // |ConsumeBytes|. Separating out read lengths makes it easier modify the
2233cab2bb3Spatrick // contents of the data that is actually read.
2243cab2bb3Spatrick --remaining_bytes_;
2253cab2bb3Spatrick result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
2263cab2bb3Spatrick offset += CHAR_BIT;
2273cab2bb3Spatrick }
2283cab2bb3Spatrick
2293cab2bb3Spatrick // Avoid division by 0, in case |range + 1| results in overflow.
2303cab2bb3Spatrick if (range != std::numeric_limits<decltype(range)>::max())
2313cab2bb3Spatrick result = result % (range + 1);
2323cab2bb3Spatrick
2333cab2bb3Spatrick return static_cast<T>(min + result);
2343cab2bb3Spatrick }
2353cab2bb3Spatrick
2363cab2bb3Spatrick // Returns a floating point value in the range [Type's lowest, Type's max] by
2373cab2bb3Spatrick // consuming bytes from the input data. If there's no input data left, always
2383cab2bb3Spatrick // returns approximately 0.
ConsumeFloatingPoint()2391f9cb04fSpatrick template <typename T> T FuzzedDataProvider::ConsumeFloatingPoint() {
2403cab2bb3Spatrick return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(),
2413cab2bb3Spatrick std::numeric_limits<T>::max());
2423cab2bb3Spatrick }
2433cab2bb3Spatrick
2443cab2bb3Spatrick // Returns a floating point value in the given range by consuming bytes from
2453cab2bb3Spatrick // the input data. If there's no input data left, returns |min|. Note that
2463cab2bb3Spatrick // |min| must be less than or equal to |max|.
2471f9cb04fSpatrick template <typename T>
ConsumeFloatingPointInRange(T min,T max)2481f9cb04fSpatrick T FuzzedDataProvider::ConsumeFloatingPointInRange(T min, T max) {
2493cab2bb3Spatrick if (min > max)
2503cab2bb3Spatrick abort();
2513cab2bb3Spatrick
2523cab2bb3Spatrick T range = .0;
2533cab2bb3Spatrick T result = min;
2543cab2bb3Spatrick constexpr T zero(.0);
2553cab2bb3Spatrick if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) {
2563cab2bb3Spatrick // The diff |max - min| would overflow the given floating point type. Use
2573cab2bb3Spatrick // the half of the diff as the range and consume a bool to decide whether
2583cab2bb3Spatrick // the result is in the first of the second part of the diff.
2593cab2bb3Spatrick range = (max / 2.0) - (min / 2.0);
2603cab2bb3Spatrick if (ConsumeBool()) {
2613cab2bb3Spatrick result += range;
2623cab2bb3Spatrick }
2633cab2bb3Spatrick } else {
2643cab2bb3Spatrick range = max - min;
2653cab2bb3Spatrick }
2663cab2bb3Spatrick
2673cab2bb3Spatrick return result + range * ConsumeProbability<T>();
2683cab2bb3Spatrick }
2693cab2bb3Spatrick
2701f9cb04fSpatrick // Returns a floating point number in the range [0.0, 1.0]. If there's no
2711f9cb04fSpatrick // input data left, always returns 0.
ConsumeProbability()2721f9cb04fSpatrick template <typename T> T FuzzedDataProvider::ConsumeProbability() {
2731f9cb04fSpatrick static_assert(std::is_floating_point<T>::value,
2741f9cb04fSpatrick "A floating point type is required.");
2753cab2bb3Spatrick
2761f9cb04fSpatrick // Use different integral types for different floating point types in order
2771f9cb04fSpatrick // to provide better density of the resulting values.
2781f9cb04fSpatrick using IntegralType =
2791f9cb04fSpatrick typename std::conditional<(sizeof(T) <= sizeof(uint32_t)), uint32_t,
2801f9cb04fSpatrick uint64_t>::type;
2813cab2bb3Spatrick
2821f9cb04fSpatrick T result = static_cast<T>(ConsumeIntegral<IntegralType>());
2831f9cb04fSpatrick result /= static_cast<T>(std::numeric_limits<IntegralType>::max());
2841f9cb04fSpatrick return result;
2851f9cb04fSpatrick }
2861f9cb04fSpatrick
2871f9cb04fSpatrick // Reads one byte and returns a bool, or false when no data remains.
ConsumeBool()2881f9cb04fSpatrick inline bool FuzzedDataProvider::ConsumeBool() {
2891f9cb04fSpatrick return 1 & ConsumeIntegral<uint8_t>();
2901f9cb04fSpatrick }
2911f9cb04fSpatrick
2921f9cb04fSpatrick // Returns an enum value. The enum must start at 0 and be contiguous. It must
2931f9cb04fSpatrick // also contain |kMaxValue| aliased to its largest (inclusive) value. Such as:
2941f9cb04fSpatrick // enum class Foo { SomeValue, OtherValue, kMaxValue = OtherValue };
ConsumeEnum()2951f9cb04fSpatrick template <typename T> T FuzzedDataProvider::ConsumeEnum() {
2961f9cb04fSpatrick static_assert(std::is_enum<T>::value, "|T| must be an enum type.");
2971f9cb04fSpatrick return static_cast<T>(
2981f9cb04fSpatrick ConsumeIntegralInRange<uint32_t>(0, static_cast<uint32_t>(T::kMaxValue)));
2991f9cb04fSpatrick }
3001f9cb04fSpatrick
3011f9cb04fSpatrick // Returns a copy of the value selected from the given fixed-size |array|.
3021f9cb04fSpatrick template <typename T, size_t size>
PickValueInArray(const T (& array)[size])3031f9cb04fSpatrick T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) {
3041f9cb04fSpatrick static_assert(size > 0, "The array must be non empty.");
3051f9cb04fSpatrick return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
3061f9cb04fSpatrick }
3071f9cb04fSpatrick
308*d89ec533Spatrick template <typename T, size_t size>
PickValueInArray(const std::array<T,size> & array)309*d89ec533Spatrick T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) {
310*d89ec533Spatrick static_assert(size > 0, "The array must be non empty.");
311*d89ec533Spatrick return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
312*d89ec533Spatrick }
313*d89ec533Spatrick
3141f9cb04fSpatrick template <typename T>
PickValueInArray(std::initializer_list<const T> list)3151f9cb04fSpatrick T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) {
3161f9cb04fSpatrick // TODO(Dor1s): switch to static_assert once C++14 is allowed.
3171f9cb04fSpatrick if (!list.size())
3181f9cb04fSpatrick abort();
3191f9cb04fSpatrick
3201f9cb04fSpatrick return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1));
3211f9cb04fSpatrick }
3221f9cb04fSpatrick
3231f9cb04fSpatrick // Writes |num_bytes| of input data to the given destination pointer. If there
3241f9cb04fSpatrick // is not enough data left, writes all remaining bytes. Return value is the
3251f9cb04fSpatrick // number of bytes written.
3261f9cb04fSpatrick // In general, it's better to avoid using this function, but it may be useful
3271f9cb04fSpatrick // in cases when it's necessary to fill a certain buffer or object with
3281f9cb04fSpatrick // fuzzing data.
ConsumeData(void * destination,size_t num_bytes)3291f9cb04fSpatrick inline size_t FuzzedDataProvider::ConsumeData(void *destination,
3301f9cb04fSpatrick size_t num_bytes) {
3311f9cb04fSpatrick num_bytes = std::min(num_bytes, remaining_bytes_);
3321f9cb04fSpatrick CopyAndAdvance(destination, num_bytes);
3331f9cb04fSpatrick return num_bytes;
3341f9cb04fSpatrick }
3351f9cb04fSpatrick
3361f9cb04fSpatrick // Private methods.
CopyAndAdvance(void * destination,size_t num_bytes)3371f9cb04fSpatrick inline void FuzzedDataProvider::CopyAndAdvance(void *destination,
3381f9cb04fSpatrick size_t num_bytes) {
3391f9cb04fSpatrick std::memcpy(destination, data_ptr_, num_bytes);
3401f9cb04fSpatrick Advance(num_bytes);
3411f9cb04fSpatrick }
3421f9cb04fSpatrick
Advance(size_t num_bytes)3431f9cb04fSpatrick inline void FuzzedDataProvider::Advance(size_t num_bytes) {
3443cab2bb3Spatrick if (num_bytes > remaining_bytes_)
3453cab2bb3Spatrick abort();
3463cab2bb3Spatrick
3473cab2bb3Spatrick data_ptr_ += num_bytes;
3483cab2bb3Spatrick remaining_bytes_ -= num_bytes;
3493cab2bb3Spatrick }
3503cab2bb3Spatrick
3513cab2bb3Spatrick template <typename T>
ConsumeBytes(size_t size,size_t num_bytes)3521f9cb04fSpatrick std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t size, size_t num_bytes) {
3533cab2bb3Spatrick static_assert(sizeof(T) == sizeof(uint8_t), "Incompatible data type.");
3543cab2bb3Spatrick
3553cab2bb3Spatrick // The point of using the size-based constructor below is to increase the
3563cab2bb3Spatrick // odds of having a vector object with capacity being equal to the length.
3573cab2bb3Spatrick // That part is always implementation specific, but at least both libc++ and
3583cab2bb3Spatrick // libstdc++ allocate the requested number of bytes in that constructor,
3593cab2bb3Spatrick // which seems to be a natural choice for other implementations as well.
3603cab2bb3Spatrick // To increase the odds even more, we also call |shrink_to_fit| below.
3613cab2bb3Spatrick std::vector<T> result(size);
3623cab2bb3Spatrick if (size == 0) {
3631f9cb04fSpatrick if (num_bytes != 0)
3643cab2bb3Spatrick abort();
3653cab2bb3Spatrick return result;
3663cab2bb3Spatrick }
3673cab2bb3Spatrick
3681f9cb04fSpatrick CopyAndAdvance(result.data(), num_bytes);
3693cab2bb3Spatrick
3703cab2bb3Spatrick // Even though |shrink_to_fit| is also implementation specific, we expect it
3713cab2bb3Spatrick // to provide an additional assurance in case vector's constructor allocated
3723cab2bb3Spatrick // a buffer which is larger than the actual amount of data we put inside it.
3733cab2bb3Spatrick result.shrink_to_fit();
3743cab2bb3Spatrick return result;
3753cab2bb3Spatrick }
3763cab2bb3Spatrick
3771f9cb04fSpatrick template <typename TS, typename TU>
ConvertUnsignedToSigned(TU value)3781f9cb04fSpatrick TS FuzzedDataProvider::ConvertUnsignedToSigned(TU value) {
3793cab2bb3Spatrick static_assert(sizeof(TS) == sizeof(TU), "Incompatible data types.");
3803cab2bb3Spatrick static_assert(!std::numeric_limits<TU>::is_signed,
3813cab2bb3Spatrick "Source type must be unsigned.");
3823cab2bb3Spatrick
3833cab2bb3Spatrick // TODO(Dor1s): change to `if constexpr` once C++17 becomes mainstream.
3843cab2bb3Spatrick if (std::numeric_limits<TS>::is_modulo)
3853cab2bb3Spatrick return static_cast<TS>(value);
3863cab2bb3Spatrick
3871f9cb04fSpatrick // Avoid using implementation-defined unsigned to signed conversions.
3883cab2bb3Spatrick // To learn more, see https://stackoverflow.com/questions/13150449.
3893cab2bb3Spatrick if (value <= std::numeric_limits<TS>::max()) {
3903cab2bb3Spatrick return static_cast<TS>(value);
3913cab2bb3Spatrick } else {
3923cab2bb3Spatrick constexpr auto TS_min = std::numeric_limits<TS>::min();
393*d89ec533Spatrick return TS_min + static_cast<TS>(value - TS_min);
3943cab2bb3Spatrick }
3953cab2bb3Spatrick }
3963cab2bb3Spatrick
3973cab2bb3Spatrick #endif // LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
398