a7093300798202affddd58c26bfca2bc0a19586d
[openwrt/staging/dedeckeh.git] / toolchain / musl / patches / 030-fix-x87-stack-imbalance-in-corner-cases-of-i386-math.patch
1 From f3ed8bfe8a82af1870ddc8696ed4cc1d5aa6b441 Mon Sep 17 00:00:00 2001
2 From: Rich Felker <dalias@aerifal.cx>
3 Date: Mon, 5 Aug 2019 18:41:47 -0400
4 Subject: fix x87 stack imbalance in corner cases of i386 math asm
5
6 commit 31c5fb80b9eae86f801be4f46025bc6532a554c5 introduced underflow
7 code paths for the i386 math asm, along with checks on the fpu status
8 word to skip the underflow-generation instructions if the underflow
9 flag was already raised. unfortunately, at least one such path, in
10 log1p, returned with 2 items on the x87 stack rather than just 1 item
11 for the return value. this is a violation of the ABI's calling
12 convention, and could cause subsequent floating point code to produce
13 NANs due to x87 stack overflow. if floating point results are used in
14 flow control, this can lead to runaway wrong code execution.
15
16 rather than reviewing each "underflow already raised" code path for
17 correctness, remove them all. they're likely slower than just
18 performing the underflow code unconditionally, and significantly more
19 complex.
20
21 all of this code should be ripped out and replaced by C source files
22 with inline asm. doing so would preclude this kind of error by having
23 the compiler perform all x87 stack register allocation and stack
24 manipulation, and would produce comparable or better code. however
25 such a change is a much larger project.
26 ---
27 src/math/i386/asin.s | 10 ++--------
28 src/math/i386/atan.s | 7 ++-----
29 src/math/i386/atan2.s | 5 +----
30 src/math/i386/atan2f.s | 5 +----
31 src/math/i386/atanf.s | 7 ++-----
32 src/math/i386/exp.s | 10 ++--------
33 src/math/i386/log1p.s | 7 ++-----
34 src/math/i386/log1pf.s | 7 ++-----
35 8 files changed, 14 insertions(+), 44 deletions(-)
36
37 --- a/src/math/i386/asin.s
38 +++ b/src/math/i386/asin.s
39 @@ -7,13 +7,10 @@ asinf:
40 cmp $0x01000000,%eax
41 jae 1f
42 # subnormal x, return x with underflow
43 - fnstsw %ax
44 - and $16,%ax
45 - jnz 2f
46 fld %st(0)
47 fmul %st(1)
48 fstps 4(%esp)
49 -2: ret
50 + ret
51
52 .global asinl
53 .type asinl,@function
54 @@ -30,11 +27,8 @@ asin:
55 cmp $0x00200000,%eax
56 jae 1f
57 # subnormal x, return x with underflow
58 - fnstsw %ax
59 - and $16,%ax
60 - jnz 2f
61 fsts 4(%esp)
62 -2: ret
63 + ret
64 1: fld %st(0)
65 fld1
66 fsub %st(0),%st(1)
67 --- a/src/math/i386/atan.s
68 +++ b/src/math/i386/atan.s
69 @@ -10,8 +10,5 @@ atan:
70 fpatan
71 ret
72 # subnormal x, return x with underflow
73 -1: fnstsw %ax
74 - and $16,%ax
75 - jnz 2f
76 - fsts 4(%esp)
77 -2: ret
78 +1: fsts 4(%esp)
79 + ret
80 --- a/src/math/i386/atan2.s
81 +++ b/src/math/i386/atan2.s
82 @@ -10,8 +10,5 @@ atan2:
83 cmp $0x00200000,%eax
84 jae 1f
85 # subnormal x, return x with underflow
86 - fnstsw %ax
87 - and $16,%ax
88 - jnz 1f
89 fsts 4(%esp)
90 -1: ret
91 + ret
92 --- a/src/math/i386/atan2f.s
93 +++ b/src/math/i386/atan2f.s
94 @@ -10,10 +10,7 @@ atan2f:
95 cmp $0x01000000,%eax
96 jae 1f
97 # subnormal x, return x with underflow
98 - fnstsw %ax
99 - and $16,%ax
100 - jnz 1f
101 fld %st(0)
102 fmul %st(1)
103 fstps 4(%esp)
104 -1: ret
105 + ret
106 --- a/src/math/i386/atanf.s
107 +++ b/src/math/i386/atanf.s
108 @@ -10,10 +10,7 @@ atanf:
109 fpatan
110 ret
111 # subnormal x, return x with underflow
112 -1: fnstsw %ax
113 - and $16,%ax
114 - jnz 2f
115 - fld %st(0)
116 +1: fld %st(0)
117 fmul %st(1)
118 fstps 4(%esp)
119 -2: ret
120 + ret
121 --- a/src/math/i386/exp.s
122 +++ b/src/math/i386/exp.s
123 @@ -7,13 +7,10 @@ expm1f:
124 cmp $0x01000000,%eax
125 jae 1f
126 # subnormal x, return x with underflow
127 - fnstsw %ax
128 - and $16,%ax
129 - jnz 2f
130 fld %st(0)
131 fmul %st(1)
132 fstps 4(%esp)
133 -2: ret
134 + ret
135
136 .global expm1l
137 .type expm1l,@function
138 @@ -30,11 +27,8 @@ expm1:
139 cmp $0x00200000,%eax
140 jae 1f
141 # subnormal x, return x with underflow
142 - fnstsw %ax
143 - and $16,%ax
144 - jnz 2f
145 fsts 4(%esp)
146 -2: ret
147 + ret
148 1: fldl2e
149 fmulp
150 mov $0xc2820000,%eax
151 --- a/src/math/i386/log1p.s
152 +++ b/src/math/i386/log1p.s
153 @@ -16,9 +16,6 @@ log1p:
154 fyl2x
155 ret
156 # subnormal x, return x with underflow
157 -2: fnstsw %ax
158 - and $16,%ax
159 - jnz 1f
160 - fsts 4(%esp)
161 +2: fsts 4(%esp)
162 fstp %st(1)
163 -1: ret
164 + ret
165 --- a/src/math/i386/log1pf.s
166 +++ b/src/math/i386/log1pf.s
167 @@ -16,10 +16,7 @@ log1pf:
168 fyl2x
169 ret
170 # subnormal x, return x with underflow
171 -2: fnstsw %ax
172 - and $16,%ax
173 - jnz 1f
174 - fxch
175 +2: fxch
176 fmul %st(1)
177 fstps 4(%esp)
178 -1: ret
179 + ret