From 05efde62f5f2bd670b9cf1dba923bae8e7a81478 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Fri, 30 Jan 2015 23:26:57 +0000 Subject: [PATCH] [fuzzer] Add a gtest-style test Summary: Add one gtest-style test. Test Plan: run on bot Reviewers: samsonov Reviewed By: samsonov Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D7287 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227639 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Fuzzer/CMakeLists.txt | 8 ++-- lib/Fuzzer/test/CMakeLists.txt | 39 +++++++++++----- lib/Fuzzer/test/FuzzerUnittest.cpp | 62 +++++++++++++++++++++++++ lib/Fuzzer/test/TestFuzzerCrossOver.cpp | 13 ------ lib/Fuzzer/test/unit/lit.cfg | 7 +++ lib/Fuzzer/test/unit/lit.site.cfg.in | 2 + 6 files changed, 104 insertions(+), 27 deletions(-) create mode 100644 lib/Fuzzer/test/FuzzerUnittest.cpp delete mode 100644 lib/Fuzzer/test/TestFuzzerCrossOver.cpp create mode 100644 lib/Fuzzer/test/unit/lit.cfg create mode 100644 lib/Fuzzer/test/unit/lit.site.cfg.in diff --git a/lib/Fuzzer/CMakeLists.txt b/lib/Fuzzer/CMakeLists.txt index 499cf98a6b9..a68df7f0feb 100644 --- a/lib/Fuzzer/CMakeLists.txt +++ b/lib/Fuzzer/CMakeLists.txt @@ -1,15 +1,17 @@ # Disable the coverage instrumentation for the fuzzer itself. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -fsanitize-coverage=0") if( LLVM_USE_SANITIZE_COVERAGE ) - add_library(LLVMFuzzer STATIC - EXCLUDE_FROM_ALL # Do not build if you are not building fuzzers. + add_library(LLVMFuzzerNoMain OBJECT FuzzerCrossOver.cpp FuzzerIO.cpp FuzzerLoop.cpp - FuzzerMain.cpp FuzzerMutate.cpp FuzzerUtil.cpp ) + add_library(LLVMFuzzer STATIC + FuzzerMain.cpp + $ + ) if( LLVM_INCLUDE_TESTS ) add_subdirectory(test) diff --git a/lib/Fuzzer/test/CMakeLists.txt b/lib/Fuzzer/test/CMakeLists.txt index 17afd92c3b4..2b94b4b49ae 100644 --- a/lib/Fuzzer/test/CMakeLists.txt +++ b/lib/Fuzzer/test/CMakeLists.txt @@ -25,19 +25,36 @@ foreach(Test ${Tests}) set(TestBinaries ${TestBinaries} LLVMFuzzer-${Test}) endforeach() -set_target_properties(${TestBinaries} - PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - ) - -set(EXCLUDE_FROM_ALL TRUE) -add_lit_testsuite(check-fuzzer "Running Fuzzer tests" - ${CMAKE_CURRENT_BINARY_DIR} - DEPENDS ${TestBinaries} - ) -set(EXCLUDE_FROM_ALL FALSE) - configure_lit_site_cfg( ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg ) +configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/unit/lit.site.cfg.in + ${CMAKE_CURRENT_BINARY_DIR}/unit/lit.site.cfg + ) + +include_directories(..) +include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include) + +add_executable(LLVMFuzzer-Unittest + FuzzerUnittest.cpp + $ + ) + +target_link_libraries(LLVMFuzzer-Unittest + gtest + gtest_main + ) + +set(TestBinaries ${TestBinaries} LLVMFuzzer-Unittest) + +set_target_properties(${TestBinaries} + PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) + +add_lit_testsuite(check-fuzzer "Running Fuzzer tests" + ${CMAKE_CURRENT_BINARY_DIR} + DEPENDS ${TestBinaries} + ) diff --git a/lib/Fuzzer/test/FuzzerUnittest.cpp b/lib/Fuzzer/test/FuzzerUnittest.cpp new file mode 100644 index 00000000000..368a0f2bf00 --- /dev/null +++ b/lib/Fuzzer/test/FuzzerUnittest.cpp @@ -0,0 +1,62 @@ +#include "FuzzerInternal.h" +#include "gtest/gtest.h" +#include + +// For now, have TestOneInput just to make it link. +// Later we may want to make unittests that actually call TestOneInput. +extern "C" void TestOneInput(const uint8_t *Data, size_t Size) { + abort(); +} + +TEST(Fuzzer, CrossOver) { + using namespace fuzzer; + Unit A({0, 1, 2}), B({5, 6, 7}); + Unit C; + Unit Expected[] = { + { 0 }, + { 0, 1 }, + { 0, 5 }, + { 0, 1, 2 }, + { 0, 1, 5 }, + { 0, 5, 1 }, + { 0, 5, 6 }, + { 0, 1, 2, 5 }, + { 0, 1, 5, 2 }, + { 0, 1, 5, 6 }, + { 0, 5, 1, 2 }, + { 0, 5, 1, 6 }, + { 0, 5, 6, 1 }, + { 0, 5, 6, 7 }, + { 0, 1, 2, 5, 6 }, + { 0, 1, 5, 2, 6 }, + { 0, 1, 5, 6, 2 }, + { 0, 1, 5, 6, 7 }, + { 0, 5, 1, 2, 6 }, + { 0, 5, 1, 6, 2 }, + { 0, 5, 1, 6, 7 }, + { 0, 5, 6, 1, 2 }, + { 0, 5, 6, 1, 7 }, + { 0, 5, 6, 7, 1 }, + { 0, 1, 2, 5, 6, 7 }, + { 0, 1, 5, 2, 6, 7 }, + { 0, 1, 5, 6, 2, 7 }, + { 0, 1, 5, 6, 7, 2 }, + { 0, 5, 1, 2, 6, 7 }, + { 0, 5, 1, 6, 2, 7 }, + { 0, 5, 1, 6, 7, 2 }, + { 0, 5, 6, 1, 2, 7 }, + { 0, 5, 6, 1, 7, 2 }, + { 0, 5, 6, 7, 1, 2 } + }; + for (size_t Len = 1; Len < 8; Len++) { + std::set FoundUnits, ExpectedUnitsWitThisLength; + for (int Iter = 0; Iter < 3000; Iter++) { + CrossOver(A, B, &C, Len); + FoundUnits.insert(C); + } + for (const Unit &U : Expected) + if (U.size() <= Len) + ExpectedUnitsWitThisLength.insert(U); + EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits); + } +} diff --git a/lib/Fuzzer/test/TestFuzzerCrossOver.cpp b/lib/Fuzzer/test/TestFuzzerCrossOver.cpp deleted file mode 100644 index 7f7619618ec..00000000000 --- a/lib/Fuzzer/test/TestFuzzerCrossOver.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "FuzzerInternal.h" - -int main() { - using namespace fuzzer; - Unit A({0, 1, 2, 3, 4}), B({5, 6, 7, 8, 9}); - Unit C; - for (size_t Len = 1; Len < 15; Len++) { - for (int Iter = 0; Iter < 1000; Iter++) { - CrossOver(A, B, &C, Len); - Print(C); - } - } -} diff --git a/lib/Fuzzer/test/unit/lit.cfg b/lib/Fuzzer/test/unit/lit.cfg new file mode 100644 index 00000000000..0cc31939c55 --- /dev/null +++ b/lib/Fuzzer/test/unit/lit.cfg @@ -0,0 +1,7 @@ +import lit.formats + +config.name = "LLVMFuzzer-Unittest" +print config.test_exec_root +config.test_format = lit.formats.GoogleTest(".", "Unittest") +config.suffixes = [] +config.test_source_root = config.test_exec_root diff --git a/lib/Fuzzer/test/unit/lit.site.cfg.in b/lib/Fuzzer/test/unit/lit.site.cfg.in new file mode 100644 index 00000000000..114daf474b6 --- /dev/null +++ b/lib/Fuzzer/test/unit/lit.site.cfg.in @@ -0,0 +1,2 @@ +config.test_exec_root = "@CMAKE_CURRENT_BINARY_DIR@" +lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/unit/lit.cfg")