From 0abe4b4be8406edc9b9acb7f0bad18a185c6f4f2 Mon Sep 17 00:00:00 2001 From: Ruomenger Date: Mon, 9 Oct 2023 09:02:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=20getg=20=E5=9C=A8am?= =?UTF-8?q?d64=E5=B9=B3=E5=8F=B0=E4=B8=8A=E7=9A=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根据 https://go.googlesource.com/go/+/refs/heads/dev.regabi/src/cmd/compile/internal-abi.md 的描述,go使用r14寄存器保存当前协程。 --- .github/workflows/go.yml | 4 ++-- README.md | 4 +--- getg_amd64.go | 10 ++++++++++ getg_darwin.go | 11 ----------- getg_linux.go | 11 ----------- getg_windows.go | 13 ------------- 6 files changed, 13 insertions(+), 40 deletions(-) create mode 100644 getg_amd64.go delete mode 100644 getg_darwin.go delete mode 100644 getg_linux.go delete mode 100644 getg_windows.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5bb21b9..825b873 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,8 +4,8 @@ jobs: test: strategy: matrix: - os: [ubuntu-latest, macos-latest] - version: [1.18, 1.19, "1.20", 1.21] + os: [ubuntu-latest, macos-latest, windows-latest] + version: [1.18, 1.19, 1.20, 1.21] runs-on: ${{ matrix.os }} steps: - name: Set up Go ${{ matrix.version }} diff --git a/README.md b/README.md index f033834..11560c5 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,4 @@ func main() { 1. Monkey 需要关闭 Go 语言的内联优化才能生效,比如测试的时候需要:`go test -gcflags='all=-N -l'`。 2. Monkey 需要在运行的时候修改内存代码段,因而无法在一些对安全性要求比较高的系统上工作。 3. Monkey 不应该用于生产系统,但用来 mock 测试代码还是没有问题的。 -4. Monkey 目前仅支持 amd64 指令架构,支持 linux/macos/~~windows~~[^win] 平台。 - -[^win]: 目前在 Win 平台下 Go 1.20 版本之后会报错。欢迎熟悉 Win 平台的同学提供 PR。问题解决之前建议 Win 用户使用 WSL。 +4. Monkey 目前仅支持 amd64 指令架构,支持 linux/macos/windows 平台。 diff --git a/getg_amd64.go b/getg_amd64.go new file mode 100644 index 0000000..a951e0f --- /dev/null +++ b/getg_amd64.go @@ -0,0 +1,10 @@ +package monkey + +// getg 获取当前协程的指针,根据go内部abi的文档在amd64架构中使用r14寄存器保存当前的协程g地址 +// 参见 https://go.googlesource.com/go/+/refs/heads/dev.regabi/src/cmd/compile/internal-abi.md +func getg() []byte { + return []byte{ + // mov r12, r14 + 0x4D, 0x89, 0xF4, + } +} diff --git a/getg_darwin.go b/getg_darwin.go deleted file mode 100644 index 256cb7f..0000000 --- a/getg_darwin.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build darwin -// +build darwin - -package monkey - -func getg() []byte { - return []byte{ - // mov r12,QWORD PTR gs:0x30 - 0x65, 0x4C, 0x8B, 0x24, 0x25, 0x30, 0x00, 0x00, 0x00, - } -} diff --git a/getg_linux.go b/getg_linux.go deleted file mode 100644 index e83e912..0000000 --- a/getg_linux.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build linux -// +build linux - -package monkey - -func getg() []byte { - return []byte{ - // mov r12,QWORD PTR fs:0xfffffffffffffff8 - 0x64, 0x4C, 0x8B, 0x24, 0x25, 0xF8, 0xFF, 0xFF, 0xFF, - } -} diff --git a/getg_windows.go b/getg_windows.go deleted file mode 100644 index 19bd778..0000000 --- a/getg_windows.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build windows -// +build windows - -package monkey - -func getg() []byte { - return []byte{ - // mov r13,QWORD PTR gs:0x28 - 0x65, 0x4C, 0x8B, 0x2C, 0x25, 0x28, 0x00, 0x00, 0x00, - // mov r12,QWORD PTR [r13] - 0x4D, 0x8B, 0x65, 0x00, - } -}