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