diff mbox series

musl: Keep clang from raising spurious FP exceptions

Message ID 20261028051959.2975770-1-khem.raj@oss.qualcomm.com
State Under Review
Headers show
Series musl: Keep clang from raising spurious FP exceptions | expand

Commit Message

Khem Raj Oct. 28, 2026, 5:19 a.m. UTC
With clang built musl's math code assumes
floating point exceptions are never inspected and speculates FP
operations across branches. expm1() then evaluates both scaling
multiplies and selects one, raising a spurious OVERFLOW, and libc-test
math/expm1, expm1f, hypot, hypotf and sinh fail on qemuarm64.

Pass -ffp-exception-behavior=maytrap from musl's configure when the
compiler accepts it. GCC does not accept it, so GCC builds are
unchanged.

Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
---
 ...p-exception-behavior-maytrap-when-su.patch | 46 +++++++++++++++++++
 meta/recipes-core/musl/musl_git.bb            |  1 +
 2 files changed, 47 insertions(+)
 create mode 100644 meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch
diff mbox series

Patch

diff --git a/meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch b/meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch
new file mode 100644
index 0000000000..dfff81299b
--- /dev/null
+++ b/meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch
@@ -0,0 +1,46 @@ 
+From fe310a5e2fe6be3c28f800bde383d7df5aac4b49 Mon Sep 17 00:00:00 2001
+From: Khem Raj <khem.raj@oss.qualcomm.com>
+Date: Sun, 25 Oct 2026 21:54:26 -0700
+Subject: [PATCH] configure: use -ffp-exception-behavior=maytrap when supported
+
+The math library relies on floating point exception flags being raised
+exactly as the C code would raise them. GCC keeps that by default
+(-ftrapping-math), but clang assumes floating point exceptions are never
+inspected unless told otherwise, and may then evaluate both arms of a
+branch and select the result. Built with clang 23 for aarch64, expm1()
+computes both y*2.0*0x1p1023 and y*twopk for the k < 0 || k > 56 case
+and picks one with fcsel, so the unused multiply raises a spurious
+OVERFLOW. libc-test's math/expm1, expm1f, hypot, hypotf and sinh fail
+because of that.
+
+-ffp-exception-behavior=maytrap tells clang to avoid transformations
+that raise exceptions the original code would not raise. GCC does not
+accept the option, so tryflag leaves GCC builds unchanged.
+
+Upstream-Status: Submitted [https://www.openwall.com/lists/musl/2026/10/28/1]
+Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
+---
+ configure | 10 ++++++++++
+ 1 file changed, 10 insertions(+)
+
+diff --git a/configure b/configure
+index bc9fbe48..7e1e1b72 100755
+--- a/configure
++++ b/configure
+@@ -355,6 +355,16 @@ tryflag CFLAGS_C99FSE -fexcess-precision=standard \
+ || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
+ tryflag CFLAGS_C99FSE -frounding-math
+ 
++#
++# The math library relies on floating point exception flags being
++# raised exactly as the C code would raise them. GCC does not move
++# floating point operations that may trap across branches by default
++# (-ftrapping-math), but clang assumes exceptions are never inspected
++# unless told otherwise, and may then evaluate both sides of a branch,
++# raising spurious exceptions (e.g. OVERFLOW from expm1, hypot, sinh).
++#
++tryflag CFLAGS_C99FSE -ffp-exception-behavior=maytrap
++
+ #
+ # Semantically we want to insist that our sources follow the
+ # C rules for type-based aliasing, but most if not all real-world
diff --git a/meta/recipes-core/musl/musl_git.bb b/meta/recipes-core/musl/musl_git.bb
index c81178c15a..b53b4be84c 100644
--- a/meta/recipes-core/musl/musl_git.bb
+++ b/meta/recipes-core/musl/musl_git.bb
@@ -14,6 +14,7 @@  SRC_URI = "git://git.musl-libc.org/musl;branch=master \
            file://0001-Make-dynamic-linker-a-relative-symlink-to-libc.patch \
            file://0002-ldso-Use-syslibdir-and-libdir-as-default-pathes-to-l.patch \
            file://0003-tools-version.sh-fall-back-to-VERSION-when-git-descr.patch \
+           file://0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch \
           "
 
 PROVIDES += "virtual/libc virtual/libiconv virtual/libintl virtual/crypt"