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