Skip to content

Commit 986ff24

Browse files
committed
added fuzzing client [skip ci]
1 parent ad9b49d commit 986ff24

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

Makefile

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,33 @@ CXXFLAGS = -Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarati
44
LDFLAGS = -g
55

66
%.o: %.cpp simplecpp.h
7-
$(CXX) $(CXXFLAGS) -c $<
7+
$(CXX) $(CXXFLAGS) -c $< $(LIB_FUZZING_ENGINE)
88

9+
fuzz_no.o: fuzz.cpp
10+
$(CXX) $(CXXFLAGS) -DNO_FUZZ -c -o $@ fuzz.cpp
911

1012
testrunner: test.o simplecpp.o
11-
$(CXX) $(LDFLAGS) simplecpp.o test.o -o testrunner
13+
$(CXX) $(LDFLAGS) -o $@ $^
1214

1315
test: testrunner simplecpp
1416
# The -std=c++03 makes sure that simplecpp.cpp is C++03 conformant. We don't require a C++11 compiler
1517
g++ -std=c++03 -fsyntax-only simplecpp.cpp
1618
./testrunner
1719
python3 run-tests.py
1820

21+
fuzz: fuzz.o simplecpp.o
22+
# TODO: use -stdlib=libc++ -lc++
23+
# make fuzz CXX=clang++ CXXFLAGS="-O2 -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" LIB_FUZZING_ENGINE="-fsanitize=fuzzer"
24+
$(CXX) $(LDFLAGS) $(CXXFLAGS) -o $@ $^ $(LIB_FUZZING_ENGINE)
25+
26+
no-fuzz: fuzz_no.o simplecpp.o
27+
$(CXX) $(LDFLAGS) $(CXXFLAGS) -o $@ $^
28+
1929
selfcheck: simplecpp
2030
./selfcheck.sh
2131

2232
simplecpp: main.o simplecpp.o
23-
$(CXX) $(LDFLAGS) main.o simplecpp.o -o simplecpp
33+
$(CXX) $(LDFLAGS) -o $@ $^
2434

2535
clean:
26-
rm -f testrunner simplecpp *.o
36+
rm -f testrunner fuzz no-fuzz simplecpp *.o

fuzz.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* simplecpp - A simple and high-fidelity C/C++ preprocessor library
3+
* Copyright (C) 2016-2024 simplecpp team
4+
*/
5+
6+
#include "simplecpp.h"
7+
8+
#include <cstdint>
9+
10+
#ifdef NO_FUZZ
11+
#include <cstdlib>
12+
#include <fstream>
13+
#include <sstream>
14+
#endif
15+
16+
static void doProcess(const uint8_t *data, size_t dataSize)
17+
{
18+
simplecpp::OutputList outputList;
19+
std::vector<std::string> files;
20+
simplecpp::TokenList rawtokens(data, dataSize, files, "test.cpp", &outputList);
21+
rawtokens.removeComments();
22+
23+
simplecpp::TokenList outputTokens(files);
24+
std::map<std::string, simplecpp::TokenList*> filedata;
25+
simplecpp::DUI dui;
26+
dui.removeComments = true;
27+
std::list<simplecpp::MacroUsage> macroUsage;
28+
std::list<simplecpp::IfCond> ifCond;
29+
simplecpp::preprocess(outputTokens, rawtokens, files, filedata, dui, &outputList, &macroUsage, &ifCond);
30+
31+
simplecpp::cleanup(filedata);
32+
}
33+
34+
#ifndef NO_FUZZ
35+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);
36+
37+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
38+
{
39+
doProcess(data, dataSize);
40+
return 0;
41+
}
42+
#else
43+
int main(int argc, char * argv[])
44+
{
45+
if (argc != 2)
46+
return EXIT_FAILURE;
47+
48+
std::ifstream f(argv[1]);
49+
if (!f.is_open())
50+
return EXIT_FAILURE;
51+
52+
std::ostringstream oss;
53+
oss << f.rdbuf();
54+
55+
if (!f.good())
56+
return EXIT_FAILURE;
57+
58+
const std::string code = oss.str();
59+
doProcess(reinterpret_cast<const uint8_t*>(code.data()), code.size());
60+
61+
return EXIT_SUCCESS;
62+
}
63+
#endif

0 commit comments

Comments
 (0)