Predator  [unstable] git snapshot
fixed_point.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Kamil Dudka <kdudka@redhat.com>
3  *
4  * This file is part of predator.
5  *
6  * predator is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * predator is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with predator. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef H_GUARD_FIXED_POINT_H
21 #define H_GUARD_FIXED_POINT_H
22 
23 #include "clean_list.hh"
24 #include "fixed_point_proxy.hh"
25 #include "id_mapper.hh"
26 #include "shape.hh"
27 #include "symstate.hh"
28 
29 #include <climits> // for INT_MIN/INT_MAX
30 #include <utility>
31 
32 namespace CodeStorage {
33  struct Fnc;
34 }
35 
36 namespace FixedPoint {
37 
38 typedef const CodeStorage::Fnc *TFnc;
39 
40 typedef int TLocIdx;
41 typedef int THeapIdx;
42 typedef int TShapeIdx;
43 
44 /// heap (in our case represented by SMG) identity
45 typedef std::pair<TLocIdx, THeapIdx> THeapIdent;
46 
47 /// container shape (in our case represented by Shape) identity
48 typedef std::pair<THeapIdent, TShapeIdx> TShapeIdent;
49 
50 /// fixed heap ID for signalling failures where ID is expected
51 extern const THeapIdent InvalidHeap;
52 
53 /// NOTE: we can omit specifying INT_MIN/INT_MAX when compiling as C++11
56 
57 class GenericInsn {
58  public:
59  virtual ~GenericInsn() { }
60 
61  // NVI to catch missing/incorrect overrides of doClone()
62  GenericInsn* clone() const;
63 
64  /// write human-readable representation of the insn to an output stream
65  virtual void writeToStream(std::ostream &) const = 0;
66 
67  /// return CL representation of the insn if exists; 0 otherwise
68  virtual TInsn clInsn() const = 0;
69 
70  private:
71  /// see Herb Sutter: C++ Coding Standards (rules #39 and #54) for details
72  virtual GenericInsn* doClone() const = 0;
73 };
74 
75 inline std::ostream& operator<<(std::ostream &str, const GenericInsn &insn)
76 {
77  insn.writeToStream(str);
78  return str;
79 }
80 
81 /// single heap-level trace edge holding inner ID mappings inside
82 struct TraceEdge {
83  THeapIdent src; /// source heap
84  THeapIdent dst; /// destination heap
85  TShapeMapper csMap; /// container shapes mapping
86  TObjectMapper objMap; /// heap object IDs mapping
87 
88  TraceEdge(const THeapIdent &src_, const THeapIdent &dst_):
89  src(src_),
90  dst(dst_)
91  {
92  }
93 };
94 
95 typedef std::vector<TraceEdge *> TTraceEdgeList;
96 typedef std::vector<TTraceEdgeList> TEdgeListByHeapIdx;
97 
98 /// single control-flow edge connecting instructions (no basic blocks here)
99 struct CfgEdge {
100  TLocIdx targetLoc; /// the opposite adjacent location
101  bool closesLoop; /// true for loop-closing CFG edge
102 
103  CfgEdge(const TLocIdx targetLoc_, const bool closesLoop_ = false):
104  targetLoc(targetLoc_),
105  closesLoop(closesLoop_)
106  {
107  }
108 };
109 
110 typedef std::vector<CfgEdge> TCfgEdgeList;
111 
112 /// state summary for a single location (preceding a single instruction)
113 struct LocalState {
114  GenericInsn *insn; /// insn using the state as input
115  SymHeapList heapList; /// union of heaps giving the state
116  TShapeListByHeapIdx shapeListByHeapIdx; /// container shapes per heap
117  TCfgEdgeList cfgInEdges; /// ingoing control-flow edges
118  TCfgEdgeList cfgOutEdges; /// outgoing control-flow edges
119  TEdgeListByHeapIdx traceInEdges; /// ingoing heap-level trace edges
120  TEdgeListByHeapIdx traceOutEdges; /// outgoing heap-level trace edges
121 
122  LocalState(): insn(0) { }
123  ~LocalState() { delete insn; }
124  LocalState(const LocalState &);
125  LocalState& operator=(const LocalState &);
126 };
127 
128 /// annotated fixed-point of a program (or its part, e.g. a function)
129 class GlobalState {
130  public:
132 
133  /// return state summary local to the given location
135  return *stateList_[loc];
136  }
137 
138  /// return state summary local to the given location
139  const LocalState& operator[](const TLocIdx loc) const {
140  return *stateList_[loc];
141  }
142 
143  /// return total count of locations maintained by the container
144  TLocIdx size() const {
145  return stateList_.size();
146  }
147 
148  private:
151 
152  private:
153  // intentionally not implemented
154  GlobalState(const GlobalState &);
156 
157  friend GlobalState* computeStateOf(const TFnc,
158  const StateByInsn::TStateMap &);
159 
160  friend void exportControlFlow(GlobalState *pDst,
161  const GlobalState &glState);
162 
163  friend class StateRewriter;
164 };
165 
166 /// return heap of the given state by its identity
167 const SymHeap *heapByIdent(const GlobalState &, THeapIdent);
168 
169 /// return heap of the given state by its identity
171 
172 /// return shape of the given state by its identity
173 const Shape *shapeByIdent(const GlobalState &, const TShapeIdent &);
174 
175 /// caller is responsible to destroy the returned instance
177 
178 /// write the CFG-only skeleton of glState into *pDst
179 void exportControlFlow(GlobalState *pDst, const GlobalState &glState);
180 
181 /// remove instructions assigning values to dead variables
182 void removeDeadCode(GlobalState *pState);
183 
184 /// pretty print the given ID mapping
185 void sl_dump(const TShapeMapper &);
186 
187 /// pretty print the given ID mapping
188 void sl_dump(const TObjectMapper &);
189 
190 } // namespace FixedPoint
191 
192 #endif /* H_GUARD_FIXED_POINT_H */