ROSE 2.15.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::string toString(ValueKind);
47
49std::ostream& operator<<(std::ostream&, ValueKind);
50
51
53// Semantic Values
55
71public:
73 using Ptr = SValuePtr;
74
75protected:
76 size_t width;
77 ValueKind kind_ = ValueKind::Unknown;
79 std::string typeDescriptor_;
80 std::string symbolName_;
83 // Serialization
84#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
85private:
86 friend class boost::serialization::access;
87
88 template<class S>
89 void serialize(S &s, const unsigned /*version*/) {
90 s & BOOST_SERIALIZATION_NVP(width);
91 s & BOOST_SERIALIZATION_NVP(kind_);
92 s & BOOST_SERIALIZATION_NVP(arrayLength_);
93 s & BOOST_SERIALIZATION_NVP(typeDescriptor_);
94 s & BOOST_SERIALIZATION_NVP(symbolName_);
95 }
96#endif
97
99 // Normal, protected, C++ constructors
100protected:
101 SValue(); // needed for serialization
102 explicit SValue(size_t nbits); // hot
103 SValue(const SValue&);
104 SValue& operator=(const SValue&);
105
106public:
107 virtual ~SValue();
108
110 // Allocating static constructor. None are needed--this class is abstract.
111
113 // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
114 // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
115 // different: the object (this) is use for more than just selecting which virtual method to invoke.
116 //
117 // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
118 // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
119 // other difference (at least in this base class) is that we don't define any real constructors or static allocating
120 // constructors (usually named "instance")--it's because this is an abstract class.
121public:
127 virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
128
137 virtual SValuePtr unspecified_(size_t nbits) const = 0;
138
144 virtual SValuePtr bottom_(size_t nBits) const = 0;
145
149 virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
150
154 virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
155
159 virtual SValuePtr copy(size_t new_width=0) const = 0;
160
188 createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
189
196 SValuePtr createMerged(const SValuePtr &other, const MergerPtr&, const SmtSolverPtr&) const /*final*/;
197
199 // Dynamic pointer casts. No-ops since this is the base class
200public:
201 static SValuePtr promote(const SValuePtr&);
202
204 // The rest of the API...
205public:
211 size_t nBits() const /*final*/;
212
214 virtual ValueKind kind() const;
215 virtual void kind(ValueKind k);
216
218 virtual void arrayLength(const SValuePtr &sval);
219 virtual SValuePtr arrayLength() const;
220 virtual bool hasArrayLength() const;
221
223 virtual void typeDescriptor(const std::string &s);
224 virtual const std::string& typeDescriptor() const;
225 virtual bool hasTypeDescriptor() const;
226
228 virtual void symbolName(const std::string &s);
229 virtual const std::string& symbolName() const;
230 virtual bool hasSymbolName() const;
231
237
244
250 virtual bool isBottom() const = 0;
251
259 bool isConcrete() const /*final*/;
260
267 Sawyer::Optional<uint64_t> toUnsigned() const /*final*/;
268
275 Sawyer::Optional<int64_t> toSigned() const /*final*/;
276
282 bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
283
289 bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
290
294 bool isTrue() const /*final*/;
295
299 bool isFalse() const /*final*/;
300
311 std::string comment() const /*final*/;
312 void comment(const std::string&) const /*final*/; // const is intentional (see documentation)
318 virtual void hash(Combinatorics::Hasher&) const = 0;
319
323 void print(std::ostream&) const;
324 virtual void print(std::ostream&, Formatter&) const = 0;
331 std::string toString() const;
332
335 SValuePtr obj;
336 Formatter &fmt;
337 public:
339 void print(std::ostream&) const;
340 };
341
351 WithFormatter operator+(const std::string &linePrefix);
355 // This is the virtual interface that uses names that are not consistent with most of the rest of binary analysis. Calling
356 // these directly is deprecated and we may make them protected at some time. [Robb Matzke 2021-03-18].
358public: // for backward compatibility for now, but assume protected
360 virtual bool is_number() const = 0;
361
363 virtual uint64_t get_number() const = 0;
364
368 virtual size_t get_width() const { return width; }
369 virtual void set_width(size_t nbits) { width = nbits; }
373 virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
374
376 virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
377
383 virtual std::string get_comment() const;
384 virtual void set_comment(const std::string&) const; // const is intended; cf. doxygen comment
386};
387
388std::ostream& operator<<(std::ostream&, const SValue&);
389std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
390
391} // namespace
392} // namespace
393} // namespace
394} // namespace
395
396#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
398#endif
399
400#endif
401#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:369
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.
std::string symbolName_
Description of the type if a Reference, empty means no descriptor.
Definition SValue.h:80
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:79
bool isJvmCategory2()
Determines whether a value is JVM category-2.
virtual SValuePtr boolean_(bool value) const
Create a new, Boolean value.
Definition SValue.h:154
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.
std::string toString(ValueKind)
Prints a ValueKind to a string.
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.