diff options
author | Russ Cox <rsc@golang.org> | 2014-05-12 17:19:02 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2014-05-12 17:19:02 -0400 |
commit | ff9383c66e107280adcb1b38eee215f1da54e07d (patch) | |
tree | 5cea29e0244e5727a226fd5ae56e4add92a99b68 /src/cmd/6g | |
parent | d8543db621c41762e475c2e45f6b48918c2ce898 (diff) | |
download | go-ff9383c66e107280adcb1b38eee215f1da54e07d.tar.gz |
cmd/gc: fix liveness vs regopt mismatch for input variables
The inputs to a function are marked live at all times in the
liveness bitmaps, so that the garbage collector will not free
the things they point at and reuse the pointers, so that the
pointers shown in stack traces are guaranteed not to have
been recycled.
Unfortunately, no one told the register optimizer that the
inputs need to be preserved at all call sites. If a function
is done with a particular input value, the optimizer will stop
preserving it across calls. For single-word values this just
means that the value recorded might be stale. For multi-word
values like slices, the value recorded could be only partially stale:
it can happen that, say, the cap was updated but not the len,
or that the len was updated but not the base pointer.
Either of these possibilities (and others) would make the
garbage collector misinterpret memory, leading to memory
corruption.
This came up in a real program, in which the garbage collector's
'slice len ? slice cap' check caught the inconsistency.
Fixes issue 7944.
LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews, khr
https://codereview.appspot.com/100370045
Diffstat (limited to 'src/cmd/6g')
-rw-r--r-- | src/cmd/6g/opt.h | 1 | ||||
-rw-r--r-- | src/cmd/6g/reg.c | 21 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/cmd/6g/opt.h b/src/cmd/6g/opt.h index 3dcc3d747..bf356af0c 100644 --- a/src/cmd/6g/opt.h +++ b/src/cmd/6g/opt.h @@ -94,6 +94,7 @@ EXTERN Bits externs; EXTERN Bits params; EXTERN Bits consts; EXTERN Bits addrs; +EXTERN Bits ivar; EXTERN Bits ovar; EXTERN int change; EXTERN int32 maxnr; diff --git a/src/cmd/6g/reg.c b/src/cmd/6g/reg.c index 484c1c0cd..a1f0c756a 100644 --- a/src/cmd/6g/reg.c +++ b/src/cmd/6g/reg.c @@ -55,7 +55,7 @@ rcmp(const void *a1, const void *a2) } static void -setoutvar(void) +setvar(Bits *dst, Type **args) { Type *t; Node *n; @@ -64,18 +64,16 @@ setoutvar(void) Bits bit; int z; - t = structfirst(&save, getoutarg(curfn->type)); + t = structfirst(&save, args); while(t != T) { n = nodarg(t, 1); a = zprog.from; naddr(n, &a, 0); bit = mkvar(R, &a); for(z=0; z<BITS; z++) - ovar.b[z] |= bit.b[z]; + dst->b[z] |= bit.b[z]; t = structnext(&save); } -//if(bany(&ovar)) -//print("ovars = %Q\n", ovar); } static void @@ -176,11 +174,14 @@ regopt(Prog *firstp) params.b[z] = 0; consts.b[z] = 0; addrs.b[z] = 0; + ivar.b[z] = 0; ovar.b[z] = 0; } - // build list of return variables - setoutvar(); + // build lists of parameters and results + setvar(&ivar, getthis(curfn->type)); + setvar(&ivar, getinarg(curfn->type)); + setvar(&ovar, getoutarg(curfn->type)); /* * pass 1 @@ -750,8 +751,12 @@ prop(Reg *r, Bits ref, Bits cal) case ACALL: if(noreturn(r1->f.prog)) break; + + // Mark all input variables (ivar) as used, because that's what the + // liveness bitmaps say. The liveness bitmaps say that so that a + // panic will not show stale values in the parameter dump. for(z=0; z<BITS; z++) { - cal.b[z] |= ref.b[z] | externs.b[z]; + cal.b[z] |= ref.b[z] | externs.b[z] | ivar.b[z]; ref.b[z] = 0; } |