diff mbox series

libgo: Don't rely on GNU-specific strerror_r variant on Linux

Message ID 20221129174331.3124-1-soeren@soeren-tempel.net
State New
Headers show
Series libgo: Don't rely on GNU-specific strerror_r variant on Linux | expand

Commit Message

Li, Pan2 via Gcc-patches Nov. 29, 2022, 5:43 p.m. UTC
From: Sören Tempel <soeren@soeren-tempel.net>

On glibc, there are two versions of strerror_r: An XSI-compliant and a
GNU-specific version. The latter is only available on glibc. In order
to avoid duplicating the post-processing code of error messages, this
commit provides a separate strerror_go symbol which always refers to the
XSI-compliant version of strerror_r (even on glibc) by selectively
undefining the corresponding feature test macro.

Previously, gofrontend assumed that the GNU-specific version of
strerror_r was always available on Linux (which isn't the case when
using a musl as a libc, for example). This commit thereby improves
compatibility with Linux systems that are not using glibc.

Tested on x86_64 Alpine Linux Edge and Arch Linux (glibc 2.36).

Alternative: Use a GNU autoconf macro to detect which version is in
use. However, this requires moving the allocations and post-processing
logic from Go to C.

Signed-off-by: Sören Tempel <soeren@soeren-tempel.net>
---
I previously experimented a bit with the aforementioned GNU autoconf
macro, however, I believe this is the more elegant and portable solution
as it doesn't require dealing with memory allocation for XSI strerror_r
buffers etc.

 libgo/Makefile.am                |  1 +
 libgo/Makefile.in                |  6 +++++-
 libgo/go/syscall/errstr.go       |  9 +++------
 libgo/go/syscall/errstr_glibc.go | 34 --------------------------------
 libgo/runtime/go-strerror.c      | 30 ++++++++++++++++++++++++++++
 5 files changed, 39 insertions(+), 41 deletions(-)
 delete mode 100644 libgo/go/syscall/errstr_glibc.go
 create mode 100644 libgo/runtime/go-strerror.c
diff mbox series

Patch

diff --git a/libgo/Makefile.am b/libgo/Makefile.am
index b03e6553..207d5a98 100644
--- a/libgo/Makefile.am
+++ b/libgo/Makefile.am
@@ -465,6 +465,7 @@  runtime_files = \
 	runtime/go-nanotime.c \
 	runtime/go-now.c \
 	runtime/go-nosys.c \
+	runtime/go-strerror.c \
 	runtime/go-reflect-call.c \
 	runtime/go-setenv.c \
 	runtime/go-signal.c \
diff --git a/libgo/Makefile.in b/libgo/Makefile.in
index 16ed62a8..ab9fa376 100644
--- a/libgo/Makefile.in
+++ b/libgo/Makefile.in
@@ -247,7 +247,7 @@  am__objects_4 = runtime/aeshash.lo runtime/go-assert.lo \
 	runtime/go-fieldtrack.lo runtime/go-matherr.lo \
 	runtime/go-memclr.lo runtime/go-memmove.lo \
 	runtime/go-memequal.lo runtime/go-nanotime.lo \
-	runtime/go-now.lo runtime/go-nosys.lo \
+	runtime/go-now.lo runtime/go-nosys.lo runtime/go-strerror.lo \
 	runtime/go-reflect-call.lo runtime/go-setenv.lo \
 	runtime/go-signal.lo runtime/go-unsafe-pointer.lo \
 	runtime/go-unsetenv.lo runtime/go-unwind.lo \
@@ -917,6 +917,7 @@  runtime_files = \
 	runtime/go-nanotime.c \
 	runtime/go-now.c \
 	runtime/go-nosys.c \
+	runtime/go-strerror.c \
 	runtime/go-reflect-call.c \
 	runtime/go-setenv.c \
 	runtime/go-signal.c \
@@ -1390,6 +1391,8 @@  runtime/go-now.lo: runtime/$(am__dirstamp) \
 	runtime/$(DEPDIR)/$(am__dirstamp)
 runtime/go-nosys.lo: runtime/$(am__dirstamp) \
 	runtime/$(DEPDIR)/$(am__dirstamp)
+runtime/strerror.lo: runtime/$(am__dirstamp) \
+	runtime/$(DEPDIR)/$(am__dirstamp)
 runtime/go-reflect-call.lo: runtime/$(am__dirstamp) \
 	runtime/$(DEPDIR)/$(am__dirstamp)
 runtime/go-setenv.lo: runtime/$(am__dirstamp) \
