Skip to content

Commit fe50e8e

Browse files
committed
added fuzzing client [skip ci]
1 parent 875b429 commit fe50e8e

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
#include <string>
15+
#endif
16+
17+
static void doProcess(const uint8_t *data, size_t dataSize)
18+
{
19+
simplecpp::OutputList outputList;
20+
std::vector<std::string> files;
21+
simplecpp::TokenList rawtokens(data, dataSize, files, "test.cpp", &outputList);
22+
rawtokens.removeComments();
23+
24+
simplecpp::TokenList outputTokens(files);
25+
std::map<std::string, simplecpp::TokenList*> filedata;
26+
simplecpp::DUI dui;
27+
dui.removeComments = true;
28+
std::list<simplecpp::MacroUsage> macroUsage;
29+
std::list<simplecpp::IfCond> ifCond;
30+
simplecpp::preprocess(outputTokens, rawtokens, files, filedata, dui, &outputList, &macroUsage, &ifCond);
31+
32+
simplecpp::cleanup(filedata);
33+
}
34+
35+
#ifndef NO_FUZZ
36+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);
37+
38+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
39+
{
40+
doProcess(data, dataSize);
41+
return 0;
42+
}
43+
#else
44+
int main(int argc, char * argv[])
45+
{
46+
if (argc < 2 || argc > 3)
47+
return EXIT_FAILURE;
48+
49+
std::ifstream f(argv[1]);
50+
if (!f.is_open())
51+
return EXIT_FAILURE;
52+
53+
std::ostringstream oss;
54+
oss << f.rdbuf();
55+
56+
if (!f.good())
57+
return EXIT_FAILURE;
58+
59+
const int cnt = (argc == 3) ? std::stoi(argv[2]) : 1;
60+
61+
const std::string code = oss.str();
62+
for (int i = 0; i < cnt; ++i)
63+
doProcess(reinterpret_cast<const uint8_t*>(code.data()), code.size());
64+
65+
return EXIT_SUCCESS;
66+
}
67+
#endif

0 commit comments

Comments
 (0)