From patchwork Tue Nov 29 17:54:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 27391 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp484659wrr; Tue, 29 Nov 2022 09:55:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ueSByblxJSzsixYgRL44xbRW+TwrSm/Lw/D3urmuIiClL9vyvMNweuqAKJY2uzCT/MZxd X-Received: by 2002:a17:906:2302:b0:7b9:de77:f0ef with SMTP id l2-20020a170906230200b007b9de77f0efmr27791184eja.5.1669744545269; Tue, 29 Nov 2022 09:55:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669744545; cv=none; d=google.com; s=arc-20160816; b=XfHctRpKt1+HOQa9ypwQZQnsJAOCoFZgQQgbQsvkCKJ9SY5dU4OuWptynMJP724GmY m4NVbufbVxid1uRxy2qBOtkOQY4ilA8RoLZVcI4SoA5mSGhXwJZyrrYqwomUnFV4UbzC wEZ99my8WHo/S1/9xP0ynlisNwTV6GoUIGmNWEfLuL43dnef1orI4u2qA4x3wO0IP4Fq ynmgYYLXUkqRKiWUMQ3W5iGGeJKtOEbWlQlZ7DRoiXHnoP8cYXzcYgvgzQG42kF/vQOX dYe/9OlQgiPkitCD2Da4PbyFXJTKV8kMLcTUdj6MZiUKma2mcx3andle5lUyBlqDnmIJ 57pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=9YQTHTav+oB/V/3cPwiicJ/J1PmIt9ivvtQB/zRFlrI=; b=S+aFLtffCI7+9PMiBfaYF3++3YrrRgvxUg6IvpuXM1QCNUwbNK/7MqW+3ttG7hcXJE vwa5GebwyDy+ZPihovczWuRH1179VCNxQ2UKBb2wvTyVRHyLdCu2UqA0XZqbhECIvspL 3RXWptYv3yaMjvkWTa2Wb6wiYHk6QiBrp8Tt9YCEb85TQLJE3D7F2LaKPYnw3SrebR0s XNWD2zf5Ju4NCAeMmuDQcXVgZQ2WfFS9Q9X83DrSEKflgG+sz1+Zuo8JyJ3uVYfPwQuF Z80c8WRWGGbbD9GITIjkjPmCxP/SmUlPbyQunQC+NIARVfTTluyjmgDtdV8UKA4r1uAC yvPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="i/3jma6e"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id p6-20020a056402044600b0046197a8b7a1si11728479edw.485.2022.11.29.09.55.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 09:55:45 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="i/3jma6e"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1139F3858D28 for ; Tue, 29 Nov 2022 17:55:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1139F3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669744544; bh=9YQTHTav+oB/V/3cPwiicJ/J1PmIt9ivvtQB/zRFlrI=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=i/3jma6e6IRmM2RnskrFDP7W8/v97W5ICzUtV12xLU04+jUWUTaXbDrAqmOz38VAs j3Ik8u/C0osYN6htjVccMp3X5sRMimZdNh2+MMY0A625iPKKDJ7fWSYKzcdI3GHjmF LQpdB5zo4cT1SXmyCgUPYjSxqv1+7CxeUsySWOIs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from magnesium.8pit.net (magnesium.8pit.net [45.76.88.171]) by sourceware.org (Postfix) with ESMTPS id CEC923858D28; Tue, 29 Nov 2022 17:55:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CEC923858D28 Received: from localhost ( [2a02:8109:3b40:22d0:9588:a21c:776f:828c]) by magnesium.8pit.net (OpenSMTPD) with ESMTPSA id d2a47a9c (TLSv1.3:TLS_AES_256_GCM_SHA384:256:YES); Tue, 29 Nov 2022 18:54:59 +0100 (CET) To: iant@golang.org Cc: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: [PATCH v2] libgo: Don't rely on GNU-specific strerror_r variant on Linux Date: Tue, 29 Nov 2022 18:54:53 +0100 Message-Id: <20221129175453.3644-1-soeren@soeren-tempel.net> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221129174331.3124-1-soeren@soeren-tempel.net> References: <20221129174331.3124-1-soeren@soeren-tempel.net> MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: soeren--- via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: soeren@soeren-tempel.net Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750853354962407435?= X-GMAIL-MSGID: =?utf-8?q?1750854056291156878?= From: Sören Tempel 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 --- Changes since v1: Fixed a typo in the Makefile. 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 --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..0407e09c 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/go-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 + +#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); +}