ROSE 0.11.145.281
BasicBlock.h
1#ifndef ROSE_BinaryAnalysis_Partitioner2_BasicBlock_H
2#define ROSE_BinaryAnalysis_Partitioner2_BasicBlock_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
6
7#include <Rose/BinaryAnalysis/Partitioner2/Semantics.h>
8#include <Rose/SourceLocation.h>
9
10#include <Sawyer/Attribute.h>
11#include <Sawyer/Cached.h>
12#include <Sawyer/Map.h>
13#include <Sawyer/Optional.h>
14#include <Sawyer/SharedPointer.h>
15#include <Sawyer/Synchronization.h>
16
17#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
18#include <boost/serialization/access.hpp>
19#endif
20
21namespace Rose {
22namespace BinaryAnalysis {
23namespace Partitioner2 {
24
26
28// BasicBlockSemantics
30
102
105private:
107 EdgeType type_;
108 Confidence confidence_;
109
110#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
111private:
112 friend class boost::serialization::access;
113 template<class S> void serialize(S &s, const unsigned /*version*/);
114#endif
115
116public: // "protected" fails for boost-1.58.
117 // intentionally undocumented; needed for serialization
119 : type_(E_USER_DEFINED), confidence_(ASSUMED) {}
120
121public:
123 : expr_(expr), type_(type), confidence_(confidence) {}
124
126 const Semantics::SValuePtr& expr() const { return expr_; }
127
129 EdgeType type() const { return type_; }
130 void type(EdgeType t) { type_ = t; }
131
135 Confidence confidence() const { return confidence_; }
136 void confidence(Confidence c) { confidence_ = c; }
138};
139
141// BasicBlock
143
166public:
169
172
175
176private:
177 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
178
179 bool isFrozen_; // True when the object becomes read-only
180 Address startVa_; // Starting address, perhaps redundant with insns_[0]->p_address
181 std::string comment_; // Mutli-line plain-text comment
182 std::vector<SgAsmInstruction*> insns_; // Instructions in the order they're executed
183 BasicBlockSemantics semantics_; // All semantics-related information
184 std::vector<DataBlockPtr> dblocks_; // Data blocks owned by this basic block, sorted
185 SourceLocation sourceLocation_; // Optional location of basic block in source code
186
187 // When a basic block gets lots of instructions some operations become slow due to the linear nature of the instruction
188 // list. Therefore, we also keep a mapping from instruction address to position in the list. The mapping is only used when
189 // the bigBlock size is reached.
190 static const size_t bigBlock_ = 200;
192 InsnAddrMap insnAddrMap_; // maps instruction address to index in insns_ vector
193 bool hasIndirectControlFlow_ = false; // true if block has indirect control flow
194
195 // The following members are caches either because their value is seldom needed and expensive to compute, or because
196 // the value is best computed at a higher layer than a single basic block (e.g., in the partitioner) yet it makes the
197 // most sense to store it here. Make sure clearCache() resets these to initial values.
198 Sawyer::Cached<Successors> successors_; // control flow successors out of final instruction
199 Sawyer::Cached<std::set<Address> > ghostSuccessors_;// non-followed successors from opaque predicates, all insns
200 Sawyer::Cached<bool> isFunctionCall_; // is this block semantically a function call?
201 Sawyer::Cached<bool> isFunctionReturn_; // is this block semantically a return from the function?
202 Sawyer::Cached<bool> mayReturn_; // a function return is reachable from this basic block in the CFG
203 Sawyer::Cached<bool> popsStack_; // basic block has a net popping effect
204 void clearCacheNS() const;
205public:
206 void copyCache(const BasicBlockPtr &other);
207
208
210 // Serialization
212#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
213private:
214 friend class boost::serialization::access;
215 template<class S> void serializeCommon(S&, const unsigned version);
216 template<class S> void save(S&, const unsigned version) const;
217 template<class S> void load(S&, const unsigned version);
218 BOOST_SERIALIZATION_SPLIT_MEMBER();
219#endif
220
221
223 // Constructors
225protected:
226 BasicBlock(); // needed for serialization
227
228 // use instance() instead
229 BasicBlock(Address startVa, const PartitionerConstPtr&);
230public:
231 ~BasicBlock();
232
233public:
239 static Ptr instance(Address startVa, const PartitionerConstPtr &partitioner) {
240 return Ptr(new BasicBlock(startVa, partitioner));
241 }
242
248 virtual Ptr create(Address startVa, const PartitionerConstPtr &partitioner) const {
249 return instance(startVa, partitioner);
250 }
251
253 // Cache
255public:
259 void clearCache() {
260 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
261 clearCacheNS();
262 }
263
265 // Status
267public:
273 bool isFrozen() const {
274 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
275 return isFrozen_;
276 }
277
285 const std::string& comment() const { return comment_; }
286 void comment(const std::string &s) { comment_ = s; }
292 const SourceLocation& sourceLocation() const { return sourceLocation_; }
293 void sourceLocation(const SourceLocation &loc) { sourceLocation_ = loc; }
298 // Instructions
300public:
308 Address address() const {
309 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
310 return startVa_;
311 }
312
320
328
332 size_t nInstructions() const {
333 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
334 return insns_.size();
335 }
336
344 bool isEmpty() const {
345 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
346 return insns_.empty();
347 }
348
356
364
382 const std::vector<SgAsmInstruction*>& instructions() const { return insns_; }
383
402
409 void pop();
410
417 std::set<Address> explicitConstants() const;
418
419
421 // Static data blocks
423public:
427 size_t nDataBlocks() const;
428
435
443
453
462
468 const std::vector<DataBlockPtr>& dataBlocks() const { return dblocks_; }
469
470
472 // Semantics
474public:
482
491
500
501
502
504 // Control flow
506public:
517 const Sawyer::Cached<Successors>& successors() const { return successors_; }
518 void successors(const Successors&);
531 const Sawyer::Cached<std::set<Address> >& ghostSuccessors() const { return ghostSuccessors_; }
532
544 void insertSuccessor(Address va, size_t nBits, EdgeType type=E_NORMAL, Confidence confidence=ASSUMED);
562
572 // Cached properties computed elsewhere
574public:
583 const Sawyer::Cached<bool>& isFunctionCall() const { return isFunctionCall_; }
584 void isFunctionCall(bool flag) const { isFunctionCall_.set(flag); }
585
593 const Sawyer::Cached<bool>& isFunctionReturn() const { return isFunctionReturn_; }
594 void isFunctionReturn(bool flag) const { isFunctionReturn_.set(flag); }
595
602 const Sawyer::Cached<bool>& mayReturn() const { return mayReturn_; }
603 void mayReturn(bool flag) const { mayReturn_.set(flag); }
604
610 const Sawyer::Cached<bool>& popsStack() const { return popsStack_; }
611 void popsStack(bool flag) const { popsStack_.set(flag); }
612
613
615 // Output
617public:
621 std::string printableName() const;
622
623
625 // Private members for the partitioner
627private:
628 friend class Partitioner;
629 void init(const PartitionerConstPtr&);
630 void freeze() { isFrozen_ = true; semantics_.optionalPenultimateState = Sawyer::Nothing(); }
631 void thaw() { isFrozen_ = false; }
632 BasicBlockSemantics undropSemanticsNS(const PartitionerConstPtr&);
633
634 // Find an equivalent data block and replace it with the specified data block, or insert the specified data block.
635 void replaceOrInsertDataBlock(const DataBlockPtr&);
636};
637
638} // namespace
639} // namespace
640} // namespace
641
642#endif
643#endif
Information related to instruction semantics.
Definition BasicBlock.h:35
BaseSemantics::DispatcherPtr dispatcher
How instructions are dispatched.
Definition BasicBlock.h:43
bool wasDropped
Whether the semantics had been dropped and reconstructed.
Definition BasicBlock.h:70
BasicBlockSemantics()
Construct an empty semantics object.
Definition BasicBlock.h:74
bool usingDispatcher
Whether semantic state is up-to-date.
Definition BasicBlock.h:61
BaseSemantics::StatePtr initialState
Initial state for semantics.
Definition BasicBlock.h:55
bool isSemanticsDropped() const
Determines whether semantics have been dropped.
Definition BasicBlock.h:81
bool isSemanticsError() const
Determines whether a semantics error was encountered.
Definition BasicBlock.h:89
Sawyer::Optional< BaseSemantics::StatePtr > optionalPenultimateState
The state just prior to executing the final instruction.
Definition BasicBlock.h:67
BaseSemantics::RiscOperatorsPtr operators
Risc operators.
Definition BasicBlock.h:49
BaseSemantics::StatePtr finalState() const
Return the final semantic state.
Definition BasicBlock.h:98
const Semantics::SValuePtr & expr() const
Symbolic expression for the successor address.
Definition BasicBlock.h:126
void confidence(Confidence c)
Confidence level of this successor.
Definition BasicBlock.h:136
Confidence confidence() const
Confidence level of this successor.
Definition BasicBlock.h:135
BasicBlockSemantics semantics() const
Return information about semantics.
const Sawyer::Cached< bool > & popsStack() const
Pops stack property.
Definition BasicBlock.h:610
void hasIndirectControlFlow(bool)
Property: Tracks whether the block has indirect control flow.
void clearCache()
Clear all cached data.
Definition BasicBlock.h:259
Sawyer::Optional< size_t > instructionIndex(SgAsmInstruction *) const
Position of an instruction.
const Sawyer::Cached< bool > & isFunctionCall() const
Is a function call?
Definition BasicBlock.h:583
const Sawyer::Cached< std::set< Address > > & ghostSuccessors() const
Ghost successors.
Definition BasicBlock.h:531
const std::vector< DataBlockPtr > & dataBlocks() const
Data blocks owned.
Definition BasicBlock.h:468
AddressIntervalSet insnAddresses() const
Get all instruction addresses.
void successors(const Successors &)
Control flow successors.
void insertSuccessor(const BaseSemantics::SValuePtr &, EdgeType type=E_NORMAL, Confidence confidence=ASSUMED)
Insert a new successor.
std::string printableName() const
A printable name for this basic block.
size_t nDataBlocks() const
Get the number of data blocks owned.
Address fallthroughVa() const
Get the address after the end of the final instruction.
bool isEmpty() const
Return true if this basic block has no instructions.
Definition BasicBlock.h:344
void insertSuccessor(Address va, size_t nBits, EdgeType type=E_NORMAL, Confidence confidence=ASSUMED)
Insert a new successor.
void sourceLocation(const SourceLocation &loc)
Optional location in source code.
Definition BasicBlock.h:293
static Ptr instance(Address startVa, const PartitionerConstPtr &partitioner)
Static allocating constructor.
Definition BasicBlock.h:239
Sawyer::Optional< size_t > instructionExists(SgAsmInstruction *) const
Determines if this basic block contains the specified instruction.
std::set< Address > explicitConstants() const
Set of explicit constants.
DataBlockPtr eraseDataBlock(const DataBlockPtr &)
Remove specified or equivalent data block from this basic block.
Address address() const
Get the address for a basic block.
Definition BasicBlock.h:308
bool insertDataBlock(const DataBlockPtr &)
Make this basic block own the specified data block or equivalent data block.
void comment(const std::string &s)
Comment.
Definition BasicBlock.h:286
void clearSuccessors()
Clear all successor information.
void dropSemantics(const PartitionerConstPtr &)
Drops semantic information.
const SourceLocation & sourceLocation() const
Optional location in source code.
Definition BasicBlock.h:292
BasicBlockSuccessors Successors
All successors in no particular order.
Definition BasicBlock.h:174
bool eraseSuccessor(const BaseSemantics::SValuePtr &, EdgeType type, Sawyer::Optional< Confidence >=Sawyer::Nothing())
Remove the specified successor if it exists.
Sawyer::SharedPointer< BasicBlock > Ptr
Shared pointer to a basic block.
Definition BasicBlock.h:168
void append(const PartitionerConstPtr &, SgAsmInstruction *)
Append an instruction to a basic block.
const Sawyer::Cached< bool > & isFunctionReturn() const
Is a function return?
Definition BasicBlock.h:593
size_t nInstructions() const
Get the number of instructions in this block.
Definition BasicBlock.h:332
const Sawyer::Cached< Successors > & successors() const
Control flow successors.
Definition BasicBlock.h:517
Sawyer::Optional< size_t > instructionIndex(Address) const
Position of an instruction.
DataBlockPtr dataBlockExists(const DataBlockPtr &) const
Determine if this basic block contains the specified data block or equivalent data block.
const std::vector< SgAsmInstruction * > & instructions() const
Get the instructions for this block.
Definition BasicBlock.h:382
SgAsmInstruction * instructionExists(Address startVa) const
Determine if this basic block contains an instruction at a specific address.
bool isFrozen() const
Determine if basic block is read-only.
Definition BasicBlock.h:273
BasicBlockSemantics undropSemantics(const PartitionerConstPtr &)
Undrop semantics.
bool eraseSuccessor(const BasicBlockSuccessor &)
Remove the specified successor if it exists.
const Sawyer::Cached< bool > & mayReturn() const
May-return property.
Definition BasicBlock.h:602
virtual Ptr create(Address startVa, const PartitionerConstPtr &partitioner) const
Virtual constructor.
Definition BasicBlock.h:248
bool hasIndirectControlFlow() const
Property: Tracks whether the block has indirect control flow.
AddressIntervalSet dataAddresses() const
Addresses that are part of static data.
const std::string & comment() const
Comment.
Definition BasicBlock.h:285
Partitions instructions into basic blocks and functions.
Information about a source location.
API and storage for attributes.
Definition Attribute.h:215
Implements cache data members.
Definition Cached.h:40
void set(const Value &x) const
Assign a new value.
Definition Cached.h:134
Container associating values with keys.
Definition Sawyer/Map.h:72
Represents no value.
Definition Optional.h:36
Holds a value or nothing.
Definition Optional.h:56
Base class for reference counted objects.
Reference-counting intrusive smart pointer.
Base class for machine instructions.
Base classes for instruction semantics.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
Sawyer::SharedPointer< const Partitioner > PartitionerConstPtr
Shared-ownership pointer for Partitioner.
std::vector< BasicBlockSuccessor > BasicBlockSuccessors
All successors in no particular order.
@ E_NORMAL
Normal control flow edge, nothing special.
@ ASSUMED
The value is an assumption without any proof.
Sawyer::SharedPointer< DataBlock > DataBlockPtr
Shared-ownership pointer for DataBlock.
std::uint64_t Address
Address.
Definition Address.h:11
The ROSE library.