From 73ea45d58be4aac298795bc810f5429ccf90c8f0 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:43:49 +0430 Subject: adding some infrastructure.wip. --- bruiser/bruiser.cpp | 156 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 148 insertions(+), 8 deletions(-) diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index a7df7ca..4371e25 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* /*standard headers*/ #include #include -#include /*LLVM headers*/ #include "clang/AST/AST.h" #include "clang/AST/ASTConsumer.h" @@ -49,32 +48,170 @@ using namespace clang::driver; using namespace clang::tooling; /**********************************************************************************************************************/ /*global vars*/ - static llvm::cl::OptionCategory BruiserCategory("Empty"); /**********************************************************************************************************************/ +/*the implementation of the bruiser logger.*/ +bruiser::BruiserReport::BruiserReport () +{ + BruiserLog.open("bruiser.log"); +} + +bruiser::BruiserReport::~BruiserReport() +{ + BruiserLog.close(); +} + +bool bruiser::BruiserReport::PrintToLog(std::string __in_arg) +{ + BruiserLog << __in_arg << "\n"; + return !BruiserLog.bad(); +} +/**********************************************************************************************************************/ +bruiser::TypeInfo::TypeInfo(const clang::ast_type_traits::DynTypedNode* __dtn) : DTN(__dtn) {}; + +bruiser::TypeInfo::~TypeInfo() {}; + +const clang::Type* bruiser::TypeInfo::getTypeInfo(clang::ASTContext* __astc) +{ + const clang::Expr* EXP = DTN->get(); + + const clang::Type* TP = EXP->getType().getTypePtr(); + + return __astc->getCanonicalType(TP); +} +/**********************************************************************************************************************/ +class AbstractMatcherHandler : public virtual MatchFinder::MatchCallback +{ + public: + AbstractMatcherHandler (Rewriter &Rewrite) : R(Rewrite) {} + + public: + virtual void run(const MatchFinder::MatchResult &MR) + { + + } + + private: + Rewriter &R; +}; +/**********************************************************************************************************************/ +class MatcherHandlerLVL0 : public AbstractMatcherHandler +{ + public: + MatcherHandlerLVL0 (Rewriter &Rewrite) : AbstractMatcherHandler(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) override + { + + } + + private: +}; +/**********************************************************************************************************************/ +class NameFinder +{ + public: + NameFinder (Rewriter &Rewrite) : Rewrite(Rewrite) {} + + virtual void runDeclRefExprMatcher(const llvm::StringRef __sr) + { + //Matcher.addMatcher(); + } + + virtual void runDeclMatcher(const llvm::StringRef __sr) + { + + } + + private: + Rewriter &Rewrite; + MatchFinder Matcher; +}; +/**********************************************************************************************************************/ +/**********************************************************************************************************************/ +/**********************************************************************************************************************/ +class IfBreaker : public MatchFinder::MatchCallback +{ + public: + IfBreaker (Rewriter &Rewrite) : Rewrite(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) + { + if (MR.Nodes.getNodeAs("uno") != nullptr) + { + const clang::UnaryOperator* UO = MR.Nodes.getNodeAs("uno"); + + SourceLocation SL = UO->getLocStart(); + CheckSLValidity(SL); + SL = Devi::SourceLocationHasMacro(SL, Rewrite); + + const Expr* EXP = UO->getSubExpr(); + const ast_type_traits::DynTypedNode DynNode = ast_type_traits::DynTypedNode::create(*EXP); + bruiser::TypeInfo TIProto(&DynNode); + + const clang::Type* CTP = TIProto.getTypeInfo(MR.Context); + + //Matcher.addMatcher(); + } + + if (MR.Nodes.getNodeAs("dous") != nullptr) + { + const clang::BinaryOperator* BO = MR.Nodes.getNodeAs("dous"); + + SourceLocation SL = BO->getLocStart(); + CheckSLValidity(SL); + SL = Devi::SourceLocationHasMacro(SL, Rewrite); + + const Expr* LHS = BO->getLHS(); + const Expr* RHS = BO->getRHS(); + + QualType LQT = LHS->getType(); + QualType RQT = RHS->getType(); + + const clang::Type* LTP = LQT.getTypePtr(); + const clang::Type* RTP = RQT.getTypePtr(); + + const clang::Type* CLTP = MR.Context->getCanonicalType(LTP); + const clang::Type* CRTP = MR.Context->getCanonicalType(RTP); + } + } + + private: + Rewriter &Rewrite; + MatchFinder Matcher; +}; /**********************************************************************************************************************/ /**********************************************************************************************************************/ class MyASTConsumer : public ASTConsumer { public: - MyASTConsumer(Rewriter &R) {} + MyASTConsumer(Rewriter &R) : HIfBreaker(R) + {} + + void HandleTranslationUnit(ASTContext &Context) override + { + Matcher.addMatcher(ifStmt(hasDescendant(expr(anyOf(unaryOperator().bind("uno"), binaryOperator().bind("dous"))))), &HIfBreaker); - void HandleTranslationUnit(ASTContext &Context) override { Matcher.matchAST(Context); } private: + IfBreaker HIfBreaker; MatchFinder Matcher; + Rewriter R; }; /**********************************************************************************************************************/ -class MyFrontendAction : public ASTFrontendAction { +class MyFrontendAction : public ASTFrontendAction +{ public: MyFrontendAction() {} - void EndSourceFileAction() override { + void EndSourceFileAction() override + { TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID()).write(llvm::outs()); } - std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef file) override { + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef file) override + { TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); return llvm::make_unique(TheRewriter); } @@ -84,10 +221,13 @@ private: }; /**********************************************************************************************************************/ /*Main*/ -int main(int argc, const char **argv) { +int main(int argc, const char **argv) +{ CommonOptionsParser op(argc, argv, BruiserCategory); ClangTool Tool(op.getCompilations(), op.getSourcePathList()); + bruiser::BruiserReport BruiseRep; + return Tool.run(newFrontendActionFactory().get()); } /*last line interntionally left blank.*/ -- cgit v1.2.3 From 989518ead00d635b11fc2384feecd66cb2e3e50a Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:44:08 +0430 Subject: adding some infrastructure.wip. --- bruiser/bruiser.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/bruiser/bruiser.h b/bruiser/bruiser.h index cf22e5e..fcfcaec 100644 --- a/bruiser/bruiser.h +++ b/bruiser/bruiser.h @@ -27,9 +27,57 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* /*included modules*/ /*project headers*/ /*standard library headers*/ +#include +#include +/*clang headers*/ +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Type.h" +#include "clang/AST/ASTTypeTraits.h" +/*llvm headers*/ + +/**********************************************************************************************************************/ +/*using*/ +using namespace llvm; +using namespace clang; +/**********************************************************************************************************************/ +namespace bruiser +{ +/**********************************************************************************************************************/ +/** + * @brief This class hanhles the logging for bruiser. + */ +class BruiserReport +{ + public: + BruiserReport (); + + ~BruiserReport(); + + bool PrintToLog(std::string __in_arg); + + private: + std::ofstream BruiserLog; +}; +/**********************************************************************************************************************/ +/** + * @brief looks through types. + */ +class TypeInfo +{ + public: + explicit TypeInfo(const clang::ast_type_traits::DynTypedNode* __dtn); + + ~TypeInfo(); + + const clang::Type* getTypeInfo(clang::ASTContext* __astc); + + private: + const clang::ast_type_traits::DynTypedNode* DTN; +}; /**********************************************************************************************************************/ -/*externals*/ /**********************************************************************************************************************/ +} // end of namespace bruiser #endif /*last line intentionally left balnk.*/ -- cgit v1.2.3 From 5a0b18dc3d1db4efe427968ede383f22adcbbb82 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:45:01 +0430 Subject: added a newerv method for checking Macro expansions --- mutator_aux.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mutator_aux.cpp b/mutator_aux.cpp index 9153004..9eac0dd 100644 --- a/mutator_aux.cpp +++ b/mutator_aux.cpp @@ -36,7 +36,7 @@ using namespace clang; namespace Devi { /*a simple function that checks the sourcelocations for a macro expansion. returns the sourcelocation without macro expansion address.*/ -SourceLocation SourceLocationHasMacro (SourceLocation SL, Rewriter &Rewrite, std::string Kind) +SourceLocation SourceLocationHasMacro [[deprecated("doesnt work")]] (SourceLocation SL, Rewriter &Rewrite, std::string Kind) { /*does the sourcelocation include a macro expansion?*/ if ( SL.isMacroID() ) @@ -65,6 +65,18 @@ SourceLocation SourceLocationHasMacro (SourceLocation SL, Rewriter &Rewrite, std return (SL); } + +SourceLocation SourceLocationHasMacro(SourceLocation __sl, Rewriter &__rewrite) +{ + if (__sl.isMacroID()) + { + return __rewrite.getSourceMgr().getSpellingLoc(__sl); + } + else + { + return __sl; + } +} /*********************************************************************************************************************/ /*********************************************************************************************************************/ /*********************************************************************************************************************/ -- cgit v1.2.3 From bedd50319a11e2a5482d7c715e1d64998e8009e2 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:45:22 +0430 Subject: added a newer method for checking macro expansions. marked the old one as deprecated. --- mutator_aux.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mutator_aux.h b/mutator_aux.h index 5385742..f1ad3fe 100644 --- a/mutator_aux.h +++ b/mutator_aux.h @@ -44,7 +44,9 @@ enum class Scope {NoValue, TU, Block}; enum class FunctionDeclKind {NoValue, Definition, Declaration}; /*********************************************************************************************************************/ -SourceLocation SourceLocationHasMacro (SourceLocation SL, Rewriter &Rewrite, std::string Kind); +SourceLocation SourceLocationHasMacro [[deprecated("doesnt work")]] (SourceLocation SL, Rewriter &Rewrite, std::string Kind); + +SourceLocation SourceLocationHasMacro(SourceLocation __sl, Rewriter &__rewrite); /*********************************************************************************************************************/ bool IsTheMatchInSysHeader(bool SysHeaderFlag, const ast_matchers::MatchFinder::MatchResult &MR, SourceLocation SL); -- cgit v1.2.3 From b1d1fcd650ee6f0c2d1ebb3183364fb50517f4c1 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:45:59 +0430 Subject: re-enabled the custom DiagnosticConsumer --- mutator-lvl0.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/mutator-lvl0.cpp b/mutator-lvl0.cpp index 6a37c01..3aa55c5 100644 --- a/mutator-lvl0.cpp +++ b/mutator-lvl0.cpp @@ -8116,20 +8116,12 @@ public: DiagnosticsEngine &DiagEngine = CI.getPreprocessor().getDiagnostics(); -#if 0 - std::unique_ptr M0DiagConsumer(new Mutator0DiagnosticConsumer); -#endif - -#if 0 +#if 1 Mutator0DiagnosticConsumer* M0DiagConsumer = new Mutator0DiagnosticConsumer; DiagEngine.setClient(M0DiagConsumer, true); #endif -#if 0 - const IdentifierTable &IT [[maybe_unused]] = CI.getPreprocessor().getIdentifierTable(); -#endif - TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); return llvm::make_unique(TheRewriter); } -- cgit v1.2.3 From 45621f027b420acfd05c8a04b646ac73a3dd89cd Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:46:29 +0430 Subject: nothing really --- mutator_report.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mutator_report.cpp b/mutator_report.cpp index 94ddd6b..e61cb5d 100644 --- a/mutator_report.cpp +++ b/mutator_report.cpp @@ -257,9 +257,12 @@ void JSONReport::CloseReport(void) JSONRepFile.close(); } /*********************************************************************************************************************/ -/****************************************************End Of JSONReport************************************************/ +/*************************************************End Of JSONReport***************************************************/ +/*********************************************************************************************************************/ + /*********************************************************************************************************************/ /*End of namespace Devi*/ +/*********************************************************************************************************************/ } /*********************************************************************************************************************/ /*last line intentionally left blank.*/ -- cgit v1.2.3 From 1801c72709d781bf883750dd984d793519f43b74 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:46:59 +0430 Subject: nothing --- mutator_report.h | 1 + 1 file changed, 1 insertion(+) diff --git a/mutator_report.h b/mutator_report.h index e621bcd..a1e955b 100644 --- a/mutator_report.h +++ b/mutator_report.h @@ -89,6 +89,7 @@ private: std::ofstream JSONRepFile; }; /*********************************************************************************************************************/ +/*********************************************************************************************************************/ /*end of namespace Devi*/ } #endif -- cgit v1.2.3 From 43af08e7481faa65bf9a427b5b939ccea645977b Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:47:15 +0430 Subject: added a new part called how to get it --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 91f6c68..0b28a35 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,23 @@ Mutation levels have nothing to do with the order of mutants.
**bruiser** tries to break your code.

+## How To get project mutator + +Assuming you already have the LLVM/Clang libraries, just run : + +```bash + +git clone https://github.com/bloodstalker/mutator +make +make install + +``` + +If you don't have them, you can build them or get them from a repo.
+To build LLVM/Clang from source take a look at [here](https://clang.llvm.org/get_started.html) and [here](http://llvm.org/docs/GettingStarted.html).
+To build `safercpp-arr` you to need to build Clang with RTTI enabled.
+On Fedora you can just get the Requirements by dnf. For Ubuntu and Debian either look at mutator's `.travis.yaml` or check out the [nightly builds for Debian/Ubuntu](apt.llvm.org).
+ ### Dev Status All the as-of-yet implemented features of the project are very much buildable and usable at all times, even during the dev phase on the master branch. If something's not working properly let me know.
-- cgit v1.2.3 From 5a0f84b4a0615002b603954c112427b59d78b782 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 13:52:16 +0430 Subject: fixed a broken link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b28a35..6849750 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ make install If you don't have them, you can build them or get them from a repo.
To build LLVM/Clang from source take a look at [here](https://clang.llvm.org/get_started.html) and [here](http://llvm.org/docs/GettingStarted.html).
To build `safercpp-arr` you to need to build Clang with RTTI enabled.
-On Fedora you can just get the Requirements by dnf. For Ubuntu and Debian either look at mutator's `.travis.yaml` or check out the [nightly builds for Debian/Ubuntu](apt.llvm.org).
+On Fedora you can just get the Requirements by dnf. For Ubuntu and Debian either look at mutator's `.travis.yaml` or check out the [nightly builds for Debian/Ubuntu](http://apt.llvm.org).
### Dev Status All the as-of-yet implemented features of the project are very much buildable and usable at all times, even during the dev phase on the master branch. If something's not working properly let me know.
-- cgit v1.2.3 From 11d22f7d8b9ae021c234b581adcbc273ea8a1208 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 13 Apr 2017 23:31:38 +0430 Subject: added some more experimental stuff. right now bruiser can only hijack the main. --- bruiser/bruiser.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 4371e25..439605b 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -98,7 +98,7 @@ class AbstractMatcherHandler : public virtual MatchFinder::MatchCallback class MatcherHandlerLVL0 : public AbstractMatcherHandler { public: - MatcherHandlerLVL0 (Rewriter &Rewrite) : AbstractMatcherHandler(Rewrite) {} + explicit MatcherHandlerLVL0 (Rewriter &Rewrite) : AbstractMatcherHandler(Rewrite) {} virtual void run(const MatchFinder::MatchResult &MR) override { @@ -111,21 +111,45 @@ class MatcherHandlerLVL0 : public AbstractMatcherHandler class NameFinder { public: - NameFinder (Rewriter &Rewrite) : Rewrite(Rewrite) {} + NameFinder () {} - virtual void runDeclRefExprMatcher(const llvm::StringRef __sr) - { - //Matcher.addMatcher(); - } - - virtual void runDeclMatcher(const llvm::StringRef __sr) - { - - } + class runDeclRefExprMatcher + { + public: + runDeclRefExprMatcher (Rewriter &__rwrt) : LVL0Proto(__rwrt), __rwrt(__rwrt) {} + + virtual void runMatcher(const StringRef __sr, ASTContext &__ctx) + { + Matcher.addMatcher(declRefExpr(to(namedDecl(hasName(__sr.str())))).bind("declrefexpbyname"), &LVL0Proto); + Matcher.matchAST(__ctx); + } + + private: + MatchFinder Matcher; + MatcherHandlerLVL0 LVL0Proto; + Rewriter __rwrt; + StringRef __sr; + }; + + class runNamedDeclMatcher + { + public: + runNamedDeclMatcher (Rewriter &__rwrt) : LVL0Proto(__rwrt), __rwrt(__rwrt) {} + + virtual void runMatcher(const StringRef __sr, ASTContext &__ctx) + { + Matcher.addMatcher(declRefExpr(to(namedDecl(hasName(__sr.str())))).bind("nameddeclbyname"), &LVL0Proto); + Matcher.matchAST(__ctx); + } + + private: + MatchFinder Matcher; + MatcherHandlerLVL0 LVL0Proto; + Rewriter __rwrt; + StringRef __sr; + }; private: - Rewriter &Rewrite; - MatchFinder Matcher; }; /**********************************************************************************************************************/ /**********************************************************************************************************************/ @@ -151,7 +175,6 @@ class IfBreaker : public MatchFinder::MatchCallback const clang::Type* CTP = TIProto.getTypeInfo(MR.Context); - //Matcher.addMatcher(); } if (MR.Nodes.getNodeAs("dous") != nullptr) @@ -181,22 +204,68 @@ class IfBreaker : public MatchFinder::MatchCallback MatchFinder Matcher; }; /**********************************************************************************************************************/ +/** + * @brief Hijacks the main main and replaces it with bruiser's main. + */ +class MainWrapper : public MatchFinder::MatchCallback +{ +public: + MainWrapper (Rewriter &Rewrite) : Rewrite(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) + { + if (MR.Nodes.getNodeAs("mainwrapper") != nullptr) + { + const FunctionDecl* FD = MR.Nodes.getNodeAs("mainwrapper"); + + SourceLocation SL = FD->getLocStart(); + CheckSLValidity(SL); + SL = Devi::SourceLocationHasMacro(SL, Rewrite); + + SourceLocation SLE = FD->getLocEnd(); + CheckSLValidity(SLE); + SLE = Devi::SourceLocationHasMacro(SLE, Rewrite); + + SourceRange SR(SL, SLE); + + std::string MainSig = Rewrite.getRewrittenText(SR); + + size_t mainbegin = MainSig.find("main"); + + StringRef __sr("sub_main"); + + Rewrite.ReplaceText(SL.getLocWithOffset(mainbegin), 4U, __sr); + + Rewrite.InsertTextAfter(SLE.getLocWithOffset(1U), StringRef("\n\nint main(int argc, const char **argv)\n{\n\tsub_main(argc, argv);\n}")); + + //ruiseRep << "changed main main's name.\n" + } + } + + private: + Rewriter &Rewrite; +}; +/**********************************************************************************************************************/ +/**********************************************************************************************************************/ /**********************************************************************************************************************/ class MyASTConsumer : public ASTConsumer { public: - MyASTConsumer(Rewriter &R) : HIfBreaker(R) + MyASTConsumer(Rewriter &R) : HIfBreaker(R), HMainWrapper(R) {} void HandleTranslationUnit(ASTContext &Context) override { Matcher.addMatcher(ifStmt(hasDescendant(expr(anyOf(unaryOperator().bind("uno"), binaryOperator().bind("dous"))))), &HIfBreaker); + Matcher.addMatcher(functionDecl(hasName("main")).bind("mainwrapper"), &HMainWrapper); + Matcher.matchAST(Context); } private: IfBreaker HIfBreaker; + MainWrapper HMainWrapper; MatchFinder Matcher; Rewriter R; }; -- cgit v1.2.3