Cleanup fuzzing

This commit is contained in:
Victor Zverovich
2020-10-13 09:08:04 -07:00
parent 63e40c9614
commit 82c4e2236a
6 changed files with 126 additions and 153 deletions
+27 -6
View File
@@ -6,6 +6,9 @@
#include <cstdint> // std::uint8_t
#include <cstring> // memcpy
#include <vector>
#include <fmt/core.h>
// One can format to either a string, or a buffer. The latter is faster, but
// one may be interested in formatting to a string instead to verify it works
@@ -18,13 +21,11 @@
// the fuzzing.
#define FMT_FUZZ_SEPARATE_ALLOCATION 1
namespace fmt_fuzzer {
// The size of the largest possible type in use.
// To let the the fuzzer mutation be efficient at cross pollinating between
// different types, use a fixed size format. The same bit pattern, interpreted
// as another type, is likely interesting.
constexpr auto nfixed = 16;
constexpr auto fixed_size = 16;
// Casts data to a char pointer.
template <typename T> inline const char* as_chars(const T* data) {
@@ -38,17 +39,37 @@ template <typename T> inline const std::uint8_t* as_bytes(const T* data) {
// Blits bytes from data to form an (assumed trivially constructible) object
// of type Item.
template <class Item> inline Item assignFromBuf(const std::uint8_t* data) {
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
auto item = Item();
std::memcpy(&item, data, sizeof(Item));
return item;
}
// Reads a boolean value by looking at the first byte from data.
template <> inline bool assignFromBuf<bool>(const std::uint8_t* data) {
template <> inline bool assign_from_buf<bool>(const std::uint8_t* data) {
return *data != 0;
}
} // namespace fmt_fuzzer
struct data_to_string {
#if FMT_FUZZ_SEPARATE_ALLOCATION
std::vector<char> buffer;
data_to_string(const uint8_t* data, size_t size, bool add_terminator = false)
: buffer(size + (add_terminator ? 1 : 0)) {
std::memcpy(buffer.data(), data, size);
}
fmt::string_view get() const { return {buffer.data(), buffer.size()}; }
#else
fmt::string_view sv;
data_to_string(const uint8_t* data, size_t size, bool = false)
: str(as_chars(data), size) {}
fmt::string_view get() const { return sv; }
#endif
const char* data() const { return get().data(); }
};
#endif // FUZZER_COMMON_H