1
0
mirror of https://github.com/golang/go synced 2024-11-13 19:00:25 -07:00
go/doc
Russ Cox 1ac637c766 cmd/compile: recognize Syscall-like functions for liveness analysis
Consider this code:

	func f(*int)

	func g() {
		p := new(int)
		f(p)
	}

where f is an assembly function.
In general liveness analysis assumes that during the call to f, p is dead
in this frame. If f has retained p, p will be found alive in f's frame and keep
the new(int) from being garbage collected. This is all correct and works.
We use the Go func declaration for f to give the assembly function
liveness information (the arguments are assumed live for the entire call).

Now consider this code:

	func h1() {
		p := new(int)
		syscall.Syscall(1, 2, 3, uintptr(unsafe.Pointer(p)))
	}

Here syscall.Syscall is taking the place of f, but because its arguments
are uintptr, the liveness analysis and the garbage collector ignore them.
Since p is no longer live in h once the call starts, if the garbage collector
scans the stack while the system call is blocked, it will find no reference
to the new(int) and reclaim it. If the kernel is going to write to *p once
the call finishes, reclaiming the memory is a mistake.

We can't change the arguments or the liveness information for
syscall.Syscall itself, both for compatibility and because sometimes the
arguments really are integers, and the garbage collector will get quite upset
if it finds an integer where it expects a pointer. The problem is that
these arguments are fundamentally untyped.

The solution we have taken in the syscall package's wrappers in past
releases is to insert a call to a dummy function named "use", to make
it look like the argument is live during the call to syscall.Syscall:

	func h2() {
		p := new(int)
		syscall.Syscall(1, 2, 3, uintptr(unsafe.Pointer(p)))
		use(unsafe.Pointer(p))
	}

Keeping p alive during the call means that if the garbage collector
scans the stack during the system call now, it will find the reference to p.

Unfortunately, this approach is not available to users outside syscall,
because 'use' is unexported, and people also have to realize they need
to use it and do so. There is much existing code using syscall.Syscall
without a 'use'-like function. That code will fail very occasionally in
mysterious ways (see #13372).

This CL fixes all that existing code by making the compiler do the right
thing automatically, without any code modifications. That is, it takes h1
above, which is incorrect code today, and makes it correct code.

Specifically, if the compiler sees a foreign func definition (one
without a body) that has uintptr arguments, it marks those arguments
as "unsafe uintptrs". If it later sees the function being called
with uintptr(unsafe.Pointer(x)) as an argument, it arranges to mark x
as having escaped, and it makes sure to hold x in a live temporary
variable until the call returns, so that the garbage collector cannot
reclaim whatever heap memory x points to.

For now I am leaving the explicit calls to use in package syscall,
but they can be removed early in a future cycle (likely Go 1.7).

The rule has no effect on escape analysis, only on liveness analysis.

Fixes #13372.

Change-Id: I2addb83f70d08db08c64d394f9d06ff0a063c500
Reviewed-on: https://go-review.googlesource.com/18584
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-01-14 01:16:45 +00:00
..
articles doc/articles/wiki: remove generated final-test.go after test 2015-06-17 03:11:01 +00:00
codewalk doc: fix typo in sharemem codewalk 2014-05-21 14:34:20 -07:00
devel doc: document Go 1.5.3 2016-01-13 17:48:41 +00:00
gopher all: link to https for golang subdomains too 2015-07-12 04:42:40 +00:00
play doc/play: update URL for concurrent pi 2016-01-06 20:23:21 +00:00
progs all: remove executable bit from several files 2015-09-04 02:59:49 +00:00
asm.html cmd/compile: recognize Syscall-like functions for liveness analysis 2016-01-14 01:16:45 +00:00
cmd.html all: use golang.org/x/... import paths 2014-11-10 09:15:57 +11:00
code.html doc: add Overview and other small edits to How To Write Go Code 2016-01-12 01:56:04 +00:00
conduct.html doc: Americanise spelling of wilful 2015-12-17 21:32:59 +00:00
contrib.html doc: add Code of Conduct 2015-11-24 04:07:58 +00:00
contribute.html doc: discuss copyright changes in contribute.html 2016-01-06 21:50:20 +00:00
debugging_with_gdb.html doc: fix source link in gdb docs 2016-01-06 21:58:19 +00:00
docs.html doc: drop scheme from links that are known to support HTTPS 2014-07-25 10:28:39 +10:00
effective_go.html doc: remove mention of default GOMAXPROCS(1) in Effective Go 2015-07-21 02:45:44 +00:00
gccgo_contribute.html doc: update gccgo docs for move of gofrontend to git 2015-06-19 01:03:21 +00:00
gccgo_install.html all: link to https instead of http 2015-07-11 14:36:33 +00:00
go1.1.html all: fix misprints in comments 2015-06-11 14:18:57 +00:00
go1.2.html doc: drop scheme from links that are known to support HTTPS 2014-07-25 10:28:39 +10:00
go1.3.html [release-branch.go1.4] doc: scrub references to code.google.com 2014-12-12 14:00:48 +11:00
go1.4.html doc/go1.4.html: fix typo 2015-03-24 18:17:49 +00:00
go1.5.html doc: mention that go install removes binaries built by go build 2015-09-08 18:46:07 +00:00
go1.6.html doc: note GCM behaviour change in Go 1.6. 2016-01-10 19:03:54 +00:00
go1.html doc: link directly to https://golang.org/dl/ 2014-09-12 09:15:58 +10:00
go1compat.html doc: add a clause about embedded methods to go1compat 2015-07-22 01:25:32 +00:00
go_faq.html doc: fix typo "heirarchy" 2015-12-14 15:16:25 +00:00
go_mem.html doc/go_mem.html: correct the channel example 2015-03-16 21:43:31 +00:00
go_spec.html spec: New year, new spec update (to refer to Unicode 8.0). 2016-01-05 22:39:26 +00:00
go-logo-black.png
go-logo-blue.png
go-logo-white.png
help.html doc: add Code of Conduct 2015-11-24 04:07:58 +00:00
ie.css
install-source.html doc: document linux/ppc64 kernel requirement (2.6.37 or later) 2016-01-06 22:06:36 +00:00
install.html doc: remove note about installing from source for ARM 2016-01-07 04:12:01 +00:00
root.html doc: only show Share button when enabled 2015-09-02 05:58:52 +00:00
security.html doc: add Go Security Policy document 2015-09-03 03:02:17 +00:00
share.png
tos.html doc: drop scheme from links that are known to support HTTPS 2014-07-25 10:28:39 +10:00