Skip to content

Commit

Permalink
support go1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Feb 7, 2023
1 parent 66e27a5 commit d6c27bd
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 62 deletions.
3 changes: 2 additions & 1 deletion decoder/assembler_amd64_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ var (
type _Assembler struct {
jit.BaseAssembler
p _Program
name string
}

func newAssembler(p _Program) *_Assembler {
Expand All @@ -224,7 +225,7 @@ func newAssembler(p _Program) *_Assembler {
/** Assembler Interface **/

func (self *_Assembler) Load() _Decoder {
return ptodec(self.BaseAssembler.LoadWithFaker("json_decoder", _FP_size, _FP_args, _Decoder_Shadow))
return ptodec(self.BaseAssembler.LoadWithFaker("decode_"+self.name, _FP_size, _FP_args, _Decoder_Shadow))
}

func (self *_Assembler) Init(p _Program) *_Assembler {
Expand Down
2 changes: 1 addition & 1 deletion decoder/assembler_amd64_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ var (
)

func (self *_Assembler) Load() _Decoder {
return ptodec(self.BaseAssembler.Load(self.name+".Decoder", _FP_size, _FP_args, argPtrs, localPtrs))
return ptodec(self.BaseAssembler.Load("decode_"+self.name, _FP_size, _FP_args, argPtrs, localPtrs))
}

func (self *_Assembler) Init(p _Program) *_Assembler {
Expand Down
2 changes: 1 addition & 1 deletion decoder/generic_amd64_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var (

func (self *_ValueDecoder) build() uintptr {
self.Init(self.compile)
return *(*uintptr)(self.Load("generic_decoder", _VD_size, _VD_args, argPtrs1, localPtrs1))
return *(*uintptr)(self.Load("decode_value", _VD_size, _VD_args, argPtrs1, localPtrs1))
}

/** Function Calling Helpers **/
Expand Down
112 changes: 112 additions & 0 deletions decoder/stubs_go115.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//go:build go1.15,!go1.20
// +build go1.15,!go1.20

/*
* Copyright 2021 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 decoder

import (
`unsafe`
`reflect`

_ `github.com/chenzhuoyu/base64x`

`github.com/bytedance/sonic/internal/rt`
)

//go:linkname _subr__b64decode github.com/chenzhuoyu/base64x._subr__b64decode
var _subr__b64decode uintptr

// runtime.maxElementSize
const _max_map_element_size uintptr = 128

func mapfast(vt reflect.Type) bool {
return vt.Elem().Size() <= _max_map_element_size
}

//go:nosplit
//go:linkname throw runtime.throw
//goland:noinspection GoUnusedParameter
func throw(s string)

//go:linkname convT64 runtime.convT64
//goland:noinspection GoUnusedParameter
func convT64(v uint64) unsafe.Pointer

//go:linkname convTslice runtime.convTslice
//goland:noinspection GoUnusedParameter
func convTslice(v []byte) unsafe.Pointer

//go:linkname convTstring runtime.convTstring
//goland:noinspection GoUnusedParameter
func convTstring(v string) unsafe.Pointer

//go:noescape
//go:linkname memequal runtime.memequal
//goland:noinspection GoUnusedParameter
func memequal(a unsafe.Pointer, b unsafe.Pointer, size uintptr) bool

//go:noescape
//go:linkname memmove runtime.memmove
//goland:noinspection GoUnusedParameter
func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)

//go:linkname mallocgc runtime.mallocgc
//goland:noinspection GoUnusedParameter
func mallocgc(size uintptr, typ *rt.GoType, needzero bool) unsafe.Pointer

//go:linkname makeslice runtime.makeslice
//goland:noinspection GoUnusedParameter
func makeslice(et *rt.GoType, len int, cap int) unsafe.Pointer

//go:noescape
//go:linkname growslice runtime.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:linkname makemap_small runtime.makemap_small
func makemap_small() unsafe.Pointer

//go:linkname mapassign runtime.mapassign
//goland:noinspection GoUnusedParameter
func mapassign(t *rt.GoType, h unsafe.Pointer, k unsafe.Pointer) unsafe.Pointer

//go:linkname mapassign_fast32 runtime.mapassign_fast32
//goland:noinspection GoUnusedParameter
func mapassign_fast32(t *rt.GoType, h unsafe.Pointer, k uint32) unsafe.Pointer

//go:linkname mapassign_fast64 runtime.mapassign_fast64
//goland:noinspection GoUnusedParameter
func mapassign_fast64(t *rt.GoType, h unsafe.Pointer, k uint64) unsafe.Pointer

//go:linkname mapassign_fast64ptr runtime.mapassign_fast64ptr
//goland:noinspection GoUnusedParameter
func mapassign_fast64ptr(t *rt.GoType, h unsafe.Pointer, k unsafe.Pointer) unsafe.Pointer

//go:linkname mapassign_faststr runtime.mapassign_faststr
//goland:noinspection GoUnusedParameter
func mapassign_faststr(t *rt.GoType, h unsafe.Pointer, s string) unsafe.Pointer

//go:nosplit
//go:linkname memclrHasPointers runtime.memclrHasPointers
//goland:noinspection GoUnusedParameter
func memclrHasPointers(ptr unsafe.Pointer, n uintptr)

//go:noescape
//go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointers
//goland:noinspection GoUnusedParameter
func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)
2 changes: 1 addition & 1 deletion encoder/assembler_amd64_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func newAssembler(p _Program) *_Assembler {
/** Assembler Interface **/

func (self *_Assembler) Load() _Encoder {
return ptoenc(self.BaseAssembler.Load(self.name+".Encoder", _FP_size, _FP_args, argStackmap, localStackmap))
return ptoenc(self.BaseAssembler.LoadWithFaker("encode_"+self.name, _FP_size, _FP_args, _Encoder_Shadow))
}

func (self *_Assembler) Init(p _Program) *_Assembler {
Expand Down
2 changes: 1 addition & 1 deletion encoder/assembler_amd64_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func newAssembler(p _Program) *_Assembler {

/** Assembler Interface **/
func (self *_Assembler) Load() _Encoder {
return ptoenc(self.BaseAssembler.Load(self.name+".Encoder", _FP_size, _FP_args, argStackmap, localStackmap))
return ptoenc(self.BaseAssembler.Load("encode_"+self.name, _FP_size, _FP_args, argStackmap, localStackmap))
}

func (self *_Assembler) Init(p _Program) *_Assembler {
Expand Down
7 changes: 6 additions & 1 deletion internal/jit/assembler_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,19 @@ func (self *BaseAssembler) Init(f func()) {

func (self *BaseAssembler) Load(name string, frameSize int, argSize int, argStackmap []bool, localStackmap []bool) loader.Function {
self.build()
l := loader.Loader{
l := loader.ModuleLoader{
Name: "sonic.jit",
File: "github.com/bytedance/sonic/jit.go",
}
l.NoPreempt = true
return l.LoadFunc(self.c, name, frameSize, argSize, argStackmap, localStackmap)
}

func (self *BaseAssembler) LoadWithFaker(name string, frameSize int, argSize int, faker interface{}) loader.Function {
self.build()
return loader.Loader(self.c).LoadWithFaker(name, frameSize, argSize, faker)
}

/** Assembler Stages **/

func (self *BaseAssembler) init() {
Expand Down
22 changes: 12 additions & 10 deletions internal/loader/funcdata_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
`unsafe`
)

type _Func struct {
type _func struct {
entry uintptr // start pc
nameoff int32 // function name
args int32 // in/out args size
Expand All @@ -39,7 +39,7 @@ type _Func struct {
localptrs uintptr
}

type _FuncTab struct {
type _funcTab struct {
entry uintptr
funcoff uintptr
}
Expand Down Expand Up @@ -74,14 +74,14 @@ type _TextSection struct {
baseaddr uintptr // relocated section address
}

type _ModuleData struct {
type moduledata struct {
pcHeader *_PCHeader
funcnametab []byte
cutab []uint32
filetab []byte
pctab []byte
pclntable []_Func
ftab []_FuncTab
pclntable []_func
ftab []_funcTab
findfunctab *_FindFuncBucket
minpc, maxpc uintptr
text, etext uintptr
Expand All @@ -103,7 +103,7 @@ type _ModuleData struct {
gcdatamask, gcbssmask _BitVector
typemap map[int32]unsafe.Pointer
bad bool
next *_ModuleData
next *moduledata
}

type _FindFuncBucket struct {
Expand Down Expand Up @@ -131,7 +131,7 @@ func registerFunction(name string, pc uintptr, textSize uintptr, fp int, args in
maxpc := pc + size

/* function entry */
lnt := []_Func {{
lnt := []_func {{
entry : pc,
nameoff : 1,
args : int32(args),
Expand All @@ -142,14 +142,14 @@ func registerFunction(name string, pc uintptr, textSize uintptr, fp int, args in
}}

/* function table */
tab := []_FuncTab {
tab := []_funcTab {
{entry: pc},
{entry: pc},
{entry: maxpc},
}

/* module data */
mod := &_ModuleData {
mod := &moduledata {
pcHeader : modHeader,
funcnametab : append(append([]byte{0}, name...), 0),
pctab : append(makePCtab(fp), encodeVariant(int(size))...),
Expand All @@ -166,4 +166,6 @@ func registerFunction(name string, pc uintptr, textSize uintptr, fp int, args in
/* verify and register the new module */
moduledataverify1(mod)
registerModule(mod)
}
}

func makeModuledata(name string, filenames []string, funcs []Func, text []byte) (mod *moduledata)
88 changes: 43 additions & 45 deletions internal/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
package loader

import (
"fmt"
"os"
"syscall"
"unsafe"

"github.com/bytedance/sonic/internal/rt"
)

const (
Expand All @@ -30,8 +34,28 @@ const (
_RW = syscall.PROT_READ | syscall.PROT_WRITE
)

type Loader []byte

func (self Loader) LoadWithFaker(fn string, fp int, args int, faker interface{}) (f Function) {
p := os.Getpagesize()
n := int(rnd(int64(len(self)), int64(p)))

/* register the function */
m := mmap(n)
v := fmt.Sprintf("runtime.__%s_%x", fn, m)
argsptr, localsptr := GetStackMap(faker)
registerFunction(v, m, uintptr(n), fp, args, uintptr(len(self)), argsptr, localsptr)

/* reference as a slice */
s := rt.BytesFrom(unsafe.Pointer(m), len(self), n)

/* copy the machine code, and make it executable */
copy(s, self)
mprotect(m, n)
return Function(&m)
}

type Loader struct {
type ModuleLoader struct {
Name string
File string
Options
Expand All @@ -43,51 +67,8 @@ type Options struct {

type Function unsafe.Pointer

// func (self Loader) LoadWithFaker(fn string, fp int, args int, faker interface{}) (f Function) {
// p := os.Getpagesize()
// n := int(rnd(int64(len(self)), int64(p)))

// /* register the function */
// m := mmap(n)
// v := fmt.Sprintf("runtime.__%s_%x", fn, m)
// argsptr, localsptr := GetStackMap(faker)
// registerFunction(v, m, uintptr(n), fp, args, uintptr(len(self)), argsptr, localsptr)

// /* reference as a slice */
// s := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader {
// Data : m,
// Cap : n,
// Len : len(self),
// }))

// /* copy the machine code, and make it executable */
// copy(s, self)
// mprotect(m, n)
// return Function(&m)
// }

// func (self Loader) Load(fn string, fp int, args int) (f Function) {
// return self.LoadWithFaker(fn, fp, args, func(){})
// }

func Load(modulename string, filenames []string, funcs []Func, text []byte) (out []Function) {
// generate module data and allocate memory address
mod := makeModuledata(modulename, filenames, funcs, text)

// verify and register the new module
moduledataverify1(mod)
registerModule(mod)

// encapsulate function address
out = make([]Function, len(funcs))
for i, f := range funcs {
m := uintptr(mod.text + uintptr(f.EntryOff))
out[i] = Function(&m)
}
return
}

func (self Loader) LoadFunc(text []byte, funcName string, frameSize int, argSize int, argStackmap []bool, localStackmap []bool) Function {
func (self ModuleLoader) LoadFunc(text []byte, funcName string, frameSize int, argSize int, argStackmap []bool, localStackmap []bool) Function {
size := uint32(len(text))

fn := Func{
Expand Down Expand Up @@ -132,4 +113,21 @@ func (self Loader) LoadFunc(text []byte, funcName string, frameSize int, argSize

out := Load(self.Name + funcName, []string{self.File}, []Func{fn}, text)
return out[0]
}

func Load(modulename string, filenames []string, funcs []Func, text []byte) (out []Function) {
// generate module data and allocate memory address
mod := makeModuledata(modulename, filenames, funcs, text)

// verify and register the new module
moduledataverify1(mod)
registerModule(mod)

// encapsulate function address
out = make([]Function, len(funcs))
for i, f := range funcs {
m := uintptr(mod.text + uintptr(f.EntryOff))
out[i] = Function(&m)
}
return
}
2 changes: 1 addition & 1 deletion internal/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoader_LoadFunc(t *testing.T) {
0xc3, // RET
}
v0 := 0
l := Loader{
l := ModuleLoader{
Name: "test",
File: "test.go",
}
Expand Down

0 comments on commit d6c27bd

Please sign in to comment.