0034-Use-generic-errstr.go-implementation-on-musl.patch (7090B)
1 From b3840bdcc0eee2a4d099a9af52199944ade7acb9 Mon Sep 17 00:00:00 2001 2 From: Ian Lance Taylor <iant@golang.org> 3 Date: Tue, 29 Nov 2022 17:28:44 -0800 4 Subject: [PATCH] syscall, runtime: always call XSI strerror_r 5 MIME-Version: 1.0 6 Content-Type: text/plain; charset=UTF-8 7 Content-Transfer-Encoding: 8bit 8 9 This does the right thing for either glibc or musl on GNU/Linux. 10 11 Based on patch by Sören Tempel. 12 13 Change-Id: If2969e131f0fae456d58b35d839d8abe191fcc59 14 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/454176 15 Reviewed-by: Cherry Mui <cherryyz@google.com> 16 Reviewed-by: Ian Lance Taylor <iant@google.com> 17 TryBot-Bypass: Ian Lance Taylor <iant@google.com> 18 --- 19 libgo/Makefile.am | 1 + 20 libgo/Makefile.in | 6 +++++- 21 libgo/go/syscall/errstr.go | 22 ++++++++----------- 22 libgo/go/syscall/errstr_glibc.go | 33 ---------------------------- 23 libgo/runtime/go-strerror.c | 37 ++++++++++++++++++++++++++++++++ 24 5 files changed, 52 insertions(+), 47 deletions(-) 25 delete mode 100644 libgo/go/syscall/errstr_glibc.go 26 create mode 100644 libgo/runtime/go-strerror.c 27 28 diff --git a/libgo/Makefile.am b/libgo/Makefile.am 29 index e0a1eec5..df568743 100644 30 --- a/libgo/Makefile.am 31 +++ b/libgo/Makefile.am 32 @@ -465,6 +465,7 @@ runtime_files = \ 33 runtime/go-nanotime.c \ 34 runtime/go-now.c \ 35 runtime/go-nosys.c \ 36 + runtime/go-strerror.c \ 37 runtime/go-reflect-call.c \ 38 runtime/go-setenv.c \ 39 runtime/go-signal.c \ 40 diff --git a/libgo/Makefile.in b/libgo/Makefile.in 41 index 7bef5df9..f2f3f0fc 100644 42 --- a/libgo/Makefile.in 43 +++ b/libgo/Makefile.in 44 @@ -247,7 +247,7 @@ am__objects_4 = runtime/aeshash.lo runtime/go-assert.lo \ 45 runtime/go-fieldtrack.lo runtime/go-matherr.lo \ 46 runtime/go-memclr.lo runtime/go-memmove.lo \ 47 runtime/go-memequal.lo runtime/go-nanotime.lo \ 48 - runtime/go-now.lo runtime/go-nosys.lo \ 49 + runtime/go-now.lo runtime/go-nosys.lo runtime/go-strerror.lo \ 50 runtime/go-reflect-call.lo runtime/go-setenv.lo \ 51 runtime/go-signal.lo runtime/go-unsafe-pointer.lo \ 52 runtime/go-unsetenv.lo runtime/go-unwind.lo \ 53 @@ -917,6 +917,7 @@ runtime_files = \ 54 runtime/go-nanotime.c \ 55 runtime/go-now.c \ 56 runtime/go-nosys.c \ 57 + runtime/go-strerror.c \ 58 runtime/go-reflect-call.c \ 59 runtime/go-setenv.c \ 60 runtime/go-signal.c \ 61 @@ -1390,6 +1391,8 @@ runtime/go-now.lo: runtime/$(am__dirstamp) \ 62 runtime/$(DEPDIR)/$(am__dirstamp) 63 runtime/go-nosys.lo: runtime/$(am__dirstamp) \ 64 runtime/$(DEPDIR)/$(am__dirstamp) 65 +runtime/go-strerror.lo: runtime/$(am__dirstamp) \ 66 + runtime/$(DEPDIR)/$(am__dirstamp) 67 runtime/go-reflect-call.lo: runtime/$(am__dirstamp) \ 68 runtime/$(DEPDIR)/$(am__dirstamp) 69 runtime/go-setenv.lo: runtime/$(am__dirstamp) \ 70 @@ -1457,6 +1460,7 @@ distclean-compile: 71 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-reflect-call.Plo@am__quote@ 72 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-setenv.Plo@am__quote@ 73 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-signal.Plo@am__quote@ 74 +@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-strerror.Plo@am__quote@ 75 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-unsafe-pointer.Plo@am__quote@ 76 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-unsetenv.Plo@am__quote@ 77 @AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-unwind.Plo@am__quote@ 78 diff --git a/libgo/go/syscall/errstr.go b/libgo/go/syscall/errstr.go 79 index 6c2441d3..9f688e2a 100644 80 --- a/libgo/go/syscall/errstr.go 81 +++ b/libgo/go/syscall/errstr.go 82 @@ -4,23 +4,19 @@ 83 // Use of this source code is governed by a BSD-style 84 // license that can be found in the LICENSE file. 85 86 -// +build !hurd 87 -// +build !linux 88 - 89 package syscall 90 91 -//sysnb strerror_r(errnum int, buf []byte) (err Errno) 92 -//strerror_r(errnum _C_int, buf *byte, buflen Size_t) _C_int 93 +import "internal/bytealg" 94 + 95 +//extern go_strerror 96 +func go_strerror(_C_int, *byte, Size_t) _C_int 97 98 func Errstr(errnum int) string { 99 - for len := 128; ; len *= 2 { 100 - b := make([]byte, len) 101 - errno := strerror_r(errnum, b) 102 + for size := 128; ; size *= 2 { 103 + b := make([]byte, size) 104 + errno := go_strerror(_C_int(errnum), &b[0], Size_t(len(b))) 105 if errno == 0 { 106 - i := 0 107 - for b[i] != 0 { 108 - i++ 109 - } 110 + i := bytealg.IndexByte(b, 0) 111 // Lowercase first letter: Bad -> bad, but 112 // STREAM -> STREAM. 113 if i > 1 && 'A' <= b[0] && b[0] <= 'Z' && 'a' <= b[1] && b[1] <= 'z' { 114 @@ -29,7 +25,7 @@ func Errstr(errnum int) string { 115 return string(b[:i]) 116 } 117 if errno != ERANGE { 118 - return "errstr failure" 119 + return "strerror_r failure" 120 } 121 } 122 } 123 diff --git a/libgo/go/syscall/errstr_glibc.go b/libgo/go/syscall/errstr_glibc.go 124 deleted file mode 100644 125 index 5b19e6f2..00000000 126 --- a/libgo/go/syscall/errstr_glibc.go 127 +++ /dev/null 128 @@ -1,33 +0,0 @@ 129 -// errstr_glibc.go -- GNU/Linux and GNU/Hurd specific error strings. 130 - 131 -// Copyright 2010 The Go Authors. All rights reserved. 132 -// Use of this source code is governed by a BSD-style 133 -// license that can be found in the LICENSE file. 134 - 135 -// We use this rather than errstr.go because on GNU/Linux sterror_r 136 -// returns a pointer to the error message, and may not use buf at all. 137 - 138 -// +build hurd linux 139 - 140 -package syscall 141 - 142 -import "unsafe" 143 - 144 -//sysnb strerror_r(errnum int, b []byte) (errstr *byte) 145 -//strerror_r(errnum _C_int, b *byte, len Size_t) *byte 146 - 147 -func Errstr(errnum int) string { 148 - a := make([]byte, 128) 149 - p := strerror_r(errnum, a) 150 - b := (*[1000]byte)(unsafe.Pointer(p)) 151 - i := 0 152 - for b[i] != 0 { 153 - i++ 154 - } 155 - // Lowercase first letter: Bad -> bad, but STREAM -> STREAM. 156 - if i > 1 && 'A' <= b[0] && b[0] <= 'Z' && 'a' <= b[1] && b[1] <= 'z' { 157 - c := b[0] + 'a' - 'A' 158 - return string(c) + string(b[1:i]) 159 - } 160 - return string(b[:i]) 161 -} 162 diff --git a/libgo/runtime/go-strerror.c b/libgo/runtime/go-strerror.c 163 new file mode 100644 164 index 00000000..13d1d91d 165 --- /dev/null 166 +++ b/libgo/runtime/go-strerror.c 167 @@ -0,0 +1,37 @@ 168 +/* go-strerror.c -- wrapper around XSI-compliant strerror_r. 169 + 170 + Copyright 2022 The Go Authors. All rights reserved. 171 + Use of this source code is governed by a BSD-style 172 + license that can be found in the LICENSE file. */ 173 + 174 +/* There are two version of strerror_r on GNU/Linux: a GNU-specific 175 + and an XSI-compliant version. The former version is only available 176 + on glibc. Since glibc 2.13, the XSI-compliant version is also 177 + provided by glibc if _GNU_SOURCE is not defined. Since the 178 + entirety of gofrontend is compiled with _GNU_SOURCE, this file 179 + exists to selectively undefine it and provides an alias to the 180 + XSI-compliant version of strerror_r(3). */ 181 + 182 +#ifdef __linux__ 183 + 184 +/* Force selection of XSI-compliant strerror_r by glibc. */ 185 +#undef XOPEN_SOURCE 186 +#define XOPEN_SOURCE 600 187 +#undef _POSIX_C_SOURCE 188 +#define _POSIX_C_SOURCE 200112L 189 +#undef _GNU_SOURCE 190 + 191 +#endif /* __linux__ */ 192 + 193 +#include <string.h> 194 + 195 +#ifndef HAVE_STRERROR_R 196 +// Provided by go-nosys.c if not provided by libc itself. 197 +extern int strerror_r (int, char *, size_t); 198 +#endif 199 + 200 +int 201 +go_strerror (int errnum, char *buf, size_t buflen) 202 +{ 203 + return strerror_r (errnum, buf, buflen); 204 +}