1 // RUN: %clangxx -fsanitize=float-cast-overflow %s -o %t 2 // RUN: %run %t _ 3 // RUN: %env_ubsan_opts=print_summary=1:report_error_type=1 %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-0 4 // RUN: %run %t 1 2>&1 | FileCheck %s --check-prefix=CHECK-1 5 // RUN: %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-2 6 // RUN: %run %t 3 2>&1 | FileCheck %s --check-prefix=CHECK-3 7 // RUN: %run %t 4 2>&1 | FileCheck %s --check-prefix=CHECK-4 8 // RUN: %run %t 5 2>&1 | FileCheck %s --check-prefix=CHECK-5 9 // RUN: %run %t 6 2>&1 | FileCheck %s --check-prefix=CHECK-6 10 // FIXME: %run %t 7 2>&1 | FileCheck %s --check-prefix=CHECK-7 11 // FIXME: not %run %t 8 2>&1 | FileCheck %s --check-prefix=CHECK-8 12 // RUN: not %run %t 9 2>&1 | FileCheck %s --check-prefix=CHECK-9 13 14 // This test assumes float and double are IEEE-754 single- and double-precision. 15 16 #if defined(__APPLE__) 17 # include <machine/endian.h> 18 # define BYTE_ORDER __DARWIN_BYTE_ORDER 19 # define BIG_ENDIAN __DARWIN_BIG_ENDIAN 20 # define LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN 21 #elif defined(__FreeBSD__) || defined(__NetBSD__) 22 # include <sys/endian.h> 23 # ifndef BYTE_ORDER 24 # define BYTE_ORDER _BYTE_ORDER 25 # endif 26 # ifndef BIG_ENDIAN 27 # define BIG_ENDIAN _BIG_ENDIAN 28 # endif 29 # ifndef LITTLE_ENDIAN 30 # define LITTLE_ENDIAN _LITTLE_ENDIAN 31 # endif 32 #elif defined(_WIN32) 33 # define BYTE_ORDER 0 34 # define BIG_ENDIAN 1 35 # define LITTLE_ENDIAN 0 36 #else 37 # include <endian.h> 38 # define BYTE_ORDER __BYTE_ORDER 39 # define BIG_ENDIAN __BIG_ENDIAN 40 # define LITTLE_ENDIAN __LITTLE_ENDIAN 41 #endif // __APPLE__ 42 #include <stdint.h> 43 #include <stdio.h> 44 #include <string.h> 45 46 float Inf; 47 float NaN; 48 49 int main(int argc, char **argv) { 50 float MaxFloatRepresentableAsInt = 0x7fffff80; 51 (int)MaxFloatRepresentableAsInt; // ok 52 (int)-MaxFloatRepresentableAsInt; // ok 53 54 float MinFloatRepresentableAsInt = -0x7fffffff - 1; 55 (int)MinFloatRepresentableAsInt; // ok 56 57 float MaxFloatRepresentableAsUInt = 0xffffff00u; 58 (unsigned int)MaxFloatRepresentableAsUInt; // ok 59 60 #ifdef __SIZEOF_INT128__ 61 unsigned __int128 FloatMaxAsUInt128 = -((unsigned __int128)1 << 104); 62 (void)(float)FloatMaxAsUInt128; // ok 63 #endif 64 65 float NearlyMinusOne = -0.99999; 66 unsigned Zero = NearlyMinusOne; // ok 67 68 // Build a '+Inf'. 69 #if BYTE_ORDER == LITTLE_ENDIAN 70 unsigned char InfVal[] = { 0x00, 0x00, 0x80, 0x7f }; 71 #else 72 unsigned char InfVal[] = { 0x7f, 0x80, 0x00, 0x00 }; 73 #endif 74 float Inf; 75 memcpy(&Inf, InfVal, 4); 76 77 // Build a 'NaN'. 78 #if BYTE_ORDER == LITTLE_ENDIAN 79 unsigned char NaNVal[] = { 0x01, 0x00, 0x80, 0x7f }; 80 #else 81 unsigned char NaNVal[] = { 0x7f, 0x80, 0x00, 0x01 }; 82 #endif 83 float NaN; 84 memcpy(&NaN, NaNVal, 4); 85 86 double DblInf = (double)Inf; // ok 87 88 switch (argv[1][0]) { 89 // FIXME: Produce a source location for these checks and test for it here. 90 91 // Floating point -> integer overflow. 92 case '0': { 93 // Note that values between 0x7ffffe00 and 0x80000000 may or may not 94 // successfully round-trip, depending on the rounding mode. 95 // CHECK-0: {{.*}}cast-overflow.cpp:[[@LINE+1]]:27: runtime error: 2.14748{{.*}} is outside the range of representable values of type 'int' 96 static int test_int = MaxFloatRepresentableAsInt + 0x80; 97 // CHECK-0: SUMMARY: {{.*}}Sanitizer: float-cast-overflow {{.*}}cast-overflow.cpp:[[@LINE-1]] 98 return 0; 99 } 100 case '1': { 101 // CHECK-1: {{.*}}cast-overflow.cpp:[[@LINE+1]]:27: runtime error: -2.14748{{.*}} is outside the range of representable values of type 'int' 102 static int test_int = MinFloatRepresentableAsInt - 0x100; 103 return 0; 104 } 105 case '2': { 106 // CHECK-2: {{.*}}cast-overflow.cpp:[[@LINE+2]]:37: runtime error: -1 is outside the range of representable values of type 'unsigned int' 107 volatile float f = -1.0; 108 volatile unsigned u = (unsigned)f; 109 return 0; 110 } 111 case '3': { 112 // CHECK-3: {{.*}}cast-overflow.cpp:[[@LINE+1]]:37: runtime error: 4.2949{{.*}} is outside the range of representable values of type 'unsigned int' 113 static int test_int = (unsigned)(MaxFloatRepresentableAsUInt + 0x100); 114 return 0; 115 } 116 117 case '4': { 118 // CHECK-4: {{.*}}cast-overflow.cpp:[[@LINE+1]]:27: runtime error: {{.*}} is outside the range of representable values of type 'int' 119 static int test_int = Inf; 120 return 0; 121 } 122 case '5': { 123 // CHECK-5: {{.*}}cast-overflow.cpp:[[@LINE+1]]:27: runtime error: {{.*}} is outside the range of representable values of type 'int' 124 static int test_int = NaN; 125 return 0; 126 } 127 128 // Integer -> floating point overflow. 129 case '6': { 130 // CHECK-6: cast-overflow.cpp:[[@LINE+2]]:{{34: runtime error: 0xffffff00000000000000000000000001 is outside the range of representable values of type 'float'| __int128 not supported}} 131 #if defined(__SIZEOF_INT128__) && !defined(_WIN32) 132 static int test_int = (float)(FloatMaxAsUInt128 + 1); 133 return 0; 134 #else 135 // Print the same line as the check above. That way the test is robust to 136 // line changes around it 137 printf("%s:%d: __int128 not supported", __FILE__, __LINE__ - 5); 138 return 0; 139 #endif 140 } 141 // FIXME: The backend cannot lower __fp16 operations on x86 yet. 142 //case '7': 143 // (__fp16)65504; // ok 144 // // CHECK-7: runtime error: 65505 is outside the range of representable values of type '__fp16' 145 // return (__fp16)65505; 146 147 // Floating point -> floating point overflow. 148 case '8': 149 // CHECK-8: {{.*}}cast-overflow.cpp:[[@LINE+1]]:19: runtime error: 1e+39 is outside the range of representable values of type 'float' 150 return (float)1e39; 151 case '9': 152 volatile long double ld = 300.0; 153 // CHECK-9: {{.*}}cast-overflow.cpp:[[@LINE+1]]:14: runtime error: 300 is outside the range of representable values of type 'char' 154 char c = ld; 155 return c; 156 } 157 } 158