bytedance / mockey

a simple and easy-to-use golang mock library
Apache License 2.0
556 stars 22 forks source link

GetMethod panic with go1.21 #34

Closed LAShZ closed 10 months ago

LAShZ commented 10 months ago

Describe the bug

panic with go1.21

To Reproduce

type One interface {
    ReturnOne() string
}

type S struct{}

func (s S) ReturnOne() string {
    return "one"
}

func TestMock(t *testing.T) {
    one := S{}

    defer mockey.Mock(mockey.GetMethod(one, "ReturnOne")).Return("two").Build().UnPatch()

    fmt.Println(one.ReturnOne())
}

Run the test

Expected behavior

=== RUN   TestMock
two
--- PASS: TestMock (0.00s)
PASS
ok      test_code       0.589s

Screenshots

=== RUN   TestMock
fatal error: index out of range

goroutine 34 [running]:
runtime.throw({0x1027ede82?, 0x1027ebfe2?})
        /usr/local/go/src/runtime/panic.go:1077 +0x40 fp=0x14000187510 sp=0x140001874e0 pc=0x1025ea9d0
runtime.panicCheck1(0x13?, {0x1027ede82, 0x12})
        /usr/local/go/src/runtime/panic.go:58 +0x78 fp=0x14000187540 sp=0x14000187510 pc=0x1025e84c8
runtime.goPanicIndex(0xb0, 0x11)
        /usr/local/go/src/runtime/panic.go:113 +0x34 fp=0x14000187580 sp=0x14000187540 pc=0x1025e8594
runtime.stwReason.String(0x14?)
        /usr/local/go/src/runtime/proc.go:1217 +0x54 fp=0x140001875a0 sp=0x14000187580 pc=0x1025ef1b4
runtime.stopTheWorld(0xb0)
        /usr/local/go/src/runtime/proc.go:1260 +0x34 fp=0x140001875e0 sp=0x140001875a0 pc=0x1025ef204
github.com/bytedance/mockey/internal/monkey/mem.WriteWithSTW(0x1027e98b0, {0x14000130260, 0x18, 0x20})
        /Users/lavch/.gvm/pkgsets/system/global/pkg/mod/github.com/bytedance/mockey@v1.2.0/internal/monkey/mem/write.go:27 +0x30 fp=0x140001876b0 sp=0x140001875e0 pc=0x1027e2880
github.com/bytedance/mockey/internal/monkey.PatchValue({0x140001005a0, 0x14000114130, 0x13}, {0x140001005a0, 0x1400010f170, 0x13}, {0x14000128e00, 0x14000114150, 0x16}, 0x0)
        /Users/lavch/.gvm/pkgsets/system/global/pkg/mod/github.com/bytedance/mockey@v1.2.0/internal/monkey/patch.go:71 +0x918 fp=0x14000187a10 sp=0x140001876b0 pc=0x1027e5748
github.com/bytedance/mockey.(*Mocker).Patch(0x14000117450)
        /Users/lavch/.gvm/pkgsets/system/global/pkg/mod/github.com/bytedance/mockey@v1.2.0/mock.go:243 +0x164 fp=0x14000187c10 sp=0x14000187a10 pc=0x1027e7b24
github.com/bytedance/mockey.(*MockBuilder).Build(0x14000124300)
        /Users/lavch/.gvm/pkgsets/system/global/pkg/mod/github.com/bytedance/mockey@v1.2.0/mock.go:156 +0x174 fp=0x14000187d60 sp=0x14000187c10 pc=0x1027e6d74
test_code.TestMock(0x14000126820)
        /Users/lavch/repo/test_code/main_test.go:23 +0xd0 fp=0x14000187e80 sp=0x14000187d60 pc=0x1027e99b0
testing.tRunner(0x14000126820, 0x10286ec40)
        /usr/local/go/src/testing/testing.go:1595 +0x164 fp=0x14000187fa0 sp=0x14000187e80 pc=0x102702f84
testing.(*T).Run.func1()
        /usr/local/go/src/testing/testing.go:1648 +0x38 fp=0x14000187fd0 sp=0x14000187fa0 pc=0x102704618
