1. Add timing statistics for parsing
[dyninst.git] / parseAPI / src / JumpTablePred.C
1 #include "dyntypes.h"
2 #include "Node.h"
3 #include "Graph.h"
4
5 #include "debug_parse.h"
6 #include "CodeObject.h"
7 #include "JumpTablePred.h"
8 #include "IndirectASTVisitor.h"
9
10 #include "Instruction.h"
11 #include "InstructionDecoder.h"
12
13 #include "AbslocInterface.h"
14 #include "SymEval.h"
15
16 using namespace Dyninst;
17 using namespace Dyninst::DataflowAPI;
18 using namespace Dyninst::ParseAPI;
19 using namespace Dyninst::InstructionAPI;
20 #define SIGNEX_64_32 0xffffffff00000000LL
21 #define SIGNEX_64_16 0xffffffffffff0000LL
22 #define SIGNEX_64_8  0xffffffffffffff00LL
23 #define SIGNEX_32_16 0xffff0000
24 #define SIGNEX_32_8 0xffffff00
25
26 // Assume the table contain less than this many entries.
27 #define MAX_TABLE_ENTRY 1000000
28 static void BuildEdgesAux(SliceNode::Ptr srcNode,
29                           ParseAPI::Block* curBlock,
30                           map<ParseAPI::Block*, map<AssignmentPtr, SliceNode::Ptr> > &targetMap,
31                           GraphPtr newG,
32                           set<ParseAPI::Block*> &visit,
33                           EdgeTypeEnum t,
34                           set<ParseAPI::Edge*> allowedEdges) {                   
35     if (targetMap.find(curBlock) != targetMap.end()) {
36         // This block contains at least one silce node 
37         // that is reachable from the source DFS node
38         map<AssignmentPtr, SliceNode::Ptr> &candNodes = targetMap[curBlock];
39         Address addr = 0;
40         for (auto cit = candNodes.begin(); cit != candNodes.end(); ++cit)
41             // The node has to be either in a different block from the source node
42             // or in the same block but has a larger address to be considered 
43             // reachable from the source node
44             if (cit->first->addr() > srcNode->addr() || curBlock != srcNode->block())
45                 if (addr == 0 || addr > cit->first->addr()) {
46                     addr = cit->first->addr();
47                 }
48         if (addr != 0) {
49             // There may be several assignments locating 
50             // at the same address. Need to connecting all.
51             if (t == _edgetype_end_) t = FALLTHROUGH;
52             for (auto cit = candNodes.begin(); cit != candNodes.end(); ++cit)
53                 if (cit->first->addr() > srcNode->addr() || curBlock != srcNode->block())
54                     if (addr == cit->first->addr()) {
55                         newG->insertPair(srcNode, cit->second, TypedSliceEdge::create(srcNode, cit->second, t));
56                     }
57             return;
58         }
59     }
60
61     if (visit.find(curBlock) != visit.end()) return;
62     visit.insert(curBlock);
63     for (auto eit = curBlock->targets().begin(); eit != curBlock->targets().end(); ++eit) {
64         // Xiaozhu:
65         // Our current slicing code ignores tail calls 
66         // (the slice code only checks if an edge type is CALL or not)
67         // so, I should be consistent here.
68         // If the slice code considers tail calls, need to change
69         // the predicate to (*eit)->interproc()
70         if ((*eit)->type() != CALL && (*eit)->type() != RET && allowedEdges.find(*eit) != allowedEdges.end()) {
71             EdgeTypeEnum newT = t; 
72             if (t == _edgetype_end_) {
73                 if ((*eit)->type() == COND_TAKEN || (*eit)->type() == COND_NOT_TAKEN) 
74                     newT = (*eit)->type();
75                 else 
76                     newT = FALLTHROUGH;
77             } 
78             BuildEdgesAux(srcNode, (*eit)->trg(), targetMap, newG, visit, newT, allowedEdges);     
79         }
80     }
81 }                         
82
83
84 static void BuildEdges(SliceNode::Ptr curNode,
85                        map<ParseAPI::Block*, map<AssignmentPtr, SliceNode::Ptr> > &targetMap,
86                        GraphPtr newG,
87                        set<ParseAPI::Edge*> &allowedEdges) {
88     set<ParseAPI::Block*> visit;                     
89     BuildEdgesAux(curNode, curNode->block(), targetMap, newG, visit, _edgetype_end_, allowedEdges);
90 }                      
91
92 static bool AssignIsZF(Assignment::Ptr a) {
93     return a->out().absloc().type() == Absloc::Register &&
94            (a->out().absloc().reg() == MachRegister::getZeroFlag(a->out().absloc().reg().getArchitecture()));
95 }
96
97 static bool IsPushAndChangeSP(Assignment::Ptr a) {
98     entryID id = a->insn()->getOperation().getID();
99     if (id != e_push) return false;
100     Absloc aloc = a->out().absloc();
101     if (aloc.type() == Absloc::Register && aloc.reg().isStackPointer()) return true;
102     return false;;
103
104 }
105
106 static int AdjustGraphEntryAndExit(GraphPtr gp) {
107     int nodeCount = 0;
108     NodeIterator gbegin, gend;
109     gp->allNodes(gbegin, gend);
110     for (; gbegin != gend; ++gbegin) {
111         ++nodeCount;
112         Node::Ptr ptr = *gbegin;
113         if (!ptr->hasInEdges()) gp->insertEntryNode(ptr);
114         if (!ptr->hasOutEdges()) gp->insertExitNode(ptr);
115     }
116     return nodeCount;
117 }
118
119 GraphPtr JumpTablePred::BuildAnalysisGraph(set<ParseAPI::Edge*> &visitedEdges) {
120     GraphPtr newG = Graph::createGraph();
121     
122     NodeIterator gbegin, gend;
123     parsing_printf("\t\t start to build analysis graph\n");
124
125     // Create a map to help find whether we have reached a node
126     map<ParseAPI::Block*, map<AssignmentPtr, SliceNode::Ptr> > targetMap;
127
128     // Assignments that are at these addresses have flag assignment colocated
129     set<Address> shouldSkip;
130     for (auto ait = currentAssigns.begin(); ait != currentAssigns.end(); ++ait) {
131         if (AssignIsZF(*ait))
132             shouldSkip.insert((*ait)->addr());
133     }
134     parsing_printf("\t\t calculate skipped nodes\n");
135
136     // We only need one assignment from xchg instruction at each address
137     set<Address> xchgCount;
138     set<Assignment::Ptr> xchgAssign;
139     for (auto ait = currentAssigns.begin(); ait != currentAssigns.end(); ++ait) {
140         if ((*ait)->insn()->getOperation().getID() == e_xchg) {
141             if (xchgCount.find( (*ait)->addr() ) != xchgCount.end() ) continue;
142             xchgCount.insert((*ait)->addr());
143             xchgAssign.insert(*ait);
144         }
145     }
146     parsing_printf("\t\t calculate xchg assignments\n");
147    
148     for (auto ait = currentAssigns.begin(); ait != currentAssigns.end(); ++ait) {
149         Assignment::Ptr a = *ait;
150         if (   (AssignIsZF(a) || shouldSkip.find(a->addr()) == shouldSkip.end()) 
151             && !IsPushAndChangeSP(a)
152             && (!a->insn()->writesMemory() || MatchReadAST(a))) {
153             if (a->insn()->getOperation().getID() == e_xchg && xchgAssign.find(a) == xchgAssign.end()) continue;
154             SliceNode::Ptr newNode = SliceNode::create(a, a->block(), a->func());
155             targetMap[a->block()][a] = newNode;
156             newG->addNode(newNode);
157         }
158     }
159     parsing_printf("\t\t calculate nodes in the new graph\n");
160     // Start from each node to do DFS and build edges
161     newG->allNodes(gbegin, gend);
162     for (; gbegin != gend; ++gbegin) {
163         SliceNode::Ptr node = boost::static_pointer_cast<SliceNode>(*gbegin);
164         BuildEdges(node, targetMap, newG, visitedEdges);
165     }
166     parsing_printf("\t\t calculate edges in the new graph\n");
167
168     // Build a virtual exit node
169     SliceNode::Ptr virtualExit = SliceNode::create(Assignment::Ptr(), NULL, NULL);
170     newG->addNode(virtualExit);
171     newG->allNodes(gbegin, gend);
172     for (; gbegin != gend; ++gbegin) {
173         SliceNode::Ptr cur = boost::static_pointer_cast<SliceNode>(*gbegin);
174         if (!cur->hasOutEdges() && cur != virtualExit) {
175             newG->insertPair(cur, virtualExit, TypedSliceEdge::create(cur, virtualExit, FALLTHROUGH));
176         }
177     }
178     parsing_printf("\t\t calculate virtual nodes in the new graph\n");
179
180     AdjustGraphEntryAndExit(newG);
181
182
183     return newG;
184
185 }
186
187
188 bool JumpTablePred::addNodeCallback(AssignmentPtr ap, set<ParseAPI::Edge*> &visitedEdges) {
189     if (!jumpTableFormat) return false;
190     if (currentAssigns.find(ap) != currentAssigns.end()) return true;
191     if (currentAssigns.size() > 50) return false; 
192     // For flags, we only analyze zf
193     if (ap->out().absloc().type() == Absloc::Register) {
194         MachRegister reg = ap->out().absloc().reg();
195         if (reg.isFlag() && reg != MachRegister::getZeroFlag(reg.getArchitecture())) {
196             return true;
197         }
198     }
199     pair<AST::Ptr, bool> expandRet = ExpandAssignment(ap);
200
201     currentAssigns.insert(ap);
202
203     parsing_printf("Adding assignment %s in instruction %s at %lx, total %d\n", ap->format().c_str(), ap->insn()->format().c_str(), ap->addr(), currentAssigns.size());
204 /*
205     if (ap->insn() && ap->insn()->readsMemory() && firstMemoryRead) {
206         firstMemoryRead = false;
207         parsing_printf("\tThe first memory read, check if format is correct\n");
208         if 
209     }
210 */
211
212     if (!expandRet.second || expandRet.first == NULL) return true;
213
214     // If this assignment writes memory,
215     // we only want to analyze it when it writes to 
216     // an AST we have seen before and potentially
217     // can used for aliasing
218     if (ap->insn()->writesMemory()) {
219         if (!MatchReadAST(ap)) return true;
220     }
221
222     // If this assignment reads memory,
223     // we record the AST of the read so
224     // that in the future we can match a
225     // corresponding write to identify aliasing
226     if (ap->insn()->readsMemory() && expandRet.first->getID() == AST::V_RoseAST) {
227         RoseAST::Ptr roseAST = boost::static_pointer_cast<RoseAST>(expandRet.first);
228         if (roseAST->val().op == ROSEOperation::derefOp) {
229             readAST.push_back(expandRet.first);
230         }
231     }
232     // I really do not want to redo the analysis from scratch,
233     // but it seems like with newly added edges and nodes,
234     // it is necessary to redo.
235
236     // We create the CFG based on the found nodes
237     GraphPtr g = BuildAnalysisGraph(visitedEdges);
238     BoundFactsCalculator bfc(func, g, func->entry() == block, rf, thunks, block->last(), false, expandCache);
239     bfc.CalculateBoundedFacts();
240
241     BoundValue target;
242     bool ijt = IsJumpTable(g, bfc, target);
243     if (ijt) {
244         bool ret = !FillInOutEdges(target, outEdges) || outEdges.empty();
245         // Now we have stopped slicing in advance, so the cache contents are not complete any more.
246         if (!ret) setClearCache(true);
247         return ret;
248     } else {
249         return true;
250     }   
251
252
253
254 }
255 bool JumpTablePred::FillInOutEdges(BoundValue &target, 
256                                                  vector<pair< Address, Dyninst::ParseAPI::EdgeTypeEnum > >& outEdges) {
257     set<Address> jumpTargets;                                            
258     outEdges.clear();
259     Address tableBase = target.interval.low;
260     Address tableLastEntry = target.interval.high;
261     int addressWidth = block->obj()->cs()->getAddressWidth();
262     if (addressWidth == 4) {
263         tableBase &= 0xffffffff;
264         tableLastEntry &= 0xffffffff;
265     }
266
267 #if defined(os_windows)
268     tableBase -= block->obj()->cs()->loadAddress();
269     tableLastEntry -= block->obj()->cs()->loadAddress();
270 #endif
271
272     parsing_printf("The final target bound fact:\n");
273     target.Print();
274     if (!block->obj()->cs()->isCode(tableBase) && !block->obj()->cs()->isData(tableBase)) {
275         parsing_printf("\ttableBase 0x%lx invalid, returning false\n", tableBase);
276         jumpTableFormat = false;
277         parsing_printf("Not jump table format!\n");
278         return false;
279     }
280     if (!block->obj()->cs()->isReadOnly(tableBase)) {
281         parsing_printf("\ttableBase 0x%lx not read only, returning false\n", tableBase);
282         jumpTableFormat = false;
283         parsing_printf("Not jump table format!\n");
284         return false;
285     }
286
287
288     for (Address tableEntry = tableBase; tableEntry <= tableLastEntry; tableEntry += target.interval.stride) {
289         if (!block->obj()->cs()->isCode(tableEntry) && !block->obj()->cs()->isData(tableEntry)) continue;
290         if (!block->obj()->cs()->isReadOnly(tableEntry)) continue;
291         int targetAddress = 0;
292         if (target.tableReadSize > 0) {
293             switch (target.tableReadSize) {
294                 case 8:
295                     targetAddress = *(const uint64_t *) block->obj()->cs()->getPtrToInstruction(tableEntry);
296                     break;
297                 case 4:
298                     targetAddress = *(const uint32_t *) block->obj()->cs()->getPtrToInstruction(tableEntry);
299                     if (target.isZeroExtend) break;
300                     if ((addressWidth == 8) && (targetAddress & 0x80000000)) {
301                         targetAddress |= SIGNEX_64_32;
302                     }
303                     break;
304                 case 2:
305                     targetAddress = *(const uint16_t *) block->obj()->cs()->getPtrToInstruction(tableEntry);
306                     if (target.isZeroExtend) break;
307                     if ((addressWidth == 8) && (targetAddress & 0x8000)) {
308                         targetAddress |= SIGNEX_64_16;
309                     }
310                     if ((addressWidth == 4) && (targetAddress & 0x8000)) {
311                         targetAddress |= SIGNEX_32_16;
312                     }
313
314                     break;
315                 case 1:
316                     targetAddress = *(const uint8_t *) block->obj()->cs()->getPtrToInstruction(tableEntry);
317                     if (target.isZeroExtend) break;
318                     if ((addressWidth == 8) && (targetAddress & 0x80)) {
319                         targetAddress |= SIGNEX_64_8;
320                     }
321                     if ((addressWidth == 4) && (targetAddress & 0x80)) {
322                         targetAddress |= SIGNEX_32_8;
323                     }
324
325                     break;
326
327                 default:
328                     parsing_printf("Invalid memory read size %d\n", target.tableReadSize);
329                     return false;
330             }
331             targetAddress *= target.multiply;
332             if (target.targetBase != 0) {
333                 if (target.isSubReadContent) 
334                     targetAddress = target.targetBase - targetAddress;
335                 else 
336                     targetAddress += target.targetBase; 
337
338             }
339 #if defined(os_windows)
340             targetAddress -= block->obj()->cs()->loadAddress();
341 #endif
342         } else targetAddress = tableEntry;
343
344         if (addressWidth == 4) targetAddress &= 0xffffffff;
345         parsing_printf("Jumping to target %lx,", targetAddress);
346         if (block->obj()->cs()->isCode(targetAddress)) {
347             // Jump tables may contain may repetitious entries.
348             // We only want to create one edge for disctinct each jump target.
349             jumpTargets.insert(targetAddress);
350             parsing_printf(" is code.\n" );
351         } else {
352             parsing_printf(" not code.\n");
353         }
354         // If the jump target is resolved to be a constant, 
355         if (target.interval.stride == 0) break;
356     }
357     for (auto tit = jumpTargets.begin(); tit != jumpTargets.end(); ++tit) {
358         outEdges.push_back(make_pair(*tit, INDIRECT));
359     }
360     return true;
361 }
362 bool JumpTablePred::IsJumpTable(GraphPtr slice, 
363                                               BoundFactsCalculator &bfc,
364                                               BoundValue &target) {
365     NodeIterator exitBegin, exitEnd, srcBegin, srcEnd;
366     slice->exitNodes(exitBegin, exitEnd);
367     SliceNode::Ptr virtualExit = boost::static_pointer_cast<SliceNode>(*exitBegin);
368     virtualExit->ins(srcBegin, srcEnd);
369     SliceNode::Ptr jumpNode = boost::static_pointer_cast<SliceNode>(*srcBegin);
370     
371     const Absloc &loc = jumpNode->assign()->out().absloc();
372     parsing_printf("Checking final bound fact for %s\n",loc.format().c_str()); 
373     BoundFact *bf = bfc.GetBoundFactOut(virtualExit);
374     VariableAST::Ptr ip = VariableAST::create(Variable(loc));
375     BoundValue *tarBoundValue = bf->GetBound(ip);
376     if (tarBoundValue != NULL) {
377         target = *(tarBoundValue);
378         uint64_t s = target.interval.size();
379         if (s > 0 && s <= MAX_TABLE_ENTRY) return true;
380     }
381     AST::Ptr ipExp = bf->GetAlias(ip);
382     if (ipExp) {
383         parsing_printf("\t jump target expression %s\n", ipExp->format().c_str());
384         JumpTableFormatVisitor jtfv(block);
385         ipExp->accept(&jtfv);
386         if (!jtfv.format) {
387             parsing_printf("\t Not jump table format!\n");
388             jumpTableFormat = false;
389         }
390     }
391
392     return false;
393 }
394
395 bool JumpTablePred::MatchReadAST(Assignment::Ptr a) {
396     pair<AST::Ptr, bool> expandRet = ExpandAssignment(a);
397     if (!expandRet.second || expandRet.first == NULL) return false;
398     if (a->out().generator() == NULL) return false;
399     AST::Ptr write = SimplifyAnAST(RoseAST::create(ROSEOperation(ROSEOperation::derefOp, a->out().size()), a->out().generator()), 
400                                    PCValue(a->addr(),
401                                            a->insn()->size(),
402                                            a->block()->obj()->cs()->getArch()));
403
404     if (write == NULL) return false;
405     for (auto ait = readAST.begin(); ait != readAST.end(); ++ait) 
406         if (*write == **ait) return true;
407     return false;
408 }
409
410 pair<AST::Ptr, bool> JumpTablePred::ExpandAssignment(Assignment::Ptr assign) {
411     if (expandCache.find(assign) != expandCache.end()) {
412         AST::Ptr ast = expandCache[assign];
413         if (ast) return make_pair(ast, true); else return make_pair(ast, false);
414
415     } else {
416                 parsing_printf("\t\tExpanding instruction @ %x: %s\n", assign->addr(), assign->insn()->format().c_str());
417         pair<AST::Ptr, bool> expandRet = SymEval::expand(assign, false);
418         if (expandRet.second && expandRet.first) {
419 parsing_printf("Original expand: %s\n", expandRet.first->format().c_str());
420
421             AST::Ptr calculation = SimplifyAnAST(expandRet.first, 
422                                                  PCValue(assign->addr(),
423                                                          assign->insn()->size(),
424                                                          assign->block()->obj()->cs()->getArch()));
425             expandCache[assign] = calculation;
426         } else {
427             expandCache[assign] = AST::Ptr();
428         }
429         return make_pair( expandCache[assign], expandRet.second );
430     }
431 }
432