d8307d
commit 2ef427168818ce04b03cecb7b739f9db0156e3e4
d8307d
Author: Aurelien Jarno <aurelien@aurel32.net>
d8307d
Date:   Thu Jan 3 15:51:37 2019 +0100
d8307d
d8307d
    Only build libm with -fno-math-errno (bug 24024)
d8307d
    
d8307d
    Commit 1294b1892e ("Add support for sqrt asm redirects") added the
d8307d
    -fno-math-errno flag to build most of the glibc in order to enable GCC
d8307d
    to inline math functions. Due to GCC bug #88576, saving and restoring
d8307d
    errno around calls to malloc are optimized-out. In turn this causes
d8307d
    strerror to set errno to ENOMEM if it get passed an invalid error number
d8307d
    and if malloc sets errno to ENOMEM (which might happen even if it
d8307d
    succeeds). This is not allowed by POSIX.
d8307d
    
d8307d
    This patch changes the build flags, building only libm with
d8307d
    -fno-math-errno and all the remaining code with -fno-math-errno. This
d8307d
    should be safe as libm doesn't contain any code saving and restoring
d8307d
    errno around malloc. This patch can probably be reverted once the GCC
d8307d
    bug is fixed and available in stable releases.
d8307d
    
d8307d
    Tested on x86-64, no regression in the testsuite.
d8307d
    
d8307d
    Changelog:
d8307d
            [BZ #24024]
d8307d
            * Makeconfig: Build libm with -fno-math-errno but build the remaining
d8307d
            code with -fmath-errno.
d8307d
            * string/Makefile [$(build-shared)] (tests): Add test-strerror-errno.
d8307d
            [$(build-shared)] (LDLIBS-test-strerror-errno): New variable.
d8307d
            * string/test-strerror-errno.c: New file.
d8307d
d8307d
diff --git a/Makeconfig b/Makeconfig
d8307d
index 92e76d6200bbcd5b..8dc2fec9dc683416 100644
d8307d
--- a/Makeconfig
d8307d
+++ b/Makeconfig
d8307d
@@ -831,8 +831,10 @@ endif
d8307d
 # disable any optimization that assume default rounding mode.
d8307d
 +math-flags = -frounding-math
d8307d
 
d8307d
-# Build libc/libm using -fno-math-errno, but run testsuite with -fmath-errno.
d8307d
-+extra-math-flags = $(if $(filter libnldbl nonlib testsuite,$(in-module)),-fmath-errno,-fno-math-errno)
d8307d
+# Logically only "libnldbl", "nonlib" and "testsuite" should be using
d8307d
+# -fno-math-errno. However due to GCC bug #88576, only "libm" can use
d8307d
+# -fno-math-errno.
d8307d
++extra-math-flags = $(if $(filter libm,$(in-module)),-fno-math-errno,-fmath-errno)
d8307d
 
d8307d
 # We might want to compile with some stack-protection flag.
d8307d
 ifneq ($(stack-protector),)
d8307d
diff --git a/string/Makefile b/string/Makefile
d8307d
index 680431f92185f914..aa2da9ca72262886 100644
d8307d
--- a/string/Makefile
d8307d
+++ b/string/Makefile
d8307d
@@ -64,6 +64,12 @@ tests		:= tester inl-tester noinl-tester testcopy test-ffs	\
d8307d
 # This test allocates a lot of memory and can run for a long time.
d8307d
 xtests = tst-strcoll-overflow
d8307d
 
d8307d
+# This test needs libdl.
d8307d
+ifeq (yes,$(build-shared))
d8307d
+tests += test-strerror-errno
d8307d
+LDLIBS-test-strerror-errno = $(libdl)
d8307d
+endif
d8307d
+
d8307d
 ifeq ($(run-built-tests),yes)
d8307d
 tests-special += $(objpfx)tst-svc-cmp.out
d8307d
 endif
d8307d
diff --git a/string/test-strerror-errno.c b/string/test-strerror-errno.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..8e744e7ed9df5924
d8307d
--- /dev/null
d8307d
+++ b/string/test-strerror-errno.c
d8307d
@@ -0,0 +1,61 @@
d8307d
+/* BZ #24024 strerror and errno test.
d8307d
+
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <dlfcn.h>
d8307d
+#include <errno.h>
d8307d
+#include <string.h>
d8307d
+
d8307d
+#include <support/check.h>
d8307d
+#include <support/support.h>
d8307d
+
d8307d
+/* malloc is allowed to change errno to a value different than 0, even when
d8307d
+   there is no actual error.  This happens for example when the memory
d8307d
+   allocation through sbrk fails.  Simulate this by interposing our own
d8307d
+   malloc implementation which sets errno to ENOMEM and calls the original
d8307d
+   malloc.  */
d8307d
+void
d8307d
+*malloc (size_t size)
d8307d
+{
d8307d
+  static void *(*real_malloc) (size_t size);
d8307d
+
d8307d
+  if (!real_malloc)
d8307d
+    real_malloc = dlsym (RTLD_NEXT, "malloc");
d8307d
+
d8307d
+  errno = ENOMEM;
d8307d
+
d8307d
+  return (*real_malloc) (size);
d8307d
+}
d8307d
+
d8307d
+/* strerror must not change the value of errno.  Unfortunately due to GCC bug
d8307d
+   #88576, this happens when -fmath-errno is used.  This simple test checks
d8307d
+   that it doesn't happen.  */
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  char *msg;
d8307d
+
d8307d
+  errno = 0;
d8307d
+  msg = strerror (-3);
d8307d
+  (void) msg;
d8307d
+  TEST_COMPARE (errno, 0);
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+#include <support/test-driver.c>