runtime.goexit()
        /usr/local/go/src/runtime/asm_arm64.s:1197 +0x4 fp=0x14000187fd0 sp=0x14000187fd0 pc=0x10261fb24
created by testing.(*T).Run in goroutine 1
        /usr/local/go/src/testing/testing.go:1648 +0x660

goroutine 1 [chan receive]:
runtime.gopark(0x10286ed80, 0x14000166288, 0xe, 0x17, 0x2)
        /usr/local/go/src/runtime/proc.go:398 +0xe0 fp=0x140001553e0 sp=0x140001553b0 pc=0x1025ed3c0
runtime.chanrecv(0x14000166230, 0x140001554de, 0x1)
        /usr/local/go/src/runtime/chan.go:583 +0x27c fp=0x14000155460 sp=0x140001553e0 pc=0x1025b689c
runtime.chanrecv1(0x1029bb400?, 0x1028298e0?)
        /usr/local/go/src/runtime/chan.go:442 +0x14 fp=0x14000155490 sp=0x14000155460 pc=0x1025b65e4
testing.(*T).Run(0x14000126820, {0x1027eae19, 0x8}, 0x10286ec40)
        /usr/local/go/src/testing/testing.go:1649 +0x680 fp=0x14000155690 sp=0x14000155490 pc=0x102704420
testing.runTests.func1(0x14000126680)
        /usr/local/go/src/testing/testing.go:2054 +0x98 fp=0x14000155730 sp=0x14000155690 pc=0x102707708
testing.tRunner(0x14000126680, 0x14000155958)
        /usr/local/go/src/testing/testing.go:1595 +0x164 fp=0x14000155850 sp=0x14000155730 pc=0x102702f84
testing.runTests(0x1400011c120, {0x1029b19f0, 0x1, 0x1}, {0xc12fa2bdf604cbe0, 0x6fc342e67, 0x1029bb800})
        /usr/local/go/src/testing/testing.go:2052 +0x420 fp=0x14000155980 sp=0x14000155850 pc=0x102707550
testing.(*M).Run(0x14000119220)
        /usr/local/go/src/testing/testing.go:1925 +0x984 fp=0x14000155ec0 sp=0x14000155980 pc=0x102705654
main.main()
        _testmain.go:47 +0x98 fp=0x14000155f50 sp=0x14000155ec0 pc=0x1027e9cd8
runtime.main()
        /usr/local/go/src/runtime/proc.go:267 +0x244 fp=0x14000155fd0 sp=0x14000155f50 pc=0x1025ecf64
runtime.goexit()
        /usr/local/go/src/runtime/asm_arm64.s:1197 +0x4 fp=0x14000155fd0 sp=0x14000155fd0 pc=0x10261fb24

goroutine 2 [force gc (idle)]:
runtime.gopark(0x10286f0b0, 0x1029bb060, 0x11, 0x14, 0x1)
        /usr/local/go/src/runtime/proc.go:398 +0xe0 fp=0x14000050f70 sp=0x14000050f40 pc=0x1025ed3c0
runtime.goparkunlock(0x0?, 0x0?, 0x0?, 0x0?)
        /usr/local/go/src/runtime/proc.go:404 +0x34 fp=0x14000050fa0 sp=0x14000050f70 pc=0x1025ed454
runtime.forcegchelper()
        /usr/local/go/src/runtime/proc.go:322 +0xb4 fp=0x14000050fd0 sp=0x14000050fa0 pc=0x1025ed1e4
runtime.goexit()
        /usr/local/go/src/runtime/asm_arm64.s:1197 +0x4 fp=0x14000050fd0 sp=0x14000050fd0 pc=0x10261fb24
created by runtime.init.6 in goroutine 1
        /usr/local/go/src/runtime/proc.go:310 +0x24

goroutine 3 [GC sweep wait]:
runtime.gopark(0x10286f0b0, 0x1029bb560, 0xc, 0x14, 0x1)
        /usr/local/go/src/runtime/proc.go:398 +0xe0 fp=0x14000051730 sp=0x14000051700 pc=0x1025ed3c0
runtime.goparkunlock(0x0?, 0x0?, 0x0?, 0x0?)
        /usr/local/go/src/runtime/proc.go:404 +0x34 fp=0x14000051760 sp=0x14000051730 pc=0x1025ed454
