Skip to content

refs #341 - added fuzzing client #351

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
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
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@ CXXFLAGS = -Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarati
LDFLAGS = -g $(LDOPTS)

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

fuzz_no.o: fuzz.cpp
$(CXX) $(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="-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"
$(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
67 changes: 67 additions & 0 deletions fuzz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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);
rawtokens.removeComments();

simplecpp::TokenList outputTokens(files);
simplecpp::FileDataCache filedata;
simplecpp::DUI dui;
dui.removeComments = true;
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
4 changes: 4 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ int main(int argc, char **argv)
std::cout << "error: could not open file '" << filename << "'" << std::endl;
std::exit(1);
}
if (!simplecpp::isFile(filename)) {
std::cout << "error: could not open file '" << filename << "' - not a regular file" << std::endl;
std::exit(1);
}
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
} else {
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
Expand Down
115 changes: 70 additions & 45 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@

#ifdef _WIN32
# include <direct.h>
using mode_t = unsigned short;
#else
# include <sys/stat.h>
# include <sys/types.h>
#endif

static bool isHex(const std::string &s)
Expand Down Expand Up @@ -685,22 +687,35 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,

if (oldLastToken != cback()) {
oldLastToken = cback();
if (!isLastLinePreprocessor())
const Token * const llTok = isLastLinePreprocessor();
if (!llTok)
continue;
const std::string lastline(lastLine());
if (lastline == "# file %str%") {
const Token * const llNextToken = llTok->next;
if (!llTok->next)
continue;
// #file "file.c"
if (llNextToken->str() == "file" &&
llNextToken->next &&
llNextToken->next->str()[0] == '\"')
{
const Token *strtok = cback();
while (strtok->comment)
strtok = strtok->previous;
loc.push(location);
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
location.line = 1U;
} else if (lastline == "# line %num%") {
const Token *numtok = cback();
while (numtok->comment)
numtok = numtok->previous;
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
}
// #3 "file.c"
// #line 3 "file.c"
else if ((llNextToken->number &&
llNextToken->next &&
llNextToken->next->str()[0] == '\"') ||
(llNextToken->str() == "line" &&
llNextToken->next &&
llNextToken->next->number &&
llNextToken->next->next &&
llNextToken->next->next->str()[0] == '\"'))
{
const Token *strtok = cback();
while (strtok->comment)
strtok = strtok->previous;
Expand All @@ -710,8 +725,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
std::atol(numtok->str().c_str()), &location);
}
// #line 3
else if (llNextToken->str() == "line" &&
llNextToken->next &&
llNextToken->next->number)
{
const Token *numtok = cback();
while (numtok->comment)
numtok = numtok->previous;
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
}
// #endfile
else if (lastline == "# endfile" && !loc.empty()) {
else if (llNextToken->str() == "endfile" && !loc.empty())
{
location = loc.top();
loc.pop();
}
Expand Down Expand Up @@ -1405,34 +1431,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
return ret;
}

std::string simplecpp::TokenList::lastLine(int maxsize) const
{
std::string ret;
int count = 0;
for (const Token *tok = cback(); ; tok = tok->previous) {
if (!sameline(tok, cback())) {
break;
}
if (tok->comment)
continue;
if (++count > maxsize)
return "";
if (!ret.empty())
ret += ' ';
// add tokens in reverse for performance reasons
if (tok->str()[0] == '\"')
ret += "%rts%"; // %str%
else if (tok->number)
ret += "%mun%"; // %num%
else {
ret += tok->str();
std::reverse(ret.end() - tok->str().length(), ret.end());
}
}
std::reverse(ret.begin(), ret.end());
return ret;
}

const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
{
const Token* prevTok = nullptr;
Expand All @@ -1449,10 +1447,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
return prevTok;
}

bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
{
const Token * const prevTok = lastLineTok(maxsize);
return prevTok && prevTok->op == '#';
if (prevTok && prevTok->op == '#')
return prevTok;
return nullptr;
}

unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)
Expand Down Expand Up @@ -1700,7 +1700,9 @@ namespace simplecpp {
nameTokDef = nametoken;
variadic = false;
variadicOpt = false;
delete optExpandValue;
optExpandValue = nullptr;
delete optNoExpandValue;
optNoExpandValue = nullptr;
if (!nameTokDef) {
valueToken = endToken = nullptr;
Expand Down Expand Up @@ -2374,8 +2376,8 @@ namespace simplecpp {
bool variadicOpt;

/** Expansion value for varadic macros with __VA_OPT__ expanded and discarded respectively */
const TokenList *optExpandValue;
const TokenList *optNoExpandValue;
const TokenList *optExpandValue = nullptr;
const TokenList *optNoExpandValue = nullptr;

/** was the value of this macro actually defined in the code? */
bool valueDefinedInCode_;
Expand Down Expand Up @@ -2979,9 +2981,11 @@ static std::string openHeaderDirect(std::ifstream &f, const std::string &path)
if (nonExistingFilesCache.contains(path))
return ""; // file is known not to exist, skip expensive file open call
#endif
f.open(path.c_str());
if (f.is_open())
return path;
if (simplecpp::isFile(path)) {
f.open(path.c_str());
if (f.is_open())
return path;
}
#ifdef SIMPLECPP_WINDOWS
nonExistingFilesCache.add(path);
#endif
Expand Down Expand Up @@ -3102,6 +3106,9 @@ bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id)
if (stat(path.c_str(), &statbuf) != 0)
return false;

if ((statbuf.st_mode & S_IFMT) != S_IFREG)
return false;

id.dev = statbuf.st_dev;
id.ino = statbuf.st_ino;

Expand Down Expand Up @@ -3834,3 +3841,21 @@ std::string simplecpp::getCppStdString(const std::string &std)
{
return getCppStdString(getCppStd(std));
}

static mode_t file_type(const std::string &path)
{
struct stat file_stat;
if (stat(path.c_str(), &file_stat) == -1)
return 0;
return file_stat.st_mode & S_IFMT;
}

bool simplecpp::isFile(const std::string &path)
{
return file_type(path) == S_IFREG;
}

bool simplecpp::isDirectory(const std::string &path)
{
return file_type(path) == S_IFDIR;
}
17 changes: 15 additions & 2 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,8 @@ namespace simplecpp {
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);

std::string lastLine(int maxsize=1000) const;
const Token* lastLineTok(int maxsize=1000) const;
bool isLastLinePreprocessor(int maxsize=1000) const;
const Token* isLastLinePreprocessor(int maxsize=1000) const;

unsigned int fileIndex(const std::string &filename);

Expand Down Expand Up @@ -396,6 +395,20 @@ namespace simplecpp {
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);

/**
* @brief Checks if given path is a file
* @param path Path to be checked
* @return true if given path is a file
*/
SIMPLECPP_LIB bool isFile(const std::string &path);

/**
* @brief Checks if a given path is a directory
* @param path Path to be checked
* @return true if given path is a directory
*/
SIMPLECPP_LIB bool isDirectory(const std::string &path);

struct SIMPLECPP_LIB FileData {
/** The canonical filename associated with this data */
std::string filename;
Expand Down
Loading