Skip to content

Commit

Permalink
fix:(encoder) not use stack address when OP_recurse
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Aug 25, 2023
1 parent a726a4a commit 210cb03
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 20 deletions.
22 changes: 18 additions & 4 deletions internal/encoder/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,24 +1011,38 @@ func (self *_Assembler) _asm_OP_drop(_ *_Instr) {
self.drop_state(_StateSize)
}

var (
_F_mallocgc = jit.Func(mallocgc)
)

func (self *_Assembler) valloc(vt reflect.Type, ret obj.Addr) {
self.Emit("MOVQ", jit.Imm(int64(vt.Size())), _AX) // MOVQ ${vt.Size()}, AX
self.Emit("MOVQ", jit.Type(vt), _BX) // MOVQ ${vt}, BX
self.Emit("MOVB", jit.Imm(1), _CX) // MOVB $1, CX
self.call_go(_F_mallocgc) // CALL_GO mallocgc
self.Emit("MOVQ", _AX, ret) // MOVQ AX, ${ret}
}

func (self *_Assembler) _asm_OP_drop_2(_ *_Instr) {
self.drop_state(_StateSize * 2) // DROP $(_StateSize * 2)
self.Emit("MOVOU", _X0, jit.Sib(_ST, _AX, 1, 56)) // MOVOU X0, 56(ST)(AX)
}

