Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 1 | //===-- 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 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 15 | #include "ubsan_platform.h" |
| 16 | #if CAN_SANITIZE_UB |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 17 | #include "ubsan_value.h" |
Richard Smith | ec7962d | 2013-03-22 00:47:05 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_common.h" |
| 19 | #include "sanitizer_common/sanitizer_libc.h" |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace __ubsan; |
| 22 | |
| 23 | SIntMax Value::getSIntValue() const { |
| 24 | CHECK(getType().isSignedIntegerTy()); |
| 25 | if (isInlineInt()) { |
| 26 | // Val was zero-extended to ValueHandle. Sign-extend from original width |
| 27 | // to SIntMax. |
| 28 | const unsigned ExtraBits = |
| 29 | sizeof(SIntMax) * 8 - getType().getIntegerBitWidth(); |
| 30 | return SIntMax(Val) << ExtraBits >> ExtraBits; |
| 31 | } |
| 32 | if (getType().getIntegerBitWidth() == 64) |
| 33 | return *reinterpret_cast<s64*>(Val); |
Chandler Carruth | ba3fde6 | 2012-10-13 02:30:10 +0000 | [diff] [blame] | 34 | #if HAVE_INT128_T |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 35 | if (getType().getIntegerBitWidth() == 128) |
| 36 | return *reinterpret_cast<s128*>(Val); |
Richard Smith | 0aad69a | 2012-10-14 23:53:37 +0000 | [diff] [blame] | 37 | #else |
| 38 | if (getType().getIntegerBitWidth() == 128) |
| 39 | UNREACHABLE("libclang_rt.ubsan was built without __int128 support"); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 40 | #endif |
| 41 | UNREACHABLE("unexpected bit width"); |
| 42 | } |
| 43 | |
| 44 | UIntMax Value::getUIntValue() const { |
| 45 | CHECK(getType().isUnsignedIntegerTy()); |
| 46 | if (isInlineInt()) |
| 47 | return Val; |
| 48 | if (getType().getIntegerBitWidth() == 64) |
| 49 | return *reinterpret_cast<u64*>(Val); |
Chandler Carruth | ba3fde6 | 2012-10-13 02:30:10 +0000 | [diff] [blame] | 50 | #if HAVE_INT128_T |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 51 | if (getType().getIntegerBitWidth() == 128) |
| 52 | return *reinterpret_cast<u128*>(Val); |
Richard Smith | 0aad69a | 2012-10-14 23:53:37 +0000 | [diff] [blame] | 53 | #else |
| 54 | if (getType().getIntegerBitWidth() == 128) |
| 55 | UNREACHABLE("libclang_rt.ubsan was built without __int128 support"); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 56 | #endif |
| 57 | UNREACHABLE("unexpected bit width"); |
| 58 | } |
| 59 | |
| 60 | UIntMax Value::getPositiveIntValue() const { |
| 61 | if (getType().isUnsignedIntegerTy()) |
| 62 | return getUIntValue(); |
| 63 | SIntMax Val = getSIntValue(); |
| 64 | CHECK(Val >= 0); |
| 65 | return Val; |
| 66 | } |
| 67 | |
| 68 | /// Get the floating-point value of this object, extended to a long double. |
| 69 | /// These are always passed by address (our calling convention doesn't allow |
| 70 | /// them to be passed in floating-point registers, so this has little cost). |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 71 | FloatMax Value::getFloatValue() const { |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 72 | CHECK(getType().isFloatTy()); |
Richard Smith | ec7962d | 2013-03-22 00:47:05 +0000 | [diff] [blame] | 73 | if (isInlineFloat()) { |
| 74 | switch (getType().getFloatBitWidth()) { |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 75 | #if 0 |
Richard Smith | ec7962d | 2013-03-22 00:47:05 +0000 | [diff] [blame] | 76 | // FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion |
| 77 | // from '__fp16' to 'long double'. |
| 78 | case 16: { |
| 79 | __fp16 Value; |
| 80 | internal_memcpy(&Value, &Val, 4); |
| 81 | return Value; |
| 82 | } |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 83 | #endif |
Richard Smith | ec7962d | 2013-03-22 00:47:05 +0000 | [diff] [blame] | 84 | case 32: { |
| 85 | float Value; |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 86 | #if defined(__BIG_ENDIAN__) |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 87 | // For big endian the float value is in the last 4 bytes. |
| 88 | // On some targets we may only have 4 bytes so we count backwards from |
| 89 | // the end of Val to account for both the 32-bit and 64-bit cases. |
| 90 | internal_memcpy(&Value, ((const char*)(&Val + 1)) - 4, 4); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 91 | #else |
| 92 | internal_memcpy(&Value, &Val, 4); |
| 93 | #endif |
Richard Smith | ec7962d | 2013-03-22 00:47:05 +0000 | [diff] [blame] | 94 | return Value; |
| 95 | } |
| 96 | case 64: { |
| 97 | double Value; |
| 98 | internal_memcpy(&Value, &Val, 8); |
| 99 | return Value; |
| 100 | } |
| 101 | } |
| 102 | } else { |
| 103 | switch (getType().getFloatBitWidth()) { |
| 104 | case 64: return *reinterpret_cast<double*>(Val); |
| 105 | case 80: return *reinterpret_cast<long double*>(Val); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 106 | case 96: return *reinterpret_cast<long double*>(Val); |
Richard Smith | ec7962d | 2013-03-22 00:47:05 +0000 | [diff] [blame] | 107 | case 128: return *reinterpret_cast<long double*>(Val); |
| 108 | } |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 109 | } |
| 110 | UNREACHABLE("unexpected floating point bit width"); |
| 111 | } |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 112 | |
| 113 | #endif // CAN_SANITIZE_UB |