From 5a7ade66e807ed5fad16145cb3a04f2b079f13a2 Mon Sep 17 00:00:00 2001 From: Kyle Xiao Date: Fri, 2 Aug 2024 14:17:40 +0800 Subject: [PATCH] feat(jit): go1.23 for the last supported version (#57) --- ..._amd64.go => pgen_gcwb_go121_123_amd64.go} | 4 +- ...ata_go118_122.go => funcdata_go118_123.go} | 4 +- internal/loader/funcdata_invalid.go | 8 +- internal/loader/moduledata_go123.go | 88 +++++++++++++++++++ internal/rt/stackmap_test.go | 42 --------- tests/debughook_amd64.go | 58 ------------ tests/go.mod | 10 +-- tests/go.sum | 15 ++-- 8 files changed, 111 insertions(+), 118 deletions(-) rename internal/atm/pgen/{pgen_gcwb_go121_121_amd64.go => pgen_gcwb_go121_123_amd64.go} (98%) rename internal/loader/{funcdata_go118_122.go => funcdata_go118_123.go} (98%) create mode 100644 internal/loader/moduledata_go123.go delete mode 100644 tests/debughook_amd64.go diff --git a/internal/atm/pgen/pgen_gcwb_go121_121_amd64.go b/internal/atm/pgen/pgen_gcwb_go121_123_amd64.go similarity index 98% rename from internal/atm/pgen/pgen_gcwb_go121_121_amd64.go rename to internal/atm/pgen/pgen_gcwb_go121_123_amd64.go index 332bd6e..367f1ca 100644 --- a/internal/atm/pgen/pgen_gcwb_go121_121_amd64.go +++ b/internal/atm/pgen/pgen_gcwb_go121_123_amd64.go @@ -1,5 +1,5 @@ -//go:build go1.21 && !go1.23 -// +build go1.21,!go1.23 +//go:build go1.21 && !go1.24 +// +build go1.21,!go1.24 /* * Copyright 2022 CloudWeGo Authors diff --git a/internal/loader/funcdata_go118_122.go b/internal/loader/funcdata_go118_123.go similarity index 98% rename from internal/loader/funcdata_go118_122.go rename to internal/loader/funcdata_go118_123.go index 60adfbc..a03daa6 100644 --- a/internal/loader/funcdata_go118_122.go +++ b/internal/loader/funcdata_go118_123.go @@ -1,5 +1,5 @@ -//go:build go1.18 && !go1.23 -// +build go1.18,!go1.23 +//go:build go1.18 && !go1.24 +// +build go1.18,!go1.24 /* * Copyright 2022 CloudWeGo Authors diff --git a/internal/loader/funcdata_invalid.go b/internal/loader/funcdata_invalid.go index c0b0e28..eb575e9 100644 --- a/internal/loader/funcdata_invalid.go +++ b/internal/loader/funcdata_invalid.go @@ -1,5 +1,5 @@ -//go:build !go1.16 || go1.23 -// +build !go1.16 go1.23 +//go:build !go1.16 || go1.24 +// +build !go1.16 go1.24 /* * Copyright 2022 CloudWeGo Authors @@ -25,9 +25,9 @@ import ( // triggers a compilation error const ( - _ = panic("Unsupported Go version. Supported versions are 1.17 ~ 1.22") + _ = panic("Unsupported Go version. Supported versions are 1.17 ~ 1.23") ) func registerFunction(_ string, _ uintptr, _ uintptr, _ rt.Frame) { - panic("Unsupported Go version. Supported versions are 1.17 ~ 1.22") + panic("Unsupported Go version. Supported versions are 1.17 ~ 1.23") } diff --git a/internal/loader/moduledata_go123.go b/internal/loader/moduledata_go123.go new file mode 100644 index 0000000..94cb28e --- /dev/null +++ b/internal/loader/moduledata_go123.go @@ -0,0 +1,88 @@ +//go:build go1.23 +// +build go1.23 + +/* + * Copyright 2024 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package loader + +import ( + "unsafe" + + "github.com/cloudwego/frugal/internal/rt" +) + +type _Func struct { + entryOff uint32 + nameoff int32 + args int32 + deferreturn uint32 + pcsp uint32 + pcfile uint32 + pcln uint32 + npcdata uint32 + cuOffset uint32 + startLine int32 + funcID uint8 + flag uint8 + _ [1]byte + nfuncdata uint8 + pcdata [2]uint32 + argptrs uint32 + localptrs uint32 +} + +type _ModuleData struct { + pcHeader *_PCHeader + funcnametab []byte + cutab []uint32 + filetab []byte + pctab []byte + pclntable []byte + ftab []_FuncTab + findfunctab uintptr + minpc, maxpc uintptr + text, etext uintptr + noptrdata, enoptrdata uintptr + data, edata uintptr + bss, ebss uintptr + noptrbss, enoptrbss uintptr + covctrs, ecovctrs uintptr + end, gcdata, gcbss uintptr + types, etypes uintptr + rodata uintptr + gofunc uintptr + textsectmap [][3]uintptr + typelinks []int32 + itablinks []*rt.GoItab + ptab [][2]int32 + pluginpath string + pkghashes []struct{} + // This slice records the initializing tasks that need to be + // done to start up the program. It is built by the linker. + inittasks []unsafe.Pointer + modulename string + modulehashes []struct{} + hasmain uint8 + bad bool + gcdatamask, gcbssmask _BitVector + typemap map[int32]*rt.GoType + next *_ModuleData +} + +const ( + _ModuleMagic = 0xfffffff1 +) diff --git a/internal/rt/stackmap_test.go b/internal/rt/stackmap_test.go index f7bf4a5..9563a3b 100644 --- a/internal/rt/stackmap_test.go +++ b/internal/rt/stackmap_test.go @@ -17,9 +17,6 @@ package rt import ( - "fmt" - "reflect" - "testing" "unsafe" ) @@ -48,37 +45,6 @@ func (bv *bitvector) ptrbit(i uintptr) uint8 { return (b >> (i % 8)) & 1 } -//go:linkname findfunc runtime.findfunc -func findfunc(_ uintptr) funcInfo - -//go:linkname funcdata runtime.funcdata -func funcdata(_ funcInfo, _ uint8) unsafe.Pointer - -//go:linkname stackmapdata runtime.stackmapdata -func stackmapdata(_ *StackMap, _ int32) bitvector - -func stackMap(f interface{}) (*StackMap, *StackMap) { - fv := reflect.ValueOf(f) - if fv.Kind() != reflect.Func { - panic("f must be reflect.Func kind!") - } - fi := findfunc(fv.Pointer()) - args := funcdata(fi, uint8(_FUNCDATA_ArgsPointerMaps)) - locals := funcdata(fi, uint8(_FUNCDATA_LocalsPointerMaps)) - return (*StackMap)(args), (*StackMap)(locals) -} - -func dumpstackmap(m *StackMap) { - for i := int32(0); i < m.N; i++ { - fmt.Printf("bitmap #%d/%d: ", i, m.L) - bv := stackmapdata(m, i) - for j := int32(0); j < bv.n; j++ { - fmt.Printf("%d ", bv.ptrbit(uintptr(j))) - } - fmt.Printf("\n") - } -} - var keepalive struct { s string i int @@ -98,11 +64,3 @@ func stackmaptestfunc(s string, i int, vp unsafe.Pointer, sb interface{}, fv uin keepalive.fv = fv return } - -func TestStackMap_Dump(t *testing.T) { - args, locals := stackMap(stackmaptestfunc) - println("--- args ---") - dumpstackmap(args) - println("--- locals ---") - dumpstackmap(locals) -} diff --git a/tests/debughook_amd64.go b/tests/debughook_amd64.go deleted file mode 100644 index f67c64c..0000000 --- a/tests/debughook_amd64.go +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2022 CloudWeGo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package tests - -import ( - "os" - "syscall" - "unsafe" - - "github.com/cloudwego/frugal/internal/rt" -) - -//go:nosplit -//go:linkname rt_exit runtime.exit -func rt_exit(int) - -//go:nosplit -func rt_exit_hook(r int) { - if r != 0 { - println("Non-zero exit code:", r) - println("Now it's time to attach debugger, PID:", os.Getpid()) - for { - _ = true - } - } -} - -func mprotectpage(addr unsafe.Pointer, prot uintptr) { - if _, _, err := syscall.RawSyscall(syscall.SYS_MPROTECT, uintptr(addr)&^4095, 4096, prot); err != 0 { - panic(err) - } -} - -func init() { - if os.Getenv("FRUGAL_DEBUGGER_HOOK") == "yes" { - fp := rt.FuncAddr(rt_exit) - to := rt.FuncAddr(rt_exit_hook) - mprotectpage(fp, syscall.PROT_READ|syscall.PROT_WRITE) - *(*[2]byte)(fp) = [2]byte{0x48, 0xba} - *(*uintptr)(unsafe.Pointer(uintptr(fp) + 2)) = uintptr(to) - *(*[2]byte)(unsafe.Pointer(uintptr(fp) + 10)) = [2]byte{0xff, 0xe2} - mprotectpage(fp, syscall.PROT_READ|syscall.PROT_EXEC) - } -} diff --git a/tests/go.mod b/tests/go.mod index 88c3d62..8e5211a 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -13,8 +13,8 @@ require ( require ( github.com/bytedance/gopkg v0.0.0-20240711085056-a03554c296f8 // indirect - github.com/bytedance/sonic v1.11.8 // indirect - github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/bytedance/sonic v1.12.0 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/gopkg v0.1.0 // indirect github.com/cloudwego/iasm v0.2.0 // indirect @@ -22,13 +22,13 @@ require ( github.com/cloudwego/thriftgo v0.3.6 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect - github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/oleiade/lane v1.0.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - golang.org/x/arch v0.2.0 // indirect - golang.org/x/sys v0.13.0 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/sys v0.22.0 // indirect google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/tests/go.sum b/tests/go.sum index 482ebd7..82eadf0 100644 --- a/tests/go.sum +++ b/tests/go.sum @@ -9,10 +9,12 @@ github.com/bytedance/gopkg v0.0.0-20240711085056-a03554c296f8 h1:rDwLxYTMoKHaw4c github.com/bytedance/gopkg v0.0.0-20240711085056-a03554c296f8/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= github.com/bytedance/mockey v1.2.7/go.mod h1:bNrUnI1u7+pAc0TYDgPATM+wF2yzHxmNH+iDXg4AOCU= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA= github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls= +github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY= github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= @@ -89,8 +91,9 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= +github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -138,8 +141,9 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.2.0 h1:W1sUEHXiJTfjaFJ5SLo0N6lZn+0eO5gWD1MFeTGqQEY= golang.org/x/arch v0.2.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -205,8 +209,9 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=