From b267569e302cb4d921835a557608ce8a61b4f933 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Wed, 31 Aug 2016 04:31:33 +0430 Subject: a simple c mutator. --- makefile | 45 ++++++++++++++++++++++++++++ mutator.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mutator.sh | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 makefile create mode 100644 mutator.cpp create mode 100755 mutator.sh diff --git a/makefile b/makefile new file mode 100644 index 0000000..1df41ac --- /dev/null +++ b/makefile @@ -0,0 +1,45 @@ + +#######################################VARS#################################### +#CXX=g++ +CXX=/home/bloodstalker/llvm/llvm/build/bin/clang++ + +CXX_FLAGS=$(shell /home/bloodstalker/llvm/llvm/build/bin/llvm-config --cxxflags) +#CXX_FLAGS=$(shell llvm-config --cxxflags) + +EXTRA_CXX_FALGS=-I/home/bloodstalker/llvm/llvm/llvm/tools/clang/include -I/home/bloodstalker/llvm/llvm/build/tools/clang/include +EXTRA_LD_FLAGS= + +LD_FLAGS=-Wl,--start-group -lclangAST -lclangAnalysis -lclangBasic\ +-lclangDriver -lclangEdit -lclangFrontend -lclangFrontendTool\ +-lclangLex -lclangParse -lclangSema -lclangEdit -lclangASTMatchers\ +-lclangRewrite -lclangRewriteFrontend -lclangStaticAnalyzerFrontend\ +-lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore\ +-lclangSerialization -lclangToolingCore -lclangTooling -Wl,--end-group +LD_FLAGS+=$(shell /home/bloodstalker/llvm/llvm/build/bin/llvm-config --ldflags --libs --system-libs) +#LD_FLAGS+=$(shell llvm-config --ldflags --libs --system-libs) + +CXX_FLAGS+=$(EXTRA_CXX_FALGS) +LD_FLAGS+=$(EXTRA_LD_FLAGS) + +TARGET=mutator + +######################################RULES#################################### +.DEFAULT: all + +.PHONY:all clean help + +all: $(TARGET) + +.cpp.o: + $(CXX) $(CXX_FLAGS) -c $< -o $@ + +$(TARGET): $(TARGET).o + $(CXX) $< $(LD_FLAGS) -o $@ + +clean: + rm -f *.o *~ $(TARGET) + +help: + @echo 'there is help.' + @echo 'all is the default.' + @echo 'clean.' diff --git a/mutator.cpp b/mutator.cpp new file mode 100644 index 0000000..a53dc8a --- /dev/null +++ b/mutator.cpp @@ -0,0 +1,98 @@ + +/*first line intentionally left blank.*/ +/**********************************************************************************************************************/ +/*included modules*/ +/*standard library*/ +#include +/*LLVM-libs*/ +#include "clang/AST/AST.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendActions.h" +#include "clang/Lex/Lexer.h" +#include "clang/Tooling/CommonOptionsParser.h" +#include "clang/Tooling/Tooling.h" +#include "clang/Rewrite/Core/Rewriter.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/IR/Function.h" +/*adding the -- deafult option is not a good choice since we need extra flags to compile code or else the parsers not gonna parse all of the target file.*/ +#if 0 +#include "llvm/support/CommandLine.h" +#endif +/**********************************************************************************************************************/ +/*used namespaces*/ +using namespace clang; +using namespace clang::ast_matchers; +using namespace clang::driver; +using namespace clang::tooling; +/**********************************************************************************************************************/ +/*global vars*/ + + +static llvm::cl::OptionCategory MatcherSampleCategory("Matcher Sample"); + +/**********************************************************************************************************************/ +/*matcher callback for 'if' and 'else if'.*/ +class FunctionHandler : public MatchFinder::MatchCallback { +public: + FunctionHandler (Rewriter &Rewrite) : Rewrite(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) + { + /*dev*/ + } + +private: + Rewriter &Rewrite; +}; +/**********************************************************************************************************************/ + +/**********************************************************************************************************************/ +// Implementation of the ASTConsumer interface for reading an AST produced +// by the Clang parser. It registers a couple of matchers and runs them on +// the AST. +class MyASTConsumer : public ASTConsumer { +//friend class CalleeHandler; + +public: + MyASTConsumer(Rewriter &R) : HandlerForFunction(R) { + Matcher.addMatcher (functionDecl(), &HandlerForFunction); + } + + void HandleTranslationUnit(ASTContext &Context) override { + // Run the matchers when we have the whole TU parsed. + Matcher.matchAST(Context); + } + +private: + FunctionHandler HandlerForFunction; + MatchFinder Matcher; +}; +/**********************************************************************************************************************/ +// For each source file provided to the tool, a new FrontendAction is created. +class MyFrontendAction : public ASTFrontendAction { +public: + MyFrontendAction() {} + void EndSourceFileAction() override { + TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID()).write(llvm::outs()); + } + + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef file) override { + TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); + return llvm::make_unique(TheRewriter); + } + +private: + Rewriter TheRewriter; +}; +/**********************************************************************************************************************/ +/*MAIN*/ +int main(int argc, const char **argv) { + CommonOptionsParser op(argc, argv, MatcherSampleCategory); + ClangTool Tool(op.getCompilations(), op.getSourcePathList()); + + return Tool.run(newFrontendActionFactory().get()); +} +/*last line intentionally left blank.*/ diff --git a/mutator.sh b/mutator.sh new file mode 100755 index 0000000..1448921 --- /dev/null +++ b/mutator.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +#default args +INPUT="./covtest/testFuncs1.c" +OUTPUT="./mutant.c" +COMMAND="all" + +while [[ $# -gt 0 ]] +do + passarg="$1" + + case $passarg in + -c|--command) + COMMAND="$2" + shift + ;; + -h|--help) + COMMAND="$2" + echo "Currently there is no help for this." + ;; + -v|--version) + echo "Version 1.0.0" + break + ;; + -t|--target) + INPUT="$2" + shift + ;; + *) + #not a valid argument + echo "$2 is not a valid argument." + break + ;; + esac + shift +done + +if [[ "$COMMAND" == clean ]]; then + echo "Running make clean..." + echo "Killing all mutants..." + "make" clean + rm "$OUTPUT" +elif [[ "$COMMAND" == build-all ]]; then + echo "Building all executables..." + "make" all +elif [[ "$COMMAND" == run ]];then + echo "Running executables on target file..." + "$OUTPUT" "$INPUT" -- +elif [[ "$COMMAND" == "default" ]]; then + echo "Building all target executables." + echo "Running all exetubales on target input." + "make" all + "./mutator" "$INPUT" -- > "$OUTPUT" +fi -- cgit v1.2.3