virtual void run(const MatchFinder::MatchResult &Result){//free const CallExpr* CE = Result.Nodes.getNodeAs<CallExpr>("free"); #ifdef DEBUG llvm::errs()<<"----CallExpr(free) find----\n"; #endif if(!CE) return ; //得到free()里的第0个参数,进行类似的操作 const SourceManager *SM = Result.SourceManager; const Expr * arg = CE->getArg(0); checkPoint cp; cp.name = rewrite.ConvertToString((Stmt*)arg); SourceLocation locStart = CE->getLocStart(); std::string str_locStart = locStart.printToString(*SM); loc_strToint(cp.row,cp.col,str_locStart.c_str()); #ifdef DEBUG llvm::errs()<<"cp:"<<cp.row<<" "<<cp.col<<" " + str_locStart + "\n"; #endif bool findFlag = false; for(unsigned i=0;i<cpVecF.size();++i){ if(cp.name == cpVecF[i].name && cp.row == cpVecF[i].row){ findFlag = true; cp.col = cpVecF[i].col; cp.declName = cpVecF[i].declName; cp.declRow = cpVecF[i].declRow; cp.declCol = cpVecF[i].declCol; break; } } if(!findFlag){ cp.declName = cp.name; cp.declRow = cp.row; cp.declCol = cp.col; } char buf[4][32]; sprintf(buf[0],"%d",cp.row); sprintf(buf[1],"%d",cp.col); sprintf(buf[2],"%d",cp.declRow); sprintf(buf[3],"%d",cp.declCol); //之后可以进行开文件的优化 std::string str_insert = "\n{\n\tFILE *fp = fopen(\"" + checkDataFileName +"\",\"a\");\n" "\tif(fp == NULL){\n" + "\t\tprintf(\"fail to open file " + checkDataFileName + "!\\n\");\n" + "\t\texit(-1);\n" + "\t}\n" + //"\tfprintf(fp,\"f " + cp.name + " " + buf[0] + " " + buf[1] + " " + cp.declName + " " + buf[2] + " " + buf[3] + " %x \\n\"," + cp.name + ");\n" + "\tfprintf(fp,\"f " + cp.name + " " + buf[0] + " " + buf[1] + " %x \\n\"," + cp.name + ");\n" + "\tfclose(fp);\n" + "\tint fd=open(FIFO_SERVER,O_WRONLY |O_NONBLOCK,0);\n" + "\tif(fd==-1){perror(\"open\");exit(1);}\n" + "\tchar w_buf[100];\n" + "\tsprintf(w_buf,\"f "+ cp.name + " " + buf[0] + " " + buf[1] + " %x \\n\"," + cp.name + ");\n" + "\tif(write(fd,w_buf,100)==-1){\n" + "\t\tif(errno==EAGAIN)\n" + "\t\t\tprintf(\"The FIFO has not been read yet.Please try later\\n\");\n" + "\t}\n" + "\telse\n" + "\t\tprintf(\"write %s to the FIFO\\n\",w_buf);\n" + "\tsleep(1);\n" + "\tclose(fd);\n" + "}\n"; // llvm::errs() << "-----\n"<<str_insert<<"\n----\n"; SourceLocation SL_locWithOffset = locStart; rewrite.InsertText(SL_locWithOffset,str_insert.c_str(),true,true); #ifdef DEBUG llvm::errs()<<"----CallExpr(free) end----\n"; #endif }
virtual void run(const MatchFinder::MatchResult &Result){ const BinaryOperator* BO = Result.Nodes.getNodeAs<BinaryOperator>("malloc"); #ifdef DEBUG llvm::errs()<<"----BinaryOperator(malloc) find----\n"; #endif if(!BO) return ; const SourceManager *SM = Result.SourceManager; SourceLocation locEnd = BO->getLocEnd(); checkPoint cp; //得到插装位置,找到mallocVarMatcher之前对应匹配到的信息(其实可以不用MallocVarMatcher,MallocVarMatcher只能匹配到纯粹的指针(不带*的)) std::string str_locEnd = locEnd.printToString(*SM); loc_strToint(cp.row,cp.col,str_locEnd.c_str()); bool findFlag = false; int findI; #ifdef DEBUG llvm::errs() <<"binary loc:" <<"|"<<cp.row<<"|"<<cp.col<<"\n"; #endif for(unsigned i=0;i<cpVec.size();++i){ if(cpVec[i].row == cp.row){ findFlag = true; findI = i; break; } } //左子树得到的 = 的左边 Expr * lhs = BO->getLHS(); cp.name = rewrite.ConvertToString((Stmt*)lhs); QualType qt = lhs->getType(); #ifdef DEBUG llvm::errs()<<"lhs cp.name :"<<cp.name<<"\n"; llvm::errs()<<"lhs type :"<<qt.getAsString()<<"\n"; lhs->dump(); #endif //找到的话直接用 if(findFlag){ const NamedDecl *ND = ((DeclRefExpr*)lhs)->getFoundDecl(); std::string str_decl = ND->getNameAsString(); cp.declName = str_decl; SourceLocation declLocStart = ND->getLocStart(); std::string str_declLocStart = declLocStart.printToString(*SM); loc_strToint(cp.declRow,cp.declCol,str_declLocStart.c_str()); }else{//没找到的话,添加进来 cp.declName = cp.name; cp.declRow = cp.row; cp.declCol = cp.col; } //string + 不支持int类型,所以先换成char* char buf[4][32]; sprintf(buf[0],"%d",cp.row); sprintf(buf[1],"%d",cp.col); sprintf(buf[2],"%d",cp.declRow); sprintf(buf[3],"%d",cp.declCol); //将程序运行时的指针值存下来 %x std::string str_insert = "\n{\n\tFILE *fp = fopen(\"" + checkDataFileName +"\",\"a\");\n" "\tif(fp == NULL){\n" + "\t\tprintf(\"fail to open file " + checkDataFileName + "!\\n\");\n" + "\t\texit(-1);\n" + "\t}\n" + //"\tfprintf(fp,\"m " + cp.name + " " + buf[0] + " " + buf[1] + " " + cp.declName + " " + buf[2] + " " + buf[3] + " %x \\n\"," + cp.name + ");\n" + "\tfprintf(fp,\"m " + cp.name + " " + buf[0] + " " + buf[1] + " %x \\n\"," + cp.name + ");\n" + "\tfclose(fp);\n\n" + "\tint fd=open(FIFO_SERVER,O_WRONLY |O_NONBLOCK,0);\n" + "\tif(fd==-1){perror(\"open\");exit(1);}\n" + "\tchar w_buf[100];\n" + "\tsprintf(w_buf,\"m "+ cp.name + " " + buf[0] + " " + buf[1] + " %x \\n\"," + cp.name + ");\n" + "\tif(write(fd,w_buf,100)==-1){\n" + "\t\tif(errno==EAGAIN)\n" + "\t\t\tprintf(\"The FIFO has not been read yet.Please try later\\n\");\n" + "\t}\n" + "\telse\n" + "\t\tprintf(\"write %s to the FIFO\\n\",w_buf);\n" + "\tsleep(1);\n" + "\tclose(fd);\n" + "}\n"; //llvm::errs() << "-----\n"<<str_insert<<"\n----\n"; //找位置插装 int locOffset = 2; SourceLocation SL_locWithOffset = locEnd.getLocWithOffset(locOffset); rewrite.InsertText(SL_locWithOffset,str_insert.c_str(),true,true); if(!findFlag){ cpVec.push_back(cp); } #ifdef DEBUG llvm::errs()<<"----BinaryOperator(malloc) end----\n"; #endif }