Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/CI-unixish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ jobs:
wget https://github.com/danmar/simplecpp/archive/refs/tags/1.5.1.tar.gz
tar xvf 1.5.1.tar.gz
make clean
make -j$(nproc) CXXOPTS="-O2 -g3"
make -j$(nproc) CXXOPTS="-O3 -g3 -flto" LDOPTS="-flto"
valgrind --tool=callgrind ./simplecpp -e simplecpp-1.5.1/simplecpp.cpp 2>callgrind.log || (cat callgrind.log && false)
cat callgrind.log
callgrind_annotate --auto=no > callgrind.annotated.log
Expand Down
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,32 @@ TEST_CPPFLAGS = -DSIMPLECPP_TEST_SOURCE_DIR=\"$(CURDIR)\"
test.o: CPPFLAGS += $(TEST_CPPFLAGS)

%.o: %.cpp simplecpp.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< $(LIB_FUZZING_ENGINE)

fuzz_no.o: fuzz.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -DNO_FUZZ -c -o $@ fuzz.cpp

testrunner: test.o simplecpp.o
$(CXX) $(LDFLAGS) simplecpp.o test.o -o testrunner
$(CXX) $(LDFLAGS) -o $@ $^

test: testrunner simplecpp
./testrunner
python3 run-tests.py
python3 -m pytest integration_test.py -vv

fuzz: fuzz.o simplecpp.o
# TODO: use -stdlib=libc++ -lc++
# make fuzz CXX=clang++ CXXOPTS="-O3 -flto -fno-omit-frame-pointer -g -gline-tables-only -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=address,undefined -fsanitize-address-use-after-scope -fno-sanitize=integer -fno-sanitize-recover=undefined" LDOPTS="-flto" LIB_FUZZING_ENGINE="-fsanitize=fuzzer"
$(CXX) $(LDFLAGS) $(CXXFLAGS) -o $@ $^ $(LIB_FUZZING_ENGINE)

no-fuzz: fuzz_no.o simplecpp.o
$(CXX) $(LDFLAGS) $(CXXFLAGS) -o $@ $^

selfcheck: simplecpp
./selfcheck.sh

simplecpp: main.o simplecpp.o
$(CXX) $(LDFLAGS) main.o simplecpp.o -o simplecpp
$(CXX) $(LDFLAGS) -o $@ $^

clean:
rm -f testrunner simplecpp *.o
rm -f testrunner fuzz no-fuzz simplecpp *.o
66 changes: 66 additions & 0 deletions fuzz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* simplecpp - A simple and high-fidelity C/C++ preprocessor library
* Copyright (C) 2016-2024 simplecpp team
*/

#include "simplecpp.h"

#include <cstdint>

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

static void doProcess(const uint8_t *data, size_t dataSize)
{
simplecpp::OutputList outputList;
std::vector<std::string> files;
simplecpp::TokenList rawtokens(data, dataSize, files, "test.cpp", &outputList);

simplecpp::TokenList outputTokens(files);
simplecpp::FileDataCache filedata;
simplecpp::DUI dui;
dui.includePaths = { "/usr/include" };
std::list<simplecpp::MacroUsage> macroUsage;
std::list<simplecpp::IfCond> ifCond;
simplecpp::preprocess(outputTokens, rawtokens, files, filedata, dui, &outputList, &macroUsage, &ifCond);

simplecpp::cleanup(filedata);
}

#ifndef NO_FUZZ
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
{
doProcess(data, dataSize);
return 0;
}
#else
int main(int argc, char * argv[])
{
if (argc < 2 || argc > 3)
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 int cnt = (argc == 3) ? std::stoi(argv[2]) : 1;

const std::string code = oss.str();
for (int i = 0; i < cnt; ++i)
doProcess(reinterpret_cast<const uint8_t*>(code.data()), code.size());

return EXIT_SUCCESS;
}
#endif
Loading