aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/cgo
AgeCommit message (Collapse)Author
2023-01-24runtime/cgo: use //go:build lines in C and assembly filesTobias Klauser
Replace deprecated // +build lines by their respective //go:build line counterpart. Also remove build constraints implied by file name or type. Change-Id: I8d18cd40071ca28d7654da8f0d22841f43729ca6 Reviewed-on: https://go-review.googlesource.com/c/go/+/460538 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-01-24runtime: use explicit NOFRAME on windows/amd64qmuntal
This CL marks non-leaf nosplit assembly functions as NOFRAME to avoid relying on the implicit amd64 NOFRAME heuristic, where NOSPLIT functions without stack were also marked as NOFRAME. Updates #57302 Updates #40044 Change-Id: Ia4d26f8420dcf2b54528969ffbf40a73f1315d61 Reviewed-on: https://go-review.googlesource.com/c/go/+/459395 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-01-23runtime,cmd/internal/obj/x86: use TEB TLS slots on windows/i386qmuntal
This CL redesign how we get the TLS pointer on windows/i386. It applies the same changes as done in CL 431775 for windows/amd64. We were previously reading it from the [TEB] arbitrary data slot, located at 0x14(FS), which can only hold 1 TLS pointer. With this CL, we will read the TLS pointer from the TEB TLS slot array, located at 0xE10(GS). The TLS slot array can hold multiple TLS pointers, up to 64, so multiple Go runtimes running on the same thread can coexists with different TLS. Each new TLS slot has to be allocated via [TlsAlloc], which returns the slot index. This index can then be used to get the slot offset from GS with the following formula: 0xE10 + index*4. The slot index is fixed per Go runtime, so we can store it in runtime.tls_g and use it latter on to read/update the TLS pointer. Loading the TLS pointer requires the following asm instructions: MOVQ runtime.tls_g, AX MOVQ AX(FS), AX Notice that this approach will now be implemented in all the supported windows arches. [TEB]: https://en.wikipedia.org/wiki/Win32_Thread_Information_Block [TlsAlloc]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc Change-Id: If4550b0d44694ee6480d4093b851f4991a088b32 Reviewed-on: https://go-review.googlesource.com/c/go/+/454675 Reviewed-by: Michael Pratt <mpratt@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-12-05runtime/cgo: fix typo in gcc_loong64.SCherry Mui
Fix typo in CL 454838. Change-Id: I0e91d22cf09949f0bf924ebcf09f1ddac525bac4 Reviewed-on: https://go-review.googlesource.com/c/go/+/455161 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2022-12-05runtime/cgo: add .file directive to GNU assembly filesCherry Mui
Without it, at least on ARM64 with older BFD linker, it will include the file of the object file (which is of a temporary path) as a debug symbol into the binary, causing the build to be nondeterministic. Adding a .file directive makes it to create a STT_FILE symbol with deterministic input, and prevent the linker creating one using the temporary object file path. Fixes #57035. Change-Id: I3ab716b240f60f7a891af2f7e10b467df67d1f31 Reviewed-on: https://go-review.googlesource.com/c/go/+/454838 Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
2022-11-14runtime,cmd/internal/obj/x86: use TEB TLS slots on windows/amd64qmuntal
This CL redesign how we get the TLS pointer on windows/amd64. We were previously reading it from the [TEB] arbitrary data slot, located at 0x28(GS), which can only hold 1 TLS pointer. With this CL, we will read the TLS pointer from the TEB TLS slot array, located at 0x1480(GS). The TLS slot array can hold multiple TLS pointers, up to 64, so multiple Go runtimes running on the same thread can coexists with different TLS. Each new TLS slot has to be allocated via [TlsAlloc], which returns the slot index. This index can then be used to get the slot offset from GS with the following formula: 0x1480 + index*8 The slot index is fixed per Go runtime, so we can store it in runtime.tls_g and use it latter on to read/update the TLS pointer. Loading the TLS pointer requires the following asm instructions: MOVQ runtime.tls_g, AX MOVQ AX(GS), AX Notice that this approach is also implemented on windows/arm64. [TEB]: https://en.wikipedia.org/wiki/Win32_Thread_Information_Block [TlsAlloc]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc Updates #22192 Change-Id: Idea7119fd76a3cd083979a4d57ed64b552fa101b Reviewed-on: https://go-review.googlesource.com/c/go/+/431775 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2022-10-14runtime: add msan support on freebsd/amd64Dmitri Goutnik
Adjust build constraints and change the runtime to call the C mmap function when using cgo. R=go1.20 For #53298 Change-Id: If9c3306dc16a8645d1bb9be0343e0472d6c4783f Reviewed-on: https://go-review.googlesource.com/c/go/+/411274 Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2022-10-05runtime/cgo: let darwin pthread stacksize follow rlimitYoulin Feng
On Mac OS X, the default stack size for non-main threads created by cgo is fixed at 512KB and cannot be altered by setrlimit. This stack size is too small for some recursive scenarios. We can solve this problem by explicitly copying the stack size of the main thread when creating a new thread. Change-Id: I400d5b2e929a1ee261502914a991e208759f64a8 GitHub-Last-Rev: b29c74599ea1cce4683d25e1ac8a70ffd9b86381 GitHub-Pull-Request: golang/go#53667 Reviewed-on: https://go-review.googlesource.com/c/go/+/415915 Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2022-09-28cmd/nm, runtime/cgo: add cgo support for freebsd/riscv64Mikael Urankar
Updates #53466 Change-Id: I08ea279c905e265a579b6b3e23aee012165beaee Reviewed-on: https://go-review.googlesource.com/c/go/+/431658 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Goutnik <dgoutnik@gmail.com>
2022-08-28cmd/cgo: add and use runtime/cgo.Incomplete instead of //go:notinheapCuong Manh Le
Updates #46731 Change-Id: Ia83f27c177cc2f57e240cb5c6708d4552423f5be Reviewed-on: https://go-review.googlesource.com/c/go/+/421879 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-08runtime/cgo: add -fno-stack-protector to CFLAGSIan Lance Taylor
Some compilers default to having -fstack-protector on, which breaks when using internal linking because the linker doesn't know how to find the support functions. Fixes #52919 Fixes #54313 Change-Id: I6f51d5e906503f61fc768ad8e30c163bad135087 Reviewed-on: https://go-review.googlesource.com/c/go/+/421935 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2022-07-26runtime/cgo: use frame address to set g0 stack boundCherry Mui
For a cgo binary, at startup we set g0's stack bounds using the address of a local variable (&size) in a C function x_cgo_init and the stack size from pthread_attr_getstacksize. Normally, &size is an address within the current stack frame. However, when it is compiled with ASAN, it may be instrumented to __asan_stack_malloc_0 and the address may not live in the current stack frame, causing the stack bound to be set incorrectly, e.g. lo > hi. Using __builtin_frame_address(0) to get the stack address instead. Change-Id: I41df929e5ed24d8bbf3e15027af6dcdfc3736e37 Reviewed-on: https://go-review.googlesource.com/c/go/+/419434 Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-06-09runtime/cgo: retry _beginthread on EACCESMichael Pratt
We occassionally see _beginthread failing with EACCES, meaning "insufficient resources" according to the Microsoft documentation. Exactly which resources is unclear. Similar to pthread_create on unix systems, we can wait a bit and retry to try to get success. The alternative is to abort, so we may as well give it a try. Fixes #52572. Change-Id: I6e05add53b4ae36c61e53b1ee3fed6bc74e17dfa Reviewed-on: https://go-review.googlesource.com/c/go/+/410355 Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Michael Pratt <mpratt@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-06-09runtime/cgo: merge bodies of cgo_sys_thread_start on windowsMichael Pratt
The bodies of cgo_sys_thread_start (and x_cgo_sys_thread_create) are nearly identical on all of the windows ports. Create a single _cgo_beginthread implementation that contains the body and is used on all ports. This will reduce churn in an upcoming CL to add retry logic. We could theoretically have a single implementation of _cgo_sys_thread_start shared by all ports, but I keep them separate for ease of searching. Right now every single port implements this function in their gcc_GOOS_GOARCH.c file, so it is nice to keep this symmetry. _cgo_dummy_export must move out of libcgo_windows.h because it is a definition and the inclusion of libcgo_windows.h in multiple files creates duplicate definitions. For #52572. Change-Id: I9fa22009389349c754210274c7db2631b061f9c7 Reviewed-on: https://go-review.googlesource.com/c/go/+/410354 Run-TryBot: Michael Pratt <mpratt@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-05-19runtime/cgo: add cgo function call support for loong64Xiaodong Liu
Contributors to the loong64 port are: Weining Lu <luweining@loongson.cn> Lei Wang <wanglei@loongson.cn> Lingqin Gong <gonglingqin@loongson.cn> Xiaolin Zhao <zhaoxiaolin@loongson.cn> Meidan Li <limeidan@loongson.cn> Xiaojuan Zhai <zhaixiaojuan@loongson.cn> Qiyuan Pu <puqiyuan@loongson.cn> Guoqi Chen <chenguoqi@loongson.cn> This port has been updated to Go 1.15.6: https://github.com/loongson/go Updates #46229 Change-Id: I8ef0e7f17d6ada3d2f07c81524136b78457e7795 Reviewed-on: https://go-review.googlesource.com/c/go/+/342319 Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
2022-05-06runtime/cgo: remove memset in _cgo_sys_thread_start on linuxTobias Klauser
pthread_attr_init in glibc and musl libc already explicitly clear the pthread_attr argument before setting it, see https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_attr_init.c and https://git.musl-libc.org/cgit/musl/log/src/thread/pthread_attr_init.c It looks like pthread_attr_init has been implemented like this for a long time in both libcs. The comment and memset in _cgo_sys_thread_start probably stem from a time where not all libcs did the explicit memset in pthread_attr_init. Also, the memset in _cgo_sys_thread_start is not performed on all linux platforms further indicating that this isn't an issue anymore. Change-Id: I920148b5d24751195ced7af5bb7c52a7f8293259 Reviewed-on: https://go-review.googlesource.com/c/go/+/404275 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-05-03internal/poll, net, syscall: use accept4 on solarisTobias Klauser
Solaris supports accept4 since version 11.4, see https://docs.oracle.com/cd/E88353_01/html/E37843/accept4-3c.html Use it in internal/poll.accept like on other platforms. Change-Id: I3d9830a85e93bbbed60486247c2f91abc646371f Reviewed-on: https://go-review.googlesource.com/c/go/+/403394 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Benny Siegert <bsiegert@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com>
2022-04-22runtime/cgo: remove stdlib.h warning workaround on darwinBryan C. Mills
CL 205457 added the flag -Wno-nullability-completeness to work around a user-reported build breakage on macOS Catalina. However, according to https://golang.org/issue/35247#issuecomment-589115489 the root cause of the breakage may be a toolchain misconfiguration on the host (perhaps compiling the XCode stdlib using a Homebrew build of the "clang" compiler?). Adding an obscure warning flag to enable building stdlib.h with an otherwise-broken toolchain seems clearly inappropriate to me. If need be we can instead provide guidance to users on how to unbreak their toolchain. Updates #35247 Fixes #49913 Change-Id: I84def34e101bed7911d8d78a991a29095b8791fa Reviewed-on: https://go-review.googlesource.com/c/go/+/368634 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-04-05all: separate doc comment from //go: directivesRuss Cox
A future change to gofmt will rewrite // Doc comment. //go:foo to // Doc comment. // //go:foo Apply that change preemptively to all comments (not necessarily just doc comments). For #51082. Change-Id: Iffe0285418d1e79d34526af3520b415a12203ca9 Reviewed-on: https://go-review.googlesource.com/c/go/+/384260 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-03-30runtime: unify C->Go ABI transitions on arm64eric fang
There are several of places that save and restore the C callee-saved registers, the operation is the same everywhere, so this CL defines several macros to do this, which will help reduce code redundancy and unify the operation. This CL also replaced consecutive MOVD instructions with STP and LDP instructions in several places where these macros do not apply. Change-Id: I815f39fe484a9ab9b6bd157dfcbc8ad99c1420fe Reviewed-on: https://go-review.googlesource.com/c/go/+/374397 Trust: Eric Fang <eric.fang@arm.com> Run-TryBot: Eric Fang <eric.fang@arm.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
2022-03-29all: use new "unix" build tag where appropriateIan Lance Taylor
For #20322 For #51572 Change-Id: Id0b4799d097d01128e98ba4cc0092298357bca45 Reviewed-on: https://go-review.googlesource.com/c/go/+/389935 Trust: Ian Lance Taylor <iant@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
2022-03-18runtime/cgo: remove memset in _cgo_sys_thread_start on freebsd/armTobias Klauser
pthread_attr_init on freebsd properly initializes the pthread_attr, there is no need to zero it before the call. The comment and code were probably copied from the linux/arm implementation. This aligns the implementation on freebsd/arm with the implementation on other freebsd architectures. Fixes #44248 Change-Id: If82ebb115b877b6c6f4862018a9419ba8d870f12 Reviewed-on: https://go-review.googlesource.com/c/go/+/393617 Trust: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com> Trust: Hajime Hoshi <hajimehoshi@gmail.com>
2021-12-15runtime/cgo: fix signature of crosscall_amd64 in commentMoZhonghua
In CL 289192, crosscall_amd64() was changed to recieve 3 arguments, but the comment was not updated. Change-Id: Iba36c27aa5189e50f3fcc2a50291fecb2ef722c1 GitHub-Last-Rev: e7c041f00c562fdfeec84f1f3ea341713dcc7bf5 GitHub-Pull-Request: golang/go#49539 Reviewed-on: https://go-review.googlesource.com/c/go/+/363442 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-11-20runtime/cgo: add example of Handle with void* parameterAlan Donovan
Fixes #49633 Change-Id: I12ca350f7dd6bfc8753a4a169f29b89ef219b035 Reviewed-on: https://go-review.googlesource.com/c/go/+/364774 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Bryan C. Mills <bcmills@google.com>
2021-11-03cmd/asm,cmd/compile,runtime: stop using X3 (aka GP) on riscv64Joel Sing
The X3 (aka GP) register will potentially be loaded with the __global_pointer$ symbol during program start up (usually by the dynamic linker). As such, non-Go code may depend on the contents of GP and calculate offsets based on it, including code called via cgo and signal handlers installed by non-Go code. As such, stop using the X3 register so that there are fewer issues interacting between Go and non-Go code. While here remove the X4 (TP) name from the assembler such that any references must use the 'TP' name. This should reduce the likelihood of accidental use (like we do for the 'g' register). The same applies for X3 (GP) when the -shared flag is given. Updates #47100 Change-Id: I72e82b5ca3f80c46a781781345ca0432a4111b74 Reviewed-on: https://go-review.googlesource.com/c/go/+/351859 Trust: Joel Sing <joel@sing.id.au> Run-TryBot: Joel Sing <joel@sing.id.au> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-10-28all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor)Russ Cox
When these packages are released as part of Go 1.18, Go 1.16 will no longer be supported, so we can remove the +build tags in these files. Ran go fix -fix=buildtag std cmd and then reverted the bootstrapDirs as defined in src/cmd/dist/buildtool.go, which need to continue to build with Go 1.4 for now. Also reverted src/vendor and src/cmd/vendor, which will need to be updated in their own repos first. Manual changes in runtime/pprof/mprof_test.go to adjust line numbers. For #41184. Change-Id: Ic0f93f7091295b6abc76ed5cd6e6746e1280861e Reviewed-on: https://go-review.googlesource.com/c/go/+/344955 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-09-17runtime/cgo: save and restore X3 (aka GP) for crosscall1 on riscv64Joel Sing
The C code that is calling crosscall1 may depend on the GP register, which Go code will currently clobber. Save and restore both X3 (aka GP) and X4 (aka TP) in this code path (note that the Go code does not currently clobber X4, however there is minimal downside to saving and restoring it here, which then also matches crosscall2). Updates #47100 Change-Id: Icbb706d7889d5dc59de3efb2b510fa6ea2069496 Reviewed-on: https://go-review.googlesource.com/c/go/+/334870 Trust: Joel Sing <joel@sing.id.au> Trust: Meng Zhuo <mzh@golangcn.org> Reviewed-by: Meng Zhuo <mzh@golangcn.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Meng Zhuo <mzh@golangcn.org>
2021-09-09cmd/cgo, runtime/cgo: avoid GCC/clang conversion warningsIan Lance Taylor
Add explicit conversions to avoid warnings from -Wsign-conversion and -Wshorten-64-to-32. Also avoid runtime errors from -fsanitize=undefined. Fixes #48121 Change-Id: I29dc8d976884fc42826392c10f1e1759bb1a3989 Reviewed-on: https://go-review.googlesource.com/c/go/+/348739 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
2021-08-09runtime/cgo: when using msan explicitly unpoison cgoCallersIan Lance Taylor
This avoids an incorrect msan uninitialized memory report when using runtime.SetCgoTraceback when a signal occurs while the fifth argument register is undefined. See the issue for more details. Fixes #47543 Change-Id: I3d1b673e2c93471ccdae0171a99b88b5a6062840 Reviewed-on: https://go-review.googlesource.com/c/go/+/339902 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
2021-05-17runtime/cgo: fix crosscall2 on ppc64xLynn Boger
Some uses of crosscall2 did not work on ppc64le and probably aix-ppc64. In particular, if there was a main program compiled with -buildmode=pie and used a plugin which invoked crosscall2, then failures could occur due to R2 getting set incorrectly along the way. The problem was due to R2 being saved on the caller's stack; it is now saved on the crosscall2 stack. More details can be found in the issue. This adds a testcase where the main program is built with pie and the plugin invokes crosscall2. This also changes the save of the CR bits from MOVD to MOVW as it should be. Fixes #43228 Change-Id: Ib5673e25a2ec5ee46bf9a1ffb0cb1f3ef5449086 Reviewed-on: https://go-review.googlesource.com/c/go/+/319489 Run-TryBot: Lynn Boger <laboger@linux.vnet.ibm.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Trust: Heschi Kreinick <heschi@google.com>
2021-05-13all: add //go:build lines to assembly filesTobias Klauser
Don't add them to files in vendor and cmd/vendor though. These will be pulled in by updating the respective dependencies. For #41184 Change-Id: Icc57458c9b3033c347124323f33084c85b224c70 Reviewed-on: https://go-review.googlesource.com/c/go/+/319389 Trust: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2021-05-10runtime/cgo,cmd/internal/obj/ppc64: fix signals with cgoLynn Boger
Recently some tsan tests were enabled on ppc64le which had not been enabled before. This resulted in failures on systems with tsan available, and while debugging it was determined that there were other issues related to the use of signals with cgo. Signals were not being forwarded within programs linked against libtsan because the nocgo sigaction was being called for ppc64le with or without cgo. Adding callCgoSigaction and calling that allows signals to be registered so that signal forwarding works. For linux-ppc64 and aix-ppc64, this won't change. On linux-ppc64 there is no cgo. I can't test aix-ppc64 so those owners can enable it if they want. In reviewing comments about sigtramp in sys_linux_arm64 it was noted that a previous issue in arm64 due to missing callee save registers could also be a problem on ppc64x, so code was added to save and restore those. Also, the use of R31 as a temp register in some cases caused an issue since it is a nonvolatile register in C and was being clobbered in cases where the C code expected it to be valid. The code sequences to load these addresses were changed to avoid the use of R31 when loading such an address. To get around a vet error, the stubs_ppc64x.go file in runtime was split into stubs_ppc64.go and stubs_ppc64le.go. Updates #45040 Change-Id: Ia4ecff950613cbe1b89471790b1d3819d5b5cfb9 Reviewed-on: https://go-review.googlesource.com/c/go/+/306369 Trust: Lynn Boger <laboger@linux.vnet.ibm.com> Run-TryBot: Lynn Boger <laboger@linux.vnet.ibm.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Carlos Eduardo Seo <carlos.seo@linaro.org>
2021-04-28cmd/dist,runtime: support cgo on openbsd/mips64Joel Sing
Add support for cgo on openbsd/mips64. Fixes #43005 Change-Id: I2386204f53fa984a01a9d89f0b6c96455768f326 Reviewed-on: https://go-review.googlesource.com/c/go/+/275896 Trust: Joel Sing <joel@sing.id.au> Run-TryBot: Joel Sing <joel@sing.id.au> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2021-04-23runtime: add windows/arm64 cgo-linking codeRuss Cox
This code is needed for use with cgo proper (as opposed to hand-written DLL calls, which we always use but only exercise cgo execution, not cgo linking). Change-Id: Iddc31d9c1c924d83d032b80dca65ddfda6624046 Reviewed-on: https://go-review.googlesource.com/c/go/+/312041 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2021-04-15runtime: unify C->Go ABI transitionsAustin Clements
The previous CL introduced macros for transitions from the Windows ABI to the Go ABI. This CL does the same for SysV and uses them in almost all places where we transition from the C ABI to the Go ABI. Compared to Windows, this transition is much simpler and I didn't find any places that were getting it wrong. But this does let us unify a lot of code nicely and introduces some degree of abstraction around these ABI transitions. Change-Id: Ib6bdecafce587ce18fca4c8300fcf401284a2bcd Reviewed-on: https://go-review.googlesource.com/c/go/+/309930 Trust: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2021-04-15runtime,runtime/cgo: save all necessary registers on entry to Go on WindowsAustin Clements
There are several assembly functions that transition from the Windows ABI to the Go ABI. These all need to save all registers that are callee-save in the Windows ABI and caller-save in the Go ABI and prepare the register state for Go. However, they all do this slightly differently and most of them don't save the necessary XMM registers for this transition (which could corrupt them in the C caller). Furthermore, now that we have a carefully specified Go ABI, it's clear that none of these actually get all of the details 100% right. So, unify this code into two macros in a shared header in runtime/cgo/abi_amd64.h that handle all necessary registers and setup and use these macros everywhere on Windows that handles transitions from C to Go. Change-Id: I62f41345a507aad1ca383814ac8b7e2a9ffb821e Reviewed-on: https://go-review.googlesource.com/c/go/+/309769 Trust: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2021-04-13all: simplify the spelling of LinuxBrad Fitzpatrick
The prefix didn't help clarify anything. Change-Id: I897fd4022ce9df42a548b15714e4b592618ca547 Reviewed-on: https://go-review.googlesource.com/c/go/+/309573 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Filippo Valsorda <filippo@golang.org>
2021-04-08runtime/cgo: clarify Handle documentationIan Lance Taylor
Fixes #45427 Change-Id: Ic67630a5f39d8789a4a30c6b4ee30946bc50382e Reviewed-on: https://go-review.googlesource.com/c/go/+/308230 Trust: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ainar Garipov <gugl.zadolbal@gmail.com> Reviewed-by: Changkun Ou <euryugasaki@gmail.com> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
2021-04-06runtime/cgo: add Handle for managing (c)go pointersChangkun Ou
A non-trivial Cgo program may need to use callbacks and interact with go objects per goroutine. Because of the rules for passing pointers between Go and C, such a program needs to store handles to associated Go values. This often causes much extra effort to figure out a way to correctly deal with: 1) map collision; 2) identifying leaks and 3) concurrency. This CL implements a Handle representation in runtime/cgo package, and related methods such as Value, Delete, etc. which allows Go users can use a standard way to handle the above difficulties. In addition, the CL allows a Go value to have multiple handles, and the NewHandle always returns a different handle compare to the previously returned handles. In comparison, CL 294670 implements a different behavior of NewHandle that returns a unique handle when the Go value is referring to the same object. Benchmark: name time/op Handle/non-concurrent-16 487ns ± 1% Handle/concurrent-16 674ns ± 1% Fixes #37033 Change-Id: I0eadb9d44332fffef8fb567c745246a49dd6d4c1 Reviewed-on: https://go-review.googlesource.com/c/go/+/295369 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Zhang <cherryyz@google.com>
2021-02-23runtime/cgo: use correct lean and mean macroJason A. Donenfeld
WIN64_LEAN_AND_MEAN is not the correct macro to use and doesn't ever exist. Change-Id: I32a5523cc0f7cc3f3a4d022071cf81f88db39aa9 Reviewed-on: https://go-review.googlesource.com/c/go/+/291634 Trust: Jason A. Donenfeld <Jason@zx2c4.com> Trust: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-02-20all: go fmt std cmd (but revert vendor)Russ Cox
Make all our package sources use Go 1.17 gofmt format (adding //go:build lines). Part of //go:build change (#41184). See https://golang.org/design/draft-gobuild Change-Id: Ia0534360e4957e58cd9a18429c39d0e32a6addb4 Reviewed-on: https://go-review.googlesource.com/c/go/+/294430 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-02-18runtime/cgo: add cast in C code to avoid C compiler warningIan Lance Taylor
Fixes #44340 Change-Id: Id80dd1f44a988b653933732afcc8e49a826affc4 Reviewed-on: https://go-review.googlesource.com/c/go/+/293209 Reviewed-by: Andrew G. Morgan <agm@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com> Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org>
2021-02-05[dev.regabi] runtime/cgo: call setg_gcc in crosscall_amd64Cherry Zhang
Currently, when using cgo, the g pointer is set via a separate call to setg_gcc or with inline assembly in threadentry. This CL changes it to call setg_gcc in crosscall_amd64, like other g- register platforms. When we have an actual g register on AMD64, we'll need to set the register immediately before calling into Go. Change-Id: Ib1171e05cd0dabba3b7d12e072084d141051cf3d Reviewed-on: https://go-review.googlesource.com/c/go/+/289192 Trust: Cherry Zhang <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2020-12-26runtime/cgo: fix Android build with NDK 22Elias Naur
Fixes #42655 Change-Id: I7d2b70098a4ba4dcb325fb0be076043789b86135 Reviewed-on: https://go-review.googlesource.com/c/go/+/280312 Run-TryBot: Elias Naur <mail@eliasnaur.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Elias Naur <mail@eliasnaur.com>
2020-12-07runtime/cgo: fix building on muslTonis Tiigi
sys/unistd.h only exists in glibc and not in musl so use the standard location. This is a regression from CL 210639 Change-Id: Idd4c75510d9829316b44300c36c34df6d667cc05 GitHub-Last-Rev: 0fa4162f1c7c460bda7585300285f47d1781985d GitHub-Pull-Request: golang/go#43038 Reviewed-on: https://go-review.googlesource.com/c/go/+/275732 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Andrew G. Morgan <agm@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Filippo Valsorda <filippo@golang.org>
2020-11-03cmd/dist,cmd/go,runtime: add support for cgo on linux/riscv64Joel Sing
Fixes #36641 Change-Id: I51868d83ce341d78d33b221d184c5a5110c60d14 Reviewed-on: https://go-review.googlesource.com/c/go/+/263598 Trust: Joel Sing <joel@sing.id.au> Run-TryBot: Joel Sing <joel@sing.id.au> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-11-02cmd/link: avoid exporting all symbols on windows buildmode=pieQuim Muntal
Marking one functions with __declspec(dllexport) forces mingw to create .reloc section without having to export all symbols. See https://insights.sei.cmu.edu/cert/2018/08/when-aslr-is-not-really-aslr---the-case-of-incorrect-assumptions-and-bad-defaults.html for more info. This change cuts 73kb of a "hello world" pie binary. Updates #6853 Fixes #40795 Change-Id: I3cc57c3b64f61187550bc8751dfa085f106c8475 Reviewed-on: https://go-review.googlesource.com/c/go/+/264459 Trust: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Reviewed-by: Austin Clements <austin@google.com>
2020-10-28runtime: set up TLS without cgo on darwin/arm64Cherry Zhang
Currently, on darwin/arm64 we set up TLS using cgo. TLS is not set for pure Go programs. As we use libc for syscalls on darwin, we need to save the G register before the libc call. Otherwise it is not signal-safe, as a signal may land during the execution of a libc function, where the G register may be clobbered. This CL initializes TLS in Go, by calling the pthread functions directly without cgo. This makes it possible to save the G register to TLS in pure Go programs (done in a later CL). Inspired by Elias's CL 209197. Write the logic in Go instead of assembly. Updates #38485, #35853. Change-Id: I257ba2a411ad387b2f4d50d10129d37fec7a226e Reviewed-on: https://go-review.googlesource.com/c/go/+/265118 Trust: Cherry Zhang <cherryyz@google.com> Trust: Elias Naur <mail@eliasnaur.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-10-26runtime,cmd/cgo: simplify C -> Go call pathAustin Clements
This redesigns the way calls work from C to exported Go functions. It removes several steps from the call path, makes cmd/cgo no longer sensitive to the Go calling convention, and eliminates the use of reflectcall from cgo. In order to avoid generating a large amount of FFI glue between the C and Go ABIs, the cgo tool has long depended on generating a C function that marshals the arguments into a struct, and then the actual ABI switch happens in functions with fixed signatures that simply take a pointer to this struct. In a way, this CL simply pushes this idea further. Currently, the cgo tool generates this argument struct in the exact layout of the Go stack frame and depends on reflectcall to unpack it into the appropriate Go call (even though it's actually reflectcall'ing a function generated by cgo). In this CL, we decouple this struct from the Go stack layout. Instead, cgo generates a Go function that takes the struct, unpacks it, and calls the exported function. Since this generated function has a generic signature (like the rest of the call path), we don't need reflectcall and can instead depend on the Go compiler itself to implement the call to the exported Go function. One complication is that syscall.NewCallback on Windows, which converts a Go function into a C function pointer, depends on cgocallback's current dynamic calling approach since the signatures of the callbacks aren't known statically. For this specific case, we continue to depend on reflectcall. Really, the current approach makes some overly simplistic assumptions about translating the C ABI to the Go ABI. Now we're at least in a much better position to do a proper ABI translation. For comparison, the current cgo call path looks like: GoF (generated C function) -> crosscall2 (in cgo/asm_*.s) -> _cgoexp_GoF (generated Go function) -> cgocallback (in asm_*.s) -> cgocallback_gofunc (in asm_*.s) -> cgocallbackg (in cgocall.go) -> cgocallbackg1 (in cgocall.go) -> reflectcall (in asm_*.s) -> _cgoexpwrap_GoF (generated Go function) -> p.GoF Now the call path looks like: GoF (generated C function) -> crosscall2 (in cgo/asm_*.s) -> cgocallback (in asm_*.s) -> cgocallbackg (in cgocall.go) -> cgocallbackg1 (in cgocall.go) -> _cgoexp_GoF (generated Go function) -> p.GoF Notably: 1. We combine _cgoexp_GoF and _cgoexpwrap_GoF and move the combined operation to the end of the sequence. This combined function also handles reflectcall's previous role. 2. We combined cgocallback and cgocallback_gofunc since the only purpose of having both was to convert a raw PC into a Go function value. We instead construct the Go function value in cgocallbackg1. 3. cgocallbackg1 no longer reaches backwards through the stack to get the arguments to cgocallback_gofunc. Instead, we just pass the arguments down. 4. Currently, we need an explicit msanwrite to mark the results struct as written because reflectcall doesn't do this. Now, the results are written by regular Go assignments, so the Go compiler generates the necessary MSAN annotations. This also means we no longer need to track the size of the arguments frame. Updates #40724, since now we don't need to teach cgo about the register ABI or change how it uses reflectcall. Change-Id: I7840489a2597962aeb670e0c1798a16a7359c94f Reviewed-on: https://go-review.googlesource.com/c/go/+/258938 Trust: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>