diff --git a/README.md b/README.md index fed58b4..87f6d23 100644 --- a/README.md +++ b/README.md @@ -255,9 +255,6 @@ PS: It's also possible to specify the goroutine where the mocking takes effect u ## FAQ/troubleshooting -### Go 1.23 compile error `"link: github.com/bytedance/mockey/internal/monkey/common: invalid reference to runtime.sysAllocOS"`? -add build flag `-ldflags=-checklinkname=0` - ### How to disable inline and compile optimization? 1. Command line:`go test -gcflags="all=-l -N" -v ./...` 2. Goland:fill `-gcflags="all=-l -N"` in the **Run/Debug Configurations > Go tool arguments** dialog box diff --git a/go.mod b/go.mod index 4a16079..e838bb9 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,13 @@ module github.com/bytedance/mockey go 1.18 require ( - github.com/smartystreets/goconvey v1.6.4 - golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff - golang.org/x/sys v0.24.0 + github.com/smartystreets/goconvey v1.7.2 + golang.org/x/arch v0.11.0 + golang.org/x/sys v0.26.0 ) require ( - github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect + github.com/gopherjs/gopherjs v1.12.80 // indirect github.com/jtolds/gls v4.20.0+incompatible // indirect - github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect + github.com/smartystreets/assertions v1.2.0 // indirect ) diff --git a/go.sum b/go.sum index f947e5c..b632c76 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,39 @@ -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gopherjs/gopherjs v1.12.80 h1:aC68NT6VK715WeUapxcPSFq/a3gZdS32HdtghdOIgAo= +github.com/gopherjs/gopherjs v1.12.80/go.mod h1:d55Q4EjGQHeJVms+9LGtXul6ykz5Xzx1E1gaXQXdimY= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= -github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= -golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff h1:XmKBi9R6duxOB3lfc72wyrwiOY7X2Jl1wuI+RFOyMDE= -golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= +github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= +github.com/rogpeppe/go-internal v1.0.1-alpha.1/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk= +github.com/shurcooL/httpfs v0.0.0-20181222201310-74dc9339e414/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= +github.com/shurcooL/vfsgen v0.0.0-20180915214035-33ae1944be3f/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= +github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= +github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= +github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= +github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= +github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4= +golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.0.0-20180807104621-f027049dab0a/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20180807162357-acbc56fc7007/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190308142131-b40df0fb21c3/go.mod h1:25r3+/G6/xytQM8iWZKq3Hn0kr0rgFKPUNVEL/dr3z4= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/internal/monkey/common/runtime_link/stw/stw_1_21.go b/internal/monkey/common/runtime_link/stw/stw_1_21.go index 0a19eef..292dcd0 100644 --- a/internal/monkey/common/runtime_link/stw/stw_1_21.go +++ b/internal/monkey/common/runtime_link/stw/stw_1_21.go @@ -27,8 +27,7 @@ func newSTWCtx() ctx { return &stwCtx{} } -type stwCtx struct { -} +type stwCtx struct{} const stwForTestResetDebugLog = 16 diff --git a/internal/monkey/common/runtime_link/stw/stw_1_22.go b/internal/monkey/common/runtime_link/stw/stw_1_22.go index 447a6c0..efc57b5 100644 --- a/internal/monkey/common/runtime_link/stw/stw_1_22.go +++ b/internal/monkey/common/runtime_link/stw/stw_1_22.go @@ -36,6 +36,7 @@ const stwForTestResetDebugLog = 16 func (ctx *stwCtx) StopTheWorld() { ctx.w = stopTheWorld(stwForTestResetDebugLog) } + func (ctx *stwCtx) StartTheWorld() { startTheWorld(ctx.w) } diff --git a/internal/monkey/common/runtime_link/stw/stw_above_1_22.go b/internal/monkey/common/runtime_link/stw/stw_above_1_22.go index d86c689..2892864 100644 --- a/internal/monkey/common/runtime_link/stw/stw_above_1_22.go +++ b/internal/monkey/common/runtime_link/stw/stw_above_1_22.go @@ -36,6 +36,7 @@ const stwForTestResetDebugLog = 16 func (ctx *stwCtx) StopTheWorld() { ctx.w = stopTheWorld(stwForTestResetDebugLog) } + func (ctx *stwCtx) StartTheWorld() { startTheWorld(ctx.w) } diff --git a/internal/monkey/common/runtime_link/stw/stw_below_1_21.go b/internal/monkey/common/runtime_link/stw/stw_below_1_21.go index 5bf4c5f..d8cf871 100644 --- a/internal/monkey/common/runtime_link/stw/stw_below_1_21.go +++ b/internal/monkey/common/runtime_link/stw/stw_below_1_21.go @@ -27,8 +27,7 @@ func newSTWCtx() ctx { return &stwCtx{} } -type stwCtx struct { -} +type stwCtx struct{} func (ctx *stwCtx) StopTheWorld() { stopTheWorld("mockey") diff --git a/internal/monkey/common/runtime_link/stw/stw_disable.go b/internal/monkey/common/runtime_link/stw/stw_disable.go index 4e1bf6b..c7dba0d 100644 --- a/internal/monkey/common/runtime_link/stw/stw_disable.go +++ b/internal/monkey/common/runtime_link/stw/stw_disable.go @@ -27,8 +27,7 @@ func newSTWCtx() ctx { return &stwCtx{} } -type stwCtx struct { -} +type stwCtx struct{} func (ctx *stwCtx) StopTheWorld() { } diff --git a/internal/monkey/inst/disasm_amd64.go b/internal/monkey/inst/disasm_amd64.go index 193f3e0..29201ec 100644 --- a/internal/monkey/inst/disasm_amd64.go +++ b/internal/monkey/inst/disasm_amd64.go @@ -41,11 +41,9 @@ func calcFnAddrRange(name string, fn func()) (uintptr, uintptr) { for i := range inst.Args { args = append(args, inst.Args[i]) } - tool.DebugPrintf("init: <%v>\t%v\t%v\t%v\t%v\t%v\t%v\n", args...) if inst.Op == x86asm.RET { end = start + uintptr(pos) - tool.DebugPrintf("init: %v(%v,%v)\n", name, start, end) return start, end } diff --git a/internal/monkey/inst/disasm_arm64.go b/internal/monkey/inst/disasm_arm64.go index ef3a54d..3c66efc 100644 --- a/internal/monkey/inst/disasm_arm64.go +++ b/internal/monkey/inst/disasm_arm64.go @@ -40,11 +40,9 @@ func calcFnAddrRange(name string, fn func()) (uintptr, uintptr) { for i := range inst.Args { args = append(args, inst.Args[i]) } - tool.DebugPrintf("init: <%v>\t%v\t%v\t%v\t%v\t%v\t%v\n", args...) if inst.Op == arm64asm.RET { end = start + uintptr(pos) - tool.DebugPrintf("init: %v(%v,%v)\n", name, start, end) return start, end } @@ -80,7 +78,7 @@ func GetGenericJumpAddr(addr uintptr, maxScan uint64) uintptr { } if inst.Op == arm64asm.BL { - fnAddr := calcAddr(uintptr(unsafe.Pointer(&code[0]))+uintptr(pos), inst.Enc) + fnAddr := calcAddr(uintptr(unsafe.Pointer(&code[0]))+uintptr(pos), inst) isExtraCall, extraName := isGenericProxyCallExtra(fnAddr) tool.DebugPrintf("found BL, raw is: %x, fnAddr: %v, isExtraCall: %v, extraName: %v\n", inst.String(), fnAddr, isExtraCall, extraName) if !isExtraCall { @@ -93,23 +91,11 @@ func GetGenericJumpAddr(addr uintptr, maxScan uint64) uintptr { return allAddrs[0] } -func calcAddr(from uintptr, bl uint32) uintptr { - tool.DebugPrintf("calc BL addr, from: %x(%v) bl: %x\n", from, from, bl) - offset := bl << 8 >> 8 - flag := (offset << 9 >> 9) == offset // 是否小于0 - - var dest uintptr - if flag { - // L -> H - // (dest - cur) / 4 = offset - // dest = cur + offset * 4 - dest = from + uintptr(offset*4) +func calcAddr(from uintptr, bl arm64asm.Inst) uintptr { + distance := int64(bl.Args[0].(arm64asm.PCRel)) + if distance < 0 { + return from - uintptr(-distance) } else { - // H -> L - // (cur - dest) / 4 = (0x00ffffff - offset + 1) - // dest = cur - (0x00ffffff - offset + 1) * 4 - dest = from - uintptr((0x00ffffff-offset+1)*4) + return from + uintptr(distance) } - tool.DebugPrintf("2th complement, L->H:%v offset: %x from: %x(%v) dest: %x(%v), distance: %v\n", flag, offset, from, from, dest, dest, from-dest) - return dest } diff --git a/internal/monkey/inst/generic_extra_race.go b/internal/monkey/inst/generic_extra_race.go index 65b0f89..ab16420 100644 --- a/internal/monkey/inst/generic_extra_race.go +++ b/internal/monkey/inst/generic_extra_race.go @@ -22,6 +22,8 @@ package inst import ( "reflect" _ "unsafe" + + "github.com/bytedance/mockey/internal/tool" ) //go:linkname racefuncenter runtime.racefuncenter @@ -68,4 +70,8 @@ func init() { reflect.ValueOf(racewriterangepc1).Pointer(): "racewriterangepc1", reflect.ValueOf(racecallbackthunk).Pointer(): "racecallbackthunk", } + + for addr, name := range proxyCallRace { + tool.DebugPrintf("race func: %x(%v)\n", addr, name) + } } diff --git a/internal/monkey/mem/write_darwin.go b/internal/monkey/mem/write_darwin.go index 5c80fae..529a300 100644 --- a/internal/monkey/mem/write_darwin.go +++ b/internal/monkey/mem/write_darwin.go @@ -33,6 +33,7 @@ func Write(target uintptr, data []byte) error { if res != 0 { return fmt.Errorf("write failed, code %v", res) } + tool.DebugPrintf("Write end(darwin)") return nil } diff --git a/internal/monkey/mem/write_linux.go b/internal/monkey/mem/write_linux.go index 4f60687..cab438c 100644 --- a/internal/monkey/mem/write_linux.go +++ b/internal/monkey/mem/write_linux.go @@ -33,6 +33,7 @@ func Write(target uintptr, data []byte) error { if res != 0 { return fmt.Errorf("write failed, code %v", res) } + tool.DebugPrintf("Write end(linux)") return nil }