Skip to content

Commit

Permalink
refactor and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed Jun 14, 2019
1 parent 02afb12 commit a33b45a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 58 deletions.
11 changes: 3 additions & 8 deletions test/fuzzing/chrono_duration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void invoke_inner(fmt::string_view formatstring, const Item item) {
fmt::memory_buffer buf;
fmt::format_to(buf, formatstring, value);
#endif
} catch (std::exception& e) {
} catch (std::exception& /*e*/) {
}
}

Expand All @@ -36,20 +36,15 @@ void invoke_outer(const uint8_t* Data, std::size_t Size, const int scaling) {
return;
}

#if __cplusplus >= 201402L
static_assert(std::is_trivially_copyable<Item>::value,
"Item must be blittable");
#endif
Item item{};
std::memcpy(&item, Data, N);
const Item item = fmt_fuzzer::assignFromBuf<Item>(Data);

// fast forward
Data += Nfixed;
Size -= Nfixed;

// Data is already allocated separately in libFuzzer so reading past
// the end will most likely be detected anyway
const auto formatstring = fmt::string_view((const char*)Data, Size);
const auto formatstring = fmt::string_view(fmt_fuzzer::as_chars(Data), Size);

// doit_impl<Item,std::yocto>(buf.data(),item);
// doit_impl<Item,std::zepto>(buf.data(),item);
Expand Down
29 changes: 29 additions & 0 deletions test/fuzzing/fuzzer_common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifndef FUZZER_COMMON_H
#define FUZZER_COMMON_H

#include <cstring> // memcpy

// Copyright (c) 2019, Paul Dreik
// License: see LICENSE.rst in the fmt root directory

Expand Down Expand Up @@ -34,7 +37,33 @@ namespace fmt_fuzzer {
}
#endif

namespace fmt_fuzzer {
template <typename T>
inline const char* as_chars(const T* data) {
return static_cast<const char*>(static_cast<const void*>(data));
}
template <typename T>
inline const std::uint8_t* as_bytes(const T* data) {
return static_cast<const std::uint8_t*>(static_cast<const void*>(data));
}


template <class Item>
inline Item assignFromBuf(const uint8_t* Data) {
#if __cplusplus >= 201402L
static_assert(std::is_trivially_copyable<Item>::value,
"Item must be blittable");
#endif
Item item{};
std::memcpy(&item, Data, sizeof(Item));
return item;
}

template <> inline bool assignFromBuf<bool>(const uint8_t* Data) {
return !!Data[0];
}

}


#endif // FUZZER_COMMON_H
8 changes: 5 additions & 3 deletions test/fuzzing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
# include <fstream>
# include <sstream>
# include <vector>
#include "fuzzer_common.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size);
int main(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i) {
std::ifstream in(argv[i]);
assert(in);
in.seekg(0, std::ios_base::end);
const auto pos = in.tellg();
assert(pos>=0);
in.seekg(0, std::ios_base::beg);
std::vector<char> buf(pos);
in.read(buf.data(), buf.size());
std::vector<char> buf(static_cast<std::size_t>(pos));
in.read(buf.data(), static_cast<long>(buf.size()));
assert(in.gcount() == pos);
LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size());
LLVMFuzzerTestOneInput(fmt_fuzzer::as_bytes(buf.data()), buf.size());
}
}
#endif
14 changes: 5 additions & 9 deletions test/fuzzing/named_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@
#include "fuzzer_common.h"

