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
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.
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
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.
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(-)
37 --- a/src/math/i386/asin.s
38 +++ b/src/math/i386/asin.s
39 @@ -7,13 +7,10 @@ asinf:
42 # subnormal x, return x with underflow
54 @@ -30,11 +27,8 @@ asin:
57 # subnormal x, return x with underflow
67 --- a/src/math/i386/atan.s
68 +++ b/src/math/i386/atan.s
69 @@ -10,8 +10,5 @@ atan:
72 # subnormal x, return x with underflow
80 --- a/src/math/i386/atan2.s
81 +++ b/src/math/i386/atan2.s
82 @@ -10,8 +10,5 @@ atan2:
85 # subnormal x, return x with underflow
92 --- a/src/math/i386/atan2f.s
93 +++ b/src/math/i386/atan2f.s
94 @@ -10,10 +10,7 @@ atan2f:
97 # subnormal x, return x with underflow
106 --- a/src/math/i386/atanf.s
107 +++ b/src/math/i386/atanf.s
108 @@ -10,10 +10,7 @@ atanf:
111 # subnormal x, return x with underflow
121 --- a/src/math/i386/exp.s
122 +++ b/src/math/i386/exp.s
123 @@ -7,13 +7,10 @@ expm1f:
126 # subnormal x, return x with underflow
137 .type expm1l,@function
138 @@ -30,11 +27,8 @@ expm1:
141 # subnormal x, return x with underflow
151 --- a/src/math/i386/log1p.s
152 +++ b/src/math/i386/log1p.s
153 @@ -16,9 +16,6 @@ log1p:
156 # subnormal x, return x with underflow
165 --- a/src/math/i386/log1pf.s
166 +++ b/src/math/i386/log1pf.s
167 @@ -16,10 +16,7 @@ log1pf:
170 # subnormal x, return x with underflow