Trying to understand *itab / runtime representation of interfaces.

132 views
Skip to first unread message

dave.t...@gmail.com

unread,
Oct 29, 2015, 11:41:46 AM10/29/15
to golang-nuts


    http://play.golang.org/p/JjzURx7rDN

     I've read Russ Cox's great explanation of this stuff at http://research.swtch.com/interfaces, and I'm trying to play with it a little bit.

     I'm using reflect.Value.InterfaceData() and assuming that the the two words it returns are the the two word of the interface value Russ is talking about - he doesn't explicitly say this but looking at the source of InterfaceData() seems to confirm it - they exist side-by-side in memory and that memory location is the interface value, etc.

     I'm seeing the second word pointing to the implementation binary data, as expected.

     But the first word, which is supposed to point to an itab struct, appears to not be.  The first word in the memory it points to is a low integer (not a valid memory address - I expected an *interfacetype as per the runtime source), and when a later field in the struct layout is accessed, the program segfaults with a memory protection violation.

    So, what's going on?   Did things somehow change in 1.5?   Is the above explanation still valid as far as what to expect in that second word?   Or is it something else, now?   Or am I doing something wrong?

Ian Lance Taylor

unread,
Oct 29, 2015, 12:04:58 PM10/29/15
to dave.t...@gmail.com, golang-nuts
The problem is that your ValueHolder type uses an empty interface.
The first word of an empty interface points to a type descriptor (that
is, it has type *reflect.rtype == *runtime._type). Only the first
word of a non-empty interface points to an itab struct. Try this
version: http://play.golang.org/p/XMtrXBx_bL .

Ian

Dave Trombley

unread,
Oct 29, 2015, 12:17:13 PM10/29/15
to Ian Lance Taylor, golang-nuts
Ah, got it, TYVM.   Yeah, I read a couple of times that empty-interfaces are special-cased (it's right in the thing I linked... =/)  but it didn't "sink in".   Thanks.
Reply all
Reply to author
Forward
0 new messages