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

修复 struct 传值导致泛型打桩卡住的问题 #10

Merged
merged 1 commit into from
Aug 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion monkey_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ func getFirstCallFunc(from uintptr) uintptr {
f := rawMemoryAccess(from, 1024)

s := 0
var lastLea x86asm.Inst
for {
i, err := x86asm.Decode(f[s:], 64)
if err != nil {
panic(err)
}
if i.Op == x86asm.CALL {
if i.Op == x86asm.CALL && lastLea.Args[0].(x86asm.Reg) == x86asm.RAX {
arg := i.Args[0]
imm := arg.(x86asm.Rel)
next := from + uintptr(s+i.Len)
Expand All @@ -116,5 +117,9 @@ func getFirstCallFunc(from uintptr) uintptr {
if s >= 1024 {
panic("Can not find CALL instruction")
}
// 有些情况下会生成 NOPW 指令,需要跳过
if i.Op == x86asm.LEA {
lastLea = i
}
}
}
25 changes: 25 additions & 0 deletions monkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,28 @@ func TestGeneric(t *testing.T) {
g2.Unpatch()
assert(t, 2 == s.Foo())
}

type TreeNode struct {
Name string
ID int
Children []*TreeNode
A int
B int
C int
// 注释掉go test可以正常运行
A1 int
B2 int
C1 int
}

func first[T any](a, b T) T { return a }

func second[T any](a, b T) T { return b }

// https://github.com/go-kiss/monkey/issues/8
func TestIssue8(t *testing.T) {
t1 := TreeNode{ID: 1}
t2 := TreeNode{ID: 2}
monkey.Patch(first[TreeNode], second[TreeNode], monkey.OptGeneric)
assert(t, t2.ID == first(t1, t2).ID)
}