From 56315bee3eb7415a4c3d88d4f26bd79c8333a434 Mon Sep 17 00:00:00 2001 From: xhd2015 Date: Tue, 16 Apr 2024 13:58:00 +0800 Subject: [PATCH] fix __xgo_on_gonewproc, see https://github.com/xhd2015/xgo/issues/67 --- README.md | 3 +-- README_zh_cn.md | 5 ++-- cmd/xgo/patch.go | 2 +- cmd/xgo/patch/runtime_def.go | 3 +-- cmd/xgo/patch_runtime.go | 52 +++++++++++++++++++++++++++++++----- cmd/xgo/version.go | 4 +-- runtime/core/version.go | 4 +-- 7 files changed, 55 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 16d61dae..7132dc30 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,6 @@ go mod init demo package demo_test import ( - "context" "testing" "github.com/xhd2015/xgo/runtime/mock" @@ -86,7 +85,7 @@ func MyFunc() string { func TestFuncMock(t *testing.T) { mock.Patch(MyFunc, func() string { - return "mock func" + return "mock func" }) text := MyFunc() if text != "mock func" { diff --git a/README_zh_cn.md b/README_zh_cn.md index 40662843..50307ac2 100644 --- a/README_zh_cn.md +++ b/README_zh_cn.md @@ -70,7 +70,6 @@ go mod init demo package demo_test import ( - "context" "testing" "github.com/xhd2015/xgo/runtime/mock" @@ -81,8 +80,8 @@ func MyFunc() string { } func TestFuncMock(t *testing.T) { - mock.Patch(MyFunc, func() string { - return "mock func" + mock.Patch(MyFunc, func() string { + return "mock func" }) text := MyFunc() if text != "mock func" { diff --git a/cmd/xgo/patch.go b/cmd/xgo/patch.go index ae3bbcb1..87dd7625 100644 --- a/cmd/xgo/patch.go +++ b/cmd/xgo/patch.go @@ -52,7 +52,7 @@ func patchRuntimeAndCompiler(origGoroot string, goroot string, xgoSrc string, go } // runtime - err := patchRuntimeAndTesting(goroot) + err := patchRuntimeAndTesting(goroot, goVersion) if err != nil { return err } diff --git a/cmd/xgo/patch/runtime_def.go b/cmd/xgo/patch/runtime_def.go index 37985879..d8fbb87b 100644 --- a/cmd/xgo/patch/runtime_def.go +++ b/cmd/xgo/patch/runtime_def.go @@ -8,9 +8,8 @@ __xgo_on_init_finished_callbacks = nil ` const RuntimeProcGoroutineCreatedPatch = `for _, fn := range __xgo_on_gonewproc_callbacks { - fn(uintptr(unsafe.Pointer(newg))) + fn(uintptr(unsafe.Pointer(xgo_newg))) } -return newg ` // added after goroutine exit1 diff --git a/cmd/xgo/patch_runtime.go b/cmd/xgo/patch_runtime.go index c520162e..3d6c133f 100644 --- a/cmd/xgo/patch_runtime.go +++ b/cmd/xgo/patch_runtime.go @@ -57,8 +57,8 @@ var runtimeFiles = []_FilePath{ timeSleep, } -func patchRuntimeAndTesting(goroot string) error { - err := patchRuntimeProc(goroot) +func patchRuntimeAndTesting(goroot string, goVersion *goinfo.GoVersion) error { + err := patchRuntimeProc(goroot, goVersion) if err != nil { return err } @@ -129,7 +129,7 @@ func addRuntimeFunctions(goroot string, goVersion *goinfo.GoVersion, xgoSrc stri return true, os.WriteFile(dstFile, content, 0755) } -func patchRuntimeProc(goroot string) error { +func patchRuntimeProc(goroot string, goVersion *goinfo.GoVersion) error { procFile := filepath.Join(goroot, filepath.Join(runtimeProc...)) anchors := []string{ "func main() {", @@ -148,12 +148,52 @@ func patchRuntimeProc(goroot string) error { patch.RuntimeProcGoroutineExitPatch, ) - content = replaceContentAfter(content, + procDecl := `func newproc(fn` + newProc := `newg := newproc1(fn, gp, pc)` + if goVersion.Major == 1 && goVersion.Minor <= 17 { + procDecl = `func newproc(siz int32` + newProc = `newg := newproc1(fn, argp, siz, gp, pc)` + } + + // see https://github.com/xhd2015/xgo/issues/67 + content = addContentAtIndex( + content, + "/**/", "/**/", + []string{ + procDecl, + `systemstack(func() {`, + newProc, + }, + 1, + true, + "var xgo_newg *g", + ) + content = addContentAtIndex( + content, + "/**/", "/**/", + []string{ + procDecl, + `systemstack(func() {`, + newProc, + "\n", + }, + 3, + false, + "xgo_newg = newg", + ) + + content = addContentAtIndex(content, "/**/", "/**/", []string{ - "func newproc1(", "*g {", + procDecl, + `systemstack(func() {`, + newProc, + "\n", + "})", + "}", }, - "return newg", + 5, + true, patch.RuntimeProcGoroutineCreatedPatch, ) return content, nil diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 6fbd88c3..f5da0648 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.24" -const REVISION = "26299095b0e070cff5e5daa2d6e59cea598d1b35+1" -const NUMBER = 183 +const REVISION = "ccc82ab8c3514f4c1a71039dad9ee7c30109935c+1" +const NUMBER = 184 func getRevision() string { revSuffix := "" diff --git a/runtime/core/version.go b/runtime/core/version.go index 929e728f..7eedaeb7 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.24" -const REVISION = "26299095b0e070cff5e5daa2d6e59cea598d1b35+1" -const NUMBER = 183 +const REVISION = "ccc82ab8c3514f4c1a71039dad9ee7c30109935c+1" +const NUMBER = 184 // these fields will be filled by compiler const XGO_VERSION = ""