ROSE  0.9.9.109
Function.h
1 #ifndef ROSE_Partitioner2_Function_H
2 #define ROSE_Partitioner2_Function_H
3 
4 #include <BaseSemantics2.h>
5 #include <BinaryCallingConvention.h>
6 #include <BinaryStackDelta.h>
7 #include <Partitioner2/BasicTypes.h>
8 #include <Partitioner2/DataBlock.h>
9 
10 #include <Sawyer/Attribute.h>
11 #include <Sawyer/Cached.h>
12 #include <Sawyer/Map.h>
13 #include <Sawyer/Set.h>
14 #include <Sawyer/SharedPointer.h>
15 
16 #include <boost/serialization/access.hpp>
17 #include <boost/serialization/set.hpp>
18 #include <boost/serialization/string.hpp>
19 #include <boost/serialization/vector.hpp>
20 
21 namespace Rose {
22 namespace BinaryAnalysis {
23 namespace Partitioner2 {
24 
27 
40 public:
45  };
46 
48  typedef FunctionPtr Ptr;
49 
50 private:
51  rose_addr_t entryVa_; // entry address; destination for calls to this function
52  std::string name_; // optional function name
53  std::string demangledName_; // optional demangled name
54  std::string comment_; // optional multi-line, plain-text, commment
55  unsigned reasons_; // reason bits from SgAsmFunction::FunctionReason
56  std::set<rose_addr_t> bblockVas_; // addresses of basic blocks
57  std::vector<DataBlock::Ptr> dblocks_; // data blocks owned by this function, sorted by starting address
58  bool isFrozen_; // true if function is represented by the CFG
59  CallingConvention::Analysis ccAnalysis_; // analysis computing how registers etc. are used
60  CallingConvention::Definition::Ptr ccDefinition_; // best definition or null
61  StackDelta::Analysis stackDeltaAnalysis_; // analysis computing stack deltas for each block and whole function
62  InstructionSemantics2::BaseSemantics::SValuePtr stackDeltaOverride_; // special value to override stack delta analysis
63 
64  // The following members are caches either because their value is seldom needed and expensive to compute, or because the
65  // value is best computed at a higher layer (e.g., in the partitioner) yet it makes the most sense to store it here. Make
66  // sure clearCache() resets these to initial values.
67  Sawyer::Cached<bool> isNoop_;
68 
69  void clearCache() {
70  isNoop_.clear();
71  }
72 
73 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
74 private:
75  friend class boost::serialization::access;
76 
77  template<class S>
78  void serialize(S &s, const unsigned version) {
79  //s & boost::serialization::base_object<Sawyer::Attribute::Storage<> >(*this); -- not stored
80  s & BOOST_SERIALIZATION_NVP(entryVa_);
81  s & BOOST_SERIALIZATION_NVP(name_);
82  s & BOOST_SERIALIZATION_NVP(demangledName_);
83  s & BOOST_SERIALIZATION_NVP(comment_);
84  s & BOOST_SERIALIZATION_NVP(reasons_);
85  s & BOOST_SERIALIZATION_NVP(bblockVas_);
86  s & BOOST_SERIALIZATION_NVP(dblocks_);
87  s & BOOST_SERIALIZATION_NVP(isFrozen_);
88  s & BOOST_SERIALIZATION_NVP(ccAnalysis_);
89  s & BOOST_SERIALIZATION_NVP(ccDefinition_);
90  s & BOOST_SERIALIZATION_NVP(stackDeltaAnalysis_);
91  s & BOOST_SERIALIZATION_NVP(stackDeltaOverride_);
92  }
93 #endif
94 
95 protected:
96  // Needed for serialization
97  Function()
98  : entryVa_(0), reasons_(0), isFrozen_(false) {}
99 
100  // Use instance() instead
101  explicit Function(rose_addr_t entryVa, const std::string &name, unsigned reasons)
102  : entryVa_(entryVa), name_(name), reasons_(reasons), isFrozen_(false) {
103  bblockVas_.insert(entryVa);
104  }
105 
106 public:
110  static Ptr instance(rose_addr_t entryVa, const std::string &name="", unsigned reasons=0) {
111  return Ptr(new Function(entryVa, name, reasons));
112  }
113  static Ptr instance(rose_addr_t entryVa, unsigned reasons) {
114  return Ptr(new Function(entryVa, "", reasons));
115  }
120  rose_addr_t address() const { return entryVa_; }
121 
127  const std::string& name() const { return name_; }
128  void name(const std::string &name) { name_ = name; }
137  const std::string& demangledName() const;
138  void demangledName(const std::string &name) { demangledName_ = name; }
146  const std::string& comment() const { return comment_; }
147  void comment(const std::string &s) { comment_ = s; }
153  unsigned reasons() const { return reasons_; }
154  void reasons(unsigned reasons) { reasons_ = reasons; }
155  void insertReasons(unsigned reasons) { reasons_ |= reasons; }
156  void eraseReasons(unsigned reasons) { reasons_ &= ~reasons; }
164  const std::set<rose_addr_t>& basicBlockAddresses() const { return bblockVas_; }
165 
167  bool ownsBasicBlock(rose_addr_t bblockVa) const {
168  return bblockVas_.find(bblockVa) != bblockVas_.end();
169  }
170 
179  bool insertBasicBlock(rose_addr_t bblockVa) {
180  ASSERT_forbid(isFrozen_);
181  bool wasInserted = bblockVas_.insert(bblockVa).second;
182  if (wasInserted)
183  clearCache();
184  return wasInserted;
185  }
186 
192  void eraseBasicBlock(rose_addr_t bblockVa) { // no-op if not existing
193  ASSERT_forbid(isFrozen_);
194  ASSERT_forbid2(bblockVa==entryVa_, "function entry block cannot be removed");
195  clearCache();
196  bblockVas_.erase(bblockVa);
197  }
198 
201  const std::vector<DataBlock::Ptr>& dataBlocks() const { return dblocks_; }
202 
210  bool insertDataBlock(const DataBlock::Ptr&);
211 
216  void eraseDataBlock(const DataBlock::Ptr&);
217 
221  bool isFrozen() const { return isFrozen_; }
222 
230  bool isThunk() const;
231 
233  size_t nBasicBlocks() const { return bblockVas_.size(); }
234 
244  int64_t stackDeltaConcrete() const;
267  const StackDelta::Analysis& stackDeltaAnalysis() const { return stackDeltaAnalysis_; }
268  StackDelta::Analysis& stackDeltaAnalysis() { return stackDeltaAnalysis_; }
285  const CallingConvention::Analysis& callingConventionAnalysis() const { return ccAnalysis_; }
299  void callingConventionDefinition(const CallingConvention::Definition::Ptr &ccdef) { ccDefinition_ = ccdef; }
306  std::string printableName() const;
307 
312  const Sawyer::Cached<bool>& isNoop() const { return isNoop_; }
313 
314 private:
315  friend class Partitioner;
316  void freeze() { isFrozen_ = true; }
317  void thaw() { isFrozen_ = false; }
318 };
319 
321 typedef Sawyer::Container::Set<Function::Ptr> FunctionSet;
322 
323 } // namespace
324 } // namespace
325 } // namespace
326 
327 #endif
void name(const std::string &name)
Optional function name.
Definition: Function.h:128
bool insertDataBlock(const DataBlock::Ptr &)
Add a data block to this function.
bool ownsBasicBlock(rose_addr_t bblockVa) const
Predicate to test whether a function owns a basic block address.
Definition: Function.h:167
void callingConventionDefinition(const CallingConvention::Definition::Ptr &ccdef)
Property: Best calling convention definition.
Definition: Function.h:299
std::string printableName() const
A printable name for the function.
int64_t stackDeltaConcrete() const
Property: Stack delta.
CallingConvention::Definition::Ptr callingConventionDefinition()
Property: Best calling convention definition.
Definition: Function.h:298
CallingConvention::Analysis & callingConventionAnalysis()
Property: Calling convention analysis results.
Definition: Function.h:286
void eraseReasons(unsigned reasons)
Function reasons.
Definition: Function.h:156
void reasons(unsigned reasons)
Function reasons.
Definition: Function.h:154
StackDelta::Analysis & stackDeltaAnalysis()
Property: Stack delta analysis results.
Definition: Function.h:268
const std::string & demangledName() const
Optional demangled name.
Main namespace for the ROSE library.
static Ptr instance(rose_addr_t entryVa, const std::string &name="", unsigned reasons=0)
Static allocating constructor.
Definition: Function.h:110
void demangledName(const std::string &name)
Optional demangled name.
Definition: Function.h:138
size_t nBasicBlocks() const
Number of basic blocks in the function.
Definition: Function.h:233
Reference-counting smart pointer.
Definition: SharedPointer.h:34
bool insertBasicBlock(rose_addr_t bblockVa)
Add a basic block to this function.
Definition: Function.h:179
Sawyer::SharedPointer< Function > FunctionPtr
Shared-ownership pointer for function.
Definition: BasicTypes.h:372
void comment(const std::string &s)
Optional function comment.
Definition: Function.h:147
unsigned reasons() const
Function reasons.
Definition: Function.h:153
const Sawyer::Cached< bool > & isNoop() const
Cached results of function no-op analysis.
Definition: Function.h:312
const std::vector< DataBlock::Ptr > & dataBlocks() const
Returns data blocks owned by this function.
Definition: Function.h:201
void eraseDataBlock(const DataBlock::Ptr &)
Remove a data block from this function.
Function might own the block in the future.
Definition: Function.h:44
void clear() const
Remove cached value.
Definition: Cached.h:69
const std::set< rose_addr_t > & basicBlockAddresses() const
Returns basic block addresses.
Definition: Function.h:164
Base class for reference counted objects.
Definition: SharedObject.h:22
void insertReasons(unsigned reasons)
Function reasons.
Definition: Function.h:155
const StackDelta::Analysis & stackDeltaAnalysis() const
Property: Stack delta analysis results.
Definition: Function.h:267
InstructionSemantics2::BaseSemantics::SValuePtr stackDelta() const
Property: Stack delta.
Ownership
Manner in which a function owns a block.
Definition: Function.h:42
InstructionSemantics2::BaseSemantics::SValuePtr stackDeltaOverride() const
Property: Stack delta override.
bool isThunk() const
True if function is a thunk.
const std::string & name() const
Optional function name.
Definition: Function.h:127
API and storage for attributes.
Definition: Attribute.h:208
const std::string & comment() const
Optional function comment.
Definition: Function.h:146
static Ptr instance(rose_addr_t entryVa, unsigned reasons)
Static allocating constructor.
Definition: Function.h:113
rose_addr_t address() const
Return the entry address.
Definition: Function.h:120
void eraseBasicBlock(rose_addr_t bblockVa)
Remove a basic block from this function.
Definition: Function.h:192
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:289
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:48
bool isFrozen() const
Determines whether a function is frozen.
Definition: Function.h:221
Function owns the block explicitly, the normal ownership.
Definition: Function.h:43
const CallingConvention::Analysis & callingConventionAnalysis() const
Property: Calling convention analysis results.
Definition: Function.h:285