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