ubsan_value.h revision 6ebe45146a2d93eb010b9bb5ea34cb94c6900f83
1//===-- ubsan_value.h -------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Representation of data which is passed from the compiler-generated calls into
11// the ubsan runtime.
12//
13//===----------------------------------------------------------------------===//
14#ifndef UBSAN_VALUE_H
15#define UBSAN_VALUE_H
16
17// For now, only support linux. Other platforms should be easy to add, and
18// probably work as-is.
19#if !defined(__linux__)
20#error "UBSan not supported for this platform!"
21#endif
22
23#include "sanitizer_common/sanitizer_common.h"
24
25// FIXME: Move this out to a config header.
26typedef __int128 s128;
27typedef unsigned __int128 u128;
28#define HAVE_INT128_T 1
29
30
31namespace __ubsan {
32
33/// \brief Largest integer types we support.
34#ifdef HAVE_INT128_T
35typedef s128 SIntMax;
36typedef u128 UIntMax;
37#else
38typedef s64 SIntMax;
39typedef u64 UIntMax;
40#endif
41
42
43/// \brief A description of a source location. This corresponds to Clang's
44/// \c PresumedLoc type.
45class SourceLocation {
46  const char *Filename;
47  u32 Line;
48  u32 Column;
49
50public:
51  SourceLocation(const char *Filename, unsigned Line, unsigned Column)
52    : Filename(Filename), Line(Line), Column(Column) {}
53
54  /// \brief Determine whether the source location is known.
55  bool isInvalid() const { return !Filename; }
56
57  /// \brief Get the presumed filename for the source location.
58  const char *getFilename() const { return Filename; }
59  /// \brief Get the presumed line number.
60  unsigned getLine() const { return Line; }
61  /// \brief Get the column within the presumed line.
62  unsigned getColumn() const { return Column; }
63};
64
65
66/// \brief A description of a type.
67class TypeDescriptor {
68  /// The name of the type, in a format suitable for including in diagnostics.
69  const char *TypeName;
70
71  /// A value from the \c Kind enumeration, specifying what flavor of type we
72  /// have.
73  u16 TypeKind;
74
75  /// A \c Type-specific value providing information which allows us to
76  /// interpret the meaning of a ValueHandle of this type.
77  u16 TypeInfo;
78
79public:
80  enum Kind {
81    /// An integer type. Lowest bit is 1 for a signed value, 0 for an unsigned
82    /// value. Remaining bits are log_2(bit width). The value representation is
83    /// the integer itself if it fits into a ValueHandle, and a pointer to the
84    /// integer otherwise.
85    TK_Integer = 0x0000,
86    /// A floating-point type. Low 16 bits are bit width. The value
87    /// representation is a pointer to the floating-point value.
88    TK_Float = 0x0001,
89    /// Any other type. The value representation is unspecified.
90    TK_Unknown = 0xffff
91  };
92
93  const char *getTypeName() const { return TypeName; }
94
95  Kind getKind() const {
96    return static_cast<Kind>(TypeKind);
97  }
98
99  bool isIntegerTy() const { return getKind() == TK_Integer; }
100  bool isSignedIntegerTy() const {
101    return isIntegerTy() && (TypeInfo & 1);
102  }
103  bool isUnsignedIntegerTy() const {
104    return isIntegerTy() && !(TypeInfo & 1);
105  }
106  unsigned getIntegerBitWidth() const {
107    CHECK(isIntegerTy());
108    return 1 << (TypeInfo >> 1);
109  }
110
111  bool isFloatTy() const { return getKind() == TK_Float; }
112  unsigned getFloatBitWidth() const {
113    CHECK(isFloatTy());
114    return TypeInfo;
115  }
116};
117
118/// \brief An opaque handle to a value.
119typedef uptr ValueHandle;
120
121
122/// \brief Representation of an operand value provided by the instrumented code.
123///
124/// This is a combination of a TypeDescriptor (which is emitted as constant data
125/// as an operand to a handler function) and a ValueHandle (which is passed at
126/// runtime when a check failure occurs).
127class Value {
128  /// The type of the value.
129  const TypeDescriptor &Type;
130  /// The encoded value itself.
131  ValueHandle Val;
132
133  /// Is \c Val a (zero-extended) integer?
134  bool isInlineInt() const {
135    CHECK(getType().isIntegerTy());
136    const unsigned InlineBits = sizeof(ValueHandle) * 8;
137    const unsigned Bits = getType().getIntegerBitWidth();
138    return Bits <= InlineBits;
139  }
140
141public:
142  Value(const TypeDescriptor &Type, ValueHandle Val) : Type(Type), Val(Val) {}
143
144  const TypeDescriptor &getType() const { return Type; }
145
146  /// \brief Get this value as a signed integer.
147  SIntMax getSIntValue() const;
148
149  /// \brief Get this value as an unsigned integer.
150  UIntMax getUIntValue() const;
151
152  /// \brief Decode this value, which must be a positive or unsigned integer.
153  UIntMax getPositiveIntValue() const;
154
155  /// Is this an integer with value -1?
156  bool isMinusOne() const {
157    return getType().isSignedIntegerTy() && getSIntValue() == -1;
158  }
159
160  /// Is this a negative integer?
161  bool isNegative() const {
162    return getType().isSignedIntegerTy() && getSIntValue() < 0;
163  }
164
165  /// \brief Get this value as a floating-point quantity.
166  long double getFloatValue() const;
167};
168
169} // namespace __ubsan
170
171#endif // UBSAN_VALUE_H
172