]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_value.cc
Unbreak DRM KMS build by adding the needed compatibility field in the LinuxKPI.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / ubsan / ubsan_value.cc
1 //===-- ubsan_value.cc ----------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Representation of a runtime value, as marshaled from the generated code to
10 // the ubsan runtime.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ubsan_platform.h"
15 #if CAN_SANITIZE_UB
16 #include "ubsan_value.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_libc.h"
19
20 using namespace __ubsan;
21
22 SIntMax Value::getSIntValue() const {
23   CHECK(getType().isSignedIntegerTy());
24   if (isInlineInt()) {
25     // Val was zero-extended to ValueHandle. Sign-extend from original width
26     // to SIntMax.
27     const unsigned ExtraBits =
28       sizeof(SIntMax) * 8 - getType().getIntegerBitWidth();
29     return SIntMax(Val) << ExtraBits >> ExtraBits;
30   }
31   if (getType().getIntegerBitWidth() == 64)
32     return *reinterpret_cast<s64*>(Val);
33 #if HAVE_INT128_T
34   if (getType().getIntegerBitWidth() == 128)
35     return *reinterpret_cast<s128*>(Val);
36 #else
37   if (getType().getIntegerBitWidth() == 128)
38     UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
39 #endif
40   UNREACHABLE("unexpected bit width");
41 }
42
43 UIntMax Value::getUIntValue() const {
44   CHECK(getType().isUnsignedIntegerTy());
45   if (isInlineInt())
46     return Val;
47   if (getType().getIntegerBitWidth() == 64)
48     return *reinterpret_cast<u64*>(Val);
49 #if HAVE_INT128_T
50   if (getType().getIntegerBitWidth() == 128)
51     return *reinterpret_cast<u128*>(Val);
52 #else
53   if (getType().getIntegerBitWidth() == 128)
54     UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
55 #endif
56   UNREACHABLE("unexpected bit width");
57 }
58
59 UIntMax Value::getPositiveIntValue() const {
60   if (getType().isUnsignedIntegerTy())
61     return getUIntValue();
62   SIntMax Val = getSIntValue();
63   CHECK(Val >= 0);
64   return Val;
65 }
66
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()) {
74 #if 0
75       // FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion
76       //        from '__fp16' to 'long double'.
77       case 16: {
78         __fp16 Value;
79         internal_memcpy(&Value, &Val, 4);
80         return Value;
81       }
82 #endif
83       case 32: {
84         float Value;
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);
90 #else
91        internal_memcpy(&Value, &Val, 4);
92 #endif
93         return Value;
94       }
95       case 64: {
96         double Value;
97         internal_memcpy(&Value, &Val, 8);
98         return Value;
99       }
100     }
101   } else {
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);
107     }
108   }
109   UNREACHABLE("unexpected floating point bit width");
110 }
111
112 #endif  // CAN_SANITIZE_UB