ROSE 2.9.0
Loading...
Searching...
No Matches
SValue.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_SValue_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_SValue_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7#include <Combinatorics.h> // ROSE
8
9#include <Sawyer/SharedObject.h>
10#include <Sawyer/SmallObject.h>
11
12#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
13#include <boost/serialization/access.hpp>
14#include <boost/serialization/export.hpp>
15#include <boost/serialization/nvp.hpp>
16#endif
17
18namespace Rose {
19namespace BinaryAnalysis {
20namespace InstructionSemantics {
21namespace BaseSemantics {
22
24// CIL and JVM Semantic Values
26
28enum class ValueKind {
29 Unknown,
30 Integer32, // JVM Integer
31 Integer64, // JVM Long
32 NativeInt, // CIL native integer
33 Float32, // JVM Float
34 Float64, // JVM Double
35 ArrayReference,
36 ObjectReference,
37 ManagedPointer,
38 UnmanagedPointer,
39 ReturnAddress,
40
41 Category2Tail, // second slot of long/double
42 Invalid // unusable/destroyed/corrupted slot
43};
44
46std::ostream& operator<<(std::ostream&, ValueKind);
47
48
50// Semantic Values
52
68public:
70 using Ptr = SValuePtr;
71
72protected:
73 size_t width;
74 ValueKind kind_ = ValueKind::Unknown;
76 std::string typeDescriptor_;
79 // Serialization
80#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
81private:
82 friend class boost::serialization::access;
83
84 template<class S>
85 void serialize(S &s, const unsigned /*version*/) {
86 s & BOOST_SERIALIZATION_NVP(width);
87 s & BOOST_SERIALIZATION_NVP(kind_);
88 s & BOOST_SERIALIZATION_NVP(arrayLength_);
89 s & BOOST_SERIALIZATION_NVP(typeDescriptor_);
90 }
91#endif
92
94 // Normal, protected, C++ constructors
95protected:
96 SValue(); // needed for serialization
97 explicit SValue(size_t nbits); // hot
98 SValue(const SValue&);
99 SValue& operator=(const SValue&);
100
101public:
102 virtual ~SValue();
103
105 // Allocating static constructor. None are needed--this class is abstract.
106
108 // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
109 // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
110 // different: the object (this) is use for more than just selecting which virtual method to invoke.
111 //
112 // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
113 // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
114 // other difference (at least in this base class) is that we don't define any real constructors or static allocating
115 // constructors (usually named "instance")--it's because this is an abstract class.
116public:
122 virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
123
132 virtual SValuePtr unspecified_(size_t nbits) const = 0;
133
139 virtual SValuePtr bottom_(size_t nBits) const = 0;
140
144 virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
145
149 virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
150
154 virtual SValuePtr copy(size_t new_width=0) const = 0;
155
183 createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
184
191 SValuePtr createMerged(const SValuePtr &other, const MergerPtr&, const SmtSolverPtr&) const /*final*/;
192
194 // Dynamic pointer casts. No-ops since this is the base class
195public:
196 static SValuePtr promote(const SValuePtr&);
197
199 // The rest of the API...
200public:
206 size_t nBits() const /*final*/;
207
209 virtual ValueKind kind() const;
210 virtual void kind(ValueKind k);
211
213 virtual void arrayLength(const SValuePtr &sval);
214 virtual SValuePtr arrayLength() const;
215 virtual bool hasArrayLength() const;
216
218 virtual void typeDescriptor(const std::string &s);
219 virtual const std::string& typeDescriptor() const;
220 virtual bool hasTypeDescriptor() const;
221
227
234
240 virtual bool isBottom() const = 0;
241
249 bool isConcrete() const /*final*/;
250
257 Sawyer::Optional<uint64_t> toUnsigned() const /*final*/;
258
265 Sawyer::Optional<int64_t> toSigned() const /*final*/;
266
272 bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
273
279 bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
280
284 bool isTrue() const /*final*/;
285
289 bool isFalse() const /*final*/;
290
301 std::string comment() const /*final*/;
302 void comment(const std::string&) const /*final*/; // const is intentional (see documentation)
308 virtual void hash(Combinatorics::Hasher&) const = 0;
309
313 void print(std::ostream&) const;
314 virtual void print(std::ostream&, Formatter&) const = 0;
321 std::string toString() const;
322
325 SValuePtr obj;
326 Formatter &fmt;
327 public:
329 void print(std::ostream&) const;
330 };
331
341 WithFormatter operator+(const std::string &linePrefix);
345 // This is the virtual interface that uses names that are not consistent with most of the rest of binary analysis. Calling
346 // these directly is deprecated and we may make them protected at some time. [Robb Matzke 2021-03-18].
348public: // for backward compatibility for now, but assume protected
350 virtual bool is_number() const = 0;
351
353 virtual uint64_t get_number() const = 0;
354
358 virtual size_t get_width() const { return width; }
359 virtual void set_width(size_t nbits) { width = nbits; }
363 virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
364
366 virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
367
373 virtual std::string get_comment() const;
374 virtual void set_comment(const std::string&) const; // const is intended; cf. doxygen comment
376};
377
378std::ostream& operator<<(std::ostream&, const SValue&);
379std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
380
381} // namespace
382} // namespace
383} // namespace
384} // namespace
385
386#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
388#endif
389
390#endif
391#endif
Sawyer::Optional< uint64_t > toUnsigned() const
Converts a concrete value to a native unsigned integer.
void print(std::ostream &) const
Print a value to a stream using default format.
virtual Sawyer::Optional< SValuePtr > createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const =0
Possibly create a new value by merging two existing values.
virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Virtual API.
virtual void set_width(size_t nbits)
Virtual API.
Definition SValue.h:359
virtual void set_comment(const std::string &) const
Some subclasses support the ability to add comments to values.
WithFormatter with_format(Formatter &)
Used for printing values with formatting.
SValuePtr createMerged(const SValuePtr &other, const MergerPtr &, const SmtSolverPtr &) const
Create a new value by merging two existing values.
virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Virtual API.
virtual SValuePtr number_(size_t nbits, uint64_t number) const =0
Create a new concrete semantic value.
WithFormatter operator+(Formatter &)
Used for printing values with formatting.
WithFormatter operator+(const std::string &linePrefix)
Used for printing values with formatting.
std::string toString() const
Render this symbolic expression as a string.
bool isJvmCategory1()
Determines whether a value is JVM category-1.
std::string typeDescriptor_
Symbolic length of the array if an ArrayReference.
Definition SValue.h:76
bool isJvmCategory2()
Determines whether a value is JVM category-2.
virtual SValuePtr boolean_(bool value) const
Create a new, Boolean value.
Definition SValue.h:149
virtual SValuePtr undefined_(size_t nbits) const =0
Create a new undefined semantic value.
virtual bool isBottom() const =0
Determines whether a value is a data-flow bottom.
virtual SValuePtr unspecified_(size_t nbits) const =0
Create a new unspecified semantic value.
bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const
Tests two values for possible equality.
virtual std::string get_comment() const
Some subclasses support the ability to add comments to values.
bool isFalse() const
Returns true if concrete zero.
bool isTrue() const
Returns true if concrete non-zero.
virtual ValueKind kind() const
Property: value kind.
virtual SValuePtr copy(size_t new_width=0) const =0
Create a new value from an existing value, changing the width if new_width is non-zero.
bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const
Tests two values for equality.
Sawyer::Optional< int64_t > toSigned() const
Converts a concrete value to a native signed integer.
bool isConcrete() const
Determines if the value is a concrete number.
virtual SValuePtr bottom_(size_t nBits) const =0
Data-flow bottom value.
Holds a value or nothing.
Definition Optional.h:54
Creates SharedPointer from this.
Base class for reference counted objects.
Small object support.
Definition SmallObject.h:19
Base classes for instruction semantics.
ValueKind
Kind of an SValue for emulating JVM and CIL instructions.
Definition SValue.h:28
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.
Sawyer support library.