Exemplo n.º 1
0
void Graph::toDotLines(std::string s, raw_ostream *stream) {

		Instruction *A;
		StringRef File;
		OpNode *op;
		VarNode *va;
		CallNode *ca;
		ostringstream label;
		MDNode *N;

		unsigned Line;


        (*stream) << "digraph \"DFG for \'" << s << "\'  \"{\n";
        (*stream) << "label=\"DFG for \'" << s << "\' \";\n";

        std::map<GraphNode*, int> DefinedNodes;

        for (std::set<GraphNode*>::iterator node = nodes.begin(), end = nodes.end(); node != end; node++) {
        		label.str("");
        		if ((op = dyn_cast<OpNode>((*node)))) {
        			if (op->getValue() != NULL) {
						if ((A = dyn_cast<Instruction>(op->getValue()))) {
							if ((N = A->getMetadata("dbg"))) {
								DILocation Loc(N);
								File = Loc.getFilename();
								Line = Loc.getLineNumber();
								label << File.str() << " " << Line;
							} else {
								label << "null";
							}
						}
        			}else
        				label << "null";
        		}else if ((va = dyn_cast<VarNode>((*node)))) {
        			if (va->getValue() != NULL) {
						if ((A = dyn_cast<Instruction>(va->getValue()))) {
							if ((N = A->getMetadata("dbg"))) {
								DILocation Loc(N);
								File = Loc.getFilename();
								Line = Loc.getLineNumber();
								label << File.str() << " " << Line;
							} else {
								label << "null";
							}
						}
        			}else
        				label << "null";
        		}else if ((ca = dyn_cast<CallNode>((*node)))) {
        			if (ca->getValue() != NULL) {
						if ((A = dyn_cast<Instruction>(ca->getValue()))) {
							if ((N = A->getMetadata("dbg"))) {
								DILocation Loc(N);
								File = Loc.getFilename();
								Line = Loc.getLineNumber();
								label << File.str() << " " << Line;
							} else {
								label << "null";
							}
						}
        			}else
        				label << "null";
        		}


        		if (DefinedNodes.count(*node) == 0) {
                        (*stream) << (*node)->getName() << "[shape=" << (*node)->getShape() << ",style=" << (*node)->getStyle() << ",label=\"" << label.str() << "\"]\n";
                        DefinedNodes[*node] = 1;
                }


                std::map<GraphNode*, edgeType> succs = (*node)->getSuccessors();

                for (std::map<GraphNode*, edgeType>::iterator succ = succs.begin(), s_end = succs.end(); succ != s_end; succ++) {
                					label.str("");
                					if ((op = dyn_cast<OpNode>((succ->first)))) {
                						if (op->getValue() != NULL) {
											if ((A = dyn_cast<Instruction>(op->getValue()))) {
												if ((N = A->getMetadata("dbg"))) {
													DILocation Loc(N);
													File = Loc.getFilename();
													Line = Loc.getLineNumber();
													label << File.str() << " " << Line;
												} else {
													label << "null";
												}
											}
                						}else
                						   label << "null";
                	        		}else if ((va = dyn_cast<VarNode>((succ->first)))) {
                	        			if (va->getValue() != NULL) {
											if ((A = dyn_cast<Instruction>(va->getValue()))) {
												if ((N = A->getMetadata("dbg"))) {
													DILocation Loc(N);
													File = Loc.getFilename();
													Line = Loc.getLineNumber();
													label << File.str() << " " << Line;
												} else {
													label << "null";
												}
											}
                	        			}else
                	        			   label << "null";
                	        		}else if ((ca = dyn_cast<CallNode>((succ->first)))) {
                	        			if (ca->getValue() != NULL) {
											if ((A = dyn_cast<Instruction>(ca->getValue()))) {
												if ((N = A->getMetadata("dbg"))) {
													DILocation Loc(N);
													File = Loc.getFilename();
													Line = Loc.getLineNumber();
													label << File.str() << " " << Line;
												} else {
													label << "null";
												}
											}
                	        			}else
                	        			  label << "null";
                	        		}


                        if (DefinedNodes.count(succ->first) == 0) {
                                (*stream) << (succ->first)->getName() << "[shape="
                                                << (succ->first)->getShape() << ",style="
                                                << (succ->first)->getStyle() << ",label=\""
                                                << label.str() << "\"]\n";
                                DefinedNodes[succ->first] = 1;
                        }

                        //Source
                        (*stream) << "\"" << (*node)->getName() << "\"";

                        (*stream) << "->";

                        //Destination
                        (*stream) << "\"" << (succ->first)->getName() << "\"";

                        if (succ->second == etControl)
                                (*stream) << " [style=dashed]";

                        (*stream) << "\n";

                }

        }

        (*stream) << "}\n\n";

}
Exemplo n.º 2
0
void ComputeSSO::PrintTainted(std::set<GraphNode*> tainted)
{
    //  errs()<<"\n\n Tainted Nodes: "<<tainted.size();


    for(set<GraphNode*>::iterator taintNode = tainted.begin();taintNode != tainted.end();++taintNode)
    {
        //errs()<<"\n Node Label : "<<(*taintNode)->getLabel();
        // errs()<<"--";
        if(isa<MemNode>(*taintNode))
        {
            // errs()<<"\n is mem node";
            //string nodeLab = (*taintNode)->getLabel();
           // string str = "sub_42BC4";
          //  std::size_t found = nodeLab.find(str);
            // if (found!=std::string::npos || 1)
            {

                MemNode * memNew = dyn_cast<MemNode>(*taintNode);
                Value * val = memNew->defLocation.second;
                std::set<Value*> aliases = memNew->getAliases();

                if(val)
                {
                    errs()<<"\n Sink Node Tainted : "<<(*taintNode)->getLabel();
                    if(isa<Instruction>(val))
                    {
                        Instruction * inst = dyn_cast<Instruction>(val);
                        string funcName = inst->getParent()->getParent()->getName();
                        errs()<<"\n Function: "<<funcName;
                    }
                    val->dump();
                }
                if(aliases.size()>0)
                    errs()<<"\n Sink Node Tainted : "<<(*taintNode)->getLabel();
                for(set<Value*>::iterator alVal = aliases.begin(); alVal != aliases.end();++alVal)
                {
                    if(isa<Instruction>(*alVal))
                    {
                        Instruction * inst = dyn_cast<Instruction>(*alVal);
                        string funcName = inst->getParent()->getParent()->getName();
                        errs()<<"\n Function: "<<funcName;
                    }
                    (*alVal)->dump();
                }
            }

        }
        if(isa<VarNode>(*taintNode))
        {
            VarNode * varNew = dyn_cast<VarNode>(*taintNode);
            Value * val = varNew->getValue(); //->defLocation.second;
            if(val)
            {
                errs()<<"\n Sink Node Tainted : "<<(*taintNode)->getLabel();
                if(isa<Instruction>(val))
                {
                    Instruction * inst = dyn_cast<Instruction>(val);
                    string funcName = inst->getParent()->getParent()->getName();
                    errs()<<"\n Function: "<<funcName;
                }
                val->dump();
            }
        }
        //if
    }
}