fuzz: Update FuzzedDataProvider.h from upstream (LLVM)

Upstream revision: 6d0488f75b/compiler-rt/include/fuzzer/FuzzedDataProvider.h

Changes:
* [compiler-rt] FuzzedDataProvider: add ConsumeData and method.
* [compiler-rt] Fix a typo in a comment in FuzzedDataProvider.h.
* [compiler-rt] Add ConsumeRandomLengthString() version without arguments.
* [compiler-rt] Refactor FuzzedDataProvider for better readability.
* [compiler-rt] FuzzedDataProvider: make linter happy.
* [compiler-rt] Mark FDP non-template methods inline to avoid ODR violations.
pull/826/head
practicalswift 4 years ago
parent f4ac48d30a
commit e3d2ba7c70

@ -34,11 +34,74 @@ class FuzzedDataProvider {
: data_ptr_(data), remaining_bytes_(size) {} : data_ptr_(data), remaining_bytes_(size) {}
~FuzzedDataProvider() = default; ~FuzzedDataProvider() = default;
// See the implementation below (after the class definition) for more verbose
// comments for each of the methods.
// 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.
template <typename T> std::vector<T> ConsumeBytes(size_t num_bytes);
template <typename T>
std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes, T terminator = 0);
template <typename T> std::vector<T> ConsumeRemainingBytes();
// Methods returning strings. Use only when you need a std::string or a null
// terminated C-string. Otherwise, prefer the methods returning std::vector.
std::string ConsumeBytesAsString(size_t num_bytes);
std::string ConsumeRandomLengthString(size_t max_length);
std::string ConsumeRandomLengthString();
std::string ConsumeRemainingBytesAsString();
// Methods returning integer values.
template <typename T> T ConsumeIntegral();
template <typename T> T ConsumeIntegralInRange(T min, T max);
// Methods returning floating point values.
template <typename T> T ConsumeFloatingPoint();
template <typename T> T ConsumeFloatingPointInRange(T min, T max);
// 0 <= return value <= 1.
template <typename T> T ConsumeProbability();
bool ConsumeBool();
// Returns a value chosen from the given enum.
template <typename T> T ConsumeEnum();
// Returns a value from the given array.
template <typename T, size_t size> T PickValueInArray(const T (&array)[size]);
template <typename T> T PickValueInArray(std::initializer_list<const T> list);
// Writes data to the given destination and returns number of bytes written.
size_t ConsumeData(void *destination, size_t num_bytes);
// Reports the remaining bytes available for fuzzed input.
size_t remaining_bytes() { return remaining_bytes_; }
private:
FuzzedDataProvider(const FuzzedDataProvider &) = delete;
FuzzedDataProvider &operator=(const FuzzedDataProvider &) = delete;
void CopyAndAdvance(void *destination, size_t num_bytes);
void Advance(size_t num_bytes);
template <typename T>
std::vector<T> ConsumeBytes(size_t size, size_t num_bytes);
template <typename TS, typename TU> TS ConvertUnsignedToSigned(TU value);
const uint8_t *data_ptr_;
size_t remaining_bytes_;
};
// Returns a std::vector containing |num_bytes| of input data. If fewer than // 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 // |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 // of the data that's left. Can be used with any byte sized type, such as
// char, unsigned char, uint8_t, etc. // char, unsigned char, uint8_t, etc.
template <typename T> std::vector<T> ConsumeBytes(size_t num_bytes) { template <typename T>
std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t num_bytes) {
num_bytes = std::min(num_bytes, remaining_bytes_); num_bytes = std::min(num_bytes, remaining_bytes_);
return ConsumeBytes<T>(num_bytes, num_bytes); return ConsumeBytes<T>(num_bytes, num_bytes);
} }
@ -48,71 +111,41 @@ class FuzzedDataProvider {
// needed, for example. But that is a rare case. Better avoid it, if possible, // needed, for example. But that is a rare case. Better avoid it, if possible,
// and prefer using |ConsumeBytes| or |ConsumeBytesAsString| methods. // and prefer using |ConsumeBytes| or |ConsumeBytesAsString| methods.
template <typename T> template <typename T>
std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes, std::vector<T> FuzzedDataProvider::ConsumeBytesWithTerminator(size_t num_bytes,
T terminator = 0) { T terminator) {
num_bytes = std::min(num_bytes, remaining_bytes_); num_bytes = std::min(num_bytes, remaining_bytes_);
std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes); std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes);
result.back() = terminator; result.back() = terminator;
return result; return result;
} }
// Returns a std::vector containing all remaining bytes of the input data.
template <typename T>
std::vector<T> FuzzedDataProvider::ConsumeRemainingBytes() {
return ConsumeBytes<T>(remaining_bytes_);
}
// Returns a std::string containing |num_bytes| of input data. Using this and // 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 // |.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 // 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. // a shorter std::string containing all of the data that's left.
std::string ConsumeBytesAsString(size_t num_bytes) { inline std::string FuzzedDataProvider::ConsumeBytesAsString(size_t num_bytes) {
static_assert(sizeof(std::string::value_type) == sizeof(uint8_t), static_assert(sizeof(std::string::value_type) == sizeof(uint8_t),
"ConsumeBytesAsString cannot convert the data to a string."); "ConsumeBytesAsString cannot convert the data to a string.");
num_bytes = std::min(num_bytes, remaining_bytes_); num_bytes = std::min(num_bytes, remaining_bytes_);
std::string result( std::string result(
reinterpret_cast<const std::string::value_type *>(data_ptr_), reinterpret_cast<const std::string::value_type *>(data_ptr_), num_bytes);
num_bytes);
Advance(num_bytes); Advance(num_bytes);
return result; return result;
} }
// 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|.
template <typename T> T ConsumeIntegralInRange(T min, T max) {
static_assert(std::is_integral<T>::value, "An integral type is required.");
static_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);
}
// Returns a std::string of length from 0 to |max_length|. When it runs out of // 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 // 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 // with respect to a fuzzer inserting characters than just picking a random
// length and then consuming that many bytes with |ConsumeBytes|. // length and then consuming that many bytes with |ConsumeBytes|.
std::string ConsumeRandomLengthString(size_t max_length) { inline std::string
FuzzedDataProvider::ConsumeRandomLengthString(size_t max_length) {
// Reads bytes from the start of |data_ptr_|. Maps "\\" to "\", and maps "\" // 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 // 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 // logic, a fuzzer can insert characters into the string, and the string
@ -139,75 +172,67 @@ class FuzzedDataProvider {
return result; return result;
} }
// Returns a std::vector containing all remaining bytes of the input data. // Returns a std::string of length from 0 to |remaining_bytes_|.
template <typename T> std::vector<T> ConsumeRemainingBytes() { inline std::string FuzzedDataProvider::ConsumeRandomLengthString() {
return ConsumeBytes<T>(remaining_bytes_); return ConsumeRandomLengthString(remaining_bytes_);
} }
// Returns a std::string containing all remaining bytes of the input data. // Returns a std::string containing all remaining bytes of the input data.
// Prefer using |ConsumeRemainingBytes| unless you actually need a std::string // Prefer using |ConsumeRemainingBytes| unless you actually need a std::string
// object. // object.
std::string ConsumeRemainingBytesAsString() { inline std::string FuzzedDataProvider::ConsumeRemainingBytesAsString() {
return ConsumeBytesAsString(remaining_bytes_); return ConsumeBytesAsString(remaining_bytes_);
} }
// Returns a number in the range [Type's min, Type's max]. The value might // 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 // not be uniformly distributed in the given range. If there's no input data
// left, always returns |min|. // left, always returns |min|.
template <typename T> T ConsumeIntegral() { template <typename T> T FuzzedDataProvider::ConsumeIntegral() {
return ConsumeIntegralInRange(std::numeric_limits<T>::min(), return ConsumeIntegralInRange(std::numeric_limits<T>::min(),
std::numeric_limits<T>::max()); std::numeric_limits<T>::max());
} }
// Reads one byte and returns a bool, or false when no data remains. // Returns a number in the range [min, max] by consuming bytes from the
bool ConsumeBool() { return 1 & ConsumeIntegral<uint8_t>(); } // 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
// Returns a copy of the value selected from the given fixed-size |array|. // be less than or equal to |max|.
template <typename T, size_t size>
T PickValueInArray(const T (&array)[size]) {
static_assert(size > 0, "The array must be non empty.");
return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
}
template <typename T> template <typename T>
T PickValueInArray(std::initializer_list<const T> list) { T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
// TODO(Dor1s): switch to static_assert once C++14 is allowed. static_assert(std::is_integral<T>::value, "An integral type is required.");
if (!list.size()) static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
if (min > max)
abort(); abort();
return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); // 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;
// Returns an enum value. The enum must start at 0 and be contiguous. It must while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
// also contain |kMaxValue| aliased to its largest (inclusive) value. Such as: remaining_bytes_ != 0) {
// enum class Foo { SomeValue, OtherValue, kMaxValue = OtherValue }; // Pull bytes off the end of the seed data. Experimentally, this seems to
template <typename T> T ConsumeEnum() { // allow the fuzzer to more easily explore the input space. This makes
static_assert(std::is_enum<T>::value, "|T| must be an enum type."); // sense, since it works by modifying inputs that caused new code to run,
return static_cast<T>(ConsumeIntegralInRange<uint32_t>( // and this data is often used to encode length of data read by
0, static_cast<uint32_t>(T::kMaxValue))); // |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;
} }
// Returns a floating point number in the range [0.0, 1.0]. If there's no // Avoid division by 0, in case |range + 1| results in overflow.
// input data left, always returns 0. if (range != std::numeric_limits<decltype(range)>::max())
template <typename T> T ConsumeProbability() { result = result % (range + 1);
static_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>()); return static_cast<T>(min + result);
result /= static_cast<T>(std::numeric_limits<IntegralType>::max());
return result;
} }
// Returns a floating point value in the range [Type's lowest, Type's max] by // 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 // consuming bytes from the input data. If there's no input data left, always
// returns approximately 0. // returns approximately 0.
template <typename T> T ConsumeFloatingPoint() { template <typename T> T FuzzedDataProvider::ConsumeFloatingPoint() {
return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(), return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(),
std::numeric_limits<T>::max()); std::numeric_limits<T>::max());
} }
@ -215,7 +240,8 @@ class FuzzedDataProvider {
// Returns a floating point value in the given range by consuming bytes from // 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 // the input data. If there's no input data left, returns |min|. Note that
// |min| must be less than or equal to |max|. // |min| must be less than or equal to |max|.
template <typename T> T ConsumeFloatingPointInRange(T min, T max) { template <typename T>
T FuzzedDataProvider::ConsumeFloatingPointInRange(T min, T max) {
if (min > max) if (min > max)
abort(); abort();
@ -237,14 +263,74 @@ class FuzzedDataProvider {
return result + range * ConsumeProbability<T>(); return result + range * ConsumeProbability<T>();
} }
// Reports the remaining bytes available for fuzzed input. // Returns a floating point number in the range [0.0, 1.0]. If there's no
size_t remaining_bytes() { return remaining_bytes_; } // input data left, always returns 0.
template <typename T> T FuzzedDataProvider::ConsumeProbability() {
static_assert(std::is_floating_point<T>::value,
"A floating point type is required.");
private: // Use different integral types for different floating point types in order
FuzzedDataProvider(const FuzzedDataProvider &) = delete; // to provide better density of the resulting values.
FuzzedDataProvider &operator=(const FuzzedDataProvider &) = delete; 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 bool FuzzedDataProvider::ConsumeBool() {
return 1 & ConsumeIntegral<uint8_t>();
}
// 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 };
template <typename T> T FuzzedDataProvider::ConsumeEnum() {
static_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|.
template <typename T, size_t size>
T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) {
static_assert(size > 0, "The array must be non empty.");
return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
}
template <typename T>
T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) {
// TODO(Dor1s): switch to static_assert once C++14 is allowed.
if (!list.size())
abort();
return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1));
}
// 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 size_t FuzzedDataProvider::ConsumeData(void *destination,
size_t num_bytes) {
num_bytes = std::min(num_bytes, remaining_bytes_);
CopyAndAdvance(destination, num_bytes);
return num_bytes;
}
void Advance(size_t num_bytes) { // Private methods.
inline void FuzzedDataProvider::CopyAndAdvance(void *destination,
size_t num_bytes) {
std::memcpy(destination, data_ptr_, num_bytes);
Advance(num_bytes);
}
inline void FuzzedDataProvider::Advance(size_t num_bytes) {
if (num_bytes > remaining_bytes_) if (num_bytes > remaining_bytes_)
abort(); abort();
@ -253,7 +339,7 @@ class FuzzedDataProvider {
} }
template <typename T> template <typename T>
std::vector<T> ConsumeBytes(size_t size, size_t num_bytes_to_consume) { std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t size, size_t num_bytes) {
static_assert(sizeof(T) == sizeof(uint8_t), "Incompatible data type."); static_assert(sizeof(T) == sizeof(uint8_t), "Incompatible data type.");
// The point of using the size-based constructor below is to increase the // The point of using the size-based constructor below is to increase the
@ -264,13 +350,12 @@ class FuzzedDataProvider {
// To increase the odds even more, we also call |shrink_to_fit| below. // To increase the odds even more, we also call |shrink_to_fit| below.
std::vector<T> result(size); std::vector<T> result(size);
if (size == 0) { if (size == 0) {
if (num_bytes_to_consume != 0) if (num_bytes != 0)
abort(); abort();
return result; return result;
} }
std::memcpy(result.data(), data_ptr_, num_bytes_to_consume); CopyAndAdvance(result.data(), num_bytes);
Advance(num_bytes_to_consume);
// Even though |shrink_to_fit| is also implementation specific, we expect it // Even though |shrink_to_fit| is also implementation specific, we expect it
// to provide an additional assurance in case vector's constructor allocated // to provide an additional assurance in case vector's constructor allocated
@ -279,7 +364,8 @@ class FuzzedDataProvider {
return result; return result;
} }
template <typename TS, typename TU> TS ConvertUnsignedToSigned(TU value) { template <typename TS, typename TU>
TS FuzzedDataProvider::ConvertUnsignedToSigned(TU value) {
static_assert(sizeof(TS) == sizeof(TU), "Incompatible data types."); static_assert(sizeof(TS) == sizeof(TU), "Incompatible data types.");
static_assert(!std::numeric_limits<TU>::is_signed, static_assert(!std::numeric_limits<TU>::is_signed,
"Source type must be unsigned."); "Source type must be unsigned.");
@ -288,7 +374,7 @@ class FuzzedDataProvider {
if (std::numeric_limits<TS>::is_modulo) if (std::numeric_limits<TS>::is_modulo)
return static_cast<TS>(value); return static_cast<TS>(value);
// Avoid using implementation-defined unsigned to signer conversions. // Avoid using implementation-defined unsigned to signed conversions.
// To learn more, see https://stackoverflow.com/questions/13150449. // To learn more, see https://stackoverflow.com/questions/13150449.
if (value <= std::numeric_limits<TS>::max()) { if (value <= std::numeric_limits<TS>::max()) {
return static_cast<TS>(value); return static_cast<TS>(value);
@ -298,8 +384,4 @@ class FuzzedDataProvider {
} }
} }
const uint8_t *data_ptr_;
size_t remaining_bytes_;
};
#endif // LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_ #endif // LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_

Loading…
Cancel
Save