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

Conversation

taoso
Copy link
Member

@taoso taoso commented Aug 19, 2022

Fix #8

Go 会为泛型函数生成中间函数。但实际需要 mock 的是中间函数所
调用的公共函数。为此,我们需要遍历中间函数机器码,通过 CALL
指令确定公共函数的地址。

如果函数使用了 struct 值传递,Go 在一些场景下会插入若干额外
的 CALL 指令。这样 monkey 就会拿到错误的公共函数地址。

为此,我们需要跳过因为 struct 传值而需要执行的 CALL 指令。

通过观察生成的汇编代码,我发现这些 CALL 指令用 BP 寄存器,而
调用公共函数用的是 AX 寄存器。所以可以根据寄存器参数来过滤。

Fix #8

Go 会为泛型函数生成中间函数。但实际需要 mock 的是中间函数所
调用的公共函数。为此,我们需要遍历中间函数机器码,通过 CALL
指令确定公共函数的地址。

如果函数使用了 struct 值传递,Go 在一些场景下会插入若干额外
的 CALL 指令。这样 monkey 就会拿到错误的公共函数地址。

为此,我们需要跳过因为 struct 传值而需要执行的 CALL 指令。

通过观察生成的汇编代码,我发现这些 CALL 指令用 BP 寄存器,而
调用公共函数用的是 AX 寄存器。所以可以根据寄存器参数来过滤。
Copy link

@crazyhulk crazyhulk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下次建议放上部分 你观察到的代码汇编区别.

@taoso taoso merged commit a0227a6 into master Aug 20, 2022
@taoso taoso deleted the fix-issue-8 branch August 20, 2022 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

结构体字段多导致 go test 卡住
2 participants