ROSE  0.11.51.0
Variables.h
1 #ifndef ROSE_BinaryAnalysis_Variables_H
2 #define ROSE_BinaryAnalysis_Variables_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics/Types.h>
7 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics/MemoryCellState.h>
8 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
9 #include <Rose/BinaryAnalysis/SymbolicExpr.h>
10 
11 #include <Sawyer/IntervalMap.h>
12 #include <Sawyer/Message.h>
13 #include <Sawyer/Optional.h>
14 #include <Sawyer/Set.h>
15 
16 #include <boost/serialization/access.hpp>
17 #include <boost/serialization/split_member.hpp>
18 
19 namespace Rose {
20 namespace BinaryAnalysis {
21 
23 namespace Variables {
24 
26 // Basic types
28 
31 
33 typedef std::map<int64_t /*offset*/, AddressSet> OffsetToAddresses;
34 
36 typedef std::map<rose_addr_t /*globalVa*/, AddressSet /*insns*/> AddressToAddresses;
37 
40 
42 // Global variables
44 
47 
49 // Global utilities
51 
53 void initDiagnostics();
54 
56 std::string offsetStr(int64_t offset);
57 
59 std::string sizeStr(uint64_t size);
60 
62 // Base class for variable descriptors
64 
66 class BaseVariable {
67  rose_addr_t maxSizeBytes_; // maximum possible size of this variable in bytes
68  AddressSet insnVas_; // instructions where the variable was detected that reference the variable
70  std::string name_; // optional variable name
71 
72 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
73 private:
74  friend class boost::serialization::access;
75 
76  template<class S>
77  void serialize(S &s, const unsigned /*version*/) {
78  s & BOOST_SERIALIZATION_NVP(maxSizeBytes_);
79  s & BOOST_SERIALIZATION_NVP(insnVas_);
80  s & BOOST_SERIALIZATION_NVP(ioProperties_);
81  s & BOOST_SERIALIZATION_NVP(name_);
82  }
83 #endif
84 
85 protected:
90  : maxSizeBytes_(0) {}
91 
93  BaseVariable(size_t maxSizeBytes, const AddressSet &definingInstructionVas, const std::string &name)
94  // following arithmetic is to work around lack of SSIZE_MAX on windows. The maxSizeBytes should not be more than the
95  // maximum value of the signed type with the same conversion rank.
96  : maxSizeBytes_(std::min(maxSizeBytes, ((size_t)(1) << (8*sizeof(size_t)-1))-1)),
97  insnVas_(definingInstructionVas), name_(name) {}
98 
99 public:
106  rose_addr_t maxSizeBytes() const { return maxSizeBytes_; }
107  void maxSizeBytes(rose_addr_t size);
116  const AddressSet& definingInstructionVas() const { return insnVas_; }
117  AddressSet& definingInstructionVas() { return insnVas_; }
118  void definingInstructionVas(const AddressSet &vas) { insnVas_ = vas; }
137  const std::string& name() const { return name_; }
138  void name(const std::string &s) { name_ = s; }
140 };
141 
143 // Local variable descriptors
145 
148  Partitioner2::FunctionPtr function_; // function in which local variable exists
149  int64_t frameOffset_; // offset where variable is located in the function's stack frame
150 
151 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
152 private:
153  friend class boost::serialization::access;
154 
155  template<class S>
156  void serialize(S &s, const unsigned /*version*/) {
157  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseVariable);
158  s & BOOST_SERIALIZATION_NVP(function_);
159  s & BOOST_SERIALIZATION_NVP(frameOffset_);
160  }
161 #endif
162 
163 public:
167  StackVariable();
168 
171  const AddressSet &definingInstructionVas = AddressSet(), const std::string &name = "");
172 
173  ~StackVariable();
174 
180  Partitioner2::FunctionPtr function() const;
181  void function(const Partitioner2::FunctionPtr&);
191  int64_t frameOffset() const { return frameOffset_; }
192  void frameOffset(int64_t offset) { frameOffset_ = offset; }
198  const std::string& setDefaultName();
199 
206  bool operator==(const StackVariable &other) const;
207  bool operator!=(const StackVariable &other) const;
211  OffsetInterval interval() const;
212 
216  void print(std::ostream&) const;
217  std::string toString() const;
221  friend std::ostream& operator<<(std::ostream&, const Rose::BinaryAnalysis::Variables::StackVariable&);
222 };
223 
226 
231 void print(const StackVariables&, const Partitioner2::Partitioner&, std::ostream &out, const std::string &prefix = "");
232 
234 // Global variable descriptors
236 
239  rose_addr_t address_; // starting (lowest) virtual address
240 
241 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
242 private:
243  friend class boost::serialization::access;
244 
245  template<class S>
246  void serialize(S &s, const unsigned /*version*/) {
247  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseVariable);
248  s & BOOST_SERIALIZATION_NVP(address_);
249  }
250 #endif
251 
252 public:
256  GlobalVariable();
257 
263  GlobalVariable(rose_addr_t startingAddress, rose_addr_t maxSizeBytes,
264  const AddressSet &definingInstructionVas = AddressSet(), const std::string &name = "");
265 
271  rose_addr_t address() const { return address_; }
272  void address(rose_addr_t va) { address_ = va; }
278  const std::string& setDefaultName();
279 
285  bool operator==(const GlobalVariable &other) const;
286  bool operator!=(const GlobalVariable &other) const;
290  AddressInterval interval() const;
291 
295  void print(std::ostream&) const;
296  std::string toString() const;
300  friend std::ostream& operator<<(std::ostream&, const Rose::BinaryAnalysis::Variables::GlobalVariable&);
301 };
302 
309 
314 void print(const GlobalVariables&,const Partitioner2::Partitioner&, std::ostream &out, const std::string &prefix = "");
315 
317 // Analyzer
319 
322 
325 public:
327  struct Settings {};
328 
331 
332 private:
333  Settings settings_;
334 
335 protected:
336  explicit VariableFinder(const Settings&);
337 
338 public:
340  static Ptr instance(const Settings &settings = Settings());
341 
345  const Settings& settings() const { return settings_; }
346  Settings& settings() { return settings_; }
382  GlobalVariables findGlobalVariables(const Partitioner2::Partitioner&);
383 
390  void evict(const Partitioner2::FunctionPtr&);
391  void evict(const Partitioner2::Partitioner&);
398  bool isCached(const Partitioner2::FunctionPtr&);
399 
402 
405 
411  OffsetInterval referencedFrameArea(const Partitioner2::Partitioner&,
413  const SymbolicExpr::Ptr &address, size_t nBytes);
414 
419  std::set<int64_t> findStackOffsets(const Partitioner2::Partitioner&, SgAsmInstruction*);
420 
426 
462  AddressToAddresses findGlobalVariableVas(const Partitioner2::Partitioner&);
463 
467  std::set<rose_addr_t> findConstants(const SymbolicExpr::Ptr&);
468 
470  std::set<rose_addr_t> findConstants(SgAsmInstruction*);
471 
476 
481 
488 };
489 
490 } // namespace
491 } // namespace
492 } // namespace
493 
494 #endif
495 #endif
std::string toString() const
Printing global variable.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
friend std::ostream & operator<<(std::ostream &, const Rose::BinaryAnalysis::Variables::StackVariable &)
Print local variable descriptor.
const Settings & settings() const
Settings for this analysis.
Definition: Variables.h:345
const AddressSet & definingInstructionVas() const
Property: Addresses of instructions related to this variable.
Definition: Variables.h:116
static RegisterDescriptor frameOrStackPointer(const Partitioner2::Partitioner &)
The register typically used as local variable base address.
Description of a global variable.
Definition: Variables.h:238
boost::shared_ptr< class MemoryCellState > MemoryCellStatePtr
Shared-ownership pointer to a cell-based memory state.
Sawyer::Message::Facility mlog
Diagnostic facility.
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1606
AddressInterval interval() const
Location of variable in memory.
Analysis to find variable locations.
Definition: Variables.h:324
Describes a local or global variable.
Definition: Variables.h:66
const std::string & name() const
Property: Optional variable name.
Definition: Variables.h:137
InstructionSemantics2::BaseSemantics::InputOutputPropertySet & ioProperties()
Property: I/O properties.
Definition: Variables.h:127
void address(rose_addr_t va)
Property: Starting address.
Definition: Variables.h:272
STL namespace.
const std::string & setDefaultName()
Give variable a defult name.
void evict(const Partitioner2::FunctionPtr &)
Removed cached information.
AddressToAddresses findGlobalVariableVas(const Partitioner2::Partitioner &)
Find global variable addresses.
friend std::ostream & operator<<(std::ostream &, const Rose::BinaryAnalysis::Variables::GlobalVariable &)
Print global variable descriptor.
bool operator==(const StackVariable &other) const
Compare two local variables.
void name(const std::string &s)
Property: Optional variable name.
Definition: Variables.h:138
void print(const StackVariables &, const Partitioner2::Partitioner &, std::ostream &out, const std::string &prefix="")
Print info about multiple local variables.
void initDiagnostics()
Initialize diagnostic output.
Main namespace for the ROSE library.
std::map< rose_addr_t, AddressSet > AddressToAddresses
Mapping from addresses to address sets.
Definition: Variables.h:36
static Ptr instance(const Settings &settings=Settings())
Allocating constructor.
rose_addr_t address() const
Property: Starting address.
Definition: Variables.h:271
OffsetInterval interval() const
Location within the function stack frame.
void ioProperties(const InstructionSemantics2::BaseSemantics::InputOutputPropertySet &set)
Property: I/O properties.
Definition: Variables.h:128
std::string sizeStr(uint64_t size)
Format size as a string.
void definingInstructionVas(const AddressSet &vas)
Property: Addresses of instructions related to this variable.
Definition: Variables.h:118
void print(std::ostream &) const
Printing global variable.
std::map< int64_t, AddressSet > OffsetToAddresses
Mapping from stack offsets to address sets.
Definition: Variables.h:33
std::set< SymbolicExpr::Ptr > getMemoryAddresses(const InstructionSemantics2::BaseSemantics::MemoryCellStatePtr &)
Find addresses in memory state.
Describes (part of) a physical CPU register.
const InstructionSemantics2::BaseSemantics::InputOutputPropertySet & ioProperties() const
Property: I/O properties.
Definition: Variables.h:126
BaseVariable(size_t maxSizeBytes, const AddressSet &definingInstructionVas, const std::string &name)
Construct a variable with a given maximum size.
Definition: Variables.h:93
bool isCached(const Partitioner2::FunctionPtr &)
Test whether local variable information is cached.
OffsetInterval referencedFrameArea(const Partitioner2::Partitioner &, const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &, const SymbolicExpr::Ptr &address, size_t nBytes)
Find frame location for address.
rose_addr_t maxSizeBytes() const
Property: Maximum variable size in bytes.
Definition: Variables.h:106
std::set< rose_addr_t > findConstants(const SymbolicExpr::Ptr &)
Find address constants in an expression.
std::string offsetStr(int64_t offset)
Format a stack offset as a string.
Settings that control this analysis.
Definition: Variables.h:327
const std::string & setDefaultName()
Give variable a defult name.
bool operator!=(const StackVariable &other) const
Compare two local variables.
InstructionSemantics2::BaseSemantics::SValuePtr symbolicAddress(const Partitioner2::Partitioner &, const StackVariable &, const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &)
Return symbolic address of stack variable.
Sawyer::Container::Set< rose_addr_t > AddressSet
Set of addresses.
Definition: Variables.h:30
Base class for reference counted objects.
Definition: SharedObject.h:64
std::string toString() const
Printing local variable.
Sawyer::Optional< uint64_t > functionFrameSize(const Partitioner2::Partitioner &, const Partitioner2::FunctionPtr &)
Figure out the amount of stack space reserved by this function for local variables.
Range of values delimited by endpoints.
Definition: Interval.h:33
void frameOffset(int64_t offset)
Property: Frame offset.
Definition: Variables.h:192
Sawyer::SharedPointer< class VariableFinder > VariableFinderPtr
Shared ownership pointer for VariableFinder.
Definition: Variables.h:321
int64_t frameOffset() const
Property: Frame offset.
Definition: Variables.h:191
void print(std::ostream &) const
Printing local variable.
AddressSet & definingInstructionVas()
Property: Addresses of instructions related to this variable.
Definition: Variables.h:117
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:290
Sawyer::Container::IntervalMap< AddressInterval, GlobalVariable > GlobalVariables
Maps virtual addresses to global variables.
Definition: Variables.h:308
Partitioner2::FunctionPtr functionForInstruction(const Partitioner2::Partitioner &, SgAsmInstruction *)
Function that owns an instruction.
std::set< rose_addr_t > findAddressConstants(const InstructionSemantics2::BaseSemantics::MemoryCellStatePtr &)
Find constants in memory.
Sawyer::Container::Interval< int64_t > OffsetInterval
Interval of signed offsets.
Definition: Variables.h:39
std::set< int64_t > findStackOffsets(const Partitioner2::Partitioner &, SgAsmInstruction *)
Find stack variable addresses.
StackVariables findStackVariables(const Partitioner2::Partitioner &, const Partitioner2::FunctionPtr &)
Find local variables in a function.
GlobalVariables findGlobalVariables(const Partitioner2::Partitioner &)
Find global variables.
Description of a local stack variable within a function.
Definition: Variables.h:147
Settings & settings()
Settings for this analysis.
Definition: Variables.h:346
bool operator!=(const GlobalVariable &other) const
Compare two global variable descriptors.
bool operator==(const GlobalVariable &other) const
Compare two global variable descriptors.