ROSE  0.11.31.0
BinaryVariables.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 <BaseSemanticsTypes.h>
7 #include <boost/serialization/access.hpp>
8 #include <boost/serialization/split_member.hpp>
9 #include <MemoryCellState.h>
10 #include <Partitioner2/BasicTypes.h>
11 #include <Sawyer/IntervalMap.h>
12 #include <Sawyer/Message.h>
13 #include <Sawyer/Optional.h>
14 #include <Sawyer/Set.h>
15 
16 namespace Rose {
17 namespace BinaryAnalysis {
18 
20 namespace Variables {
21 
23 // Basic types
25 
28 
30 typedef std::map<int64_t /*offset*/, AddressSet> OffsetToAddresses;
31 
33 typedef std::map<rose_addr_t /*globalVa*/, AddressSet /*insns*/> AddressToAddresses;
34 
37 
39 // Global variables
41 
44 
46 // Global utilities
48 
50 void initDiagnostics();
51 
53 std::string offsetStr(int64_t offset);
54 
56 std::string sizeStr(uint64_t size);
57 
59 // Base class for variable descriptors
61 
63 class BaseVariable {
64  rose_addr_t maxSizeBytes_; // maximum possible size of this variable in bytes
65  AddressSet insnVas_; // instructions where the variable was detected that reference the variable
67  std::string name_; // optional variable name
68 
69 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
70 private:
71  friend class boost::serialization::access;
72 
73  template<class S>
74  void serialize(S &s, const unsigned /*version*/) {
75  s & BOOST_SERIALIZATION_NVP(maxSizeBytes_);
76  s & BOOST_SERIALIZATION_NVP(insnVas_);
77  s & BOOST_SERIALIZATION_NVP(ioProperties_);
78  s & BOOST_SERIALIZATION_NVP(name_);
79  }
80 #endif
81 
82 protected:
87  : maxSizeBytes_(0) {}
88 
90  BaseVariable(size_t maxSizeBytes, const AddressSet &definingInstructionVas, const std::string &name)
91  // following arithmetic is to work around lack of SSIZE_MAX on windows. The maxSizeBytes should not be more than the
92  // maximum value of the signed type with the same conversion rank.
93  : maxSizeBytes_(std::min(maxSizeBytes, ((size_t)(1) << (8*sizeof(size_t)-1))-1)),
94  insnVas_(definingInstructionVas), name_(name) {}
95 
96 public:
103  rose_addr_t maxSizeBytes() const { return maxSizeBytes_; }
104  void maxSizeBytes(rose_addr_t size);
113  const AddressSet& definingInstructionVas() const { return insnVas_; }
114  AddressSet& definingInstructionVas() { return insnVas_; }
115  void definingInstructionVas(const AddressSet &vas) { insnVas_ = vas; }
134  const std::string& name() const { return name_; }
135  void name(const std::string &s) { name_ = s; }
137 };
138 
140 // Local variable descriptors
142 
145  Partitioner2::FunctionPtr function_; // function in which local variable exists
146  int64_t frameOffset_; // offset where variable is located in the function's stack frame
147 
148 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
149 private:
150  friend class boost::serialization::access;
151 
152  template<class S>
153  void serialize(S &s, const unsigned /*version*/) {
154  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseVariable);
155  s & BOOST_SERIALIZATION_NVP(function_);
156  s & BOOST_SERIALIZATION_NVP(frameOffset_);
157  }
158 #endif
159 
160 public:
164  StackVariable();
165 
168  const AddressSet &definingInstructionVas = AddressSet(), const std::string &name = "");
169 
170  ~StackVariable();
171 
177  Partitioner2::FunctionPtr function() const;
178  void function(const Partitioner2::FunctionPtr&);
188  int64_t frameOffset() const { return frameOffset_; }
189  void frameOffset(int64_t offset) { frameOffset_ = offset; }
195  const std::string& setDefaultName();
196 
203  bool operator==(const StackVariable &other) const;
204  bool operator!=(const StackVariable &other) const;
208  OffsetInterval interval() const;
209 
213  void print(std::ostream&) const;
214  std::string toString() const;
218  friend std::ostream& operator<<(std::ostream&, const Rose::BinaryAnalysis::Variables::StackVariable&);
219 };
220 
223 
228 void print(const StackVariables&,const Partitioner2::Partitioner&, std::ostream &out, const std::string &prefix = "");
229 
231 // Global variable descriptors
233 
236  rose_addr_t address_; // starting (lowest) virtual address
237 
238 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
239 private:
240  friend class boost::serialization::access;
241 
242  template<class S>
243  void serialize(S &s, const unsigned /*version*/) {
244  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseVariable);
245  s & BOOST_SERIALIZATION_NVP(address_);
246  }
247 #endif
248 
249 public:
253  GlobalVariable();
254 
260  GlobalVariable(rose_addr_t startingAddress, rose_addr_t maxSizeBytes,
261  const AddressSet &definingInstructionVas = AddressSet(), const std::string &name = "");
262 
268  rose_addr_t address() const { return address_; }
269  void address(rose_addr_t va) { address_ = va; }
275  const std::string& setDefaultName();
276 
282  bool operator==(const GlobalVariable &other) const;
283  bool operator!=(const GlobalVariable &other) const;
287  AddressInterval interval() const;
288 
292  void print(std::ostream&) const;
293  std::string toString() const;
297  friend std::ostream& operator<<(std::ostream&, const Rose::BinaryAnalysis::Variables::GlobalVariable&);
298 };
299 
306 
311 void print(const GlobalVariables&,const Partitioner2::Partitioner&, std::ostream &out, const std::string &prefix = "");
312 
314 // Analyzer
316 
319 public:
321  struct Settings {};
322 
323 private:
324  Settings settings_;
325 
326 public:
328  explicit VariableFinder(const Settings &settings = Settings());
329 
333  const Settings& settings() const { return settings_; }
334  Settings& settings() { return settings_; }
370  GlobalVariables findGlobalVariables(const Partitioner2::Partitioner&);
371 
378  void evict(const Partitioner2::FunctionPtr&);
379  void evict(const Partitioner2::Partitioner&);
386  bool isCached(const Partitioner2::FunctionPtr&);
387 
390 
393 
399  OffsetInterval referencedFrameArea(const Partitioner2::Partitioner&,
401  const SymbolicExpr::Ptr &address, size_t nBytes);
402 
407  std::set<int64_t> findStackOffsets(const Partitioner2::Partitioner&, SgAsmInstruction*);
408 
414 
450  AddressToAddresses findGlobalVariableVas(const Partitioner2::Partitioner&);
451 
455  std::set<rose_addr_t> findConstants(const SymbolicExpr::Ptr&);
456 
458  std::set<rose_addr_t> findConstants(SgAsmInstruction*);
459 
464 
469 
476 };
477 
478 } // namespace
479 } // namespace
480 } // namespace
481 
482 #endif
483 #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.
const AddressSet & definingInstructionVas() const
Property: Addresses of instructions related to this variable.
static RegisterDescriptor frameOrStackPointer(const Partitioner2::Partitioner &)
The register typically used as local variable base address.
Description of a global variable.
boost::shared_ptr< class MemoryCellState > MemoryCellStatePtr
Shared-ownership pointer to a cell-based memory state.
VariableFinder(const Settings &settings=Settings())
Construct a new analyzer.
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.
Describes a local or global variable.
const std::string & name() const
Property: Optional variable name.
InstructionSemantics2::BaseSemantics::InputOutputPropertySet & ioProperties()
Property: I/O properties.
void address(rose_addr_t va)
Property: Starting address.
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.
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.
rose_addr_t address() const
Property: Starting address.
OffsetInterval interval() const
Location within the function stack frame.
void ioProperties(const InstructionSemantics2::BaseSemantics::InputOutputPropertySet &set)
Property: I/O properties.
std::string sizeStr(uint64_t size)
Format size as a string.
void definingInstructionVas(const AddressSet &vas)
Property: Addresses of instructions related to this variable.
void print(std::ostream &) const
Printing global variable.
std::map< int64_t, AddressSet > OffsetToAddresses
Mapping from stack offsets to address sets.
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.
BaseVariable(size_t maxSizeBytes, const AddressSet &definingInstructionVas, const std::string &name)
Construct a variable with a given maximum size.
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.
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.
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.
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.
int64_t frameOffset() const
Property: Frame offset.
void print(std::ostream &) const
Printing local variable.
AddressSet & definingInstructionVas()
Property: Addresses of instructions related to this variable.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:323
Sawyer::Container::IntervalMap< AddressInterval, GlobalVariable > GlobalVariables
Maps virtual addresses to global variables.
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.
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.
Settings & settings()
Settings for this analysis.
Sawyer::Container::IntervalMap< OffsetInterval, StackVariable > StackVariables
Collection of local variables.
bool operator!=(const GlobalVariable &other) const
Compare two global variable descriptors.
bool operator==(const GlobalVariable &other) const
Compare two global variable descriptors.