diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index b3a9b6ce9a6a..535cf9e2d8e8 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -3,19 +3,22 @@ # ==================== # # Local libfuzzer client: -# make CXX=clang++-6.0 CXXFLAGS="-fsanitize=address" LIB_FUZZING_ENGINE="-fsanitize=fuzzer" oss-fuzz-client +# make CXX=clang++-6.0 CXXFLAGS="-O1 -fno-omit-frame-pointer -gline-tables-only -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=address -fsanitize-address-use-after-scope -fsanitize=fuzzer-no-link -stdlib=libc++" LIB_FUZZING_ENGINE="-fsanitize=fuzzer" oss-fuzz-client CPPCHECK_DIR=.. INCLUDE_DIR=-I ${CPPCHECK_DIR}/lib -I ${CPPCHECK_DIR}/externals/picojson -I ${CPPCHECK_DIR}/externals/simplecpp -I ${CPPCHECK_DIR}/externals/tinyxml2 -I ${CPPCHECK_DIR}/externals -SRC_FILES=main.cpp type2.cpp ${CPPCHECK_DIR}/externals/simplecpp/simplecpp.cpp ${CPPCHECK_DIR}/externals/tinyxml2/tinyxml2.cpp ${CPPCHECK_DIR}/lib/*.cpp +SRC_FILES=main.cpp ${CPPCHECK_DIR}/externals/simplecpp/simplecpp.cpp ${CPPCHECK_DIR}/externals/tinyxml2/tinyxml2.cpp ${CPPCHECK_DIR}/lib/*.cpp all: oss-fuzz-client translate oss-fuzz-client: main.cpp type2.cpp type2.h - ${CXX} -std=c++11 -g ${CXXFLAGS} -o oss-fuzz-client ${INCLUDE_DIR} ${SRC_FILES} ${LIB_FUZZING_ENGINE} + ${CXX} -std=c++11 -g ${CXXFLAGS} -o $@ ${INCLUDE_DIR} ${SRC_FILES} type2.cpp ${LIB_FUZZING_ENGINE} + +no-fuzz: main.cpp + ${CXX} -std=c++11 -g ${CXXFLAGS} -DNO_FUZZ -o $@ ${INCLUDE_DIR} ${SRC_FILES} translate: translate.cpp type2.cpp type2.h - ${CXX} -std=c++11 -g ${CXXFLAGS} -o translate type2.cpp translate.cpp + ${CXX} -std=c++11 -g ${CXXFLAGS} -o $@ type2.cpp translate.cpp clean: - rm -f oss-fuzz-client translate + rm -f oss-fuzz-client no-fuzz translate diff --git a/oss-fuzz/main.cpp b/oss-fuzz/main.cpp index be33779d1041..7ba4933fbdd1 100644 --- a/oss-fuzz/main.cpp +++ b/oss-fuzz/main.cpp @@ -19,6 +19,12 @@ #include "cppcheck.h" #include "type2.h" +#ifdef NO_FUZZ +#include +#include +#include +#endif + enum class Color; class DummyErrorLogger : public ErrorLogger { @@ -30,20 +36,48 @@ class DummyErrorLogger : public ErrorLogger { const std::size_t /*value*/) override {} // FN }; +static DummyErrorLogger s_errorLogger; + +static void doCheck(const std::string& code) +{ + CppCheck cppcheck(s_errorLogger, false, nullptr); + cppcheck.settings().addEnabled("all"); + cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true); + cppcheck.check("test.cpp", code); +} + +#ifndef NO_FUZZ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize); int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) { if (dataSize < 10000) { const std::string code = generateCode2(data, dataSize); - - DummyErrorLogger errorLogger; - CppCheck cppcheck(errorLogger, false, nullptr); - cppcheck.settings().addEnabled("all"); - cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true); - cppcheck.check("test.cpp", code); + doCheck(code); } return 0; } +#else +int main(int argc, char * argv[]) +{ + if (argc != 1) + return EXIT_FAILURE; + + std::ifstream f(argv[1]); + if (!f.is_open()) + return EXIT_FAILURE; + + std::ostringstream oss; + oss << f.rdbuf(); + + if (!f.good()) + return EXIT_FAILURE; + + const std::string code = oss.str(); + doCheck(code); + + return EXIT_SUCCESS; +} +#endif