Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: cgo can't set new environment variables for Go #44108

Closed
rakeshlacework opened this issue Feb 4, 2021 · 7 comments
Closed

runtime: cgo can't set new environment variables for Go #44108

rakeshlacework opened this issue Feb 4, 2021 · 7 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@rakeshlacework
Copy link

rakeshlacework commented Feb 4, 2021

What version of Go are you using (go version)?

$ go version
go version go1.15.6 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/works/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/works"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go-1.15.6"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go-1.15.6/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build987882216=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. Set environment variable before invoking the executable
  2. Overwrite and add new environment variable in cgo constructor
  3. print them in main

Here is the source code (also on playground does not work https://play.golang.org/p/knyUM81kREi)

vm: 17:11:38 ~/go/src/clear_env$ cat main.go
package main
/*
#cgo CFLAGS: -Werror -I/usr/include
#cgo LDFLAGS: -L/usr/lib64/ -L/usr/lib/x86_64-linux-gnu/ -ldl
#include <stdio.h>
#include <stdlib.h>
__attribute__((constructor)) void setup_env(void) {
	clearenv();
	setenv("EXISTING_VAR", "newValue", 1);
	setenv("NEW_VAR", "newValue", 1);
}
void print_env(void) {
	printf("C> PATH='%s'\n", getenv("PATH"));
	printf("C> EXISTING_VAR='%s'\n", getenv("EXISTING_VAR"));
	printf("C> NEW_VAR='%s'\n", getenv("NEW_VAR"));
}
*/
import "C"
import (
	"fmt"
	"os"
)
func main() {
	fmt.Printf("GO> PATH='%v'\n", os.Getenv("PATH"))
	fmt.Printf("GO> EXISTING_VAR='%v'\n", os.Getenv("EXISTING_VAR"))
	fmt.Printf("GO> NEW_VAR='%v'\n", os.Getenv("NEW_VAR"))
	C.print_env()
}

What did you expect to see?

vm: 17:11:42 ~/go/src/clear_env$ EXISTING_VAR="oldValue" ./main
GO> PATH='/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin'
GO> EXISTING_VAR='newValue'
GO> NEW_VAR='newValue'
C> PATH='/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin'
C> EXISTING_VAR='newValue'
C> NEW_VAR='newValue'

What did you see instead?

vm: 17:11:42 ~/go/src/clear_env$ EXISTING_VAR="oldValue" ./main
GO> PATH='/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin'
GO> EXISTING_VAR='oldValue'
GO> NEW_VAR=''
C> PATH='(null)'
C> EXISTING_VAR='newValue'
C> NEW_VAR='newValue'
@seankhliao
Copy link
Member

given that __attribute__((constructor)) is 1. a gcc compiler extension 2. only run when it is loaded, I don't see why it should run before main in go

@seankhliao seankhliao changed the title Discrepancy with set/get environment variable usage cmd/cgo: __attribute__((constructor)) not run before main Feb 4, 2021
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 4, 2021
@deepak-lacework
Copy link

@seankhliao I don't think the problem is with __attribute__((constructor)) not running before Go main (or C main). Here's a slightly different code that illustrates the problem better. Notice that Go reports the new value forEXISTING_VAR but old value for NEW_VAR while C/libc reports new values for both.

package main
/*
#cgo CFLAGS: -Werror -I/usr/include
#cgo LDFLAGS: -L/usr/lib64/ -L/usr/lib/x86_64-linux-gnu/ -ldl
#include <stdio.h>
#include <stdlib.h>
__attribute__((constructor)) void setup_env(void) {
	setenv("EXISTING_VAR", "newValue", 1);
	setenv("NEW_VAR", "newValue", 1);
}
void print_env(void) {
	printf("C> EXISTING_VAR='%s'\n", getenv("EXISTING_VAR"));
	printf("C> NEW_VAR='%s'\n", getenv("NEW_VAR"));
}
*/
import "C"
import (
	"fmt"
	"os"
)
func main() {
	fmt.Printf("GO> EXISTING_VAR='%v'\n", os.Getenv("EXISTING_VAR"))
	fmt.Printf("GO> NEW_VAR='%v'\n", os.Getenv("NEW_VAR"))
	C.print_env()
}

Here’s how to run the code to repro the issue. Notice the missing value for NEW_VAR when accessed via Go’s os package.

$ EXISTING_VAR="oldValue" ./main
GO> EXISTING_VAR='newValue'
GO> NEW_VAR=''
C> EXISTING_VAR='newValue'
C> NEW_VAR='newValue'

@seankhliao seankhliao changed the title cmd/cgo: __attribute__((constructor)) not run before main runtime: cgo can't set new environment variables for Go Feb 4, 2021
@seankhliao
Copy link
Member

I see, I think it's related to #27693 but not sure if it's the same issue

@deepak-lacework
Copy link

I agree, this is similar to #27693. The only difference is that in #27693, C.setenv after Go runtime is setup while in this case it is being called before Go runtime is setup. It is ok to not support the former because C.setenv isn't thread-safe.

Looking at the Go runtime setup code, it gets the environment from the arguments to C main. If instead Go runtime setup code get the environment from char **environ in libc, it should see the changes made to the environment by any preceding C code.

@ianlancetaylor
Copy link
Member

I think it's OK for us to just say that the behavior here is implementation defined. Especially when we start considering the behavior with -buildmode=c-archive, there is no perfect solution here.

For what it's worth, this is what I see for the first program:

GO> EXISTING_VAR=''
GO> NEW_VAR=''
C> PATH='(null)'
C> EXISTING_VAR='newValue'
C> NEW_VAR='newValue'

@deepak-lacework
Copy link

Thanks @ianlancetaylor.
Yes, I see the same output as you from the first program.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 7, 2022
@mknyszek mknyszek moved this to Triage Backlog in Go Compiler / Runtime Jul 15, 2022
@seankhliao
Copy link
Member

Closing as working as intended

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 20, 2022
Repository owner moved this from Triage Backlog to Done in Go Compiler / Runtime Aug 20, 2022
@golang golang locked and limited conversation to collaborators Aug 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants