public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ian Lance Taylor <iant@golang.org>
To: soeren@soeren-tempel.net
Cc: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com
Subject: Re: [PATCH v2] libgo: Don't rely on GNU-specific strerror_r variant on Linux
Date: Tue, 29 Nov 2022 16:10:11 -0800	[thread overview]
Message-ID: <CAOyqgcUXZ3HjQ58yEHLPNg9bhwc8B0G-2SpynEeP=iw9Y+VOqw@mail.gmail.com> (raw)
In-Reply-To: <20221129175453.3644-1-soeren@soeren-tempel.net>

[-- Attachment #1: Type: text/plain, Size: 947 bytes --]

On Tue, Nov 29, 2022 at 9:54 AM <soeren@soeren-tempel.net> wrote:
>
> 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).

Thanks.  I committed a version of this, as attached.

Ian

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 4592 bytes --]

b6c6a3d64f2e4e9347733290aca3c75898c44b2e
diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE
index 7e531c3f90b..984d8324004 100644
--- a/gcc/go/gofrontend/MERGE
+++ b/gcc/go/gofrontend/MERGE
@@ -1,4 +1,4 @@
-5e658f4659c551330ea68f5667e4f951b218f32d
+fef6aa3c1678cdbe7dca454b2cebb369d8ba81bf
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
diff --git a/libgo/Makefile.am b/libgo/Makefile.am
index b03e6553e90..207d5a98127 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/go/syscall/errstr.go b/libgo/go/syscall/errstr.go
index 59f7a82c6d7..9f688e2a0c7 100644
--- a/libgo/go/syscall/errstr.go
+++ b/libgo/go/syscall/errstr.go
@@ -4,23 +4,19 @@
 // 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
+import "internal/bytealg"
+
+//extern go_strerror
+func go_strerror(_C_int, *byte, Size_t) _C_int
 
 func Errstr(errnum int) string {
-	for len := 128; ; len *= 2 {
-		b := make([]byte, len)
-		errno := strerror_r(errnum, b)
+	for size := 128; ; size *= 2 {
+		b := make([]byte, size)
+		errno := go_strerror(_C_int(errnum), &b[0], Size_t(len(b)))
 		if errno == 0 {
-			i := 0
-			for b[i] != 0 {
-				i++
-			}
+			i := bytealg.IndexByte(b, 0)
 			// Lowercase first letter: Bad -> bad, but
 			// STREAM -> STREAM.
 			if i > 1 && 'A' <= b[0] && b[0] <= 'Z' && 'a' <= b[1] && b[1] <= 'z' {
@@ -29,7 +25,7 @@ func Errstr(errnum int) string {
 			return string(b[:i])
 		}
 		if errno != ERANGE {
-			return "errstr failure"
+			return "strerror_r failure"
 		}
 	}
 }
diff --git a/libgo/go/syscall/errstr_glibc.go b/libgo/go/syscall/errstr_glibc.go
deleted file mode 100644
index 03a327dbc90..00000000000
--- 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 00000000000..13d1d91df84
--- /dev/null
+++ b/libgo/runtime/go-strerror.c
@@ -0,0 +1,37 @@
+/* go-strerror.c -- wrapper around XSI-compliant strerror_r.
+
+   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 on GNU/Linux: 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).  */
+
+#ifdef __linux__
+
+/* Force selection of XSI-compliant strerror_r by glibc.  */
+#undef XOPEN_SOURCE
+#define XOPEN_SOURCE 600
+#undef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200112L
+#undef _GNU_SOURCE
+
+#endif /* __linux__ */
+
+#include <string.h>
+
+#ifndef HAVE_STRERROR_R
+// Provided by go-nosys.c if not provided by libc itself.
+extern int strerror_r (int, char *, size_t);
+#endif
+
+int
+go_strerror (int errnum, char *buf, size_t buflen)
+{
+  return strerror_r (errnum, buf, buflen);
+}

  reply	other threads:[~2022-11-30  0:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-29 17:43 [PATCH] " soeren
2022-11-29 17:54 ` [PATCH v2] " soeren
2022-11-30  0:10   ` Ian Lance Taylor [this message]
2022-11-30 20:21     ` Ian Lance Taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOyqgcUXZ3HjQ58yEHLPNg9bhwc8B0G-2SpynEeP=iw9Y+VOqw@mail.gmail.com' \
    --to=iant@golang.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gofrontend-dev@googlegroups.com \
    --cc=soeren@soeren-tempel.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).