xhd2015 / xgo

All-in-one go testing library
MIT License
330 stars 19 forks source link

Bug: test hugo: map[*int]string: got "PTR???:0 PTR???:1 PTR???:2", want "PTR0:0 PTR1:1 PTR2:2" #177

Closed xhd2015 closed 4 months ago

xhd2015 commented 4 months ago

xgo test -v -run TestOrder ./tpl/internal/go_templates/fmtsort/

Go Version: Go1.22

Go test succeeds, xgo test fails:

$ go test -v -run TestOrder ./tpl/internal/go_templates/fmtsort/
=== RUN   TestOrder
--- PASS: TestOrder (0.00s)
PASS
ok      github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort      0.865s
You have mail in /var/mail/xhd2015
xhd2015@10.53.96.16 $X/hugo (master) 09:23:14 

$ xgo test -v -run TestOrder ./tpl/internal/go_templates/fmtsort/
=== RUN   TestOrder
    sort_test.go:242: map[*int]string: got "PTR???:0 PTR???:1 PTR???:2", want "PTR0:0 PTR1:1 PTR2:2"
    sort_test.go:242: map[unsafe.Pointer]string: got "UNSAFEPTR???:0 UNSAFEPTR???:1 UNSAFEPTR???:2", want "UNSAFEPTR0:0 UNSAFEPTR1:1 UNSAFEPTR2:2"
--- FAIL: TestOrder (0.00s)
FAIL
FAIL    github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort      0.690s
FAIL
exit status 1
xhd2015 commented 4 months ago

This can be replicated with the following test:

var ints [3]int

func TestArrayPointer(t *testing.T) {

    x := &ints[0]
    y := &ints[0]

    t.Logf("x=0x%x", x)
    t.Logf("y=0x%x", y)
}

Running go test succeeds, while xgo test fails:

$ go test -v ./
=== RUN   TestArrayPointer
    debug_test.go:19: x=0x124f640
    debug_test.go:20: y=0x124f640
--- PASS: TestArrayPointer (0.00s)
PASS
ok      github.com/xhd2015/xgo/runtime/test/debug       0.663s

$ xgo test -v ./
=== RUN   TestArrayPointer
    debug_test.go:19: x=0xc00001c1b0
    debug_test.go:20: y=0xc00001c1c8
    debug_test.go:22: x != y
--- FAIL: TestArrayPointer (0.00s)
FAIL
FAIL    github.com/xhd2015/xgo/runtime/test/debug       0.831s
FAIL
exit status 1
xhd2015 commented 4 months ago

The compiler will rewrite &ints[0] to

tmp_ints1 := ints
__trap(&tmp_ints1)
x := &tmp_ints1[0]

tmp_ints2 := ints
__trap(&tmp_ints2)
y:=&tmp_ints2[0]

That's why x and y has 24 byte(3 ints) offset when testing with xgo

xhd2015 commented 4 months ago

When encountering an Operation{Op: AND, X: Index}, where the Index might be slice, array or map, skip intercepting the expression. Since it's invalid for map. And for slice and array, it's value is not useful.