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