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