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