1 //===-- ubsan_value.cc ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Representation of a runtime value, as marshaled from the generated code to
12 //===----------------------------------------------------------------------===//
14 #include "ubsan_platform.h"
16 #include "ubsan_value.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_libc.h"
20 using namespace __ubsan;
22 SIntMax Value::getSIntValue() const {
23 CHECK(getType().isSignedIntegerTy());
25 // Val was zero-extended to ValueHandle. Sign-extend from original width
27 const unsigned ExtraBits =
28 sizeof(SIntMax) * 8 - getType().getIntegerBitWidth();
29 return SIntMax(Val) << ExtraBits >> ExtraBits;
31 if (getType().getIntegerBitWidth() == 64)
32 return *reinterpret_cast<s64*>(Val);
34 if (getType().getIntegerBitWidth() == 128)
35 return *reinterpret_cast<s128*>(Val);
37 if (getType().getIntegerBitWidth() == 128)
38 UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
40 UNREACHABLE("unexpected bit width");
43 UIntMax Value::getUIntValue() const {
44 CHECK(getType().isUnsignedIntegerTy());
47 if (getType().getIntegerBitWidth() == 64)
48 return *reinterpret_cast<u64*>(Val);
50 if (getType().getIntegerBitWidth() == 128)
51 return *reinterpret_cast<u128*>(Val);
53 if (getType().getIntegerBitWidth() == 128)
54 UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
56 UNREACHABLE("unexpected bit width");
59 UIntMax Value::getPositiveIntValue() const {
60 if (getType().isUnsignedIntegerTy())
61 return getUIntValue();
62 SIntMax Val = getSIntValue();
67 /// Get the floating-point value of this object, extended to a long double.
68 /// These are always passed by address (our calling convention doesn't allow
69 /// them to be passed in floating-point registers, so this has little cost).
70 FloatMax Value::getFloatValue() const {
71 CHECK(getType().isFloatTy());
72 if (isInlineFloat()) {
73 switch (getType().getFloatBitWidth()) {
75 // FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion
76 // from '__fp16' to 'long double'.
79 internal_memcpy(&Value, &Val, 4);
85 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
86 // For big endian the float value is in the last 4 bytes.
87 // On some targets we may only have 4 bytes so we count backwards from
88 // the end of Val to account for both the 32-bit and 64-bit cases.
89 internal_memcpy(&Value, ((const char*)(&Val + 1)) - 4, 4);
91 internal_memcpy(&Value, &Val, 4);
97 internal_memcpy(&Value, &Val, 8);
102 switch (getType().getFloatBitWidth()) {
103 case 64: return *reinterpret_cast<double*>(Val);
104 case 80: return *reinterpret_cast<long double*>(Val);
105 case 96: return *reinterpret_cast<long double*>(Val);
106 case 128: return *reinterpret_cast<long double*>(Val);
109 UNREACHABLE("unexpected floating point bit width");
112 #endif // CAN_SANITIZE_UB