1 //===-- ubsan_value.cc ----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Representation of a runtime value, as marshaled from the generated code to
11 //===----------------------------------------------------------------------===//
13 #include "ubsan_platform.h"
15 #include "ubsan_value.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_libc.h"
19 using namespace __ubsan
;
21 SIntMax
Value::getSIntValue() const {
22 CHECK(getType().isSignedIntegerTy());
24 // Val was zero-extended to ValueHandle. Sign-extend from original width
26 const unsigned ExtraBits
=
27 sizeof(SIntMax
) * 8 - getType().getIntegerBitWidth();
28 return SIntMax(Val
) << ExtraBits
>> ExtraBits
;
30 if (getType().getIntegerBitWidth() == 64)
31 return *reinterpret_cast<s64
*>(Val
);
33 if (getType().getIntegerBitWidth() == 128)
34 return *reinterpret_cast<s128
*>(Val
);
36 if (getType().getIntegerBitWidth() == 128)
37 UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
39 UNREACHABLE("unexpected bit width");
42 UIntMax
Value::getUIntValue() const {
43 CHECK(getType().isUnsignedIntegerTy());
46 if (getType().getIntegerBitWidth() == 64)
47 return *reinterpret_cast<u64
*>(Val
);
49 if (getType().getIntegerBitWidth() == 128)
50 return *reinterpret_cast<u128
*>(Val
);
52 if (getType().getIntegerBitWidth() == 128)
53 UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
55 UNREACHABLE("unexpected bit width");
58 UIntMax
Value::getPositiveIntValue() const {
59 if (getType().isUnsignedIntegerTy())
60 return getUIntValue();
61 SIntMax Val
= getSIntValue();
66 /// Get the floating-point value of this object, extended to a long double.
67 /// These are always passed by address (our calling convention doesn't allow
68 /// them to be passed in floating-point registers, so this has little cost).
69 FloatMax
Value::getFloatValue() const {
70 CHECK(getType().isFloatTy());
71 if (isInlineFloat()) {
72 switch (getType().getFloatBitWidth()) {
74 // FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion
75 // from '__fp16' to 'long double'.
78 internal_memcpy(&Value
, &Val
, 4);
84 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
85 // For big endian the float value is in the last 4 bytes.
86 // On some targets we may only have 4 bytes so we count backwards from
87 // the end of Val to account for both the 32-bit and 64-bit cases.
88 internal_memcpy(&Value
, ((const char*)(&Val
+ 1)) - 4, 4);
90 internal_memcpy(&Value
, &Val
, 4);
96 internal_memcpy(&Value
, &Val
, 8);
101 switch (getType().getFloatBitWidth()) {
102 case 64: return *reinterpret_cast<double*>(Val
);
103 case 80: return *reinterpret_cast<long double*>(Val
);
104 case 96: return *reinterpret_cast<long double*>(Val
);
105 case 128: return *reinterpret_cast<long double*>(Val
);
108 UNREACHABLE("unexpected floating point bit width");
111 #endif // CAN_SANITIZE_UB