gcc/libgo/go
Ian Lance Taylor 7b28fa2c6b reflect: allocate correct type in assignTo and cvtT2I
Backport https://codereview.appspot.com/155450044 from the
master Go library.  Original description:

I came across this while debugging a GC problem in gccgo.
There is code in assignTo and cvtT2I that handles assignment
to all interface values.  It allocates an empty interface even
if the real type is a non-empty interface.  The fields are
then set for a non-empty interface, but the memory is recorded
as holding an empty interface.  This means that the GC has
incorrect information.

This is extremely unlikely to fail, because the code in the GC
that handles empty interfaces looks like this:

obj = nil;
typ = eface->type;
if(typ != nil) {
        if(!(typ->kind&KindDirectIface) || !(typ->kind&KindNoPointers))
                obj = eface->data;

In the current runtime the condition is always true--if
KindDirectIface is set, then KindNoPointers is clear--and we
always want to set obj = eface->data.  So the question is what
happens when we incorrectly store a non-empty interface value
in memory marked as an empty interface.  In that case
eface->type will not be a *rtype as we expect, but will
instead be a pointer to an Itab.  We are going to use this
pointer to look at a *rtype kind field.  The *rtype struct
starts out like this:

type rtype struct {
        size          uintptr
        hash          uint32            // hash of type; avoids computation in hash tables
        _             uint8             // unused/padding
        align         uint8             // alignment of variable with this type
        fieldAlign    uint8             // alignment of struct field with this type
        kind          uint8             // enumeration for C

An Itab always has at least two pointers, so on a
little-endian 64-bit system the kind field will be the high
byte of the second pointer.  This will normally be zero, so
the test of typ->kind will succeed, which is what we want.

On a 32-bit system it might be possible to construct a failing
case by somehow getting the Itab for an interface with one
method to be immediately followed by a word that is all ones.
The effect would be that the test would sometimes fail and the
GC would not mark obj, leading to an invalid dangling
pointer.  I have not tried to construct this test.

I noticed this in gccgo, where this error is much more likely
to cause trouble for a rather random reason: gccgo uses a
different layout of rtype, and in gccgo the kind field happens
to be the low byte of a pointer, not the high byte.

From-SVN: r216489
2014-10-20 18:04:55 +00:00
..
archive libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
bufio libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
builtin
bytes libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
compress libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
container libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
crypto libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
database/sql libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
debug libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
encoding libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
errors
exp
expvar libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
flag libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
fmt libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
go libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
hash libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
html libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
image libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
index/suffixarray
io libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
log libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
math libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
mime libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
net libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
old
os libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
path libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
reflect reflect: allocate correct type in assignTo and cvtT2I 2014-10-20 18:04:55 +00:00
regexp libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
runtime compiler: Add precise type information on the heap. 2014-09-03 22:56:09 +00:00
sort libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
strconv libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
strings libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
sync libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
syscall runtime: Use the clone system call on GNU/Linux. 2014-09-05 15:24:36 +00:00
testing libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
text libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
time libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00
unicode libgo: Update to Go 1.3 release. 2014-07-19 08:53:52 +00:00