ROSE  0.11.101.0
NullSemantics.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics_NullSemantics_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics_NullSemantics_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
8 
9 namespace Rose {
10 namespace BinaryAnalysis { // documented elsewhere
11 namespace InstructionSemantics { // documented elsewhere
12 
13 
18 namespace NullSemantics {
19 
21 // Semantic values
23 
26 
29 public:
32 
34  using Ptr = SValuePtr;
35 
37  // Real constructors.
38 protected:
39  explicit SValue(size_t nbits): BaseSemantics::SValue(nbits) {}
40 
42  // Static allocating constructors
43 public:
45  static SValuePtr instance() {
46  return SValuePtr(new SValue(1));
47  }
48 
50  static SValuePtr instance(size_t nbits) {
51  return SValuePtr(new SValue(nbits));
52  }
53 
55  static SValuePtr instance(size_t nbits, uint64_t number) {
56  return SValuePtr(new SValue(nbits)); // the number is not important in this domain
57  }
58 
60  static SValuePtr instance(const SValuePtr &other) {
61  return SValuePtr(new SValue(*other));
62  }
63 
65  // Virtual constructors
66 public:
67  virtual BaseSemantics::SValuePtr bottom_(size_t nBits) const override {
68  return instance(nBits);
69  }
70  virtual BaseSemantics::SValuePtr undefined_(size_t nBits) const override {
71  return instance(nBits);
72  }
73  virtual BaseSemantics::SValuePtr unspecified_(size_t nBits) const override {
74  return instance(nBits);
75  }
76  virtual BaseSemantics::SValuePtr number_(size_t nBits, uint64_t number) const override {
77  return instance(nBits, number);
78  }
79  virtual BaseSemantics::SValuePtr copy(size_t new_width=0) const override {
80  SValuePtr retval(new SValue(*this));
81  if (new_width!=0 && new_width!=retval->nBits())
82  retval->set_width(new_width);
83  return retval;
84  }
87  const SmtSolverPtr&) const override {
88  return Sawyer::Nothing();
89  }
90 
92  // Dynamic pointer casting
93 public:
95  static SValuePtr promote(const BaseSemantics::SValuePtr &v) {
96  SValuePtr retval = v.dynamicCast<SValue>();
97  ASSERT_not_null(retval);
98  return retval;
99  }
100 
102  // Implementations of functions inherited
103 public:
104  virtual bool isBottom() const override {
105  return false;
106  }
107 
108  virtual void print(std::ostream &stream, BaseSemantics::Formatter&) const override {
109  stream <<"VOID[" <<nBits() <<"]";
110  }
111 
112  virtual void hash(Combinatorics::Hasher &hasher) const override {
113  hasher.insert(0); // hash depends on number of SValues hashed, but not any content
114  }
115 
117  // Override legacy members. These now are called by the camelCase names in the base class. Eventually we'll switch the
118  // camelCase names to be the virtual functions and get rid of the snake_case names, so be sure to specify "override" in
119  // your own code so you know when we make the switch.
120 public:
121  // See isConcrete
122  virtual bool is_number() const override {
123  return false;
124  }
125 
126  // See toUnsigned and toSigned
127  virtual uint64_t get_number() const override {
128  ASSERT_not_reachable("not a number");
129  uint64_t retval;
130  return retval;
131  }
132 
133  // See mayEqual
134  virtual bool may_equal(const BaseSemantics::SValuePtr &other,
135  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
136  return true;
137  }
138 
139  // See mustEqual
140  virtual bool must_equal(const BaseSemantics::SValuePtr &other,
141  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
142  return this == getRawPointer(other); // must be equal if they're both the same object
143  }
144 
145 };
146 
147 
149 // Register state
151 
153 typedef boost::shared_ptr<class RegisterState> RegisterStatePtr;
154 
159 public:
162 
165 
166 protected:
167  RegisterState(const RegisterState &other)
168  : BaseSemantics::RegisterState(other) {}
169 
171  : BaseSemantics::RegisterState(protoval, regdict) {}
172 
173 public:
174  static RegisterStatePtr instance(const BaseSemantics::SValuePtr &protoval, const RegisterDictionaryPtr &regdict) {
175  return RegisterStatePtr(new RegisterState(protoval, regdict));
176  }
177 
179  const RegisterDictionaryPtr &regdict) const override {
180  return instance(protoval, regdict);
181  }
182 
183  virtual BaseSemantics::RegisterStatePtr clone() const override {
184  return RegisterStatePtr(new RegisterState(*this));
185  }
186 
187  static RegisterStatePtr promote(const BaseSemantics::RegisterStatePtr &from) {
188  RegisterStatePtr retval = boost::dynamic_pointer_cast<RegisterState>(from);
189  ASSERT_not_null(retval);
190  return retval;
191  }
192 
193  virtual bool merge(const BaseSemantics::RegisterStatePtr &other_, BaseSemantics::RiscOperators*) override {
194  return false;
195  }
196 
197  virtual void clear() override {}
198  virtual void zero() override {}
199 
202  BaseSemantics::RiscOperators *ops) override {
203  return protoval()->undefined_(reg.nBits());
204  }
205 
208  BaseSemantics::RiscOperators *ops) override {
209  return protoval()->undefined_(reg.nBits());
210  }
211 
213  BaseSemantics::RiscOperators *ops) override {}
214 
215  virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators*) const override {}
216 
217  virtual void print(std::ostream&, BaseSemantics::Formatter&) const override {}
218 };
219 
220 
222 // Memory state
224 
226 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
227 
232 public:
235 
238 
239 protected:
240  MemoryState(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval)
241  : BaseSemantics::MemoryState(addrProtoval, valProtoval) {}
242 
243  MemoryState(const MemoryStatePtr &other)
244  : BaseSemantics::MemoryState(other) {}
245 
246 public:
247  static MemoryStatePtr instance(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval) {
248  return MemoryStatePtr(new MemoryState(addrProtoval, valProtoval));
249  }
250 
251 public:
253  const BaseSemantics::SValuePtr &valProtoval) const override {
254  return instance(addrProtoval, valProtoval);
255  }
256 
257  virtual BaseSemantics::MemoryStatePtr clone() const override {
258  return MemoryStatePtr(new MemoryState(*this));
259  }
260 
261 public:
262  static MemoryStatePtr promote(const BaseSemantics::MemoryStatePtr &x) {
263  MemoryStatePtr retval = boost::dynamic_pointer_cast<MemoryState>(x);
264  ASSERT_not_null(x);
265  return retval;
266  }
267 
268 public:
269  virtual void clear() override {}
270 
273  BaseSemantics::RiscOperators *valOps) override {
274  return dflt->copy();
275  }
276 
277  virtual void writeMemory(const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &value,
278  BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override {}
279 
282  BaseSemantics::RiscOperators *valOps) override {
283  return dflt->copy();
284  }
285 
287  BaseSemantics::RiscOperators *valOps) const override {}
288 
289  virtual void print(std::ostream&, BaseSemantics::Formatter&) const override {}
290 
292  BaseSemantics::RiscOperators *valOps) override {
293  return false;
294  }
295 };
296 
297 
299 // Complete state
301 
302 typedef BaseSemantics::State State;
303 typedef BaseSemantics::StatePtr StatePtr;
304 
305 
307 // RISC operators
309 
311 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
312 
315 public:
318 
321 
323  // Real constructors
324 protected:
325  explicit RiscOperators(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr&);
326 
327  explicit RiscOperators(const BaseSemantics::StatePtr&, const SmtSolverPtr&);
328 
330  // Static allocating constructors
331 public:
332  ~RiscOperators();
333 
336  static RiscOperatorsPtr instanceFromRegisters(const RegisterDictionaryPtr &regdict);
337 
339  static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval,
340  const SmtSolverPtr &solver = SmtSolverPtr());
341 
343  static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr&, const SmtSolverPtr &solver = SmtSolverPtr());
344 
346  // Virtual constructors
347 public:
349  const SmtSolverPtr &solver = SmtSolverPtr()) const override;
350 
352  const SmtSolverPtr &solver = SmtSolverPtr()) const override;
353 
355  // Risc operators inherited
356 public:
358  const BaseSemantics::SValuePtr &b_) override;
359 
361  const BaseSemantics::SValuePtr &b_) override;
362 
364  const BaseSemantics::SValuePtr &b_) override;
365 
366  virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &a_) override;
367 
369  size_t begin_bit, size_t end_bit) override;
370 
372  const BaseSemantics::SValuePtr &b_) override;
373 
375 
377 
379  const BaseSemantics::SValuePtr &sa_) override;
380 
382  const BaseSemantics::SValuePtr &sa_) override;
383 
385  const BaseSemantics::SValuePtr &sa_) override;
386 
388  const BaseSemantics::SValuePtr &sa_) override;
389 
391  const BaseSemantics::SValuePtr &sa_) override;
392 
394 
396  const BaseSemantics::SValuePtr &a_,
397  const BaseSemantics::SValuePtr &b_,
398  IteStatus&) override;
399 
400  virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override;
401 
403  const BaseSemantics::SValuePtr &b_) override;
404 
406  const BaseSemantics::SValuePtr &b_,
407  const BaseSemantics::SValuePtr &c_,
408  BaseSemantics::SValuePtr &carry_out/*out*/) override;
409 
410  virtual BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &a_) override;
411 
413  const BaseSemantics::SValuePtr &b_) override;
414 
416  const BaseSemantics::SValuePtr &b_) override;
417 
419  const BaseSemantics::SValuePtr &b_) override;
420 
422  const BaseSemantics::SValuePtr &b_) override;
423 
425  const BaseSemantics::SValuePtr &b_) override;
426 
428  const BaseSemantics::SValuePtr &b_) override;
429 
431  const BaseSemantics::SValuePtr &addr,
432  const BaseSemantics::SValuePtr &dflt,
433  const BaseSemantics::SValuePtr &cond) override;
434 
436  const BaseSemantics::SValuePtr &addr,
437  const BaseSemantics::SValuePtr &dflt) override;
438 
439  virtual void writeMemory(RegisterDescriptor segreg,
440  const BaseSemantics::SValuePtr &addr,
441  const BaseSemantics::SValuePtr &data,
442  const BaseSemantics::SValuePtr &cond) override;
443 };
444 
445 } // namespace
446 } // namespace
447 } // namespace
448 } // namespace
449 
450 #endif
451 #endif
Base classes for instruction semantics.
Definition: Dispatcher.h:18
static RiscOperatorsPtr instanceFromRegisters(const RegisterDictionaryPtr &regdict)
Instantiate a new RiscOperators object and configures it to use semantic values and states that are d...
virtual BaseSemantics::SValuePtr shiftLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted left.
virtual bool merge(const BaseSemantics::RegisterStatePtr &other_, BaseSemantics::RiscOperators *) override
Merge register states for data flow analysis.
virtual BaseSemantics::SValuePtr equalToZero(const BaseSemantics::SValuePtr &a_) override
Determines whether a value is equal to zero.
virtual void hash(Combinatorics::Hasher &, BaseSemantics::RiscOperators *) const override
Hash the register state.
virtual uint64_t get_number() const override
Virtual API.
size_t nBits() const
Property: Size in bits.
virtual bool is_number() const override
Virtual API.
virtual BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise OR of two values.
virtual BaseSemantics::SValuePtr addWithCarries(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_, const BaseSemantics::SValuePtr &c_, BaseSemantics::SValuePtr &carry_out) override
Add two values of equal size and a carry bit.
static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr())
Constructor.
virtual BaseSemantics::SValuePtr unsignedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with unsigned values.
virtual void clear() override
Removes stored values from the register state.
virtual BaseSemantics::SValuePtr leastSignificantSetBit(const BaseSemantics::SValuePtr &a_) override
Returns position of least significant set bit; zero when no bits are set.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
virtual BaseSemantics::SValuePtr number_(size_t nBits, uint64_t number) const override
Create a new concrete semantic value.
Definition: NullSemantics.h:76
void insert(const std::string &x)
Insert data into the digest.
RegisterDictionaryPtr regdict
Registers that are able to be stored by this state.
Definition: RegisterState.h:40
virtual void hash(Combinatorics::Hasher &, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) const override
Calculate a hash for this memory state.
virtual bool may_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual API.
virtual BaseSemantics::RegisterStatePtr create(const BaseSemantics::SValuePtr &protoval, const RegisterDictionaryPtr &regdict) const override
Virtual constructor.
Holds a value or nothing.
Definition: Optional.h:49
virtual BaseSemantics::MemoryStatePtr create(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval) const override
Virtual allocating constructor.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
virtual BaseSemantics::SValuePtr concat(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Concatenates the bits of two values.
virtual BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &a_) override
Two's complement.
Main namespace for the ROSE library.
virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override
Sign extends a value.
virtual Sawyer::Optional< BaseSemantics::SValuePtr > createOptionalMerge(const BaseSemantics::SValuePtr &other, const BaseSemantics::MergerPtr &, const SmtSolverPtr &) const override
Possibly create a new value by merging two existing values.
Definition: NullSemantics.h:86
virtual BaseSemantics::SValuePtr signedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two signed values.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to null RISC operations.
static SValuePtr instance(size_t nbits)
Instantiate a new undefined value.
Definition: NullSemantics.h:50
virtual BaseSemantics::SValuePtr mostSignificantSetBit(const BaseSemantics::SValuePtr &a_) override
Returns position of most significant set bit; zero when no bits are set.
virtual BaseSemantics::SValuePtr readMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Read a value from memory.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *ops) override
Read a register without side effects.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer to null register state.
virtual BaseSemantics::SValuePtr copy(size_t new_width=0) const override
Create a new value from an existing value, changing the width if new_width is non-zero.
Definition: NullSemantics.h:79
virtual BaseSemantics::SValuePtr undefined_(size_t nBits) const override
Create a new undefined semantic value.
Definition: NullSemantics.h:70
virtual BaseSemantics::SValuePtr signedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with signed values.
virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &a_, size_t begin_bit, size_t end_bit) override
Extracts bits from a value.
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer to null register state.
static SValuePtr promote(const BaseSemantics::SValuePtr &v)
Promote a base value to a NullSemantics value.
Definition: NullSemantics.h:95
virtual void zero() override
Set all registers to the zero.
virtual void writeMemory(const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &value, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Write a value to memory.
virtual BaseSemantics::SValuePtr shiftRightArithmetic(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right arithmetically (with sign bit).
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
virtual BaseSemantics::SValuePtr unspecified_(size_t nBits) const override
Create a new unspecified semantic value.
Definition: NullSemantics.h:73
virtual BaseSemantics::MemoryStatePtr clone() const override
Virtual allocating copy constructor.
virtual BaseSemantics::RegisterStatePtr clone() const override
Make a copy of this register state.
Describes (part of) a physical CPU register.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print a memory state to more than one line of output.
NullSemantics operators always return a new undefined value.
virtual BaseSemantics::SValuePtr unsignedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiply two unsigned values.
virtual BaseSemantics::SValuePtr peekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt) override
Read memory without side effects.
virtual BaseSemantics::SValuePtr add(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Adds two integers of equal size.
virtual BaseSemantics::SValuePtr peekMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Read a value from memory without side effects.
virtual BaseSemantics::SValuePtr xor_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise XOR of two values.
virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &a_) override
One's complement.
SharedPointer< U > dynamicCast() const
Dynamic cast.
static SValuePtr instance()
Instantiate a new prototypical values.
Definition: NullSemantics.h:45
Binary analysis.
virtual void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override
Writes a value to memory.
virtual bool isBottom() const override
Determines whether a value is a data-flow bottom.
virtual void writeRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &value, BaseSemantics::RiscOperators *ops) override
Write a value to a register.
static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Static allocating constructor.
virtual BaseSemantics::SValuePtr rotateRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the right.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a null semantic value.
Definition: NullSemantics.h:25
virtual BaseSemantics::SValuePtr iteWithStatus(const BaseSemantics::SValuePtr &sel_, const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_, IteStatus &) override
If-then-else with status.
static SValuePtr instance(size_t nbits, uint64_t number)
Instantiate a new concrete value.
Definition: NullSemantics.h:55
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
virtual BaseSemantics::SValuePtr bottom_(size_t nBits) const override
Data-flow bottom value.
Definition: NullSemantics.h:67
virtual BaseSemantics::SValuePtr and_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise AND of two values.
virtual void hash(Combinatorics::Hasher &hasher) const override
Hash this semantic value.
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
virtual bool merge(const BaseSemantics::MemoryStatePtr &other, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Merge memory states for data flow analysis.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:48
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
Represents no value.
Definition: Optional.h:32
virtual void print(std::ostream &stream, BaseSemantics::Formatter &) const override
Print a value to a stream using default format.
static SValuePtr instance(const SValuePtr &other)
Instantiate a new copy of an existing value.
Definition: NullSemantics.h:60
virtual BaseSemantics::SValuePtr rotateLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the left.
Base class for semantics machine states.
Definition: State.h:39
virtual BaseSemantics::SValuePtr unsignedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two unsigned values.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print the register contents.
virtual BaseSemantics::SValuePtr readMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt, const BaseSemantics::SValuePtr &cond) override
Reads a value from memory.
virtual BaseSemantics::SValuePtr signedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiplies two signed values.
virtual BaseSemantics::SValuePtr shiftRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right logically (no sign bit).
virtual bool must_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual API.
virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *ops) override
Read a value from a register.