aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/cgo
AgeCommit message (Collapse)Author
2026-03-31runtime/cgo: remove SIGSEGV emulation on iOSqmuntal
The misc/ios exec wrapper doesn't support lldb since https://go.dev/cl/573175. This means that the SIGSEGV emulation code in the cgo signal handling on iOS is no longer needed, and can be removed. Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64_26 Change-Id: I39827cb20756e4730352d87cb3514bb6a3f1cee8 Reviewed-on: https://go-review.googlesource.com/c/go/+/756800 Reviewed-by: Junyang Shao <shaojunyang@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2026-03-30runtime/cgo: remove unused _cgo_openbsd_try_pthread_create prototypeTobias Klauser
The function is no longer defined since CL 40331. Remove its prototype from libcgo_unix.h as well. Change-Id: I155ca632cd61bddcd798def691856c04b03040d7 Reviewed-on: https://go-review.googlesource.com/c/go/+/760163 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Florian Lehner <lehner.florian86@gmail.com> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2026-03-24cmd/link: raise minimum windows version to 10Jason A. Donenfeld
The minimum Windows version has been 10 for a few releases, but the PE headers weren't updated. Windows sometimes can use these in determining what kind of subsystem compatibility hacks to apply, which of course we don't want now, since Go targets Windows 10. This also causes older OSes to refuse to run the executables, rather than having them crash in some undefined way. This isn't trivial to do, because subsystem ≥ 10.0 means that the Windows loader expects to see either _load_config_used.SecurityCookie set to the initial magic value, or for IMAGE_GUARD_SECURITY_COOKIE_UNUSED to be set. Go obviously isn't making use of these features, and neither does clang/gcc for that matter; libssp doesn't even use SecurityCookie. Rather, it's exclusively for MSVC's /GS protection. So it seems like the proper thing to do is signal to the OS that it doesn't need to initialize SecurityCookie. This check lives in ntdll!LdrInitSecurityCookie. So, add the _load_config_used structure to the right PE section and give it the right flag. This lets the Windows 10-marked binaries actually run. Change-Id: I91887073c7ad01aeb0237906aafa4ea5574ac8fa Reviewed-on: https://go-review.googlesource.com/c/go/+/756680 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Jason Donenfeld <Jason@zx2c4.com> Reviewed-by: Jason Donenfeld <Jason@zx2c4.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-18runtime,runtime/cgo: do cgo thread initialization in Go on Windowsqmuntal
Windows doesn't require any special handling for cgo threads. They can be created in the same way as in non-cgo code. In fact, the code to create threads in runtime and in runtime/cgo is basically the same, except that the latter does some retries on failure. Cq-Include-Trybots: luci.golang.try:gotip-windows-amd64-longtest,gotip-windows-amd64-race,gotip-windows-arm64 Change-Id: I49d4de93d4d3b07a4c89e2bfb6b7302c6dfb9877 Reviewed-on: https://go-review.googlesource.com/c/go/+/746300 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-18runtime: implement part of library initialization in Goqmuntal
All architectures supporting c-shared and c-archive share the same initialization code in assembly, and most of it can be implemented in pure Go. Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-ppc64le_power10,gotip-linux-riscv64,gotip-linux-loong64,gotip-linux-s390x Change-Id: Iaa9fb7d6f9ca8785f1098461646d607ef6b00d47 Reviewed-on: https://go-review.googlesource.com/c/go/+/706417 Auto-Submit: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
2026-03-11runtime/cgo: clarify pinning requirement in Handle examplekovan
Add a comment to the void* example in the Handle documentation highlighting that if the C code keeps the pointer after the call returns, runtime.Pinner should be used to pin it. The existing documentation already mentions this requirement in prose (lines 69-73), but adding a comment directly in the example makes it more visible and less likely to be missed when copying the example. Fixes #68044 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Change-Id: I20a5b1ad10de1b441980dc7ed3185720e77912bb Reviewed-on: https://go-review.googlesource.com/c/go/+/741443 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-10syscall,runtime/cgo: use cgo_ldflag to link against -lsocket and -lxnet on ↵qmuntal
Solaris The -lsocket and -lxnet flags are needed for accept4 syscall on Solaris. The runtime/cgo package doens't use them, so it doesn't make sense for it to explicitly link against those libraries. Instead, use the //go:cgo_ldflag in the syscall package. Cq-Include-Trybots: luci.golang.try:gotip-solaris-amd64 Change-Id: I10db524ebf1c720a460515d8c1f362b0070bd771 Reviewed-on: https://go-review.googlesource.com/c/go/+/751760 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Mark Freeman <markfreeman@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Auto-Submit: Quim Muntal <quimmuntal@gmail.com>
2026-02-28runtime/cgo: build with C compiler option -std=gnu90Ian Lance Taylor
This will ensure we remain portable. We use gnu90 rather than c90 because c90 doesn't permit C++ style line comments, and that is just too painful. Change-Id: Ia0e52c4aa75493ceb3e8ce383cdd2ec85afa0bd2 Reviewed-on: https://go-review.googlesource.com/c/go/+/653138 Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Junyang Shao <shaojunyang@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-02-28runtime/cgo: add abi_riscv64.h for callee-saved register macrosGeorge Adams
Add abi_riscv64.h with SAVE_GPR/SAVE_FPR macros for saving and restoring the RISC-V callee-saved registers, following the pattern established by the other abi_*.h headers. Refactor asm_riscv64.s (cgo crosscall2), asm_riscv64.s (runtime _rt0_riscv64_lib), and race_riscv64.s (racecallbackthunk) to use the new macros, replacing inline register save/restore sequences. Change-Id: I83cef17a35c57fc8b5e045e7181460283ae5b423 Reviewed-on: https://go-review.googlesource.com/c/go/+/749900 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Russ Cox <rsc@golang.org>
2026-02-27runtime/cgo: avoid unused parameter warningTamir Duberstein
Annotate the unused parameter with `__attribute__((unused))` to avoid build failures in environments that treat unused parameters as errors. We see this when using `rules_go`: ``` @@rules_go+//:stdlib builder failed: error executing GoStdlib command (from stdlib rule target @@rules_go+//:stdlib) bazel-out/linux_host_platform-opt-exec-ST-f2d1f3f5d18d/bin/external/rules_go++go_sdk+main___download_0/builder_reset/builder stdlib -sdk external/rules_go++go_sdk+main___download_0 -goroot ... (remaining 16 arguments skipped) Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging stderr: # runtime/cgo gcc_clearenv.c:13:23: error: unused parameter '_unused' [-Werror,-Wunused-parameter] stdlib: error running subcommand external/rules_go++go_sdk+main___download_0/bin/go: exit status 1 ``` This matches the style used in `src/runtime/cgo/gcc_libinit.c` since dc2ae2886fbcd2297d2a0ea67a5d220ae2c74152 which fixed an identical issue. Change-Id: I8c805962b88af480839f8662a1fcf4ed1e5d574d GitHub-Last-Rev: b99fbda4f6fe7a199a59af425e997111137262a4 GitHub-Pull-Request: golang/go#77847 Reviewed-on: https://go-review.googlesource.com/c/go/+/749840 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: David Chase <drchase@google.com>
2026-02-23runtime/cgo: fix setg_gcc call on aix/ppc64qmuntal
aix/ppc64 uses function descriptors instead of function pointers. CL 708235 assumed the latter, which caused a crash when calling setg_gcc. Fix the call by dereferencing the function descriptor to get the entry point and TOC. Tested using gomote, as LUCI builders still don't support aix/ppc64. Fixes #77753. Change-Id: I6a0a6e971341f18f400c044f47b0daa42b93599e Reviewed-on: https://go-review.googlesource.com/c/go/+/747960 Run-TryBot: Paul Murphy <paumurph@redhat.com> Reviewed-by: Paul Murphy <paumurph@redhat.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Mark Freeman <markfreeman@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com>
2026-02-19runtime/cgo: remove unnecessary ARM_TP_ADDRESS check on FreeBSDqmuntal
ARM_TP_ADDRESS has not been used since https://golang.org/cl/89150043. There is no need to check that value anymore. Change-Id: I9772de3587aec562c9ab48d0def11e3714e5263a Reviewed-on: https://go-review.googlesource.com/c/go/+/745603 Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Pratt <mpratt@google.com> Auto-Submit: Quim Muntal <quimmuntal@gmail.com>
2026-02-18runtime/cgo: deduplicate Android's fatalfqmuntal
Most of the fatalf code is shared between Android and other Unix-like platforms, but Android has some special handling for the case where the fatalf is called from an .apk file. Put the Android-specific code in the generic fatalf code, and implement the Android-specific handling using a #ifdef guard. Change-Id: Ic7a49792648260c83b213380b77c0c352e9486f0 Reviewed-on: https://go-review.googlesource.com/c/go/+/745604 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Auto-Submit: Quim Muntal <quimmuntal@gmail.com>
2026-02-18runtime/cgo: use pthreads to get stack bound on Solarisqmuntal
x_cgo_getstackbound already supports Solaris using pthread_getattr_np, which is more robust than getcontext. Cq-Include-Trybots: luci.golang.try:gotip-solaris-amd64 Change-Id: Ib96d9c30df3413b4c5044a1b2c392b25835c8004 Reviewed-on: https://go-review.googlesource.com/c/go/+/745602 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2026-02-18runtime/cgo: rename and update ppc64x and s390x croscall1 functionsqmuntal
crosscall1 has the same signature and behavior on all platforms except for ppc64x and s390x, fix that. Cq-Include-Trybots: luci.golang.try:gotip-linux-ppc64le_power9,gotip-linux-ppc64_power8,gotip-linux-s390x Change-Id: Iead8b578a45787bb1fb2dd2dcfcb181514595637 Reviewed-on: https://go-review.googlesource.com/c/go/+/708235 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2026-02-17runtime/cgo: remove clang mmap bug workaroundqmuntal
Clang 3.8 was released in 2016, that is 10 years ago. The issue that the workaround was addressing (a bug in the memory sanitizer) is no longer relevant. Change-Id: I277ea94cf1bfbc6a9da63841be8ac990c839d7b7 Reviewed-on: https://go-review.googlesource.com/c/go/+/745662 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Mark Freeman <markfreeman@google.com>
2026-02-17all: use LF line ending for C filesCherry Mui
For Go files, gofmt already converts CRLF line ending to LF. For C and assembly files, we don't enforce a format, but most files in tree are written with LF line ending, and the C toolchain can handle them file, even on Windows. Convert all to LF line ending for consistency. Will look into adding a test for them. Updates #9281. Change-Id: Idc0dc13f0ab90b8cd8ea118abf9cb195ec438fe7 Reviewed-on: https://go-review.googlesource.com/c/go/+/746220 Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-02-13runtime/cgo: deduplicate x_cgo_init and crosscall1qmuntal
Most platforms share the same implementation for x_cgo_init and crosscall1. Solaris diverges too much and is left for a future CL. Cq-Include-Trybots: luci.golang.try:gotip-freebsd-amd64,gotip-darwin-amd64_14,gotip-darwin-arm64_15,gotip-netbsd-arm64,gotip-openbsd-ppc64,gotip-solaris-amd64,gotip-linux-ppc64_power10 Change-Id: Ib2eeb6456caa5c055e1ac1907c2fdf63db58dafc Reviewed-on: https://go-review.googlesource.com/c/go/+/708035 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Junyang Shao <shaojunyang@google.com>
2026-02-13runtime/cgo: deduplicate pthreads-related functionsqmuntal
Almost all pthread-related functions are exactly the same for Unix OSes. Their implementation can be shared, taking into account the small differences using standard predefined macros. Cq-Include-Trybots: luci.golang.try:gotip-freebsd-amd64,gotip-darwin-amd64_14,gotip-darwin-arm64_15,gotip-netbsd-arm64,gotip-openbsd-amd64,gotip-openbsd-ppc64,gotip-solaris-amd64,gotip-linux-ppc64_power10 Change-Id: I8bee25f0619a5b315439cf12d94312c36c3e5a73 Reviewed-on: https://go-review.googlesource.com/c/go/+/707955 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-09-29cmd/link: use a .def file to mark exported symbols on Windowsqmuntal
Binutils defaults to exporting all symbols when building a Windows DLL. To avoid that we were marking symbols with __declspec(dllexport) in the cgo-generated headers, which instructs ld to export only those symbols. However, that approach makes the headers hard to reuse when importing the resulting DLL into other projects, as imported symbols should be marked with __declspec(dllimport). A better approach is to generate a .def file listing the symbols to export, which gets the same effect without having to modify the headers. Updates #30674 Fixes #56994 Change-Id: I22bd0aa079e2be4ae43b13d893f6b804eaeddabf Reviewed-on: https://go-review.googlesource.com/c/go/+/705776 Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Junyang Shao <shaojunyang@google.com> Reviewed-by: Than McIntosh <thanm@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-09-25runtime: acquire/release C TSAN lock when calling cgo symbolizer/tracebackerMichael Pratt
When calling into C via cmd/cgo, the generated code calls _cgo_tsan_acquire / _cgo_tsan_release around the C call to report a dummy lock to the C/C++ TSAN runtime. This is necessary because the C/C++ TSAN runtime does not understand synchronization within Go and would otherwise report false positive race reports. See the comment in cmd/cgo/out.go for more details. Various C functions in runtime/cgo also contain manual calls to _cgo_tsan_acquire/release where necessary to suppress race reports. However, the cgo symbolizer and cgo traceback functions called from callCgoSymbolizer and cgoContextPCs, respectively, do not have any instrumentation [1]. They call directly into user C functions with no TSAN instrumentation. This means they have an opportunity to report false race conditions. The most direct way is via their argument. Both are passed a pointer to a struct stored on the Go stack, and both write to fields of the struct. If two calls are passed the same pointer from different threads, the C TSAN runtime will think this is a race. This is simple to achieve for the cgo symbolizer function, which the new regression test does. callCgoSymbolizer is called on the standard goroutine stack, so the argument is a pointer into the goroutine stack. If the goroutine moves Ms between two calls, it will look like a race. On the other hand, cgoContextPCs is called on the system stack. Each M has a unique system stack, so for it to pass the same argument pointer on different threads would require the first M to exit, free its stack, and the same region of address space to be used as the stack for a new M. Theoretically possible, but quite unlikely. Both of these are addressed by providing a C wrapper in runtime/cgo that calls _cgo_tsan_acquire/_cgo_tsan_release around calls to the symbolizer and traceback functions. There is a lot of room for future cleanup here. Most runtime/cgo functions have manual instrumentation in their C implementation. That could be removed in favor of instrumentation in the runtime. We could even theoretically remove the instrumentation from cmd/cgo and move it to cgocall. None of these are necessary, but may make things more consistent and easier to follow. [1] Note that the cgo traceback function called from the signal handler via x_cgo_callers _does_ have manual instrumentation. Fixes #73949. Cq-Include-Trybots: luci.golang.try:gotip-freebsd-amd64,gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Change-Id: I6a6a636c9daa38f7fd00694af76b75cb93ba1886 Reviewed-on: https://go-review.googlesource.com/c/go/+/677955 Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Michael Pratt <mpratt@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-09-23runtime/cgo: retry when CreateThread fails with ERROR_ACCESS_DENIEDqmuntal
_cgo_beginthread used to retry _beginthread only when it failed with EACCESS, but CL 651995 switched to CreateThread and incorrectly mapped EACCESS to ERROR_NOT_ENOUGH_MEMORY. The correct mapping is ERROR_ACCESS_DENIED. Fixes #72814 Fixes #75381 Change-Id: I8ba060114aae4e8249576f11a21eff613caa8001 Reviewed-on: https://go-review.googlesource.com/c/go/+/706075 Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2025-09-09runtime: when using cgo on 386, call C sigaction functionIan Lance Taylor
On 386 the C sigaction function assumes that the caller does not set the SA_RESTORER flag. It does not copy the C sa_restorer field to the kernel sa_restorer field. The effect is that the kernel sees the SA_RESTORER flag but a NULL sa_restorer field, and the program crashes when returning from a signal handler. On the other hand, the C sigaction function will return the SA_RESTORER flag and the sa_restorer field stored in the kernel. This means that if the Go runtime installs a signal handler, with SA_RESTORER as is required when calling the kernel, and the Go program calls C code that calls the C sigaction function to query the current signal handler, that C code will get a result that it can't pass back to sigaction. This CL fixes the problem by using the C sigaction function for 386 programs that use cgo. This reuses the functionality used on amd64 and other GOARCHs to support the race detector. See #75253, or runtime/testdata/testprogcgo/eintr.go, for sample code that used to fail on 386. No new test case is required, we just remove the skip we used to have for eintr.go. Fixes #75253 Change-Id: I803059b1fb9e09e9fbb43f68eccb6a59a92c2991 Reviewed-on: https://go-review.googlesource.com/c/go/+/701375 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org>
2025-09-09syscall: optimise cgo clearenvAleksa Sarai
For programs with very large environments, calling unsetenv(3) for each environment variable can be very expensive because of CGo overhead, but clearenv(3) is much faster. The only thing we have to track is whether GODEBUG is being unset by the operation, which can be done very quickly without resorting to doing unsetenv(3) for every variable. This change makes syscall.Clearenv() >98% faster when run in an environment with as little as 100 environment variables. (Note that due to golang/go#27217, it is necessary to modify BenchmarkClearenv to use t.StopTimer() and -benchtime=100x in order to get these benchmark times -- otherwise syscall.Setenv() time is included and the benchmarks give a more pessimistic 50% performance improvement.) goos: linux goarch: amd64 pkg: syscall cpu: AMD Ryzen 7 7840U w/ Radeon 780M Graphics │ before │ after │ │ sec/op │ sec/op vs base │ Clearenv/100-16 22276.5n ± 5% 285.8n ± 3% -98.72% (p=0.000 n=10) Clearenv/1000-16 1414104.0n ± 1% 783.1n ± 8% -99.94% (p=0.000 n=10) Clearenv/10000-16 143827.554µ ± 1% 7.591µ ± 5% -99.99% (p=0.000 n=10) geomean 1.655m 1.193µ -99.93% The above benchmarks are CGo builds, which require CGo overhead for every setenv(2). If you run the same benchmarks for a non-CGo package (i.e., outside of the "syscall" package), you get slightly more modest performance improvements: goos: linux goarch: amd64 pkg: clearenv_nocgo cpu: AMD Ryzen 7 7840U w/ Radeon 780M Graphics │ before │ after │ │ sec/op │ sec/op vs base │ Clearenv/100-16 1106.0n ± 3% 230.7n ± 8% -79.14% (p=0.000 n=10) Clearenv/1000-16 11222.0n ± 1% 305.4n ± 6% -97.28% (p=0.000 n=10) Clearenv/10000-16 195676.5n ± 6% 759.9n ± 10% -99.61% (p=0.000 n=10) geomean 13.44µ 376.9n -97.20% (As above, this requires modifying the benchmarks to use t.StopTimer() and -benchtime=100x.) Change-Id: I53b96a75f189e91affbde423c907888b7e0fafcd GitHub-Last-Rev: f8d7a8140d8490189d726eb380522dccacc5f176 GitHub-Pull-Request: golang/go#70672 Reviewed-on: https://go-review.googlesource.com/c/go/+/633515 Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Kirill Kolyshkin <kolyshkin@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com>
2025-09-05runtime/cgo: save and restore R31 for crosscall1 on loong64Guoqi Chen
According to the Loong64 procedure call standard [1], R31 is a static register and therefore needs to be saved and restored. Also, the R2 (thread pointer) register has been removed here, as it is not involved in allocation. [1]: https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc Change-Id: I02e5d4bedf131e491f1a262aa3cbc0896cbc9488 Reviewed-on: https://go-review.googlesource.com/c/go/+/700817 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: sophie zhao <zhaoxiaolin@loongson.cn> Reviewed-by: Meidan Li <limeidan@loongson.cn> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2025-03-17runtime, runtime/cgo: use libc for sigaction syscalls when cgo is enabled on ↵limeidan
loong64 This ensures that runtime's signal handlers pass through the TSAN and MSAN libc interceptors and subsequent calls to the intercepted sigaction function from C will correctly see them. Change-Id: I243a70d9dcb6d95a65c8494d5f9f9f09a316c693 Reviewed-on: https://go-review.googlesource.com/c/go/+/654995 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: abner chenc <chenguoqi@loongson.cn> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-02-26runtime/cgo: use standard ABI call setg_gcc in crosscall1 on loong64Guoqi Chen
Change-Id: Ie38583d667d579751d643b2da2aa56390b69904c Reviewed-on: https://go-review.googlesource.com/c/go/+/652255 Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: sophie zhao <zhaoxiaolin@loongson.cn> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Meidan Li <limeidan@loongson.cn>
2025-02-26runtime/cgo: avoid errors from -Wdeclaration-after-statementIan Lance Taylor
CL 652181 accidentally missed this iPhone only code. For #71961 Change-Id: I567f8bb38958907442e69494da330d5199d11f54 Reviewed-on: https://go-review.googlesource.com/c/go/+/653135 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2025-02-26runtime/cgo: avoid errors from -Wdeclaration-after-statementIan Lance Taylor
It's used by the SWIG CI build, at least, and it's an easy fix. Fixes #71961 Change-Id: Id21071a5aef216b35ecf0e9cd3e05d08972d92fe Reviewed-on: https://go-review.googlesource.com/c/go/+/652181 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Michael Pratt <mpratt@google.com>
2025-02-25runtime/cgo: use CreateThread instead of _beginthreadqmuntal
_beginthread is intended to be used together with the C runtime. The cgo runtime doesn't use it, so better use CreateThread directly, which is the Windows API for creating threads. Cq-Include-Trybots: luci.golang.try:gotip-windows-amd64-longtest,gotip-windows-arm64 Change-Id: Ic6cf75f69f62a3babf5e74155da1aac70961886c Reviewed-on: https://go-review.googlesource.com/c/go/+/651995 Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-02-06runtime/cgo: clarify that C code must not retain pointerIan Lance Taylor
For #71566 Change-Id: I6dc365dd799d7b506b4a55895f1736d3dfd4684b Reviewed-on: https://go-review.googlesource.com/c/go/+/647095 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Commit-Queue: Ian Lance Taylor <iant@google.com>
2024-11-14runtime/cgo: report a meaningful error message when using Cygwinqmuntal
Go has never supported Cygwin as a C compiler, but users get the following cryptic error message when they try to use it: implicit declaration of function '_beginthread' This is because Cygwin doesn't implement _beginthread. Note that this is not the only problem with Cygwin, but it's the one that users are most likely to run into first. This CL improves the error message to make it clear that Cygwin is not supported, and suggests using MinGW instead. Fixes #59490 Fixes #36691 Change-Id: Ifeec7a2cb38d7c5f50d6362c95504f72818c6a76 Reviewed-on: https://go-review.googlesource.com/c/go/+/627935 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-11-07runtime/cgo: use pthread_getattr_np on AndroidCherry Mui
It is defined in bionic libc since at least API level 3. Use it. Updates #68285. Change-Id: I215c2d61d5612e7c0298b2cb69875690f8fbea66 Reviewed-on: https://go-review.googlesource.com/c/go/+/626275 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2024-10-30runtime: update and restore g0 stack bounds at cgocallbackCherry Mui
Currently, at a cgo callback where there is already a Go frame on the stack (i.e. C->Go->C->Go), we require that at the inner Go callback the SP is within the g0's stack bounds set by a previous callback. This is to prevent that the C code switches stack while having a Go frame on the stack, which we don't really support. But this could also happen when we cannot get accurate stack bounds, e.g. when pthread_getattr_np is not available. Since the stack bounds are just estimates based on the current SP, if there are multiple C->Go callbacks with various stack depth, it is possible that the SP of a later callback falls out of a previous call's estimate. This leads to runtime throw in a seemingly reasonable program. This CL changes it to save the old g0 stack bounds at cgocallback, update the bounds, and restore the old bounds at return. So each callback will get its own stack bounds based on the current SP, and when it returns, the outer callback has the its old stack bounds restored. Also, at a cgo callback when there is no Go frame on the stack, we currently always get new stack bounds. We do this because if we can only get estimated bounds based on the SP, and the stack depth varies a lot between two C->Go calls, the previous estimates may be off and we fall out or nearly fall out of the previous bounds. But this causes a performance problem: the pthread API to get accurate stack bounds (pthread_getattr_np) is very slow when called on the main thread. Getting the stack bounds every time significantly slows down repeated C->Go calls on the main thread. This CL fixes it by "caching" the stack bounds if they are accurate. I.e. at the second time Go calls into C, if the previous stack bounds are accurate, and the current SP is in bounds, we can be sure it is the same stack and we don't need to update the bounds. This avoids the repeated calls to pthread_getattr_np. If we cannot get the accurate bounds, we continue to update the stack bounds based on the SP, and that operation is very cheap. On a Linux/AMD64 machine with glibc: name old time/op new time/op delta CgoCallbackMainThread-8 96.4µs ± 3% 0.1µs ± 2% -99.92% (p=0.000 n=10+9) Fixes #68285. Fixes #68587. Change-Id: I3422badd5ad8ff63e1a733152d05fb7a44d5d435 Reviewed-on: https://go-review.googlesource.com/c/go/+/600296 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2024-08-13runtime/cgo: create C pthreads in detached stateIan Lance Taylor
Rather than explicitly calling pthread_detach. Fixes #68850 Change-Id: I7b4042283f9feb5383bffd40fae6db6d23217f97 Reviewed-on: https://go-review.googlesource.com/c/go/+/605257 Commit-Queue: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-07-23runtime,internal: move runtime/internal/sys to internal/runtime/sysDavid Chase
Cleanup and friction reduction For #65355. Change-Id: Ia14c9dc584a529a35b97801dd3e95b9acc99a511 Reviewed-on: https://go-review.googlesource.com/c/go/+/600436 Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org>
2024-05-24runtime: x_cgo_getstackbound: initialize pthread attrKir Kolyshkin
In glibc versions older than 2.32 (before commit 4721f95058), pthread_getattr_np does not always initialize the `attr` argument, and when it fails, it results in a NULL pointer dereference in pthread_attr_destroy down the road. This is the simplest way to avoid this, and an alternative to CL 585019. Updates #65625. Change-Id: If490fd37020b03eb084ebbdbf9ae0248916426d0 Reviewed-on: https://go-review.googlesource.com/c/go/+/587919 Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
2024-05-16runtime: always update stack bounds on cgocallbackMichael Pratt
callbackUpdateSystemStack contains a fast path to exit early without update if SP is already within the g0.stack bounds. This is not safe, as a subsequent call may have new stack bounds that only partially overlap the old stack bounds. In this case it is possible to see an SP that is in the old stack bounds, but very close to the bottom of the bounds due to the partial overlap. In that case we're very likely to "run out" of space on the system stack. We only need to do this on extra Ms, as normal Ms have precise bounds defined when we allocated the stack. TSAN annotations are added to x_cgo_getstackbounds because bounds is a pointer into the Go stack. The stack can be reused when an old thread exits and a new thread starts, but TSAN can't see the synchronization there. This isn't a new case, but we are now calling more often. Fixes #62440. Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest Change-Id: I5389050494987b7668d0b317fb92f85e61d798ac Reviewed-on: https://go-review.googlesource.com/c/go/+/584597 Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-03-21runtime/cgo: fix clang mach_port_t cast warningRoland Shoemaker
Converting *void directly to mach_port_t causes newer clang to throw a void-pointer-to-int-cast warning/error. Change-Id: I709955d4678bed3f690a8337ce85fd8678d217bb Reviewed-on: https://go-review.googlesource.com/c/go/+/573415 Auto-Submit: Roland Shoemaker <roland@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-29runtime/cgo: ignore unknown warning optionsIan Lance Taylor
For #65290 Fixes #65971 Change-Id: If15853f287e06b85bb1cb038b3785516d5812f84 Reviewed-on: https://go-review.googlesource.com/c/go/+/567556 Auto-Submit: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com> Reviewed-by: Bryan Mills <bcmills@google.com>
2024-02-13runtime/cgo: mark fatalf as noreturnMauri de Souza Meneguzzo
Fixes #64553 Change-Id: I7860cd9ba74d70a7d988538ea4df8e122f94cde6 GitHub-Last-Rev: 06164374734aef5b94566930426005ad66d0a5b6 GitHub-Pull-Request: golang/go#64727 Reviewed-on: https://go-review.googlesource.com/c/go/+/550115 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org> Run-TryBot: Mauri de Souza Meneguzzo <mauri870@gmail.com> Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-02-12runtime/cgo: add note about default stack boundsMichael Pratt
The default case in x_cgo_getstackbound does not actually get the stack bound of the current thread, but estimates the bound based on the default stack size. Add a comment noting this. Change-Id: I7d886461f0bbc795834bed37b554417cf3837a2d Reviewed-on: https://go-review.googlesource.com/c/go/+/563376 Auto-Submit: Michael Pratt <mpratt@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-10runtime/cgo: ignore -Watomic-alignment in gcc_libinit.cMauri de Souza Meneguzzo
When cross-compiling a cgo program with CC=clang for Linux/ARMv5, atomic warnings cause build errors, as cgo uses -Werror. These warnings seem to be harmless and come from the usage of __atomic_load_n, which is emulated due to the lack of atomic instructions in armv5. Fixes #65290 Change-Id: Ie72efb77468f06888f81f15850401dc8ce2c78f9 GitHub-Last-Rev: fbad847b962f6b4599cd843018e79f4b55be097e GitHub-Pull-Request: golang/go#65588 Reviewed-on: https://go-review.googlesource.com/c/go/+/562348 Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2023-11-20all: add floating point option for ARM targetsLudi Rehak
This change introduces new options to set the floating point mode on ARM targets. The GOARM version number can optionally be followed by ',hardfloat' or ',softfloat' to select whether to use hardware instructions or software emulation for floating point computations, respectively. For example, GOARM=7,softfloat. Previously, software floating point support was limited to GOARM=5. With these options, software floating point is now extended to all ARM versions, including GOARM=6 and 7. This change also extends hardware floating point to GOARM=5. GOARM=5 defaults to softfloat and GOARM=6 and 7 default to hardfloat. For #61588 Change-Id: I23dc86fbd0733b262004a2ed001e1032cf371e94 Reviewed-on: https://go-review.googlesource.com/c/go/+/514907 Run-TryBot: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com>
2023-11-08runtime: add available godoc linkcui fliter
Change-Id: Ifb4844efddcb0369b0302eeab72394eeaf5c8072 Reviewed-on: https://go-review.googlesource.com/c/go/+/540022 Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: shuang cui <imcusg@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-10-12runtime/cgo: avoid taking the address of crosscall2 in codeCherry Mui
Currently, set_crosscall2 takes the address of crosscall2 without using the GOT, which, on some architectures, results in a PC-relative relocation (e.g. R_AARCH64_ADR_PREL_PG_HI21 on ARM64) to the crosscall2 symbol. But crosscall2 is dynamically exported, so the C linker thinks it may bind to a symbol from a different DSO. Some C linker may not like a PC-relative relocation to such a symbol. Using a local trampoline to avoid taking the address of a dynamically exported symbol. It may be possible to not dynamically export crosscall2. But this CL is safer for backport. Later we may remove the trampolines after unexport crosscall2, if they are not needed. Fixes #62556. Change-Id: Id28457f65ef121d3f87d8189803abc65ed453283 Reviewed-on: https://go-review.googlesource.com/c/go/+/533535 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-10-05runtime: support SetUnhandledExceptionFilter on Windowsqmuntal
The Windows unhandled exception mechanism fails to call the callback set in SetUnhandledExceptionFilter if the stack can't be correctly unwound. Some cgo glue code was not properly chaining the frame pointer, making the stack unwind to fail in case of an exception inside a cgo call. This CL fix that and adds a test case to avoid regressions. Fixes #50951 Change-Id: Ic782b5257fe90b05e3def8dbf0bb8d4ed37a190b Reviewed-on: https://go-review.googlesource.com/c/go/+/525475 Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-08-25runtime, cmd/go: enable memory sanitizer on linux/loong64Xiaolin Zhao
The msan feature depends on llvm. The currently released llvm-16 already supports the LoongArch architecture, and msan's support for LoongArch64 has been added in this patch[1], and it has been merged in branches main and release/17.x. [1]: https://reviews.llvm.org/D140528 Change-Id: If537c5ffb1c9d4b3316b9b3794d411953bc5764b Reviewed-on: https://go-review.googlesource.com/c/go/+/481315 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: abner chenc <chenguoqi@loongson.cn> Run-TryBot: Ian Lance Taylor <iant@google.com> Run-TryBot: WANG Xuerui <git@xen0n.name>
2023-08-22runtime/cgo: get getstackbound for set_stackloIan Lance Taylor
Change-Id: Ia63a4604449b5e460e6f54c962fb7d6db2bc6a43 Reviewed-on: https://go-review.googlesource.com/c/go/+/519457 Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-08-15runtime/cgo: use fatalf on solarisJoel Sing
Change-Id: I3302cc2f0e03014e9497976e36d1c7a381a2f962 Reviewed-on: https://go-review.googlesource.com/c/go/+/518623 Run-TryBot: Joel Sing <joel@sing.id.au> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>