Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use x19 register(arm64) for BR instruction #33

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/monkey/inst/inst_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ func BranchTo(to uintptr) (res []byte) {
return
}

// create a branch into command
//
// Go supports passing function arguments from go 1.17 (see https://go.dev/doc/go1.17).
// We could not use x0~x18 register. As an alternative, we use R19 register (see https://go.googlesource.com/go/+/refs/heads/master/src/cmd/compile/abi-internal.md).
func BranchInto(to uintptr) (res []byte) {
// do not use x0~x18
res = append(res, x26MOV(to)...) // MOV x26, to // fake
res = append(res, []byte{0x4a, 0x03, 0x40, 0xf9}...) // LDR x10, [x26]
res = append(res, []byte{0x40, 0x01, 0x1f, 0xd6}...) // BR x10
res = append(res, []byte{0x53, 0x03, 0x40, 0xf9}...) // LDR x19, [x26]
res = append(res, []byte{0x60, 0x02, 0x1f, 0xd6}...) // BR x19
return
}

Expand Down
36 changes: 36 additions & 0 deletions mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,39 @@ func TestMockOrigin(t *testing.T) {
So((&foo{3}).Foo(), ShouldEqual, 4)
})
}

func TestMultiArgs(t *testing.T) {
PatchConvey("multi-arg-result", t, func() {
PatchConvey("multi-arg", func() {
// Go supports passing function arguments from go 1.17
//
// Mockey used to use X10 register to make BR instruction in
// arm64, which will cause arguments and results get a wrong value
//
// _0~_15 use x0~x15 register
fn := func(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20 int64) {
fmt.Println(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20)
}
ori := fn
Mock(fn).To(func(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20 int64) {
for _, _x := range []int64{_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20} {
So(_x, ShouldEqual, 0)
}
}).Origin(&ori).Build()
fn(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
})
PatchConvey("multi-result", func() {
fn := func() (_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20 int64) {
return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}
ori := fn
Mock(fn).To(func() (_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20 int64) {
return ori()
}).Origin(&ori).Build()
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20 := fn()
for _, _x := range []int64{_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20} {
So(_x, ShouldEqual, 0)
}
})
})
}