From b1da5d6d487a3209c81ca99c99ac823b44b97937 Mon Sep 17 00:00:00 2001 From: Peter Goodman Date: Thu, 27 Sep 2018 16:45:08 -0700 Subject: [PATCH] Improved the CMakeLists.txt just slightly. Provided a default, weak-attributed main function into libdeepstate so that tests don't need to define them. The weak attribute means that any main function defined by libfuzzer will be a strong symbol and take over, so there won't be a conflict. Adjusted examples accordingly to omit now redundant conditionally-compiled main functions, as that leaked out too many underlying details. --- CMakeLists.txt | 28 ++++++++++++++++++++++------ examples/Crash.cpp | 7 ------- examples/Euler.cpp | 6 ------ examples/Fixture.cpp | 7 ------- examples/IntegerArithmetic.cpp | 7 ------- examples/IntegerOverflow.cpp | 8 +------- examples/Lists.cpp | 7 ------- examples/OneOf.cpp | 7 ------- examples/Primes.cpp | 7 ------- examples/StreamingAndFormatting.cpp | 6 ------ src/lib/DeepState.c | 6 ++++++ src/lib/Log.c | 11 +++++++++++ 12 files changed, 40 insertions(+), 67 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c50e85a..eb62c96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,15 +15,31 @@ project(deepstate) cmake_minimum_required(VERSION 2.8) +if (NOT DEFINED BUILD_LIBFUZZER AND DEFINED $ENV{BUILD_LIBFUZZER}) + set(BUILD_LIBFUZZER "$ENV{BUILD_LIBFUZZER}") +endif() + +if (BUILD_LIBFUZZER) + if(NOT DEFINED CMAKE_C_COMPILER) + set(CMAKE_C_COMPILER clang) + endif() + + if(NOT DEFINED CMAKE_CXX_COMPILER) + set(CMAKE_CXX_COMPILER clang++) + endif() + + if (NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" AND NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang") + message(FATAL_ERROR "DeepState's libFuzzer mode requires the Clang C compiler.") + endif() + + if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") + message(FATAL_ERROR "DeepState's libFuzzer mode requires the Clang C++ compiler.") + endif() +endif() + enable_language(C) enable_language(CXX) -set(BUILD_LIBFUZZER "$ENV{BUILD_LIBFUZZER}") -if (BUILD_LIBFUZZER) - SET(CMAKE_C_COMPILER clang) - SET(CMAKE_CXX_COMPILER clang++) -endif() - set(CMAKE_POSITION_INDEPENDENT_CODE ON) if(NOT CMAKE_BUILD_TYPE) diff --git a/examples/Crash.cpp b/examples/Crash.cpp index 45e6983..88a0d4a 100644 --- a/examples/Crash.cpp +++ b/examples/Crash.cpp @@ -35,10 +35,3 @@ TEST(Crash, SegFault) { ASSERT_EQ(x, x); } - -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/examples/Euler.cpp b/examples/Euler.cpp index 8f2dd0d..b7b771a 100644 --- a/examples/Euler.cpp +++ b/examples/Euler.cpp @@ -39,9 +39,3 @@ TEST(Euler, SumsOfLikePowers) { << "^5 + " << d << "^5 = " << e << "^5"; } -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/examples/Fixture.cpp b/examples/Fixture.cpp index 2c864c6..ba288b5 100644 --- a/examples/Fixture.cpp +++ b/examples/Fixture.cpp @@ -35,10 +35,3 @@ class MyTest : public deepstate::Test { TEST_F(MyTest, Something) { ASSUME_NE(x, 0); } - -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/examples/IntegerArithmetic.cpp b/examples/IntegerArithmetic.cpp index 7af644d..2ac07c2 100644 --- a/examples/IntegerArithmetic.cpp +++ b/examples/IntegerArithmetic.cpp @@ -44,10 +44,3 @@ TEST(Arithmetic, InvertibleMultiplication_CanFail) { << x << " != (" << x << " / " << y << ") * " << y; }); } - -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/examples/IntegerOverflow.cpp b/examples/IntegerOverflow.cpp index c5add45..d3d592f 100644 --- a/examples/IntegerOverflow.cpp +++ b/examples/IntegerOverflow.cpp @@ -15,7 +15,7 @@ */ #include - +#include using namespace deepstate; DEEPSTATE_NOINLINE int ident1(int x) { @@ -40,9 +40,3 @@ TEST(SignedInteger, MultiplicationOverflow) { << x << " squared overflowed."; } -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/examples/Lists.cpp b/examples/Lists.cpp index 8d21f6c..1456869 100644 --- a/examples/Lists.cpp +++ b/examples/Lists.cpp @@ -30,10 +30,3 @@ TEST(Vector, DoubleReversal) { << "Double reverse of vectors must be equal."; }); } - -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - DeepState_Run(); -} -#endif diff --git a/examples/OneOf.cpp b/examples/OneOf.cpp index 46cd551..a8e949a 100644 --- a/examples/OneOf.cpp +++ b/examples/OneOf.cpp @@ -63,10 +63,3 @@ TEST(OneOfExample, ProduceSixtyOrHigher) { << x << " is >= 60: " << " did " << choices << " from " << start; } } - -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/examples/Primes.cpp b/examples/Primes.cpp index 1401d35..52e3354 100644 --- a/examples/Primes.cpp +++ b/examples/Primes.cpp @@ -52,10 +52,3 @@ TEST(PrimePolynomial, OnlyGeneratesPrimes_NoStreaming) { DeepState_Assert(poly != (y * z)); DeepState_Assert(IsPrime(Pump(poly))); } - -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/examples/StreamingAndFormatting.cpp b/examples/StreamingAndFormatting.cpp index f34c8fe..9c5ff3d 100644 --- a/examples/StreamingAndFormatting.cpp +++ b/examples/StreamingAndFormatting.cpp @@ -40,9 +40,3 @@ TEST(Formatting, OverridePrintf) { printf("hello again!"); } -#ifndef LIBFUZZER -int main(int argc, char *argv[]) { - DeepState_InitOptions(argc, argv); - return DeepState_Run(); -} -#endif diff --git a/src/lib/DeepState.c b/src/lib/DeepState.c index 9aea252..fb5fede 100644 --- a/src/lib/DeepState.c +++ b/src/lib/DeepState.c @@ -618,4 +618,10 @@ void __stack_chk_fail(void) { __builtin_unreachable(); } +__attribute__((weak)) +int main(int argc, char *argv[]) { + DeepState_InitOptions(argc, argv); + return DeepState_Run(); +} + DEEPSTATE_END_EXTERN_C diff --git a/src/lib/Log.c b/src/lib/Log.c index cbcb03f..dd22ea9 100644 --- a/src/lib/Log.c +++ b/src/lib/Log.c @@ -126,6 +126,14 @@ void DeepState_LogFormat(enum DeepState_LogLevel level, va_end(args); } +#pragma clang diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +/*#pragma clang diagnostic ignored "-Wbuiltin-declaration-mismatch" +*/ + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" + /* Override libc! */ DEEPSTATE_NOINLINE int puts(const char *str) { @@ -216,4 +224,7 @@ int __vfprintf_chk(int flag, FILE *file, const char *format, va_list args) { return 0; } +#pragma clang diagnostic pop +#pragma GCC diagnostic pop + DEEPSTATE_END_EXTERN_C