Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

os: optimise cgo clearenv #70672

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

Open
cyphar wants to merge 1 commit into golang:master
base: master
Choose a base branch
Loading
from cyphar:os-clearenv-optimisation
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/runtime/cgo/clearenv.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2025 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.

//go:build linux

package cgo

import _ "unsafe" // for go:linkname

//go:cgo_import_static x_cgo_clearenv
//go:linkname x_cgo_clearenv x_cgo_clearenv
//go:linkname _cgo_clearenv runtime._cgo_clearenv
var x_cgo_clearenv byte
var _cgo_clearenv = &x_cgo_clearenv
19 changes: 19 additions & 0 deletions src/runtime/cgo/gcc_clearenv.c
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2025 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.

//go:build linux

#include "libcgo.h"

#include <stdlib.h>

/* Stub for calling clearenv */
void
x_cgo_clearenv(void **_unused)
{
_cgo_tsan_acquire();
clearenv();
_cgo_tsan_release();
}

Copy link
Contributor

@kolyshkin kolyshkin Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra newline

29 changes: 29 additions & 0 deletions src/runtime/runtime_clearenv.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2025 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.

//go:build linux

package runtime

import "unsafe"

var _cgo_clearenv unsafe.Pointer // pointer to C function

// Clear the C environment if cgo is loaded.
func clearenv_c() {
if _cgo_clearenv == nil {
return
}
asmcgocall(_cgo_clearenv, nil)
}

//go:linkname syscall_runtimeClearenv syscall.runtimeClearenv
func syscall_runtimeClearenv(env map[string]int) {
clearenv_c()
// Did we just unset GODEBUG?
if _, ok := env["GODEBUG"]; ok {
godebugEnv.Store(nil)
godebugNotify(true)
}
}
18 changes: 18 additions & 0 deletions src/runtime/runtime_noclearenv.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2025 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.

//go:build !linux

package runtime

import _ "unsafe" // for go:linkname

//go:linkname syscall_runtimeClearenv syscall.runtimeClearenv
func syscall_runtimeClearenv(env map[string]int) {
// The system doesn't have clearenv(3) so emulate it by unsetting all of
// the variables manually.
for k := range env {
syscall_runtimeUnsetenv(k)
}
}
5 changes: 2 additions & 3 deletions src/syscall/env_unix.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ func Clearenv() {
envLock.Lock()
defer envLock.Unlock()

for k := range env {
runtimeUnsetenv(k)
}
runtimeClearenv(env)

env = make(map[string]int)
envs = []string{}
}
Expand Down
4 changes: 4 additions & 0 deletions src/syscall/syscall.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@ func Exit(code int)
// runtimeSetenv and runtimeUnsetenv are provided by the runtime.
func runtimeSetenv(k, v string)
func runtimeUnsetenv(k string)

// runtimeClearenv is provided by the runtime (on platforms without
// clearenv(3), it is just a wrapper around runtimeUnsetenv).
func runtimeClearenv(env map[string]int)

AltStyle によって変換されたページ (->オリジナル) /