blob: ab74720f60cf104a199592a259c4b5528e233b4f [file] [log] [blame]
Richard Smith6ebe4512012-10-09 19:34:32 +00001//===-- ubsan_value.cc ----------------------------------------------------===//
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 a runtime value, as marshaled from the generated code to
11// the ubsan runtime.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ubsan_value.h"
Richard Smithec7962d2013-03-22 00:47:05 +000016#include "sanitizer_common/sanitizer_common.h"
17#include "sanitizer_common/sanitizer_libc.h"
Richard Smith6ebe4512012-10-09 19:34:32 +000018
19using namespace __ubsan;
20
21SIntMax Value::getSIntValue() const {
22 CHECK(getType().isSignedIntegerTy());
23 if (isInlineInt()) {
24 // Val was zero-extended to ValueHandle. Sign-extend from original width
25 // to SIntMax.
26 const unsigned ExtraBits =
27 sizeof(SIntMax) * 8 - getType().getIntegerBitWidth();
28 return SIntMax(Val) << ExtraBits >> ExtraBits;
29 }
30 if (getType().getIntegerBitWidth() == 64)
31 return *reinterpret_cast<s64*>(Val);
Chandler Carruthba3fde62012-10-13 02:30:10 +000032#if HAVE_INT128_T
Richard Smith6ebe4512012-10-09 19:34:32 +000033 if (getType().getIntegerBitWidth() == 128)
34 return *reinterpret_cast<s128*>(Val);
Richard Smith0aad69a2012-10-14 23:53:37 +000035#else
36 if (getType().getIntegerBitWidth() == 128)
37 UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
Richard Smith6ebe4512012-10-09 19:34:32 +000038#endif
39 UNREACHABLE("unexpected bit width");
40}
41
42UIntMax Value::getUIntValue() const {
43 CHECK(getType().isUnsignedIntegerTy());
44 if (isInlineInt())
45 return Val;
46 if (getType().getIntegerBitWidth() == 64)
47 return *reinterpret_cast<u64*>(Val);
Chandler Carruthba3fde62012-10-13 02:30:10 +000048#if HAVE_INT128_T
Richard Smith6ebe4512012-10-09 19:34:32 +000049 if (getType().getIntegerBitWidth() == 128)
50 return *reinterpret_cast<u128*>(Val);
Richard Smith0aad69a2012-10-14 23:53:37 +000051#else
52 if (getType().getIntegerBitWidth() == 128)
53 UNREACHABLE("libclang_rt.ubsan was built without __int128 support");
Richard Smith6ebe4512012-10-09 19:34:32 +000054#endif
55 UNREACHABLE("unexpected bit width");
56}
57
58UIntMax Value::getPositiveIntValue() const {
59 if (getType().isUnsignedIntegerTy())
60 return getUIntValue();
61 SIntMax Val = getSIntValue();
62 CHECK(Val >= 0);
63 return Val;
64}
65
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).
Richard Smith58561702012-10-12 22:57:15 +000069FloatMax Value::getFloatValue() const {
Richard Smith6ebe4512012-10-09 19:34:32 +000070 CHECK(getType().isFloatTy());
Richard Smithec7962d2013-03-22 00:47:05 +000071 if (isInlineFloat()) {
72 switch (getType().getFloatBitWidth()) {
Richard Smith6ebe4512012-10-09 19:34:32 +000073#if 0
Richard Smithec7962d2013-03-22 00:47:05 +000074 // FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion
75 // from '__fp16' to 'long double'.
76 case 16: {
77 __fp16 Value;
78 internal_memcpy(&Value, &Val, 4);
79 return Value;
80 }
Richard Smith6ebe4512012-10-09 19:34:32 +000081#endif
Richard Smithec7962d2013-03-22 00:47:05 +000082 case 32: {
83 float Value;
84 internal_memcpy(&Value, &Val, 4);
85 return Value;
86 }
87 case 64: {
88 double Value;
89 internal_memcpy(&Value, &Val, 8);
90 return Value;
91 }
92 }
93 } else {
94 switch (getType().getFloatBitWidth()) {
95 case 64: return *reinterpret_cast<double*>(Val);
96 case 80: return *reinterpret_cast<long double*>(Val);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070097 case 96: return *reinterpret_cast<long double*>(Val);
Richard Smithec7962d2013-03-22 00:47:05 +000098 case 128: return *reinterpret_cast<long double*>(Val);
99 }
Richard Smith6ebe4512012-10-09 19:34:32 +0000100 }
101 UNREACHABLE("unexpected floating point bit width");
102}