ROSE  0.11.131.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 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
6 
7 #include <Rose/BinaryAnalysis/CallingConvention.h>
8 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
9 #include <Rose/BinaryAnalysis/StackDelta.h>
10 #include <Rose/SourceLocation.h>
11 
12 #include <Sawyer/Attribute.h>
13 #include <Sawyer/Cached.h>
14 #include <Sawyer/Map.h>
15 #include <Sawyer/Set.h>
16 #include <Sawyer/SharedPointer.h>
17 
18 #include <boost/serialization/access.hpp>
19 #include <boost/serialization/set.hpp>
20 #include <boost/serialization/string.hpp>
21 #include <boost/serialization/vector.hpp>
22 
23 namespace Rose {
24 namespace BinaryAnalysis {
25 namespace Partitioner2 {
26 
29 
42 public:
47  };
48 
50  typedef FunctionPtr Ptr;
51 
52 private:
53  rose_addr_t entryVa_; // entry address; destination for calls to this function
54  std::string name_; // optional function name
55  std::string demangledName_; // optional demangled name
56  std::string comment_; // optional multi-line, plain-text, commment
57  unsigned reasons_; // reason bits from SgAsmFunction::FunctionReason
58  std::string reasonComment_; // additional commentary about reasons_
59  std::set<rose_addr_t> bblockVas_; // addresses of basic blocks
60  std::vector<DataBlockPtr> dblocks_; // data blocks owned by this function, sorted by starting address
61  bool isFrozen_; // true if function is represented by the CFG
62  CallingConvention::Analysis ccAnalysis_; // analysis computing how registers etc. are used
63  CallingConvention::DefinitionPtr ccDefinition_; // best definition or null
64  StackDelta::Analysis stackDeltaAnalysis_; // analysis computing stack deltas for each block and whole function
65  InstructionSemantics::BaseSemantics::SValuePtr stackDeltaOverride_; // special value to override stack delta analysis
66  SourceLocation sourceLocation_; // corresponding location of function in source code if known
67 
68  // The following members are caches either because their value is seldom needed and expensive to compute, or because the
69  // value is best computed at a higher layer (e.g., in the partitioner) yet it makes the most sense to store it here. Make
70  // sure clearCache() resets these to initial values.
71  Sawyer::Cached<bool> isNoop_;
72 
73  void clearCache() {
74  isNoop_.clear();
75  }
76 
77 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
78 private:
79  friend class boost::serialization::access;
80 
81  template<class S>
82  void serialize(S &s, const unsigned version) {
83  //s & boost::serialization::base_object<Sawyer::Attribute::Storage<> >(*this); -- not stored
84  s & BOOST_SERIALIZATION_NVP(entryVa_);
85  s & BOOST_SERIALIZATION_NVP(name_);
86  s & BOOST_SERIALIZATION_NVP(demangledName_);
87  s & BOOST_SERIALIZATION_NVP(comment_);
88  s & BOOST_SERIALIZATION_NVP(reasons_);
89  s & BOOST_SERIALIZATION_NVP(bblockVas_);
90  s & BOOST_SERIALIZATION_NVP(dblocks_);
91  s & BOOST_SERIALIZATION_NVP(isFrozen_);
92  s & BOOST_SERIALIZATION_NVP(ccAnalysis_);
93  s & BOOST_SERIALIZATION_NVP(ccDefinition_);
94  s & BOOST_SERIALIZATION_NVP(stackDeltaAnalysis_);
95  s & BOOST_SERIALIZATION_NVP(stackDeltaOverride_);
96  if (version >= 1)
97  s & BOOST_SERIALIZATION_NVP(reasonComment_);
98  if (version >= 2)
99  s & BOOST_SERIALIZATION_NVP(sourceLocation_);
100  }
101 #endif
102 
103 public:
104  ~Function();
105 
106 protected:
107  // Needed for serialization
108  Function();
109 
110  // Use instance() instead
111  explicit Function(rose_addr_t entryVa, const std::string &name, unsigned reasons);
112 
113 public:
117  static Ptr instance(rose_addr_t entryVa, const std::string &name="", unsigned reasons=0);
118  static Ptr instance(rose_addr_t entryVa, unsigned reasons);
125  rose_addr_t address() const;
126 
132  const std::string& name() const;
133  void name(const std::string&);
142  const std::string& demangledName() const;
143  void demangledName(const std::string&);
151  const std::string& comment() const;
152  void comment(const std::string&);
158  const SourceLocation& sourceLocation() const;
159  void sourceLocation(const SourceLocation&);
165  unsigned reasons() const;
166  void reasons(unsigned);
173  void insertReasons(unsigned reasons);
174 
179  void eraseReasons(unsigned reasons);
185  const std::string& reasonComment() const;
186  void reasonComment(const std::string&);
194  const std::set<rose_addr_t>& basicBlockAddresses() const;
195 
197  bool ownsBasicBlock(rose_addr_t bblockVa) const;
198 
207  bool insertBasicBlock(rose_addr_t bblockVa);
208 
214  void eraseBasicBlock(rose_addr_t bblockVa); // no-op if not existing
215 
218  const std::vector<DataBlockPtr>& dataBlocks() const;
219 
227  bool insertDataBlock(const DataBlockPtr&);
228 
237 
245 
252 
256  bool isFrozen() const;
257 
265  bool isThunk() const;
266 
268  size_t nBasicBlocks() const;
269 
271  size_t nDataBlocks() const;
272 
282  int64_t stackDeltaConcrete() const;
344  std::string printableName() const;
345 
350  const Sawyer::Cached<bool>& isNoop() const;
351 
352 private:
353  friend class Partitioner;
354  void freeze();
355  void thaw();
356 
357  // Find an equivalent data block and replace it with the specified data block, or insert the specified data block
358  void replaceOrInsertDataBlock(const DataBlockPtr&);
359 };
360 
361 } // namespace
362 } // namespace
363 } // namespace
364 
365 // Class versions must be at global scope
366 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Partitioner2::Function, 2);
367 
368 #endif
369 #endif
CallingConvention::DefinitionPtr callingConventionDefinition()
Property: Best calling convention definition.
bool ownsBasicBlock(rose_addr_t bblockVa) const
Predicate to test whether a function owns a basic block address.
Information about a source location.
bool insertDataBlock(const DataBlockPtr &)
Add a data block to this function.
std::string printableName() const
A printable name for the function.
int64_t stackDeltaConcrete() const
Property: Stack delta.
void eraseReasons(unsigned reasons)
Remove function reason bits.
DataBlockPtr dataBlockExists(const DataBlockPtr &) const
Determine if this function contains the specified data block, or equivalent.
InstructionSemantics::BaseSemantics::SValuePtr stackDelta() const
Property: Stack delta.
const std::string & demangledName() const
Property: Optional demangled name.
const std::string & reasonComment() const
Property: Additional comment for why function was detected.
Main namespace for the ROSE library.
static Ptr instance(rose_addr_t entryVa, const std::string &name="", unsigned reasons=0)
Static allocating constructor.
size_t nBasicBlocks() const
Number of basic blocks in the function.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
bool insertBasicBlock(rose_addr_t bblockVa)
Add a basic block to this function.
Sawyer::SharedPointer< Function > FunctionPtr
Shared-ownership pointer for Function.
AddressIntervalSet dataAddresses() const
Addresses that are part of static data.
unsigned reasons() const
Property: Bit vector of function reasons.
const Sawyer::Cached< bool > & isNoop() const
Cached results of function no-op analysis.
const SourceLocation & sourceLocation() const
Property: Location of function definition in source code, if known.
size_t nDataBlocks() const
Number of data blocks in the function.
Function might own the block in the future.
Definition: Function.h:46
void clear() const
Remove cached value.
Definition: Cached.h:69
const std::set< rose_addr_t > & basicBlockAddresses() const
Returns basic block addresses.
Base class for reference counted objects.
Definition: SharedObject.h:64
void insertReasons(unsigned reasons)
Insert additional function reason bits.
const StackDelta::Analysis & stackDeltaAnalysis() const
Property: Stack delta analysis results.
DataBlockPtr eraseDataBlock(const DataBlockPtr &)
Remove specified or equivalent data block from this function.
Ownership
Manner in which a function owns a block.
Definition: Function.h:44
bool isThunk() const
True if function is a thunk.
const std::string & name() const
Property: Optional function name.
API and storage for attributes.
Definition: Attribute.h:208
const std::string & comment() const
Property: Optional function comment.
Sawyer::SharedPointer< DataBlock > DataBlockPtr
Shared-ownership pointer for DataBlock.
rose_addr_t address() const
Read-only property: Entry address.
void eraseBasicBlock(rose_addr_t bblockVa)
Remove a basic block from this function.
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:50
bool isFrozen() const
Determines whether a function is frozen.
InstructionSemantics::BaseSemantics::SValuePtr stackDeltaOverride() const
Property: Stack delta override.
const std::vector< DataBlockPtr > & dataBlocks() const
Returns data blocks owned by this function.
Function owns the block explicitly, the normal ownership.
Definition: Function.h:45
const CallingConvention::Analysis & callingConventionAnalysis() const
Property: Calling convention analysis results.