1*7c3d14c8STreehugger Robot //===-- ubsan_value.cc ----------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // Representation of a runtime value, as marshaled from the generated code to
11*7c3d14c8STreehugger Robot // the ubsan runtime.
12*7c3d14c8STreehugger Robot //
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot #include "ubsan_platform.h"
16*7c3d14c8STreehugger Robot #if CAN_SANITIZE_UB
17*7c3d14c8STreehugger Robot #include "ubsan_value.h"
18*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
19*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_libc.h"
20*7c3d14c8STreehugger Robot
21*7c3d14c8STreehugger Robot using namespace __ubsan;
22*7c3d14c8STreehugger Robot
getSIntValue() const23*7c3d14c8STreehugger Robot SIntMax Value::getSIntValue() const {
24*7c3d14c8STreehugger Robot CHECK(getType().isSignedIntegerTy());
25*7c3d14c8STreehugger Robot if (isInlineInt()) {
26*7c3d14c8STreehugger Robot // Val was zero-extended to ValueHandle. Sign-extend from original width
27*7c3d14c8STreehugger Robot // to SIntMax.
28*7c3d14c8STreehugger Robot const unsigned ExtraBits =
29*7c3d14c8STreehugger Robot sizeof(SIntMax) * 8 - getType().getIntegerBitWidth();
30*7c3d14c8STreehugger Robot return SIntMax(Val) << ExtraBits >> ExtraBits;
31*7c3d14c8STreehugger Robot }
32*7c3d14c8STreehugger Robot if (getType().getIntegerBitWidth() == 64)
33*7c3d14c8STreehugger Robot return *reinterpret_cast<s64*>(Val);
34*7c3d14c8STreehugger Robot #if HAVE_INT128_T
35*7c3d14c8STreehugger Robot if (getType().getIntegerBitWidth() == 128)
36*7c3d14c8STreehugger Robot return *reinterpret_cast<s128*>(Val);
37*7c3d14c8STreehugger Robot #else
38*7c3d14c8STreehugger Robot if (getType().getIntegerBitWidth() == 128)
39*7c3d14c8STreehugger Robot UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
40*7c3d14c8STreehugger Robot #endif
41*7c3d14c8STreehugger Robot UNREACHABLE("unexpected bit width");
42*7c3d14c8STreehugger Robot }
43*7c3d14c8STreehugger Robot
getUIntValue() const44*7c3d14c8STreehugger Robot UIntMax Value::getUIntValue() const {
45*7c3d14c8STreehugger Robot CHECK(getType().isUnsignedIntegerTy());
46*7c3d14c8STreehugger Robot if (isInlineInt())
47*7c3d14c8STreehugger Robot return Val;
48*7c3d14c8STreehugger Robot if (getType().getIntegerBitWidth() == 64)
49*7c3d14c8STreehugger Robot return *reinterpret_cast<u64*>(Val);
50*7c3d14c8STreehugger Robot #if HAVE_INT128_T
51*7c3d14c8STreehugger Robot if (getType().getIntegerBitWidth() == 128)
52*7c3d14c8STreehugger Robot return *reinterpret_cast<u128*>(Val);
53*7c3d14c8STreehugger Robot #else
54*7c3d14c8STreehugger Robot if (getType().getIntegerBitWidth() == 128)
55*7c3d14c8STreehugger Robot UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
56*7c3d14c8STreehugger Robot #endif
57*7c3d14c8STreehugger Robot UNREACHABLE("unexpected bit width");
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot
getPositiveIntValue() const60*7c3d14c8STreehugger Robot UIntMax Value::getPositiveIntValue() const {
61*7c3d14c8STreehugger Robot if (getType().isUnsignedIntegerTy())
62*7c3d14c8STreehugger Robot return getUIntValue();
63*7c3d14c8STreehugger Robot SIntMax Val = getSIntValue();
64*7c3d14c8STreehugger Robot CHECK(Val >= 0);
65*7c3d14c8STreehugger Robot return Val;
66*7c3d14c8STreehugger Robot }
67*7c3d14c8STreehugger Robot
68*7c3d14c8STreehugger Robot /// Get the floating-point value of this object, extended to a long double.
69*7c3d14c8STreehugger Robot /// These are always passed by address (our calling convention doesn't allow
70*7c3d14c8STreehugger Robot /// them to be passed in floating-point registers, so this has little cost).
getFloatValue() const71*7c3d14c8STreehugger Robot FloatMax Value::getFloatValue() const {
72*7c3d14c8STreehugger Robot CHECK(getType().isFloatTy());
73*7c3d14c8STreehugger Robot if (isInlineFloat()) {
74*7c3d14c8STreehugger Robot switch (getType().getFloatBitWidth()) {
75*7c3d14c8STreehugger Robot #if 0
76*7c3d14c8STreehugger Robot // FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion
77*7c3d14c8STreehugger Robot // from '__fp16' to 'long double'.
78*7c3d14c8STreehugger Robot case 16: {
79*7c3d14c8STreehugger Robot __fp16 Value;
80*7c3d14c8STreehugger Robot internal_memcpy(&Value, &Val, 4);
81*7c3d14c8STreehugger Robot return Value;
82*7c3d14c8STreehugger Robot }
83*7c3d14c8STreehugger Robot #endif
84*7c3d14c8STreehugger Robot case 32: {
85*7c3d14c8STreehugger Robot float Value;
86*7c3d14c8STreehugger Robot #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
87*7c3d14c8STreehugger Robot // For big endian the float value is in the last 4 bytes.
88*7c3d14c8STreehugger Robot // On some targets we may only have 4 bytes so we count backwards from
89*7c3d14c8STreehugger Robot // the end of Val to account for both the 32-bit and 64-bit cases.
90*7c3d14c8STreehugger Robot internal_memcpy(&Value, ((const char*)(&Val + 1)) - 4, 4);
91*7c3d14c8STreehugger Robot #else
92*7c3d14c8STreehugger Robot internal_memcpy(&Value, &Val, 4);
93*7c3d14c8STreehugger Robot #endif
94*7c3d14c8STreehugger Robot return Value;
95*7c3d14c8STreehugger Robot }
96*7c3d14c8STreehugger Robot case 64: {
97*7c3d14c8STreehugger Robot double Value;
98*7c3d14c8STreehugger Robot internal_memcpy(&Value, &Val, 8);
99*7c3d14c8STreehugger Robot return Value;
100*7c3d14c8STreehugger Robot }
101*7c3d14c8STreehugger Robot }
102*7c3d14c8STreehugger Robot } else {
103*7c3d14c8STreehugger Robot switch (getType().getFloatBitWidth()) {
104*7c3d14c8STreehugger Robot case 64: return *reinterpret_cast<double*>(Val);
105*7c3d14c8STreehugger Robot case 80: return *reinterpret_cast<long double*>(Val);
106*7c3d14c8STreehugger Robot case 96: return *reinterpret_cast<long double*>(Val);
107*7c3d14c8STreehugger Robot case 128: return *reinterpret_cast<long double*>(Val);
108*7c3d14c8STreehugger Robot }
109*7c3d14c8STreehugger Robot }
110*7c3d14c8STreehugger Robot UNREACHABLE("unexpected floating point bit width");
111*7c3d14c8STreehugger Robot }
112*7c3d14c8STreehugger Robot
113*7c3d14c8STreehugger Robot #endif // CAN_SANITIZE_UB
114