Skip to content

Commit

Permalink
factor out main into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed Jun 14, 2019
1 parent a23b7a1 commit 35f84c8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 101 deletions.
4 changes: 2 additions & 2 deletions test/fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ two_args.cpp
)

if(FMT_FUZZ_LINKMAIN)
add_definitions(-DIMPLEMENT_MAIN=1)
add_definitions(-DFMT_FUZZ_IMPLEMENT_MAIN=1)
endif()

macro(implement_fuzzer sourcefile)
get_filename_component(basename ${sourcefile} NAME_WE)
set(name fuzzer_${basename})
add_executable(${name} ${sourcefile} fuzzer_common.h)
add_executable(${name} ${sourcefile} fuzzer_common.h main.cpp)
target_link_libraries(${name} PRIVATE fmt)
if(FMT_FUZZ_LDFLAGS)
target_link_libraries(${name} PRIVATE ${FMT_FUZZ_LDFLAGS})
Expand Down
20 changes: 0 additions & 20 deletions test/fuzzing/chrono_duration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {

return 0;
}

#ifdef IMPLEMENT_MAIN
# include <cassert>
# include <fstream>
# include <sstream>
# include <vector>
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();
in.seekg(0, std::ios_base::beg);
std::vector<char> buf(pos);
in.read(buf.data(), buf.size());
assert(in.gcount() == pos);
LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size());
}
}
#endif
3 changes: 3 additions & 0 deletions test/fuzzing/fuzzer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ namespace fmt_fuzzer {
}
#endif




#endif // FUZZER_COMMON_H
21 changes: 21 additions & 0 deletions test/fuzzing/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// if an int main() is needed.
#if FMT_FUZZ_IMPLEMENT_MAIN
# include <cassert>
# include <fstream>
# include <sstream>
# include <vector>
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();
in.seekg(0, std::ios_base::beg);
std::vector<char> buf(pos);
in.read(buf.data(), buf.size());
assert(in.gcount() == pos);
LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size());
}
}
#endif
19 changes: 0 additions & 19 deletions test/fuzzing/named_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {
return 0;
}

#ifdef IMPLEMENT_MAIN
# include <cassert>
# include <fstream>
# include <sstream>
# include <vector>
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();
in.seekg(0, std::ios_base::beg);
std::vector<char> buf(pos);
in.read(buf.data(), buf.size());
assert(in.gcount() == pos);
LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size());
}
}
#endif
20 changes: 0 additions & 20 deletions test/fuzzing/one_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {
}
return 0;
}

#ifdef IMPLEMENT_MAIN
# include <cassert>
# include <fstream>
# include <sstream>
# include <vector>
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();
in.seekg(0, std::ios_base::beg);
std::vector<char> buf(pos);
in.read(buf.data(), buf.size());
assert(in.gcount() == pos);
LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size());
}
}
#endif
20 changes: 0 additions & 20 deletions test/fuzzing/sprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {
}
return 0;
}

#ifdef IMPLEMENT_MAIN
# include <cassert>
# include <fstream>
# include <sstream>
# include <vector>
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();
in.seekg(0, std::ios_base::beg);
std::vector<char> buf(pos);
in.read(buf.data(), buf.size());
assert(in.gcount() == pos);
LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size());
}
}
#endif
20 changes: 0 additions & 20 deletions test/fuzzing/two_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {
}
return 0;
}

#ifdef IMPLEMENT_MAIN
# include <cassert>
# include <fstream>
# include <sstream>
# include <vector>
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();
in.seekg(0, std::ios_base::beg);
std::vector<char> buf(pos);
in.read(buf.data(), buf.size());
assert(in.gcount() == pos);
LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size());
}
}
#endif

0 comments on commit 35f84c8

Please sign in to comment.