Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oss-fuzz: added no-fuzz target / updated build command to match actual flags #6060

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 40 additions & 6 deletions oss-fuzz/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#include "cppcheck.h"
#include "type2.h"

#ifdef NO_FUZZ
#include <cstdlib>
#include <fstream>
#include <sstream>
#endif

enum class Color;

class DummyErrorLogger : public ErrorLogger {
Expand All @@ -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 != 2)
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


Loading