From 02fa1b3b25cd300bc96ee7f833b932c6a22acce1 Mon Sep 17 00:00:00 2001 From: Adam Benhassen Date: Sat, 7 Sep 2024 09:55:02 +0200 Subject: [PATCH] update --- src/runtime/cgo/gcc_libinit.c | 10 ---------- src/runtime/cgo/gcc_linux.c | 10 ++++++++++ src/runtime/cgo/gcc_linux_amd64.c | 10 ++++++++++ src/runtime/cgo/gcc_linux_arm64.c | 10 ++++++++++ src/runtime/os_linux.go | 2 +- src/runtime/proc.go | 12 +++++++++--- src/runtime/runtime1.go | 30 +++++++++++++++++++++++------- 7 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/runtime/cgo/gcc_libinit.c b/src/runtime/cgo/gcc_libinit.c index 53bcbefccc1..8fc7ae8e7f2 100644 --- a/src/runtime/cgo/gcc_libinit.c +++ b/src/runtime/cgo/gcc_libinit.c @@ -180,13 +180,3 @@ pthread_key_destructor(void* g) { x_crosscall2_ptr(NULL, g, 0, 0); } } - -// x_cgo_is_musl reports whether the C library is musl. -int -x_cgo_is_musl() { - #if defined(__GLIBC__) || defined(__UCLIBC__) - return 0; - #else - return 1; - #endif -} diff --git a/src/runtime/cgo/gcc_linux.c b/src/runtime/cgo/gcc_linux.c index fbdb5e652a2..c7e566cb16e 100644 --- a/src/runtime/cgo/gcc_linux.c +++ b/src/runtime/cgo/gcc_linux.c @@ -65,3 +65,13 @@ threadentry(void *v) crosscall1(ts.fn, setg_gcc, ts.g); return nil; } + +// x_cgo_is_musl reports whether the C library is musl. +int +x_cgo_is_musl() { + #if defined(__GLIBC__) || defined(__UCLIBC__) + return 0; + #else + return 1; + #endif +} diff --git a/src/runtime/cgo/gcc_linux_amd64.c b/src/runtime/cgo/gcc_linux_amd64.c index c81fde2887d..9364de78fac 100644 --- a/src/runtime/cgo/gcc_linux_amd64.c +++ b/src/runtime/cgo/gcc_linux_amd64.c @@ -90,3 +90,13 @@ threadentry(void *v) crosscall1(ts.fn, setg_gcc, (void*)ts.g); return nil; } + +// x_cgo_is_musl reports whether the C library is musl. +int +x_cgo_is_musl() { + #if defined(__GLIBC__) || defined(__UCLIBC__) + return 0; + #else + return 1; + #endif +} diff --git a/src/runtime/cgo/gcc_linux_arm64.c b/src/runtime/cgo/gcc_linux_arm64.c index ce46804ba29..85697470ee9 100644 --- a/src/runtime/cgo/gcc_linux_arm64.c +++ b/src/runtime/cgo/gcc_linux_arm64.c @@ -86,3 +86,13 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase) x_cgo_inittls(tlsg, tlsbase); } } + +// x_cgo_is_musl reports whether the C library is musl. +int +x_cgo_is_musl() { + #if defined(__GLIBC__) || defined(__UCLIBC__) + return 0; + #else + return 1; + #endif +} diff --git a/src/runtime/os_linux.go b/src/runtime/os_linux.go index f5a75543b0a..10b4370714b 100644 --- a/src/runtime/os_linux.go +++ b/src/runtime/os_linux.go @@ -234,7 +234,7 @@ func sysargs(argc int32, argv **byte) { n := argc + 1 // auxv on argv is not available on musl library/archive. avoid for musl. - if !((islibrary || isarchive) && isMusl()) { + if !((islibrary || isarchive) && GOOS == "linux" && isMusl()) { // skip over argv, envp to get to auxv for argv_index(argv, n) != nil { n++ diff --git a/src/runtime/proc.go b/src/runtime/proc.go index ce1a2c710ec..81ae24936d3 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -764,12 +764,18 @@ func getGodebugEarly() string { // Similar to goenv_unix but extracts the environment value for // GODEBUG directly. // TODO(moehrmann): remove when general goenvs() can be called before cpuinit() - if (isarchive || islibrary) && isMusl() { - for _, value := range fetch_from_fd(procEnviron) { - if hasPrefix(value, prefix) { + + // If the binary is an archive or a library, the operating system is Linux, + // and the system uses Musl, then read the environment variables from the + // /proc/self/environ file. Iterate over each null-terminated string read + // from the file. If any string has the specified prefix, return that string. + if (isarchive || islibrary) && GOOS == "linux" && isMusl() { + for _, value := range readNullTerminatedStringsFromFile(procEnviron) { + if stringslite.HasPrefix(value, prefix) { return value } } + return env } n := int32(0) diff --git a/src/runtime/runtime1.go b/src/runtime/runtime1.go index 6fa0668ad6b..628a93ff847 100644 --- a/src/runtime/runtime1.go +++ b/src/runtime/runtime1.go @@ -82,8 +82,9 @@ func goargs() { return } - if (isarchive || islibrary) && isMusl() { - argslice = fetch_from_fd(procCmdline) + // musl-linux library: Read argv from /proc/self/cmdline instead + if (isarchive || islibrary) && GOOS == "linux" && isMusl() { + argslice = readNullTerminatedStringsFromFile(procCmdline) return } @@ -94,8 +95,9 @@ func goargs() { } func goenvs_unix() { - if (isarchive || islibrary) && isMusl() { - envs = fetch_from_fd(procEnviron) + // musl-linux library: Read envs from /proc/self/environ instead + if (isarchive || islibrary) && GOOS == "linux" && isMusl() { + envs = readNullTerminatedStringsFromFile(procEnviron) return } @@ -113,15 +115,29 @@ func goenvs_unix() { } } -func fetch_from_fd(path []byte) []string { +// readNullTerminatedStringsFromFile reads a file specified by the given path +// and returns a slice of strings. Each string in the slice is null-terminated +// in the file. +// +// Parameters: +// - path: A null-terminated byte slice representing the file path. +// +// Returns: +// - A slice of strings read from the file, where each string is null-terminated. +// +// It opens the file, reads its contents in chunks, +// and parses the data into a slice of strings based on null-termination. +// +// Note: This function will return nil if the file cannot be opened. +func readNullTerminatedStringsFromFile(path []byte) []string { fd := open(&path[0], 0 /* O_RDONLY */, 0) if fd <= 0 { return nil } - // Read the file in 16-byte chunks. + // Read the file. var data []byte - var buf [16]byte + var buf [1024]byte for { n := read(fd, noescape(unsafe.Pointer(&buf[0])), int32(unsafe.Sizeof(buf))) if n <= 0 { // EOF