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