#ifndef SMPFUNCTION_H #define SMPFUNCTION_H 1 // SMPFunction.h // // This header defines the interfaces needed for analyzing functions, performing live variable analysis, // putting code into SSA form, etc. #include <utility> #include <list> #include <vector> #include <map> #include <set> #include <cstddef> #include <pro.h> #include <ida.hpp> #include <ua.hpp> #include "SMPDataFlowAnalysis.h" #include "SMPInstr.h" #include "SMPBasicBlock.h" using namespace std; // Class encapsulating all that the SMP static analyzer cares to know // about a function. class SMPFunction { public: // Constructors SMPFunction(func_t *Info); // Default constructor // Get methods inline const char *GetFuncName(void) const { return FuncName; }; // Set methods // Query methods inline bool HasIndirectCalls(void) const { return IndirectCalls; }; inline bool HasSharedChunks(void) const { return SharedChunks; }; bool IsGlobalName(op_t RefOp) const { return (GlobalNames.end() != GlobalNames.find(RefOp)); }; // Printing methods void Dump(void); // debug dump // Analysis methods void Analyze(void); // Analyze all instructions in function void EmitAnnotations(FILE *AnnotFile); void RPONumberBlocks(void); void SetLinks(void); // Link basic blocks and map instructions to blocks void LiveVariableAnalysis(void); // Perform Live Variable Analysis across all blocks void ComputeSSA(void); // Compute SSA form data structures private: // Data func_t FuncInfo; char FuncName[MAXSTR]; list<SMPInstr> Instrs; list<SMPBasicBlock> Blocks; map<ea_t, list<SMPBasicBlock>::iterator> InstBlockMap; vector<list<SMPBasicBlock>::iterator> RPOBlocks; vector<int> IDom; // Immediate dominators, indexed and valued by block RPO numbers vector<pair<int, list<int> > > DomTree; // Dominator tree, as parent # and list of children set<op_t, LessOp> GlobalNames; // operands used in more than one block; needed in SSA vector<list<int> > BlocksDefinedIn; // What blocks DEF each GlobalName; index = op # in GlobalNames vector<int> SSACounter; // SSA subscript #, indexed by GlobalNames op # vector<list<int> > SSAStack; // SSA stack of most recent SSA number, indexed by global # int BlockCount; // number of basic blocks in the function bool UseFP; // Does function use a frame pointer? bool StaticFunc; // Is function declared static? bool IndirectCalls; // Does function make indirect calls? bool SharedChunks; // Does function share a tail chunk with other functions? size_t Size; // Function size in code bytes asize_t LocalVarsSize; // size of local vars region of stack frame ushort CalleeSavedRegsSize; // stack size of callee pushed regs int RetAddrSize; // size of return address on stack (4 for most machines) asize_t IncomingArgsSize; // size of incoming args on stack ea_t LocalVarsAllocInstr; // address of instr that allocates stack frame ea_t LocalVarsDeallocInstr; // address of epilogue instr that deallocs frame // Methods void SetStackFrameInfo(void); ea_t FindAllocPoint(asize_t); // Deal with difficult to find stack frame allocations bool MDFixFrameInfo(void); // Redefine stack regions for our needs bool MDFixUseFP(void); // Fix IDA errors affecting UseFP void EmitStackFrameAnnotations(FILE *AnnotFile, list<SMPInstr>::iterator Instr); void ComputeIDoms(void); // Compute immediate dominators of all blocks into IDom[] int IntersectDoms(int, int) const; // Find Dom intersection (as IDom[] index) for 2 blocks void ComputeDomFrontiers(void); // Compute dominance frontiers for all blocks void ComputeGlobalNames(void); // Compute the GlobalNames set void ComputeBlocksDefinedIn(void); // Compute the BlocksDefinedIn vector void InsertPhiFunctions(void); // Insert SSA phi functions at top of each basic block void BuildDominatorTree(void); // Build the DomTree structure int SSANewNumber(size_t GlobNameIndex); // SSA helper: increment and return SSA number void SSARename(int BlockNumber); // SSA man helper: rename throughout block void SSARenumber(void); // Renumber SSA subscripts for all names }; // end class SMPFunction #endif