Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zchee/d0c3c5fc339bca4826eeb98ebc73e064 to your computer and use it in GitHub Desktop.
Save zchee/d0c3c5fc339bca4826eeb98ebc73e064 to your computer and use it in GitHub Desktop.
Printing addresses of empty structs affects their values (https://github.com/golang/go/issues/25699)

From https://godbolt.org/

x86 gc 1.10.1

package main

import (
	"fmt"
)

func main() {
	var a, b struct{}
	fmt.Println(&a, &b)
	fmt.Println(&a == &b) // true
}
        text    "".main(SB), $120-0
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     258
        subq    $120, SP
        movq    BP, 112(SP)
        leaq    112(SP), BP
        funcdata        $0, gclocals·f6bd6b3389b872033d462029172c8612(SB)
        funcdata        $1, gclocals·1044ffefec669683581bed15df4f1d5b(SB)
        leaq    type.struct {}(SB), AX
        movq    AX, (SP)
        pcdata  $0, $0
        call    runtime.newobject(SB)
        movq    8(SP), AX
        movq    AX, "".&a+56(SP)
        leaq    type.struct {}(SB), CX
        movq    CX, (SP)
        pcdata  $0, $1
        call    runtime.newobject(SB)
        movq    8(SP), AX
        movq    AX, "".&b+48(SP)
        xorps   X0, X0
        movups  X0, ""..autotmp_4+80(SP)
        movups  X0, ""..autotmp_4+96(SP)
        leaq    type.*struct {}(SB), CX
        movq    CX, ""..autotmp_4+80(SP)
        movq    "".&a+56(SP), DX
        movq    DX, ""..autotmp_4+88(SP)
        movq    CX, ""..autotmp_4+96(SP)
        movq    AX, ""..autotmp_4+104(SP)
        leaq    ""..autotmp_4+80(SP), CX
        movq    CX, (SP)
        movq    $2, 8(SP)
        movq    $2, 16(SP)
        pcdata  $0, $2
        call    fmt.Println(SB)
        xorps   X0, X0
        movups  X0, ""..autotmp_7+64(SP)
        leaq    type.bool(SB), AX
        movq    AX, ""..autotmp_7+64(SP)
        movq    "".&a+56(SP), AX
        movq    "".&b+48(SP), CX
        cmpq    AX, CX
        seteq   AL
        movblzx AL, AX
        leaq    runtime.staticbytes(SB), CX
        addq    CX, AX
        movq    AX, ""..autotmp_7+72(SP)
        leaq    ""..autotmp_7+64(SP), AX
        movq    AX, (SP)
        movq    $1, 8(SP)
        movq    $1, 16(SP)
        pcdata  $0, $3
        call    fmt.Println(SB)
        movq    112(SP), BP
        addq    $120, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
        text    "".init(SB), $8-0
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     84
        subq    $8, SP
        movq    BP, (SP)
        leaq    (SP), BP
        funcdata        $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        funcdata        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        movblzx "".initdone·(SB), AX
        cmpb    AL, $1
        jls     47
        movq    (SP), BP
        addq    $8, SP
        ret
        jne     56
        pcdata  $0, $0
        call    runtime.throwinit(SB)
        undef
        movb    $1, "".initdone·(SB)
        pcdata  $0, $0
        call    fmt.init(SB)
        movb    $2, "".initdone·(SB)
        movq    (SP), BP
        addq    $8, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
        text    type..hash.[2]interface {}(SB), DUPOK, $40-24
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     103
        subq    $40, SP
        movq    BP, 32(SP)
        leaq    32(SP), BP
        funcdata        $0, gclocals·d4dc2f11db048877dbc0f60a22b4adb3(SB)
        funcdata        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        xorl    AX, AX
        movq    "".h+56(SP), CX
        jmp     82
        movq    AX, "".i+24(SP)
        shlq    $4, AX
        movq    "".p+48(SP), BX
        addq    BX, AX
        movq    AX, (SP)
        movq    CX, 8(SP)
        pcdata  $0, $0
        call    runtime.nilinterhash(SB)
        movq    16(SP), CX
        movq    "".i+24(SP), AX
        incq    AX
        cmpq    AX, $2
        jlt     38
        movq    CX, "".~r2+64(SP)
        movq    32(SP), BP
        addq    $40, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
        text    type..eq.[2]interface {}(SB), DUPOK, $48-24
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     155
        subq    $48, SP
        movq    BP, 40(SP)
        leaq    40(SP), BP
        funcdata        $0, gclocals·8f9cec06d1ae35cc9900c511c5e4bdab(SB)
        funcdata        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        xorl    AX, AX
        jmp     46
        movq    ""..autotmp_8+32(SP), CX
        leaq    1(CX), AX
        cmpq    AX, $2
        jge     140
        movq    AX, CX
        shlq    $4, AX
        movq    "".p+56(SP), DX
        movq    8(AX)(DX*1), BX
        movq    (AX)(DX*1), SI
        movq    "".q+64(SP), DI
        movq    8(AX)(DI*1), R8
        movq    (AX)(DI*1), AX
        cmpq    SI, AX
        jne     125
        movq    CX, ""..autotmp_8+32(SP)
        movq    SI, (SP)
        movq    BX, 8(SP)
        movq    R8, 16(SP)
        pcdata  $0, $0
        call    runtime.efaceeq(SB)
        movblzx 24(SP), AX
        testb   AL, AL
        jne     37
        movb    $0, "".~r2+72(SP)
        movq    40(SP), BP
        addq    $48, SP
        ret
        movb    $1, "".~r2+72(SP)
        movq    40(SP), BP
        addq    $48, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0

From https://godbolt.org/

x86 gc 1.10.1

package main

import (
	"fmt"
)

func main() {
	var a, b struct{}
	// fmt.Println(&a, &b)
	fmt.Println(&a == &b) // false
}
        text    "".main(SB), $72-0
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     111
        subq    $72, SP
        movq    BP, 64(SP)
        leaq    64(SP), BP
        funcdata        $0, gclocals·69c1753bd5f81501d95132d08af04464(SB)
        funcdata        $1, gclocals·e226d4ae4a7cad8835311c6a4683c14f(SB)
        xorps   X0, X0
        movups  X0, ""..autotmp_2+48(SP)
        leaq    type.bool(SB), AX
        movq    AX, ""..autotmp_2+48(SP)
        xorl    AX, AX
        movblzx AL, AX
        leaq    runtime.staticbytes(SB), CX
        addq    CX, AX
        movq    AX, ""..autotmp_2+56(SP)
        leaq    ""..autotmp_2+48(SP), AX
        movq    AX, (SP)
        movq    $1, 8(SP)
        movq    $1, 16(SP)
        pcdata  $0, $1
        call    fmt.Println(SB)
        movq    64(SP), BP
        addq    $72, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
        text    "".init(SB), $8-0
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     84
        subq    $8, SP
        movq    BP, (SP)
        leaq    (SP), BP
        funcdata        $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        funcdata        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        movblzx "".initdone·(SB), AX
        cmpb    AL, $1
        jls     47
        movq    (SP), BP
        addq    $8, SP
        ret
        jne     56
        pcdata  $0, $0
        call    runtime.throwinit(SB)
        undef
        movb    $1, "".initdone·(SB)
        pcdata  $0, $0
        call    fmt.init(SB)
        movb    $2, "".initdone·(SB)
        movq    (SP), BP
        addq    $8, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0

From https://godbolt.org/

x86 gc 1.10.1

package main

import (
	"fmt"
)

func main() {
	var a, b struct{}
	fmt.Println(&a == &b) // true. wtf
	fmt.Printf("a: %p, b: %p\n", &a, &b)
}
        text    "".main(SB), $136-0
        movq    (TLS), CX
        leaq    -8(SP), AX
        cmpq    AX, 16(CX)
        jls     299
        subq    $136, SP
        movq    BP, 128(SP)
        leaq    128(SP), BP
        funcdata        $0, gclocals·f6bd6b3389b872033d462029172c8612(SB)
        funcdata        $1, gclocals·fc04784923b15e2be0d0059277510785(SB)
        leaq    type.struct {}(SB), AX
        movq    AX, (SP)
        pcdata  $0, $0
        call    runtime.newobject(SB)
        movq    8(SP), AX
        movq    AX, "".&a+72(SP)
        leaq    type.struct {}(SB), CX
        movq    CX, (SP)
        pcdata  $0, $1
        call    runtime.newobject(SB)
        movq    8(SP), AX
        movq    AX, "".&b+64(SP)
        xorps   X0, X0
        movups  X0, ""..autotmp_4+80(SP)
        leaq    type.bool(SB), CX
        movq    CX, ""..autotmp_4+80(SP)
        movq    "".&a+72(SP), CX
        cmpq    CX, AX
        seteq   DL
        movblzx DL, DX
        leaq    runtime.staticbytes(SB), BX
        addq    BX, DX
        movq    DX, ""..autotmp_4+88(SP)
        leaq    ""..autotmp_4+80(SP), DX
        movq    DX, (SP)
        movq    $1, 8(SP)
        movq    $1, 16(SP)
        pcdata  $0, $2
        call    fmt.Println(SB)
        xorps   X0, X0
        movups  X0, ""..autotmp_6+96(SP)
        movups  X0, ""..autotmp_6+112(SP)
        leaq    type.*struct {}(SB), AX
        movq    AX, ""..autotmp_6+96(SP)
        movq    "".&a+72(SP), CX
        movq    CX, ""..autotmp_6+104(SP)
        movq    AX, ""..autotmp_6+112(SP)
        movq    "".&b+64(SP), AX
        movq    AX, ""..autotmp_6+120(SP)
        leaq    go.string."a: %p, b: %p\n"(SB), AX
        movq    AX, (SP)
        movq    $13, 8(SP)
        leaq    ""..autotmp_6+96(SP), AX
        movq    AX, 16(SP)
        movq    $2, 24(SP)
        movq    $2, 32(SP)
        pcdata  $0, $3
        call    fmt.Printf(SB)
        movq    128(SP), BP
        addq    $136, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
        text    "".init(SB), $8-0
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     84
        subq    $8, SP
        movq    BP, (SP)
        leaq    (SP), BP
        funcdata        $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        funcdata        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        movblzx "".initdone·(SB), AX
        cmpb    AL, $1
        jls     47
        movq    (SP), BP
        addq    $8, SP
        ret
        jne     56
        pcdata  $0, $0
        call    runtime.throwinit(SB)
        undef
        movb    $1, "".initdone·(SB)
        pcdata  $0, $0
        call    fmt.init(SB)
        movb    $2, "".initdone·(SB)
        movq    (SP), BP
        addq    $8, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
        text    type..hash.[2]interface {}(SB), DUPOK, $40-24
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     103
        subq    $40, SP
        movq    BP, 32(SP)
        leaq    32(SP), BP
        funcdata        $0, gclocals·d4dc2f11db048877dbc0f60a22b4adb3(SB)
        funcdata        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        xorl    AX, AX
        movq    "".h+56(SP), CX
        jmp     82
        movq    AX, "".i+24(SP)
        shlq    $4, AX
        movq    "".p+48(SP), BX
        addq    BX, AX
        movq    AX, (SP)
        movq    CX, 8(SP)
        pcdata  $0, $0
        call    runtime.nilinterhash(SB)
        movq    16(SP), CX
        movq    "".i+24(SP), AX
        incq    AX
        cmpq    AX, $2
        jlt     38
        movq    CX, "".~r2+64(SP)
        movq    32(SP), BP
        addq    $40, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
        text    type..eq.[2]interface {}(SB), DUPOK, $48-24
        movq    (TLS), CX
        cmpq    SP, 16(CX)
        jls     155
        subq    $48, SP
        movq    BP, 40(SP)
        leaq    40(SP), BP
        funcdata        $0, gclocals·8f9cec06d1ae35cc9900c511c5e4bdab(SB)
        funcdata        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        xorl    AX, AX
        jmp     46
        movq    ""..autotmp_8+32(SP), CX
        leaq    1(CX), AX
        cmpq    AX, $2
        jge     140
        movq    AX, CX
        shlq    $4, AX
        movq    "".p+56(SP), DX
        movq    8(AX)(DX*1), BX
        movq    (AX)(DX*1), SI
        movq    "".q+64(SP), DI
        movq    8(AX)(DI*1), R8
        movq    (AX)(DI*1), AX
        cmpq    SI, AX
        jne     125
        movq    CX, ""..autotmp_8+32(SP)
        movq    SI, (SP)
        movq    BX, 8(SP)
        movq    R8, 16(SP)
        pcdata  $0, $0
        call    runtime.efaceeq(SB)
        movblzx 24(SP), AX
        testb   AL, AL
        jne     37
        movb    $0, "".~r2+72(SP)
        movq    40(SP), BP
        addq    $48, SP
        ret
        movb    $1, "".~r2+72(SP)
        movq    40(SP), BP
        addq    $48, SP
        ret
        nop
        pcdata  $0, $-1
        call    runtime.morestack_noctxt(SB)
        jmp     0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment