Skip to content

proposal: runtime: allow changing debug.cgocheck at runtime #60426

@spacewander

Description

@spacewander

debug.cgocheck is a configuration in the runtime package which controls whether to check cgo parameter. It's set by the GODEBUG=cgocheck=xxx. But unlike debug variable GODEBUG=panicnil=xxx, the cgocheck=xxx is only set once and can't be changed at runtime.

If a framework requires disabling cgocheck, as there is no way to change cgocheck=xxx at runtime, the framework developers have to require the user to set this env var manually which is inconvenient.

At first glance, it looks like we can implement this idea by changing

{name: "cgocheck", value: &debug.cgocheck},
to an atomic. Then the cgocheck=xxx can be updated like the panicnil.

Background

As mentioned in #56378 (comment), We are implementing the Envoy Golang filter extension by using cgo. Currently, we disable cgocheck for a better performance.

We do it by using the env var GODEBUG=cgocheck=0, but it is inconvenient for users to set up this variable. As discussed in envoyproxy/envoy#25178 (comment), we hope to change Go's code to allow disabling cgocheck at runtime.

Why we can't set the env var outside? Because Go's env vars are coming from the argv:

func goenvs_unix() {
// TODO(austin): ppc64 in dynamic linking mode doesn't
// guarantee env[] will immediately follow argv. Might cause
// problems.
n := int32(0)
for argv_index(argv, argc+1+n) != nil {
n++
}
envs = make([]string, n)
for i := int32(0); i < n; i++ {
envs[i] = gostring(argv_index(argv, argc+1+i))
}
}

when loading a Go library via dlopen, the argv is passed by libc with a pre-stored var (assigned in the _init function when the host starts). So there is no way for the host to add the env var.

Another solution is to use the //go:debug flag. Unfortunately, as a framework developer, we cannot control users’ main package or the compile flags.

If such a change is acceptable, I am willing to submit it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions