From 818ba5ecde1b05a169b80562df1e24a0f28ffcfe Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Wed, 29 Mar 2017 15:07:14 +0430 Subject: changed the matchers for safercpparr. i still have to add a matcher that recognizes pointers pointing to an array. --- mutator-lvl0.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/mutator-lvl0.cpp b/mutator-lvl0.cpp index cacb098..17afc46 100644 --- a/mutator-lvl0.cpp +++ b/mutator-lvl0.cpp @@ -72,10 +72,18 @@ using namespace clang::driver; using namespace clang::tooling; /**********************************************************************************************************************/ /*macros and defs*/ + +/*@DEVI-disbale debugs info printouts.*/ #define _MUT0_TEST -#if 1 +#if 0 #undef _MUT0_TEST #endif + +/*@DEVI-disbale all matchers.*/ +#define _MUT0_DIS_MATCHERS +#if 0 +#undef _MUT0_DIS_MATCHERS +#endif /**********************************************************************************************************************/ /*global vars*/ Devi::XMLReport XMLDocOut; @@ -5774,11 +5782,11 @@ class SFCPPARR01 : public MatchFinder::MatchCallback JSONDocOUT.JSONAddElement(MR.Context, SL, "SaferCPP01", "Native CPP array declared:"); } - if (MR.Nodes.getNodeAs("sfcpparrsubscript") != nullptr) + if (MR.Nodes.getNodeAs("sfcpparrcastexpr") != nullptr) { - const ArraySubscriptExpr* ASE = MR.Nodes.getNodeAs("sfcpparrsubscript"); + const CastExpr* CS = MR.Nodes.getNodeAs("sfcpparrcastexpr"); - SourceLocation SL = ASE->getLocStart(); + SourceLocation SL = CS->getLocStart(); CheckSLValidity(SL); SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); @@ -7299,20 +7307,16 @@ public: HandlerForFunction167(R), HandlerForCF143(R), HandlerForExpr1212(R), HandlerForExpr1211(R), HandlerForAtc105(R), HandlerForCSE135(R), \ HandlerForTypes612(R), HandlerForConst71(R), HandlerForIdent5X(R), HandlerForSFCPPARR01(R) { -#if 1 - /*forstmts whithout a compound statement.*/ +/*@DEVI-disables all matchers*/ +#if defined(_MUT0_DIS_MATCHERS) Matcher.addMatcher(forStmt(unless(hasDescendant(compoundStmt()))).bind("mcfor"), &HandlerForCmpless); - /*whilestmts without a compound statement.*/ Matcher.addMatcher(whileStmt(unless(hasDescendant(compoundStmt()))).bind("mcwhile"), &HandlerWhileCmpless); - /*else blocks that dont have a compound statemnt.*/ Matcher.addMatcher(ifStmt(allOf(hasElse(unless(ifStmt())), hasElse(unless(compoundStmt())))).bind("mcelse"), &HandlerElseCmpless); - /*if blocks that dont have a compound statement.*/ Matcher.addMatcher(ifStmt(unless(hasDescendant(compoundStmt()))).bind("mcif"), &HandlerIfCmpless); - /*if-elseif statements that are missing the else block.*/ Matcher.addMatcher(ifStmt(allOf(hasElse(ifStmt()), unless(hasAncestor(ifStmt())), unless(hasDescendant(ifStmt(hasElse(unless(ifStmt()))))))).bind("mcifelse"), &HandlerForIfElse); Matcher.addMatcher(switchStmt(hasDescendant(compoundStmt(hasDescendant(switchCase(unless(hasDescendant(breakStmt()))))))).bind("mcswitchbrk"), &HandlerForSwitchBrkLess); @@ -7561,9 +7565,11 @@ public: Matcher.addMatcher(enumConstantDecl(hasAncestor(functionDecl().bind("id5funcscope"))).bind("ident5enumconst"), &HandlerForIdent5X); /*end of matchers for 5.x*/ - Matcher.addMatcher(arraySubscriptExpr().bind("sfcpparrsubscript"), &HandlerForSFCPPARR01); - Matcher.addMatcher(varDecl(hasType(arrayType())).bind("sfcpparrdecl"), &HandlerForSFCPPARR01); + + Matcher.addMatcher(implicitCastExpr(hasCastKind(CK_ArrayToPointerDecay)).bind("sfcpparrcastexpr"), &HandlerForSFCPPARR01); + + Matcher.addMatcher(cStyleCastExpr(hasCastKind(CK_ArrayToPointerDecay)).bind("sfcpparrcastexpr"), &HandlerForSFCPPARR01); #endif } -- cgit v1.2.3 From 9c7bba0c8c1631b06e64185c2ea64a41047ee68f Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Wed, 29 Mar 2017 15:08:25 +0430 Subject: wip --- mutator-lvl0.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mutator-lvl0.h b/mutator-lvl0.h index b79656a..20b26db 100644 --- a/mutator-lvl0.h +++ b/mutator-lvl0.h @@ -325,6 +325,8 @@ std::multimap MC1EquivalencyMap = { {"127","20.12"} }; +std::pair Mutator0RuleChecks; + std::multimap MC3EquivalencyMap; std::unordered_map SaferCPPEquivalencyMap; -- cgit v1.2.3 From 68a2b37aed75a6717ef6aa5f39f548a39dbefaf3 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Wed, 29 Mar 2017 15:08:54 +0430 Subject: added the frtti option,added a check for rtti before trying ot build it --- safercpp/makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/safercpp/makefile b/safercpp/makefile index cba8d9e..cd5a0b8 100644 --- a/safercpp/makefile +++ b/safercpp/makefile @@ -3,7 +3,12 @@ include ../macros.mk #######################################VARS#################################### +CXX_FLAGS+=-frtti SFCPP01=safercpp-arr + +ifeq ($(shell $(LLVM_CONF) --has-rtti), NO) +$(error your llvm-config says you dont have rtti. you cant build safercpparr without rtti support.) +endif ######################################RULES#################################### .DEFAULT: all -- cgit v1.2.3 From 77e3931df0a5aed6bd49bea083ec1617324abd8b Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Thu, 30 Mar 2017 13:24:45 +0430 Subject: added safercpp native array detection, now also detects use of pointers pointing to arrays. tdds already exist in testFuncs2 --- mutator-lvl0.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) diff --git a/mutator-lvl0.cpp b/mutator-lvl0.cpp index 17afc46..606dc6e 100644 --- a/mutator-lvl0.cpp +++ b/mutator-lvl0.cpp @@ -5811,6 +5811,124 @@ class SFCPPARR01 : public MatchFinder::MatchCallback Rewriter &Rewrite; }; /**********************************************************************************************************************/ +/** + * @brief The matcher run by SFCPPARR02. This ones does all the real tagging. + */ +class SFCPPARR02SUB : public MatchFinder::MatchCallback +{ + public: + SFCPPARR02SUB (Rewriter &Rewrite) : Rewrite(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) + { + if (MR.Nodes.getNodeAs("sfcpp02sub") != nullptr) + { + const DeclRefExpr* DRE = MR.Nodes.getNodeAs("sfcpp02sub"); + + SourceManager *const SM = MR.SourceManager; + + SourceLocation SL = DRE->getLocStart(); + CheckSLValidity(SL); + //SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); + SL = SM->getSpellingLoc(SL); + + if (Devi::IsTheMatchInSysHeader(CheckSystemHeader, MR, SL)) + { + return void(); + } + + if (!Devi::IsTheMatchInMainFile(MainFileOnly, MR, SL)) + { + return void(); + } + + const NamedDecl* ND = DRE->getFoundDecl(); + + SourceLocation OriginSL = ND->getLocStart(); + CheckSLValidity(OriginSL); + //OriginSL = Devi::SourceLocationHasMacro(OriginSL, Rewrite, "start"); + OriginSL = SM->getSpellingLoc(OriginSL); + + StringRef OriginFileName [[maybe_unused]] = SM->getFilename(OriginSL); + +#if 0 + std::cout << "GarbageOut" << ":" << "Origin:" << DRE->getFoundDecl()->getName().str() << std::endl; + std::cout << "GarbageOut" << ":" << "Origin:" << ExtOriginFileName.str() << ":" << "Proto:" << OriginFileName.str() << std::endl; + std::cout << "GarbageOut" << ":" << "Origin:" << ExtOriginSL.printToString(*SM) << ":" << "Proto:" << OriginSL.printToString(*SM) << std::endl; +#endif + + if (OriginSL == ExtOriginSL && OriginFileName == ExtOriginFileName) + { + std::cout << "SaferCPP01" << ":" << "Native Array used - pointer points to an array:" << SL.printToString(*MR.SourceManager) << ":" << DRE->getFoundDecl()->getName().str() << std::endl; + } + + XMLDocOut.XMLAddNode(MR.Context, SL, "SaferCPP01", "Native Array used - pointer points to an array:"); + JSONDocOUT.JSONAddElement(MR.Context, SL, "SaferCPP01", "Native Array used - pointer points to an array:"); + } + } + + void setOriginSourceLocation(SourceLocation inSL) + { + ExtOriginSL = inSL; + } + + void setOriginFileName(StringRef inStrRef) + { + ExtOriginFileName = inStrRef; + } + + private: + Rewriter &Rewrite; + SourceLocation ExtOriginSL; + StringRef ExtOriginFileName; +}; +/**********************************************************************************************************************/ +/** + * @brief MatchCallback for safercpp matching of pointers pointing to arrays. + */ +class SFCPPARR02 : public MatchFinder::MatchCallback +{ + public: + SFCPPARR02 (Rewriter &Rewrite) : Rewrite(Rewrite), SubHandler(Rewrite) {} + + virtual void run(const MatchFinder::MatchResult &MR) + { + if (MR.Nodes.getNodeAs("sfcpparrdeep") != nullptr) + { + const DeclRefExpr* DRE = MR.Nodes.getNodeAs("sfcpparrdeep"); + + ASTContext *const ASTC = MR.Context; + + SourceManager *const SM = MR.SourceManager; + + SourceLocation SL = DRE->getLocStart(); + CheckSLValidity(SL); + SL = SM->getSpellingLoc(SL); + + const NamedDecl* ND = DRE->getFoundDecl(); + + StringRef NDName = ND->getName(); + + SubHandler.setOriginSourceLocation(SM->getSpellingLoc(ND->getLocStart())); + SubHandler.setOriginFileName(SM->getFilename(SM->getSpellingLoc(ND->getLocStart()))); + + Matcher.addMatcher(declRefExpr(to(varDecl(hasName(NDName.str())))).bind("sfcpp02sub"), &SubHandler); + + Matcher.matchAST(*ASTC); + +#if 0 + std::cout << "GarbageOutOrigin" << ":" << "GarbageOutOrigin:" << SL.printToString(*MR.SourceManager) << ":" << NDName.str() << std::endl; +#endif + } + } + + private: + Rewriter &Rewrite; + MatchFinder Matcher; + SFCPPARR02SUB SubHandler; +}; +/**********************************************************************************************************************/ +/**********************************************************************************************************************/ /**********************************************************************************************************************/ /**********************************************************************************************************************/ /**********************************************************************************************************************/ @@ -7305,7 +7423,7 @@ public: HandlerForPointer1723(R), HandlerForPointer174(R), HandlerForPointer175(R), HandlerForTypes61(R), HandlerForSU181(R), \ HandlerForMCPTCCSTYLE(R), HandlerForATC101(R), HandlerForIdent51(R), HandlerForDCDF87(R), HandlerForDCDF88(R), HandlerForLangX23(R), \ HandlerForFunction167(R), HandlerForCF143(R), HandlerForExpr1212(R), HandlerForExpr1211(R), HandlerForAtc105(R), HandlerForCSE135(R), \ - HandlerForTypes612(R), HandlerForConst71(R), HandlerForIdent5X(R), HandlerForSFCPPARR01(R) { + HandlerForTypes612(R), HandlerForConst71(R), HandlerForIdent5X(R), HandlerForSFCPPARR01(R), HandlerForSFCPPARR02(R) { /*@DEVI-disables all matchers*/ #if defined(_MUT0_DIS_MATCHERS) @@ -7570,6 +7688,9 @@ public: Matcher.addMatcher(implicitCastExpr(hasCastKind(CK_ArrayToPointerDecay)).bind("sfcpparrcastexpr"), &HandlerForSFCPPARR01); Matcher.addMatcher(cStyleCastExpr(hasCastKind(CK_ArrayToPointerDecay)).bind("sfcpparrcastexpr"), &HandlerForSFCPPARR01); + + Matcher.addMatcher(declRefExpr(hasAncestor(binaryOperator(allOf(hasLHS(declRefExpr().bind("sfcpparrdeep")), hasRHS(hasDescendant(implicitCastExpr(hasCastKind(CK_ArrayToPointerDecay))))\ + , hasOperatorName("="))))), &HandlerForSFCPPARR02); #endif } @@ -7648,6 +7769,7 @@ private: MCConst71 HandlerForConst71; MCIdent5x HandlerForIdent5X; SFCPPARR01 HandlerForSFCPPARR01; + SFCPPARR02 HandlerForSFCPPARR02; MatchFinder Matcher; }; /**********************************************************************************************************************/ -- cgit v1.2.3