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