template <typename Item1>
void invoke_fmt(const uint8_t* Data, std::size_t Size, int argsize) {
void invoke_fmt(const uint8_t* Data, std::size_t Size, unsigned int argsize) {
constexpr auto N1 = sizeof(Item1);
static_assert (N1<=fmt_fuzzer::Nfixed,"Nfixed too small");
if (Size <= fmt_fuzzer::Nfixed) {
return;
}
Item1 item1{};
if /*constexpr*/ (std::is_same<Item1, bool>::value) {
item1 = !!Data[0];
} else {
std::memcpy(&item1, Data, N1);
}
const Item1 item1 = fmt_fuzzer::assignFromBuf<Item1>(Data);

Data += fmt_fuzzer::Nfixed;
Size -= fmt_fuzzer::Nfixed;

Expand Down Expand Up @@ -110,7 +106,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {

// switch types depending on the first byte of the input
const auto first = Data[0] & 0x0F;
const auto second = (Data[0] & 0xF0) >> 4;
const unsigned int second = (Data[0] & 0xF0) >> 4;
Data++;
Size--;

Expand All @@ -120,7 +116,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {

try {
invoke(first, outerfcn);
} catch (std::exception& e) {
} catch (std::exception& /*e*/) {
}
return 0;
}
Expand Down
12 changes: 3 additions & 9 deletions test/fuzzing/one_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size) {
if (Size <= Nfixed) {
return;
}
Item item{};
if /*constexpr*/ (std::is_same<Item, bool>::value) {
item = !!Data[0];
} else {
std::memcpy(&item, Data, N);
}
const Item item = fmt_fuzzer::assignFromBuf<Item>(Data);
Data += Nfixed;
Size -= Nfixed;

Expand Down Expand Up @@ -52,8 +47,7 @@ void invoke_fmt_time(const uint8_t* Data, std::size_t Size) {
if (Size <= Nfixed) {
return;
}
Item item{};
std::memcpy(&item, Data, N);
const Item item = fmt_fuzzer::assignFromBuf<Item>(Data);
Data += Nfixed;
Size -= Nfixed;
#if FMT_FUZZ_SEPARATE_ALLOCATION
Expand Down Expand Up @@ -131,7 +125,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {
default:
break;
}
} catch (std::exception& e) {
} catch (std::exception& /*e*/) {
}
return 0;
}
19 changes: 4 additions & 15 deletions test/fuzzing/sprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@

using fmt_fuzzer::Nfixed;

template <class Item>
Item assignFromBuf(const uint8_t* Data, std::size_t Size) {
Item item{};
std::memcpy(&item, Data, sizeof(Item));
return item;
}

template <> bool assignFromBuf<bool>(const uint8_t* Data, std::size_t Size) {
return !!Data[0];
}

template <typename Item1, typename Item2>
void invoke_fmt(const uint8_t* Data, std::size_t Size) {
constexpr auto N1 = sizeof(Item1);
Expand All @@ -29,15 +18,15 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size) {
if (Size <= Nfixed + Nfixed) {
return;
}
Item1 item1 = assignFromBuf<Item1>(Data, Size);
Item1 item1 = fmt_fuzzer::assignFromBuf<Item1>(Data);
Data += Nfixed;
Size -= Nfixed;

Item2 item2 = assignFromBuf<Item2>(Data, Size);
Item2 item2 = fmt_fuzzer::assignFromBuf<Item2>(Data);
Data += Nfixed;
Size -= Nfixed;

auto fmtstring = fmt::string_view((const char*)Data, Size);
auto fmtstring = fmt::string_view(fmt_fuzzer::as_chars(Data), Size);

#if FMT_FUZZ_FORMAT_TO_STRING
std::string message = fmt::format(fmtstring, item1, item2);
Expand Down Expand Up @@ -121,7 +110,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {

try {
invoke(first, outer);
} catch (std::exception& e) {
} catch (std::exception& /*e*/) {
}
return 0;
}
18 changes: 4 additions & 14 deletions test/fuzzing/two_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,15 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size) {
if (Size <= Nfixed + Nfixed) {
return;
}
Item1 item1{};
if /*constexpr*/ (std::is_same<Item1, bool>::value) {
item1 = !!Data[0];
} else {
std::memcpy(&item1, Data, N1);
}
const Item1 item1=fmt_fuzzer::assignFromBuf<Item1>(Data);
Data += Nfixed;
Size -= Nfixed;

Item2 item2{};
if /*constexpr*/ (std::is_same<Item2, bool>::value) {
item2 = !!Data[0];
} else {
std::memcpy(&item2, Data, N2);
}
const Item2 item2=fmt_fuzzer::assignFromBuf<Item2>(Data);
Data += Nfixed;
Size -= Nfixed;

auto fmtstring = fmt::string_view((const char*)Data, Size);
auto fmtstring = fmt::string_view(fmt_fuzzer::as_chars(Data), Size);

#if FMT_FUZZ_FORMAT_TO_STRING
std::string message = fmt::format(fmtstring, item1, item2);
Expand Down Expand Up @@ -116,7 +106,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {

try {
invoke(first, outer);
} catch (std::exception& e) {
} catch (std::exception& /*e*/) {
}
return 0;
}

0 comments on commit a33b45a

Please sign in to comment.