runtime.bgsweep(0x0?)
        /usr/local/go/src/runtime/mgcsweep.go:280 +0x9c fp=0x140000517b0 sp=0x14000051760 pc=0x1025d6b1c
runtime.gcenable.func1()
        /usr/local/go/src/runtime/mgc.go:200 +0x28 fp=0x140000517d0 sp=0x140000517b0 pc=0x1025cb918
runtime.goexit()
        /usr/local/go/src/runtime/asm_arm64.s:1197 +0x4 fp=0x140000517d0 sp=0x140000517d0 pc=0x10261fb24
created by runtime.gcenable in goroutine 1
        /usr/local/go/src/runtime/mgc.go:200 +0x6c

goroutine 17 [GC scavenge wait]:
runtime.gopark(0x10286f0b0, 0x1029bb900, 0xd, 0x14, 0x2)
        /usr/local/go/src/runtime/proc.go:398 +0xe0 fp=0x1400004c720 sp=0x1400004c6f0 pc=0x1025ed3c0
runtime.goparkunlock(0x140000240e0?, 0x40?, 0xe3?, 0x1?)
        /usr/local/go/src/runtime/proc.go:404 +0x34 fp=0x1400004c750 sp=0x1400004c720 pc=0x1025ed454
runtime.(*scavengerState).park(0x1029bb900)
        /usr/local/go/src/runtime/mgcscavenge.go:425 +0x4c fp=0x1400004c780 sp=0x1400004c750 pc=0x1025d3f9c
runtime.bgscavenge(0x0?)
        /usr/local/go/src/runtime/mgcscavenge.go:653 +0x44 fp=0x1400004c7b0 sp=0x1400004c780 pc=0x1025d44d4
runtime.gcenable.func2()
        /usr/local/go/src/runtime/mgc.go:201 +0x28 fp=0x1400004c7d0 sp=0x1400004c7b0 pc=0x1025cb8b8
runtime.goexit()
        /usr/local/go/src/runtime/asm_arm64.s:1197 +0x4 fp=0x1400004c7d0 sp=0x1400004c7d0 pc=0x10261fb24
created by runtime.gcenable in goroutine 1
        /usr/local/go/src/runtime/mgc.go:201 +0xac

goroutine 33 [finalizer wait]:
runtime.gopark(0x10286ede0, 0x1029ef788, 0x10, 0x14, 0x1)
        /usr/local/go/src/runtime/proc.go:398 +0xe0 fp=0x14000050580 sp=0x14000050550 pc=0x1025ed3c0
runtime.runfinq()
        /usr/local/go/src/runtime/mfinal.go:193 +0xf0 fp=0x140000507d0 sp=0x14000050580 pc=0x1025cab00
runtime.goexit()
        /usr/local/go/src/runtime/asm_arm64.s:1197 +0x4 fp=0x140000507d0 sp=0x140000507d0 pc=0x10261fb24
created by runtime.createfing in goroutine 1
        /usr/local/go/src/runtime/mfinal.go:163 +0x4c
FAIL    test_code       0.765s
FAIL

Mockey version:

v1.2.0

Environment:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/lavch/Library/Caches/go-build'
GOENV='/Users/lavch/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/lavch/.gvm/pkgsets/system/global/pkg/mod'
GONOPROXY='*.byted.org,*.everphoto.cn,git.smartisan.com'
GONOSUMDB='*.byted.org,*.everphoto.cn,git.smartisan.com'
GOOS='darwin'
GOPATH='/Users/lavch/.gvm/pkgsets/system/global'
GOPRIVATE='*.byted.org,*.everphoto.cn,git.smartisan.com'
GOPROXY='https://goproxy.byted.org|https://goproxy.cn|direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.google.cn'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='0'
GOMOD='/Users/lavch/repo/test_code/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/6d/3ws4l1qx5jn9crwf_j_1x2hw0000gp/T/go-build3704825066=/tmp/go-build -gno-record-gcc-switches -fno-common'
gousteris commented 9 months ago

is there any workaround for this? I have the same issue

Sychorius commented 9 months ago

is there any workaround for this? I have the same issue

Mockey currently does not support go1.21, we will fix it in one month. you can use go1.20 instead