func (self *_Assembler) _asm_OP_recurse(p *_Instr) {
self.prep_buffer_AX() // MOVE {buf}, (SP)
vt, pv := p.vp()
self.Emit("MOVQ", jit.Type(vt), _BX) // MOVQ $(type(p.vt())), BX

/* check for indirection */
if !rt.UnpackType(vt).Indirect() {
self.Emit("MOVQ", _SP_p, _CX) // MOVQ SP.p, CX
} else {
self.Emit("MOVQ", _SP_p, _VAR_vp) // MOVQ SP.p, VAR.vp
self.Emit("LEAQ", _VAR_vp, _CX) // LEAQ VAR.vp, CX
self.valloc(ptrType, _CX)
self.Emit("MOVQ", _CX, _VAR_vp)
self.WritePtr(3, _SP_p, jit.Ptr(_CX, 0))
}

self.prep_buffer_AX() // MOVE {buf}, (SP)
self.Emit("MOVQ", jit.Type(vt), _BX) // MOVQ $(type(p.vt())), BX

/* call the encoder */
self.Emit("MOVQ" , _ST, _DI) // MOVQ ST, DI
self.Emit("MOVQ" , _ARG_fv, _SI) // MOVQ $fv, SI
Expand Down
30 changes: 23 additions & 7 deletions internal/encoder/assembler_stkabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,20 @@ const (
_StackLimit = _MaxStack * _StateSize
)

var (
_F_mallocgc = jit.Func(mallocgc)
)

func (self *_Assembler) valloc(vt reflect.Type, ret obj.Addr) {
self.Emit("MOVQ", jit.Imm(int64(vt.Size())), _AX) // MOVQ ${vt.Size()}, AX
self.Emit("MOVQ", _AX, jit.Ptr(_SP, 0)) // MOVQ AX, (SP)
self.Emit("MOVQ", jit.Type(vt), _AX) // MOVQ ${vt}, AX
self.Emit("MOVQ", _AX, jit.Ptr(_SP, 8)) // MOVQ AX, 8(SP)
self.Emit("MOVB", jit.Imm(1), jit.Ptr(_SP, 16)) // MOVB $1, 16(SP)
self.call_go(_F_mallocgc) // CALL_GO mallocgc
self.Emit("MOVQ", jit.Ptr(_SP, 24), ret) // MOVQ 24(SP), ${ret}
}

func (self *_Assembler) save_state() {
self.Emit("MOVQ", jit.Ptr(_ST, 0), _CX) // MOVQ (ST), CX
self.Emit("LEAQ", jit.Ptr(_CX, _StateSize), _R8) // LEAQ _StateSize(CX), R8
Expand Down Expand Up @@ -1008,21 +1022,23 @@ func (self *_Assembler) _asm_OP_drop_2(_ *_Instr) {
}

func (self *_Assembler) _asm_OP_recurse(p *_Instr) {
self.prep_buffer() // MOVE {buf}, (SP)
vt, pv := p.vp()
self.Emit("MOVQ", jit.Type(vt), _AX) // MOVQ $(type(p.vt())), AX
self.Emit("MOVQ", _AX, jit.Ptr(_SP, 8)) // MOVQ AX, 8(SP)

/* check for indirection */
if !rt.UnpackType(vt).Indirect() {
self.Emit("MOVQ", _SP_p, _AX) // MOVQ SP.p, AX
self.Emit("MOVQ", _SP_p, _CX)
} else {
self.Emit("MOVQ", _SP_p, _VAR_vp) // MOVQ SP.p, 48(SP)
self.Emit("LEAQ", _VAR_vp, _AX) // LEAQ 48(SP), AX
self.valloc(ptrType, _CX)
self.Emit("MOVQ", _CX, _VAR_vp)
self.WritePtr(3, _SP_p, jit.Ptr(_CX, 0))
}

self.prep_buffer() // MOVE {buf}, (SP)
self.Emit("MOVQ", jit.Type(vt), _AX) // MOVQ $(type(p.vt())), AX
self.Emit("MOVQ", _AX, jit.Ptr(_SP, 8)) // MOVQ AX, 8(SP)

/* call the encoder */
self.Emit("MOVQ" , _AX, jit.Ptr(_SP, 16)) // MOVQ AX, 16(SP)
self.Emit("MOVQ" , _CX, jit.Ptr(_SP, 16)) // MOVQ CX, 16(SP)
self.Emit("MOVQ" , _ST, jit.Ptr(_SP, 24)) // MOVQ ST, 24(SP)
self.Emit("MOVQ" , _ARG_fv, _AX) // MOVQ fv, AX
if pv {
Expand Down
12 changes: 6 additions & 6 deletions internal/encoder/mapiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package encoder

import (
"encoding"
"reflect"
"sync"
"unsafe"
`encoding`
`reflect`
`sync`
`unsafe`

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

type _MapPair struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/encoder/stubs_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
//go:linkname _subr__b64encode github.com/chenzhuoyu/base64x._subr__b64encode
var _subr__b64encode uintptr

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

//go:noescape
//go:linkname memmove runtime.memmove
//goland:noinspection GoUnusedParameter
Expand Down
4 changes: 4 additions & 0 deletions internal/encoder/stubs_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
//go:linkname _subr__b64encode github.com/chenzhuoyu/base64x._subr__b64encode
var _subr__b64encode uintptr

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

//go:noescape
//go:linkname memmove runtime.memmove
//goland:noinspection GoUnusedParameter
Expand Down
4 changes: 4 additions & 0 deletions internal/encoder/stubs_go120.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
//go:linkname _subr__b64encode github.com/chenzhuoyu/base64x._subr__b64encode
var _subr__b64encode uintptr

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

//go:noescape
//go:linkname memmove runtime.memmove
//goland:noinspection GoUnusedParameter
Expand Down
4 changes: 4 additions & 0 deletions internal/encoder/stubs_go121.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
//go:linkname _subr__b64encode github.com/chenzhuoyu/base64x._subr__b64encode
var _subr__b64encode uintptr

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

//go:noescape
//go:linkname memmove runtime.memmove
//goland:noinspection GoUnusedParameter
Expand Down
8 changes: 5 additions & 3 deletions internal/encoder/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package encoder

import (
`encoding`
`encoding/json`
`reflect`
"encoding"
"encoding/json"
"reflect"
"unsafe"
)

var (
byteType = reflect.TypeOf(byte(0))
ptrType = reflect.TypeOf(unsafe.Pointer(nil))
jsonNumberType = reflect.TypeOf(json.Number(""))
jsonUnsupportedValueType = reflect.TypeOf(new(json.UnsupportedValueError))
)
Expand Down

0 comments on commit 210cb03

Please sign in to comment.