ROSE 2.20.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
46enum class Nullness {
47 DefinitelyNull,
48 MaybeNull,
49 DefinitelyNonNull
50};
51
53std::string toString(ValueKind);
54
56std::ostream& operator<<(std::ostream&, ValueKind);
57
58
60// Semantic Values
62
78public:
80 using Ptr = SValuePtr;
81
82protected:
83 size_t width = 0;
84 ValueKind kind_ = ValueKind::Unknown;
85 Nullness nullness_ = Nullness::MaybeNull;
87 std::vector<SValuePtr> arrayDimensions_;
88 std::string typeDescriptor_;
89 std::string symbolName_;
92 // Serialization
93#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
94private:
95 friend class boost::serialization::access;
96
97 template<class S>
98 void serialize(S &s, const unsigned /*version*/) {
99 s & BOOST_SERIALIZATION_NVP(width);
100 s & BOOST_SERIALIZATION_NVP(kind_);
101 s & BOOST_SERIALIZATION_NVP(nullness_);
102 s & BOOST_SERIALIZATION_NVP(arrayLength_);
103 s & BOOST_SERIALIZATION_NVP(arrayDimensions_);
104 s & BOOST_SERIALIZATION_NVP(typeDescriptor_);
105 s & BOOST_SERIALIZATION_NVP(symbolName_);
106 }
107#endif
108
110 // Normal, protected, C++ constructors
111protected:
112 SValue() = default;
113 explicit SValue(size_t nbits);
114
115 SValue(const SValue&);
116 SValue& operator=(const SValue&);
117
118public:
119 virtual ~SValue();
120
122 // Allocating static constructor. None are needed--this class is abstract.
123
125 // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
126 // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
127 // different: the object (this) is used for more than just selecting which virtual method to invoke.
128 //
129 // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
130 // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
131 // other difference (at least in this base class) is that we don't define any real constructors or static allocating
132 // constructors (usually named "instance")--it's because this is an abstract class.
133public:
139 virtual SValuePtr undefined_(size_t nbits) const = 0;
140
149 virtual SValuePtr unspecified_(size_t nbits) const = 0;
150
156 virtual SValuePtr bottom_(size_t nBits) const = 0;
157
161 virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0;
162
166 virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
167
171 virtual SValuePtr copy(size_t new_width=0) const = 0;
172
200 createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
201
208 SValuePtr createMerged(const SValuePtr &other, const MergerPtr&, const SmtSolverPtr&) const /*final*/;
209
211 // Dynamic pointer casts. No-ops since this is the base class
212public:
213 static SValuePtr promote(const SValuePtr&);
214
216 // The rest of the API...
217public:
223 size_t nBits() const /*final*/;
224
226 virtual ValueKind kind() const;
227 virtual void kind(ValueKind k);
228
230 virtual void arrayLength(const SValuePtr &sval);
231 virtual SValuePtr arrayLength() const;
232 virtual bool hasArrayLength() const;
233
235 virtual void arrayDimensions(const std::vector<SValuePtr> &dims);
236 virtual const std::vector<SValuePtr>& arrayDimensions() const;
237
239 virtual void typeDescriptor(const std::string &s);
240 virtual const std::string& typeDescriptor() const;
241 virtual bool hasTypeDescriptor() const;
242
244 virtual void symbolName(const std::string &s);
245 virtual const std::string& symbolName() const;
246 virtual bool hasSymbolName() const;
247
253
260
266 virtual bool isBottom() const = 0;
267
275 bool isConcrete() const /*final*/;
276
283 Sawyer::Optional<uint64_t> toUnsigned() const /*final*/;
284
291 Sawyer::Optional<int64_t> toSigned() const /*final*/;
292
298 bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
299
305 bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
306
310 bool isTrue() const /*final*/;
311
315 bool isFalse() const /*final*/;
316
322 void nullness(Nullness value);
323
334 std::string comment() const /*final*/;
335 void comment(const std::string&) const /*final*/; // const is intentional (see documentation)
341 virtual void hash(Combinatorics::Hasher&) const = 0;
342
346 void print(std::ostream&) const;
347 virtual void print(std::ostream&, Formatter&) const = 0;
354 std::string toString() const;
355
358 SValuePtr obj;
359 Formatter &fmt;
360 public:
362 void print(std::ostream&) const;
363 };
364
374 WithFormatter operator+(const std::string &linePrefix);
378 // This is the virtual interface that uses names that are not consistent with most of the rest of binary analysis. Calling
379 // these directly is deprecated and we may make them protected at some time. [Robb Matzke 2021-03-18].
381public: // for backward compatibility for now, but assume protected
383 virtual bool is_number() const = 0;
384
386 virtual uint64_t get_number() const = 0;
387
391 virtual size_t get_width() const { return width; }
392 virtual void set_width(size_t nbits) { width = nbits; }
396 virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
397
399 virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
400
406 virtual std::string get_comment() const;
407 virtual void set_comment(const std::string&) const; // const is intended; cf. doxygen comment
409};
410
411std::ostream& operator<<(std::ostream&, const SValue&);
412std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
413
414} // namespace
415} // namespace
416} // namespace
417} // namespace
418
419#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
421#endif
422
423#endif
424#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:392
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:89
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.
SValuePtr arrayLength_
Nullness state for reference-like values.
Definition SValue.h:86
std::string toString() const
Render this symbolic expression as a string.
bool isJvmCategory1()
Determines whether a value is JVM category-1.
std::string typeDescriptor_
Lengths of allocated nested dimensions below this array.
Definition SValue.h:88
std::vector< SValuePtr > arrayDimensions_
Length of this array object.
Definition SValue.h:87
bool isJvmCategory2()
Determines whether a value is JVM category-2.
virtual SValuePtr boolean_(bool value) const
Create a new, Boolean value.
Definition SValue.h:166
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.
Nullness
Nullness state of a reference value.
Definition SValue.h:46
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.