Bug 22225 - nearbyint arithmetic moved before feholdexcept
Summary: nearbyint arithmetic moved before feholdexcept
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: math (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.27
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-27 23:10 UTC by Joseph Myers
Modified: 2017-09-28 10:04 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph Myers 2017-09-27 23:10:08 UTC
In https://sourceware.org/ml/libc-alpha/2013-05/msg00722.html I remarked on the possibility of arithmetic in various nearbyint implementations being scheduled before feholdexcept calls, resulting in spurious "inexact" exceptions.

I'm now actually observing this occurring in glibc built for ARM with GCC 7 (in fact, both copies of the same addition/subtraction sequence being combined and moved out before the conditionals and feholdexcept/fesetenv pairs), resulting in test failures.

FAIL: math/test-double-finite-lrint
FAIL: math/test-double-finite-nearbyint
FAIL: math/test-double-lrint
FAIL: math/test-double-nearbyint
FAIL: math/test-float-finite-nearbyint
FAIL: math/test-float-nearbyint

Testing a patch.
Comment 1 Sourceware Commits 2017-09-28 01:59:57 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  f124cb381116b5809de198327690ad0bd8d1478e (commit)
      from  7edd06916d0f18714f323b180b890f1fea39e1ff (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=f124cb381116b5809de198327690ad0bd8d1478e

commit f124cb381116b5809de198327690ad0bd8d1478e
Author: Joseph Myers <joseph@codesourcery.com>
Date:   Thu Sep 28 01:59:02 2017 +0000

    Fix nearbyint arithmetic moved before feholdexcept (bug 22225).
    
    In <https://sourceware.org/ml/libc-alpha/2013-05/msg00722.html> I
    remarked on the possibility of arithmetic in various nearbyint
    implementations being scheduled before feholdexcept calls, resulting
    in spurious "inexact" exceptions.
    
    I'm now actually observing this occurring in glibc built for ARM with
    GCC 7 (in fact, both copies of the same addition/subtraction sequence
    being combined and moved out before the conditionals and
    feholdexcept/fesetenv pairs), resulting in test failures.
    
    This patch makes the nearbyint implementations with this particular
    feholdexcept / arithmetic / fesetenv pattern consistently use
    math_opt_barrier on the function argument when first used in
    arithmetic, and also consistently use math_force_eval before fesetenv
    (the latter was generally already done, but the dbl-64/wordsize-64
    implementation used math_opt_barrier instead, and as
    math_opt_barrier's intended effect is through its output value being
    used, such a use that doesn't use the return value is suspect).
    
    Tested for x86_64 (--disable-multi-arch so more of these
    implementations get used), and for ARM in a configuration where I saw
    the problem scheduling.
    
    	[BZ #22225]
    	* sysdeps/ieee754/dbl-64/s_nearbyint.c (__nearbyint): Use
    	math_opt_barrier on argument when doing arithmetic on it.
    	* sysdeps/ieee754/dbl-64/wordsize-64/s_nearbyint.c (__nearbyint):
    	Likewise.  Use math_force_eval not math_opt_barrier after
    	arithmetic.
    	* sysdeps/ieee754/flt-32/s_nearbyintf.c (__nearbyintf): Use
    	math_opt_barrier on argument when doing arithmetic on it.
    	* sysdeps/ieee754/ldbl-128/s_nearbyintl.c (__nearbyintl):
    	Likewise.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                                        |   13 +++++++++++++
 sysdeps/ieee754/dbl-64/s_nearbyint.c             |    4 ++--
 sysdeps/ieee754/dbl-64/wordsize-64/s_nearbyint.c |    8 ++++----
 sysdeps/ieee754/flt-32/s_nearbyintf.c            |    4 ++--
 sysdeps/ieee754/ldbl-128/s_nearbyintl.c          |    4 ++--
 5 files changed, 23 insertions(+), 10 deletions(-)
Comment 2 Joseph Myers 2017-09-28 02:01:32 UTC
Fixed for 2.27.
Comment 3 Sourceware Commits 2017-09-28 02:04:13 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, release/2.26/master has been updated
       via  548cc83c38a91852b1e44045ead3d20ccd5db4cf (commit)
      from  d37c951fde57e8acb320a9a7d437ba50a1fc3c8a (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=548cc83c38a91852b1e44045ead3d20ccd5db4cf

commit 548cc83c38a91852b1e44045ead3d20ccd5db4cf
Author: Joseph Myers <joseph@codesourcery.com>
Date:   Thu Sep 28 01:59:02 2017 +0000

    Fix nearbyint arithmetic moved before feholdexcept (bug 22225).
    
    In <https://sourceware.org/ml/libc-alpha/2013-05/msg00722.html> I
    remarked on the possibility of arithmetic in various nearbyint
    implementations being scheduled before feholdexcept calls, resulting
    in spurious "inexact" exceptions.
    
    I'm now actually observing this occurring in glibc built for ARM with
    GCC 7 (in fact, both copies of the same addition/subtraction sequence
    being combined and moved out before the conditionals and
    feholdexcept/fesetenv pairs), resulting in test failures.
    
    This patch makes the nearbyint implementations with this particular
    feholdexcept / arithmetic / fesetenv pattern consistently use
    math_opt_barrier on the function argument when first used in
    arithmetic, and also consistently use math_force_eval before fesetenv
    (the latter was generally already done, but the dbl-64/wordsize-64
    implementation used math_opt_barrier instead, and as
    math_opt_barrier's intended effect is through its output value being
    used, such a use that doesn't use the return value is suspect).
    
    Tested for x86_64 (--disable-multi-arch so more of these
    implementations get used), and for ARM in a configuration where I saw
    the problem scheduling.
    
    	[BZ #22225]
    	* sysdeps/ieee754/dbl-64/s_nearbyint.c (__nearbyint): Use
    	math_opt_barrier on argument when doing arithmetic on it.
    	* sysdeps/ieee754/dbl-64/wordsize-64/s_nearbyint.c (__nearbyint):
    	Likewise.  Use math_force_eval not math_opt_barrier after
    	arithmetic.
    	* sysdeps/ieee754/flt-32/s_nearbyintf.c (__nearbyintf): Use
    	math_opt_barrier on argument when doing arithmetic on it.
    	* sysdeps/ieee754/ldbl-128/s_nearbyintl.c (__nearbyintl):
    	Likewise.
    
    (cherry picked from commit f124cb381116b5809de198327690ad0bd8d1478e)

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                                        |   13 +++++++++++++
 sysdeps/ieee754/dbl-64/s_nearbyint.c             |    4 ++--
 sysdeps/ieee754/dbl-64/wordsize-64/s_nearbyint.c |    8 ++++----
 sysdeps/ieee754/flt-32/s_nearbyintf.c            |    4 ++--
 sysdeps/ieee754/ldbl-128/s_nearbyintl.c          |    4 ++--
 5 files changed, 23 insertions(+), 10 deletions(-)