1 //===-- ubsan_value.h -------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Representation of data which is passed from the compiler-generated calls into
11 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_atomic.h"
16 #include "sanitizer_common/sanitizer_common.h"
18 // FIXME: Move this out to a config header.
20 __extension__
typedef __int128 s128
;
21 __extension__
typedef unsigned __int128 u128
;
22 #define HAVE_INT128_T 1
24 #define HAVE_INT128_T 0
29 /// \brief Largest integer types we support.
38 /// \brief Largest floating-point type we support.
39 typedef long double FloatMax
;
41 /// \brief A description of a source location. This corresponds to Clang's
42 /// \c PresumedLoc type.
43 class SourceLocation
{
49 SourceLocation() : Filename(), Line(), Column() {}
50 SourceLocation(const char *Filename
, unsigned Line
, unsigned Column
)
51 : Filename(Filename
), Line(Line
), Column(Column
) {}
53 /// \brief Determine whether the source location is known.
54 bool isInvalid() const { return !Filename
; }
56 /// \brief Atomically acquire a copy, disabling original in-place.
57 /// Exactly one call to acquire() returns a copy that isn't disabled.
58 SourceLocation
acquire() {
59 u32 OldColumn
= __sanitizer::atomic_exchange(
60 (__sanitizer::atomic_uint32_t
*)&Column
, ~u32(0),
61 __sanitizer::memory_order_relaxed
);
62 return SourceLocation(Filename
, Line
, OldColumn
);
65 /// \brief Determine if this Location has been disabled.
66 /// Disabled SourceLocations are invalid to use.
68 return Column
== ~u32(0);
71 /// \brief Get the presumed filename for the source location.
72 const char *getFilename() const { return Filename
; }
73 /// \brief Get the presumed line number.
74 unsigned getLine() const { return Line
; }
75 /// \brief Get the column within the presumed line.
76 unsigned getColumn() const { return Column
; }
80 /// \brief A description of a type.
81 class TypeDescriptor
{
82 /// A value from the \c Kind enumeration, specifying what flavor of type we
86 /// A \c Type-specific value providing information which allows us to
87 /// interpret the meaning of a ValueHandle of this type.
90 /// The name of the type follows, in a format suitable for including in
96 /// An integer type. Lowest bit is 1 for a signed value, 0 for an unsigned
97 /// value. Remaining bits are log_2(bit width). The value representation is
98 /// the integer itself if it fits into a ValueHandle, and a pointer to the
99 /// integer otherwise.
101 /// A floating-point type. Low 16 bits are bit width. The value
102 /// representation is that of bitcasting the floating-point value to an
105 /// Any other type. The value representation is unspecified.
109 const char *getTypeName() const { return TypeName
; }
111 Kind
getKind() const {
112 return static_cast<Kind
>(TypeKind
);
115 bool isIntegerTy() const { return getKind() == TK_Integer
; }
116 bool isSignedIntegerTy() const {
117 return isIntegerTy() && (TypeInfo
& 1);
119 bool isUnsignedIntegerTy() const {
120 return isIntegerTy() && !(TypeInfo
& 1);
122 unsigned getIntegerBitWidth() const {
123 CHECK(isIntegerTy());
124 return 1 << (TypeInfo
>> 1);
127 bool isFloatTy() const { return getKind() == TK_Float
; }
128 unsigned getFloatBitWidth() const {
134 /// \brief An opaque handle to a value.
135 typedef uptr ValueHandle
;
138 /// \brief Representation of an operand value provided by the instrumented code.
140 /// This is a combination of a TypeDescriptor (which is emitted as constant data
141 /// as an operand to a handler function) and a ValueHandle (which is passed at
142 /// runtime when a check failure occurs).
144 /// The type of the value.
145 const TypeDescriptor
&Type
;
146 /// The encoded value itself.
149 /// Is \c Val a (zero-extended) integer?
150 bool isInlineInt() const {
151 CHECK(getType().isIntegerTy());
152 const unsigned InlineBits
= sizeof(ValueHandle
) * 8;
153 const unsigned Bits
= getType().getIntegerBitWidth();
154 return Bits
<= InlineBits
;
157 /// Is \c Val a (zero-extended) integer representation of a float?
158 bool isInlineFloat() const {
159 CHECK(getType().isFloatTy());
160 const unsigned InlineBits
= sizeof(ValueHandle
) * 8;
161 const unsigned Bits
= getType().getFloatBitWidth();
162 return Bits
<= InlineBits
;
166 Value(const TypeDescriptor
&Type
, ValueHandle Val
) : Type(Type
), Val(Val
) {}
168 const TypeDescriptor
&getType() const { return Type
; }
170 /// \brief Get this value as a signed integer.
171 SIntMax
getSIntValue() const;
173 /// \brief Get this value as an unsigned integer.
174 UIntMax
getUIntValue() const;
176 /// \brief Decode this value, which must be a positive or unsigned integer.
177 UIntMax
getPositiveIntValue() const;
179 /// Is this an integer with value -1?
180 bool isMinusOne() const {
181 return getType().isSignedIntegerTy() && getSIntValue() == -1;
184 /// Is this a negative integer?
185 bool isNegative() const {
186 return getType().isSignedIntegerTy() && getSIntValue() < 0;
189 /// \brief Get this value as a floating-point quantity.
190 FloatMax
getFloatValue() const;
193 } // namespace __ubsan
195 #endif // UBSAN_VALUE_H