@@ -1453,6 +1456,7 @@  distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-memmove.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-nanotime.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-nosys.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-strerror.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-now.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-reflect-call.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-setenv.Plo@am__quote@
diff --git a/libgo/go/syscall/errstr.go b/libgo/go/syscall/errstr.go
index 59f7a82c..6cc73853 100644
--- a/libgo/go/syscall/errstr.go
+++ b/libgo/go/syscall/errstr.go
@@ -4,18 +4,15 @@ 
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build !hurd && !linux
-// +build !hurd,!linux
-
 package syscall
 
-//sysnb	strerror_r(errnum int, buf []byte) (err Errno)
-//strerror_r(errnum _C_int, buf *byte, buflen Size_t) _C_int
+//sysnb	strerror_go(errnum int, buf []byte) (err Errno)
+//strerror_go(errnum _C_int, buf *byte, buflen Size_t) _C_int
 
 func Errstr(errnum int) string {
 	for len := 128; ; len *= 2 {
 		b := make([]byte, len)
-		errno := strerror_r(errnum, b)
+		errno := strerror_go(errnum, b)
 		if errno == 0 {
 			i := 0
 			for b[i] != 0 {
diff --git a/libgo/go/syscall/errstr_glibc.go b/libgo/go/syscall/errstr_glibc.go
deleted file mode 100644
index 03a327db..00000000
--- a/libgo/go/syscall/errstr_glibc.go
+++ /dev/null
@@ -1,34 +0,0 @@ 
-// errstr_glibc.go -- GNU/Linux and GNU/Hurd specific error strings.
-
-// Copyright 2010 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// We use this rather than errstr.go because on GNU/Linux sterror_r
-// returns a pointer to the error message, and may not use buf at all.
-
-//go:build hurd || linux
-// +build hurd linux
-
-package syscall
-
-import "unsafe"
-
-//sysnb	strerror_r(errnum int, b []byte) (errstr *byte)
-//strerror_r(errnum _C_int, b *byte, len Size_t) *byte
-
-func Errstr(errnum int) string {
-	a := make([]byte, 128)
-	p := strerror_r(errnum, a)
-	b := (*[1000]byte)(unsafe.Pointer(p))
-	i := 0
-	for b[i] != 0 {
-		i++
-	}
-	// Lowercase first letter: Bad -> bad, but STREAM -> STREAM.
-	if i > 1 && 'A' <= b[0] && b[0] <= 'Z' && 'a' <= b[1] && b[1] <= 'z' {
-		c := b[0] + 'a' - 'A'
-		return string(c) + string(b[1:i])
-	}
-	return string(b[:i])
-}
diff --git a/libgo/runtime/go-strerror.c b/libgo/runtime/go-strerror.c
new file mode 100644
index 00000000..9c769cf8
--- /dev/null
+++ b/libgo/runtime/go-strerror.c
@@ -0,0 +1,30 @@ 
+/* go-strerror.c -- wrapper around XSI-compliant strerror_r(3).
+
+   Copyright 2022 The Go Authors. All rights reserved.
+   Use of this source code is governed by a BSD-style
+   license that can be found in the LICENSE file.  */
+
+/* There are two version of strerror_r(3): A GNU-specific
+   and an XSI-compliant version.  The former version is only available
+   on glibc.  Since glibc 2.13, the XSI-compliant version is also
+   provided by glibc if _GNU_SOURCE is not defined.  Since the
+   entirety of gofrontend is compiled with _GNU_SOURCE, this file
+   exists to selectively undefine it and provides an alias to the
+   XSI-compliant version of strerror_r(3).  */
+
+// Force selection of XSI-compliant strerror_r(3) on glibc.
+#define XOPEN_SOURCE 600
+#undef _GNU_SOURCE
+#include <string.h>
+
+#ifndef HAVE_STRERROR_R
+// Provided by go-nosys.c if not provided by libc itself.
+extern int strerror_r (int errnum, char *buf, size_t buflen);
+#endif
+
+int
+strerror_go (int errnum, char *buf, size_t buflen)
+{
+  // Always call the XSI-compliant version of strerror_r(3).
+  return strerror_r (errnum, buf, buflen);
+}