ROSE  0.11.2.0
Function.h
1 #ifndef ROSE_Partitioner2_Function_H
2 #define ROSE_Partitioner2_Function_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <BaseSemantics2.h>
8 #include <BinaryCallingConvention.h>
9 #include <BinaryStackDelta.h>
10 #include <Partitioner2/BasicTypes.h>
11 #include <Partitioner2/DataBlock.h>
12 #include <SourceLocation.h>
13 
14 #include <Sawyer/Attribute.h>
15 #include <Sawyer/Cached.h>
16 #include <Sawyer/Map.h>
17 #include <Sawyer/Set.h>
18 #include <Sawyer/SharedPointer.h>
19 
20 #include <boost/serialization/access.hpp>
21 #include <boost/serialization/set.hpp>
22 #include <boost/serialization/string.hpp>
23 #include <boost/serialization/vector.hpp>
24 
25 namespace Rose {
26 namespace BinaryAnalysis {
27 namespace Partitioner2 {
28 
31 
44 public:
49  };
50 
52  typedef FunctionPtr Ptr;
53 
54 private:
55  rose_addr_t entryVa_; // entry address; destination for calls to this function
56  std::string name_; // optional function name
57  std::string demangledName_; // optional demangled name
58  std::string comment_; // optional multi-line, plain-text, commment
59  unsigned reasons_; // reason bits from SgAsmFunction::FunctionReason
60  std::string reasonComment_; // additional commentary about reasons_
61  std::set<rose_addr_t> bblockVas_; // addresses of basic blocks
62  std::vector<DataBlock::Ptr> dblocks_; // data blocks owned by this function, sorted by starting address
63  bool isFrozen_; // true if function is represented by the CFG
64  CallingConvention::Analysis ccAnalysis_; // analysis computing how registers etc. are used
65  CallingConvention::Definition::Ptr ccDefinition_; // best definition or null
66  StackDelta::Analysis stackDeltaAnalysis_; // analysis computing stack deltas for each block and whole function
67  InstructionSemantics2::BaseSemantics::SValuePtr stackDeltaOverride_; // special value to override stack delta analysis
68  SourceLocation sourceLocation_; // corresponding location of function in source code if known
69 
70  // The following members are caches either because their value is seldom needed and expensive to compute, or because the
71  // value is best computed at a higher layer (e.g., in the partitioner) yet it makes the most sense to store it here. Make
72  // sure clearCache() resets these to initial values.
73  Sawyer::Cached<bool> isNoop_;
74 
75  void clearCache() {
76  isNoop_.clear();
77  }
78 
79 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
80 private:
81  friend class boost::serialization::access;
82 
83  template<class S>
84  void serialize(S &s, const unsigned version) {
85  //s & boost::serialization::base_object<Sawyer::Attribute::Storage<> >(*this); -- not stored
86  s & BOOST_SERIALIZATION_NVP(entryVa_);
87  s & BOOST_SERIALIZATION_NVP(name_);
88  s & BOOST_SERIALIZATION_NVP(demangledName_);
89  s & BOOST_SERIALIZATION_NVP(comment_);
90  s & BOOST_SERIALIZATION_NVP(reasons_);
91  s & BOOST_SERIALIZATION_NVP(bblockVas_);
92  s & BOOST_SERIALIZATION_NVP(dblocks_);
93  s & BOOST_SERIALIZATION_NVP(isFrozen_);
94  s & BOOST_SERIALIZATION_NVP(ccAnalysis_);
95  s & BOOST_SERIALIZATION_NVP(ccDefinition_);
96  s & BOOST_SERIALIZATION_NVP(stackDeltaAnalysis_);
97  s & BOOST_SERIALIZATION_NVP(stackDeltaOverride_);
98  if (version >= 1)
99  s & BOOST_SERIALIZATION_NVP(reasonComment_);
100  if (version >= 2)
101  s & BOOST_SERIALIZATION_NVP(sourceLocation_);
102  }
103 #endif
104 
105 protected:
106  // Needed for serialization
107  Function()
108  : entryVa_(0), reasons_(0), isFrozen_(false) {}
109 
110  // Use instance() instead
111  explicit Function(rose_addr_t entryVa, const std::string &name, unsigned reasons)
112  : entryVa_(entryVa), name_(name), reasons_(reasons), isFrozen_(false) {
113  bblockVas_.insert(entryVa);
114  }
115 
116 public:
120  static Ptr instance(rose_addr_t entryVa, const std::string &name="", unsigned reasons=0) {
121  return Ptr(new Function(entryVa, name, reasons));
122  }
123  static Ptr instance(rose_addr_t entryVa, unsigned reasons) {
124  return Ptr(new Function(entryVa, "", reasons));
125  }
132  rose_addr_t address() const { return entryVa_; }
133 
139  const std::string& name() const { return name_; }
140  void name(const std::string &name) { name_ = name; }
149  const std::string& demangledName() const;
150  void demangledName(const std::string &name) { demangledName_ = name; }
158  const std::string& comment() const { return comment_; }
159  void comment(const std::string &s) { comment_ = s; }
165  const SourceLocation& sourceLocation() const { return sourceLocation_; }
166  void sourceLocation(const SourceLocation &loc) { sourceLocation_ = loc; }
172  unsigned reasons() const { return reasons_; }
173  void reasons(unsigned reasons) { reasons_ = reasons; }
180  void insertReasons(unsigned reasons) { reasons_ = (reasons_ & 0xffff0000) | reasons; }
181 
186  void eraseReasons(unsigned reasons) { reasons_ &= ~((0xffff0000 & reasons) | ((reasons & 0xffff) != 0 ? 0xffff : 0x0)); }
192  const std::string& reasonComment() const { return reasonComment_; }
193  void reasonComment(const std::string &s) { reasonComment_ = s; }
201  const std::set<rose_addr_t>& basicBlockAddresses() const { return bblockVas_; }
202 
204  bool ownsBasicBlock(rose_addr_t bblockVa) const {
205  return bblockVas_.find(bblockVa) != bblockVas_.end();
206  }
207 
216  bool insertBasicBlock(rose_addr_t bblockVa) {
217  ASSERT_forbid(isFrozen_);
218  bool wasInserted = bblockVas_.insert(bblockVa).second;
219  if (wasInserted)
220  clearCache();
221  return wasInserted;
222  }
223 
229  void eraseBasicBlock(rose_addr_t bblockVa) { // no-op if not existing
230  ASSERT_forbid(isFrozen_);
231  ASSERT_forbid2(bblockVa==entryVa_, "function entry block cannot be removed");
232  clearCache();
233  bblockVas_.erase(bblockVa);
234  }
235 
238  const std::vector<DataBlock::Ptr>& dataBlocks() const { return dblocks_; }
239 
247  bool insertDataBlock(const DataBlock::Ptr&);
248 
257 
265 
272 
276  bool isFrozen() const { return isFrozen_; }
277 
285  bool isThunk() const;
286 
288  size_t nBasicBlocks() const { return bblockVas_.size(); }
289 
291  size_t nDataBlocks() const { return dblocks_.size(); }
292 
302  int64_t stackDeltaConcrete() const;
325  const StackDelta::Analysis& stackDeltaAnalysis() const { return stackDeltaAnalysis_; }
326  StackDelta::Analysis& stackDeltaAnalysis() { return stackDeltaAnalysis_; }
343  const CallingConvention::Analysis& callingConventionAnalysis() const { return ccAnalysis_; }
357  void callingConventionDefinition(const CallingConvention::Definition::Ptr &ccdef) { ccDefinition_ = ccdef; }
364  std::string printableName() const;
365 
370  const Sawyer::Cached<bool>& isNoop() const { return isNoop_; }
371 
372 private:
373  friend class Partitioner;
374  void freeze() { isFrozen_ = true; }
375  void thaw() { isFrozen_ = false; }
376 
377  // Find an equivalent data block and replace it with the specified data block, or insert the specified data block
378  void replaceOrInsertDataBlock(const DataBlock::Ptr&);
379 };
380 
382 typedef Sawyer::Container::Set<Function::Ptr> FunctionSet;
383 
384 } // namespace
385 } // namespace
386 } // namespace
387 
388 // Class versions must be at global scope
389 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Partitioner2::Function, 2);
390 
391 #endif
392 #endif
void name(const std::string &name)
Property: Optional function name.
Definition: Function.h:140
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:204
Information about a source location.
void sourceLocation(const SourceLocation &loc)
Property: Location of function definition in source code, if known.
Definition: Function.h:166
void callingConventionDefinition(const CallingConvention::Definition::Ptr &ccdef)
Property: Best calling convention definition.
Definition: Function.h:357
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:356
CallingConvention::Analysis & callingConventionAnalysis()
Property: Calling convention analysis results.
Definition: Function.h:344
void eraseReasons(unsigned reasons)
Remove function reason bits.
Definition: Function.h:186
Sawyer::SharedPointer< DataBlock > Ptr
Shared pointer to a data block.
Definition: DataBlock.h:35
void reasons(unsigned reasons)
Property: Bit vector of function reasons.
Definition: Function.h:173
StackDelta::Analysis & stackDeltaAnalysis()
Property: Stack delta analysis results.
Definition: Function.h:326
const std::string & demangledName() const
Property: Optional demangled name.
const std::string & reasonComment() const
Property: Additional comment for why function was detected.
Definition: Function.h:192
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:120
void demangledName(const std::string &name)
Property: Optional demangled name.
Definition: Function.h:150
size_t nBasicBlocks() const
Number of basic blocks in the function.
Definition: Function.h:288
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
bool insertBasicBlock(rose_addr_t bblockVa)
Add a basic block to this function.
Definition: Function.h:216
Sawyer::SharedPointer< Function > FunctionPtr
Shared-ownership pointer for function.
Definition: BasicTypes.h:492
void reasonComment(const std::string &s)
Property: Additional comment for why function was detected.
Definition: Function.h:193
void comment(const std::string &s)
Property: Optional function comment.
Definition: Function.h:159
AddressIntervalSet dataAddresses() const
Addresses that are part of static data.
unsigned reasons() const
Property: Bit vector of function reasons.
Definition: Function.h:172
const Sawyer::Cached< bool > & isNoop() const
Cached results of function no-op analysis.
Definition: Function.h:370
const std::vector< DataBlock::Ptr > & dataBlocks() const
Returns data blocks owned by this function.
Definition: Function.h:238
const SourceLocation & sourceLocation() const
Property: Location of function definition in source code, if known.
Definition: Function.h:165
DataBlock::Ptr dataBlockExists(const DataBlock::Ptr &) const
Determine if this function contains the specified data block, or equivalent.
size_t nDataBlocks() const
Number of data blocks in the function.
Definition: Function.h:291
Function might own the block in the future.
Definition: Function.h:48
void clear() const
Remove cached value.
Definition: Cached.h:69
DataBlock::Ptr eraseDataBlock(const DataBlock::Ptr &)
Remove specified or equivalent data block from this function.
const std::set< rose_addr_t > & basicBlockAddresses() const
Returns basic block addresses.
Definition: Function.h:201
Base class for reference counted objects.
Definition: SharedObject.h:64
void insertReasons(unsigned reasons)
Insert additional function reason bits.
Definition: Function.h:180
const StackDelta::Analysis & stackDeltaAnalysis() const
Property: Stack delta analysis results.
Definition: Function.h:325
InstructionSemantics2::BaseSemantics::SValuePtr stackDelta() const
Property: Stack delta.
Ownership
Manner in which a function owns a block.
Definition: Function.h:46
InstructionSemantics2::BaseSemantics::SValuePtr stackDeltaOverride() const
Property: Stack delta override.
bool isThunk() const
True if function is a thunk.
const std::string & name() const
Property: Optional function name.
Definition: Function.h:139
API and storage for attributes.
Definition: Attribute.h:208
const std::string & comment() const
Property: Optional function comment.
Definition: Function.h:158
static Ptr instance(rose_addr_t entryVa, unsigned reasons)
Static allocating constructor.
Definition: Function.h:123
rose_addr_t address() const
Read-only property: Entry address.
Definition: Function.h:132
void eraseBasicBlock(rose_addr_t bblockVa)
Remove a basic block from this function.
Definition: Function.h:229
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:322
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:52
bool isFrozen() const
Determines whether a function is frozen.
Definition: Function.h:276
Function owns the block explicitly, the normal ownership.
Definition: Function.h:47
const CallingConvention::Analysis & callingConventionAnalysis() const
Property: Calling convention analysis results.
Definition: Function.h:343