1
0
mirror of https://github.com/golang/go synced 2024-11-13 19:00:25 -07:00
go/test
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
..
bench encoding/base64: fix streaming decode of padding-free base64 2016-01-08 15:07:45 +00:00
bugs test: migrate remaining tests to run.go 2014-12-22 22:41:34 +00:00
chan Fix several out of date references to 4g/5g/6g/8g/9g. 2015-06-26 03:38:21 +00:00
ddd2.dir
dwarf
fixedbugs cmd/compile: better modeling of escape across loop levels 2016-01-13 04:01:00 +00:00
import2.dir
import4.dir test: adjust gccgo expected import errors 2015-10-07 14:37:44 +00:00
interface cmd/internal/gc: improve "type *X has no field or method M" message 2015-05-07 16:21:57 +00:00
ken
method4.dir
safe
stress build: adjustments for move from src/pkg to src 2014-09-08 00:06:45 -04:00
syntax cmd/compile: better syntax error recovery 2015-11-20 19:56:27 +00:00
64bit.go
235.go
alias1.go
alias.go
append.go
args.go
assign1.go
assign.go cmd/gc: prohibit short variable declarations containing duplicate symbols 2014-10-06 17:16:39 -04:00
bigalg.go
bigmap.go
blank1.go cmd/compile: don't allow blank method declarations on builtins 2015-12-02 18:26:38 +00:00
blank.go
bom.go
bombad.go
bounds.go
chancap.go
chanlinear.go runtime: test for linear enqueue/dequeue behavior 2014-12-08 22:18:17 +00:00
char_lit1.go
char_lit.go
clearfat.go [dev.power64] 9g: fix under-zeroing in clearfat 2014-10-31 11:08:27 -04:00
closedchan.go
closure1.go cmd/gc: capture variables by value 2015-01-29 13:07:30 +00:00
closure2.go cmd/gc: fix capturing by value for range statements 2015-02-03 15:48:48 +00:00
closure.go
cmp6.go
cmp.go Fix several out of date references to 4g/5g/6g/8g/9g. 2015-06-26 03:38:21 +00:00
cmplx.go
cmplxdivide1.go
cmplxdivide.c test: comment the behavior and use of cmplxdivide* 2015-01-15 00:00:06 +00:00
cmplxdivide.go test: comment the behavior and use of cmplxdivide* 2015-01-15 00:00:06 +00:00
complit1.go cmd/internal/gc: accept map literals with omitted key type 2015-06-04 02:31:38 +00:00
complit.go
compos.go
const1.go
const2.go
const3.go
const4.go all: link to https instead of http 2015-07-11 14:36:33 +00:00
const5.go all: link to https instead of http 2015-07-11 14:36:33 +00:00
const6.go
const.go cmd/compile: allow huge rsh in constants arithmetic 2015-08-21 20:27:22 +00:00
convert1.go
convert3.go
convert.go
convlit1.go
convlit.go cmd/internal/gc: unsafe.Pointer constants may only be converted to uintptr 2015-05-07 23:54:28 +00:00
convT2X.go
copy.go
crlf.go
ddd1.go
ddd2.go
ddd.go
decl.go
declbad.go
defer.go
deferfin.go
deferprint.go
deferprint.out
divide.go
divmod.go
empty.go
env.go
eof1.go
eof.go
errchk Fix several out of date references to 4g/5g/6g/8g/9g. 2015-06-26 03:38:21 +00:00
escape2.go cmd/compile: better modeling of escape across loop levels 2016-01-13 04:01:00 +00:00
escape2n.go cmd/compile: better modeling of escape across loop levels 2016-01-13 04:01:00 +00:00
escape3.go
escape4.go cmd/internal/gc: transitive inlining 2015-02-26 17:36:00 +00:00
escape5.go cmd/internal/gc: move check for large-hence-heap-allocated types into escape analysis 2015-05-22 02:13:54 +00:00
escape_array.go cmd/internal/gc: move check for large-hence-heap-allocated types into escape analysis 2015-05-22 02:13:54 +00:00
escape_calls.go cmd/internal/gc: make indirect calls properly escape-y 2015-05-22 01:36:11 +00:00
escape_closure.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape_field.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape_iface.go cmd/compile: add case for ODOTTYPE to escwalk 2015-07-30 17:39:44 +00:00
escape_indir.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape_level.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape_map.go test: add tests for escape analysis of interface conversions 2015-03-28 16:15:27 +00:00
escape_param.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape_slice.go cmd/internal/gc: extend escape analysis to pointers in slices 2015-05-18 15:34:39 +00:00
escape_struct_param1.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape_struct_param2.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape_struct_return.go cmd/internal/gc: improve flow of input params to output params 2015-05-01 13:47:20 +00:00
escape.go
fibo.go Fix whitespace (use blanks consistently). 2014-12-08 22:22:58 +00:00
finprofiled.go runtime: fix finalization and profiling of tiny allocations 2015-11-03 18:57:18 +00:00
float_lit2.go
float_lit3.go
float_lit.go
floatcmp.go
for.go
func1.go
func2.go
func3.go
func4.go
func5.go
func6.go Fix several out of date references to 4g/5g/6g/8g/9g. 2015-06-26 03:38:21 +00:00
func7.go Fix several out of date references to 4g/5g/6g/8g/9g. 2015-06-26 03:38:21 +00:00
func8.go test: use go:noinline consistently 2015-11-03 02:01:34 +00:00
func.go
funcdup2.go
funcdup.go
gc1.go
gc2.go test: check for build constraints only upto the first blank line 2015-02-06 05:36:26 +00:00
gc.go
gcstring.go
goprint.go
goprint.out
goto.go Revert "cmd/internal/gc: ignore declarations of types for goto validation" 2015-05-15 16:35:49 +00:00
heapsampling.go test: really force heap profile update in heapsampling.go 2015-11-12 18:32:13 +00:00
helloworld.go
helloworld.out
if.go
import1.go
import2.go
import4.go
import5.go
import.go
index0.go
index1.go
index2.go
index.go Fix several out of date references to 4g/5g/6g/8g/9g. 2015-06-26 03:38:21 +00:00
indirect1.go
indirect.go
init1.go test: deflake init1.go test, tighten its bounds 2015-02-13 22:13:56 +00:00
init.go
initcomma.go
initialize.go
initializerr.go
initloop.go cmd/compile: use []*Node instead of NodeList in sinit 2015-09-05 02:25:01 +00:00
inline.go cmd/compile: add support for a go:noinline directive 2015-10-29 23:16:27 +00:00
int_lit.go
intcvt.go
iota.go
label1.go
label.go
linkmain_run.go test: fix linkmain test 2015-12-17 23:36:13 +00:00
linkmain.go cmd/link: reject non-package main toplevel.a file, remove dead code 2015-12-17 20:59:51 +00:00
linkx_run.go cmd/link: detect -X setting non-string variable 2015-06-29 20:28:36 +00:00
linkx.go cmd/link: detect -X setting non-string variable 2015-06-29 20:28:36 +00:00
literal.go
live1.go
live2.go cmd/gc: allocate non-escaping maps on stack 2015-02-12 09:53:52 +00:00
live_syscall.go cmd/compile: recognize Syscall-like functions for liveness analysis 2016-01-14 01:16:45 +00:00
live.go test: add -update_errors flag to run script 2015-04-10 11:33:42 +00:00
mallocfin.go
map1.go
map.go test/mapnan.go: add regression test for non-empty interfaces. 2014-08-06 17:02:55 -04:00
maplinear.go test: make maplinear more robust 2014-10-27 18:59:02 -04:00
method1.go
method2.go
method3.go
method4.go
method5.go
method.go
named1.go cmd/gc: comma-ok assignments produce untyped bool as 2nd result 2014-08-11 16:11:55 -07:00
named.go gc: return of ideal bool 2012-02-21 22:54:07 -05:00
nil.go
nilcheck.go cmd/gc: optimize existence-only map lookups 2015-01-07 22:36:06 +00:00
nilptr2.go
nilptr3.go test: fix nosplit.go, fixedbugs/issue11656.go and skip two tests for mips64{,le} 2015-11-12 04:52:31 +00:00
nilptr4.go
nilptr.go
nosplit.go cmd/internal/obj, cmd/link, runtime: increase stack limit to accommodate larger frames on ppc64x 2015-11-12 22:32:16 +00:00
nul1.go
parentype.go
peano.go
print.go runtime: move built-in print routines to go. 2014-07-31 13:48:48 -07:00
print.out runtime: move built-in print routines to go. 2014-07-31 13:48:48 -07:00
printbig.go
printbig.out
range.go
recover1.go
recover2.go
recover3.go
recover4.go test: move allocation before munmap in recover4 2015-09-16 08:51:48 +00:00
recover.go test: add more cases to recover.go 2014-10-22 08:06:15 -07:00
rename1.go
rename.go
reorder2.go test: use go:noinline consistently 2015-11-03 02:01:34 +00:00
reorder.go
return.go
rotate0.go test: re-enable rotate tests 2015-05-28 19:40:58 +00:00
rotate1.go test: re-enable rotate tests 2015-05-28 19:40:58 +00:00
rotate2.go test: re-enable rotate tests 2015-05-28 19:40:58 +00:00
rotate3.go test: re-enable rotate tests 2015-05-28 19:40:58 +00:00
rotate.go
run.go misc/cgo/stdio: reenable tests 2015-12-05 21:24:30 +00:00
rune.go
runtime.go
shift1.go test: avoid "declared but not used" errors in shift1.go 2014-07-20 12:25:24 -07:00
shift2.go
sieve.go
sigchld.go
sigchld.out
simassign.go
sinit_run.go all: retire architecture letter in file names, public API 2015-05-21 17:32:17 +00:00
sinit.go cmd/compile: allow static init for unsafe.Pointer(&x) where x is global 2015-07-07 21:51:57 +00:00
sizeof.go
slice3.go cmd/gc, runtime: treat slices and strings like pointers in garbage collection 2014-08-25 14:38:19 -04:00
slice3err.go
slicecap.go cmd/gc, runtime: treat slices and strings like pointers in garbage collection 2014-08-25 14:38:19 -04:00
sliceopt.go cmd/internal/gc: optimize slice + write barrier 2015-05-13 19:20:39 +00:00
solitaire.go
stack.go
string_lit.go
stringrange.go
struct0.go
switch2.go cmd/compile: address several more 1.6 TODOs in parser 2015-11-21 07:21:23 +00:00
switch3.go
switch4.go
switch.go
tinyfin.go runtime: fix finalization and profiling of tiny allocations 2015-11-03 18:57:18 +00:00
torture.go
turing.go
typecheck.go
typecheckloop.go cmd/compile: convert typecheck_stack to []*Node 2015-09-06 23:50:51 +00:00
typeswitch1.go
typeswitch2.go
typeswitch3.go
typeswitch.go
undef.go
utf.go
varerr.go
varinit.go
writebarrier.go cmd/internal/gc: add missing write barrier in append(x, BigStructWithPointers) 2015-05-19 15:28:29 +00:00
zerodivide.go